re PR c++/37875 ([c++0x] misinterpreted closing angle bracket in decltype operand)
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 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 "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40 #include "plugin.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
50
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
59 };
60
61 /* A C++ token. */
62
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
69 /* Token flags. */
70 unsigned char flags;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
80 location_t location;
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
103 it is created. */
104
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
110 buffer. */
111 size_t buffer_length;
112
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
116
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
120
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
124 tokens. */
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
129
130 /* True if we should output debugging information. */
131 bool debugging_p;
132
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
135 bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
142 a lexer. */
143
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
147
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes. */
153
154 static cp_lexer *cp_lexer_new_main
155 (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159 (cp_lexer *);
160 static int cp_lexer_saving_tokens
161 (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163 (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169 (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179 (cp_lexer *);
180 static void cp_lexer_purge_token
181 (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185 (cp_lexer *);
186 static void cp_lexer_commit_tokens
187 (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189 (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194 (cp_lexer *);
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213 (cp_token *);
214
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables. */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255 preprocessor. */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260 cp_token first_token;
261 cp_lexer *lexer;
262 cp_token *pos;
263 size_t alloc;
264 size_t space;
265 cp_token *buffer;
266
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
271
272 c_common_no_more_pch ();
273
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
283
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
287
288 /* Put the first token in the buffer. */
289 space = alloc;
290 pos = buffer;
291 *pos = first_token;
292
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
295 {
296 pos++;
297 if (!--space)
298 {
299 space = alloc;
300 alloc *= 2;
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
303 }
304 cp_lexer_get_preprocessor_token (lexer, pos);
305 }
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 done_lexing = true;
314
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
316 return lexer;
317 }
318
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
321
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 {
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
328
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
334
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
337
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
341 #endif
342
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
344 return lexer;
345 }
346
347 /* Frees all resources associated with LEXER. */
348
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
351 {
352 if (lexer->buffer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
355 ggc_free (lexer);
356 }
357
358 /* Returns nonzero if debugging information should be output. */
359
360 #ifdef ENABLE_CHECKING
361
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
364 {
365 return lexer->debugging_p;
366 }
367
368 #endif /* ENABLE_CHECKING */
369
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 {
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
374
375 return lexer->next_token - previous_p;
376 }
377
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 {
381 return pos;
382 }
383
384 /* nonzero if we are presently saving tokens. */
385
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 {
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
390 }
391
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
396
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 {
400 static int is_extern_c = 0;
401
402 /* Get a new token from the preprocessor. */
403 token->type
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
408
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
415
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
418 {
419 if (C_IS_RESERVED_WORD (token->u.value))
420 {
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
425 }
426 else
427 {
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
431 {
432 /* Warn about the C++0x keyword (but still treat it as
433 an identifier). */
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
436 token->u.value);
437
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
441 }
442
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
445 }
446 }
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
449 {
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
452 {
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
462 }
463 }
464 else if (token->type == CPP_PRAGMA)
465 {
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
470 }
471 }
472
473 /* Update the globals input_location and the input file stack from TOKEN. */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
476 {
477 if (token->type != CPP_EOF)
478 {
479 input_location = token->location;
480 }
481 }
482
483 /* Return a pointer to the next token in the token stream, but do not
484 consume it. */
485
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
488 {
489 if (cp_lexer_debugging_p (lexer))
490 {
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
494 }
495 return lexer->next_token;
496 }
497
498 /* Return true if the next token has the indicated TYPE. */
499
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
502 {
503 return cp_lexer_peek_token (lexer)->type == type;
504 }
505
506 /* Return true if the next token does not have the indicated TYPE. */
507
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
510 {
511 return !cp_lexer_next_token_is (lexer, type);
512 }
513
514 /* Return true if the next token is the indicated KEYWORD. */
515
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
518 {
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
520 }
521
522 /* Return true if the next token is not the indicated KEYWORD. */
523
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
526 {
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
528 }
529
530 /* Return true if the next token is a keyword for a decl-specifier. */
531
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 {
535 cp_token *token;
536
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
539 {
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
542 case RID_AUTO:
543 /* Storage classes. */
544 case RID_REGISTER:
545 case RID_STATIC:
546 case RID_EXTERN:
547 case RID_MUTABLE:
548 case RID_THREAD:
549 /* Elaborated type specifiers. */
550 case RID_ENUM:
551 case RID_CLASS:
552 case RID_STRUCT:
553 case RID_UNION:
554 case RID_TYPENAME:
555 /* Simple type specifiers. */
556 case RID_CHAR:
557 case RID_CHAR16:
558 case RID_CHAR32:
559 case RID_WCHAR:
560 case RID_BOOL:
561 case RID_SHORT:
562 case RID_INT:
563 case RID_LONG:
564 case RID_SIGNED:
565 case RID_UNSIGNED:
566 case RID_FLOAT:
567 case RID_DOUBLE:
568 case RID_VOID:
569 /* GNU extensions. */
570 case RID_ATTRIBUTE:
571 case RID_TYPEOF:
572 /* C++0x extensions. */
573 case RID_DECLTYPE:
574 return true;
575
576 default:
577 return false;
578 }
579 }
580
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590 cp_token *token;
591
592 /* N is 1-based, not zero-based. */
593 gcc_assert (n > 0);
594
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599 --n;
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
602 while (n != 0)
603 {
604 ++token;
605 if (token == lexer->last_token)
606 {
607 token = &eof_token;
608 break;
609 }
610
611 if (token->type != CPP_PURGED)
612 --n;
613 }
614
615 if (cp_lexer_debugging_p (lexer))
616 {
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
619 }
620
621 return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630 cp_token *token = lexer->next_token;
631
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635 do
636 {
637 lexer->next_token++;
638 if (lexer->next_token == lexer->last_token)
639 {
640 lexer->next_token = &eof_token;
641 break;
642 }
643
644 }
645 while (lexer->next_token->type == CPP_PURGED);
646
647 cp_lexer_set_source_position_from_token (token);
648
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
651 {
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
655 }
656
657 return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
662 token. */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667 cp_token *tok = lexer->next_token;
668
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
674
675 do
676 {
677 tok++;
678 if (tok == lexer->last_token)
679 {
680 tok = &eof_token;
681 break;
682 }
683 }
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695 cp_token *peek = lexer->next_token;
696
697 if (peek == &eof_token)
698 peek = lexer->last_token;
699
700 gcc_assert (tok < peek);
701
702 for ( tok += 1; tok != peek; tok += 1)
703 {
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
708 }
709 }
710
711 /* Begin saving tokens. All tokens consumed after this point will be
712 preserved. */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved. */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734 VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM. */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763 TTYPE_TABLE
764 #undef OP
765 #undef TK
766 /* C++ parser token types - see "Manifest constants", above. */
767 "KEYWORD",
768 "TEMPLATE_ID",
769 "NESTED_NAME_SPECIFIER",
770 "PURGED"
771 };
772
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
777
778 /* For some tokens, print the associated data. */
779 switch (token->type)
780 {
781 case CPP_KEYWORD:
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785 break;
786 /* else fall through */
787 case CPP_NAME:
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
789 break;
790
791 case CPP_STRING:
792 case CPP_STRING16:
793 case CPP_STRING32:
794 case CPP_WSTRING:
795 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796 break;
797
798 default:
799 break;
800 }
801 }
802
803 /* Start emitting debugging information. */
804
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
807 {
808 lexer->debugging_p = true;
809 }
810
811 /* Stop emitting debugging information. */
812
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
815 {
816 lexer->debugging_p = false;
817 }
818
819 #endif /* ENABLE_CHECKING */
820
821 /* Create a new cp_token_cache, representing a range of tokens. */
822
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
825 {
826 cp_token_cache *cache = GGC_NEW (cp_token_cache);
827 cache->first = first;
828 cache->last = last;
829 return cache;
830 }
831
832 \f
833 /* Decl-specifiers. */
834
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
836
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
839 {
840 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 }
842
843 /* Declarators. */
844
845 /* Nothing other than the parser should be creating declarators;
846 declarators are a semi-syntactic representation of C++ entities.
847 Other parts of the front end that need to create entities (like
848 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
849
850 static cp_declarator *make_call_declarator
851 (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853 (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855 (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857 (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859 (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861 (cp_cv_quals, tree, cp_declarator *);
862
863 /* An erroneous declarator. */
864 static cp_declarator *cp_error_declarator;
865
866 /* The obstack on which declarators and related data structures are
867 allocated. */
868 static struct obstack declarator_obstack;
869
870 /* Alloc BYTES from the declarator memory pool. */
871
872 static inline void *
873 alloc_declarator (size_t bytes)
874 {
875 return obstack_alloc (&declarator_obstack, bytes);
876 }
877
878 /* Allocate a declarator of the indicated KIND. Clear fields that are
879 common to all declarators. */
880
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
883 {
884 cp_declarator *declarator;
885
886 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887 declarator->kind = kind;
888 declarator->attributes = NULL_TREE;
889 declarator->declarator = NULL;
890 declarator->parameter_pack_p = false;
891
892 return declarator;
893 }
894
895 /* Make a declarator for a generalized identifier. If
896 QUALIFYING_SCOPE is non-NULL, the identifier is
897 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898 UNQUALIFIED_NAME. SFK indicates the kind of special function this
899 is, if any. */
900
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903 special_function_kind sfk)
904 {
905 cp_declarator *declarator;
906
907 /* It is valid to write:
908
909 class C { void f(); };
910 typedef C D;
911 void D::f();
912
913 The standard is not clear about whether `typedef const C D' is
914 legal; as of 2002-09-15 the committee is considering that
915 question. EDG 3.0 allows that syntax. Therefore, we do as
916 well. */
917 if (qualifying_scope && TYPE_P (qualifying_scope))
918 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
919
920 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
923
924 declarator = make_declarator (cdk_id);
925 declarator->u.id.qualifying_scope = qualifying_scope;
926 declarator->u.id.unqualified_name = unqualified_name;
927 declarator->u.id.sfk = sfk;
928
929 return declarator;
930 }
931
932 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
933 of modifiers such as const or volatile to apply to the pointer
934 type, represented as identifiers. */
935
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 {
939 cp_declarator *declarator;
940
941 declarator = make_declarator (cdk_pointer);
942 declarator->declarator = target;
943 declarator->u.pointer.qualifiers = cv_qualifiers;
944 declarator->u.pointer.class_type = NULL_TREE;
945 if (target)
946 {
947 declarator->parameter_pack_p = target->parameter_pack_p;
948 target->parameter_pack_p = false;
949 }
950 else
951 declarator->parameter_pack_p = false;
952
953 return declarator;
954 }
955
956 /* Like make_pointer_declarator -- but for references. */
957
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960 bool rvalue_ref)
961 {
962 cp_declarator *declarator;
963
964 declarator = make_declarator (cdk_reference);
965 declarator->declarator = target;
966 declarator->u.reference.qualifiers = cv_qualifiers;
967 declarator->u.reference.rvalue_ref = rvalue_ref;
968 if (target)
969 {
970 declarator->parameter_pack_p = target->parameter_pack_p;
971 target->parameter_pack_p = false;
972 }
973 else
974 declarator->parameter_pack_p = false;
975
976 return declarator;
977 }
978
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980 member of CLASS_TYPE. */
981
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984 cp_declarator *pointee)
985 {
986 cp_declarator *declarator;
987
988 declarator = make_declarator (cdk_ptrmem);
989 declarator->declarator = pointee;
990 declarator->u.pointer.qualifiers = cv_qualifiers;
991 declarator->u.pointer.class_type = class_type;
992
993 if (pointee)
994 {
995 declarator->parameter_pack_p = pointee->parameter_pack_p;
996 pointee->parameter_pack_p = false;
997 }
998 else
999 declarator->parameter_pack_p = false;
1000
1001 return declarator;
1002 }
1003
1004 /* Make a declarator for the function given by TARGET, with the
1005 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1006 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1007 indicates what exceptions can be thrown. */
1008
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011 tree parms,
1012 cp_cv_quals cv_qualifiers,
1013 tree exception_specification,
1014 tree late_return_type)
1015 {
1016 cp_declarator *declarator;
1017
1018 declarator = make_declarator (cdk_function);
1019 declarator->declarator = target;
1020 declarator->u.function.parameters = parms;
1021 declarator->u.function.qualifiers = cv_qualifiers;
1022 declarator->u.function.exception_specification = exception_specification;
1023 declarator->u.function.late_return_type = late_return_type;
1024 if (target)
1025 {
1026 declarator->parameter_pack_p = target->parameter_pack_p;
1027 target->parameter_pack_p = false;
1028 }
1029 else
1030 declarator->parameter_pack_p = false;
1031
1032 return declarator;
1033 }
1034
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036 defined by ELEMENT. */
1037
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1040 {
1041 cp_declarator *declarator;
1042
1043 declarator = make_declarator (cdk_array);
1044 declarator->declarator = element;
1045 declarator->u.array.bounds = bounds;
1046 if (element)
1047 {
1048 declarator->parameter_pack_p = element->parameter_pack_p;
1049 element->parameter_pack_p = false;
1050 }
1051 else
1052 declarator->parameter_pack_p = false;
1053
1054 return declarator;
1055 }
1056
1057 /* Determine whether the declarator we've seen so far can be a
1058 parameter pack, when followed by an ellipsis. */
1059 static bool
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1061 {
1062 /* Search for a declarator name, or any other declarator that goes
1063 after the point where the ellipsis could appear in a parameter
1064 pack. If we find any of these, then this declarator can not be
1065 made into a parameter pack. */
1066 bool found = false;
1067 while (declarator && !found)
1068 {
1069 switch ((int)declarator->kind)
1070 {
1071 case cdk_id:
1072 case cdk_array:
1073 found = true;
1074 break;
1075
1076 case cdk_error:
1077 return true;
1078
1079 default:
1080 declarator = declarator->declarator;
1081 break;
1082 }
1083 }
1084
1085 return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1097 {
1098 cp_parameter_declarator *parameter;
1099
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1105 else
1106 clear_decl_specs (&parameter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1110
1111 return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119 while (declarator)
1120 {
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1123 return true;
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1126 return false;
1127 declarator = declarator->declarator;
1128 }
1129 return false;
1130 }
1131
1132 /* The parser. */
1133
1134 /* Overview
1135 --------
1136
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1144
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1147 code processed.
1148
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1157
1158 Methodology
1159 -----------
1160
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1177
1178 Future Improvements
1179 -------------------
1180
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1185 case. */
1186
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1189
1190 enum
1191 {
1192 /* No flags. */
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 };
1200
1201 /* This type is used for parameters and variables which hold
1202 combinations of the above flags. */
1203 typedef int cp_parser_flags;
1204
1205 /* The different kinds of declarators we want to parse. */
1206
1207 typedef enum cp_parser_declarator_kind
1208 {
1209 /* We want an abstract declarator. */
1210 CP_PARSER_DECLARATOR_ABSTRACT,
1211 /* We want a named declarator. */
1212 CP_PARSER_DECLARATOR_NAMED,
1213 /* We don't mind, but the name must be an unqualified-id. */
1214 CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1216
1217 /* The precedence values used to parse binary expressions. The minimum value
1218 of PREC must be 1, because zero is reserved to quickly discriminate
1219 binary operators from other tokens. */
1220
1221 enum cp_parser_prec
1222 {
1223 PREC_NOT_OPERATOR,
1224 PREC_LOGICAL_OR_EXPRESSION,
1225 PREC_LOGICAL_AND_EXPRESSION,
1226 PREC_INCLUSIVE_OR_EXPRESSION,
1227 PREC_EXCLUSIVE_OR_EXPRESSION,
1228 PREC_AND_EXPRESSION,
1229 PREC_EQUALITY_EXPRESSION,
1230 PREC_RELATIONAL_EXPRESSION,
1231 PREC_SHIFT_EXPRESSION,
1232 PREC_ADDITIVE_EXPRESSION,
1233 PREC_MULTIPLICATIVE_EXPRESSION,
1234 PREC_PM_EXPRESSION,
1235 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 };
1237
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239 precedence value. */
1240
1241 typedef struct cp_parser_binary_operations_map_node
1242 {
1243 /* The token type. */
1244 enum cpp_ttype token_type;
1245 /* The corresponding tree code. */
1246 enum tree_code tree_type;
1247 /* The precedence of this operator. */
1248 enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1250
1251 /* The status of a tentative parse. */
1252
1253 typedef enum cp_parser_status_kind
1254 {
1255 /* No errors have occurred. */
1256 CP_PARSER_STATUS_KIND_NO_ERROR,
1257 /* An error has occurred. */
1258 CP_PARSER_STATUS_KIND_ERROR,
1259 /* We are committed to this tentative parse, whether or not an error
1260 has occurred. */
1261 CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1263
1264 typedef struct cp_parser_expression_stack_entry
1265 {
1266 /* Left hand side of the binary operation we are currently
1267 parsing. */
1268 tree lhs;
1269 /* Original tree code for left hand side, if it was a binary
1270 expression itself (used for -Wparentheses). */
1271 enum tree_code lhs_type;
1272 /* Tree code for the binary operation we are parsing. */
1273 enum tree_code tree_type;
1274 /* Precedence of the binary operation we are parsing. */
1275 enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1277
1278 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1279 entries because precedence levels on the stack are monotonically
1280 increasing. */
1281 typedef struct cp_parser_expression_stack_entry
1282 cp_parser_expression_stack[NUM_PREC_VALUES];
1283
1284 /* Context that is saved and restored when parsing tentatively. */
1285 typedef struct GTY (()) cp_parser_context {
1286 /* If this is a tentative parsing context, the status of the
1287 tentative parse. */
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1293 tree object_type;
1294
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes. */
1300
1301 /* Constructors and destructors. */
1302
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1305
1306 /* Class variables. */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1312 cp_parser_new. */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1349 for speed. */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors. */
1353
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360 cp_parser_context *context;
1361
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1364 {
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1369 }
1370 else
1371 context = GGC_CNEW (cp_parser_context);
1372
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1377 if (next)
1378 {
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1384 }
1385
1386 return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser. */
1390
1391 typedef struct GTY(()) cp_parser {
1392 /* The lexer from which we are obtaining tokens. */
1393 cp_lexer *lexer;
1394
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look. It can
1399 also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401 This value is not cleared automatically after a name is looked
1402 up, so we must be careful to clear it before starting a new look
1403 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1404 will look up `Z' in the scope of `X', rather than the current
1405 scope.) Unfortunately, it is difficult to tell when name lookup
1406 is complete, because we sometimes peek at a token, look it up,
1407 and then decide not to consume it. */
1408 tree scope;
1409
1410 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411 last lookup took place. OBJECT_SCOPE is used if an expression
1412 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413 respectively. QUALIFYING_SCOPE is used for an expression of the
1414 form "X::Y"; it refers to X. */
1415 tree object_scope;
1416 tree qualifying_scope;
1417
1418 /* A stack of parsing contexts. All but the bottom entry on the
1419 stack will be tentative contexts.
1420
1421 We parse tentatively in order to determine which construct is in
1422 use in some situations. For example, in order to determine
1423 whether a statement is an expression-statement or a
1424 declaration-statement we parse it tentatively as a
1425 declaration-statement. If that fails, we then reparse the same
1426 token stream as an expression-statement. */
1427 cp_parser_context *context;
1428
1429 /* True if we are parsing GNU C++. If this flag is not set, then
1430 GNU extensions are not recognized. */
1431 bool allow_gnu_extensions_p;
1432
1433 /* TRUE if the `>' token should be interpreted as the greater-than
1434 operator. FALSE if it is the end of a template-id or
1435 template-parameter-list. In C++0x mode, this flag also applies to
1436 `>>' tokens, which are viewed as two consecutive `>' tokens when
1437 this flag is FALSE. */
1438 bool greater_than_is_operator_p;
1439
1440 /* TRUE if default arguments are allowed within a parameter list
1441 that starts at this point. FALSE if only a gnu extension makes
1442 them permissible. */
1443 bool default_arg_ok_p;
1444
1445 /* TRUE if we are parsing an integral constant-expression. See
1446 [expr.const] for a precise definition. */
1447 bool integral_constant_expression_p;
1448
1449 /* TRUE if we are parsing an integral constant-expression -- but a
1450 non-constant expression should be permitted as well. This flag
1451 is used when parsing an array bound so that GNU variable-length
1452 arrays are tolerated. */
1453 bool allow_non_integral_constant_expression_p;
1454
1455 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456 been seen that makes the expression non-constant. */
1457 bool non_integral_constant_expression_p;
1458
1459 /* TRUE if local variable names and `this' are forbidden in the
1460 current context. */
1461 bool local_variables_forbidden_p;
1462
1463 /* TRUE if the declaration we are parsing is part of a
1464 linkage-specification of the form `extern string-literal
1465 declaration'. */
1466 bool in_unbraced_linkage_specification_p;
1467
1468 /* TRUE if we are presently parsing a declarator, after the
1469 direct-declarator. */
1470 bool in_declarator_p;
1471
1472 /* TRUE if we are presently parsing a template-argument-list. */
1473 bool in_template_argument_list_p;
1474
1475 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476 to IN_OMP_BLOCK if parsing OpenMP structured block and
1477 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1478 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479 iteration-statement, OpenMP block or loop within that switch. */
1480 #define IN_SWITCH_STMT 1
1481 #define IN_ITERATION_STMT 2
1482 #define IN_OMP_BLOCK 4
1483 #define IN_OMP_FOR 8
1484 #define IN_IF_STMT 16
1485 unsigned char in_statement;
1486
1487 /* TRUE if we are presently parsing the body of a switch statement.
1488 Note that this doesn't quite overlap with in_statement above.
1489 The difference relates to giving the right sets of error messages:
1490 "case not in switch" vs "break statement used with OpenMP...". */
1491 bool in_switch_statement_p;
1492
1493 /* TRUE if we are parsing a type-id in an expression context. In
1494 such a situation, both "type (expr)" and "type (type)" are valid
1495 alternatives. */
1496 bool in_type_id_in_expr_p;
1497
1498 /* TRUE if we are currently in a header file where declarations are
1499 implicitly extern "C". */
1500 bool implicit_extern_c;
1501
1502 /* TRUE if strings in expressions should be translated to the execution
1503 character set. */
1504 bool translate_strings_p;
1505
1506 /* TRUE if we are presently parsing the body of a function, but not
1507 a local class. */
1508 bool in_function_body;
1509
1510 /* If non-NULL, then we are parsing a construct where new type
1511 definitions are not permitted. The string stored here will be
1512 issued as an error message if a type is defined. */
1513 const char *type_definition_forbidden_message;
1514
1515 /* A list of lists. The outer list is a stack, used for member
1516 functions of local classes. At each level there are two sub-list,
1517 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519 TREE_VALUE's. The functions are chained in reverse declaration
1520 order.
1521
1522 The TREE_PURPOSE sublist contains those functions with default
1523 arguments that need post processing, and the TREE_VALUE sublist
1524 contains those functions with definitions that need post
1525 processing.
1526
1527 These lists can only be processed once the outermost class being
1528 defined is complete. */
1529 tree unparsed_functions_queues;
1530
1531 /* The number of classes whose definitions are currently in
1532 progress. */
1533 unsigned num_classes_being_defined;
1534
1535 /* The number of template parameter lists that apply directly to the
1536 current declaration. */
1537 unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes. */
1541
1542 /* Constructors and destructors. */
1543
1544 static cp_parser *cp_parser_new
1545 (void);
1546
1547 /* Routines to parse various constructs.
1548
1549 Those that return `tree' will return the error_mark_node (rather
1550 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551 Sometimes, they will return an ordinary node if error-recovery was
1552 attempted, even though a parse error occurred. So, to check
1553 whether or not a parse error occurred, you should always use
1554 cp_parser_error_occurred. If the construct is optional (indicated
1555 either by an `_opt' in the name of the function that does the
1556 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557 the construct is not present. */
1558
1559 /* Lexical conventions [gram.lex] */
1560
1561 static tree cp_parser_identifier
1562 (cp_parser *);
1563 static tree cp_parser_string_literal
1564 (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic] */
1567
1568 static bool cp_parser_translation_unit
1569 (cp_parser *);
1570
1571 /* Expressions [gram.expr] */
1572
1573 static tree cp_parser_primary_expression
1574 (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576 (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584 (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586 (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588 (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594 (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596 (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1598 (cp_token *);
1599 static tree cp_parser_new_expression
1600 (cp_parser *);
1601 static VEC(tree,gc) *cp_parser_new_placement
1602 (cp_parser *);
1603 static tree cp_parser_new_type_id
1604 (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606 (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608 (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_initializer
1610 (cp_parser *);
1611 static tree cp_parser_delete_expression
1612 (cp_parser *);
1613 static tree cp_parser_cast_expression
1614 (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618 (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620 (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622 (cp_parser *);
1623 static tree cp_parser_expression
1624 (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626 (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628 (cp_parser *);
1629 static tree cp_parser_lambda_expression
1630 (cp_parser *);
1631 static void cp_parser_lambda_introducer
1632 (cp_parser *, tree);
1633 static void cp_parser_lambda_declarator_opt
1634 (cp_parser *, tree);
1635 static void cp_parser_lambda_body
1636 (cp_parser *, tree);
1637
1638 /* Statements [gram.stmt.stmt] */
1639
1640 static void cp_parser_statement
1641 (cp_parser *, tree, bool, bool *);
1642 static void cp_parser_label_for_labeled_statement
1643 (cp_parser *);
1644 static tree cp_parser_expression_statement
1645 (cp_parser *, tree);
1646 static tree cp_parser_compound_statement
1647 (cp_parser *, tree, bool);
1648 static void cp_parser_statement_seq_opt
1649 (cp_parser *, tree);
1650 static tree cp_parser_selection_statement
1651 (cp_parser *, bool *);
1652 static tree cp_parser_condition
1653 (cp_parser *);
1654 static tree cp_parser_iteration_statement
1655 (cp_parser *);
1656 static void cp_parser_for_init_statement
1657 (cp_parser *);
1658 static tree cp_parser_jump_statement
1659 (cp_parser *);
1660 static void cp_parser_declaration_statement
1661 (cp_parser *);
1662
1663 static tree cp_parser_implicitly_scoped_statement
1664 (cp_parser *, bool *);
1665 static void cp_parser_already_scoped_statement
1666 (cp_parser *);
1667
1668 /* Declarations [gram.dcl.dcl] */
1669
1670 static void cp_parser_declaration_seq_opt
1671 (cp_parser *);
1672 static void cp_parser_declaration
1673 (cp_parser *);
1674 static void cp_parser_block_declaration
1675 (cp_parser *, bool);
1676 static void cp_parser_simple_declaration
1677 (cp_parser *, bool);
1678 static void cp_parser_decl_specifier_seq
1679 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1680 static tree cp_parser_storage_class_specifier_opt
1681 (cp_parser *);
1682 static tree cp_parser_function_specifier_opt
1683 (cp_parser *, cp_decl_specifier_seq *);
1684 static tree cp_parser_type_specifier
1685 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1686 int *, bool *);
1687 static tree cp_parser_simple_type_specifier
1688 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1689 static tree cp_parser_type_name
1690 (cp_parser *);
1691 static tree cp_parser_nonclass_name
1692 (cp_parser* parser);
1693 static tree cp_parser_elaborated_type_specifier
1694 (cp_parser *, bool, bool);
1695 static tree cp_parser_enum_specifier
1696 (cp_parser *);
1697 static void cp_parser_enumerator_list
1698 (cp_parser *, tree);
1699 static void cp_parser_enumerator_definition
1700 (cp_parser *, tree);
1701 static tree cp_parser_namespace_name
1702 (cp_parser *);
1703 static void cp_parser_namespace_definition
1704 (cp_parser *);
1705 static void cp_parser_namespace_body
1706 (cp_parser *);
1707 static tree cp_parser_qualified_namespace_specifier
1708 (cp_parser *);
1709 static void cp_parser_namespace_alias_definition
1710 (cp_parser *);
1711 static bool cp_parser_using_declaration
1712 (cp_parser *, bool);
1713 static void cp_parser_using_directive
1714 (cp_parser *);
1715 static void cp_parser_asm_definition
1716 (cp_parser *);
1717 static void cp_parser_linkage_specification
1718 (cp_parser *);
1719 static void cp_parser_static_assert
1720 (cp_parser *, bool);
1721 static tree cp_parser_decltype
1722 (cp_parser *);
1723
1724 /* Declarators [gram.dcl.decl] */
1725
1726 static tree cp_parser_init_declarator
1727 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1728 static cp_declarator *cp_parser_declarator
1729 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1730 static cp_declarator *cp_parser_direct_declarator
1731 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1732 static enum tree_code cp_parser_ptr_operator
1733 (cp_parser *, tree *, cp_cv_quals *);
1734 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1735 (cp_parser *);
1736 static tree cp_parser_late_return_type_opt
1737 (cp_parser *);
1738 static tree cp_parser_declarator_id
1739 (cp_parser *, bool);
1740 static tree cp_parser_type_id
1741 (cp_parser *);
1742 static tree cp_parser_template_type_arg
1743 (cp_parser *);
1744 static tree cp_parser_type_id_1
1745 (cp_parser *, bool);
1746 static void cp_parser_type_specifier_seq
1747 (cp_parser *, bool, cp_decl_specifier_seq *);
1748 static tree cp_parser_parameter_declaration_clause
1749 (cp_parser *);
1750 static tree cp_parser_parameter_declaration_list
1751 (cp_parser *, bool *);
1752 static cp_parameter_declarator *cp_parser_parameter_declaration
1753 (cp_parser *, bool, bool *);
1754 static tree cp_parser_default_argument
1755 (cp_parser *, bool);
1756 static void cp_parser_function_body
1757 (cp_parser *);
1758 static tree cp_parser_initializer
1759 (cp_parser *, bool *, bool *);
1760 static tree cp_parser_initializer_clause
1761 (cp_parser *, bool *);
1762 static tree cp_parser_braced_list
1763 (cp_parser*, bool*);
1764 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1765 (cp_parser *, bool *);
1766
1767 static bool cp_parser_ctor_initializer_opt_and_function_body
1768 (cp_parser *);
1769
1770 /* Classes [gram.class] */
1771
1772 static tree cp_parser_class_name
1773 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1774 static tree cp_parser_class_specifier
1775 (cp_parser *);
1776 static tree cp_parser_class_head
1777 (cp_parser *, bool *, tree *, tree *);
1778 static enum tag_types cp_parser_class_key
1779 (cp_parser *);
1780 static void cp_parser_member_specification_opt
1781 (cp_parser *);
1782 static void cp_parser_member_declaration
1783 (cp_parser *);
1784 static tree cp_parser_pure_specifier
1785 (cp_parser *);
1786 static tree cp_parser_constant_initializer
1787 (cp_parser *);
1788
1789 /* Derived classes [gram.class.derived] */
1790
1791 static tree cp_parser_base_clause
1792 (cp_parser *);
1793 static tree cp_parser_base_specifier
1794 (cp_parser *);
1795
1796 /* Special member functions [gram.special] */
1797
1798 static tree cp_parser_conversion_function_id
1799 (cp_parser *);
1800 static tree cp_parser_conversion_type_id
1801 (cp_parser *);
1802 static cp_declarator *cp_parser_conversion_declarator_opt
1803 (cp_parser *);
1804 static bool cp_parser_ctor_initializer_opt
1805 (cp_parser *);
1806 static void cp_parser_mem_initializer_list
1807 (cp_parser *);
1808 static tree cp_parser_mem_initializer
1809 (cp_parser *);
1810 static tree cp_parser_mem_initializer_id
1811 (cp_parser *);
1812
1813 /* Overloading [gram.over] */
1814
1815 static tree cp_parser_operator_function_id
1816 (cp_parser *);
1817 static tree cp_parser_operator
1818 (cp_parser *);
1819
1820 /* Templates [gram.temp] */
1821
1822 static void cp_parser_template_declaration
1823 (cp_parser *, bool);
1824 static tree cp_parser_template_parameter_list
1825 (cp_parser *);
1826 static tree cp_parser_template_parameter
1827 (cp_parser *, bool *, bool *);
1828 static tree cp_parser_type_parameter
1829 (cp_parser *, bool *);
1830 static tree cp_parser_template_id
1831 (cp_parser *, bool, bool, bool);
1832 static tree cp_parser_template_name
1833 (cp_parser *, bool, bool, bool, bool *);
1834 static tree cp_parser_template_argument_list
1835 (cp_parser *);
1836 static tree cp_parser_template_argument
1837 (cp_parser *);
1838 static void cp_parser_explicit_instantiation
1839 (cp_parser *);
1840 static void cp_parser_explicit_specialization
1841 (cp_parser *);
1842
1843 /* Exception handling [gram.exception] */
1844
1845 static tree cp_parser_try_block
1846 (cp_parser *);
1847 static bool cp_parser_function_try_block
1848 (cp_parser *);
1849 static void cp_parser_handler_seq
1850 (cp_parser *);
1851 static void cp_parser_handler
1852 (cp_parser *);
1853 static tree cp_parser_exception_declaration
1854 (cp_parser *);
1855 static tree cp_parser_throw_expression
1856 (cp_parser *);
1857 static tree cp_parser_exception_specification_opt
1858 (cp_parser *);
1859 static tree cp_parser_type_id_list
1860 (cp_parser *);
1861
1862 /* GNU Extensions */
1863
1864 static tree cp_parser_asm_specification_opt
1865 (cp_parser *);
1866 static tree cp_parser_asm_operand_list
1867 (cp_parser *);
1868 static tree cp_parser_asm_clobber_list
1869 (cp_parser *);
1870 static tree cp_parser_asm_label_list
1871 (cp_parser *);
1872 static tree cp_parser_attributes_opt
1873 (cp_parser *);
1874 static tree cp_parser_attribute_list
1875 (cp_parser *);
1876 static bool cp_parser_extension_opt
1877 (cp_parser *, int *);
1878 static void cp_parser_label_declaration
1879 (cp_parser *);
1880
1881 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1882 static bool cp_parser_pragma
1883 (cp_parser *, enum pragma_context);
1884
1885 /* Objective-C++ Productions */
1886
1887 static tree cp_parser_objc_message_receiver
1888 (cp_parser *);
1889 static tree cp_parser_objc_message_args
1890 (cp_parser *);
1891 static tree cp_parser_objc_message_expression
1892 (cp_parser *);
1893 static tree cp_parser_objc_encode_expression
1894 (cp_parser *);
1895 static tree cp_parser_objc_defs_expression
1896 (cp_parser *);
1897 static tree cp_parser_objc_protocol_expression
1898 (cp_parser *);
1899 static tree cp_parser_objc_selector_expression
1900 (cp_parser *);
1901 static tree cp_parser_objc_expression
1902 (cp_parser *);
1903 static bool cp_parser_objc_selector_p
1904 (enum cpp_ttype);
1905 static tree cp_parser_objc_selector
1906 (cp_parser *);
1907 static tree cp_parser_objc_protocol_refs_opt
1908 (cp_parser *);
1909 static void cp_parser_objc_declaration
1910 (cp_parser *);
1911 static tree cp_parser_objc_statement
1912 (cp_parser *);
1913
1914 /* Utility Routines */
1915
1916 static tree cp_parser_lookup_name
1917 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1918 static tree cp_parser_lookup_name_simple
1919 (cp_parser *, tree, location_t);
1920 static tree cp_parser_maybe_treat_template_as_class
1921 (tree, bool);
1922 static bool cp_parser_check_declarator_template_parameters
1923 (cp_parser *, cp_declarator *, location_t);
1924 static bool cp_parser_check_template_parameters
1925 (cp_parser *, unsigned, location_t, cp_declarator *);
1926 static tree cp_parser_simple_cast_expression
1927 (cp_parser *);
1928 static tree cp_parser_global_scope_opt
1929 (cp_parser *, bool);
1930 static bool cp_parser_constructor_declarator_p
1931 (cp_parser *, bool);
1932 static tree cp_parser_function_definition_from_specifiers_and_declarator
1933 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1934 static tree cp_parser_function_definition_after_declarator
1935 (cp_parser *, bool);
1936 static void cp_parser_template_declaration_after_export
1937 (cp_parser *, bool);
1938 static void cp_parser_perform_template_parameter_access_checks
1939 (VEC (deferred_access_check,gc)*);
1940 static tree cp_parser_single_declaration
1941 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1942 static tree cp_parser_functional_cast
1943 (cp_parser *, tree);
1944 static tree cp_parser_save_member_function_body
1945 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1946 static tree cp_parser_enclosed_template_argument_list
1947 (cp_parser *);
1948 static void cp_parser_save_default_args
1949 (cp_parser *, tree);
1950 static void cp_parser_late_parsing_for_member
1951 (cp_parser *, tree);
1952 static void cp_parser_late_parsing_default_args
1953 (cp_parser *, tree);
1954 static tree cp_parser_sizeof_operand
1955 (cp_parser *, enum rid);
1956 static tree cp_parser_trait_expr
1957 (cp_parser *, enum rid);
1958 static bool cp_parser_declares_only_class_p
1959 (cp_parser *);
1960 static void cp_parser_set_storage_class
1961 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1962 static void cp_parser_set_decl_spec_type
1963 (cp_decl_specifier_seq *, tree, location_t, bool);
1964 static bool cp_parser_friend_p
1965 (const cp_decl_specifier_seq *);
1966 static cp_token *cp_parser_require
1967 (cp_parser *, enum cpp_ttype, const char *);
1968 static cp_token *cp_parser_require_keyword
1969 (cp_parser *, enum rid, const char *);
1970 static bool cp_parser_token_starts_function_definition_p
1971 (cp_token *);
1972 static bool cp_parser_next_token_starts_class_definition_p
1973 (cp_parser *);
1974 static bool cp_parser_next_token_ends_template_argument_p
1975 (cp_parser *);
1976 static bool cp_parser_nth_token_starts_template_argument_list_p
1977 (cp_parser *, size_t);
1978 static enum tag_types cp_parser_token_is_class_key
1979 (cp_token *);
1980 static void cp_parser_check_class_key
1981 (enum tag_types, tree type);
1982 static void cp_parser_check_access_in_redeclaration
1983 (tree type, location_t location);
1984 static bool cp_parser_optional_template_keyword
1985 (cp_parser *);
1986 static void cp_parser_pre_parsed_nested_name_specifier
1987 (cp_parser *);
1988 static bool cp_parser_cache_group
1989 (cp_parser *, enum cpp_ttype, unsigned);
1990 static void cp_parser_parse_tentatively
1991 (cp_parser *);
1992 static void cp_parser_commit_to_tentative_parse
1993 (cp_parser *);
1994 static void cp_parser_abort_tentative_parse
1995 (cp_parser *);
1996 static bool cp_parser_parse_definitely
1997 (cp_parser *);
1998 static inline bool cp_parser_parsing_tentatively
1999 (cp_parser *);
2000 static bool cp_parser_uncommitted_to_tentative_parse_p
2001 (cp_parser *);
2002 static void cp_parser_error
2003 (cp_parser *, const char *);
2004 static void cp_parser_name_lookup_error
2005 (cp_parser *, tree, tree, const char *, location_t);
2006 static bool cp_parser_simulate_error
2007 (cp_parser *);
2008 static bool cp_parser_check_type_definition
2009 (cp_parser *);
2010 static void cp_parser_check_for_definition_in_return_type
2011 (cp_declarator *, tree, location_t type_location);
2012 static void cp_parser_check_for_invalid_template_id
2013 (cp_parser *, tree, location_t location);
2014 static bool cp_parser_non_integral_constant_expression
2015 (cp_parser *, const char *);
2016 static void cp_parser_diagnose_invalid_type_name
2017 (cp_parser *, tree, tree, location_t);
2018 static bool cp_parser_parse_and_diagnose_invalid_type_name
2019 (cp_parser *);
2020 static int cp_parser_skip_to_closing_parenthesis
2021 (cp_parser *, bool, bool, bool);
2022 static void cp_parser_skip_to_end_of_statement
2023 (cp_parser *);
2024 static void cp_parser_consume_semicolon_at_end_of_statement
2025 (cp_parser *);
2026 static void cp_parser_skip_to_end_of_block_or_statement
2027 (cp_parser *);
2028 static bool cp_parser_skip_to_closing_brace
2029 (cp_parser *);
2030 static void cp_parser_skip_to_end_of_template_parameter_list
2031 (cp_parser *);
2032 static void cp_parser_skip_to_pragma_eol
2033 (cp_parser*, cp_token *);
2034 static bool cp_parser_error_occurred
2035 (cp_parser *);
2036 static bool cp_parser_allow_gnu_extensions_p
2037 (cp_parser *);
2038 static bool cp_parser_is_string_literal
2039 (cp_token *);
2040 static bool cp_parser_is_keyword
2041 (cp_token *, enum rid);
2042 static tree cp_parser_make_typename_type
2043 (cp_parser *, tree, tree, location_t location);
2044 static cp_declarator * cp_parser_make_indirect_declarator
2045 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2046
2047 /* Returns nonzero if we are parsing tentatively. */
2048
2049 static inline bool
2050 cp_parser_parsing_tentatively (cp_parser* parser)
2051 {
2052 return parser->context->next != NULL;
2053 }
2054
2055 /* Returns nonzero if TOKEN is a string literal. */
2056
2057 static bool
2058 cp_parser_is_string_literal (cp_token* token)
2059 {
2060 return (token->type == CPP_STRING ||
2061 token->type == CPP_STRING16 ||
2062 token->type == CPP_STRING32 ||
2063 token->type == CPP_WSTRING);
2064 }
2065
2066 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2067
2068 static bool
2069 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2070 {
2071 return token->keyword == keyword;
2072 }
2073
2074 /* If not parsing tentatively, issue a diagnostic of the form
2075 FILE:LINE: MESSAGE before TOKEN
2076 where TOKEN is the next token in the input stream. MESSAGE
2077 (specified by the caller) is usually of the form "expected
2078 OTHER-TOKEN". */
2079
2080 static void
2081 cp_parser_error (cp_parser* parser, const char* message)
2082 {
2083 if (!cp_parser_simulate_error (parser))
2084 {
2085 cp_token *token = cp_lexer_peek_token (parser->lexer);
2086 /* This diagnostic makes more sense if it is tagged to the line
2087 of the token we just peeked at. */
2088 cp_lexer_set_source_position_from_token (token);
2089
2090 if (token->type == CPP_PRAGMA)
2091 {
2092 error_at (token->location,
2093 "%<#pragma%> is not allowed here");
2094 cp_parser_skip_to_pragma_eol (parser, token);
2095 return;
2096 }
2097
2098 c_parse_error (message,
2099 /* Because c_parser_error does not understand
2100 CPP_KEYWORD, keywords are treated like
2101 identifiers. */
2102 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2103 token->u.value, token->flags);
2104 }
2105 }
2106
2107 /* Issue an error about name-lookup failing. NAME is the
2108 IDENTIFIER_NODE DECL is the result of
2109 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2110 the thing that we hoped to find. */
2111
2112 static void
2113 cp_parser_name_lookup_error (cp_parser* parser,
2114 tree name,
2115 tree decl,
2116 const char* desired,
2117 location_t location)
2118 {
2119 /* If name lookup completely failed, tell the user that NAME was not
2120 declared. */
2121 if (decl == error_mark_node)
2122 {
2123 if (parser->scope && parser->scope != global_namespace)
2124 error_at (location, "%<%E::%E%> has not been declared",
2125 parser->scope, name);
2126 else if (parser->scope == global_namespace)
2127 error_at (location, "%<::%E%> has not been declared", name);
2128 else if (parser->object_scope
2129 && !CLASS_TYPE_P (parser->object_scope))
2130 error_at (location, "request for member %qE in non-class type %qT",
2131 name, parser->object_scope);
2132 else if (parser->object_scope)
2133 error_at (location, "%<%T::%E%> has not been declared",
2134 parser->object_scope, name);
2135 else
2136 error_at (location, "%qE has not been declared", name);
2137 }
2138 else if (parser->scope && parser->scope != global_namespace)
2139 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2140 else if (parser->scope == global_namespace)
2141 error_at (location, "%<::%E%> %s", name, desired);
2142 else
2143 error_at (location, "%qE %s", name, desired);
2144 }
2145
2146 /* If we are parsing tentatively, remember that an error has occurred
2147 during this tentative parse. Returns true if the error was
2148 simulated; false if a message should be issued by the caller. */
2149
2150 static bool
2151 cp_parser_simulate_error (cp_parser* parser)
2152 {
2153 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2154 {
2155 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2156 return true;
2157 }
2158 return false;
2159 }
2160
2161 /* Check for repeated decl-specifiers. */
2162
2163 static void
2164 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2165 location_t location)
2166 {
2167 int ds;
2168
2169 for (ds = ds_first; ds != ds_last; ++ds)
2170 {
2171 unsigned count = decl_specs->specs[ds];
2172 if (count < 2)
2173 continue;
2174 /* The "long" specifier is a special case because of "long long". */
2175 if (ds == ds_long)
2176 {
2177 if (count > 2)
2178 error_at (location, "%<long long long%> is too long for GCC");
2179 else
2180 pedwarn_cxx98 (location, OPT_Wlong_long,
2181 "ISO C++ 1998 does not support %<long long%>");
2182 }
2183 else if (count > 1)
2184 {
2185 static const char *const decl_spec_names[] = {
2186 "signed",
2187 "unsigned",
2188 "short",
2189 "long",
2190 "const",
2191 "volatile",
2192 "restrict",
2193 "inline",
2194 "virtual",
2195 "explicit",
2196 "friend",
2197 "typedef",
2198 "constexpr",
2199 "__complex",
2200 "__thread"
2201 };
2202 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2203 }
2204 }
2205 }
2206
2207 /* This function is called when a type is defined. If type
2208 definitions are forbidden at this point, an error message is
2209 issued. */
2210
2211 static bool
2212 cp_parser_check_type_definition (cp_parser* parser)
2213 {
2214 /* If types are forbidden here, issue a message. */
2215 if (parser->type_definition_forbidden_message)
2216 {
2217 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2218 in the message need to be interpreted. */
2219 error (parser->type_definition_forbidden_message);
2220 return false;
2221 }
2222 return true;
2223 }
2224
2225 /* This function is called when the DECLARATOR is processed. The TYPE
2226 was a type defined in the decl-specifiers. If it is invalid to
2227 define a type in the decl-specifiers for DECLARATOR, an error is
2228 issued. TYPE_LOCATION is the location of TYPE and is used
2229 for error reporting. */
2230
2231 static void
2232 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2233 tree type, location_t type_location)
2234 {
2235 /* [dcl.fct] forbids type definitions in return types.
2236 Unfortunately, it's not easy to know whether or not we are
2237 processing a return type until after the fact. */
2238 while (declarator
2239 && (declarator->kind == cdk_pointer
2240 || declarator->kind == cdk_reference
2241 || declarator->kind == cdk_ptrmem))
2242 declarator = declarator->declarator;
2243 if (declarator
2244 && declarator->kind == cdk_function)
2245 {
2246 error_at (type_location,
2247 "new types may not be defined in a return type");
2248 inform (type_location,
2249 "(perhaps a semicolon is missing after the definition of %qT)",
2250 type);
2251 }
2252 }
2253
2254 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2255 "<" in any valid C++ program. If the next token is indeed "<",
2256 issue a message warning the user about what appears to be an
2257 invalid attempt to form a template-id. LOCATION is the location
2258 of the type-specifier (TYPE) */
2259
2260 static void
2261 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2262 tree type, location_t location)
2263 {
2264 cp_token_position start = 0;
2265
2266 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2267 {
2268 if (TYPE_P (type))
2269 error_at (location, "%qT is not a template", type);
2270 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2271 error_at (location, "%qE is not a template", type);
2272 else
2273 error_at (location, "invalid template-id");
2274 /* Remember the location of the invalid "<". */
2275 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2276 start = cp_lexer_token_position (parser->lexer, true);
2277 /* Consume the "<". */
2278 cp_lexer_consume_token (parser->lexer);
2279 /* Parse the template arguments. */
2280 cp_parser_enclosed_template_argument_list (parser);
2281 /* Permanently remove the invalid template arguments so that
2282 this error message is not issued again. */
2283 if (start)
2284 cp_lexer_purge_tokens_after (parser->lexer, start);
2285 }
2286 }
2287
2288 /* If parsing an integral constant-expression, issue an error message
2289 about the fact that THING appeared and return true. Otherwise,
2290 return false. In either case, set
2291 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2292
2293 static bool
2294 cp_parser_non_integral_constant_expression (cp_parser *parser,
2295 const char *thing)
2296 {
2297 parser->non_integral_constant_expression_p = true;
2298 if (parser->integral_constant_expression_p)
2299 {
2300 if (!parser->allow_non_integral_constant_expression_p)
2301 {
2302 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2303 in the message need to be interpreted. */
2304 char *message = concat (thing,
2305 " cannot appear in a constant-expression",
2306 NULL);
2307 error (message);
2308 free (message);
2309 return true;
2310 }
2311 }
2312 return false;
2313 }
2314
2315 /* Emit a diagnostic for an invalid type name. SCOPE is the
2316 qualifying scope (or NULL, if none) for ID. This function commits
2317 to the current active tentative parse, if any. (Otherwise, the
2318 problematic construct might be encountered again later, resulting
2319 in duplicate error messages.) LOCATION is the location of ID. */
2320
2321 static void
2322 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2323 tree scope, tree id,
2324 location_t location)
2325 {
2326 tree decl, old_scope;
2327 /* Try to lookup the identifier. */
2328 old_scope = parser->scope;
2329 parser->scope = scope;
2330 decl = cp_parser_lookup_name_simple (parser, id, location);
2331 parser->scope = old_scope;
2332 /* If the lookup found a template-name, it means that the user forgot
2333 to specify an argument list. Emit a useful error message. */
2334 if (TREE_CODE (decl) == TEMPLATE_DECL)
2335 error_at (location,
2336 "invalid use of template-name %qE without an argument list",
2337 decl);
2338 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2339 error_at (location, "invalid use of destructor %qD as a type", id);
2340 else if (TREE_CODE (decl) == TYPE_DECL)
2341 /* Something like 'unsigned A a;' */
2342 error_at (location, "invalid combination of multiple type-specifiers");
2343 else if (!parser->scope)
2344 {
2345 /* Issue an error message. */
2346 error_at (location, "%qE does not name a type", id);
2347 /* If we're in a template class, it's possible that the user was
2348 referring to a type from a base class. For example:
2349
2350 template <typename T> struct A { typedef T X; };
2351 template <typename T> struct B : public A<T> { X x; };
2352
2353 The user should have said "typename A<T>::X". */
2354 if (processing_template_decl && current_class_type
2355 && TYPE_BINFO (current_class_type))
2356 {
2357 tree b;
2358
2359 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2360 b;
2361 b = TREE_CHAIN (b))
2362 {
2363 tree base_type = BINFO_TYPE (b);
2364 if (CLASS_TYPE_P (base_type)
2365 && dependent_type_p (base_type))
2366 {
2367 tree field;
2368 /* Go from a particular instantiation of the
2369 template (which will have an empty TYPE_FIELDs),
2370 to the main version. */
2371 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2372 for (field = TYPE_FIELDS (base_type);
2373 field;
2374 field = TREE_CHAIN (field))
2375 if (TREE_CODE (field) == TYPE_DECL
2376 && DECL_NAME (field) == id)
2377 {
2378 inform (location,
2379 "(perhaps %<typename %T::%E%> was intended)",
2380 BINFO_TYPE (b), id);
2381 break;
2382 }
2383 if (field)
2384 break;
2385 }
2386 }
2387 }
2388 }
2389 /* Here we diagnose qualified-ids where the scope is actually correct,
2390 but the identifier does not resolve to a valid type name. */
2391 else if (parser->scope != error_mark_node)
2392 {
2393 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2394 error_at (location, "%qE in namespace %qE does not name a type",
2395 id, parser->scope);
2396 else if (TYPE_P (parser->scope))
2397 error_at (location, "%qE in class %qT does not name a type",
2398 id, parser->scope);
2399 else
2400 gcc_unreachable ();
2401 }
2402 cp_parser_commit_to_tentative_parse (parser);
2403 }
2404
2405 /* Check for a common situation where a type-name should be present,
2406 but is not, and issue a sensible error message. Returns true if an
2407 invalid type-name was detected.
2408
2409 The situation handled by this function are variable declarations of the
2410 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2411 Usually, `ID' should name a type, but if we got here it means that it
2412 does not. We try to emit the best possible error message depending on
2413 how exactly the id-expression looks like. */
2414
2415 static bool
2416 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2417 {
2418 tree id;
2419 cp_token *token = cp_lexer_peek_token (parser->lexer);
2420
2421 cp_parser_parse_tentatively (parser);
2422 id = cp_parser_id_expression (parser,
2423 /*template_keyword_p=*/false,
2424 /*check_dependency_p=*/true,
2425 /*template_p=*/NULL,
2426 /*declarator_p=*/true,
2427 /*optional_p=*/false);
2428 /* After the id-expression, there should be a plain identifier,
2429 otherwise this is not a simple variable declaration. Also, if
2430 the scope is dependent, we cannot do much. */
2431 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2432 || (parser->scope && TYPE_P (parser->scope)
2433 && dependent_type_p (parser->scope))
2434 || TREE_CODE (id) == TYPE_DECL)
2435 {
2436 cp_parser_abort_tentative_parse (parser);
2437 return false;
2438 }
2439 if (!cp_parser_parse_definitely (parser))
2440 return false;
2441
2442 /* Emit a diagnostic for the invalid type. */
2443 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2444 id, token->location);
2445 /* Skip to the end of the declaration; there's no point in
2446 trying to process it. */
2447 cp_parser_skip_to_end_of_block_or_statement (parser);
2448 return true;
2449 }
2450
2451 /* Consume tokens up to, and including, the next non-nested closing `)'.
2452 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2453 are doing error recovery. Returns -1 if OR_COMMA is true and we
2454 found an unnested comma. */
2455
2456 static int
2457 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2458 bool recovering,
2459 bool or_comma,
2460 bool consume_paren)
2461 {
2462 unsigned paren_depth = 0;
2463 unsigned brace_depth = 0;
2464 unsigned square_depth = 0;
2465
2466 if (recovering && !or_comma
2467 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2468 return 0;
2469
2470 while (true)
2471 {
2472 cp_token * token = cp_lexer_peek_token (parser->lexer);
2473
2474 switch (token->type)
2475 {
2476 case CPP_EOF:
2477 case CPP_PRAGMA_EOL:
2478 /* If we've run out of tokens, then there is no closing `)'. */
2479 return 0;
2480
2481 /* This is good for lambda expression capture-lists. */
2482 case CPP_OPEN_SQUARE:
2483 ++square_depth;
2484 break;
2485 case CPP_CLOSE_SQUARE:
2486 if (!square_depth--)
2487 return 0;
2488 break;
2489
2490 case CPP_SEMICOLON:
2491 /* This matches the processing in skip_to_end_of_statement. */
2492 if (!brace_depth)
2493 return 0;
2494 break;
2495
2496 case CPP_OPEN_BRACE:
2497 ++brace_depth;
2498 break;
2499 case CPP_CLOSE_BRACE:
2500 if (!brace_depth--)
2501 return 0;
2502 break;
2503
2504 case CPP_COMMA:
2505 if (recovering && or_comma && !brace_depth && !paren_depth
2506 && !square_depth)
2507 return -1;
2508 break;
2509
2510 case CPP_OPEN_PAREN:
2511 if (!brace_depth)
2512 ++paren_depth;
2513 break;
2514
2515 case CPP_CLOSE_PAREN:
2516 if (!brace_depth && !paren_depth--)
2517 {
2518 if (consume_paren)
2519 cp_lexer_consume_token (parser->lexer);
2520 return 1;
2521 }
2522 break;
2523
2524 default:
2525 break;
2526 }
2527
2528 /* Consume the token. */
2529 cp_lexer_consume_token (parser->lexer);
2530 }
2531 }
2532
2533 /* Consume tokens until we reach the end of the current statement.
2534 Normally, that will be just before consuming a `;'. However, if a
2535 non-nested `}' comes first, then we stop before consuming that. */
2536
2537 static void
2538 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2539 {
2540 unsigned nesting_depth = 0;
2541
2542 while (true)
2543 {
2544 cp_token *token = cp_lexer_peek_token (parser->lexer);
2545
2546 switch (token->type)
2547 {
2548 case CPP_EOF:
2549 case CPP_PRAGMA_EOL:
2550 /* If we've run out of tokens, stop. */
2551 return;
2552
2553 case CPP_SEMICOLON:
2554 /* If the next token is a `;', we have reached the end of the
2555 statement. */
2556 if (!nesting_depth)
2557 return;
2558 break;
2559
2560 case CPP_CLOSE_BRACE:
2561 /* If this is a non-nested '}', stop before consuming it.
2562 That way, when confronted with something like:
2563
2564 { 3 + }
2565
2566 we stop before consuming the closing '}', even though we
2567 have not yet reached a `;'. */
2568 if (nesting_depth == 0)
2569 return;
2570
2571 /* If it is the closing '}' for a block that we have
2572 scanned, stop -- but only after consuming the token.
2573 That way given:
2574
2575 void f g () { ... }
2576 typedef int I;
2577
2578 we will stop after the body of the erroneously declared
2579 function, but before consuming the following `typedef'
2580 declaration. */
2581 if (--nesting_depth == 0)
2582 {
2583 cp_lexer_consume_token (parser->lexer);
2584 return;
2585 }
2586
2587 case CPP_OPEN_BRACE:
2588 ++nesting_depth;
2589 break;
2590
2591 default:
2592 break;
2593 }
2594
2595 /* Consume the token. */
2596 cp_lexer_consume_token (parser->lexer);
2597 }
2598 }
2599
2600 /* This function is called at the end of a statement or declaration.
2601 If the next token is a semicolon, it is consumed; otherwise, error
2602 recovery is attempted. */
2603
2604 static void
2605 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2606 {
2607 /* Look for the trailing `;'. */
2608 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2609 {
2610 /* If there is additional (erroneous) input, skip to the end of
2611 the statement. */
2612 cp_parser_skip_to_end_of_statement (parser);
2613 /* If the next token is now a `;', consume it. */
2614 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2615 cp_lexer_consume_token (parser->lexer);
2616 }
2617 }
2618
2619 /* Skip tokens until we have consumed an entire block, or until we
2620 have consumed a non-nested `;'. */
2621
2622 static void
2623 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2624 {
2625 int nesting_depth = 0;
2626
2627 while (nesting_depth >= 0)
2628 {
2629 cp_token *token = cp_lexer_peek_token (parser->lexer);
2630
2631 switch (token->type)
2632 {
2633 case CPP_EOF:
2634 case CPP_PRAGMA_EOL:
2635 /* If we've run out of tokens, stop. */
2636 return;
2637
2638 case CPP_SEMICOLON:
2639 /* Stop if this is an unnested ';'. */
2640 if (!nesting_depth)
2641 nesting_depth = -1;
2642 break;
2643
2644 case CPP_CLOSE_BRACE:
2645 /* Stop if this is an unnested '}', or closes the outermost
2646 nesting level. */
2647 nesting_depth--;
2648 if (nesting_depth < 0)
2649 return;
2650 if (!nesting_depth)
2651 nesting_depth = -1;
2652 break;
2653
2654 case CPP_OPEN_BRACE:
2655 /* Nest. */
2656 nesting_depth++;
2657 break;
2658
2659 default:
2660 break;
2661 }
2662
2663 /* Consume the token. */
2664 cp_lexer_consume_token (parser->lexer);
2665 }
2666 }
2667
2668 /* Skip tokens until a non-nested closing curly brace is the next
2669 token, or there are no more tokens. Return true in the first case,
2670 false otherwise. */
2671
2672 static bool
2673 cp_parser_skip_to_closing_brace (cp_parser *parser)
2674 {
2675 unsigned nesting_depth = 0;
2676
2677 while (true)
2678 {
2679 cp_token *token = cp_lexer_peek_token (parser->lexer);
2680
2681 switch (token->type)
2682 {
2683 case CPP_EOF:
2684 case CPP_PRAGMA_EOL:
2685 /* If we've run out of tokens, stop. */
2686 return false;
2687
2688 case CPP_CLOSE_BRACE:
2689 /* If the next token is a non-nested `}', then we have reached
2690 the end of the current block. */
2691 if (nesting_depth-- == 0)
2692 return true;
2693 break;
2694
2695 case CPP_OPEN_BRACE:
2696 /* If it the next token is a `{', then we are entering a new
2697 block. Consume the entire block. */
2698 ++nesting_depth;
2699 break;
2700
2701 default:
2702 break;
2703 }
2704
2705 /* Consume the token. */
2706 cp_lexer_consume_token (parser->lexer);
2707 }
2708 }
2709
2710 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2711 parameter is the PRAGMA token, allowing us to purge the entire pragma
2712 sequence. */
2713
2714 static void
2715 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2716 {
2717 cp_token *token;
2718
2719 parser->lexer->in_pragma = false;
2720
2721 do
2722 token = cp_lexer_consume_token (parser->lexer);
2723 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2724
2725 /* Ensure that the pragma is not parsed again. */
2726 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2727 }
2728
2729 /* Require pragma end of line, resyncing with it as necessary. The
2730 arguments are as for cp_parser_skip_to_pragma_eol. */
2731
2732 static void
2733 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2734 {
2735 parser->lexer->in_pragma = false;
2736 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2737 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2738 }
2739
2740 /* This is a simple wrapper around make_typename_type. When the id is
2741 an unresolved identifier node, we can provide a superior diagnostic
2742 using cp_parser_diagnose_invalid_type_name. */
2743
2744 static tree
2745 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2746 tree id, location_t id_location)
2747 {
2748 tree result;
2749 if (TREE_CODE (id) == IDENTIFIER_NODE)
2750 {
2751 result = make_typename_type (scope, id, typename_type,
2752 /*complain=*/tf_none);
2753 if (result == error_mark_node)
2754 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2755 return result;
2756 }
2757 return make_typename_type (scope, id, typename_type, tf_error);
2758 }
2759
2760 /* This is a wrapper around the
2761 make_{pointer,ptrmem,reference}_declarator functions that decides
2762 which one to call based on the CODE and CLASS_TYPE arguments. The
2763 CODE argument should be one of the values returned by
2764 cp_parser_ptr_operator. */
2765 static cp_declarator *
2766 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2767 cp_cv_quals cv_qualifiers,
2768 cp_declarator *target)
2769 {
2770 if (code == ERROR_MARK)
2771 return cp_error_declarator;
2772
2773 if (code == INDIRECT_REF)
2774 if (class_type == NULL_TREE)
2775 return make_pointer_declarator (cv_qualifiers, target);
2776 else
2777 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2778 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2779 return make_reference_declarator (cv_qualifiers, target, false);
2780 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2781 return make_reference_declarator (cv_qualifiers, target, true);
2782 gcc_unreachable ();
2783 }
2784
2785 /* Create a new C++ parser. */
2786
2787 static cp_parser *
2788 cp_parser_new (void)
2789 {
2790 cp_parser *parser;
2791 cp_lexer *lexer;
2792 unsigned i;
2793
2794 /* cp_lexer_new_main is called before calling ggc_alloc because
2795 cp_lexer_new_main might load a PCH file. */
2796 lexer = cp_lexer_new_main ();
2797
2798 /* Initialize the binops_by_token so that we can get the tree
2799 directly from the token. */
2800 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2801 binops_by_token[binops[i].token_type] = binops[i];
2802
2803 parser = GGC_CNEW (cp_parser);
2804 parser->lexer = lexer;
2805 parser->context = cp_parser_context_new (NULL);
2806
2807 /* For now, we always accept GNU extensions. */
2808 parser->allow_gnu_extensions_p = 1;
2809
2810 /* The `>' token is a greater-than operator, not the end of a
2811 template-id. */
2812 parser->greater_than_is_operator_p = true;
2813
2814 parser->default_arg_ok_p = true;
2815
2816 /* We are not parsing a constant-expression. */
2817 parser->integral_constant_expression_p = false;
2818 parser->allow_non_integral_constant_expression_p = false;
2819 parser->non_integral_constant_expression_p = false;
2820
2821 /* Local variable names are not forbidden. */
2822 parser->local_variables_forbidden_p = false;
2823
2824 /* We are not processing an `extern "C"' declaration. */
2825 parser->in_unbraced_linkage_specification_p = false;
2826
2827 /* We are not processing a declarator. */
2828 parser->in_declarator_p = false;
2829
2830 /* We are not processing a template-argument-list. */
2831 parser->in_template_argument_list_p = false;
2832
2833 /* We are not in an iteration statement. */
2834 parser->in_statement = 0;
2835
2836 /* We are not in a switch statement. */
2837 parser->in_switch_statement_p = false;
2838
2839 /* We are not parsing a type-id inside an expression. */
2840 parser->in_type_id_in_expr_p = false;
2841
2842 /* Declarations aren't implicitly extern "C". */
2843 parser->implicit_extern_c = false;
2844
2845 /* String literals should be translated to the execution character set. */
2846 parser->translate_strings_p = true;
2847
2848 /* We are not parsing a function body. */
2849 parser->in_function_body = false;
2850
2851 /* The unparsed function queue is empty. */
2852 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2853
2854 /* There are no classes being defined. */
2855 parser->num_classes_being_defined = 0;
2856
2857 /* No template parameters apply. */
2858 parser->num_template_parameter_lists = 0;
2859
2860 return parser;
2861 }
2862
2863 /* Create a cp_lexer structure which will emit the tokens in CACHE
2864 and push it onto the parser's lexer stack. This is used for delayed
2865 parsing of in-class method bodies and default arguments, and should
2866 not be confused with tentative parsing. */
2867 static void
2868 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2869 {
2870 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2871 lexer->next = parser->lexer;
2872 parser->lexer = lexer;
2873
2874 /* Move the current source position to that of the first token in the
2875 new lexer. */
2876 cp_lexer_set_source_position_from_token (lexer->next_token);
2877 }
2878
2879 /* Pop the top lexer off the parser stack. This is never used for the
2880 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2881 static void
2882 cp_parser_pop_lexer (cp_parser *parser)
2883 {
2884 cp_lexer *lexer = parser->lexer;
2885 parser->lexer = lexer->next;
2886 cp_lexer_destroy (lexer);
2887
2888 /* Put the current source position back where it was before this
2889 lexer was pushed. */
2890 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2891 }
2892
2893 /* Lexical conventions [gram.lex] */
2894
2895 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2896 identifier. */
2897
2898 static tree
2899 cp_parser_identifier (cp_parser* parser)
2900 {
2901 cp_token *token;
2902
2903 /* Look for the identifier. */
2904 token = cp_parser_require (parser, CPP_NAME, "identifier");
2905 /* Return the value. */
2906 return token ? token->u.value : error_mark_node;
2907 }
2908
2909 /* Parse a sequence of adjacent string constants. Returns a
2910 TREE_STRING representing the combined, nul-terminated string
2911 constant. If TRANSLATE is true, translate the string to the
2912 execution character set. If WIDE_OK is true, a wide string is
2913 invalid here.
2914
2915 C++98 [lex.string] says that if a narrow string literal token is
2916 adjacent to a wide string literal token, the behavior is undefined.
2917 However, C99 6.4.5p4 says that this results in a wide string literal.
2918 We follow C99 here, for consistency with the C front end.
2919
2920 This code is largely lifted from lex_string() in c-lex.c.
2921
2922 FUTURE: ObjC++ will need to handle @-strings here. */
2923 static tree
2924 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2925 {
2926 tree value;
2927 size_t count;
2928 struct obstack str_ob;
2929 cpp_string str, istr, *strs;
2930 cp_token *tok;
2931 enum cpp_ttype type;
2932
2933 tok = cp_lexer_peek_token (parser->lexer);
2934 if (!cp_parser_is_string_literal (tok))
2935 {
2936 cp_parser_error (parser, "expected string-literal");
2937 return error_mark_node;
2938 }
2939
2940 type = tok->type;
2941
2942 /* Try to avoid the overhead of creating and destroying an obstack
2943 for the common case of just one string. */
2944 if (!cp_parser_is_string_literal
2945 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2946 {
2947 cp_lexer_consume_token (parser->lexer);
2948
2949 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2950 str.len = TREE_STRING_LENGTH (tok->u.value);
2951 count = 1;
2952
2953 strs = &str;
2954 }
2955 else
2956 {
2957 gcc_obstack_init (&str_ob);
2958 count = 0;
2959
2960 do
2961 {
2962 cp_lexer_consume_token (parser->lexer);
2963 count++;
2964 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2965 str.len = TREE_STRING_LENGTH (tok->u.value);
2966
2967 if (type != tok->type)
2968 {
2969 if (type == CPP_STRING)
2970 type = tok->type;
2971 else if (tok->type != CPP_STRING)
2972 error_at (tok->location,
2973 "unsupported non-standard concatenation "
2974 "of string literals");
2975 }
2976
2977 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2978
2979 tok = cp_lexer_peek_token (parser->lexer);
2980 }
2981 while (cp_parser_is_string_literal (tok));
2982
2983 strs = (cpp_string *) obstack_finish (&str_ob);
2984 }
2985
2986 if (type != CPP_STRING && !wide_ok)
2987 {
2988 cp_parser_error (parser, "a wide string is invalid in this context");
2989 type = CPP_STRING;
2990 }
2991
2992 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2993 (parse_in, strs, count, &istr, type))
2994 {
2995 value = build_string (istr.len, (const char *)istr.text);
2996 free (CONST_CAST (unsigned char *, istr.text));
2997
2998 switch (type)
2999 {
3000 default:
3001 case CPP_STRING:
3002 TREE_TYPE (value) = char_array_type_node;
3003 break;
3004 case CPP_STRING16:
3005 TREE_TYPE (value) = char16_array_type_node;
3006 break;
3007 case CPP_STRING32:
3008 TREE_TYPE (value) = char32_array_type_node;
3009 break;
3010 case CPP_WSTRING:
3011 TREE_TYPE (value) = wchar_array_type_node;
3012 break;
3013 }
3014
3015 value = fix_string_type (value);
3016 }
3017 else
3018 /* cpp_interpret_string has issued an error. */
3019 value = error_mark_node;
3020
3021 if (count > 1)
3022 obstack_free (&str_ob, 0);
3023
3024 return value;
3025 }
3026
3027
3028 /* Basic concepts [gram.basic] */
3029
3030 /* Parse a translation-unit.
3031
3032 translation-unit:
3033 declaration-seq [opt]
3034
3035 Returns TRUE if all went well. */
3036
3037 static bool
3038 cp_parser_translation_unit (cp_parser* parser)
3039 {
3040 /* The address of the first non-permanent object on the declarator
3041 obstack. */
3042 static void *declarator_obstack_base;
3043
3044 bool success;
3045
3046 /* Create the declarator obstack, if necessary. */
3047 if (!cp_error_declarator)
3048 {
3049 gcc_obstack_init (&declarator_obstack);
3050 /* Create the error declarator. */
3051 cp_error_declarator = make_declarator (cdk_error);
3052 /* Create the empty parameter list. */
3053 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3054 /* Remember where the base of the declarator obstack lies. */
3055 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3056 }
3057
3058 cp_parser_declaration_seq_opt (parser);
3059
3060 /* If there are no tokens left then all went well. */
3061 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3062 {
3063 /* Get rid of the token array; we don't need it any more. */
3064 cp_lexer_destroy (parser->lexer);
3065 parser->lexer = NULL;
3066
3067 /* This file might have been a context that's implicitly extern
3068 "C". If so, pop the lang context. (Only relevant for PCH.) */
3069 if (parser->implicit_extern_c)
3070 {
3071 pop_lang_context ();
3072 parser->implicit_extern_c = false;
3073 }
3074
3075 /* Finish up. */
3076 finish_translation_unit ();
3077
3078 success = true;
3079 }
3080 else
3081 {
3082 cp_parser_error (parser, "expected declaration");
3083 success = false;
3084 }
3085
3086 /* Make sure the declarator obstack was fully cleaned up. */
3087 gcc_assert (obstack_next_free (&declarator_obstack)
3088 == declarator_obstack_base);
3089
3090 /* All went well. */
3091 return success;
3092 }
3093
3094 /* Expressions [gram.expr] */
3095
3096 /* Parse a primary-expression.
3097
3098 primary-expression:
3099 literal
3100 this
3101 ( expression )
3102 id-expression
3103
3104 GNU Extensions:
3105
3106 primary-expression:
3107 ( compound-statement )
3108 __builtin_va_arg ( assignment-expression , type-id )
3109 __builtin_offsetof ( type-id , offsetof-expression )
3110
3111 C++ Extensions:
3112 __has_nothrow_assign ( type-id )
3113 __has_nothrow_constructor ( type-id )
3114 __has_nothrow_copy ( type-id )
3115 __has_trivial_assign ( type-id )
3116 __has_trivial_constructor ( type-id )
3117 __has_trivial_copy ( type-id )
3118 __has_trivial_destructor ( type-id )
3119 __has_virtual_destructor ( type-id )
3120 __is_abstract ( type-id )
3121 __is_base_of ( type-id , type-id )
3122 __is_class ( type-id )
3123 __is_convertible_to ( type-id , type-id )
3124 __is_empty ( type-id )
3125 __is_enum ( type-id )
3126 __is_pod ( type-id )
3127 __is_polymorphic ( type-id )
3128 __is_union ( type-id )
3129
3130 Objective-C++ Extension:
3131
3132 primary-expression:
3133 objc-expression
3134
3135 literal:
3136 __null
3137
3138 ADDRESS_P is true iff this expression was immediately preceded by
3139 "&" and therefore might denote a pointer-to-member. CAST_P is true
3140 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3141 true iff this expression is a template argument.
3142
3143 Returns a representation of the expression. Upon return, *IDK
3144 indicates what kind of id-expression (if any) was present. */
3145
3146 static tree
3147 cp_parser_primary_expression (cp_parser *parser,
3148 bool address_p,
3149 bool cast_p,
3150 bool template_arg_p,
3151 cp_id_kind *idk)
3152 {
3153 cp_token *token = NULL;
3154
3155 /* Assume the primary expression is not an id-expression. */
3156 *idk = CP_ID_KIND_NONE;
3157
3158 /* Peek at the next token. */
3159 token = cp_lexer_peek_token (parser->lexer);
3160 switch (token->type)
3161 {
3162 /* literal:
3163 integer-literal
3164 character-literal
3165 floating-literal
3166 string-literal
3167 boolean-literal */
3168 case CPP_CHAR:
3169 case CPP_CHAR16:
3170 case CPP_CHAR32:
3171 case CPP_WCHAR:
3172 case CPP_NUMBER:
3173 token = cp_lexer_consume_token (parser->lexer);
3174 if (TREE_CODE (token->u.value) == FIXED_CST)
3175 {
3176 error_at (token->location,
3177 "fixed-point types not supported in C++");
3178 return error_mark_node;
3179 }
3180 /* Floating-point literals are only allowed in an integral
3181 constant expression if they are cast to an integral or
3182 enumeration type. */
3183 if (TREE_CODE (token->u.value) == REAL_CST
3184 && parser->integral_constant_expression_p
3185 && pedantic)
3186 {
3187 /* CAST_P will be set even in invalid code like "int(2.7 +
3188 ...)". Therefore, we have to check that the next token
3189 is sure to end the cast. */
3190 if (cast_p)
3191 {
3192 cp_token *next_token;
3193
3194 next_token = cp_lexer_peek_token (parser->lexer);
3195 if (/* The comma at the end of an
3196 enumerator-definition. */
3197 next_token->type != CPP_COMMA
3198 /* The curly brace at the end of an enum-specifier. */
3199 && next_token->type != CPP_CLOSE_BRACE
3200 /* The end of a statement. */
3201 && next_token->type != CPP_SEMICOLON
3202 /* The end of the cast-expression. */
3203 && next_token->type != CPP_CLOSE_PAREN
3204 /* The end of an array bound. */
3205 && next_token->type != CPP_CLOSE_SQUARE
3206 /* The closing ">" in a template-argument-list. */
3207 && (next_token->type != CPP_GREATER
3208 || parser->greater_than_is_operator_p)
3209 /* C++0x only: A ">>" treated like two ">" tokens,
3210 in a template-argument-list. */
3211 && (next_token->type != CPP_RSHIFT
3212 || (cxx_dialect == cxx98)
3213 || parser->greater_than_is_operator_p))
3214 cast_p = false;
3215 }
3216
3217 /* If we are within a cast, then the constraint that the
3218 cast is to an integral or enumeration type will be
3219 checked at that point. If we are not within a cast, then
3220 this code is invalid. */
3221 if (!cast_p)
3222 cp_parser_non_integral_constant_expression
3223 (parser, "floating-point literal");
3224 }
3225 return token->u.value;
3226
3227 case CPP_STRING:
3228 case CPP_STRING16:
3229 case CPP_STRING32:
3230 case CPP_WSTRING:
3231 /* ??? Should wide strings be allowed when parser->translate_strings_p
3232 is false (i.e. in attributes)? If not, we can kill the third
3233 argument to cp_parser_string_literal. */
3234 return cp_parser_string_literal (parser,
3235 parser->translate_strings_p,
3236 true);
3237
3238 case CPP_OPEN_PAREN:
3239 {
3240 tree expr;
3241 bool saved_greater_than_is_operator_p;
3242
3243 /* Consume the `('. */
3244 cp_lexer_consume_token (parser->lexer);
3245 /* Within a parenthesized expression, a `>' token is always
3246 the greater-than operator. */
3247 saved_greater_than_is_operator_p
3248 = parser->greater_than_is_operator_p;
3249 parser->greater_than_is_operator_p = true;
3250 /* If we see `( { ' then we are looking at the beginning of
3251 a GNU statement-expression. */
3252 if (cp_parser_allow_gnu_extensions_p (parser)
3253 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3254 {
3255 /* Statement-expressions are not allowed by the standard. */
3256 pedwarn (token->location, OPT_pedantic,
3257 "ISO C++ forbids braced-groups within expressions");
3258
3259 /* And they're not allowed outside of a function-body; you
3260 cannot, for example, write:
3261
3262 int i = ({ int j = 3; j + 1; });
3263
3264 at class or namespace scope. */
3265 if (!parser->in_function_body
3266 || parser->in_template_argument_list_p)
3267 {
3268 error_at (token->location,
3269 "statement-expressions are not allowed outside "
3270 "functions nor in template-argument lists");
3271 cp_parser_skip_to_end_of_block_or_statement (parser);
3272 expr = error_mark_node;
3273 }
3274 else
3275 {
3276 /* Start the statement-expression. */
3277 expr = begin_stmt_expr ();
3278 /* Parse the compound-statement. */
3279 cp_parser_compound_statement (parser, expr, false);
3280 /* Finish up. */
3281 expr = finish_stmt_expr (expr, false);
3282 }
3283 }
3284 else
3285 {
3286 /* Parse the parenthesized expression. */
3287 expr = cp_parser_expression (parser, cast_p, idk);
3288 /* Let the front end know that this expression was
3289 enclosed in parentheses. This matters in case, for
3290 example, the expression is of the form `A::B', since
3291 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3292 not. */
3293 finish_parenthesized_expr (expr);
3294 }
3295 /* The `>' token might be the end of a template-id or
3296 template-parameter-list now. */
3297 parser->greater_than_is_operator_p
3298 = saved_greater_than_is_operator_p;
3299 /* Consume the `)'. */
3300 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3301 cp_parser_skip_to_end_of_statement (parser);
3302
3303 return expr;
3304 }
3305
3306 case CPP_OPEN_SQUARE:
3307 if (c_dialect_objc ())
3308 /* We have an Objective-C++ message. */
3309 return cp_parser_objc_expression (parser);
3310 maybe_warn_cpp0x ("lambda expressions");
3311 return cp_parser_lambda_expression (parser);
3312
3313 case CPP_OBJC_STRING:
3314 if (c_dialect_objc ())
3315 /* We have an Objective-C++ string literal. */
3316 return cp_parser_objc_expression (parser);
3317 cp_parser_error (parser, "expected primary-expression");
3318 return error_mark_node;
3319
3320 case CPP_KEYWORD:
3321 switch (token->keyword)
3322 {
3323 /* These two are the boolean literals. */
3324 case RID_TRUE:
3325 cp_lexer_consume_token (parser->lexer);
3326 return boolean_true_node;
3327 case RID_FALSE:
3328 cp_lexer_consume_token (parser->lexer);
3329 return boolean_false_node;
3330
3331 /* The `__null' literal. */
3332 case RID_NULL:
3333 cp_lexer_consume_token (parser->lexer);
3334 return null_node;
3335
3336 /* Recognize the `this' keyword. */
3337 case RID_THIS:
3338 cp_lexer_consume_token (parser->lexer);
3339 if (parser->local_variables_forbidden_p)
3340 {
3341 error_at (token->location,
3342 "%<this%> may not be used in this context");
3343 return error_mark_node;
3344 }
3345 /* Pointers cannot appear in constant-expressions. */
3346 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3347 return error_mark_node;
3348 return finish_this_expr ();
3349
3350 /* The `operator' keyword can be the beginning of an
3351 id-expression. */
3352 case RID_OPERATOR:
3353 goto id_expression;
3354
3355 case RID_FUNCTION_NAME:
3356 case RID_PRETTY_FUNCTION_NAME:
3357 case RID_C99_FUNCTION_NAME:
3358 {
3359 const char *name;
3360
3361 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3362 __func__ are the names of variables -- but they are
3363 treated specially. Therefore, they are handled here,
3364 rather than relying on the generic id-expression logic
3365 below. Grammatically, these names are id-expressions.
3366
3367 Consume the token. */
3368 token = cp_lexer_consume_token (parser->lexer);
3369
3370 switch (token->keyword)
3371 {
3372 case RID_FUNCTION_NAME:
3373 name = "%<__FUNCTION__%>";
3374 break;
3375 case RID_PRETTY_FUNCTION_NAME:
3376 name = "%<__PRETTY_FUNCTION__%>";
3377 break;
3378 case RID_C99_FUNCTION_NAME:
3379 name = "%<__func__%>";
3380 break;
3381 default:
3382 gcc_unreachable ();
3383 }
3384
3385 if (cp_parser_non_integral_constant_expression (parser, name))
3386 return error_mark_node;
3387
3388 /* Look up the name. */
3389 return finish_fname (token->u.value);
3390 }
3391
3392 case RID_VA_ARG:
3393 {
3394 tree expression;
3395 tree type;
3396
3397 /* The `__builtin_va_arg' construct is used to handle
3398 `va_arg'. Consume the `__builtin_va_arg' token. */
3399 cp_lexer_consume_token (parser->lexer);
3400 /* Look for the opening `('. */
3401 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3402 /* Now, parse the assignment-expression. */
3403 expression = cp_parser_assignment_expression (parser,
3404 /*cast_p=*/false, NULL);
3405 /* Look for the `,'. */
3406 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3407 /* Parse the type-id. */
3408 type = cp_parser_type_id (parser);
3409 /* Look for the closing `)'. */
3410 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3411 /* Using `va_arg' in a constant-expression is not
3412 allowed. */
3413 if (cp_parser_non_integral_constant_expression (parser,
3414 "%<va_arg%>"))
3415 return error_mark_node;
3416 return build_x_va_arg (expression, type);
3417 }
3418
3419 case RID_OFFSETOF:
3420 return cp_parser_builtin_offsetof (parser);
3421
3422 case RID_HAS_NOTHROW_ASSIGN:
3423 case RID_HAS_NOTHROW_CONSTRUCTOR:
3424 case RID_HAS_NOTHROW_COPY:
3425 case RID_HAS_TRIVIAL_ASSIGN:
3426 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3427 case RID_HAS_TRIVIAL_COPY:
3428 case RID_HAS_TRIVIAL_DESTRUCTOR:
3429 case RID_HAS_VIRTUAL_DESTRUCTOR:
3430 case RID_IS_ABSTRACT:
3431 case RID_IS_BASE_OF:
3432 case RID_IS_CLASS:
3433 case RID_IS_CONVERTIBLE_TO:
3434 case RID_IS_EMPTY:
3435 case RID_IS_ENUM:
3436 case RID_IS_POD:
3437 case RID_IS_POLYMORPHIC:
3438 case RID_IS_STD_LAYOUT:
3439 case RID_IS_TRIVIAL:
3440 case RID_IS_UNION:
3441 return cp_parser_trait_expr (parser, token->keyword);
3442
3443 /* Objective-C++ expressions. */
3444 case RID_AT_ENCODE:
3445 case RID_AT_PROTOCOL:
3446 case RID_AT_SELECTOR:
3447 return cp_parser_objc_expression (parser);
3448
3449 default:
3450 cp_parser_error (parser, "expected primary-expression");
3451 return error_mark_node;
3452 }
3453
3454 /* An id-expression can start with either an identifier, a
3455 `::' as the beginning of a qualified-id, or the "operator"
3456 keyword. */
3457 case CPP_NAME:
3458 case CPP_SCOPE:
3459 case CPP_TEMPLATE_ID:
3460 case CPP_NESTED_NAME_SPECIFIER:
3461 {
3462 tree id_expression;
3463 tree decl;
3464 const char *error_msg;
3465 bool template_p;
3466 bool done;
3467 cp_token *id_expr_token;
3468
3469 id_expression:
3470 /* Parse the id-expression. */
3471 id_expression
3472 = cp_parser_id_expression (parser,
3473 /*template_keyword_p=*/false,
3474 /*check_dependency_p=*/true,
3475 &template_p,
3476 /*declarator_p=*/false,
3477 /*optional_p=*/false);
3478 if (id_expression == error_mark_node)
3479 return error_mark_node;
3480 id_expr_token = token;
3481 token = cp_lexer_peek_token (parser->lexer);
3482 done = (token->type != CPP_OPEN_SQUARE
3483 && token->type != CPP_OPEN_PAREN
3484 && token->type != CPP_DOT
3485 && token->type != CPP_DEREF
3486 && token->type != CPP_PLUS_PLUS
3487 && token->type != CPP_MINUS_MINUS);
3488 /* If we have a template-id, then no further lookup is
3489 required. If the template-id was for a template-class, we
3490 will sometimes have a TYPE_DECL at this point. */
3491 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3492 || TREE_CODE (id_expression) == TYPE_DECL)
3493 decl = id_expression;
3494 /* Look up the name. */
3495 else
3496 {
3497 tree ambiguous_decls;
3498
3499 decl = cp_parser_lookup_name (parser, id_expression,
3500 none_type,
3501 template_p,
3502 /*is_namespace=*/false,
3503 /*check_dependency=*/true,
3504 &ambiguous_decls,
3505 id_expr_token->location);
3506 /* If the lookup was ambiguous, an error will already have
3507 been issued. */
3508 if (ambiguous_decls)
3509 return error_mark_node;
3510
3511 /* In Objective-C++, an instance variable (ivar) may be preferred
3512 to whatever cp_parser_lookup_name() found. */
3513 decl = objc_lookup_ivar (decl, id_expression);
3514
3515 /* If name lookup gives us a SCOPE_REF, then the
3516 qualifying scope was dependent. */
3517 if (TREE_CODE (decl) == SCOPE_REF)
3518 {
3519 /* At this point, we do not know if DECL is a valid
3520 integral constant expression. We assume that it is
3521 in fact such an expression, so that code like:
3522
3523 template <int N> struct A {
3524 int a[B<N>::i];
3525 };
3526
3527 is accepted. At template-instantiation time, we
3528 will check that B<N>::i is actually a constant. */
3529 return decl;
3530 }
3531 /* Check to see if DECL is a local variable in a context
3532 where that is forbidden. */
3533 if (parser->local_variables_forbidden_p
3534 && local_variable_p (decl))
3535 {
3536 /* It might be that we only found DECL because we are
3537 trying to be generous with pre-ISO scoping rules.
3538 For example, consider:
3539
3540 int i;
3541 void g() {
3542 for (int i = 0; i < 10; ++i) {}
3543 extern void f(int j = i);
3544 }
3545
3546 Here, name look up will originally find the out
3547 of scope `i'. We need to issue a warning message,
3548 but then use the global `i'. */
3549 decl = check_for_out_of_scope_variable (decl);
3550 if (local_variable_p (decl))
3551 {
3552 error_at (id_expr_token->location,
3553 "local variable %qD may not appear in this context",
3554 decl);
3555 return error_mark_node;
3556 }
3557 }
3558 }
3559
3560 decl = (finish_id_expression
3561 (id_expression, decl, parser->scope,
3562 idk,
3563 parser->integral_constant_expression_p,
3564 parser->allow_non_integral_constant_expression_p,
3565 &parser->non_integral_constant_expression_p,
3566 template_p, done, address_p,
3567 template_arg_p,
3568 &error_msg,
3569 id_expr_token->location));
3570 if (error_msg)
3571 cp_parser_error (parser, error_msg);
3572 return decl;
3573 }
3574
3575 /* Anything else is an error. */
3576 default:
3577 cp_parser_error (parser, "expected primary-expression");
3578 return error_mark_node;
3579 }
3580 }
3581
3582 /* Parse an id-expression.
3583
3584 id-expression:
3585 unqualified-id
3586 qualified-id
3587
3588 qualified-id:
3589 :: [opt] nested-name-specifier template [opt] unqualified-id
3590 :: identifier
3591 :: operator-function-id
3592 :: template-id
3593
3594 Return a representation of the unqualified portion of the
3595 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3596 a `::' or nested-name-specifier.
3597
3598 Often, if the id-expression was a qualified-id, the caller will
3599 want to make a SCOPE_REF to represent the qualified-id. This
3600 function does not do this in order to avoid wastefully creating
3601 SCOPE_REFs when they are not required.
3602
3603 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3604 `template' keyword.
3605
3606 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3607 uninstantiated templates.
3608
3609 If *TEMPLATE_P is non-NULL, it is set to true iff the
3610 `template' keyword is used to explicitly indicate that the entity
3611 named is a template.
3612
3613 If DECLARATOR_P is true, the id-expression is appearing as part of
3614 a declarator, rather than as part of an expression. */
3615
3616 static tree
3617 cp_parser_id_expression (cp_parser *parser,
3618 bool template_keyword_p,
3619 bool check_dependency_p,
3620 bool *template_p,
3621 bool declarator_p,
3622 bool optional_p)
3623 {
3624 bool global_scope_p;
3625 bool nested_name_specifier_p;
3626
3627 /* Assume the `template' keyword was not used. */
3628 if (template_p)
3629 *template_p = template_keyword_p;
3630
3631 /* Look for the optional `::' operator. */
3632 global_scope_p
3633 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3634 != NULL_TREE);
3635 /* Look for the optional nested-name-specifier. */
3636 nested_name_specifier_p
3637 = (cp_parser_nested_name_specifier_opt (parser,
3638 /*typename_keyword_p=*/false,
3639 check_dependency_p,
3640 /*type_p=*/false,
3641 declarator_p)
3642 != NULL_TREE);
3643 /* If there is a nested-name-specifier, then we are looking at
3644 the first qualified-id production. */
3645 if (nested_name_specifier_p)
3646 {
3647 tree saved_scope;
3648 tree saved_object_scope;
3649 tree saved_qualifying_scope;
3650 tree unqualified_id;
3651 bool is_template;
3652
3653 /* See if the next token is the `template' keyword. */
3654 if (!template_p)
3655 template_p = &is_template;
3656 *template_p = cp_parser_optional_template_keyword (parser);
3657 /* Name lookup we do during the processing of the
3658 unqualified-id might obliterate SCOPE. */
3659 saved_scope = parser->scope;
3660 saved_object_scope = parser->object_scope;
3661 saved_qualifying_scope = parser->qualifying_scope;
3662 /* Process the final unqualified-id. */
3663 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3664 check_dependency_p,
3665 declarator_p,
3666 /*optional_p=*/false);
3667 /* Restore the SAVED_SCOPE for our caller. */
3668 parser->scope = saved_scope;
3669 parser->object_scope = saved_object_scope;
3670 parser->qualifying_scope = saved_qualifying_scope;
3671
3672 return unqualified_id;
3673 }
3674 /* Otherwise, if we are in global scope, then we are looking at one
3675 of the other qualified-id productions. */
3676 else if (global_scope_p)
3677 {
3678 cp_token *token;
3679 tree id;
3680
3681 /* Peek at the next token. */
3682 token = cp_lexer_peek_token (parser->lexer);
3683
3684 /* If it's an identifier, and the next token is not a "<", then
3685 we can avoid the template-id case. This is an optimization
3686 for this common case. */
3687 if (token->type == CPP_NAME
3688 && !cp_parser_nth_token_starts_template_argument_list_p
3689 (parser, 2))
3690 return cp_parser_identifier (parser);
3691
3692 cp_parser_parse_tentatively (parser);
3693 /* Try a template-id. */
3694 id = cp_parser_template_id (parser,
3695 /*template_keyword_p=*/false,
3696 /*check_dependency_p=*/true,
3697 declarator_p);
3698 /* If that worked, we're done. */
3699 if (cp_parser_parse_definitely (parser))
3700 return id;
3701
3702 /* Peek at the next token. (Changes in the token buffer may
3703 have invalidated the pointer obtained above.) */
3704 token = cp_lexer_peek_token (parser->lexer);
3705
3706 switch (token->type)
3707 {
3708 case CPP_NAME:
3709 return cp_parser_identifier (parser);
3710
3711 case CPP_KEYWORD:
3712 if (token->keyword == RID_OPERATOR)
3713 return cp_parser_operator_function_id (parser);
3714 /* Fall through. */
3715
3716 default:
3717 cp_parser_error (parser, "expected id-expression");
3718 return error_mark_node;
3719 }
3720 }
3721 else
3722 return cp_parser_unqualified_id (parser, template_keyword_p,
3723 /*check_dependency_p=*/true,
3724 declarator_p,
3725 optional_p);
3726 }
3727
3728 /* Parse an unqualified-id.
3729
3730 unqualified-id:
3731 identifier
3732 operator-function-id
3733 conversion-function-id
3734 ~ class-name
3735 template-id
3736
3737 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3738 keyword, in a construct like `A::template ...'.
3739
3740 Returns a representation of unqualified-id. For the `identifier'
3741 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3742 production a BIT_NOT_EXPR is returned; the operand of the
3743 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3744 other productions, see the documentation accompanying the
3745 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3746 names are looked up in uninstantiated templates. If DECLARATOR_P
3747 is true, the unqualified-id is appearing as part of a declarator,
3748 rather than as part of an expression. */
3749
3750 static tree
3751 cp_parser_unqualified_id (cp_parser* parser,
3752 bool template_keyword_p,
3753 bool check_dependency_p,
3754 bool declarator_p,
3755 bool optional_p)
3756 {
3757 cp_token *token;
3758
3759 /* Peek at the next token. */
3760 token = cp_lexer_peek_token (parser->lexer);
3761
3762 switch (token->type)
3763 {
3764 case CPP_NAME:
3765 {
3766 tree id;
3767
3768 /* We don't know yet whether or not this will be a
3769 template-id. */
3770 cp_parser_parse_tentatively (parser);
3771 /* Try a template-id. */
3772 id = cp_parser_template_id (parser, template_keyword_p,
3773 check_dependency_p,
3774 declarator_p);
3775 /* If it worked, we're done. */
3776 if (cp_parser_parse_definitely (parser))
3777 return id;
3778 /* Otherwise, it's an ordinary identifier. */
3779 return cp_parser_identifier (parser);
3780 }
3781
3782 case CPP_TEMPLATE_ID:
3783 return cp_parser_template_id (parser, template_keyword_p,
3784 check_dependency_p,
3785 declarator_p);
3786
3787 case CPP_COMPL:
3788 {
3789 tree type_decl;
3790 tree qualifying_scope;
3791 tree object_scope;
3792 tree scope;
3793 bool done;
3794
3795 /* Consume the `~' token. */
3796 cp_lexer_consume_token (parser->lexer);
3797 /* Parse the class-name. The standard, as written, seems to
3798 say that:
3799
3800 template <typename T> struct S { ~S (); };
3801 template <typename T> S<T>::~S() {}
3802
3803 is invalid, since `~' must be followed by a class-name, but
3804 `S<T>' is dependent, and so not known to be a class.
3805 That's not right; we need to look in uninstantiated
3806 templates. A further complication arises from:
3807
3808 template <typename T> void f(T t) {
3809 t.T::~T();
3810 }
3811
3812 Here, it is not possible to look up `T' in the scope of `T'
3813 itself. We must look in both the current scope, and the
3814 scope of the containing complete expression.
3815
3816 Yet another issue is:
3817
3818 struct S {
3819 int S;
3820 ~S();
3821 };
3822
3823 S::~S() {}
3824
3825 The standard does not seem to say that the `S' in `~S'
3826 should refer to the type `S' and not the data member
3827 `S::S'. */
3828
3829 /* DR 244 says that we look up the name after the "~" in the
3830 same scope as we looked up the qualifying name. That idea
3831 isn't fully worked out; it's more complicated than that. */
3832 scope = parser->scope;
3833 object_scope = parser->object_scope;
3834 qualifying_scope = parser->qualifying_scope;
3835
3836 /* Check for invalid scopes. */
3837 if (scope == error_mark_node)
3838 {
3839 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3840 cp_lexer_consume_token (parser->lexer);
3841 return error_mark_node;
3842 }
3843 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3844 {
3845 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3846 error_at (token->location,
3847 "scope %qT before %<~%> is not a class-name",
3848 scope);
3849 cp_parser_simulate_error (parser);
3850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3851 cp_lexer_consume_token (parser->lexer);
3852 return error_mark_node;
3853 }
3854 gcc_assert (!scope || TYPE_P (scope));
3855
3856 /* If the name is of the form "X::~X" it's OK. */
3857 token = cp_lexer_peek_token (parser->lexer);
3858 if (scope
3859 && token->type == CPP_NAME
3860 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3861 == CPP_OPEN_PAREN)
3862 && constructor_name_p (token->u.value, scope))
3863 {
3864 cp_lexer_consume_token (parser->lexer);
3865 return build_nt (BIT_NOT_EXPR, scope);
3866 }
3867
3868 /* If there was an explicit qualification (S::~T), first look
3869 in the scope given by the qualification (i.e., S). */
3870 done = false;
3871 type_decl = NULL_TREE;
3872 if (scope)
3873 {
3874 cp_parser_parse_tentatively (parser);
3875 type_decl = cp_parser_class_name (parser,
3876 /*typename_keyword_p=*/false,
3877 /*template_keyword_p=*/false,
3878 none_type,
3879 /*check_dependency=*/false,
3880 /*class_head_p=*/false,
3881 declarator_p);
3882 if (cp_parser_parse_definitely (parser))
3883 done = true;
3884 }
3885 /* In "N::S::~S", look in "N" as well. */
3886 if (!done && scope && qualifying_scope)
3887 {
3888 cp_parser_parse_tentatively (parser);
3889 parser->scope = qualifying_scope;
3890 parser->object_scope = NULL_TREE;
3891 parser->qualifying_scope = NULL_TREE;
3892 type_decl
3893 = cp_parser_class_name (parser,
3894 /*typename_keyword_p=*/false,
3895 /*template_keyword_p=*/false,
3896 none_type,
3897 /*check_dependency=*/false,
3898 /*class_head_p=*/false,
3899 declarator_p);
3900 if (cp_parser_parse_definitely (parser))
3901 done = true;
3902 }
3903 /* In "p->S::~T", look in the scope given by "*p" as well. */
3904 else if (!done && object_scope)
3905 {
3906 cp_parser_parse_tentatively (parser);
3907 parser->scope = object_scope;
3908 parser->object_scope = NULL_TREE;
3909 parser->qualifying_scope = NULL_TREE;
3910 type_decl
3911 = cp_parser_class_name (parser,
3912 /*typename_keyword_p=*/false,
3913 /*template_keyword_p=*/false,
3914 none_type,
3915 /*check_dependency=*/false,
3916 /*class_head_p=*/false,
3917 declarator_p);
3918 if (cp_parser_parse_definitely (parser))
3919 done = true;
3920 }
3921 /* Look in the surrounding context. */
3922 if (!done)
3923 {
3924 parser->scope = NULL_TREE;
3925 parser->object_scope = NULL_TREE;
3926 parser->qualifying_scope = NULL_TREE;
3927 if (processing_template_decl)
3928 cp_parser_parse_tentatively (parser);
3929 type_decl
3930 = cp_parser_class_name (parser,
3931 /*typename_keyword_p=*/false,
3932 /*template_keyword_p=*/false,
3933 none_type,
3934 /*check_dependency=*/false,
3935 /*class_head_p=*/false,
3936 declarator_p);
3937 if (processing_template_decl
3938 && ! cp_parser_parse_definitely (parser))
3939 {
3940 /* We couldn't find a type with this name, so just accept
3941 it and check for a match at instantiation time. */
3942 type_decl = cp_parser_identifier (parser);
3943 if (type_decl != error_mark_node)
3944 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3945 return type_decl;
3946 }
3947 }
3948 /* If an error occurred, assume that the name of the
3949 destructor is the same as the name of the qualifying
3950 class. That allows us to keep parsing after running
3951 into ill-formed destructor names. */
3952 if (type_decl == error_mark_node && scope)
3953 return build_nt (BIT_NOT_EXPR, scope);
3954 else if (type_decl == error_mark_node)
3955 return error_mark_node;
3956
3957 /* Check that destructor name and scope match. */
3958 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3959 {
3960 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3961 error_at (token->location,
3962 "declaration of %<~%T%> as member of %qT",
3963 type_decl, scope);
3964 cp_parser_simulate_error (parser);
3965 return error_mark_node;
3966 }
3967
3968 /* [class.dtor]
3969
3970 A typedef-name that names a class shall not be used as the
3971 identifier in the declarator for a destructor declaration. */
3972 if (declarator_p
3973 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3974 && !DECL_SELF_REFERENCE_P (type_decl)
3975 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3976 error_at (token->location,
3977 "typedef-name %qD used as destructor declarator",
3978 type_decl);
3979
3980 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3981 }
3982
3983 case CPP_KEYWORD:
3984 if (token->keyword == RID_OPERATOR)
3985 {
3986 tree id;
3987
3988 /* This could be a template-id, so we try that first. */
3989 cp_parser_parse_tentatively (parser);
3990 /* Try a template-id. */
3991 id = cp_parser_template_id (parser, template_keyword_p,
3992 /*check_dependency_p=*/true,
3993 declarator_p);
3994 /* If that worked, we're done. */
3995 if (cp_parser_parse_definitely (parser))
3996 return id;
3997 /* We still don't know whether we're looking at an
3998 operator-function-id or a conversion-function-id. */
3999 cp_parser_parse_tentatively (parser);
4000 /* Try an operator-function-id. */
4001 id = cp_parser_operator_function_id (parser);
4002 /* If that didn't work, try a conversion-function-id. */
4003 if (!cp_parser_parse_definitely (parser))
4004 id = cp_parser_conversion_function_id (parser);
4005
4006 return id;
4007 }
4008 /* Fall through. */
4009
4010 default:
4011 if (optional_p)
4012 return NULL_TREE;
4013 cp_parser_error (parser, "expected unqualified-id");
4014 return error_mark_node;
4015 }
4016 }
4017
4018 /* Parse an (optional) nested-name-specifier.
4019
4020 nested-name-specifier: [C++98]
4021 class-or-namespace-name :: nested-name-specifier [opt]
4022 class-or-namespace-name :: template nested-name-specifier [opt]
4023
4024 nested-name-specifier: [C++0x]
4025 type-name ::
4026 namespace-name ::
4027 nested-name-specifier identifier ::
4028 nested-name-specifier template [opt] simple-template-id ::
4029
4030 PARSER->SCOPE should be set appropriately before this function is
4031 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4032 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4033 in name lookups.
4034
4035 Sets PARSER->SCOPE to the class (TYPE) or namespace
4036 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4037 it unchanged if there is no nested-name-specifier. Returns the new
4038 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4039
4040 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4041 part of a declaration and/or decl-specifier. */
4042
4043 static tree
4044 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4045 bool typename_keyword_p,
4046 bool check_dependency_p,
4047 bool type_p,
4048 bool is_declaration)
4049 {
4050 bool success = false;
4051 cp_token_position start = 0;
4052 cp_token *token;
4053
4054 /* Remember where the nested-name-specifier starts. */
4055 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4056 {
4057 start = cp_lexer_token_position (parser->lexer, false);
4058 push_deferring_access_checks (dk_deferred);
4059 }
4060
4061 while (true)
4062 {
4063 tree new_scope;
4064 tree old_scope;
4065 tree saved_qualifying_scope;
4066 bool template_keyword_p;
4067
4068 /* Spot cases that cannot be the beginning of a
4069 nested-name-specifier. */
4070 token = cp_lexer_peek_token (parser->lexer);
4071
4072 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4073 the already parsed nested-name-specifier. */
4074 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4075 {
4076 /* Grab the nested-name-specifier and continue the loop. */
4077 cp_parser_pre_parsed_nested_name_specifier (parser);
4078 /* If we originally encountered this nested-name-specifier
4079 with IS_DECLARATION set to false, we will not have
4080 resolved TYPENAME_TYPEs, so we must do so here. */
4081 if (is_declaration
4082 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4083 {
4084 new_scope = resolve_typename_type (parser->scope,
4085 /*only_current_p=*/false);
4086 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4087 parser->scope = new_scope;
4088 }
4089 success = true;
4090 continue;
4091 }
4092
4093 /* Spot cases that cannot be the beginning of a
4094 nested-name-specifier. On the second and subsequent times
4095 through the loop, we look for the `template' keyword. */
4096 if (success && token->keyword == RID_TEMPLATE)
4097 ;
4098 /* A template-id can start a nested-name-specifier. */
4099 else if (token->type == CPP_TEMPLATE_ID)
4100 ;
4101 else
4102 {
4103 /* If the next token is not an identifier, then it is
4104 definitely not a type-name or namespace-name. */
4105 if (token->type != CPP_NAME)
4106 break;
4107 /* If the following token is neither a `<' (to begin a
4108 template-id), nor a `::', then we are not looking at a
4109 nested-name-specifier. */
4110 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4111 if (token->type != CPP_SCOPE
4112 && !cp_parser_nth_token_starts_template_argument_list_p
4113 (parser, 2))
4114 break;
4115 }
4116
4117 /* The nested-name-specifier is optional, so we parse
4118 tentatively. */
4119 cp_parser_parse_tentatively (parser);
4120
4121 /* Look for the optional `template' keyword, if this isn't the
4122 first time through the loop. */
4123 if (success)
4124 template_keyword_p = cp_parser_optional_template_keyword (parser);
4125 else
4126 template_keyword_p = false;
4127
4128 /* Save the old scope since the name lookup we are about to do
4129 might destroy it. */
4130 old_scope = parser->scope;
4131 saved_qualifying_scope = parser->qualifying_scope;
4132 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4133 look up names in "X<T>::I" in order to determine that "Y" is
4134 a template. So, if we have a typename at this point, we make
4135 an effort to look through it. */
4136 if (is_declaration
4137 && !typename_keyword_p
4138 && parser->scope
4139 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4140 parser->scope = resolve_typename_type (parser->scope,
4141 /*only_current_p=*/false);
4142 /* Parse the qualifying entity. */
4143 new_scope
4144 = cp_parser_qualifying_entity (parser,
4145 typename_keyword_p,
4146 template_keyword_p,
4147 check_dependency_p,
4148 type_p,
4149 is_declaration);
4150 /* Look for the `::' token. */
4151 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4152
4153 /* If we found what we wanted, we keep going; otherwise, we're
4154 done. */
4155 if (!cp_parser_parse_definitely (parser))
4156 {
4157 bool error_p = false;
4158
4159 /* Restore the OLD_SCOPE since it was valid before the
4160 failed attempt at finding the last
4161 class-or-namespace-name. */
4162 parser->scope = old_scope;
4163 parser->qualifying_scope = saved_qualifying_scope;
4164 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4165 break;
4166 /* If the next token is an identifier, and the one after
4167 that is a `::', then any valid interpretation would have
4168 found a class-or-namespace-name. */
4169 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4170 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4171 == CPP_SCOPE)
4172 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4173 != CPP_COMPL))
4174 {
4175 token = cp_lexer_consume_token (parser->lexer);
4176 if (!error_p)
4177 {
4178 if (!token->ambiguous_p)
4179 {
4180 tree decl;
4181 tree ambiguous_decls;
4182
4183 decl = cp_parser_lookup_name (parser, token->u.value,
4184 none_type,
4185 /*is_template=*/false,
4186 /*is_namespace=*/false,
4187 /*check_dependency=*/true,
4188 &ambiguous_decls,
4189 token->location);
4190 if (TREE_CODE (decl) == TEMPLATE_DECL)
4191 error_at (token->location,
4192 "%qD used without template parameters",
4193 decl);
4194 else if (ambiguous_decls)
4195 {
4196 error_at (token->location,
4197 "reference to %qD is ambiguous",
4198 token->u.value);
4199 print_candidates (ambiguous_decls);
4200 decl = error_mark_node;
4201 }
4202 else
4203 {
4204 const char* msg = "is not a class or namespace";
4205 if (cxx_dialect != cxx98)
4206 msg = "is not a class, namespace, or enumeration";
4207 cp_parser_name_lookup_error
4208 (parser, token->u.value, decl, msg,
4209 token->location);
4210 }
4211 }
4212 parser->scope = error_mark_node;
4213 error_p = true;
4214 /* Treat this as a successful nested-name-specifier
4215 due to:
4216
4217 [basic.lookup.qual]
4218
4219 If the name found is not a class-name (clause
4220 _class_) or namespace-name (_namespace.def_), the
4221 program is ill-formed. */
4222 success = true;
4223 }
4224 cp_lexer_consume_token (parser->lexer);
4225 }
4226 break;
4227 }
4228 /* We've found one valid nested-name-specifier. */
4229 success = true;
4230 /* Name lookup always gives us a DECL. */
4231 if (TREE_CODE (new_scope) == TYPE_DECL)
4232 new_scope = TREE_TYPE (new_scope);
4233 /* Uses of "template" must be followed by actual templates. */
4234 if (template_keyword_p
4235 && !(CLASS_TYPE_P (new_scope)
4236 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4237 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4238 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4239 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4240 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4241 == TEMPLATE_ID_EXPR)))
4242 permerror (input_location, TYPE_P (new_scope)
4243 ? "%qT is not a template"
4244 : "%qD is not a template",
4245 new_scope);
4246 /* If it is a class scope, try to complete it; we are about to
4247 be looking up names inside the class. */
4248 if (TYPE_P (new_scope)
4249 /* Since checking types for dependency can be expensive,
4250 avoid doing it if the type is already complete. */
4251 && !COMPLETE_TYPE_P (new_scope)
4252 /* Do not try to complete dependent types. */
4253 && !dependent_type_p (new_scope))
4254 {
4255 new_scope = complete_type (new_scope);
4256 /* If it is a typedef to current class, use the current
4257 class instead, as the typedef won't have any names inside
4258 it yet. */
4259 if (!COMPLETE_TYPE_P (new_scope)
4260 && currently_open_class (new_scope))
4261 new_scope = TYPE_MAIN_VARIANT (new_scope);
4262 }
4263 /* Make sure we look in the right scope the next time through
4264 the loop. */
4265 parser->scope = new_scope;
4266 }
4267
4268 /* If parsing tentatively, replace the sequence of tokens that makes
4269 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4270 token. That way, should we re-parse the token stream, we will
4271 not have to repeat the effort required to do the parse, nor will
4272 we issue duplicate error messages. */
4273 if (success && start)
4274 {
4275 cp_token *token;
4276
4277 token = cp_lexer_token_at (parser->lexer, start);
4278 /* Reset the contents of the START token. */
4279 token->type = CPP_NESTED_NAME_SPECIFIER;
4280 /* Retrieve any deferred checks. Do not pop this access checks yet
4281 so the memory will not be reclaimed during token replacing below. */
4282 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4283 token->u.tree_check_value->value = parser->scope;
4284 token->u.tree_check_value->checks = get_deferred_access_checks ();
4285 token->u.tree_check_value->qualifying_scope =
4286 parser->qualifying_scope;
4287 token->keyword = RID_MAX;
4288
4289 /* Purge all subsequent tokens. */
4290 cp_lexer_purge_tokens_after (parser->lexer, start);
4291 }
4292
4293 if (start)
4294 pop_to_parent_deferring_access_checks ();
4295
4296 return success ? parser->scope : NULL_TREE;
4297 }
4298
4299 /* Parse a nested-name-specifier. See
4300 cp_parser_nested_name_specifier_opt for details. This function
4301 behaves identically, except that it will an issue an error if no
4302 nested-name-specifier is present. */
4303
4304 static tree
4305 cp_parser_nested_name_specifier (cp_parser *parser,
4306 bool typename_keyword_p,
4307 bool check_dependency_p,
4308 bool type_p,
4309 bool is_declaration)
4310 {
4311 tree scope;
4312
4313 /* Look for the nested-name-specifier. */
4314 scope = cp_parser_nested_name_specifier_opt (parser,
4315 typename_keyword_p,
4316 check_dependency_p,
4317 type_p,
4318 is_declaration);
4319 /* If it was not present, issue an error message. */
4320 if (!scope)
4321 {
4322 cp_parser_error (parser, "expected nested-name-specifier");
4323 parser->scope = NULL_TREE;
4324 }
4325
4326 return scope;
4327 }
4328
4329 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4330 this is either a class-name or a namespace-name (which corresponds
4331 to the class-or-namespace-name production in the grammar). For
4332 C++0x, it can also be a type-name that refers to an enumeration
4333 type.
4334
4335 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4336 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4337 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4338 TYPE_P is TRUE iff the next name should be taken as a class-name,
4339 even the same name is declared to be another entity in the same
4340 scope.
4341
4342 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4343 specified by the class-or-namespace-name. If neither is found the
4344 ERROR_MARK_NODE is returned. */
4345
4346 static tree
4347 cp_parser_qualifying_entity (cp_parser *parser,
4348 bool typename_keyword_p,
4349 bool template_keyword_p,
4350 bool check_dependency_p,
4351 bool type_p,
4352 bool is_declaration)
4353 {
4354 tree saved_scope;
4355 tree saved_qualifying_scope;
4356 tree saved_object_scope;
4357 tree scope;
4358 bool only_class_p;
4359 bool successful_parse_p;
4360
4361 /* Before we try to parse the class-name, we must save away the
4362 current PARSER->SCOPE since cp_parser_class_name will destroy
4363 it. */
4364 saved_scope = parser->scope;
4365 saved_qualifying_scope = parser->qualifying_scope;
4366 saved_object_scope = parser->object_scope;
4367 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4368 there is no need to look for a namespace-name. */
4369 only_class_p = template_keyword_p
4370 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4371 if (!only_class_p)
4372 cp_parser_parse_tentatively (parser);
4373 scope = cp_parser_class_name (parser,
4374 typename_keyword_p,
4375 template_keyword_p,
4376 type_p ? class_type : none_type,
4377 check_dependency_p,
4378 /*class_head_p=*/false,
4379 is_declaration);
4380 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4381 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4382 if (!only_class_p
4383 && cxx_dialect != cxx98
4384 && !successful_parse_p)
4385 {
4386 /* Restore the saved scope. */
4387 parser->scope = saved_scope;
4388 parser->qualifying_scope = saved_qualifying_scope;
4389 parser->object_scope = saved_object_scope;
4390
4391 /* Parse tentatively. */
4392 cp_parser_parse_tentatively (parser);
4393
4394 /* Parse a typedef-name or enum-name. */
4395 scope = cp_parser_nonclass_name (parser);
4396 successful_parse_p = cp_parser_parse_definitely (parser);
4397 }
4398 /* If that didn't work, try for a namespace-name. */
4399 if (!only_class_p && !successful_parse_p)
4400 {
4401 /* Restore the saved scope. */
4402 parser->scope = saved_scope;
4403 parser->qualifying_scope = saved_qualifying_scope;
4404 parser->object_scope = saved_object_scope;
4405 /* If we are not looking at an identifier followed by the scope
4406 resolution operator, then this is not part of a
4407 nested-name-specifier. (Note that this function is only used
4408 to parse the components of a nested-name-specifier.) */
4409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4410 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4411 return error_mark_node;
4412 scope = cp_parser_namespace_name (parser);
4413 }
4414
4415 return scope;
4416 }
4417
4418 /* Parse a postfix-expression.
4419
4420 postfix-expression:
4421 primary-expression
4422 postfix-expression [ expression ]
4423 postfix-expression ( expression-list [opt] )
4424 simple-type-specifier ( expression-list [opt] )
4425 typename :: [opt] nested-name-specifier identifier
4426 ( expression-list [opt] )
4427 typename :: [opt] nested-name-specifier template [opt] template-id
4428 ( expression-list [opt] )
4429 postfix-expression . template [opt] id-expression
4430 postfix-expression -> template [opt] id-expression
4431 postfix-expression . pseudo-destructor-name
4432 postfix-expression -> pseudo-destructor-name
4433 postfix-expression ++
4434 postfix-expression --
4435 dynamic_cast < type-id > ( expression )
4436 static_cast < type-id > ( expression )
4437 reinterpret_cast < type-id > ( expression )
4438 const_cast < type-id > ( expression )
4439 typeid ( expression )
4440 typeid ( type-id )
4441
4442 GNU Extension:
4443
4444 postfix-expression:
4445 ( type-id ) { initializer-list , [opt] }
4446
4447 This extension is a GNU version of the C99 compound-literal
4448 construct. (The C99 grammar uses `type-name' instead of `type-id',
4449 but they are essentially the same concept.)
4450
4451 If ADDRESS_P is true, the postfix expression is the operand of the
4452 `&' operator. CAST_P is true if this expression is the target of a
4453 cast.
4454
4455 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4456 class member access expressions [expr.ref].
4457
4458 Returns a representation of the expression. */
4459
4460 static tree
4461 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4462 bool member_access_only_p,
4463 cp_id_kind * pidk_return)
4464 {
4465 cp_token *token;
4466 enum rid keyword;
4467 cp_id_kind idk = CP_ID_KIND_NONE;
4468 tree postfix_expression = NULL_TREE;
4469 bool is_member_access = false;
4470
4471 /* Peek at the next token. */
4472 token = cp_lexer_peek_token (parser->lexer);
4473 /* Some of the productions are determined by keywords. */
4474 keyword = token->keyword;
4475 switch (keyword)
4476 {
4477 case RID_DYNCAST:
4478 case RID_STATCAST:
4479 case RID_REINTCAST:
4480 case RID_CONSTCAST:
4481 {
4482 tree type;
4483 tree expression;
4484 const char *saved_message;
4485
4486 /* All of these can be handled in the same way from the point
4487 of view of parsing. Begin by consuming the token
4488 identifying the cast. */
4489 cp_lexer_consume_token (parser->lexer);
4490
4491 /* New types cannot be defined in the cast. */
4492 saved_message = parser->type_definition_forbidden_message;
4493 parser->type_definition_forbidden_message
4494 = "types may not be defined in casts";
4495
4496 /* Look for the opening `<'. */
4497 cp_parser_require (parser, CPP_LESS, "%<<%>");
4498 /* Parse the type to which we are casting. */
4499 type = cp_parser_type_id (parser);
4500 /* Look for the closing `>'. */
4501 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4502 /* Restore the old message. */
4503 parser->type_definition_forbidden_message = saved_message;
4504
4505 /* And the expression which is being cast. */
4506 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4507 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4508 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4509
4510 /* Only type conversions to integral or enumeration types
4511 can be used in constant-expressions. */
4512 if (!cast_valid_in_integral_constant_expression_p (type)
4513 && (cp_parser_non_integral_constant_expression
4514 (parser,
4515 "a cast to a type other than an integral or "
4516 "enumeration type")))
4517 return error_mark_node;
4518
4519 switch (keyword)
4520 {
4521 case RID_DYNCAST:
4522 postfix_expression
4523 = build_dynamic_cast (type, expression, tf_warning_or_error);
4524 break;
4525 case RID_STATCAST:
4526 postfix_expression
4527 = build_static_cast (type, expression, tf_warning_or_error);
4528 break;
4529 case RID_REINTCAST:
4530 postfix_expression
4531 = build_reinterpret_cast (type, expression,
4532 tf_warning_or_error);
4533 break;
4534 case RID_CONSTCAST:
4535 postfix_expression
4536 = build_const_cast (type, expression, tf_warning_or_error);
4537 break;
4538 default:
4539 gcc_unreachable ();
4540 }
4541 }
4542 break;
4543
4544 case RID_TYPEID:
4545 {
4546 tree type;
4547 const char *saved_message;
4548 bool saved_in_type_id_in_expr_p;
4549
4550 /* Consume the `typeid' token. */
4551 cp_lexer_consume_token (parser->lexer);
4552 /* Look for the `(' token. */
4553 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554 /* Types cannot be defined in a `typeid' expression. */
4555 saved_message = parser->type_definition_forbidden_message;
4556 parser->type_definition_forbidden_message
4557 = "types may not be defined in a %<typeid%> expression";
4558 /* We can't be sure yet whether we're looking at a type-id or an
4559 expression. */
4560 cp_parser_parse_tentatively (parser);
4561 /* Try a type-id first. */
4562 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4563 parser->in_type_id_in_expr_p = true;
4564 type = cp_parser_type_id (parser);
4565 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4566 /* Look for the `)' token. Otherwise, we can't be sure that
4567 we're not looking at an expression: consider `typeid (int
4568 (3))', for example. */
4569 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4570 /* If all went well, simply lookup the type-id. */
4571 if (cp_parser_parse_definitely (parser))
4572 postfix_expression = get_typeid (type);
4573 /* Otherwise, fall back to the expression variant. */
4574 else
4575 {
4576 tree expression;
4577
4578 /* Look for an expression. */
4579 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4580 /* Compute its typeid. */
4581 postfix_expression = build_typeid (expression);
4582 /* Look for the `)' token. */
4583 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4584 }
4585 /* Restore the saved message. */
4586 parser->type_definition_forbidden_message = saved_message;
4587 /* `typeid' may not appear in an integral constant expression. */
4588 if (cp_parser_non_integral_constant_expression(parser,
4589 "%<typeid%> operator"))
4590 return error_mark_node;
4591 }
4592 break;
4593
4594 case RID_TYPENAME:
4595 {
4596 tree type;
4597 /* The syntax permitted here is the same permitted for an
4598 elaborated-type-specifier. */
4599 type = cp_parser_elaborated_type_specifier (parser,
4600 /*is_friend=*/false,
4601 /*is_declaration=*/false);
4602 postfix_expression = cp_parser_functional_cast (parser, type);
4603 }
4604 break;
4605
4606 default:
4607 {
4608 tree type;
4609
4610 /* If the next thing is a simple-type-specifier, we may be
4611 looking at a functional cast. We could also be looking at
4612 an id-expression. So, we try the functional cast, and if
4613 that doesn't work we fall back to the primary-expression. */
4614 cp_parser_parse_tentatively (parser);
4615 /* Look for the simple-type-specifier. */
4616 type = cp_parser_simple_type_specifier (parser,
4617 /*decl_specs=*/NULL,
4618 CP_PARSER_FLAGS_NONE);
4619 /* Parse the cast itself. */
4620 if (!cp_parser_error_occurred (parser))
4621 postfix_expression
4622 = cp_parser_functional_cast (parser, type);
4623 /* If that worked, we're done. */
4624 if (cp_parser_parse_definitely (parser))
4625 break;
4626
4627 /* If the functional-cast didn't work out, try a
4628 compound-literal. */
4629 if (cp_parser_allow_gnu_extensions_p (parser)
4630 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4631 {
4632 VEC(constructor_elt,gc) *initializer_list = NULL;
4633 bool saved_in_type_id_in_expr_p;
4634
4635 cp_parser_parse_tentatively (parser);
4636 /* Consume the `('. */
4637 cp_lexer_consume_token (parser->lexer);
4638 /* Parse the type. */
4639 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4640 parser->in_type_id_in_expr_p = true;
4641 type = cp_parser_type_id (parser);
4642 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4643 /* Look for the `)'. */
4644 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4645 /* Look for the `{'. */
4646 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4647 /* If things aren't going well, there's no need to
4648 keep going. */
4649 if (!cp_parser_error_occurred (parser))
4650 {
4651 bool non_constant_p;
4652 /* Parse the initializer-list. */
4653 initializer_list
4654 = cp_parser_initializer_list (parser, &non_constant_p);
4655 /* Allow a trailing `,'. */
4656 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4657 cp_lexer_consume_token (parser->lexer);
4658 /* Look for the final `}'. */
4659 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4660 }
4661 /* If that worked, we're definitely looking at a
4662 compound-literal expression. */
4663 if (cp_parser_parse_definitely (parser))
4664 {
4665 /* Warn the user that a compound literal is not
4666 allowed in standard C++. */
4667 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4668 /* For simplicity, we disallow compound literals in
4669 constant-expressions. We could
4670 allow compound literals of integer type, whose
4671 initializer was a constant, in constant
4672 expressions. Permitting that usage, as a further
4673 extension, would not change the meaning of any
4674 currently accepted programs. (Of course, as
4675 compound literals are not part of ISO C++, the
4676 standard has nothing to say.) */
4677 if (cp_parser_non_integral_constant_expression
4678 (parser, "non-constant compound literals"))
4679 {
4680 postfix_expression = error_mark_node;
4681 break;
4682 }
4683 /* Form the representation of the compound-literal. */
4684 postfix_expression
4685 = (finish_compound_literal
4686 (type, build_constructor (init_list_type_node,
4687 initializer_list)));
4688 break;
4689 }
4690 }
4691
4692 /* It must be a primary-expression. */
4693 postfix_expression
4694 = cp_parser_primary_expression (parser, address_p, cast_p,
4695 /*template_arg_p=*/false,
4696 &idk);
4697 }
4698 break;
4699 }
4700
4701 /* Keep looping until the postfix-expression is complete. */
4702 while (true)
4703 {
4704 if (idk == CP_ID_KIND_UNQUALIFIED
4705 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4706 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4707 /* It is not a Koenig lookup function call. */
4708 postfix_expression
4709 = unqualified_name_lookup_error (postfix_expression);
4710
4711 /* Peek at the next token. */
4712 token = cp_lexer_peek_token (parser->lexer);
4713
4714 switch (token->type)
4715 {
4716 case CPP_OPEN_SQUARE:
4717 postfix_expression
4718 = cp_parser_postfix_open_square_expression (parser,
4719 postfix_expression,
4720 false);
4721 idk = CP_ID_KIND_NONE;
4722 is_member_access = false;
4723 break;
4724
4725 case CPP_OPEN_PAREN:
4726 /* postfix-expression ( expression-list [opt] ) */
4727 {
4728 bool koenig_p;
4729 bool is_builtin_constant_p;
4730 bool saved_integral_constant_expression_p = false;
4731 bool saved_non_integral_constant_expression_p = false;
4732 VEC(tree,gc) *args;
4733
4734 is_member_access = false;
4735
4736 is_builtin_constant_p
4737 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4738 if (is_builtin_constant_p)
4739 {
4740 /* The whole point of __builtin_constant_p is to allow
4741 non-constant expressions to appear as arguments. */
4742 saved_integral_constant_expression_p
4743 = parser->integral_constant_expression_p;
4744 saved_non_integral_constant_expression_p
4745 = parser->non_integral_constant_expression_p;
4746 parser->integral_constant_expression_p = false;
4747 }
4748 args = (cp_parser_parenthesized_expression_list
4749 (parser, /*is_attribute_list=*/false,
4750 /*cast_p=*/false, /*allow_expansion_p=*/true,
4751 /*non_constant_p=*/NULL));
4752 if (is_builtin_constant_p)
4753 {
4754 parser->integral_constant_expression_p
4755 = saved_integral_constant_expression_p;
4756 parser->non_integral_constant_expression_p
4757 = saved_non_integral_constant_expression_p;
4758 }
4759
4760 if (args == NULL)
4761 {
4762 postfix_expression = error_mark_node;
4763 break;
4764 }
4765
4766 /* Function calls are not permitted in
4767 constant-expressions. */
4768 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4769 && cp_parser_non_integral_constant_expression (parser,
4770 "a function call"))
4771 {
4772 postfix_expression = error_mark_node;
4773 release_tree_vector (args);
4774 break;
4775 }
4776
4777 koenig_p = false;
4778 if (idk == CP_ID_KIND_UNQUALIFIED
4779 || idk == CP_ID_KIND_TEMPLATE_ID)
4780 {
4781 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4782 {
4783 if (!VEC_empty (tree, args))
4784 {
4785 koenig_p = true;
4786 if (!any_type_dependent_arguments_p (args))
4787 postfix_expression
4788 = perform_koenig_lookup (postfix_expression, args);
4789 }
4790 else
4791 postfix_expression
4792 = unqualified_fn_lookup_error (postfix_expression);
4793 }
4794 /* We do not perform argument-dependent lookup if
4795 normal lookup finds a non-function, in accordance
4796 with the expected resolution of DR 218. */
4797 else if (!VEC_empty (tree, args)
4798 && is_overloaded_fn (postfix_expression))
4799 {
4800 tree fn = get_first_fn (postfix_expression);
4801
4802 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4803 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4804
4805 /* Only do argument dependent lookup if regular
4806 lookup does not find a set of member functions.
4807 [basic.lookup.koenig]/2a */
4808 if (!DECL_FUNCTION_MEMBER_P (fn))
4809 {
4810 koenig_p = true;
4811 if (!any_type_dependent_arguments_p (args))
4812 postfix_expression
4813 = perform_koenig_lookup (postfix_expression, args);
4814 }
4815 }
4816 }
4817
4818 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4819 {
4820 tree instance = TREE_OPERAND (postfix_expression, 0);
4821 tree fn = TREE_OPERAND (postfix_expression, 1);
4822
4823 if (processing_template_decl
4824 && (type_dependent_expression_p (instance)
4825 || (!BASELINK_P (fn)
4826 && TREE_CODE (fn) != FIELD_DECL)
4827 || type_dependent_expression_p (fn)
4828 || any_type_dependent_arguments_p (args)))
4829 {
4830 postfix_expression
4831 = build_nt_call_vec (postfix_expression, args);
4832 release_tree_vector (args);
4833 break;
4834 }
4835
4836 if (BASELINK_P (fn))
4837 {
4838 postfix_expression
4839 = (build_new_method_call
4840 (instance, fn, &args, NULL_TREE,
4841 (idk == CP_ID_KIND_QUALIFIED
4842 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4843 /*fn_p=*/NULL,
4844 tf_warning_or_error));
4845 }
4846 else
4847 postfix_expression
4848 = finish_call_expr (postfix_expression, &args,
4849 /*disallow_virtual=*/false,
4850 /*koenig_p=*/false,
4851 tf_warning_or_error);
4852 }
4853 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4854 || TREE_CODE (postfix_expression) == MEMBER_REF
4855 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4856 postfix_expression = (build_offset_ref_call_from_tree
4857 (postfix_expression, &args));
4858 else if (idk == CP_ID_KIND_QUALIFIED)
4859 /* A call to a static class member, or a namespace-scope
4860 function. */
4861 postfix_expression
4862 = finish_call_expr (postfix_expression, &args,
4863 /*disallow_virtual=*/true,
4864 koenig_p,
4865 tf_warning_or_error);
4866 else
4867 /* All other function calls. */
4868 postfix_expression
4869 = finish_call_expr (postfix_expression, &args,
4870 /*disallow_virtual=*/false,
4871 koenig_p,
4872 tf_warning_or_error);
4873
4874 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4875 idk = CP_ID_KIND_NONE;
4876
4877 release_tree_vector (args);
4878 }
4879 break;
4880
4881 case CPP_DOT:
4882 case CPP_DEREF:
4883 /* postfix-expression . template [opt] id-expression
4884 postfix-expression . pseudo-destructor-name
4885 postfix-expression -> template [opt] id-expression
4886 postfix-expression -> pseudo-destructor-name */
4887
4888 /* Consume the `.' or `->' operator. */
4889 cp_lexer_consume_token (parser->lexer);
4890
4891 postfix_expression
4892 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4893 postfix_expression,
4894 false, &idk,
4895 token->location);
4896
4897 is_member_access = true;
4898 break;
4899
4900 case CPP_PLUS_PLUS:
4901 /* postfix-expression ++ */
4902 /* Consume the `++' token. */
4903 cp_lexer_consume_token (parser->lexer);
4904 /* Generate a representation for the complete expression. */
4905 postfix_expression
4906 = finish_increment_expr (postfix_expression,
4907 POSTINCREMENT_EXPR);
4908 /* Increments may not appear in constant-expressions. */
4909 if (cp_parser_non_integral_constant_expression (parser,
4910 "an increment"))
4911 postfix_expression = error_mark_node;
4912 idk = CP_ID_KIND_NONE;
4913 is_member_access = false;
4914 break;
4915
4916 case CPP_MINUS_MINUS:
4917 /* postfix-expression -- */
4918 /* Consume the `--' token. */
4919 cp_lexer_consume_token (parser->lexer);
4920 /* Generate a representation for the complete expression. */
4921 postfix_expression
4922 = finish_increment_expr (postfix_expression,
4923 POSTDECREMENT_EXPR);
4924 /* Decrements may not appear in constant-expressions. */
4925 if (cp_parser_non_integral_constant_expression (parser,
4926 "a decrement"))
4927 postfix_expression = error_mark_node;
4928 idk = CP_ID_KIND_NONE;
4929 is_member_access = false;
4930 break;
4931
4932 default:
4933 if (pidk_return != NULL)
4934 * pidk_return = idk;
4935 if (member_access_only_p)
4936 return is_member_access? postfix_expression : error_mark_node;
4937 else
4938 return postfix_expression;
4939 }
4940 }
4941
4942 /* We should never get here. */
4943 gcc_unreachable ();
4944 return error_mark_node;
4945 }
4946
4947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4948 by cp_parser_builtin_offsetof. We're looking for
4949
4950 postfix-expression [ expression ]
4951
4952 FOR_OFFSETOF is set if we're being called in that context, which
4953 changes how we deal with integer constant expressions. */
4954
4955 static tree
4956 cp_parser_postfix_open_square_expression (cp_parser *parser,
4957 tree postfix_expression,
4958 bool for_offsetof)
4959 {
4960 tree index;
4961
4962 /* Consume the `[' token. */
4963 cp_lexer_consume_token (parser->lexer);
4964
4965 /* Parse the index expression. */
4966 /* ??? For offsetof, there is a question of what to allow here. If
4967 offsetof is not being used in an integral constant expression context,
4968 then we *could* get the right answer by computing the value at runtime.
4969 If we are in an integral constant expression context, then we might
4970 could accept any constant expression; hard to say without analysis.
4971 Rather than open the barn door too wide right away, allow only integer
4972 constant expressions here. */
4973 if (for_offsetof)
4974 index = cp_parser_constant_expression (parser, false, NULL);
4975 else
4976 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4977
4978 /* Look for the closing `]'. */
4979 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4980
4981 /* Build the ARRAY_REF. */
4982 postfix_expression = grok_array_decl (postfix_expression, index);
4983
4984 /* When not doing offsetof, array references are not permitted in
4985 constant-expressions. */
4986 if (!for_offsetof
4987 && (cp_parser_non_integral_constant_expression
4988 (parser, "an array reference")))
4989 postfix_expression = error_mark_node;
4990
4991 return postfix_expression;
4992 }
4993
4994 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4995 by cp_parser_builtin_offsetof. We're looking for
4996
4997 postfix-expression . template [opt] id-expression
4998 postfix-expression . pseudo-destructor-name
4999 postfix-expression -> template [opt] id-expression
5000 postfix-expression -> pseudo-destructor-name
5001
5002 FOR_OFFSETOF is set if we're being called in that context. That sorta
5003 limits what of the above we'll actually accept, but nevermind.
5004 TOKEN_TYPE is the "." or "->" token, which will already have been
5005 removed from the stream. */
5006
5007 static tree
5008 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5009 enum cpp_ttype token_type,
5010 tree postfix_expression,
5011 bool for_offsetof, cp_id_kind *idk,
5012 location_t location)
5013 {
5014 tree name;
5015 bool dependent_p;
5016 bool pseudo_destructor_p;
5017 tree scope = NULL_TREE;
5018
5019 /* If this is a `->' operator, dereference the pointer. */
5020 if (token_type == CPP_DEREF)
5021 postfix_expression = build_x_arrow (postfix_expression);
5022 /* Check to see whether or not the expression is type-dependent. */
5023 dependent_p = type_dependent_expression_p (postfix_expression);
5024 /* The identifier following the `->' or `.' is not qualified. */
5025 parser->scope = NULL_TREE;
5026 parser->qualifying_scope = NULL_TREE;
5027 parser->object_scope = NULL_TREE;
5028 *idk = CP_ID_KIND_NONE;
5029
5030 /* Enter the scope corresponding to the type of the object
5031 given by the POSTFIX_EXPRESSION. */
5032 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5033 {
5034 scope = TREE_TYPE (postfix_expression);
5035 /* According to the standard, no expression should ever have
5036 reference type. Unfortunately, we do not currently match
5037 the standard in this respect in that our internal representation
5038 of an expression may have reference type even when the standard
5039 says it does not. Therefore, we have to manually obtain the
5040 underlying type here. */
5041 scope = non_reference (scope);
5042 /* The type of the POSTFIX_EXPRESSION must be complete. */
5043 if (scope == unknown_type_node)
5044 {
5045 error_at (location, "%qE does not have class type",
5046 postfix_expression);
5047 scope = NULL_TREE;
5048 }
5049 else
5050 scope = complete_type_or_else (scope, NULL_TREE);
5051 /* Let the name lookup machinery know that we are processing a
5052 class member access expression. */
5053 parser->context->object_type = scope;
5054 /* If something went wrong, we want to be able to discern that case,
5055 as opposed to the case where there was no SCOPE due to the type
5056 of expression being dependent. */
5057 if (!scope)
5058 scope = error_mark_node;
5059 /* If the SCOPE was erroneous, make the various semantic analysis
5060 functions exit quickly -- and without issuing additional error
5061 messages. */
5062 if (scope == error_mark_node)
5063 postfix_expression = error_mark_node;
5064 }
5065
5066 /* Assume this expression is not a pseudo-destructor access. */
5067 pseudo_destructor_p = false;
5068
5069 /* If the SCOPE is a scalar type, then, if this is a valid program,
5070 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5071 is type dependent, it can be pseudo-destructor-name or something else.
5072 Try to parse it as pseudo-destructor-name first. */
5073 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5074 {
5075 tree s;
5076 tree type;
5077
5078 cp_parser_parse_tentatively (parser);
5079 /* Parse the pseudo-destructor-name. */
5080 s = NULL_TREE;
5081 cp_parser_pseudo_destructor_name (parser, &s, &type);
5082 if (dependent_p
5083 && (cp_parser_error_occurred (parser)
5084 || TREE_CODE (type) != TYPE_DECL
5085 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5086 cp_parser_abort_tentative_parse (parser);
5087 else if (cp_parser_parse_definitely (parser))
5088 {
5089 pseudo_destructor_p = true;
5090 postfix_expression
5091 = finish_pseudo_destructor_expr (postfix_expression,
5092 s, TREE_TYPE (type));
5093 }
5094 }
5095
5096 if (!pseudo_destructor_p)
5097 {
5098 /* If the SCOPE is not a scalar type, we are looking at an
5099 ordinary class member access expression, rather than a
5100 pseudo-destructor-name. */
5101 bool template_p;
5102 cp_token *token = cp_lexer_peek_token (parser->lexer);
5103 /* Parse the id-expression. */
5104 name = (cp_parser_id_expression
5105 (parser,
5106 cp_parser_optional_template_keyword (parser),
5107 /*check_dependency_p=*/true,
5108 &template_p,
5109 /*declarator_p=*/false,
5110 /*optional_p=*/false));
5111 /* In general, build a SCOPE_REF if the member name is qualified.
5112 However, if the name was not dependent and has already been
5113 resolved; there is no need to build the SCOPE_REF. For example;
5114
5115 struct X { void f(); };
5116 template <typename T> void f(T* t) { t->X::f(); }
5117
5118 Even though "t" is dependent, "X::f" is not and has been resolved
5119 to a BASELINK; there is no need to include scope information. */
5120
5121 /* But we do need to remember that there was an explicit scope for
5122 virtual function calls. */
5123 if (parser->scope)
5124 *idk = CP_ID_KIND_QUALIFIED;
5125
5126 /* If the name is a template-id that names a type, we will get a
5127 TYPE_DECL here. That is invalid code. */
5128 if (TREE_CODE (name) == TYPE_DECL)
5129 {
5130 error_at (token->location, "invalid use of %qD", name);
5131 postfix_expression = error_mark_node;
5132 }
5133 else
5134 {
5135 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5136 {
5137 name = build_qualified_name (/*type=*/NULL_TREE,
5138 parser->scope,
5139 name,
5140 template_p);
5141 parser->scope = NULL_TREE;
5142 parser->qualifying_scope = NULL_TREE;
5143 parser->object_scope = NULL_TREE;
5144 }
5145 if (scope && name && BASELINK_P (name))
5146 adjust_result_of_qualified_name_lookup
5147 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5148 postfix_expression
5149 = finish_class_member_access_expr (postfix_expression, name,
5150 template_p,
5151 tf_warning_or_error);
5152 }
5153 }
5154
5155 /* We no longer need to look up names in the scope of the object on
5156 the left-hand side of the `.' or `->' operator. */
5157 parser->context->object_type = NULL_TREE;
5158
5159 /* Outside of offsetof, these operators may not appear in
5160 constant-expressions. */
5161 if (!for_offsetof
5162 && (cp_parser_non_integral_constant_expression
5163 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5164 postfix_expression = error_mark_node;
5165
5166 return postfix_expression;
5167 }
5168
5169 /* Parse a parenthesized expression-list.
5170
5171 expression-list:
5172 assignment-expression
5173 expression-list, assignment-expression
5174
5175 attribute-list:
5176 expression-list
5177 identifier
5178 identifier, expression-list
5179
5180 CAST_P is true if this expression is the target of a cast.
5181
5182 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5183 argument pack.
5184
5185 Returns a vector of trees. Each element is a representation of an
5186 assignment-expression. NULL is returned if the ( and or ) are
5187 missing. An empty, but allocated, vector is returned on no
5188 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5189 if this is really an attribute list being parsed. If
5190 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5191 not all of the expressions in the list were constant. */
5192
5193 static VEC(tree,gc) *
5194 cp_parser_parenthesized_expression_list (cp_parser* parser,
5195 bool is_attribute_list,
5196 bool cast_p,
5197 bool allow_expansion_p,
5198 bool *non_constant_p)
5199 {
5200 VEC(tree,gc) *expression_list;
5201 bool fold_expr_p = is_attribute_list;
5202 tree identifier = NULL_TREE;
5203 bool saved_greater_than_is_operator_p;
5204
5205 /* Assume all the expressions will be constant. */
5206 if (non_constant_p)
5207 *non_constant_p = false;
5208
5209 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5210 return NULL;
5211
5212 expression_list = make_tree_vector ();
5213
5214 /* Within a parenthesized expression, a `>' token is always
5215 the greater-than operator. */
5216 saved_greater_than_is_operator_p
5217 = parser->greater_than_is_operator_p;
5218 parser->greater_than_is_operator_p = true;
5219
5220 /* Consume expressions until there are no more. */
5221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5222 while (true)
5223 {
5224 tree expr;
5225
5226 /* At the beginning of attribute lists, check to see if the
5227 next token is an identifier. */
5228 if (is_attribute_list
5229 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5230 {
5231 cp_token *token;
5232
5233 /* Consume the identifier. */
5234 token = cp_lexer_consume_token (parser->lexer);
5235 /* Save the identifier. */
5236 identifier = token->u.value;
5237 }
5238 else
5239 {
5240 bool expr_non_constant_p;
5241
5242 /* Parse the next assignment-expression. */
5243 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5244 {
5245 /* A braced-init-list. */
5246 maybe_warn_cpp0x ("extended initializer lists");
5247 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5248 if (non_constant_p && expr_non_constant_p)
5249 *non_constant_p = true;
5250 }
5251 else if (non_constant_p)
5252 {
5253 expr = (cp_parser_constant_expression
5254 (parser, /*allow_non_constant_p=*/true,
5255 &expr_non_constant_p));
5256 if (expr_non_constant_p)
5257 *non_constant_p = true;
5258 }
5259 else
5260 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5261
5262 if (fold_expr_p)
5263 expr = fold_non_dependent_expr (expr);
5264
5265 /* If we have an ellipsis, then this is an expression
5266 expansion. */
5267 if (allow_expansion_p
5268 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5269 {
5270 /* Consume the `...'. */
5271 cp_lexer_consume_token (parser->lexer);
5272
5273 /* Build the argument pack. */
5274 expr = make_pack_expansion (expr);
5275 }
5276
5277 /* Add it to the list. We add error_mark_node
5278 expressions to the list, so that we can still tell if
5279 the correct form for a parenthesized expression-list
5280 is found. That gives better errors. */
5281 VEC_safe_push (tree, gc, expression_list, expr);
5282
5283 if (expr == error_mark_node)
5284 goto skip_comma;
5285 }
5286
5287 /* After the first item, attribute lists look the same as
5288 expression lists. */
5289 is_attribute_list = false;
5290
5291 get_comma:;
5292 /* If the next token isn't a `,', then we are done. */
5293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5294 break;
5295
5296 /* Otherwise, consume the `,' and keep going. */
5297 cp_lexer_consume_token (parser->lexer);
5298 }
5299
5300 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5301 {
5302 int ending;
5303
5304 skip_comma:;
5305 /* We try and resync to an unnested comma, as that will give the
5306 user better diagnostics. */
5307 ending = cp_parser_skip_to_closing_parenthesis (parser,
5308 /*recovering=*/true,
5309 /*or_comma=*/true,
5310 /*consume_paren=*/true);
5311 if (ending < 0)
5312 goto get_comma;
5313 if (!ending)
5314 {
5315 parser->greater_than_is_operator_p
5316 = saved_greater_than_is_operator_p;
5317 return NULL;
5318 }
5319 }
5320
5321 parser->greater_than_is_operator_p
5322 = saved_greater_than_is_operator_p;
5323
5324 if (identifier)
5325 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5326
5327 return expression_list;
5328 }
5329
5330 /* Parse a pseudo-destructor-name.
5331
5332 pseudo-destructor-name:
5333 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5334 :: [opt] nested-name-specifier template template-id :: ~ type-name
5335 :: [opt] nested-name-specifier [opt] ~ type-name
5336
5337 If either of the first two productions is used, sets *SCOPE to the
5338 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5339 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5340 or ERROR_MARK_NODE if the parse fails. */
5341
5342 static void
5343 cp_parser_pseudo_destructor_name (cp_parser* parser,
5344 tree* scope,
5345 tree* type)
5346 {
5347 bool nested_name_specifier_p;
5348
5349 /* Assume that things will not work out. */
5350 *type = error_mark_node;
5351
5352 /* Look for the optional `::' operator. */
5353 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5354 /* Look for the optional nested-name-specifier. */
5355 nested_name_specifier_p
5356 = (cp_parser_nested_name_specifier_opt (parser,
5357 /*typename_keyword_p=*/false,
5358 /*check_dependency_p=*/true,
5359 /*type_p=*/false,
5360 /*is_declaration=*/false)
5361 != NULL_TREE);
5362 /* Now, if we saw a nested-name-specifier, we might be doing the
5363 second production. */
5364 if (nested_name_specifier_p
5365 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5366 {
5367 /* Consume the `template' keyword. */
5368 cp_lexer_consume_token (parser->lexer);
5369 /* Parse the template-id. */
5370 cp_parser_template_id (parser,
5371 /*template_keyword_p=*/true,
5372 /*check_dependency_p=*/false,
5373 /*is_declaration=*/true);
5374 /* Look for the `::' token. */
5375 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5376 }
5377 /* If the next token is not a `~', then there might be some
5378 additional qualification. */
5379 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5380 {
5381 /* At this point, we're looking for "type-name :: ~". The type-name
5382 must not be a class-name, since this is a pseudo-destructor. So,
5383 it must be either an enum-name, or a typedef-name -- both of which
5384 are just identifiers. So, we peek ahead to check that the "::"
5385 and "~" tokens are present; if they are not, then we can avoid
5386 calling type_name. */
5387 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5388 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5389 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5390 {
5391 cp_parser_error (parser, "non-scalar type");
5392 return;
5393 }
5394
5395 /* Look for the type-name. */
5396 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5397 if (*scope == error_mark_node)
5398 return;
5399
5400 /* Look for the `::' token. */
5401 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5402 }
5403 else
5404 *scope = NULL_TREE;
5405
5406 /* Look for the `~'. */
5407 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5408 /* Look for the type-name again. We are not responsible for
5409 checking that it matches the first type-name. */
5410 *type = cp_parser_nonclass_name (parser);
5411 }
5412
5413 /* Parse a unary-expression.
5414
5415 unary-expression:
5416 postfix-expression
5417 ++ cast-expression
5418 -- cast-expression
5419 unary-operator cast-expression
5420 sizeof unary-expression
5421 sizeof ( type-id )
5422 new-expression
5423 delete-expression
5424
5425 GNU Extensions:
5426
5427 unary-expression:
5428 __extension__ cast-expression
5429 __alignof__ unary-expression
5430 __alignof__ ( type-id )
5431 __real__ cast-expression
5432 __imag__ cast-expression
5433 && identifier
5434
5435 ADDRESS_P is true iff the unary-expression is appearing as the
5436 operand of the `&' operator. CAST_P is true if this expression is
5437 the target of a cast.
5438
5439 Returns a representation of the expression. */
5440
5441 static tree
5442 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5443 cp_id_kind * pidk)
5444 {
5445 cp_token *token;
5446 enum tree_code unary_operator;
5447
5448 /* Peek at the next token. */
5449 token = cp_lexer_peek_token (parser->lexer);
5450 /* Some keywords give away the kind of expression. */
5451 if (token->type == CPP_KEYWORD)
5452 {
5453 enum rid keyword = token->keyword;
5454
5455 switch (keyword)
5456 {
5457 case RID_ALIGNOF:
5458 case RID_SIZEOF:
5459 {
5460 tree operand;
5461 enum tree_code op;
5462
5463 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5464 /* Consume the token. */
5465 cp_lexer_consume_token (parser->lexer);
5466 /* Parse the operand. */
5467 operand = cp_parser_sizeof_operand (parser, keyword);
5468
5469 if (TYPE_P (operand))
5470 return cxx_sizeof_or_alignof_type (operand, op, true);
5471 else
5472 return cxx_sizeof_or_alignof_expr (operand, op, true);
5473 }
5474
5475 case RID_NEW:
5476 return cp_parser_new_expression (parser);
5477
5478 case RID_DELETE:
5479 return cp_parser_delete_expression (parser);
5480
5481 case RID_EXTENSION:
5482 {
5483 /* The saved value of the PEDANTIC flag. */
5484 int saved_pedantic;
5485 tree expr;
5486
5487 /* Save away the PEDANTIC flag. */
5488 cp_parser_extension_opt (parser, &saved_pedantic);
5489 /* Parse the cast-expression. */
5490 expr = cp_parser_simple_cast_expression (parser);
5491 /* Restore the PEDANTIC flag. */
5492 pedantic = saved_pedantic;
5493
5494 return expr;
5495 }
5496
5497 case RID_REALPART:
5498 case RID_IMAGPART:
5499 {
5500 tree expression;
5501
5502 /* Consume the `__real__' or `__imag__' token. */
5503 cp_lexer_consume_token (parser->lexer);
5504 /* Parse the cast-expression. */
5505 expression = cp_parser_simple_cast_expression (parser);
5506 /* Create the complete representation. */
5507 return build_x_unary_op ((keyword == RID_REALPART
5508 ? REALPART_EXPR : IMAGPART_EXPR),
5509 expression,
5510 tf_warning_or_error);
5511 }
5512 break;
5513
5514 default:
5515 break;
5516 }
5517 }
5518
5519 /* Look for the `:: new' and `:: delete', which also signal the
5520 beginning of a new-expression, or delete-expression,
5521 respectively. If the next token is `::', then it might be one of
5522 these. */
5523 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5524 {
5525 enum rid keyword;
5526
5527 /* See if the token after the `::' is one of the keywords in
5528 which we're interested. */
5529 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5530 /* If it's `new', we have a new-expression. */
5531 if (keyword == RID_NEW)
5532 return cp_parser_new_expression (parser);
5533 /* Similarly, for `delete'. */
5534 else if (keyword == RID_DELETE)
5535 return cp_parser_delete_expression (parser);
5536 }
5537
5538 /* Look for a unary operator. */
5539 unary_operator = cp_parser_unary_operator (token);
5540 /* The `++' and `--' operators can be handled similarly, even though
5541 they are not technically unary-operators in the grammar. */
5542 if (unary_operator == ERROR_MARK)
5543 {
5544 if (token->type == CPP_PLUS_PLUS)
5545 unary_operator = PREINCREMENT_EXPR;
5546 else if (token->type == CPP_MINUS_MINUS)
5547 unary_operator = PREDECREMENT_EXPR;
5548 /* Handle the GNU address-of-label extension. */
5549 else if (cp_parser_allow_gnu_extensions_p (parser)
5550 && token->type == CPP_AND_AND)
5551 {
5552 tree identifier;
5553 tree expression;
5554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5555
5556 /* Consume the '&&' token. */
5557 cp_lexer_consume_token (parser->lexer);
5558 /* Look for the identifier. */
5559 identifier = cp_parser_identifier (parser);
5560 /* Create an expression representing the address. */
5561 expression = finish_label_address_expr (identifier, loc);
5562 if (cp_parser_non_integral_constant_expression (parser,
5563 "the address of a label"))
5564 expression = error_mark_node;
5565 return expression;
5566 }
5567 }
5568 if (unary_operator != ERROR_MARK)
5569 {
5570 tree cast_expression;
5571 tree expression = error_mark_node;
5572 const char *non_constant_p = NULL;
5573
5574 /* Consume the operator token. */
5575 token = cp_lexer_consume_token (parser->lexer);
5576 /* Parse the cast-expression. */
5577 cast_expression
5578 = cp_parser_cast_expression (parser,
5579 unary_operator == ADDR_EXPR,
5580 /*cast_p=*/false, pidk);
5581 /* Now, build an appropriate representation. */
5582 switch (unary_operator)
5583 {
5584 case INDIRECT_REF:
5585 non_constant_p = "%<*%>";
5586 expression = build_x_indirect_ref (cast_expression, "unary *",
5587 tf_warning_or_error);
5588 break;
5589
5590 case ADDR_EXPR:
5591 non_constant_p = "%<&%>";
5592 /* Fall through. */
5593 case BIT_NOT_EXPR:
5594 expression = build_x_unary_op (unary_operator, cast_expression,
5595 tf_warning_or_error);
5596 break;
5597
5598 case PREINCREMENT_EXPR:
5599 case PREDECREMENT_EXPR:
5600 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5601 ? "%<++%>" : "%<--%>");
5602 /* Fall through. */
5603 case UNARY_PLUS_EXPR:
5604 case NEGATE_EXPR:
5605 case TRUTH_NOT_EXPR:
5606 expression = finish_unary_op_expr (unary_operator, cast_expression);
5607 break;
5608
5609 default:
5610 gcc_unreachable ();
5611 }
5612
5613 if (non_constant_p
5614 && cp_parser_non_integral_constant_expression (parser,
5615 non_constant_p))
5616 expression = error_mark_node;
5617
5618 return expression;
5619 }
5620
5621 return cp_parser_postfix_expression (parser, address_p, cast_p,
5622 /*member_access_only_p=*/false,
5623 pidk);
5624 }
5625
5626 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5627 unary-operator, the corresponding tree code is returned. */
5628
5629 static enum tree_code
5630 cp_parser_unary_operator (cp_token* token)
5631 {
5632 switch (token->type)
5633 {
5634 case CPP_MULT:
5635 return INDIRECT_REF;
5636
5637 case CPP_AND:
5638 return ADDR_EXPR;
5639
5640 case CPP_PLUS:
5641 return UNARY_PLUS_EXPR;
5642
5643 case CPP_MINUS:
5644 return NEGATE_EXPR;
5645
5646 case CPP_NOT:
5647 return TRUTH_NOT_EXPR;
5648
5649 case CPP_COMPL:
5650 return BIT_NOT_EXPR;
5651
5652 default:
5653 return ERROR_MARK;
5654 }
5655 }
5656
5657 /* Parse a new-expression.
5658
5659 new-expression:
5660 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5661 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5662
5663 Returns a representation of the expression. */
5664
5665 static tree
5666 cp_parser_new_expression (cp_parser* parser)
5667 {
5668 bool global_scope_p;
5669 VEC(tree,gc) *placement;
5670 tree type;
5671 VEC(tree,gc) *initializer;
5672 tree nelts;
5673 tree ret;
5674
5675 /* Look for the optional `::' operator. */
5676 global_scope_p
5677 = (cp_parser_global_scope_opt (parser,
5678 /*current_scope_valid_p=*/false)
5679 != NULL_TREE);
5680 /* Look for the `new' operator. */
5681 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5682 /* There's no easy way to tell a new-placement from the
5683 `( type-id )' construct. */
5684 cp_parser_parse_tentatively (parser);
5685 /* Look for a new-placement. */
5686 placement = cp_parser_new_placement (parser);
5687 /* If that didn't work out, there's no new-placement. */
5688 if (!cp_parser_parse_definitely (parser))
5689 {
5690 if (placement != NULL)
5691 release_tree_vector (placement);
5692 placement = NULL;
5693 }
5694
5695 /* If the next token is a `(', then we have a parenthesized
5696 type-id. */
5697 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5698 {
5699 cp_token *token;
5700 /* Consume the `('. */
5701 cp_lexer_consume_token (parser->lexer);
5702 /* Parse the type-id. */
5703 type = cp_parser_type_id (parser);
5704 /* Look for the closing `)'. */
5705 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5706 token = cp_lexer_peek_token (parser->lexer);
5707 /* There should not be a direct-new-declarator in this production,
5708 but GCC used to allowed this, so we check and emit a sensible error
5709 message for this case. */
5710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5711 {
5712 error_at (token->location,
5713 "array bound forbidden after parenthesized type-id");
5714 inform (token->location,
5715 "try removing the parentheses around the type-id");
5716 cp_parser_direct_new_declarator (parser);
5717 }
5718 nelts = NULL_TREE;
5719 }
5720 /* Otherwise, there must be a new-type-id. */
5721 else
5722 type = cp_parser_new_type_id (parser, &nelts);
5723
5724 /* If the next token is a `(' or '{', then we have a new-initializer. */
5725 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5726 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5727 initializer = cp_parser_new_initializer (parser);
5728 else
5729 initializer = NULL;
5730
5731 /* A new-expression may not appear in an integral constant
5732 expression. */
5733 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5734 ret = error_mark_node;
5735 else
5736 {
5737 /* Create a representation of the new-expression. */
5738 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5739 tf_warning_or_error);
5740 }
5741
5742 if (placement != NULL)
5743 release_tree_vector (placement);
5744 if (initializer != NULL)
5745 release_tree_vector (initializer);
5746
5747 return ret;
5748 }
5749
5750 /* Parse a new-placement.
5751
5752 new-placement:
5753 ( expression-list )
5754
5755 Returns the same representation as for an expression-list. */
5756
5757 static VEC(tree,gc) *
5758 cp_parser_new_placement (cp_parser* parser)
5759 {
5760 VEC(tree,gc) *expression_list;
5761
5762 /* Parse the expression-list. */
5763 expression_list = (cp_parser_parenthesized_expression_list
5764 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5765 /*non_constant_p=*/NULL));
5766
5767 return expression_list;
5768 }
5769
5770 /* Parse a new-type-id.
5771
5772 new-type-id:
5773 type-specifier-seq new-declarator [opt]
5774
5775 Returns the TYPE allocated. If the new-type-id indicates an array
5776 type, *NELTS is set to the number of elements in the last array
5777 bound; the TYPE will not include the last array bound. */
5778
5779 static tree
5780 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5781 {
5782 cp_decl_specifier_seq type_specifier_seq;
5783 cp_declarator *new_declarator;
5784 cp_declarator *declarator;
5785 cp_declarator *outer_declarator;
5786 const char *saved_message;
5787 tree type;
5788
5789 /* The type-specifier sequence must not contain type definitions.
5790 (It cannot contain declarations of new types either, but if they
5791 are not definitions we will catch that because they are not
5792 complete.) */
5793 saved_message = parser->type_definition_forbidden_message;
5794 parser->type_definition_forbidden_message
5795 = "types may not be defined in a new-type-id";
5796 /* Parse the type-specifier-seq. */
5797 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5798 &type_specifier_seq);
5799 /* Restore the old message. */
5800 parser->type_definition_forbidden_message = saved_message;
5801 /* Parse the new-declarator. */
5802 new_declarator = cp_parser_new_declarator_opt (parser);
5803
5804 /* Determine the number of elements in the last array dimension, if
5805 any. */
5806 *nelts = NULL_TREE;
5807 /* Skip down to the last array dimension. */
5808 declarator = new_declarator;
5809 outer_declarator = NULL;
5810 while (declarator && (declarator->kind == cdk_pointer
5811 || declarator->kind == cdk_ptrmem))
5812 {
5813 outer_declarator = declarator;
5814 declarator = declarator->declarator;
5815 }
5816 while (declarator
5817 && declarator->kind == cdk_array
5818 && declarator->declarator
5819 && declarator->declarator->kind == cdk_array)
5820 {
5821 outer_declarator = declarator;
5822 declarator = declarator->declarator;
5823 }
5824
5825 if (declarator && declarator->kind == cdk_array)
5826 {
5827 *nelts = declarator->u.array.bounds;
5828 if (*nelts == error_mark_node)
5829 *nelts = integer_one_node;
5830
5831 if (outer_declarator)
5832 outer_declarator->declarator = declarator->declarator;
5833 else
5834 new_declarator = NULL;
5835 }
5836
5837 type = groktypename (&type_specifier_seq, new_declarator, false);
5838 return type;
5839 }
5840
5841 /* Parse an (optional) new-declarator.
5842
5843 new-declarator:
5844 ptr-operator new-declarator [opt]
5845 direct-new-declarator
5846
5847 Returns the declarator. */
5848
5849 static cp_declarator *
5850 cp_parser_new_declarator_opt (cp_parser* parser)
5851 {
5852 enum tree_code code;
5853 tree type;
5854 cp_cv_quals cv_quals;
5855
5856 /* We don't know if there's a ptr-operator next, or not. */
5857 cp_parser_parse_tentatively (parser);
5858 /* Look for a ptr-operator. */
5859 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5860 /* If that worked, look for more new-declarators. */
5861 if (cp_parser_parse_definitely (parser))
5862 {
5863 cp_declarator *declarator;
5864
5865 /* Parse another optional declarator. */
5866 declarator = cp_parser_new_declarator_opt (parser);
5867
5868 return cp_parser_make_indirect_declarator
5869 (code, type, cv_quals, declarator);
5870 }
5871
5872 /* If the next token is a `[', there is a direct-new-declarator. */
5873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5874 return cp_parser_direct_new_declarator (parser);
5875
5876 return NULL;
5877 }
5878
5879 /* Parse a direct-new-declarator.
5880
5881 direct-new-declarator:
5882 [ expression ]
5883 direct-new-declarator [constant-expression]
5884
5885 */
5886
5887 static cp_declarator *
5888 cp_parser_direct_new_declarator (cp_parser* parser)
5889 {
5890 cp_declarator *declarator = NULL;
5891
5892 while (true)
5893 {
5894 tree expression;
5895
5896 /* Look for the opening `['. */
5897 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5898 /* The first expression is not required to be constant. */
5899 if (!declarator)
5900 {
5901 cp_token *token = cp_lexer_peek_token (parser->lexer);
5902 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5903 /* The standard requires that the expression have integral
5904 type. DR 74 adds enumeration types. We believe that the
5905 real intent is that these expressions be handled like the
5906 expression in a `switch' condition, which also allows
5907 classes with a single conversion to integral or
5908 enumeration type. */
5909 if (!processing_template_decl)
5910 {
5911 expression
5912 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5913 expression,
5914 /*complain=*/true);
5915 if (!expression)
5916 {
5917 error_at (token->location,
5918 "expression in new-declarator must have integral "
5919 "or enumeration type");
5920 expression = error_mark_node;
5921 }
5922 }
5923 }
5924 /* But all the other expressions must be. */
5925 else
5926 expression
5927 = cp_parser_constant_expression (parser,
5928 /*allow_non_constant=*/false,
5929 NULL);
5930 /* Look for the closing `]'. */
5931 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5932
5933 /* Add this bound to the declarator. */
5934 declarator = make_array_declarator (declarator, expression);
5935
5936 /* If the next token is not a `[', then there are no more
5937 bounds. */
5938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5939 break;
5940 }
5941
5942 return declarator;
5943 }
5944
5945 /* Parse a new-initializer.
5946
5947 new-initializer:
5948 ( expression-list [opt] )
5949 braced-init-list
5950
5951 Returns a representation of the expression-list. */
5952
5953 static VEC(tree,gc) *
5954 cp_parser_new_initializer (cp_parser* parser)
5955 {
5956 VEC(tree,gc) *expression_list;
5957
5958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5959 {
5960 tree t;
5961 bool expr_non_constant_p;
5962 maybe_warn_cpp0x ("extended initializer lists");
5963 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5964 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5965 expression_list = make_tree_vector_single (t);
5966 }
5967 else
5968 expression_list = (cp_parser_parenthesized_expression_list
5969 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5970 /*non_constant_p=*/NULL));
5971
5972 return expression_list;
5973 }
5974
5975 /* Parse a delete-expression.
5976
5977 delete-expression:
5978 :: [opt] delete cast-expression
5979 :: [opt] delete [ ] cast-expression
5980
5981 Returns a representation of the expression. */
5982
5983 static tree
5984 cp_parser_delete_expression (cp_parser* parser)
5985 {
5986 bool global_scope_p;
5987 bool array_p;
5988 tree expression;
5989
5990 /* Look for the optional `::' operator. */
5991 global_scope_p
5992 = (cp_parser_global_scope_opt (parser,
5993 /*current_scope_valid_p=*/false)
5994 != NULL_TREE);
5995 /* Look for the `delete' keyword. */
5996 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5997 /* See if the array syntax is in use. */
5998 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5999 {
6000 /* Consume the `[' token. */
6001 cp_lexer_consume_token (parser->lexer);
6002 /* Look for the `]' token. */
6003 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6004 /* Remember that this is the `[]' construct. */
6005 array_p = true;
6006 }
6007 else
6008 array_p = false;
6009
6010 /* Parse the cast-expression. */
6011 expression = cp_parser_simple_cast_expression (parser);
6012
6013 /* A delete-expression may not appear in an integral constant
6014 expression. */
6015 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6016 return error_mark_node;
6017
6018 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6019 }
6020
6021 /* Returns true if TOKEN may start a cast-expression and false
6022 otherwise. */
6023
6024 static bool
6025 cp_parser_token_starts_cast_expression (cp_token *token)
6026 {
6027 switch (token->type)
6028 {
6029 case CPP_COMMA:
6030 case CPP_SEMICOLON:
6031 case CPP_QUERY:
6032 case CPP_COLON:
6033 case CPP_CLOSE_SQUARE:
6034 case CPP_CLOSE_PAREN:
6035 case CPP_CLOSE_BRACE:
6036 case CPP_DOT:
6037 case CPP_DOT_STAR:
6038 case CPP_DEREF:
6039 case CPP_DEREF_STAR:
6040 case CPP_DIV:
6041 case CPP_MOD:
6042 case CPP_LSHIFT:
6043 case CPP_RSHIFT:
6044 case CPP_LESS:
6045 case CPP_GREATER:
6046 case CPP_LESS_EQ:
6047 case CPP_GREATER_EQ:
6048 case CPP_EQ_EQ:
6049 case CPP_NOT_EQ:
6050 case CPP_EQ:
6051 case CPP_MULT_EQ:
6052 case CPP_DIV_EQ:
6053 case CPP_MOD_EQ:
6054 case CPP_PLUS_EQ:
6055 case CPP_MINUS_EQ:
6056 case CPP_RSHIFT_EQ:
6057 case CPP_LSHIFT_EQ:
6058 case CPP_AND_EQ:
6059 case CPP_XOR_EQ:
6060 case CPP_OR_EQ:
6061 case CPP_XOR:
6062 case CPP_OR:
6063 case CPP_OR_OR:
6064 case CPP_EOF:
6065 return false;
6066
6067 /* '[' may start a primary-expression in obj-c++. */
6068 case CPP_OPEN_SQUARE:
6069 return c_dialect_objc ();
6070
6071 default:
6072 return true;
6073 }
6074 }
6075
6076 /* Parse a cast-expression.
6077
6078 cast-expression:
6079 unary-expression
6080 ( type-id ) cast-expression
6081
6082 ADDRESS_P is true iff the unary-expression is appearing as the
6083 operand of the `&' operator. CAST_P is true if this expression is
6084 the target of a cast.
6085
6086 Returns a representation of the expression. */
6087
6088 static tree
6089 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6090 cp_id_kind * pidk)
6091 {
6092 /* If it's a `(', then we might be looking at a cast. */
6093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6094 {
6095 tree type = NULL_TREE;
6096 tree expr = NULL_TREE;
6097 bool compound_literal_p;
6098 const char *saved_message;
6099
6100 /* There's no way to know yet whether or not this is a cast.
6101 For example, `(int (3))' is a unary-expression, while `(int)
6102 3' is a cast. So, we resort to parsing tentatively. */
6103 cp_parser_parse_tentatively (parser);
6104 /* Types may not be defined in a cast. */
6105 saved_message = parser->type_definition_forbidden_message;
6106 parser->type_definition_forbidden_message
6107 = "types may not be defined in casts";
6108 /* Consume the `('. */
6109 cp_lexer_consume_token (parser->lexer);
6110 /* A very tricky bit is that `(struct S) { 3 }' is a
6111 compound-literal (which we permit in C++ as an extension).
6112 But, that construct is not a cast-expression -- it is a
6113 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6114 is legal; if the compound-literal were a cast-expression,
6115 you'd need an extra set of parentheses.) But, if we parse
6116 the type-id, and it happens to be a class-specifier, then we
6117 will commit to the parse at that point, because we cannot
6118 undo the action that is done when creating a new class. So,
6119 then we cannot back up and do a postfix-expression.
6120
6121 Therefore, we scan ahead to the closing `)', and check to see
6122 if the token after the `)' is a `{'. If so, we are not
6123 looking at a cast-expression.
6124
6125 Save tokens so that we can put them back. */
6126 cp_lexer_save_tokens (parser->lexer);
6127 /* Skip tokens until the next token is a closing parenthesis.
6128 If we find the closing `)', and the next token is a `{', then
6129 we are looking at a compound-literal. */
6130 compound_literal_p
6131 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6132 /*consume_paren=*/true)
6133 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6134 /* Roll back the tokens we skipped. */
6135 cp_lexer_rollback_tokens (parser->lexer);
6136 /* If we were looking at a compound-literal, simulate an error
6137 so that the call to cp_parser_parse_definitely below will
6138 fail. */
6139 if (compound_literal_p)
6140 cp_parser_simulate_error (parser);
6141 else
6142 {
6143 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6144 parser->in_type_id_in_expr_p = true;
6145 /* Look for the type-id. */
6146 type = cp_parser_type_id (parser);
6147 /* Look for the closing `)'. */
6148 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6149 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6150 }
6151
6152 /* Restore the saved message. */
6153 parser->type_definition_forbidden_message = saved_message;
6154
6155 /* At this point this can only be either a cast or a
6156 parenthesized ctor such as `(T ())' that looks like a cast to
6157 function returning T. */
6158 if (!cp_parser_error_occurred (parser)
6159 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6160 (parser->lexer)))
6161 {
6162 cp_parser_parse_definitely (parser);
6163 expr = cp_parser_cast_expression (parser,
6164 /*address_p=*/false,
6165 /*cast_p=*/true, pidk);
6166
6167 /* Warn about old-style casts, if so requested. */
6168 if (warn_old_style_cast
6169 && !in_system_header
6170 && !VOID_TYPE_P (type)
6171 && current_lang_name != lang_name_c)
6172 warning (OPT_Wold_style_cast, "use of old-style cast");
6173
6174 /* Only type conversions to integral or enumeration types
6175 can be used in constant-expressions. */
6176 if (!cast_valid_in_integral_constant_expression_p (type)
6177 && (cp_parser_non_integral_constant_expression
6178 (parser,
6179 "a cast to a type other than an integral or "
6180 "enumeration type")))
6181 return error_mark_node;
6182
6183 /* Perform the cast. */
6184 expr = build_c_cast (input_location, type, expr);
6185 return expr;
6186 }
6187 else
6188 cp_parser_abort_tentative_parse (parser);
6189 }
6190
6191 /* If we get here, then it's not a cast, so it must be a
6192 unary-expression. */
6193 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6194 }
6195
6196 /* Parse a binary expression of the general form:
6197
6198 pm-expression:
6199 cast-expression
6200 pm-expression .* cast-expression
6201 pm-expression ->* cast-expression
6202
6203 multiplicative-expression:
6204 pm-expression
6205 multiplicative-expression * pm-expression
6206 multiplicative-expression / pm-expression
6207 multiplicative-expression % pm-expression
6208
6209 additive-expression:
6210 multiplicative-expression
6211 additive-expression + multiplicative-expression
6212 additive-expression - multiplicative-expression
6213
6214 shift-expression:
6215 additive-expression
6216 shift-expression << additive-expression
6217 shift-expression >> additive-expression
6218
6219 relational-expression:
6220 shift-expression
6221 relational-expression < shift-expression
6222 relational-expression > shift-expression
6223 relational-expression <= shift-expression
6224 relational-expression >= shift-expression
6225
6226 GNU Extension:
6227
6228 relational-expression:
6229 relational-expression <? shift-expression
6230 relational-expression >? shift-expression
6231
6232 equality-expression:
6233 relational-expression
6234 equality-expression == relational-expression
6235 equality-expression != relational-expression
6236
6237 and-expression:
6238 equality-expression
6239 and-expression & equality-expression
6240
6241 exclusive-or-expression:
6242 and-expression
6243 exclusive-or-expression ^ and-expression
6244
6245 inclusive-or-expression:
6246 exclusive-or-expression
6247 inclusive-or-expression | exclusive-or-expression
6248
6249 logical-and-expression:
6250 inclusive-or-expression
6251 logical-and-expression && inclusive-or-expression
6252
6253 logical-or-expression:
6254 logical-and-expression
6255 logical-or-expression || logical-and-expression
6256
6257 All these are implemented with a single function like:
6258
6259 binary-expression:
6260 simple-cast-expression
6261 binary-expression <token> binary-expression
6262
6263 CAST_P is true if this expression is the target of a cast.
6264
6265 The binops_by_token map is used to get the tree codes for each <token> type.
6266 binary-expressions are associated according to a precedence table. */
6267
6268 #define TOKEN_PRECEDENCE(token) \
6269 (((token->type == CPP_GREATER \
6270 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6271 && !parser->greater_than_is_operator_p) \
6272 ? PREC_NOT_OPERATOR \
6273 : binops_by_token[token->type].prec)
6274
6275 static tree
6276 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6277 bool no_toplevel_fold_p,
6278 enum cp_parser_prec prec,
6279 cp_id_kind * pidk)
6280 {
6281 cp_parser_expression_stack stack;
6282 cp_parser_expression_stack_entry *sp = &stack[0];
6283 tree lhs, rhs;
6284 cp_token *token;
6285 enum tree_code tree_type, lhs_type, rhs_type;
6286 enum cp_parser_prec new_prec, lookahead_prec;
6287 bool overloaded_p;
6288
6289 /* Parse the first expression. */
6290 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6291 lhs_type = ERROR_MARK;
6292
6293 for (;;)
6294 {
6295 /* Get an operator token. */
6296 token = cp_lexer_peek_token (parser->lexer);
6297
6298 if (warn_cxx0x_compat
6299 && token->type == CPP_RSHIFT
6300 && !parser->greater_than_is_operator_p)
6301 {
6302 if (warning_at (token->location, OPT_Wc__0x_compat,
6303 "%<>>%> operator will be treated as"
6304 " two right angle brackets in C++0x"))
6305 inform (token->location,
6306 "suggest parentheses around %<>>%> expression");
6307 }
6308
6309 new_prec = TOKEN_PRECEDENCE (token);
6310
6311 /* Popping an entry off the stack means we completed a subexpression:
6312 - either we found a token which is not an operator (`>' where it is not
6313 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6314 will happen repeatedly;
6315 - or, we found an operator which has lower priority. This is the case
6316 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6317 parsing `3 * 4'. */
6318 if (new_prec <= prec)
6319 {
6320 if (sp == stack)
6321 break;
6322 else
6323 goto pop;
6324 }
6325
6326 get_rhs:
6327 tree_type = binops_by_token[token->type].tree_type;
6328
6329 /* We used the operator token. */
6330 cp_lexer_consume_token (parser->lexer);
6331
6332 /* For "false && x" or "true || x", x will never be executed;
6333 disable warnings while evaluating it. */
6334 if (tree_type == TRUTH_ANDIF_EXPR)
6335 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6336 else if (tree_type == TRUTH_ORIF_EXPR)
6337 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6338
6339 /* Extract another operand. It may be the RHS of this expression
6340 or the LHS of a new, higher priority expression. */
6341 rhs = cp_parser_simple_cast_expression (parser);
6342 rhs_type = ERROR_MARK;
6343
6344 /* Get another operator token. Look up its precedence to avoid
6345 building a useless (immediately popped) stack entry for common
6346 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6347 token = cp_lexer_peek_token (parser->lexer);
6348 lookahead_prec = TOKEN_PRECEDENCE (token);
6349 if (lookahead_prec > new_prec)
6350 {
6351 /* ... and prepare to parse the RHS of the new, higher priority
6352 expression. Since precedence levels on the stack are
6353 monotonically increasing, we do not have to care about
6354 stack overflows. */
6355 sp->prec = prec;
6356 sp->tree_type = tree_type;
6357 sp->lhs = lhs;
6358 sp->lhs_type = lhs_type;
6359 sp++;
6360 lhs = rhs;
6361 lhs_type = rhs_type;
6362 prec = new_prec;
6363 new_prec = lookahead_prec;
6364 goto get_rhs;
6365
6366 pop:
6367 lookahead_prec = new_prec;
6368 /* If the stack is not empty, we have parsed into LHS the right side
6369 (`4' in the example above) of an expression we had suspended.
6370 We can use the information on the stack to recover the LHS (`3')
6371 from the stack together with the tree code (`MULT_EXPR'), and
6372 the precedence of the higher level subexpression
6373 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6374 which will be used to actually build the additive expression. */
6375 --sp;
6376 prec = sp->prec;
6377 tree_type = sp->tree_type;
6378 rhs = lhs;
6379 rhs_type = lhs_type;
6380 lhs = sp->lhs;
6381 lhs_type = sp->lhs_type;
6382 }
6383
6384 /* Undo the disabling of warnings done above. */
6385 if (tree_type == TRUTH_ANDIF_EXPR)
6386 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6387 else if (tree_type == TRUTH_ORIF_EXPR)
6388 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6389
6390 overloaded_p = false;
6391 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6392 ERROR_MARK for everything that is not a binary expression.
6393 This makes warn_about_parentheses miss some warnings that
6394 involve unary operators. For unary expressions we should
6395 pass the correct tree_code unless the unary expression was
6396 surrounded by parentheses.
6397 */
6398 if (no_toplevel_fold_p
6399 && lookahead_prec <= prec
6400 && sp == stack
6401 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6402 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6403 else
6404 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6405 &overloaded_p, tf_warning_or_error);
6406 lhs_type = tree_type;
6407
6408 /* If the binary operator required the use of an overloaded operator,
6409 then this expression cannot be an integral constant-expression.
6410 An overloaded operator can be used even if both operands are
6411 otherwise permissible in an integral constant-expression if at
6412 least one of the operands is of enumeration type. */
6413
6414 if (overloaded_p
6415 && (cp_parser_non_integral_constant_expression
6416 (parser, "calls to overloaded operators")))
6417 return error_mark_node;
6418 }
6419
6420 return lhs;
6421 }
6422
6423
6424 /* Parse the `? expression : assignment-expression' part of a
6425 conditional-expression. The LOGICAL_OR_EXPR is the
6426 logical-or-expression that started the conditional-expression.
6427 Returns a representation of the entire conditional-expression.
6428
6429 This routine is used by cp_parser_assignment_expression.
6430
6431 ? expression : assignment-expression
6432
6433 GNU Extensions:
6434
6435 ? : assignment-expression */
6436
6437 static tree
6438 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6439 {
6440 tree expr;
6441 tree assignment_expr;
6442
6443 /* Consume the `?' token. */
6444 cp_lexer_consume_token (parser->lexer);
6445 if (cp_parser_allow_gnu_extensions_p (parser)
6446 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6447 {
6448 /* Implicit true clause. */
6449 expr = NULL_TREE;
6450 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6451 }
6452 else
6453 {
6454 /* Parse the expression. */
6455 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6456 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6457 c_inhibit_evaluation_warnings +=
6458 ((logical_or_expr == truthvalue_true_node)
6459 - (logical_or_expr == truthvalue_false_node));
6460 }
6461
6462 /* The next token should be a `:'. */
6463 cp_parser_require (parser, CPP_COLON, "%<:%>");
6464 /* Parse the assignment-expression. */
6465 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6466 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6467
6468 /* Build the conditional-expression. */
6469 return build_x_conditional_expr (logical_or_expr,
6470 expr,
6471 assignment_expr,
6472 tf_warning_or_error);
6473 }
6474
6475 /* Parse an assignment-expression.
6476
6477 assignment-expression:
6478 conditional-expression
6479 logical-or-expression assignment-operator assignment_expression
6480 throw-expression
6481
6482 CAST_P is true if this expression is the target of a cast.
6483
6484 Returns a representation for the expression. */
6485
6486 static tree
6487 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6488 cp_id_kind * pidk)
6489 {
6490 tree expr;
6491
6492 /* If the next token is the `throw' keyword, then we're looking at
6493 a throw-expression. */
6494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6495 expr = cp_parser_throw_expression (parser);
6496 /* Otherwise, it must be that we are looking at a
6497 logical-or-expression. */
6498 else
6499 {
6500 /* Parse the binary expressions (logical-or-expression). */
6501 expr = cp_parser_binary_expression (parser, cast_p, false,
6502 PREC_NOT_OPERATOR, pidk);
6503 /* If the next token is a `?' then we're actually looking at a
6504 conditional-expression. */
6505 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6506 return cp_parser_question_colon_clause (parser, expr);
6507 else
6508 {
6509 enum tree_code assignment_operator;
6510
6511 /* If it's an assignment-operator, we're using the second
6512 production. */
6513 assignment_operator
6514 = cp_parser_assignment_operator_opt (parser);
6515 if (assignment_operator != ERROR_MARK)
6516 {
6517 bool non_constant_p;
6518
6519 /* Parse the right-hand side of the assignment. */
6520 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6521
6522 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6523 maybe_warn_cpp0x ("extended initializer lists");
6524
6525 /* An assignment may not appear in a
6526 constant-expression. */
6527 if (cp_parser_non_integral_constant_expression (parser,
6528 "an assignment"))
6529 return error_mark_node;
6530 /* Build the assignment expression. */
6531 expr = build_x_modify_expr (expr,
6532 assignment_operator,
6533 rhs,
6534 tf_warning_or_error);
6535 }
6536 }
6537 }
6538
6539 return expr;
6540 }
6541
6542 /* Parse an (optional) assignment-operator.
6543
6544 assignment-operator: one of
6545 = *= /= %= += -= >>= <<= &= ^= |=
6546
6547 GNU Extension:
6548
6549 assignment-operator: one of
6550 <?= >?=
6551
6552 If the next token is an assignment operator, the corresponding tree
6553 code is returned, and the token is consumed. For example, for
6554 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6555 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6556 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6557 operator, ERROR_MARK is returned. */
6558
6559 static enum tree_code
6560 cp_parser_assignment_operator_opt (cp_parser* parser)
6561 {
6562 enum tree_code op;
6563 cp_token *token;
6564
6565 /* Peek at the next token. */
6566 token = cp_lexer_peek_token (parser->lexer);
6567
6568 switch (token->type)
6569 {
6570 case CPP_EQ:
6571 op = NOP_EXPR;
6572 break;
6573
6574 case CPP_MULT_EQ:
6575 op = MULT_EXPR;
6576 break;
6577
6578 case CPP_DIV_EQ:
6579 op = TRUNC_DIV_EXPR;
6580 break;
6581
6582 case CPP_MOD_EQ:
6583 op = TRUNC_MOD_EXPR;
6584 break;
6585
6586 case CPP_PLUS_EQ:
6587 op = PLUS_EXPR;
6588 break;
6589
6590 case CPP_MINUS_EQ:
6591 op = MINUS_EXPR;
6592 break;
6593
6594 case CPP_RSHIFT_EQ:
6595 op = RSHIFT_EXPR;
6596 break;
6597
6598 case CPP_LSHIFT_EQ:
6599 op = LSHIFT_EXPR;
6600 break;
6601
6602 case CPP_AND_EQ:
6603 op = BIT_AND_EXPR;
6604 break;
6605
6606 case CPP_XOR_EQ:
6607 op = BIT_XOR_EXPR;
6608 break;
6609
6610 case CPP_OR_EQ:
6611 op = BIT_IOR_EXPR;
6612 break;
6613
6614 default:
6615 /* Nothing else is an assignment operator. */
6616 op = ERROR_MARK;
6617 }
6618
6619 /* If it was an assignment operator, consume it. */
6620 if (op != ERROR_MARK)
6621 cp_lexer_consume_token (parser->lexer);
6622
6623 return op;
6624 }
6625
6626 /* Parse an expression.
6627
6628 expression:
6629 assignment-expression
6630 expression , assignment-expression
6631
6632 CAST_P is true if this expression is the target of a cast.
6633
6634 Returns a representation of the expression. */
6635
6636 static tree
6637 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6638 {
6639 tree expression = NULL_TREE;
6640
6641 while (true)
6642 {
6643 tree assignment_expression;
6644
6645 /* Parse the next assignment-expression. */
6646 assignment_expression
6647 = cp_parser_assignment_expression (parser, cast_p, pidk);
6648 /* If this is the first assignment-expression, we can just
6649 save it away. */
6650 if (!expression)
6651 expression = assignment_expression;
6652 else
6653 expression = build_x_compound_expr (expression,
6654 assignment_expression,
6655 tf_warning_or_error);
6656 /* If the next token is not a comma, then we are done with the
6657 expression. */
6658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6659 break;
6660 /* Consume the `,'. */
6661 cp_lexer_consume_token (parser->lexer);
6662 /* A comma operator cannot appear in a constant-expression. */
6663 if (cp_parser_non_integral_constant_expression (parser,
6664 "a comma operator"))
6665 expression = error_mark_node;
6666 }
6667
6668 return expression;
6669 }
6670
6671 /* Parse a constant-expression.
6672
6673 constant-expression:
6674 conditional-expression
6675
6676 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6677 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6678 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6679 is false, NON_CONSTANT_P should be NULL. */
6680
6681 static tree
6682 cp_parser_constant_expression (cp_parser* parser,
6683 bool allow_non_constant_p,
6684 bool *non_constant_p)
6685 {
6686 bool saved_integral_constant_expression_p;
6687 bool saved_allow_non_integral_constant_expression_p;
6688 bool saved_non_integral_constant_expression_p;
6689 tree expression;
6690
6691 /* It might seem that we could simply parse the
6692 conditional-expression, and then check to see if it were
6693 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6694 one that the compiler can figure out is constant, possibly after
6695 doing some simplifications or optimizations. The standard has a
6696 precise definition of constant-expression, and we must honor
6697 that, even though it is somewhat more restrictive.
6698
6699 For example:
6700
6701 int i[(2, 3)];
6702
6703 is not a legal declaration, because `(2, 3)' is not a
6704 constant-expression. The `,' operator is forbidden in a
6705 constant-expression. However, GCC's constant-folding machinery
6706 will fold this operation to an INTEGER_CST for `3'. */
6707
6708 /* Save the old settings. */
6709 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6710 saved_allow_non_integral_constant_expression_p
6711 = parser->allow_non_integral_constant_expression_p;
6712 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6713 /* We are now parsing a constant-expression. */
6714 parser->integral_constant_expression_p = true;
6715 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6716 parser->non_integral_constant_expression_p = false;
6717 /* Although the grammar says "conditional-expression", we parse an
6718 "assignment-expression", which also permits "throw-expression"
6719 and the use of assignment operators. In the case that
6720 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6721 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6722 actually essential that we look for an assignment-expression.
6723 For example, cp_parser_initializer_clauses uses this function to
6724 determine whether a particular assignment-expression is in fact
6725 constant. */
6726 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6727 /* Restore the old settings. */
6728 parser->integral_constant_expression_p
6729 = saved_integral_constant_expression_p;
6730 parser->allow_non_integral_constant_expression_p
6731 = saved_allow_non_integral_constant_expression_p;
6732 if (allow_non_constant_p)
6733 *non_constant_p = parser->non_integral_constant_expression_p;
6734 else if (parser->non_integral_constant_expression_p)
6735 expression = error_mark_node;
6736 parser->non_integral_constant_expression_p
6737 = saved_non_integral_constant_expression_p;
6738
6739 return expression;
6740 }
6741
6742 /* Parse __builtin_offsetof.
6743
6744 offsetof-expression:
6745 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6746
6747 offsetof-member-designator:
6748 id-expression
6749 | offsetof-member-designator "." id-expression
6750 | offsetof-member-designator "[" expression "]"
6751 | offsetof-member-designator "->" id-expression */
6752
6753 static tree
6754 cp_parser_builtin_offsetof (cp_parser *parser)
6755 {
6756 int save_ice_p, save_non_ice_p;
6757 tree type, expr;
6758 cp_id_kind dummy;
6759 cp_token *token;
6760
6761 /* We're about to accept non-integral-constant things, but will
6762 definitely yield an integral constant expression. Save and
6763 restore these values around our local parsing. */
6764 save_ice_p = parser->integral_constant_expression_p;
6765 save_non_ice_p = parser->non_integral_constant_expression_p;
6766
6767 /* Consume the "__builtin_offsetof" token. */
6768 cp_lexer_consume_token (parser->lexer);
6769 /* Consume the opening `('. */
6770 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6771 /* Parse the type-id. */
6772 type = cp_parser_type_id (parser);
6773 /* Look for the `,'. */
6774 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6775 token = cp_lexer_peek_token (parser->lexer);
6776
6777 /* Build the (type *)null that begins the traditional offsetof macro. */
6778 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6779 tf_warning_or_error);
6780
6781 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6782 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6783 true, &dummy, token->location);
6784 while (true)
6785 {
6786 token = cp_lexer_peek_token (parser->lexer);
6787 switch (token->type)
6788 {
6789 case CPP_OPEN_SQUARE:
6790 /* offsetof-member-designator "[" expression "]" */
6791 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6792 break;
6793
6794 case CPP_DEREF:
6795 /* offsetof-member-designator "->" identifier */
6796 expr = grok_array_decl (expr, integer_zero_node);
6797 /* FALLTHRU */
6798
6799 case CPP_DOT:
6800 /* offsetof-member-designator "." identifier */
6801 cp_lexer_consume_token (parser->lexer);
6802 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6803 expr, true, &dummy,
6804 token->location);
6805 break;
6806
6807 case CPP_CLOSE_PAREN:
6808 /* Consume the ")" token. */
6809 cp_lexer_consume_token (parser->lexer);
6810 goto success;
6811
6812 default:
6813 /* Error. We know the following require will fail, but
6814 that gives the proper error message. */
6815 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6816 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6817 expr = error_mark_node;
6818 goto failure;
6819 }
6820 }
6821
6822 success:
6823 /* If we're processing a template, we can't finish the semantics yet.
6824 Otherwise we can fold the entire expression now. */
6825 if (processing_template_decl)
6826 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6827 else
6828 expr = finish_offsetof (expr);
6829
6830 failure:
6831 parser->integral_constant_expression_p = save_ice_p;
6832 parser->non_integral_constant_expression_p = save_non_ice_p;
6833
6834 return expr;
6835 }
6836
6837 /* Parse a trait expression. */
6838
6839 static tree
6840 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6841 {
6842 cp_trait_kind kind;
6843 tree type1, type2 = NULL_TREE;
6844 bool binary = false;
6845 cp_decl_specifier_seq decl_specs;
6846
6847 switch (keyword)
6848 {
6849 case RID_HAS_NOTHROW_ASSIGN:
6850 kind = CPTK_HAS_NOTHROW_ASSIGN;
6851 break;
6852 case RID_HAS_NOTHROW_CONSTRUCTOR:
6853 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6854 break;
6855 case RID_HAS_NOTHROW_COPY:
6856 kind = CPTK_HAS_NOTHROW_COPY;
6857 break;
6858 case RID_HAS_TRIVIAL_ASSIGN:
6859 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6860 break;
6861 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6862 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6863 break;
6864 case RID_HAS_TRIVIAL_COPY:
6865 kind = CPTK_HAS_TRIVIAL_COPY;
6866 break;
6867 case RID_HAS_TRIVIAL_DESTRUCTOR:
6868 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6869 break;
6870 case RID_HAS_VIRTUAL_DESTRUCTOR:
6871 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6872 break;
6873 case RID_IS_ABSTRACT:
6874 kind = CPTK_IS_ABSTRACT;
6875 break;
6876 case RID_IS_BASE_OF:
6877 kind = CPTK_IS_BASE_OF;
6878 binary = true;
6879 break;
6880 case RID_IS_CLASS:
6881 kind = CPTK_IS_CLASS;
6882 break;
6883 case RID_IS_CONVERTIBLE_TO:
6884 kind = CPTK_IS_CONVERTIBLE_TO;
6885 binary = true;
6886 break;
6887 case RID_IS_EMPTY:
6888 kind = CPTK_IS_EMPTY;
6889 break;
6890 case RID_IS_ENUM:
6891 kind = CPTK_IS_ENUM;
6892 break;
6893 case RID_IS_POD:
6894 kind = CPTK_IS_POD;
6895 break;
6896 case RID_IS_POLYMORPHIC:
6897 kind = CPTK_IS_POLYMORPHIC;
6898 break;
6899 case RID_IS_STD_LAYOUT:
6900 kind = CPTK_IS_STD_LAYOUT;
6901 break;
6902 case RID_IS_TRIVIAL:
6903 kind = CPTK_IS_TRIVIAL;
6904 break;
6905 case RID_IS_UNION:
6906 kind = CPTK_IS_UNION;
6907 break;
6908 default:
6909 gcc_unreachable ();
6910 }
6911
6912 /* Consume the token. */
6913 cp_lexer_consume_token (parser->lexer);
6914
6915 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6916
6917 type1 = cp_parser_type_id (parser);
6918
6919 if (type1 == error_mark_node)
6920 return error_mark_node;
6921
6922 /* Build a trivial decl-specifier-seq. */
6923 clear_decl_specs (&decl_specs);
6924 decl_specs.type = type1;
6925
6926 /* Call grokdeclarator to figure out what type this is. */
6927 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6928 /*initialized=*/0, /*attrlist=*/NULL);
6929
6930 if (binary)
6931 {
6932 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6933
6934 type2 = cp_parser_type_id (parser);
6935
6936 if (type2 == error_mark_node)
6937 return error_mark_node;
6938
6939 /* Build a trivial decl-specifier-seq. */
6940 clear_decl_specs (&decl_specs);
6941 decl_specs.type = type2;
6942
6943 /* Call grokdeclarator to figure out what type this is. */
6944 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6945 /*initialized=*/0, /*attrlist=*/NULL);
6946 }
6947
6948 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6949
6950 /* Complete the trait expression, which may mean either processing
6951 the trait expr now or saving it for template instantiation. */
6952 return finish_trait_expr (kind, type1, type2);
6953 }
6954
6955 /* Lambdas that appear in variable initializer or default argument scope
6956 get that in their mangling, so we need to record it. We might as well
6957 use the count for function and namespace scopes as well. */
6958 static GTY(()) tree lambda_scope;
6959 static GTY(()) int lambda_count;
6960 typedef struct GTY(()) tree_int
6961 {
6962 tree t;
6963 int i;
6964 } tree_int;
6965 DEF_VEC_O(tree_int);
6966 DEF_VEC_ALLOC_O(tree_int,gc);
6967 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6968
6969 static void
6970 start_lambda_scope (tree decl)
6971 {
6972 tree_int ti;
6973 gcc_assert (decl);
6974 /* Once we're inside a function, we ignore other scopes and just push
6975 the function again so that popping works properly. */
6976 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6977 decl = current_function_decl;
6978 ti.t = lambda_scope;
6979 ti.i = lambda_count;
6980 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6981 if (lambda_scope != decl)
6982 {
6983 /* Don't reset the count if we're still in the same function. */
6984 lambda_scope = decl;
6985 lambda_count = 0;
6986 }
6987 }
6988
6989 static void
6990 record_lambda_scope (tree lambda)
6991 {
6992 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
6993 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
6994 }
6995
6996 static void
6997 finish_lambda_scope (void)
6998 {
6999 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7000 if (lambda_scope != p->t)
7001 {
7002 lambda_scope = p->t;
7003 lambda_count = p->i;
7004 }
7005 VEC_pop (tree_int, lambda_scope_stack);
7006 }
7007
7008 /* Parse a lambda expression.
7009
7010 lambda-expression:
7011 lambda-introducer lambda-declarator [opt] compound-statement
7012
7013 Returns a representation of the expression. */
7014
7015 static tree
7016 cp_parser_lambda_expression (cp_parser* parser)
7017 {
7018 tree lambda_expr = build_lambda_expr ();
7019 tree type;
7020
7021 LAMBDA_EXPR_LOCATION (lambda_expr)
7022 = cp_lexer_peek_token (parser->lexer)->location;
7023
7024 /* We may be in the middle of deferred access check. Disable
7025 it now. */
7026 push_deferring_access_checks (dk_no_deferred);
7027
7028 type = begin_lambda_type (lambda_expr);
7029
7030 record_lambda_scope (lambda_expr);
7031
7032 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7033 determine_visibility (TYPE_NAME (type));
7034
7035 {
7036 /* Inside the class, surrounding template-parameter-lists do not apply. */
7037 unsigned int saved_num_template_parameter_lists
7038 = parser->num_template_parameter_lists;
7039
7040 parser->num_template_parameter_lists = 0;
7041
7042 cp_parser_lambda_introducer (parser, lambda_expr);
7043
7044 /* By virtue of defining a local class, a lambda expression has access to
7045 the private variables of enclosing classes. */
7046
7047 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7048
7049 cp_parser_lambda_body (parser, lambda_expr);
7050
7051 /* The capture list was built up in reverse order; fix that now. */
7052 {
7053 tree newlist = NULL_TREE;
7054 tree elt, next;
7055
7056 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7057 elt; elt = next)
7058 {
7059 tree field = TREE_PURPOSE (elt);
7060 char *buf;
7061
7062 next = TREE_CHAIN (elt);
7063 TREE_CHAIN (elt) = newlist;
7064 newlist = elt;
7065
7066 /* Also add __ to the beginning of the field name so that code
7067 outside the lambda body can't see the captured name. We could
7068 just remove the name entirely, but this is more useful for
7069 debugging. */
7070 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7071 /* The 'this' capture already starts with __. */
7072 continue;
7073
7074 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7075 buf[1] = buf[0] = '_';
7076 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7077 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7078 DECL_NAME (field) = get_identifier (buf);
7079 }
7080 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7081 }
7082
7083 type = finish_struct (type, /*attributes=*/NULL_TREE);
7084
7085 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7086 }
7087
7088 pop_deferring_access_checks ();
7089
7090 return build_lambda_object (lambda_expr);
7091 }
7092
7093 /* Parse the beginning of a lambda expression.
7094
7095 lambda-introducer:
7096 [ lambda-capture [opt] ]
7097
7098 LAMBDA_EXPR is the current representation of the lambda expression. */
7099
7100 static void
7101 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7102 {
7103 /* Need commas after the first capture. */
7104 bool first = true;
7105
7106 /* Eat the leading `['. */
7107 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7108
7109 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7110 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7111 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7112 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7113 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7114 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7115
7116 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7117 {
7118 cp_lexer_consume_token (parser->lexer);
7119 first = false;
7120 }
7121
7122 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7123 {
7124 cp_token* capture_token;
7125 tree capture_id;
7126 tree capture_init_expr;
7127 cp_id_kind idk = CP_ID_KIND_NONE;
7128 bool explicit_init_p = false;
7129
7130 enum capture_kind_type
7131 {
7132 BY_COPY,
7133 BY_REFERENCE
7134 };
7135 enum capture_kind_type capture_kind = BY_COPY;
7136
7137 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7138 {
7139 error ("expected end of capture-list");
7140 return;
7141 }
7142
7143 if (first)
7144 first = false;
7145 else
7146 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7147
7148 /* Possibly capture `this'. */
7149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7150 {
7151 cp_lexer_consume_token (parser->lexer);
7152 add_capture (lambda_expr,
7153 /*id=*/get_identifier ("__this"),
7154 /*initializer=*/finish_this_expr(),
7155 /*by_reference_p=*/false,
7156 explicit_init_p);
7157 continue;
7158 }
7159
7160 /* Remember whether we want to capture as a reference or not. */
7161 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7162 {
7163 capture_kind = BY_REFERENCE;
7164 cp_lexer_consume_token (parser->lexer);
7165 }
7166
7167 /* Get the identifier. */
7168 capture_token = cp_lexer_peek_token (parser->lexer);
7169 capture_id = cp_parser_identifier (parser);
7170
7171 if (capture_id == error_mark_node)
7172 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7173 delimiters, but I modified this to stop on unnested ']' as well. It
7174 was already changed to stop on unnested '}', so the
7175 "closing_parenthesis" name is no more misleading with my change. */
7176 {
7177 cp_parser_skip_to_closing_parenthesis (parser,
7178 /*recovering=*/true,
7179 /*or_comma=*/true,
7180 /*consume_paren=*/true);
7181 break;
7182 }
7183
7184 /* Find the initializer for this capture. */
7185 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7186 {
7187 /* An explicit expression exists. */
7188 cp_lexer_consume_token (parser->lexer);
7189 pedwarn (input_location, OPT_pedantic,
7190 "ISO C++ does not allow initializers "
7191 "in lambda expression capture lists");
7192 capture_init_expr = cp_parser_assignment_expression (parser,
7193 /*cast_p=*/true,
7194 &idk);
7195 explicit_init_p = true;
7196 }
7197 else
7198 {
7199 const char* error_msg;
7200
7201 /* Turn the identifier into an id-expression. */
7202 capture_init_expr
7203 = cp_parser_lookup_name
7204 (parser,
7205 capture_id,
7206 none_type,
7207 /*is_template=*/false,
7208 /*is_namespace=*/false,
7209 /*check_dependency=*/true,
7210 /*ambiguous_decls=*/NULL,
7211 capture_token->location);
7212
7213 capture_init_expr
7214 = finish_id_expression
7215 (capture_id,
7216 capture_init_expr,
7217 parser->scope,
7218 &idk,
7219 /*integral_constant_expression_p=*/false,
7220 /*allow_non_integral_constant_expression_p=*/false,
7221 /*non_integral_constant_expression_p=*/NULL,
7222 /*template_p=*/false,
7223 /*done=*/true,
7224 /*address_p=*/false,
7225 /*template_arg_p=*/false,
7226 &error_msg,
7227 capture_token->location);
7228 }
7229
7230 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7231 capture_init_expr
7232 = unqualified_name_lookup_error (capture_init_expr);
7233
7234 add_capture (lambda_expr,
7235 capture_id,
7236 capture_init_expr,
7237 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7238 explicit_init_p);
7239 }
7240
7241 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7242 }
7243
7244 /* Parse the (optional) middle of a lambda expression.
7245
7246 lambda-declarator:
7247 ( parameter-declaration-clause [opt] )
7248 attribute-specifier [opt]
7249 mutable [opt]
7250 exception-specification [opt]
7251 lambda-return-type-clause [opt]
7252
7253 LAMBDA_EXPR is the current representation of the lambda expression. */
7254
7255 static void
7256 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7257 {
7258 /* 5.1.1.4 of the standard says:
7259 If a lambda-expression does not include a lambda-declarator, it is as if
7260 the lambda-declarator were ().
7261 This means an empty parameter list, no attributes, and no exception
7262 specification. */
7263 tree param_list = void_list_node;
7264 tree attributes = NULL_TREE;
7265 tree exception_spec = NULL_TREE;
7266 tree t;
7267
7268 /* The lambda-declarator is optional, but must begin with an opening
7269 parenthesis if present. */
7270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7271 {
7272 cp_lexer_consume_token (parser->lexer);
7273
7274 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7275
7276 /* Parse parameters. */
7277 param_list = cp_parser_parameter_declaration_clause (parser);
7278
7279 /* Default arguments shall not be specified in the
7280 parameter-declaration-clause of a lambda-declarator. */
7281 for (t = param_list; t; t = TREE_CHAIN (t))
7282 if (TREE_PURPOSE (t))
7283 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7284 "default argument specified for lambda parameter");
7285
7286 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7287
7288 attributes = cp_parser_attributes_opt (parser);
7289
7290 /* Parse optional `mutable' keyword. */
7291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7292 {
7293 cp_lexer_consume_token (parser->lexer);
7294 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7295 }
7296
7297 /* Parse optional exception specification. */
7298 exception_spec = cp_parser_exception_specification_opt (parser);
7299
7300 /* Parse optional trailing return type. */
7301 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7302 {
7303 cp_lexer_consume_token (parser->lexer);
7304 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7305 }
7306
7307 /* The function parameters must be in scope all the way until after the
7308 trailing-return-type in case of decltype. */
7309 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7310 pop_binding (DECL_NAME (t), t);
7311
7312 leave_scope ();
7313 }
7314
7315 /* Create the function call operator.
7316
7317 Messing with declarators like this is no uglier than building up the
7318 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7319 other code. */
7320 {
7321 cp_decl_specifier_seq return_type_specs;
7322 cp_declarator* declarator;
7323 tree fco;
7324 int quals;
7325 void *p;
7326
7327 clear_decl_specs (&return_type_specs);
7328 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7329 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7330 else
7331 /* Maybe we will deduce the return type later, but we can use void
7332 as a placeholder return type anyways. */
7333 return_type_specs.type = void_type_node;
7334
7335 p = obstack_alloc (&declarator_obstack, 0);
7336
7337 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7338 sfk_none);
7339
7340 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7341 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7342 declarator = make_call_declarator (declarator, param_list, quals,
7343 exception_spec,
7344 /*late_return_type=*/NULL_TREE);
7345
7346 fco = grokmethod (&return_type_specs,
7347 declarator,
7348 attributes);
7349 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7350 DECL_ARTIFICIAL (fco) = 1;
7351
7352 finish_member_declaration (fco);
7353
7354 obstack_free (&declarator_obstack, p);
7355 }
7356 }
7357
7358 /* Parse the body of a lambda expression, which is simply
7359
7360 compound-statement
7361
7362 but which requires special handling.
7363 LAMBDA_EXPR is the current representation of the lambda expression. */
7364
7365 static void
7366 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7367 {
7368 bool nested = (current_function_decl != NULL_TREE);
7369 if (nested)
7370 push_function_context ();
7371
7372 /* Finish the function call operator
7373 - class_specifier
7374 + late_parsing_for_member
7375 + function_definition_after_declarator
7376 + ctor_initializer_opt_and_function_body */
7377 {
7378 tree fco = lambda_function (lambda_expr);
7379 tree body;
7380 bool done = false;
7381
7382 /* Let the front end know that we are going to be defining this
7383 function. */
7384 start_preparsed_function (fco,
7385 NULL_TREE,
7386 SF_PRE_PARSED | SF_INCLASS_INLINE);
7387
7388 start_lambda_scope (fco);
7389 body = begin_function_body ();
7390
7391 /* 5.1.1.4 of the standard says:
7392 If a lambda-expression does not include a trailing-return-type, it
7393 is as if the trailing-return-type denotes the following type:
7394 * if the compound-statement is of the form
7395 { return attribute-specifier [opt] expression ; }
7396 the type of the returned expression after lvalue-to-rvalue
7397 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7398 (_conv.array_ 4.2), and function-to-pointer conversion
7399 (_conv.func_ 4.3);
7400 * otherwise, void. */
7401
7402 /* In a lambda that has neither a lambda-return-type-clause
7403 nor a deducible form, errors should be reported for return statements
7404 in the body. Since we used void as the placeholder return type, parsing
7405 the body as usual will give such desired behavior. */
7406 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7407 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7408 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7409 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7410 {
7411 tree compound_stmt;
7412 tree expr = NULL_TREE;
7413 cp_id_kind idk = CP_ID_KIND_NONE;
7414
7415 /* Parse tentatively in case there's more after the initial return
7416 statement. */
7417 cp_parser_parse_tentatively (parser);
7418
7419 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7420 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7421
7422 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7423
7424 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7425 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7426
7427 if (cp_parser_parse_definitely (parser))
7428 {
7429 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7430
7431 compound_stmt = begin_compound_stmt (0);
7432 /* Will get error here if type not deduced yet. */
7433 finish_return_stmt (expr);
7434 finish_compound_stmt (compound_stmt);
7435
7436 done = true;
7437 }
7438 }
7439
7440 if (!done)
7441 {
7442 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7443 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7444 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7445 cp_parser_compound_stmt does not pass it. */
7446 cp_parser_function_body (parser);
7447 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7448 }
7449
7450 finish_function_body (body);
7451 finish_lambda_scope ();
7452
7453 /* Finish the function and generate code for it if necessary. */
7454 expand_or_defer_fn (finish_function (/*inline*/2));
7455 }
7456
7457 if (nested)
7458 pop_function_context();
7459 }
7460
7461 /* Statements [gram.stmt.stmt] */
7462
7463 /* Parse a statement.
7464
7465 statement:
7466 labeled-statement
7467 expression-statement
7468 compound-statement
7469 selection-statement
7470 iteration-statement
7471 jump-statement
7472 declaration-statement
7473 try-block
7474
7475 IN_COMPOUND is true when the statement is nested inside a
7476 cp_parser_compound_statement; this matters for certain pragmas.
7477
7478 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7479 is a (possibly labeled) if statement which is not enclosed in braces
7480 and has an else clause. This is used to implement -Wparentheses. */
7481
7482 static void
7483 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7484 bool in_compound, bool *if_p)
7485 {
7486 tree statement;
7487 cp_token *token;
7488 location_t statement_location;
7489
7490 restart:
7491 if (if_p != NULL)
7492 *if_p = false;
7493 /* There is no statement yet. */
7494 statement = NULL_TREE;
7495 /* Peek at the next token. */
7496 token = cp_lexer_peek_token (parser->lexer);
7497 /* Remember the location of the first token in the statement. */
7498 statement_location = token->location;
7499 /* If this is a keyword, then that will often determine what kind of
7500 statement we have. */
7501 if (token->type == CPP_KEYWORD)
7502 {
7503 enum rid keyword = token->keyword;
7504
7505 switch (keyword)
7506 {
7507 case RID_CASE:
7508 case RID_DEFAULT:
7509 /* Looks like a labeled-statement with a case label.
7510 Parse the label, and then use tail recursion to parse
7511 the statement. */
7512 cp_parser_label_for_labeled_statement (parser);
7513 goto restart;
7514
7515 case RID_IF:
7516 case RID_SWITCH:
7517 statement = cp_parser_selection_statement (parser, if_p);
7518 break;
7519
7520 case RID_WHILE:
7521 case RID_DO:
7522 case RID_FOR:
7523 statement = cp_parser_iteration_statement (parser);
7524 break;
7525
7526 case RID_BREAK:
7527 case RID_CONTINUE:
7528 case RID_RETURN:
7529 case RID_GOTO:
7530 statement = cp_parser_jump_statement (parser);
7531 break;
7532
7533 /* Objective-C++ exception-handling constructs. */
7534 case RID_AT_TRY:
7535 case RID_AT_CATCH:
7536 case RID_AT_FINALLY:
7537 case RID_AT_SYNCHRONIZED:
7538 case RID_AT_THROW:
7539 statement = cp_parser_objc_statement (parser);
7540 break;
7541
7542 case RID_TRY:
7543 statement = cp_parser_try_block (parser);
7544 break;
7545
7546 case RID_NAMESPACE:
7547 /* This must be a namespace alias definition. */
7548 cp_parser_declaration_statement (parser);
7549 return;
7550
7551 default:
7552 /* It might be a keyword like `int' that can start a
7553 declaration-statement. */
7554 break;
7555 }
7556 }
7557 else if (token->type == CPP_NAME)
7558 {
7559 /* If the next token is a `:', then we are looking at a
7560 labeled-statement. */
7561 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7562 if (token->type == CPP_COLON)
7563 {
7564 /* Looks like a labeled-statement with an ordinary label.
7565 Parse the label, and then use tail recursion to parse
7566 the statement. */
7567 cp_parser_label_for_labeled_statement (parser);
7568 goto restart;
7569 }
7570 }
7571 /* Anything that starts with a `{' must be a compound-statement. */
7572 else if (token->type == CPP_OPEN_BRACE)
7573 statement = cp_parser_compound_statement (parser, NULL, false);
7574 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7575 a statement all its own. */
7576 else if (token->type == CPP_PRAGMA)
7577 {
7578 /* Only certain OpenMP pragmas are attached to statements, and thus
7579 are considered statements themselves. All others are not. In
7580 the context of a compound, accept the pragma as a "statement" and
7581 return so that we can check for a close brace. Otherwise we
7582 require a real statement and must go back and read one. */
7583 if (in_compound)
7584 cp_parser_pragma (parser, pragma_compound);
7585 else if (!cp_parser_pragma (parser, pragma_stmt))
7586 goto restart;
7587 return;
7588 }
7589 else if (token->type == CPP_EOF)
7590 {
7591 cp_parser_error (parser, "expected statement");
7592 return;
7593 }
7594
7595 /* Everything else must be a declaration-statement or an
7596 expression-statement. Try for the declaration-statement
7597 first, unless we are looking at a `;', in which case we know that
7598 we have an expression-statement. */
7599 if (!statement)
7600 {
7601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7602 {
7603 cp_parser_parse_tentatively (parser);
7604 /* Try to parse the declaration-statement. */
7605 cp_parser_declaration_statement (parser);
7606 /* If that worked, we're done. */
7607 if (cp_parser_parse_definitely (parser))
7608 return;
7609 }
7610 /* Look for an expression-statement instead. */
7611 statement = cp_parser_expression_statement (parser, in_statement_expr);
7612 }
7613
7614 /* Set the line number for the statement. */
7615 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7616 SET_EXPR_LOCATION (statement, statement_location);
7617 }
7618
7619 /* Parse the label for a labeled-statement, i.e.
7620
7621 identifier :
7622 case constant-expression :
7623 default :
7624
7625 GNU Extension:
7626 case constant-expression ... constant-expression : statement
7627
7628 When a label is parsed without errors, the label is added to the
7629 parse tree by the finish_* functions, so this function doesn't
7630 have to return the label. */
7631
7632 static void
7633 cp_parser_label_for_labeled_statement (cp_parser* parser)
7634 {
7635 cp_token *token;
7636 tree label = NULL_TREE;
7637
7638 /* The next token should be an identifier. */
7639 token = cp_lexer_peek_token (parser->lexer);
7640 if (token->type != CPP_NAME
7641 && token->type != CPP_KEYWORD)
7642 {
7643 cp_parser_error (parser, "expected labeled-statement");
7644 return;
7645 }
7646
7647 switch (token->keyword)
7648 {
7649 case RID_CASE:
7650 {
7651 tree expr, expr_hi;
7652 cp_token *ellipsis;
7653
7654 /* Consume the `case' token. */
7655 cp_lexer_consume_token (parser->lexer);
7656 /* Parse the constant-expression. */
7657 expr = cp_parser_constant_expression (parser,
7658 /*allow_non_constant_p=*/false,
7659 NULL);
7660
7661 ellipsis = cp_lexer_peek_token (parser->lexer);
7662 if (ellipsis->type == CPP_ELLIPSIS)
7663 {
7664 /* Consume the `...' token. */
7665 cp_lexer_consume_token (parser->lexer);
7666 expr_hi =
7667 cp_parser_constant_expression (parser,
7668 /*allow_non_constant_p=*/false,
7669 NULL);
7670 /* We don't need to emit warnings here, as the common code
7671 will do this for us. */
7672 }
7673 else
7674 expr_hi = NULL_TREE;
7675
7676 if (parser->in_switch_statement_p)
7677 finish_case_label (token->location, expr, expr_hi);
7678 else
7679 error_at (token->location,
7680 "case label %qE not within a switch statement",
7681 expr);
7682 }
7683 break;
7684
7685 case RID_DEFAULT:
7686 /* Consume the `default' token. */
7687 cp_lexer_consume_token (parser->lexer);
7688
7689 if (parser->in_switch_statement_p)
7690 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7691 else
7692 error_at (token->location, "case label not within a switch statement");
7693 break;
7694
7695 default:
7696 /* Anything else must be an ordinary label. */
7697 label = finish_label_stmt (cp_parser_identifier (parser));
7698 break;
7699 }
7700
7701 /* Require the `:' token. */
7702 cp_parser_require (parser, CPP_COLON, "%<:%>");
7703
7704 /* An ordinary label may optionally be followed by attributes.
7705 However, this is only permitted if the attributes are then
7706 followed by a semicolon. This is because, for backward
7707 compatibility, when parsing
7708 lab: __attribute__ ((unused)) int i;
7709 we want the attribute to attach to "i", not "lab". */
7710 if (label != NULL_TREE
7711 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7712 {
7713 tree attrs;
7714
7715 cp_parser_parse_tentatively (parser);
7716 attrs = cp_parser_attributes_opt (parser);
7717 if (attrs == NULL_TREE
7718 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7719 cp_parser_abort_tentative_parse (parser);
7720 else if (!cp_parser_parse_definitely (parser))
7721 ;
7722 else
7723 cplus_decl_attributes (&label, attrs, 0);
7724 }
7725 }
7726
7727 /* Parse an expression-statement.
7728
7729 expression-statement:
7730 expression [opt] ;
7731
7732 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7733 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7734 indicates whether this expression-statement is part of an
7735 expression statement. */
7736
7737 static tree
7738 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7739 {
7740 tree statement = NULL_TREE;
7741
7742 /* If the next token is a ';', then there is no expression
7743 statement. */
7744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7745 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7746
7747 /* Consume the final `;'. */
7748 cp_parser_consume_semicolon_at_end_of_statement (parser);
7749
7750 if (in_statement_expr
7751 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7752 /* This is the final expression statement of a statement
7753 expression. */
7754 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7755 else if (statement)
7756 statement = finish_expr_stmt (statement);
7757 else
7758 finish_stmt ();
7759
7760 return statement;
7761 }
7762
7763 /* Parse a compound-statement.
7764
7765 compound-statement:
7766 { statement-seq [opt] }
7767
7768 GNU extension:
7769
7770 compound-statement:
7771 { label-declaration-seq [opt] statement-seq [opt] }
7772
7773 label-declaration-seq:
7774 label-declaration
7775 label-declaration-seq label-declaration
7776
7777 Returns a tree representing the statement. */
7778
7779 static tree
7780 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7781 bool in_try)
7782 {
7783 tree compound_stmt;
7784
7785 /* Consume the `{'. */
7786 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7787 return error_mark_node;
7788 /* Begin the compound-statement. */
7789 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7790 /* If the next keyword is `__label__' we have a label declaration. */
7791 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7792 cp_parser_label_declaration (parser);
7793 /* Parse an (optional) statement-seq. */
7794 cp_parser_statement_seq_opt (parser, in_statement_expr);
7795 /* Finish the compound-statement. */
7796 finish_compound_stmt (compound_stmt);
7797 /* Consume the `}'. */
7798 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7799
7800 return compound_stmt;
7801 }
7802
7803 /* Parse an (optional) statement-seq.
7804
7805 statement-seq:
7806 statement
7807 statement-seq [opt] statement */
7808
7809 static void
7810 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7811 {
7812 /* Scan statements until there aren't any more. */
7813 while (true)
7814 {
7815 cp_token *token = cp_lexer_peek_token (parser->lexer);
7816
7817 /* If we're looking at a `}', then we've run out of statements. */
7818 if (token->type == CPP_CLOSE_BRACE
7819 || token->type == CPP_EOF
7820 || token->type == CPP_PRAGMA_EOL)
7821 break;
7822
7823 /* If we are in a compound statement and find 'else' then
7824 something went wrong. */
7825 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7826 {
7827 if (parser->in_statement & IN_IF_STMT)
7828 break;
7829 else
7830 {
7831 token = cp_lexer_consume_token (parser->lexer);
7832 error_at (token->location, "%<else%> without a previous %<if%>");
7833 }
7834 }
7835
7836 /* Parse the statement. */
7837 cp_parser_statement (parser, in_statement_expr, true, NULL);
7838 }
7839 }
7840
7841 /* Parse a selection-statement.
7842
7843 selection-statement:
7844 if ( condition ) statement
7845 if ( condition ) statement else statement
7846 switch ( condition ) statement
7847
7848 Returns the new IF_STMT or SWITCH_STMT.
7849
7850 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7851 is a (possibly labeled) if statement which is not enclosed in
7852 braces and has an else clause. This is used to implement
7853 -Wparentheses. */
7854
7855 static tree
7856 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7857 {
7858 cp_token *token;
7859 enum rid keyword;
7860
7861 if (if_p != NULL)
7862 *if_p = false;
7863
7864 /* Peek at the next token. */
7865 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7866
7867 /* See what kind of keyword it is. */
7868 keyword = token->keyword;
7869 switch (keyword)
7870 {
7871 case RID_IF:
7872 case RID_SWITCH:
7873 {
7874 tree statement;
7875 tree condition;
7876
7877 /* Look for the `('. */
7878 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7879 {
7880 cp_parser_skip_to_end_of_statement (parser);
7881 return error_mark_node;
7882 }
7883
7884 /* Begin the selection-statement. */
7885 if (keyword == RID_IF)
7886 statement = begin_if_stmt ();
7887 else
7888 statement = begin_switch_stmt ();
7889
7890 /* Parse the condition. */
7891 condition = cp_parser_condition (parser);
7892 /* Look for the `)'. */
7893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7894 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7895 /*consume_paren=*/true);
7896
7897 if (keyword == RID_IF)
7898 {
7899 bool nested_if;
7900 unsigned char in_statement;
7901
7902 /* Add the condition. */
7903 finish_if_stmt_cond (condition, statement);
7904
7905 /* Parse the then-clause. */
7906 in_statement = parser->in_statement;
7907 parser->in_statement |= IN_IF_STMT;
7908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7909 {
7910 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7911 add_stmt (build_empty_stmt (loc));
7912 cp_lexer_consume_token (parser->lexer);
7913 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7914 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7915 "empty body in an %<if%> statement");
7916 nested_if = false;
7917 }
7918 else
7919 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7920 parser->in_statement = in_statement;
7921
7922 finish_then_clause (statement);
7923
7924 /* If the next token is `else', parse the else-clause. */
7925 if (cp_lexer_next_token_is_keyword (parser->lexer,
7926 RID_ELSE))
7927 {
7928 /* Consume the `else' keyword. */
7929 cp_lexer_consume_token (parser->lexer);
7930 begin_else_clause (statement);
7931 /* Parse the else-clause. */
7932 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7933 {
7934 location_t loc;
7935 loc = cp_lexer_peek_token (parser->lexer)->location;
7936 warning_at (loc,
7937 OPT_Wempty_body, "suggest braces around "
7938 "empty body in an %<else%> statement");
7939 add_stmt (build_empty_stmt (loc));
7940 cp_lexer_consume_token (parser->lexer);
7941 }
7942 else
7943 cp_parser_implicitly_scoped_statement (parser, NULL);
7944
7945 finish_else_clause (statement);
7946
7947 /* If we are currently parsing a then-clause, then
7948 IF_P will not be NULL. We set it to true to
7949 indicate that this if statement has an else clause.
7950 This may trigger the Wparentheses warning below
7951 when we get back up to the parent if statement. */
7952 if (if_p != NULL)
7953 *if_p = true;
7954 }
7955 else
7956 {
7957 /* This if statement does not have an else clause. If
7958 NESTED_IF is true, then the then-clause is an if
7959 statement which does have an else clause. We warn
7960 about the potential ambiguity. */
7961 if (nested_if)
7962 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7963 "suggest explicit braces to avoid ambiguous"
7964 " %<else%>");
7965 }
7966
7967 /* Now we're all done with the if-statement. */
7968 finish_if_stmt (statement);
7969 }
7970 else
7971 {
7972 bool in_switch_statement_p;
7973 unsigned char in_statement;
7974
7975 /* Add the condition. */
7976 finish_switch_cond (condition, statement);
7977
7978 /* Parse the body of the switch-statement. */
7979 in_switch_statement_p = parser->in_switch_statement_p;
7980 in_statement = parser->in_statement;
7981 parser->in_switch_statement_p = true;
7982 parser->in_statement |= IN_SWITCH_STMT;
7983 cp_parser_implicitly_scoped_statement (parser, NULL);
7984 parser->in_switch_statement_p = in_switch_statement_p;
7985 parser->in_statement = in_statement;
7986
7987 /* Now we're all done with the switch-statement. */
7988 finish_switch_stmt (statement);
7989 }
7990
7991 return statement;
7992 }
7993 break;
7994
7995 default:
7996 cp_parser_error (parser, "expected selection-statement");
7997 return error_mark_node;
7998 }
7999 }
8000
8001 /* Parse a condition.
8002
8003 condition:
8004 expression
8005 type-specifier-seq declarator = initializer-clause
8006 type-specifier-seq declarator braced-init-list
8007
8008 GNU Extension:
8009
8010 condition:
8011 type-specifier-seq declarator asm-specification [opt]
8012 attributes [opt] = assignment-expression
8013
8014 Returns the expression that should be tested. */
8015
8016 static tree
8017 cp_parser_condition (cp_parser* parser)
8018 {
8019 cp_decl_specifier_seq type_specifiers;
8020 const char *saved_message;
8021
8022 /* Try the declaration first. */
8023 cp_parser_parse_tentatively (parser);
8024 /* New types are not allowed in the type-specifier-seq for a
8025 condition. */
8026 saved_message = parser->type_definition_forbidden_message;
8027 parser->type_definition_forbidden_message
8028 = "types may not be defined in conditions";
8029 /* Parse the type-specifier-seq. */
8030 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8031 &type_specifiers);
8032 /* Restore the saved message. */
8033 parser->type_definition_forbidden_message = saved_message;
8034 /* If all is well, we might be looking at a declaration. */
8035 if (!cp_parser_error_occurred (parser))
8036 {
8037 tree decl;
8038 tree asm_specification;
8039 tree attributes;
8040 cp_declarator *declarator;
8041 tree initializer = NULL_TREE;
8042
8043 /* Parse the declarator. */
8044 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8045 /*ctor_dtor_or_conv_p=*/NULL,
8046 /*parenthesized_p=*/NULL,
8047 /*member_p=*/false);
8048 /* Parse the attributes. */
8049 attributes = cp_parser_attributes_opt (parser);
8050 /* Parse the asm-specification. */
8051 asm_specification = cp_parser_asm_specification_opt (parser);
8052 /* If the next token is not an `=' or '{', then we might still be
8053 looking at an expression. For example:
8054
8055 if (A(a).x)
8056
8057 looks like a decl-specifier-seq and a declarator -- but then
8058 there is no `=', so this is an expression. */
8059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8060 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8061 cp_parser_simulate_error (parser);
8062
8063 /* If we did see an `=' or '{', then we are looking at a declaration
8064 for sure. */
8065 if (cp_parser_parse_definitely (parser))
8066 {
8067 tree pushed_scope;
8068 bool non_constant_p;
8069 bool flags = LOOKUP_ONLYCONVERTING;
8070
8071 /* Create the declaration. */
8072 decl = start_decl (declarator, &type_specifiers,
8073 /*initialized_p=*/true,
8074 attributes, /*prefix_attributes=*/NULL_TREE,
8075 &pushed_scope);
8076
8077 /* Parse the initializer. */
8078 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8079 {
8080 initializer = cp_parser_braced_list (parser, &non_constant_p);
8081 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8082 flags = 0;
8083 }
8084 else
8085 {
8086 /* Consume the `='. */
8087 cp_parser_require (parser, CPP_EQ, "%<=%>");
8088 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8089 }
8090 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8091 maybe_warn_cpp0x ("extended initializer lists");
8092
8093 if (!non_constant_p)
8094 initializer = fold_non_dependent_expr (initializer);
8095
8096 /* Process the initializer. */
8097 cp_finish_decl (decl,
8098 initializer, !non_constant_p,
8099 asm_specification,
8100 flags);
8101
8102 if (pushed_scope)
8103 pop_scope (pushed_scope);
8104
8105 return convert_from_reference (decl);
8106 }
8107 }
8108 /* If we didn't even get past the declarator successfully, we are
8109 definitely not looking at a declaration. */
8110 else
8111 cp_parser_abort_tentative_parse (parser);
8112
8113 /* Otherwise, we are looking at an expression. */
8114 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8115 }
8116
8117 /* Parse an iteration-statement.
8118
8119 iteration-statement:
8120 while ( condition ) statement
8121 do statement while ( expression ) ;
8122 for ( for-init-statement condition [opt] ; expression [opt] )
8123 statement
8124
8125 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8126
8127 static tree
8128 cp_parser_iteration_statement (cp_parser* parser)
8129 {
8130 cp_token *token;
8131 enum rid keyword;
8132 tree statement;
8133 unsigned char in_statement;
8134
8135 /* Peek at the next token. */
8136 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8137 if (!token)
8138 return error_mark_node;
8139
8140 /* Remember whether or not we are already within an iteration
8141 statement. */
8142 in_statement = parser->in_statement;
8143
8144 /* See what kind of keyword it is. */
8145 keyword = token->keyword;
8146 switch (keyword)
8147 {
8148 case RID_WHILE:
8149 {
8150 tree condition;
8151
8152 /* Begin the while-statement. */
8153 statement = begin_while_stmt ();
8154 /* Look for the `('. */
8155 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8156 /* Parse the condition. */
8157 condition = cp_parser_condition (parser);
8158 finish_while_stmt_cond (condition, statement);
8159 /* Look for the `)'. */
8160 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8161 /* Parse the dependent statement. */
8162 parser->in_statement = IN_ITERATION_STMT;
8163 cp_parser_already_scoped_statement (parser);
8164 parser->in_statement = in_statement;
8165 /* We're done with the while-statement. */
8166 finish_while_stmt (statement);
8167 }
8168 break;
8169
8170 case RID_DO:
8171 {
8172 tree expression;
8173
8174 /* Begin the do-statement. */
8175 statement = begin_do_stmt ();
8176 /* Parse the body of the do-statement. */
8177 parser->in_statement = IN_ITERATION_STMT;
8178 cp_parser_implicitly_scoped_statement (parser, NULL);
8179 parser->in_statement = in_statement;
8180 finish_do_body (statement);
8181 /* Look for the `while' keyword. */
8182 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8183 /* Look for the `('. */
8184 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8185 /* Parse the expression. */
8186 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8187 /* We're done with the do-statement. */
8188 finish_do_stmt (expression, statement);
8189 /* Look for the `)'. */
8190 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8191 /* Look for the `;'. */
8192 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8193 }
8194 break;
8195
8196 case RID_FOR:
8197 {
8198 tree condition = NULL_TREE;
8199 tree expression = NULL_TREE;
8200
8201 /* Begin the for-statement. */
8202 statement = begin_for_stmt ();
8203 /* Look for the `('. */
8204 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8205 /* Parse the initialization. */
8206 cp_parser_for_init_statement (parser);
8207 finish_for_init_stmt (statement);
8208
8209 /* If there's a condition, process it. */
8210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8211 condition = cp_parser_condition (parser);
8212 finish_for_cond (condition, statement);
8213 /* Look for the `;'. */
8214 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8215
8216 /* If there's an expression, process it. */
8217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8218 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8219 finish_for_expr (expression, statement);
8220 /* Look for the `)'. */
8221 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8222
8223 /* Parse the body of the for-statement. */
8224 parser->in_statement = IN_ITERATION_STMT;
8225 cp_parser_already_scoped_statement (parser);
8226 parser->in_statement = in_statement;
8227
8228 /* We're done with the for-statement. */
8229 finish_for_stmt (statement);
8230 }
8231 break;
8232
8233 default:
8234 cp_parser_error (parser, "expected iteration-statement");
8235 statement = error_mark_node;
8236 break;
8237 }
8238
8239 return statement;
8240 }
8241
8242 /* Parse a for-init-statement.
8243
8244 for-init-statement:
8245 expression-statement
8246 simple-declaration */
8247
8248 static void
8249 cp_parser_for_init_statement (cp_parser* parser)
8250 {
8251 /* If the next token is a `;', then we have an empty
8252 expression-statement. Grammatically, this is also a
8253 simple-declaration, but an invalid one, because it does not
8254 declare anything. Therefore, if we did not handle this case
8255 specially, we would issue an error message about an invalid
8256 declaration. */
8257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8258 {
8259 /* We're going to speculatively look for a declaration, falling back
8260 to an expression, if necessary. */
8261 cp_parser_parse_tentatively (parser);
8262 /* Parse the declaration. */
8263 cp_parser_simple_declaration (parser,
8264 /*function_definition_allowed_p=*/false);
8265 /* If the tentative parse failed, then we shall need to look for an
8266 expression-statement. */
8267 if (cp_parser_parse_definitely (parser))
8268 return;
8269 }
8270
8271 cp_parser_expression_statement (parser, false);
8272 }
8273
8274 /* Parse a jump-statement.
8275
8276 jump-statement:
8277 break ;
8278 continue ;
8279 return expression [opt] ;
8280 return braced-init-list ;
8281 goto identifier ;
8282
8283 GNU extension:
8284
8285 jump-statement:
8286 goto * expression ;
8287
8288 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8289
8290 static tree
8291 cp_parser_jump_statement (cp_parser* parser)
8292 {
8293 tree statement = error_mark_node;
8294 cp_token *token;
8295 enum rid keyword;
8296 unsigned char in_statement;
8297
8298 /* Peek at the next token. */
8299 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8300 if (!token)
8301 return error_mark_node;
8302
8303 /* See what kind of keyword it is. */
8304 keyword = token->keyword;
8305 switch (keyword)
8306 {
8307 case RID_BREAK:
8308 in_statement = parser->in_statement & ~IN_IF_STMT;
8309 switch (in_statement)
8310 {
8311 case 0:
8312 error_at (token->location, "break statement not within loop or switch");
8313 break;
8314 default:
8315 gcc_assert ((in_statement & IN_SWITCH_STMT)
8316 || in_statement == IN_ITERATION_STMT);
8317 statement = finish_break_stmt ();
8318 break;
8319 case IN_OMP_BLOCK:
8320 error_at (token->location, "invalid exit from OpenMP structured block");
8321 break;
8322 case IN_OMP_FOR:
8323 error_at (token->location, "break statement used with OpenMP for loop");
8324 break;
8325 }
8326 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8327 break;
8328
8329 case RID_CONTINUE:
8330 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8331 {
8332 case 0:
8333 error_at (token->location, "continue statement not within a loop");
8334 break;
8335 case IN_ITERATION_STMT:
8336 case IN_OMP_FOR:
8337 statement = finish_continue_stmt ();
8338 break;
8339 case IN_OMP_BLOCK:
8340 error_at (token->location, "invalid exit from OpenMP structured block");
8341 break;
8342 default:
8343 gcc_unreachable ();
8344 }
8345 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8346 break;
8347
8348 case RID_RETURN:
8349 {
8350 tree expr;
8351 bool expr_non_constant_p;
8352
8353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8354 {
8355 maybe_warn_cpp0x ("extended initializer lists");
8356 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8357 }
8358 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8359 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8360 else
8361 /* If the next token is a `;', then there is no
8362 expression. */
8363 expr = NULL_TREE;
8364 /* Build the return-statement. */
8365 statement = finish_return_stmt (expr);
8366 /* Look for the final `;'. */
8367 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8368 }
8369 break;
8370
8371 case RID_GOTO:
8372 /* Create the goto-statement. */
8373 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8374 {
8375 /* Issue a warning about this use of a GNU extension. */
8376 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8377 /* Consume the '*' token. */
8378 cp_lexer_consume_token (parser->lexer);
8379 /* Parse the dependent expression. */
8380 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8381 }
8382 else
8383 finish_goto_stmt (cp_parser_identifier (parser));
8384 /* Look for the final `;'. */
8385 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8386 break;
8387
8388 default:
8389 cp_parser_error (parser, "expected jump-statement");
8390 break;
8391 }
8392
8393 return statement;
8394 }
8395
8396 /* Parse a declaration-statement.
8397
8398 declaration-statement:
8399 block-declaration */
8400
8401 static void
8402 cp_parser_declaration_statement (cp_parser* parser)
8403 {
8404 void *p;
8405
8406 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8407 p = obstack_alloc (&declarator_obstack, 0);
8408
8409 /* Parse the block-declaration. */
8410 cp_parser_block_declaration (parser, /*statement_p=*/true);
8411
8412 /* Free any declarators allocated. */
8413 obstack_free (&declarator_obstack, p);
8414
8415 /* Finish off the statement. */
8416 finish_stmt ();
8417 }
8418
8419 /* Some dependent statements (like `if (cond) statement'), are
8420 implicitly in their own scope. In other words, if the statement is
8421 a single statement (as opposed to a compound-statement), it is
8422 none-the-less treated as if it were enclosed in braces. Any
8423 declarations appearing in the dependent statement are out of scope
8424 after control passes that point. This function parses a statement,
8425 but ensures that is in its own scope, even if it is not a
8426 compound-statement.
8427
8428 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8429 is a (possibly labeled) if statement which is not enclosed in
8430 braces and has an else clause. This is used to implement
8431 -Wparentheses.
8432
8433 Returns the new statement. */
8434
8435 static tree
8436 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8437 {
8438 tree statement;
8439
8440 if (if_p != NULL)
8441 *if_p = false;
8442
8443 /* Mark if () ; with a special NOP_EXPR. */
8444 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8445 {
8446 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8447 cp_lexer_consume_token (parser->lexer);
8448 statement = add_stmt (build_empty_stmt (loc));
8449 }
8450 /* if a compound is opened, we simply parse the statement directly. */
8451 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8452 statement = cp_parser_compound_statement (parser, NULL, false);
8453 /* If the token is not a `{', then we must take special action. */
8454 else
8455 {
8456 /* Create a compound-statement. */
8457 statement = begin_compound_stmt (0);
8458 /* Parse the dependent-statement. */
8459 cp_parser_statement (parser, NULL_TREE, false, if_p);
8460 /* Finish the dummy compound-statement. */
8461 finish_compound_stmt (statement);
8462 }
8463
8464 /* Return the statement. */
8465 return statement;
8466 }
8467
8468 /* For some dependent statements (like `while (cond) statement'), we
8469 have already created a scope. Therefore, even if the dependent
8470 statement is a compound-statement, we do not want to create another
8471 scope. */
8472
8473 static void
8474 cp_parser_already_scoped_statement (cp_parser* parser)
8475 {
8476 /* If the token is a `{', then we must take special action. */
8477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8478 cp_parser_statement (parser, NULL_TREE, false, NULL);
8479 else
8480 {
8481 /* Avoid calling cp_parser_compound_statement, so that we
8482 don't create a new scope. Do everything else by hand. */
8483 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8484 /* If the next keyword is `__label__' we have a label declaration. */
8485 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8486 cp_parser_label_declaration (parser);
8487 /* Parse an (optional) statement-seq. */
8488 cp_parser_statement_seq_opt (parser, NULL_TREE);
8489 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8490 }
8491 }
8492
8493 /* Declarations [gram.dcl.dcl] */
8494
8495 /* Parse an optional declaration-sequence.
8496
8497 declaration-seq:
8498 declaration
8499 declaration-seq declaration */
8500
8501 static void
8502 cp_parser_declaration_seq_opt (cp_parser* parser)
8503 {
8504 while (true)
8505 {
8506 cp_token *token;
8507
8508 token = cp_lexer_peek_token (parser->lexer);
8509
8510 if (token->type == CPP_CLOSE_BRACE
8511 || token->type == CPP_EOF
8512 || token->type == CPP_PRAGMA_EOL)
8513 break;
8514
8515 if (token->type == CPP_SEMICOLON)
8516 {
8517 /* A declaration consisting of a single semicolon is
8518 invalid. Allow it unless we're being pedantic. */
8519 cp_lexer_consume_token (parser->lexer);
8520 if (!in_system_header)
8521 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8522 continue;
8523 }
8524
8525 /* If we're entering or exiting a region that's implicitly
8526 extern "C", modify the lang context appropriately. */
8527 if (!parser->implicit_extern_c && token->implicit_extern_c)
8528 {
8529 push_lang_context (lang_name_c);
8530 parser->implicit_extern_c = true;
8531 }
8532 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8533 {
8534 pop_lang_context ();
8535 parser->implicit_extern_c = false;
8536 }
8537
8538 if (token->type == CPP_PRAGMA)
8539 {
8540 /* A top-level declaration can consist solely of a #pragma.
8541 A nested declaration cannot, so this is done here and not
8542 in cp_parser_declaration. (A #pragma at block scope is
8543 handled in cp_parser_statement.) */
8544 cp_parser_pragma (parser, pragma_external);
8545 continue;
8546 }
8547
8548 /* Parse the declaration itself. */
8549 cp_parser_declaration (parser);
8550 }
8551 }
8552
8553 /* Parse a declaration.
8554
8555 declaration:
8556 block-declaration
8557 function-definition
8558 template-declaration
8559 explicit-instantiation
8560 explicit-specialization
8561 linkage-specification
8562 namespace-definition
8563
8564 GNU extension:
8565
8566 declaration:
8567 __extension__ declaration */
8568
8569 static void
8570 cp_parser_declaration (cp_parser* parser)
8571 {
8572 cp_token token1;
8573 cp_token token2;
8574 int saved_pedantic;
8575 void *p;
8576
8577 /* Check for the `__extension__' keyword. */
8578 if (cp_parser_extension_opt (parser, &saved_pedantic))
8579 {
8580 /* Parse the qualified declaration. */
8581 cp_parser_declaration (parser);
8582 /* Restore the PEDANTIC flag. */
8583 pedantic = saved_pedantic;
8584
8585 return;
8586 }
8587
8588 /* Try to figure out what kind of declaration is present. */
8589 token1 = *cp_lexer_peek_token (parser->lexer);
8590
8591 if (token1.type != CPP_EOF)
8592 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8593 else
8594 {
8595 token2.type = CPP_EOF;
8596 token2.keyword = RID_MAX;
8597 }
8598
8599 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8600 p = obstack_alloc (&declarator_obstack, 0);
8601
8602 /* If the next token is `extern' and the following token is a string
8603 literal, then we have a linkage specification. */
8604 if (token1.keyword == RID_EXTERN
8605 && cp_parser_is_string_literal (&token2))
8606 cp_parser_linkage_specification (parser);
8607 /* If the next token is `template', then we have either a template
8608 declaration, an explicit instantiation, or an explicit
8609 specialization. */
8610 else if (token1.keyword == RID_TEMPLATE)
8611 {
8612 /* `template <>' indicates a template specialization. */
8613 if (token2.type == CPP_LESS
8614 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8615 cp_parser_explicit_specialization (parser);
8616 /* `template <' indicates a template declaration. */
8617 else if (token2.type == CPP_LESS)
8618 cp_parser_template_declaration (parser, /*member_p=*/false);
8619 /* Anything else must be an explicit instantiation. */
8620 else
8621 cp_parser_explicit_instantiation (parser);
8622 }
8623 /* If the next token is `export', then we have a template
8624 declaration. */
8625 else if (token1.keyword == RID_EXPORT)
8626 cp_parser_template_declaration (parser, /*member_p=*/false);
8627 /* If the next token is `extern', 'static' or 'inline' and the one
8628 after that is `template', we have a GNU extended explicit
8629 instantiation directive. */
8630 else if (cp_parser_allow_gnu_extensions_p (parser)
8631 && (token1.keyword == RID_EXTERN
8632 || token1.keyword == RID_STATIC
8633 || token1.keyword == RID_INLINE)
8634 && token2.keyword == RID_TEMPLATE)
8635 cp_parser_explicit_instantiation (parser);
8636 /* If the next token is `namespace', check for a named or unnamed
8637 namespace definition. */
8638 else if (token1.keyword == RID_NAMESPACE
8639 && (/* A named namespace definition. */
8640 (token2.type == CPP_NAME
8641 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8642 != CPP_EQ))
8643 /* An unnamed namespace definition. */
8644 || token2.type == CPP_OPEN_BRACE
8645 || token2.keyword == RID_ATTRIBUTE))
8646 cp_parser_namespace_definition (parser);
8647 /* An inline (associated) namespace definition. */
8648 else if (token1.keyword == RID_INLINE
8649 && token2.keyword == RID_NAMESPACE)
8650 cp_parser_namespace_definition (parser);
8651 /* Objective-C++ declaration/definition. */
8652 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8653 cp_parser_objc_declaration (parser);
8654 /* We must have either a block declaration or a function
8655 definition. */
8656 else
8657 /* Try to parse a block-declaration, or a function-definition. */
8658 cp_parser_block_declaration (parser, /*statement_p=*/false);
8659
8660 /* Free any declarators allocated. */
8661 obstack_free (&declarator_obstack, p);
8662 }
8663
8664 /* Parse a block-declaration.
8665
8666 block-declaration:
8667 simple-declaration
8668 asm-definition
8669 namespace-alias-definition
8670 using-declaration
8671 using-directive
8672
8673 GNU Extension:
8674
8675 block-declaration:
8676 __extension__ block-declaration
8677
8678 C++0x Extension:
8679
8680 block-declaration:
8681 static_assert-declaration
8682
8683 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8684 part of a declaration-statement. */
8685
8686 static void
8687 cp_parser_block_declaration (cp_parser *parser,
8688 bool statement_p)
8689 {
8690 cp_token *token1;
8691 int saved_pedantic;
8692
8693 /* Check for the `__extension__' keyword. */
8694 if (cp_parser_extension_opt (parser, &saved_pedantic))
8695 {
8696 /* Parse the qualified declaration. */
8697 cp_parser_block_declaration (parser, statement_p);
8698 /* Restore the PEDANTIC flag. */
8699 pedantic = saved_pedantic;
8700
8701 return;
8702 }
8703
8704 /* Peek at the next token to figure out which kind of declaration is
8705 present. */
8706 token1 = cp_lexer_peek_token (parser->lexer);
8707
8708 /* If the next keyword is `asm', we have an asm-definition. */
8709 if (token1->keyword == RID_ASM)
8710 {
8711 if (statement_p)
8712 cp_parser_commit_to_tentative_parse (parser);
8713 cp_parser_asm_definition (parser);
8714 }
8715 /* If the next keyword is `namespace', we have a
8716 namespace-alias-definition. */
8717 else if (token1->keyword == RID_NAMESPACE)
8718 cp_parser_namespace_alias_definition (parser);
8719 /* If the next keyword is `using', we have either a
8720 using-declaration or a using-directive. */
8721 else if (token1->keyword == RID_USING)
8722 {
8723 cp_token *token2;
8724
8725 if (statement_p)
8726 cp_parser_commit_to_tentative_parse (parser);
8727 /* If the token after `using' is `namespace', then we have a
8728 using-directive. */
8729 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8730 if (token2->keyword == RID_NAMESPACE)
8731 cp_parser_using_directive (parser);
8732 /* Otherwise, it's a using-declaration. */
8733 else
8734 cp_parser_using_declaration (parser,
8735 /*access_declaration_p=*/false);
8736 }
8737 /* If the next keyword is `__label__' we have a misplaced label
8738 declaration. */
8739 else if (token1->keyword == RID_LABEL)
8740 {
8741 cp_lexer_consume_token (parser->lexer);
8742 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8743 cp_parser_skip_to_end_of_statement (parser);
8744 /* If the next token is now a `;', consume it. */
8745 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8746 cp_lexer_consume_token (parser->lexer);
8747 }
8748 /* If the next token is `static_assert' we have a static assertion. */
8749 else if (token1->keyword == RID_STATIC_ASSERT)
8750 cp_parser_static_assert (parser, /*member_p=*/false);
8751 /* Anything else must be a simple-declaration. */
8752 else
8753 cp_parser_simple_declaration (parser, !statement_p);
8754 }
8755
8756 /* Parse a simple-declaration.
8757
8758 simple-declaration:
8759 decl-specifier-seq [opt] init-declarator-list [opt] ;
8760
8761 init-declarator-list:
8762 init-declarator
8763 init-declarator-list , init-declarator
8764
8765 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8766 function-definition as a simple-declaration. */
8767
8768 static void
8769 cp_parser_simple_declaration (cp_parser* parser,
8770 bool function_definition_allowed_p)
8771 {
8772 cp_decl_specifier_seq decl_specifiers;
8773 int declares_class_or_enum;
8774 bool saw_declarator;
8775
8776 /* Defer access checks until we know what is being declared; the
8777 checks for names appearing in the decl-specifier-seq should be
8778 done as if we were in the scope of the thing being declared. */
8779 push_deferring_access_checks (dk_deferred);
8780
8781 /* Parse the decl-specifier-seq. We have to keep track of whether
8782 or not the decl-specifier-seq declares a named class or
8783 enumeration type, since that is the only case in which the
8784 init-declarator-list is allowed to be empty.
8785
8786 [dcl.dcl]
8787
8788 In a simple-declaration, the optional init-declarator-list can be
8789 omitted only when declaring a class or enumeration, that is when
8790 the decl-specifier-seq contains either a class-specifier, an
8791 elaborated-type-specifier, or an enum-specifier. */
8792 cp_parser_decl_specifier_seq (parser,
8793 CP_PARSER_FLAGS_OPTIONAL,
8794 &decl_specifiers,
8795 &declares_class_or_enum);
8796 /* We no longer need to defer access checks. */
8797 stop_deferring_access_checks ();
8798
8799 /* In a block scope, a valid declaration must always have a
8800 decl-specifier-seq. By not trying to parse declarators, we can
8801 resolve the declaration/expression ambiguity more quickly. */
8802 if (!function_definition_allowed_p
8803 && !decl_specifiers.any_specifiers_p)
8804 {
8805 cp_parser_error (parser, "expected declaration");
8806 goto done;
8807 }
8808
8809 /* If the next two tokens are both identifiers, the code is
8810 erroneous. The usual cause of this situation is code like:
8811
8812 T t;
8813
8814 where "T" should name a type -- but does not. */
8815 if (!decl_specifiers.type
8816 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8817 {
8818 /* If parsing tentatively, we should commit; we really are
8819 looking at a declaration. */
8820 cp_parser_commit_to_tentative_parse (parser);
8821 /* Give up. */
8822 goto done;
8823 }
8824
8825 /* If we have seen at least one decl-specifier, and the next token
8826 is not a parenthesis, then we must be looking at a declaration.
8827 (After "int (" we might be looking at a functional cast.) */
8828 if (decl_specifiers.any_specifiers_p
8829 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8830 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8831 && !cp_parser_error_occurred (parser))
8832 cp_parser_commit_to_tentative_parse (parser);
8833
8834 /* Keep going until we hit the `;' at the end of the simple
8835 declaration. */
8836 saw_declarator = false;
8837 while (cp_lexer_next_token_is_not (parser->lexer,
8838 CPP_SEMICOLON))
8839 {
8840 cp_token *token;
8841 bool function_definition_p;
8842 tree decl;
8843
8844 if (saw_declarator)
8845 {
8846 /* If we are processing next declarator, coma is expected */
8847 token = cp_lexer_peek_token (parser->lexer);
8848 gcc_assert (token->type == CPP_COMMA);
8849 cp_lexer_consume_token (parser->lexer);
8850 }
8851 else
8852 saw_declarator = true;
8853
8854 /* Parse the init-declarator. */
8855 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8856 /*checks=*/NULL,
8857 function_definition_allowed_p,
8858 /*member_p=*/false,
8859 declares_class_or_enum,
8860 &function_definition_p);
8861 /* If an error occurred while parsing tentatively, exit quickly.
8862 (That usually happens when in the body of a function; each
8863 statement is treated as a declaration-statement until proven
8864 otherwise.) */
8865 if (cp_parser_error_occurred (parser))
8866 goto done;
8867 /* Handle function definitions specially. */
8868 if (function_definition_p)
8869 {
8870 /* If the next token is a `,', then we are probably
8871 processing something like:
8872
8873 void f() {}, *p;
8874
8875 which is erroneous. */
8876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8877 {
8878 cp_token *token = cp_lexer_peek_token (parser->lexer);
8879 error_at (token->location,
8880 "mixing"
8881 " declarations and function-definitions is forbidden");
8882 }
8883 /* Otherwise, we're done with the list of declarators. */
8884 else
8885 {
8886 pop_deferring_access_checks ();
8887 return;
8888 }
8889 }
8890 /* The next token should be either a `,' or a `;'. */
8891 token = cp_lexer_peek_token (parser->lexer);
8892 /* If it's a `,', there are more declarators to come. */
8893 if (token->type == CPP_COMMA)
8894 /* will be consumed next time around */;
8895 /* If it's a `;', we are done. */
8896 else if (token->type == CPP_SEMICOLON)
8897 break;
8898 /* Anything else is an error. */
8899 else
8900 {
8901 /* If we have already issued an error message we don't need
8902 to issue another one. */
8903 if (decl != error_mark_node
8904 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8905 cp_parser_error (parser, "expected %<,%> or %<;%>");
8906 /* Skip tokens until we reach the end of the statement. */
8907 cp_parser_skip_to_end_of_statement (parser);
8908 /* If the next token is now a `;', consume it. */
8909 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8910 cp_lexer_consume_token (parser->lexer);
8911 goto done;
8912 }
8913 /* After the first time around, a function-definition is not
8914 allowed -- even if it was OK at first. For example:
8915
8916 int i, f() {}
8917
8918 is not valid. */
8919 function_definition_allowed_p = false;
8920 }
8921
8922 /* Issue an error message if no declarators are present, and the
8923 decl-specifier-seq does not itself declare a class or
8924 enumeration. */
8925 if (!saw_declarator)
8926 {
8927 if (cp_parser_declares_only_class_p (parser))
8928 shadow_tag (&decl_specifiers);
8929 /* Perform any deferred access checks. */
8930 perform_deferred_access_checks ();
8931 }
8932
8933 /* Consume the `;'. */
8934 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8935
8936 done:
8937 pop_deferring_access_checks ();
8938 }
8939
8940 /* Parse a decl-specifier-seq.
8941
8942 decl-specifier-seq:
8943 decl-specifier-seq [opt] decl-specifier
8944
8945 decl-specifier:
8946 storage-class-specifier
8947 type-specifier
8948 function-specifier
8949 friend
8950 typedef
8951
8952 GNU Extension:
8953
8954 decl-specifier:
8955 attributes
8956
8957 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8958
8959 The parser flags FLAGS is used to control type-specifier parsing.
8960
8961 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8962 flags:
8963
8964 1: one of the decl-specifiers is an elaborated-type-specifier
8965 (i.e., a type declaration)
8966 2: one of the decl-specifiers is an enum-specifier or a
8967 class-specifier (i.e., a type definition)
8968
8969 */
8970
8971 static void
8972 cp_parser_decl_specifier_seq (cp_parser* parser,
8973 cp_parser_flags flags,
8974 cp_decl_specifier_seq *decl_specs,
8975 int* declares_class_or_enum)
8976 {
8977 bool constructor_possible_p = !parser->in_declarator_p;
8978 cp_token *start_token = NULL;
8979
8980 /* Clear DECL_SPECS. */
8981 clear_decl_specs (decl_specs);
8982
8983 /* Assume no class or enumeration type is declared. */
8984 *declares_class_or_enum = 0;
8985
8986 /* Keep reading specifiers until there are no more to read. */
8987 while (true)
8988 {
8989 bool constructor_p;
8990 bool found_decl_spec;
8991 cp_token *token;
8992
8993 /* Peek at the next token. */
8994 token = cp_lexer_peek_token (parser->lexer);
8995
8996 /* Save the first token of the decl spec list for error
8997 reporting. */
8998 if (!start_token)
8999 start_token = token;
9000 /* Handle attributes. */
9001 if (token->keyword == RID_ATTRIBUTE)
9002 {
9003 /* Parse the attributes. */
9004 decl_specs->attributes
9005 = chainon (decl_specs->attributes,
9006 cp_parser_attributes_opt (parser));
9007 continue;
9008 }
9009 /* Assume we will find a decl-specifier keyword. */
9010 found_decl_spec = true;
9011 /* If the next token is an appropriate keyword, we can simply
9012 add it to the list. */
9013 switch (token->keyword)
9014 {
9015 /* decl-specifier:
9016 friend
9017 constexpr */
9018 case RID_FRIEND:
9019 if (!at_class_scope_p ())
9020 {
9021 error_at (token->location, "%<friend%> used outside of class");
9022 cp_lexer_purge_token (parser->lexer);
9023 }
9024 else
9025 {
9026 ++decl_specs->specs[(int) ds_friend];
9027 /* Consume the token. */
9028 cp_lexer_consume_token (parser->lexer);
9029 }
9030 break;
9031
9032 case RID_CONSTEXPR:
9033 ++decl_specs->specs[(int) ds_constexpr];
9034 cp_lexer_consume_token (parser->lexer);
9035 break;
9036
9037 /* function-specifier:
9038 inline
9039 virtual
9040 explicit */
9041 case RID_INLINE:
9042 case RID_VIRTUAL:
9043 case RID_EXPLICIT:
9044 cp_parser_function_specifier_opt (parser, decl_specs);
9045 break;
9046
9047 /* decl-specifier:
9048 typedef */
9049 case RID_TYPEDEF:
9050 ++decl_specs->specs[(int) ds_typedef];
9051 /* Consume the token. */
9052 cp_lexer_consume_token (parser->lexer);
9053 /* A constructor declarator cannot appear in a typedef. */
9054 constructor_possible_p = false;
9055 /* The "typedef" keyword can only occur in a declaration; we
9056 may as well commit at this point. */
9057 cp_parser_commit_to_tentative_parse (parser);
9058
9059 if (decl_specs->storage_class != sc_none)
9060 decl_specs->conflicting_specifiers_p = true;
9061 break;
9062
9063 /* storage-class-specifier:
9064 auto
9065 register
9066 static
9067 extern
9068 mutable
9069
9070 GNU Extension:
9071 thread */
9072 case RID_AUTO:
9073 if (cxx_dialect == cxx98)
9074 {
9075 /* Consume the token. */
9076 cp_lexer_consume_token (parser->lexer);
9077
9078 /* Complain about `auto' as a storage specifier, if
9079 we're complaining about C++0x compatibility. */
9080 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9081 " will change meaning in C++0x; please remove it");
9082
9083 /* Set the storage class anyway. */
9084 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9085 token->location);
9086 }
9087 else
9088 /* C++0x auto type-specifier. */
9089 found_decl_spec = false;
9090 break;
9091
9092 case RID_REGISTER:
9093 case RID_STATIC:
9094 case RID_EXTERN:
9095 case RID_MUTABLE:
9096 /* Consume the token. */
9097 cp_lexer_consume_token (parser->lexer);
9098 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9099 token->location);
9100 break;
9101 case RID_THREAD:
9102 /* Consume the token. */
9103 cp_lexer_consume_token (parser->lexer);
9104 ++decl_specs->specs[(int) ds_thread];
9105 break;
9106
9107 default:
9108 /* We did not yet find a decl-specifier yet. */
9109 found_decl_spec = false;
9110 break;
9111 }
9112
9113 /* Constructors are a special case. The `S' in `S()' is not a
9114 decl-specifier; it is the beginning of the declarator. */
9115 constructor_p
9116 = (!found_decl_spec
9117 && constructor_possible_p
9118 && (cp_parser_constructor_declarator_p
9119 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9120
9121 /* If we don't have a DECL_SPEC yet, then we must be looking at
9122 a type-specifier. */
9123 if (!found_decl_spec && !constructor_p)
9124 {
9125 int decl_spec_declares_class_or_enum;
9126 bool is_cv_qualifier;
9127 tree type_spec;
9128
9129 type_spec
9130 = cp_parser_type_specifier (parser, flags,
9131 decl_specs,
9132 /*is_declaration=*/true,
9133 &decl_spec_declares_class_or_enum,
9134 &is_cv_qualifier);
9135 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9136
9137 /* If this type-specifier referenced a user-defined type
9138 (a typedef, class-name, etc.), then we can't allow any
9139 more such type-specifiers henceforth.
9140
9141 [dcl.spec]
9142
9143 The longest sequence of decl-specifiers that could
9144 possibly be a type name is taken as the
9145 decl-specifier-seq of a declaration. The sequence shall
9146 be self-consistent as described below.
9147
9148 [dcl.type]
9149
9150 As a general rule, at most one type-specifier is allowed
9151 in the complete decl-specifier-seq of a declaration. The
9152 only exceptions are the following:
9153
9154 -- const or volatile can be combined with any other
9155 type-specifier.
9156
9157 -- signed or unsigned can be combined with char, long,
9158 short, or int.
9159
9160 -- ..
9161
9162 Example:
9163
9164 typedef char* Pc;
9165 void g (const int Pc);
9166
9167 Here, Pc is *not* part of the decl-specifier seq; it's
9168 the declarator. Therefore, once we see a type-specifier
9169 (other than a cv-qualifier), we forbid any additional
9170 user-defined types. We *do* still allow things like `int
9171 int' to be considered a decl-specifier-seq, and issue the
9172 error message later. */
9173 if (type_spec && !is_cv_qualifier)
9174 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9175 /* A constructor declarator cannot follow a type-specifier. */
9176 if (type_spec)
9177 {
9178 constructor_possible_p = false;
9179 found_decl_spec = true;
9180 }
9181 }
9182
9183 /* If we still do not have a DECL_SPEC, then there are no more
9184 decl-specifiers. */
9185 if (!found_decl_spec)
9186 break;
9187
9188 decl_specs->any_specifiers_p = true;
9189 /* After we see one decl-specifier, further decl-specifiers are
9190 always optional. */
9191 flags |= CP_PARSER_FLAGS_OPTIONAL;
9192 }
9193
9194 cp_parser_check_decl_spec (decl_specs, start_token->location);
9195
9196 /* Don't allow a friend specifier with a class definition. */
9197 if (decl_specs->specs[(int) ds_friend] != 0
9198 && (*declares_class_or_enum & 2))
9199 error_at (start_token->location,
9200 "class definition may not be declared a friend");
9201 }
9202
9203 /* Parse an (optional) storage-class-specifier.
9204
9205 storage-class-specifier:
9206 auto
9207 register
9208 static
9209 extern
9210 mutable
9211
9212 GNU Extension:
9213
9214 storage-class-specifier:
9215 thread
9216
9217 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9218
9219 static tree
9220 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9221 {
9222 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9223 {
9224 case RID_AUTO:
9225 if (cxx_dialect != cxx98)
9226 return NULL_TREE;
9227 /* Fall through for C++98. */
9228
9229 case RID_REGISTER:
9230 case RID_STATIC:
9231 case RID_EXTERN:
9232 case RID_MUTABLE:
9233 case RID_THREAD:
9234 /* Consume the token. */
9235 return cp_lexer_consume_token (parser->lexer)->u.value;
9236
9237 default:
9238 return NULL_TREE;
9239 }
9240 }
9241
9242 /* Parse an (optional) function-specifier.
9243
9244 function-specifier:
9245 inline
9246 virtual
9247 explicit
9248
9249 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9250 Updates DECL_SPECS, if it is non-NULL. */
9251
9252 static tree
9253 cp_parser_function_specifier_opt (cp_parser* parser,
9254 cp_decl_specifier_seq *decl_specs)
9255 {
9256 cp_token *token = cp_lexer_peek_token (parser->lexer);
9257 switch (token->keyword)
9258 {
9259 case RID_INLINE:
9260 if (decl_specs)
9261 ++decl_specs->specs[(int) ds_inline];
9262 break;
9263
9264 case RID_VIRTUAL:
9265 /* 14.5.2.3 [temp.mem]
9266
9267 A member function template shall not be virtual. */
9268 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9269 error_at (token->location, "templates may not be %<virtual%>");
9270 else if (decl_specs)
9271 ++decl_specs->specs[(int) ds_virtual];
9272 break;
9273
9274 case RID_EXPLICIT:
9275 if (decl_specs)
9276 ++decl_specs->specs[(int) ds_explicit];
9277 break;
9278
9279 default:
9280 return NULL_TREE;
9281 }
9282
9283 /* Consume the token. */
9284 return cp_lexer_consume_token (parser->lexer)->u.value;
9285 }
9286
9287 /* Parse a linkage-specification.
9288
9289 linkage-specification:
9290 extern string-literal { declaration-seq [opt] }
9291 extern string-literal declaration */
9292
9293 static void
9294 cp_parser_linkage_specification (cp_parser* parser)
9295 {
9296 tree linkage;
9297
9298 /* Look for the `extern' keyword. */
9299 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9300
9301 /* Look for the string-literal. */
9302 linkage = cp_parser_string_literal (parser, false, false);
9303
9304 /* Transform the literal into an identifier. If the literal is a
9305 wide-character string, or contains embedded NULs, then we can't
9306 handle it as the user wants. */
9307 if (strlen (TREE_STRING_POINTER (linkage))
9308 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9309 {
9310 cp_parser_error (parser, "invalid linkage-specification");
9311 /* Assume C++ linkage. */
9312 linkage = lang_name_cplusplus;
9313 }
9314 else
9315 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9316
9317 /* We're now using the new linkage. */
9318 push_lang_context (linkage);
9319
9320 /* If the next token is a `{', then we're using the first
9321 production. */
9322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9323 {
9324 /* Consume the `{' token. */
9325 cp_lexer_consume_token (parser->lexer);
9326 /* Parse the declarations. */
9327 cp_parser_declaration_seq_opt (parser);
9328 /* Look for the closing `}'. */
9329 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9330 }
9331 /* Otherwise, there's just one declaration. */
9332 else
9333 {
9334 bool saved_in_unbraced_linkage_specification_p;
9335
9336 saved_in_unbraced_linkage_specification_p
9337 = parser->in_unbraced_linkage_specification_p;
9338 parser->in_unbraced_linkage_specification_p = true;
9339 cp_parser_declaration (parser);
9340 parser->in_unbraced_linkage_specification_p
9341 = saved_in_unbraced_linkage_specification_p;
9342 }
9343
9344 /* We're done with the linkage-specification. */
9345 pop_lang_context ();
9346 }
9347
9348 /* Parse a static_assert-declaration.
9349
9350 static_assert-declaration:
9351 static_assert ( constant-expression , string-literal ) ;
9352
9353 If MEMBER_P, this static_assert is a class member. */
9354
9355 static void
9356 cp_parser_static_assert(cp_parser *parser, bool member_p)
9357 {
9358 tree condition;
9359 tree message;
9360 cp_token *token;
9361 location_t saved_loc;
9362
9363 /* Peek at the `static_assert' token so we can keep track of exactly
9364 where the static assertion started. */
9365 token = cp_lexer_peek_token (parser->lexer);
9366 saved_loc = token->location;
9367
9368 /* Look for the `static_assert' keyword. */
9369 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9370 "%<static_assert%>"))
9371 return;
9372
9373 /* We know we are in a static assertion; commit to any tentative
9374 parse. */
9375 if (cp_parser_parsing_tentatively (parser))
9376 cp_parser_commit_to_tentative_parse (parser);
9377
9378 /* Parse the `(' starting the static assertion condition. */
9379 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9380
9381 /* Parse the constant-expression. */
9382 condition =
9383 cp_parser_constant_expression (parser,
9384 /*allow_non_constant_p=*/false,
9385 /*non_constant_p=*/NULL);
9386
9387 /* Parse the separating `,'. */
9388 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9389
9390 /* Parse the string-literal message. */
9391 message = cp_parser_string_literal (parser,
9392 /*translate=*/false,
9393 /*wide_ok=*/true);
9394
9395 /* A `)' completes the static assertion. */
9396 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9397 cp_parser_skip_to_closing_parenthesis (parser,
9398 /*recovering=*/true,
9399 /*or_comma=*/false,
9400 /*consume_paren=*/true);
9401
9402 /* A semicolon terminates the declaration. */
9403 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9404
9405 /* Complete the static assertion, which may mean either processing
9406 the static assert now or saving it for template instantiation. */
9407 finish_static_assert (condition, message, saved_loc, member_p);
9408 }
9409
9410 /* Parse a `decltype' type. Returns the type.
9411
9412 simple-type-specifier:
9413 decltype ( expression ) */
9414
9415 static tree
9416 cp_parser_decltype (cp_parser *parser)
9417 {
9418 tree expr;
9419 bool id_expression_or_member_access_p = false;
9420 const char *saved_message;
9421 bool saved_integral_constant_expression_p;
9422 bool saved_non_integral_constant_expression_p;
9423 cp_token *id_expr_start_token;
9424
9425 /* Look for the `decltype' token. */
9426 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9427 return error_mark_node;
9428
9429 /* Types cannot be defined in a `decltype' expression. Save away the
9430 old message. */
9431 saved_message = parser->type_definition_forbidden_message;
9432
9433 /* And create the new one. */
9434 parser->type_definition_forbidden_message
9435 = "types may not be defined in %<decltype%> expressions";
9436
9437 /* The restrictions on constant-expressions do not apply inside
9438 decltype expressions. */
9439 saved_integral_constant_expression_p
9440 = parser->integral_constant_expression_p;
9441 saved_non_integral_constant_expression_p
9442 = parser->non_integral_constant_expression_p;
9443 parser->integral_constant_expression_p = false;
9444
9445 /* Do not actually evaluate the expression. */
9446 ++cp_unevaluated_operand;
9447
9448 /* Do not warn about problems with the expression. */
9449 ++c_inhibit_evaluation_warnings;
9450
9451 /* Parse the opening `('. */
9452 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9453 return error_mark_node;
9454
9455 /* First, try parsing an id-expression. */
9456 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9457 cp_parser_parse_tentatively (parser);
9458 expr = cp_parser_id_expression (parser,
9459 /*template_keyword_p=*/false,
9460 /*check_dependency_p=*/true,
9461 /*template_p=*/NULL,
9462 /*declarator_p=*/false,
9463 /*optional_p=*/false);
9464
9465 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9466 {
9467 bool non_integral_constant_expression_p = false;
9468 tree id_expression = expr;
9469 cp_id_kind idk;
9470 const char *error_msg;
9471
9472 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9473 /* Lookup the name we got back from the id-expression. */
9474 expr = cp_parser_lookup_name (parser, expr,
9475 none_type,
9476 /*is_template=*/false,
9477 /*is_namespace=*/false,
9478 /*check_dependency=*/true,
9479 /*ambiguous_decls=*/NULL,
9480 id_expr_start_token->location);
9481
9482 if (expr
9483 && expr != error_mark_node
9484 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9485 && TREE_CODE (expr) != TYPE_DECL
9486 && (TREE_CODE (expr) != BIT_NOT_EXPR
9487 || !TYPE_P (TREE_OPERAND (expr, 0)))
9488 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9489 {
9490 /* Complete lookup of the id-expression. */
9491 expr = (finish_id_expression
9492 (id_expression, expr, parser->scope, &idk,
9493 /*integral_constant_expression_p=*/false,
9494 /*allow_non_integral_constant_expression_p=*/true,
9495 &non_integral_constant_expression_p,
9496 /*template_p=*/false,
9497 /*done=*/true,
9498 /*address_p=*/false,
9499 /*template_arg_p=*/false,
9500 &error_msg,
9501 id_expr_start_token->location));
9502
9503 if (expr == error_mark_node)
9504 /* We found an id-expression, but it was something that we
9505 should not have found. This is an error, not something
9506 we can recover from, so note that we found an
9507 id-expression and we'll recover as gracefully as
9508 possible. */
9509 id_expression_or_member_access_p = true;
9510 }
9511
9512 if (expr
9513 && expr != error_mark_node
9514 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9515 /* We have an id-expression. */
9516 id_expression_or_member_access_p = true;
9517 }
9518
9519 if (!id_expression_or_member_access_p)
9520 {
9521 /* Abort the id-expression parse. */
9522 cp_parser_abort_tentative_parse (parser);
9523
9524 /* Parsing tentatively, again. */
9525 cp_parser_parse_tentatively (parser);
9526
9527 /* Parse a class member access. */
9528 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9529 /*cast_p=*/false,
9530 /*member_access_only_p=*/true, NULL);
9531
9532 if (expr
9533 && expr != error_mark_node
9534 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9535 /* We have an id-expression. */
9536 id_expression_or_member_access_p = true;
9537 }
9538
9539 if (id_expression_or_member_access_p)
9540 /* We have parsed the complete id-expression or member access. */
9541 cp_parser_parse_definitely (parser);
9542 else
9543 {
9544 bool saved_greater_than_is_operator_p;
9545
9546 /* Abort our attempt to parse an id-expression or member access
9547 expression. */
9548 cp_parser_abort_tentative_parse (parser);
9549
9550 /* Within a parenthesized expression, a `>' token is always
9551 the greater-than operator. */
9552 saved_greater_than_is_operator_p
9553 = parser->greater_than_is_operator_p;
9554 parser->greater_than_is_operator_p = true;
9555
9556 /* Parse a full expression. */
9557 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9558
9559 /* The `>' token might be the end of a template-id or
9560 template-parameter-list now. */
9561 parser->greater_than_is_operator_p
9562 = saved_greater_than_is_operator_p;
9563 }
9564
9565 /* Go back to evaluating expressions. */
9566 --cp_unevaluated_operand;
9567 --c_inhibit_evaluation_warnings;
9568
9569 /* Restore the old message and the integral constant expression
9570 flags. */
9571 parser->type_definition_forbidden_message = saved_message;
9572 parser->integral_constant_expression_p
9573 = saved_integral_constant_expression_p;
9574 parser->non_integral_constant_expression_p
9575 = saved_non_integral_constant_expression_p;
9576
9577 if (expr == error_mark_node)
9578 {
9579 /* Skip everything up to the closing `)'. */
9580 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9581 /*consume_paren=*/true);
9582 return error_mark_node;
9583 }
9584
9585 /* Parse to the closing `)'. */
9586 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9587 {
9588 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9589 /*consume_paren=*/true);
9590 return error_mark_node;
9591 }
9592
9593 return finish_decltype_type (expr, id_expression_or_member_access_p);
9594 }
9595
9596 /* Special member functions [gram.special] */
9597
9598 /* Parse a conversion-function-id.
9599
9600 conversion-function-id:
9601 operator conversion-type-id
9602
9603 Returns an IDENTIFIER_NODE representing the operator. */
9604
9605 static tree
9606 cp_parser_conversion_function_id (cp_parser* parser)
9607 {
9608 tree type;
9609 tree saved_scope;
9610 tree saved_qualifying_scope;
9611 tree saved_object_scope;
9612 tree pushed_scope = NULL_TREE;
9613
9614 /* Look for the `operator' token. */
9615 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9616 return error_mark_node;
9617 /* When we parse the conversion-type-id, the current scope will be
9618 reset. However, we need that information in able to look up the
9619 conversion function later, so we save it here. */
9620 saved_scope = parser->scope;
9621 saved_qualifying_scope = parser->qualifying_scope;
9622 saved_object_scope = parser->object_scope;
9623 /* We must enter the scope of the class so that the names of
9624 entities declared within the class are available in the
9625 conversion-type-id. For example, consider:
9626
9627 struct S {
9628 typedef int I;
9629 operator I();
9630 };
9631
9632 S::operator I() { ... }
9633
9634 In order to see that `I' is a type-name in the definition, we
9635 must be in the scope of `S'. */
9636 if (saved_scope)
9637 pushed_scope = push_scope (saved_scope);
9638 /* Parse the conversion-type-id. */
9639 type = cp_parser_conversion_type_id (parser);
9640 /* Leave the scope of the class, if any. */
9641 if (pushed_scope)
9642 pop_scope (pushed_scope);
9643 /* Restore the saved scope. */
9644 parser->scope = saved_scope;
9645 parser->qualifying_scope = saved_qualifying_scope;
9646 parser->object_scope = saved_object_scope;
9647 /* If the TYPE is invalid, indicate failure. */
9648 if (type == error_mark_node)
9649 return error_mark_node;
9650 return mangle_conv_op_name_for_type (type);
9651 }
9652
9653 /* Parse a conversion-type-id:
9654
9655 conversion-type-id:
9656 type-specifier-seq conversion-declarator [opt]
9657
9658 Returns the TYPE specified. */
9659
9660 static tree
9661 cp_parser_conversion_type_id (cp_parser* parser)
9662 {
9663 tree attributes;
9664 cp_decl_specifier_seq type_specifiers;
9665 cp_declarator *declarator;
9666 tree type_specified;
9667
9668 /* Parse the attributes. */
9669 attributes = cp_parser_attributes_opt (parser);
9670 /* Parse the type-specifiers. */
9671 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9672 &type_specifiers);
9673 /* If that didn't work, stop. */
9674 if (type_specifiers.type == error_mark_node)
9675 return error_mark_node;
9676 /* Parse the conversion-declarator. */
9677 declarator = cp_parser_conversion_declarator_opt (parser);
9678
9679 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9680 /*initialized=*/0, &attributes);
9681 if (attributes)
9682 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9683
9684 /* Don't give this error when parsing tentatively. This happens to
9685 work because we always parse this definitively once. */
9686 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9687 && type_uses_auto (type_specified))
9688 {
9689 error ("invalid use of %<auto%> in conversion operator");
9690 return error_mark_node;
9691 }
9692
9693 return type_specified;
9694 }
9695
9696 /* Parse an (optional) conversion-declarator.
9697
9698 conversion-declarator:
9699 ptr-operator conversion-declarator [opt]
9700
9701 */
9702
9703 static cp_declarator *
9704 cp_parser_conversion_declarator_opt (cp_parser* parser)
9705 {
9706 enum tree_code code;
9707 tree class_type;
9708 cp_cv_quals cv_quals;
9709
9710 /* We don't know if there's a ptr-operator next, or not. */
9711 cp_parser_parse_tentatively (parser);
9712 /* Try the ptr-operator. */
9713 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9714 /* If it worked, look for more conversion-declarators. */
9715 if (cp_parser_parse_definitely (parser))
9716 {
9717 cp_declarator *declarator;
9718
9719 /* Parse another optional declarator. */
9720 declarator = cp_parser_conversion_declarator_opt (parser);
9721
9722 return cp_parser_make_indirect_declarator
9723 (code, class_type, cv_quals, declarator);
9724 }
9725
9726 return NULL;
9727 }
9728
9729 /* Parse an (optional) ctor-initializer.
9730
9731 ctor-initializer:
9732 : mem-initializer-list
9733
9734 Returns TRUE iff the ctor-initializer was actually present. */
9735
9736 static bool
9737 cp_parser_ctor_initializer_opt (cp_parser* parser)
9738 {
9739 /* If the next token is not a `:', then there is no
9740 ctor-initializer. */
9741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9742 {
9743 /* Do default initialization of any bases and members. */
9744 if (DECL_CONSTRUCTOR_P (current_function_decl))
9745 finish_mem_initializers (NULL_TREE);
9746
9747 return false;
9748 }
9749
9750 /* Consume the `:' token. */
9751 cp_lexer_consume_token (parser->lexer);
9752 /* And the mem-initializer-list. */
9753 cp_parser_mem_initializer_list (parser);
9754
9755 return true;
9756 }
9757
9758 /* Parse a mem-initializer-list.
9759
9760 mem-initializer-list:
9761 mem-initializer ... [opt]
9762 mem-initializer ... [opt] , mem-initializer-list */
9763
9764 static void
9765 cp_parser_mem_initializer_list (cp_parser* parser)
9766 {
9767 tree mem_initializer_list = NULL_TREE;
9768 cp_token *token = cp_lexer_peek_token (parser->lexer);
9769
9770 /* Let the semantic analysis code know that we are starting the
9771 mem-initializer-list. */
9772 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9773 error_at (token->location,
9774 "only constructors take base initializers");
9775
9776 /* Loop through the list. */
9777 while (true)
9778 {
9779 tree mem_initializer;
9780
9781 token = cp_lexer_peek_token (parser->lexer);
9782 /* Parse the mem-initializer. */
9783 mem_initializer = cp_parser_mem_initializer (parser);
9784 /* If the next token is a `...', we're expanding member initializers. */
9785 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9786 {
9787 /* Consume the `...'. */
9788 cp_lexer_consume_token (parser->lexer);
9789
9790 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9791 can be expanded but members cannot. */
9792 if (mem_initializer != error_mark_node
9793 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9794 {
9795 error_at (token->location,
9796 "cannot expand initializer for member %<%D%>",
9797 TREE_PURPOSE (mem_initializer));
9798 mem_initializer = error_mark_node;
9799 }
9800
9801 /* Construct the pack expansion type. */
9802 if (mem_initializer != error_mark_node)
9803 mem_initializer = make_pack_expansion (mem_initializer);
9804 }
9805 /* Add it to the list, unless it was erroneous. */
9806 if (mem_initializer != error_mark_node)
9807 {
9808 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9809 mem_initializer_list = mem_initializer;
9810 }
9811 /* If the next token is not a `,', we're done. */
9812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9813 break;
9814 /* Consume the `,' token. */
9815 cp_lexer_consume_token (parser->lexer);
9816 }
9817
9818 /* Perform semantic analysis. */
9819 if (DECL_CONSTRUCTOR_P (current_function_decl))
9820 finish_mem_initializers (mem_initializer_list);
9821 }
9822
9823 /* Parse a mem-initializer.
9824
9825 mem-initializer:
9826 mem-initializer-id ( expression-list [opt] )
9827 mem-initializer-id braced-init-list
9828
9829 GNU extension:
9830
9831 mem-initializer:
9832 ( expression-list [opt] )
9833
9834 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9835 class) or FIELD_DECL (for a non-static data member) to initialize;
9836 the TREE_VALUE is the expression-list. An empty initialization
9837 list is represented by void_list_node. */
9838
9839 static tree
9840 cp_parser_mem_initializer (cp_parser* parser)
9841 {
9842 tree mem_initializer_id;
9843 tree expression_list;
9844 tree member;
9845 cp_token *token = cp_lexer_peek_token (parser->lexer);
9846
9847 /* Find out what is being initialized. */
9848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9849 {
9850 permerror (token->location,
9851 "anachronistic old-style base class initializer");
9852 mem_initializer_id = NULL_TREE;
9853 }
9854 else
9855 {
9856 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9857 if (mem_initializer_id == error_mark_node)
9858 return mem_initializer_id;
9859 }
9860 member = expand_member_init (mem_initializer_id);
9861 if (member && !DECL_P (member))
9862 in_base_initializer = 1;
9863
9864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9865 {
9866 bool expr_non_constant_p;
9867 maybe_warn_cpp0x ("extended initializer lists");
9868 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9869 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9870 expression_list = build_tree_list (NULL_TREE, expression_list);
9871 }
9872 else
9873 {
9874 VEC(tree,gc)* vec;
9875 vec = cp_parser_parenthesized_expression_list (parser, false,
9876 /*cast_p=*/false,
9877 /*allow_expansion_p=*/true,
9878 /*non_constant_p=*/NULL);
9879 if (vec == NULL)
9880 return error_mark_node;
9881 expression_list = build_tree_list_vec (vec);
9882 release_tree_vector (vec);
9883 }
9884
9885 if (expression_list == error_mark_node)
9886 return error_mark_node;
9887 if (!expression_list)
9888 expression_list = void_type_node;
9889
9890 in_base_initializer = 0;
9891
9892 return member ? build_tree_list (member, expression_list) : error_mark_node;
9893 }
9894
9895 /* Parse a mem-initializer-id.
9896
9897 mem-initializer-id:
9898 :: [opt] nested-name-specifier [opt] class-name
9899 identifier
9900
9901 Returns a TYPE indicating the class to be initializer for the first
9902 production. Returns an IDENTIFIER_NODE indicating the data member
9903 to be initialized for the second production. */
9904
9905 static tree
9906 cp_parser_mem_initializer_id (cp_parser* parser)
9907 {
9908 bool global_scope_p;
9909 bool nested_name_specifier_p;
9910 bool template_p = false;
9911 tree id;
9912
9913 cp_token *token = cp_lexer_peek_token (parser->lexer);
9914
9915 /* `typename' is not allowed in this context ([temp.res]). */
9916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9917 {
9918 error_at (token->location,
9919 "keyword %<typename%> not allowed in this context (a qualified "
9920 "member initializer is implicitly a type)");
9921 cp_lexer_consume_token (parser->lexer);
9922 }
9923 /* Look for the optional `::' operator. */
9924 global_scope_p
9925 = (cp_parser_global_scope_opt (parser,
9926 /*current_scope_valid_p=*/false)
9927 != NULL_TREE);
9928 /* Look for the optional nested-name-specifier. The simplest way to
9929 implement:
9930
9931 [temp.res]
9932
9933 The keyword `typename' is not permitted in a base-specifier or
9934 mem-initializer; in these contexts a qualified name that
9935 depends on a template-parameter is implicitly assumed to be a
9936 type name.
9937
9938 is to assume that we have seen the `typename' keyword at this
9939 point. */
9940 nested_name_specifier_p
9941 = (cp_parser_nested_name_specifier_opt (parser,
9942 /*typename_keyword_p=*/true,
9943 /*check_dependency_p=*/true,
9944 /*type_p=*/true,
9945 /*is_declaration=*/true)
9946 != NULL_TREE);
9947 if (nested_name_specifier_p)
9948 template_p = cp_parser_optional_template_keyword (parser);
9949 /* If there is a `::' operator or a nested-name-specifier, then we
9950 are definitely looking for a class-name. */
9951 if (global_scope_p || nested_name_specifier_p)
9952 return cp_parser_class_name (parser,
9953 /*typename_keyword_p=*/true,
9954 /*template_keyword_p=*/template_p,
9955 none_type,
9956 /*check_dependency_p=*/true,
9957 /*class_head_p=*/false,
9958 /*is_declaration=*/true);
9959 /* Otherwise, we could also be looking for an ordinary identifier. */
9960 cp_parser_parse_tentatively (parser);
9961 /* Try a class-name. */
9962 id = cp_parser_class_name (parser,
9963 /*typename_keyword_p=*/true,
9964 /*template_keyword_p=*/false,
9965 none_type,
9966 /*check_dependency_p=*/true,
9967 /*class_head_p=*/false,
9968 /*is_declaration=*/true);
9969 /* If we found one, we're done. */
9970 if (cp_parser_parse_definitely (parser))
9971 return id;
9972 /* Otherwise, look for an ordinary identifier. */
9973 return cp_parser_identifier (parser);
9974 }
9975
9976 /* Overloading [gram.over] */
9977
9978 /* Parse an operator-function-id.
9979
9980 operator-function-id:
9981 operator operator
9982
9983 Returns an IDENTIFIER_NODE for the operator which is a
9984 human-readable spelling of the identifier, e.g., `operator +'. */
9985
9986 static tree
9987 cp_parser_operator_function_id (cp_parser* parser)
9988 {
9989 /* Look for the `operator' keyword. */
9990 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9991 return error_mark_node;
9992 /* And then the name of the operator itself. */
9993 return cp_parser_operator (parser);
9994 }
9995
9996 /* Parse an operator.
9997
9998 operator:
9999 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10000 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10001 || ++ -- , ->* -> () []
10002
10003 GNU Extensions:
10004
10005 operator:
10006 <? >? <?= >?=
10007
10008 Returns an IDENTIFIER_NODE for the operator which is a
10009 human-readable spelling of the identifier, e.g., `operator +'. */
10010
10011 static tree
10012 cp_parser_operator (cp_parser* parser)
10013 {
10014 tree id = NULL_TREE;
10015 cp_token *token;
10016
10017 /* Peek at the next token. */
10018 token = cp_lexer_peek_token (parser->lexer);
10019 /* Figure out which operator we have. */
10020 switch (token->type)
10021 {
10022 case CPP_KEYWORD:
10023 {
10024 enum tree_code op;
10025
10026 /* The keyword should be either `new' or `delete'. */
10027 if (token->keyword == RID_NEW)
10028 op = NEW_EXPR;
10029 else if (token->keyword == RID_DELETE)
10030 op = DELETE_EXPR;
10031 else
10032 break;
10033
10034 /* Consume the `new' or `delete' token. */
10035 cp_lexer_consume_token (parser->lexer);
10036
10037 /* Peek at the next token. */
10038 token = cp_lexer_peek_token (parser->lexer);
10039 /* If it's a `[' token then this is the array variant of the
10040 operator. */
10041 if (token->type == CPP_OPEN_SQUARE)
10042 {
10043 /* Consume the `[' token. */
10044 cp_lexer_consume_token (parser->lexer);
10045 /* Look for the `]' token. */
10046 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10047 id = ansi_opname (op == NEW_EXPR
10048 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10049 }
10050 /* Otherwise, we have the non-array variant. */
10051 else
10052 id = ansi_opname (op);
10053
10054 return id;
10055 }
10056
10057 case CPP_PLUS:
10058 id = ansi_opname (PLUS_EXPR);
10059 break;
10060
10061 case CPP_MINUS:
10062 id = ansi_opname (MINUS_EXPR);
10063 break;
10064
10065 case CPP_MULT:
10066 id = ansi_opname (MULT_EXPR);
10067 break;
10068
10069 case CPP_DIV:
10070 id = ansi_opname (TRUNC_DIV_EXPR);
10071 break;
10072
10073 case CPP_MOD:
10074 id = ansi_opname (TRUNC_MOD_EXPR);
10075 break;
10076
10077 case CPP_XOR:
10078 id = ansi_opname (BIT_XOR_EXPR);
10079 break;
10080
10081 case CPP_AND:
10082 id = ansi_opname (BIT_AND_EXPR);
10083 break;
10084
10085 case CPP_OR:
10086 id = ansi_opname (BIT_IOR_EXPR);
10087 break;
10088
10089 case CPP_COMPL:
10090 id = ansi_opname (BIT_NOT_EXPR);
10091 break;
10092
10093 case CPP_NOT:
10094 id = ansi_opname (TRUTH_NOT_EXPR);
10095 break;
10096
10097 case CPP_EQ:
10098 id = ansi_assopname (NOP_EXPR);
10099 break;
10100
10101 case CPP_LESS:
10102 id = ansi_opname (LT_EXPR);
10103 break;
10104
10105 case CPP_GREATER:
10106 id = ansi_opname (GT_EXPR);
10107 break;
10108
10109 case CPP_PLUS_EQ:
10110 id = ansi_assopname (PLUS_EXPR);
10111 break;
10112
10113 case CPP_MINUS_EQ:
10114 id = ansi_assopname (MINUS_EXPR);
10115 break;
10116
10117 case CPP_MULT_EQ:
10118 id = ansi_assopname (MULT_EXPR);
10119 break;
10120
10121 case CPP_DIV_EQ:
10122 id = ansi_assopname (TRUNC_DIV_EXPR);
10123 break;
10124
10125 case CPP_MOD_EQ:
10126 id = ansi_assopname (TRUNC_MOD_EXPR);
10127 break;
10128
10129 case CPP_XOR_EQ:
10130 id = ansi_assopname (BIT_XOR_EXPR);
10131 break;
10132
10133 case CPP_AND_EQ:
10134 id = ansi_assopname (BIT_AND_EXPR);
10135 break;
10136
10137 case CPP_OR_EQ:
10138 id = ansi_assopname (BIT_IOR_EXPR);
10139 break;
10140
10141 case CPP_LSHIFT:
10142 id = ansi_opname (LSHIFT_EXPR);
10143 break;
10144
10145 case CPP_RSHIFT:
10146 id = ansi_opname (RSHIFT_EXPR);
10147 break;
10148
10149 case CPP_LSHIFT_EQ:
10150 id = ansi_assopname (LSHIFT_EXPR);
10151 break;
10152
10153 case CPP_RSHIFT_EQ:
10154 id = ansi_assopname (RSHIFT_EXPR);
10155 break;
10156
10157 case CPP_EQ_EQ:
10158 id = ansi_opname (EQ_EXPR);
10159 break;
10160
10161 case CPP_NOT_EQ:
10162 id = ansi_opname (NE_EXPR);
10163 break;
10164
10165 case CPP_LESS_EQ:
10166 id = ansi_opname (LE_EXPR);
10167 break;
10168
10169 case CPP_GREATER_EQ:
10170 id = ansi_opname (GE_EXPR);
10171 break;
10172
10173 case CPP_AND_AND:
10174 id = ansi_opname (TRUTH_ANDIF_EXPR);
10175 break;
10176
10177 case CPP_OR_OR:
10178 id = ansi_opname (TRUTH_ORIF_EXPR);
10179 break;
10180
10181 case CPP_PLUS_PLUS:
10182 id = ansi_opname (POSTINCREMENT_EXPR);
10183 break;
10184
10185 case CPP_MINUS_MINUS:
10186 id = ansi_opname (PREDECREMENT_EXPR);
10187 break;
10188
10189 case CPP_COMMA:
10190 id = ansi_opname (COMPOUND_EXPR);
10191 break;
10192
10193 case CPP_DEREF_STAR:
10194 id = ansi_opname (MEMBER_REF);
10195 break;
10196
10197 case CPP_DEREF:
10198 id = ansi_opname (COMPONENT_REF);
10199 break;
10200
10201 case CPP_OPEN_PAREN:
10202 /* Consume the `('. */
10203 cp_lexer_consume_token (parser->lexer);
10204 /* Look for the matching `)'. */
10205 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10206 return ansi_opname (CALL_EXPR);
10207
10208 case CPP_OPEN_SQUARE:
10209 /* Consume the `['. */
10210 cp_lexer_consume_token (parser->lexer);
10211 /* Look for the matching `]'. */
10212 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10213 return ansi_opname (ARRAY_REF);
10214
10215 default:
10216 /* Anything else is an error. */
10217 break;
10218 }
10219
10220 /* If we have selected an identifier, we need to consume the
10221 operator token. */
10222 if (id)
10223 cp_lexer_consume_token (parser->lexer);
10224 /* Otherwise, no valid operator name was present. */
10225 else
10226 {
10227 cp_parser_error (parser, "expected operator");
10228 id = error_mark_node;
10229 }
10230
10231 return id;
10232 }
10233
10234 /* Parse a template-declaration.
10235
10236 template-declaration:
10237 export [opt] template < template-parameter-list > declaration
10238
10239 If MEMBER_P is TRUE, this template-declaration occurs within a
10240 class-specifier.
10241
10242 The grammar rule given by the standard isn't correct. What
10243 is really meant is:
10244
10245 template-declaration:
10246 export [opt] template-parameter-list-seq
10247 decl-specifier-seq [opt] init-declarator [opt] ;
10248 export [opt] template-parameter-list-seq
10249 function-definition
10250
10251 template-parameter-list-seq:
10252 template-parameter-list-seq [opt]
10253 template < template-parameter-list > */
10254
10255 static void
10256 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10257 {
10258 /* Check for `export'. */
10259 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10260 {
10261 /* Consume the `export' token. */
10262 cp_lexer_consume_token (parser->lexer);
10263 /* Warn that we do not support `export'. */
10264 warning (0, "keyword %<export%> not implemented, and will be ignored");
10265 }
10266
10267 cp_parser_template_declaration_after_export (parser, member_p);
10268 }
10269
10270 /* Parse a template-parameter-list.
10271
10272 template-parameter-list:
10273 template-parameter
10274 template-parameter-list , template-parameter
10275
10276 Returns a TREE_LIST. Each node represents a template parameter.
10277 The nodes are connected via their TREE_CHAINs. */
10278
10279 static tree
10280 cp_parser_template_parameter_list (cp_parser* parser)
10281 {
10282 tree parameter_list = NULL_TREE;
10283
10284 begin_template_parm_list ();
10285 while (true)
10286 {
10287 tree parameter;
10288 bool is_non_type;
10289 bool is_parameter_pack;
10290 location_t parm_loc;
10291
10292 /* Parse the template-parameter. */
10293 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10294 parameter = cp_parser_template_parameter (parser,
10295 &is_non_type,
10296 &is_parameter_pack);
10297 /* Add it to the list. */
10298 if (parameter != error_mark_node)
10299 parameter_list = process_template_parm (parameter_list,
10300 parm_loc,
10301 parameter,
10302 is_non_type,
10303 is_parameter_pack);
10304 else
10305 {
10306 tree err_parm = build_tree_list (parameter, parameter);
10307 TREE_VALUE (err_parm) = error_mark_node;
10308 parameter_list = chainon (parameter_list, err_parm);
10309 }
10310
10311 /* If the next token is not a `,', we're done. */
10312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10313 break;
10314 /* Otherwise, consume the `,' token. */
10315 cp_lexer_consume_token (parser->lexer);
10316 }
10317
10318 return end_template_parm_list (parameter_list);
10319 }
10320
10321 /* Parse a template-parameter.
10322
10323 template-parameter:
10324 type-parameter
10325 parameter-declaration
10326
10327 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10328 the parameter. The TREE_PURPOSE is the default value, if any.
10329 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10330 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10331 set to true iff this parameter is a parameter pack. */
10332
10333 static tree
10334 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10335 bool *is_parameter_pack)
10336 {
10337 cp_token *token;
10338 cp_parameter_declarator *parameter_declarator;
10339 cp_declarator *id_declarator;
10340 tree parm;
10341
10342 /* Assume it is a type parameter or a template parameter. */
10343 *is_non_type = false;
10344 /* Assume it not a parameter pack. */
10345 *is_parameter_pack = false;
10346 /* Peek at the next token. */
10347 token = cp_lexer_peek_token (parser->lexer);
10348 /* If it is `class' or `template', we have a type-parameter. */
10349 if (token->keyword == RID_TEMPLATE)
10350 return cp_parser_type_parameter (parser, is_parameter_pack);
10351 /* If it is `class' or `typename' we do not know yet whether it is a
10352 type parameter or a non-type parameter. Consider:
10353
10354 template <typename T, typename T::X X> ...
10355
10356 or:
10357
10358 template <class C, class D*> ...
10359
10360 Here, the first parameter is a type parameter, and the second is
10361 a non-type parameter. We can tell by looking at the token after
10362 the identifier -- if it is a `,', `=', or `>' then we have a type
10363 parameter. */
10364 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10365 {
10366 /* Peek at the token after `class' or `typename'. */
10367 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10368 /* If it's an ellipsis, we have a template type parameter
10369 pack. */
10370 if (token->type == CPP_ELLIPSIS)
10371 return cp_parser_type_parameter (parser, is_parameter_pack);
10372 /* If it's an identifier, skip it. */
10373 if (token->type == CPP_NAME)
10374 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10375 /* Now, see if the token looks like the end of a template
10376 parameter. */
10377 if (token->type == CPP_COMMA
10378 || token->type == CPP_EQ
10379 || token->type == CPP_GREATER)
10380 return cp_parser_type_parameter (parser, is_parameter_pack);
10381 }
10382
10383 /* Otherwise, it is a non-type parameter.
10384
10385 [temp.param]
10386
10387 When parsing a default template-argument for a non-type
10388 template-parameter, the first non-nested `>' is taken as the end
10389 of the template parameter-list rather than a greater-than
10390 operator. */
10391 *is_non_type = true;
10392 parameter_declarator
10393 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10394 /*parenthesized_p=*/NULL);
10395
10396 /* If the parameter declaration is marked as a parameter pack, set
10397 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10398 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10399 grokdeclarator. */
10400 if (parameter_declarator
10401 && parameter_declarator->declarator
10402 && parameter_declarator->declarator->parameter_pack_p)
10403 {
10404 *is_parameter_pack = true;
10405 parameter_declarator->declarator->parameter_pack_p = false;
10406 }
10407
10408 /* If the next token is an ellipsis, and we don't already have it
10409 marked as a parameter pack, then we have a parameter pack (that
10410 has no declarator). */
10411 if (!*is_parameter_pack
10412 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10413 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10414 {
10415 /* Consume the `...'. */
10416 cp_lexer_consume_token (parser->lexer);
10417 maybe_warn_variadic_templates ();
10418
10419 *is_parameter_pack = true;
10420 }
10421 /* We might end up with a pack expansion as the type of the non-type
10422 template parameter, in which case this is a non-type template
10423 parameter pack. */
10424 else if (parameter_declarator
10425 && parameter_declarator->decl_specifiers.type
10426 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10427 {
10428 *is_parameter_pack = true;
10429 parameter_declarator->decl_specifiers.type =
10430 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10431 }
10432
10433 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10434 {
10435 /* Parameter packs cannot have default arguments. However, a
10436 user may try to do so, so we'll parse them and give an
10437 appropriate diagnostic here. */
10438
10439 /* Consume the `='. */
10440 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10441 cp_lexer_consume_token (parser->lexer);
10442
10443 /* Find the name of the parameter pack. */
10444 id_declarator = parameter_declarator->declarator;
10445 while (id_declarator && id_declarator->kind != cdk_id)
10446 id_declarator = id_declarator->declarator;
10447
10448 if (id_declarator && id_declarator->kind == cdk_id)
10449 error_at (start_token->location,
10450 "template parameter pack %qD cannot have a default argument",
10451 id_declarator->u.id.unqualified_name);
10452 else
10453 error_at (start_token->location,
10454 "template parameter pack cannot have a default argument");
10455
10456 /* Parse the default argument, but throw away the result. */
10457 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10458 }
10459
10460 parm = grokdeclarator (parameter_declarator->declarator,
10461 &parameter_declarator->decl_specifiers,
10462 PARM, /*initialized=*/0,
10463 /*attrlist=*/NULL);
10464 if (parm == error_mark_node)
10465 return error_mark_node;
10466
10467 return build_tree_list (parameter_declarator->default_argument, parm);
10468 }
10469
10470 /* Parse a type-parameter.
10471
10472 type-parameter:
10473 class identifier [opt]
10474 class identifier [opt] = type-id
10475 typename identifier [opt]
10476 typename identifier [opt] = type-id
10477 template < template-parameter-list > class identifier [opt]
10478 template < template-parameter-list > class identifier [opt]
10479 = id-expression
10480
10481 GNU Extension (variadic templates):
10482
10483 type-parameter:
10484 class ... identifier [opt]
10485 typename ... identifier [opt]
10486
10487 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10488 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10489 the declaration of the parameter.
10490
10491 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10492
10493 static tree
10494 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10495 {
10496 cp_token *token;
10497 tree parameter;
10498
10499 /* Look for a keyword to tell us what kind of parameter this is. */
10500 token = cp_parser_require (parser, CPP_KEYWORD,
10501 "%<class%>, %<typename%>, or %<template%>");
10502 if (!token)
10503 return error_mark_node;
10504
10505 switch (token->keyword)
10506 {
10507 case RID_CLASS:
10508 case RID_TYPENAME:
10509 {
10510 tree identifier;
10511 tree default_argument;
10512
10513 /* If the next token is an ellipsis, we have a template
10514 argument pack. */
10515 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10516 {
10517 /* Consume the `...' token. */
10518 cp_lexer_consume_token (parser->lexer);
10519 maybe_warn_variadic_templates ();
10520
10521 *is_parameter_pack = true;
10522 }
10523
10524 /* If the next token is an identifier, then it names the
10525 parameter. */
10526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10527 identifier = cp_parser_identifier (parser);
10528 else
10529 identifier = NULL_TREE;
10530
10531 /* Create the parameter. */
10532 parameter = finish_template_type_parm (class_type_node, identifier);
10533
10534 /* If the next token is an `=', we have a default argument. */
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10536 {
10537 /* Consume the `=' token. */
10538 cp_lexer_consume_token (parser->lexer);
10539 /* Parse the default-argument. */
10540 push_deferring_access_checks (dk_no_deferred);
10541 default_argument = cp_parser_type_id (parser);
10542
10543 /* Template parameter packs cannot have default
10544 arguments. */
10545 if (*is_parameter_pack)
10546 {
10547 if (identifier)
10548 error_at (token->location,
10549 "template parameter pack %qD cannot have a "
10550 "default argument", identifier);
10551 else
10552 error_at (token->location,
10553 "template parameter packs cannot have "
10554 "default arguments");
10555 default_argument = NULL_TREE;
10556 }
10557 pop_deferring_access_checks ();
10558 }
10559 else
10560 default_argument = NULL_TREE;
10561
10562 /* Create the combined representation of the parameter and the
10563 default argument. */
10564 parameter = build_tree_list (default_argument, parameter);
10565 }
10566 break;
10567
10568 case RID_TEMPLATE:
10569 {
10570 tree parameter_list;
10571 tree identifier;
10572 tree default_argument;
10573
10574 /* Look for the `<'. */
10575 cp_parser_require (parser, CPP_LESS, "%<<%>");
10576 /* Parse the template-parameter-list. */
10577 parameter_list = cp_parser_template_parameter_list (parser);
10578 /* Look for the `>'. */
10579 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10580 /* Look for the `class' keyword. */
10581 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10582 /* If the next token is an ellipsis, we have a template
10583 argument pack. */
10584 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10585 {
10586 /* Consume the `...' token. */
10587 cp_lexer_consume_token (parser->lexer);
10588 maybe_warn_variadic_templates ();
10589
10590 *is_parameter_pack = true;
10591 }
10592 /* If the next token is an `=', then there is a
10593 default-argument. If the next token is a `>', we are at
10594 the end of the parameter-list. If the next token is a `,',
10595 then we are at the end of this parameter. */
10596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10597 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10598 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10599 {
10600 identifier = cp_parser_identifier (parser);
10601 /* Treat invalid names as if the parameter were nameless. */
10602 if (identifier == error_mark_node)
10603 identifier = NULL_TREE;
10604 }
10605 else
10606 identifier = NULL_TREE;
10607
10608 /* Create the template parameter. */
10609 parameter = finish_template_template_parm (class_type_node,
10610 identifier);
10611
10612 /* If the next token is an `=', then there is a
10613 default-argument. */
10614 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10615 {
10616 bool is_template;
10617
10618 /* Consume the `='. */
10619 cp_lexer_consume_token (parser->lexer);
10620 /* Parse the id-expression. */
10621 push_deferring_access_checks (dk_no_deferred);
10622 /* save token before parsing the id-expression, for error
10623 reporting */
10624 token = cp_lexer_peek_token (parser->lexer);
10625 default_argument
10626 = cp_parser_id_expression (parser,
10627 /*template_keyword_p=*/false,
10628 /*check_dependency_p=*/true,
10629 /*template_p=*/&is_template,
10630 /*declarator_p=*/false,
10631 /*optional_p=*/false);
10632 if (TREE_CODE (default_argument) == TYPE_DECL)
10633 /* If the id-expression was a template-id that refers to
10634 a template-class, we already have the declaration here,
10635 so no further lookup is needed. */
10636 ;
10637 else
10638 /* Look up the name. */
10639 default_argument
10640 = cp_parser_lookup_name (parser, default_argument,
10641 none_type,
10642 /*is_template=*/is_template,
10643 /*is_namespace=*/false,
10644 /*check_dependency=*/true,
10645 /*ambiguous_decls=*/NULL,
10646 token->location);
10647 /* See if the default argument is valid. */
10648 default_argument
10649 = check_template_template_default_arg (default_argument);
10650
10651 /* Template parameter packs cannot have default
10652 arguments. */
10653 if (*is_parameter_pack)
10654 {
10655 if (identifier)
10656 error_at (token->location,
10657 "template parameter pack %qD cannot "
10658 "have a default argument",
10659 identifier);
10660 else
10661 error_at (token->location, "template parameter packs cannot "
10662 "have default arguments");
10663 default_argument = NULL_TREE;
10664 }
10665 pop_deferring_access_checks ();
10666 }
10667 else
10668 default_argument = NULL_TREE;
10669
10670 /* Create the combined representation of the parameter and the
10671 default argument. */
10672 parameter = build_tree_list (default_argument, parameter);
10673 }
10674 break;
10675
10676 default:
10677 gcc_unreachable ();
10678 break;
10679 }
10680
10681 return parameter;
10682 }
10683
10684 /* Parse a template-id.
10685
10686 template-id:
10687 template-name < template-argument-list [opt] >
10688
10689 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10690 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10691 returned. Otherwise, if the template-name names a function, or set
10692 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10693 names a class, returns a TYPE_DECL for the specialization.
10694
10695 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10696 uninstantiated templates. */
10697
10698 static tree
10699 cp_parser_template_id (cp_parser *parser,
10700 bool template_keyword_p,
10701 bool check_dependency_p,
10702 bool is_declaration)
10703 {
10704 int i;
10705 tree templ;
10706 tree arguments;
10707 tree template_id;
10708 cp_token_position start_of_id = 0;
10709 deferred_access_check *chk;
10710 VEC (deferred_access_check,gc) *access_check;
10711 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10712 bool is_identifier;
10713
10714 /* If the next token corresponds to a template-id, there is no need
10715 to reparse it. */
10716 next_token = cp_lexer_peek_token (parser->lexer);
10717 if (next_token->type == CPP_TEMPLATE_ID)
10718 {
10719 struct tree_check *check_value;
10720
10721 /* Get the stored value. */
10722 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10723 /* Perform any access checks that were deferred. */
10724 access_check = check_value->checks;
10725 if (access_check)
10726 {
10727 for (i = 0 ;
10728 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10729 ++i)
10730 {
10731 perform_or_defer_access_check (chk->binfo,
10732 chk->decl,
10733 chk->diag_decl);
10734 }
10735 }
10736 /* Return the stored value. */
10737 return check_value->value;
10738 }
10739
10740 /* Avoid performing name lookup if there is no possibility of
10741 finding a template-id. */
10742 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10743 || (next_token->type == CPP_NAME
10744 && !cp_parser_nth_token_starts_template_argument_list_p
10745 (parser, 2)))
10746 {
10747 cp_parser_error (parser, "expected template-id");
10748 return error_mark_node;
10749 }
10750
10751 /* Remember where the template-id starts. */
10752 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10753 start_of_id = cp_lexer_token_position (parser->lexer, false);
10754
10755 push_deferring_access_checks (dk_deferred);
10756
10757 /* Parse the template-name. */
10758 is_identifier = false;
10759 token = cp_lexer_peek_token (parser->lexer);
10760 templ = cp_parser_template_name (parser, template_keyword_p,
10761 check_dependency_p,
10762 is_declaration,
10763 &is_identifier);
10764 if (templ == error_mark_node || is_identifier)
10765 {
10766 pop_deferring_access_checks ();
10767 return templ;
10768 }
10769
10770 /* If we find the sequence `[:' after a template-name, it's probably
10771 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10772 parse correctly the argument list. */
10773 next_token = cp_lexer_peek_token (parser->lexer);
10774 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10775 if (next_token->type == CPP_OPEN_SQUARE
10776 && next_token->flags & DIGRAPH
10777 && next_token_2->type == CPP_COLON
10778 && !(next_token_2->flags & PREV_WHITE))
10779 {
10780 cp_parser_parse_tentatively (parser);
10781 /* Change `:' into `::'. */
10782 next_token_2->type = CPP_SCOPE;
10783 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10784 CPP_LESS. */
10785 cp_lexer_consume_token (parser->lexer);
10786
10787 /* Parse the arguments. */
10788 arguments = cp_parser_enclosed_template_argument_list (parser);
10789 if (!cp_parser_parse_definitely (parser))
10790 {
10791 /* If we couldn't parse an argument list, then we revert our changes
10792 and return simply an error. Maybe this is not a template-id
10793 after all. */
10794 next_token_2->type = CPP_COLON;
10795 cp_parser_error (parser, "expected %<<%>");
10796 pop_deferring_access_checks ();
10797 return error_mark_node;
10798 }
10799 /* Otherwise, emit an error about the invalid digraph, but continue
10800 parsing because we got our argument list. */
10801 if (permerror (next_token->location,
10802 "%<<::%> cannot begin a template-argument list"))
10803 {
10804 static bool hint = false;
10805 inform (next_token->location,
10806 "%<<:%> is an alternate spelling for %<[%>."
10807 " Insert whitespace between %<<%> and %<::%>");
10808 if (!hint && !flag_permissive)
10809 {
10810 inform (next_token->location, "(if you use %<-fpermissive%>"
10811 " G++ will accept your code)");
10812 hint = true;
10813 }
10814 }
10815 }
10816 else
10817 {
10818 /* Look for the `<' that starts the template-argument-list. */
10819 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10820 {
10821 pop_deferring_access_checks ();
10822 return error_mark_node;
10823 }
10824 /* Parse the arguments. */
10825 arguments = cp_parser_enclosed_template_argument_list (parser);
10826 }
10827
10828 /* Build a representation of the specialization. */
10829 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10830 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10831 else if (DECL_CLASS_TEMPLATE_P (templ)
10832 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10833 {
10834 bool entering_scope;
10835 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10836 template (rather than some instantiation thereof) only if
10837 is not nested within some other construct. For example, in
10838 "template <typename T> void f(T) { A<T>::", A<T> is just an
10839 instantiation of A. */
10840 entering_scope = (template_parm_scope_p ()
10841 && cp_lexer_next_token_is (parser->lexer,
10842 CPP_SCOPE));
10843 template_id
10844 = finish_template_type (templ, arguments, entering_scope);
10845 }
10846 else
10847 {
10848 /* If it's not a class-template or a template-template, it should be
10849 a function-template. */
10850 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10851 || TREE_CODE (templ) == OVERLOAD
10852 || BASELINK_P (templ)));
10853
10854 template_id = lookup_template_function (templ, arguments);
10855 }
10856
10857 /* If parsing tentatively, replace the sequence of tokens that makes
10858 up the template-id with a CPP_TEMPLATE_ID token. That way,
10859 should we re-parse the token stream, we will not have to repeat
10860 the effort required to do the parse, nor will we issue duplicate
10861 error messages about problems during instantiation of the
10862 template. */
10863 if (start_of_id)
10864 {
10865 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10866
10867 /* Reset the contents of the START_OF_ID token. */
10868 token->type = CPP_TEMPLATE_ID;
10869 /* Retrieve any deferred checks. Do not pop this access checks yet
10870 so the memory will not be reclaimed during token replacing below. */
10871 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10872 token->u.tree_check_value->value = template_id;
10873 token->u.tree_check_value->checks = get_deferred_access_checks ();
10874 token->keyword = RID_MAX;
10875
10876 /* Purge all subsequent tokens. */
10877 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10878
10879 /* ??? Can we actually assume that, if template_id ==
10880 error_mark_node, we will have issued a diagnostic to the
10881 user, as opposed to simply marking the tentative parse as
10882 failed? */
10883 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10884 error_at (token->location, "parse error in template argument list");
10885 }
10886
10887 pop_deferring_access_checks ();
10888 return template_id;
10889 }
10890
10891 /* Parse a template-name.
10892
10893 template-name:
10894 identifier
10895
10896 The standard should actually say:
10897
10898 template-name:
10899 identifier
10900 operator-function-id
10901
10902 A defect report has been filed about this issue.
10903
10904 A conversion-function-id cannot be a template name because they cannot
10905 be part of a template-id. In fact, looking at this code:
10906
10907 a.operator K<int>()
10908
10909 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10910 It is impossible to call a templated conversion-function-id with an
10911 explicit argument list, since the only allowed template parameter is
10912 the type to which it is converting.
10913
10914 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10915 `template' keyword, in a construction like:
10916
10917 T::template f<3>()
10918
10919 In that case `f' is taken to be a template-name, even though there
10920 is no way of knowing for sure.
10921
10922 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10923 name refers to a set of overloaded functions, at least one of which
10924 is a template, or an IDENTIFIER_NODE with the name of the template,
10925 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10926 names are looked up inside uninstantiated templates. */
10927
10928 static tree
10929 cp_parser_template_name (cp_parser* parser,
10930 bool template_keyword_p,
10931 bool check_dependency_p,
10932 bool is_declaration,
10933 bool *is_identifier)
10934 {
10935 tree identifier;
10936 tree decl;
10937 tree fns;
10938 cp_token *token = cp_lexer_peek_token (parser->lexer);
10939
10940 /* If the next token is `operator', then we have either an
10941 operator-function-id or a conversion-function-id. */
10942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10943 {
10944 /* We don't know whether we're looking at an
10945 operator-function-id or a conversion-function-id. */
10946 cp_parser_parse_tentatively (parser);
10947 /* Try an operator-function-id. */
10948 identifier = cp_parser_operator_function_id (parser);
10949 /* If that didn't work, try a conversion-function-id. */
10950 if (!cp_parser_parse_definitely (parser))
10951 {
10952 cp_parser_error (parser, "expected template-name");
10953 return error_mark_node;
10954 }
10955 }
10956 /* Look for the identifier. */
10957 else
10958 identifier = cp_parser_identifier (parser);
10959
10960 /* If we didn't find an identifier, we don't have a template-id. */
10961 if (identifier == error_mark_node)
10962 return error_mark_node;
10963
10964 /* If the name immediately followed the `template' keyword, then it
10965 is a template-name. However, if the next token is not `<', then
10966 we do not treat it as a template-name, since it is not being used
10967 as part of a template-id. This enables us to handle constructs
10968 like:
10969
10970 template <typename T> struct S { S(); };
10971 template <typename T> S<T>::S();
10972
10973 correctly. We would treat `S' as a template -- if it were `S<T>'
10974 -- but we do not if there is no `<'. */
10975
10976 if (processing_template_decl
10977 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10978 {
10979 /* In a declaration, in a dependent context, we pretend that the
10980 "template" keyword was present in order to improve error
10981 recovery. For example, given:
10982
10983 template <typename T> void f(T::X<int>);
10984
10985 we want to treat "X<int>" as a template-id. */
10986 if (is_declaration
10987 && !template_keyword_p
10988 && parser->scope && TYPE_P (parser->scope)
10989 && check_dependency_p
10990 && dependent_scope_p (parser->scope)
10991 /* Do not do this for dtors (or ctors), since they never
10992 need the template keyword before their name. */
10993 && !constructor_name_p (identifier, parser->scope))
10994 {
10995 cp_token_position start = 0;
10996
10997 /* Explain what went wrong. */
10998 error_at (token->location, "non-template %qD used as template",
10999 identifier);
11000 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11001 parser->scope, identifier);
11002 /* If parsing tentatively, find the location of the "<" token. */
11003 if (cp_parser_simulate_error (parser))
11004 start = cp_lexer_token_position (parser->lexer, true);
11005 /* Parse the template arguments so that we can issue error
11006 messages about them. */
11007 cp_lexer_consume_token (parser->lexer);
11008 cp_parser_enclosed_template_argument_list (parser);
11009 /* Skip tokens until we find a good place from which to
11010 continue parsing. */
11011 cp_parser_skip_to_closing_parenthesis (parser,
11012 /*recovering=*/true,
11013 /*or_comma=*/true,
11014 /*consume_paren=*/false);
11015 /* If parsing tentatively, permanently remove the
11016 template argument list. That will prevent duplicate
11017 error messages from being issued about the missing
11018 "template" keyword. */
11019 if (start)
11020 cp_lexer_purge_tokens_after (parser->lexer, start);
11021 if (is_identifier)
11022 *is_identifier = true;
11023 return identifier;
11024 }
11025
11026 /* If the "template" keyword is present, then there is generally
11027 no point in doing name-lookup, so we just return IDENTIFIER.
11028 But, if the qualifying scope is non-dependent then we can
11029 (and must) do name-lookup normally. */
11030 if (template_keyword_p
11031 && (!parser->scope
11032 || (TYPE_P (parser->scope)
11033 && dependent_type_p (parser->scope))))
11034 return identifier;
11035 }
11036
11037 /* Look up the name. */
11038 decl = cp_parser_lookup_name (parser, identifier,
11039 none_type,
11040 /*is_template=*/false,
11041 /*is_namespace=*/false,
11042 check_dependency_p,
11043 /*ambiguous_decls=*/NULL,
11044 token->location);
11045 decl = maybe_get_template_decl_from_type_decl (decl);
11046
11047 /* If DECL is a template, then the name was a template-name. */
11048 if (TREE_CODE (decl) == TEMPLATE_DECL)
11049 ;
11050 else
11051 {
11052 tree fn = NULL_TREE;
11053
11054 /* The standard does not explicitly indicate whether a name that
11055 names a set of overloaded declarations, some of which are
11056 templates, is a template-name. However, such a name should
11057 be a template-name; otherwise, there is no way to form a
11058 template-id for the overloaded templates. */
11059 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11060 if (TREE_CODE (fns) == OVERLOAD)
11061 for (fn = fns; fn; fn = OVL_NEXT (fn))
11062 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11063 break;
11064
11065 if (!fn)
11066 {
11067 /* The name does not name a template. */
11068 cp_parser_error (parser, "expected template-name");
11069 return error_mark_node;
11070 }
11071 }
11072
11073 /* If DECL is dependent, and refers to a function, then just return
11074 its name; we will look it up again during template instantiation. */
11075 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11076 {
11077 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11078 if (TYPE_P (scope) && dependent_type_p (scope))
11079 return identifier;
11080 }
11081
11082 return decl;
11083 }
11084
11085 /* Parse a template-argument-list.
11086
11087 template-argument-list:
11088 template-argument ... [opt]
11089 template-argument-list , template-argument ... [opt]
11090
11091 Returns a TREE_VEC containing the arguments. */
11092
11093 static tree
11094 cp_parser_template_argument_list (cp_parser* parser)
11095 {
11096 tree fixed_args[10];
11097 unsigned n_args = 0;
11098 unsigned alloced = 10;
11099 tree *arg_ary = fixed_args;
11100 tree vec;
11101 bool saved_in_template_argument_list_p;
11102 bool saved_ice_p;
11103 bool saved_non_ice_p;
11104
11105 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11106 parser->in_template_argument_list_p = true;
11107 /* Even if the template-id appears in an integral
11108 constant-expression, the contents of the argument list do
11109 not. */
11110 saved_ice_p = parser->integral_constant_expression_p;
11111 parser->integral_constant_expression_p = false;
11112 saved_non_ice_p = parser->non_integral_constant_expression_p;
11113 parser->non_integral_constant_expression_p = false;
11114 /* Parse the arguments. */
11115 do
11116 {
11117 tree argument;
11118
11119 if (n_args)
11120 /* Consume the comma. */
11121 cp_lexer_consume_token (parser->lexer);
11122
11123 /* Parse the template-argument. */
11124 argument = cp_parser_template_argument (parser);
11125
11126 /* If the next token is an ellipsis, we're expanding a template
11127 argument pack. */
11128 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11129 {
11130 if (argument == error_mark_node)
11131 {
11132 cp_token *token = cp_lexer_peek_token (parser->lexer);
11133 error_at (token->location,
11134 "expected parameter pack before %<...%>");
11135 }
11136 /* Consume the `...' token. */
11137 cp_lexer_consume_token (parser->lexer);
11138
11139 /* Make the argument into a TYPE_PACK_EXPANSION or
11140 EXPR_PACK_EXPANSION. */
11141 argument = make_pack_expansion (argument);
11142 }
11143
11144 if (n_args == alloced)
11145 {
11146 alloced *= 2;
11147
11148 if (arg_ary == fixed_args)
11149 {
11150 arg_ary = XNEWVEC (tree, alloced);
11151 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11152 }
11153 else
11154 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11155 }
11156 arg_ary[n_args++] = argument;
11157 }
11158 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11159
11160 vec = make_tree_vec (n_args);
11161
11162 while (n_args--)
11163 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11164
11165 if (arg_ary != fixed_args)
11166 free (arg_ary);
11167 parser->non_integral_constant_expression_p = saved_non_ice_p;
11168 parser->integral_constant_expression_p = saved_ice_p;
11169 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11170 return vec;
11171 }
11172
11173 /* Parse a template-argument.
11174
11175 template-argument:
11176 assignment-expression
11177 type-id
11178 id-expression
11179
11180 The representation is that of an assignment-expression, type-id, or
11181 id-expression -- except that the qualified id-expression is
11182 evaluated, so that the value returned is either a DECL or an
11183 OVERLOAD.
11184
11185 Although the standard says "assignment-expression", it forbids
11186 throw-expressions or assignments in the template argument.
11187 Therefore, we use "conditional-expression" instead. */
11188
11189 static tree
11190 cp_parser_template_argument (cp_parser* parser)
11191 {
11192 tree argument;
11193 bool template_p;
11194 bool address_p;
11195 bool maybe_type_id = false;
11196 cp_token *token = NULL, *argument_start_token = NULL;
11197 cp_id_kind idk;
11198
11199 /* There's really no way to know what we're looking at, so we just
11200 try each alternative in order.
11201
11202 [temp.arg]
11203
11204 In a template-argument, an ambiguity between a type-id and an
11205 expression is resolved to a type-id, regardless of the form of
11206 the corresponding template-parameter.
11207
11208 Therefore, we try a type-id first. */
11209 cp_parser_parse_tentatively (parser);
11210 argument = cp_parser_template_type_arg (parser);
11211 /* If there was no error parsing the type-id but the next token is a
11212 '>>', our behavior depends on which dialect of C++ we're
11213 parsing. In C++98, we probably found a typo for '> >'. But there
11214 are type-id which are also valid expressions. For instance:
11215
11216 struct X { int operator >> (int); };
11217 template <int V> struct Foo {};
11218 Foo<X () >> 5> r;
11219
11220 Here 'X()' is a valid type-id of a function type, but the user just
11221 wanted to write the expression "X() >> 5". Thus, we remember that we
11222 found a valid type-id, but we still try to parse the argument as an
11223 expression to see what happens.
11224
11225 In C++0x, the '>>' will be considered two separate '>'
11226 tokens. */
11227 if (!cp_parser_error_occurred (parser)
11228 && cxx_dialect == cxx98
11229 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11230 {
11231 maybe_type_id = true;
11232 cp_parser_abort_tentative_parse (parser);
11233 }
11234 else
11235 {
11236 /* If the next token isn't a `,' or a `>', then this argument wasn't
11237 really finished. This means that the argument is not a valid
11238 type-id. */
11239 if (!cp_parser_next_token_ends_template_argument_p (parser))
11240 cp_parser_error (parser, "expected template-argument");
11241 /* If that worked, we're done. */
11242 if (cp_parser_parse_definitely (parser))
11243 return argument;
11244 }
11245 /* We're still not sure what the argument will be. */
11246 cp_parser_parse_tentatively (parser);
11247 /* Try a template. */
11248 argument_start_token = cp_lexer_peek_token (parser->lexer);
11249 argument = cp_parser_id_expression (parser,
11250 /*template_keyword_p=*/false,
11251 /*check_dependency_p=*/true,
11252 &template_p,
11253 /*declarator_p=*/false,
11254 /*optional_p=*/false);
11255 /* If the next token isn't a `,' or a `>', then this argument wasn't
11256 really finished. */
11257 if (!cp_parser_next_token_ends_template_argument_p (parser))
11258 cp_parser_error (parser, "expected template-argument");
11259 if (!cp_parser_error_occurred (parser))
11260 {
11261 /* Figure out what is being referred to. If the id-expression
11262 was for a class template specialization, then we will have a
11263 TYPE_DECL at this point. There is no need to do name lookup
11264 at this point in that case. */
11265 if (TREE_CODE (argument) != TYPE_DECL)
11266 argument = cp_parser_lookup_name (parser, argument,
11267 none_type,
11268 /*is_template=*/template_p,
11269 /*is_namespace=*/false,
11270 /*check_dependency=*/true,
11271 /*ambiguous_decls=*/NULL,
11272 argument_start_token->location);
11273 if (TREE_CODE (argument) != TEMPLATE_DECL
11274 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11275 cp_parser_error (parser, "expected template-name");
11276 }
11277 if (cp_parser_parse_definitely (parser))
11278 return argument;
11279 /* It must be a non-type argument. There permitted cases are given
11280 in [temp.arg.nontype]:
11281
11282 -- an integral constant-expression of integral or enumeration
11283 type; or
11284
11285 -- the name of a non-type template-parameter; or
11286
11287 -- the name of an object or function with external linkage...
11288
11289 -- the address of an object or function with external linkage...
11290
11291 -- a pointer to member... */
11292 /* Look for a non-type template parameter. */
11293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11294 {
11295 cp_parser_parse_tentatively (parser);
11296 argument = cp_parser_primary_expression (parser,
11297 /*address_p=*/false,
11298 /*cast_p=*/false,
11299 /*template_arg_p=*/true,
11300 &idk);
11301 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11302 || !cp_parser_next_token_ends_template_argument_p (parser))
11303 cp_parser_simulate_error (parser);
11304 if (cp_parser_parse_definitely (parser))
11305 return argument;
11306 }
11307
11308 /* If the next token is "&", the argument must be the address of an
11309 object or function with external linkage. */
11310 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11311 if (address_p)
11312 cp_lexer_consume_token (parser->lexer);
11313 /* See if we might have an id-expression. */
11314 token = cp_lexer_peek_token (parser->lexer);
11315 if (token->type == CPP_NAME
11316 || token->keyword == RID_OPERATOR
11317 || token->type == CPP_SCOPE
11318 || token->type == CPP_TEMPLATE_ID
11319 || token->type == CPP_NESTED_NAME_SPECIFIER)
11320 {
11321 cp_parser_parse_tentatively (parser);
11322 argument = cp_parser_primary_expression (parser,
11323 address_p,
11324 /*cast_p=*/false,
11325 /*template_arg_p=*/true,
11326 &idk);
11327 if (cp_parser_error_occurred (parser)
11328 || !cp_parser_next_token_ends_template_argument_p (parser))
11329 cp_parser_abort_tentative_parse (parser);
11330 else
11331 {
11332 if (TREE_CODE (argument) == INDIRECT_REF)
11333 {
11334 gcc_assert (REFERENCE_REF_P (argument));
11335 argument = TREE_OPERAND (argument, 0);
11336 }
11337
11338 if (TREE_CODE (argument) == VAR_DECL)
11339 {
11340 /* A variable without external linkage might still be a
11341 valid constant-expression, so no error is issued here
11342 if the external-linkage check fails. */
11343 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11344 cp_parser_simulate_error (parser);
11345 }
11346 else if (is_overloaded_fn (argument))
11347 /* All overloaded functions are allowed; if the external
11348 linkage test does not pass, an error will be issued
11349 later. */
11350 ;
11351 else if (address_p
11352 && (TREE_CODE (argument) == OFFSET_REF
11353 || TREE_CODE (argument) == SCOPE_REF))
11354 /* A pointer-to-member. */
11355 ;
11356 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11357 ;
11358 else
11359 cp_parser_simulate_error (parser);
11360
11361 if (cp_parser_parse_definitely (parser))
11362 {
11363 if (address_p)
11364 argument = build_x_unary_op (ADDR_EXPR, argument,
11365 tf_warning_or_error);
11366 return argument;
11367 }
11368 }
11369 }
11370 /* If the argument started with "&", there are no other valid
11371 alternatives at this point. */
11372 if (address_p)
11373 {
11374 cp_parser_error (parser, "invalid non-type template argument");
11375 return error_mark_node;
11376 }
11377
11378 /* If the argument wasn't successfully parsed as a type-id followed
11379 by '>>', the argument can only be a constant expression now.
11380 Otherwise, we try parsing the constant-expression tentatively,
11381 because the argument could really be a type-id. */
11382 if (maybe_type_id)
11383 cp_parser_parse_tentatively (parser);
11384 argument = cp_parser_constant_expression (parser,
11385 /*allow_non_constant_p=*/false,
11386 /*non_constant_p=*/NULL);
11387 argument = fold_non_dependent_expr (argument);
11388 if (!maybe_type_id)
11389 return argument;
11390 if (!cp_parser_next_token_ends_template_argument_p (parser))
11391 cp_parser_error (parser, "expected template-argument");
11392 if (cp_parser_parse_definitely (parser))
11393 return argument;
11394 /* We did our best to parse the argument as a non type-id, but that
11395 was the only alternative that matched (albeit with a '>' after
11396 it). We can assume it's just a typo from the user, and a
11397 diagnostic will then be issued. */
11398 return cp_parser_template_type_arg (parser);
11399 }
11400
11401 /* Parse an explicit-instantiation.
11402
11403 explicit-instantiation:
11404 template declaration
11405
11406 Although the standard says `declaration', what it really means is:
11407
11408 explicit-instantiation:
11409 template decl-specifier-seq [opt] declarator [opt] ;
11410
11411 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11412 supposed to be allowed. A defect report has been filed about this
11413 issue.
11414
11415 GNU Extension:
11416
11417 explicit-instantiation:
11418 storage-class-specifier template
11419 decl-specifier-seq [opt] declarator [opt] ;
11420 function-specifier template
11421 decl-specifier-seq [opt] declarator [opt] ; */
11422
11423 static void
11424 cp_parser_explicit_instantiation (cp_parser* parser)
11425 {
11426 int declares_class_or_enum;
11427 cp_decl_specifier_seq decl_specifiers;
11428 tree extension_specifier = NULL_TREE;
11429 cp_token *token;
11430
11431 /* Look for an (optional) storage-class-specifier or
11432 function-specifier. */
11433 if (cp_parser_allow_gnu_extensions_p (parser))
11434 {
11435 extension_specifier
11436 = cp_parser_storage_class_specifier_opt (parser);
11437 if (!extension_specifier)
11438 extension_specifier
11439 = cp_parser_function_specifier_opt (parser,
11440 /*decl_specs=*/NULL);
11441 }
11442
11443 /* Look for the `template' keyword. */
11444 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11445 /* Let the front end know that we are processing an explicit
11446 instantiation. */
11447 begin_explicit_instantiation ();
11448 /* [temp.explicit] says that we are supposed to ignore access
11449 control while processing explicit instantiation directives. */
11450 push_deferring_access_checks (dk_no_check);
11451 /* Parse a decl-specifier-seq. */
11452 token = cp_lexer_peek_token (parser->lexer);
11453 cp_parser_decl_specifier_seq (parser,
11454 CP_PARSER_FLAGS_OPTIONAL,
11455 &decl_specifiers,
11456 &declares_class_or_enum);
11457 /* If there was exactly one decl-specifier, and it declared a class,
11458 and there's no declarator, then we have an explicit type
11459 instantiation. */
11460 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11461 {
11462 tree type;
11463
11464 type = check_tag_decl (&decl_specifiers);
11465 /* Turn access control back on for names used during
11466 template instantiation. */
11467 pop_deferring_access_checks ();
11468 if (type)
11469 do_type_instantiation (type, extension_specifier,
11470 /*complain=*/tf_error);
11471 }
11472 else
11473 {
11474 cp_declarator *declarator;
11475 tree decl;
11476
11477 /* Parse the declarator. */
11478 declarator
11479 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11480 /*ctor_dtor_or_conv_p=*/NULL,
11481 /*parenthesized_p=*/NULL,
11482 /*member_p=*/false);
11483 if (declares_class_or_enum & 2)
11484 cp_parser_check_for_definition_in_return_type (declarator,
11485 decl_specifiers.type,
11486 decl_specifiers.type_location);
11487 if (declarator != cp_error_declarator)
11488 {
11489 decl = grokdeclarator (declarator, &decl_specifiers,
11490 NORMAL, 0, &decl_specifiers.attributes);
11491 /* Turn access control back on for names used during
11492 template instantiation. */
11493 pop_deferring_access_checks ();
11494 /* Do the explicit instantiation. */
11495 do_decl_instantiation (decl, extension_specifier);
11496 }
11497 else
11498 {
11499 pop_deferring_access_checks ();
11500 /* Skip the body of the explicit instantiation. */
11501 cp_parser_skip_to_end_of_statement (parser);
11502 }
11503 }
11504 /* We're done with the instantiation. */
11505 end_explicit_instantiation ();
11506
11507 cp_parser_consume_semicolon_at_end_of_statement (parser);
11508 }
11509
11510 /* Parse an explicit-specialization.
11511
11512 explicit-specialization:
11513 template < > declaration
11514
11515 Although the standard says `declaration', what it really means is:
11516
11517 explicit-specialization:
11518 template <> decl-specifier [opt] init-declarator [opt] ;
11519 template <> function-definition
11520 template <> explicit-specialization
11521 template <> template-declaration */
11522
11523 static void
11524 cp_parser_explicit_specialization (cp_parser* parser)
11525 {
11526 bool need_lang_pop;
11527 cp_token *token = cp_lexer_peek_token (parser->lexer);
11528
11529 /* Look for the `template' keyword. */
11530 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11531 /* Look for the `<'. */
11532 cp_parser_require (parser, CPP_LESS, "%<<%>");
11533 /* Look for the `>'. */
11534 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11535 /* We have processed another parameter list. */
11536 ++parser->num_template_parameter_lists;
11537 /* [temp]
11538
11539 A template ... explicit specialization ... shall not have C
11540 linkage. */
11541 if (current_lang_name == lang_name_c)
11542 {
11543 error_at (token->location, "template specialization with C linkage");
11544 /* Give it C++ linkage to avoid confusing other parts of the
11545 front end. */
11546 push_lang_context (lang_name_cplusplus);
11547 need_lang_pop = true;
11548 }
11549 else
11550 need_lang_pop = false;
11551 /* Let the front end know that we are beginning a specialization. */
11552 if (!begin_specialization ())
11553 {
11554 end_specialization ();
11555 return;
11556 }
11557
11558 /* If the next keyword is `template', we need to figure out whether
11559 or not we're looking a template-declaration. */
11560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11561 {
11562 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11563 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11564 cp_parser_template_declaration_after_export (parser,
11565 /*member_p=*/false);
11566 else
11567 cp_parser_explicit_specialization (parser);
11568 }
11569 else
11570 /* Parse the dependent declaration. */
11571 cp_parser_single_declaration (parser,
11572 /*checks=*/NULL,
11573 /*member_p=*/false,
11574 /*explicit_specialization_p=*/true,
11575 /*friend_p=*/NULL);
11576 /* We're done with the specialization. */
11577 end_specialization ();
11578 /* For the erroneous case of a template with C linkage, we pushed an
11579 implicit C++ linkage scope; exit that scope now. */
11580 if (need_lang_pop)
11581 pop_lang_context ();
11582 /* We're done with this parameter list. */
11583 --parser->num_template_parameter_lists;
11584 }
11585
11586 /* Parse a type-specifier.
11587
11588 type-specifier:
11589 simple-type-specifier
11590 class-specifier
11591 enum-specifier
11592 elaborated-type-specifier
11593 cv-qualifier
11594
11595 GNU Extension:
11596
11597 type-specifier:
11598 __complex__
11599
11600 Returns a representation of the type-specifier. For a
11601 class-specifier, enum-specifier, or elaborated-type-specifier, a
11602 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11603
11604 The parser flags FLAGS is used to control type-specifier parsing.
11605
11606 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11607 in a decl-specifier-seq.
11608
11609 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11610 class-specifier, enum-specifier, or elaborated-type-specifier, then
11611 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11612 if a type is declared; 2 if it is defined. Otherwise, it is set to
11613 zero.
11614
11615 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11616 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11617 is set to FALSE. */
11618
11619 static tree
11620 cp_parser_type_specifier (cp_parser* parser,
11621 cp_parser_flags flags,
11622 cp_decl_specifier_seq *decl_specs,
11623 bool is_declaration,
11624 int* declares_class_or_enum,
11625 bool* is_cv_qualifier)
11626 {
11627 tree type_spec = NULL_TREE;
11628 cp_token *token;
11629 enum rid keyword;
11630 cp_decl_spec ds = ds_last;
11631
11632 /* Assume this type-specifier does not declare a new type. */
11633 if (declares_class_or_enum)
11634 *declares_class_or_enum = 0;
11635 /* And that it does not specify a cv-qualifier. */
11636 if (is_cv_qualifier)
11637 *is_cv_qualifier = false;
11638 /* Peek at the next token. */
11639 token = cp_lexer_peek_token (parser->lexer);
11640
11641 /* If we're looking at a keyword, we can use that to guide the
11642 production we choose. */
11643 keyword = token->keyword;
11644 switch (keyword)
11645 {
11646 case RID_ENUM:
11647 /* Look for the enum-specifier. */
11648 type_spec = cp_parser_enum_specifier (parser);
11649 /* If that worked, we're done. */
11650 if (type_spec)
11651 {
11652 if (declares_class_or_enum)
11653 *declares_class_or_enum = 2;
11654 if (decl_specs)
11655 cp_parser_set_decl_spec_type (decl_specs,
11656 type_spec,
11657 token->location,
11658 /*user_defined_p=*/true);
11659 return type_spec;
11660 }
11661 else
11662 goto elaborated_type_specifier;
11663
11664 /* Any of these indicate either a class-specifier, or an
11665 elaborated-type-specifier. */
11666 case RID_CLASS:
11667 case RID_STRUCT:
11668 case RID_UNION:
11669 /* Parse tentatively so that we can back up if we don't find a
11670 class-specifier. */
11671 cp_parser_parse_tentatively (parser);
11672 /* Look for the class-specifier. */
11673 type_spec = cp_parser_class_specifier (parser);
11674 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11675 /* If that worked, we're done. */
11676 if (cp_parser_parse_definitely (parser))
11677 {
11678 if (declares_class_or_enum)
11679 *declares_class_or_enum = 2;
11680 if (decl_specs)
11681 cp_parser_set_decl_spec_type (decl_specs,
11682 type_spec,
11683 token->location,
11684 /*user_defined_p=*/true);
11685 return type_spec;
11686 }
11687
11688 /* Fall through. */
11689 elaborated_type_specifier:
11690 /* We're declaring (not defining) a class or enum. */
11691 if (declares_class_or_enum)
11692 *declares_class_or_enum = 1;
11693
11694 /* Fall through. */
11695 case RID_TYPENAME:
11696 /* Look for an elaborated-type-specifier. */
11697 type_spec
11698 = (cp_parser_elaborated_type_specifier
11699 (parser,
11700 decl_specs && decl_specs->specs[(int) ds_friend],
11701 is_declaration));
11702 if (decl_specs)
11703 cp_parser_set_decl_spec_type (decl_specs,
11704 type_spec,
11705 token->location,
11706 /*user_defined_p=*/true);
11707 return type_spec;
11708
11709 case RID_CONST:
11710 ds = ds_const;
11711 if (is_cv_qualifier)
11712 *is_cv_qualifier = true;
11713 break;
11714
11715 case RID_VOLATILE:
11716 ds = ds_volatile;
11717 if (is_cv_qualifier)
11718 *is_cv_qualifier = true;
11719 break;
11720
11721 case RID_RESTRICT:
11722 ds = ds_restrict;
11723 if (is_cv_qualifier)
11724 *is_cv_qualifier = true;
11725 break;
11726
11727 case RID_COMPLEX:
11728 /* The `__complex__' keyword is a GNU extension. */
11729 ds = ds_complex;
11730 break;
11731
11732 default:
11733 break;
11734 }
11735
11736 /* Handle simple keywords. */
11737 if (ds != ds_last)
11738 {
11739 if (decl_specs)
11740 {
11741 ++decl_specs->specs[(int)ds];
11742 decl_specs->any_specifiers_p = true;
11743 }
11744 return cp_lexer_consume_token (parser->lexer)->u.value;
11745 }
11746
11747 /* If we do not already have a type-specifier, assume we are looking
11748 at a simple-type-specifier. */
11749 type_spec = cp_parser_simple_type_specifier (parser,
11750 decl_specs,
11751 flags);
11752
11753 /* If we didn't find a type-specifier, and a type-specifier was not
11754 optional in this context, issue an error message. */
11755 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11756 {
11757 cp_parser_error (parser, "expected type specifier");
11758 return error_mark_node;
11759 }
11760
11761 return type_spec;
11762 }
11763
11764 /* Parse a simple-type-specifier.
11765
11766 simple-type-specifier:
11767 :: [opt] nested-name-specifier [opt] type-name
11768 :: [opt] nested-name-specifier template template-id
11769 char
11770 wchar_t
11771 bool
11772 short
11773 int
11774 long
11775 signed
11776 unsigned
11777 float
11778 double
11779 void
11780
11781 C++0x Extension:
11782
11783 simple-type-specifier:
11784 auto
11785 decltype ( expression )
11786 char16_t
11787 char32_t
11788
11789 GNU Extension:
11790
11791 simple-type-specifier:
11792 __typeof__ unary-expression
11793 __typeof__ ( type-id )
11794
11795 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11796 appropriately updated. */
11797
11798 static tree
11799 cp_parser_simple_type_specifier (cp_parser* parser,
11800 cp_decl_specifier_seq *decl_specs,
11801 cp_parser_flags flags)
11802 {
11803 tree type = NULL_TREE;
11804 cp_token *token;
11805
11806 /* Peek at the next token. */
11807 token = cp_lexer_peek_token (parser->lexer);
11808
11809 /* If we're looking at a keyword, things are easy. */
11810 switch (token->keyword)
11811 {
11812 case RID_CHAR:
11813 if (decl_specs)
11814 decl_specs->explicit_char_p = true;
11815 type = char_type_node;
11816 break;
11817 case RID_CHAR16:
11818 type = char16_type_node;
11819 break;
11820 case RID_CHAR32:
11821 type = char32_type_node;
11822 break;
11823 case RID_WCHAR:
11824 type = wchar_type_node;
11825 break;
11826 case RID_BOOL:
11827 type = boolean_type_node;
11828 break;
11829 case RID_SHORT:
11830 if (decl_specs)
11831 ++decl_specs->specs[(int) ds_short];
11832 type = short_integer_type_node;
11833 break;
11834 case RID_INT:
11835 if (decl_specs)
11836 decl_specs->explicit_int_p = true;
11837 type = integer_type_node;
11838 break;
11839 case RID_LONG:
11840 if (decl_specs)
11841 ++decl_specs->specs[(int) ds_long];
11842 type = long_integer_type_node;
11843 break;
11844 case RID_SIGNED:
11845 if (decl_specs)
11846 ++decl_specs->specs[(int) ds_signed];
11847 type = integer_type_node;
11848 break;
11849 case RID_UNSIGNED:
11850 if (decl_specs)
11851 ++decl_specs->specs[(int) ds_unsigned];
11852 type = unsigned_type_node;
11853 break;
11854 case RID_FLOAT:
11855 type = float_type_node;
11856 break;
11857 case RID_DOUBLE:
11858 type = double_type_node;
11859 break;
11860 case RID_VOID:
11861 type = void_type_node;
11862 break;
11863
11864 case RID_AUTO:
11865 maybe_warn_cpp0x ("C++0x auto");
11866 type = make_auto ();
11867 break;
11868
11869 case RID_DECLTYPE:
11870 /* Parse the `decltype' type. */
11871 type = cp_parser_decltype (parser);
11872
11873 if (decl_specs)
11874 cp_parser_set_decl_spec_type (decl_specs, type,
11875 token->location,
11876 /*user_defined_p=*/true);
11877
11878 return type;
11879
11880 case RID_TYPEOF:
11881 /* Consume the `typeof' token. */
11882 cp_lexer_consume_token (parser->lexer);
11883 /* Parse the operand to `typeof'. */
11884 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11885 /* If it is not already a TYPE, take its type. */
11886 if (!TYPE_P (type))
11887 type = finish_typeof (type);
11888
11889 if (decl_specs)
11890 cp_parser_set_decl_spec_type (decl_specs, type,
11891 token->location,
11892 /*user_defined_p=*/true);
11893
11894 return type;
11895
11896 default:
11897 break;
11898 }
11899
11900 /* If the type-specifier was for a built-in type, we're done. */
11901 if (type)
11902 {
11903 tree id;
11904
11905 /* Record the type. */
11906 if (decl_specs
11907 && (token->keyword != RID_SIGNED
11908 && token->keyword != RID_UNSIGNED
11909 && token->keyword != RID_SHORT
11910 && token->keyword != RID_LONG))
11911 cp_parser_set_decl_spec_type (decl_specs,
11912 type,
11913 token->location,
11914 /*user_defined=*/false);
11915 if (decl_specs)
11916 decl_specs->any_specifiers_p = true;
11917
11918 /* Consume the token. */
11919 id = cp_lexer_consume_token (parser->lexer)->u.value;
11920
11921 /* There is no valid C++ program where a non-template type is
11922 followed by a "<". That usually indicates that the user thought
11923 that the type was a template. */
11924 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11925
11926 return TYPE_NAME (type);
11927 }
11928
11929 /* The type-specifier must be a user-defined type. */
11930 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11931 {
11932 bool qualified_p;
11933 bool global_p;
11934
11935 /* Don't gobble tokens or issue error messages if this is an
11936 optional type-specifier. */
11937 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11938 cp_parser_parse_tentatively (parser);
11939
11940 /* Look for the optional `::' operator. */
11941 global_p
11942 = (cp_parser_global_scope_opt (parser,
11943 /*current_scope_valid_p=*/false)
11944 != NULL_TREE);
11945 /* Look for the nested-name specifier. */
11946 qualified_p
11947 = (cp_parser_nested_name_specifier_opt (parser,
11948 /*typename_keyword_p=*/false,
11949 /*check_dependency_p=*/true,
11950 /*type_p=*/false,
11951 /*is_declaration=*/false)
11952 != NULL_TREE);
11953 token = cp_lexer_peek_token (parser->lexer);
11954 /* If we have seen a nested-name-specifier, and the next token
11955 is `template', then we are using the template-id production. */
11956 if (parser->scope
11957 && cp_parser_optional_template_keyword (parser))
11958 {
11959 /* Look for the template-id. */
11960 type = cp_parser_template_id (parser,
11961 /*template_keyword_p=*/true,
11962 /*check_dependency_p=*/true,
11963 /*is_declaration=*/false);
11964 /* If the template-id did not name a type, we are out of
11965 luck. */
11966 if (TREE_CODE (type) != TYPE_DECL)
11967 {
11968 cp_parser_error (parser, "expected template-id for type");
11969 type = NULL_TREE;
11970 }
11971 }
11972 /* Otherwise, look for a type-name. */
11973 else
11974 type = cp_parser_type_name (parser);
11975 /* Keep track of all name-lookups performed in class scopes. */
11976 if (type
11977 && !global_p
11978 && !qualified_p
11979 && TREE_CODE (type) == TYPE_DECL
11980 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11981 maybe_note_name_used_in_class (DECL_NAME (type), type);
11982 /* If it didn't work out, we don't have a TYPE. */
11983 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11984 && !cp_parser_parse_definitely (parser))
11985 type = NULL_TREE;
11986 if (type && decl_specs)
11987 cp_parser_set_decl_spec_type (decl_specs, type,
11988 token->location,
11989 /*user_defined=*/true);
11990 }
11991
11992 /* If we didn't get a type-name, issue an error message. */
11993 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11994 {
11995 cp_parser_error (parser, "expected type-name");
11996 return error_mark_node;
11997 }
11998
11999 /* There is no valid C++ program where a non-template type is
12000 followed by a "<". That usually indicates that the user thought
12001 that the type was a template. */
12002 if (type && type != error_mark_node)
12003 {
12004 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12005 If it is, then the '<'...'>' enclose protocol names rather than
12006 template arguments, and so everything is fine. */
12007 if (c_dialect_objc ()
12008 && (objc_is_id (type) || objc_is_class_name (type)))
12009 {
12010 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12011 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12012
12013 /* Clobber the "unqualified" type previously entered into
12014 DECL_SPECS with the new, improved protocol-qualified version. */
12015 if (decl_specs)
12016 decl_specs->type = qual_type;
12017
12018 return qual_type;
12019 }
12020
12021 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12022 token->location);
12023 }
12024
12025 return type;
12026 }
12027
12028 /* Parse a type-name.
12029
12030 type-name:
12031 class-name
12032 enum-name
12033 typedef-name
12034
12035 enum-name:
12036 identifier
12037
12038 typedef-name:
12039 identifier
12040
12041 Returns a TYPE_DECL for the type. */
12042
12043 static tree
12044 cp_parser_type_name (cp_parser* parser)
12045 {
12046 tree type_decl;
12047
12048 /* We can't know yet whether it is a class-name or not. */
12049 cp_parser_parse_tentatively (parser);
12050 /* Try a class-name. */
12051 type_decl = cp_parser_class_name (parser,
12052 /*typename_keyword_p=*/false,
12053 /*template_keyword_p=*/false,
12054 none_type,
12055 /*check_dependency_p=*/true,
12056 /*class_head_p=*/false,
12057 /*is_declaration=*/false);
12058 /* If it's not a class-name, keep looking. */
12059 if (!cp_parser_parse_definitely (parser))
12060 {
12061 /* It must be a typedef-name or an enum-name. */
12062 return cp_parser_nonclass_name (parser);
12063 }
12064
12065 return type_decl;
12066 }
12067
12068 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12069
12070 enum-name:
12071 identifier
12072
12073 typedef-name:
12074 identifier
12075
12076 Returns a TYPE_DECL for the type. */
12077
12078 static tree
12079 cp_parser_nonclass_name (cp_parser* parser)
12080 {
12081 tree type_decl;
12082 tree identifier;
12083
12084 cp_token *token = cp_lexer_peek_token (parser->lexer);
12085 identifier = cp_parser_identifier (parser);
12086 if (identifier == error_mark_node)
12087 return error_mark_node;
12088
12089 /* Look up the type-name. */
12090 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12091
12092 if (TREE_CODE (type_decl) != TYPE_DECL
12093 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12094 {
12095 /* See if this is an Objective-C type. */
12096 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12097 tree type = objc_get_protocol_qualified_type (identifier, protos);
12098 if (type)
12099 type_decl = TYPE_NAME (type);
12100 }
12101
12102 /* Issue an error if we did not find a type-name. */
12103 if (TREE_CODE (type_decl) != TYPE_DECL)
12104 {
12105 if (!cp_parser_simulate_error (parser))
12106 cp_parser_name_lookup_error (parser, identifier, type_decl,
12107 "is not a type", token->location);
12108 return error_mark_node;
12109 }
12110 /* Remember that the name was used in the definition of the
12111 current class so that we can check later to see if the
12112 meaning would have been different after the class was
12113 entirely defined. */
12114 else if (type_decl != error_mark_node
12115 && !parser->scope)
12116 maybe_note_name_used_in_class (identifier, type_decl);
12117
12118 return type_decl;
12119 }
12120
12121 /* Parse an elaborated-type-specifier. Note that the grammar given
12122 here incorporates the resolution to DR68.
12123
12124 elaborated-type-specifier:
12125 class-key :: [opt] nested-name-specifier [opt] identifier
12126 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12127 enum-key :: [opt] nested-name-specifier [opt] identifier
12128 typename :: [opt] nested-name-specifier identifier
12129 typename :: [opt] nested-name-specifier template [opt]
12130 template-id
12131
12132 GNU extension:
12133
12134 elaborated-type-specifier:
12135 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12136 class-key attributes :: [opt] nested-name-specifier [opt]
12137 template [opt] template-id
12138 enum attributes :: [opt] nested-name-specifier [opt] identifier
12139
12140 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12141 declared `friend'. If IS_DECLARATION is TRUE, then this
12142 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12143 something is being declared.
12144
12145 Returns the TYPE specified. */
12146
12147 static tree
12148 cp_parser_elaborated_type_specifier (cp_parser* parser,
12149 bool is_friend,
12150 bool is_declaration)
12151 {
12152 enum tag_types tag_type;
12153 tree identifier;
12154 tree type = NULL_TREE;
12155 tree attributes = NULL_TREE;
12156 tree globalscope;
12157 cp_token *token = NULL;
12158
12159 /* See if we're looking at the `enum' keyword. */
12160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12161 {
12162 /* Consume the `enum' token. */
12163 cp_lexer_consume_token (parser->lexer);
12164 /* Remember that it's an enumeration type. */
12165 tag_type = enum_type;
12166 /* Parse the optional `struct' or `class' key (for C++0x scoped
12167 enums). */
12168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12169 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12170 {
12171 if (cxx_dialect == cxx98)
12172 maybe_warn_cpp0x ("scoped enums");
12173
12174 /* Consume the `struct' or `class'. */
12175 cp_lexer_consume_token (parser->lexer);
12176 }
12177 /* Parse the attributes. */
12178 attributes = cp_parser_attributes_opt (parser);
12179 }
12180 /* Or, it might be `typename'. */
12181 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12182 RID_TYPENAME))
12183 {
12184 /* Consume the `typename' token. */
12185 cp_lexer_consume_token (parser->lexer);
12186 /* Remember that it's a `typename' type. */
12187 tag_type = typename_type;
12188 }
12189 /* Otherwise it must be a class-key. */
12190 else
12191 {
12192 tag_type = cp_parser_class_key (parser);
12193 if (tag_type == none_type)
12194 return error_mark_node;
12195 /* Parse the attributes. */
12196 attributes = cp_parser_attributes_opt (parser);
12197 }
12198
12199 /* Look for the `::' operator. */
12200 globalscope = cp_parser_global_scope_opt (parser,
12201 /*current_scope_valid_p=*/false);
12202 /* Look for the nested-name-specifier. */
12203 if (tag_type == typename_type && !globalscope)
12204 {
12205 if (!cp_parser_nested_name_specifier (parser,
12206 /*typename_keyword_p=*/true,
12207 /*check_dependency_p=*/true,
12208 /*type_p=*/true,
12209 is_declaration))
12210 return error_mark_node;
12211 }
12212 else
12213 /* Even though `typename' is not present, the proposed resolution
12214 to Core Issue 180 says that in `class A<T>::B', `B' should be
12215 considered a type-name, even if `A<T>' is dependent. */
12216 cp_parser_nested_name_specifier_opt (parser,
12217 /*typename_keyword_p=*/true,
12218 /*check_dependency_p=*/true,
12219 /*type_p=*/true,
12220 is_declaration);
12221 /* For everything but enumeration types, consider a template-id.
12222 For an enumeration type, consider only a plain identifier. */
12223 if (tag_type != enum_type)
12224 {
12225 bool template_p = false;
12226 tree decl;
12227
12228 /* Allow the `template' keyword. */
12229 template_p = cp_parser_optional_template_keyword (parser);
12230 /* If we didn't see `template', we don't know if there's a
12231 template-id or not. */
12232 if (!template_p)
12233 cp_parser_parse_tentatively (parser);
12234 /* Parse the template-id. */
12235 token = cp_lexer_peek_token (parser->lexer);
12236 decl = cp_parser_template_id (parser, template_p,
12237 /*check_dependency_p=*/true,
12238 is_declaration);
12239 /* If we didn't find a template-id, look for an ordinary
12240 identifier. */
12241 if (!template_p && !cp_parser_parse_definitely (parser))
12242 ;
12243 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12244 in effect, then we must assume that, upon instantiation, the
12245 template will correspond to a class. */
12246 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12247 && tag_type == typename_type)
12248 type = make_typename_type (parser->scope, decl,
12249 typename_type,
12250 /*complain=*/tf_error);
12251 /* If the `typename' keyword is in effect and DECL is not a type
12252 decl. Then type is non existant. */
12253 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12254 type = NULL_TREE;
12255 else
12256 type = TREE_TYPE (decl);
12257 }
12258
12259 if (!type)
12260 {
12261 token = cp_lexer_peek_token (parser->lexer);
12262 identifier = cp_parser_identifier (parser);
12263
12264 if (identifier == error_mark_node)
12265 {
12266 parser->scope = NULL_TREE;
12267 return error_mark_node;
12268 }
12269
12270 /* For a `typename', we needn't call xref_tag. */
12271 if (tag_type == typename_type
12272 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12273 return cp_parser_make_typename_type (parser, parser->scope,
12274 identifier,
12275 token->location);
12276 /* Look up a qualified name in the usual way. */
12277 if (parser->scope)
12278 {
12279 tree decl;
12280 tree ambiguous_decls;
12281
12282 decl = cp_parser_lookup_name (parser, identifier,
12283 tag_type,
12284 /*is_template=*/false,
12285 /*is_namespace=*/false,
12286 /*check_dependency=*/true,
12287 &ambiguous_decls,
12288 token->location);
12289
12290 /* If the lookup was ambiguous, an error will already have been
12291 issued. */
12292 if (ambiguous_decls)
12293 return error_mark_node;
12294
12295 /* If we are parsing friend declaration, DECL may be a
12296 TEMPLATE_DECL tree node here. However, we need to check
12297 whether this TEMPLATE_DECL results in valid code. Consider
12298 the following example:
12299
12300 namespace N {
12301 template <class T> class C {};
12302 }
12303 class X {
12304 template <class T> friend class N::C; // #1, valid code
12305 };
12306 template <class T> class Y {
12307 friend class N::C; // #2, invalid code
12308 };
12309
12310 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12311 name lookup of `N::C'. We see that friend declaration must
12312 be template for the code to be valid. Note that
12313 processing_template_decl does not work here since it is
12314 always 1 for the above two cases. */
12315
12316 decl = (cp_parser_maybe_treat_template_as_class
12317 (decl, /*tag_name_p=*/is_friend
12318 && parser->num_template_parameter_lists));
12319
12320 if (TREE_CODE (decl) != TYPE_DECL)
12321 {
12322 cp_parser_diagnose_invalid_type_name (parser,
12323 parser->scope,
12324 identifier,
12325 token->location);
12326 return error_mark_node;
12327 }
12328
12329 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12330 {
12331 bool allow_template = (parser->num_template_parameter_lists
12332 || DECL_SELF_REFERENCE_P (decl));
12333 type = check_elaborated_type_specifier (tag_type, decl,
12334 allow_template);
12335
12336 if (type == error_mark_node)
12337 return error_mark_node;
12338 }
12339
12340 /* Forward declarations of nested types, such as
12341
12342 class C1::C2;
12343 class C1::C2::C3;
12344
12345 are invalid unless all components preceding the final '::'
12346 are complete. If all enclosing types are complete, these
12347 declarations become merely pointless.
12348
12349 Invalid forward declarations of nested types are errors
12350 caught elsewhere in parsing. Those that are pointless arrive
12351 here. */
12352
12353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12354 && !is_friend && !processing_explicit_instantiation)
12355 warning (0, "declaration %qD does not declare anything", decl);
12356
12357 type = TREE_TYPE (decl);
12358 }
12359 else
12360 {
12361 /* An elaborated-type-specifier sometimes introduces a new type and
12362 sometimes names an existing type. Normally, the rule is that it
12363 introduces a new type only if there is not an existing type of
12364 the same name already in scope. For example, given:
12365
12366 struct S {};
12367 void f() { struct S s; }
12368
12369 the `struct S' in the body of `f' is the same `struct S' as in
12370 the global scope; the existing definition is used. However, if
12371 there were no global declaration, this would introduce a new
12372 local class named `S'.
12373
12374 An exception to this rule applies to the following code:
12375
12376 namespace N { struct S; }
12377
12378 Here, the elaborated-type-specifier names a new type
12379 unconditionally; even if there is already an `S' in the
12380 containing scope this declaration names a new type.
12381 This exception only applies if the elaborated-type-specifier
12382 forms the complete declaration:
12383
12384 [class.name]
12385
12386 A declaration consisting solely of `class-key identifier ;' is
12387 either a redeclaration of the name in the current scope or a
12388 forward declaration of the identifier as a class name. It
12389 introduces the name into the current scope.
12390
12391 We are in this situation precisely when the next token is a `;'.
12392
12393 An exception to the exception is that a `friend' declaration does
12394 *not* name a new type; i.e., given:
12395
12396 struct S { friend struct T; };
12397
12398 `T' is not a new type in the scope of `S'.
12399
12400 Also, `new struct S' or `sizeof (struct S)' never results in the
12401 definition of a new type; a new type can only be declared in a
12402 declaration context. */
12403
12404 tag_scope ts;
12405 bool template_p;
12406
12407 if (is_friend)
12408 /* Friends have special name lookup rules. */
12409 ts = ts_within_enclosing_non_class;
12410 else if (is_declaration
12411 && cp_lexer_next_token_is (parser->lexer,
12412 CPP_SEMICOLON))
12413 /* This is a `class-key identifier ;' */
12414 ts = ts_current;
12415 else
12416 ts = ts_global;
12417
12418 template_p =
12419 (parser->num_template_parameter_lists
12420 && (cp_parser_next_token_starts_class_definition_p (parser)
12421 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12422 /* An unqualified name was used to reference this type, so
12423 there were no qualifying templates. */
12424 if (!cp_parser_check_template_parameters (parser,
12425 /*num_templates=*/0,
12426 token->location,
12427 /*declarator=*/NULL))
12428 return error_mark_node;
12429 type = xref_tag (tag_type, identifier, ts, template_p);
12430 }
12431 }
12432
12433 if (type == error_mark_node)
12434 return error_mark_node;
12435
12436 /* Allow attributes on forward declarations of classes. */
12437 if (attributes)
12438 {
12439 if (TREE_CODE (type) == TYPENAME_TYPE)
12440 warning (OPT_Wattributes,
12441 "attributes ignored on uninstantiated type");
12442 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12443 && ! processing_explicit_instantiation)
12444 warning (OPT_Wattributes,
12445 "attributes ignored on template instantiation");
12446 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12447 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12448 else
12449 warning (OPT_Wattributes,
12450 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12451 }
12452
12453 if (tag_type != enum_type)
12454 cp_parser_check_class_key (tag_type, type);
12455
12456 /* A "<" cannot follow an elaborated type specifier. If that
12457 happens, the user was probably trying to form a template-id. */
12458 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12459
12460 return type;
12461 }
12462
12463 /* Parse an enum-specifier.
12464
12465 enum-specifier:
12466 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12467
12468 enum-key:
12469 enum
12470 enum class [C++0x]
12471 enum struct [C++0x]
12472
12473 enum-base: [C++0x]
12474 : type-specifier-seq
12475
12476 GNU Extensions:
12477 enum-key attributes[opt] identifier [opt] enum-base [opt]
12478 { enumerator-list [opt] }attributes[opt]
12479
12480 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12481 if the token stream isn't an enum-specifier after all. */
12482
12483 static tree
12484 cp_parser_enum_specifier (cp_parser* parser)
12485 {
12486 tree identifier;
12487 tree type;
12488 tree attributes;
12489 bool scoped_enum_p = false;
12490 bool has_underlying_type = false;
12491 tree underlying_type = NULL_TREE;
12492
12493 /* Parse tentatively so that we can back up if we don't find a
12494 enum-specifier. */
12495 cp_parser_parse_tentatively (parser);
12496
12497 /* Caller guarantees that the current token is 'enum', an identifier
12498 possibly follows, and the token after that is an opening brace.
12499 If we don't have an identifier, fabricate an anonymous name for
12500 the enumeration being defined. */
12501 cp_lexer_consume_token (parser->lexer);
12502
12503 /* Parse the "class" or "struct", which indicates a scoped
12504 enumeration type in C++0x. */
12505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12506 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12507 {
12508 if (cxx_dialect == cxx98)
12509 maybe_warn_cpp0x ("scoped enums");
12510
12511 /* Consume the `struct' or `class' token. */
12512 cp_lexer_consume_token (parser->lexer);
12513
12514 scoped_enum_p = true;
12515 }
12516
12517 attributes = cp_parser_attributes_opt (parser);
12518
12519 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12520 identifier = cp_parser_identifier (parser);
12521 else
12522 identifier = make_anon_name ();
12523
12524 /* Check for the `:' that denotes a specified underlying type in C++0x.
12525 Note that a ':' could also indicate a bitfield width, however. */
12526 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12527 {
12528 cp_decl_specifier_seq type_specifiers;
12529
12530 /* Consume the `:'. */
12531 cp_lexer_consume_token (parser->lexer);
12532
12533 /* Parse the type-specifier-seq. */
12534 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12535 &type_specifiers);
12536
12537 /* At this point this is surely not elaborated type specifier. */
12538 if (!cp_parser_parse_definitely (parser))
12539 return NULL_TREE;
12540
12541 if (cxx_dialect == cxx98)
12542 maybe_warn_cpp0x ("scoped enums");
12543
12544 has_underlying_type = true;
12545
12546 /* If that didn't work, stop. */
12547 if (type_specifiers.type != error_mark_node)
12548 {
12549 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12550 /*initialized=*/0, NULL);
12551 if (underlying_type == error_mark_node)
12552 underlying_type = NULL_TREE;
12553 }
12554 }
12555
12556 /* Look for the `{' but don't consume it yet. */
12557 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12558 {
12559 cp_parser_error (parser, "expected %<{%>");
12560 if (has_underlying_type)
12561 return NULL_TREE;
12562 }
12563
12564 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12565 return NULL_TREE;
12566
12567 /* Issue an error message if type-definitions are forbidden here. */
12568 if (!cp_parser_check_type_definition (parser))
12569 type = error_mark_node;
12570 else
12571 /* Create the new type. We do this before consuming the opening
12572 brace so the enum will be recorded as being on the line of its
12573 tag (or the 'enum' keyword, if there is no tag). */
12574 type = start_enum (identifier, underlying_type, scoped_enum_p);
12575
12576 /* Consume the opening brace. */
12577 cp_lexer_consume_token (parser->lexer);
12578
12579 if (type == error_mark_node)
12580 {
12581 cp_parser_skip_to_end_of_block_or_statement (parser);
12582 return error_mark_node;
12583 }
12584
12585 /* If the next token is not '}', then there are some enumerators. */
12586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12587 cp_parser_enumerator_list (parser, type);
12588
12589 /* Consume the final '}'. */
12590 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12591
12592 /* Look for trailing attributes to apply to this enumeration, and
12593 apply them if appropriate. */
12594 if (cp_parser_allow_gnu_extensions_p (parser))
12595 {
12596 tree trailing_attr = cp_parser_attributes_opt (parser);
12597 trailing_attr = chainon (trailing_attr, attributes);
12598 cplus_decl_attributes (&type,
12599 trailing_attr,
12600 (int) ATTR_FLAG_TYPE_IN_PLACE);
12601 }
12602
12603 /* Finish up the enumeration. */
12604 finish_enum (type);
12605
12606 return type;
12607 }
12608
12609 /* Parse an enumerator-list. The enumerators all have the indicated
12610 TYPE.
12611
12612 enumerator-list:
12613 enumerator-definition
12614 enumerator-list , enumerator-definition */
12615
12616 static void
12617 cp_parser_enumerator_list (cp_parser* parser, tree type)
12618 {
12619 while (true)
12620 {
12621 /* Parse an enumerator-definition. */
12622 cp_parser_enumerator_definition (parser, type);
12623
12624 /* If the next token is not a ',', we've reached the end of
12625 the list. */
12626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12627 break;
12628 /* Otherwise, consume the `,' and keep going. */
12629 cp_lexer_consume_token (parser->lexer);
12630 /* If the next token is a `}', there is a trailing comma. */
12631 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12632 {
12633 if (!in_system_header)
12634 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12635 break;
12636 }
12637 }
12638 }
12639
12640 /* Parse an enumerator-definition. The enumerator has the indicated
12641 TYPE.
12642
12643 enumerator-definition:
12644 enumerator
12645 enumerator = constant-expression
12646
12647 enumerator:
12648 identifier */
12649
12650 static void
12651 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12652 {
12653 tree identifier;
12654 tree value;
12655
12656 /* Look for the identifier. */
12657 identifier = cp_parser_identifier (parser);
12658 if (identifier == error_mark_node)
12659 return;
12660
12661 /* If the next token is an '=', then there is an explicit value. */
12662 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12663 {
12664 /* Consume the `=' token. */
12665 cp_lexer_consume_token (parser->lexer);
12666 /* Parse the value. */
12667 value = cp_parser_constant_expression (parser,
12668 /*allow_non_constant_p=*/false,
12669 NULL);
12670 }
12671 else
12672 value = NULL_TREE;
12673
12674 /* If we are processing a template, make sure the initializer of the
12675 enumerator doesn't contain any bare template parameter pack. */
12676 if (check_for_bare_parameter_packs (value))
12677 value = error_mark_node;
12678
12679 /* Create the enumerator. */
12680 build_enumerator (identifier, value, type);
12681 }
12682
12683 /* Parse a namespace-name.
12684
12685 namespace-name:
12686 original-namespace-name
12687 namespace-alias
12688
12689 Returns the NAMESPACE_DECL for the namespace. */
12690
12691 static tree
12692 cp_parser_namespace_name (cp_parser* parser)
12693 {
12694 tree identifier;
12695 tree namespace_decl;
12696
12697 cp_token *token = cp_lexer_peek_token (parser->lexer);
12698
12699 /* Get the name of the namespace. */
12700 identifier = cp_parser_identifier (parser);
12701 if (identifier == error_mark_node)
12702 return error_mark_node;
12703
12704 /* Look up the identifier in the currently active scope. Look only
12705 for namespaces, due to:
12706
12707 [basic.lookup.udir]
12708
12709 When looking up a namespace-name in a using-directive or alias
12710 definition, only namespace names are considered.
12711
12712 And:
12713
12714 [basic.lookup.qual]
12715
12716 During the lookup of a name preceding the :: scope resolution
12717 operator, object, function, and enumerator names are ignored.
12718
12719 (Note that cp_parser_qualifying_entity only calls this
12720 function if the token after the name is the scope resolution
12721 operator.) */
12722 namespace_decl = cp_parser_lookup_name (parser, identifier,
12723 none_type,
12724 /*is_template=*/false,
12725 /*is_namespace=*/true,
12726 /*check_dependency=*/true,
12727 /*ambiguous_decls=*/NULL,
12728 token->location);
12729 /* If it's not a namespace, issue an error. */
12730 if (namespace_decl == error_mark_node
12731 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12732 {
12733 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12734 error_at (token->location, "%qD is not a namespace-name", identifier);
12735 cp_parser_error (parser, "expected namespace-name");
12736 namespace_decl = error_mark_node;
12737 }
12738
12739 return namespace_decl;
12740 }
12741
12742 /* Parse a namespace-definition.
12743
12744 namespace-definition:
12745 named-namespace-definition
12746 unnamed-namespace-definition
12747
12748 named-namespace-definition:
12749 original-namespace-definition
12750 extension-namespace-definition
12751
12752 original-namespace-definition:
12753 namespace identifier { namespace-body }
12754
12755 extension-namespace-definition:
12756 namespace original-namespace-name { namespace-body }
12757
12758 unnamed-namespace-definition:
12759 namespace { namespace-body } */
12760
12761 static void
12762 cp_parser_namespace_definition (cp_parser* parser)
12763 {
12764 tree identifier, attribs;
12765 bool has_visibility;
12766 bool is_inline;
12767
12768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12769 {
12770 is_inline = true;
12771 cp_lexer_consume_token (parser->lexer);
12772 }
12773 else
12774 is_inline = false;
12775
12776 /* Look for the `namespace' keyword. */
12777 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12778
12779 /* Get the name of the namespace. We do not attempt to distinguish
12780 between an original-namespace-definition and an
12781 extension-namespace-definition at this point. The semantic
12782 analysis routines are responsible for that. */
12783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12784 identifier = cp_parser_identifier (parser);
12785 else
12786 identifier = NULL_TREE;
12787
12788 /* Parse any specified attributes. */
12789 attribs = cp_parser_attributes_opt (parser);
12790
12791 /* Look for the `{' to start the namespace. */
12792 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12793 /* Start the namespace. */
12794 push_namespace (identifier);
12795
12796 /* "inline namespace" is equivalent to a stub namespace definition
12797 followed by a strong using directive. */
12798 if (is_inline)
12799 {
12800 tree name_space = current_namespace;
12801 /* Set up namespace association. */
12802 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12803 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12804 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12805 /* Import the contents of the inline namespace. */
12806 pop_namespace ();
12807 do_using_directive (name_space);
12808 push_namespace (identifier);
12809 }
12810
12811 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12812
12813 /* Parse the body of the namespace. */
12814 cp_parser_namespace_body (parser);
12815
12816 #ifdef HANDLE_PRAGMA_VISIBILITY
12817 if (has_visibility)
12818 pop_visibility ();
12819 #endif
12820
12821 /* Finish the namespace. */
12822 pop_namespace ();
12823 /* Look for the final `}'. */
12824 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12825 }
12826
12827 /* Parse a namespace-body.
12828
12829 namespace-body:
12830 declaration-seq [opt] */
12831
12832 static void
12833 cp_parser_namespace_body (cp_parser* parser)
12834 {
12835 cp_parser_declaration_seq_opt (parser);
12836 }
12837
12838 /* Parse a namespace-alias-definition.
12839
12840 namespace-alias-definition:
12841 namespace identifier = qualified-namespace-specifier ; */
12842
12843 static void
12844 cp_parser_namespace_alias_definition (cp_parser* parser)
12845 {
12846 tree identifier;
12847 tree namespace_specifier;
12848
12849 cp_token *token = cp_lexer_peek_token (parser->lexer);
12850
12851 /* Look for the `namespace' keyword. */
12852 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12853 /* Look for the identifier. */
12854 identifier = cp_parser_identifier (parser);
12855 if (identifier == error_mark_node)
12856 return;
12857 /* Look for the `=' token. */
12858 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12859 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12860 {
12861 error_at (token->location, "%<namespace%> definition is not allowed here");
12862 /* Skip the definition. */
12863 cp_lexer_consume_token (parser->lexer);
12864 if (cp_parser_skip_to_closing_brace (parser))
12865 cp_lexer_consume_token (parser->lexer);
12866 return;
12867 }
12868 cp_parser_require (parser, CPP_EQ, "%<=%>");
12869 /* Look for the qualified-namespace-specifier. */
12870 namespace_specifier
12871 = cp_parser_qualified_namespace_specifier (parser);
12872 /* Look for the `;' token. */
12873 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12874
12875 /* Register the alias in the symbol table. */
12876 do_namespace_alias (identifier, namespace_specifier);
12877 }
12878
12879 /* Parse a qualified-namespace-specifier.
12880
12881 qualified-namespace-specifier:
12882 :: [opt] nested-name-specifier [opt] namespace-name
12883
12884 Returns a NAMESPACE_DECL corresponding to the specified
12885 namespace. */
12886
12887 static tree
12888 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12889 {
12890 /* Look for the optional `::'. */
12891 cp_parser_global_scope_opt (parser,
12892 /*current_scope_valid_p=*/false);
12893
12894 /* Look for the optional nested-name-specifier. */
12895 cp_parser_nested_name_specifier_opt (parser,
12896 /*typename_keyword_p=*/false,
12897 /*check_dependency_p=*/true,
12898 /*type_p=*/false,
12899 /*is_declaration=*/true);
12900
12901 return cp_parser_namespace_name (parser);
12902 }
12903
12904 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12905 access declaration.
12906
12907 using-declaration:
12908 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12909 using :: unqualified-id ;
12910
12911 access-declaration:
12912 qualified-id ;
12913
12914 */
12915
12916 static bool
12917 cp_parser_using_declaration (cp_parser* parser,
12918 bool access_declaration_p)
12919 {
12920 cp_token *token;
12921 bool typename_p = false;
12922 bool global_scope_p;
12923 tree decl;
12924 tree identifier;
12925 tree qscope;
12926
12927 if (access_declaration_p)
12928 cp_parser_parse_tentatively (parser);
12929 else
12930 {
12931 /* Look for the `using' keyword. */
12932 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12933
12934 /* Peek at the next token. */
12935 token = cp_lexer_peek_token (parser->lexer);
12936 /* See if it's `typename'. */
12937 if (token->keyword == RID_TYPENAME)
12938 {
12939 /* Remember that we've seen it. */
12940 typename_p = true;
12941 /* Consume the `typename' token. */
12942 cp_lexer_consume_token (parser->lexer);
12943 }
12944 }
12945
12946 /* Look for the optional global scope qualification. */
12947 global_scope_p
12948 = (cp_parser_global_scope_opt (parser,
12949 /*current_scope_valid_p=*/false)
12950 != NULL_TREE);
12951
12952 /* If we saw `typename', or didn't see `::', then there must be a
12953 nested-name-specifier present. */
12954 if (typename_p || !global_scope_p)
12955 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12956 /*check_dependency_p=*/true,
12957 /*type_p=*/false,
12958 /*is_declaration=*/true);
12959 /* Otherwise, we could be in either of the two productions. In that
12960 case, treat the nested-name-specifier as optional. */
12961 else
12962 qscope = cp_parser_nested_name_specifier_opt (parser,
12963 /*typename_keyword_p=*/false,
12964 /*check_dependency_p=*/true,
12965 /*type_p=*/false,
12966 /*is_declaration=*/true);
12967 if (!qscope)
12968 qscope = global_namespace;
12969
12970 if (access_declaration_p && cp_parser_error_occurred (parser))
12971 /* Something has already gone wrong; there's no need to parse
12972 further. Since an error has occurred, the return value of
12973 cp_parser_parse_definitely will be false, as required. */
12974 return cp_parser_parse_definitely (parser);
12975
12976 token = cp_lexer_peek_token (parser->lexer);
12977 /* Parse the unqualified-id. */
12978 identifier = cp_parser_unqualified_id (parser,
12979 /*template_keyword_p=*/false,
12980 /*check_dependency_p=*/true,
12981 /*declarator_p=*/true,
12982 /*optional_p=*/false);
12983
12984 if (access_declaration_p)
12985 {
12986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12987 cp_parser_simulate_error (parser);
12988 if (!cp_parser_parse_definitely (parser))
12989 return false;
12990 }
12991
12992 /* The function we call to handle a using-declaration is different
12993 depending on what scope we are in. */
12994 if (qscope == error_mark_node || identifier == error_mark_node)
12995 ;
12996 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12997 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12998 /* [namespace.udecl]
12999
13000 A using declaration shall not name a template-id. */
13001 error_at (token->location,
13002 "a template-id may not appear in a using-declaration");
13003 else
13004 {
13005 if (at_class_scope_p ())
13006 {
13007 /* Create the USING_DECL. */
13008 decl = do_class_using_decl (parser->scope, identifier);
13009
13010 if (check_for_bare_parameter_packs (decl))
13011 return false;
13012 else
13013 /* Add it to the list of members in this class. */
13014 finish_member_declaration (decl);
13015 }
13016 else
13017 {
13018 decl = cp_parser_lookup_name_simple (parser,
13019 identifier,
13020 token->location);
13021 if (decl == error_mark_node)
13022 cp_parser_name_lookup_error (parser, identifier,
13023 decl, NULL,
13024 token->location);
13025 else if (check_for_bare_parameter_packs (decl))
13026 return false;
13027 else if (!at_namespace_scope_p ())
13028 do_local_using_decl (decl, qscope, identifier);
13029 else
13030 do_toplevel_using_decl (decl, qscope, identifier);
13031 }
13032 }
13033
13034 /* Look for the final `;'. */
13035 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13036
13037 return true;
13038 }
13039
13040 /* Parse a using-directive.
13041
13042 using-directive:
13043 using namespace :: [opt] nested-name-specifier [opt]
13044 namespace-name ; */
13045
13046 static void
13047 cp_parser_using_directive (cp_parser* parser)
13048 {
13049 tree namespace_decl;
13050 tree attribs;
13051
13052 /* Look for the `using' keyword. */
13053 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13054 /* And the `namespace' keyword. */
13055 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13056 /* Look for the optional `::' operator. */
13057 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13058 /* And the optional nested-name-specifier. */
13059 cp_parser_nested_name_specifier_opt (parser,
13060 /*typename_keyword_p=*/false,
13061 /*check_dependency_p=*/true,
13062 /*type_p=*/false,
13063 /*is_declaration=*/true);
13064 /* Get the namespace being used. */
13065 namespace_decl = cp_parser_namespace_name (parser);
13066 /* And any specified attributes. */
13067 attribs = cp_parser_attributes_opt (parser);
13068 /* Update the symbol table. */
13069 parse_using_directive (namespace_decl, attribs);
13070 /* Look for the final `;'. */
13071 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13072 }
13073
13074 /* Parse an asm-definition.
13075
13076 asm-definition:
13077 asm ( string-literal ) ;
13078
13079 GNU Extension:
13080
13081 asm-definition:
13082 asm volatile [opt] ( string-literal ) ;
13083 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13084 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13085 : asm-operand-list [opt] ) ;
13086 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13087 : asm-operand-list [opt]
13088 : asm-clobber-list [opt] ) ;
13089 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13090 : asm-clobber-list [opt]
13091 : asm-goto-list ) ; */
13092
13093 static void
13094 cp_parser_asm_definition (cp_parser* parser)
13095 {
13096 tree string;
13097 tree outputs = NULL_TREE;
13098 tree inputs = NULL_TREE;
13099 tree clobbers = NULL_TREE;
13100 tree labels = NULL_TREE;
13101 tree asm_stmt;
13102 bool volatile_p = false;
13103 bool extended_p = false;
13104 bool invalid_inputs_p = false;
13105 bool invalid_outputs_p = false;
13106 bool goto_p = false;
13107 const char *missing = NULL;
13108
13109 /* Look for the `asm' keyword. */
13110 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13111 /* See if the next token is `volatile'. */
13112 if (cp_parser_allow_gnu_extensions_p (parser)
13113 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13114 {
13115 /* Remember that we saw the `volatile' keyword. */
13116 volatile_p = true;
13117 /* Consume the token. */
13118 cp_lexer_consume_token (parser->lexer);
13119 }
13120 if (cp_parser_allow_gnu_extensions_p (parser)
13121 && parser->in_function_body
13122 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13123 {
13124 /* Remember that we saw the `goto' keyword. */
13125 goto_p = true;
13126 /* Consume the token. */
13127 cp_lexer_consume_token (parser->lexer);
13128 }
13129 /* Look for the opening `('. */
13130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13131 return;
13132 /* Look for the string. */
13133 string = cp_parser_string_literal (parser, false, false);
13134 if (string == error_mark_node)
13135 {
13136 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13137 /*consume_paren=*/true);
13138 return;
13139 }
13140
13141 /* If we're allowing GNU extensions, check for the extended assembly
13142 syntax. Unfortunately, the `:' tokens need not be separated by
13143 a space in C, and so, for compatibility, we tolerate that here
13144 too. Doing that means that we have to treat the `::' operator as
13145 two `:' tokens. */
13146 if (cp_parser_allow_gnu_extensions_p (parser)
13147 && parser->in_function_body
13148 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13149 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13150 {
13151 bool inputs_p = false;
13152 bool clobbers_p = false;
13153 bool labels_p = false;
13154
13155 /* The extended syntax was used. */
13156 extended_p = true;
13157
13158 /* Look for outputs. */
13159 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13160 {
13161 /* Consume the `:'. */
13162 cp_lexer_consume_token (parser->lexer);
13163 /* Parse the output-operands. */
13164 if (cp_lexer_next_token_is_not (parser->lexer,
13165 CPP_COLON)
13166 && cp_lexer_next_token_is_not (parser->lexer,
13167 CPP_SCOPE)
13168 && cp_lexer_next_token_is_not (parser->lexer,
13169 CPP_CLOSE_PAREN)
13170 && !goto_p)
13171 outputs = cp_parser_asm_operand_list (parser);
13172
13173 if (outputs == error_mark_node)
13174 invalid_outputs_p = true;
13175 }
13176 /* If the next token is `::', there are no outputs, and the
13177 next token is the beginning of the inputs. */
13178 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13179 /* The inputs are coming next. */
13180 inputs_p = true;
13181
13182 /* Look for inputs. */
13183 if (inputs_p
13184 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13185 {
13186 /* Consume the `:' or `::'. */
13187 cp_lexer_consume_token (parser->lexer);
13188 /* Parse the output-operands. */
13189 if (cp_lexer_next_token_is_not (parser->lexer,
13190 CPP_COLON)
13191 && cp_lexer_next_token_is_not (parser->lexer,
13192 CPP_SCOPE)
13193 && cp_lexer_next_token_is_not (parser->lexer,
13194 CPP_CLOSE_PAREN))
13195 inputs = cp_parser_asm_operand_list (parser);
13196
13197 if (inputs == error_mark_node)
13198 invalid_inputs_p = true;
13199 }
13200 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13201 /* The clobbers are coming next. */
13202 clobbers_p = true;
13203
13204 /* Look for clobbers. */
13205 if (clobbers_p
13206 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13207 {
13208 clobbers_p = true;
13209 /* Consume the `:' or `::'. */
13210 cp_lexer_consume_token (parser->lexer);
13211 /* Parse the clobbers. */
13212 if (cp_lexer_next_token_is_not (parser->lexer,
13213 CPP_COLON)
13214 && cp_lexer_next_token_is_not (parser->lexer,
13215 CPP_CLOSE_PAREN))
13216 clobbers = cp_parser_asm_clobber_list (parser);
13217 }
13218 else if (goto_p
13219 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13220 /* The labels are coming next. */
13221 labels_p = true;
13222
13223 /* Look for labels. */
13224 if (labels_p
13225 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13226 {
13227 labels_p = true;
13228 /* Consume the `:' or `::'. */
13229 cp_lexer_consume_token (parser->lexer);
13230 /* Parse the labels. */
13231 labels = cp_parser_asm_label_list (parser);
13232 }
13233
13234 if (goto_p && !labels_p)
13235 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13236 }
13237 else if (goto_p)
13238 missing = "%<:%> or %<::%>";
13239
13240 /* Look for the closing `)'. */
13241 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13242 missing ? missing : "%<)%>"))
13243 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13244 /*consume_paren=*/true);
13245 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13246
13247 if (!invalid_inputs_p && !invalid_outputs_p)
13248 {
13249 /* Create the ASM_EXPR. */
13250 if (parser->in_function_body)
13251 {
13252 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13253 inputs, clobbers, labels);
13254 /* If the extended syntax was not used, mark the ASM_EXPR. */
13255 if (!extended_p)
13256 {
13257 tree temp = asm_stmt;
13258 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13259 temp = TREE_OPERAND (temp, 0);
13260
13261 ASM_INPUT_P (temp) = 1;
13262 }
13263 }
13264 else
13265 cgraph_add_asm_node (string);
13266 }
13267 }
13268
13269 /* Declarators [gram.dcl.decl] */
13270
13271 /* Parse an init-declarator.
13272
13273 init-declarator:
13274 declarator initializer [opt]
13275
13276 GNU Extension:
13277
13278 init-declarator:
13279 declarator asm-specification [opt] attributes [opt] initializer [opt]
13280
13281 function-definition:
13282 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13283 function-body
13284 decl-specifier-seq [opt] declarator function-try-block
13285
13286 GNU Extension:
13287
13288 function-definition:
13289 __extension__ function-definition
13290
13291 The DECL_SPECIFIERS apply to this declarator. Returns a
13292 representation of the entity declared. If MEMBER_P is TRUE, then
13293 this declarator appears in a class scope. The new DECL created by
13294 this declarator is returned.
13295
13296 The CHECKS are access checks that should be performed once we know
13297 what entity is being declared (and, therefore, what classes have
13298 befriended it).
13299
13300 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13301 for a function-definition here as well. If the declarator is a
13302 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13303 be TRUE upon return. By that point, the function-definition will
13304 have been completely parsed.
13305
13306 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13307 is FALSE. */
13308
13309 static tree
13310 cp_parser_init_declarator (cp_parser* parser,
13311 cp_decl_specifier_seq *decl_specifiers,
13312 VEC (deferred_access_check,gc)* checks,
13313 bool function_definition_allowed_p,
13314 bool member_p,
13315 int declares_class_or_enum,
13316 bool* function_definition_p)
13317 {
13318 cp_token *token = NULL, *asm_spec_start_token = NULL,
13319 *attributes_start_token = NULL;
13320 cp_declarator *declarator;
13321 tree prefix_attributes;
13322 tree attributes;
13323 tree asm_specification;
13324 tree initializer;
13325 tree decl = NULL_TREE;
13326 tree scope;
13327 int is_initialized;
13328 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13329 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13330 "(...)". */
13331 enum cpp_ttype initialization_kind;
13332 bool is_direct_init = false;
13333 bool is_non_constant_init;
13334 int ctor_dtor_or_conv_p;
13335 bool friend_p;
13336 tree pushed_scope = NULL;
13337
13338 /* Gather the attributes that were provided with the
13339 decl-specifiers. */
13340 prefix_attributes = decl_specifiers->attributes;
13341
13342 /* Assume that this is not the declarator for a function
13343 definition. */
13344 if (function_definition_p)
13345 *function_definition_p = false;
13346
13347 /* Defer access checks while parsing the declarator; we cannot know
13348 what names are accessible until we know what is being
13349 declared. */
13350 resume_deferring_access_checks ();
13351
13352 /* Parse the declarator. */
13353 token = cp_lexer_peek_token (parser->lexer);
13354 declarator
13355 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13356 &ctor_dtor_or_conv_p,
13357 /*parenthesized_p=*/NULL,
13358 /*member_p=*/false);
13359 /* Gather up the deferred checks. */
13360 stop_deferring_access_checks ();
13361
13362 /* If the DECLARATOR was erroneous, there's no need to go
13363 further. */
13364 if (declarator == cp_error_declarator)
13365 return error_mark_node;
13366
13367 /* Check that the number of template-parameter-lists is OK. */
13368 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13369 token->location))
13370 return error_mark_node;
13371
13372 if (declares_class_or_enum & 2)
13373 cp_parser_check_for_definition_in_return_type (declarator,
13374 decl_specifiers->type,
13375 decl_specifiers->type_location);
13376
13377 /* Figure out what scope the entity declared by the DECLARATOR is
13378 located in. `grokdeclarator' sometimes changes the scope, so
13379 we compute it now. */
13380 scope = get_scope_of_declarator (declarator);
13381
13382 /* If we're allowing GNU extensions, look for an asm-specification
13383 and attributes. */
13384 if (cp_parser_allow_gnu_extensions_p (parser))
13385 {
13386 /* Look for an asm-specification. */
13387 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13388 asm_specification = cp_parser_asm_specification_opt (parser);
13389 /* And attributes. */
13390 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13391 attributes = cp_parser_attributes_opt (parser);
13392 }
13393 else
13394 {
13395 asm_specification = NULL_TREE;
13396 attributes = NULL_TREE;
13397 }
13398
13399 /* Peek at the next token. */
13400 token = cp_lexer_peek_token (parser->lexer);
13401 /* Check to see if the token indicates the start of a
13402 function-definition. */
13403 if (function_declarator_p (declarator)
13404 && cp_parser_token_starts_function_definition_p (token))
13405 {
13406 if (!function_definition_allowed_p)
13407 {
13408 /* If a function-definition should not appear here, issue an
13409 error message. */
13410 cp_parser_error (parser,
13411 "a function-definition is not allowed here");
13412 return error_mark_node;
13413 }
13414 else
13415 {
13416 location_t func_brace_location
13417 = cp_lexer_peek_token (parser->lexer)->location;
13418
13419 /* Neither attributes nor an asm-specification are allowed
13420 on a function-definition. */
13421 if (asm_specification)
13422 error_at (asm_spec_start_token->location,
13423 "an asm-specification is not allowed "
13424 "on a function-definition");
13425 if (attributes)
13426 error_at (attributes_start_token->location,
13427 "attributes are not allowed on a function-definition");
13428 /* This is a function-definition. */
13429 *function_definition_p = true;
13430
13431 /* Parse the function definition. */
13432 if (member_p)
13433 decl = cp_parser_save_member_function_body (parser,
13434 decl_specifiers,
13435 declarator,
13436 prefix_attributes);
13437 else
13438 decl
13439 = (cp_parser_function_definition_from_specifiers_and_declarator
13440 (parser, decl_specifiers, prefix_attributes, declarator));
13441
13442 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13443 {
13444 /* This is where the prologue starts... */
13445 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13446 = func_brace_location;
13447 }
13448
13449 return decl;
13450 }
13451 }
13452
13453 /* [dcl.dcl]
13454
13455 Only in function declarations for constructors, destructors, and
13456 type conversions can the decl-specifier-seq be omitted.
13457
13458 We explicitly postpone this check past the point where we handle
13459 function-definitions because we tolerate function-definitions
13460 that are missing their return types in some modes. */
13461 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13462 {
13463 cp_parser_error (parser,
13464 "expected constructor, destructor, or type conversion");
13465 return error_mark_node;
13466 }
13467
13468 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13469 if (token->type == CPP_EQ
13470 || token->type == CPP_OPEN_PAREN
13471 || token->type == CPP_OPEN_BRACE)
13472 {
13473 is_initialized = SD_INITIALIZED;
13474 initialization_kind = token->type;
13475
13476 if (token->type == CPP_EQ
13477 && function_declarator_p (declarator))
13478 {
13479 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13480 if (t2->keyword == RID_DEFAULT)
13481 is_initialized = SD_DEFAULTED;
13482 else if (t2->keyword == RID_DELETE)
13483 is_initialized = SD_DELETED;
13484 }
13485 }
13486 else
13487 {
13488 /* If the init-declarator isn't initialized and isn't followed by a
13489 `,' or `;', it's not a valid init-declarator. */
13490 if (token->type != CPP_COMMA
13491 && token->type != CPP_SEMICOLON)
13492 {
13493 cp_parser_error (parser, "expected initializer");
13494 return error_mark_node;
13495 }
13496 is_initialized = SD_UNINITIALIZED;
13497 initialization_kind = CPP_EOF;
13498 }
13499
13500 /* Because start_decl has side-effects, we should only call it if we
13501 know we're going ahead. By this point, we know that we cannot
13502 possibly be looking at any other construct. */
13503 cp_parser_commit_to_tentative_parse (parser);
13504
13505 /* If the decl specifiers were bad, issue an error now that we're
13506 sure this was intended to be a declarator. Then continue
13507 declaring the variable(s), as int, to try to cut down on further
13508 errors. */
13509 if (decl_specifiers->any_specifiers_p
13510 && decl_specifiers->type == error_mark_node)
13511 {
13512 cp_parser_error (parser, "invalid type in declaration");
13513 decl_specifiers->type = integer_type_node;
13514 }
13515
13516 /* Check to see whether or not this declaration is a friend. */
13517 friend_p = cp_parser_friend_p (decl_specifiers);
13518
13519 /* Enter the newly declared entry in the symbol table. If we're
13520 processing a declaration in a class-specifier, we wait until
13521 after processing the initializer. */
13522 if (!member_p)
13523 {
13524 if (parser->in_unbraced_linkage_specification_p)
13525 decl_specifiers->storage_class = sc_extern;
13526 decl = start_decl (declarator, decl_specifiers,
13527 is_initialized, attributes, prefix_attributes,
13528 &pushed_scope);
13529 }
13530 else if (scope)
13531 /* Enter the SCOPE. That way unqualified names appearing in the
13532 initializer will be looked up in SCOPE. */
13533 pushed_scope = push_scope (scope);
13534
13535 /* Perform deferred access control checks, now that we know in which
13536 SCOPE the declared entity resides. */
13537 if (!member_p && decl)
13538 {
13539 tree saved_current_function_decl = NULL_TREE;
13540
13541 /* If the entity being declared is a function, pretend that we
13542 are in its scope. If it is a `friend', it may have access to
13543 things that would not otherwise be accessible. */
13544 if (TREE_CODE (decl) == FUNCTION_DECL)
13545 {
13546 saved_current_function_decl = current_function_decl;
13547 current_function_decl = decl;
13548 }
13549
13550 /* Perform access checks for template parameters. */
13551 cp_parser_perform_template_parameter_access_checks (checks);
13552
13553 /* Perform the access control checks for the declarator and the
13554 decl-specifiers. */
13555 perform_deferred_access_checks ();
13556
13557 /* Restore the saved value. */
13558 if (TREE_CODE (decl) == FUNCTION_DECL)
13559 current_function_decl = saved_current_function_decl;
13560 }
13561
13562 /* Parse the initializer. */
13563 initializer = NULL_TREE;
13564 is_direct_init = false;
13565 is_non_constant_init = true;
13566 if (is_initialized)
13567 {
13568 if (function_declarator_p (declarator))
13569 {
13570 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13571 if (initialization_kind == CPP_EQ)
13572 initializer = cp_parser_pure_specifier (parser);
13573 else
13574 {
13575 /* If the declaration was erroneous, we don't really
13576 know what the user intended, so just silently
13577 consume the initializer. */
13578 if (decl != error_mark_node)
13579 error_at (initializer_start_token->location,
13580 "initializer provided for function");
13581 cp_parser_skip_to_closing_parenthesis (parser,
13582 /*recovering=*/true,
13583 /*or_comma=*/false,
13584 /*consume_paren=*/true);
13585 }
13586 }
13587 else
13588 {
13589 /* We want to record the extra mangling scope for in-class
13590 initializers of class members and initializers of static data
13591 member templates. The former is a C++0x feature which isn't
13592 implemented yet, and I expect it will involve deferring
13593 parsing of the initializer until end of class as with default
13594 arguments. So right here we only handle the latter. */
13595 if (!member_p && processing_template_decl)
13596 start_lambda_scope (decl);
13597 initializer = cp_parser_initializer (parser,
13598 &is_direct_init,
13599 &is_non_constant_init);
13600 if (!member_p && processing_template_decl)
13601 finish_lambda_scope ();
13602 }
13603 }
13604
13605 /* The old parser allows attributes to appear after a parenthesized
13606 initializer. Mark Mitchell proposed removing this functionality
13607 on the GCC mailing lists on 2002-08-13. This parser accepts the
13608 attributes -- but ignores them. */
13609 if (cp_parser_allow_gnu_extensions_p (parser)
13610 && initialization_kind == CPP_OPEN_PAREN)
13611 if (cp_parser_attributes_opt (parser))
13612 warning (OPT_Wattributes,
13613 "attributes after parenthesized initializer ignored");
13614
13615 /* For an in-class declaration, use `grokfield' to create the
13616 declaration. */
13617 if (member_p)
13618 {
13619 if (pushed_scope)
13620 {
13621 pop_scope (pushed_scope);
13622 pushed_scope = false;
13623 }
13624 decl = grokfield (declarator, decl_specifiers,
13625 initializer, !is_non_constant_init,
13626 /*asmspec=*/NULL_TREE,
13627 prefix_attributes);
13628 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13629 cp_parser_save_default_args (parser, decl);
13630 }
13631
13632 /* Finish processing the declaration. But, skip friend
13633 declarations. */
13634 if (!friend_p && decl && decl != error_mark_node)
13635 {
13636 cp_finish_decl (decl,
13637 initializer, !is_non_constant_init,
13638 asm_specification,
13639 /* If the initializer is in parentheses, then this is
13640 a direct-initialization, which means that an
13641 `explicit' constructor is OK. Otherwise, an
13642 `explicit' constructor cannot be used. */
13643 ((is_direct_init || !is_initialized)
13644 ? 0 : LOOKUP_ONLYCONVERTING));
13645 }
13646 else if ((cxx_dialect != cxx98) && friend_p
13647 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13648 /* Core issue #226 (C++0x only): A default template-argument
13649 shall not be specified in a friend class template
13650 declaration. */
13651 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13652 /*is_partial=*/0, /*is_friend_decl=*/1);
13653
13654 if (!friend_p && pushed_scope)
13655 pop_scope (pushed_scope);
13656
13657 return decl;
13658 }
13659
13660 /* Parse a declarator.
13661
13662 declarator:
13663 direct-declarator
13664 ptr-operator declarator
13665
13666 abstract-declarator:
13667 ptr-operator abstract-declarator [opt]
13668 direct-abstract-declarator
13669
13670 GNU Extensions:
13671
13672 declarator:
13673 attributes [opt] direct-declarator
13674 attributes [opt] ptr-operator declarator
13675
13676 abstract-declarator:
13677 attributes [opt] ptr-operator abstract-declarator [opt]
13678 attributes [opt] direct-abstract-declarator
13679
13680 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13681 detect constructor, destructor or conversion operators. It is set
13682 to -1 if the declarator is a name, and +1 if it is a
13683 function. Otherwise it is set to zero. Usually you just want to
13684 test for >0, but internally the negative value is used.
13685
13686 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13687 a decl-specifier-seq unless it declares a constructor, destructor,
13688 or conversion. It might seem that we could check this condition in
13689 semantic analysis, rather than parsing, but that makes it difficult
13690 to handle something like `f()'. We want to notice that there are
13691 no decl-specifiers, and therefore realize that this is an
13692 expression, not a declaration.)
13693
13694 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13695 the declarator is a direct-declarator of the form "(...)".
13696
13697 MEMBER_P is true iff this declarator is a member-declarator. */
13698
13699 static cp_declarator *
13700 cp_parser_declarator (cp_parser* parser,
13701 cp_parser_declarator_kind dcl_kind,
13702 int* ctor_dtor_or_conv_p,
13703 bool* parenthesized_p,
13704 bool member_p)
13705 {
13706 cp_token *token;
13707 cp_declarator *declarator;
13708 enum tree_code code;
13709 cp_cv_quals cv_quals;
13710 tree class_type;
13711 tree attributes = NULL_TREE;
13712
13713 /* Assume this is not a constructor, destructor, or type-conversion
13714 operator. */
13715 if (ctor_dtor_or_conv_p)
13716 *ctor_dtor_or_conv_p = 0;
13717
13718 if (cp_parser_allow_gnu_extensions_p (parser))
13719 attributes = cp_parser_attributes_opt (parser);
13720
13721 /* Peek at the next token. */
13722 token = cp_lexer_peek_token (parser->lexer);
13723
13724 /* Check for the ptr-operator production. */
13725 cp_parser_parse_tentatively (parser);
13726 /* Parse the ptr-operator. */
13727 code = cp_parser_ptr_operator (parser,
13728 &class_type,
13729 &cv_quals);
13730 /* If that worked, then we have a ptr-operator. */
13731 if (cp_parser_parse_definitely (parser))
13732 {
13733 /* If a ptr-operator was found, then this declarator was not
13734 parenthesized. */
13735 if (parenthesized_p)
13736 *parenthesized_p = true;
13737 /* The dependent declarator is optional if we are parsing an
13738 abstract-declarator. */
13739 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13740 cp_parser_parse_tentatively (parser);
13741
13742 /* Parse the dependent declarator. */
13743 declarator = cp_parser_declarator (parser, dcl_kind,
13744 /*ctor_dtor_or_conv_p=*/NULL,
13745 /*parenthesized_p=*/NULL,
13746 /*member_p=*/false);
13747
13748 /* If we are parsing an abstract-declarator, we must handle the
13749 case where the dependent declarator is absent. */
13750 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13751 && !cp_parser_parse_definitely (parser))
13752 declarator = NULL;
13753
13754 declarator = cp_parser_make_indirect_declarator
13755 (code, class_type, cv_quals, declarator);
13756 }
13757 /* Everything else is a direct-declarator. */
13758 else
13759 {
13760 if (parenthesized_p)
13761 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13762 CPP_OPEN_PAREN);
13763 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13764 ctor_dtor_or_conv_p,
13765 member_p);
13766 }
13767
13768 if (attributes && declarator && declarator != cp_error_declarator)
13769 declarator->attributes = attributes;
13770
13771 return declarator;
13772 }
13773
13774 /* Parse a direct-declarator or direct-abstract-declarator.
13775
13776 direct-declarator:
13777 declarator-id
13778 direct-declarator ( parameter-declaration-clause )
13779 cv-qualifier-seq [opt]
13780 exception-specification [opt]
13781 direct-declarator [ constant-expression [opt] ]
13782 ( declarator )
13783
13784 direct-abstract-declarator:
13785 direct-abstract-declarator [opt]
13786 ( parameter-declaration-clause )
13787 cv-qualifier-seq [opt]
13788 exception-specification [opt]
13789 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13790 ( abstract-declarator )
13791
13792 Returns a representation of the declarator. DCL_KIND is
13793 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13794 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13795 we are parsing a direct-declarator. It is
13796 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13797 of ambiguity we prefer an abstract declarator, as per
13798 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13799 cp_parser_declarator. */
13800
13801 static cp_declarator *
13802 cp_parser_direct_declarator (cp_parser* parser,
13803 cp_parser_declarator_kind dcl_kind,
13804 int* ctor_dtor_or_conv_p,
13805 bool member_p)
13806 {
13807 cp_token *token;
13808 cp_declarator *declarator = NULL;
13809 tree scope = NULL_TREE;
13810 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13811 bool saved_in_declarator_p = parser->in_declarator_p;
13812 bool first = true;
13813 tree pushed_scope = NULL_TREE;
13814
13815 while (true)
13816 {
13817 /* Peek at the next token. */
13818 token = cp_lexer_peek_token (parser->lexer);
13819 if (token->type == CPP_OPEN_PAREN)
13820 {
13821 /* This is either a parameter-declaration-clause, or a
13822 parenthesized declarator. When we know we are parsing a
13823 named declarator, it must be a parenthesized declarator
13824 if FIRST is true. For instance, `(int)' is a
13825 parameter-declaration-clause, with an omitted
13826 direct-abstract-declarator. But `((*))', is a
13827 parenthesized abstract declarator. Finally, when T is a
13828 template parameter `(T)' is a
13829 parameter-declaration-clause, and not a parenthesized
13830 named declarator.
13831
13832 We first try and parse a parameter-declaration-clause,
13833 and then try a nested declarator (if FIRST is true).
13834
13835 It is not an error for it not to be a
13836 parameter-declaration-clause, even when FIRST is
13837 false. Consider,
13838
13839 int i (int);
13840 int i (3);
13841
13842 The first is the declaration of a function while the
13843 second is the definition of a variable, including its
13844 initializer.
13845
13846 Having seen only the parenthesis, we cannot know which of
13847 these two alternatives should be selected. Even more
13848 complex are examples like:
13849
13850 int i (int (a));
13851 int i (int (3));
13852
13853 The former is a function-declaration; the latter is a
13854 variable initialization.
13855
13856 Thus again, we try a parameter-declaration-clause, and if
13857 that fails, we back out and return. */
13858
13859 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13860 {
13861 tree params;
13862 unsigned saved_num_template_parameter_lists;
13863 bool is_declarator = false;
13864 tree t;
13865
13866 /* In a member-declarator, the only valid interpretation
13867 of a parenthesis is the start of a
13868 parameter-declaration-clause. (It is invalid to
13869 initialize a static data member with a parenthesized
13870 initializer; only the "=" form of initialization is
13871 permitted.) */
13872 if (!member_p)
13873 cp_parser_parse_tentatively (parser);
13874
13875 /* Consume the `('. */
13876 cp_lexer_consume_token (parser->lexer);
13877 if (first)
13878 {
13879 /* If this is going to be an abstract declarator, we're
13880 in a declarator and we can't have default args. */
13881 parser->default_arg_ok_p = false;
13882 parser->in_declarator_p = true;
13883 }
13884
13885 /* Inside the function parameter list, surrounding
13886 template-parameter-lists do not apply. */
13887 saved_num_template_parameter_lists
13888 = parser->num_template_parameter_lists;
13889 parser->num_template_parameter_lists = 0;
13890
13891 begin_scope (sk_function_parms, NULL_TREE);
13892
13893 /* Parse the parameter-declaration-clause. */
13894 params = cp_parser_parameter_declaration_clause (parser);
13895
13896 parser->num_template_parameter_lists
13897 = saved_num_template_parameter_lists;
13898
13899 /* If all went well, parse the cv-qualifier-seq and the
13900 exception-specification. */
13901 if (member_p || cp_parser_parse_definitely (parser))
13902 {
13903 cp_cv_quals cv_quals;
13904 tree exception_specification;
13905 tree late_return;
13906
13907 is_declarator = true;
13908
13909 if (ctor_dtor_or_conv_p)
13910 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13911 first = false;
13912 /* Consume the `)'. */
13913 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13914
13915 /* Parse the cv-qualifier-seq. */
13916 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13917 /* And the exception-specification. */
13918 exception_specification
13919 = cp_parser_exception_specification_opt (parser);
13920
13921 late_return
13922 = cp_parser_late_return_type_opt (parser);
13923
13924 /* Create the function-declarator. */
13925 declarator = make_call_declarator (declarator,
13926 params,
13927 cv_quals,
13928 exception_specification,
13929 late_return);
13930 /* Any subsequent parameter lists are to do with
13931 return type, so are not those of the declared
13932 function. */
13933 parser->default_arg_ok_p = false;
13934 }
13935
13936 /* Remove the function parms from scope. */
13937 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13938 pop_binding (DECL_NAME (t), t);
13939 leave_scope();
13940
13941 if (is_declarator)
13942 /* Repeat the main loop. */
13943 continue;
13944 }
13945
13946 /* If this is the first, we can try a parenthesized
13947 declarator. */
13948 if (first)
13949 {
13950 bool saved_in_type_id_in_expr_p;
13951
13952 parser->default_arg_ok_p = saved_default_arg_ok_p;
13953 parser->in_declarator_p = saved_in_declarator_p;
13954
13955 /* Consume the `('. */
13956 cp_lexer_consume_token (parser->lexer);
13957 /* Parse the nested declarator. */
13958 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13959 parser->in_type_id_in_expr_p = true;
13960 declarator
13961 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13962 /*parenthesized_p=*/NULL,
13963 member_p);
13964 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13965 first = false;
13966 /* Expect a `)'. */
13967 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13968 declarator = cp_error_declarator;
13969 if (declarator == cp_error_declarator)
13970 break;
13971
13972 goto handle_declarator;
13973 }
13974 /* Otherwise, we must be done. */
13975 else
13976 break;
13977 }
13978 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13979 && token->type == CPP_OPEN_SQUARE)
13980 {
13981 /* Parse an array-declarator. */
13982 tree bounds;
13983
13984 if (ctor_dtor_or_conv_p)
13985 *ctor_dtor_or_conv_p = 0;
13986
13987 first = false;
13988 parser->default_arg_ok_p = false;
13989 parser->in_declarator_p = true;
13990 /* Consume the `['. */
13991 cp_lexer_consume_token (parser->lexer);
13992 /* Peek at the next token. */
13993 token = cp_lexer_peek_token (parser->lexer);
13994 /* If the next token is `]', then there is no
13995 constant-expression. */
13996 if (token->type != CPP_CLOSE_SQUARE)
13997 {
13998 bool non_constant_p;
13999
14000 bounds
14001 = cp_parser_constant_expression (parser,
14002 /*allow_non_constant=*/true,
14003 &non_constant_p);
14004 if (!non_constant_p)
14005 bounds = fold_non_dependent_expr (bounds);
14006 /* Normally, the array bound must be an integral constant
14007 expression. However, as an extension, we allow VLAs
14008 in function scopes. */
14009 else if (!parser->in_function_body)
14010 {
14011 error_at (token->location,
14012 "array bound is not an integer constant");
14013 bounds = error_mark_node;
14014 }
14015 else if (processing_template_decl && !error_operand_p (bounds))
14016 {
14017 /* Remember this wasn't a constant-expression. */
14018 bounds = build_nop (TREE_TYPE (bounds), bounds);
14019 TREE_SIDE_EFFECTS (bounds) = 1;
14020 }
14021 }
14022 else
14023 bounds = NULL_TREE;
14024 /* Look for the closing `]'. */
14025 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14026 {
14027 declarator = cp_error_declarator;
14028 break;
14029 }
14030
14031 declarator = make_array_declarator (declarator, bounds);
14032 }
14033 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14034 {
14035 {
14036 tree qualifying_scope;
14037 tree unqualified_name;
14038 special_function_kind sfk;
14039 bool abstract_ok;
14040 bool pack_expansion_p = false;
14041 cp_token *declarator_id_start_token;
14042
14043 /* Parse a declarator-id */
14044 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14045 if (abstract_ok)
14046 {
14047 cp_parser_parse_tentatively (parser);
14048
14049 /* If we see an ellipsis, we should be looking at a
14050 parameter pack. */
14051 if (token->type == CPP_ELLIPSIS)
14052 {
14053 /* Consume the `...' */
14054 cp_lexer_consume_token (parser->lexer);
14055
14056 pack_expansion_p = true;
14057 }
14058 }
14059
14060 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14061 unqualified_name
14062 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14063 qualifying_scope = parser->scope;
14064 if (abstract_ok)
14065 {
14066 bool okay = false;
14067
14068 if (!unqualified_name && pack_expansion_p)
14069 {
14070 /* Check whether an error occurred. */
14071 okay = !cp_parser_error_occurred (parser);
14072
14073 /* We already consumed the ellipsis to mark a
14074 parameter pack, but we have no way to report it,
14075 so abort the tentative parse. We will be exiting
14076 immediately anyway. */
14077 cp_parser_abort_tentative_parse (parser);
14078 }
14079 else
14080 okay = cp_parser_parse_definitely (parser);
14081
14082 if (!okay)
14083 unqualified_name = error_mark_node;
14084 else if (unqualified_name
14085 && (qualifying_scope
14086 || (TREE_CODE (unqualified_name)
14087 != IDENTIFIER_NODE)))
14088 {
14089 cp_parser_error (parser, "expected unqualified-id");
14090 unqualified_name = error_mark_node;
14091 }
14092 }
14093
14094 if (!unqualified_name)
14095 return NULL;
14096 if (unqualified_name == error_mark_node)
14097 {
14098 declarator = cp_error_declarator;
14099 pack_expansion_p = false;
14100 declarator->parameter_pack_p = false;
14101 break;
14102 }
14103
14104 if (qualifying_scope && at_namespace_scope_p ()
14105 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14106 {
14107 /* In the declaration of a member of a template class
14108 outside of the class itself, the SCOPE will sometimes
14109 be a TYPENAME_TYPE. For example, given:
14110
14111 template <typename T>
14112 int S<T>::R::i = 3;
14113
14114 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14115 this context, we must resolve S<T>::R to an ordinary
14116 type, rather than a typename type.
14117
14118 The reason we normally avoid resolving TYPENAME_TYPEs
14119 is that a specialization of `S' might render
14120 `S<T>::R' not a type. However, if `S' is
14121 specialized, then this `i' will not be used, so there
14122 is no harm in resolving the types here. */
14123 tree type;
14124
14125 /* Resolve the TYPENAME_TYPE. */
14126 type = resolve_typename_type (qualifying_scope,
14127 /*only_current_p=*/false);
14128 /* If that failed, the declarator is invalid. */
14129 if (TREE_CODE (type) == TYPENAME_TYPE)
14130 error_at (declarator_id_start_token->location,
14131 "%<%T::%E%> is not a type",
14132 TYPE_CONTEXT (qualifying_scope),
14133 TYPE_IDENTIFIER (qualifying_scope));
14134 qualifying_scope = type;
14135 }
14136
14137 sfk = sfk_none;
14138
14139 if (unqualified_name)
14140 {
14141 tree class_type;
14142
14143 if (qualifying_scope
14144 && CLASS_TYPE_P (qualifying_scope))
14145 class_type = qualifying_scope;
14146 else
14147 class_type = current_class_type;
14148
14149 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14150 {
14151 tree name_type = TREE_TYPE (unqualified_name);
14152 if (class_type && same_type_p (name_type, class_type))
14153 {
14154 if (qualifying_scope
14155 && CLASSTYPE_USE_TEMPLATE (name_type))
14156 {
14157 error_at (declarator_id_start_token->location,
14158 "invalid use of constructor as a template");
14159 inform (declarator_id_start_token->location,
14160 "use %<%T::%D%> instead of %<%T::%D%> to "
14161 "name the constructor in a qualified name",
14162 class_type,
14163 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14164 class_type, name_type);
14165 declarator = cp_error_declarator;
14166 break;
14167 }
14168 else
14169 unqualified_name = constructor_name (class_type);
14170 }
14171 else
14172 {
14173 /* We do not attempt to print the declarator
14174 here because we do not have enough
14175 information about its original syntactic
14176 form. */
14177 cp_parser_error (parser, "invalid declarator");
14178 declarator = cp_error_declarator;
14179 break;
14180 }
14181 }
14182
14183 if (class_type)
14184 {
14185 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14186 sfk = sfk_destructor;
14187 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14188 sfk = sfk_conversion;
14189 else if (/* There's no way to declare a constructor
14190 for an anonymous type, even if the type
14191 got a name for linkage purposes. */
14192 !TYPE_WAS_ANONYMOUS (class_type)
14193 && constructor_name_p (unqualified_name,
14194 class_type))
14195 {
14196 unqualified_name = constructor_name (class_type);
14197 sfk = sfk_constructor;
14198 }
14199
14200 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14201 *ctor_dtor_or_conv_p = -1;
14202 }
14203 }
14204 declarator = make_id_declarator (qualifying_scope,
14205 unqualified_name,
14206 sfk);
14207 declarator->id_loc = token->location;
14208 declarator->parameter_pack_p = pack_expansion_p;
14209
14210 if (pack_expansion_p)
14211 maybe_warn_variadic_templates ();
14212 }
14213
14214 handle_declarator:;
14215 scope = get_scope_of_declarator (declarator);
14216 if (scope)
14217 /* Any names that appear after the declarator-id for a
14218 member are looked up in the containing scope. */
14219 pushed_scope = push_scope (scope);
14220 parser->in_declarator_p = true;
14221 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14222 || (declarator && declarator->kind == cdk_id))
14223 /* Default args are only allowed on function
14224 declarations. */
14225 parser->default_arg_ok_p = saved_default_arg_ok_p;
14226 else
14227 parser->default_arg_ok_p = false;
14228
14229 first = false;
14230 }
14231 /* We're done. */
14232 else
14233 break;
14234 }
14235
14236 /* For an abstract declarator, we might wind up with nothing at this
14237 point. That's an error; the declarator is not optional. */
14238 if (!declarator)
14239 cp_parser_error (parser, "expected declarator");
14240
14241 /* If we entered a scope, we must exit it now. */
14242 if (pushed_scope)
14243 pop_scope (pushed_scope);
14244
14245 parser->default_arg_ok_p = saved_default_arg_ok_p;
14246 parser->in_declarator_p = saved_in_declarator_p;
14247
14248 return declarator;
14249 }
14250
14251 /* Parse a ptr-operator.
14252
14253 ptr-operator:
14254 * cv-qualifier-seq [opt]
14255 &
14256 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14257
14258 GNU Extension:
14259
14260 ptr-operator:
14261 & cv-qualifier-seq [opt]
14262
14263 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14264 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14265 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14266 filled in with the TYPE containing the member. *CV_QUALS is
14267 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14268 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14269 Note that the tree codes returned by this function have nothing
14270 to do with the types of trees that will be eventually be created
14271 to represent the pointer or reference type being parsed. They are
14272 just constants with suggestive names. */
14273 static enum tree_code
14274 cp_parser_ptr_operator (cp_parser* parser,
14275 tree* type,
14276 cp_cv_quals *cv_quals)
14277 {
14278 enum tree_code code = ERROR_MARK;
14279 cp_token *token;
14280
14281 /* Assume that it's not a pointer-to-member. */
14282 *type = NULL_TREE;
14283 /* And that there are no cv-qualifiers. */
14284 *cv_quals = TYPE_UNQUALIFIED;
14285
14286 /* Peek at the next token. */
14287 token = cp_lexer_peek_token (parser->lexer);
14288
14289 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14290 if (token->type == CPP_MULT)
14291 code = INDIRECT_REF;
14292 else if (token->type == CPP_AND)
14293 code = ADDR_EXPR;
14294 else if ((cxx_dialect != cxx98) &&
14295 token->type == CPP_AND_AND) /* C++0x only */
14296 code = NON_LVALUE_EXPR;
14297
14298 if (code != ERROR_MARK)
14299 {
14300 /* Consume the `*', `&' or `&&'. */
14301 cp_lexer_consume_token (parser->lexer);
14302
14303 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14304 `&', if we are allowing GNU extensions. (The only qualifier
14305 that can legally appear after `&' is `restrict', but that is
14306 enforced during semantic analysis. */
14307 if (code == INDIRECT_REF
14308 || cp_parser_allow_gnu_extensions_p (parser))
14309 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14310 }
14311 else
14312 {
14313 /* Try the pointer-to-member case. */
14314 cp_parser_parse_tentatively (parser);
14315 /* Look for the optional `::' operator. */
14316 cp_parser_global_scope_opt (parser,
14317 /*current_scope_valid_p=*/false);
14318 /* Look for the nested-name specifier. */
14319 token = cp_lexer_peek_token (parser->lexer);
14320 cp_parser_nested_name_specifier (parser,
14321 /*typename_keyword_p=*/false,
14322 /*check_dependency_p=*/true,
14323 /*type_p=*/false,
14324 /*is_declaration=*/false);
14325 /* If we found it, and the next token is a `*', then we are
14326 indeed looking at a pointer-to-member operator. */
14327 if (!cp_parser_error_occurred (parser)
14328 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14329 {
14330 /* Indicate that the `*' operator was used. */
14331 code = INDIRECT_REF;
14332
14333 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14334 error_at (token->location, "%qD is a namespace", parser->scope);
14335 else
14336 {
14337 /* The type of which the member is a member is given by the
14338 current SCOPE. */
14339 *type = parser->scope;
14340 /* The next name will not be qualified. */
14341 parser->scope = NULL_TREE;
14342 parser->qualifying_scope = NULL_TREE;
14343 parser->object_scope = NULL_TREE;
14344 /* Look for the optional cv-qualifier-seq. */
14345 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14346 }
14347 }
14348 /* If that didn't work we don't have a ptr-operator. */
14349 if (!cp_parser_parse_definitely (parser))
14350 cp_parser_error (parser, "expected ptr-operator");
14351 }
14352
14353 return code;
14354 }
14355
14356 /* Parse an (optional) cv-qualifier-seq.
14357
14358 cv-qualifier-seq:
14359 cv-qualifier cv-qualifier-seq [opt]
14360
14361 cv-qualifier:
14362 const
14363 volatile
14364
14365 GNU Extension:
14366
14367 cv-qualifier:
14368 __restrict__
14369
14370 Returns a bitmask representing the cv-qualifiers. */
14371
14372 static cp_cv_quals
14373 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14374 {
14375 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14376
14377 while (true)
14378 {
14379 cp_token *token;
14380 cp_cv_quals cv_qualifier;
14381
14382 /* Peek at the next token. */
14383 token = cp_lexer_peek_token (parser->lexer);
14384 /* See if it's a cv-qualifier. */
14385 switch (token->keyword)
14386 {
14387 case RID_CONST:
14388 cv_qualifier = TYPE_QUAL_CONST;
14389 break;
14390
14391 case RID_VOLATILE:
14392 cv_qualifier = TYPE_QUAL_VOLATILE;
14393 break;
14394
14395 case RID_RESTRICT:
14396 cv_qualifier = TYPE_QUAL_RESTRICT;
14397 break;
14398
14399 default:
14400 cv_qualifier = TYPE_UNQUALIFIED;
14401 break;
14402 }
14403
14404 if (!cv_qualifier)
14405 break;
14406
14407 if (cv_quals & cv_qualifier)
14408 {
14409 error_at (token->location, "duplicate cv-qualifier");
14410 cp_lexer_purge_token (parser->lexer);
14411 }
14412 else
14413 {
14414 cp_lexer_consume_token (parser->lexer);
14415 cv_quals |= cv_qualifier;
14416 }
14417 }
14418
14419 return cv_quals;
14420 }
14421
14422 /* Parse a late-specified return type, if any. This is not a separate
14423 non-terminal, but part of a function declarator, which looks like
14424
14425 -> type-id
14426
14427 Returns the type indicated by the type-id. */
14428
14429 static tree
14430 cp_parser_late_return_type_opt (cp_parser* parser)
14431 {
14432 cp_token *token;
14433
14434 /* Peek at the next token. */
14435 token = cp_lexer_peek_token (parser->lexer);
14436 /* A late-specified return type is indicated by an initial '->'. */
14437 if (token->type != CPP_DEREF)
14438 return NULL_TREE;
14439
14440 /* Consume the ->. */
14441 cp_lexer_consume_token (parser->lexer);
14442
14443 return cp_parser_type_id (parser);
14444 }
14445
14446 /* Parse a declarator-id.
14447
14448 declarator-id:
14449 id-expression
14450 :: [opt] nested-name-specifier [opt] type-name
14451
14452 In the `id-expression' case, the value returned is as for
14453 cp_parser_id_expression if the id-expression was an unqualified-id.
14454 If the id-expression was a qualified-id, then a SCOPE_REF is
14455 returned. The first operand is the scope (either a NAMESPACE_DECL
14456 or TREE_TYPE), but the second is still just a representation of an
14457 unqualified-id. */
14458
14459 static tree
14460 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14461 {
14462 tree id;
14463 /* The expression must be an id-expression. Assume that qualified
14464 names are the names of types so that:
14465
14466 template <class T>
14467 int S<T>::R::i = 3;
14468
14469 will work; we must treat `S<T>::R' as the name of a type.
14470 Similarly, assume that qualified names are templates, where
14471 required, so that:
14472
14473 template <class T>
14474 int S<T>::R<T>::i = 3;
14475
14476 will work, too. */
14477 id = cp_parser_id_expression (parser,
14478 /*template_keyword_p=*/false,
14479 /*check_dependency_p=*/false,
14480 /*template_p=*/NULL,
14481 /*declarator_p=*/true,
14482 optional_p);
14483 if (id && BASELINK_P (id))
14484 id = BASELINK_FUNCTIONS (id);
14485 return id;
14486 }
14487
14488 /* Parse a type-id.
14489
14490 type-id:
14491 type-specifier-seq abstract-declarator [opt]
14492
14493 Returns the TYPE specified. */
14494
14495 static tree
14496 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
14497 {
14498 cp_decl_specifier_seq type_specifier_seq;
14499 cp_declarator *abstract_declarator;
14500
14501 /* Parse the type-specifier-seq. */
14502 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14503 &type_specifier_seq);
14504 if (type_specifier_seq.type == error_mark_node)
14505 return error_mark_node;
14506
14507 /* There might or might not be an abstract declarator. */
14508 cp_parser_parse_tentatively (parser);
14509 /* Look for the declarator. */
14510 abstract_declarator
14511 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14512 /*parenthesized_p=*/NULL,
14513 /*member_p=*/false);
14514 /* Check to see if there really was a declarator. */
14515 if (!cp_parser_parse_definitely (parser))
14516 abstract_declarator = NULL;
14517
14518 if (type_specifier_seq.type
14519 && type_uses_auto (type_specifier_seq.type))
14520 {
14521 /* A type-id with type 'auto' is only ok if the abstract declarator
14522 is a function declarator with a late-specified return type. */
14523 if (abstract_declarator
14524 && abstract_declarator->kind == cdk_function
14525 && abstract_declarator->u.function.late_return_type)
14526 /* OK */;
14527 else
14528 {
14529 error ("invalid use of %<auto%>");
14530 return error_mark_node;
14531 }
14532 }
14533
14534 return groktypename (&type_specifier_seq, abstract_declarator,
14535 is_template_arg);
14536 }
14537
14538 static tree cp_parser_type_id (cp_parser *parser)
14539 {
14540 return cp_parser_type_id_1 (parser, false);
14541 }
14542
14543 static tree cp_parser_template_type_arg (cp_parser *parser)
14544 {
14545 return cp_parser_type_id_1 (parser, true);
14546 }
14547
14548 /* Parse a type-specifier-seq.
14549
14550 type-specifier-seq:
14551 type-specifier type-specifier-seq [opt]
14552
14553 GNU extension:
14554
14555 type-specifier-seq:
14556 attributes type-specifier-seq [opt]
14557
14558 If IS_CONDITION is true, we are at the start of a "condition",
14559 e.g., we've just seen "if (".
14560
14561 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14562
14563 static void
14564 cp_parser_type_specifier_seq (cp_parser* parser,
14565 bool is_condition,
14566 cp_decl_specifier_seq *type_specifier_seq)
14567 {
14568 bool seen_type_specifier = false;
14569 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14570 cp_token *start_token = NULL;
14571
14572 /* Clear the TYPE_SPECIFIER_SEQ. */
14573 clear_decl_specs (type_specifier_seq);
14574
14575 /* Parse the type-specifiers and attributes. */
14576 while (true)
14577 {
14578 tree type_specifier;
14579 bool is_cv_qualifier;
14580
14581 /* Check for attributes first. */
14582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14583 {
14584 type_specifier_seq->attributes =
14585 chainon (type_specifier_seq->attributes,
14586 cp_parser_attributes_opt (parser));
14587 continue;
14588 }
14589
14590 /* record the token of the beginning of the type specifier seq,
14591 for error reporting purposes*/
14592 if (!start_token)
14593 start_token = cp_lexer_peek_token (parser->lexer);
14594
14595 /* Look for the type-specifier. */
14596 type_specifier = cp_parser_type_specifier (parser,
14597 flags,
14598 type_specifier_seq,
14599 /*is_declaration=*/false,
14600 NULL,
14601 &is_cv_qualifier);
14602 if (!type_specifier)
14603 {
14604 /* If the first type-specifier could not be found, this is not a
14605 type-specifier-seq at all. */
14606 if (!seen_type_specifier)
14607 {
14608 cp_parser_error (parser, "expected type-specifier");
14609 type_specifier_seq->type = error_mark_node;
14610 return;
14611 }
14612 /* If subsequent type-specifiers could not be found, the
14613 type-specifier-seq is complete. */
14614 break;
14615 }
14616
14617 seen_type_specifier = true;
14618 /* The standard says that a condition can be:
14619
14620 type-specifier-seq declarator = assignment-expression
14621
14622 However, given:
14623
14624 struct S {};
14625 if (int S = ...)
14626
14627 we should treat the "S" as a declarator, not as a
14628 type-specifier. The standard doesn't say that explicitly for
14629 type-specifier-seq, but it does say that for
14630 decl-specifier-seq in an ordinary declaration. Perhaps it
14631 would be clearer just to allow a decl-specifier-seq here, and
14632 then add a semantic restriction that if any decl-specifiers
14633 that are not type-specifiers appear, the program is invalid. */
14634 if (is_condition && !is_cv_qualifier)
14635 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14636 }
14637
14638 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14639 }
14640
14641 /* Parse a parameter-declaration-clause.
14642
14643 parameter-declaration-clause:
14644 parameter-declaration-list [opt] ... [opt]
14645 parameter-declaration-list , ...
14646
14647 Returns a representation for the parameter declarations. A return
14648 value of NULL indicates a parameter-declaration-clause consisting
14649 only of an ellipsis. */
14650
14651 static tree
14652 cp_parser_parameter_declaration_clause (cp_parser* parser)
14653 {
14654 tree parameters;
14655 cp_token *token;
14656 bool ellipsis_p;
14657 bool is_error;
14658
14659 /* Peek at the next token. */
14660 token = cp_lexer_peek_token (parser->lexer);
14661 /* Check for trivial parameter-declaration-clauses. */
14662 if (token->type == CPP_ELLIPSIS)
14663 {
14664 /* Consume the `...' token. */
14665 cp_lexer_consume_token (parser->lexer);
14666 return NULL_TREE;
14667 }
14668 else if (token->type == CPP_CLOSE_PAREN)
14669 /* There are no parameters. */
14670 {
14671 #ifndef NO_IMPLICIT_EXTERN_C
14672 if (in_system_header && current_class_type == NULL
14673 && current_lang_name == lang_name_c)
14674 return NULL_TREE;
14675 else
14676 #endif
14677 return void_list_node;
14678 }
14679 /* Check for `(void)', too, which is a special case. */
14680 else if (token->keyword == RID_VOID
14681 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14682 == CPP_CLOSE_PAREN))
14683 {
14684 /* Consume the `void' token. */
14685 cp_lexer_consume_token (parser->lexer);
14686 /* There are no parameters. */
14687 return void_list_node;
14688 }
14689
14690 /* Parse the parameter-declaration-list. */
14691 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14692 /* If a parse error occurred while parsing the
14693 parameter-declaration-list, then the entire
14694 parameter-declaration-clause is erroneous. */
14695 if (is_error)
14696 return NULL;
14697
14698 /* Peek at the next token. */
14699 token = cp_lexer_peek_token (parser->lexer);
14700 /* If it's a `,', the clause should terminate with an ellipsis. */
14701 if (token->type == CPP_COMMA)
14702 {
14703 /* Consume the `,'. */
14704 cp_lexer_consume_token (parser->lexer);
14705 /* Expect an ellipsis. */
14706 ellipsis_p
14707 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14708 }
14709 /* It might also be `...' if the optional trailing `,' was
14710 omitted. */
14711 else if (token->type == CPP_ELLIPSIS)
14712 {
14713 /* Consume the `...' token. */
14714 cp_lexer_consume_token (parser->lexer);
14715 /* And remember that we saw it. */
14716 ellipsis_p = true;
14717 }
14718 else
14719 ellipsis_p = false;
14720
14721 /* Finish the parameter list. */
14722 if (!ellipsis_p)
14723 parameters = chainon (parameters, void_list_node);
14724
14725 return parameters;
14726 }
14727
14728 /* Parse a parameter-declaration-list.
14729
14730 parameter-declaration-list:
14731 parameter-declaration
14732 parameter-declaration-list , parameter-declaration
14733
14734 Returns a representation of the parameter-declaration-list, as for
14735 cp_parser_parameter_declaration_clause. However, the
14736 `void_list_node' is never appended to the list. Upon return,
14737 *IS_ERROR will be true iff an error occurred. */
14738
14739 static tree
14740 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14741 {
14742 tree parameters = NULL_TREE;
14743 tree *tail = &parameters;
14744 bool saved_in_unbraced_linkage_specification_p;
14745 int index = 0;
14746
14747 /* Assume all will go well. */
14748 *is_error = false;
14749 /* The special considerations that apply to a function within an
14750 unbraced linkage specifications do not apply to the parameters
14751 to the function. */
14752 saved_in_unbraced_linkage_specification_p
14753 = parser->in_unbraced_linkage_specification_p;
14754 parser->in_unbraced_linkage_specification_p = false;
14755
14756 /* Look for more parameters. */
14757 while (true)
14758 {
14759 cp_parameter_declarator *parameter;
14760 tree decl = error_mark_node;
14761 bool parenthesized_p;
14762 /* Parse the parameter. */
14763 parameter
14764 = cp_parser_parameter_declaration (parser,
14765 /*template_parm_p=*/false,
14766 &parenthesized_p);
14767
14768 /* We don't know yet if the enclosing context is deprecated, so wait
14769 and warn in grokparms if appropriate. */
14770 deprecated_state = DEPRECATED_SUPPRESS;
14771
14772 if (parameter)
14773 decl = grokdeclarator (parameter->declarator,
14774 &parameter->decl_specifiers,
14775 PARM,
14776 parameter->default_argument != NULL_TREE,
14777 &parameter->decl_specifiers.attributes);
14778
14779 deprecated_state = DEPRECATED_NORMAL;
14780
14781 /* If a parse error occurred parsing the parameter declaration,
14782 then the entire parameter-declaration-list is erroneous. */
14783 if (decl == error_mark_node)
14784 {
14785 *is_error = true;
14786 parameters = error_mark_node;
14787 break;
14788 }
14789
14790 if (parameter->decl_specifiers.attributes)
14791 cplus_decl_attributes (&decl,
14792 parameter->decl_specifiers.attributes,
14793 0);
14794 if (DECL_NAME (decl))
14795 decl = pushdecl (decl);
14796
14797 if (decl != error_mark_node)
14798 {
14799 retrofit_lang_decl (decl);
14800 DECL_PARM_INDEX (decl) = ++index;
14801 }
14802
14803 /* Add the new parameter to the list. */
14804 *tail = build_tree_list (parameter->default_argument, decl);
14805 tail = &TREE_CHAIN (*tail);
14806
14807 /* Peek at the next token. */
14808 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14809 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14810 /* These are for Objective-C++ */
14811 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14812 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14813 /* The parameter-declaration-list is complete. */
14814 break;
14815 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14816 {
14817 cp_token *token;
14818
14819 /* Peek at the next token. */
14820 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14821 /* If it's an ellipsis, then the list is complete. */
14822 if (token->type == CPP_ELLIPSIS)
14823 break;
14824 /* Otherwise, there must be more parameters. Consume the
14825 `,'. */
14826 cp_lexer_consume_token (parser->lexer);
14827 /* When parsing something like:
14828
14829 int i(float f, double d)
14830
14831 we can tell after seeing the declaration for "f" that we
14832 are not looking at an initialization of a variable "i",
14833 but rather at the declaration of a function "i".
14834
14835 Due to the fact that the parsing of template arguments
14836 (as specified to a template-id) requires backtracking we
14837 cannot use this technique when inside a template argument
14838 list. */
14839 if (!parser->in_template_argument_list_p
14840 && !parser->in_type_id_in_expr_p
14841 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14842 /* However, a parameter-declaration of the form
14843 "foat(f)" (which is a valid declaration of a
14844 parameter "f") can also be interpreted as an
14845 expression (the conversion of "f" to "float"). */
14846 && !parenthesized_p)
14847 cp_parser_commit_to_tentative_parse (parser);
14848 }
14849 else
14850 {
14851 cp_parser_error (parser, "expected %<,%> or %<...%>");
14852 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14853 cp_parser_skip_to_closing_parenthesis (parser,
14854 /*recovering=*/true,
14855 /*or_comma=*/false,
14856 /*consume_paren=*/false);
14857 break;
14858 }
14859 }
14860
14861 parser->in_unbraced_linkage_specification_p
14862 = saved_in_unbraced_linkage_specification_p;
14863
14864 return parameters;
14865 }
14866
14867 /* Parse a parameter declaration.
14868
14869 parameter-declaration:
14870 decl-specifier-seq ... [opt] declarator
14871 decl-specifier-seq declarator = assignment-expression
14872 decl-specifier-seq ... [opt] abstract-declarator [opt]
14873 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14874
14875 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14876 declares a template parameter. (In that case, a non-nested `>'
14877 token encountered during the parsing of the assignment-expression
14878 is not interpreted as a greater-than operator.)
14879
14880 Returns a representation of the parameter, or NULL if an error
14881 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14882 true iff the declarator is of the form "(p)". */
14883
14884 static cp_parameter_declarator *
14885 cp_parser_parameter_declaration (cp_parser *parser,
14886 bool template_parm_p,
14887 bool *parenthesized_p)
14888 {
14889 int declares_class_or_enum;
14890 bool greater_than_is_operator_p;
14891 cp_decl_specifier_seq decl_specifiers;
14892 cp_declarator *declarator;
14893 tree default_argument;
14894 cp_token *token = NULL, *declarator_token_start = NULL;
14895 const char *saved_message;
14896
14897 /* In a template parameter, `>' is not an operator.
14898
14899 [temp.param]
14900
14901 When parsing a default template-argument for a non-type
14902 template-parameter, the first non-nested `>' is taken as the end
14903 of the template parameter-list rather than a greater-than
14904 operator. */
14905 greater_than_is_operator_p = !template_parm_p;
14906
14907 /* Type definitions may not appear in parameter types. */
14908 saved_message = parser->type_definition_forbidden_message;
14909 parser->type_definition_forbidden_message
14910 = "types may not be defined in parameter types";
14911
14912 /* Parse the declaration-specifiers. */
14913 cp_parser_decl_specifier_seq (parser,
14914 CP_PARSER_FLAGS_NONE,
14915 &decl_specifiers,
14916 &declares_class_or_enum);
14917 /* If an error occurred, there's no reason to attempt to parse the
14918 rest of the declaration. */
14919 if (cp_parser_error_occurred (parser))
14920 {
14921 parser->type_definition_forbidden_message = saved_message;
14922 return NULL;
14923 }
14924
14925 /* Peek at the next token. */
14926 token = cp_lexer_peek_token (parser->lexer);
14927
14928 /* If the next token is a `)', `,', `=', `>', or `...', then there
14929 is no declarator. However, when variadic templates are enabled,
14930 there may be a declarator following `...'. */
14931 if (token->type == CPP_CLOSE_PAREN
14932 || token->type == CPP_COMMA
14933 || token->type == CPP_EQ
14934 || token->type == CPP_GREATER)
14935 {
14936 declarator = NULL;
14937 if (parenthesized_p)
14938 *parenthesized_p = false;
14939 }
14940 /* Otherwise, there should be a declarator. */
14941 else
14942 {
14943 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14944 parser->default_arg_ok_p = false;
14945
14946 /* After seeing a decl-specifier-seq, if the next token is not a
14947 "(", there is no possibility that the code is a valid
14948 expression. Therefore, if parsing tentatively, we commit at
14949 this point. */
14950 if (!parser->in_template_argument_list_p
14951 /* In an expression context, having seen:
14952
14953 (int((char ...
14954
14955 we cannot be sure whether we are looking at a
14956 function-type (taking a "char" as a parameter) or a cast
14957 of some object of type "char" to "int". */
14958 && !parser->in_type_id_in_expr_p
14959 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14960 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14961 cp_parser_commit_to_tentative_parse (parser);
14962 /* Parse the declarator. */
14963 declarator_token_start = token;
14964 declarator = cp_parser_declarator (parser,
14965 CP_PARSER_DECLARATOR_EITHER,
14966 /*ctor_dtor_or_conv_p=*/NULL,
14967 parenthesized_p,
14968 /*member_p=*/false);
14969 parser->default_arg_ok_p = saved_default_arg_ok_p;
14970 /* After the declarator, allow more attributes. */
14971 decl_specifiers.attributes
14972 = chainon (decl_specifiers.attributes,
14973 cp_parser_attributes_opt (parser));
14974 }
14975
14976 /* If the next token is an ellipsis, and we have not seen a
14977 declarator name, and the type of the declarator contains parameter
14978 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14979 a parameter pack expansion expression. Otherwise, leave the
14980 ellipsis for a C-style variadic function. */
14981 token = cp_lexer_peek_token (parser->lexer);
14982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14983 {
14984 tree type = decl_specifiers.type;
14985
14986 if (type && DECL_P (type))
14987 type = TREE_TYPE (type);
14988
14989 if (type
14990 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14991 && declarator_can_be_parameter_pack (declarator)
14992 && (!declarator || !declarator->parameter_pack_p)
14993 && uses_parameter_packs (type))
14994 {
14995 /* Consume the `...'. */
14996 cp_lexer_consume_token (parser->lexer);
14997 maybe_warn_variadic_templates ();
14998
14999 /* Build a pack expansion type */
15000 if (declarator)
15001 declarator->parameter_pack_p = true;
15002 else
15003 decl_specifiers.type = make_pack_expansion (type);
15004 }
15005 }
15006
15007 /* The restriction on defining new types applies only to the type
15008 of the parameter, not to the default argument. */
15009 parser->type_definition_forbidden_message = saved_message;
15010
15011 /* If the next token is `=', then process a default argument. */
15012 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15013 {
15014 /* Consume the `='. */
15015 cp_lexer_consume_token (parser->lexer);
15016
15017 /* If we are defining a class, then the tokens that make up the
15018 default argument must be saved and processed later. */
15019 if (!template_parm_p && at_class_scope_p ()
15020 && TYPE_BEING_DEFINED (current_class_type)
15021 && !LAMBDA_TYPE_P (current_class_type))
15022 {
15023 unsigned depth = 0;
15024 int maybe_template_id = 0;
15025 cp_token *first_token;
15026 cp_token *token;
15027
15028 /* Add tokens until we have processed the entire default
15029 argument. We add the range [first_token, token). */
15030 first_token = cp_lexer_peek_token (parser->lexer);
15031 while (true)
15032 {
15033 bool done = false;
15034
15035 /* Peek at the next token. */
15036 token = cp_lexer_peek_token (parser->lexer);
15037 /* What we do depends on what token we have. */
15038 switch (token->type)
15039 {
15040 /* In valid code, a default argument must be
15041 immediately followed by a `,' `)', or `...'. */
15042 case CPP_COMMA:
15043 if (depth == 0 && maybe_template_id)
15044 {
15045 /* If we've seen a '<', we might be in a
15046 template-argument-list. Until Core issue 325 is
15047 resolved, we don't know how this situation ought
15048 to be handled, so try to DTRT. We check whether
15049 what comes after the comma is a valid parameter
15050 declaration list. If it is, then the comma ends
15051 the default argument; otherwise the default
15052 argument continues. */
15053 bool error = false;
15054
15055 /* Set ITALP so cp_parser_parameter_declaration_list
15056 doesn't decide to commit to this parse. */
15057 bool saved_italp = parser->in_template_argument_list_p;
15058 parser->in_template_argument_list_p = true;
15059
15060 cp_parser_parse_tentatively (parser);
15061 cp_lexer_consume_token (parser->lexer);
15062 cp_parser_parameter_declaration_list (parser, &error);
15063 if (!cp_parser_error_occurred (parser) && !error)
15064 done = true;
15065 cp_parser_abort_tentative_parse (parser);
15066
15067 parser->in_template_argument_list_p = saved_italp;
15068 break;
15069 }
15070 case CPP_CLOSE_PAREN:
15071 case CPP_ELLIPSIS:
15072 /* If we run into a non-nested `;', `}', or `]',
15073 then the code is invalid -- but the default
15074 argument is certainly over. */
15075 case CPP_SEMICOLON:
15076 case CPP_CLOSE_BRACE:
15077 case CPP_CLOSE_SQUARE:
15078 if (depth == 0)
15079 done = true;
15080 /* Update DEPTH, if necessary. */
15081 else if (token->type == CPP_CLOSE_PAREN
15082 || token->type == CPP_CLOSE_BRACE
15083 || token->type == CPP_CLOSE_SQUARE)
15084 --depth;
15085 break;
15086
15087 case CPP_OPEN_PAREN:
15088 case CPP_OPEN_SQUARE:
15089 case CPP_OPEN_BRACE:
15090 ++depth;
15091 break;
15092
15093 case CPP_LESS:
15094 if (depth == 0)
15095 /* This might be the comparison operator, or it might
15096 start a template argument list. */
15097 ++maybe_template_id;
15098 break;
15099
15100 case CPP_RSHIFT:
15101 if (cxx_dialect == cxx98)
15102 break;
15103 /* Fall through for C++0x, which treats the `>>'
15104 operator like two `>' tokens in certain
15105 cases. */
15106
15107 case CPP_GREATER:
15108 if (depth == 0)
15109 {
15110 /* This might be an operator, or it might close a
15111 template argument list. But if a previous '<'
15112 started a template argument list, this will have
15113 closed it, so we can't be in one anymore. */
15114 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15115 if (maybe_template_id < 0)
15116 maybe_template_id = 0;
15117 }
15118 break;
15119
15120 /* If we run out of tokens, issue an error message. */
15121 case CPP_EOF:
15122 case CPP_PRAGMA_EOL:
15123 error_at (token->location, "file ends in default argument");
15124 done = true;
15125 break;
15126
15127 case CPP_NAME:
15128 case CPP_SCOPE:
15129 /* In these cases, we should look for template-ids.
15130 For example, if the default argument is
15131 `X<int, double>()', we need to do name lookup to
15132 figure out whether or not `X' is a template; if
15133 so, the `,' does not end the default argument.
15134
15135 That is not yet done. */
15136 break;
15137
15138 default:
15139 break;
15140 }
15141
15142 /* If we've reached the end, stop. */
15143 if (done)
15144 break;
15145
15146 /* Add the token to the token block. */
15147 token = cp_lexer_consume_token (parser->lexer);
15148 }
15149
15150 /* Create a DEFAULT_ARG to represent the unparsed default
15151 argument. */
15152 default_argument = make_node (DEFAULT_ARG);
15153 DEFARG_TOKENS (default_argument)
15154 = cp_token_cache_new (first_token, token);
15155 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15156 }
15157 /* Outside of a class definition, we can just parse the
15158 assignment-expression. */
15159 else
15160 {
15161 token = cp_lexer_peek_token (parser->lexer);
15162 default_argument
15163 = cp_parser_default_argument (parser, template_parm_p);
15164 }
15165
15166 if (!parser->default_arg_ok_p)
15167 {
15168 if (flag_permissive)
15169 warning (0, "deprecated use of default argument for parameter of non-function");
15170 else
15171 {
15172 error_at (token->location,
15173 "default arguments are only "
15174 "permitted for function parameters");
15175 default_argument = NULL_TREE;
15176 }
15177 }
15178 else if ((declarator && declarator->parameter_pack_p)
15179 || (decl_specifiers.type
15180 && PACK_EXPANSION_P (decl_specifiers.type)))
15181 {
15182 /* Find the name of the parameter pack. */
15183 cp_declarator *id_declarator = declarator;
15184 while (id_declarator && id_declarator->kind != cdk_id)
15185 id_declarator = id_declarator->declarator;
15186
15187 if (id_declarator && id_declarator->kind == cdk_id)
15188 error_at (declarator_token_start->location,
15189 template_parm_p
15190 ? "template parameter pack %qD"
15191 " cannot have a default argument"
15192 : "parameter pack %qD cannot have a default argument",
15193 id_declarator->u.id.unqualified_name);
15194 else
15195 error_at (declarator_token_start->location,
15196 template_parm_p
15197 ? "template parameter pack cannot have a default argument"
15198 : "parameter pack cannot have a default argument");
15199
15200 default_argument = NULL_TREE;
15201 }
15202 }
15203 else
15204 default_argument = NULL_TREE;
15205
15206 return make_parameter_declarator (&decl_specifiers,
15207 declarator,
15208 default_argument);
15209 }
15210
15211 /* Parse a default argument and return it.
15212
15213 TEMPLATE_PARM_P is true if this is a default argument for a
15214 non-type template parameter. */
15215 static tree
15216 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15217 {
15218 tree default_argument = NULL_TREE;
15219 bool saved_greater_than_is_operator_p;
15220 bool saved_local_variables_forbidden_p;
15221
15222 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15223 set correctly. */
15224 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15225 parser->greater_than_is_operator_p = !template_parm_p;
15226 /* Local variable names (and the `this' keyword) may not
15227 appear in a default argument. */
15228 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15229 parser->local_variables_forbidden_p = true;
15230 /* Parse the assignment-expression. */
15231 if (template_parm_p)
15232 push_deferring_access_checks (dk_no_deferred);
15233 default_argument
15234 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15235 if (template_parm_p)
15236 pop_deferring_access_checks ();
15237 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15238 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15239
15240 return default_argument;
15241 }
15242
15243 /* Parse a function-body.
15244
15245 function-body:
15246 compound_statement */
15247
15248 static void
15249 cp_parser_function_body (cp_parser *parser)
15250 {
15251 cp_parser_compound_statement (parser, NULL, false);
15252 }
15253
15254 /* Parse a ctor-initializer-opt followed by a function-body. Return
15255 true if a ctor-initializer was present. */
15256
15257 static bool
15258 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15259 {
15260 tree body;
15261 bool ctor_initializer_p;
15262
15263 /* Begin the function body. */
15264 body = begin_function_body ();
15265 /* Parse the optional ctor-initializer. */
15266 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15267 /* Parse the function-body. */
15268 cp_parser_function_body (parser);
15269 /* Finish the function body. */
15270 finish_function_body (body);
15271
15272 return ctor_initializer_p;
15273 }
15274
15275 /* Parse an initializer.
15276
15277 initializer:
15278 = initializer-clause
15279 ( expression-list )
15280
15281 Returns an expression representing the initializer. If no
15282 initializer is present, NULL_TREE is returned.
15283
15284 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15285 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15286 set to TRUE if there is no initializer present. If there is an
15287 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15288 is set to true; otherwise it is set to false. */
15289
15290 static tree
15291 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15292 bool* non_constant_p)
15293 {
15294 cp_token *token;
15295 tree init;
15296
15297 /* Peek at the next token. */
15298 token = cp_lexer_peek_token (parser->lexer);
15299
15300 /* Let our caller know whether or not this initializer was
15301 parenthesized. */
15302 *is_direct_init = (token->type != CPP_EQ);
15303 /* Assume that the initializer is constant. */
15304 *non_constant_p = false;
15305
15306 if (token->type == CPP_EQ)
15307 {
15308 /* Consume the `='. */
15309 cp_lexer_consume_token (parser->lexer);
15310 /* Parse the initializer-clause. */
15311 init = cp_parser_initializer_clause (parser, non_constant_p);
15312 }
15313 else if (token->type == CPP_OPEN_PAREN)
15314 {
15315 VEC(tree,gc) *vec;
15316 vec = cp_parser_parenthesized_expression_list (parser, false,
15317 /*cast_p=*/false,
15318 /*allow_expansion_p=*/true,
15319 non_constant_p);
15320 if (vec == NULL)
15321 return error_mark_node;
15322 init = build_tree_list_vec (vec);
15323 release_tree_vector (vec);
15324 }
15325 else if (token->type == CPP_OPEN_BRACE)
15326 {
15327 maybe_warn_cpp0x ("extended initializer lists");
15328 init = cp_parser_braced_list (parser, non_constant_p);
15329 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15330 }
15331 else
15332 {
15333 /* Anything else is an error. */
15334 cp_parser_error (parser, "expected initializer");
15335 init = error_mark_node;
15336 }
15337
15338 return init;
15339 }
15340
15341 /* Parse an initializer-clause.
15342
15343 initializer-clause:
15344 assignment-expression
15345 braced-init-list
15346
15347 Returns an expression representing the initializer.
15348
15349 If the `assignment-expression' production is used the value
15350 returned is simply a representation for the expression.
15351
15352 Otherwise, calls cp_parser_braced_list. */
15353
15354 static tree
15355 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15356 {
15357 tree initializer;
15358
15359 /* Assume the expression is constant. */
15360 *non_constant_p = false;
15361
15362 /* If it is not a `{', then we are looking at an
15363 assignment-expression. */
15364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15365 {
15366 initializer
15367 = cp_parser_constant_expression (parser,
15368 /*allow_non_constant_p=*/true,
15369 non_constant_p);
15370 if (!*non_constant_p)
15371 initializer = fold_non_dependent_expr (initializer);
15372 }
15373 else
15374 initializer = cp_parser_braced_list (parser, non_constant_p);
15375
15376 return initializer;
15377 }
15378
15379 /* Parse a brace-enclosed initializer list.
15380
15381 braced-init-list:
15382 { initializer-list , [opt] }
15383 { }
15384
15385 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15386 the elements of the initializer-list (or NULL, if the last
15387 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15388 NULL_TREE. There is no way to detect whether or not the optional
15389 trailing `,' was provided. NON_CONSTANT_P is as for
15390 cp_parser_initializer. */
15391
15392 static tree
15393 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15394 {
15395 tree initializer;
15396
15397 /* Consume the `{' token. */
15398 cp_lexer_consume_token (parser->lexer);
15399 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15400 initializer = make_node (CONSTRUCTOR);
15401 /* If it's not a `}', then there is a non-trivial initializer. */
15402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15403 {
15404 /* Parse the initializer list. */
15405 CONSTRUCTOR_ELTS (initializer)
15406 = cp_parser_initializer_list (parser, non_constant_p);
15407 /* A trailing `,' token is allowed. */
15408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15409 cp_lexer_consume_token (parser->lexer);
15410 }
15411 /* Now, there should be a trailing `}'. */
15412 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15413 TREE_TYPE (initializer) = init_list_type_node;
15414 return initializer;
15415 }
15416
15417 /* Parse an initializer-list.
15418
15419 initializer-list:
15420 initializer-clause ... [opt]
15421 initializer-list , initializer-clause ... [opt]
15422
15423 GNU Extension:
15424
15425 initializer-list:
15426 identifier : initializer-clause
15427 initializer-list, identifier : initializer-clause
15428
15429 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15430 for the initializer. If the INDEX of the elt is non-NULL, it is the
15431 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15432 as for cp_parser_initializer. */
15433
15434 static VEC(constructor_elt,gc) *
15435 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15436 {
15437 VEC(constructor_elt,gc) *v = NULL;
15438
15439 /* Assume all of the expressions are constant. */
15440 *non_constant_p = false;
15441
15442 /* Parse the rest of the list. */
15443 while (true)
15444 {
15445 cp_token *token;
15446 tree identifier;
15447 tree initializer;
15448 bool clause_non_constant_p;
15449
15450 /* If the next token is an identifier and the following one is a
15451 colon, we are looking at the GNU designated-initializer
15452 syntax. */
15453 if (cp_parser_allow_gnu_extensions_p (parser)
15454 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15455 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15456 {
15457 /* Warn the user that they are using an extension. */
15458 pedwarn (input_location, OPT_pedantic,
15459 "ISO C++ does not allow designated initializers");
15460 /* Consume the identifier. */
15461 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15462 /* Consume the `:'. */
15463 cp_lexer_consume_token (parser->lexer);
15464 }
15465 else
15466 identifier = NULL_TREE;
15467
15468 /* Parse the initializer. */
15469 initializer = cp_parser_initializer_clause (parser,
15470 &clause_non_constant_p);
15471 /* If any clause is non-constant, so is the entire initializer. */
15472 if (clause_non_constant_p)
15473 *non_constant_p = true;
15474
15475 /* If we have an ellipsis, this is an initializer pack
15476 expansion. */
15477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15478 {
15479 /* Consume the `...'. */
15480 cp_lexer_consume_token (parser->lexer);
15481
15482 /* Turn the initializer into an initializer expansion. */
15483 initializer = make_pack_expansion (initializer);
15484 }
15485
15486 /* Add it to the vector. */
15487 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15488
15489 /* If the next token is not a comma, we have reached the end of
15490 the list. */
15491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15492 break;
15493
15494 /* Peek at the next token. */
15495 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15496 /* If the next token is a `}', then we're still done. An
15497 initializer-clause can have a trailing `,' after the
15498 initializer-list and before the closing `}'. */
15499 if (token->type == CPP_CLOSE_BRACE)
15500 break;
15501
15502 /* Consume the `,' token. */
15503 cp_lexer_consume_token (parser->lexer);
15504 }
15505
15506 return v;
15507 }
15508
15509 /* Classes [gram.class] */
15510
15511 /* Parse a class-name.
15512
15513 class-name:
15514 identifier
15515 template-id
15516
15517 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15518 to indicate that names looked up in dependent types should be
15519 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15520 keyword has been used to indicate that the name that appears next
15521 is a template. TAG_TYPE indicates the explicit tag given before
15522 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15523 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15524 is the class being defined in a class-head.
15525
15526 Returns the TYPE_DECL representing the class. */
15527
15528 static tree
15529 cp_parser_class_name (cp_parser *parser,
15530 bool typename_keyword_p,
15531 bool template_keyword_p,
15532 enum tag_types tag_type,
15533 bool check_dependency_p,
15534 bool class_head_p,
15535 bool is_declaration)
15536 {
15537 tree decl;
15538 tree scope;
15539 bool typename_p;
15540 cp_token *token;
15541 tree identifier = NULL_TREE;
15542
15543 /* All class-names start with an identifier. */
15544 token = cp_lexer_peek_token (parser->lexer);
15545 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15546 {
15547 cp_parser_error (parser, "expected class-name");
15548 return error_mark_node;
15549 }
15550
15551 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15552 to a template-id, so we save it here. */
15553 scope = parser->scope;
15554 if (scope == error_mark_node)
15555 return error_mark_node;
15556
15557 /* Any name names a type if we're following the `typename' keyword
15558 in a qualified name where the enclosing scope is type-dependent. */
15559 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15560 && dependent_type_p (scope));
15561 /* Handle the common case (an identifier, but not a template-id)
15562 efficiently. */
15563 if (token->type == CPP_NAME
15564 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15565 {
15566 cp_token *identifier_token;
15567 bool ambiguous_p;
15568
15569 /* Look for the identifier. */
15570 identifier_token = cp_lexer_peek_token (parser->lexer);
15571 ambiguous_p = identifier_token->ambiguous_p;
15572 identifier = cp_parser_identifier (parser);
15573 /* If the next token isn't an identifier, we are certainly not
15574 looking at a class-name. */
15575 if (identifier == error_mark_node)
15576 decl = error_mark_node;
15577 /* If we know this is a type-name, there's no need to look it
15578 up. */
15579 else if (typename_p)
15580 decl = identifier;
15581 else
15582 {
15583 tree ambiguous_decls;
15584 /* If we already know that this lookup is ambiguous, then
15585 we've already issued an error message; there's no reason
15586 to check again. */
15587 if (ambiguous_p)
15588 {
15589 cp_parser_simulate_error (parser);
15590 return error_mark_node;
15591 }
15592 /* If the next token is a `::', then the name must be a type
15593 name.
15594
15595 [basic.lookup.qual]
15596
15597 During the lookup for a name preceding the :: scope
15598 resolution operator, object, function, and enumerator
15599 names are ignored. */
15600 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15601 tag_type = typename_type;
15602 /* Look up the name. */
15603 decl = cp_parser_lookup_name (parser, identifier,
15604 tag_type,
15605 /*is_template=*/false,
15606 /*is_namespace=*/false,
15607 check_dependency_p,
15608 &ambiguous_decls,
15609 identifier_token->location);
15610 if (ambiguous_decls)
15611 {
15612 error_at (identifier_token->location,
15613 "reference to %qD is ambiguous", identifier);
15614 print_candidates (ambiguous_decls);
15615 if (cp_parser_parsing_tentatively (parser))
15616 {
15617 identifier_token->ambiguous_p = true;
15618 cp_parser_simulate_error (parser);
15619 }
15620 return error_mark_node;
15621 }
15622 }
15623 }
15624 else
15625 {
15626 /* Try a template-id. */
15627 decl = cp_parser_template_id (parser, template_keyword_p,
15628 check_dependency_p,
15629 is_declaration);
15630 if (decl == error_mark_node)
15631 return error_mark_node;
15632 }
15633
15634 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15635
15636 /* If this is a typename, create a TYPENAME_TYPE. */
15637 if (typename_p && decl != error_mark_node)
15638 {
15639 decl = make_typename_type (scope, decl, typename_type,
15640 /*complain=*/tf_error);
15641 if (decl != error_mark_node)
15642 decl = TYPE_NAME (decl);
15643 }
15644
15645 /* Check to see that it is really the name of a class. */
15646 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15647 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15648 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15649 /* Situations like this:
15650
15651 template <typename T> struct A {
15652 typename T::template X<int>::I i;
15653 };
15654
15655 are problematic. Is `T::template X<int>' a class-name? The
15656 standard does not seem to be definitive, but there is no other
15657 valid interpretation of the following `::'. Therefore, those
15658 names are considered class-names. */
15659 {
15660 decl = make_typename_type (scope, decl, tag_type, tf_error);
15661 if (decl != error_mark_node)
15662 decl = TYPE_NAME (decl);
15663 }
15664 else if (TREE_CODE (decl) != TYPE_DECL
15665 || TREE_TYPE (decl) == error_mark_node
15666 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15667 decl = error_mark_node;
15668
15669 if (decl == error_mark_node)
15670 cp_parser_error (parser, "expected class-name");
15671 else if (identifier && !parser->scope)
15672 maybe_note_name_used_in_class (identifier, decl);
15673
15674 return decl;
15675 }
15676
15677 /* Parse a class-specifier.
15678
15679 class-specifier:
15680 class-head { member-specification [opt] }
15681
15682 Returns the TREE_TYPE representing the class. */
15683
15684 static tree
15685 cp_parser_class_specifier (cp_parser* parser)
15686 {
15687 tree type;
15688 tree attributes = NULL_TREE;
15689 bool nested_name_specifier_p;
15690 unsigned saved_num_template_parameter_lists;
15691 bool saved_in_function_body;
15692 bool saved_in_unbraced_linkage_specification_p;
15693 tree old_scope = NULL_TREE;
15694 tree scope = NULL_TREE;
15695 tree bases;
15696
15697 push_deferring_access_checks (dk_no_deferred);
15698
15699 /* Parse the class-head. */
15700 type = cp_parser_class_head (parser,
15701 &nested_name_specifier_p,
15702 &attributes,
15703 &bases);
15704 /* If the class-head was a semantic disaster, skip the entire body
15705 of the class. */
15706 if (!type)
15707 {
15708 cp_parser_skip_to_end_of_block_or_statement (parser);
15709 pop_deferring_access_checks ();
15710 return error_mark_node;
15711 }
15712
15713 /* Look for the `{'. */
15714 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15715 {
15716 pop_deferring_access_checks ();
15717 return error_mark_node;
15718 }
15719
15720 /* Process the base classes. If they're invalid, skip the
15721 entire class body. */
15722 if (!xref_basetypes (type, bases))
15723 {
15724 /* Consuming the closing brace yields better error messages
15725 later on. */
15726 if (cp_parser_skip_to_closing_brace (parser))
15727 cp_lexer_consume_token (parser->lexer);
15728 pop_deferring_access_checks ();
15729 return error_mark_node;
15730 }
15731
15732 /* Issue an error message if type-definitions are forbidden here. */
15733 cp_parser_check_type_definition (parser);
15734 /* Remember that we are defining one more class. */
15735 ++parser->num_classes_being_defined;
15736 /* Inside the class, surrounding template-parameter-lists do not
15737 apply. */
15738 saved_num_template_parameter_lists
15739 = parser->num_template_parameter_lists;
15740 parser->num_template_parameter_lists = 0;
15741 /* We are not in a function body. */
15742 saved_in_function_body = parser->in_function_body;
15743 parser->in_function_body = false;
15744 /* We are not immediately inside an extern "lang" block. */
15745 saved_in_unbraced_linkage_specification_p
15746 = parser->in_unbraced_linkage_specification_p;
15747 parser->in_unbraced_linkage_specification_p = false;
15748
15749 /* Start the class. */
15750 if (nested_name_specifier_p)
15751 {
15752 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15753 old_scope = push_inner_scope (scope);
15754 }
15755 type = begin_class_definition (type, attributes);
15756
15757 if (type == error_mark_node)
15758 /* If the type is erroneous, skip the entire body of the class. */
15759 cp_parser_skip_to_closing_brace (parser);
15760 else
15761 /* Parse the member-specification. */
15762 cp_parser_member_specification_opt (parser);
15763
15764 /* Look for the trailing `}'. */
15765 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15766 /* Look for trailing attributes to apply to this class. */
15767 if (cp_parser_allow_gnu_extensions_p (parser))
15768 attributes = cp_parser_attributes_opt (parser);
15769 if (type != error_mark_node)
15770 type = finish_struct (type, attributes);
15771 if (nested_name_specifier_p)
15772 pop_inner_scope (old_scope, scope);
15773 /* If this class is not itself within the scope of another class,
15774 then we need to parse the bodies of all of the queued function
15775 definitions. Note that the queued functions defined in a class
15776 are not always processed immediately following the
15777 class-specifier for that class. Consider:
15778
15779 struct A {
15780 struct B { void f() { sizeof (A); } };
15781 };
15782
15783 If `f' were processed before the processing of `A' were
15784 completed, there would be no way to compute the size of `A'.
15785 Note that the nesting we are interested in here is lexical --
15786 not the semantic nesting given by TYPE_CONTEXT. In particular,
15787 for:
15788
15789 struct A { struct B; };
15790 struct A::B { void f() { } };
15791
15792 there is no need to delay the parsing of `A::B::f'. */
15793 if (--parser->num_classes_being_defined == 0)
15794 {
15795 tree queue_entry;
15796 tree fn;
15797 tree class_type = NULL_TREE;
15798 tree pushed_scope = NULL_TREE;
15799
15800 /* In a first pass, parse default arguments to the functions.
15801 Then, in a second pass, parse the bodies of the functions.
15802 This two-phased approach handles cases like:
15803
15804 struct S {
15805 void f() { g(); }
15806 void g(int i = 3);
15807 };
15808
15809 */
15810 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15811 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15812 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15813 TREE_PURPOSE (parser->unparsed_functions_queues)
15814 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15815 {
15816 fn = TREE_VALUE (queue_entry);
15817 /* If there are default arguments that have not yet been processed,
15818 take care of them now. */
15819 if (class_type != TREE_PURPOSE (queue_entry))
15820 {
15821 if (pushed_scope)
15822 pop_scope (pushed_scope);
15823 class_type = TREE_PURPOSE (queue_entry);
15824 pushed_scope = push_scope (class_type);
15825 }
15826 /* Make sure that any template parameters are in scope. */
15827 maybe_begin_member_template_processing (fn);
15828 /* Parse the default argument expressions. */
15829 cp_parser_late_parsing_default_args (parser, fn);
15830 /* Remove any template parameters from the symbol table. */
15831 maybe_end_member_template_processing ();
15832 }
15833 if (pushed_scope)
15834 pop_scope (pushed_scope);
15835 /* Now parse the body of the functions. */
15836 for (TREE_VALUE (parser->unparsed_functions_queues)
15837 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15838 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15839 TREE_VALUE (parser->unparsed_functions_queues)
15840 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15841 {
15842 /* Figure out which function we need to process. */
15843 fn = TREE_VALUE (queue_entry);
15844 /* Parse the function. */
15845 cp_parser_late_parsing_for_member (parser, fn);
15846 }
15847 }
15848
15849 /* Put back any saved access checks. */
15850 pop_deferring_access_checks ();
15851
15852 /* Restore saved state. */
15853 parser->in_function_body = saved_in_function_body;
15854 parser->num_template_parameter_lists
15855 = saved_num_template_parameter_lists;
15856 parser->in_unbraced_linkage_specification_p
15857 = saved_in_unbraced_linkage_specification_p;
15858
15859 return type;
15860 }
15861
15862 /* Parse a class-head.
15863
15864 class-head:
15865 class-key identifier [opt] base-clause [opt]
15866 class-key nested-name-specifier identifier base-clause [opt]
15867 class-key nested-name-specifier [opt] template-id
15868 base-clause [opt]
15869
15870 GNU Extensions:
15871 class-key attributes identifier [opt] base-clause [opt]
15872 class-key attributes nested-name-specifier identifier base-clause [opt]
15873 class-key attributes nested-name-specifier [opt] template-id
15874 base-clause [opt]
15875
15876 Upon return BASES is initialized to the list of base classes (or
15877 NULL, if there are none) in the same form returned by
15878 cp_parser_base_clause.
15879
15880 Returns the TYPE of the indicated class. Sets
15881 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15882 involving a nested-name-specifier was used, and FALSE otherwise.
15883
15884 Returns error_mark_node if this is not a class-head.
15885
15886 Returns NULL_TREE if the class-head is syntactically valid, but
15887 semantically invalid in a way that means we should skip the entire
15888 body of the class. */
15889
15890 static tree
15891 cp_parser_class_head (cp_parser* parser,
15892 bool* nested_name_specifier_p,
15893 tree *attributes_p,
15894 tree *bases)
15895 {
15896 tree nested_name_specifier;
15897 enum tag_types class_key;
15898 tree id = NULL_TREE;
15899 tree type = NULL_TREE;
15900 tree attributes;
15901 bool template_id_p = false;
15902 bool qualified_p = false;
15903 bool invalid_nested_name_p = false;
15904 bool invalid_explicit_specialization_p = false;
15905 tree pushed_scope = NULL_TREE;
15906 unsigned num_templates;
15907 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15908 /* Assume no nested-name-specifier will be present. */
15909 *nested_name_specifier_p = false;
15910 /* Assume no template parameter lists will be used in defining the
15911 type. */
15912 num_templates = 0;
15913
15914 *bases = NULL_TREE;
15915
15916 /* Look for the class-key. */
15917 class_key = cp_parser_class_key (parser);
15918 if (class_key == none_type)
15919 return error_mark_node;
15920
15921 /* Parse the attributes. */
15922 attributes = cp_parser_attributes_opt (parser);
15923
15924 /* If the next token is `::', that is invalid -- but sometimes
15925 people do try to write:
15926
15927 struct ::S {};
15928
15929 Handle this gracefully by accepting the extra qualifier, and then
15930 issuing an error about it later if this really is a
15931 class-head. If it turns out just to be an elaborated type
15932 specifier, remain silent. */
15933 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15934 qualified_p = true;
15935
15936 push_deferring_access_checks (dk_no_check);
15937
15938 /* Determine the name of the class. Begin by looking for an
15939 optional nested-name-specifier. */
15940 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15941 nested_name_specifier
15942 = cp_parser_nested_name_specifier_opt (parser,
15943 /*typename_keyword_p=*/false,
15944 /*check_dependency_p=*/false,
15945 /*type_p=*/false,
15946 /*is_declaration=*/false);
15947 /* If there was a nested-name-specifier, then there *must* be an
15948 identifier. */
15949 if (nested_name_specifier)
15950 {
15951 type_start_token = cp_lexer_peek_token (parser->lexer);
15952 /* Although the grammar says `identifier', it really means
15953 `class-name' or `template-name'. You are only allowed to
15954 define a class that has already been declared with this
15955 syntax.
15956
15957 The proposed resolution for Core Issue 180 says that wherever
15958 you see `class T::X' you should treat `X' as a type-name.
15959
15960 It is OK to define an inaccessible class; for example:
15961
15962 class A { class B; };
15963 class A::B {};
15964
15965 We do not know if we will see a class-name, or a
15966 template-name. We look for a class-name first, in case the
15967 class-name is a template-id; if we looked for the
15968 template-name first we would stop after the template-name. */
15969 cp_parser_parse_tentatively (parser);
15970 type = cp_parser_class_name (parser,
15971 /*typename_keyword_p=*/false,
15972 /*template_keyword_p=*/false,
15973 class_type,
15974 /*check_dependency_p=*/false,
15975 /*class_head_p=*/true,
15976 /*is_declaration=*/false);
15977 /* If that didn't work, ignore the nested-name-specifier. */
15978 if (!cp_parser_parse_definitely (parser))
15979 {
15980 invalid_nested_name_p = true;
15981 type_start_token = cp_lexer_peek_token (parser->lexer);
15982 id = cp_parser_identifier (parser);
15983 if (id == error_mark_node)
15984 id = NULL_TREE;
15985 }
15986 /* If we could not find a corresponding TYPE, treat this
15987 declaration like an unqualified declaration. */
15988 if (type == error_mark_node)
15989 nested_name_specifier = NULL_TREE;
15990 /* Otherwise, count the number of templates used in TYPE and its
15991 containing scopes. */
15992 else
15993 {
15994 tree scope;
15995
15996 for (scope = TREE_TYPE (type);
15997 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15998 scope = (TYPE_P (scope)
15999 ? TYPE_CONTEXT (scope)
16000 : DECL_CONTEXT (scope)))
16001 if (TYPE_P (scope)
16002 && CLASS_TYPE_P (scope)
16003 && CLASSTYPE_TEMPLATE_INFO (scope)
16004 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16005 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16006 ++num_templates;
16007 }
16008 }
16009 /* Otherwise, the identifier is optional. */
16010 else
16011 {
16012 /* We don't know whether what comes next is a template-id,
16013 an identifier, or nothing at all. */
16014 cp_parser_parse_tentatively (parser);
16015 /* Check for a template-id. */
16016 type_start_token = cp_lexer_peek_token (parser->lexer);
16017 id = cp_parser_template_id (parser,
16018 /*template_keyword_p=*/false,
16019 /*check_dependency_p=*/true,
16020 /*is_declaration=*/true);
16021 /* If that didn't work, it could still be an identifier. */
16022 if (!cp_parser_parse_definitely (parser))
16023 {
16024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16025 {
16026 type_start_token = cp_lexer_peek_token (parser->lexer);
16027 id = cp_parser_identifier (parser);
16028 }
16029 else
16030 id = NULL_TREE;
16031 }
16032 else
16033 {
16034 template_id_p = true;
16035 ++num_templates;
16036 }
16037 }
16038
16039 pop_deferring_access_checks ();
16040
16041 if (id)
16042 cp_parser_check_for_invalid_template_id (parser, id,
16043 type_start_token->location);
16044
16045 /* If it's not a `:' or a `{' then we can't really be looking at a
16046 class-head, since a class-head only appears as part of a
16047 class-specifier. We have to detect this situation before calling
16048 xref_tag, since that has irreversible side-effects. */
16049 if (!cp_parser_next_token_starts_class_definition_p (parser))
16050 {
16051 cp_parser_error (parser, "expected %<{%> or %<:%>");
16052 return error_mark_node;
16053 }
16054
16055 /* At this point, we're going ahead with the class-specifier, even
16056 if some other problem occurs. */
16057 cp_parser_commit_to_tentative_parse (parser);
16058 /* Issue the error about the overly-qualified name now. */
16059 if (qualified_p)
16060 {
16061 cp_parser_error (parser,
16062 "global qualification of class name is invalid");
16063 return error_mark_node;
16064 }
16065 else if (invalid_nested_name_p)
16066 {
16067 cp_parser_error (parser,
16068 "qualified name does not name a class");
16069 return error_mark_node;
16070 }
16071 else if (nested_name_specifier)
16072 {
16073 tree scope;
16074
16075 /* Reject typedef-names in class heads. */
16076 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16077 {
16078 error_at (type_start_token->location,
16079 "invalid class name in declaration of %qD",
16080 type);
16081 type = NULL_TREE;
16082 goto done;
16083 }
16084
16085 /* Figure out in what scope the declaration is being placed. */
16086 scope = current_scope ();
16087 /* If that scope does not contain the scope in which the
16088 class was originally declared, the program is invalid. */
16089 if (scope && !is_ancestor (scope, nested_name_specifier))
16090 {
16091 if (at_namespace_scope_p ())
16092 error_at (type_start_token->location,
16093 "declaration of %qD in namespace %qD which does not "
16094 "enclose %qD",
16095 type, scope, nested_name_specifier);
16096 else
16097 error_at (type_start_token->location,
16098 "declaration of %qD in %qD which does not enclose %qD",
16099 type, scope, nested_name_specifier);
16100 type = NULL_TREE;
16101 goto done;
16102 }
16103 /* [dcl.meaning]
16104
16105 A declarator-id shall not be qualified except for the
16106 definition of a ... nested class outside of its class
16107 ... [or] the definition or explicit instantiation of a
16108 class member of a namespace outside of its namespace. */
16109 if (scope == nested_name_specifier)
16110 {
16111 permerror (nested_name_specifier_token_start->location,
16112 "extra qualification not allowed");
16113 nested_name_specifier = NULL_TREE;
16114 num_templates = 0;
16115 }
16116 }
16117 /* An explicit-specialization must be preceded by "template <>". If
16118 it is not, try to recover gracefully. */
16119 if (at_namespace_scope_p ()
16120 && parser->num_template_parameter_lists == 0
16121 && template_id_p)
16122 {
16123 error_at (type_start_token->location,
16124 "an explicit specialization must be preceded by %<template <>%>");
16125 invalid_explicit_specialization_p = true;
16126 /* Take the same action that would have been taken by
16127 cp_parser_explicit_specialization. */
16128 ++parser->num_template_parameter_lists;
16129 begin_specialization ();
16130 }
16131 /* There must be no "return" statements between this point and the
16132 end of this function; set "type "to the correct return value and
16133 use "goto done;" to return. */
16134 /* Make sure that the right number of template parameters were
16135 present. */
16136 if (!cp_parser_check_template_parameters (parser, num_templates,
16137 type_start_token->location,
16138 /*declarator=*/NULL))
16139 {
16140 /* If something went wrong, there is no point in even trying to
16141 process the class-definition. */
16142 type = NULL_TREE;
16143 goto done;
16144 }
16145
16146 /* Look up the type. */
16147 if (template_id_p)
16148 {
16149 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16150 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16151 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16152 {
16153 error_at (type_start_token->location,
16154 "function template %qD redeclared as a class template", id);
16155 type = error_mark_node;
16156 }
16157 else
16158 {
16159 type = TREE_TYPE (id);
16160 type = maybe_process_partial_specialization (type);
16161 }
16162 if (nested_name_specifier)
16163 pushed_scope = push_scope (nested_name_specifier);
16164 }
16165 else if (nested_name_specifier)
16166 {
16167 tree class_type;
16168
16169 /* Given:
16170
16171 template <typename T> struct S { struct T };
16172 template <typename T> struct S<T>::T { };
16173
16174 we will get a TYPENAME_TYPE when processing the definition of
16175 `S::T'. We need to resolve it to the actual type before we
16176 try to define it. */
16177 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16178 {
16179 class_type = resolve_typename_type (TREE_TYPE (type),
16180 /*only_current_p=*/false);
16181 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16182 type = TYPE_NAME (class_type);
16183 else
16184 {
16185 cp_parser_error (parser, "could not resolve typename type");
16186 type = error_mark_node;
16187 }
16188 }
16189
16190 if (maybe_process_partial_specialization (TREE_TYPE (type))
16191 == error_mark_node)
16192 {
16193 type = NULL_TREE;
16194 goto done;
16195 }
16196
16197 class_type = current_class_type;
16198 /* Enter the scope indicated by the nested-name-specifier. */
16199 pushed_scope = push_scope (nested_name_specifier);
16200 /* Get the canonical version of this type. */
16201 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16202 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16203 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16204 {
16205 type = push_template_decl (type);
16206 if (type == error_mark_node)
16207 {
16208 type = NULL_TREE;
16209 goto done;
16210 }
16211 }
16212
16213 type = TREE_TYPE (type);
16214 *nested_name_specifier_p = true;
16215 }
16216 else /* The name is not a nested name. */
16217 {
16218 /* If the class was unnamed, create a dummy name. */
16219 if (!id)
16220 id = make_anon_name ();
16221 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16222 parser->num_template_parameter_lists);
16223 }
16224
16225 /* Indicate whether this class was declared as a `class' or as a
16226 `struct'. */
16227 if (TREE_CODE (type) == RECORD_TYPE)
16228 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16229 cp_parser_check_class_key (class_key, type);
16230
16231 /* If this type was already complete, and we see another definition,
16232 that's an error. */
16233 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16234 {
16235 error_at (type_start_token->location, "redefinition of %q#T",
16236 type);
16237 error_at (type_start_token->location, "previous definition of %q+#T",
16238 type);
16239 type = NULL_TREE;
16240 goto done;
16241 }
16242 else if (type == error_mark_node)
16243 type = NULL_TREE;
16244
16245 /* We will have entered the scope containing the class; the names of
16246 base classes should be looked up in that context. For example:
16247
16248 struct A { struct B {}; struct C; };
16249 struct A::C : B {};
16250
16251 is valid. */
16252
16253 /* Get the list of base-classes, if there is one. */
16254 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16255 *bases = cp_parser_base_clause (parser);
16256
16257 done:
16258 /* Leave the scope given by the nested-name-specifier. We will
16259 enter the class scope itself while processing the members. */
16260 if (pushed_scope)
16261 pop_scope (pushed_scope);
16262
16263 if (invalid_explicit_specialization_p)
16264 {
16265 end_specialization ();
16266 --parser->num_template_parameter_lists;
16267 }
16268 *attributes_p = attributes;
16269 return type;
16270 }
16271
16272 /* Parse a class-key.
16273
16274 class-key:
16275 class
16276 struct
16277 union
16278
16279 Returns the kind of class-key specified, or none_type to indicate
16280 error. */
16281
16282 static enum tag_types
16283 cp_parser_class_key (cp_parser* parser)
16284 {
16285 cp_token *token;
16286 enum tag_types tag_type;
16287
16288 /* Look for the class-key. */
16289 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16290 if (!token)
16291 return none_type;
16292
16293 /* Check to see if the TOKEN is a class-key. */
16294 tag_type = cp_parser_token_is_class_key (token);
16295 if (!tag_type)
16296 cp_parser_error (parser, "expected class-key");
16297 return tag_type;
16298 }
16299
16300 /* Parse an (optional) member-specification.
16301
16302 member-specification:
16303 member-declaration member-specification [opt]
16304 access-specifier : member-specification [opt] */
16305
16306 static void
16307 cp_parser_member_specification_opt (cp_parser* parser)
16308 {
16309 while (true)
16310 {
16311 cp_token *token;
16312 enum rid keyword;
16313
16314 /* Peek at the next token. */
16315 token = cp_lexer_peek_token (parser->lexer);
16316 /* If it's a `}', or EOF then we've seen all the members. */
16317 if (token->type == CPP_CLOSE_BRACE
16318 || token->type == CPP_EOF
16319 || token->type == CPP_PRAGMA_EOL)
16320 break;
16321
16322 /* See if this token is a keyword. */
16323 keyword = token->keyword;
16324 switch (keyword)
16325 {
16326 case RID_PUBLIC:
16327 case RID_PROTECTED:
16328 case RID_PRIVATE:
16329 /* Consume the access-specifier. */
16330 cp_lexer_consume_token (parser->lexer);
16331 /* Remember which access-specifier is active. */
16332 current_access_specifier = token->u.value;
16333 /* Look for the `:'. */
16334 cp_parser_require (parser, CPP_COLON, "%<:%>");
16335 break;
16336
16337 default:
16338 /* Accept #pragmas at class scope. */
16339 if (token->type == CPP_PRAGMA)
16340 {
16341 cp_parser_pragma (parser, pragma_external);
16342 break;
16343 }
16344
16345 /* Otherwise, the next construction must be a
16346 member-declaration. */
16347 cp_parser_member_declaration (parser);
16348 }
16349 }
16350 }
16351
16352 /* Parse a member-declaration.
16353
16354 member-declaration:
16355 decl-specifier-seq [opt] member-declarator-list [opt] ;
16356 function-definition ; [opt]
16357 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16358 using-declaration
16359 template-declaration
16360
16361 member-declarator-list:
16362 member-declarator
16363 member-declarator-list , member-declarator
16364
16365 member-declarator:
16366 declarator pure-specifier [opt]
16367 declarator constant-initializer [opt]
16368 identifier [opt] : constant-expression
16369
16370 GNU Extensions:
16371
16372 member-declaration:
16373 __extension__ member-declaration
16374
16375 member-declarator:
16376 declarator attributes [opt] pure-specifier [opt]
16377 declarator attributes [opt] constant-initializer [opt]
16378 identifier [opt] attributes [opt] : constant-expression
16379
16380 C++0x Extensions:
16381
16382 member-declaration:
16383 static_assert-declaration */
16384
16385 static void
16386 cp_parser_member_declaration (cp_parser* parser)
16387 {
16388 cp_decl_specifier_seq decl_specifiers;
16389 tree prefix_attributes;
16390 tree decl;
16391 int declares_class_or_enum;
16392 bool friend_p;
16393 cp_token *token = NULL;
16394 cp_token *decl_spec_token_start = NULL;
16395 cp_token *initializer_token_start = NULL;
16396 int saved_pedantic;
16397
16398 /* Check for the `__extension__' keyword. */
16399 if (cp_parser_extension_opt (parser, &saved_pedantic))
16400 {
16401 /* Recurse. */
16402 cp_parser_member_declaration (parser);
16403 /* Restore the old value of the PEDANTIC flag. */
16404 pedantic = saved_pedantic;
16405
16406 return;
16407 }
16408
16409 /* Check for a template-declaration. */
16410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16411 {
16412 /* An explicit specialization here is an error condition, and we
16413 expect the specialization handler to detect and report this. */
16414 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16415 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16416 cp_parser_explicit_specialization (parser);
16417 else
16418 cp_parser_template_declaration (parser, /*member_p=*/true);
16419
16420 return;
16421 }
16422
16423 /* Check for a using-declaration. */
16424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16425 {
16426 /* Parse the using-declaration. */
16427 cp_parser_using_declaration (parser,
16428 /*access_declaration_p=*/false);
16429 return;
16430 }
16431
16432 /* Check for @defs. */
16433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16434 {
16435 tree ivar, member;
16436 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16437 ivar = ivar_chains;
16438 while (ivar)
16439 {
16440 member = ivar;
16441 ivar = TREE_CHAIN (member);
16442 TREE_CHAIN (member) = NULL_TREE;
16443 finish_member_declaration (member);
16444 }
16445 return;
16446 }
16447
16448 /* If the next token is `static_assert' we have a static assertion. */
16449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16450 {
16451 cp_parser_static_assert (parser, /*member_p=*/true);
16452 return;
16453 }
16454
16455 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16456 return;
16457
16458 /* Parse the decl-specifier-seq. */
16459 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16460 cp_parser_decl_specifier_seq (parser,
16461 CP_PARSER_FLAGS_OPTIONAL,
16462 &decl_specifiers,
16463 &declares_class_or_enum);
16464 prefix_attributes = decl_specifiers.attributes;
16465 decl_specifiers.attributes = NULL_TREE;
16466 /* Check for an invalid type-name. */
16467 if (!decl_specifiers.type
16468 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16469 return;
16470 /* If there is no declarator, then the decl-specifier-seq should
16471 specify a type. */
16472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16473 {
16474 /* If there was no decl-specifier-seq, and the next token is a
16475 `;', then we have something like:
16476
16477 struct S { ; };
16478
16479 [class.mem]
16480
16481 Each member-declaration shall declare at least one member
16482 name of the class. */
16483 if (!decl_specifiers.any_specifiers_p)
16484 {
16485 cp_token *token = cp_lexer_peek_token (parser->lexer);
16486 if (!in_system_header_at (token->location))
16487 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16488 }
16489 else
16490 {
16491 tree type;
16492
16493 /* See if this declaration is a friend. */
16494 friend_p = cp_parser_friend_p (&decl_specifiers);
16495 /* If there were decl-specifiers, check to see if there was
16496 a class-declaration. */
16497 type = check_tag_decl (&decl_specifiers);
16498 /* Nested classes have already been added to the class, but
16499 a `friend' needs to be explicitly registered. */
16500 if (friend_p)
16501 {
16502 /* If the `friend' keyword was present, the friend must
16503 be introduced with a class-key. */
16504 if (!declares_class_or_enum)
16505 error_at (decl_spec_token_start->location,
16506 "a class-key must be used when declaring a friend");
16507 /* In this case:
16508
16509 template <typename T> struct A {
16510 friend struct A<T>::B;
16511 };
16512
16513 A<T>::B will be represented by a TYPENAME_TYPE, and
16514 therefore not recognized by check_tag_decl. */
16515 if (!type
16516 && decl_specifiers.type
16517 && TYPE_P (decl_specifiers.type))
16518 type = decl_specifiers.type;
16519 if (!type || !TYPE_P (type))
16520 error_at (decl_spec_token_start->location,
16521 "friend declaration does not name a class or "
16522 "function");
16523 else
16524 make_friend_class (current_class_type, type,
16525 /*complain=*/true);
16526 }
16527 /* If there is no TYPE, an error message will already have
16528 been issued. */
16529 else if (!type || type == error_mark_node)
16530 ;
16531 /* An anonymous aggregate has to be handled specially; such
16532 a declaration really declares a data member (with a
16533 particular type), as opposed to a nested class. */
16534 else if (ANON_AGGR_TYPE_P (type))
16535 {
16536 /* Remove constructors and such from TYPE, now that we
16537 know it is an anonymous aggregate. */
16538 fixup_anonymous_aggr (type);
16539 /* And make the corresponding data member. */
16540 decl = build_decl (decl_spec_token_start->location,
16541 FIELD_DECL, NULL_TREE, type);
16542 /* Add it to the class. */
16543 finish_member_declaration (decl);
16544 }
16545 else
16546 cp_parser_check_access_in_redeclaration
16547 (TYPE_NAME (type),
16548 decl_spec_token_start->location);
16549 }
16550 }
16551 else
16552 {
16553 /* See if these declarations will be friends. */
16554 friend_p = cp_parser_friend_p (&decl_specifiers);
16555
16556 /* Keep going until we hit the `;' at the end of the
16557 declaration. */
16558 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16559 {
16560 tree attributes = NULL_TREE;
16561 tree first_attribute;
16562
16563 /* Peek at the next token. */
16564 token = cp_lexer_peek_token (parser->lexer);
16565
16566 /* Check for a bitfield declaration. */
16567 if (token->type == CPP_COLON
16568 || (token->type == CPP_NAME
16569 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16570 == CPP_COLON))
16571 {
16572 tree identifier;
16573 tree width;
16574
16575 /* Get the name of the bitfield. Note that we cannot just
16576 check TOKEN here because it may have been invalidated by
16577 the call to cp_lexer_peek_nth_token above. */
16578 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16579 identifier = cp_parser_identifier (parser);
16580 else
16581 identifier = NULL_TREE;
16582
16583 /* Consume the `:' token. */
16584 cp_lexer_consume_token (parser->lexer);
16585 /* Get the width of the bitfield. */
16586 width
16587 = cp_parser_constant_expression (parser,
16588 /*allow_non_constant=*/false,
16589 NULL);
16590
16591 /* Look for attributes that apply to the bitfield. */
16592 attributes = cp_parser_attributes_opt (parser);
16593 /* Remember which attributes are prefix attributes and
16594 which are not. */
16595 first_attribute = attributes;
16596 /* Combine the attributes. */
16597 attributes = chainon (prefix_attributes, attributes);
16598
16599 /* Create the bitfield declaration. */
16600 decl = grokbitfield (identifier
16601 ? make_id_declarator (NULL_TREE,
16602 identifier,
16603 sfk_none)
16604 : NULL,
16605 &decl_specifiers,
16606 width,
16607 attributes);
16608 }
16609 else
16610 {
16611 cp_declarator *declarator;
16612 tree initializer;
16613 tree asm_specification;
16614 int ctor_dtor_or_conv_p;
16615
16616 /* Parse the declarator. */
16617 declarator
16618 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16619 &ctor_dtor_or_conv_p,
16620 /*parenthesized_p=*/NULL,
16621 /*member_p=*/true);
16622
16623 /* If something went wrong parsing the declarator, make sure
16624 that we at least consume some tokens. */
16625 if (declarator == cp_error_declarator)
16626 {
16627 /* Skip to the end of the statement. */
16628 cp_parser_skip_to_end_of_statement (parser);
16629 /* If the next token is not a semicolon, that is
16630 probably because we just skipped over the body of
16631 a function. So, we consume a semicolon if
16632 present, but do not issue an error message if it
16633 is not present. */
16634 if (cp_lexer_next_token_is (parser->lexer,
16635 CPP_SEMICOLON))
16636 cp_lexer_consume_token (parser->lexer);
16637 return;
16638 }
16639
16640 if (declares_class_or_enum & 2)
16641 cp_parser_check_for_definition_in_return_type
16642 (declarator, decl_specifiers.type,
16643 decl_specifiers.type_location);
16644
16645 /* Look for an asm-specification. */
16646 asm_specification = cp_parser_asm_specification_opt (parser);
16647 /* Look for attributes that apply to the declaration. */
16648 attributes = cp_parser_attributes_opt (parser);
16649 /* Remember which attributes are prefix attributes and
16650 which are not. */
16651 first_attribute = attributes;
16652 /* Combine the attributes. */
16653 attributes = chainon (prefix_attributes, attributes);
16654
16655 /* If it's an `=', then we have a constant-initializer or a
16656 pure-specifier. It is not correct to parse the
16657 initializer before registering the member declaration
16658 since the member declaration should be in scope while
16659 its initializer is processed. However, the rest of the
16660 front end does not yet provide an interface that allows
16661 us to handle this correctly. */
16662 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16663 {
16664 /* In [class.mem]:
16665
16666 A pure-specifier shall be used only in the declaration of
16667 a virtual function.
16668
16669 A member-declarator can contain a constant-initializer
16670 only if it declares a static member of integral or
16671 enumeration type.
16672
16673 Therefore, if the DECLARATOR is for a function, we look
16674 for a pure-specifier; otherwise, we look for a
16675 constant-initializer. When we call `grokfield', it will
16676 perform more stringent semantics checks. */
16677 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16678 if (function_declarator_p (declarator))
16679 initializer = cp_parser_pure_specifier (parser);
16680 else
16681 /* Parse the initializer. */
16682 initializer = cp_parser_constant_initializer (parser);
16683 }
16684 /* Otherwise, there is no initializer. */
16685 else
16686 initializer = NULL_TREE;
16687
16688 /* See if we are probably looking at a function
16689 definition. We are certainly not looking at a
16690 member-declarator. Calling `grokfield' has
16691 side-effects, so we must not do it unless we are sure
16692 that we are looking at a member-declarator. */
16693 if (cp_parser_token_starts_function_definition_p
16694 (cp_lexer_peek_token (parser->lexer)))
16695 {
16696 /* The grammar does not allow a pure-specifier to be
16697 used when a member function is defined. (It is
16698 possible that this fact is an oversight in the
16699 standard, since a pure function may be defined
16700 outside of the class-specifier. */
16701 if (initializer)
16702 error_at (initializer_token_start->location,
16703 "pure-specifier on function-definition");
16704 decl = cp_parser_save_member_function_body (parser,
16705 &decl_specifiers,
16706 declarator,
16707 attributes);
16708 /* If the member was not a friend, declare it here. */
16709 if (!friend_p)
16710 finish_member_declaration (decl);
16711 /* Peek at the next token. */
16712 token = cp_lexer_peek_token (parser->lexer);
16713 /* If the next token is a semicolon, consume it. */
16714 if (token->type == CPP_SEMICOLON)
16715 cp_lexer_consume_token (parser->lexer);
16716 return;
16717 }
16718 else
16719 if (declarator->kind == cdk_function)
16720 declarator->id_loc = token->location;
16721 /* Create the declaration. */
16722 decl = grokfield (declarator, &decl_specifiers,
16723 initializer, /*init_const_expr_p=*/true,
16724 asm_specification,
16725 attributes);
16726 }
16727
16728 /* Reset PREFIX_ATTRIBUTES. */
16729 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16730 attributes = TREE_CHAIN (attributes);
16731 if (attributes)
16732 TREE_CHAIN (attributes) = NULL_TREE;
16733
16734 /* If there is any qualification still in effect, clear it
16735 now; we will be starting fresh with the next declarator. */
16736 parser->scope = NULL_TREE;
16737 parser->qualifying_scope = NULL_TREE;
16738 parser->object_scope = NULL_TREE;
16739 /* If it's a `,', then there are more declarators. */
16740 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16741 cp_lexer_consume_token (parser->lexer);
16742 /* If the next token isn't a `;', then we have a parse error. */
16743 else if (cp_lexer_next_token_is_not (parser->lexer,
16744 CPP_SEMICOLON))
16745 {
16746 cp_parser_error (parser, "expected %<;%>");
16747 /* Skip tokens until we find a `;'. */
16748 cp_parser_skip_to_end_of_statement (parser);
16749
16750 break;
16751 }
16752
16753 if (decl)
16754 {
16755 /* Add DECL to the list of members. */
16756 if (!friend_p)
16757 finish_member_declaration (decl);
16758
16759 if (TREE_CODE (decl) == FUNCTION_DECL)
16760 cp_parser_save_default_args (parser, decl);
16761 }
16762 }
16763 }
16764
16765 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16766 }
16767
16768 /* Parse a pure-specifier.
16769
16770 pure-specifier:
16771 = 0
16772
16773 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16774 Otherwise, ERROR_MARK_NODE is returned. */
16775
16776 static tree
16777 cp_parser_pure_specifier (cp_parser* parser)
16778 {
16779 cp_token *token;
16780
16781 /* Look for the `=' token. */
16782 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16783 return error_mark_node;
16784 /* Look for the `0' token. */
16785 token = cp_lexer_peek_token (parser->lexer);
16786
16787 if (token->type == CPP_EOF
16788 || token->type == CPP_PRAGMA_EOL)
16789 return error_mark_node;
16790
16791 cp_lexer_consume_token (parser->lexer);
16792
16793 /* Accept = default or = delete in c++0x mode. */
16794 if (token->keyword == RID_DEFAULT
16795 || token->keyword == RID_DELETE)
16796 {
16797 maybe_warn_cpp0x ("defaulted and deleted functions");
16798 return token->u.value;
16799 }
16800
16801 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16802 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16803 {
16804 cp_parser_error (parser,
16805 "invalid pure specifier (only %<= 0%> is allowed)");
16806 cp_parser_skip_to_end_of_statement (parser);
16807 return error_mark_node;
16808 }
16809 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16810 {
16811 error_at (token->location, "templates may not be %<virtual%>");
16812 return error_mark_node;
16813 }
16814
16815 return integer_zero_node;
16816 }
16817
16818 /* Parse a constant-initializer.
16819
16820 constant-initializer:
16821 = constant-expression
16822
16823 Returns a representation of the constant-expression. */
16824
16825 static tree
16826 cp_parser_constant_initializer (cp_parser* parser)
16827 {
16828 /* Look for the `=' token. */
16829 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16830 return error_mark_node;
16831
16832 /* It is invalid to write:
16833
16834 struct S { static const int i = { 7 }; };
16835
16836 */
16837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16838 {
16839 cp_parser_error (parser,
16840 "a brace-enclosed initializer is not allowed here");
16841 /* Consume the opening brace. */
16842 cp_lexer_consume_token (parser->lexer);
16843 /* Skip the initializer. */
16844 cp_parser_skip_to_closing_brace (parser);
16845 /* Look for the trailing `}'. */
16846 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16847
16848 return error_mark_node;
16849 }
16850
16851 return cp_parser_constant_expression (parser,
16852 /*allow_non_constant=*/false,
16853 NULL);
16854 }
16855
16856 /* Derived classes [gram.class.derived] */
16857
16858 /* Parse a base-clause.
16859
16860 base-clause:
16861 : base-specifier-list
16862
16863 base-specifier-list:
16864 base-specifier ... [opt]
16865 base-specifier-list , base-specifier ... [opt]
16866
16867 Returns a TREE_LIST representing the base-classes, in the order in
16868 which they were declared. The representation of each node is as
16869 described by cp_parser_base_specifier.
16870
16871 In the case that no bases are specified, this function will return
16872 NULL_TREE, not ERROR_MARK_NODE. */
16873
16874 static tree
16875 cp_parser_base_clause (cp_parser* parser)
16876 {
16877 tree bases = NULL_TREE;
16878
16879 /* Look for the `:' that begins the list. */
16880 cp_parser_require (parser, CPP_COLON, "%<:%>");
16881
16882 /* Scan the base-specifier-list. */
16883 while (true)
16884 {
16885 cp_token *token;
16886 tree base;
16887 bool pack_expansion_p = false;
16888
16889 /* Look for the base-specifier. */
16890 base = cp_parser_base_specifier (parser);
16891 /* Look for the (optional) ellipsis. */
16892 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16893 {
16894 /* Consume the `...'. */
16895 cp_lexer_consume_token (parser->lexer);
16896
16897 pack_expansion_p = true;
16898 }
16899
16900 /* Add BASE to the front of the list. */
16901 if (base != error_mark_node)
16902 {
16903 if (pack_expansion_p)
16904 /* Make this a pack expansion type. */
16905 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16906
16907
16908 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16909 {
16910 TREE_CHAIN (base) = bases;
16911 bases = base;
16912 }
16913 }
16914 /* Peek at the next token. */
16915 token = cp_lexer_peek_token (parser->lexer);
16916 /* If it's not a comma, then the list is complete. */
16917 if (token->type != CPP_COMMA)
16918 break;
16919 /* Consume the `,'. */
16920 cp_lexer_consume_token (parser->lexer);
16921 }
16922
16923 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16924 base class had a qualified name. However, the next name that
16925 appears is certainly not qualified. */
16926 parser->scope = NULL_TREE;
16927 parser->qualifying_scope = NULL_TREE;
16928 parser->object_scope = NULL_TREE;
16929
16930 return nreverse (bases);
16931 }
16932
16933 /* Parse a base-specifier.
16934
16935 base-specifier:
16936 :: [opt] nested-name-specifier [opt] class-name
16937 virtual access-specifier [opt] :: [opt] nested-name-specifier
16938 [opt] class-name
16939 access-specifier virtual [opt] :: [opt] nested-name-specifier
16940 [opt] class-name
16941
16942 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16943 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16944 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16945 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16946
16947 static tree
16948 cp_parser_base_specifier (cp_parser* parser)
16949 {
16950 cp_token *token;
16951 bool done = false;
16952 bool virtual_p = false;
16953 bool duplicate_virtual_error_issued_p = false;
16954 bool duplicate_access_error_issued_p = false;
16955 bool class_scope_p, template_p;
16956 tree access = access_default_node;
16957 tree type;
16958
16959 /* Process the optional `virtual' and `access-specifier'. */
16960 while (!done)
16961 {
16962 /* Peek at the next token. */
16963 token = cp_lexer_peek_token (parser->lexer);
16964 /* Process `virtual'. */
16965 switch (token->keyword)
16966 {
16967 case RID_VIRTUAL:
16968 /* If `virtual' appears more than once, issue an error. */
16969 if (virtual_p && !duplicate_virtual_error_issued_p)
16970 {
16971 cp_parser_error (parser,
16972 "%<virtual%> specified more than once in base-specified");
16973 duplicate_virtual_error_issued_p = true;
16974 }
16975
16976 virtual_p = true;
16977
16978 /* Consume the `virtual' token. */
16979 cp_lexer_consume_token (parser->lexer);
16980
16981 break;
16982
16983 case RID_PUBLIC:
16984 case RID_PROTECTED:
16985 case RID_PRIVATE:
16986 /* If more than one access specifier appears, issue an
16987 error. */
16988 if (access != access_default_node
16989 && !duplicate_access_error_issued_p)
16990 {
16991 cp_parser_error (parser,
16992 "more than one access specifier in base-specified");
16993 duplicate_access_error_issued_p = true;
16994 }
16995
16996 access = ridpointers[(int) token->keyword];
16997
16998 /* Consume the access-specifier. */
16999 cp_lexer_consume_token (parser->lexer);
17000
17001 break;
17002
17003 default:
17004 done = true;
17005 break;
17006 }
17007 }
17008 /* It is not uncommon to see programs mechanically, erroneously, use
17009 the 'typename' keyword to denote (dependent) qualified types
17010 as base classes. */
17011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17012 {
17013 token = cp_lexer_peek_token (parser->lexer);
17014 if (!processing_template_decl)
17015 error_at (token->location,
17016 "keyword %<typename%> not allowed outside of templates");
17017 else
17018 error_at (token->location,
17019 "keyword %<typename%> not allowed in this context "
17020 "(the base class is implicitly a type)");
17021 cp_lexer_consume_token (parser->lexer);
17022 }
17023
17024 /* Look for the optional `::' operator. */
17025 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17026 /* Look for the nested-name-specifier. The simplest way to
17027 implement:
17028
17029 [temp.res]
17030
17031 The keyword `typename' is not permitted in a base-specifier or
17032 mem-initializer; in these contexts a qualified name that
17033 depends on a template-parameter is implicitly assumed to be a
17034 type name.
17035
17036 is to pretend that we have seen the `typename' keyword at this
17037 point. */
17038 cp_parser_nested_name_specifier_opt (parser,
17039 /*typename_keyword_p=*/true,
17040 /*check_dependency_p=*/true,
17041 typename_type,
17042 /*is_declaration=*/true);
17043 /* If the base class is given by a qualified name, assume that names
17044 we see are type names or templates, as appropriate. */
17045 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17046 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17047
17048 /* Finally, look for the class-name. */
17049 type = cp_parser_class_name (parser,
17050 class_scope_p,
17051 template_p,
17052 typename_type,
17053 /*check_dependency_p=*/true,
17054 /*class_head_p=*/false,
17055 /*is_declaration=*/true);
17056
17057 if (type == error_mark_node)
17058 return error_mark_node;
17059
17060 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17061 }
17062
17063 /* Exception handling [gram.exception] */
17064
17065 /* Parse an (optional) exception-specification.
17066
17067 exception-specification:
17068 throw ( type-id-list [opt] )
17069
17070 Returns a TREE_LIST representing the exception-specification. The
17071 TREE_VALUE of each node is a type. */
17072
17073 static tree
17074 cp_parser_exception_specification_opt (cp_parser* parser)
17075 {
17076 cp_token *token;
17077 tree type_id_list;
17078
17079 /* Peek at the next token. */
17080 token = cp_lexer_peek_token (parser->lexer);
17081 /* If it's not `throw', then there's no exception-specification. */
17082 if (!cp_parser_is_keyword (token, RID_THROW))
17083 return NULL_TREE;
17084
17085 /* Consume the `throw'. */
17086 cp_lexer_consume_token (parser->lexer);
17087
17088 /* Look for the `('. */
17089 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17090
17091 /* Peek at the next token. */
17092 token = cp_lexer_peek_token (parser->lexer);
17093 /* If it's not a `)', then there is a type-id-list. */
17094 if (token->type != CPP_CLOSE_PAREN)
17095 {
17096 const char *saved_message;
17097
17098 /* Types may not be defined in an exception-specification. */
17099 saved_message = parser->type_definition_forbidden_message;
17100 parser->type_definition_forbidden_message
17101 = "types may not be defined in an exception-specification";
17102 /* Parse the type-id-list. */
17103 type_id_list = cp_parser_type_id_list (parser);
17104 /* Restore the saved message. */
17105 parser->type_definition_forbidden_message = saved_message;
17106 }
17107 else
17108 type_id_list = empty_except_spec;
17109
17110 /* Look for the `)'. */
17111 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17112
17113 return type_id_list;
17114 }
17115
17116 /* Parse an (optional) type-id-list.
17117
17118 type-id-list:
17119 type-id ... [opt]
17120 type-id-list , type-id ... [opt]
17121
17122 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17123 in the order that the types were presented. */
17124
17125 static tree
17126 cp_parser_type_id_list (cp_parser* parser)
17127 {
17128 tree types = NULL_TREE;
17129
17130 while (true)
17131 {
17132 cp_token *token;
17133 tree type;
17134
17135 /* Get the next type-id. */
17136 type = cp_parser_type_id (parser);
17137 /* Parse the optional ellipsis. */
17138 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17139 {
17140 /* Consume the `...'. */
17141 cp_lexer_consume_token (parser->lexer);
17142
17143 /* Turn the type into a pack expansion expression. */
17144 type = make_pack_expansion (type);
17145 }
17146 /* Add it to the list. */
17147 types = add_exception_specifier (types, type, /*complain=*/1);
17148 /* Peek at the next token. */
17149 token = cp_lexer_peek_token (parser->lexer);
17150 /* If it is not a `,', we are done. */
17151 if (token->type != CPP_COMMA)
17152 break;
17153 /* Consume the `,'. */
17154 cp_lexer_consume_token (parser->lexer);
17155 }
17156
17157 return nreverse (types);
17158 }
17159
17160 /* Parse a try-block.
17161
17162 try-block:
17163 try compound-statement handler-seq */
17164
17165 static tree
17166 cp_parser_try_block (cp_parser* parser)
17167 {
17168 tree try_block;
17169
17170 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17171 try_block = begin_try_block ();
17172 cp_parser_compound_statement (parser, NULL, true);
17173 finish_try_block (try_block);
17174 cp_parser_handler_seq (parser);
17175 finish_handler_sequence (try_block);
17176
17177 return try_block;
17178 }
17179
17180 /* Parse a function-try-block.
17181
17182 function-try-block:
17183 try ctor-initializer [opt] function-body handler-seq */
17184
17185 static bool
17186 cp_parser_function_try_block (cp_parser* parser)
17187 {
17188 tree compound_stmt;
17189 tree try_block;
17190 bool ctor_initializer_p;
17191
17192 /* Look for the `try' keyword. */
17193 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17194 return false;
17195 /* Let the rest of the front end know where we are. */
17196 try_block = begin_function_try_block (&compound_stmt);
17197 /* Parse the function-body. */
17198 ctor_initializer_p
17199 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17200 /* We're done with the `try' part. */
17201 finish_function_try_block (try_block);
17202 /* Parse the handlers. */
17203 cp_parser_handler_seq (parser);
17204 /* We're done with the handlers. */
17205 finish_function_handler_sequence (try_block, compound_stmt);
17206
17207 return ctor_initializer_p;
17208 }
17209
17210 /* Parse a handler-seq.
17211
17212 handler-seq:
17213 handler handler-seq [opt] */
17214
17215 static void
17216 cp_parser_handler_seq (cp_parser* parser)
17217 {
17218 while (true)
17219 {
17220 cp_token *token;
17221
17222 /* Parse the handler. */
17223 cp_parser_handler (parser);
17224 /* Peek at the next token. */
17225 token = cp_lexer_peek_token (parser->lexer);
17226 /* If it's not `catch' then there are no more handlers. */
17227 if (!cp_parser_is_keyword (token, RID_CATCH))
17228 break;
17229 }
17230 }
17231
17232 /* Parse a handler.
17233
17234 handler:
17235 catch ( exception-declaration ) compound-statement */
17236
17237 static void
17238 cp_parser_handler (cp_parser* parser)
17239 {
17240 tree handler;
17241 tree declaration;
17242
17243 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17244 handler = begin_handler ();
17245 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17246 declaration = cp_parser_exception_declaration (parser);
17247 finish_handler_parms (declaration, handler);
17248 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17249 cp_parser_compound_statement (parser, NULL, false);
17250 finish_handler (handler);
17251 }
17252
17253 /* Parse an exception-declaration.
17254
17255 exception-declaration:
17256 type-specifier-seq declarator
17257 type-specifier-seq abstract-declarator
17258 type-specifier-seq
17259 ...
17260
17261 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17262 ellipsis variant is used. */
17263
17264 static tree
17265 cp_parser_exception_declaration (cp_parser* parser)
17266 {
17267 cp_decl_specifier_seq type_specifiers;
17268 cp_declarator *declarator;
17269 const char *saved_message;
17270
17271 /* If it's an ellipsis, it's easy to handle. */
17272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17273 {
17274 /* Consume the `...' token. */
17275 cp_lexer_consume_token (parser->lexer);
17276 return NULL_TREE;
17277 }
17278
17279 /* Types may not be defined in exception-declarations. */
17280 saved_message = parser->type_definition_forbidden_message;
17281 parser->type_definition_forbidden_message
17282 = "types may not be defined in exception-declarations";
17283
17284 /* Parse the type-specifier-seq. */
17285 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17286 &type_specifiers);
17287 /* If it's a `)', then there is no declarator. */
17288 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17289 declarator = NULL;
17290 else
17291 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17292 /*ctor_dtor_or_conv_p=*/NULL,
17293 /*parenthesized_p=*/NULL,
17294 /*member_p=*/false);
17295
17296 /* Restore the saved message. */
17297 parser->type_definition_forbidden_message = saved_message;
17298
17299 if (!type_specifiers.any_specifiers_p)
17300 return error_mark_node;
17301
17302 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17303 }
17304
17305 /* Parse a throw-expression.
17306
17307 throw-expression:
17308 throw assignment-expression [opt]
17309
17310 Returns a THROW_EXPR representing the throw-expression. */
17311
17312 static tree
17313 cp_parser_throw_expression (cp_parser* parser)
17314 {
17315 tree expression;
17316 cp_token* token;
17317
17318 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17319 token = cp_lexer_peek_token (parser->lexer);
17320 /* Figure out whether or not there is an assignment-expression
17321 following the "throw" keyword. */
17322 if (token->type == CPP_COMMA
17323 || token->type == CPP_SEMICOLON
17324 || token->type == CPP_CLOSE_PAREN
17325 || token->type == CPP_CLOSE_SQUARE
17326 || token->type == CPP_CLOSE_BRACE
17327 || token->type == CPP_COLON)
17328 expression = NULL_TREE;
17329 else
17330 expression = cp_parser_assignment_expression (parser,
17331 /*cast_p=*/false, NULL);
17332
17333 return build_throw (expression);
17334 }
17335
17336 /* GNU Extensions */
17337
17338 /* Parse an (optional) asm-specification.
17339
17340 asm-specification:
17341 asm ( string-literal )
17342
17343 If the asm-specification is present, returns a STRING_CST
17344 corresponding to the string-literal. Otherwise, returns
17345 NULL_TREE. */
17346
17347 static tree
17348 cp_parser_asm_specification_opt (cp_parser* parser)
17349 {
17350 cp_token *token;
17351 tree asm_specification;
17352
17353 /* Peek at the next token. */
17354 token = cp_lexer_peek_token (parser->lexer);
17355 /* If the next token isn't the `asm' keyword, then there's no
17356 asm-specification. */
17357 if (!cp_parser_is_keyword (token, RID_ASM))
17358 return NULL_TREE;
17359
17360 /* Consume the `asm' token. */
17361 cp_lexer_consume_token (parser->lexer);
17362 /* Look for the `('. */
17363 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17364
17365 /* Look for the string-literal. */
17366 asm_specification = cp_parser_string_literal (parser, false, false);
17367
17368 /* Look for the `)'. */
17369 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17370
17371 return asm_specification;
17372 }
17373
17374 /* Parse an asm-operand-list.
17375
17376 asm-operand-list:
17377 asm-operand
17378 asm-operand-list , asm-operand
17379
17380 asm-operand:
17381 string-literal ( expression )
17382 [ string-literal ] string-literal ( expression )
17383
17384 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17385 each node is the expression. The TREE_PURPOSE is itself a
17386 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17387 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17388 is a STRING_CST for the string literal before the parenthesis. Returns
17389 ERROR_MARK_NODE if any of the operands are invalid. */
17390
17391 static tree
17392 cp_parser_asm_operand_list (cp_parser* parser)
17393 {
17394 tree asm_operands = NULL_TREE;
17395 bool invalid_operands = false;
17396
17397 while (true)
17398 {
17399 tree string_literal;
17400 tree expression;
17401 tree name;
17402
17403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17404 {
17405 /* Consume the `[' token. */
17406 cp_lexer_consume_token (parser->lexer);
17407 /* Read the operand name. */
17408 name = cp_parser_identifier (parser);
17409 if (name != error_mark_node)
17410 name = build_string (IDENTIFIER_LENGTH (name),
17411 IDENTIFIER_POINTER (name));
17412 /* Look for the closing `]'. */
17413 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17414 }
17415 else
17416 name = NULL_TREE;
17417 /* Look for the string-literal. */
17418 string_literal = cp_parser_string_literal (parser, false, false);
17419
17420 /* Look for the `('. */
17421 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17422 /* Parse the expression. */
17423 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17424 /* Look for the `)'. */
17425 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17426
17427 if (name == error_mark_node
17428 || string_literal == error_mark_node
17429 || expression == error_mark_node)
17430 invalid_operands = true;
17431
17432 /* Add this operand to the list. */
17433 asm_operands = tree_cons (build_tree_list (name, string_literal),
17434 expression,
17435 asm_operands);
17436 /* If the next token is not a `,', there are no more
17437 operands. */
17438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17439 break;
17440 /* Consume the `,'. */
17441 cp_lexer_consume_token (parser->lexer);
17442 }
17443
17444 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17445 }
17446
17447 /* Parse an asm-clobber-list.
17448
17449 asm-clobber-list:
17450 string-literal
17451 asm-clobber-list , string-literal
17452
17453 Returns a TREE_LIST, indicating the clobbers in the order that they
17454 appeared. The TREE_VALUE of each node is a STRING_CST. */
17455
17456 static tree
17457 cp_parser_asm_clobber_list (cp_parser* parser)
17458 {
17459 tree clobbers = NULL_TREE;
17460
17461 while (true)
17462 {
17463 tree string_literal;
17464
17465 /* Look for the string literal. */
17466 string_literal = cp_parser_string_literal (parser, false, false);
17467 /* Add it to the list. */
17468 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17469 /* If the next token is not a `,', then the list is
17470 complete. */
17471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17472 break;
17473 /* Consume the `,' token. */
17474 cp_lexer_consume_token (parser->lexer);
17475 }
17476
17477 return clobbers;
17478 }
17479
17480 /* Parse an asm-label-list.
17481
17482 asm-label-list:
17483 identifier
17484 asm-label-list , identifier
17485
17486 Returns a TREE_LIST, indicating the labels in the order that they
17487 appeared. The TREE_VALUE of each node is a label. */
17488
17489 static tree
17490 cp_parser_asm_label_list (cp_parser* parser)
17491 {
17492 tree labels = NULL_TREE;
17493
17494 while (true)
17495 {
17496 tree identifier, label, name;
17497
17498 /* Look for the identifier. */
17499 identifier = cp_parser_identifier (parser);
17500 if (!error_operand_p (identifier))
17501 {
17502 label = lookup_label (identifier);
17503 if (TREE_CODE (label) == LABEL_DECL)
17504 {
17505 TREE_USED (label) = 1;
17506 check_goto (label);
17507 name = build_string (IDENTIFIER_LENGTH (identifier),
17508 IDENTIFIER_POINTER (identifier));
17509 labels = tree_cons (name, label, labels);
17510 }
17511 }
17512 /* If the next token is not a `,', then the list is
17513 complete. */
17514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17515 break;
17516 /* Consume the `,' token. */
17517 cp_lexer_consume_token (parser->lexer);
17518 }
17519
17520 return nreverse (labels);
17521 }
17522
17523 /* Parse an (optional) series of attributes.
17524
17525 attributes:
17526 attributes attribute
17527
17528 attribute:
17529 __attribute__ (( attribute-list [opt] ))
17530
17531 The return value is as for cp_parser_attribute_list. */
17532
17533 static tree
17534 cp_parser_attributes_opt (cp_parser* parser)
17535 {
17536 tree attributes = NULL_TREE;
17537
17538 while (true)
17539 {
17540 cp_token *token;
17541 tree attribute_list;
17542
17543 /* Peek at the next token. */
17544 token = cp_lexer_peek_token (parser->lexer);
17545 /* If it's not `__attribute__', then we're done. */
17546 if (token->keyword != RID_ATTRIBUTE)
17547 break;
17548
17549 /* Consume the `__attribute__' keyword. */
17550 cp_lexer_consume_token (parser->lexer);
17551 /* Look for the two `(' tokens. */
17552 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17553 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17554
17555 /* Peek at the next token. */
17556 token = cp_lexer_peek_token (parser->lexer);
17557 if (token->type != CPP_CLOSE_PAREN)
17558 /* Parse the attribute-list. */
17559 attribute_list = cp_parser_attribute_list (parser);
17560 else
17561 /* If the next token is a `)', then there is no attribute
17562 list. */
17563 attribute_list = NULL;
17564
17565 /* Look for the two `)' tokens. */
17566 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17567 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17568
17569 /* Add these new attributes to the list. */
17570 attributes = chainon (attributes, attribute_list);
17571 }
17572
17573 return attributes;
17574 }
17575
17576 /* Parse an attribute-list.
17577
17578 attribute-list:
17579 attribute
17580 attribute-list , attribute
17581
17582 attribute:
17583 identifier
17584 identifier ( identifier )
17585 identifier ( identifier , expression-list )
17586 identifier ( expression-list )
17587
17588 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17589 to an attribute. The TREE_PURPOSE of each node is the identifier
17590 indicating which attribute is in use. The TREE_VALUE represents
17591 the arguments, if any. */
17592
17593 static tree
17594 cp_parser_attribute_list (cp_parser* parser)
17595 {
17596 tree attribute_list = NULL_TREE;
17597 bool save_translate_strings_p = parser->translate_strings_p;
17598
17599 parser->translate_strings_p = false;
17600 while (true)
17601 {
17602 cp_token *token;
17603 tree identifier;
17604 tree attribute;
17605
17606 /* Look for the identifier. We also allow keywords here; for
17607 example `__attribute__ ((const))' is legal. */
17608 token = cp_lexer_peek_token (parser->lexer);
17609 if (token->type == CPP_NAME
17610 || token->type == CPP_KEYWORD)
17611 {
17612 tree arguments = NULL_TREE;
17613
17614 /* Consume the token. */
17615 token = cp_lexer_consume_token (parser->lexer);
17616
17617 /* Save away the identifier that indicates which attribute
17618 this is. */
17619 identifier = (token->type == CPP_KEYWORD)
17620 /* For keywords, use the canonical spelling, not the
17621 parsed identifier. */
17622 ? ridpointers[(int) token->keyword]
17623 : token->u.value;
17624
17625 attribute = build_tree_list (identifier, NULL_TREE);
17626
17627 /* Peek at the next token. */
17628 token = cp_lexer_peek_token (parser->lexer);
17629 /* If it's an `(', then parse the attribute arguments. */
17630 if (token->type == CPP_OPEN_PAREN)
17631 {
17632 VEC(tree,gc) *vec;
17633 vec = cp_parser_parenthesized_expression_list
17634 (parser, true, /*cast_p=*/false,
17635 /*allow_expansion_p=*/false,
17636 /*non_constant_p=*/NULL);
17637 if (vec == NULL)
17638 arguments = error_mark_node;
17639 else
17640 {
17641 arguments = build_tree_list_vec (vec);
17642 release_tree_vector (vec);
17643 }
17644 /* Save the arguments away. */
17645 TREE_VALUE (attribute) = arguments;
17646 }
17647
17648 if (arguments != error_mark_node)
17649 {
17650 /* Add this attribute to the list. */
17651 TREE_CHAIN (attribute) = attribute_list;
17652 attribute_list = attribute;
17653 }
17654
17655 token = cp_lexer_peek_token (parser->lexer);
17656 }
17657 /* Now, look for more attributes. If the next token isn't a
17658 `,', we're done. */
17659 if (token->type != CPP_COMMA)
17660 break;
17661
17662 /* Consume the comma and keep going. */
17663 cp_lexer_consume_token (parser->lexer);
17664 }
17665 parser->translate_strings_p = save_translate_strings_p;
17666
17667 /* We built up the list in reverse order. */
17668 return nreverse (attribute_list);
17669 }
17670
17671 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17672 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17673 current value of the PEDANTIC flag, regardless of whether or not
17674 the `__extension__' keyword is present. The caller is responsible
17675 for restoring the value of the PEDANTIC flag. */
17676
17677 static bool
17678 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17679 {
17680 /* Save the old value of the PEDANTIC flag. */
17681 *saved_pedantic = pedantic;
17682
17683 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17684 {
17685 /* Consume the `__extension__' token. */
17686 cp_lexer_consume_token (parser->lexer);
17687 /* We're not being pedantic while the `__extension__' keyword is
17688 in effect. */
17689 pedantic = 0;
17690
17691 return true;
17692 }
17693
17694 return false;
17695 }
17696
17697 /* Parse a label declaration.
17698
17699 label-declaration:
17700 __label__ label-declarator-seq ;
17701
17702 label-declarator-seq:
17703 identifier , label-declarator-seq
17704 identifier */
17705
17706 static void
17707 cp_parser_label_declaration (cp_parser* parser)
17708 {
17709 /* Look for the `__label__' keyword. */
17710 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17711
17712 while (true)
17713 {
17714 tree identifier;
17715
17716 /* Look for an identifier. */
17717 identifier = cp_parser_identifier (parser);
17718 /* If we failed, stop. */
17719 if (identifier == error_mark_node)
17720 break;
17721 /* Declare it as a label. */
17722 finish_label_decl (identifier);
17723 /* If the next token is a `;', stop. */
17724 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17725 break;
17726 /* Look for the `,' separating the label declarations. */
17727 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17728 }
17729
17730 /* Look for the final `;'. */
17731 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17732 }
17733
17734 /* Support Functions */
17735
17736 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17737 NAME should have one of the representations used for an
17738 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17739 is returned. If PARSER->SCOPE is a dependent type, then a
17740 SCOPE_REF is returned.
17741
17742 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17743 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17744 was formed. Abstractly, such entities should not be passed to this
17745 function, because they do not need to be looked up, but it is
17746 simpler to check for this special case here, rather than at the
17747 call-sites.
17748
17749 In cases not explicitly covered above, this function returns a
17750 DECL, OVERLOAD, or baselink representing the result of the lookup.
17751 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17752 is returned.
17753
17754 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17755 (e.g., "struct") that was used. In that case bindings that do not
17756 refer to types are ignored.
17757
17758 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17759 ignored.
17760
17761 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17762 are ignored.
17763
17764 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17765 types.
17766
17767 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17768 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17769 NULL_TREE otherwise. */
17770
17771 static tree
17772 cp_parser_lookup_name (cp_parser *parser, tree name,
17773 enum tag_types tag_type,
17774 bool is_template,
17775 bool is_namespace,
17776 bool check_dependency,
17777 tree *ambiguous_decls,
17778 location_t name_location)
17779 {
17780 int flags = 0;
17781 tree decl;
17782 tree object_type = parser->context->object_type;
17783
17784 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17785 flags |= LOOKUP_COMPLAIN;
17786
17787 /* Assume that the lookup will be unambiguous. */
17788 if (ambiguous_decls)
17789 *ambiguous_decls = NULL_TREE;
17790
17791 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17792 no longer valid. Note that if we are parsing tentatively, and
17793 the parse fails, OBJECT_TYPE will be automatically restored. */
17794 parser->context->object_type = NULL_TREE;
17795
17796 if (name == error_mark_node)
17797 return error_mark_node;
17798
17799 /* A template-id has already been resolved; there is no lookup to
17800 do. */
17801 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17802 return name;
17803 if (BASELINK_P (name))
17804 {
17805 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17806 == TEMPLATE_ID_EXPR);
17807 return name;
17808 }
17809
17810 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17811 it should already have been checked to make sure that the name
17812 used matches the type being destroyed. */
17813 if (TREE_CODE (name) == BIT_NOT_EXPR)
17814 {
17815 tree type;
17816
17817 /* Figure out to which type this destructor applies. */
17818 if (parser->scope)
17819 type = parser->scope;
17820 else if (object_type)
17821 type = object_type;
17822 else
17823 type = current_class_type;
17824 /* If that's not a class type, there is no destructor. */
17825 if (!type || !CLASS_TYPE_P (type))
17826 return error_mark_node;
17827 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17828 lazily_declare_fn (sfk_destructor, type);
17829 if (!CLASSTYPE_DESTRUCTORS (type))
17830 return error_mark_node;
17831 /* If it was a class type, return the destructor. */
17832 return CLASSTYPE_DESTRUCTORS (type);
17833 }
17834
17835 /* By this point, the NAME should be an ordinary identifier. If
17836 the id-expression was a qualified name, the qualifying scope is
17837 stored in PARSER->SCOPE at this point. */
17838 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17839
17840 /* Perform the lookup. */
17841 if (parser->scope)
17842 {
17843 bool dependent_p;
17844
17845 if (parser->scope == error_mark_node)
17846 return error_mark_node;
17847
17848 /* If the SCOPE is dependent, the lookup must be deferred until
17849 the template is instantiated -- unless we are explicitly
17850 looking up names in uninstantiated templates. Even then, we
17851 cannot look up the name if the scope is not a class type; it
17852 might, for example, be a template type parameter. */
17853 dependent_p = (TYPE_P (parser->scope)
17854 && dependent_scope_p (parser->scope));
17855 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17856 && dependent_p)
17857 /* Defer lookup. */
17858 decl = error_mark_node;
17859 else
17860 {
17861 tree pushed_scope = NULL_TREE;
17862
17863 /* If PARSER->SCOPE is a dependent type, then it must be a
17864 class type, and we must not be checking dependencies;
17865 otherwise, we would have processed this lookup above. So
17866 that PARSER->SCOPE is not considered a dependent base by
17867 lookup_member, we must enter the scope here. */
17868 if (dependent_p)
17869 pushed_scope = push_scope (parser->scope);
17870 /* If the PARSER->SCOPE is a template specialization, it
17871 may be instantiated during name lookup. In that case,
17872 errors may be issued. Even if we rollback the current
17873 tentative parse, those errors are valid. */
17874 decl = lookup_qualified_name (parser->scope, name,
17875 tag_type != none_type,
17876 /*complain=*/true);
17877
17878 /* If we have a single function from a using decl, pull it out. */
17879 if (TREE_CODE (decl) == OVERLOAD
17880 && !really_overloaded_fn (decl))
17881 decl = OVL_FUNCTION (decl);
17882
17883 if (pushed_scope)
17884 pop_scope (pushed_scope);
17885 }
17886
17887 /* If the scope is a dependent type and either we deferred lookup or
17888 we did lookup but didn't find the name, rememeber the name. */
17889 if (decl == error_mark_node && TYPE_P (parser->scope)
17890 && dependent_type_p (parser->scope))
17891 {
17892 if (tag_type)
17893 {
17894 tree type;
17895
17896 /* The resolution to Core Issue 180 says that `struct
17897 A::B' should be considered a type-name, even if `A'
17898 is dependent. */
17899 type = make_typename_type (parser->scope, name, tag_type,
17900 /*complain=*/tf_error);
17901 decl = TYPE_NAME (type);
17902 }
17903 else if (is_template
17904 && (cp_parser_next_token_ends_template_argument_p (parser)
17905 || cp_lexer_next_token_is (parser->lexer,
17906 CPP_CLOSE_PAREN)))
17907 decl = make_unbound_class_template (parser->scope,
17908 name, NULL_TREE,
17909 /*complain=*/tf_error);
17910 else
17911 decl = build_qualified_name (/*type=*/NULL_TREE,
17912 parser->scope, name,
17913 is_template);
17914 }
17915 parser->qualifying_scope = parser->scope;
17916 parser->object_scope = NULL_TREE;
17917 }
17918 else if (object_type)
17919 {
17920 tree object_decl = NULL_TREE;
17921 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17922 OBJECT_TYPE is not a class. */
17923 if (CLASS_TYPE_P (object_type))
17924 /* If the OBJECT_TYPE is a template specialization, it may
17925 be instantiated during name lookup. In that case, errors
17926 may be issued. Even if we rollback the current tentative
17927 parse, those errors are valid. */
17928 object_decl = lookup_member (object_type,
17929 name,
17930 /*protect=*/0,
17931 tag_type != none_type);
17932 /* Look it up in the enclosing context, too. */
17933 decl = lookup_name_real (name, tag_type != none_type,
17934 /*nonclass=*/0,
17935 /*block_p=*/true, is_namespace, flags);
17936 parser->object_scope = object_type;
17937 parser->qualifying_scope = NULL_TREE;
17938 if (object_decl)
17939 decl = object_decl;
17940 }
17941 else
17942 {
17943 decl = lookup_name_real (name, tag_type != none_type,
17944 /*nonclass=*/0,
17945 /*block_p=*/true, is_namespace, flags);
17946 parser->qualifying_scope = NULL_TREE;
17947 parser->object_scope = NULL_TREE;
17948 }
17949
17950 /* If the lookup failed, let our caller know. */
17951 if (!decl || decl == error_mark_node)
17952 return error_mark_node;
17953
17954 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17955 if (TREE_CODE (decl) == TREE_LIST)
17956 {
17957 if (ambiguous_decls)
17958 *ambiguous_decls = decl;
17959 /* The error message we have to print is too complicated for
17960 cp_parser_error, so we incorporate its actions directly. */
17961 if (!cp_parser_simulate_error (parser))
17962 {
17963 error_at (name_location, "reference to %qD is ambiguous",
17964 name);
17965 print_candidates (decl);
17966 }
17967 return error_mark_node;
17968 }
17969
17970 gcc_assert (DECL_P (decl)
17971 || TREE_CODE (decl) == OVERLOAD
17972 || TREE_CODE (decl) == SCOPE_REF
17973 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17974 || BASELINK_P (decl));
17975
17976 /* If we have resolved the name of a member declaration, check to
17977 see if the declaration is accessible. When the name resolves to
17978 set of overloaded functions, accessibility is checked when
17979 overload resolution is done.
17980
17981 During an explicit instantiation, access is not checked at all,
17982 as per [temp.explicit]. */
17983 if (DECL_P (decl))
17984 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17985
17986 return decl;
17987 }
17988
17989 /* Like cp_parser_lookup_name, but for use in the typical case where
17990 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17991 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17992
17993 static tree
17994 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17995 {
17996 return cp_parser_lookup_name (parser, name,
17997 none_type,
17998 /*is_template=*/false,
17999 /*is_namespace=*/false,
18000 /*check_dependency=*/true,
18001 /*ambiguous_decls=*/NULL,
18002 location);
18003 }
18004
18005 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18006 the current context, return the TYPE_DECL. If TAG_NAME_P is
18007 true, the DECL indicates the class being defined in a class-head,
18008 or declared in an elaborated-type-specifier.
18009
18010 Otherwise, return DECL. */
18011
18012 static tree
18013 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18014 {
18015 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18016 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18017
18018 struct A {
18019 template <typename T> struct B;
18020 };
18021
18022 template <typename T> struct A::B {};
18023
18024 Similarly, in an elaborated-type-specifier:
18025
18026 namespace N { struct X{}; }
18027
18028 struct A {
18029 template <typename T> friend struct N::X;
18030 };
18031
18032 However, if the DECL refers to a class type, and we are in
18033 the scope of the class, then the name lookup automatically
18034 finds the TYPE_DECL created by build_self_reference rather
18035 than a TEMPLATE_DECL. For example, in:
18036
18037 template <class T> struct S {
18038 S s;
18039 };
18040
18041 there is no need to handle such case. */
18042
18043 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18044 return DECL_TEMPLATE_RESULT (decl);
18045
18046 return decl;
18047 }
18048
18049 /* If too many, or too few, template-parameter lists apply to the
18050 declarator, issue an error message. Returns TRUE if all went well,
18051 and FALSE otherwise. */
18052
18053 static bool
18054 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18055 cp_declarator *declarator,
18056 location_t declarator_location)
18057 {
18058 unsigned num_templates;
18059
18060 /* We haven't seen any classes that involve template parameters yet. */
18061 num_templates = 0;
18062
18063 switch (declarator->kind)
18064 {
18065 case cdk_id:
18066 if (declarator->u.id.qualifying_scope)
18067 {
18068 tree scope;
18069 tree member;
18070
18071 scope = declarator->u.id.qualifying_scope;
18072 member = declarator->u.id.unqualified_name;
18073
18074 while (scope && CLASS_TYPE_P (scope))
18075 {
18076 /* You're supposed to have one `template <...>'
18077 for every template class, but you don't need one
18078 for a full specialization. For example:
18079
18080 template <class T> struct S{};
18081 template <> struct S<int> { void f(); };
18082 void S<int>::f () {}
18083
18084 is correct; there shouldn't be a `template <>' for
18085 the definition of `S<int>::f'. */
18086 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18087 /* If SCOPE does not have template information of any
18088 kind, then it is not a template, nor is it nested
18089 within a template. */
18090 break;
18091 if (explicit_class_specialization_p (scope))
18092 break;
18093 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18094 ++num_templates;
18095
18096 scope = TYPE_CONTEXT (scope);
18097 }
18098 }
18099 else if (TREE_CODE (declarator->u.id.unqualified_name)
18100 == TEMPLATE_ID_EXPR)
18101 /* If the DECLARATOR has the form `X<y>' then it uses one
18102 additional level of template parameters. */
18103 ++num_templates;
18104
18105 return cp_parser_check_template_parameters
18106 (parser, num_templates, declarator_location, declarator);
18107
18108
18109 case cdk_function:
18110 case cdk_array:
18111 case cdk_pointer:
18112 case cdk_reference:
18113 case cdk_ptrmem:
18114 return (cp_parser_check_declarator_template_parameters
18115 (parser, declarator->declarator, declarator_location));
18116
18117 case cdk_error:
18118 return true;
18119
18120 default:
18121 gcc_unreachable ();
18122 }
18123 return false;
18124 }
18125
18126 /* NUM_TEMPLATES were used in the current declaration. If that is
18127 invalid, return FALSE and issue an error messages. Otherwise,
18128 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18129 declarator and we can print more accurate diagnostics. */
18130
18131 static bool
18132 cp_parser_check_template_parameters (cp_parser* parser,
18133 unsigned num_templates,
18134 location_t location,
18135 cp_declarator *declarator)
18136 {
18137 /* If there are the same number of template classes and parameter
18138 lists, that's OK. */
18139 if (parser->num_template_parameter_lists == num_templates)
18140 return true;
18141 /* If there are more, but only one more, then we are referring to a
18142 member template. That's OK too. */
18143 if (parser->num_template_parameter_lists == num_templates + 1)
18144 return true;
18145 /* If there are more template classes than parameter lists, we have
18146 something like:
18147
18148 template <class T> void S<T>::R<T>::f (); */
18149 if (parser->num_template_parameter_lists < num_templates)
18150 {
18151 if (declarator)
18152 error_at (location, "specializing member %<%T::%E%> "
18153 "requires %<template<>%> syntax",
18154 declarator->u.id.qualifying_scope,
18155 declarator->u.id.unqualified_name);
18156 else
18157 error_at (location, "too few template-parameter-lists");
18158 return false;
18159 }
18160 /* Otherwise, there are too many template parameter lists. We have
18161 something like:
18162
18163 template <class T> template <class U> void S::f(); */
18164 error_at (location, "too many template-parameter-lists");
18165 return false;
18166 }
18167
18168 /* Parse an optional `::' token indicating that the following name is
18169 from the global namespace. If so, PARSER->SCOPE is set to the
18170 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18171 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18172 Returns the new value of PARSER->SCOPE, if the `::' token is
18173 present, and NULL_TREE otherwise. */
18174
18175 static tree
18176 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18177 {
18178 cp_token *token;
18179
18180 /* Peek at the next token. */
18181 token = cp_lexer_peek_token (parser->lexer);
18182 /* If we're looking at a `::' token then we're starting from the
18183 global namespace, not our current location. */
18184 if (token->type == CPP_SCOPE)
18185 {
18186 /* Consume the `::' token. */
18187 cp_lexer_consume_token (parser->lexer);
18188 /* Set the SCOPE so that we know where to start the lookup. */
18189 parser->scope = global_namespace;
18190 parser->qualifying_scope = global_namespace;
18191 parser->object_scope = NULL_TREE;
18192
18193 return parser->scope;
18194 }
18195 else if (!current_scope_valid_p)
18196 {
18197 parser->scope = NULL_TREE;
18198 parser->qualifying_scope = NULL_TREE;
18199 parser->object_scope = NULL_TREE;
18200 }
18201
18202 return NULL_TREE;
18203 }
18204
18205 /* Returns TRUE if the upcoming token sequence is the start of a
18206 constructor declarator. If FRIEND_P is true, the declarator is
18207 preceded by the `friend' specifier. */
18208
18209 static bool
18210 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18211 {
18212 bool constructor_p;
18213 tree type_decl = NULL_TREE;
18214 bool nested_name_p;
18215 cp_token *next_token;
18216
18217 /* The common case is that this is not a constructor declarator, so
18218 try to avoid doing lots of work if at all possible. It's not
18219 valid declare a constructor at function scope. */
18220 if (parser->in_function_body)
18221 return false;
18222 /* And only certain tokens can begin a constructor declarator. */
18223 next_token = cp_lexer_peek_token (parser->lexer);
18224 if (next_token->type != CPP_NAME
18225 && next_token->type != CPP_SCOPE
18226 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18227 && next_token->type != CPP_TEMPLATE_ID)
18228 return false;
18229
18230 /* Parse tentatively; we are going to roll back all of the tokens
18231 consumed here. */
18232 cp_parser_parse_tentatively (parser);
18233 /* Assume that we are looking at a constructor declarator. */
18234 constructor_p = true;
18235
18236 /* Look for the optional `::' operator. */
18237 cp_parser_global_scope_opt (parser,
18238 /*current_scope_valid_p=*/false);
18239 /* Look for the nested-name-specifier. */
18240 nested_name_p
18241 = (cp_parser_nested_name_specifier_opt (parser,
18242 /*typename_keyword_p=*/false,
18243 /*check_dependency_p=*/false,
18244 /*type_p=*/false,
18245 /*is_declaration=*/false)
18246 != NULL_TREE);
18247 /* Outside of a class-specifier, there must be a
18248 nested-name-specifier. */
18249 if (!nested_name_p &&
18250 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18251 || friend_p))
18252 constructor_p = false;
18253 /* If we still think that this might be a constructor-declarator,
18254 look for a class-name. */
18255 if (constructor_p)
18256 {
18257 /* If we have:
18258
18259 template <typename T> struct S { S(); };
18260 template <typename T> S<T>::S ();
18261
18262 we must recognize that the nested `S' names a class.
18263 Similarly, for:
18264
18265 template <typename T> S<T>::S<T> ();
18266
18267 we must recognize that the nested `S' names a template. */
18268 type_decl = cp_parser_class_name (parser,
18269 /*typename_keyword_p=*/false,
18270 /*template_keyword_p=*/false,
18271 none_type,
18272 /*check_dependency_p=*/false,
18273 /*class_head_p=*/false,
18274 /*is_declaration=*/false);
18275 /* If there was no class-name, then this is not a constructor. */
18276 constructor_p = !cp_parser_error_occurred (parser);
18277 }
18278
18279 /* If we're still considering a constructor, we have to see a `(',
18280 to begin the parameter-declaration-clause, followed by either a
18281 `)', an `...', or a decl-specifier. We need to check for a
18282 type-specifier to avoid being fooled into thinking that:
18283
18284 S::S (f) (int);
18285
18286 is a constructor. (It is actually a function named `f' that
18287 takes one parameter (of type `int') and returns a value of type
18288 `S::S'. */
18289 if (constructor_p
18290 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18291 {
18292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18293 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18294 /* A parameter declaration begins with a decl-specifier,
18295 which is either the "attribute" keyword, a storage class
18296 specifier, or (usually) a type-specifier. */
18297 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18298 {
18299 tree type;
18300 tree pushed_scope = NULL_TREE;
18301 unsigned saved_num_template_parameter_lists;
18302
18303 /* Names appearing in the type-specifier should be looked up
18304 in the scope of the class. */
18305 if (current_class_type)
18306 type = NULL_TREE;
18307 else
18308 {
18309 type = TREE_TYPE (type_decl);
18310 if (TREE_CODE (type) == TYPENAME_TYPE)
18311 {
18312 type = resolve_typename_type (type,
18313 /*only_current_p=*/false);
18314 if (TREE_CODE (type) == TYPENAME_TYPE)
18315 {
18316 cp_parser_abort_tentative_parse (parser);
18317 return false;
18318 }
18319 }
18320 pushed_scope = push_scope (type);
18321 }
18322
18323 /* Inside the constructor parameter list, surrounding
18324 template-parameter-lists do not apply. */
18325 saved_num_template_parameter_lists
18326 = parser->num_template_parameter_lists;
18327 parser->num_template_parameter_lists = 0;
18328
18329 /* Look for the type-specifier. */
18330 cp_parser_type_specifier (parser,
18331 CP_PARSER_FLAGS_NONE,
18332 /*decl_specs=*/NULL,
18333 /*is_declarator=*/true,
18334 /*declares_class_or_enum=*/NULL,
18335 /*is_cv_qualifier=*/NULL);
18336
18337 parser->num_template_parameter_lists
18338 = saved_num_template_parameter_lists;
18339
18340 /* Leave the scope of the class. */
18341 if (pushed_scope)
18342 pop_scope (pushed_scope);
18343
18344 constructor_p = !cp_parser_error_occurred (parser);
18345 }
18346 }
18347 else
18348 constructor_p = false;
18349 /* We did not really want to consume any tokens. */
18350 cp_parser_abort_tentative_parse (parser);
18351
18352 return constructor_p;
18353 }
18354
18355 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18356 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18357 they must be performed once we are in the scope of the function.
18358
18359 Returns the function defined. */
18360
18361 static tree
18362 cp_parser_function_definition_from_specifiers_and_declarator
18363 (cp_parser* parser,
18364 cp_decl_specifier_seq *decl_specifiers,
18365 tree attributes,
18366 const cp_declarator *declarator)
18367 {
18368 tree fn;
18369 bool success_p;
18370
18371 /* Begin the function-definition. */
18372 success_p = start_function (decl_specifiers, declarator, attributes);
18373
18374 /* The things we're about to see are not directly qualified by any
18375 template headers we've seen thus far. */
18376 reset_specialization ();
18377
18378 /* If there were names looked up in the decl-specifier-seq that we
18379 did not check, check them now. We must wait until we are in the
18380 scope of the function to perform the checks, since the function
18381 might be a friend. */
18382 perform_deferred_access_checks ();
18383
18384 if (!success_p)
18385 {
18386 /* Skip the entire function. */
18387 cp_parser_skip_to_end_of_block_or_statement (parser);
18388 fn = error_mark_node;
18389 }
18390 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18391 {
18392 /* Seen already, skip it. An error message has already been output. */
18393 cp_parser_skip_to_end_of_block_or_statement (parser);
18394 fn = current_function_decl;
18395 current_function_decl = NULL_TREE;
18396 /* If this is a function from a class, pop the nested class. */
18397 if (current_class_name)
18398 pop_nested_class ();
18399 }
18400 else
18401 fn = cp_parser_function_definition_after_declarator (parser,
18402 /*inline_p=*/false);
18403
18404 return fn;
18405 }
18406
18407 /* Parse the part of a function-definition that follows the
18408 declarator. INLINE_P is TRUE iff this function is an inline
18409 function defined within a class-specifier.
18410
18411 Returns the function defined. */
18412
18413 static tree
18414 cp_parser_function_definition_after_declarator (cp_parser* parser,
18415 bool inline_p)
18416 {
18417 tree fn;
18418 bool ctor_initializer_p = false;
18419 bool saved_in_unbraced_linkage_specification_p;
18420 bool saved_in_function_body;
18421 unsigned saved_num_template_parameter_lists;
18422 cp_token *token;
18423
18424 saved_in_function_body = parser->in_function_body;
18425 parser->in_function_body = true;
18426 /* If the next token is `return', then the code may be trying to
18427 make use of the "named return value" extension that G++ used to
18428 support. */
18429 token = cp_lexer_peek_token (parser->lexer);
18430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18431 {
18432 /* Consume the `return' keyword. */
18433 cp_lexer_consume_token (parser->lexer);
18434 /* Look for the identifier that indicates what value is to be
18435 returned. */
18436 cp_parser_identifier (parser);
18437 /* Issue an error message. */
18438 error_at (token->location,
18439 "named return values are no longer supported");
18440 /* Skip tokens until we reach the start of the function body. */
18441 while (true)
18442 {
18443 cp_token *token = cp_lexer_peek_token (parser->lexer);
18444 if (token->type == CPP_OPEN_BRACE
18445 || token->type == CPP_EOF
18446 || token->type == CPP_PRAGMA_EOL)
18447 break;
18448 cp_lexer_consume_token (parser->lexer);
18449 }
18450 }
18451 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18452 anything declared inside `f'. */
18453 saved_in_unbraced_linkage_specification_p
18454 = parser->in_unbraced_linkage_specification_p;
18455 parser->in_unbraced_linkage_specification_p = false;
18456 /* Inside the function, surrounding template-parameter-lists do not
18457 apply. */
18458 saved_num_template_parameter_lists
18459 = parser->num_template_parameter_lists;
18460 parser->num_template_parameter_lists = 0;
18461
18462 start_lambda_scope (current_function_decl);
18463
18464 /* If the next token is `try', then we are looking at a
18465 function-try-block. */
18466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18467 ctor_initializer_p = cp_parser_function_try_block (parser);
18468 /* A function-try-block includes the function-body, so we only do
18469 this next part if we're not processing a function-try-block. */
18470 else
18471 ctor_initializer_p
18472 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18473
18474 finish_lambda_scope ();
18475
18476 /* Finish the function. */
18477 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18478 (inline_p ? 2 : 0));
18479 /* Generate code for it, if necessary. */
18480 expand_or_defer_fn (fn);
18481 /* Restore the saved values. */
18482 parser->in_unbraced_linkage_specification_p
18483 = saved_in_unbraced_linkage_specification_p;
18484 parser->num_template_parameter_lists
18485 = saved_num_template_parameter_lists;
18486 parser->in_function_body = saved_in_function_body;
18487
18488 return fn;
18489 }
18490
18491 /* Parse a template-declaration, assuming that the `export' (and
18492 `extern') keywords, if present, has already been scanned. MEMBER_P
18493 is as for cp_parser_template_declaration. */
18494
18495 static void
18496 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18497 {
18498 tree decl = NULL_TREE;
18499 VEC (deferred_access_check,gc) *checks;
18500 tree parameter_list;
18501 bool friend_p = false;
18502 bool need_lang_pop;
18503 cp_token *token;
18504
18505 /* Look for the `template' keyword. */
18506 token = cp_lexer_peek_token (parser->lexer);
18507 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18508 return;
18509
18510 /* And the `<'. */
18511 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18512 return;
18513 if (at_class_scope_p () && current_function_decl)
18514 {
18515 /* 14.5.2.2 [temp.mem]
18516
18517 A local class shall not have member templates. */
18518 error_at (token->location,
18519 "invalid declaration of member template in local class");
18520 cp_parser_skip_to_end_of_block_or_statement (parser);
18521 return;
18522 }
18523 /* [temp]
18524
18525 A template ... shall not have C linkage. */
18526 if (current_lang_name == lang_name_c)
18527 {
18528 error_at (token->location, "template with C linkage");
18529 /* Give it C++ linkage to avoid confusing other parts of the
18530 front end. */
18531 push_lang_context (lang_name_cplusplus);
18532 need_lang_pop = true;
18533 }
18534 else
18535 need_lang_pop = false;
18536
18537 /* We cannot perform access checks on the template parameter
18538 declarations until we know what is being declared, just as we
18539 cannot check the decl-specifier list. */
18540 push_deferring_access_checks (dk_deferred);
18541
18542 /* If the next token is `>', then we have an invalid
18543 specialization. Rather than complain about an invalid template
18544 parameter, issue an error message here. */
18545 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18546 {
18547 cp_parser_error (parser, "invalid explicit specialization");
18548 begin_specialization ();
18549 parameter_list = NULL_TREE;
18550 }
18551 else
18552 /* Parse the template parameters. */
18553 parameter_list = cp_parser_template_parameter_list (parser);
18554
18555 /* Get the deferred access checks from the parameter list. These
18556 will be checked once we know what is being declared, as for a
18557 member template the checks must be performed in the scope of the
18558 class containing the member. */
18559 checks = get_deferred_access_checks ();
18560
18561 /* Look for the `>'. */
18562 cp_parser_skip_to_end_of_template_parameter_list (parser);
18563 /* We just processed one more parameter list. */
18564 ++parser->num_template_parameter_lists;
18565 /* If the next token is `template', there are more template
18566 parameters. */
18567 if (cp_lexer_next_token_is_keyword (parser->lexer,
18568 RID_TEMPLATE))
18569 cp_parser_template_declaration_after_export (parser, member_p);
18570 else
18571 {
18572 /* There are no access checks when parsing a template, as we do not
18573 know if a specialization will be a friend. */
18574 push_deferring_access_checks (dk_no_check);
18575 token = cp_lexer_peek_token (parser->lexer);
18576 decl = cp_parser_single_declaration (parser,
18577 checks,
18578 member_p,
18579 /*explicit_specialization_p=*/false,
18580 &friend_p);
18581 pop_deferring_access_checks ();
18582
18583 /* If this is a member template declaration, let the front
18584 end know. */
18585 if (member_p && !friend_p && decl)
18586 {
18587 if (TREE_CODE (decl) == TYPE_DECL)
18588 cp_parser_check_access_in_redeclaration (decl, token->location);
18589
18590 decl = finish_member_template_decl (decl);
18591 }
18592 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18593 make_friend_class (current_class_type, TREE_TYPE (decl),
18594 /*complain=*/true);
18595 }
18596 /* We are done with the current parameter list. */
18597 --parser->num_template_parameter_lists;
18598
18599 pop_deferring_access_checks ();
18600
18601 /* Finish up. */
18602 finish_template_decl (parameter_list);
18603
18604 /* Register member declarations. */
18605 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18606 finish_member_declaration (decl);
18607 /* For the erroneous case of a template with C linkage, we pushed an
18608 implicit C++ linkage scope; exit that scope now. */
18609 if (need_lang_pop)
18610 pop_lang_context ();
18611 /* If DECL is a function template, we must return to parse it later.
18612 (Even though there is no definition, there might be default
18613 arguments that need handling.) */
18614 if (member_p && decl
18615 && (TREE_CODE (decl) == FUNCTION_DECL
18616 || DECL_FUNCTION_TEMPLATE_P (decl)))
18617 TREE_VALUE (parser->unparsed_functions_queues)
18618 = tree_cons (NULL_TREE, decl,
18619 TREE_VALUE (parser->unparsed_functions_queues));
18620 }
18621
18622 /* Perform the deferred access checks from a template-parameter-list.
18623 CHECKS is a TREE_LIST of access checks, as returned by
18624 get_deferred_access_checks. */
18625
18626 static void
18627 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18628 {
18629 ++processing_template_parmlist;
18630 perform_access_checks (checks);
18631 --processing_template_parmlist;
18632 }
18633
18634 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18635 `function-definition' sequence. MEMBER_P is true, this declaration
18636 appears in a class scope.
18637
18638 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18639 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18640
18641 static tree
18642 cp_parser_single_declaration (cp_parser* parser,
18643 VEC (deferred_access_check,gc)* checks,
18644 bool member_p,
18645 bool explicit_specialization_p,
18646 bool* friend_p)
18647 {
18648 int declares_class_or_enum;
18649 tree decl = NULL_TREE;
18650 cp_decl_specifier_seq decl_specifiers;
18651 bool function_definition_p = false;
18652 cp_token *decl_spec_token_start;
18653
18654 /* This function is only used when processing a template
18655 declaration. */
18656 gcc_assert (innermost_scope_kind () == sk_template_parms
18657 || innermost_scope_kind () == sk_template_spec);
18658
18659 /* Defer access checks until we know what is being declared. */
18660 push_deferring_access_checks (dk_deferred);
18661
18662 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18663 alternative. */
18664 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18665 cp_parser_decl_specifier_seq (parser,
18666 CP_PARSER_FLAGS_OPTIONAL,
18667 &decl_specifiers,
18668 &declares_class_or_enum);
18669 if (friend_p)
18670 *friend_p = cp_parser_friend_p (&decl_specifiers);
18671
18672 /* There are no template typedefs. */
18673 if (decl_specifiers.specs[(int) ds_typedef])
18674 {
18675 error_at (decl_spec_token_start->location,
18676 "template declaration of %<typedef%>");
18677 decl = error_mark_node;
18678 }
18679
18680 /* Gather up the access checks that occurred the
18681 decl-specifier-seq. */
18682 stop_deferring_access_checks ();
18683
18684 /* Check for the declaration of a template class. */
18685 if (declares_class_or_enum)
18686 {
18687 if (cp_parser_declares_only_class_p (parser))
18688 {
18689 decl = shadow_tag (&decl_specifiers);
18690
18691 /* In this case:
18692
18693 struct C {
18694 friend template <typename T> struct A<T>::B;
18695 };
18696
18697 A<T>::B will be represented by a TYPENAME_TYPE, and
18698 therefore not recognized by shadow_tag. */
18699 if (friend_p && *friend_p
18700 && !decl
18701 && decl_specifiers.type
18702 && TYPE_P (decl_specifiers.type))
18703 decl = decl_specifiers.type;
18704
18705 if (decl && decl != error_mark_node)
18706 decl = TYPE_NAME (decl);
18707 else
18708 decl = error_mark_node;
18709
18710 /* Perform access checks for template parameters. */
18711 cp_parser_perform_template_parameter_access_checks (checks);
18712 }
18713 }
18714 /* If it's not a template class, try for a template function. If
18715 the next token is a `;', then this declaration does not declare
18716 anything. But, if there were errors in the decl-specifiers, then
18717 the error might well have come from an attempted class-specifier.
18718 In that case, there's no need to warn about a missing declarator. */
18719 if (!decl
18720 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18721 || decl_specifiers.type != error_mark_node))
18722 {
18723 decl = cp_parser_init_declarator (parser,
18724 &decl_specifiers,
18725 checks,
18726 /*function_definition_allowed_p=*/true,
18727 member_p,
18728 declares_class_or_enum,
18729 &function_definition_p);
18730
18731 /* 7.1.1-1 [dcl.stc]
18732
18733 A storage-class-specifier shall not be specified in an explicit
18734 specialization... */
18735 if (decl
18736 && explicit_specialization_p
18737 && decl_specifiers.storage_class != sc_none)
18738 {
18739 error_at (decl_spec_token_start->location,
18740 "explicit template specialization cannot have a storage class");
18741 decl = error_mark_node;
18742 }
18743 }
18744
18745 pop_deferring_access_checks ();
18746
18747 /* Clear any current qualification; whatever comes next is the start
18748 of something new. */
18749 parser->scope = NULL_TREE;
18750 parser->qualifying_scope = NULL_TREE;
18751 parser->object_scope = NULL_TREE;
18752 /* Look for a trailing `;' after the declaration. */
18753 if (!function_definition_p
18754 && (decl == error_mark_node
18755 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18756 cp_parser_skip_to_end_of_block_or_statement (parser);
18757
18758 return decl;
18759 }
18760
18761 /* Parse a cast-expression that is not the operand of a unary "&". */
18762
18763 static tree
18764 cp_parser_simple_cast_expression (cp_parser *parser)
18765 {
18766 return cp_parser_cast_expression (parser, /*address_p=*/false,
18767 /*cast_p=*/false, NULL);
18768 }
18769
18770 /* Parse a functional cast to TYPE. Returns an expression
18771 representing the cast. */
18772
18773 static tree
18774 cp_parser_functional_cast (cp_parser* parser, tree type)
18775 {
18776 VEC(tree,gc) *vec;
18777 tree expression_list;
18778 tree cast;
18779 bool nonconst_p;
18780
18781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18782 {
18783 maybe_warn_cpp0x ("extended initializer lists");
18784 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18785 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18786 if (TREE_CODE (type) == TYPE_DECL)
18787 type = TREE_TYPE (type);
18788 return finish_compound_literal (type, expression_list);
18789 }
18790
18791
18792 vec = cp_parser_parenthesized_expression_list (parser, false,
18793 /*cast_p=*/true,
18794 /*allow_expansion_p=*/true,
18795 /*non_constant_p=*/NULL);
18796 if (vec == NULL)
18797 expression_list = error_mark_node;
18798 else
18799 {
18800 expression_list = build_tree_list_vec (vec);
18801 release_tree_vector (vec);
18802 }
18803
18804 cast = build_functional_cast (type, expression_list,
18805 tf_warning_or_error);
18806 /* [expr.const]/1: In an integral constant expression "only type
18807 conversions to integral or enumeration type can be used". */
18808 if (TREE_CODE (type) == TYPE_DECL)
18809 type = TREE_TYPE (type);
18810 if (cast != error_mark_node
18811 && !cast_valid_in_integral_constant_expression_p (type)
18812 && (cp_parser_non_integral_constant_expression
18813 (parser, "a call to a constructor")))
18814 return error_mark_node;
18815 return cast;
18816 }
18817
18818 /* Save the tokens that make up the body of a member function defined
18819 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18820 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18821 specifiers applied to the declaration. Returns the FUNCTION_DECL
18822 for the member function. */
18823
18824 static tree
18825 cp_parser_save_member_function_body (cp_parser* parser,
18826 cp_decl_specifier_seq *decl_specifiers,
18827 cp_declarator *declarator,
18828 tree attributes)
18829 {
18830 cp_token *first;
18831 cp_token *last;
18832 tree fn;
18833
18834 /* Create the FUNCTION_DECL. */
18835 fn = grokmethod (decl_specifiers, declarator, attributes);
18836 /* If something went badly wrong, bail out now. */
18837 if (fn == error_mark_node)
18838 {
18839 /* If there's a function-body, skip it. */
18840 if (cp_parser_token_starts_function_definition_p
18841 (cp_lexer_peek_token (parser->lexer)))
18842 cp_parser_skip_to_end_of_block_or_statement (parser);
18843 return error_mark_node;
18844 }
18845
18846 /* Remember it, if there default args to post process. */
18847 cp_parser_save_default_args (parser, fn);
18848
18849 /* Save away the tokens that make up the body of the
18850 function. */
18851 first = parser->lexer->next_token;
18852 /* We can have braced-init-list mem-initializers before the fn body. */
18853 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18854 {
18855 cp_lexer_consume_token (parser->lexer);
18856 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18857 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18858 {
18859 /* cache_group will stop after an un-nested { } pair, too. */
18860 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18861 break;
18862
18863 /* variadic mem-inits have ... after the ')'. */
18864 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18865 cp_lexer_consume_token (parser->lexer);
18866 }
18867 }
18868 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18869 /* Handle function try blocks. */
18870 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18871 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18872 last = parser->lexer->next_token;
18873
18874 /* Save away the inline definition; we will process it when the
18875 class is complete. */
18876 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18877 DECL_PENDING_INLINE_P (fn) = 1;
18878
18879 /* We need to know that this was defined in the class, so that
18880 friend templates are handled correctly. */
18881 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18882
18883 /* Add FN to the queue of functions to be parsed later. */
18884 TREE_VALUE (parser->unparsed_functions_queues)
18885 = tree_cons (NULL_TREE, fn,
18886 TREE_VALUE (parser->unparsed_functions_queues));
18887
18888 return fn;
18889 }
18890
18891 /* Parse a template-argument-list, as well as the trailing ">" (but
18892 not the opening ">"). See cp_parser_template_argument_list for the
18893 return value. */
18894
18895 static tree
18896 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18897 {
18898 tree arguments;
18899 tree saved_scope;
18900 tree saved_qualifying_scope;
18901 tree saved_object_scope;
18902 bool saved_greater_than_is_operator_p;
18903 int saved_unevaluated_operand;
18904 int saved_inhibit_evaluation_warnings;
18905
18906 /* [temp.names]
18907
18908 When parsing a template-id, the first non-nested `>' is taken as
18909 the end of the template-argument-list rather than a greater-than
18910 operator. */
18911 saved_greater_than_is_operator_p
18912 = parser->greater_than_is_operator_p;
18913 parser->greater_than_is_operator_p = false;
18914 /* Parsing the argument list may modify SCOPE, so we save it
18915 here. */
18916 saved_scope = parser->scope;
18917 saved_qualifying_scope = parser->qualifying_scope;
18918 saved_object_scope = parser->object_scope;
18919 /* We need to evaluate the template arguments, even though this
18920 template-id may be nested within a "sizeof". */
18921 saved_unevaluated_operand = cp_unevaluated_operand;
18922 cp_unevaluated_operand = 0;
18923 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18924 c_inhibit_evaluation_warnings = 0;
18925 /* Parse the template-argument-list itself. */
18926 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18927 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18928 arguments = NULL_TREE;
18929 else
18930 arguments = cp_parser_template_argument_list (parser);
18931 /* Look for the `>' that ends the template-argument-list. If we find
18932 a '>>' instead, it's probably just a typo. */
18933 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18934 {
18935 if (cxx_dialect != cxx98)
18936 {
18937 /* In C++0x, a `>>' in a template argument list or cast
18938 expression is considered to be two separate `>'
18939 tokens. So, change the current token to a `>', but don't
18940 consume it: it will be consumed later when the outer
18941 template argument list (or cast expression) is parsed.
18942 Note that this replacement of `>' for `>>' is necessary
18943 even if we are parsing tentatively: in the tentative
18944 case, after calling
18945 cp_parser_enclosed_template_argument_list we will always
18946 throw away all of the template arguments and the first
18947 closing `>', either because the template argument list
18948 was erroneous or because we are replacing those tokens
18949 with a CPP_TEMPLATE_ID token. The second `>' (which will
18950 not have been thrown away) is needed either to close an
18951 outer template argument list or to complete a new-style
18952 cast. */
18953 cp_token *token = cp_lexer_peek_token (parser->lexer);
18954 token->type = CPP_GREATER;
18955 }
18956 else if (!saved_greater_than_is_operator_p)
18957 {
18958 /* If we're in a nested template argument list, the '>>' has
18959 to be a typo for '> >'. We emit the error message, but we
18960 continue parsing and we push a '>' as next token, so that
18961 the argument list will be parsed correctly. Note that the
18962 global source location is still on the token before the
18963 '>>', so we need to say explicitly where we want it. */
18964 cp_token *token = cp_lexer_peek_token (parser->lexer);
18965 error_at (token->location, "%<>>%> should be %<> >%> "
18966 "within a nested template argument list");
18967
18968 token->type = CPP_GREATER;
18969 }
18970 else
18971 {
18972 /* If this is not a nested template argument list, the '>>'
18973 is a typo for '>'. Emit an error message and continue.
18974 Same deal about the token location, but here we can get it
18975 right by consuming the '>>' before issuing the diagnostic. */
18976 cp_token *token = cp_lexer_consume_token (parser->lexer);
18977 error_at (token->location,
18978 "spurious %<>>%>, use %<>%> to terminate "
18979 "a template argument list");
18980 }
18981 }
18982 else
18983 cp_parser_skip_to_end_of_template_parameter_list (parser);
18984 /* The `>' token might be a greater-than operator again now. */
18985 parser->greater_than_is_operator_p
18986 = saved_greater_than_is_operator_p;
18987 /* Restore the SAVED_SCOPE. */
18988 parser->scope = saved_scope;
18989 parser->qualifying_scope = saved_qualifying_scope;
18990 parser->object_scope = saved_object_scope;
18991 cp_unevaluated_operand = saved_unevaluated_operand;
18992 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18993
18994 return arguments;
18995 }
18996
18997 /* MEMBER_FUNCTION is a member function, or a friend. If default
18998 arguments, or the body of the function have not yet been parsed,
18999 parse them now. */
19000
19001 static void
19002 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19003 {
19004 /* If this member is a template, get the underlying
19005 FUNCTION_DECL. */
19006 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19007 member_function = DECL_TEMPLATE_RESULT (member_function);
19008
19009 /* There should not be any class definitions in progress at this
19010 point; the bodies of members are only parsed outside of all class
19011 definitions. */
19012 gcc_assert (parser->num_classes_being_defined == 0);
19013 /* While we're parsing the member functions we might encounter more
19014 classes. We want to handle them right away, but we don't want
19015 them getting mixed up with functions that are currently in the
19016 queue. */
19017 parser->unparsed_functions_queues
19018 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19019
19020 /* Make sure that any template parameters are in scope. */
19021 maybe_begin_member_template_processing (member_function);
19022
19023 /* If the body of the function has not yet been parsed, parse it
19024 now. */
19025 if (DECL_PENDING_INLINE_P (member_function))
19026 {
19027 tree function_scope;
19028 cp_token_cache *tokens;
19029
19030 /* The function is no longer pending; we are processing it. */
19031 tokens = DECL_PENDING_INLINE_INFO (member_function);
19032 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19033 DECL_PENDING_INLINE_P (member_function) = 0;
19034
19035 /* If this is a local class, enter the scope of the containing
19036 function. */
19037 function_scope = current_function_decl;
19038 if (function_scope)
19039 push_function_context ();
19040
19041 /* Push the body of the function onto the lexer stack. */
19042 cp_parser_push_lexer_for_tokens (parser, tokens);
19043
19044 /* Let the front end know that we going to be defining this
19045 function. */
19046 start_preparsed_function (member_function, NULL_TREE,
19047 SF_PRE_PARSED | SF_INCLASS_INLINE);
19048
19049 /* Don't do access checking if it is a templated function. */
19050 if (processing_template_decl)
19051 push_deferring_access_checks (dk_no_check);
19052
19053 /* Now, parse the body of the function. */
19054 cp_parser_function_definition_after_declarator (parser,
19055 /*inline_p=*/true);
19056
19057 if (processing_template_decl)
19058 pop_deferring_access_checks ();
19059
19060 /* Leave the scope of the containing function. */
19061 if (function_scope)
19062 pop_function_context ();
19063 cp_parser_pop_lexer (parser);
19064 }
19065
19066 /* Remove any template parameters from the symbol table. */
19067 maybe_end_member_template_processing ();
19068
19069 /* Restore the queue. */
19070 parser->unparsed_functions_queues
19071 = TREE_CHAIN (parser->unparsed_functions_queues);
19072 }
19073
19074 /* If DECL contains any default args, remember it on the unparsed
19075 functions queue. */
19076
19077 static void
19078 cp_parser_save_default_args (cp_parser* parser, tree decl)
19079 {
19080 tree probe;
19081
19082 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19083 probe;
19084 probe = TREE_CHAIN (probe))
19085 if (TREE_PURPOSE (probe))
19086 {
19087 TREE_PURPOSE (parser->unparsed_functions_queues)
19088 = tree_cons (current_class_type, decl,
19089 TREE_PURPOSE (parser->unparsed_functions_queues));
19090 break;
19091 }
19092 }
19093
19094 /* FN is a FUNCTION_DECL which may contains a parameter with an
19095 unparsed DEFAULT_ARG. Parse the default args now. This function
19096 assumes that the current scope is the scope in which the default
19097 argument should be processed. */
19098
19099 static void
19100 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19101 {
19102 bool saved_local_variables_forbidden_p;
19103 tree parm, parmdecl;
19104
19105 /* While we're parsing the default args, we might (due to the
19106 statement expression extension) encounter more classes. We want
19107 to handle them right away, but we don't want them getting mixed
19108 up with default args that are currently in the queue. */
19109 parser->unparsed_functions_queues
19110 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19111
19112 /* Local variable names (and the `this' keyword) may not appear
19113 in a default argument. */
19114 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19115 parser->local_variables_forbidden_p = true;
19116
19117 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19118 parmdecl = DECL_ARGUMENTS (fn);
19119 parm && parm != void_list_node;
19120 parm = TREE_CHAIN (parm),
19121 parmdecl = TREE_CHAIN (parmdecl))
19122 {
19123 cp_token_cache *tokens;
19124 tree default_arg = TREE_PURPOSE (parm);
19125 tree parsed_arg;
19126 VEC(tree,gc) *insts;
19127 tree copy;
19128 unsigned ix;
19129
19130 if (!default_arg)
19131 continue;
19132
19133 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19134 /* This can happen for a friend declaration for a function
19135 already declared with default arguments. */
19136 continue;
19137
19138 /* Push the saved tokens for the default argument onto the parser's
19139 lexer stack. */
19140 tokens = DEFARG_TOKENS (default_arg);
19141 cp_parser_push_lexer_for_tokens (parser, tokens);
19142
19143 start_lambda_scope (parmdecl);
19144
19145 /* Parse the assignment-expression. */
19146 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19147 if (parsed_arg == error_mark_node)
19148 {
19149 cp_parser_pop_lexer (parser);
19150 continue;
19151 }
19152
19153 if (!processing_template_decl)
19154 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19155
19156 TREE_PURPOSE (parm) = parsed_arg;
19157
19158 /* Update any instantiations we've already created. */
19159 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19160 VEC_iterate (tree, insts, ix, copy); ix++)
19161 TREE_PURPOSE (copy) = parsed_arg;
19162
19163 finish_lambda_scope ();
19164
19165 /* If the token stream has not been completely used up, then
19166 there was extra junk after the end of the default
19167 argument. */
19168 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19169 cp_parser_error (parser, "expected %<,%>");
19170
19171 /* Revert to the main lexer. */
19172 cp_parser_pop_lexer (parser);
19173 }
19174
19175 /* Make sure no default arg is missing. */
19176 check_default_args (fn);
19177
19178 /* Restore the state of local_variables_forbidden_p. */
19179 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19180
19181 /* Restore the queue. */
19182 parser->unparsed_functions_queues
19183 = TREE_CHAIN (parser->unparsed_functions_queues);
19184 }
19185
19186 /* Parse the operand of `sizeof' (or a similar operator). Returns
19187 either a TYPE or an expression, depending on the form of the
19188 input. The KEYWORD indicates which kind of expression we have
19189 encountered. */
19190
19191 static tree
19192 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19193 {
19194 tree expr = NULL_TREE;
19195 const char *saved_message;
19196 char *tmp;
19197 bool saved_integral_constant_expression_p;
19198 bool saved_non_integral_constant_expression_p;
19199 bool pack_expansion_p = false;
19200
19201 /* Types cannot be defined in a `sizeof' expression. Save away the
19202 old message. */
19203 saved_message = parser->type_definition_forbidden_message;
19204 /* And create the new one. */
19205 tmp = concat ("types may not be defined in %<",
19206 IDENTIFIER_POINTER (ridpointers[keyword]),
19207 "%> expressions", NULL);
19208 parser->type_definition_forbidden_message = tmp;
19209
19210 /* The restrictions on constant-expressions do not apply inside
19211 sizeof expressions. */
19212 saved_integral_constant_expression_p
19213 = parser->integral_constant_expression_p;
19214 saved_non_integral_constant_expression_p
19215 = parser->non_integral_constant_expression_p;
19216 parser->integral_constant_expression_p = false;
19217
19218 /* If it's a `...', then we are computing the length of a parameter
19219 pack. */
19220 if (keyword == RID_SIZEOF
19221 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19222 {
19223 /* Consume the `...'. */
19224 cp_lexer_consume_token (parser->lexer);
19225 maybe_warn_variadic_templates ();
19226
19227 /* Note that this is an expansion. */
19228 pack_expansion_p = true;
19229 }
19230
19231 /* Do not actually evaluate the expression. */
19232 ++cp_unevaluated_operand;
19233 ++c_inhibit_evaluation_warnings;
19234 /* If it's a `(', then we might be looking at the type-id
19235 construction. */
19236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19237 {
19238 tree type;
19239 bool saved_in_type_id_in_expr_p;
19240
19241 /* We can't be sure yet whether we're looking at a type-id or an
19242 expression. */
19243 cp_parser_parse_tentatively (parser);
19244 /* Consume the `('. */
19245 cp_lexer_consume_token (parser->lexer);
19246 /* Parse the type-id. */
19247 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19248 parser->in_type_id_in_expr_p = true;
19249 type = cp_parser_type_id (parser);
19250 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19251 /* Now, look for the trailing `)'. */
19252 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19253 /* If all went well, then we're done. */
19254 if (cp_parser_parse_definitely (parser))
19255 {
19256 cp_decl_specifier_seq decl_specs;
19257
19258 /* Build a trivial decl-specifier-seq. */
19259 clear_decl_specs (&decl_specs);
19260 decl_specs.type = type;
19261
19262 /* Call grokdeclarator to figure out what type this is. */
19263 expr = grokdeclarator (NULL,
19264 &decl_specs,
19265 TYPENAME,
19266 /*initialized=*/0,
19267 /*attrlist=*/NULL);
19268 }
19269 }
19270
19271 /* If the type-id production did not work out, then we must be
19272 looking at the unary-expression production. */
19273 if (!expr)
19274 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19275 /*cast_p=*/false, NULL);
19276
19277 if (pack_expansion_p)
19278 /* Build a pack expansion. */
19279 expr = make_pack_expansion (expr);
19280
19281 /* Go back to evaluating expressions. */
19282 --cp_unevaluated_operand;
19283 --c_inhibit_evaluation_warnings;
19284
19285 /* Free the message we created. */
19286 free (tmp);
19287 /* And restore the old one. */
19288 parser->type_definition_forbidden_message = saved_message;
19289 parser->integral_constant_expression_p
19290 = saved_integral_constant_expression_p;
19291 parser->non_integral_constant_expression_p
19292 = saved_non_integral_constant_expression_p;
19293
19294 return expr;
19295 }
19296
19297 /* If the current declaration has no declarator, return true. */
19298
19299 static bool
19300 cp_parser_declares_only_class_p (cp_parser *parser)
19301 {
19302 /* If the next token is a `;' or a `,' then there is no
19303 declarator. */
19304 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19305 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19306 }
19307
19308 /* Update the DECL_SPECS to reflect the storage class indicated by
19309 KEYWORD. */
19310
19311 static void
19312 cp_parser_set_storage_class (cp_parser *parser,
19313 cp_decl_specifier_seq *decl_specs,
19314 enum rid keyword,
19315 location_t location)
19316 {
19317 cp_storage_class storage_class;
19318
19319 if (parser->in_unbraced_linkage_specification_p)
19320 {
19321 error_at (location, "invalid use of %qD in linkage specification",
19322 ridpointers[keyword]);
19323 return;
19324 }
19325 else if (decl_specs->storage_class != sc_none)
19326 {
19327 decl_specs->conflicting_specifiers_p = true;
19328 return;
19329 }
19330
19331 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19332 && decl_specs->specs[(int) ds_thread])
19333 {
19334 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19335 decl_specs->specs[(int) ds_thread] = 0;
19336 }
19337
19338 switch (keyword)
19339 {
19340 case RID_AUTO:
19341 storage_class = sc_auto;
19342 break;
19343 case RID_REGISTER:
19344 storage_class = sc_register;
19345 break;
19346 case RID_STATIC:
19347 storage_class = sc_static;
19348 break;
19349 case RID_EXTERN:
19350 storage_class = sc_extern;
19351 break;
19352 case RID_MUTABLE:
19353 storage_class = sc_mutable;
19354 break;
19355 default:
19356 gcc_unreachable ();
19357 }
19358 decl_specs->storage_class = storage_class;
19359
19360 /* A storage class specifier cannot be applied alongside a typedef
19361 specifier. If there is a typedef specifier present then set
19362 conflicting_specifiers_p which will trigger an error later
19363 on in grokdeclarator. */
19364 if (decl_specs->specs[(int)ds_typedef])
19365 decl_specs->conflicting_specifiers_p = true;
19366 }
19367
19368 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19369 is true, the type is a user-defined type; otherwise it is a
19370 built-in type specified by a keyword. */
19371
19372 static void
19373 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19374 tree type_spec,
19375 location_t location,
19376 bool user_defined_p)
19377 {
19378 decl_specs->any_specifiers_p = true;
19379
19380 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19381 (with, for example, in "typedef int wchar_t;") we remember that
19382 this is what happened. In system headers, we ignore these
19383 declarations so that G++ can work with system headers that are not
19384 C++-safe. */
19385 if (decl_specs->specs[(int) ds_typedef]
19386 && !user_defined_p
19387 && (type_spec == boolean_type_node
19388 || type_spec == char16_type_node
19389 || type_spec == char32_type_node
19390 || type_spec == wchar_type_node)
19391 && (decl_specs->type
19392 || decl_specs->specs[(int) ds_long]
19393 || decl_specs->specs[(int) ds_short]
19394 || decl_specs->specs[(int) ds_unsigned]
19395 || decl_specs->specs[(int) ds_signed]))
19396 {
19397 decl_specs->redefined_builtin_type = type_spec;
19398 if (!decl_specs->type)
19399 {
19400 decl_specs->type = type_spec;
19401 decl_specs->user_defined_type_p = false;
19402 decl_specs->type_location = location;
19403 }
19404 }
19405 else if (decl_specs->type)
19406 decl_specs->multiple_types_p = true;
19407 else
19408 {
19409 decl_specs->type = type_spec;
19410 decl_specs->user_defined_type_p = user_defined_p;
19411 decl_specs->redefined_builtin_type = NULL_TREE;
19412 decl_specs->type_location = location;
19413 }
19414 }
19415
19416 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19417 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19418
19419 static bool
19420 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19421 {
19422 return decl_specifiers->specs[(int) ds_friend] != 0;
19423 }
19424
19425 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19426 issue an error message indicating that TOKEN_DESC was expected.
19427
19428 Returns the token consumed, if the token had the appropriate type.
19429 Otherwise, returns NULL. */
19430
19431 static cp_token *
19432 cp_parser_require (cp_parser* parser,
19433 enum cpp_ttype type,
19434 const char* token_desc)
19435 {
19436 if (cp_lexer_next_token_is (parser->lexer, type))
19437 return cp_lexer_consume_token (parser->lexer);
19438 else
19439 {
19440 /* Output the MESSAGE -- unless we're parsing tentatively. */
19441 if (!cp_parser_simulate_error (parser))
19442 {
19443 char *message = concat ("expected ", token_desc, NULL);
19444 cp_parser_error (parser, message);
19445 free (message);
19446 }
19447 return NULL;
19448 }
19449 }
19450
19451 /* An error message is produced if the next token is not '>'.
19452 All further tokens are skipped until the desired token is
19453 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19454
19455 static void
19456 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19457 {
19458 /* Current level of '< ... >'. */
19459 unsigned level = 0;
19460 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19461 unsigned nesting_depth = 0;
19462
19463 /* Are we ready, yet? If not, issue error message. */
19464 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19465 return;
19466
19467 /* Skip tokens until the desired token is found. */
19468 while (true)
19469 {
19470 /* Peek at the next token. */
19471 switch (cp_lexer_peek_token (parser->lexer)->type)
19472 {
19473 case CPP_LESS:
19474 if (!nesting_depth)
19475 ++level;
19476 break;
19477
19478 case CPP_RSHIFT:
19479 if (cxx_dialect == cxx98)
19480 /* C++0x views the `>>' operator as two `>' tokens, but
19481 C++98 does not. */
19482 break;
19483 else if (!nesting_depth && level-- == 0)
19484 {
19485 /* We've hit a `>>' where the first `>' closes the
19486 template argument list, and the second `>' is
19487 spurious. Just consume the `>>' and stop; we've
19488 already produced at least one error. */
19489 cp_lexer_consume_token (parser->lexer);
19490 return;
19491 }
19492 /* Fall through for C++0x, so we handle the second `>' in
19493 the `>>'. */
19494
19495 case CPP_GREATER:
19496 if (!nesting_depth && level-- == 0)
19497 {
19498 /* We've reached the token we want, consume it and stop. */
19499 cp_lexer_consume_token (parser->lexer);
19500 return;
19501 }
19502 break;
19503
19504 case CPP_OPEN_PAREN:
19505 case CPP_OPEN_SQUARE:
19506 ++nesting_depth;
19507 break;
19508
19509 case CPP_CLOSE_PAREN:
19510 case CPP_CLOSE_SQUARE:
19511 if (nesting_depth-- == 0)
19512 return;
19513 break;
19514
19515 case CPP_EOF:
19516 case CPP_PRAGMA_EOL:
19517 case CPP_SEMICOLON:
19518 case CPP_OPEN_BRACE:
19519 case CPP_CLOSE_BRACE:
19520 /* The '>' was probably forgotten, don't look further. */
19521 return;
19522
19523 default:
19524 break;
19525 }
19526
19527 /* Consume this token. */
19528 cp_lexer_consume_token (parser->lexer);
19529 }
19530 }
19531
19532 /* If the next token is the indicated keyword, consume it. Otherwise,
19533 issue an error message indicating that TOKEN_DESC was expected.
19534
19535 Returns the token consumed, if the token had the appropriate type.
19536 Otherwise, returns NULL. */
19537
19538 static cp_token *
19539 cp_parser_require_keyword (cp_parser* parser,
19540 enum rid keyword,
19541 const char* token_desc)
19542 {
19543 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19544
19545 if (token && token->keyword != keyword)
19546 {
19547 dyn_string_t error_msg;
19548
19549 /* Format the error message. */
19550 error_msg = dyn_string_new (0);
19551 dyn_string_append_cstr (error_msg, "expected ");
19552 dyn_string_append_cstr (error_msg, token_desc);
19553 cp_parser_error (parser, error_msg->s);
19554 dyn_string_delete (error_msg);
19555 return NULL;
19556 }
19557
19558 return token;
19559 }
19560
19561 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19562 function-definition. */
19563
19564 static bool
19565 cp_parser_token_starts_function_definition_p (cp_token* token)
19566 {
19567 return (/* An ordinary function-body begins with an `{'. */
19568 token->type == CPP_OPEN_BRACE
19569 /* A ctor-initializer begins with a `:'. */
19570 || token->type == CPP_COLON
19571 /* A function-try-block begins with `try'. */
19572 || token->keyword == RID_TRY
19573 /* The named return value extension begins with `return'. */
19574 || token->keyword == RID_RETURN);
19575 }
19576
19577 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19578 definition. */
19579
19580 static bool
19581 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19582 {
19583 cp_token *token;
19584
19585 token = cp_lexer_peek_token (parser->lexer);
19586 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19587 }
19588
19589 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19590 C++0x) ending a template-argument. */
19591
19592 static bool
19593 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19594 {
19595 cp_token *token;
19596
19597 token = cp_lexer_peek_token (parser->lexer);
19598 return (token->type == CPP_COMMA
19599 || token->type == CPP_GREATER
19600 || token->type == CPP_ELLIPSIS
19601 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19602 }
19603
19604 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19605 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19606
19607 static bool
19608 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19609 size_t n)
19610 {
19611 cp_token *token;
19612
19613 token = cp_lexer_peek_nth_token (parser->lexer, n);
19614 if (token->type == CPP_LESS)
19615 return true;
19616 /* Check for the sequence `<::' in the original code. It would be lexed as
19617 `[:', where `[' is a digraph, and there is no whitespace before
19618 `:'. */
19619 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19620 {
19621 cp_token *token2;
19622 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19623 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19624 return true;
19625 }
19626 return false;
19627 }
19628
19629 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19630 or none_type otherwise. */
19631
19632 static enum tag_types
19633 cp_parser_token_is_class_key (cp_token* token)
19634 {
19635 switch (token->keyword)
19636 {
19637 case RID_CLASS:
19638 return class_type;
19639 case RID_STRUCT:
19640 return record_type;
19641 case RID_UNION:
19642 return union_type;
19643
19644 default:
19645 return none_type;
19646 }
19647 }
19648
19649 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19650
19651 static void
19652 cp_parser_check_class_key (enum tag_types class_key, tree type)
19653 {
19654 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19655 permerror (input_location, "%qs tag used in naming %q#T",
19656 class_key == union_type ? "union"
19657 : class_key == record_type ? "struct" : "class",
19658 type);
19659 }
19660
19661 /* Issue an error message if DECL is redeclared with different
19662 access than its original declaration [class.access.spec/3].
19663 This applies to nested classes and nested class templates.
19664 [class.mem/1]. */
19665
19666 static void
19667 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19668 {
19669 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19670 return;
19671
19672 if ((TREE_PRIVATE (decl)
19673 != (current_access_specifier == access_private_node))
19674 || (TREE_PROTECTED (decl)
19675 != (current_access_specifier == access_protected_node)))
19676 error_at (location, "%qD redeclared with different access", decl);
19677 }
19678
19679 /* Look for the `template' keyword, as a syntactic disambiguator.
19680 Return TRUE iff it is present, in which case it will be
19681 consumed. */
19682
19683 static bool
19684 cp_parser_optional_template_keyword (cp_parser *parser)
19685 {
19686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19687 {
19688 /* The `template' keyword can only be used within templates;
19689 outside templates the parser can always figure out what is a
19690 template and what is not. */
19691 if (!processing_template_decl)
19692 {
19693 cp_token *token = cp_lexer_peek_token (parser->lexer);
19694 error_at (token->location,
19695 "%<template%> (as a disambiguator) is only allowed "
19696 "within templates");
19697 /* If this part of the token stream is rescanned, the same
19698 error message would be generated. So, we purge the token
19699 from the stream. */
19700 cp_lexer_purge_token (parser->lexer);
19701 return false;
19702 }
19703 else
19704 {
19705 /* Consume the `template' keyword. */
19706 cp_lexer_consume_token (parser->lexer);
19707 return true;
19708 }
19709 }
19710
19711 return false;
19712 }
19713
19714 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19715 set PARSER->SCOPE, and perform other related actions. */
19716
19717 static void
19718 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19719 {
19720 int i;
19721 struct tree_check *check_value;
19722 deferred_access_check *chk;
19723 VEC (deferred_access_check,gc) *checks;
19724
19725 /* Get the stored value. */
19726 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19727 /* Perform any access checks that were deferred. */
19728 checks = check_value->checks;
19729 if (checks)
19730 {
19731 for (i = 0 ;
19732 VEC_iterate (deferred_access_check, checks, i, chk) ;
19733 ++i)
19734 {
19735 perform_or_defer_access_check (chk->binfo,
19736 chk->decl,
19737 chk->diag_decl);
19738 }
19739 }
19740 /* Set the scope from the stored value. */
19741 parser->scope = check_value->value;
19742 parser->qualifying_scope = check_value->qualifying_scope;
19743 parser->object_scope = NULL_TREE;
19744 }
19745
19746 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19747 encounter the end of a block before what we were looking for. */
19748
19749 static bool
19750 cp_parser_cache_group (cp_parser *parser,
19751 enum cpp_ttype end,
19752 unsigned depth)
19753 {
19754 while (true)
19755 {
19756 cp_token *token = cp_lexer_peek_token (parser->lexer);
19757
19758 /* Abort a parenthesized expression if we encounter a semicolon. */
19759 if ((end == CPP_CLOSE_PAREN || depth == 0)
19760 && token->type == CPP_SEMICOLON)
19761 return true;
19762 /* If we've reached the end of the file, stop. */
19763 if (token->type == CPP_EOF
19764 || (end != CPP_PRAGMA_EOL
19765 && token->type == CPP_PRAGMA_EOL))
19766 return true;
19767 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19768 /* We've hit the end of an enclosing block, so there's been some
19769 kind of syntax error. */
19770 return true;
19771
19772 /* Consume the token. */
19773 cp_lexer_consume_token (parser->lexer);
19774 /* See if it starts a new group. */
19775 if (token->type == CPP_OPEN_BRACE)
19776 {
19777 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19778 /* In theory this should probably check end == '}', but
19779 cp_parser_save_member_function_body needs it to exit
19780 after either '}' or ')' when called with ')'. */
19781 if (depth == 0)
19782 return false;
19783 }
19784 else if (token->type == CPP_OPEN_PAREN)
19785 {
19786 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19787 if (depth == 0 && end == CPP_CLOSE_PAREN)
19788 return false;
19789 }
19790 else if (token->type == CPP_PRAGMA)
19791 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19792 else if (token->type == end)
19793 return false;
19794 }
19795 }
19796
19797 /* Begin parsing tentatively. We always save tokens while parsing
19798 tentatively so that if the tentative parsing fails we can restore the
19799 tokens. */
19800
19801 static void
19802 cp_parser_parse_tentatively (cp_parser* parser)
19803 {
19804 /* Enter a new parsing context. */
19805 parser->context = cp_parser_context_new (parser->context);
19806 /* Begin saving tokens. */
19807 cp_lexer_save_tokens (parser->lexer);
19808 /* In order to avoid repetitive access control error messages,
19809 access checks are queued up until we are no longer parsing
19810 tentatively. */
19811 push_deferring_access_checks (dk_deferred);
19812 }
19813
19814 /* Commit to the currently active tentative parse. */
19815
19816 static void
19817 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19818 {
19819 cp_parser_context *context;
19820 cp_lexer *lexer;
19821
19822 /* Mark all of the levels as committed. */
19823 lexer = parser->lexer;
19824 for (context = parser->context; context->next; context = context->next)
19825 {
19826 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19827 break;
19828 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19829 while (!cp_lexer_saving_tokens (lexer))
19830 lexer = lexer->next;
19831 cp_lexer_commit_tokens (lexer);
19832 }
19833 }
19834
19835 /* Abort the currently active tentative parse. All consumed tokens
19836 will be rolled back, and no diagnostics will be issued. */
19837
19838 static void
19839 cp_parser_abort_tentative_parse (cp_parser* parser)
19840 {
19841 cp_parser_simulate_error (parser);
19842 /* Now, pretend that we want to see if the construct was
19843 successfully parsed. */
19844 cp_parser_parse_definitely (parser);
19845 }
19846
19847 /* Stop parsing tentatively. If a parse error has occurred, restore the
19848 token stream. Otherwise, commit to the tokens we have consumed.
19849 Returns true if no error occurred; false otherwise. */
19850
19851 static bool
19852 cp_parser_parse_definitely (cp_parser* parser)
19853 {
19854 bool error_occurred;
19855 cp_parser_context *context;
19856
19857 /* Remember whether or not an error occurred, since we are about to
19858 destroy that information. */
19859 error_occurred = cp_parser_error_occurred (parser);
19860 /* Remove the topmost context from the stack. */
19861 context = parser->context;
19862 parser->context = context->next;
19863 /* If no parse errors occurred, commit to the tentative parse. */
19864 if (!error_occurred)
19865 {
19866 /* Commit to the tokens read tentatively, unless that was
19867 already done. */
19868 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19869 cp_lexer_commit_tokens (parser->lexer);
19870
19871 pop_to_parent_deferring_access_checks ();
19872 }
19873 /* Otherwise, if errors occurred, roll back our state so that things
19874 are just as they were before we began the tentative parse. */
19875 else
19876 {
19877 cp_lexer_rollback_tokens (parser->lexer);
19878 pop_deferring_access_checks ();
19879 }
19880 /* Add the context to the front of the free list. */
19881 context->next = cp_parser_context_free_list;
19882 cp_parser_context_free_list = context;
19883
19884 return !error_occurred;
19885 }
19886
19887 /* Returns true if we are parsing tentatively and are not committed to
19888 this tentative parse. */
19889
19890 static bool
19891 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19892 {
19893 return (cp_parser_parsing_tentatively (parser)
19894 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19895 }
19896
19897 /* Returns nonzero iff an error has occurred during the most recent
19898 tentative parse. */
19899
19900 static bool
19901 cp_parser_error_occurred (cp_parser* parser)
19902 {
19903 return (cp_parser_parsing_tentatively (parser)
19904 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19905 }
19906
19907 /* Returns nonzero if GNU extensions are allowed. */
19908
19909 static bool
19910 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19911 {
19912 return parser->allow_gnu_extensions_p;
19913 }
19914 \f
19915 /* Objective-C++ Productions */
19916
19917
19918 /* Parse an Objective-C expression, which feeds into a primary-expression
19919 above.
19920
19921 objc-expression:
19922 objc-message-expression
19923 objc-string-literal
19924 objc-encode-expression
19925 objc-protocol-expression
19926 objc-selector-expression
19927
19928 Returns a tree representation of the expression. */
19929
19930 static tree
19931 cp_parser_objc_expression (cp_parser* parser)
19932 {
19933 /* Try to figure out what kind of declaration is present. */
19934 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19935
19936 switch (kwd->type)
19937 {
19938 case CPP_OPEN_SQUARE:
19939 return cp_parser_objc_message_expression (parser);
19940
19941 case CPP_OBJC_STRING:
19942 kwd = cp_lexer_consume_token (parser->lexer);
19943 return objc_build_string_object (kwd->u.value);
19944
19945 case CPP_KEYWORD:
19946 switch (kwd->keyword)
19947 {
19948 case RID_AT_ENCODE:
19949 return cp_parser_objc_encode_expression (parser);
19950
19951 case RID_AT_PROTOCOL:
19952 return cp_parser_objc_protocol_expression (parser);
19953
19954 case RID_AT_SELECTOR:
19955 return cp_parser_objc_selector_expression (parser);
19956
19957 default:
19958 break;
19959 }
19960 default:
19961 error_at (kwd->location,
19962 "misplaced %<@%D%> Objective-C++ construct",
19963 kwd->u.value);
19964 cp_parser_skip_to_end_of_block_or_statement (parser);
19965 }
19966
19967 return error_mark_node;
19968 }
19969
19970 /* Parse an Objective-C message expression.
19971
19972 objc-message-expression:
19973 [ objc-message-receiver objc-message-args ]
19974
19975 Returns a representation of an Objective-C message. */
19976
19977 static tree
19978 cp_parser_objc_message_expression (cp_parser* parser)
19979 {
19980 tree receiver, messageargs;
19981
19982 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19983 receiver = cp_parser_objc_message_receiver (parser);
19984 messageargs = cp_parser_objc_message_args (parser);
19985 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19986
19987 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19988 }
19989
19990 /* Parse an objc-message-receiver.
19991
19992 objc-message-receiver:
19993 expression
19994 simple-type-specifier
19995
19996 Returns a representation of the type or expression. */
19997
19998 static tree
19999 cp_parser_objc_message_receiver (cp_parser* parser)
20000 {
20001 tree rcv;
20002
20003 /* An Objective-C message receiver may be either (1) a type
20004 or (2) an expression. */
20005 cp_parser_parse_tentatively (parser);
20006 rcv = cp_parser_expression (parser, false, NULL);
20007
20008 if (cp_parser_parse_definitely (parser))
20009 return rcv;
20010
20011 rcv = cp_parser_simple_type_specifier (parser,
20012 /*decl_specs=*/NULL,
20013 CP_PARSER_FLAGS_NONE);
20014
20015 return objc_get_class_reference (rcv);
20016 }
20017
20018 /* Parse the arguments and selectors comprising an Objective-C message.
20019
20020 objc-message-args:
20021 objc-selector
20022 objc-selector-args
20023 objc-selector-args , objc-comma-args
20024
20025 objc-selector-args:
20026 objc-selector [opt] : assignment-expression
20027 objc-selector-args objc-selector [opt] : assignment-expression
20028
20029 objc-comma-args:
20030 assignment-expression
20031 objc-comma-args , assignment-expression
20032
20033 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20034 selector arguments and TREE_VALUE containing a list of comma
20035 arguments. */
20036
20037 static tree
20038 cp_parser_objc_message_args (cp_parser* parser)
20039 {
20040 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20041 bool maybe_unary_selector_p = true;
20042 cp_token *token = cp_lexer_peek_token (parser->lexer);
20043
20044 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20045 {
20046 tree selector = NULL_TREE, arg;
20047
20048 if (token->type != CPP_COLON)
20049 selector = cp_parser_objc_selector (parser);
20050
20051 /* Detect if we have a unary selector. */
20052 if (maybe_unary_selector_p
20053 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20054 return build_tree_list (selector, NULL_TREE);
20055
20056 maybe_unary_selector_p = false;
20057 cp_parser_require (parser, CPP_COLON, "%<:%>");
20058 arg = cp_parser_assignment_expression (parser, false, NULL);
20059
20060 sel_args
20061 = chainon (sel_args,
20062 build_tree_list (selector, arg));
20063
20064 token = cp_lexer_peek_token (parser->lexer);
20065 }
20066
20067 /* Handle non-selector arguments, if any. */
20068 while (token->type == CPP_COMMA)
20069 {
20070 tree arg;
20071
20072 cp_lexer_consume_token (parser->lexer);
20073 arg = cp_parser_assignment_expression (parser, false, NULL);
20074
20075 addl_args
20076 = chainon (addl_args,
20077 build_tree_list (NULL_TREE, arg));
20078
20079 token = cp_lexer_peek_token (parser->lexer);
20080 }
20081
20082 return build_tree_list (sel_args, addl_args);
20083 }
20084
20085 /* Parse an Objective-C encode expression.
20086
20087 objc-encode-expression:
20088 @encode objc-typename
20089
20090 Returns an encoded representation of the type argument. */
20091
20092 static tree
20093 cp_parser_objc_encode_expression (cp_parser* parser)
20094 {
20095 tree type;
20096 cp_token *token;
20097
20098 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20099 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20100 token = cp_lexer_peek_token (parser->lexer);
20101 type = complete_type (cp_parser_type_id (parser));
20102 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20103
20104 if (!type)
20105 {
20106 error_at (token->location,
20107 "%<@encode%> must specify a type as an argument");
20108 return error_mark_node;
20109 }
20110
20111 return objc_build_encode_expr (type);
20112 }
20113
20114 /* Parse an Objective-C @defs expression. */
20115
20116 static tree
20117 cp_parser_objc_defs_expression (cp_parser *parser)
20118 {
20119 tree name;
20120
20121 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20122 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20123 name = cp_parser_identifier (parser);
20124 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20125
20126 return objc_get_class_ivars (name);
20127 }
20128
20129 /* Parse an Objective-C protocol expression.
20130
20131 objc-protocol-expression:
20132 @protocol ( identifier )
20133
20134 Returns a representation of the protocol expression. */
20135
20136 static tree
20137 cp_parser_objc_protocol_expression (cp_parser* parser)
20138 {
20139 tree proto;
20140
20141 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20142 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20143 proto = cp_parser_identifier (parser);
20144 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20145
20146 return objc_build_protocol_expr (proto);
20147 }
20148
20149 /* Parse an Objective-C selector expression.
20150
20151 objc-selector-expression:
20152 @selector ( objc-method-signature )
20153
20154 objc-method-signature:
20155 objc-selector
20156 objc-selector-seq
20157
20158 objc-selector-seq:
20159 objc-selector :
20160 objc-selector-seq objc-selector :
20161
20162 Returns a representation of the method selector. */
20163
20164 static tree
20165 cp_parser_objc_selector_expression (cp_parser* parser)
20166 {
20167 tree sel_seq = NULL_TREE;
20168 bool maybe_unary_selector_p = true;
20169 cp_token *token;
20170 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20171
20172 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20173 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20174 token = cp_lexer_peek_token (parser->lexer);
20175
20176 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20177 || token->type == CPP_SCOPE)
20178 {
20179 tree selector = NULL_TREE;
20180
20181 if (token->type != CPP_COLON
20182 || token->type == CPP_SCOPE)
20183 selector = cp_parser_objc_selector (parser);
20184
20185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20186 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20187 {
20188 /* Detect if we have a unary selector. */
20189 if (maybe_unary_selector_p)
20190 {
20191 sel_seq = selector;
20192 goto finish_selector;
20193 }
20194 else
20195 {
20196 cp_parser_error (parser, "expected %<:%>");
20197 }
20198 }
20199 maybe_unary_selector_p = false;
20200 token = cp_lexer_consume_token (parser->lexer);
20201
20202 if (token->type == CPP_SCOPE)
20203 {
20204 sel_seq
20205 = chainon (sel_seq,
20206 build_tree_list (selector, NULL_TREE));
20207 sel_seq
20208 = chainon (sel_seq,
20209 build_tree_list (NULL_TREE, NULL_TREE));
20210 }
20211 else
20212 sel_seq
20213 = chainon (sel_seq,
20214 build_tree_list (selector, NULL_TREE));
20215
20216 token = cp_lexer_peek_token (parser->lexer);
20217 }
20218
20219 finish_selector:
20220 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20221
20222 return objc_build_selector_expr (loc, sel_seq);
20223 }
20224
20225 /* Parse a list of identifiers.
20226
20227 objc-identifier-list:
20228 identifier
20229 objc-identifier-list , identifier
20230
20231 Returns a TREE_LIST of identifier nodes. */
20232
20233 static tree
20234 cp_parser_objc_identifier_list (cp_parser* parser)
20235 {
20236 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20237 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20238
20239 while (sep->type == CPP_COMMA)
20240 {
20241 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20242 list = chainon (list,
20243 build_tree_list (NULL_TREE,
20244 cp_parser_identifier (parser)));
20245 sep = cp_lexer_peek_token (parser->lexer);
20246 }
20247
20248 return list;
20249 }
20250
20251 /* Parse an Objective-C alias declaration.
20252
20253 objc-alias-declaration:
20254 @compatibility_alias identifier identifier ;
20255
20256 This function registers the alias mapping with the Objective-C front end.
20257 It returns nothing. */
20258
20259 static void
20260 cp_parser_objc_alias_declaration (cp_parser* parser)
20261 {
20262 tree alias, orig;
20263
20264 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20265 alias = cp_parser_identifier (parser);
20266 orig = cp_parser_identifier (parser);
20267 objc_declare_alias (alias, orig);
20268 cp_parser_consume_semicolon_at_end_of_statement (parser);
20269 }
20270
20271 /* Parse an Objective-C class forward-declaration.
20272
20273 objc-class-declaration:
20274 @class objc-identifier-list ;
20275
20276 The function registers the forward declarations with the Objective-C
20277 front end. It returns nothing. */
20278
20279 static void
20280 cp_parser_objc_class_declaration (cp_parser* parser)
20281 {
20282 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20283 objc_declare_class (cp_parser_objc_identifier_list (parser));
20284 cp_parser_consume_semicolon_at_end_of_statement (parser);
20285 }
20286
20287 /* Parse a list of Objective-C protocol references.
20288
20289 objc-protocol-refs-opt:
20290 objc-protocol-refs [opt]
20291
20292 objc-protocol-refs:
20293 < objc-identifier-list >
20294
20295 Returns a TREE_LIST of identifiers, if any. */
20296
20297 static tree
20298 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20299 {
20300 tree protorefs = NULL_TREE;
20301
20302 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20303 {
20304 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20305 protorefs = cp_parser_objc_identifier_list (parser);
20306 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20307 }
20308
20309 return protorefs;
20310 }
20311
20312 /* Parse a Objective-C visibility specification. */
20313
20314 static void
20315 cp_parser_objc_visibility_spec (cp_parser* parser)
20316 {
20317 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20318
20319 switch (vis->keyword)
20320 {
20321 case RID_AT_PRIVATE:
20322 objc_set_visibility (2);
20323 break;
20324 case RID_AT_PROTECTED:
20325 objc_set_visibility (0);
20326 break;
20327 case RID_AT_PUBLIC:
20328 objc_set_visibility (1);
20329 break;
20330 default:
20331 return;
20332 }
20333
20334 /* Eat '@private'/'@protected'/'@public'. */
20335 cp_lexer_consume_token (parser->lexer);
20336 }
20337
20338 /* Parse an Objective-C method type. */
20339
20340 static void
20341 cp_parser_objc_method_type (cp_parser* parser)
20342 {
20343 objc_set_method_type
20344 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20345 ? PLUS_EXPR
20346 : MINUS_EXPR);
20347 }
20348
20349 /* Parse an Objective-C protocol qualifier. */
20350
20351 static tree
20352 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20353 {
20354 tree quals = NULL_TREE, node;
20355 cp_token *token = cp_lexer_peek_token (parser->lexer);
20356
20357 node = token->u.value;
20358
20359 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20360 && (node == ridpointers [(int) RID_IN]
20361 || node == ridpointers [(int) RID_OUT]
20362 || node == ridpointers [(int) RID_INOUT]
20363 || node == ridpointers [(int) RID_BYCOPY]
20364 || node == ridpointers [(int) RID_BYREF]
20365 || node == ridpointers [(int) RID_ONEWAY]))
20366 {
20367 quals = tree_cons (NULL_TREE, node, quals);
20368 cp_lexer_consume_token (parser->lexer);
20369 token = cp_lexer_peek_token (parser->lexer);
20370 node = token->u.value;
20371 }
20372
20373 return quals;
20374 }
20375
20376 /* Parse an Objective-C typename. */
20377
20378 static tree
20379 cp_parser_objc_typename (cp_parser* parser)
20380 {
20381 tree type_name = NULL_TREE;
20382
20383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20384 {
20385 tree proto_quals, cp_type = NULL_TREE;
20386
20387 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20388 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20389
20390 /* An ObjC type name may consist of just protocol qualifiers, in which
20391 case the type shall default to 'id'. */
20392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20393 cp_type = cp_parser_type_id (parser);
20394
20395 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20396 type_name = build_tree_list (proto_quals, cp_type);
20397 }
20398
20399 return type_name;
20400 }
20401
20402 /* Check to see if TYPE refers to an Objective-C selector name. */
20403
20404 static bool
20405 cp_parser_objc_selector_p (enum cpp_ttype type)
20406 {
20407 return (type == CPP_NAME || type == CPP_KEYWORD
20408 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20409 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20410 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20411 || type == CPP_XOR || type == CPP_XOR_EQ);
20412 }
20413
20414 /* Parse an Objective-C selector. */
20415
20416 static tree
20417 cp_parser_objc_selector (cp_parser* parser)
20418 {
20419 cp_token *token = cp_lexer_consume_token (parser->lexer);
20420
20421 if (!cp_parser_objc_selector_p (token->type))
20422 {
20423 error_at (token->location, "invalid Objective-C++ selector name");
20424 return error_mark_node;
20425 }
20426
20427 /* C++ operator names are allowed to appear in ObjC selectors. */
20428 switch (token->type)
20429 {
20430 case CPP_AND_AND: return get_identifier ("and");
20431 case CPP_AND_EQ: return get_identifier ("and_eq");
20432 case CPP_AND: return get_identifier ("bitand");
20433 case CPP_OR: return get_identifier ("bitor");
20434 case CPP_COMPL: return get_identifier ("compl");
20435 case CPP_NOT: return get_identifier ("not");
20436 case CPP_NOT_EQ: return get_identifier ("not_eq");
20437 case CPP_OR_OR: return get_identifier ("or");
20438 case CPP_OR_EQ: return get_identifier ("or_eq");
20439 case CPP_XOR: return get_identifier ("xor");
20440 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20441 default: return token->u.value;
20442 }
20443 }
20444
20445 /* Parse an Objective-C params list. */
20446
20447 static tree
20448 cp_parser_objc_method_keyword_params (cp_parser* parser)
20449 {
20450 tree params = NULL_TREE;
20451 bool maybe_unary_selector_p = true;
20452 cp_token *token = cp_lexer_peek_token (parser->lexer);
20453
20454 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20455 {
20456 tree selector = NULL_TREE, type_name, identifier;
20457
20458 if (token->type != CPP_COLON)
20459 selector = cp_parser_objc_selector (parser);
20460
20461 /* Detect if we have a unary selector. */
20462 if (maybe_unary_selector_p
20463 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20464 return selector;
20465
20466 maybe_unary_selector_p = false;
20467 cp_parser_require (parser, CPP_COLON, "%<:%>");
20468 type_name = cp_parser_objc_typename (parser);
20469 identifier = cp_parser_identifier (parser);
20470
20471 params
20472 = chainon (params,
20473 objc_build_keyword_decl (selector,
20474 type_name,
20475 identifier));
20476
20477 token = cp_lexer_peek_token (parser->lexer);
20478 }
20479
20480 return params;
20481 }
20482
20483 /* Parse the non-keyword Objective-C params. */
20484
20485 static tree
20486 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20487 {
20488 tree params = make_node (TREE_LIST);
20489 cp_token *token = cp_lexer_peek_token (parser->lexer);
20490 *ellipsisp = false; /* Initially, assume no ellipsis. */
20491
20492 while (token->type == CPP_COMMA)
20493 {
20494 cp_parameter_declarator *parmdecl;
20495 tree parm;
20496
20497 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20498 token = cp_lexer_peek_token (parser->lexer);
20499
20500 if (token->type == CPP_ELLIPSIS)
20501 {
20502 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20503 *ellipsisp = true;
20504 break;
20505 }
20506
20507 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20508 parm = grokdeclarator (parmdecl->declarator,
20509 &parmdecl->decl_specifiers,
20510 PARM, /*initialized=*/0,
20511 /*attrlist=*/NULL);
20512
20513 chainon (params, build_tree_list (NULL_TREE, parm));
20514 token = cp_lexer_peek_token (parser->lexer);
20515 }
20516
20517 return params;
20518 }
20519
20520 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20521
20522 static void
20523 cp_parser_objc_interstitial_code (cp_parser* parser)
20524 {
20525 cp_token *token = cp_lexer_peek_token (parser->lexer);
20526
20527 /* If the next token is `extern' and the following token is a string
20528 literal, then we have a linkage specification. */
20529 if (token->keyword == RID_EXTERN
20530 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20531 cp_parser_linkage_specification (parser);
20532 /* Handle #pragma, if any. */
20533 else if (token->type == CPP_PRAGMA)
20534 cp_parser_pragma (parser, pragma_external);
20535 /* Allow stray semicolons. */
20536 else if (token->type == CPP_SEMICOLON)
20537 cp_lexer_consume_token (parser->lexer);
20538 /* Finally, try to parse a block-declaration, or a function-definition. */
20539 else
20540 cp_parser_block_declaration (parser, /*statement_p=*/false);
20541 }
20542
20543 /* Parse a method signature. */
20544
20545 static tree
20546 cp_parser_objc_method_signature (cp_parser* parser)
20547 {
20548 tree rettype, kwdparms, optparms;
20549 bool ellipsis = false;
20550
20551 cp_parser_objc_method_type (parser);
20552 rettype = cp_parser_objc_typename (parser);
20553 kwdparms = cp_parser_objc_method_keyword_params (parser);
20554 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20555
20556 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20557 }
20558
20559 /* Pars an Objective-C method prototype list. */
20560
20561 static void
20562 cp_parser_objc_method_prototype_list (cp_parser* parser)
20563 {
20564 cp_token *token = cp_lexer_peek_token (parser->lexer);
20565
20566 while (token->keyword != RID_AT_END)
20567 {
20568 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20569 {
20570 objc_add_method_declaration
20571 (cp_parser_objc_method_signature (parser));
20572 cp_parser_consume_semicolon_at_end_of_statement (parser);
20573 }
20574 else
20575 /* Allow for interspersed non-ObjC++ code. */
20576 cp_parser_objc_interstitial_code (parser);
20577
20578 token = cp_lexer_peek_token (parser->lexer);
20579 }
20580
20581 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20582 objc_finish_interface ();
20583 }
20584
20585 /* Parse an Objective-C method definition list. */
20586
20587 static void
20588 cp_parser_objc_method_definition_list (cp_parser* parser)
20589 {
20590 cp_token *token = cp_lexer_peek_token (parser->lexer);
20591
20592 while (token->keyword != RID_AT_END)
20593 {
20594 tree meth;
20595
20596 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20597 {
20598 push_deferring_access_checks (dk_deferred);
20599 objc_start_method_definition
20600 (cp_parser_objc_method_signature (parser));
20601
20602 /* For historical reasons, we accept an optional semicolon. */
20603 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20604 cp_lexer_consume_token (parser->lexer);
20605
20606 perform_deferred_access_checks ();
20607 stop_deferring_access_checks ();
20608 meth = cp_parser_function_definition_after_declarator (parser,
20609 false);
20610 pop_deferring_access_checks ();
20611 objc_finish_method_definition (meth);
20612 }
20613 else
20614 /* Allow for interspersed non-ObjC++ code. */
20615 cp_parser_objc_interstitial_code (parser);
20616
20617 token = cp_lexer_peek_token (parser->lexer);
20618 }
20619
20620 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20621 objc_finish_implementation ();
20622 }
20623
20624 /* Parse Objective-C ivars. */
20625
20626 static void
20627 cp_parser_objc_class_ivars (cp_parser* parser)
20628 {
20629 cp_token *token = cp_lexer_peek_token (parser->lexer);
20630
20631 if (token->type != CPP_OPEN_BRACE)
20632 return; /* No ivars specified. */
20633
20634 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20635 token = cp_lexer_peek_token (parser->lexer);
20636
20637 while (token->type != CPP_CLOSE_BRACE)
20638 {
20639 cp_decl_specifier_seq declspecs;
20640 int decl_class_or_enum_p;
20641 tree prefix_attributes;
20642
20643 cp_parser_objc_visibility_spec (parser);
20644
20645 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20646 break;
20647
20648 cp_parser_decl_specifier_seq (parser,
20649 CP_PARSER_FLAGS_OPTIONAL,
20650 &declspecs,
20651 &decl_class_or_enum_p);
20652 prefix_attributes = declspecs.attributes;
20653 declspecs.attributes = NULL_TREE;
20654
20655 /* Keep going until we hit the `;' at the end of the
20656 declaration. */
20657 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20658 {
20659 tree width = NULL_TREE, attributes, first_attribute, decl;
20660 cp_declarator *declarator = NULL;
20661 int ctor_dtor_or_conv_p;
20662
20663 /* Check for a (possibly unnamed) bitfield declaration. */
20664 token = cp_lexer_peek_token (parser->lexer);
20665 if (token->type == CPP_COLON)
20666 goto eat_colon;
20667
20668 if (token->type == CPP_NAME
20669 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20670 == CPP_COLON))
20671 {
20672 /* Get the name of the bitfield. */
20673 declarator = make_id_declarator (NULL_TREE,
20674 cp_parser_identifier (parser),
20675 sfk_none);
20676
20677 eat_colon:
20678 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20679 /* Get the width of the bitfield. */
20680 width
20681 = cp_parser_constant_expression (parser,
20682 /*allow_non_constant=*/false,
20683 NULL);
20684 }
20685 else
20686 {
20687 /* Parse the declarator. */
20688 declarator
20689 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20690 &ctor_dtor_or_conv_p,
20691 /*parenthesized_p=*/NULL,
20692 /*member_p=*/false);
20693 }
20694
20695 /* Look for attributes that apply to the ivar. */
20696 attributes = cp_parser_attributes_opt (parser);
20697 /* Remember which attributes are prefix attributes and
20698 which are not. */
20699 first_attribute = attributes;
20700 /* Combine the attributes. */
20701 attributes = chainon (prefix_attributes, attributes);
20702
20703 if (width)
20704 /* Create the bitfield declaration. */
20705 decl = grokbitfield (declarator, &declspecs,
20706 width,
20707 attributes);
20708 else
20709 decl = grokfield (declarator, &declspecs,
20710 NULL_TREE, /*init_const_expr_p=*/false,
20711 NULL_TREE, attributes);
20712
20713 /* Add the instance variable. */
20714 objc_add_instance_variable (decl);
20715
20716 /* Reset PREFIX_ATTRIBUTES. */
20717 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20718 attributes = TREE_CHAIN (attributes);
20719 if (attributes)
20720 TREE_CHAIN (attributes) = NULL_TREE;
20721
20722 token = cp_lexer_peek_token (parser->lexer);
20723
20724 if (token->type == CPP_COMMA)
20725 {
20726 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20727 continue;
20728 }
20729 break;
20730 }
20731
20732 cp_parser_consume_semicolon_at_end_of_statement (parser);
20733 token = cp_lexer_peek_token (parser->lexer);
20734 }
20735
20736 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20737 /* For historical reasons, we accept an optional semicolon. */
20738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20739 cp_lexer_consume_token (parser->lexer);
20740 }
20741
20742 /* Parse an Objective-C protocol declaration. */
20743
20744 static void
20745 cp_parser_objc_protocol_declaration (cp_parser* parser)
20746 {
20747 tree proto, protorefs;
20748 cp_token *tok;
20749
20750 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20752 {
20753 tok = cp_lexer_peek_token (parser->lexer);
20754 error_at (tok->location, "identifier expected after %<@protocol%>");
20755 goto finish;
20756 }
20757
20758 /* See if we have a forward declaration or a definition. */
20759 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20760
20761 /* Try a forward declaration first. */
20762 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20763 {
20764 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20765 finish:
20766 cp_parser_consume_semicolon_at_end_of_statement (parser);
20767 }
20768
20769 /* Ok, we got a full-fledged definition (or at least should). */
20770 else
20771 {
20772 proto = cp_parser_identifier (parser);
20773 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20774 objc_start_protocol (proto, protorefs);
20775 cp_parser_objc_method_prototype_list (parser);
20776 }
20777 }
20778
20779 /* Parse an Objective-C superclass or category. */
20780
20781 static void
20782 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20783 tree *categ)
20784 {
20785 cp_token *next = cp_lexer_peek_token (parser->lexer);
20786
20787 *super = *categ = NULL_TREE;
20788 if (next->type == CPP_COLON)
20789 {
20790 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20791 *super = cp_parser_identifier (parser);
20792 }
20793 else if (next->type == CPP_OPEN_PAREN)
20794 {
20795 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20796 *categ = cp_parser_identifier (parser);
20797 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20798 }
20799 }
20800
20801 /* Parse an Objective-C class interface. */
20802
20803 static void
20804 cp_parser_objc_class_interface (cp_parser* parser)
20805 {
20806 tree name, super, categ, protos;
20807
20808 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20809 name = cp_parser_identifier (parser);
20810 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20811 protos = cp_parser_objc_protocol_refs_opt (parser);
20812
20813 /* We have either a class or a category on our hands. */
20814 if (categ)
20815 objc_start_category_interface (name, categ, protos);
20816 else
20817 {
20818 objc_start_class_interface (name, super, protos);
20819 /* Handle instance variable declarations, if any. */
20820 cp_parser_objc_class_ivars (parser);
20821 objc_continue_interface ();
20822 }
20823
20824 cp_parser_objc_method_prototype_list (parser);
20825 }
20826
20827 /* Parse an Objective-C class implementation. */
20828
20829 static void
20830 cp_parser_objc_class_implementation (cp_parser* parser)
20831 {
20832 tree name, super, categ;
20833
20834 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20835 name = cp_parser_identifier (parser);
20836 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20837
20838 /* We have either a class or a category on our hands. */
20839 if (categ)
20840 objc_start_category_implementation (name, categ);
20841 else
20842 {
20843 objc_start_class_implementation (name, super);
20844 /* Handle instance variable declarations, if any. */
20845 cp_parser_objc_class_ivars (parser);
20846 objc_continue_implementation ();
20847 }
20848
20849 cp_parser_objc_method_definition_list (parser);
20850 }
20851
20852 /* Consume the @end token and finish off the implementation. */
20853
20854 static void
20855 cp_parser_objc_end_implementation (cp_parser* parser)
20856 {
20857 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20858 objc_finish_implementation ();
20859 }
20860
20861 /* Parse an Objective-C declaration. */
20862
20863 static void
20864 cp_parser_objc_declaration (cp_parser* parser)
20865 {
20866 /* Try to figure out what kind of declaration is present. */
20867 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20868
20869 switch (kwd->keyword)
20870 {
20871 case RID_AT_ALIAS:
20872 cp_parser_objc_alias_declaration (parser);
20873 break;
20874 case RID_AT_CLASS:
20875 cp_parser_objc_class_declaration (parser);
20876 break;
20877 case RID_AT_PROTOCOL:
20878 cp_parser_objc_protocol_declaration (parser);
20879 break;
20880 case RID_AT_INTERFACE:
20881 cp_parser_objc_class_interface (parser);
20882 break;
20883 case RID_AT_IMPLEMENTATION:
20884 cp_parser_objc_class_implementation (parser);
20885 break;
20886 case RID_AT_END:
20887 cp_parser_objc_end_implementation (parser);
20888 break;
20889 default:
20890 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20891 kwd->u.value);
20892 cp_parser_skip_to_end_of_block_or_statement (parser);
20893 }
20894 }
20895
20896 /* Parse an Objective-C try-catch-finally statement.
20897
20898 objc-try-catch-finally-stmt:
20899 @try compound-statement objc-catch-clause-seq [opt]
20900 objc-finally-clause [opt]
20901
20902 objc-catch-clause-seq:
20903 objc-catch-clause objc-catch-clause-seq [opt]
20904
20905 objc-catch-clause:
20906 @catch ( exception-declaration ) compound-statement
20907
20908 objc-finally-clause
20909 @finally compound-statement
20910
20911 Returns NULL_TREE. */
20912
20913 static tree
20914 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20915 location_t location;
20916 tree stmt;
20917
20918 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20919 location = cp_lexer_peek_token (parser->lexer)->location;
20920 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20921 node, lest it get absorbed into the surrounding block. */
20922 stmt = push_stmt_list ();
20923 cp_parser_compound_statement (parser, NULL, false);
20924 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20925
20926 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20927 {
20928 cp_parameter_declarator *parmdecl;
20929 tree parm;
20930
20931 cp_lexer_consume_token (parser->lexer);
20932 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20933 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20934 parm = grokdeclarator (parmdecl->declarator,
20935 &parmdecl->decl_specifiers,
20936 PARM, /*initialized=*/0,
20937 /*attrlist=*/NULL);
20938 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20939 objc_begin_catch_clause (parm);
20940 cp_parser_compound_statement (parser, NULL, false);
20941 objc_finish_catch_clause ();
20942 }
20943
20944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20945 {
20946 cp_lexer_consume_token (parser->lexer);
20947 location = cp_lexer_peek_token (parser->lexer)->location;
20948 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20949 node, lest it get absorbed into the surrounding block. */
20950 stmt = push_stmt_list ();
20951 cp_parser_compound_statement (parser, NULL, false);
20952 objc_build_finally_clause (location, pop_stmt_list (stmt));
20953 }
20954
20955 return objc_finish_try_stmt ();
20956 }
20957
20958 /* Parse an Objective-C synchronized statement.
20959
20960 objc-synchronized-stmt:
20961 @synchronized ( expression ) compound-statement
20962
20963 Returns NULL_TREE. */
20964
20965 static tree
20966 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20967 location_t location;
20968 tree lock, stmt;
20969
20970 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20971
20972 location = cp_lexer_peek_token (parser->lexer)->location;
20973 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20974 lock = cp_parser_expression (parser, false, NULL);
20975 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20976
20977 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20978 node, lest it get absorbed into the surrounding block. */
20979 stmt = push_stmt_list ();
20980 cp_parser_compound_statement (parser, NULL, false);
20981
20982 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20983 }
20984
20985 /* Parse an Objective-C throw statement.
20986
20987 objc-throw-stmt:
20988 @throw assignment-expression [opt] ;
20989
20990 Returns a constructed '@throw' statement. */
20991
20992 static tree
20993 cp_parser_objc_throw_statement (cp_parser *parser) {
20994 tree expr = NULL_TREE;
20995 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20996
20997 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20998
20999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21000 expr = cp_parser_assignment_expression (parser, false, NULL);
21001
21002 cp_parser_consume_semicolon_at_end_of_statement (parser);
21003
21004 return objc_build_throw_stmt (loc, expr);
21005 }
21006
21007 /* Parse an Objective-C statement. */
21008
21009 static tree
21010 cp_parser_objc_statement (cp_parser * parser) {
21011 /* Try to figure out what kind of declaration is present. */
21012 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21013
21014 switch (kwd->keyword)
21015 {
21016 case RID_AT_TRY:
21017 return cp_parser_objc_try_catch_finally_statement (parser);
21018 case RID_AT_SYNCHRONIZED:
21019 return cp_parser_objc_synchronized_statement (parser);
21020 case RID_AT_THROW:
21021 return cp_parser_objc_throw_statement (parser);
21022 default:
21023 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21024 kwd->u.value);
21025 cp_parser_skip_to_end_of_block_or_statement (parser);
21026 }
21027
21028 return error_mark_node;
21029 }
21030 \f
21031 /* OpenMP 2.5 parsing routines. */
21032
21033 /* Returns name of the next clause.
21034 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21035 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21036 returned and the token is consumed. */
21037
21038 static pragma_omp_clause
21039 cp_parser_omp_clause_name (cp_parser *parser)
21040 {
21041 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21042
21043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21044 result = PRAGMA_OMP_CLAUSE_IF;
21045 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21046 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21047 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21048 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21049 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21050 {
21051 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21052 const char *p = IDENTIFIER_POINTER (id);
21053
21054 switch (p[0])
21055 {
21056 case 'c':
21057 if (!strcmp ("collapse", p))
21058 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21059 else if (!strcmp ("copyin", p))
21060 result = PRAGMA_OMP_CLAUSE_COPYIN;
21061 else if (!strcmp ("copyprivate", p))
21062 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21063 break;
21064 case 'f':
21065 if (!strcmp ("firstprivate", p))
21066 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21067 break;
21068 case 'l':
21069 if (!strcmp ("lastprivate", p))
21070 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21071 break;
21072 case 'n':
21073 if (!strcmp ("nowait", p))
21074 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21075 else if (!strcmp ("num_threads", p))
21076 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21077 break;
21078 case 'o':
21079 if (!strcmp ("ordered", p))
21080 result = PRAGMA_OMP_CLAUSE_ORDERED;
21081 break;
21082 case 'r':
21083 if (!strcmp ("reduction", p))
21084 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21085 break;
21086 case 's':
21087 if (!strcmp ("schedule", p))
21088 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21089 else if (!strcmp ("shared", p))
21090 result = PRAGMA_OMP_CLAUSE_SHARED;
21091 break;
21092 case 'u':
21093 if (!strcmp ("untied", p))
21094 result = PRAGMA_OMP_CLAUSE_UNTIED;
21095 break;
21096 }
21097 }
21098
21099 if (result != PRAGMA_OMP_CLAUSE_NONE)
21100 cp_lexer_consume_token (parser->lexer);
21101
21102 return result;
21103 }
21104
21105 /* Validate that a clause of the given type does not already exist. */
21106
21107 static void
21108 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21109 const char *name, location_t location)
21110 {
21111 tree c;
21112
21113 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21114 if (OMP_CLAUSE_CODE (c) == code)
21115 {
21116 error_at (location, "too many %qs clauses", name);
21117 break;
21118 }
21119 }
21120
21121 /* OpenMP 2.5:
21122 variable-list:
21123 identifier
21124 variable-list , identifier
21125
21126 In addition, we match a closing parenthesis. An opening parenthesis
21127 will have been consumed by the caller.
21128
21129 If KIND is nonzero, create the appropriate node and install the decl
21130 in OMP_CLAUSE_DECL and add the node to the head of the list.
21131
21132 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21133 return the list created. */
21134
21135 static tree
21136 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21137 tree list)
21138 {
21139 cp_token *token;
21140 while (1)
21141 {
21142 tree name, decl;
21143
21144 token = cp_lexer_peek_token (parser->lexer);
21145 name = cp_parser_id_expression (parser, /*template_p=*/false,
21146 /*check_dependency_p=*/true,
21147 /*template_p=*/NULL,
21148 /*declarator_p=*/false,
21149 /*optional_p=*/false);
21150 if (name == error_mark_node)
21151 goto skip_comma;
21152
21153 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21154 if (decl == error_mark_node)
21155 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21156 else if (kind != 0)
21157 {
21158 tree u = build_omp_clause (token->location, kind);
21159 OMP_CLAUSE_DECL (u) = decl;
21160 OMP_CLAUSE_CHAIN (u) = list;
21161 list = u;
21162 }
21163 else
21164 list = tree_cons (decl, NULL_TREE, list);
21165
21166 get_comma:
21167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21168 break;
21169 cp_lexer_consume_token (parser->lexer);
21170 }
21171
21172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21173 {
21174 int ending;
21175
21176 /* Try to resync to an unnested comma. Copied from
21177 cp_parser_parenthesized_expression_list. */
21178 skip_comma:
21179 ending = cp_parser_skip_to_closing_parenthesis (parser,
21180 /*recovering=*/true,
21181 /*or_comma=*/true,
21182 /*consume_paren=*/true);
21183 if (ending < 0)
21184 goto get_comma;
21185 }
21186
21187 return list;
21188 }
21189
21190 /* Similarly, but expect leading and trailing parenthesis. This is a very
21191 common case for omp clauses. */
21192
21193 static tree
21194 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21195 {
21196 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21197 return cp_parser_omp_var_list_no_open (parser, kind, list);
21198 return list;
21199 }
21200
21201 /* OpenMP 3.0:
21202 collapse ( constant-expression ) */
21203
21204 static tree
21205 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21206 {
21207 tree c, num;
21208 location_t loc;
21209 HOST_WIDE_INT n;
21210
21211 loc = cp_lexer_peek_token (parser->lexer)->location;
21212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21213 return list;
21214
21215 num = cp_parser_constant_expression (parser, false, NULL);
21216
21217 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21219 /*or_comma=*/false,
21220 /*consume_paren=*/true);
21221
21222 if (num == error_mark_node)
21223 return list;
21224 num = fold_non_dependent_expr (num);
21225 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21226 || !host_integerp (num, 0)
21227 || (n = tree_low_cst (num, 0)) <= 0
21228 || (int) n != n)
21229 {
21230 error_at (loc, "collapse argument needs positive constant integer expression");
21231 return list;
21232 }
21233
21234 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21235 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21236 OMP_CLAUSE_CHAIN (c) = list;
21237 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21238
21239 return c;
21240 }
21241
21242 /* OpenMP 2.5:
21243 default ( shared | none ) */
21244
21245 static tree
21246 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21247 {
21248 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21249 tree c;
21250
21251 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21252 return list;
21253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21254 {
21255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21256 const char *p = IDENTIFIER_POINTER (id);
21257
21258 switch (p[0])
21259 {
21260 case 'n':
21261 if (strcmp ("none", p) != 0)
21262 goto invalid_kind;
21263 kind = OMP_CLAUSE_DEFAULT_NONE;
21264 break;
21265
21266 case 's':
21267 if (strcmp ("shared", p) != 0)
21268 goto invalid_kind;
21269 kind = OMP_CLAUSE_DEFAULT_SHARED;
21270 break;
21271
21272 default:
21273 goto invalid_kind;
21274 }
21275
21276 cp_lexer_consume_token (parser->lexer);
21277 }
21278 else
21279 {
21280 invalid_kind:
21281 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21282 }
21283
21284 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21286 /*or_comma=*/false,
21287 /*consume_paren=*/true);
21288
21289 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21290 return list;
21291
21292 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21293 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21294 OMP_CLAUSE_CHAIN (c) = list;
21295 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21296
21297 return c;
21298 }
21299
21300 /* OpenMP 2.5:
21301 if ( expression ) */
21302
21303 static tree
21304 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21305 {
21306 tree t, c;
21307
21308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21309 return list;
21310
21311 t = cp_parser_condition (parser);
21312
21313 if (t == error_mark_node
21314 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21315 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21316 /*or_comma=*/false,
21317 /*consume_paren=*/true);
21318
21319 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21320
21321 c = build_omp_clause (location, OMP_CLAUSE_IF);
21322 OMP_CLAUSE_IF_EXPR (c) = t;
21323 OMP_CLAUSE_CHAIN (c) = list;
21324
21325 return c;
21326 }
21327
21328 /* OpenMP 2.5:
21329 nowait */
21330
21331 static tree
21332 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21333 tree list, location_t location)
21334 {
21335 tree c;
21336
21337 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21338
21339 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21340 OMP_CLAUSE_CHAIN (c) = list;
21341 return c;
21342 }
21343
21344 /* OpenMP 2.5:
21345 num_threads ( expression ) */
21346
21347 static tree
21348 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21349 location_t location)
21350 {
21351 tree t, c;
21352
21353 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21354 return list;
21355
21356 t = cp_parser_expression (parser, false, NULL);
21357
21358 if (t == error_mark_node
21359 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21360 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21361 /*or_comma=*/false,
21362 /*consume_paren=*/true);
21363
21364 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21365 "num_threads", location);
21366
21367 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21368 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21369 OMP_CLAUSE_CHAIN (c) = list;
21370
21371 return c;
21372 }
21373
21374 /* OpenMP 2.5:
21375 ordered */
21376
21377 static tree
21378 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21379 tree list, location_t location)
21380 {
21381 tree c;
21382
21383 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21384 "ordered", location);
21385
21386 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21387 OMP_CLAUSE_CHAIN (c) = list;
21388 return c;
21389 }
21390
21391 /* OpenMP 2.5:
21392 reduction ( reduction-operator : variable-list )
21393
21394 reduction-operator:
21395 One of: + * - & ^ | && || */
21396
21397 static tree
21398 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21399 {
21400 enum tree_code code;
21401 tree nlist, c;
21402
21403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21404 return list;
21405
21406 switch (cp_lexer_peek_token (parser->lexer)->type)
21407 {
21408 case CPP_PLUS:
21409 code = PLUS_EXPR;
21410 break;
21411 case CPP_MULT:
21412 code = MULT_EXPR;
21413 break;
21414 case CPP_MINUS:
21415 code = MINUS_EXPR;
21416 break;
21417 case CPP_AND:
21418 code = BIT_AND_EXPR;
21419 break;
21420 case CPP_XOR:
21421 code = BIT_XOR_EXPR;
21422 break;
21423 case CPP_OR:
21424 code = BIT_IOR_EXPR;
21425 break;
21426 case CPP_AND_AND:
21427 code = TRUTH_ANDIF_EXPR;
21428 break;
21429 case CPP_OR_OR:
21430 code = TRUTH_ORIF_EXPR;
21431 break;
21432 default:
21433 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21434 "%<|%>, %<&&%>, or %<||%>");
21435 resync_fail:
21436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21437 /*or_comma=*/false,
21438 /*consume_paren=*/true);
21439 return list;
21440 }
21441 cp_lexer_consume_token (parser->lexer);
21442
21443 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21444 goto resync_fail;
21445
21446 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21447 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21448 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21449
21450 return nlist;
21451 }
21452
21453 /* OpenMP 2.5:
21454 schedule ( schedule-kind )
21455 schedule ( schedule-kind , expression )
21456
21457 schedule-kind:
21458 static | dynamic | guided | runtime | auto */
21459
21460 static tree
21461 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21462 {
21463 tree c, t;
21464
21465 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21466 return list;
21467
21468 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21469
21470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21471 {
21472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21473 const char *p = IDENTIFIER_POINTER (id);
21474
21475 switch (p[0])
21476 {
21477 case 'd':
21478 if (strcmp ("dynamic", p) != 0)
21479 goto invalid_kind;
21480 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21481 break;
21482
21483 case 'g':
21484 if (strcmp ("guided", p) != 0)
21485 goto invalid_kind;
21486 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21487 break;
21488
21489 case 'r':
21490 if (strcmp ("runtime", p) != 0)
21491 goto invalid_kind;
21492 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21493 break;
21494
21495 default:
21496 goto invalid_kind;
21497 }
21498 }
21499 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21500 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21501 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21502 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21503 else
21504 goto invalid_kind;
21505 cp_lexer_consume_token (parser->lexer);
21506
21507 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21508 {
21509 cp_token *token;
21510 cp_lexer_consume_token (parser->lexer);
21511
21512 token = cp_lexer_peek_token (parser->lexer);
21513 t = cp_parser_assignment_expression (parser, false, NULL);
21514
21515 if (t == error_mark_node)
21516 goto resync_fail;
21517 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21518 error_at (token->location, "schedule %<runtime%> does not take "
21519 "a %<chunk_size%> parameter");
21520 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21521 error_at (token->location, "schedule %<auto%> does not take "
21522 "a %<chunk_size%> parameter");
21523 else
21524 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21525
21526 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21527 goto resync_fail;
21528 }
21529 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21530 goto resync_fail;
21531
21532 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21533 OMP_CLAUSE_CHAIN (c) = list;
21534 return c;
21535
21536 invalid_kind:
21537 cp_parser_error (parser, "invalid schedule kind");
21538 resync_fail:
21539 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21540 /*or_comma=*/false,
21541 /*consume_paren=*/true);
21542 return list;
21543 }
21544
21545 /* OpenMP 3.0:
21546 untied */
21547
21548 static tree
21549 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21550 tree list, location_t location)
21551 {
21552 tree c;
21553
21554 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21555
21556 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21557 OMP_CLAUSE_CHAIN (c) = list;
21558 return c;
21559 }
21560
21561 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21562 is a bitmask in MASK. Return the list of clauses found; the result
21563 of clause default goes in *pdefault. */
21564
21565 static tree
21566 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21567 const char *where, cp_token *pragma_tok)
21568 {
21569 tree clauses = NULL;
21570 bool first = true;
21571 cp_token *token = NULL;
21572
21573 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21574 {
21575 pragma_omp_clause c_kind;
21576 const char *c_name;
21577 tree prev = clauses;
21578
21579 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21580 cp_lexer_consume_token (parser->lexer);
21581
21582 token = cp_lexer_peek_token (parser->lexer);
21583 c_kind = cp_parser_omp_clause_name (parser);
21584 first = false;
21585
21586 switch (c_kind)
21587 {
21588 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21589 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21590 token->location);
21591 c_name = "collapse";
21592 break;
21593 case PRAGMA_OMP_CLAUSE_COPYIN:
21594 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21595 c_name = "copyin";
21596 break;
21597 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21598 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21599 clauses);
21600 c_name = "copyprivate";
21601 break;
21602 case PRAGMA_OMP_CLAUSE_DEFAULT:
21603 clauses = cp_parser_omp_clause_default (parser, clauses,
21604 token->location);
21605 c_name = "default";
21606 break;
21607 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21608 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21609 clauses);
21610 c_name = "firstprivate";
21611 break;
21612 case PRAGMA_OMP_CLAUSE_IF:
21613 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21614 c_name = "if";
21615 break;
21616 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21617 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21618 clauses);
21619 c_name = "lastprivate";
21620 break;
21621 case PRAGMA_OMP_CLAUSE_NOWAIT:
21622 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21623 c_name = "nowait";
21624 break;
21625 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21626 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21627 token->location);
21628 c_name = "num_threads";
21629 break;
21630 case PRAGMA_OMP_CLAUSE_ORDERED:
21631 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21632 token->location);
21633 c_name = "ordered";
21634 break;
21635 case PRAGMA_OMP_CLAUSE_PRIVATE:
21636 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21637 clauses);
21638 c_name = "private";
21639 break;
21640 case PRAGMA_OMP_CLAUSE_REDUCTION:
21641 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21642 c_name = "reduction";
21643 break;
21644 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21645 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21646 token->location);
21647 c_name = "schedule";
21648 break;
21649 case PRAGMA_OMP_CLAUSE_SHARED:
21650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21651 clauses);
21652 c_name = "shared";
21653 break;
21654 case PRAGMA_OMP_CLAUSE_UNTIED:
21655 clauses = cp_parser_omp_clause_untied (parser, clauses,
21656 token->location);
21657 c_name = "nowait";
21658 break;
21659 default:
21660 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21661 goto saw_error;
21662 }
21663
21664 if (((mask >> c_kind) & 1) == 0)
21665 {
21666 /* Remove the invalid clause(s) from the list to avoid
21667 confusing the rest of the compiler. */
21668 clauses = prev;
21669 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21670 }
21671 }
21672 saw_error:
21673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21674 return finish_omp_clauses (clauses);
21675 }
21676
21677 /* OpenMP 2.5:
21678 structured-block:
21679 statement
21680
21681 In practice, we're also interested in adding the statement to an
21682 outer node. So it is convenient if we work around the fact that
21683 cp_parser_statement calls add_stmt. */
21684
21685 static unsigned
21686 cp_parser_begin_omp_structured_block (cp_parser *parser)
21687 {
21688 unsigned save = parser->in_statement;
21689
21690 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21691 This preserves the "not within loop or switch" style error messages
21692 for nonsense cases like
21693 void foo() {
21694 #pragma omp single
21695 break;
21696 }
21697 */
21698 if (parser->in_statement)
21699 parser->in_statement = IN_OMP_BLOCK;
21700
21701 return save;
21702 }
21703
21704 static void
21705 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21706 {
21707 parser->in_statement = save;
21708 }
21709
21710 static tree
21711 cp_parser_omp_structured_block (cp_parser *parser)
21712 {
21713 tree stmt = begin_omp_structured_block ();
21714 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21715
21716 cp_parser_statement (parser, NULL_TREE, false, NULL);
21717
21718 cp_parser_end_omp_structured_block (parser, save);
21719 return finish_omp_structured_block (stmt);
21720 }
21721
21722 /* OpenMP 2.5:
21723 # pragma omp atomic new-line
21724 expression-stmt
21725
21726 expression-stmt:
21727 x binop= expr | x++ | ++x | x-- | --x
21728 binop:
21729 +, *, -, /, &, ^, |, <<, >>
21730
21731 where x is an lvalue expression with scalar type. */
21732
21733 static void
21734 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21735 {
21736 tree lhs, rhs;
21737 enum tree_code code;
21738
21739 cp_parser_require_pragma_eol (parser, pragma_tok);
21740
21741 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21742 /*cast_p=*/false, NULL);
21743 switch (TREE_CODE (lhs))
21744 {
21745 case ERROR_MARK:
21746 goto saw_error;
21747
21748 case PREINCREMENT_EXPR:
21749 case POSTINCREMENT_EXPR:
21750 lhs = TREE_OPERAND (lhs, 0);
21751 code = PLUS_EXPR;
21752 rhs = integer_one_node;
21753 break;
21754
21755 case PREDECREMENT_EXPR:
21756 case POSTDECREMENT_EXPR:
21757 lhs = TREE_OPERAND (lhs, 0);
21758 code = MINUS_EXPR;
21759 rhs = integer_one_node;
21760 break;
21761
21762 default:
21763 switch (cp_lexer_peek_token (parser->lexer)->type)
21764 {
21765 case CPP_MULT_EQ:
21766 code = MULT_EXPR;
21767 break;
21768 case CPP_DIV_EQ:
21769 code = TRUNC_DIV_EXPR;
21770 break;
21771 case CPP_PLUS_EQ:
21772 code = PLUS_EXPR;
21773 break;
21774 case CPP_MINUS_EQ:
21775 code = MINUS_EXPR;
21776 break;
21777 case CPP_LSHIFT_EQ:
21778 code = LSHIFT_EXPR;
21779 break;
21780 case CPP_RSHIFT_EQ:
21781 code = RSHIFT_EXPR;
21782 break;
21783 case CPP_AND_EQ:
21784 code = BIT_AND_EXPR;
21785 break;
21786 case CPP_OR_EQ:
21787 code = BIT_IOR_EXPR;
21788 break;
21789 case CPP_XOR_EQ:
21790 code = BIT_XOR_EXPR;
21791 break;
21792 default:
21793 cp_parser_error (parser,
21794 "invalid operator for %<#pragma omp atomic%>");
21795 goto saw_error;
21796 }
21797 cp_lexer_consume_token (parser->lexer);
21798
21799 rhs = cp_parser_expression (parser, false, NULL);
21800 if (rhs == error_mark_node)
21801 goto saw_error;
21802 break;
21803 }
21804 finish_omp_atomic (code, lhs, rhs);
21805 cp_parser_consume_semicolon_at_end_of_statement (parser);
21806 return;
21807
21808 saw_error:
21809 cp_parser_skip_to_end_of_block_or_statement (parser);
21810 }
21811
21812
21813 /* OpenMP 2.5:
21814 # pragma omp barrier new-line */
21815
21816 static void
21817 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21818 {
21819 cp_parser_require_pragma_eol (parser, pragma_tok);
21820 finish_omp_barrier ();
21821 }
21822
21823 /* OpenMP 2.5:
21824 # pragma omp critical [(name)] new-line
21825 structured-block */
21826
21827 static tree
21828 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21829 {
21830 tree stmt, name = NULL;
21831
21832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21833 {
21834 cp_lexer_consume_token (parser->lexer);
21835
21836 name = cp_parser_identifier (parser);
21837
21838 if (name == error_mark_node
21839 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21841 /*or_comma=*/false,
21842 /*consume_paren=*/true);
21843 if (name == error_mark_node)
21844 name = NULL;
21845 }
21846 cp_parser_require_pragma_eol (parser, pragma_tok);
21847
21848 stmt = cp_parser_omp_structured_block (parser);
21849 return c_finish_omp_critical (input_location, stmt, name);
21850 }
21851
21852 /* OpenMP 2.5:
21853 # pragma omp flush flush-vars[opt] new-line
21854
21855 flush-vars:
21856 ( variable-list ) */
21857
21858 static void
21859 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21860 {
21861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21862 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21863 cp_parser_require_pragma_eol (parser, pragma_tok);
21864
21865 finish_omp_flush ();
21866 }
21867
21868 /* Helper function, to parse omp for increment expression. */
21869
21870 static tree
21871 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21872 {
21873 tree cond = cp_parser_binary_expression (parser, false, true,
21874 PREC_NOT_OPERATOR, NULL);
21875 bool overloaded_p;
21876
21877 if (cond == error_mark_node
21878 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21879 {
21880 cp_parser_skip_to_end_of_statement (parser);
21881 return error_mark_node;
21882 }
21883
21884 switch (TREE_CODE (cond))
21885 {
21886 case GT_EXPR:
21887 case GE_EXPR:
21888 case LT_EXPR:
21889 case LE_EXPR:
21890 break;
21891 default:
21892 return error_mark_node;
21893 }
21894
21895 /* If decl is an iterator, preserve LHS and RHS of the relational
21896 expr until finish_omp_for. */
21897 if (decl
21898 && (type_dependent_expression_p (decl)
21899 || CLASS_TYPE_P (TREE_TYPE (decl))))
21900 return cond;
21901
21902 return build_x_binary_op (TREE_CODE (cond),
21903 TREE_OPERAND (cond, 0), ERROR_MARK,
21904 TREE_OPERAND (cond, 1), ERROR_MARK,
21905 &overloaded_p, tf_warning_or_error);
21906 }
21907
21908 /* Helper function, to parse omp for increment expression. */
21909
21910 static tree
21911 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21912 {
21913 cp_token *token = cp_lexer_peek_token (parser->lexer);
21914 enum tree_code op;
21915 tree lhs, rhs;
21916 cp_id_kind idk;
21917 bool decl_first;
21918
21919 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21920 {
21921 op = (token->type == CPP_PLUS_PLUS
21922 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21923 cp_lexer_consume_token (parser->lexer);
21924 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21925 if (lhs != decl)
21926 return error_mark_node;
21927 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21928 }
21929
21930 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21931 if (lhs != decl)
21932 return error_mark_node;
21933
21934 token = cp_lexer_peek_token (parser->lexer);
21935 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21936 {
21937 op = (token->type == CPP_PLUS_PLUS
21938 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21939 cp_lexer_consume_token (parser->lexer);
21940 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21941 }
21942
21943 op = cp_parser_assignment_operator_opt (parser);
21944 if (op == ERROR_MARK)
21945 return error_mark_node;
21946
21947 if (op != NOP_EXPR)
21948 {
21949 rhs = cp_parser_assignment_expression (parser, false, NULL);
21950 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21951 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21952 }
21953
21954 lhs = cp_parser_binary_expression (parser, false, false,
21955 PREC_ADDITIVE_EXPRESSION, NULL);
21956 token = cp_lexer_peek_token (parser->lexer);
21957 decl_first = lhs == decl;
21958 if (decl_first)
21959 lhs = NULL_TREE;
21960 if (token->type != CPP_PLUS
21961 && token->type != CPP_MINUS)
21962 return error_mark_node;
21963
21964 do
21965 {
21966 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21967 cp_lexer_consume_token (parser->lexer);
21968 rhs = cp_parser_binary_expression (parser, false, false,
21969 PREC_ADDITIVE_EXPRESSION, NULL);
21970 token = cp_lexer_peek_token (parser->lexer);
21971 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21972 {
21973 if (lhs == NULL_TREE)
21974 {
21975 if (op == PLUS_EXPR)
21976 lhs = rhs;
21977 else
21978 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21979 }
21980 else
21981 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21982 NULL, tf_warning_or_error);
21983 }
21984 }
21985 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21986
21987 if (!decl_first)
21988 {
21989 if (rhs != decl || op == MINUS_EXPR)
21990 return error_mark_node;
21991 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21992 }
21993 else
21994 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21995
21996 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21997 }
21998
21999 /* Parse the restricted form of the for statement allowed by OpenMP. */
22000
22001 static tree
22002 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22003 {
22004 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22005 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22006 tree this_pre_body, cl;
22007 location_t loc_first;
22008 bool collapse_err = false;
22009 int i, collapse = 1, nbraces = 0;
22010
22011 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22012 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22013 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22014
22015 gcc_assert (collapse >= 1);
22016
22017 declv = make_tree_vec (collapse);
22018 initv = make_tree_vec (collapse);
22019 condv = make_tree_vec (collapse);
22020 incrv = make_tree_vec (collapse);
22021
22022 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22023
22024 for (i = 0; i < collapse; i++)
22025 {
22026 int bracecount = 0;
22027 bool add_private_clause = false;
22028 location_t loc;
22029
22030 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22031 {
22032 cp_parser_error (parser, "for statement expected");
22033 return NULL;
22034 }
22035 loc = cp_lexer_consume_token (parser->lexer)->location;
22036
22037 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22038 return NULL;
22039
22040 init = decl = real_decl = NULL;
22041 this_pre_body = push_stmt_list ();
22042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22043 {
22044 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22045
22046 init-expr:
22047 var = lb
22048 integer-type var = lb
22049 random-access-iterator-type var = lb
22050 pointer-type var = lb
22051 */
22052 cp_decl_specifier_seq type_specifiers;
22053
22054 /* First, try to parse as an initialized declaration. See
22055 cp_parser_condition, from whence the bulk of this is copied. */
22056
22057 cp_parser_parse_tentatively (parser);
22058 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22059 &type_specifiers);
22060 if (cp_parser_parse_definitely (parser))
22061 {
22062 /* If parsing a type specifier seq succeeded, then this
22063 MUST be a initialized declaration. */
22064 tree asm_specification, attributes;
22065 cp_declarator *declarator;
22066
22067 declarator = cp_parser_declarator (parser,
22068 CP_PARSER_DECLARATOR_NAMED,
22069 /*ctor_dtor_or_conv_p=*/NULL,
22070 /*parenthesized_p=*/NULL,
22071 /*member_p=*/false);
22072 attributes = cp_parser_attributes_opt (parser);
22073 asm_specification = cp_parser_asm_specification_opt (parser);
22074
22075 if (declarator == cp_error_declarator)
22076 cp_parser_skip_to_end_of_statement (parser);
22077
22078 else
22079 {
22080 tree pushed_scope, auto_node;
22081
22082 decl = start_decl (declarator, &type_specifiers,
22083 SD_INITIALIZED, attributes,
22084 /*prefix_attributes=*/NULL_TREE,
22085 &pushed_scope);
22086
22087 auto_node = type_uses_auto (TREE_TYPE (decl));
22088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22089 {
22090 if (cp_lexer_next_token_is (parser->lexer,
22091 CPP_OPEN_PAREN))
22092 error ("parenthesized initialization is not allowed in "
22093 "OpenMP %<for%> loop");
22094 else
22095 /* Trigger an error. */
22096 cp_parser_require (parser, CPP_EQ, "%<=%>");
22097
22098 init = error_mark_node;
22099 cp_parser_skip_to_end_of_statement (parser);
22100 }
22101 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22102 || type_dependent_expression_p (decl)
22103 || auto_node)
22104 {
22105 bool is_direct_init, is_non_constant_init;
22106
22107 init = cp_parser_initializer (parser,
22108 &is_direct_init,
22109 &is_non_constant_init);
22110
22111 if (auto_node && describable_type (init))
22112 {
22113 TREE_TYPE (decl)
22114 = do_auto_deduction (TREE_TYPE (decl), init,
22115 auto_node);
22116
22117 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22118 && !type_dependent_expression_p (decl))
22119 goto non_class;
22120 }
22121
22122 cp_finish_decl (decl, init, !is_non_constant_init,
22123 asm_specification,
22124 LOOKUP_ONLYCONVERTING);
22125 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22126 {
22127 for_block
22128 = tree_cons (NULL, this_pre_body, for_block);
22129 init = NULL_TREE;
22130 }
22131 else
22132 init = pop_stmt_list (this_pre_body);
22133 this_pre_body = NULL_TREE;
22134 }
22135 else
22136 {
22137 /* Consume '='. */
22138 cp_lexer_consume_token (parser->lexer);
22139 init = cp_parser_assignment_expression (parser, false, NULL);
22140
22141 non_class:
22142 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22143 init = error_mark_node;
22144 else
22145 cp_finish_decl (decl, NULL_TREE,
22146 /*init_const_expr_p=*/false,
22147 asm_specification,
22148 LOOKUP_ONLYCONVERTING);
22149 }
22150
22151 if (pushed_scope)
22152 pop_scope (pushed_scope);
22153 }
22154 }
22155 else
22156 {
22157 cp_id_kind idk;
22158 /* If parsing a type specifier sequence failed, then
22159 this MUST be a simple expression. */
22160 cp_parser_parse_tentatively (parser);
22161 decl = cp_parser_primary_expression (parser, false, false,
22162 false, &idk);
22163 if (!cp_parser_error_occurred (parser)
22164 && decl
22165 && DECL_P (decl)
22166 && CLASS_TYPE_P (TREE_TYPE (decl)))
22167 {
22168 tree rhs;
22169
22170 cp_parser_parse_definitely (parser);
22171 cp_parser_require (parser, CPP_EQ, "%<=%>");
22172 rhs = cp_parser_assignment_expression (parser, false, NULL);
22173 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22174 rhs,
22175 tf_warning_or_error));
22176 add_private_clause = true;
22177 }
22178 else
22179 {
22180 decl = NULL;
22181 cp_parser_abort_tentative_parse (parser);
22182 init = cp_parser_expression (parser, false, NULL);
22183 if (init)
22184 {
22185 if (TREE_CODE (init) == MODIFY_EXPR
22186 || TREE_CODE (init) == MODOP_EXPR)
22187 real_decl = TREE_OPERAND (init, 0);
22188 }
22189 }
22190 }
22191 }
22192 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22193 if (this_pre_body)
22194 {
22195 this_pre_body = pop_stmt_list (this_pre_body);
22196 if (pre_body)
22197 {
22198 tree t = pre_body;
22199 pre_body = push_stmt_list ();
22200 add_stmt (t);
22201 add_stmt (this_pre_body);
22202 pre_body = pop_stmt_list (pre_body);
22203 }
22204 else
22205 pre_body = this_pre_body;
22206 }
22207
22208 if (decl)
22209 real_decl = decl;
22210 if (par_clauses != NULL && real_decl != NULL_TREE)
22211 {
22212 tree *c;
22213 for (c = par_clauses; *c ; )
22214 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22215 && OMP_CLAUSE_DECL (*c) == real_decl)
22216 {
22217 error_at (loc, "iteration variable %qD"
22218 " should not be firstprivate", real_decl);
22219 *c = OMP_CLAUSE_CHAIN (*c);
22220 }
22221 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22222 && OMP_CLAUSE_DECL (*c) == real_decl)
22223 {
22224 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22225 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22226 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22227 OMP_CLAUSE_DECL (l) = real_decl;
22228 OMP_CLAUSE_CHAIN (l) = clauses;
22229 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22230 clauses = l;
22231 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22232 CP_OMP_CLAUSE_INFO (*c) = NULL;
22233 add_private_clause = false;
22234 }
22235 else
22236 {
22237 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22238 && OMP_CLAUSE_DECL (*c) == real_decl)
22239 add_private_clause = false;
22240 c = &OMP_CLAUSE_CHAIN (*c);
22241 }
22242 }
22243
22244 if (add_private_clause)
22245 {
22246 tree c;
22247 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22248 {
22249 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22250 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22251 && OMP_CLAUSE_DECL (c) == decl)
22252 break;
22253 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22254 && OMP_CLAUSE_DECL (c) == decl)
22255 error_at (loc, "iteration variable %qD "
22256 "should not be firstprivate",
22257 decl);
22258 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22259 && OMP_CLAUSE_DECL (c) == decl)
22260 error_at (loc, "iteration variable %qD should not be reduction",
22261 decl);
22262 }
22263 if (c == NULL)
22264 {
22265 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22266 OMP_CLAUSE_DECL (c) = decl;
22267 c = finish_omp_clauses (c);
22268 if (c)
22269 {
22270 OMP_CLAUSE_CHAIN (c) = clauses;
22271 clauses = c;
22272 }
22273 }
22274 }
22275
22276 cond = NULL;
22277 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22278 cond = cp_parser_omp_for_cond (parser, decl);
22279 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22280
22281 incr = NULL;
22282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22283 {
22284 /* If decl is an iterator, preserve the operator on decl
22285 until finish_omp_for. */
22286 if (decl
22287 && (type_dependent_expression_p (decl)
22288 || CLASS_TYPE_P (TREE_TYPE (decl))))
22289 incr = cp_parser_omp_for_incr (parser, decl);
22290 else
22291 incr = cp_parser_expression (parser, false, NULL);
22292 }
22293
22294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22295 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22296 /*or_comma=*/false,
22297 /*consume_paren=*/true);
22298
22299 TREE_VEC_ELT (declv, i) = decl;
22300 TREE_VEC_ELT (initv, i) = init;
22301 TREE_VEC_ELT (condv, i) = cond;
22302 TREE_VEC_ELT (incrv, i) = incr;
22303
22304 if (i == collapse - 1)
22305 break;
22306
22307 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22308 in between the collapsed for loops to be still considered perfectly
22309 nested. Hopefully the final version clarifies this.
22310 For now handle (multiple) {'s and empty statements. */
22311 cp_parser_parse_tentatively (parser);
22312 do
22313 {
22314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22315 break;
22316 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22317 {
22318 cp_lexer_consume_token (parser->lexer);
22319 bracecount++;
22320 }
22321 else if (bracecount
22322 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22323 cp_lexer_consume_token (parser->lexer);
22324 else
22325 {
22326 loc = cp_lexer_peek_token (parser->lexer)->location;
22327 error_at (loc, "not enough collapsed for loops");
22328 collapse_err = true;
22329 cp_parser_abort_tentative_parse (parser);
22330 declv = NULL_TREE;
22331 break;
22332 }
22333 }
22334 while (1);
22335
22336 if (declv)
22337 {
22338 cp_parser_parse_definitely (parser);
22339 nbraces += bracecount;
22340 }
22341 }
22342
22343 /* Note that we saved the original contents of this flag when we entered
22344 the structured block, and so we don't need to re-save it here. */
22345 parser->in_statement = IN_OMP_FOR;
22346
22347 /* Note that the grammar doesn't call for a structured block here,
22348 though the loop as a whole is a structured block. */
22349 body = push_stmt_list ();
22350 cp_parser_statement (parser, NULL_TREE, false, NULL);
22351 body = pop_stmt_list (body);
22352
22353 if (declv == NULL_TREE)
22354 ret = NULL_TREE;
22355 else
22356 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22357 pre_body, clauses);
22358
22359 while (nbraces)
22360 {
22361 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22362 {
22363 cp_lexer_consume_token (parser->lexer);
22364 nbraces--;
22365 }
22366 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22367 cp_lexer_consume_token (parser->lexer);
22368 else
22369 {
22370 if (!collapse_err)
22371 {
22372 error_at (cp_lexer_peek_token (parser->lexer)->location,
22373 "collapsed loops not perfectly nested");
22374 }
22375 collapse_err = true;
22376 cp_parser_statement_seq_opt (parser, NULL);
22377 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22378 }
22379 }
22380
22381 while (for_block)
22382 {
22383 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22384 for_block = TREE_CHAIN (for_block);
22385 }
22386
22387 return ret;
22388 }
22389
22390 /* OpenMP 2.5:
22391 #pragma omp for for-clause[optseq] new-line
22392 for-loop */
22393
22394 #define OMP_FOR_CLAUSE_MASK \
22395 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22396 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22397 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22398 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22399 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22400 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22401 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22402 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22403
22404 static tree
22405 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22406 {
22407 tree clauses, sb, ret;
22408 unsigned int save;
22409
22410 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22411 "#pragma omp for", pragma_tok);
22412
22413 sb = begin_omp_structured_block ();
22414 save = cp_parser_begin_omp_structured_block (parser);
22415
22416 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22417
22418 cp_parser_end_omp_structured_block (parser, save);
22419 add_stmt (finish_omp_structured_block (sb));
22420
22421 return ret;
22422 }
22423
22424 /* OpenMP 2.5:
22425 # pragma omp master new-line
22426 structured-block */
22427
22428 static tree
22429 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22430 {
22431 cp_parser_require_pragma_eol (parser, pragma_tok);
22432 return c_finish_omp_master (input_location,
22433 cp_parser_omp_structured_block (parser));
22434 }
22435
22436 /* OpenMP 2.5:
22437 # pragma omp ordered new-line
22438 structured-block */
22439
22440 static tree
22441 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22442 {
22443 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22444 cp_parser_require_pragma_eol (parser, pragma_tok);
22445 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22446 }
22447
22448 /* OpenMP 2.5:
22449
22450 section-scope:
22451 { section-sequence }
22452
22453 section-sequence:
22454 section-directive[opt] structured-block
22455 section-sequence section-directive structured-block */
22456
22457 static tree
22458 cp_parser_omp_sections_scope (cp_parser *parser)
22459 {
22460 tree stmt, substmt;
22461 bool error_suppress = false;
22462 cp_token *tok;
22463
22464 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22465 return NULL_TREE;
22466
22467 stmt = push_stmt_list ();
22468
22469 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22470 {
22471 unsigned save;
22472
22473 substmt = begin_omp_structured_block ();
22474 save = cp_parser_begin_omp_structured_block (parser);
22475
22476 while (1)
22477 {
22478 cp_parser_statement (parser, NULL_TREE, false, NULL);
22479
22480 tok = cp_lexer_peek_token (parser->lexer);
22481 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22482 break;
22483 if (tok->type == CPP_CLOSE_BRACE)
22484 break;
22485 if (tok->type == CPP_EOF)
22486 break;
22487 }
22488
22489 cp_parser_end_omp_structured_block (parser, save);
22490 substmt = finish_omp_structured_block (substmt);
22491 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22492 add_stmt (substmt);
22493 }
22494
22495 while (1)
22496 {
22497 tok = cp_lexer_peek_token (parser->lexer);
22498 if (tok->type == CPP_CLOSE_BRACE)
22499 break;
22500 if (tok->type == CPP_EOF)
22501 break;
22502
22503 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22504 {
22505 cp_lexer_consume_token (parser->lexer);
22506 cp_parser_require_pragma_eol (parser, tok);
22507 error_suppress = false;
22508 }
22509 else if (!error_suppress)
22510 {
22511 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22512 error_suppress = true;
22513 }
22514
22515 substmt = cp_parser_omp_structured_block (parser);
22516 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22517 add_stmt (substmt);
22518 }
22519 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22520
22521 substmt = pop_stmt_list (stmt);
22522
22523 stmt = make_node (OMP_SECTIONS);
22524 TREE_TYPE (stmt) = void_type_node;
22525 OMP_SECTIONS_BODY (stmt) = substmt;
22526
22527 add_stmt (stmt);
22528 return stmt;
22529 }
22530
22531 /* OpenMP 2.5:
22532 # pragma omp sections sections-clause[optseq] newline
22533 sections-scope */
22534
22535 #define OMP_SECTIONS_CLAUSE_MASK \
22536 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22537 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22538 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22539 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22540 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22541
22542 static tree
22543 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22544 {
22545 tree clauses, ret;
22546
22547 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22548 "#pragma omp sections", pragma_tok);
22549
22550 ret = cp_parser_omp_sections_scope (parser);
22551 if (ret)
22552 OMP_SECTIONS_CLAUSES (ret) = clauses;
22553
22554 return ret;
22555 }
22556
22557 /* OpenMP 2.5:
22558 # pragma parallel parallel-clause new-line
22559 # pragma parallel for parallel-for-clause new-line
22560 # pragma parallel sections parallel-sections-clause new-line */
22561
22562 #define OMP_PARALLEL_CLAUSE_MASK \
22563 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22564 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22565 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22566 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22567 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22568 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22569 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22570 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22571
22572 static tree
22573 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22574 {
22575 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22576 const char *p_name = "#pragma omp parallel";
22577 tree stmt, clauses, par_clause, ws_clause, block;
22578 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22579 unsigned int save;
22580 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22581
22582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22583 {
22584 cp_lexer_consume_token (parser->lexer);
22585 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22586 p_name = "#pragma omp parallel for";
22587 mask |= OMP_FOR_CLAUSE_MASK;
22588 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22589 }
22590 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22591 {
22592 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22593 const char *p = IDENTIFIER_POINTER (id);
22594 if (strcmp (p, "sections") == 0)
22595 {
22596 cp_lexer_consume_token (parser->lexer);
22597 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22598 p_name = "#pragma omp parallel sections";
22599 mask |= OMP_SECTIONS_CLAUSE_MASK;
22600 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22601 }
22602 }
22603
22604 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22605 block = begin_omp_parallel ();
22606 save = cp_parser_begin_omp_structured_block (parser);
22607
22608 switch (p_kind)
22609 {
22610 case PRAGMA_OMP_PARALLEL:
22611 cp_parser_statement (parser, NULL_TREE, false, NULL);
22612 par_clause = clauses;
22613 break;
22614
22615 case PRAGMA_OMP_PARALLEL_FOR:
22616 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22617 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22618 break;
22619
22620 case PRAGMA_OMP_PARALLEL_SECTIONS:
22621 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22622 stmt = cp_parser_omp_sections_scope (parser);
22623 if (stmt)
22624 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22625 break;
22626
22627 default:
22628 gcc_unreachable ();
22629 }
22630
22631 cp_parser_end_omp_structured_block (parser, save);
22632 stmt = finish_omp_parallel (par_clause, block);
22633 if (p_kind != PRAGMA_OMP_PARALLEL)
22634 OMP_PARALLEL_COMBINED (stmt) = 1;
22635 return stmt;
22636 }
22637
22638 /* OpenMP 2.5:
22639 # pragma omp single single-clause[optseq] new-line
22640 structured-block */
22641
22642 #define OMP_SINGLE_CLAUSE_MASK \
22643 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22644 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22645 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22646 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22647
22648 static tree
22649 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22650 {
22651 tree stmt = make_node (OMP_SINGLE);
22652 TREE_TYPE (stmt) = void_type_node;
22653
22654 OMP_SINGLE_CLAUSES (stmt)
22655 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22656 "#pragma omp single", pragma_tok);
22657 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22658
22659 return add_stmt (stmt);
22660 }
22661
22662 /* OpenMP 3.0:
22663 # pragma omp task task-clause[optseq] new-line
22664 structured-block */
22665
22666 #define OMP_TASK_CLAUSE_MASK \
22667 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22668 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22669 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22670 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22671 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22672 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22673
22674 static tree
22675 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22676 {
22677 tree clauses, block;
22678 unsigned int save;
22679
22680 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22681 "#pragma omp task", pragma_tok);
22682 block = begin_omp_task ();
22683 save = cp_parser_begin_omp_structured_block (parser);
22684 cp_parser_statement (parser, NULL_TREE, false, NULL);
22685 cp_parser_end_omp_structured_block (parser, save);
22686 return finish_omp_task (clauses, block);
22687 }
22688
22689 /* OpenMP 3.0:
22690 # pragma omp taskwait new-line */
22691
22692 static void
22693 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22694 {
22695 cp_parser_require_pragma_eol (parser, pragma_tok);
22696 finish_omp_taskwait ();
22697 }
22698
22699 /* OpenMP 2.5:
22700 # pragma omp threadprivate (variable-list) */
22701
22702 static void
22703 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22704 {
22705 tree vars;
22706
22707 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22708 cp_parser_require_pragma_eol (parser, pragma_tok);
22709
22710 finish_omp_threadprivate (vars);
22711 }
22712
22713 /* Main entry point to OpenMP statement pragmas. */
22714
22715 static void
22716 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22717 {
22718 tree stmt;
22719
22720 switch (pragma_tok->pragma_kind)
22721 {
22722 case PRAGMA_OMP_ATOMIC:
22723 cp_parser_omp_atomic (parser, pragma_tok);
22724 return;
22725 case PRAGMA_OMP_CRITICAL:
22726 stmt = cp_parser_omp_critical (parser, pragma_tok);
22727 break;
22728 case PRAGMA_OMP_FOR:
22729 stmt = cp_parser_omp_for (parser, pragma_tok);
22730 break;
22731 case PRAGMA_OMP_MASTER:
22732 stmt = cp_parser_omp_master (parser, pragma_tok);
22733 break;
22734 case PRAGMA_OMP_ORDERED:
22735 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22736 break;
22737 case PRAGMA_OMP_PARALLEL:
22738 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22739 break;
22740 case PRAGMA_OMP_SECTIONS:
22741 stmt = cp_parser_omp_sections (parser, pragma_tok);
22742 break;
22743 case PRAGMA_OMP_SINGLE:
22744 stmt = cp_parser_omp_single (parser, pragma_tok);
22745 break;
22746 case PRAGMA_OMP_TASK:
22747 stmt = cp_parser_omp_task (parser, pragma_tok);
22748 break;
22749 default:
22750 gcc_unreachable ();
22751 }
22752
22753 if (stmt)
22754 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22755 }
22756 \f
22757 /* The parser. */
22758
22759 static GTY (()) cp_parser *the_parser;
22760
22761 \f
22762 /* Special handling for the first token or line in the file. The first
22763 thing in the file might be #pragma GCC pch_preprocess, which loads a
22764 PCH file, which is a GC collection point. So we need to handle this
22765 first pragma without benefit of an existing lexer structure.
22766
22767 Always returns one token to the caller in *FIRST_TOKEN. This is
22768 either the true first token of the file, or the first token after
22769 the initial pragma. */
22770
22771 static void
22772 cp_parser_initial_pragma (cp_token *first_token)
22773 {
22774 tree name = NULL;
22775
22776 cp_lexer_get_preprocessor_token (NULL, first_token);
22777 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22778 return;
22779
22780 cp_lexer_get_preprocessor_token (NULL, first_token);
22781 if (first_token->type == CPP_STRING)
22782 {
22783 name = first_token->u.value;
22784
22785 cp_lexer_get_preprocessor_token (NULL, first_token);
22786 if (first_token->type != CPP_PRAGMA_EOL)
22787 error_at (first_token->location,
22788 "junk at end of %<#pragma GCC pch_preprocess%>");
22789 }
22790 else
22791 error_at (first_token->location, "expected string literal");
22792
22793 /* Skip to the end of the pragma. */
22794 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22795 cp_lexer_get_preprocessor_token (NULL, first_token);
22796
22797 /* Now actually load the PCH file. */
22798 if (name)
22799 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22800
22801 /* Read one more token to return to our caller. We have to do this
22802 after reading the PCH file in, since its pointers have to be
22803 live. */
22804 cp_lexer_get_preprocessor_token (NULL, first_token);
22805 }
22806
22807 /* Normal parsing of a pragma token. Here we can (and must) use the
22808 regular lexer. */
22809
22810 static bool
22811 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22812 {
22813 cp_token *pragma_tok;
22814 unsigned int id;
22815
22816 pragma_tok = cp_lexer_consume_token (parser->lexer);
22817 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22818 parser->lexer->in_pragma = true;
22819
22820 id = pragma_tok->pragma_kind;
22821 switch (id)
22822 {
22823 case PRAGMA_GCC_PCH_PREPROCESS:
22824 error_at (pragma_tok->location,
22825 "%<#pragma GCC pch_preprocess%> must be first");
22826 break;
22827
22828 case PRAGMA_OMP_BARRIER:
22829 switch (context)
22830 {
22831 case pragma_compound:
22832 cp_parser_omp_barrier (parser, pragma_tok);
22833 return false;
22834 case pragma_stmt:
22835 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22836 "used in compound statements");
22837 break;
22838 default:
22839 goto bad_stmt;
22840 }
22841 break;
22842
22843 case PRAGMA_OMP_FLUSH:
22844 switch (context)
22845 {
22846 case pragma_compound:
22847 cp_parser_omp_flush (parser, pragma_tok);
22848 return false;
22849 case pragma_stmt:
22850 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22851 "used in compound statements");
22852 break;
22853 default:
22854 goto bad_stmt;
22855 }
22856 break;
22857
22858 case PRAGMA_OMP_TASKWAIT:
22859 switch (context)
22860 {
22861 case pragma_compound:
22862 cp_parser_omp_taskwait (parser, pragma_tok);
22863 return false;
22864 case pragma_stmt:
22865 error_at (pragma_tok->location,
22866 "%<#pragma omp taskwait%> may only be "
22867 "used in compound statements");
22868 break;
22869 default:
22870 goto bad_stmt;
22871 }
22872 break;
22873
22874 case PRAGMA_OMP_THREADPRIVATE:
22875 cp_parser_omp_threadprivate (parser, pragma_tok);
22876 return false;
22877
22878 case PRAGMA_OMP_ATOMIC:
22879 case PRAGMA_OMP_CRITICAL:
22880 case PRAGMA_OMP_FOR:
22881 case PRAGMA_OMP_MASTER:
22882 case PRAGMA_OMP_ORDERED:
22883 case PRAGMA_OMP_PARALLEL:
22884 case PRAGMA_OMP_SECTIONS:
22885 case PRAGMA_OMP_SINGLE:
22886 case PRAGMA_OMP_TASK:
22887 if (context == pragma_external)
22888 goto bad_stmt;
22889 cp_parser_omp_construct (parser, pragma_tok);
22890 return true;
22891
22892 case PRAGMA_OMP_SECTION:
22893 error_at (pragma_tok->location,
22894 "%<#pragma omp section%> may only be used in "
22895 "%<#pragma omp sections%> construct");
22896 break;
22897
22898 default:
22899 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22900 c_invoke_pragma_handler (id);
22901 break;
22902
22903 bad_stmt:
22904 cp_parser_error (parser, "expected declaration specifiers");
22905 break;
22906 }
22907
22908 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22909 return false;
22910 }
22911
22912 /* The interface the pragma parsers have to the lexer. */
22913
22914 enum cpp_ttype
22915 pragma_lex (tree *value)
22916 {
22917 cp_token *tok;
22918 enum cpp_ttype ret;
22919
22920 tok = cp_lexer_peek_token (the_parser->lexer);
22921
22922 ret = tok->type;
22923 *value = tok->u.value;
22924
22925 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22926 ret = CPP_EOF;
22927 else if (ret == CPP_STRING)
22928 *value = cp_parser_string_literal (the_parser, false, false);
22929 else
22930 {
22931 cp_lexer_consume_token (the_parser->lexer);
22932 if (ret == CPP_KEYWORD)
22933 ret = CPP_NAME;
22934 }
22935
22936 return ret;
22937 }
22938
22939 \f
22940 /* External interface. */
22941
22942 /* Parse one entire translation unit. */
22943
22944 void
22945 c_parse_file (void)
22946 {
22947 bool error_occurred;
22948 static bool already_called = false;
22949
22950 if (already_called)
22951 {
22952 sorry ("inter-module optimizations not implemented for C++");
22953 return;
22954 }
22955 already_called = true;
22956
22957 the_parser = cp_parser_new ();
22958 push_deferring_access_checks (flag_access_control
22959 ? dk_no_deferred : dk_no_check);
22960 error_occurred = cp_parser_translation_unit (the_parser);
22961 the_parser = NULL;
22962 }
22963
22964 #include "gt-cp-parser.h"