re PR c++/23287 (Explicitly invoking destructor of template class in a template and...
[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
41 \f
42 /* The lexer. */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
46
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
49
50 struct tree_check GTY(())
51 {
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 cp_token GTY (())
64 {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
88 location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99 0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
105 it is created. */
106
107 typedef struct cp_lexer GTY (())
108 {
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
113 buffer. */
114 size_t buffer_length;
115
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
119
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
123
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
127 tokens. */
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
132
133 /* True if we should output debugging information. */
134 bool debugging_p;
135
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
138 bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
145 a lexer. */
146
147 typedef struct cp_token_cache GTY(())
148 {
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
151
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes. */
157
158 static cp_lexer *cp_lexer_new_main
159 (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163 (cp_lexer *);
164 static int cp_lexer_saving_tokens
165 (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167 (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173 (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183 (cp_lexer *);
184 static void cp_lexer_purge_token
185 (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189 (cp_lexer *);
190 static void cp_lexer_commit_tokens
191 (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193 (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198 (cp_lexer *);
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217 (cp_token *);
218
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables. */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
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 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
335
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
343
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
346 }
347
348 /* Frees all resources associated with LEXER. */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output. */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366 return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376 return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382 return pos;
383 }
384
385 /* nonzero if we are presently saving tokens. */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401 static int is_extern_c = 0;
402
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
409
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
416
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
419 {
420 if (C_IS_RESERVED_WORD (token->u.value))
421 {
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
431 }
432 else
433 {
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437 {
438 /* Warn about the C++0x keyword (but still treat it as
439 an identifier). */
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
443
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
447 }
448
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
451 }
452 }
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
455 {
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
458 {
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
468 }
469 }
470 else if (token->type == CPP_PRAGMA)
471 {
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
475 }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN. */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482 if (token->type != CPP_EOF)
483 {
484 input_location = token->location;
485 }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489 consume it. */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494 if (cp_lexer_debugging_p (lexer))
495 {
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
499 }
500 return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE. */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508 return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE. */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516 return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD. */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD. */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier. */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540 cp_token *token;
541
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
544 {
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
547 case RID_AUTO:
548 /* Storage classes. */
549 case RID_REGISTER:
550 case RID_STATIC:
551 case RID_EXTERN:
552 case RID_MUTABLE:
553 case RID_THREAD:
554 /* Elaborated type specifiers. */
555 case RID_ENUM:
556 case RID_CLASS:
557 case RID_STRUCT:
558 case RID_UNION:
559 case RID_TYPENAME:
560 /* Simple type specifiers. */
561 case RID_CHAR:
562 case RID_CHAR16:
563 case RID_CHAR32:
564 case RID_WCHAR:
565 case RID_BOOL:
566 case RID_SHORT:
567 case RID_INT:
568 case RID_LONG:
569 case RID_SIGNED:
570 case RID_UNSIGNED:
571 case RID_FLOAT:
572 case RID_DOUBLE:
573 case RID_VOID:
574 /* GNU extensions. */
575 case RID_ATTRIBUTE:
576 case RID_TYPEOF:
577 /* C++0x extensions. */
578 case RID_DECLTYPE:
579 return true;
580
581 default:
582 return false;
583 }
584 }
585
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595 cp_token *token;
596
597 /* N is 1-based, not zero-based. */
598 gcc_assert (n > 0);
599
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604 --n;
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
607 while (n != 0)
608 {
609 ++token;
610 if (token == lexer->last_token)
611 {
612 token = &eof_token;
613 break;
614 }
615
616 if (token->type != CPP_PURGED)
617 --n;
618 }
619
620 if (cp_lexer_debugging_p (lexer))
621 {
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
624 }
625
626 return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635 cp_token *token = lexer->next_token;
636
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640 do
641 {
642 lexer->next_token++;
643 if (lexer->next_token == lexer->last_token)
644 {
645 lexer->next_token = &eof_token;
646 break;
647 }
648
649 }
650 while (lexer->next_token->type == CPP_PURGED);
651
652 cp_lexer_set_source_position_from_token (token);
653
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
656 {
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
660 }
661
662 return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
667 token. */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672 cp_token *tok = lexer->next_token;
673
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
679
680 do
681 {
682 tok++;
683 if (tok == lexer->last_token)
684 {
685 tok = &eof_token;
686 break;
687 }
688 }
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700 cp_token *peek = lexer->next_token;
701
702 if (peek == &eof_token)
703 peek = lexer->last_token;
704
705 gcc_assert (tok < peek);
706
707 for ( tok += 1; tok != peek; tok += 1)
708 {
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
713 }
714 }
715
716 /* Begin saving tokens. All tokens consumed after this point will be
717 preserved. */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved. */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739 VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM. */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768 TTYPE_TABLE
769 #undef OP
770 #undef TK
771 /* C++ parser token types - see "Manifest constants", above. */
772 "KEYWORD",
773 "TEMPLATE_ID",
774 "NESTED_NAME_SPECIFIER",
775 "PURGED"
776 };
777
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
782
783 /* For some tokens, print the associated data. */
784 switch (token->type)
785 {
786 case CPP_KEYWORD:
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790 break;
791 /* else fall through */
792 case CPP_NAME:
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
794 break;
795
796 case CPP_STRING:
797 case CPP_STRING16:
798 case CPP_STRING32:
799 case CPP_WSTRING:
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801 break;
802
803 default:
804 break;
805 }
806 }
807
808 /* Start emitting debugging information. */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813 lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information. */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821 lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens. */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
833 cache->last = last;
834 return cache;
835 }
836
837 \f
838 /* Decl-specifiers. */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators. */
849
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872 allocated. */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool. */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880 return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889 cp_declarator *declarator;
890
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
896
897 return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
904 is, if any. */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
909 {
910 cp_declarator *declarator;
911
912 /* It is valid to write:
913
914 class C { void f(); };
915 typedef C D;
916 void D::f();
917
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
921 well. */
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
933
934 return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944 cp_declarator *declarator;
945
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
950 if (target)
951 {
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
954 }
955 else
956 declarator->parameter_pack_p = false;
957
958 return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references. */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965 bool rvalue_ref)
966 {
967 cp_declarator *declarator;
968
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
973 if (target)
974 {
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
977 }
978 else
979 declarator->parameter_pack_p = false;
980
981 return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
990 {
991 cp_declarator *declarator;
992
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
997
998 if (pointee)
999 {
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1002 }
1003 else
1004 declarator->parameter_pack_p = false;
1005
1006 return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016 tree parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification,
1019 tree late_return_type)
1020 {
1021 cp_declarator *declarator;
1022
1023 declarator = make_declarator (cdk_function);
1024 declarator->declarator = target;
1025 declarator->u.function.parameters = parms;
1026 declarator->u.function.qualifiers = cv_qualifiers;
1027 declarator->u.function.exception_specification = exception_specification;
1028 declarator->u.function.late_return_type = late_return_type;
1029 if (target)
1030 {
1031 declarator->parameter_pack_p = target->parameter_pack_p;
1032 target->parameter_pack_p = false;
1033 }
1034 else
1035 declarator->parameter_pack_p = false;
1036
1037 return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046 cp_declarator *declarator;
1047
1048 declarator = make_declarator (cdk_array);
1049 declarator->declarator = element;
1050 declarator->u.array.bounds = bounds;
1051 if (element)
1052 {
1053 declarator->parameter_pack_p = element->parameter_pack_p;
1054 element->parameter_pack_p = false;
1055 }
1056 else
1057 declarator->parameter_pack_p = false;
1058
1059 return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1064 static bool
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1071 bool found = false;
1072 while (declarator && !found)
1073 {
1074 switch ((int)declarator->kind)
1075 {
1076 case cdk_id:
1077 case cdk_array:
1078 found = true;
1079 break;
1080
1081 case cdk_error:
1082 return true;
1083
1084 default:
1085 declarator = declarator->declarator;
1086 break;
1087 }
1088 }
1089
1090 return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100 cp_declarator *declarator,
1101 tree default_argument)
1102 {
1103 cp_parameter_declarator *parameter;
1104
1105 parameter = ((cp_parameter_declarator *)
1106 alloc_declarator (sizeof (cp_parameter_declarator)));
1107 parameter->next = NULL;
1108 if (decl_specifiers)
1109 parameter->decl_specifiers = *decl_specifiers;
1110 else
1111 clear_decl_specs (&parameter->decl_specifiers);
1112 parameter->declarator = declarator;
1113 parameter->default_argument = default_argument;
1114 parameter->ellipsis_p = false;
1115
1116 return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124 while (declarator)
1125 {
1126 if (declarator->kind == cdk_function
1127 && declarator->declarator->kind == cdk_id)
1128 return true;
1129 if (declarator->kind == cdk_id
1130 || declarator->kind == cdk_error)
1131 return false;
1132 declarator = declarator->declarator;
1133 }
1134 return false;
1135 }
1136
1137 /* The parser. */
1138
1139 /* Overview
1140 --------
1141
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1149
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1152 code processed.
1153
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1162
1163 Methodology
1164 -----------
1165
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1182
1183 Future Improvements
1184 -------------------
1185
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1190 case. */
1191
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1194
1195 typedef enum cp_parser_flags
1196 {
1197 /* No flags. */
1198 CP_PARSER_FLAGS_NONE = 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse. */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1221
1222 enum cp_parser_prec
1223 {
1224 PREC_NOT_OPERATOR,
1225 PREC_LOGICAL_OR_EXPRESSION,
1226 PREC_LOGICAL_AND_EXPRESSION,
1227 PREC_INCLUSIVE_OR_EXPRESSION,
1228 PREC_EXCLUSIVE_OR_EXPRESSION,
1229 PREC_AND_EXPRESSION,
1230 PREC_EQUALITY_EXPRESSION,
1231 PREC_RELATIONAL_EXPRESSION,
1232 PREC_SHIFT_EXPRESSION,
1233 PREC_ADDITIVE_EXPRESSION,
1234 PREC_MULTIPLICATIVE_EXPRESSION,
1235 PREC_PM_EXPRESSION,
1236 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244 /* The token type. */
1245 enum cpp_ttype token_type;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse. */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR,
1260 /* We are committed to this tentative parse, whether or not an error
1261 has occurred. */
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267 /* Left hand side of the binary operation we are currently
1268 parsing. */
1269 tree lhs;
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type;
1275 /* Precedence of the binary operation we are parsing. */
1276 int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1281 increasing. */
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288 /* If this is a tentative parsing context, the status of the
1289 tentative parse. */
1290 enum cp_parser_status_kind status;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1295 tree object_type;
1296
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes. */
1302
1303 /* Constructors and destructors. */
1304
1305 static cp_parser_context *cp_parser_context_new
1306 (cp_parser_context *);
1307
1308 /* Class variables. */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1314 cp_parser_new. */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1351 for speed. */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors. */
1355
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362 cp_parser_context *context;
1363
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list != NULL)
1366 {
1367 /* Pull the first entry from the free list. */
1368 context = cp_parser_context_free_list;
1369 cp_parser_context_free_list = context->next;
1370 memset (context, 0, sizeof (*context));
1371 }
1372 else
1373 context = GGC_CNEW (cp_parser_context);
1374
1375 /* No errors have occurred yet in this context. */
1376 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1379 if (next)
1380 {
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context->object_type = next->object_type;
1384 /* Thread the stack. */
1385 context->next = next;
1386 }
1387
1388 return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser. */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395 /* The lexer from which we are obtaining tokens. */
1396 cp_lexer *lexer;
1397
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1411 tree scope;
1412
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1418 tree object_scope;
1419 tree qualifying_scope;
1420
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1423
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context *context;
1431
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p;
1435
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p;
1442
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p;
1447
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p;
1451
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p;
1457
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p;
1461
1462 /* TRUE if local variable names and `this' are forbidden in the
1463 current context. */
1464 bool local_variables_forbidden_p;
1465
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1468 declaration'. */
1469 bool in_unbraced_linkage_specification_p;
1470
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p;
1474
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p;
1477
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement;
1489
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p;
1495
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1498 alternatives. */
1499 bool in_type_id_in_expr_p;
1500
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c;
1504
1505 /* TRUE if strings in expressions should be translated to the execution
1506 character set. */
1507 bool translate_strings_p;
1508
1509 /* TRUE if we are presently parsing the body of a function, but not
1510 a local class. */
1511 bool in_function_body;
1512
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message;
1517
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1523 order.
1524
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1528 processing.
1529
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues;
1533
1534 /* The number of classes whose definitions are currently in
1535 progress. */
1536 unsigned num_classes_being_defined;
1537
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes. */
1544
1545 /* Constructors and destructors. */
1546
1547 static cp_parser *cp_parser_new
1548 (void);
1549
1550 /* Routines to parse various constructs.
1551
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1561
1562 /* Lexical conventions [gram.lex] */
1563
1564 static tree cp_parser_identifier
1565 (cp_parser *);
1566 static tree cp_parser_string_literal
1567 (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic] */
1570
1571 static bool cp_parser_translation_unit
1572 (cp_parser *);
1573
1574 /* Expressions [gram.expr] */
1575
1576 static tree cp_parser_primary_expression
1577 (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579 (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser *, bool, bool, bool, cp_id_kind *);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599 (cp_parser *, bool, bool, cp_id_kind *);
1600 static enum tree_code cp_parser_unary_operator
1601 (cp_token *);
1602 static tree cp_parser_new_expression
1603 (cp_parser *);
1604 static tree cp_parser_new_placement
1605 (cp_parser *);
1606 static tree cp_parser_new_type_id
1607 (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609 (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611 (cp_parser *);
1612 static tree cp_parser_new_initializer
1613 (cp_parser *);
1614 static tree cp_parser_delete_expression
1615 (cp_parser *);
1616 static tree cp_parser_cast_expression
1617 (cp_parser *, bool, bool, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625 (cp_parser *);
1626 static tree cp_parser_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static tree cp_parser_constant_expression
1629 (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631 (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt] */
1634
1635 static void cp_parser_statement
1636 (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638 (cp_parser *);
1639 static tree cp_parser_expression_statement
1640 (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642 (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646 (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648 (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650 (cp_parser *);
1651 static void cp_parser_for_init_statement
1652 (cp_parser *);
1653 static tree cp_parser_jump_statement
1654 (cp_parser *);
1655 static void cp_parser_declaration_statement
1656 (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661 (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666 (cp_parser *);
1667 static void cp_parser_declaration
1668 (cp_parser *);
1669 static void cp_parser_block_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676 (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681 int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685 (cp_parser *);
1686 static tree cp_parser_nonclass_name
1687 (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691 (cp_parser *);
1692 static void cp_parser_enumerator_list
1693 (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697 (cp_parser *);
1698 static void cp_parser_namespace_definition
1699 (cp_parser *);
1700 static void cp_parser_namespace_body
1701 (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703 (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705 (cp_parser *);
1706 static bool cp_parser_using_declaration
1707 (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709 (cp_parser *);
1710 static void cp_parser_asm_definition
1711 (cp_parser *);
1712 static void cp_parser_linkage_specification
1713 (cp_parser *);
1714 static void cp_parser_static_assert
1715 (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717 (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730 (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732 (cp_parser *);
1733 static tree cp_parser_declarator_id
1734 (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736 (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740 (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744 (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser *, bool);
1747 static void cp_parser_function_body
1748 (cp_parser *);
1749 static tree cp_parser_initializer
1750 (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756 (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759 (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766 (cp_parser *);
1767 static tree cp_parser_class_head
1768 (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770 (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772 (cp_parser *);
1773 static void cp_parser_member_declaration
1774 (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776 (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778 (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783 (cp_parser *);
1784 static tree cp_parser_base_specifier
1785 (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790 (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792 (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794 (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796 (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798 (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800 (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802 (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807 (cp_parser *);
1808 static tree cp_parser_operator
1809 (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814 (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816 (cp_parser *);
1817 static tree cp_parser_template_parameter
1818 (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826 (cp_parser *);
1827 static tree cp_parser_template_argument
1828 (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830 (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832 (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837 (cp_parser *);
1838 static bool cp_parser_function_try_block
1839 (cp_parser *);
1840 static void cp_parser_handler_seq
1841 (cp_parser *);
1842 static void cp_parser_handler
1843 (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845 (cp_parser *);
1846 static tree cp_parser_throw_expression
1847 (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849 (cp_parser *);
1850 static tree cp_parser_type_id_list
1851 (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856 (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858 (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860 (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862 (cp_parser *);
1863 static tree cp_parser_attribute_list
1864 (cp_parser *);
1865 static bool cp_parser_extension_opt
1866 (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868 (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872 (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877 (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879 (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881 (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883 (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885 (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887 (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889 (cp_parser *);
1890 static tree cp_parser_objc_expression
1891 (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893 (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895 (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897 (cp_parser *);
1898 static void cp_parser_objc_declaration
1899 (cp_parser *);
1900 static tree cp_parser_objc_statement
1901 (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910 (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916 (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936 (cp_parser *);
1937 static void cp_parser_save_default_args
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946 (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948 (cp_parser *);
1949 static void cp_parser_set_storage_class
1950 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956 (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958 (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960 (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962 (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964 (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968 (cp_token *);
1969 static void cp_parser_check_class_key
1970 (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974 (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976 (cp_parser *);
1977 static bool cp_parser_cache_group
1978 (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980 (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982 (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984 (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986 (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988 (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990 (cp_parser *);
1991 static void cp_parser_error
1992 (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996 (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998 (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008 (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012 (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014 (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016 (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018 (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020 (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024 (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026 (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028 (cp_token *);
2029 static bool cp_parser_is_keyword
2030 (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively. */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041 return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal. */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049 return (token->type == CPP_STRING ||
2050 token->type == CPP_STRING16 ||
2051 token->type == CPP_STRING32 ||
2052 token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060 return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2067 OTHER-TOKEN". */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072 if (!cp_parser_simulate_error (parser))
2073 {
2074 cp_token *token = cp_lexer_peek_token (parser->lexer);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token);
2078
2079 if (token->type == CPP_PRAGMA)
2080 {
2081 error ("%H%<#pragma%> is not allowed here", &token->location);
2082 cp_parser_skip_to_pragma_eol (parser, token);
2083 return;
2084 }
2085
2086 c_parse_error (message,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2089 identifiers. */
2090 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091 token->u.value);
2092 }
2093 }
2094
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102 tree name,
2103 tree decl,
2104 const char* desired,
2105 location_t location)
2106 {
2107 /* If name lookup completely failed, tell the user that NAME was not
2108 declared. */
2109 if (decl == error_mark_node)
2110 {
2111 if (parser->scope && parser->scope != global_namespace)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location, parser->scope, name);
2114 else if (parser->scope == global_namespace)
2115 error ("%H%<::%E%> has not been declared", &location, name);
2116 else if (parser->object_scope
2117 && !CLASS_TYPE_P (parser->object_scope))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location, name, parser->object_scope);
2120 else if (parser->object_scope)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location, parser->object_scope, name);
2123 else
2124 error ("%H%qE has not been declared", &location, name);
2125 }
2126 else if (parser->scope && parser->scope != global_namespace)
2127 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128 else if (parser->scope == global_namespace)
2129 error ("%H%<::%E%> %s", &location, name, desired);
2130 else
2131 error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142 {
2143 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144 return true;
2145 }
2146 return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers. */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153 location_t location)
2154 {
2155 cp_decl_spec ds;
2156
2157 for (ds = ds_first; ds != ds_last; ++ds)
2158 {
2159 unsigned count = decl_specs->specs[(int)ds];
2160 if (count < 2)
2161 continue;
2162 /* The "long" specifier is a special case because of "long long". */
2163 if (ds == ds_long)
2164 {
2165 if (count > 2)
2166 error ("%H%<long long long%> is too long for GCC", &location);
2167 else if (pedantic && !in_system_header && warn_long_long
2168 && cxx_dialect == cxx98)
2169 pedwarn (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2171 }
2172 else if (count > 1)
2173 {
2174 static const char *const decl_spec_names[] = {
2175 "signed",
2176 "unsigned",
2177 "short",
2178 "long",
2179 "const",
2180 "volatile",
2181 "restrict",
2182 "inline",
2183 "virtual",
2184 "explicit",
2185 "friend",
2186 "typedef",
2187 "__complex",
2188 "__thread"
2189 };
2190 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191 }
2192 }
2193 }
2194
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2197 issued. */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2204 {
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2208 return false;
2209 }
2210 return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2222 {
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2226 while (declarator
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2231 if (declarator
2232 && declarator->kind == cdk_function)
2233 {
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2237 type);
2238 }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2250 {
2251 cp_token_position start = 0;
2252
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254 {
2255 if (TYPE_P (type))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2259 else
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2270 if (start)
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2272 }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2282 const char *thing)
2283 {
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2286 {
2287 if (!parser->allow_non_integral_constant_expression_p)
2288 {
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2293 NULL);
2294 error (message);
2295 free (message);
2296 return true;
2297 }
2298 }
2299 return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2312 {
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2323 &location, decl);
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2329 &location);
2330 else if (!parser->scope)
2331 {
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2336
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2339
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2343 {
2344 tree b;
2345
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347 b;
2348 b = TREE_CHAIN (b))
2349 {
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2353 {
2354 tree field;
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2360 field;
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2364 {
2365 inform (location,
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2368 break;
2369 }
2370 if (field)
2371 break;
2372 }
2373 }
2374 }
2375 }
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2379 {
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2386 else
2387 gcc_unreachable ();
2388 }
2389 cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2395
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405 tree id;
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2422 {
2423 cp_parser_abort_tentative_parse (parser);
2424 return false;
2425 }
2426 if (!cp_parser_parse_definitely (parser))
2427 return false;
2428
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2435 return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445 bool recovering,
2446 bool or_comma,
2447 bool consume_paren)
2448 {
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2451
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454 return 0;
2455
2456 while (true)
2457 {
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460 switch (token->type)
2461 {
2462 case CPP_EOF:
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2465 return 0;
2466
2467 case CPP_SEMICOLON:
2468 /* This matches the processing in skip_to_end_of_statement. */
2469 if (!brace_depth)
2470 return 0;
2471 break;
2472
2473 case CPP_OPEN_BRACE:
2474 ++brace_depth;
2475 break;
2476 case CPP_CLOSE_BRACE:
2477 if (!brace_depth--)
2478 return 0;
2479 break;
2480
2481 case CPP_COMMA:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2483 return -1;
2484 break;
2485
2486 case CPP_OPEN_PAREN:
2487 if (!brace_depth)
2488 ++paren_depth;
2489 break;
2490
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2493 {
2494 if (consume_paren)
2495 cp_lexer_consume_token (parser->lexer);
2496 return 1;
2497 }
2498 break;
2499
2500 default:
2501 break;
2502 }
2503
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2506 }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516 unsigned nesting_depth = 0;
2517
2518 while (true)
2519 {
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522 switch (token->type)
2523 {
2524 case CPP_EOF:
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2527 return;
2528
2529 case CPP_SEMICOLON:
2530 /* If the next token is a `;', we have reached the end of the
2531 statement. */
2532 if (!nesting_depth)
2533 return;
2534 break;
2535
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2539
2540 { 3 + }
2541
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2545 return;
2546
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2549 That way given:
2550
2551 void f g () { ... }
2552 typedef int I;
2553
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2556 declaration. */
2557 if (--nesting_depth == 0)
2558 {
2559 cp_lexer_consume_token (parser->lexer);
2560 return;
2561 }
2562
2563 case CPP_OPEN_BRACE:
2564 ++nesting_depth;
2565 break;
2566
2567 default:
2568 break;
2569 }
2570
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2573 }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585 {
2586 /* If there is additional (erroneous) input, skip to the end of
2587 the statement. */
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2592 }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601 int nesting_depth = 0;
2602
2603 while (nesting_depth >= 0)
2604 {
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607 switch (token->type)
2608 {
2609 case CPP_EOF:
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2612 return;
2613
2614 case CPP_SEMICOLON:
2615 /* Stop if this is an unnested ';'. */
2616 if (!nesting_depth)
2617 nesting_depth = -1;
2618 break;
2619
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2622 nesting level. */
2623 nesting_depth--;
2624 if (!nesting_depth)
2625 nesting_depth = -1;
2626 break;
2627
2628 case CPP_OPEN_BRACE:
2629 /* Nest. */
2630 nesting_depth++;
2631 break;
2632
2633 default:
2634 break;
2635 }
2636
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2639 }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2644 false otherwise. */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649 unsigned nesting_depth = 0;
2650
2651 while (true)
2652 {
2653 cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655 switch (token->type)
2656 {
2657 case CPP_EOF:
2658 case CPP_PRAGMA_EOL:
2659 /* If we've run out of tokens, stop. */
2660 return false;
2661
2662 case CPP_CLOSE_BRACE:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth-- == 0)
2666 return true;
2667 break;
2668
2669 case CPP_OPEN_BRACE:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2672 ++nesting_depth;
2673 break;
2674
2675 default:
2676 break;
2677 }
2678
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser->lexer);
2681 }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2686 sequence. */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691 cp_token *token;
2692
2693 parser->lexer->in_pragma = false;
2694
2695 do
2696 token = cp_lexer_consume_token (parser->lexer);
2697 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709 parser->lexer->in_pragma = false;
2710 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720 tree id, location_t id_location)
2721 {
2722 tree result;
2723 if (TREE_CODE (id) == IDENTIFIER_NODE)
2724 {
2725 result = make_typename_type (scope, id, typename_type,
2726 /*complain=*/tf_none);
2727 if (result == error_mark_node)
2728 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729 return result;
2730 }
2731 return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741 cp_cv_quals cv_qualifiers,
2742 cp_declarator *target)
2743 {
2744 if (code == ERROR_MARK)
2745 return cp_error_declarator;
2746
2747 if (code == INDIRECT_REF)
2748 if (class_type == NULL_TREE)
2749 return make_pointer_declarator (cv_qualifiers, target);
2750 else
2751 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, false);
2754 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, true);
2756 gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser. */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764 cp_parser *parser;
2765 cp_lexer *lexer;
2766 unsigned i;
2767
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer = cp_lexer_new_main ();
2771
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775 binops_by_token[binops[i].token_type] = binops[i];
2776
2777 parser = GGC_CNEW (cp_parser);
2778 parser->lexer = lexer;
2779 parser->context = cp_parser_context_new (NULL);
2780
2781 /* For now, we always accept GNU extensions. */
2782 parser->allow_gnu_extensions_p = 1;
2783
2784 /* The `>' token is a greater-than operator, not the end of a
2785 template-id. */
2786 parser->greater_than_is_operator_p = true;
2787
2788 parser->default_arg_ok_p = true;
2789
2790 /* We are not parsing a constant-expression. */
2791 parser->integral_constant_expression_p = false;
2792 parser->allow_non_integral_constant_expression_p = false;
2793 parser->non_integral_constant_expression_p = false;
2794
2795 /* Local variable names are not forbidden. */
2796 parser->local_variables_forbidden_p = false;
2797
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser->in_unbraced_linkage_specification_p = false;
2800
2801 /* We are not processing a declarator. */
2802 parser->in_declarator_p = false;
2803
2804 /* We are not processing a template-argument-list. */
2805 parser->in_template_argument_list_p = false;
2806
2807 /* We are not in an iteration statement. */
2808 parser->in_statement = 0;
2809
2810 /* We are not in a switch statement. */
2811 parser->in_switch_statement_p = false;
2812
2813 /* We are not parsing a type-id inside an expression. */
2814 parser->in_type_id_in_expr_p = false;
2815
2816 /* Declarations aren't implicitly extern "C". */
2817 parser->implicit_extern_c = false;
2818
2819 /* String literals should be translated to the execution character set. */
2820 parser->translate_strings_p = true;
2821
2822 /* We are not parsing a function body. */
2823 parser->in_function_body = false;
2824
2825 /* The unparsed function queue is empty. */
2826 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828 /* There are no classes being defined. */
2829 parser->num_classes_being_defined = 0;
2830
2831 /* No template parameters apply. */
2832 parser->num_template_parameter_lists = 0;
2833
2834 return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845 lexer->next = parser->lexer;
2846 parser->lexer = lexer;
2847
2848 /* Move the current source position to that of the first token in the
2849 new lexer. */
2850 cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858 cp_lexer *lexer = parser->lexer;
2859 parser->lexer = lexer->next;
2860 cp_lexer_destroy (lexer);
2861
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex] */
2868
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2870 identifier. */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875 cp_token *token;
2876
2877 /* Look for the identifier. */
2878 token = cp_parser_require (parser, CPP_NAME, "identifier");
2879 /* Return the value. */
2880 return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2887 invalid here.
2888
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2893
2894 This code is largely lifted from lex_string() in c-lex.c.
2895
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900 tree value;
2901 size_t count;
2902 struct obstack str_ob;
2903 cpp_string str, istr, *strs;
2904 cp_token *tok;
2905 enum cpp_ttype type;
2906
2907 tok = cp_lexer_peek_token (parser->lexer);
2908 if (!cp_parser_is_string_literal (tok))
2909 {
2910 cp_parser_error (parser, "expected string-literal");
2911 return error_mark_node;
2912 }
2913
2914 type = tok->type;
2915
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920 {
2921 cp_lexer_consume_token (parser->lexer);
2922
2923 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924 str.len = TREE_STRING_LENGTH (tok->u.value);
2925 count = 1;
2926
2927 strs = &str;
2928 }
2929 else
2930 {
2931 gcc_obstack_init (&str_ob);
2932 count = 0;
2933
2934 do
2935 {
2936 cp_lexer_consume_token (parser->lexer);
2937 count++;
2938 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939 str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941 if (type != tok->type)
2942 {
2943 if (type == CPP_STRING)
2944 type = tok->type;
2945 else if (tok->type != CPP_STRING)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok->location);
2948 }
2949
2950 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952 tok = cp_lexer_peek_token (parser->lexer);
2953 }
2954 while (cp_parser_is_string_literal (tok));
2955
2956 strs = (cpp_string *) obstack_finish (&str_ob);
2957 }
2958
2959 if (type != CPP_STRING && !wide_ok)
2960 {
2961 cp_parser_error (parser, "a wide string is invalid in this context");
2962 type = CPP_STRING;
2963 }
2964
2965 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966 (parse_in, strs, count, &istr, type))
2967 {
2968 value = build_string (istr.len, (const char *)istr.text);
2969 free (CONST_CAST (unsigned char *, istr.text));
2970
2971 switch (type)
2972 {
2973 default:
2974 case CPP_STRING:
2975 TREE_TYPE (value) = char_array_type_node;
2976 break;
2977 case CPP_STRING16:
2978 TREE_TYPE (value) = char16_array_type_node;
2979 break;
2980 case CPP_STRING32:
2981 TREE_TYPE (value) = char32_array_type_node;
2982 break;
2983 case CPP_WSTRING:
2984 TREE_TYPE (value) = wchar_array_type_node;
2985 break;
2986 }
2987
2988 value = fix_string_type (value);
2989 }
2990 else
2991 /* cpp_interpret_string has issued an error. */
2992 value = error_mark_node;
2993
2994 if (count > 1)
2995 obstack_free (&str_ob, 0);
2996
2997 return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic] */
3002
3003 /* Parse a translation-unit.
3004
3005 translation-unit:
3006 declaration-seq [opt]
3007
3008 Returns TRUE if all went well. */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013 /* The address of the first non-permanent object on the declarator
3014 obstack. */
3015 static void *declarator_obstack_base;
3016
3017 bool success;
3018
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator)
3021 {
3022 gcc_obstack_init (&declarator_obstack);
3023 /* Create the error declarator. */
3024 cp_error_declarator = make_declarator (cdk_error);
3025 /* Create the empty parameter list. */
3026 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029 }
3030
3031 cp_parser_declaration_seq_opt (parser);
3032
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035 {
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser->lexer);
3038 parser->lexer = NULL;
3039
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser->implicit_extern_c)
3043 {
3044 pop_lang_context ();
3045 parser->implicit_extern_c = false;
3046 }
3047
3048 /* Finish up. */
3049 finish_translation_unit ();
3050
3051 success = true;
3052 }
3053 else
3054 {
3055 cp_parser_error (parser, "expected declaration");
3056 success = false;
3057 }
3058
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack)
3061 == declarator_obstack_base);
3062
3063 /* All went well. */
3064 return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071 primary-expression:
3072 literal
3073 this
3074 ( expression )
3075 id-expression
3076
3077 GNU Extensions:
3078
3079 primary-expression:
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3083
3084 C++ Extensions:
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3102
3103 Objective-C++ Extension:
3104
3105 primary-expression:
3106 objc-expression
3107
3108 literal:
3109 __null
3110
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3115
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121 bool address_p,
3122 bool cast_p,
3123 bool template_arg_p,
3124 cp_id_kind *idk)
3125 {
3126 cp_token *token = NULL;
3127
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk = CP_ID_KIND_NONE;
3130
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133 switch (token->type)
3134 {
3135 /* literal:
3136 integer-literal
3137 character-literal
3138 floating-literal
3139 string-literal
3140 boolean-literal */
3141 case CPP_CHAR:
3142 case CPP_CHAR16:
3143 case CPP_CHAR32:
3144 case CPP_WCHAR:
3145 case CPP_NUMBER:
3146 token = cp_lexer_consume_token (parser->lexer);
3147 /* Floating-point literals are only allowed in an integral
3148 constant expression if they are cast to an integral or
3149 enumeration type. */
3150 if (TREE_CODE (token->u.value) == REAL_CST
3151 && parser->integral_constant_expression_p
3152 && pedantic)
3153 {
3154 /* CAST_P will be set even in invalid code like "int(2.7 +
3155 ...)". Therefore, we have to check that the next token
3156 is sure to end the cast. */
3157 if (cast_p)
3158 {
3159 cp_token *next_token;
3160
3161 next_token = cp_lexer_peek_token (parser->lexer);
3162 if (/* The comma at the end of an
3163 enumerator-definition. */
3164 next_token->type != CPP_COMMA
3165 /* The curly brace at the end of an enum-specifier. */
3166 && next_token->type != CPP_CLOSE_BRACE
3167 /* The end of a statement. */
3168 && next_token->type != CPP_SEMICOLON
3169 /* The end of the cast-expression. */
3170 && next_token->type != CPP_CLOSE_PAREN
3171 /* The end of an array bound. */
3172 && next_token->type != CPP_CLOSE_SQUARE
3173 /* The closing ">" in a template-argument-list. */
3174 && (next_token->type != CPP_GREATER
3175 || parser->greater_than_is_operator_p)
3176 /* C++0x only: A ">>" treated like two ">" tokens,
3177 in a template-argument-list. */
3178 && (next_token->type != CPP_RSHIFT
3179 || (cxx_dialect == cxx98)
3180 || parser->greater_than_is_operator_p))
3181 cast_p = false;
3182 }
3183
3184 /* If we are within a cast, then the constraint that the
3185 cast is to an integral or enumeration type will be
3186 checked at that point. If we are not within a cast, then
3187 this code is invalid. */
3188 if (!cast_p)
3189 cp_parser_non_integral_constant_expression
3190 (parser, "floating-point literal");
3191 }
3192 return token->u.value;
3193
3194 case CPP_STRING:
3195 case CPP_STRING16:
3196 case CPP_STRING32:
3197 case CPP_WSTRING:
3198 /* ??? Should wide strings be allowed when parser->translate_strings_p
3199 is false (i.e. in attributes)? If not, we can kill the third
3200 argument to cp_parser_string_literal. */
3201 return cp_parser_string_literal (parser,
3202 parser->translate_strings_p,
3203 true);
3204
3205 case CPP_OPEN_PAREN:
3206 {
3207 tree expr;
3208 bool saved_greater_than_is_operator_p;
3209
3210 /* Consume the `('. */
3211 cp_lexer_consume_token (parser->lexer);
3212 /* Within a parenthesized expression, a `>' token is always
3213 the greater-than operator. */
3214 saved_greater_than_is_operator_p
3215 = parser->greater_than_is_operator_p;
3216 parser->greater_than_is_operator_p = true;
3217 /* If we see `( { ' then we are looking at the beginning of
3218 a GNU statement-expression. */
3219 if (cp_parser_allow_gnu_extensions_p (parser)
3220 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221 {
3222 /* Statement-expressions are not allowed by the standard. */
3223 pedwarn (token->location, OPT_pedantic,
3224 "ISO C++ forbids braced-groups within expressions");
3225
3226 /* And they're not allowed outside of a function-body; you
3227 cannot, for example, write:
3228
3229 int i = ({ int j = 3; j + 1; });
3230
3231 at class or namespace scope. */
3232 if (!parser->in_function_body
3233 || parser->in_template_argument_list_p)
3234 {
3235 error ("%Hstatement-expressions are not allowed outside "
3236 "functions nor in template-argument lists",
3237 &token->location);
3238 cp_parser_skip_to_end_of_block_or_statement (parser);
3239 expr = error_mark_node;
3240 }
3241 else
3242 {
3243 /* Start the statement-expression. */
3244 expr = begin_stmt_expr ();
3245 /* Parse the compound-statement. */
3246 cp_parser_compound_statement (parser, expr, false);
3247 /* Finish up. */
3248 expr = finish_stmt_expr (expr, false);
3249 }
3250 }
3251 else
3252 {
3253 /* Parse the parenthesized expression. */
3254 expr = cp_parser_expression (parser, cast_p, idk);
3255 /* Let the front end know that this expression was
3256 enclosed in parentheses. This matters in case, for
3257 example, the expression is of the form `A::B', since
3258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259 not. */
3260 finish_parenthesized_expr (expr);
3261 }
3262 /* The `>' token might be the end of a template-id or
3263 template-parameter-list now. */
3264 parser->greater_than_is_operator_p
3265 = saved_greater_than_is_operator_p;
3266 /* Consume the `)'. */
3267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268 cp_parser_skip_to_end_of_statement (parser);
3269
3270 return expr;
3271 }
3272
3273 case CPP_KEYWORD:
3274 switch (token->keyword)
3275 {
3276 /* These two are the boolean literals. */
3277 case RID_TRUE:
3278 cp_lexer_consume_token (parser->lexer);
3279 return boolean_true_node;
3280 case RID_FALSE:
3281 cp_lexer_consume_token (parser->lexer);
3282 return boolean_false_node;
3283
3284 /* The `__null' literal. */
3285 case RID_NULL:
3286 cp_lexer_consume_token (parser->lexer);
3287 return null_node;
3288
3289 /* Recognize the `this' keyword. */
3290 case RID_THIS:
3291 cp_lexer_consume_token (parser->lexer);
3292 if (parser->local_variables_forbidden_p)
3293 {
3294 error ("%H%<this%> may not be used in this context",
3295 &token->location);
3296 return error_mark_node;
3297 }
3298 /* Pointers cannot appear in constant-expressions. */
3299 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300 return error_mark_node;
3301 return finish_this_expr ();
3302
3303 /* The `operator' keyword can be the beginning of an
3304 id-expression. */
3305 case RID_OPERATOR:
3306 goto id_expression;
3307
3308 case RID_FUNCTION_NAME:
3309 case RID_PRETTY_FUNCTION_NAME:
3310 case RID_C99_FUNCTION_NAME:
3311 {
3312 const char *name;
3313
3314 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3315 __func__ are the names of variables -- but they are
3316 treated specially. Therefore, they are handled here,
3317 rather than relying on the generic id-expression logic
3318 below. Grammatically, these names are id-expressions.
3319
3320 Consume the token. */
3321 token = cp_lexer_consume_token (parser->lexer);
3322
3323 switch (token->keyword)
3324 {
3325 case RID_FUNCTION_NAME:
3326 name = "%<__FUNCTION__%>";
3327 break;
3328 case RID_PRETTY_FUNCTION_NAME:
3329 name = "%<__PRETTY_FUNCTION__%>";
3330 break;
3331 case RID_C99_FUNCTION_NAME:
3332 name = "%<__func__%>";
3333 break;
3334 default:
3335 gcc_unreachable ();
3336 }
3337
3338 if (cp_parser_non_integral_constant_expression (parser, name))
3339 return error_mark_node;
3340
3341 /* Look up the name. */
3342 return finish_fname (token->u.value);
3343 }
3344
3345 case RID_VA_ARG:
3346 {
3347 tree expression;
3348 tree type;
3349
3350 /* The `__builtin_va_arg' construct is used to handle
3351 `va_arg'. Consume the `__builtin_va_arg' token. */
3352 cp_lexer_consume_token (parser->lexer);
3353 /* Look for the opening `('. */
3354 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3355 /* Now, parse the assignment-expression. */
3356 expression = cp_parser_assignment_expression (parser,
3357 /*cast_p=*/false, NULL);
3358 /* Look for the `,'. */
3359 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3360 /* Parse the type-id. */
3361 type = cp_parser_type_id (parser);
3362 /* Look for the closing `)'. */
3363 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3364 /* Using `va_arg' in a constant-expression is not
3365 allowed. */
3366 if (cp_parser_non_integral_constant_expression (parser,
3367 "%<va_arg%>"))
3368 return error_mark_node;
3369 return build_x_va_arg (expression, type);
3370 }
3371
3372 case RID_OFFSETOF:
3373 return cp_parser_builtin_offsetof (parser);
3374
3375 case RID_HAS_NOTHROW_ASSIGN:
3376 case RID_HAS_NOTHROW_CONSTRUCTOR:
3377 case RID_HAS_NOTHROW_COPY:
3378 case RID_HAS_TRIVIAL_ASSIGN:
3379 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3380 case RID_HAS_TRIVIAL_COPY:
3381 case RID_HAS_TRIVIAL_DESTRUCTOR:
3382 case RID_HAS_VIRTUAL_DESTRUCTOR:
3383 case RID_IS_ABSTRACT:
3384 case RID_IS_BASE_OF:
3385 case RID_IS_CLASS:
3386 case RID_IS_CONVERTIBLE_TO:
3387 case RID_IS_EMPTY:
3388 case RID_IS_ENUM:
3389 case RID_IS_POD:
3390 case RID_IS_POLYMORPHIC:
3391 case RID_IS_UNION:
3392 return cp_parser_trait_expr (parser, token->keyword);
3393
3394 /* Objective-C++ expressions. */
3395 case RID_AT_ENCODE:
3396 case RID_AT_PROTOCOL:
3397 case RID_AT_SELECTOR:
3398 return cp_parser_objc_expression (parser);
3399
3400 default:
3401 cp_parser_error (parser, "expected primary-expression");
3402 return error_mark_node;
3403 }
3404
3405 /* An id-expression can start with either an identifier, a
3406 `::' as the beginning of a qualified-id, or the "operator"
3407 keyword. */
3408 case CPP_NAME:
3409 case CPP_SCOPE:
3410 case CPP_TEMPLATE_ID:
3411 case CPP_NESTED_NAME_SPECIFIER:
3412 {
3413 tree id_expression;
3414 tree decl;
3415 const char *error_msg;
3416 bool template_p;
3417 bool done;
3418 cp_token *id_expr_token;
3419
3420 id_expression:
3421 /* Parse the id-expression. */
3422 id_expression
3423 = cp_parser_id_expression (parser,
3424 /*template_keyword_p=*/false,
3425 /*check_dependency_p=*/true,
3426 &template_p,
3427 /*declarator_p=*/false,
3428 /*optional_p=*/false);
3429 if (id_expression == error_mark_node)
3430 return error_mark_node;
3431 id_expr_token = token;
3432 token = cp_lexer_peek_token (parser->lexer);
3433 done = (token->type != CPP_OPEN_SQUARE
3434 && token->type != CPP_OPEN_PAREN
3435 && token->type != CPP_DOT
3436 && token->type != CPP_DEREF
3437 && token->type != CPP_PLUS_PLUS
3438 && token->type != CPP_MINUS_MINUS);
3439 /* If we have a template-id, then no further lookup is
3440 required. If the template-id was for a template-class, we
3441 will sometimes have a TYPE_DECL at this point. */
3442 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3443 || TREE_CODE (id_expression) == TYPE_DECL)
3444 decl = id_expression;
3445 /* Look up the name. */
3446 else
3447 {
3448 tree ambiguous_decls;
3449
3450 decl = cp_parser_lookup_name (parser, id_expression,
3451 none_type,
3452 template_p,
3453 /*is_namespace=*/false,
3454 /*check_dependency=*/true,
3455 &ambiguous_decls,
3456 id_expr_token->location);
3457 /* If the lookup was ambiguous, an error will already have
3458 been issued. */
3459 if (ambiguous_decls)
3460 return error_mark_node;
3461
3462 /* In Objective-C++, an instance variable (ivar) may be preferred
3463 to whatever cp_parser_lookup_name() found. */
3464 decl = objc_lookup_ivar (decl, id_expression);
3465
3466 /* If name lookup gives us a SCOPE_REF, then the
3467 qualifying scope was dependent. */
3468 if (TREE_CODE (decl) == SCOPE_REF)
3469 {
3470 /* At this point, we do not know if DECL is a valid
3471 integral constant expression. We assume that it is
3472 in fact such an expression, so that code like:
3473
3474 template <int N> struct A {
3475 int a[B<N>::i];
3476 };
3477
3478 is accepted. At template-instantiation time, we
3479 will check that B<N>::i is actually a constant. */
3480 return decl;
3481 }
3482 /* Check to see if DECL is a local variable in a context
3483 where that is forbidden. */
3484 if (parser->local_variables_forbidden_p
3485 && local_variable_p (decl))
3486 {
3487 /* It might be that we only found DECL because we are
3488 trying to be generous with pre-ISO scoping rules.
3489 For example, consider:
3490
3491 int i;
3492 void g() {
3493 for (int i = 0; i < 10; ++i) {}
3494 extern void f(int j = i);
3495 }
3496
3497 Here, name look up will originally find the out
3498 of scope `i'. We need to issue a warning message,
3499 but then use the global `i'. */
3500 decl = check_for_out_of_scope_variable (decl);
3501 if (local_variable_p (decl))
3502 {
3503 error ("%Hlocal variable %qD may not appear in this context",
3504 &id_expr_token->location, decl);
3505 return error_mark_node;
3506 }
3507 }
3508 }
3509
3510 decl = (finish_id_expression
3511 (id_expression, decl, parser->scope,
3512 idk,
3513 parser->integral_constant_expression_p,
3514 parser->allow_non_integral_constant_expression_p,
3515 &parser->non_integral_constant_expression_p,
3516 template_p, done, address_p,
3517 template_arg_p,
3518 &error_msg,
3519 id_expr_token->location));
3520 if (error_msg)
3521 cp_parser_error (parser, error_msg);
3522 return decl;
3523 }
3524
3525 /* Anything else is an error. */
3526 default:
3527 /* ...unless we have an Objective-C++ message or string literal,
3528 that is. */
3529 if (c_dialect_objc ()
3530 && (token->type == CPP_OPEN_SQUARE
3531 || token->type == CPP_OBJC_STRING))
3532 return cp_parser_objc_expression (parser);
3533
3534 cp_parser_error (parser, "expected primary-expression");
3535 return error_mark_node;
3536 }
3537 }
3538
3539 /* Parse an id-expression.
3540
3541 id-expression:
3542 unqualified-id
3543 qualified-id
3544
3545 qualified-id:
3546 :: [opt] nested-name-specifier template [opt] unqualified-id
3547 :: identifier
3548 :: operator-function-id
3549 :: template-id
3550
3551 Return a representation of the unqualified portion of the
3552 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3553 a `::' or nested-name-specifier.
3554
3555 Often, if the id-expression was a qualified-id, the caller will
3556 want to make a SCOPE_REF to represent the qualified-id. This
3557 function does not do this in order to avoid wastefully creating
3558 SCOPE_REFs when they are not required.
3559
3560 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3561 `template' keyword.
3562
3563 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3564 uninstantiated templates.
3565
3566 If *TEMPLATE_P is non-NULL, it is set to true iff the
3567 `template' keyword is used to explicitly indicate that the entity
3568 named is a template.
3569
3570 If DECLARATOR_P is true, the id-expression is appearing as part of
3571 a declarator, rather than as part of an expression. */
3572
3573 static tree
3574 cp_parser_id_expression (cp_parser *parser,
3575 bool template_keyword_p,
3576 bool check_dependency_p,
3577 bool *template_p,
3578 bool declarator_p,
3579 bool optional_p)
3580 {
3581 bool global_scope_p;
3582 bool nested_name_specifier_p;
3583
3584 /* Assume the `template' keyword was not used. */
3585 if (template_p)
3586 *template_p = template_keyword_p;
3587
3588 /* Look for the optional `::' operator. */
3589 global_scope_p
3590 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3591 != NULL_TREE);
3592 /* Look for the optional nested-name-specifier. */
3593 nested_name_specifier_p
3594 = (cp_parser_nested_name_specifier_opt (parser,
3595 /*typename_keyword_p=*/false,
3596 check_dependency_p,
3597 /*type_p=*/false,
3598 declarator_p)
3599 != NULL_TREE);
3600 /* If there is a nested-name-specifier, then we are looking at
3601 the first qualified-id production. */
3602 if (nested_name_specifier_p)
3603 {
3604 tree saved_scope;
3605 tree saved_object_scope;
3606 tree saved_qualifying_scope;
3607 tree unqualified_id;
3608 bool is_template;
3609
3610 /* See if the next token is the `template' keyword. */
3611 if (!template_p)
3612 template_p = &is_template;
3613 *template_p = cp_parser_optional_template_keyword (parser);
3614 /* Name lookup we do during the processing of the
3615 unqualified-id might obliterate SCOPE. */
3616 saved_scope = parser->scope;
3617 saved_object_scope = parser->object_scope;
3618 saved_qualifying_scope = parser->qualifying_scope;
3619 /* Process the final unqualified-id. */
3620 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3621 check_dependency_p,
3622 declarator_p,
3623 /*optional_p=*/false);
3624 /* Restore the SAVED_SCOPE for our caller. */
3625 parser->scope = saved_scope;
3626 parser->object_scope = saved_object_scope;
3627 parser->qualifying_scope = saved_qualifying_scope;
3628
3629 return unqualified_id;
3630 }
3631 /* Otherwise, if we are in global scope, then we are looking at one
3632 of the other qualified-id productions. */
3633 else if (global_scope_p)
3634 {
3635 cp_token *token;
3636 tree id;
3637
3638 /* Peek at the next token. */
3639 token = cp_lexer_peek_token (parser->lexer);
3640
3641 /* If it's an identifier, and the next token is not a "<", then
3642 we can avoid the template-id case. This is an optimization
3643 for this common case. */
3644 if (token->type == CPP_NAME
3645 && !cp_parser_nth_token_starts_template_argument_list_p
3646 (parser, 2))
3647 return cp_parser_identifier (parser);
3648
3649 cp_parser_parse_tentatively (parser);
3650 /* Try a template-id. */
3651 id = cp_parser_template_id (parser,
3652 /*template_keyword_p=*/false,
3653 /*check_dependency_p=*/true,
3654 declarator_p);
3655 /* If that worked, we're done. */
3656 if (cp_parser_parse_definitely (parser))
3657 return id;
3658
3659 /* Peek at the next token. (Changes in the token buffer may
3660 have invalidated the pointer obtained above.) */
3661 token = cp_lexer_peek_token (parser->lexer);
3662
3663 switch (token->type)
3664 {
3665 case CPP_NAME:
3666 return cp_parser_identifier (parser);
3667
3668 case CPP_KEYWORD:
3669 if (token->keyword == RID_OPERATOR)
3670 return cp_parser_operator_function_id (parser);
3671 /* Fall through. */
3672
3673 default:
3674 cp_parser_error (parser, "expected id-expression");
3675 return error_mark_node;
3676 }
3677 }
3678 else
3679 return cp_parser_unqualified_id (parser, template_keyword_p,
3680 /*check_dependency_p=*/true,
3681 declarator_p,
3682 optional_p);
3683 }
3684
3685 /* Parse an unqualified-id.
3686
3687 unqualified-id:
3688 identifier
3689 operator-function-id
3690 conversion-function-id
3691 ~ class-name
3692 template-id
3693
3694 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3695 keyword, in a construct like `A::template ...'.
3696
3697 Returns a representation of unqualified-id. For the `identifier'
3698 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3699 production a BIT_NOT_EXPR is returned; the operand of the
3700 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3701 other productions, see the documentation accompanying the
3702 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3703 names are looked up in uninstantiated templates. If DECLARATOR_P
3704 is true, the unqualified-id is appearing as part of a declarator,
3705 rather than as part of an expression. */
3706
3707 static tree
3708 cp_parser_unqualified_id (cp_parser* parser,
3709 bool template_keyword_p,
3710 bool check_dependency_p,
3711 bool declarator_p,
3712 bool optional_p)
3713 {
3714 cp_token *token;
3715
3716 /* Peek at the next token. */
3717 token = cp_lexer_peek_token (parser->lexer);
3718
3719 switch (token->type)
3720 {
3721 case CPP_NAME:
3722 {
3723 tree id;
3724
3725 /* We don't know yet whether or not this will be a
3726 template-id. */
3727 cp_parser_parse_tentatively (parser);
3728 /* Try a template-id. */
3729 id = cp_parser_template_id (parser, template_keyword_p,
3730 check_dependency_p,
3731 declarator_p);
3732 /* If it worked, we're done. */
3733 if (cp_parser_parse_definitely (parser))
3734 return id;
3735 /* Otherwise, it's an ordinary identifier. */
3736 return cp_parser_identifier (parser);
3737 }
3738
3739 case CPP_TEMPLATE_ID:
3740 return cp_parser_template_id (parser, template_keyword_p,
3741 check_dependency_p,
3742 declarator_p);
3743
3744 case CPP_COMPL:
3745 {
3746 tree type_decl;
3747 tree qualifying_scope;
3748 tree object_scope;
3749 tree scope;
3750 bool done;
3751
3752 /* Consume the `~' token. */
3753 cp_lexer_consume_token (parser->lexer);
3754 /* Parse the class-name. The standard, as written, seems to
3755 say that:
3756
3757 template <typename T> struct S { ~S (); };
3758 template <typename T> S<T>::~S() {}
3759
3760 is invalid, since `~' must be followed by a class-name, but
3761 `S<T>' is dependent, and so not known to be a class.
3762 That's not right; we need to look in uninstantiated
3763 templates. A further complication arises from:
3764
3765 template <typename T> void f(T t) {
3766 t.T::~T();
3767 }
3768
3769 Here, it is not possible to look up `T' in the scope of `T'
3770 itself. We must look in both the current scope, and the
3771 scope of the containing complete expression.
3772
3773 Yet another issue is:
3774
3775 struct S {
3776 int S;
3777 ~S();
3778 };
3779
3780 S::~S() {}
3781
3782 The standard does not seem to say that the `S' in `~S'
3783 should refer to the type `S' and not the data member
3784 `S::S'. */
3785
3786 /* DR 244 says that we look up the name after the "~" in the
3787 same scope as we looked up the qualifying name. That idea
3788 isn't fully worked out; it's more complicated than that. */
3789 scope = parser->scope;
3790 object_scope = parser->object_scope;
3791 qualifying_scope = parser->qualifying_scope;
3792
3793 /* Check for invalid scopes. */
3794 if (scope == error_mark_node)
3795 {
3796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3797 cp_lexer_consume_token (parser->lexer);
3798 return error_mark_node;
3799 }
3800 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3801 {
3802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3803 error ("%Hscope %qT before %<~%> is not a class-name",
3804 &token->location, scope);
3805 cp_parser_simulate_error (parser);
3806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3807 cp_lexer_consume_token (parser->lexer);
3808 return error_mark_node;
3809 }
3810 gcc_assert (!scope || TYPE_P (scope));
3811
3812 /* If the name is of the form "X::~X" it's OK. */
3813 token = cp_lexer_peek_token (parser->lexer);
3814 if (scope
3815 && token->type == CPP_NAME
3816 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3817 == CPP_OPEN_PAREN)
3818 && constructor_name_p (token->u.value, scope))
3819 {
3820 cp_lexer_consume_token (parser->lexer);
3821 return build_nt (BIT_NOT_EXPR, scope);
3822 }
3823
3824 /* If there was an explicit qualification (S::~T), first look
3825 in the scope given by the qualification (i.e., S). */
3826 done = false;
3827 type_decl = NULL_TREE;
3828 if (scope)
3829 {
3830 cp_parser_parse_tentatively (parser);
3831 type_decl = cp_parser_class_name (parser,
3832 /*typename_keyword_p=*/false,
3833 /*template_keyword_p=*/false,
3834 none_type,
3835 /*check_dependency=*/false,
3836 /*class_head_p=*/false,
3837 declarator_p);
3838 if (cp_parser_parse_definitely (parser))
3839 done = true;
3840 }
3841 /* In "N::S::~S", look in "N" as well. */
3842 if (!done && scope && qualifying_scope)
3843 {
3844 cp_parser_parse_tentatively (parser);
3845 parser->scope = qualifying_scope;
3846 parser->object_scope = NULL_TREE;
3847 parser->qualifying_scope = NULL_TREE;
3848 type_decl
3849 = cp_parser_class_name (parser,
3850 /*typename_keyword_p=*/false,
3851 /*template_keyword_p=*/false,
3852 none_type,
3853 /*check_dependency=*/false,
3854 /*class_head_p=*/false,
3855 declarator_p);
3856 if (cp_parser_parse_definitely (parser))
3857 done = true;
3858 }
3859 /* In "p->S::~T", look in the scope given by "*p" as well. */
3860 else if (!done && object_scope)
3861 {
3862 cp_parser_parse_tentatively (parser);
3863 parser->scope = object_scope;
3864 parser->object_scope = NULL_TREE;
3865 parser->qualifying_scope = NULL_TREE;
3866 type_decl
3867 = cp_parser_class_name (parser,
3868 /*typename_keyword_p=*/false,
3869 /*template_keyword_p=*/false,
3870 none_type,
3871 /*check_dependency=*/false,
3872 /*class_head_p=*/false,
3873 declarator_p);
3874 if (cp_parser_parse_definitely (parser))
3875 done = true;
3876 }
3877 /* Look in the surrounding context. */
3878 if (!done)
3879 {
3880 parser->scope = NULL_TREE;
3881 parser->object_scope = NULL_TREE;
3882 parser->qualifying_scope = NULL_TREE;
3883 if (processing_template_decl)
3884 cp_parser_parse_tentatively (parser);
3885 type_decl
3886 = cp_parser_class_name (parser,
3887 /*typename_keyword_p=*/false,
3888 /*template_keyword_p=*/false,
3889 none_type,
3890 /*check_dependency=*/false,
3891 /*class_head_p=*/false,
3892 declarator_p);
3893 if (processing_template_decl
3894 && ! cp_parser_parse_definitely (parser))
3895 {
3896 /* We couldn't find a type with this name, so just accept
3897 it and check for a match at instantiation time. */
3898 type_decl = cp_parser_identifier (parser);
3899 return build_nt (BIT_NOT_EXPR, type_decl);
3900 }
3901 }
3902 /* If an error occurred, assume that the name of the
3903 destructor is the same as the name of the qualifying
3904 class. That allows us to keep parsing after running
3905 into ill-formed destructor names. */
3906 if (type_decl == error_mark_node && scope)
3907 return build_nt (BIT_NOT_EXPR, scope);
3908 else if (type_decl == error_mark_node)
3909 return error_mark_node;
3910
3911 /* Check that destructor name and scope match. */
3912 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3913 {
3914 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3915 error ("%Hdeclaration of %<~%T%> as member of %qT",
3916 &token->location, type_decl, scope);
3917 cp_parser_simulate_error (parser);
3918 return error_mark_node;
3919 }
3920
3921 /* [class.dtor]
3922
3923 A typedef-name that names a class shall not be used as the
3924 identifier in the declarator for a destructor declaration. */
3925 if (declarator_p
3926 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3927 && !DECL_SELF_REFERENCE_P (type_decl)
3928 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3929 error ("%Htypedef-name %qD used as destructor declarator",
3930 &token->location, type_decl);
3931
3932 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3933 }
3934
3935 case CPP_KEYWORD:
3936 if (token->keyword == RID_OPERATOR)
3937 {
3938 tree id;
3939
3940 /* This could be a template-id, so we try that first. */
3941 cp_parser_parse_tentatively (parser);
3942 /* Try a template-id. */
3943 id = cp_parser_template_id (parser, template_keyword_p,
3944 /*check_dependency_p=*/true,
3945 declarator_p);
3946 /* If that worked, we're done. */
3947 if (cp_parser_parse_definitely (parser))
3948 return id;
3949 /* We still don't know whether we're looking at an
3950 operator-function-id or a conversion-function-id. */
3951 cp_parser_parse_tentatively (parser);
3952 /* Try an operator-function-id. */
3953 id = cp_parser_operator_function_id (parser);
3954 /* If that didn't work, try a conversion-function-id. */
3955 if (!cp_parser_parse_definitely (parser))
3956 id = cp_parser_conversion_function_id (parser);
3957
3958 return id;
3959 }
3960 /* Fall through. */
3961
3962 default:
3963 if (optional_p)
3964 return NULL_TREE;
3965 cp_parser_error (parser, "expected unqualified-id");
3966 return error_mark_node;
3967 }
3968 }
3969
3970 /* Parse an (optional) nested-name-specifier.
3971
3972 nested-name-specifier: [C++98]
3973 class-or-namespace-name :: nested-name-specifier [opt]
3974 class-or-namespace-name :: template nested-name-specifier [opt]
3975
3976 nested-name-specifier: [C++0x]
3977 type-name ::
3978 namespace-name ::
3979 nested-name-specifier identifier ::
3980 nested-name-specifier template [opt] simple-template-id ::
3981
3982 PARSER->SCOPE should be set appropriately before this function is
3983 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3984 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3985 in name lookups.
3986
3987 Sets PARSER->SCOPE to the class (TYPE) or namespace
3988 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3989 it unchanged if there is no nested-name-specifier. Returns the new
3990 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3991
3992 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3993 part of a declaration and/or decl-specifier. */
3994
3995 static tree
3996 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3997 bool typename_keyword_p,
3998 bool check_dependency_p,
3999 bool type_p,
4000 bool is_declaration)
4001 {
4002 bool success = false;
4003 cp_token_position start = 0;
4004 cp_token *token;
4005
4006 /* Remember where the nested-name-specifier starts. */
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 {
4009 start = cp_lexer_token_position (parser->lexer, false);
4010 push_deferring_access_checks (dk_deferred);
4011 }
4012
4013 while (true)
4014 {
4015 tree new_scope;
4016 tree old_scope;
4017 tree saved_qualifying_scope;
4018 bool template_keyword_p;
4019
4020 /* Spot cases that cannot be the beginning of a
4021 nested-name-specifier. */
4022 token = cp_lexer_peek_token (parser->lexer);
4023
4024 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4025 the already parsed nested-name-specifier. */
4026 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4027 {
4028 /* Grab the nested-name-specifier and continue the loop. */
4029 cp_parser_pre_parsed_nested_name_specifier (parser);
4030 /* If we originally encountered this nested-name-specifier
4031 with IS_DECLARATION set to false, we will not have
4032 resolved TYPENAME_TYPEs, so we must do so here. */
4033 if (is_declaration
4034 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4035 {
4036 new_scope = resolve_typename_type (parser->scope,
4037 /*only_current_p=*/false);
4038 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4039 parser->scope = new_scope;
4040 }
4041 success = true;
4042 continue;
4043 }
4044
4045 /* Spot cases that cannot be the beginning of a
4046 nested-name-specifier. On the second and subsequent times
4047 through the loop, we look for the `template' keyword. */
4048 if (success && token->keyword == RID_TEMPLATE)
4049 ;
4050 /* A template-id can start a nested-name-specifier. */
4051 else if (token->type == CPP_TEMPLATE_ID)
4052 ;
4053 else
4054 {
4055 /* If the next token is not an identifier, then it is
4056 definitely not a type-name or namespace-name. */
4057 if (token->type != CPP_NAME)
4058 break;
4059 /* If the following token is neither a `<' (to begin a
4060 template-id), nor a `::', then we are not looking at a
4061 nested-name-specifier. */
4062 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4063 if (token->type != CPP_SCOPE
4064 && !cp_parser_nth_token_starts_template_argument_list_p
4065 (parser, 2))
4066 break;
4067 }
4068
4069 /* The nested-name-specifier is optional, so we parse
4070 tentatively. */
4071 cp_parser_parse_tentatively (parser);
4072
4073 /* Look for the optional `template' keyword, if this isn't the
4074 first time through the loop. */
4075 if (success)
4076 template_keyword_p = cp_parser_optional_template_keyword (parser);
4077 else
4078 template_keyword_p = false;
4079
4080 /* Save the old scope since the name lookup we are about to do
4081 might destroy it. */
4082 old_scope = parser->scope;
4083 saved_qualifying_scope = parser->qualifying_scope;
4084 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4085 look up names in "X<T>::I" in order to determine that "Y" is
4086 a template. So, if we have a typename at this point, we make
4087 an effort to look through it. */
4088 if (is_declaration
4089 && !typename_keyword_p
4090 && parser->scope
4091 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4092 parser->scope = resolve_typename_type (parser->scope,
4093 /*only_current_p=*/false);
4094 /* Parse the qualifying entity. */
4095 new_scope
4096 = cp_parser_qualifying_entity (parser,
4097 typename_keyword_p,
4098 template_keyword_p,
4099 check_dependency_p,
4100 type_p,
4101 is_declaration);
4102 /* Look for the `::' token. */
4103 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4104
4105 /* If we found what we wanted, we keep going; otherwise, we're
4106 done. */
4107 if (!cp_parser_parse_definitely (parser))
4108 {
4109 bool error_p = false;
4110
4111 /* Restore the OLD_SCOPE since it was valid before the
4112 failed attempt at finding the last
4113 class-or-namespace-name. */
4114 parser->scope = old_scope;
4115 parser->qualifying_scope = saved_qualifying_scope;
4116 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4117 break;
4118 /* If the next token is an identifier, and the one after
4119 that is a `::', then any valid interpretation would have
4120 found a class-or-namespace-name. */
4121 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4122 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4123 == CPP_SCOPE)
4124 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4125 != CPP_COMPL))
4126 {
4127 token = cp_lexer_consume_token (parser->lexer);
4128 if (!error_p)
4129 {
4130 if (!token->ambiguous_p)
4131 {
4132 tree decl;
4133 tree ambiguous_decls;
4134
4135 decl = cp_parser_lookup_name (parser, token->u.value,
4136 none_type,
4137 /*is_template=*/false,
4138 /*is_namespace=*/false,
4139 /*check_dependency=*/true,
4140 &ambiguous_decls,
4141 token->location);
4142 if (TREE_CODE (decl) == TEMPLATE_DECL)
4143 error ("%H%qD used without template parameters",
4144 &token->location, decl);
4145 else if (ambiguous_decls)
4146 {
4147 error ("%Hreference to %qD is ambiguous",
4148 &token->location, token->u.value);
4149 print_candidates (ambiguous_decls);
4150 decl = error_mark_node;
4151 }
4152 else
4153 {
4154 const char* msg = "is not a class or namespace";
4155 if (cxx_dialect != cxx98)
4156 msg = "is not a class, namespace, or enumeration";
4157 cp_parser_name_lookup_error
4158 (parser, token->u.value, decl, msg,
4159 token->location);
4160 }
4161 }
4162 parser->scope = error_mark_node;
4163 error_p = true;
4164 /* Treat this as a successful nested-name-specifier
4165 due to:
4166
4167 [basic.lookup.qual]
4168
4169 If the name found is not a class-name (clause
4170 _class_) or namespace-name (_namespace.def_), the
4171 program is ill-formed. */
4172 success = true;
4173 }
4174 cp_lexer_consume_token (parser->lexer);
4175 }
4176 break;
4177 }
4178 /* We've found one valid nested-name-specifier. */
4179 success = true;
4180 /* Name lookup always gives us a DECL. */
4181 if (TREE_CODE (new_scope) == TYPE_DECL)
4182 new_scope = TREE_TYPE (new_scope);
4183 /* Uses of "template" must be followed by actual templates. */
4184 if (template_keyword_p
4185 && !(CLASS_TYPE_P (new_scope)
4186 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4187 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4188 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4189 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4190 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4191 == TEMPLATE_ID_EXPR)))
4192 permerror (input_location, TYPE_P (new_scope)
4193 ? "%qT is not a template"
4194 : "%qD is not a template",
4195 new_scope);
4196 /* If it is a class scope, try to complete it; we are about to
4197 be looking up names inside the class. */
4198 if (TYPE_P (new_scope)
4199 /* Since checking types for dependency can be expensive,
4200 avoid doing it if the type is already complete. */
4201 && !COMPLETE_TYPE_P (new_scope)
4202 /* Do not try to complete dependent types. */
4203 && !dependent_type_p (new_scope))
4204 {
4205 new_scope = complete_type (new_scope);
4206 /* If it is a typedef to current class, use the current
4207 class instead, as the typedef won't have any names inside
4208 it yet. */
4209 if (!COMPLETE_TYPE_P (new_scope)
4210 && currently_open_class (new_scope))
4211 new_scope = TYPE_MAIN_VARIANT (new_scope);
4212 }
4213 /* Make sure we look in the right scope the next time through
4214 the loop. */
4215 parser->scope = new_scope;
4216 }
4217
4218 /* If parsing tentatively, replace the sequence of tokens that makes
4219 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4220 token. That way, should we re-parse the token stream, we will
4221 not have to repeat the effort required to do the parse, nor will
4222 we issue duplicate error messages. */
4223 if (success && start)
4224 {
4225 cp_token *token;
4226
4227 token = cp_lexer_token_at (parser->lexer, start);
4228 /* Reset the contents of the START token. */
4229 token->type = CPP_NESTED_NAME_SPECIFIER;
4230 /* Retrieve any deferred checks. Do not pop this access checks yet
4231 so the memory will not be reclaimed during token replacing below. */
4232 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4233 token->u.tree_check_value->value = parser->scope;
4234 token->u.tree_check_value->checks = get_deferred_access_checks ();
4235 token->u.tree_check_value->qualifying_scope =
4236 parser->qualifying_scope;
4237 token->keyword = RID_MAX;
4238
4239 /* Purge all subsequent tokens. */
4240 cp_lexer_purge_tokens_after (parser->lexer, start);
4241 }
4242
4243 if (start)
4244 pop_to_parent_deferring_access_checks ();
4245
4246 return success ? parser->scope : NULL_TREE;
4247 }
4248
4249 /* Parse a nested-name-specifier. See
4250 cp_parser_nested_name_specifier_opt for details. This function
4251 behaves identically, except that it will an issue an error if no
4252 nested-name-specifier is present. */
4253
4254 static tree
4255 cp_parser_nested_name_specifier (cp_parser *parser,
4256 bool typename_keyword_p,
4257 bool check_dependency_p,
4258 bool type_p,
4259 bool is_declaration)
4260 {
4261 tree scope;
4262
4263 /* Look for the nested-name-specifier. */
4264 scope = cp_parser_nested_name_specifier_opt (parser,
4265 typename_keyword_p,
4266 check_dependency_p,
4267 type_p,
4268 is_declaration);
4269 /* If it was not present, issue an error message. */
4270 if (!scope)
4271 {
4272 cp_parser_error (parser, "expected nested-name-specifier");
4273 parser->scope = NULL_TREE;
4274 }
4275
4276 return scope;
4277 }
4278
4279 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4280 this is either a class-name or a namespace-name (which corresponds
4281 to the class-or-namespace-name production in the grammar). For
4282 C++0x, it can also be a type-name that refers to an enumeration
4283 type.
4284
4285 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4286 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4287 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4288 TYPE_P is TRUE iff the next name should be taken as a class-name,
4289 even the same name is declared to be another entity in the same
4290 scope.
4291
4292 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4293 specified by the class-or-namespace-name. If neither is found the
4294 ERROR_MARK_NODE is returned. */
4295
4296 static tree
4297 cp_parser_qualifying_entity (cp_parser *parser,
4298 bool typename_keyword_p,
4299 bool template_keyword_p,
4300 bool check_dependency_p,
4301 bool type_p,
4302 bool is_declaration)
4303 {
4304 tree saved_scope;
4305 tree saved_qualifying_scope;
4306 tree saved_object_scope;
4307 tree scope;
4308 bool only_class_p;
4309 bool successful_parse_p;
4310
4311 /* Before we try to parse the class-name, we must save away the
4312 current PARSER->SCOPE since cp_parser_class_name will destroy
4313 it. */
4314 saved_scope = parser->scope;
4315 saved_qualifying_scope = parser->qualifying_scope;
4316 saved_object_scope = parser->object_scope;
4317 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4318 there is no need to look for a namespace-name. */
4319 only_class_p = template_keyword_p
4320 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4321 if (!only_class_p)
4322 cp_parser_parse_tentatively (parser);
4323 scope = cp_parser_class_name (parser,
4324 typename_keyword_p,
4325 template_keyword_p,
4326 type_p ? class_type : none_type,
4327 check_dependency_p,
4328 /*class_head_p=*/false,
4329 is_declaration);
4330 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4331 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4332 if (!only_class_p
4333 && cxx_dialect != cxx98
4334 && !successful_parse_p)
4335 {
4336 /* Restore the saved scope. */
4337 parser->scope = saved_scope;
4338 parser->qualifying_scope = saved_qualifying_scope;
4339 parser->object_scope = saved_object_scope;
4340
4341 /* Parse tentatively. */
4342 cp_parser_parse_tentatively (parser);
4343
4344 /* Parse a typedef-name or enum-name. */
4345 scope = cp_parser_nonclass_name (parser);
4346 successful_parse_p = cp_parser_parse_definitely (parser);
4347 }
4348 /* If that didn't work, try for a namespace-name. */
4349 if (!only_class_p && !successful_parse_p)
4350 {
4351 /* Restore the saved scope. */
4352 parser->scope = saved_scope;
4353 parser->qualifying_scope = saved_qualifying_scope;
4354 parser->object_scope = saved_object_scope;
4355 /* If we are not looking at an identifier followed by the scope
4356 resolution operator, then this is not part of a
4357 nested-name-specifier. (Note that this function is only used
4358 to parse the components of a nested-name-specifier.) */
4359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4360 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4361 return error_mark_node;
4362 scope = cp_parser_namespace_name (parser);
4363 }
4364
4365 return scope;
4366 }
4367
4368 /* Parse a postfix-expression.
4369
4370 postfix-expression:
4371 primary-expression
4372 postfix-expression [ expression ]
4373 postfix-expression ( expression-list [opt] )
4374 simple-type-specifier ( expression-list [opt] )
4375 typename :: [opt] nested-name-specifier identifier
4376 ( expression-list [opt] )
4377 typename :: [opt] nested-name-specifier template [opt] template-id
4378 ( expression-list [opt] )
4379 postfix-expression . template [opt] id-expression
4380 postfix-expression -> template [opt] id-expression
4381 postfix-expression . pseudo-destructor-name
4382 postfix-expression -> pseudo-destructor-name
4383 postfix-expression ++
4384 postfix-expression --
4385 dynamic_cast < type-id > ( expression )
4386 static_cast < type-id > ( expression )
4387 reinterpret_cast < type-id > ( expression )
4388 const_cast < type-id > ( expression )
4389 typeid ( expression )
4390 typeid ( type-id )
4391
4392 GNU Extension:
4393
4394 postfix-expression:
4395 ( type-id ) { initializer-list , [opt] }
4396
4397 This extension is a GNU version of the C99 compound-literal
4398 construct. (The C99 grammar uses `type-name' instead of `type-id',
4399 but they are essentially the same concept.)
4400
4401 If ADDRESS_P is true, the postfix expression is the operand of the
4402 `&' operator. CAST_P is true if this expression is the target of a
4403 cast.
4404
4405 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4406 class member access expressions [expr.ref].
4407
4408 Returns a representation of the expression. */
4409
4410 static tree
4411 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4412 bool member_access_only_p,
4413 cp_id_kind * pidk_return)
4414 {
4415 cp_token *token;
4416 enum rid keyword;
4417 cp_id_kind idk = CP_ID_KIND_NONE;
4418 tree postfix_expression = NULL_TREE;
4419 bool is_member_access = false;
4420
4421 /* Peek at the next token. */
4422 token = cp_lexer_peek_token (parser->lexer);
4423 /* Some of the productions are determined by keywords. */
4424 keyword = token->keyword;
4425 switch (keyword)
4426 {
4427 case RID_DYNCAST:
4428 case RID_STATCAST:
4429 case RID_REINTCAST:
4430 case RID_CONSTCAST:
4431 {
4432 tree type;
4433 tree expression;
4434 const char *saved_message;
4435
4436 /* All of these can be handled in the same way from the point
4437 of view of parsing. Begin by consuming the token
4438 identifying the cast. */
4439 cp_lexer_consume_token (parser->lexer);
4440
4441 /* New types cannot be defined in the cast. */
4442 saved_message = parser->type_definition_forbidden_message;
4443 parser->type_definition_forbidden_message
4444 = "types may not be defined in casts";
4445
4446 /* Look for the opening `<'. */
4447 cp_parser_require (parser, CPP_LESS, "%<<%>");
4448 /* Parse the type to which we are casting. */
4449 type = cp_parser_type_id (parser);
4450 /* Look for the closing `>'. */
4451 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4452 /* Restore the old message. */
4453 parser->type_definition_forbidden_message = saved_message;
4454
4455 /* And the expression which is being cast. */
4456 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4457 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4458 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4459
4460 /* Only type conversions to integral or enumeration types
4461 can be used in constant-expressions. */
4462 if (!cast_valid_in_integral_constant_expression_p (type)
4463 && (cp_parser_non_integral_constant_expression
4464 (parser,
4465 "a cast to a type other than an integral or "
4466 "enumeration type")))
4467 return error_mark_node;
4468
4469 switch (keyword)
4470 {
4471 case RID_DYNCAST:
4472 postfix_expression
4473 = build_dynamic_cast (type, expression, tf_warning_or_error);
4474 break;
4475 case RID_STATCAST:
4476 postfix_expression
4477 = build_static_cast (type, expression, tf_warning_or_error);
4478 break;
4479 case RID_REINTCAST:
4480 postfix_expression
4481 = build_reinterpret_cast (type, expression,
4482 tf_warning_or_error);
4483 break;
4484 case RID_CONSTCAST:
4485 postfix_expression
4486 = build_const_cast (type, expression, tf_warning_or_error);
4487 break;
4488 default:
4489 gcc_unreachable ();
4490 }
4491 }
4492 break;
4493
4494 case RID_TYPEID:
4495 {
4496 tree type;
4497 const char *saved_message;
4498 bool saved_in_type_id_in_expr_p;
4499
4500 /* Consume the `typeid' token. */
4501 cp_lexer_consume_token (parser->lexer);
4502 /* Look for the `(' token. */
4503 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4504 /* Types cannot be defined in a `typeid' expression. */
4505 saved_message = parser->type_definition_forbidden_message;
4506 parser->type_definition_forbidden_message
4507 = "types may not be defined in a %<typeid%> expression";
4508 /* We can't be sure yet whether we're looking at a type-id or an
4509 expression. */
4510 cp_parser_parse_tentatively (parser);
4511 /* Try a type-id first. */
4512 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4513 parser->in_type_id_in_expr_p = true;
4514 type = cp_parser_type_id (parser);
4515 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4516 /* Look for the `)' token. Otherwise, we can't be sure that
4517 we're not looking at an expression: consider `typeid (int
4518 (3))', for example. */
4519 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4520 /* If all went well, simply lookup the type-id. */
4521 if (cp_parser_parse_definitely (parser))
4522 postfix_expression = get_typeid (type);
4523 /* Otherwise, fall back to the expression variant. */
4524 else
4525 {
4526 tree expression;
4527
4528 /* Look for an expression. */
4529 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4530 /* Compute its typeid. */
4531 postfix_expression = build_typeid (expression);
4532 /* Look for the `)' token. */
4533 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4534 }
4535 /* Restore the saved message. */
4536 parser->type_definition_forbidden_message = saved_message;
4537 /* `typeid' may not appear in an integral constant expression. */
4538 if (cp_parser_non_integral_constant_expression(parser,
4539 "%<typeid%> operator"))
4540 return error_mark_node;
4541 }
4542 break;
4543
4544 case RID_TYPENAME:
4545 {
4546 tree type;
4547 /* The syntax permitted here is the same permitted for an
4548 elaborated-type-specifier. */
4549 type = cp_parser_elaborated_type_specifier (parser,
4550 /*is_friend=*/false,
4551 /*is_declaration=*/false);
4552 postfix_expression = cp_parser_functional_cast (parser, type);
4553 }
4554 break;
4555
4556 default:
4557 {
4558 tree type;
4559
4560 /* If the next thing is a simple-type-specifier, we may be
4561 looking at a functional cast. We could also be looking at
4562 an id-expression. So, we try the functional cast, and if
4563 that doesn't work we fall back to the primary-expression. */
4564 cp_parser_parse_tentatively (parser);
4565 /* Look for the simple-type-specifier. */
4566 type = cp_parser_simple_type_specifier (parser,
4567 /*decl_specs=*/NULL,
4568 CP_PARSER_FLAGS_NONE);
4569 /* Parse the cast itself. */
4570 if (!cp_parser_error_occurred (parser))
4571 postfix_expression
4572 = cp_parser_functional_cast (parser, type);
4573 /* If that worked, we're done. */
4574 if (cp_parser_parse_definitely (parser))
4575 break;
4576
4577 /* If the functional-cast didn't work out, try a
4578 compound-literal. */
4579 if (cp_parser_allow_gnu_extensions_p (parser)
4580 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4581 {
4582 VEC(constructor_elt,gc) *initializer_list = NULL;
4583 bool saved_in_type_id_in_expr_p;
4584
4585 cp_parser_parse_tentatively (parser);
4586 /* Consume the `('. */
4587 cp_lexer_consume_token (parser->lexer);
4588 /* Parse the type. */
4589 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4590 parser->in_type_id_in_expr_p = true;
4591 type = cp_parser_type_id (parser);
4592 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4593 /* Look for the `)'. */
4594 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4595 /* Look for the `{'. */
4596 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4597 /* If things aren't going well, there's no need to
4598 keep going. */
4599 if (!cp_parser_error_occurred (parser))
4600 {
4601 bool non_constant_p;
4602 /* Parse the initializer-list. */
4603 initializer_list
4604 = cp_parser_initializer_list (parser, &non_constant_p);
4605 /* Allow a trailing `,'. */
4606 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4607 cp_lexer_consume_token (parser->lexer);
4608 /* Look for the final `}'. */
4609 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4610 }
4611 /* If that worked, we're definitely looking at a
4612 compound-literal expression. */
4613 if (cp_parser_parse_definitely (parser))
4614 {
4615 /* Warn the user that a compound literal is not
4616 allowed in standard C++. */
4617 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4618 /* For simplicity, we disallow compound literals in
4619 constant-expressions. We could
4620 allow compound literals of integer type, whose
4621 initializer was a constant, in constant
4622 expressions. Permitting that usage, as a further
4623 extension, would not change the meaning of any
4624 currently accepted programs. (Of course, as
4625 compound literals are not part of ISO C++, the
4626 standard has nothing to say.) */
4627 if (cp_parser_non_integral_constant_expression
4628 (parser, "non-constant compound literals"))
4629 {
4630 postfix_expression = error_mark_node;
4631 break;
4632 }
4633 /* Form the representation of the compound-literal. */
4634 postfix_expression
4635 = (finish_compound_literal
4636 (type, build_constructor (init_list_type_node,
4637 initializer_list)));
4638 break;
4639 }
4640 }
4641
4642 /* It must be a primary-expression. */
4643 postfix_expression
4644 = cp_parser_primary_expression (parser, address_p, cast_p,
4645 /*template_arg_p=*/false,
4646 &idk);
4647 }
4648 break;
4649 }
4650
4651 /* Keep looping until the postfix-expression is complete. */
4652 while (true)
4653 {
4654 if (idk == CP_ID_KIND_UNQUALIFIED
4655 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4656 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4657 /* It is not a Koenig lookup function call. */
4658 postfix_expression
4659 = unqualified_name_lookup_error (postfix_expression);
4660
4661 /* Peek at the next token. */
4662 token = cp_lexer_peek_token (parser->lexer);
4663
4664 switch (token->type)
4665 {
4666 case CPP_OPEN_SQUARE:
4667 postfix_expression
4668 = cp_parser_postfix_open_square_expression (parser,
4669 postfix_expression,
4670 false);
4671 idk = CP_ID_KIND_NONE;
4672 is_member_access = false;
4673 break;
4674
4675 case CPP_OPEN_PAREN:
4676 /* postfix-expression ( expression-list [opt] ) */
4677 {
4678 bool koenig_p;
4679 bool is_builtin_constant_p;
4680 bool saved_integral_constant_expression_p = false;
4681 bool saved_non_integral_constant_expression_p = false;
4682 tree args;
4683
4684 is_member_access = false;
4685
4686 is_builtin_constant_p
4687 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4688 if (is_builtin_constant_p)
4689 {
4690 /* The whole point of __builtin_constant_p is to allow
4691 non-constant expressions to appear as arguments. */
4692 saved_integral_constant_expression_p
4693 = parser->integral_constant_expression_p;
4694 saved_non_integral_constant_expression_p
4695 = parser->non_integral_constant_expression_p;
4696 parser->integral_constant_expression_p = false;
4697 }
4698 args = (cp_parser_parenthesized_expression_list
4699 (parser, /*is_attribute_list=*/false,
4700 /*cast_p=*/false, /*allow_expansion_p=*/true,
4701 /*non_constant_p=*/NULL));
4702 if (is_builtin_constant_p)
4703 {
4704 parser->integral_constant_expression_p
4705 = saved_integral_constant_expression_p;
4706 parser->non_integral_constant_expression_p
4707 = saved_non_integral_constant_expression_p;
4708 }
4709
4710 if (args == error_mark_node)
4711 {
4712 postfix_expression = error_mark_node;
4713 break;
4714 }
4715
4716 /* Function calls are not permitted in
4717 constant-expressions. */
4718 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4719 && cp_parser_non_integral_constant_expression (parser,
4720 "a function call"))
4721 {
4722 postfix_expression = error_mark_node;
4723 break;
4724 }
4725
4726 koenig_p = false;
4727 if (idk == CP_ID_KIND_UNQUALIFIED)
4728 {
4729 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4730 {
4731 if (args)
4732 {
4733 koenig_p = true;
4734 postfix_expression
4735 = perform_koenig_lookup (postfix_expression, args);
4736 }
4737 else
4738 postfix_expression
4739 = unqualified_fn_lookup_error (postfix_expression);
4740 }
4741 /* We do not perform argument-dependent lookup if
4742 normal lookup finds a non-function, in accordance
4743 with the expected resolution of DR 218. */
4744 else if (args && is_overloaded_fn (postfix_expression))
4745 {
4746 tree fn = get_first_fn (postfix_expression);
4747
4748 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4749 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4750
4751 /* Only do argument dependent lookup if regular
4752 lookup does not find a set of member functions.
4753 [basic.lookup.koenig]/2a */
4754 if (!DECL_FUNCTION_MEMBER_P (fn))
4755 {
4756 koenig_p = true;
4757 postfix_expression
4758 = perform_koenig_lookup (postfix_expression, args);
4759 }
4760 }
4761 }
4762
4763 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4764 {
4765 tree instance = TREE_OPERAND (postfix_expression, 0);
4766 tree fn = TREE_OPERAND (postfix_expression, 1);
4767
4768 if (processing_template_decl
4769 && (type_dependent_expression_p (instance)
4770 || (!BASELINK_P (fn)
4771 && TREE_CODE (fn) != FIELD_DECL)
4772 || type_dependent_expression_p (fn)
4773 || any_type_dependent_arguments_p (args)))
4774 {
4775 postfix_expression
4776 = build_nt_call_list (postfix_expression, args);
4777 break;
4778 }
4779
4780 if (BASELINK_P (fn))
4781 {
4782 postfix_expression
4783 = (build_new_method_call
4784 (instance, fn, args, NULL_TREE,
4785 (idk == CP_ID_KIND_QUALIFIED
4786 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4787 /*fn_p=*/NULL,
4788 tf_warning_or_error));
4789 }
4790 else
4791 postfix_expression
4792 = finish_call_expr (postfix_expression, args,
4793 /*disallow_virtual=*/false,
4794 /*koenig_p=*/false,
4795 tf_warning_or_error);
4796 }
4797 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4798 || TREE_CODE (postfix_expression) == MEMBER_REF
4799 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4800 postfix_expression = (build_offset_ref_call_from_tree
4801 (postfix_expression, args));
4802 else if (idk == CP_ID_KIND_QUALIFIED)
4803 /* A call to a static class member, or a namespace-scope
4804 function. */
4805 postfix_expression
4806 = finish_call_expr (postfix_expression, args,
4807 /*disallow_virtual=*/true,
4808 koenig_p,
4809 tf_warning_or_error);
4810 else
4811 /* All other function calls. */
4812 postfix_expression
4813 = finish_call_expr (postfix_expression, args,
4814 /*disallow_virtual=*/false,
4815 koenig_p,
4816 tf_warning_or_error);
4817
4818 if (warn_disallowed_functions)
4819 warn_if_disallowed_function_p (postfix_expression);
4820
4821 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4822 idk = CP_ID_KIND_NONE;
4823 }
4824 break;
4825
4826 case CPP_DOT:
4827 case CPP_DEREF:
4828 /* postfix-expression . template [opt] id-expression
4829 postfix-expression . pseudo-destructor-name
4830 postfix-expression -> template [opt] id-expression
4831 postfix-expression -> pseudo-destructor-name */
4832
4833 /* Consume the `.' or `->' operator. */
4834 cp_lexer_consume_token (parser->lexer);
4835
4836 postfix_expression
4837 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4838 postfix_expression,
4839 false, &idk,
4840 token->location);
4841
4842 is_member_access = true;
4843 break;
4844
4845 case CPP_PLUS_PLUS:
4846 /* postfix-expression ++ */
4847 /* Consume the `++' token. */
4848 cp_lexer_consume_token (parser->lexer);
4849 /* Generate a representation for the complete expression. */
4850 postfix_expression
4851 = finish_increment_expr (postfix_expression,
4852 POSTINCREMENT_EXPR);
4853 /* Increments may not appear in constant-expressions. */
4854 if (cp_parser_non_integral_constant_expression (parser,
4855 "an increment"))
4856 postfix_expression = error_mark_node;
4857 idk = CP_ID_KIND_NONE;
4858 is_member_access = false;
4859 break;
4860
4861 case CPP_MINUS_MINUS:
4862 /* postfix-expression -- */
4863 /* Consume the `--' token. */
4864 cp_lexer_consume_token (parser->lexer);
4865 /* Generate a representation for the complete expression. */
4866 postfix_expression
4867 = finish_increment_expr (postfix_expression,
4868 POSTDECREMENT_EXPR);
4869 /* Decrements may not appear in constant-expressions. */
4870 if (cp_parser_non_integral_constant_expression (parser,
4871 "a decrement"))
4872 postfix_expression = error_mark_node;
4873 idk = CP_ID_KIND_NONE;
4874 is_member_access = false;
4875 break;
4876
4877 default:
4878 if (pidk_return != NULL)
4879 * pidk_return = idk;
4880 if (member_access_only_p)
4881 return is_member_access? postfix_expression : error_mark_node;
4882 else
4883 return postfix_expression;
4884 }
4885 }
4886
4887 /* We should never get here. */
4888 gcc_unreachable ();
4889 return error_mark_node;
4890 }
4891
4892 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4893 by cp_parser_builtin_offsetof. We're looking for
4894
4895 postfix-expression [ expression ]
4896
4897 FOR_OFFSETOF is set if we're being called in that context, which
4898 changes how we deal with integer constant expressions. */
4899
4900 static tree
4901 cp_parser_postfix_open_square_expression (cp_parser *parser,
4902 tree postfix_expression,
4903 bool for_offsetof)
4904 {
4905 tree index;
4906
4907 /* Consume the `[' token. */
4908 cp_lexer_consume_token (parser->lexer);
4909
4910 /* Parse the index expression. */
4911 /* ??? For offsetof, there is a question of what to allow here. If
4912 offsetof is not being used in an integral constant expression context,
4913 then we *could* get the right answer by computing the value at runtime.
4914 If we are in an integral constant expression context, then we might
4915 could accept any constant expression; hard to say without analysis.
4916 Rather than open the barn door too wide right away, allow only integer
4917 constant expressions here. */
4918 if (for_offsetof)
4919 index = cp_parser_constant_expression (parser, false, NULL);
4920 else
4921 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4922
4923 /* Look for the closing `]'. */
4924 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4925
4926 /* Build the ARRAY_REF. */
4927 postfix_expression = grok_array_decl (postfix_expression, index);
4928
4929 /* When not doing offsetof, array references are not permitted in
4930 constant-expressions. */
4931 if (!for_offsetof
4932 && (cp_parser_non_integral_constant_expression
4933 (parser, "an array reference")))
4934 postfix_expression = error_mark_node;
4935
4936 return postfix_expression;
4937 }
4938
4939 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4940 by cp_parser_builtin_offsetof. We're looking for
4941
4942 postfix-expression . template [opt] id-expression
4943 postfix-expression . pseudo-destructor-name
4944 postfix-expression -> template [opt] id-expression
4945 postfix-expression -> pseudo-destructor-name
4946
4947 FOR_OFFSETOF is set if we're being called in that context. That sorta
4948 limits what of the above we'll actually accept, but nevermind.
4949 TOKEN_TYPE is the "." or "->" token, which will already have been
4950 removed from the stream. */
4951
4952 static tree
4953 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4954 enum cpp_ttype token_type,
4955 tree postfix_expression,
4956 bool for_offsetof, cp_id_kind *idk,
4957 location_t location)
4958 {
4959 tree name;
4960 bool dependent_p;
4961 bool pseudo_destructor_p;
4962 tree scope = NULL_TREE;
4963
4964 /* If this is a `->' operator, dereference the pointer. */
4965 if (token_type == CPP_DEREF)
4966 postfix_expression = build_x_arrow (postfix_expression);
4967 /* Check to see whether or not the expression is type-dependent. */
4968 dependent_p = type_dependent_expression_p (postfix_expression);
4969 /* The identifier following the `->' or `.' is not qualified. */
4970 parser->scope = NULL_TREE;
4971 parser->qualifying_scope = NULL_TREE;
4972 parser->object_scope = NULL_TREE;
4973 *idk = CP_ID_KIND_NONE;
4974
4975 /* Enter the scope corresponding to the type of the object
4976 given by the POSTFIX_EXPRESSION. */
4977 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4978 {
4979 scope = TREE_TYPE (postfix_expression);
4980 /* According to the standard, no expression should ever have
4981 reference type. Unfortunately, we do not currently match
4982 the standard in this respect in that our internal representation
4983 of an expression may have reference type even when the standard
4984 says it does not. Therefore, we have to manually obtain the
4985 underlying type here. */
4986 scope = non_reference (scope);
4987 /* The type of the POSTFIX_EXPRESSION must be complete. */
4988 if (scope == unknown_type_node)
4989 {
4990 error ("%H%qE does not have class type", &location, postfix_expression);
4991 scope = NULL_TREE;
4992 }
4993 else
4994 scope = complete_type_or_else (scope, NULL_TREE);
4995 /* Let the name lookup machinery know that we are processing a
4996 class member access expression. */
4997 parser->context->object_type = scope;
4998 /* If something went wrong, we want to be able to discern that case,
4999 as opposed to the case where there was no SCOPE due to the type
5000 of expression being dependent. */
5001 if (!scope)
5002 scope = error_mark_node;
5003 /* If the SCOPE was erroneous, make the various semantic analysis
5004 functions exit quickly -- and without issuing additional error
5005 messages. */
5006 if (scope == error_mark_node)
5007 postfix_expression = error_mark_node;
5008 }
5009
5010 /* Assume this expression is not a pseudo-destructor access. */
5011 pseudo_destructor_p = false;
5012
5013 /* If the SCOPE is a scalar type, then, if this is a valid program,
5014 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5015 is type dependent, it can be pseudo-destructor-name or something else.
5016 Try to parse it as pseudo-destructor-name first. */
5017 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5018 {
5019 tree s;
5020 tree type;
5021
5022 cp_parser_parse_tentatively (parser);
5023 /* Parse the pseudo-destructor-name. */
5024 s = NULL_TREE;
5025 cp_parser_pseudo_destructor_name (parser, &s, &type);
5026 if (dependent_p
5027 && (cp_parser_error_occurred (parser)
5028 || TREE_CODE (type) != TYPE_DECL
5029 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5030 cp_parser_abort_tentative_parse (parser);
5031 else if (cp_parser_parse_definitely (parser))
5032 {
5033 pseudo_destructor_p = true;
5034 postfix_expression
5035 = finish_pseudo_destructor_expr (postfix_expression,
5036 s, TREE_TYPE (type));
5037 }
5038 }
5039
5040 if (!pseudo_destructor_p)
5041 {
5042 /* If the SCOPE is not a scalar type, we are looking at an
5043 ordinary class member access expression, rather than a
5044 pseudo-destructor-name. */
5045 bool template_p;
5046 cp_token *token = cp_lexer_peek_token (parser->lexer);
5047 /* Parse the id-expression. */
5048 name = (cp_parser_id_expression
5049 (parser,
5050 cp_parser_optional_template_keyword (parser),
5051 /*check_dependency_p=*/true,
5052 &template_p,
5053 /*declarator_p=*/false,
5054 /*optional_p=*/false));
5055 /* In general, build a SCOPE_REF if the member name is qualified.
5056 However, if the name was not dependent and has already been
5057 resolved; there is no need to build the SCOPE_REF. For example;
5058
5059 struct X { void f(); };
5060 template <typename T> void f(T* t) { t->X::f(); }
5061
5062 Even though "t" is dependent, "X::f" is not and has been resolved
5063 to a BASELINK; there is no need to include scope information. */
5064
5065 /* But we do need to remember that there was an explicit scope for
5066 virtual function calls. */
5067 if (parser->scope)
5068 *idk = CP_ID_KIND_QUALIFIED;
5069
5070 /* If the name is a template-id that names a type, we will get a
5071 TYPE_DECL here. That is invalid code. */
5072 if (TREE_CODE (name) == TYPE_DECL)
5073 {
5074 error ("%Hinvalid use of %qD", &token->location, name);
5075 postfix_expression = error_mark_node;
5076 }
5077 else
5078 {
5079 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5080 {
5081 name = build_qualified_name (/*type=*/NULL_TREE,
5082 parser->scope,
5083 name,
5084 template_p);
5085 parser->scope = NULL_TREE;
5086 parser->qualifying_scope = NULL_TREE;
5087 parser->object_scope = NULL_TREE;
5088 }
5089 if (scope && name && BASELINK_P (name))
5090 adjust_result_of_qualified_name_lookup
5091 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5092 postfix_expression
5093 = finish_class_member_access_expr (postfix_expression, name,
5094 template_p,
5095 tf_warning_or_error);
5096 }
5097 }
5098
5099 /* We no longer need to look up names in the scope of the object on
5100 the left-hand side of the `.' or `->' operator. */
5101 parser->context->object_type = NULL_TREE;
5102
5103 /* Outside of offsetof, these operators may not appear in
5104 constant-expressions. */
5105 if (!for_offsetof
5106 && (cp_parser_non_integral_constant_expression
5107 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5108 postfix_expression = error_mark_node;
5109
5110 return postfix_expression;
5111 }
5112
5113 /* Parse a parenthesized expression-list.
5114
5115 expression-list:
5116 assignment-expression
5117 expression-list, assignment-expression
5118
5119 attribute-list:
5120 expression-list
5121 identifier
5122 identifier, expression-list
5123
5124 CAST_P is true if this expression is the target of a cast.
5125
5126 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5127 argument pack.
5128
5129 Returns a TREE_LIST. The TREE_VALUE of each node is a
5130 representation of an assignment-expression. Note that a TREE_LIST
5131 is returned even if there is only a single expression in the list.
5132 error_mark_node is returned if the ( and or ) are
5133 missing. NULL_TREE is returned on no expressions. The parentheses
5134 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5135 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5136 indicates whether or not all of the expressions in the list were
5137 constant. */
5138
5139 static tree
5140 cp_parser_parenthesized_expression_list (cp_parser* parser,
5141 bool is_attribute_list,
5142 bool cast_p,
5143 bool allow_expansion_p,
5144 bool *non_constant_p)
5145 {
5146 tree expression_list = NULL_TREE;
5147 bool fold_expr_p = is_attribute_list;
5148 tree identifier = NULL_TREE;
5149 bool saved_greater_than_is_operator_p;
5150
5151 /* Assume all the expressions will be constant. */
5152 if (non_constant_p)
5153 *non_constant_p = false;
5154
5155 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5156 return error_mark_node;
5157
5158 /* Within a parenthesized expression, a `>' token is always
5159 the greater-than operator. */
5160 saved_greater_than_is_operator_p
5161 = parser->greater_than_is_operator_p;
5162 parser->greater_than_is_operator_p = true;
5163
5164 /* Consume expressions until there are no more. */
5165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5166 while (true)
5167 {
5168 tree expr;
5169
5170 /* At the beginning of attribute lists, check to see if the
5171 next token is an identifier. */
5172 if (is_attribute_list
5173 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5174 {
5175 cp_token *token;
5176
5177 /* Consume the identifier. */
5178 token = cp_lexer_consume_token (parser->lexer);
5179 /* Save the identifier. */
5180 identifier = token->u.value;
5181 }
5182 else
5183 {
5184 bool expr_non_constant_p;
5185
5186 /* Parse the next assignment-expression. */
5187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5188 {
5189 /* A braced-init-list. */
5190 maybe_warn_cpp0x ("extended initializer lists");
5191 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5192 if (non_constant_p && expr_non_constant_p)
5193 *non_constant_p = true;
5194 }
5195 else if (non_constant_p)
5196 {
5197 expr = (cp_parser_constant_expression
5198 (parser, /*allow_non_constant_p=*/true,
5199 &expr_non_constant_p));
5200 if (expr_non_constant_p)
5201 *non_constant_p = true;
5202 }
5203 else
5204 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5205
5206 if (fold_expr_p)
5207 expr = fold_non_dependent_expr (expr);
5208
5209 /* If we have an ellipsis, then this is an expression
5210 expansion. */
5211 if (allow_expansion_p
5212 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5213 {
5214 /* Consume the `...'. */
5215 cp_lexer_consume_token (parser->lexer);
5216
5217 /* Build the argument pack. */
5218 expr = make_pack_expansion (expr);
5219 }
5220
5221 /* Add it to the list. We add error_mark_node
5222 expressions to the list, so that we can still tell if
5223 the correct form for a parenthesized expression-list
5224 is found. That gives better errors. */
5225 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5226
5227 if (expr == error_mark_node)
5228 goto skip_comma;
5229 }
5230
5231 /* After the first item, attribute lists look the same as
5232 expression lists. */
5233 is_attribute_list = false;
5234
5235 get_comma:;
5236 /* If the next token isn't a `,', then we are done. */
5237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5238 break;
5239
5240 /* Otherwise, consume the `,' and keep going. */
5241 cp_lexer_consume_token (parser->lexer);
5242 }
5243
5244 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5245 {
5246 int ending;
5247
5248 skip_comma:;
5249 /* We try and resync to an unnested comma, as that will give the
5250 user better diagnostics. */
5251 ending = cp_parser_skip_to_closing_parenthesis (parser,
5252 /*recovering=*/true,
5253 /*or_comma=*/true,
5254 /*consume_paren=*/true);
5255 if (ending < 0)
5256 goto get_comma;
5257 if (!ending)
5258 {
5259 parser->greater_than_is_operator_p
5260 = saved_greater_than_is_operator_p;
5261 return error_mark_node;
5262 }
5263 }
5264
5265 parser->greater_than_is_operator_p
5266 = saved_greater_than_is_operator_p;
5267
5268 /* We built up the list in reverse order so we must reverse it now. */
5269 expression_list = nreverse (expression_list);
5270 if (identifier)
5271 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5272
5273 return expression_list;
5274 }
5275
5276 /* Parse a pseudo-destructor-name.
5277
5278 pseudo-destructor-name:
5279 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5280 :: [opt] nested-name-specifier template template-id :: ~ type-name
5281 :: [opt] nested-name-specifier [opt] ~ type-name
5282
5283 If either of the first two productions is used, sets *SCOPE to the
5284 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5285 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5286 or ERROR_MARK_NODE if the parse fails. */
5287
5288 static void
5289 cp_parser_pseudo_destructor_name (cp_parser* parser,
5290 tree* scope,
5291 tree* type)
5292 {
5293 bool nested_name_specifier_p;
5294
5295 /* Assume that things will not work out. */
5296 *type = error_mark_node;
5297
5298 /* Look for the optional `::' operator. */
5299 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5300 /* Look for the optional nested-name-specifier. */
5301 nested_name_specifier_p
5302 = (cp_parser_nested_name_specifier_opt (parser,
5303 /*typename_keyword_p=*/false,
5304 /*check_dependency_p=*/true,
5305 /*type_p=*/false,
5306 /*is_declaration=*/false)
5307 != NULL_TREE);
5308 /* Now, if we saw a nested-name-specifier, we might be doing the
5309 second production. */
5310 if (nested_name_specifier_p
5311 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5312 {
5313 /* Consume the `template' keyword. */
5314 cp_lexer_consume_token (parser->lexer);
5315 /* Parse the template-id. */
5316 cp_parser_template_id (parser,
5317 /*template_keyword_p=*/true,
5318 /*check_dependency_p=*/false,
5319 /*is_declaration=*/true);
5320 /* Look for the `::' token. */
5321 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5322 }
5323 /* If the next token is not a `~', then there might be some
5324 additional qualification. */
5325 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5326 {
5327 /* At this point, we're looking for "type-name :: ~". The type-name
5328 must not be a class-name, since this is a pseudo-destructor. So,
5329 it must be either an enum-name, or a typedef-name -- both of which
5330 are just identifiers. So, we peek ahead to check that the "::"
5331 and "~" tokens are present; if they are not, then we can avoid
5332 calling type_name. */
5333 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5334 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5335 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5336 {
5337 cp_parser_error (parser, "non-scalar type");
5338 return;
5339 }
5340
5341 /* Look for the type-name. */
5342 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5343 if (*scope == error_mark_node)
5344 return;
5345
5346 /* Look for the `::' token. */
5347 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5348 }
5349 else
5350 *scope = NULL_TREE;
5351
5352 /* Look for the `~'. */
5353 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5354 /* Look for the type-name again. We are not responsible for
5355 checking that it matches the first type-name. */
5356 *type = cp_parser_nonclass_name (parser);
5357 }
5358
5359 /* Parse a unary-expression.
5360
5361 unary-expression:
5362 postfix-expression
5363 ++ cast-expression
5364 -- cast-expression
5365 unary-operator cast-expression
5366 sizeof unary-expression
5367 sizeof ( type-id )
5368 new-expression
5369 delete-expression
5370
5371 GNU Extensions:
5372
5373 unary-expression:
5374 __extension__ cast-expression
5375 __alignof__ unary-expression
5376 __alignof__ ( type-id )
5377 __real__ cast-expression
5378 __imag__ cast-expression
5379 && identifier
5380
5381 ADDRESS_P is true iff the unary-expression is appearing as the
5382 operand of the `&' operator. CAST_P is true if this expression is
5383 the target of a cast.
5384
5385 Returns a representation of the expression. */
5386
5387 static tree
5388 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5389 cp_id_kind * pidk)
5390 {
5391 cp_token *token;
5392 enum tree_code unary_operator;
5393
5394 /* Peek at the next token. */
5395 token = cp_lexer_peek_token (parser->lexer);
5396 /* Some keywords give away the kind of expression. */
5397 if (token->type == CPP_KEYWORD)
5398 {
5399 enum rid keyword = token->keyword;
5400
5401 switch (keyword)
5402 {
5403 case RID_ALIGNOF:
5404 case RID_SIZEOF:
5405 {
5406 tree operand;
5407 enum tree_code op;
5408
5409 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5410 /* Consume the token. */
5411 cp_lexer_consume_token (parser->lexer);
5412 /* Parse the operand. */
5413 operand = cp_parser_sizeof_operand (parser, keyword);
5414
5415 if (TYPE_P (operand))
5416 return cxx_sizeof_or_alignof_type (operand, op, true);
5417 else
5418 return cxx_sizeof_or_alignof_expr (operand, op, true);
5419 }
5420
5421 case RID_NEW:
5422 return cp_parser_new_expression (parser);
5423
5424 case RID_DELETE:
5425 return cp_parser_delete_expression (parser);
5426
5427 case RID_EXTENSION:
5428 {
5429 /* The saved value of the PEDANTIC flag. */
5430 int saved_pedantic;
5431 tree expr;
5432
5433 /* Save away the PEDANTIC flag. */
5434 cp_parser_extension_opt (parser, &saved_pedantic);
5435 /* Parse the cast-expression. */
5436 expr = cp_parser_simple_cast_expression (parser);
5437 /* Restore the PEDANTIC flag. */
5438 pedantic = saved_pedantic;
5439
5440 return expr;
5441 }
5442
5443 case RID_REALPART:
5444 case RID_IMAGPART:
5445 {
5446 tree expression;
5447
5448 /* Consume the `__real__' or `__imag__' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 /* Parse the cast-expression. */
5451 expression = cp_parser_simple_cast_expression (parser);
5452 /* Create the complete representation. */
5453 return build_x_unary_op ((keyword == RID_REALPART
5454 ? REALPART_EXPR : IMAGPART_EXPR),
5455 expression,
5456 tf_warning_or_error);
5457 }
5458 break;
5459
5460 default:
5461 break;
5462 }
5463 }
5464
5465 /* Look for the `:: new' and `:: delete', which also signal the
5466 beginning of a new-expression, or delete-expression,
5467 respectively. If the next token is `::', then it might be one of
5468 these. */
5469 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5470 {
5471 enum rid keyword;
5472
5473 /* See if the token after the `::' is one of the keywords in
5474 which we're interested. */
5475 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5476 /* If it's `new', we have a new-expression. */
5477 if (keyword == RID_NEW)
5478 return cp_parser_new_expression (parser);
5479 /* Similarly, for `delete'. */
5480 else if (keyword == RID_DELETE)
5481 return cp_parser_delete_expression (parser);
5482 }
5483
5484 /* Look for a unary operator. */
5485 unary_operator = cp_parser_unary_operator (token);
5486 /* The `++' and `--' operators can be handled similarly, even though
5487 they are not technically unary-operators in the grammar. */
5488 if (unary_operator == ERROR_MARK)
5489 {
5490 if (token->type == CPP_PLUS_PLUS)
5491 unary_operator = PREINCREMENT_EXPR;
5492 else if (token->type == CPP_MINUS_MINUS)
5493 unary_operator = PREDECREMENT_EXPR;
5494 /* Handle the GNU address-of-label extension. */
5495 else if (cp_parser_allow_gnu_extensions_p (parser)
5496 && token->type == CPP_AND_AND)
5497 {
5498 tree identifier;
5499 tree expression;
5500 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5501
5502 /* Consume the '&&' token. */
5503 cp_lexer_consume_token (parser->lexer);
5504 /* Look for the identifier. */
5505 identifier = cp_parser_identifier (parser);
5506 /* Create an expression representing the address. */
5507 expression = finish_label_address_expr (identifier, loc);
5508 if (cp_parser_non_integral_constant_expression (parser,
5509 "the address of a label"))
5510 expression = error_mark_node;
5511 return expression;
5512 }
5513 }
5514 if (unary_operator != ERROR_MARK)
5515 {
5516 tree cast_expression;
5517 tree expression = error_mark_node;
5518 const char *non_constant_p = NULL;
5519
5520 /* Consume the operator token. */
5521 token = cp_lexer_consume_token (parser->lexer);
5522 /* Parse the cast-expression. */
5523 cast_expression
5524 = cp_parser_cast_expression (parser,
5525 unary_operator == ADDR_EXPR,
5526 /*cast_p=*/false, pidk);
5527 /* Now, build an appropriate representation. */
5528 switch (unary_operator)
5529 {
5530 case INDIRECT_REF:
5531 non_constant_p = "%<*%>";
5532 expression = build_x_indirect_ref (cast_expression, "unary *",
5533 tf_warning_or_error);
5534 break;
5535
5536 case ADDR_EXPR:
5537 non_constant_p = "%<&%>";
5538 /* Fall through. */
5539 case BIT_NOT_EXPR:
5540 expression = build_x_unary_op (unary_operator, cast_expression,
5541 tf_warning_or_error);
5542 break;
5543
5544 case PREINCREMENT_EXPR:
5545 case PREDECREMENT_EXPR:
5546 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5547 ? "%<++%>" : "%<--%>");
5548 /* Fall through. */
5549 case UNARY_PLUS_EXPR:
5550 case NEGATE_EXPR:
5551 case TRUTH_NOT_EXPR:
5552 expression = finish_unary_op_expr (unary_operator, cast_expression);
5553 break;
5554
5555 default:
5556 gcc_unreachable ();
5557 }
5558
5559 if (non_constant_p
5560 && cp_parser_non_integral_constant_expression (parser,
5561 non_constant_p))
5562 expression = error_mark_node;
5563
5564 return expression;
5565 }
5566
5567 return cp_parser_postfix_expression (parser, address_p, cast_p,
5568 /*member_access_only_p=*/false,
5569 pidk);
5570 }
5571
5572 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5573 unary-operator, the corresponding tree code is returned. */
5574
5575 static enum tree_code
5576 cp_parser_unary_operator (cp_token* token)
5577 {
5578 switch (token->type)
5579 {
5580 case CPP_MULT:
5581 return INDIRECT_REF;
5582
5583 case CPP_AND:
5584 return ADDR_EXPR;
5585
5586 case CPP_PLUS:
5587 return UNARY_PLUS_EXPR;
5588
5589 case CPP_MINUS:
5590 return NEGATE_EXPR;
5591
5592 case CPP_NOT:
5593 return TRUTH_NOT_EXPR;
5594
5595 case CPP_COMPL:
5596 return BIT_NOT_EXPR;
5597
5598 default:
5599 return ERROR_MARK;
5600 }
5601 }
5602
5603 /* Parse a new-expression.
5604
5605 new-expression:
5606 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5607 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5608
5609 Returns a representation of the expression. */
5610
5611 static tree
5612 cp_parser_new_expression (cp_parser* parser)
5613 {
5614 bool global_scope_p;
5615 tree placement;
5616 tree type;
5617 tree initializer;
5618 tree nelts;
5619
5620 /* Look for the optional `::' operator. */
5621 global_scope_p
5622 = (cp_parser_global_scope_opt (parser,
5623 /*current_scope_valid_p=*/false)
5624 != NULL_TREE);
5625 /* Look for the `new' operator. */
5626 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5627 /* There's no easy way to tell a new-placement from the
5628 `( type-id )' construct. */
5629 cp_parser_parse_tentatively (parser);
5630 /* Look for a new-placement. */
5631 placement = cp_parser_new_placement (parser);
5632 /* If that didn't work out, there's no new-placement. */
5633 if (!cp_parser_parse_definitely (parser))
5634 placement = NULL_TREE;
5635
5636 /* If the next token is a `(', then we have a parenthesized
5637 type-id. */
5638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5639 {
5640 cp_token *token;
5641 /* Consume the `('. */
5642 cp_lexer_consume_token (parser->lexer);
5643 /* Parse the type-id. */
5644 type = cp_parser_type_id (parser);
5645 /* Look for the closing `)'. */
5646 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5647 token = cp_lexer_peek_token (parser->lexer);
5648 /* There should not be a direct-new-declarator in this production,
5649 but GCC used to allowed this, so we check and emit a sensible error
5650 message for this case. */
5651 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5652 {
5653 error ("%Harray bound forbidden after parenthesized type-id",
5654 &token->location);
5655 inform (token->location,
5656 "try removing the parentheses around the type-id");
5657 cp_parser_direct_new_declarator (parser);
5658 }
5659 nelts = NULL_TREE;
5660 }
5661 /* Otherwise, there must be a new-type-id. */
5662 else
5663 type = cp_parser_new_type_id (parser, &nelts);
5664
5665 /* If the next token is a `(' or '{', then we have a new-initializer. */
5666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5667 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5668 initializer = cp_parser_new_initializer (parser);
5669 else
5670 initializer = NULL_TREE;
5671
5672 /* A new-expression may not appear in an integral constant
5673 expression. */
5674 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5675 return error_mark_node;
5676
5677 /* Create a representation of the new-expression. */
5678 return build_new (placement, type, nelts, initializer, global_scope_p,
5679 tf_warning_or_error);
5680 }
5681
5682 /* Parse a new-placement.
5683
5684 new-placement:
5685 ( expression-list )
5686
5687 Returns the same representation as for an expression-list. */
5688
5689 static tree
5690 cp_parser_new_placement (cp_parser* parser)
5691 {
5692 tree expression_list;
5693
5694 /* Parse the expression-list. */
5695 expression_list = (cp_parser_parenthesized_expression_list
5696 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5697 /*non_constant_p=*/NULL));
5698
5699 return expression_list;
5700 }
5701
5702 /* Parse a new-type-id.
5703
5704 new-type-id:
5705 type-specifier-seq new-declarator [opt]
5706
5707 Returns the TYPE allocated. If the new-type-id indicates an array
5708 type, *NELTS is set to the number of elements in the last array
5709 bound; the TYPE will not include the last array bound. */
5710
5711 static tree
5712 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5713 {
5714 cp_decl_specifier_seq type_specifier_seq;
5715 cp_declarator *new_declarator;
5716 cp_declarator *declarator;
5717 cp_declarator *outer_declarator;
5718 const char *saved_message;
5719 tree type;
5720
5721 /* The type-specifier sequence must not contain type definitions.
5722 (It cannot contain declarations of new types either, but if they
5723 are not definitions we will catch that because they are not
5724 complete.) */
5725 saved_message = parser->type_definition_forbidden_message;
5726 parser->type_definition_forbidden_message
5727 = "types may not be defined in a new-type-id";
5728 /* Parse the type-specifier-seq. */
5729 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5730 &type_specifier_seq);
5731 /* Restore the old message. */
5732 parser->type_definition_forbidden_message = saved_message;
5733 /* Parse the new-declarator. */
5734 new_declarator = cp_parser_new_declarator_opt (parser);
5735
5736 /* Determine the number of elements in the last array dimension, if
5737 any. */
5738 *nelts = NULL_TREE;
5739 /* Skip down to the last array dimension. */
5740 declarator = new_declarator;
5741 outer_declarator = NULL;
5742 while (declarator && (declarator->kind == cdk_pointer
5743 || declarator->kind == cdk_ptrmem))
5744 {
5745 outer_declarator = declarator;
5746 declarator = declarator->declarator;
5747 }
5748 while (declarator
5749 && declarator->kind == cdk_array
5750 && declarator->declarator
5751 && declarator->declarator->kind == cdk_array)
5752 {
5753 outer_declarator = declarator;
5754 declarator = declarator->declarator;
5755 }
5756
5757 if (declarator && declarator->kind == cdk_array)
5758 {
5759 *nelts = declarator->u.array.bounds;
5760 if (*nelts == error_mark_node)
5761 *nelts = integer_one_node;
5762
5763 if (outer_declarator)
5764 outer_declarator->declarator = declarator->declarator;
5765 else
5766 new_declarator = NULL;
5767 }
5768
5769 type = groktypename (&type_specifier_seq, new_declarator);
5770 return type;
5771 }
5772
5773 /* Parse an (optional) new-declarator.
5774
5775 new-declarator:
5776 ptr-operator new-declarator [opt]
5777 direct-new-declarator
5778
5779 Returns the declarator. */
5780
5781 static cp_declarator *
5782 cp_parser_new_declarator_opt (cp_parser* parser)
5783 {
5784 enum tree_code code;
5785 tree type;
5786 cp_cv_quals cv_quals;
5787
5788 /* We don't know if there's a ptr-operator next, or not. */
5789 cp_parser_parse_tentatively (parser);
5790 /* Look for a ptr-operator. */
5791 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5792 /* If that worked, look for more new-declarators. */
5793 if (cp_parser_parse_definitely (parser))
5794 {
5795 cp_declarator *declarator;
5796
5797 /* Parse another optional declarator. */
5798 declarator = cp_parser_new_declarator_opt (parser);
5799
5800 return cp_parser_make_indirect_declarator
5801 (code, type, cv_quals, declarator);
5802 }
5803
5804 /* If the next token is a `[', there is a direct-new-declarator. */
5805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5806 return cp_parser_direct_new_declarator (parser);
5807
5808 return NULL;
5809 }
5810
5811 /* Parse a direct-new-declarator.
5812
5813 direct-new-declarator:
5814 [ expression ]
5815 direct-new-declarator [constant-expression]
5816
5817 */
5818
5819 static cp_declarator *
5820 cp_parser_direct_new_declarator (cp_parser* parser)
5821 {
5822 cp_declarator *declarator = NULL;
5823
5824 while (true)
5825 {
5826 tree expression;
5827
5828 /* Look for the opening `['. */
5829 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5830 /* The first expression is not required to be constant. */
5831 if (!declarator)
5832 {
5833 cp_token *token = cp_lexer_peek_token (parser->lexer);
5834 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5835 /* The standard requires that the expression have integral
5836 type. DR 74 adds enumeration types. We believe that the
5837 real intent is that these expressions be handled like the
5838 expression in a `switch' condition, which also allows
5839 classes with a single conversion to integral or
5840 enumeration type. */
5841 if (!processing_template_decl)
5842 {
5843 expression
5844 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5845 expression,
5846 /*complain=*/true);
5847 if (!expression)
5848 {
5849 error ("%Hexpression in new-declarator must have integral "
5850 "or enumeration type", &token->location);
5851 expression = error_mark_node;
5852 }
5853 }
5854 }
5855 /* But all the other expressions must be. */
5856 else
5857 expression
5858 = cp_parser_constant_expression (parser,
5859 /*allow_non_constant=*/false,
5860 NULL);
5861 /* Look for the closing `]'. */
5862 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5863
5864 /* Add this bound to the declarator. */
5865 declarator = make_array_declarator (declarator, expression);
5866
5867 /* If the next token is not a `[', then there are no more
5868 bounds. */
5869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5870 break;
5871 }
5872
5873 return declarator;
5874 }
5875
5876 /* Parse a new-initializer.
5877
5878 new-initializer:
5879 ( expression-list [opt] )
5880 braced-init-list
5881
5882 Returns a representation of the expression-list. If there is no
5883 expression-list, VOID_ZERO_NODE is returned. */
5884
5885 static tree
5886 cp_parser_new_initializer (cp_parser* parser)
5887 {
5888 tree expression_list;
5889
5890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5891 {
5892 bool expr_non_constant_p;
5893 maybe_warn_cpp0x ("extended initializer lists");
5894 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5895 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5896 expression_list = build_tree_list (NULL_TREE, expression_list);
5897 }
5898 else
5899 expression_list = (cp_parser_parenthesized_expression_list
5900 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5901 /*non_constant_p=*/NULL));
5902 if (!expression_list)
5903 expression_list = void_zero_node;
5904
5905 return expression_list;
5906 }
5907
5908 /* Parse a delete-expression.
5909
5910 delete-expression:
5911 :: [opt] delete cast-expression
5912 :: [opt] delete [ ] cast-expression
5913
5914 Returns a representation of the expression. */
5915
5916 static tree
5917 cp_parser_delete_expression (cp_parser* parser)
5918 {
5919 bool global_scope_p;
5920 bool array_p;
5921 tree expression;
5922
5923 /* Look for the optional `::' operator. */
5924 global_scope_p
5925 = (cp_parser_global_scope_opt (parser,
5926 /*current_scope_valid_p=*/false)
5927 != NULL_TREE);
5928 /* Look for the `delete' keyword. */
5929 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5930 /* See if the array syntax is in use. */
5931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5932 {
5933 /* Consume the `[' token. */
5934 cp_lexer_consume_token (parser->lexer);
5935 /* Look for the `]' token. */
5936 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5937 /* Remember that this is the `[]' construct. */
5938 array_p = true;
5939 }
5940 else
5941 array_p = false;
5942
5943 /* Parse the cast-expression. */
5944 expression = cp_parser_simple_cast_expression (parser);
5945
5946 /* A delete-expression may not appear in an integral constant
5947 expression. */
5948 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5949 return error_mark_node;
5950
5951 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5952 }
5953
5954 /* Returns true if TOKEN may start a cast-expression and false
5955 otherwise. */
5956
5957 static bool
5958 cp_parser_token_starts_cast_expression (cp_token *token)
5959 {
5960 switch (token->type)
5961 {
5962 case CPP_COMMA:
5963 case CPP_SEMICOLON:
5964 case CPP_QUERY:
5965 case CPP_COLON:
5966 case CPP_CLOSE_SQUARE:
5967 case CPP_CLOSE_PAREN:
5968 case CPP_CLOSE_BRACE:
5969 case CPP_DOT:
5970 case CPP_DOT_STAR:
5971 case CPP_DEREF:
5972 case CPP_DEREF_STAR:
5973 case CPP_DIV:
5974 case CPP_MOD:
5975 case CPP_LSHIFT:
5976 case CPP_RSHIFT:
5977 case CPP_LESS:
5978 case CPP_GREATER:
5979 case CPP_LESS_EQ:
5980 case CPP_GREATER_EQ:
5981 case CPP_EQ_EQ:
5982 case CPP_NOT_EQ:
5983 case CPP_EQ:
5984 case CPP_MULT_EQ:
5985 case CPP_DIV_EQ:
5986 case CPP_MOD_EQ:
5987 case CPP_PLUS_EQ:
5988 case CPP_MINUS_EQ:
5989 case CPP_RSHIFT_EQ:
5990 case CPP_LSHIFT_EQ:
5991 case CPP_AND_EQ:
5992 case CPP_XOR_EQ:
5993 case CPP_OR_EQ:
5994 case CPP_XOR:
5995 case CPP_OR:
5996 case CPP_OR_OR:
5997 case CPP_EOF:
5998 return false;
5999
6000 /* '[' may start a primary-expression in obj-c++. */
6001 case CPP_OPEN_SQUARE:
6002 return c_dialect_objc ();
6003
6004 default:
6005 return true;
6006 }
6007 }
6008
6009 /* Parse a cast-expression.
6010
6011 cast-expression:
6012 unary-expression
6013 ( type-id ) cast-expression
6014
6015 ADDRESS_P is true iff the unary-expression is appearing as the
6016 operand of the `&' operator. CAST_P is true if this expression is
6017 the target of a cast.
6018
6019 Returns a representation of the expression. */
6020
6021 static tree
6022 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6023 cp_id_kind * pidk)
6024 {
6025 /* If it's a `(', then we might be looking at a cast. */
6026 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6027 {
6028 tree type = NULL_TREE;
6029 tree expr = NULL_TREE;
6030 bool compound_literal_p;
6031 const char *saved_message;
6032
6033 /* There's no way to know yet whether or not this is a cast.
6034 For example, `(int (3))' is a unary-expression, while `(int)
6035 3' is a cast. So, we resort to parsing tentatively. */
6036 cp_parser_parse_tentatively (parser);
6037 /* Types may not be defined in a cast. */
6038 saved_message = parser->type_definition_forbidden_message;
6039 parser->type_definition_forbidden_message
6040 = "types may not be defined in casts";
6041 /* Consume the `('. */
6042 cp_lexer_consume_token (parser->lexer);
6043 /* A very tricky bit is that `(struct S) { 3 }' is a
6044 compound-literal (which we permit in C++ as an extension).
6045 But, that construct is not a cast-expression -- it is a
6046 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6047 is legal; if the compound-literal were a cast-expression,
6048 you'd need an extra set of parentheses.) But, if we parse
6049 the type-id, and it happens to be a class-specifier, then we
6050 will commit to the parse at that point, because we cannot
6051 undo the action that is done when creating a new class. So,
6052 then we cannot back up and do a postfix-expression.
6053
6054 Therefore, we scan ahead to the closing `)', and check to see
6055 if the token after the `)' is a `{'. If so, we are not
6056 looking at a cast-expression.
6057
6058 Save tokens so that we can put them back. */
6059 cp_lexer_save_tokens (parser->lexer);
6060 /* Skip tokens until the next token is a closing parenthesis.
6061 If we find the closing `)', and the next token is a `{', then
6062 we are looking at a compound-literal. */
6063 compound_literal_p
6064 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6065 /*consume_paren=*/true)
6066 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6067 /* Roll back the tokens we skipped. */
6068 cp_lexer_rollback_tokens (parser->lexer);
6069 /* If we were looking at a compound-literal, simulate an error
6070 so that the call to cp_parser_parse_definitely below will
6071 fail. */
6072 if (compound_literal_p)
6073 cp_parser_simulate_error (parser);
6074 else
6075 {
6076 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6077 parser->in_type_id_in_expr_p = true;
6078 /* Look for the type-id. */
6079 type = cp_parser_type_id (parser);
6080 /* Look for the closing `)'. */
6081 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6082 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6083 }
6084
6085 /* Restore the saved message. */
6086 parser->type_definition_forbidden_message = saved_message;
6087
6088 /* At this point this can only be either a cast or a
6089 parenthesized ctor such as `(T ())' that looks like a cast to
6090 function returning T. */
6091 if (!cp_parser_error_occurred (parser)
6092 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6093 (parser->lexer)))
6094 {
6095 cp_parser_parse_definitely (parser);
6096 expr = cp_parser_cast_expression (parser,
6097 /*address_p=*/false,
6098 /*cast_p=*/true, pidk);
6099
6100 /* Warn about old-style casts, if so requested. */
6101 if (warn_old_style_cast
6102 && !in_system_header
6103 && !VOID_TYPE_P (type)
6104 && current_lang_name != lang_name_c)
6105 warning (OPT_Wold_style_cast, "use of old-style cast");
6106
6107 /* Only type conversions to integral or enumeration types
6108 can be used in constant-expressions. */
6109 if (!cast_valid_in_integral_constant_expression_p (type)
6110 && (cp_parser_non_integral_constant_expression
6111 (parser,
6112 "a cast to a type other than an integral or "
6113 "enumeration type")))
6114 return error_mark_node;
6115
6116 /* Perform the cast. */
6117 expr = build_c_cast (type, expr);
6118 return expr;
6119 }
6120 else
6121 cp_parser_abort_tentative_parse (parser);
6122 }
6123
6124 /* If we get here, then it's not a cast, so it must be a
6125 unary-expression. */
6126 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6127 }
6128
6129 /* Parse a binary expression of the general form:
6130
6131 pm-expression:
6132 cast-expression
6133 pm-expression .* cast-expression
6134 pm-expression ->* cast-expression
6135
6136 multiplicative-expression:
6137 pm-expression
6138 multiplicative-expression * pm-expression
6139 multiplicative-expression / pm-expression
6140 multiplicative-expression % pm-expression
6141
6142 additive-expression:
6143 multiplicative-expression
6144 additive-expression + multiplicative-expression
6145 additive-expression - multiplicative-expression
6146
6147 shift-expression:
6148 additive-expression
6149 shift-expression << additive-expression
6150 shift-expression >> additive-expression
6151
6152 relational-expression:
6153 shift-expression
6154 relational-expression < shift-expression
6155 relational-expression > shift-expression
6156 relational-expression <= shift-expression
6157 relational-expression >= shift-expression
6158
6159 GNU Extension:
6160
6161 relational-expression:
6162 relational-expression <? shift-expression
6163 relational-expression >? shift-expression
6164
6165 equality-expression:
6166 relational-expression
6167 equality-expression == relational-expression
6168 equality-expression != relational-expression
6169
6170 and-expression:
6171 equality-expression
6172 and-expression & equality-expression
6173
6174 exclusive-or-expression:
6175 and-expression
6176 exclusive-or-expression ^ and-expression
6177
6178 inclusive-or-expression:
6179 exclusive-or-expression
6180 inclusive-or-expression | exclusive-or-expression
6181
6182 logical-and-expression:
6183 inclusive-or-expression
6184 logical-and-expression && inclusive-or-expression
6185
6186 logical-or-expression:
6187 logical-and-expression
6188 logical-or-expression || logical-and-expression
6189
6190 All these are implemented with a single function like:
6191
6192 binary-expression:
6193 simple-cast-expression
6194 binary-expression <token> binary-expression
6195
6196 CAST_P is true if this expression is the target of a cast.
6197
6198 The binops_by_token map is used to get the tree codes for each <token> type.
6199 binary-expressions are associated according to a precedence table. */
6200
6201 #define TOKEN_PRECEDENCE(token) \
6202 (((token->type == CPP_GREATER \
6203 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6204 && !parser->greater_than_is_operator_p) \
6205 ? PREC_NOT_OPERATOR \
6206 : binops_by_token[token->type].prec)
6207
6208 static tree
6209 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6210 enum cp_parser_prec prec,
6211 cp_id_kind * pidk)
6212 {
6213 cp_parser_expression_stack stack;
6214 cp_parser_expression_stack_entry *sp = &stack[0];
6215 tree lhs, rhs;
6216 cp_token *token;
6217 enum tree_code tree_type, lhs_type, rhs_type;
6218 enum cp_parser_prec new_prec, lookahead_prec;
6219 bool overloaded_p;
6220
6221 /* Parse the first expression. */
6222 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6223 lhs_type = ERROR_MARK;
6224
6225 for (;;)
6226 {
6227 /* Get an operator token. */
6228 token = cp_lexer_peek_token (parser->lexer);
6229
6230 if (warn_cxx0x_compat
6231 && token->type == CPP_RSHIFT
6232 && !parser->greater_than_is_operator_p)
6233 {
6234 warning (OPT_Wc__0x_compat,
6235 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6236 &token->location);
6237 warning (OPT_Wc__0x_compat,
6238 "suggest parentheses around %<>>%> expression");
6239 }
6240
6241 new_prec = TOKEN_PRECEDENCE (token);
6242
6243 /* Popping an entry off the stack means we completed a subexpression:
6244 - either we found a token which is not an operator (`>' where it is not
6245 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6246 will happen repeatedly;
6247 - or, we found an operator which has lower priority. This is the case
6248 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6249 parsing `3 * 4'. */
6250 if (new_prec <= prec)
6251 {
6252 if (sp == stack)
6253 break;
6254 else
6255 goto pop;
6256 }
6257
6258 get_rhs:
6259 tree_type = binops_by_token[token->type].tree_type;
6260
6261 /* We used the operator token. */
6262 cp_lexer_consume_token (parser->lexer);
6263
6264 /* Extract another operand. It may be the RHS of this expression
6265 or the LHS of a new, higher priority expression. */
6266 rhs = cp_parser_simple_cast_expression (parser);
6267 rhs_type = ERROR_MARK;
6268
6269 /* Get another operator token. Look up its precedence to avoid
6270 building a useless (immediately popped) stack entry for common
6271 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6272 token = cp_lexer_peek_token (parser->lexer);
6273 lookahead_prec = TOKEN_PRECEDENCE (token);
6274 if (lookahead_prec > new_prec)
6275 {
6276 /* ... and prepare to parse the RHS of the new, higher priority
6277 expression. Since precedence levels on the stack are
6278 monotonically increasing, we do not have to care about
6279 stack overflows. */
6280 sp->prec = prec;
6281 sp->tree_type = tree_type;
6282 sp->lhs = lhs;
6283 sp->lhs_type = lhs_type;
6284 sp++;
6285 lhs = rhs;
6286 lhs_type = rhs_type;
6287 prec = new_prec;
6288 new_prec = lookahead_prec;
6289 goto get_rhs;
6290
6291 pop:
6292 /* If the stack is not empty, we have parsed into LHS the right side
6293 (`4' in the example above) of an expression we had suspended.
6294 We can use the information on the stack to recover the LHS (`3')
6295 from the stack together with the tree code (`MULT_EXPR'), and
6296 the precedence of the higher level subexpression
6297 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6298 which will be used to actually build the additive expression. */
6299 --sp;
6300 prec = sp->prec;
6301 tree_type = sp->tree_type;
6302 rhs = lhs;
6303 rhs_type = lhs_type;
6304 lhs = sp->lhs;
6305 lhs_type = sp->lhs_type;
6306 }
6307
6308 overloaded_p = false;
6309 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6310 ERROR_MARK for everything that is not a binary expression.
6311 This makes warn_about_parentheses miss some warnings that
6312 involve unary operators. For unary expressions we should
6313 pass the correct tree_code unless the unary expression was
6314 surrounded by parentheses.
6315 */
6316 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6317 &overloaded_p, tf_warning_or_error);
6318 lhs_type = tree_type;
6319
6320 /* If the binary operator required the use of an overloaded operator,
6321 then this expression cannot be an integral constant-expression.
6322 An overloaded operator can be used even if both operands are
6323 otherwise permissible in an integral constant-expression if at
6324 least one of the operands is of enumeration type. */
6325
6326 if (overloaded_p
6327 && (cp_parser_non_integral_constant_expression
6328 (parser, "calls to overloaded operators")))
6329 return error_mark_node;
6330 }
6331
6332 return lhs;
6333 }
6334
6335
6336 /* Parse the `? expression : assignment-expression' part of a
6337 conditional-expression. The LOGICAL_OR_EXPR is the
6338 logical-or-expression that started the conditional-expression.
6339 Returns a representation of the entire conditional-expression.
6340
6341 This routine is used by cp_parser_assignment_expression.
6342
6343 ? expression : assignment-expression
6344
6345 GNU Extensions:
6346
6347 ? : assignment-expression */
6348
6349 static tree
6350 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6351 {
6352 tree expr;
6353 tree assignment_expr;
6354
6355 /* Consume the `?' token. */
6356 cp_lexer_consume_token (parser->lexer);
6357 if (cp_parser_allow_gnu_extensions_p (parser)
6358 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6359 /* Implicit true clause. */
6360 expr = NULL_TREE;
6361 else
6362 /* Parse the expression. */
6363 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6364
6365 /* The next token should be a `:'. */
6366 cp_parser_require (parser, CPP_COLON, "%<:%>");
6367 /* Parse the assignment-expression. */
6368 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6369
6370 /* Build the conditional-expression. */
6371 return build_x_conditional_expr (logical_or_expr,
6372 expr,
6373 assignment_expr,
6374 tf_warning_or_error);
6375 }
6376
6377 /* Parse an assignment-expression.
6378
6379 assignment-expression:
6380 conditional-expression
6381 logical-or-expression assignment-operator assignment_expression
6382 throw-expression
6383
6384 CAST_P is true if this expression is the target of a cast.
6385
6386 Returns a representation for the expression. */
6387
6388 static tree
6389 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6390 cp_id_kind * pidk)
6391 {
6392 tree expr;
6393
6394 /* If the next token is the `throw' keyword, then we're looking at
6395 a throw-expression. */
6396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6397 expr = cp_parser_throw_expression (parser);
6398 /* Otherwise, it must be that we are looking at a
6399 logical-or-expression. */
6400 else
6401 {
6402 /* Parse the binary expressions (logical-or-expression). */
6403 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6404 /* If the next token is a `?' then we're actually looking at a
6405 conditional-expression. */
6406 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6407 return cp_parser_question_colon_clause (parser, expr);
6408 else
6409 {
6410 enum tree_code assignment_operator;
6411
6412 /* If it's an assignment-operator, we're using the second
6413 production. */
6414 assignment_operator
6415 = cp_parser_assignment_operator_opt (parser);
6416 if (assignment_operator != ERROR_MARK)
6417 {
6418 bool non_constant_p;
6419
6420 /* Parse the right-hand side of the assignment. */
6421 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6422
6423 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6424 maybe_warn_cpp0x ("extended initializer lists");
6425
6426 /* An assignment may not appear in a
6427 constant-expression. */
6428 if (cp_parser_non_integral_constant_expression (parser,
6429 "an assignment"))
6430 return error_mark_node;
6431 /* Build the assignment expression. */
6432 expr = build_x_modify_expr (expr,
6433 assignment_operator,
6434 rhs,
6435 tf_warning_or_error);
6436 }
6437 }
6438 }
6439
6440 return expr;
6441 }
6442
6443 /* Parse an (optional) assignment-operator.
6444
6445 assignment-operator: one of
6446 = *= /= %= += -= >>= <<= &= ^= |=
6447
6448 GNU Extension:
6449
6450 assignment-operator: one of
6451 <?= >?=
6452
6453 If the next token is an assignment operator, the corresponding tree
6454 code is returned, and the token is consumed. For example, for
6455 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6456 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6457 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6458 operator, ERROR_MARK is returned. */
6459
6460 static enum tree_code
6461 cp_parser_assignment_operator_opt (cp_parser* parser)
6462 {
6463 enum tree_code op;
6464 cp_token *token;
6465
6466 /* Peek at the next token. */
6467 token = cp_lexer_peek_token (parser->lexer);
6468
6469 switch (token->type)
6470 {
6471 case CPP_EQ:
6472 op = NOP_EXPR;
6473 break;
6474
6475 case CPP_MULT_EQ:
6476 op = MULT_EXPR;
6477 break;
6478
6479 case CPP_DIV_EQ:
6480 op = TRUNC_DIV_EXPR;
6481 break;
6482
6483 case CPP_MOD_EQ:
6484 op = TRUNC_MOD_EXPR;
6485 break;
6486
6487 case CPP_PLUS_EQ:
6488 op = PLUS_EXPR;
6489 break;
6490
6491 case CPP_MINUS_EQ:
6492 op = MINUS_EXPR;
6493 break;
6494
6495 case CPP_RSHIFT_EQ:
6496 op = RSHIFT_EXPR;
6497 break;
6498
6499 case CPP_LSHIFT_EQ:
6500 op = LSHIFT_EXPR;
6501 break;
6502
6503 case CPP_AND_EQ:
6504 op = BIT_AND_EXPR;
6505 break;
6506
6507 case CPP_XOR_EQ:
6508 op = BIT_XOR_EXPR;
6509 break;
6510
6511 case CPP_OR_EQ:
6512 op = BIT_IOR_EXPR;
6513 break;
6514
6515 default:
6516 /* Nothing else is an assignment operator. */
6517 op = ERROR_MARK;
6518 }
6519
6520 /* If it was an assignment operator, consume it. */
6521 if (op != ERROR_MARK)
6522 cp_lexer_consume_token (parser->lexer);
6523
6524 return op;
6525 }
6526
6527 /* Parse an expression.
6528
6529 expression:
6530 assignment-expression
6531 expression , assignment-expression
6532
6533 CAST_P is true if this expression is the target of a cast.
6534
6535 Returns a representation of the expression. */
6536
6537 static tree
6538 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6539 {
6540 tree expression = NULL_TREE;
6541
6542 while (true)
6543 {
6544 tree assignment_expression;
6545
6546 /* Parse the next assignment-expression. */
6547 assignment_expression
6548 = cp_parser_assignment_expression (parser, cast_p, pidk);
6549 /* If this is the first assignment-expression, we can just
6550 save it away. */
6551 if (!expression)
6552 expression = assignment_expression;
6553 else
6554 expression = build_x_compound_expr (expression,
6555 assignment_expression,
6556 tf_warning_or_error);
6557 /* If the next token is not a comma, then we are done with the
6558 expression. */
6559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6560 break;
6561 /* Consume the `,'. */
6562 cp_lexer_consume_token (parser->lexer);
6563 /* A comma operator cannot appear in a constant-expression. */
6564 if (cp_parser_non_integral_constant_expression (parser,
6565 "a comma operator"))
6566 expression = error_mark_node;
6567 }
6568
6569 return expression;
6570 }
6571
6572 /* Parse a constant-expression.
6573
6574 constant-expression:
6575 conditional-expression
6576
6577 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6578 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6579 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6580 is false, NON_CONSTANT_P should be NULL. */
6581
6582 static tree
6583 cp_parser_constant_expression (cp_parser* parser,
6584 bool allow_non_constant_p,
6585 bool *non_constant_p)
6586 {
6587 bool saved_integral_constant_expression_p;
6588 bool saved_allow_non_integral_constant_expression_p;
6589 bool saved_non_integral_constant_expression_p;
6590 tree expression;
6591
6592 /* It might seem that we could simply parse the
6593 conditional-expression, and then check to see if it were
6594 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6595 one that the compiler can figure out is constant, possibly after
6596 doing some simplifications or optimizations. The standard has a
6597 precise definition of constant-expression, and we must honor
6598 that, even though it is somewhat more restrictive.
6599
6600 For example:
6601
6602 int i[(2, 3)];
6603
6604 is not a legal declaration, because `(2, 3)' is not a
6605 constant-expression. The `,' operator is forbidden in a
6606 constant-expression. However, GCC's constant-folding machinery
6607 will fold this operation to an INTEGER_CST for `3'. */
6608
6609 /* Save the old settings. */
6610 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6611 saved_allow_non_integral_constant_expression_p
6612 = parser->allow_non_integral_constant_expression_p;
6613 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6614 /* We are now parsing a constant-expression. */
6615 parser->integral_constant_expression_p = true;
6616 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6617 parser->non_integral_constant_expression_p = false;
6618 /* Although the grammar says "conditional-expression", we parse an
6619 "assignment-expression", which also permits "throw-expression"
6620 and the use of assignment operators. In the case that
6621 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6622 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6623 actually essential that we look for an assignment-expression.
6624 For example, cp_parser_initializer_clauses uses this function to
6625 determine whether a particular assignment-expression is in fact
6626 constant. */
6627 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6628 /* Restore the old settings. */
6629 parser->integral_constant_expression_p
6630 = saved_integral_constant_expression_p;
6631 parser->allow_non_integral_constant_expression_p
6632 = saved_allow_non_integral_constant_expression_p;
6633 if (allow_non_constant_p)
6634 *non_constant_p = parser->non_integral_constant_expression_p;
6635 else if (parser->non_integral_constant_expression_p)
6636 expression = error_mark_node;
6637 parser->non_integral_constant_expression_p
6638 = saved_non_integral_constant_expression_p;
6639
6640 return expression;
6641 }
6642
6643 /* Parse __builtin_offsetof.
6644
6645 offsetof-expression:
6646 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6647
6648 offsetof-member-designator:
6649 id-expression
6650 | offsetof-member-designator "." id-expression
6651 | offsetof-member-designator "[" expression "]"
6652 | offsetof-member-designator "->" id-expression */
6653
6654 static tree
6655 cp_parser_builtin_offsetof (cp_parser *parser)
6656 {
6657 int save_ice_p, save_non_ice_p;
6658 tree type, expr;
6659 cp_id_kind dummy;
6660 cp_token *token;
6661
6662 /* We're about to accept non-integral-constant things, but will
6663 definitely yield an integral constant expression. Save and
6664 restore these values around our local parsing. */
6665 save_ice_p = parser->integral_constant_expression_p;
6666 save_non_ice_p = parser->non_integral_constant_expression_p;
6667
6668 /* Consume the "__builtin_offsetof" token. */
6669 cp_lexer_consume_token (parser->lexer);
6670 /* Consume the opening `('. */
6671 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6672 /* Parse the type-id. */
6673 type = cp_parser_type_id (parser);
6674 /* Look for the `,'. */
6675 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6676 token = cp_lexer_peek_token (parser->lexer);
6677
6678 /* Build the (type *)null that begins the traditional offsetof macro. */
6679 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6680 tf_warning_or_error);
6681
6682 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6683 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6684 true, &dummy, token->location);
6685 while (true)
6686 {
6687 token = cp_lexer_peek_token (parser->lexer);
6688 switch (token->type)
6689 {
6690 case CPP_OPEN_SQUARE:
6691 /* offsetof-member-designator "[" expression "]" */
6692 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6693 break;
6694
6695 case CPP_DEREF:
6696 /* offsetof-member-designator "->" identifier */
6697 expr = grok_array_decl (expr, integer_zero_node);
6698 /* FALLTHRU */
6699
6700 case CPP_DOT:
6701 /* offsetof-member-designator "." identifier */
6702 cp_lexer_consume_token (parser->lexer);
6703 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6704 expr, true, &dummy,
6705 token->location);
6706 break;
6707
6708 case CPP_CLOSE_PAREN:
6709 /* Consume the ")" token. */
6710 cp_lexer_consume_token (parser->lexer);
6711 goto success;
6712
6713 default:
6714 /* Error. We know the following require will fail, but
6715 that gives the proper error message. */
6716 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6717 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6718 expr = error_mark_node;
6719 goto failure;
6720 }
6721 }
6722
6723 success:
6724 /* If we're processing a template, we can't finish the semantics yet.
6725 Otherwise we can fold the entire expression now. */
6726 if (processing_template_decl)
6727 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6728 else
6729 expr = finish_offsetof (expr);
6730
6731 failure:
6732 parser->integral_constant_expression_p = save_ice_p;
6733 parser->non_integral_constant_expression_p = save_non_ice_p;
6734
6735 return expr;
6736 }
6737
6738 /* Parse a trait expression. */
6739
6740 static tree
6741 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6742 {
6743 cp_trait_kind kind;
6744 tree type1, type2 = NULL_TREE;
6745 bool binary = false;
6746 cp_decl_specifier_seq decl_specs;
6747
6748 switch (keyword)
6749 {
6750 case RID_HAS_NOTHROW_ASSIGN:
6751 kind = CPTK_HAS_NOTHROW_ASSIGN;
6752 break;
6753 case RID_HAS_NOTHROW_CONSTRUCTOR:
6754 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6755 break;
6756 case RID_HAS_NOTHROW_COPY:
6757 kind = CPTK_HAS_NOTHROW_COPY;
6758 break;
6759 case RID_HAS_TRIVIAL_ASSIGN:
6760 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6761 break;
6762 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6763 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6764 break;
6765 case RID_HAS_TRIVIAL_COPY:
6766 kind = CPTK_HAS_TRIVIAL_COPY;
6767 break;
6768 case RID_HAS_TRIVIAL_DESTRUCTOR:
6769 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6770 break;
6771 case RID_HAS_VIRTUAL_DESTRUCTOR:
6772 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6773 break;
6774 case RID_IS_ABSTRACT:
6775 kind = CPTK_IS_ABSTRACT;
6776 break;
6777 case RID_IS_BASE_OF:
6778 kind = CPTK_IS_BASE_OF;
6779 binary = true;
6780 break;
6781 case RID_IS_CLASS:
6782 kind = CPTK_IS_CLASS;
6783 break;
6784 case RID_IS_CONVERTIBLE_TO:
6785 kind = CPTK_IS_CONVERTIBLE_TO;
6786 binary = true;
6787 break;
6788 case RID_IS_EMPTY:
6789 kind = CPTK_IS_EMPTY;
6790 break;
6791 case RID_IS_ENUM:
6792 kind = CPTK_IS_ENUM;
6793 break;
6794 case RID_IS_POD:
6795 kind = CPTK_IS_POD;
6796 break;
6797 case RID_IS_POLYMORPHIC:
6798 kind = CPTK_IS_POLYMORPHIC;
6799 break;
6800 case RID_IS_UNION:
6801 kind = CPTK_IS_UNION;
6802 break;
6803 default:
6804 gcc_unreachable ();
6805 }
6806
6807 /* Consume the token. */
6808 cp_lexer_consume_token (parser->lexer);
6809
6810 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6811
6812 type1 = cp_parser_type_id (parser);
6813
6814 if (type1 == error_mark_node)
6815 return error_mark_node;
6816
6817 /* Build a trivial decl-specifier-seq. */
6818 clear_decl_specs (&decl_specs);
6819 decl_specs.type = type1;
6820
6821 /* Call grokdeclarator to figure out what type this is. */
6822 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6823 /*initialized=*/0, /*attrlist=*/NULL);
6824
6825 if (binary)
6826 {
6827 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6828
6829 type2 = cp_parser_type_id (parser);
6830
6831 if (type2 == error_mark_node)
6832 return error_mark_node;
6833
6834 /* Build a trivial decl-specifier-seq. */
6835 clear_decl_specs (&decl_specs);
6836 decl_specs.type = type2;
6837
6838 /* Call grokdeclarator to figure out what type this is. */
6839 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6840 /*initialized=*/0, /*attrlist=*/NULL);
6841 }
6842
6843 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6844
6845 /* Complete the trait expression, which may mean either processing
6846 the trait expr now or saving it for template instantiation. */
6847 return finish_trait_expr (kind, type1, type2);
6848 }
6849
6850 /* Statements [gram.stmt.stmt] */
6851
6852 /* Parse a statement.
6853
6854 statement:
6855 labeled-statement
6856 expression-statement
6857 compound-statement
6858 selection-statement
6859 iteration-statement
6860 jump-statement
6861 declaration-statement
6862 try-block
6863
6864 IN_COMPOUND is true when the statement is nested inside a
6865 cp_parser_compound_statement; this matters for certain pragmas.
6866
6867 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6868 is a (possibly labeled) if statement which is not enclosed in braces
6869 and has an else clause. This is used to implement -Wparentheses. */
6870
6871 static void
6872 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6873 bool in_compound, bool *if_p)
6874 {
6875 tree statement;
6876 cp_token *token;
6877 location_t statement_location;
6878
6879 restart:
6880 if (if_p != NULL)
6881 *if_p = false;
6882 /* There is no statement yet. */
6883 statement = NULL_TREE;
6884 /* Peek at the next token. */
6885 token = cp_lexer_peek_token (parser->lexer);
6886 /* Remember the location of the first token in the statement. */
6887 statement_location = token->location;
6888 /* If this is a keyword, then that will often determine what kind of
6889 statement we have. */
6890 if (token->type == CPP_KEYWORD)
6891 {
6892 enum rid keyword = token->keyword;
6893
6894 switch (keyword)
6895 {
6896 case RID_CASE:
6897 case RID_DEFAULT:
6898 /* Looks like a labeled-statement with a case label.
6899 Parse the label, and then use tail recursion to parse
6900 the statement. */
6901 cp_parser_label_for_labeled_statement (parser);
6902 goto restart;
6903
6904 case RID_IF:
6905 case RID_SWITCH:
6906 statement = cp_parser_selection_statement (parser, if_p);
6907 break;
6908
6909 case RID_WHILE:
6910 case RID_DO:
6911 case RID_FOR:
6912 statement = cp_parser_iteration_statement (parser);
6913 break;
6914
6915 case RID_BREAK:
6916 case RID_CONTINUE:
6917 case RID_RETURN:
6918 case RID_GOTO:
6919 statement = cp_parser_jump_statement (parser);
6920 break;
6921
6922 /* Objective-C++ exception-handling constructs. */
6923 case RID_AT_TRY:
6924 case RID_AT_CATCH:
6925 case RID_AT_FINALLY:
6926 case RID_AT_SYNCHRONIZED:
6927 case RID_AT_THROW:
6928 statement = cp_parser_objc_statement (parser);
6929 break;
6930
6931 case RID_TRY:
6932 statement = cp_parser_try_block (parser);
6933 break;
6934
6935 case RID_NAMESPACE:
6936 /* This must be a namespace alias definition. */
6937 cp_parser_declaration_statement (parser);
6938 return;
6939
6940 default:
6941 /* It might be a keyword like `int' that can start a
6942 declaration-statement. */
6943 break;
6944 }
6945 }
6946 else if (token->type == CPP_NAME)
6947 {
6948 /* If the next token is a `:', then we are looking at a
6949 labeled-statement. */
6950 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6951 if (token->type == CPP_COLON)
6952 {
6953 /* Looks like a labeled-statement with an ordinary label.
6954 Parse the label, and then use tail recursion to parse
6955 the statement. */
6956 cp_parser_label_for_labeled_statement (parser);
6957 goto restart;
6958 }
6959 }
6960 /* Anything that starts with a `{' must be a compound-statement. */
6961 else if (token->type == CPP_OPEN_BRACE)
6962 statement = cp_parser_compound_statement (parser, NULL, false);
6963 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6964 a statement all its own. */
6965 else if (token->type == CPP_PRAGMA)
6966 {
6967 /* Only certain OpenMP pragmas are attached to statements, and thus
6968 are considered statements themselves. All others are not. In
6969 the context of a compound, accept the pragma as a "statement" and
6970 return so that we can check for a close brace. Otherwise we
6971 require a real statement and must go back and read one. */
6972 if (in_compound)
6973 cp_parser_pragma (parser, pragma_compound);
6974 else if (!cp_parser_pragma (parser, pragma_stmt))
6975 goto restart;
6976 return;
6977 }
6978 else if (token->type == CPP_EOF)
6979 {
6980 cp_parser_error (parser, "expected statement");
6981 return;
6982 }
6983
6984 /* Everything else must be a declaration-statement or an
6985 expression-statement. Try for the declaration-statement
6986 first, unless we are looking at a `;', in which case we know that
6987 we have an expression-statement. */
6988 if (!statement)
6989 {
6990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6991 {
6992 cp_parser_parse_tentatively (parser);
6993 /* Try to parse the declaration-statement. */
6994 cp_parser_declaration_statement (parser);
6995 /* If that worked, we're done. */
6996 if (cp_parser_parse_definitely (parser))
6997 return;
6998 }
6999 /* Look for an expression-statement instead. */
7000 statement = cp_parser_expression_statement (parser, in_statement_expr);
7001 }
7002
7003 /* Set the line number for the statement. */
7004 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7005 SET_EXPR_LOCATION (statement, statement_location);
7006 }
7007
7008 /* Parse the label for a labeled-statement, i.e.
7009
7010 identifier :
7011 case constant-expression :
7012 default :
7013
7014 GNU Extension:
7015 case constant-expression ... constant-expression : statement
7016
7017 When a label is parsed without errors, the label is added to the
7018 parse tree by the finish_* functions, so this function doesn't
7019 have to return the label. */
7020
7021 static void
7022 cp_parser_label_for_labeled_statement (cp_parser* parser)
7023 {
7024 cp_token *token;
7025
7026 /* The next token should be an identifier. */
7027 token = cp_lexer_peek_token (parser->lexer);
7028 if (token->type != CPP_NAME
7029 && token->type != CPP_KEYWORD)
7030 {
7031 cp_parser_error (parser, "expected labeled-statement");
7032 return;
7033 }
7034
7035 switch (token->keyword)
7036 {
7037 case RID_CASE:
7038 {
7039 tree expr, expr_hi;
7040 cp_token *ellipsis;
7041
7042 /* Consume the `case' token. */
7043 cp_lexer_consume_token (parser->lexer);
7044 /* Parse the constant-expression. */
7045 expr = cp_parser_constant_expression (parser,
7046 /*allow_non_constant_p=*/false,
7047 NULL);
7048
7049 ellipsis = cp_lexer_peek_token (parser->lexer);
7050 if (ellipsis->type == CPP_ELLIPSIS)
7051 {
7052 /* Consume the `...' token. */
7053 cp_lexer_consume_token (parser->lexer);
7054 expr_hi =
7055 cp_parser_constant_expression (parser,
7056 /*allow_non_constant_p=*/false,
7057 NULL);
7058 /* We don't need to emit warnings here, as the common code
7059 will do this for us. */
7060 }
7061 else
7062 expr_hi = NULL_TREE;
7063
7064 if (parser->in_switch_statement_p)
7065 finish_case_label (expr, expr_hi);
7066 else
7067 error ("%Hcase label %qE not within a switch statement",
7068 &token->location, expr);
7069 }
7070 break;
7071
7072 case RID_DEFAULT:
7073 /* Consume the `default' token. */
7074 cp_lexer_consume_token (parser->lexer);
7075
7076 if (parser->in_switch_statement_p)
7077 finish_case_label (NULL_TREE, NULL_TREE);
7078 else
7079 error ("%Hcase label not within a switch statement", &token->location);
7080 break;
7081
7082 default:
7083 /* Anything else must be an ordinary label. */
7084 finish_label_stmt (cp_parser_identifier (parser));
7085 break;
7086 }
7087
7088 /* Require the `:' token. */
7089 cp_parser_require (parser, CPP_COLON, "%<:%>");
7090 }
7091
7092 /* Parse an expression-statement.
7093
7094 expression-statement:
7095 expression [opt] ;
7096
7097 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7098 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7099 indicates whether this expression-statement is part of an
7100 expression statement. */
7101
7102 static tree
7103 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7104 {
7105 tree statement = NULL_TREE;
7106
7107 /* If the next token is a ';', then there is no expression
7108 statement. */
7109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7110 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7111
7112 /* Consume the final `;'. */
7113 cp_parser_consume_semicolon_at_end_of_statement (parser);
7114
7115 if (in_statement_expr
7116 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7117 /* This is the final expression statement of a statement
7118 expression. */
7119 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7120 else if (statement)
7121 statement = finish_expr_stmt (statement);
7122 else
7123 finish_stmt ();
7124
7125 return statement;
7126 }
7127
7128 /* Parse a compound-statement.
7129
7130 compound-statement:
7131 { statement-seq [opt] }
7132
7133 GNU extension:
7134
7135 compound-statement:
7136 { label-declaration-seq [opt] statement-seq [opt] }
7137
7138 label-declaration-seq:
7139 label-declaration
7140 label-declaration-seq label-declaration
7141
7142 Returns a tree representing the statement. */
7143
7144 static tree
7145 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7146 bool in_try)
7147 {
7148 tree compound_stmt;
7149
7150 /* Consume the `{'. */
7151 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7152 return error_mark_node;
7153 /* Begin the compound-statement. */
7154 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7155 /* If the next keyword is `__label__' we have a label declaration. */
7156 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7157 cp_parser_label_declaration (parser);
7158 /* Parse an (optional) statement-seq. */
7159 cp_parser_statement_seq_opt (parser, in_statement_expr);
7160 /* Finish the compound-statement. */
7161 finish_compound_stmt (compound_stmt);
7162 /* Consume the `}'. */
7163 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7164
7165 return compound_stmt;
7166 }
7167
7168 /* Parse an (optional) statement-seq.
7169
7170 statement-seq:
7171 statement
7172 statement-seq [opt] statement */
7173
7174 static void
7175 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7176 {
7177 /* Scan statements until there aren't any more. */
7178 while (true)
7179 {
7180 cp_token *token = cp_lexer_peek_token (parser->lexer);
7181
7182 /* If we're looking at a `}', then we've run out of statements. */
7183 if (token->type == CPP_CLOSE_BRACE
7184 || token->type == CPP_EOF
7185 || token->type == CPP_PRAGMA_EOL)
7186 break;
7187
7188 /* If we are in a compound statement and find 'else' then
7189 something went wrong. */
7190 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7191 {
7192 if (parser->in_statement & IN_IF_STMT)
7193 break;
7194 else
7195 {
7196 token = cp_lexer_consume_token (parser->lexer);
7197 error ("%H%<else%> without a previous %<if%>", &token->location);
7198 }
7199 }
7200
7201 /* Parse the statement. */
7202 cp_parser_statement (parser, in_statement_expr, true, NULL);
7203 }
7204 }
7205
7206 /* Parse a selection-statement.
7207
7208 selection-statement:
7209 if ( condition ) statement
7210 if ( condition ) statement else statement
7211 switch ( condition ) statement
7212
7213 Returns the new IF_STMT or SWITCH_STMT.
7214
7215 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7216 is a (possibly labeled) if statement which is not enclosed in
7217 braces and has an else clause. This is used to implement
7218 -Wparentheses. */
7219
7220 static tree
7221 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7222 {
7223 cp_token *token;
7224 enum rid keyword;
7225
7226 if (if_p != NULL)
7227 *if_p = false;
7228
7229 /* Peek at the next token. */
7230 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7231
7232 /* See what kind of keyword it is. */
7233 keyword = token->keyword;
7234 switch (keyword)
7235 {
7236 case RID_IF:
7237 case RID_SWITCH:
7238 {
7239 tree statement;
7240 tree condition;
7241
7242 /* Look for the `('. */
7243 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7244 {
7245 cp_parser_skip_to_end_of_statement (parser);
7246 return error_mark_node;
7247 }
7248
7249 /* Begin the selection-statement. */
7250 if (keyword == RID_IF)
7251 statement = begin_if_stmt ();
7252 else
7253 statement = begin_switch_stmt ();
7254
7255 /* Parse the condition. */
7256 condition = cp_parser_condition (parser);
7257 /* Look for the `)'. */
7258 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7259 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7260 /*consume_paren=*/true);
7261
7262 if (keyword == RID_IF)
7263 {
7264 bool nested_if;
7265 unsigned char in_statement;
7266
7267 /* Add the condition. */
7268 finish_if_stmt_cond (condition, statement);
7269
7270 /* Parse the then-clause. */
7271 in_statement = parser->in_statement;
7272 parser->in_statement |= IN_IF_STMT;
7273 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7274 {
7275 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7276 add_stmt (build_empty_stmt ());
7277 cp_lexer_consume_token (parser->lexer);
7278 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7279 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7280 "empty body in an %<if%> statement");
7281 nested_if = false;
7282 }
7283 else
7284 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7285 parser->in_statement = in_statement;
7286
7287 finish_then_clause (statement);
7288
7289 /* If the next token is `else', parse the else-clause. */
7290 if (cp_lexer_next_token_is_keyword (parser->lexer,
7291 RID_ELSE))
7292 {
7293 /* Consume the `else' keyword. */
7294 cp_lexer_consume_token (parser->lexer);
7295 begin_else_clause (statement);
7296 /* Parse the else-clause. */
7297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7298 {
7299 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7300 OPT_Wempty_body, "suggest braces around "
7301 "empty body in an %<else%> statement");
7302 add_stmt (build_empty_stmt ());
7303 cp_lexer_consume_token (parser->lexer);
7304 }
7305 else
7306 cp_parser_implicitly_scoped_statement (parser, NULL);
7307
7308 finish_else_clause (statement);
7309
7310 /* If we are currently parsing a then-clause, then
7311 IF_P will not be NULL. We set it to true to
7312 indicate that this if statement has an else clause.
7313 This may trigger the Wparentheses warning below
7314 when we get back up to the parent if statement. */
7315 if (if_p != NULL)
7316 *if_p = true;
7317 }
7318 else
7319 {
7320 /* This if statement does not have an else clause. If
7321 NESTED_IF is true, then the then-clause is an if
7322 statement which does have an else clause. We warn
7323 about the potential ambiguity. */
7324 if (nested_if)
7325 warning (OPT_Wparentheses,
7326 ("%Hsuggest explicit braces "
7327 "to avoid ambiguous %<else%>"),
7328 EXPR_LOCUS (statement));
7329 }
7330
7331 /* Now we're all done with the if-statement. */
7332 finish_if_stmt (statement);
7333 }
7334 else
7335 {
7336 bool in_switch_statement_p;
7337 unsigned char in_statement;
7338
7339 /* Add the condition. */
7340 finish_switch_cond (condition, statement);
7341
7342 /* Parse the body of the switch-statement. */
7343 in_switch_statement_p = parser->in_switch_statement_p;
7344 in_statement = parser->in_statement;
7345 parser->in_switch_statement_p = true;
7346 parser->in_statement |= IN_SWITCH_STMT;
7347 cp_parser_implicitly_scoped_statement (parser, NULL);
7348 parser->in_switch_statement_p = in_switch_statement_p;
7349 parser->in_statement = in_statement;
7350
7351 /* Now we're all done with the switch-statement. */
7352 finish_switch_stmt (statement);
7353 }
7354
7355 return statement;
7356 }
7357 break;
7358
7359 default:
7360 cp_parser_error (parser, "expected selection-statement");
7361 return error_mark_node;
7362 }
7363 }
7364
7365 /* Parse a condition.
7366
7367 condition:
7368 expression
7369 type-specifier-seq declarator = initializer-clause
7370 type-specifier-seq declarator braced-init-list
7371
7372 GNU Extension:
7373
7374 condition:
7375 type-specifier-seq declarator asm-specification [opt]
7376 attributes [opt] = assignment-expression
7377
7378 Returns the expression that should be tested. */
7379
7380 static tree
7381 cp_parser_condition (cp_parser* parser)
7382 {
7383 cp_decl_specifier_seq type_specifiers;
7384 const char *saved_message;
7385
7386 /* Try the declaration first. */
7387 cp_parser_parse_tentatively (parser);
7388 /* New types are not allowed in the type-specifier-seq for a
7389 condition. */
7390 saved_message = parser->type_definition_forbidden_message;
7391 parser->type_definition_forbidden_message
7392 = "types may not be defined in conditions";
7393 /* Parse the type-specifier-seq. */
7394 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7395 &type_specifiers);
7396 /* Restore the saved message. */
7397 parser->type_definition_forbidden_message = saved_message;
7398 /* If all is well, we might be looking at a declaration. */
7399 if (!cp_parser_error_occurred (parser))
7400 {
7401 tree decl;
7402 tree asm_specification;
7403 tree attributes;
7404 cp_declarator *declarator;
7405 tree initializer = NULL_TREE;
7406
7407 /* Parse the declarator. */
7408 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7409 /*ctor_dtor_or_conv_p=*/NULL,
7410 /*parenthesized_p=*/NULL,
7411 /*member_p=*/false);
7412 /* Parse the attributes. */
7413 attributes = cp_parser_attributes_opt (parser);
7414 /* Parse the asm-specification. */
7415 asm_specification = cp_parser_asm_specification_opt (parser);
7416 /* If the next token is not an `=' or '{', then we might still be
7417 looking at an expression. For example:
7418
7419 if (A(a).x)
7420
7421 looks like a decl-specifier-seq and a declarator -- but then
7422 there is no `=', so this is an expression. */
7423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7424 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7425 cp_parser_simulate_error (parser);
7426
7427 /* If we did see an `=' or '{', then we are looking at a declaration
7428 for sure. */
7429 if (cp_parser_parse_definitely (parser))
7430 {
7431 tree pushed_scope;
7432 bool non_constant_p;
7433 bool flags = LOOKUP_ONLYCONVERTING;
7434
7435 /* Create the declaration. */
7436 decl = start_decl (declarator, &type_specifiers,
7437 /*initialized_p=*/true,
7438 attributes, /*prefix_attributes=*/NULL_TREE,
7439 &pushed_scope);
7440
7441 /* Parse the initializer. */
7442 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7443 {
7444 initializer = cp_parser_braced_list (parser, &non_constant_p);
7445 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7446 flags = 0;
7447 }
7448 else
7449 {
7450 /* Consume the `='. */
7451 cp_parser_require (parser, CPP_EQ, "%<=%>");
7452 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7453 }
7454 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7455 maybe_warn_cpp0x ("extended initializer lists");
7456
7457 if (!non_constant_p)
7458 initializer = fold_non_dependent_expr (initializer);
7459
7460 /* Process the initializer. */
7461 cp_finish_decl (decl,
7462 initializer, !non_constant_p,
7463 asm_specification,
7464 flags);
7465
7466 if (pushed_scope)
7467 pop_scope (pushed_scope);
7468
7469 return convert_from_reference (decl);
7470 }
7471 }
7472 /* If we didn't even get past the declarator successfully, we are
7473 definitely not looking at a declaration. */
7474 else
7475 cp_parser_abort_tentative_parse (parser);
7476
7477 /* Otherwise, we are looking at an expression. */
7478 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7479 }
7480
7481 /* Parse an iteration-statement.
7482
7483 iteration-statement:
7484 while ( condition ) statement
7485 do statement while ( expression ) ;
7486 for ( for-init-statement condition [opt] ; expression [opt] )
7487 statement
7488
7489 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7490
7491 static tree
7492 cp_parser_iteration_statement (cp_parser* parser)
7493 {
7494 cp_token *token;
7495 enum rid keyword;
7496 tree statement;
7497 unsigned char in_statement;
7498
7499 /* Peek at the next token. */
7500 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7501 if (!token)
7502 return error_mark_node;
7503
7504 /* Remember whether or not we are already within an iteration
7505 statement. */
7506 in_statement = parser->in_statement;
7507
7508 /* See what kind of keyword it is. */
7509 keyword = token->keyword;
7510 switch (keyword)
7511 {
7512 case RID_WHILE:
7513 {
7514 tree condition;
7515
7516 /* Begin the while-statement. */
7517 statement = begin_while_stmt ();
7518 /* Look for the `('. */
7519 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7520 /* Parse the condition. */
7521 condition = cp_parser_condition (parser);
7522 finish_while_stmt_cond (condition, statement);
7523 /* Look for the `)'. */
7524 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7525 /* Parse the dependent statement. */
7526 parser->in_statement = IN_ITERATION_STMT;
7527 cp_parser_already_scoped_statement (parser);
7528 parser->in_statement = in_statement;
7529 /* We're done with the while-statement. */
7530 finish_while_stmt (statement);
7531 }
7532 break;
7533
7534 case RID_DO:
7535 {
7536 tree expression;
7537
7538 /* Begin the do-statement. */
7539 statement = begin_do_stmt ();
7540 /* Parse the body of the do-statement. */
7541 parser->in_statement = IN_ITERATION_STMT;
7542 cp_parser_implicitly_scoped_statement (parser, NULL);
7543 parser->in_statement = in_statement;
7544 finish_do_body (statement);
7545 /* Look for the `while' keyword. */
7546 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7547 /* Look for the `('. */
7548 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7549 /* Parse the expression. */
7550 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7551 /* We're done with the do-statement. */
7552 finish_do_stmt (expression, statement);
7553 /* Look for the `)'. */
7554 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7555 /* Look for the `;'. */
7556 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7557 }
7558 break;
7559
7560 case RID_FOR:
7561 {
7562 tree condition = NULL_TREE;
7563 tree expression = NULL_TREE;
7564
7565 /* Begin the for-statement. */
7566 statement = begin_for_stmt ();
7567 /* Look for the `('. */
7568 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7569 /* Parse the initialization. */
7570 cp_parser_for_init_statement (parser);
7571 finish_for_init_stmt (statement);
7572
7573 /* If there's a condition, process it. */
7574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7575 condition = cp_parser_condition (parser);
7576 finish_for_cond (condition, statement);
7577 /* Look for the `;'. */
7578 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7579
7580 /* If there's an expression, process it. */
7581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7582 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7583 finish_for_expr (expression, statement);
7584 /* Look for the `)'. */
7585 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7586
7587 /* Parse the body of the for-statement. */
7588 parser->in_statement = IN_ITERATION_STMT;
7589 cp_parser_already_scoped_statement (parser);
7590 parser->in_statement = in_statement;
7591
7592 /* We're done with the for-statement. */
7593 finish_for_stmt (statement);
7594 }
7595 break;
7596
7597 default:
7598 cp_parser_error (parser, "expected iteration-statement");
7599 statement = error_mark_node;
7600 break;
7601 }
7602
7603 return statement;
7604 }
7605
7606 /* Parse a for-init-statement.
7607
7608 for-init-statement:
7609 expression-statement
7610 simple-declaration */
7611
7612 static void
7613 cp_parser_for_init_statement (cp_parser* parser)
7614 {
7615 /* If the next token is a `;', then we have an empty
7616 expression-statement. Grammatically, this is also a
7617 simple-declaration, but an invalid one, because it does not
7618 declare anything. Therefore, if we did not handle this case
7619 specially, we would issue an error message about an invalid
7620 declaration. */
7621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7622 {
7623 /* We're going to speculatively look for a declaration, falling back
7624 to an expression, if necessary. */
7625 cp_parser_parse_tentatively (parser);
7626 /* Parse the declaration. */
7627 cp_parser_simple_declaration (parser,
7628 /*function_definition_allowed_p=*/false);
7629 /* If the tentative parse failed, then we shall need to look for an
7630 expression-statement. */
7631 if (cp_parser_parse_definitely (parser))
7632 return;
7633 }
7634
7635 cp_parser_expression_statement (parser, false);
7636 }
7637
7638 /* Parse a jump-statement.
7639
7640 jump-statement:
7641 break ;
7642 continue ;
7643 return expression [opt] ;
7644 return braced-init-list ;
7645 goto identifier ;
7646
7647 GNU extension:
7648
7649 jump-statement:
7650 goto * expression ;
7651
7652 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7653
7654 static tree
7655 cp_parser_jump_statement (cp_parser* parser)
7656 {
7657 tree statement = error_mark_node;
7658 cp_token *token;
7659 enum rid keyword;
7660 unsigned char in_statement;
7661
7662 /* Peek at the next token. */
7663 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7664 if (!token)
7665 return error_mark_node;
7666
7667 /* See what kind of keyword it is. */
7668 keyword = token->keyword;
7669 switch (keyword)
7670 {
7671 case RID_BREAK:
7672 in_statement = parser->in_statement & ~IN_IF_STMT;
7673 switch (in_statement)
7674 {
7675 case 0:
7676 error ("%Hbreak statement not within loop or switch", &token->location);
7677 break;
7678 default:
7679 gcc_assert ((in_statement & IN_SWITCH_STMT)
7680 || in_statement == IN_ITERATION_STMT);
7681 statement = finish_break_stmt ();
7682 break;
7683 case IN_OMP_BLOCK:
7684 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7685 break;
7686 case IN_OMP_FOR:
7687 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7688 break;
7689 }
7690 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7691 break;
7692
7693 case RID_CONTINUE:
7694 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7695 {
7696 case 0:
7697 error ("%Hcontinue statement not within a loop", &token->location);
7698 break;
7699 case IN_ITERATION_STMT:
7700 case IN_OMP_FOR:
7701 statement = finish_continue_stmt ();
7702 break;
7703 case IN_OMP_BLOCK:
7704 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7705 break;
7706 default:
7707 gcc_unreachable ();
7708 }
7709 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7710 break;
7711
7712 case RID_RETURN:
7713 {
7714 tree expr;
7715 bool expr_non_constant_p;
7716
7717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7718 {
7719 maybe_warn_cpp0x ("extended initializer lists");
7720 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7721 }
7722 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7723 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7724 else
7725 /* If the next token is a `;', then there is no
7726 expression. */
7727 expr = NULL_TREE;
7728 /* Build the return-statement. */
7729 statement = finish_return_stmt (expr);
7730 /* Look for the final `;'. */
7731 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7732 }
7733 break;
7734
7735 case RID_GOTO:
7736 /* Create the goto-statement. */
7737 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7738 {
7739 /* Issue a warning about this use of a GNU extension. */
7740 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7741 /* Consume the '*' token. */
7742 cp_lexer_consume_token (parser->lexer);
7743 /* Parse the dependent expression. */
7744 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7745 }
7746 else
7747 finish_goto_stmt (cp_parser_identifier (parser));
7748 /* Look for the final `;'. */
7749 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7750 break;
7751
7752 default:
7753 cp_parser_error (parser, "expected jump-statement");
7754 break;
7755 }
7756
7757 return statement;
7758 }
7759
7760 /* Parse a declaration-statement.
7761
7762 declaration-statement:
7763 block-declaration */
7764
7765 static void
7766 cp_parser_declaration_statement (cp_parser* parser)
7767 {
7768 void *p;
7769
7770 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7771 p = obstack_alloc (&declarator_obstack, 0);
7772
7773 /* Parse the block-declaration. */
7774 cp_parser_block_declaration (parser, /*statement_p=*/true);
7775
7776 /* Free any declarators allocated. */
7777 obstack_free (&declarator_obstack, p);
7778
7779 /* Finish off the statement. */
7780 finish_stmt ();
7781 }
7782
7783 /* Some dependent statements (like `if (cond) statement'), are
7784 implicitly in their own scope. In other words, if the statement is
7785 a single statement (as opposed to a compound-statement), it is
7786 none-the-less treated as if it were enclosed in braces. Any
7787 declarations appearing in the dependent statement are out of scope
7788 after control passes that point. This function parses a statement,
7789 but ensures that is in its own scope, even if it is not a
7790 compound-statement.
7791
7792 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7793 is a (possibly labeled) if statement which is not enclosed in
7794 braces and has an else clause. This is used to implement
7795 -Wparentheses.
7796
7797 Returns the new statement. */
7798
7799 static tree
7800 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7801 {
7802 tree statement;
7803
7804 if (if_p != NULL)
7805 *if_p = false;
7806
7807 /* Mark if () ; with a special NOP_EXPR. */
7808 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7809 {
7810 cp_lexer_consume_token (parser->lexer);
7811 statement = add_stmt (build_empty_stmt ());
7812 }
7813 /* if a compound is opened, we simply parse the statement directly. */
7814 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7815 statement = cp_parser_compound_statement (parser, NULL, false);
7816 /* If the token is not a `{', then we must take special action. */
7817 else
7818 {
7819 /* Create a compound-statement. */
7820 statement = begin_compound_stmt (0);
7821 /* Parse the dependent-statement. */
7822 cp_parser_statement (parser, NULL_TREE, false, if_p);
7823 /* Finish the dummy compound-statement. */
7824 finish_compound_stmt (statement);
7825 }
7826
7827 /* Return the statement. */
7828 return statement;
7829 }
7830
7831 /* For some dependent statements (like `while (cond) statement'), we
7832 have already created a scope. Therefore, even if the dependent
7833 statement is a compound-statement, we do not want to create another
7834 scope. */
7835
7836 static void
7837 cp_parser_already_scoped_statement (cp_parser* parser)
7838 {
7839 /* If the token is a `{', then we must take special action. */
7840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7841 cp_parser_statement (parser, NULL_TREE, false, NULL);
7842 else
7843 {
7844 /* Avoid calling cp_parser_compound_statement, so that we
7845 don't create a new scope. Do everything else by hand. */
7846 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7847 cp_parser_statement_seq_opt (parser, NULL_TREE);
7848 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7849 }
7850 }
7851
7852 /* Declarations [gram.dcl.dcl] */
7853
7854 /* Parse an optional declaration-sequence.
7855
7856 declaration-seq:
7857 declaration
7858 declaration-seq declaration */
7859
7860 static void
7861 cp_parser_declaration_seq_opt (cp_parser* parser)
7862 {
7863 while (true)
7864 {
7865 cp_token *token;
7866
7867 token = cp_lexer_peek_token (parser->lexer);
7868
7869 if (token->type == CPP_CLOSE_BRACE
7870 || token->type == CPP_EOF
7871 || token->type == CPP_PRAGMA_EOL)
7872 break;
7873
7874 if (token->type == CPP_SEMICOLON)
7875 {
7876 /* A declaration consisting of a single semicolon is
7877 invalid. Allow it unless we're being pedantic. */
7878 cp_lexer_consume_token (parser->lexer);
7879 if (!in_system_header)
7880 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7881 continue;
7882 }
7883
7884 /* If we're entering or exiting a region that's implicitly
7885 extern "C", modify the lang context appropriately. */
7886 if (!parser->implicit_extern_c && token->implicit_extern_c)
7887 {
7888 push_lang_context (lang_name_c);
7889 parser->implicit_extern_c = true;
7890 }
7891 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7892 {
7893 pop_lang_context ();
7894 parser->implicit_extern_c = false;
7895 }
7896
7897 if (token->type == CPP_PRAGMA)
7898 {
7899 /* A top-level declaration can consist solely of a #pragma.
7900 A nested declaration cannot, so this is done here and not
7901 in cp_parser_declaration. (A #pragma at block scope is
7902 handled in cp_parser_statement.) */
7903 cp_parser_pragma (parser, pragma_external);
7904 continue;
7905 }
7906
7907 /* Parse the declaration itself. */
7908 cp_parser_declaration (parser);
7909 }
7910 }
7911
7912 /* Parse a declaration.
7913
7914 declaration:
7915 block-declaration
7916 function-definition
7917 template-declaration
7918 explicit-instantiation
7919 explicit-specialization
7920 linkage-specification
7921 namespace-definition
7922
7923 GNU extension:
7924
7925 declaration:
7926 __extension__ declaration */
7927
7928 static void
7929 cp_parser_declaration (cp_parser* parser)
7930 {
7931 cp_token token1;
7932 cp_token token2;
7933 int saved_pedantic;
7934 void *p;
7935
7936 /* Check for the `__extension__' keyword. */
7937 if (cp_parser_extension_opt (parser, &saved_pedantic))
7938 {
7939 /* Parse the qualified declaration. */
7940 cp_parser_declaration (parser);
7941 /* Restore the PEDANTIC flag. */
7942 pedantic = saved_pedantic;
7943
7944 return;
7945 }
7946
7947 /* Try to figure out what kind of declaration is present. */
7948 token1 = *cp_lexer_peek_token (parser->lexer);
7949
7950 if (token1.type != CPP_EOF)
7951 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7952 else
7953 {
7954 token2.type = CPP_EOF;
7955 token2.keyword = RID_MAX;
7956 }
7957
7958 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7959 p = obstack_alloc (&declarator_obstack, 0);
7960
7961 /* If the next token is `extern' and the following token is a string
7962 literal, then we have a linkage specification. */
7963 if (token1.keyword == RID_EXTERN
7964 && cp_parser_is_string_literal (&token2))
7965 cp_parser_linkage_specification (parser);
7966 /* If the next token is `template', then we have either a template
7967 declaration, an explicit instantiation, or an explicit
7968 specialization. */
7969 else if (token1.keyword == RID_TEMPLATE)
7970 {
7971 /* `template <>' indicates a template specialization. */
7972 if (token2.type == CPP_LESS
7973 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7974 cp_parser_explicit_specialization (parser);
7975 /* `template <' indicates a template declaration. */
7976 else if (token2.type == CPP_LESS)
7977 cp_parser_template_declaration (parser, /*member_p=*/false);
7978 /* Anything else must be an explicit instantiation. */
7979 else
7980 cp_parser_explicit_instantiation (parser);
7981 }
7982 /* If the next token is `export', then we have a template
7983 declaration. */
7984 else if (token1.keyword == RID_EXPORT)
7985 cp_parser_template_declaration (parser, /*member_p=*/false);
7986 /* If the next token is `extern', 'static' or 'inline' and the one
7987 after that is `template', we have a GNU extended explicit
7988 instantiation directive. */
7989 else if (cp_parser_allow_gnu_extensions_p (parser)
7990 && (token1.keyword == RID_EXTERN
7991 || token1.keyword == RID_STATIC
7992 || token1.keyword == RID_INLINE)
7993 && token2.keyword == RID_TEMPLATE)
7994 cp_parser_explicit_instantiation (parser);
7995 /* If the next token is `namespace', check for a named or unnamed
7996 namespace definition. */
7997 else if (token1.keyword == RID_NAMESPACE
7998 && (/* A named namespace definition. */
7999 (token2.type == CPP_NAME
8000 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8001 != CPP_EQ))
8002 /* An unnamed namespace definition. */
8003 || token2.type == CPP_OPEN_BRACE
8004 || token2.keyword == RID_ATTRIBUTE))
8005 cp_parser_namespace_definition (parser);
8006 /* An inline (associated) namespace definition. */
8007 else if (token1.keyword == RID_INLINE
8008 && token2.keyword == RID_NAMESPACE)
8009 cp_parser_namespace_definition (parser);
8010 /* Objective-C++ declaration/definition. */
8011 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8012 cp_parser_objc_declaration (parser);
8013 /* We must have either a block declaration or a function
8014 definition. */
8015 else
8016 /* Try to parse a block-declaration, or a function-definition. */
8017 cp_parser_block_declaration (parser, /*statement_p=*/false);
8018
8019 /* Free any declarators allocated. */
8020 obstack_free (&declarator_obstack, p);
8021 }
8022
8023 /* Parse a block-declaration.
8024
8025 block-declaration:
8026 simple-declaration
8027 asm-definition
8028 namespace-alias-definition
8029 using-declaration
8030 using-directive
8031
8032 GNU Extension:
8033
8034 block-declaration:
8035 __extension__ block-declaration
8036
8037 C++0x Extension:
8038
8039 block-declaration:
8040 static_assert-declaration
8041
8042 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8043 part of a declaration-statement. */
8044
8045 static void
8046 cp_parser_block_declaration (cp_parser *parser,
8047 bool statement_p)
8048 {
8049 cp_token *token1;
8050 int saved_pedantic;
8051
8052 /* Check for the `__extension__' keyword. */
8053 if (cp_parser_extension_opt (parser, &saved_pedantic))
8054 {
8055 /* Parse the qualified declaration. */
8056 cp_parser_block_declaration (parser, statement_p);
8057 /* Restore the PEDANTIC flag. */
8058 pedantic = saved_pedantic;
8059
8060 return;
8061 }
8062
8063 /* Peek at the next token to figure out which kind of declaration is
8064 present. */
8065 token1 = cp_lexer_peek_token (parser->lexer);
8066
8067 /* If the next keyword is `asm', we have an asm-definition. */
8068 if (token1->keyword == RID_ASM)
8069 {
8070 if (statement_p)
8071 cp_parser_commit_to_tentative_parse (parser);
8072 cp_parser_asm_definition (parser);
8073 }
8074 /* If the next keyword is `namespace', we have a
8075 namespace-alias-definition. */
8076 else if (token1->keyword == RID_NAMESPACE)
8077 cp_parser_namespace_alias_definition (parser);
8078 /* If the next keyword is `using', we have either a
8079 using-declaration or a using-directive. */
8080 else if (token1->keyword == RID_USING)
8081 {
8082 cp_token *token2;
8083
8084 if (statement_p)
8085 cp_parser_commit_to_tentative_parse (parser);
8086 /* If the token after `using' is `namespace', then we have a
8087 using-directive. */
8088 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8089 if (token2->keyword == RID_NAMESPACE)
8090 cp_parser_using_directive (parser);
8091 /* Otherwise, it's a using-declaration. */
8092 else
8093 cp_parser_using_declaration (parser,
8094 /*access_declaration_p=*/false);
8095 }
8096 /* If the next keyword is `__label__' we have a misplaced label
8097 declaration. */
8098 else if (token1->keyword == RID_LABEL)
8099 {
8100 cp_lexer_consume_token (parser->lexer);
8101 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8102 cp_parser_skip_to_end_of_statement (parser);
8103 /* If the next token is now a `;', consume it. */
8104 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8105 cp_lexer_consume_token (parser->lexer);
8106 }
8107 /* If the next token is `static_assert' we have a static assertion. */
8108 else if (token1->keyword == RID_STATIC_ASSERT)
8109 cp_parser_static_assert (parser, /*member_p=*/false);
8110 /* Anything else must be a simple-declaration. */
8111 else
8112 cp_parser_simple_declaration (parser, !statement_p);
8113 }
8114
8115 /* Parse a simple-declaration.
8116
8117 simple-declaration:
8118 decl-specifier-seq [opt] init-declarator-list [opt] ;
8119
8120 init-declarator-list:
8121 init-declarator
8122 init-declarator-list , init-declarator
8123
8124 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8125 function-definition as a simple-declaration. */
8126
8127 static void
8128 cp_parser_simple_declaration (cp_parser* parser,
8129 bool function_definition_allowed_p)
8130 {
8131 cp_decl_specifier_seq decl_specifiers;
8132 int declares_class_or_enum;
8133 bool saw_declarator;
8134
8135 /* Defer access checks until we know what is being declared; the
8136 checks for names appearing in the decl-specifier-seq should be
8137 done as if we were in the scope of the thing being declared. */
8138 push_deferring_access_checks (dk_deferred);
8139
8140 /* Parse the decl-specifier-seq. We have to keep track of whether
8141 or not the decl-specifier-seq declares a named class or
8142 enumeration type, since that is the only case in which the
8143 init-declarator-list is allowed to be empty.
8144
8145 [dcl.dcl]
8146
8147 In a simple-declaration, the optional init-declarator-list can be
8148 omitted only when declaring a class or enumeration, that is when
8149 the decl-specifier-seq contains either a class-specifier, an
8150 elaborated-type-specifier, or an enum-specifier. */
8151 cp_parser_decl_specifier_seq (parser,
8152 CP_PARSER_FLAGS_OPTIONAL,
8153 &decl_specifiers,
8154 &declares_class_or_enum);
8155 /* We no longer need to defer access checks. */
8156 stop_deferring_access_checks ();
8157
8158 /* In a block scope, a valid declaration must always have a
8159 decl-specifier-seq. By not trying to parse declarators, we can
8160 resolve the declaration/expression ambiguity more quickly. */
8161 if (!function_definition_allowed_p
8162 && !decl_specifiers.any_specifiers_p)
8163 {
8164 cp_parser_error (parser, "expected declaration");
8165 goto done;
8166 }
8167
8168 /* If the next two tokens are both identifiers, the code is
8169 erroneous. The usual cause of this situation is code like:
8170
8171 T t;
8172
8173 where "T" should name a type -- but does not. */
8174 if (!decl_specifiers.type
8175 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8176 {
8177 /* If parsing tentatively, we should commit; we really are
8178 looking at a declaration. */
8179 cp_parser_commit_to_tentative_parse (parser);
8180 /* Give up. */
8181 goto done;
8182 }
8183
8184 /* If we have seen at least one decl-specifier, and the next token
8185 is not a parenthesis, then we must be looking at a declaration.
8186 (After "int (" we might be looking at a functional cast.) */
8187 if (decl_specifiers.any_specifiers_p
8188 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8189 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8190 && !cp_parser_error_occurred (parser))
8191 cp_parser_commit_to_tentative_parse (parser);
8192
8193 /* Keep going until we hit the `;' at the end of the simple
8194 declaration. */
8195 saw_declarator = false;
8196 while (cp_lexer_next_token_is_not (parser->lexer,
8197 CPP_SEMICOLON))
8198 {
8199 cp_token *token;
8200 bool function_definition_p;
8201 tree decl;
8202
8203 if (saw_declarator)
8204 {
8205 /* If we are processing next declarator, coma is expected */
8206 token = cp_lexer_peek_token (parser->lexer);
8207 gcc_assert (token->type == CPP_COMMA);
8208 cp_lexer_consume_token (parser->lexer);
8209 }
8210 else
8211 saw_declarator = true;
8212
8213 /* Parse the init-declarator. */
8214 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8215 /*checks=*/NULL,
8216 function_definition_allowed_p,
8217 /*member_p=*/false,
8218 declares_class_or_enum,
8219 &function_definition_p);
8220 /* If an error occurred while parsing tentatively, exit quickly.
8221 (That usually happens when in the body of a function; each
8222 statement is treated as a declaration-statement until proven
8223 otherwise.) */
8224 if (cp_parser_error_occurred (parser))
8225 goto done;
8226 /* Handle function definitions specially. */
8227 if (function_definition_p)
8228 {
8229 /* If the next token is a `,', then we are probably
8230 processing something like:
8231
8232 void f() {}, *p;
8233
8234 which is erroneous. */
8235 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8236 {
8237 cp_token *token = cp_lexer_peek_token (parser->lexer);
8238 error ("%Hmixing declarations and function-definitions is forbidden",
8239 &token->location);
8240 }
8241 /* Otherwise, we're done with the list of declarators. */
8242 else
8243 {
8244 pop_deferring_access_checks ();
8245 return;
8246 }
8247 }
8248 /* The next token should be either a `,' or a `;'. */
8249 token = cp_lexer_peek_token (parser->lexer);
8250 /* If it's a `,', there are more declarators to come. */
8251 if (token->type == CPP_COMMA)
8252 /* will be consumed next time around */;
8253 /* If it's a `;', we are done. */
8254 else if (token->type == CPP_SEMICOLON)
8255 break;
8256 /* Anything else is an error. */
8257 else
8258 {
8259 /* If we have already issued an error message we don't need
8260 to issue another one. */
8261 if (decl != error_mark_node
8262 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8263 cp_parser_error (parser, "expected %<,%> or %<;%>");
8264 /* Skip tokens until we reach the end of the statement. */
8265 cp_parser_skip_to_end_of_statement (parser);
8266 /* If the next token is now a `;', consume it. */
8267 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8268 cp_lexer_consume_token (parser->lexer);
8269 goto done;
8270 }
8271 /* After the first time around, a function-definition is not
8272 allowed -- even if it was OK at first. For example:
8273
8274 int i, f() {}
8275
8276 is not valid. */
8277 function_definition_allowed_p = false;
8278 }
8279
8280 /* Issue an error message if no declarators are present, and the
8281 decl-specifier-seq does not itself declare a class or
8282 enumeration. */
8283 if (!saw_declarator)
8284 {
8285 if (cp_parser_declares_only_class_p (parser))
8286 shadow_tag (&decl_specifiers);
8287 /* Perform any deferred access checks. */
8288 perform_deferred_access_checks ();
8289 }
8290
8291 /* Consume the `;'. */
8292 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8293
8294 done:
8295 pop_deferring_access_checks ();
8296 }
8297
8298 /* Parse a decl-specifier-seq.
8299
8300 decl-specifier-seq:
8301 decl-specifier-seq [opt] decl-specifier
8302
8303 decl-specifier:
8304 storage-class-specifier
8305 type-specifier
8306 function-specifier
8307 friend
8308 typedef
8309
8310 GNU Extension:
8311
8312 decl-specifier:
8313 attributes
8314
8315 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8316
8317 The parser flags FLAGS is used to control type-specifier parsing.
8318
8319 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8320 flags:
8321
8322 1: one of the decl-specifiers is an elaborated-type-specifier
8323 (i.e., a type declaration)
8324 2: one of the decl-specifiers is an enum-specifier or a
8325 class-specifier (i.e., a type definition)
8326
8327 */
8328
8329 static void
8330 cp_parser_decl_specifier_seq (cp_parser* parser,
8331 cp_parser_flags flags,
8332 cp_decl_specifier_seq *decl_specs,
8333 int* declares_class_or_enum)
8334 {
8335 bool constructor_possible_p = !parser->in_declarator_p;
8336 cp_token *start_token = NULL;
8337
8338 /* Clear DECL_SPECS. */
8339 clear_decl_specs (decl_specs);
8340
8341 /* Assume no class or enumeration type is declared. */
8342 *declares_class_or_enum = 0;
8343
8344 /* Keep reading specifiers until there are no more to read. */
8345 while (true)
8346 {
8347 bool constructor_p;
8348 bool found_decl_spec;
8349 cp_token *token;
8350
8351 /* Peek at the next token. */
8352 token = cp_lexer_peek_token (parser->lexer);
8353
8354 /* Save the first token of the decl spec list for error
8355 reporting. */
8356 if (!start_token)
8357 start_token = token;
8358 /* Handle attributes. */
8359 if (token->keyword == RID_ATTRIBUTE)
8360 {
8361 /* Parse the attributes. */
8362 decl_specs->attributes
8363 = chainon (decl_specs->attributes,
8364 cp_parser_attributes_opt (parser));
8365 continue;
8366 }
8367 /* Assume we will find a decl-specifier keyword. */
8368 found_decl_spec = true;
8369 /* If the next token is an appropriate keyword, we can simply
8370 add it to the list. */
8371 switch (token->keyword)
8372 {
8373 /* decl-specifier:
8374 friend */
8375 case RID_FRIEND:
8376 if (!at_class_scope_p ())
8377 {
8378 error ("%H%<friend%> used outside of class", &token->location);
8379 cp_lexer_purge_token (parser->lexer);
8380 }
8381 else
8382 {
8383 ++decl_specs->specs[(int) ds_friend];
8384 /* Consume the token. */
8385 cp_lexer_consume_token (parser->lexer);
8386 }
8387 break;
8388
8389 /* function-specifier:
8390 inline
8391 virtual
8392 explicit */
8393 case RID_INLINE:
8394 case RID_VIRTUAL:
8395 case RID_EXPLICIT:
8396 cp_parser_function_specifier_opt (parser, decl_specs);
8397 break;
8398
8399 /* decl-specifier:
8400 typedef */
8401 case RID_TYPEDEF:
8402 ++decl_specs->specs[(int) ds_typedef];
8403 /* Consume the token. */
8404 cp_lexer_consume_token (parser->lexer);
8405 /* A constructor declarator cannot appear in a typedef. */
8406 constructor_possible_p = false;
8407 /* The "typedef" keyword can only occur in a declaration; we
8408 may as well commit at this point. */
8409 cp_parser_commit_to_tentative_parse (parser);
8410
8411 if (decl_specs->storage_class != sc_none)
8412 decl_specs->conflicting_specifiers_p = true;
8413 break;
8414
8415 /* storage-class-specifier:
8416 auto
8417 register
8418 static
8419 extern
8420 mutable
8421
8422 GNU Extension:
8423 thread */
8424 case RID_AUTO:
8425 if (cxx_dialect == cxx98)
8426 {
8427 /* Consume the token. */
8428 cp_lexer_consume_token (parser->lexer);
8429
8430 /* Complain about `auto' as a storage specifier, if
8431 we're complaining about C++0x compatibility. */
8432 warning
8433 (OPT_Wc__0x_compat,
8434 "%H%<auto%> will change meaning in C++0x; please remove it",
8435 &token->location);
8436
8437 /* Set the storage class anyway. */
8438 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8439 token->location);
8440 }
8441 else
8442 /* C++0x auto type-specifier. */
8443 found_decl_spec = false;
8444 break;
8445
8446 case RID_REGISTER:
8447 case RID_STATIC:
8448 case RID_EXTERN:
8449 case RID_MUTABLE:
8450 /* Consume the token. */
8451 cp_lexer_consume_token (parser->lexer);
8452 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8453 token->location);
8454 break;
8455 case RID_THREAD:
8456 /* Consume the token. */
8457 cp_lexer_consume_token (parser->lexer);
8458 ++decl_specs->specs[(int) ds_thread];
8459 break;
8460
8461 default:
8462 /* We did not yet find a decl-specifier yet. */
8463 found_decl_spec = false;
8464 break;
8465 }
8466
8467 /* Constructors are a special case. The `S' in `S()' is not a
8468 decl-specifier; it is the beginning of the declarator. */
8469 constructor_p
8470 = (!found_decl_spec
8471 && constructor_possible_p
8472 && (cp_parser_constructor_declarator_p
8473 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8474
8475 /* If we don't have a DECL_SPEC yet, then we must be looking at
8476 a type-specifier. */
8477 if (!found_decl_spec && !constructor_p)
8478 {
8479 int decl_spec_declares_class_or_enum;
8480 bool is_cv_qualifier;
8481 tree type_spec;
8482
8483 type_spec
8484 = cp_parser_type_specifier (parser, flags,
8485 decl_specs,
8486 /*is_declaration=*/true,
8487 &decl_spec_declares_class_or_enum,
8488 &is_cv_qualifier);
8489 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8490
8491 /* If this type-specifier referenced a user-defined type
8492 (a typedef, class-name, etc.), then we can't allow any
8493 more such type-specifiers henceforth.
8494
8495 [dcl.spec]
8496
8497 The longest sequence of decl-specifiers that could
8498 possibly be a type name is taken as the
8499 decl-specifier-seq of a declaration. The sequence shall
8500 be self-consistent as described below.
8501
8502 [dcl.type]
8503
8504 As a general rule, at most one type-specifier is allowed
8505 in the complete decl-specifier-seq of a declaration. The
8506 only exceptions are the following:
8507
8508 -- const or volatile can be combined with any other
8509 type-specifier.
8510
8511 -- signed or unsigned can be combined with char, long,
8512 short, or int.
8513
8514 -- ..
8515
8516 Example:
8517
8518 typedef char* Pc;
8519 void g (const int Pc);
8520
8521 Here, Pc is *not* part of the decl-specifier seq; it's
8522 the declarator. Therefore, once we see a type-specifier
8523 (other than a cv-qualifier), we forbid any additional
8524 user-defined types. We *do* still allow things like `int
8525 int' to be considered a decl-specifier-seq, and issue the
8526 error message later. */
8527 if (type_spec && !is_cv_qualifier)
8528 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8529 /* A constructor declarator cannot follow a type-specifier. */
8530 if (type_spec)
8531 {
8532 constructor_possible_p = false;
8533 found_decl_spec = true;
8534 }
8535 }
8536
8537 /* If we still do not have a DECL_SPEC, then there are no more
8538 decl-specifiers. */
8539 if (!found_decl_spec)
8540 break;
8541
8542 decl_specs->any_specifiers_p = true;
8543 /* After we see one decl-specifier, further decl-specifiers are
8544 always optional. */
8545 flags |= CP_PARSER_FLAGS_OPTIONAL;
8546 }
8547
8548 cp_parser_check_decl_spec (decl_specs, start_token->location);
8549
8550 /* Don't allow a friend specifier with a class definition. */
8551 if (decl_specs->specs[(int) ds_friend] != 0
8552 && (*declares_class_or_enum & 2))
8553 error ("%Hclass definition may not be declared a friend",
8554 &start_token->location);
8555 }
8556
8557 /* Parse an (optional) storage-class-specifier.
8558
8559 storage-class-specifier:
8560 auto
8561 register
8562 static
8563 extern
8564 mutable
8565
8566 GNU Extension:
8567
8568 storage-class-specifier:
8569 thread
8570
8571 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8572
8573 static tree
8574 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8575 {
8576 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8577 {
8578 case RID_AUTO:
8579 if (cxx_dialect != cxx98)
8580 return NULL_TREE;
8581 /* Fall through for C++98. */
8582
8583 case RID_REGISTER:
8584 case RID_STATIC:
8585 case RID_EXTERN:
8586 case RID_MUTABLE:
8587 case RID_THREAD:
8588 /* Consume the token. */
8589 return cp_lexer_consume_token (parser->lexer)->u.value;
8590
8591 default:
8592 return NULL_TREE;
8593 }
8594 }
8595
8596 /* Parse an (optional) function-specifier.
8597
8598 function-specifier:
8599 inline
8600 virtual
8601 explicit
8602
8603 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8604 Updates DECL_SPECS, if it is non-NULL. */
8605
8606 static tree
8607 cp_parser_function_specifier_opt (cp_parser* parser,
8608 cp_decl_specifier_seq *decl_specs)
8609 {
8610 cp_token *token = cp_lexer_peek_token (parser->lexer);
8611 switch (token->keyword)
8612 {
8613 case RID_INLINE:
8614 if (decl_specs)
8615 ++decl_specs->specs[(int) ds_inline];
8616 break;
8617
8618 case RID_VIRTUAL:
8619 /* 14.5.2.3 [temp.mem]
8620
8621 A member function template shall not be virtual. */
8622 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8623 error ("%Htemplates may not be %<virtual%>", &token->location);
8624 else if (decl_specs)
8625 ++decl_specs->specs[(int) ds_virtual];
8626 break;
8627
8628 case RID_EXPLICIT:
8629 if (decl_specs)
8630 ++decl_specs->specs[(int) ds_explicit];
8631 break;
8632
8633 default:
8634 return NULL_TREE;
8635 }
8636
8637 /* Consume the token. */
8638 return cp_lexer_consume_token (parser->lexer)->u.value;
8639 }
8640
8641 /* Parse a linkage-specification.
8642
8643 linkage-specification:
8644 extern string-literal { declaration-seq [opt] }
8645 extern string-literal declaration */
8646
8647 static void
8648 cp_parser_linkage_specification (cp_parser* parser)
8649 {
8650 tree linkage;
8651
8652 /* Look for the `extern' keyword. */
8653 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8654
8655 /* Look for the string-literal. */
8656 linkage = cp_parser_string_literal (parser, false, false);
8657
8658 /* Transform the literal into an identifier. If the literal is a
8659 wide-character string, or contains embedded NULs, then we can't
8660 handle it as the user wants. */
8661 if (strlen (TREE_STRING_POINTER (linkage))
8662 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8663 {
8664 cp_parser_error (parser, "invalid linkage-specification");
8665 /* Assume C++ linkage. */
8666 linkage = lang_name_cplusplus;
8667 }
8668 else
8669 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8670
8671 /* We're now using the new linkage. */
8672 push_lang_context (linkage);
8673
8674 /* If the next token is a `{', then we're using the first
8675 production. */
8676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8677 {
8678 /* Consume the `{' token. */
8679 cp_lexer_consume_token (parser->lexer);
8680 /* Parse the declarations. */
8681 cp_parser_declaration_seq_opt (parser);
8682 /* Look for the closing `}'. */
8683 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8684 }
8685 /* Otherwise, there's just one declaration. */
8686 else
8687 {
8688 bool saved_in_unbraced_linkage_specification_p;
8689
8690 saved_in_unbraced_linkage_specification_p
8691 = parser->in_unbraced_linkage_specification_p;
8692 parser->in_unbraced_linkage_specification_p = true;
8693 cp_parser_declaration (parser);
8694 parser->in_unbraced_linkage_specification_p
8695 = saved_in_unbraced_linkage_specification_p;
8696 }
8697
8698 /* We're done with the linkage-specification. */
8699 pop_lang_context ();
8700 }
8701
8702 /* Parse a static_assert-declaration.
8703
8704 static_assert-declaration:
8705 static_assert ( constant-expression , string-literal ) ;
8706
8707 If MEMBER_P, this static_assert is a class member. */
8708
8709 static void
8710 cp_parser_static_assert(cp_parser *parser, bool member_p)
8711 {
8712 tree condition;
8713 tree message;
8714 cp_token *token;
8715 location_t saved_loc;
8716
8717 /* Peek at the `static_assert' token so we can keep track of exactly
8718 where the static assertion started. */
8719 token = cp_lexer_peek_token (parser->lexer);
8720 saved_loc = token->location;
8721
8722 /* Look for the `static_assert' keyword. */
8723 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8724 "%<static_assert%>"))
8725 return;
8726
8727 /* We know we are in a static assertion; commit to any tentative
8728 parse. */
8729 if (cp_parser_parsing_tentatively (parser))
8730 cp_parser_commit_to_tentative_parse (parser);
8731
8732 /* Parse the `(' starting the static assertion condition. */
8733 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8734
8735 /* Parse the constant-expression. */
8736 condition =
8737 cp_parser_constant_expression (parser,
8738 /*allow_non_constant_p=*/false,
8739 /*non_constant_p=*/NULL);
8740
8741 /* Parse the separating `,'. */
8742 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8743
8744 /* Parse the string-literal message. */
8745 message = cp_parser_string_literal (parser,
8746 /*translate=*/false,
8747 /*wide_ok=*/true);
8748
8749 /* A `)' completes the static assertion. */
8750 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8751 cp_parser_skip_to_closing_parenthesis (parser,
8752 /*recovering=*/true,
8753 /*or_comma=*/false,
8754 /*consume_paren=*/true);
8755
8756 /* A semicolon terminates the declaration. */
8757 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8758
8759 /* Complete the static assertion, which may mean either processing
8760 the static assert now or saving it for template instantiation. */
8761 finish_static_assert (condition, message, saved_loc, member_p);
8762 }
8763
8764 /* Parse a `decltype' type. Returns the type.
8765
8766 simple-type-specifier:
8767 decltype ( expression ) */
8768
8769 static tree
8770 cp_parser_decltype (cp_parser *parser)
8771 {
8772 tree expr;
8773 bool id_expression_or_member_access_p = false;
8774 const char *saved_message;
8775 bool saved_integral_constant_expression_p;
8776 bool saved_non_integral_constant_expression_p;
8777 cp_token *id_expr_start_token;
8778
8779 /* Look for the `decltype' token. */
8780 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8781 return error_mark_node;
8782
8783 /* Types cannot be defined in a `decltype' expression. Save away the
8784 old message. */
8785 saved_message = parser->type_definition_forbidden_message;
8786
8787 /* And create the new one. */
8788 parser->type_definition_forbidden_message
8789 = "types may not be defined in %<decltype%> expressions";
8790
8791 /* The restrictions on constant-expressions do not apply inside
8792 decltype expressions. */
8793 saved_integral_constant_expression_p
8794 = parser->integral_constant_expression_p;
8795 saved_non_integral_constant_expression_p
8796 = parser->non_integral_constant_expression_p;
8797 parser->integral_constant_expression_p = false;
8798
8799 /* Do not actually evaluate the expression. */
8800 ++skip_evaluation;
8801
8802 /* Parse the opening `('. */
8803 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8804 return error_mark_node;
8805
8806 /* First, try parsing an id-expression. */
8807 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8808 cp_parser_parse_tentatively (parser);
8809 expr = cp_parser_id_expression (parser,
8810 /*template_keyword_p=*/false,
8811 /*check_dependency_p=*/true,
8812 /*template_p=*/NULL,
8813 /*declarator_p=*/false,
8814 /*optional_p=*/false);
8815
8816 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8817 {
8818 bool non_integral_constant_expression_p = false;
8819 tree id_expression = expr;
8820 cp_id_kind idk;
8821 const char *error_msg;
8822
8823 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8824 /* Lookup the name we got back from the id-expression. */
8825 expr = cp_parser_lookup_name (parser, expr,
8826 none_type,
8827 /*is_template=*/false,
8828 /*is_namespace=*/false,
8829 /*check_dependency=*/true,
8830 /*ambiguous_decls=*/NULL,
8831 id_expr_start_token->location);
8832
8833 if (expr
8834 && expr != error_mark_node
8835 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8836 && TREE_CODE (expr) != TYPE_DECL
8837 && (TREE_CODE (expr) != BIT_NOT_EXPR
8838 || !TYPE_P (TREE_OPERAND (expr, 0)))
8839 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8840 {
8841 /* Complete lookup of the id-expression. */
8842 expr = (finish_id_expression
8843 (id_expression, expr, parser->scope, &idk,
8844 /*integral_constant_expression_p=*/false,
8845 /*allow_non_integral_constant_expression_p=*/true,
8846 &non_integral_constant_expression_p,
8847 /*template_p=*/false,
8848 /*done=*/true,
8849 /*address_p=*/false,
8850 /*template_arg_p=*/false,
8851 &error_msg,
8852 id_expr_start_token->location));
8853
8854 if (expr == error_mark_node)
8855 /* We found an id-expression, but it was something that we
8856 should not have found. This is an error, not something
8857 we can recover from, so note that we found an
8858 id-expression and we'll recover as gracefully as
8859 possible. */
8860 id_expression_or_member_access_p = true;
8861 }
8862
8863 if (expr
8864 && expr != error_mark_node
8865 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8866 /* We have an id-expression. */
8867 id_expression_or_member_access_p = true;
8868 }
8869
8870 if (!id_expression_or_member_access_p)
8871 {
8872 /* Abort the id-expression parse. */
8873 cp_parser_abort_tentative_parse (parser);
8874
8875 /* Parsing tentatively, again. */
8876 cp_parser_parse_tentatively (parser);
8877
8878 /* Parse a class member access. */
8879 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8880 /*cast_p=*/false,
8881 /*member_access_only_p=*/true, NULL);
8882
8883 if (expr
8884 && expr != error_mark_node
8885 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8886 /* We have an id-expression. */
8887 id_expression_or_member_access_p = true;
8888 }
8889
8890 if (id_expression_or_member_access_p)
8891 /* We have parsed the complete id-expression or member access. */
8892 cp_parser_parse_definitely (parser);
8893 else
8894 {
8895 /* Abort our attempt to parse an id-expression or member access
8896 expression. */
8897 cp_parser_abort_tentative_parse (parser);
8898
8899 /* Parse a full expression. */
8900 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8901 }
8902
8903 /* Go back to evaluating expressions. */
8904 --skip_evaluation;
8905
8906 /* Restore the old message and the integral constant expression
8907 flags. */
8908 parser->type_definition_forbidden_message = saved_message;
8909 parser->integral_constant_expression_p
8910 = saved_integral_constant_expression_p;
8911 parser->non_integral_constant_expression_p
8912 = saved_non_integral_constant_expression_p;
8913
8914 if (expr == error_mark_node)
8915 {
8916 /* Skip everything up to the closing `)'. */
8917 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8918 /*consume_paren=*/true);
8919 return error_mark_node;
8920 }
8921
8922 /* Parse to the closing `)'. */
8923 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8924 {
8925 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8926 /*consume_paren=*/true);
8927 return error_mark_node;
8928 }
8929
8930 return finish_decltype_type (expr, id_expression_or_member_access_p);
8931 }
8932
8933 /* Special member functions [gram.special] */
8934
8935 /* Parse a conversion-function-id.
8936
8937 conversion-function-id:
8938 operator conversion-type-id
8939
8940 Returns an IDENTIFIER_NODE representing the operator. */
8941
8942 static tree
8943 cp_parser_conversion_function_id (cp_parser* parser)
8944 {
8945 tree type;
8946 tree saved_scope;
8947 tree saved_qualifying_scope;
8948 tree saved_object_scope;
8949 tree pushed_scope = NULL_TREE;
8950
8951 /* Look for the `operator' token. */
8952 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8953 return error_mark_node;
8954 /* When we parse the conversion-type-id, the current scope will be
8955 reset. However, we need that information in able to look up the
8956 conversion function later, so we save it here. */
8957 saved_scope = parser->scope;
8958 saved_qualifying_scope = parser->qualifying_scope;
8959 saved_object_scope = parser->object_scope;
8960 /* We must enter the scope of the class so that the names of
8961 entities declared within the class are available in the
8962 conversion-type-id. For example, consider:
8963
8964 struct S {
8965 typedef int I;
8966 operator I();
8967 };
8968
8969 S::operator I() { ... }
8970
8971 In order to see that `I' is a type-name in the definition, we
8972 must be in the scope of `S'. */
8973 if (saved_scope)
8974 pushed_scope = push_scope (saved_scope);
8975 /* Parse the conversion-type-id. */
8976 type = cp_parser_conversion_type_id (parser);
8977 /* Leave the scope of the class, if any. */
8978 if (pushed_scope)
8979 pop_scope (pushed_scope);
8980 /* Restore the saved scope. */
8981 parser->scope = saved_scope;
8982 parser->qualifying_scope = saved_qualifying_scope;
8983 parser->object_scope = saved_object_scope;
8984 /* If the TYPE is invalid, indicate failure. */
8985 if (type == error_mark_node)
8986 return error_mark_node;
8987 return mangle_conv_op_name_for_type (type);
8988 }
8989
8990 /* Parse a conversion-type-id:
8991
8992 conversion-type-id:
8993 type-specifier-seq conversion-declarator [opt]
8994
8995 Returns the TYPE specified. */
8996
8997 static tree
8998 cp_parser_conversion_type_id (cp_parser* parser)
8999 {
9000 tree attributes;
9001 cp_decl_specifier_seq type_specifiers;
9002 cp_declarator *declarator;
9003 tree type_specified;
9004
9005 /* Parse the attributes. */
9006 attributes = cp_parser_attributes_opt (parser);
9007 /* Parse the type-specifiers. */
9008 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9009 &type_specifiers);
9010 /* If that didn't work, stop. */
9011 if (type_specifiers.type == error_mark_node)
9012 return error_mark_node;
9013 /* Parse the conversion-declarator. */
9014 declarator = cp_parser_conversion_declarator_opt (parser);
9015
9016 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9017 /*initialized=*/0, &attributes);
9018 if (attributes)
9019 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9020
9021 /* Don't give this error when parsing tentatively. This happens to
9022 work because we always parse this definitively once. */
9023 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9024 && type_uses_auto (type_specified))
9025 {
9026 error ("invalid use of %<auto%> in conversion operator");
9027 return error_mark_node;
9028 }
9029
9030 return type_specified;
9031 }
9032
9033 /* Parse an (optional) conversion-declarator.
9034
9035 conversion-declarator:
9036 ptr-operator conversion-declarator [opt]
9037
9038 */
9039
9040 static cp_declarator *
9041 cp_parser_conversion_declarator_opt (cp_parser* parser)
9042 {
9043 enum tree_code code;
9044 tree class_type;
9045 cp_cv_quals cv_quals;
9046
9047 /* We don't know if there's a ptr-operator next, or not. */
9048 cp_parser_parse_tentatively (parser);
9049 /* Try the ptr-operator. */
9050 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9051 /* If it worked, look for more conversion-declarators. */
9052 if (cp_parser_parse_definitely (parser))
9053 {
9054 cp_declarator *declarator;
9055
9056 /* Parse another optional declarator. */
9057 declarator = cp_parser_conversion_declarator_opt (parser);
9058
9059 return cp_parser_make_indirect_declarator
9060 (code, class_type, cv_quals, declarator);
9061 }
9062
9063 return NULL;
9064 }
9065
9066 /* Parse an (optional) ctor-initializer.
9067
9068 ctor-initializer:
9069 : mem-initializer-list
9070
9071 Returns TRUE iff the ctor-initializer was actually present. */
9072
9073 static bool
9074 cp_parser_ctor_initializer_opt (cp_parser* parser)
9075 {
9076 /* If the next token is not a `:', then there is no
9077 ctor-initializer. */
9078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9079 {
9080 /* Do default initialization of any bases and members. */
9081 if (DECL_CONSTRUCTOR_P (current_function_decl))
9082 finish_mem_initializers (NULL_TREE);
9083
9084 return false;
9085 }
9086
9087 /* Consume the `:' token. */
9088 cp_lexer_consume_token (parser->lexer);
9089 /* And the mem-initializer-list. */
9090 cp_parser_mem_initializer_list (parser);
9091
9092 return true;
9093 }
9094
9095 /* Parse a mem-initializer-list.
9096
9097 mem-initializer-list:
9098 mem-initializer ... [opt]
9099 mem-initializer ... [opt] , mem-initializer-list */
9100
9101 static void
9102 cp_parser_mem_initializer_list (cp_parser* parser)
9103 {
9104 tree mem_initializer_list = NULL_TREE;
9105 cp_token *token = cp_lexer_peek_token (parser->lexer);
9106
9107 /* Let the semantic analysis code know that we are starting the
9108 mem-initializer-list. */
9109 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9110 error ("%Honly constructors take base initializers",
9111 &token->location);
9112
9113 /* Loop through the list. */
9114 while (true)
9115 {
9116 tree mem_initializer;
9117
9118 token = cp_lexer_peek_token (parser->lexer);
9119 /* Parse the mem-initializer. */
9120 mem_initializer = cp_parser_mem_initializer (parser);
9121 /* If the next token is a `...', we're expanding member initializers. */
9122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9123 {
9124 /* Consume the `...'. */
9125 cp_lexer_consume_token (parser->lexer);
9126
9127 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9128 can be expanded but members cannot. */
9129 if (mem_initializer != error_mark_node
9130 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9131 {
9132 error ("%Hcannot expand initializer for member %<%D%>",
9133 &token->location, TREE_PURPOSE (mem_initializer));
9134 mem_initializer = error_mark_node;
9135 }
9136
9137 /* Construct the pack expansion type. */
9138 if (mem_initializer != error_mark_node)
9139 mem_initializer = make_pack_expansion (mem_initializer);
9140 }
9141 /* Add it to the list, unless it was erroneous. */
9142 if (mem_initializer != error_mark_node)
9143 {
9144 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9145 mem_initializer_list = mem_initializer;
9146 }
9147 /* If the next token is not a `,', we're done. */
9148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9149 break;
9150 /* Consume the `,' token. */
9151 cp_lexer_consume_token (parser->lexer);
9152 }
9153
9154 /* Perform semantic analysis. */
9155 if (DECL_CONSTRUCTOR_P (current_function_decl))
9156 finish_mem_initializers (mem_initializer_list);
9157 }
9158
9159 /* Parse a mem-initializer.
9160
9161 mem-initializer:
9162 mem-initializer-id ( expression-list [opt] )
9163 mem-initializer-id braced-init-list
9164
9165 GNU extension:
9166
9167 mem-initializer:
9168 ( expression-list [opt] )
9169
9170 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9171 class) or FIELD_DECL (for a non-static data member) to initialize;
9172 the TREE_VALUE is the expression-list. An empty initialization
9173 list is represented by void_list_node. */
9174
9175 static tree
9176 cp_parser_mem_initializer (cp_parser* parser)
9177 {
9178 tree mem_initializer_id;
9179 tree expression_list;
9180 tree member;
9181 cp_token *token = cp_lexer_peek_token (parser->lexer);
9182
9183 /* Find out what is being initialized. */
9184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9185 {
9186 permerror (token->location,
9187 "anachronistic old-style base class initializer");
9188 mem_initializer_id = NULL_TREE;
9189 }
9190 else
9191 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9192 member = expand_member_init (mem_initializer_id);
9193 if (member && !DECL_P (member))
9194 in_base_initializer = 1;
9195
9196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9197 {
9198 bool expr_non_constant_p;
9199 maybe_warn_cpp0x ("extended initializer lists");
9200 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9201 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9202 expression_list = build_tree_list (NULL_TREE, expression_list);
9203 }
9204 else
9205 expression_list
9206 = cp_parser_parenthesized_expression_list (parser, false,
9207 /*cast_p=*/false,
9208 /*allow_expansion_p=*/true,
9209 /*non_constant_p=*/NULL);
9210 if (expression_list == error_mark_node)
9211 return error_mark_node;
9212 if (!expression_list)
9213 expression_list = void_type_node;
9214
9215 in_base_initializer = 0;
9216
9217 return member ? build_tree_list (member, expression_list) : error_mark_node;
9218 }
9219
9220 /* Parse a mem-initializer-id.
9221
9222 mem-initializer-id:
9223 :: [opt] nested-name-specifier [opt] class-name
9224 identifier
9225
9226 Returns a TYPE indicating the class to be initializer for the first
9227 production. Returns an IDENTIFIER_NODE indicating the data member
9228 to be initialized for the second production. */
9229
9230 static tree
9231 cp_parser_mem_initializer_id (cp_parser* parser)
9232 {
9233 bool global_scope_p;
9234 bool nested_name_specifier_p;
9235 bool template_p = false;
9236 tree id;
9237
9238 cp_token *token = cp_lexer_peek_token (parser->lexer);
9239
9240 /* `typename' is not allowed in this context ([temp.res]). */
9241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9242 {
9243 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9244 "member initializer is implicitly a type)",
9245 &token->location);
9246 cp_lexer_consume_token (parser->lexer);
9247 }
9248 /* Look for the optional `::' operator. */
9249 global_scope_p
9250 = (cp_parser_global_scope_opt (parser,
9251 /*current_scope_valid_p=*/false)
9252 != NULL_TREE);
9253 /* Look for the optional nested-name-specifier. The simplest way to
9254 implement:
9255
9256 [temp.res]
9257
9258 The keyword `typename' is not permitted in a base-specifier or
9259 mem-initializer; in these contexts a qualified name that
9260 depends on a template-parameter is implicitly assumed to be a
9261 type name.
9262
9263 is to assume that we have seen the `typename' keyword at this
9264 point. */
9265 nested_name_specifier_p
9266 = (cp_parser_nested_name_specifier_opt (parser,
9267 /*typename_keyword_p=*/true,
9268 /*check_dependency_p=*/true,
9269 /*type_p=*/true,
9270 /*is_declaration=*/true)
9271 != NULL_TREE);
9272 if (nested_name_specifier_p)
9273 template_p = cp_parser_optional_template_keyword (parser);
9274 /* If there is a `::' operator or a nested-name-specifier, then we
9275 are definitely looking for a class-name. */
9276 if (global_scope_p || nested_name_specifier_p)
9277 return cp_parser_class_name (parser,
9278 /*typename_keyword_p=*/true,
9279 /*template_keyword_p=*/template_p,
9280 none_type,
9281 /*check_dependency_p=*/true,
9282 /*class_head_p=*/false,
9283 /*is_declaration=*/true);
9284 /* Otherwise, we could also be looking for an ordinary identifier. */
9285 cp_parser_parse_tentatively (parser);
9286 /* Try a class-name. */
9287 id = cp_parser_class_name (parser,
9288 /*typename_keyword_p=*/true,
9289 /*template_keyword_p=*/false,
9290 none_type,
9291 /*check_dependency_p=*/true,
9292 /*class_head_p=*/false,
9293 /*is_declaration=*/true);
9294 /* If we found one, we're done. */
9295 if (cp_parser_parse_definitely (parser))
9296 return id;
9297 /* Otherwise, look for an ordinary identifier. */
9298 return cp_parser_identifier (parser);
9299 }
9300
9301 /* Overloading [gram.over] */
9302
9303 /* Parse an operator-function-id.
9304
9305 operator-function-id:
9306 operator operator
9307
9308 Returns an IDENTIFIER_NODE for the operator which is a
9309 human-readable spelling of the identifier, e.g., `operator +'. */
9310
9311 static tree
9312 cp_parser_operator_function_id (cp_parser* parser)
9313 {
9314 /* Look for the `operator' keyword. */
9315 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9316 return error_mark_node;
9317 /* And then the name of the operator itself. */
9318 return cp_parser_operator (parser);
9319 }
9320
9321 /* Parse an operator.
9322
9323 operator:
9324 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9325 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9326 || ++ -- , ->* -> () []
9327
9328 GNU Extensions:
9329
9330 operator:
9331 <? >? <?= >?=
9332
9333 Returns an IDENTIFIER_NODE for the operator which is a
9334 human-readable spelling of the identifier, e.g., `operator +'. */
9335
9336 static tree
9337 cp_parser_operator (cp_parser* parser)
9338 {
9339 tree id = NULL_TREE;
9340 cp_token *token;
9341
9342 /* Peek at the next token. */
9343 token = cp_lexer_peek_token (parser->lexer);
9344 /* Figure out which operator we have. */
9345 switch (token->type)
9346 {
9347 case CPP_KEYWORD:
9348 {
9349 enum tree_code op;
9350
9351 /* The keyword should be either `new' or `delete'. */
9352 if (token->keyword == RID_NEW)
9353 op = NEW_EXPR;
9354 else if (token->keyword == RID_DELETE)
9355 op = DELETE_EXPR;
9356 else
9357 break;
9358
9359 /* Consume the `new' or `delete' token. */
9360 cp_lexer_consume_token (parser->lexer);
9361
9362 /* Peek at the next token. */
9363 token = cp_lexer_peek_token (parser->lexer);
9364 /* If it's a `[' token then this is the array variant of the
9365 operator. */
9366 if (token->type == CPP_OPEN_SQUARE)
9367 {
9368 /* Consume the `[' token. */
9369 cp_lexer_consume_token (parser->lexer);
9370 /* Look for the `]' token. */
9371 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9372 id = ansi_opname (op == NEW_EXPR
9373 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9374 }
9375 /* Otherwise, we have the non-array variant. */
9376 else
9377 id = ansi_opname (op);
9378
9379 return id;
9380 }
9381
9382 case CPP_PLUS:
9383 id = ansi_opname (PLUS_EXPR);
9384 break;
9385
9386 case CPP_MINUS:
9387 id = ansi_opname (MINUS_EXPR);
9388 break;
9389
9390 case CPP_MULT:
9391 id = ansi_opname (MULT_EXPR);
9392 break;
9393
9394 case CPP_DIV:
9395 id = ansi_opname (TRUNC_DIV_EXPR);
9396 break;
9397
9398 case CPP_MOD:
9399 id = ansi_opname (TRUNC_MOD_EXPR);
9400 break;
9401
9402 case CPP_XOR:
9403 id = ansi_opname (BIT_XOR_EXPR);
9404 break;
9405
9406 case CPP_AND:
9407 id = ansi_opname (BIT_AND_EXPR);
9408 break;
9409
9410 case CPP_OR:
9411 id = ansi_opname (BIT_IOR_EXPR);
9412 break;
9413
9414 case CPP_COMPL:
9415 id = ansi_opname (BIT_NOT_EXPR);
9416 break;
9417
9418 case CPP_NOT:
9419 id = ansi_opname (TRUTH_NOT_EXPR);
9420 break;
9421
9422 case CPP_EQ:
9423 id = ansi_assopname (NOP_EXPR);
9424 break;
9425
9426 case CPP_LESS:
9427 id = ansi_opname (LT_EXPR);
9428 break;
9429
9430 case CPP_GREATER:
9431 id = ansi_opname (GT_EXPR);
9432 break;
9433
9434 case CPP_PLUS_EQ:
9435 id = ansi_assopname (PLUS_EXPR);
9436 break;
9437
9438 case CPP_MINUS_EQ:
9439 id = ansi_assopname (MINUS_EXPR);
9440 break;
9441
9442 case CPP_MULT_EQ:
9443 id = ansi_assopname (MULT_EXPR);
9444 break;
9445
9446 case CPP_DIV_EQ:
9447 id = ansi_assopname (TRUNC_DIV_EXPR);
9448 break;
9449
9450 case CPP_MOD_EQ:
9451 id = ansi_assopname (TRUNC_MOD_EXPR);
9452 break;
9453
9454 case CPP_XOR_EQ:
9455 id = ansi_assopname (BIT_XOR_EXPR);
9456 break;
9457
9458 case CPP_AND_EQ:
9459 id = ansi_assopname (BIT_AND_EXPR);
9460 break;
9461
9462 case CPP_OR_EQ:
9463 id = ansi_assopname (BIT_IOR_EXPR);
9464 break;
9465
9466 case CPP_LSHIFT:
9467 id = ansi_opname (LSHIFT_EXPR);
9468 break;
9469
9470 case CPP_RSHIFT:
9471 id = ansi_opname (RSHIFT_EXPR);
9472 break;
9473
9474 case CPP_LSHIFT_EQ:
9475 id = ansi_assopname (LSHIFT_EXPR);
9476 break;
9477
9478 case CPP_RSHIFT_EQ:
9479 id = ansi_assopname (RSHIFT_EXPR);
9480 break;
9481
9482 case CPP_EQ_EQ:
9483 id = ansi_opname (EQ_EXPR);
9484 break;
9485
9486 case CPP_NOT_EQ:
9487 id = ansi_opname (NE_EXPR);
9488 break;
9489
9490 case CPP_LESS_EQ:
9491 id = ansi_opname (LE_EXPR);
9492 break;
9493
9494 case CPP_GREATER_EQ:
9495 id = ansi_opname (GE_EXPR);
9496 break;
9497
9498 case CPP_AND_AND:
9499 id = ansi_opname (TRUTH_ANDIF_EXPR);
9500 break;
9501
9502 case CPP_OR_OR:
9503 id = ansi_opname (TRUTH_ORIF_EXPR);
9504 break;
9505
9506 case CPP_PLUS_PLUS:
9507 id = ansi_opname (POSTINCREMENT_EXPR);
9508 break;
9509
9510 case CPP_MINUS_MINUS:
9511 id = ansi_opname (PREDECREMENT_EXPR);
9512 break;
9513
9514 case CPP_COMMA:
9515 id = ansi_opname (COMPOUND_EXPR);
9516 break;
9517
9518 case CPP_DEREF_STAR:
9519 id = ansi_opname (MEMBER_REF);
9520 break;
9521
9522 case CPP_DEREF:
9523 id = ansi_opname (COMPONENT_REF);
9524 break;
9525
9526 case CPP_OPEN_PAREN:
9527 /* Consume the `('. */
9528 cp_lexer_consume_token (parser->lexer);
9529 /* Look for the matching `)'. */
9530 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9531 return ansi_opname (CALL_EXPR);
9532
9533 case CPP_OPEN_SQUARE:
9534 /* Consume the `['. */
9535 cp_lexer_consume_token (parser->lexer);
9536 /* Look for the matching `]'. */
9537 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9538 return ansi_opname (ARRAY_REF);
9539
9540 default:
9541 /* Anything else is an error. */
9542 break;
9543 }
9544
9545 /* If we have selected an identifier, we need to consume the
9546 operator token. */
9547 if (id)
9548 cp_lexer_consume_token (parser->lexer);
9549 /* Otherwise, no valid operator name was present. */
9550 else
9551 {
9552 cp_parser_error (parser, "expected operator");
9553 id = error_mark_node;
9554 }
9555
9556 return id;
9557 }
9558
9559 /* Parse a template-declaration.
9560
9561 template-declaration:
9562 export [opt] template < template-parameter-list > declaration
9563
9564 If MEMBER_P is TRUE, this template-declaration occurs within a
9565 class-specifier.
9566
9567 The grammar rule given by the standard isn't correct. What
9568 is really meant is:
9569
9570 template-declaration:
9571 export [opt] template-parameter-list-seq
9572 decl-specifier-seq [opt] init-declarator [opt] ;
9573 export [opt] template-parameter-list-seq
9574 function-definition
9575
9576 template-parameter-list-seq:
9577 template-parameter-list-seq [opt]
9578 template < template-parameter-list > */
9579
9580 static void
9581 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9582 {
9583 /* Check for `export'. */
9584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9585 {
9586 /* Consume the `export' token. */
9587 cp_lexer_consume_token (parser->lexer);
9588 /* Warn that we do not support `export'. */
9589 warning (0, "keyword %<export%> not implemented, and will be ignored");
9590 }
9591
9592 cp_parser_template_declaration_after_export (parser, member_p);
9593 }
9594
9595 /* Parse a template-parameter-list.
9596
9597 template-parameter-list:
9598 template-parameter
9599 template-parameter-list , template-parameter
9600
9601 Returns a TREE_LIST. Each node represents a template parameter.
9602 The nodes are connected via their TREE_CHAINs. */
9603
9604 static tree
9605 cp_parser_template_parameter_list (cp_parser* parser)
9606 {
9607 tree parameter_list = NULL_TREE;
9608
9609 begin_template_parm_list ();
9610 while (true)
9611 {
9612 tree parameter;
9613 bool is_non_type;
9614 bool is_parameter_pack;
9615
9616 /* Parse the template-parameter. */
9617 parameter = cp_parser_template_parameter (parser,
9618 &is_non_type,
9619 &is_parameter_pack);
9620 /* Add it to the list. */
9621 if (parameter != error_mark_node)
9622 parameter_list = process_template_parm (parameter_list,
9623 parameter,
9624 is_non_type,
9625 is_parameter_pack);
9626 else
9627 {
9628 tree err_parm = build_tree_list (parameter, parameter);
9629 TREE_VALUE (err_parm) = error_mark_node;
9630 parameter_list = chainon (parameter_list, err_parm);
9631 }
9632
9633 /* If the next token is not a `,', we're done. */
9634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9635 break;
9636 /* Otherwise, consume the `,' token. */
9637 cp_lexer_consume_token (parser->lexer);
9638 }
9639
9640 return end_template_parm_list (parameter_list);
9641 }
9642
9643 /* Parse a template-parameter.
9644
9645 template-parameter:
9646 type-parameter
9647 parameter-declaration
9648
9649 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9650 the parameter. The TREE_PURPOSE is the default value, if any.
9651 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9652 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9653 set to true iff this parameter is a parameter pack. */
9654
9655 static tree
9656 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9657 bool *is_parameter_pack)
9658 {
9659 cp_token *token;
9660 cp_parameter_declarator *parameter_declarator;
9661 cp_declarator *id_declarator;
9662 tree parm;
9663
9664 /* Assume it is a type parameter or a template parameter. */
9665 *is_non_type = false;
9666 /* Assume it not a parameter pack. */
9667 *is_parameter_pack = false;
9668 /* Peek at the next token. */
9669 token = cp_lexer_peek_token (parser->lexer);
9670 /* If it is `class' or `template', we have a type-parameter. */
9671 if (token->keyword == RID_TEMPLATE)
9672 return cp_parser_type_parameter (parser, is_parameter_pack);
9673 /* If it is `class' or `typename' we do not know yet whether it is a
9674 type parameter or a non-type parameter. Consider:
9675
9676 template <typename T, typename T::X X> ...
9677
9678 or:
9679
9680 template <class C, class D*> ...
9681
9682 Here, the first parameter is a type parameter, and the second is
9683 a non-type parameter. We can tell by looking at the token after
9684 the identifier -- if it is a `,', `=', or `>' then we have a type
9685 parameter. */
9686 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9687 {
9688 /* Peek at the token after `class' or `typename'. */
9689 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9690 /* If it's an ellipsis, we have a template type parameter
9691 pack. */
9692 if (token->type == CPP_ELLIPSIS)
9693 return cp_parser_type_parameter (parser, is_parameter_pack);
9694 /* If it's an identifier, skip it. */
9695 if (token->type == CPP_NAME)
9696 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9697 /* Now, see if the token looks like the end of a template
9698 parameter. */
9699 if (token->type == CPP_COMMA
9700 || token->type == CPP_EQ
9701 || token->type == CPP_GREATER)
9702 return cp_parser_type_parameter (parser, is_parameter_pack);
9703 }
9704
9705 /* Otherwise, it is a non-type parameter.
9706
9707 [temp.param]
9708
9709 When parsing a default template-argument for a non-type
9710 template-parameter, the first non-nested `>' is taken as the end
9711 of the template parameter-list rather than a greater-than
9712 operator. */
9713 *is_non_type = true;
9714 parameter_declarator
9715 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9716 /*parenthesized_p=*/NULL);
9717
9718 /* If the parameter declaration is marked as a parameter pack, set
9719 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9720 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9721 grokdeclarator. */
9722 if (parameter_declarator
9723 && parameter_declarator->declarator
9724 && parameter_declarator->declarator->parameter_pack_p)
9725 {
9726 *is_parameter_pack = true;
9727 parameter_declarator->declarator->parameter_pack_p = false;
9728 }
9729
9730 /* If the next token is an ellipsis, and we don't already have it
9731 marked as a parameter pack, then we have a parameter pack (that
9732 has no declarator). */
9733 if (!*is_parameter_pack
9734 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9735 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9736 {
9737 /* Consume the `...'. */
9738 cp_lexer_consume_token (parser->lexer);
9739 maybe_warn_variadic_templates ();
9740
9741 *is_parameter_pack = true;
9742 }
9743 /* We might end up with a pack expansion as the type of the non-type
9744 template parameter, in which case this is a non-type template
9745 parameter pack. */
9746 else if (parameter_declarator
9747 && parameter_declarator->decl_specifiers.type
9748 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9749 {
9750 *is_parameter_pack = true;
9751 parameter_declarator->decl_specifiers.type =
9752 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9753 }
9754
9755 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9756 {
9757 /* Parameter packs cannot have default arguments. However, a
9758 user may try to do so, so we'll parse them and give an
9759 appropriate diagnostic here. */
9760
9761 /* Consume the `='. */
9762 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9763 cp_lexer_consume_token (parser->lexer);
9764
9765 /* Find the name of the parameter pack. */
9766 id_declarator = parameter_declarator->declarator;
9767 while (id_declarator && id_declarator->kind != cdk_id)
9768 id_declarator = id_declarator->declarator;
9769
9770 if (id_declarator && id_declarator->kind == cdk_id)
9771 error ("%Htemplate parameter pack %qD cannot have a default argument",
9772 &start_token->location, id_declarator->u.id.unqualified_name);
9773 else
9774 error ("%Htemplate parameter pack cannot have a default argument",
9775 &start_token->location);
9776
9777 /* Parse the default argument, but throw away the result. */
9778 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9779 }
9780
9781 parm = grokdeclarator (parameter_declarator->declarator,
9782 &parameter_declarator->decl_specifiers,
9783 PARM, /*initialized=*/0,
9784 /*attrlist=*/NULL);
9785 if (parm == error_mark_node)
9786 return error_mark_node;
9787
9788 return build_tree_list (parameter_declarator->default_argument, parm);
9789 }
9790
9791 /* Parse a type-parameter.
9792
9793 type-parameter:
9794 class identifier [opt]
9795 class identifier [opt] = type-id
9796 typename identifier [opt]
9797 typename identifier [opt] = type-id
9798 template < template-parameter-list > class identifier [opt]
9799 template < template-parameter-list > class identifier [opt]
9800 = id-expression
9801
9802 GNU Extension (variadic templates):
9803
9804 type-parameter:
9805 class ... identifier [opt]
9806 typename ... identifier [opt]
9807
9808 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9809 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9810 the declaration of the parameter.
9811
9812 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9813
9814 static tree
9815 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9816 {
9817 cp_token *token;
9818 tree parameter;
9819
9820 /* Look for a keyword to tell us what kind of parameter this is. */
9821 token = cp_parser_require (parser, CPP_KEYWORD,
9822 "%<class%>, %<typename%>, or %<template%>");
9823 if (!token)
9824 return error_mark_node;
9825
9826 switch (token->keyword)
9827 {
9828 case RID_CLASS:
9829 case RID_TYPENAME:
9830 {
9831 tree identifier;
9832 tree default_argument;
9833
9834 /* If the next token is an ellipsis, we have a template
9835 argument pack. */
9836 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9837 {
9838 /* Consume the `...' token. */
9839 cp_lexer_consume_token (parser->lexer);
9840 maybe_warn_variadic_templates ();
9841
9842 *is_parameter_pack = true;
9843 }
9844
9845 /* If the next token is an identifier, then it names the
9846 parameter. */
9847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9848 identifier = cp_parser_identifier (parser);
9849 else
9850 identifier = NULL_TREE;
9851
9852 /* Create the parameter. */
9853 parameter = finish_template_type_parm (class_type_node, identifier);
9854
9855 /* If the next token is an `=', we have a default argument. */
9856 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9857 {
9858 /* Consume the `=' token. */
9859 cp_lexer_consume_token (parser->lexer);
9860 /* Parse the default-argument. */
9861 push_deferring_access_checks (dk_no_deferred);
9862 default_argument = cp_parser_type_id (parser);
9863
9864 /* Template parameter packs cannot have default
9865 arguments. */
9866 if (*is_parameter_pack)
9867 {
9868 if (identifier)
9869 error ("%Htemplate parameter pack %qD cannot have a "
9870 "default argument", &token->location, identifier);
9871 else
9872 error ("%Htemplate parameter packs cannot have "
9873 "default arguments", &token->location);
9874 default_argument = NULL_TREE;
9875 }
9876 pop_deferring_access_checks ();
9877 }
9878 else
9879 default_argument = NULL_TREE;
9880
9881 /* Create the combined representation of the parameter and the
9882 default argument. */
9883 parameter = build_tree_list (default_argument, parameter);
9884 }
9885 break;
9886
9887 case RID_TEMPLATE:
9888 {
9889 tree parameter_list;
9890 tree identifier;
9891 tree default_argument;
9892
9893 /* Look for the `<'. */
9894 cp_parser_require (parser, CPP_LESS, "%<<%>");
9895 /* Parse the template-parameter-list. */
9896 parameter_list = cp_parser_template_parameter_list (parser);
9897 /* Look for the `>'. */
9898 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9899 /* Look for the `class' keyword. */
9900 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9901 /* If the next token is an ellipsis, we have a template
9902 argument pack. */
9903 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9904 {
9905 /* Consume the `...' token. */
9906 cp_lexer_consume_token (parser->lexer);
9907 maybe_warn_variadic_templates ();
9908
9909 *is_parameter_pack = true;
9910 }
9911 /* If the next token is an `=', then there is a
9912 default-argument. If the next token is a `>', we are at
9913 the end of the parameter-list. If the next token is a `,',
9914 then we are at the end of this parameter. */
9915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9916 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9917 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9918 {
9919 identifier = cp_parser_identifier (parser);
9920 /* Treat invalid names as if the parameter were nameless. */
9921 if (identifier == error_mark_node)
9922 identifier = NULL_TREE;
9923 }
9924 else
9925 identifier = NULL_TREE;
9926
9927 /* Create the template parameter. */
9928 parameter = finish_template_template_parm (class_type_node,
9929 identifier);
9930
9931 /* If the next token is an `=', then there is a
9932 default-argument. */
9933 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9934 {
9935 bool is_template;
9936
9937 /* Consume the `='. */
9938 cp_lexer_consume_token (parser->lexer);
9939 /* Parse the id-expression. */
9940 push_deferring_access_checks (dk_no_deferred);
9941 /* save token before parsing the id-expression, for error
9942 reporting */
9943 token = cp_lexer_peek_token (parser->lexer);
9944 default_argument
9945 = cp_parser_id_expression (parser,
9946 /*template_keyword_p=*/false,
9947 /*check_dependency_p=*/true,
9948 /*template_p=*/&is_template,
9949 /*declarator_p=*/false,
9950 /*optional_p=*/false);
9951 if (TREE_CODE (default_argument) == TYPE_DECL)
9952 /* If the id-expression was a template-id that refers to
9953 a template-class, we already have the declaration here,
9954 so no further lookup is needed. */
9955 ;
9956 else
9957 /* Look up the name. */
9958 default_argument
9959 = cp_parser_lookup_name (parser, default_argument,
9960 none_type,
9961 /*is_template=*/is_template,
9962 /*is_namespace=*/false,
9963 /*check_dependency=*/true,
9964 /*ambiguous_decls=*/NULL,
9965 token->location);
9966 /* See if the default argument is valid. */
9967 default_argument
9968 = check_template_template_default_arg (default_argument);
9969
9970 /* Template parameter packs cannot have default
9971 arguments. */
9972 if (*is_parameter_pack)
9973 {
9974 if (identifier)
9975 error ("%Htemplate parameter pack %qD cannot "
9976 "have a default argument",
9977 &token->location, identifier);
9978 else
9979 error ("%Htemplate parameter packs cannot "
9980 "have default arguments",
9981 &token->location);
9982 default_argument = NULL_TREE;
9983 }
9984 pop_deferring_access_checks ();
9985 }
9986 else
9987 default_argument = NULL_TREE;
9988
9989 /* Create the combined representation of the parameter and the
9990 default argument. */
9991 parameter = build_tree_list (default_argument, parameter);
9992 }
9993 break;
9994
9995 default:
9996 gcc_unreachable ();
9997 break;
9998 }
9999
10000 return parameter;
10001 }
10002
10003 /* Parse a template-id.
10004
10005 template-id:
10006 template-name < template-argument-list [opt] >
10007
10008 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10009 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10010 returned. Otherwise, if the template-name names a function, or set
10011 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10012 names a class, returns a TYPE_DECL for the specialization.
10013
10014 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10015 uninstantiated templates. */
10016
10017 static tree
10018 cp_parser_template_id (cp_parser *parser,
10019 bool template_keyword_p,
10020 bool check_dependency_p,
10021 bool is_declaration)
10022 {
10023 int i;
10024 tree templ;
10025 tree arguments;
10026 tree template_id;
10027 cp_token_position start_of_id = 0;
10028 deferred_access_check *chk;
10029 VEC (deferred_access_check,gc) *access_check;
10030 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10031 bool is_identifier;
10032
10033 /* If the next token corresponds to a template-id, there is no need
10034 to reparse it. */
10035 next_token = cp_lexer_peek_token (parser->lexer);
10036 if (next_token->type == CPP_TEMPLATE_ID)
10037 {
10038 struct tree_check *check_value;
10039
10040 /* Get the stored value. */
10041 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10042 /* Perform any access checks that were deferred. */
10043 access_check = check_value->checks;
10044 if (access_check)
10045 {
10046 for (i = 0 ;
10047 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10048 ++i)
10049 {
10050 perform_or_defer_access_check (chk->binfo,
10051 chk->decl,
10052 chk->diag_decl);
10053 }
10054 }
10055 /* Return the stored value. */
10056 return check_value->value;
10057 }
10058
10059 /* Avoid performing name lookup if there is no possibility of
10060 finding a template-id. */
10061 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10062 || (next_token->type == CPP_NAME
10063 && !cp_parser_nth_token_starts_template_argument_list_p
10064 (parser, 2)))
10065 {
10066 cp_parser_error (parser, "expected template-id");
10067 return error_mark_node;
10068 }
10069
10070 /* Remember where the template-id starts. */
10071 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10072 start_of_id = cp_lexer_token_position (parser->lexer, false);
10073
10074 push_deferring_access_checks (dk_deferred);
10075
10076 /* Parse the template-name. */
10077 is_identifier = false;
10078 token = cp_lexer_peek_token (parser->lexer);
10079 templ = cp_parser_template_name (parser, template_keyword_p,
10080 check_dependency_p,
10081 is_declaration,
10082 &is_identifier);
10083 if (templ == error_mark_node || is_identifier)
10084 {
10085 pop_deferring_access_checks ();
10086 return templ;
10087 }
10088
10089 /* If we find the sequence `[:' after a template-name, it's probably
10090 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10091 parse correctly the argument list. */
10092 next_token = cp_lexer_peek_token (parser->lexer);
10093 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10094 if (next_token->type == CPP_OPEN_SQUARE
10095 && next_token->flags & DIGRAPH
10096 && next_token_2->type == CPP_COLON
10097 && !(next_token_2->flags & PREV_WHITE))
10098 {
10099 cp_parser_parse_tentatively (parser);
10100 /* Change `:' into `::'. */
10101 next_token_2->type = CPP_SCOPE;
10102 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10103 CPP_LESS. */
10104 cp_lexer_consume_token (parser->lexer);
10105
10106 /* Parse the arguments. */
10107 arguments = cp_parser_enclosed_template_argument_list (parser);
10108 if (!cp_parser_parse_definitely (parser))
10109 {
10110 /* If we couldn't parse an argument list, then we revert our changes
10111 and return simply an error. Maybe this is not a template-id
10112 after all. */
10113 next_token_2->type = CPP_COLON;
10114 cp_parser_error (parser, "expected %<<%>");
10115 pop_deferring_access_checks ();
10116 return error_mark_node;
10117 }
10118 /* Otherwise, emit an error about the invalid digraph, but continue
10119 parsing because we got our argument list. */
10120 if (permerror (next_token->location,
10121 "%<<::%> cannot begin a template-argument list"))
10122 {
10123 static bool hint = false;
10124 inform (next_token->location,
10125 "%<<:%> is an alternate spelling for %<[%>."
10126 " Insert whitespace between %<<%> and %<::%>");
10127 if (!hint && !flag_permissive)
10128 {
10129 inform (next_token->location, "(if you use %<-fpermissive%>"
10130 " G++ will accept your code)");
10131 hint = true;
10132 }
10133 }
10134 }
10135 else
10136 {
10137 /* Look for the `<' that starts the template-argument-list. */
10138 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10139 {
10140 pop_deferring_access_checks ();
10141 return error_mark_node;
10142 }
10143 /* Parse the arguments. */
10144 arguments = cp_parser_enclosed_template_argument_list (parser);
10145 }
10146
10147 /* Build a representation of the specialization. */
10148 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10149 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10150 else if (DECL_CLASS_TEMPLATE_P (templ)
10151 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10152 {
10153 bool entering_scope;
10154 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10155 template (rather than some instantiation thereof) only if
10156 is not nested within some other construct. For example, in
10157 "template <typename T> void f(T) { A<T>::", A<T> is just an
10158 instantiation of A. */
10159 entering_scope = (template_parm_scope_p ()
10160 && cp_lexer_next_token_is (parser->lexer,
10161 CPP_SCOPE));
10162 template_id
10163 = finish_template_type (templ, arguments, entering_scope);
10164 }
10165 else
10166 {
10167 /* If it's not a class-template or a template-template, it should be
10168 a function-template. */
10169 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10170 || TREE_CODE (templ) == OVERLOAD
10171 || BASELINK_P (templ)));
10172
10173 template_id = lookup_template_function (templ, arguments);
10174 }
10175
10176 /* If parsing tentatively, replace the sequence of tokens that makes
10177 up the template-id with a CPP_TEMPLATE_ID token. That way,
10178 should we re-parse the token stream, we will not have to repeat
10179 the effort required to do the parse, nor will we issue duplicate
10180 error messages about problems during instantiation of the
10181 template. */
10182 if (start_of_id)
10183 {
10184 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10185
10186 /* Reset the contents of the START_OF_ID token. */
10187 token->type = CPP_TEMPLATE_ID;
10188 /* Retrieve any deferred checks. Do not pop this access checks yet
10189 so the memory will not be reclaimed during token replacing below. */
10190 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10191 token->u.tree_check_value->value = template_id;
10192 token->u.tree_check_value->checks = get_deferred_access_checks ();
10193 token->keyword = RID_MAX;
10194
10195 /* Purge all subsequent tokens. */
10196 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10197
10198 /* ??? Can we actually assume that, if template_id ==
10199 error_mark_node, we will have issued a diagnostic to the
10200 user, as opposed to simply marking the tentative parse as
10201 failed? */
10202 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10203 error ("%Hparse error in template argument list",
10204 &token->location);
10205 }
10206
10207 pop_deferring_access_checks ();
10208 return template_id;
10209 }
10210
10211 /* Parse a template-name.
10212
10213 template-name:
10214 identifier
10215
10216 The standard should actually say:
10217
10218 template-name:
10219 identifier
10220 operator-function-id
10221
10222 A defect report has been filed about this issue.
10223
10224 A conversion-function-id cannot be a template name because they cannot
10225 be part of a template-id. In fact, looking at this code:
10226
10227 a.operator K<int>()
10228
10229 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10230 It is impossible to call a templated conversion-function-id with an
10231 explicit argument list, since the only allowed template parameter is
10232 the type to which it is converting.
10233
10234 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10235 `template' keyword, in a construction like:
10236
10237 T::template f<3>()
10238
10239 In that case `f' is taken to be a template-name, even though there
10240 is no way of knowing for sure.
10241
10242 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10243 name refers to a set of overloaded functions, at least one of which
10244 is a template, or an IDENTIFIER_NODE with the name of the template,
10245 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10246 names are looked up inside uninstantiated templates. */
10247
10248 static tree
10249 cp_parser_template_name (cp_parser* parser,
10250 bool template_keyword_p,
10251 bool check_dependency_p,
10252 bool is_declaration,
10253 bool *is_identifier)
10254 {
10255 tree identifier;
10256 tree decl;
10257 tree fns;
10258 cp_token *token = cp_lexer_peek_token (parser->lexer);
10259
10260 /* If the next token is `operator', then we have either an
10261 operator-function-id or a conversion-function-id. */
10262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10263 {
10264 /* We don't know whether we're looking at an
10265 operator-function-id or a conversion-function-id. */
10266 cp_parser_parse_tentatively (parser);
10267 /* Try an operator-function-id. */
10268 identifier = cp_parser_operator_function_id (parser);
10269 /* If that didn't work, try a conversion-function-id. */
10270 if (!cp_parser_parse_definitely (parser))
10271 {
10272 cp_parser_error (parser, "expected template-name");
10273 return error_mark_node;
10274 }
10275 }
10276 /* Look for the identifier. */
10277 else
10278 identifier = cp_parser_identifier (parser);
10279
10280 /* If we didn't find an identifier, we don't have a template-id. */
10281 if (identifier == error_mark_node)
10282 return error_mark_node;
10283
10284 /* If the name immediately followed the `template' keyword, then it
10285 is a template-name. However, if the next token is not `<', then
10286 we do not treat it as a template-name, since it is not being used
10287 as part of a template-id. This enables us to handle constructs
10288 like:
10289
10290 template <typename T> struct S { S(); };
10291 template <typename T> S<T>::S();
10292
10293 correctly. We would treat `S' as a template -- if it were `S<T>'
10294 -- but we do not if there is no `<'. */
10295
10296 if (processing_template_decl
10297 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10298 {
10299 /* In a declaration, in a dependent context, we pretend that the
10300 "template" keyword was present in order to improve error
10301 recovery. For example, given:
10302
10303 template <typename T> void f(T::X<int>);
10304
10305 we want to treat "X<int>" as a template-id. */
10306 if (is_declaration
10307 && !template_keyword_p
10308 && parser->scope && TYPE_P (parser->scope)
10309 && check_dependency_p
10310 && dependent_type_p (parser->scope)
10311 /* Do not do this for dtors (or ctors), since they never
10312 need the template keyword before their name. */
10313 && !constructor_name_p (identifier, parser->scope))
10314 {
10315 cp_token_position start = 0;
10316
10317 /* Explain what went wrong. */
10318 error ("%Hnon-template %qD used as template",
10319 &token->location, identifier);
10320 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10321 parser->scope, identifier);
10322 /* If parsing tentatively, find the location of the "<" token. */
10323 if (cp_parser_simulate_error (parser))
10324 start = cp_lexer_token_position (parser->lexer, true);
10325 /* Parse the template arguments so that we can issue error
10326 messages about them. */
10327 cp_lexer_consume_token (parser->lexer);
10328 cp_parser_enclosed_template_argument_list (parser);
10329 /* Skip tokens until we find a good place from which to
10330 continue parsing. */
10331 cp_parser_skip_to_closing_parenthesis (parser,
10332 /*recovering=*/true,
10333 /*or_comma=*/true,
10334 /*consume_paren=*/false);
10335 /* If parsing tentatively, permanently remove the
10336 template argument list. That will prevent duplicate
10337 error messages from being issued about the missing
10338 "template" keyword. */
10339 if (start)
10340 cp_lexer_purge_tokens_after (parser->lexer, start);
10341 if (is_identifier)
10342 *is_identifier = true;
10343 return identifier;
10344 }
10345
10346 /* If the "template" keyword is present, then there is generally
10347 no point in doing name-lookup, so we just return IDENTIFIER.
10348 But, if the qualifying scope is non-dependent then we can
10349 (and must) do name-lookup normally. */
10350 if (template_keyword_p
10351 && (!parser->scope
10352 || (TYPE_P (parser->scope)
10353 && dependent_type_p (parser->scope))))
10354 return identifier;
10355 }
10356
10357 /* Look up the name. */
10358 decl = cp_parser_lookup_name (parser, identifier,
10359 none_type,
10360 /*is_template=*/false,
10361 /*is_namespace=*/false,
10362 check_dependency_p,
10363 /*ambiguous_decls=*/NULL,
10364 token->location);
10365 decl = maybe_get_template_decl_from_type_decl (decl);
10366
10367 /* If DECL is a template, then the name was a template-name. */
10368 if (TREE_CODE (decl) == TEMPLATE_DECL)
10369 ;
10370 else
10371 {
10372 tree fn = NULL_TREE;
10373
10374 /* The standard does not explicitly indicate whether a name that
10375 names a set of overloaded declarations, some of which are
10376 templates, is a template-name. However, such a name should
10377 be a template-name; otherwise, there is no way to form a
10378 template-id for the overloaded templates. */
10379 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10380 if (TREE_CODE (fns) == OVERLOAD)
10381 for (fn = fns; fn; fn = OVL_NEXT (fn))
10382 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10383 break;
10384
10385 if (!fn)
10386 {
10387 /* The name does not name a template. */
10388 cp_parser_error (parser, "expected template-name");
10389 return error_mark_node;
10390 }
10391 }
10392
10393 /* If DECL is dependent, and refers to a function, then just return
10394 its name; we will look it up again during template instantiation. */
10395 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10396 {
10397 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10398 if (TYPE_P (scope) && dependent_type_p (scope))
10399 return identifier;
10400 }
10401
10402 return decl;
10403 }
10404
10405 /* Parse a template-argument-list.
10406
10407 template-argument-list:
10408 template-argument ... [opt]
10409 template-argument-list , template-argument ... [opt]
10410
10411 Returns a TREE_VEC containing the arguments. */
10412
10413 static tree
10414 cp_parser_template_argument_list (cp_parser* parser)
10415 {
10416 tree fixed_args[10];
10417 unsigned n_args = 0;
10418 unsigned alloced = 10;
10419 tree *arg_ary = fixed_args;
10420 tree vec;
10421 bool saved_in_template_argument_list_p;
10422 bool saved_ice_p;
10423 bool saved_non_ice_p;
10424
10425 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10426 parser->in_template_argument_list_p = true;
10427 /* Even if the template-id appears in an integral
10428 constant-expression, the contents of the argument list do
10429 not. */
10430 saved_ice_p = parser->integral_constant_expression_p;
10431 parser->integral_constant_expression_p = false;
10432 saved_non_ice_p = parser->non_integral_constant_expression_p;
10433 parser->non_integral_constant_expression_p = false;
10434 /* Parse the arguments. */
10435 do
10436 {
10437 tree argument;
10438
10439 if (n_args)
10440 /* Consume the comma. */
10441 cp_lexer_consume_token (parser->lexer);
10442
10443 /* Parse the template-argument. */
10444 argument = cp_parser_template_argument (parser);
10445
10446 /* If the next token is an ellipsis, we're expanding a template
10447 argument pack. */
10448 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10449 {
10450 /* Consume the `...' token. */
10451 cp_lexer_consume_token (parser->lexer);
10452
10453 /* Make the argument into a TYPE_PACK_EXPANSION or
10454 EXPR_PACK_EXPANSION. */
10455 argument = make_pack_expansion (argument);
10456 }
10457
10458 if (n_args == alloced)
10459 {
10460 alloced *= 2;
10461
10462 if (arg_ary == fixed_args)
10463 {
10464 arg_ary = XNEWVEC (tree, alloced);
10465 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10466 }
10467 else
10468 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10469 }
10470 arg_ary[n_args++] = argument;
10471 }
10472 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10473
10474 vec = make_tree_vec (n_args);
10475
10476 while (n_args--)
10477 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10478
10479 if (arg_ary != fixed_args)
10480 free (arg_ary);
10481 parser->non_integral_constant_expression_p = saved_non_ice_p;
10482 parser->integral_constant_expression_p = saved_ice_p;
10483 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10484 return vec;
10485 }
10486
10487 /* Parse a template-argument.
10488
10489 template-argument:
10490 assignment-expression
10491 type-id
10492 id-expression
10493
10494 The representation is that of an assignment-expression, type-id, or
10495 id-expression -- except that the qualified id-expression is
10496 evaluated, so that the value returned is either a DECL or an
10497 OVERLOAD.
10498
10499 Although the standard says "assignment-expression", it forbids
10500 throw-expressions or assignments in the template argument.
10501 Therefore, we use "conditional-expression" instead. */
10502
10503 static tree
10504 cp_parser_template_argument (cp_parser* parser)
10505 {
10506 tree argument;
10507 bool template_p;
10508 bool address_p;
10509 bool maybe_type_id = false;
10510 cp_token *token = NULL, *argument_start_token = NULL;
10511 cp_id_kind idk;
10512
10513 /* There's really no way to know what we're looking at, so we just
10514 try each alternative in order.
10515
10516 [temp.arg]
10517
10518 In a template-argument, an ambiguity between a type-id and an
10519 expression is resolved to a type-id, regardless of the form of
10520 the corresponding template-parameter.
10521
10522 Therefore, we try a type-id first. */
10523 cp_parser_parse_tentatively (parser);
10524 argument = cp_parser_type_id (parser);
10525 /* If there was no error parsing the type-id but the next token is a
10526 '>>', our behavior depends on which dialect of C++ we're
10527 parsing. In C++98, we probably found a typo for '> >'. But there
10528 are type-id which are also valid expressions. For instance:
10529
10530 struct X { int operator >> (int); };
10531 template <int V> struct Foo {};
10532 Foo<X () >> 5> r;
10533
10534 Here 'X()' is a valid type-id of a function type, but the user just
10535 wanted to write the expression "X() >> 5". Thus, we remember that we
10536 found a valid type-id, but we still try to parse the argument as an
10537 expression to see what happens.
10538
10539 In C++0x, the '>>' will be considered two separate '>'
10540 tokens. */
10541 if (!cp_parser_error_occurred (parser)
10542 && cxx_dialect == cxx98
10543 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10544 {
10545 maybe_type_id = true;
10546 cp_parser_abort_tentative_parse (parser);
10547 }
10548 else
10549 {
10550 /* If the next token isn't a `,' or a `>', then this argument wasn't
10551 really finished. This means that the argument is not a valid
10552 type-id. */
10553 if (!cp_parser_next_token_ends_template_argument_p (parser))
10554 cp_parser_error (parser, "expected template-argument");
10555 /* If that worked, we're done. */
10556 if (cp_parser_parse_definitely (parser))
10557 return argument;
10558 }
10559 /* We're still not sure what the argument will be. */
10560 cp_parser_parse_tentatively (parser);
10561 /* Try a template. */
10562 argument_start_token = cp_lexer_peek_token (parser->lexer);
10563 argument = cp_parser_id_expression (parser,
10564 /*template_keyword_p=*/false,
10565 /*check_dependency_p=*/true,
10566 &template_p,
10567 /*declarator_p=*/false,
10568 /*optional_p=*/false);
10569 /* If the next token isn't a `,' or a `>', then this argument wasn't
10570 really finished. */
10571 if (!cp_parser_next_token_ends_template_argument_p (parser))
10572 cp_parser_error (parser, "expected template-argument");
10573 if (!cp_parser_error_occurred (parser))
10574 {
10575 /* Figure out what is being referred to. If the id-expression
10576 was for a class template specialization, then we will have a
10577 TYPE_DECL at this point. There is no need to do name lookup
10578 at this point in that case. */
10579 if (TREE_CODE (argument) != TYPE_DECL)
10580 argument = cp_parser_lookup_name (parser, argument,
10581 none_type,
10582 /*is_template=*/template_p,
10583 /*is_namespace=*/false,
10584 /*check_dependency=*/true,
10585 /*ambiguous_decls=*/NULL,
10586 argument_start_token->location);
10587 if (TREE_CODE (argument) != TEMPLATE_DECL
10588 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10589 cp_parser_error (parser, "expected template-name");
10590 }
10591 if (cp_parser_parse_definitely (parser))
10592 return argument;
10593 /* It must be a non-type argument. There permitted cases are given
10594 in [temp.arg.nontype]:
10595
10596 -- an integral constant-expression of integral or enumeration
10597 type; or
10598
10599 -- the name of a non-type template-parameter; or
10600
10601 -- the name of an object or function with external linkage...
10602
10603 -- the address of an object or function with external linkage...
10604
10605 -- a pointer to member... */
10606 /* Look for a non-type template parameter. */
10607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10608 {
10609 cp_parser_parse_tentatively (parser);
10610 argument = cp_parser_primary_expression (parser,
10611 /*address_p=*/false,
10612 /*cast_p=*/false,
10613 /*template_arg_p=*/true,
10614 &idk);
10615 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10616 || !cp_parser_next_token_ends_template_argument_p (parser))
10617 cp_parser_simulate_error (parser);
10618 if (cp_parser_parse_definitely (parser))
10619 return argument;
10620 }
10621
10622 /* If the next token is "&", the argument must be the address of an
10623 object or function with external linkage. */
10624 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10625 if (address_p)
10626 cp_lexer_consume_token (parser->lexer);
10627 /* See if we might have an id-expression. */
10628 token = cp_lexer_peek_token (parser->lexer);
10629 if (token->type == CPP_NAME
10630 || token->keyword == RID_OPERATOR
10631 || token->type == CPP_SCOPE
10632 || token->type == CPP_TEMPLATE_ID
10633 || token->type == CPP_NESTED_NAME_SPECIFIER)
10634 {
10635 cp_parser_parse_tentatively (parser);
10636 argument = cp_parser_primary_expression (parser,
10637 address_p,
10638 /*cast_p=*/false,
10639 /*template_arg_p=*/true,
10640 &idk);
10641 if (cp_parser_error_occurred (parser)
10642 || !cp_parser_next_token_ends_template_argument_p (parser))
10643 cp_parser_abort_tentative_parse (parser);
10644 else
10645 {
10646 if (TREE_CODE (argument) == INDIRECT_REF)
10647 {
10648 gcc_assert (REFERENCE_REF_P (argument));
10649 argument = TREE_OPERAND (argument, 0);
10650 }
10651
10652 if (TREE_CODE (argument) == VAR_DECL)
10653 {
10654 /* A variable without external linkage might still be a
10655 valid constant-expression, so no error is issued here
10656 if the external-linkage check fails. */
10657 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10658 cp_parser_simulate_error (parser);
10659 }
10660 else if (is_overloaded_fn (argument))
10661 /* All overloaded functions are allowed; if the external
10662 linkage test does not pass, an error will be issued
10663 later. */
10664 ;
10665 else if (address_p
10666 && (TREE_CODE (argument) == OFFSET_REF
10667 || TREE_CODE (argument) == SCOPE_REF))
10668 /* A pointer-to-member. */
10669 ;
10670 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10671 ;
10672 else
10673 cp_parser_simulate_error (parser);
10674
10675 if (cp_parser_parse_definitely (parser))
10676 {
10677 if (address_p)
10678 argument = build_x_unary_op (ADDR_EXPR, argument,
10679 tf_warning_or_error);
10680 return argument;
10681 }
10682 }
10683 }
10684 /* If the argument started with "&", there are no other valid
10685 alternatives at this point. */
10686 if (address_p)
10687 {
10688 cp_parser_error (parser, "invalid non-type template argument");
10689 return error_mark_node;
10690 }
10691
10692 /* If the argument wasn't successfully parsed as a type-id followed
10693 by '>>', the argument can only be a constant expression now.
10694 Otherwise, we try parsing the constant-expression tentatively,
10695 because the argument could really be a type-id. */
10696 if (maybe_type_id)
10697 cp_parser_parse_tentatively (parser);
10698 argument = cp_parser_constant_expression (parser,
10699 /*allow_non_constant_p=*/false,
10700 /*non_constant_p=*/NULL);
10701 argument = fold_non_dependent_expr (argument);
10702 if (!maybe_type_id)
10703 return argument;
10704 if (!cp_parser_next_token_ends_template_argument_p (parser))
10705 cp_parser_error (parser, "expected template-argument");
10706 if (cp_parser_parse_definitely (parser))
10707 return argument;
10708 /* We did our best to parse the argument as a non type-id, but that
10709 was the only alternative that matched (albeit with a '>' after
10710 it). We can assume it's just a typo from the user, and a
10711 diagnostic will then be issued. */
10712 return cp_parser_type_id (parser);
10713 }
10714
10715 /* Parse an explicit-instantiation.
10716
10717 explicit-instantiation:
10718 template declaration
10719
10720 Although the standard says `declaration', what it really means is:
10721
10722 explicit-instantiation:
10723 template decl-specifier-seq [opt] declarator [opt] ;
10724
10725 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10726 supposed to be allowed. A defect report has been filed about this
10727 issue.
10728
10729 GNU Extension:
10730
10731 explicit-instantiation:
10732 storage-class-specifier template
10733 decl-specifier-seq [opt] declarator [opt] ;
10734 function-specifier template
10735 decl-specifier-seq [opt] declarator [opt] ; */
10736
10737 static void
10738 cp_parser_explicit_instantiation (cp_parser* parser)
10739 {
10740 int declares_class_or_enum;
10741 cp_decl_specifier_seq decl_specifiers;
10742 tree extension_specifier = NULL_TREE;
10743 cp_token *token;
10744
10745 /* Look for an (optional) storage-class-specifier or
10746 function-specifier. */
10747 if (cp_parser_allow_gnu_extensions_p (parser))
10748 {
10749 extension_specifier
10750 = cp_parser_storage_class_specifier_opt (parser);
10751 if (!extension_specifier)
10752 extension_specifier
10753 = cp_parser_function_specifier_opt (parser,
10754 /*decl_specs=*/NULL);
10755 }
10756
10757 /* Look for the `template' keyword. */
10758 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10759 /* Let the front end know that we are processing an explicit
10760 instantiation. */
10761 begin_explicit_instantiation ();
10762 /* [temp.explicit] says that we are supposed to ignore access
10763 control while processing explicit instantiation directives. */
10764 push_deferring_access_checks (dk_no_check);
10765 /* Parse a decl-specifier-seq. */
10766 token = cp_lexer_peek_token (parser->lexer);
10767 cp_parser_decl_specifier_seq (parser,
10768 CP_PARSER_FLAGS_OPTIONAL,
10769 &decl_specifiers,
10770 &declares_class_or_enum);
10771 /* If there was exactly one decl-specifier, and it declared a class,
10772 and there's no declarator, then we have an explicit type
10773 instantiation. */
10774 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10775 {
10776 tree type;
10777
10778 type = check_tag_decl (&decl_specifiers);
10779 /* Turn access control back on for names used during
10780 template instantiation. */
10781 pop_deferring_access_checks ();
10782 if (type)
10783 do_type_instantiation (type, extension_specifier,
10784 /*complain=*/tf_error);
10785 }
10786 else
10787 {
10788 cp_declarator *declarator;
10789 tree decl;
10790
10791 /* Parse the declarator. */
10792 declarator
10793 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10794 /*ctor_dtor_or_conv_p=*/NULL,
10795 /*parenthesized_p=*/NULL,
10796 /*member_p=*/false);
10797 if (declares_class_or_enum & 2)
10798 cp_parser_check_for_definition_in_return_type (declarator,
10799 decl_specifiers.type,
10800 decl_specifiers.type_location);
10801 if (declarator != cp_error_declarator)
10802 {
10803 decl = grokdeclarator (declarator, &decl_specifiers,
10804 NORMAL, 0, &decl_specifiers.attributes);
10805 /* Turn access control back on for names used during
10806 template instantiation. */
10807 pop_deferring_access_checks ();
10808 /* Do the explicit instantiation. */
10809 do_decl_instantiation (decl, extension_specifier);
10810 }
10811 else
10812 {
10813 pop_deferring_access_checks ();
10814 /* Skip the body of the explicit instantiation. */
10815 cp_parser_skip_to_end_of_statement (parser);
10816 }
10817 }
10818 /* We're done with the instantiation. */
10819 end_explicit_instantiation ();
10820
10821 cp_parser_consume_semicolon_at_end_of_statement (parser);
10822 }
10823
10824 /* Parse an explicit-specialization.
10825
10826 explicit-specialization:
10827 template < > declaration
10828
10829 Although the standard says `declaration', what it really means is:
10830
10831 explicit-specialization:
10832 template <> decl-specifier [opt] init-declarator [opt] ;
10833 template <> function-definition
10834 template <> explicit-specialization
10835 template <> template-declaration */
10836
10837 static void
10838 cp_parser_explicit_specialization (cp_parser* parser)
10839 {
10840 bool need_lang_pop;
10841 cp_token *token = cp_lexer_peek_token (parser->lexer);
10842
10843 /* Look for the `template' keyword. */
10844 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10845 /* Look for the `<'. */
10846 cp_parser_require (parser, CPP_LESS, "%<<%>");
10847 /* Look for the `>'. */
10848 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10849 /* We have processed another parameter list. */
10850 ++parser->num_template_parameter_lists;
10851 /* [temp]
10852
10853 A template ... explicit specialization ... shall not have C
10854 linkage. */
10855 if (current_lang_name == lang_name_c)
10856 {
10857 error ("%Htemplate specialization with C linkage", &token->location);
10858 /* Give it C++ linkage to avoid confusing other parts of the
10859 front end. */
10860 push_lang_context (lang_name_cplusplus);
10861 need_lang_pop = true;
10862 }
10863 else
10864 need_lang_pop = false;
10865 /* Let the front end know that we are beginning a specialization. */
10866 if (!begin_specialization ())
10867 {
10868 end_specialization ();
10869 cp_parser_skip_to_end_of_block_or_statement (parser);
10870 return;
10871 }
10872
10873 /* If the next keyword is `template', we need to figure out whether
10874 or not we're looking a template-declaration. */
10875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10876 {
10877 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10878 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10879 cp_parser_template_declaration_after_export (parser,
10880 /*member_p=*/false);
10881 else
10882 cp_parser_explicit_specialization (parser);
10883 }
10884 else
10885 /* Parse the dependent declaration. */
10886 cp_parser_single_declaration (parser,
10887 /*checks=*/NULL,
10888 /*member_p=*/false,
10889 /*explicit_specialization_p=*/true,
10890 /*friend_p=*/NULL);
10891 /* We're done with the specialization. */
10892 end_specialization ();
10893 /* For the erroneous case of a template with C linkage, we pushed an
10894 implicit C++ linkage scope; exit that scope now. */
10895 if (need_lang_pop)
10896 pop_lang_context ();
10897 /* We're done with this parameter list. */
10898 --parser->num_template_parameter_lists;
10899 }
10900
10901 /* Parse a type-specifier.
10902
10903 type-specifier:
10904 simple-type-specifier
10905 class-specifier
10906 enum-specifier
10907 elaborated-type-specifier
10908 cv-qualifier
10909
10910 GNU Extension:
10911
10912 type-specifier:
10913 __complex__
10914
10915 Returns a representation of the type-specifier. For a
10916 class-specifier, enum-specifier, or elaborated-type-specifier, a
10917 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10918
10919 The parser flags FLAGS is used to control type-specifier parsing.
10920
10921 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10922 in a decl-specifier-seq.
10923
10924 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10925 class-specifier, enum-specifier, or elaborated-type-specifier, then
10926 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10927 if a type is declared; 2 if it is defined. Otherwise, it is set to
10928 zero.
10929
10930 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10931 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10932 is set to FALSE. */
10933
10934 static tree
10935 cp_parser_type_specifier (cp_parser* parser,
10936 cp_parser_flags flags,
10937 cp_decl_specifier_seq *decl_specs,
10938 bool is_declaration,
10939 int* declares_class_or_enum,
10940 bool* is_cv_qualifier)
10941 {
10942 tree type_spec = NULL_TREE;
10943 cp_token *token;
10944 enum rid keyword;
10945 cp_decl_spec ds = ds_last;
10946
10947 /* Assume this type-specifier does not declare a new type. */
10948 if (declares_class_or_enum)
10949 *declares_class_or_enum = 0;
10950 /* And that it does not specify a cv-qualifier. */
10951 if (is_cv_qualifier)
10952 *is_cv_qualifier = false;
10953 /* Peek at the next token. */
10954 token = cp_lexer_peek_token (parser->lexer);
10955
10956 /* If we're looking at a keyword, we can use that to guide the
10957 production we choose. */
10958 keyword = token->keyword;
10959 switch (keyword)
10960 {
10961 case RID_ENUM:
10962 /* Look for the enum-specifier. */
10963 type_spec = cp_parser_enum_specifier (parser);
10964 /* If that worked, we're done. */
10965 if (type_spec)
10966 {
10967 if (declares_class_or_enum)
10968 *declares_class_or_enum = 2;
10969 if (decl_specs)
10970 cp_parser_set_decl_spec_type (decl_specs,
10971 type_spec,
10972 token->location,
10973 /*user_defined_p=*/true);
10974 return type_spec;
10975 }
10976 else
10977 goto elaborated_type_specifier;
10978
10979 /* Any of these indicate either a class-specifier, or an
10980 elaborated-type-specifier. */
10981 case RID_CLASS:
10982 case RID_STRUCT:
10983 case RID_UNION:
10984 /* Parse tentatively so that we can back up if we don't find a
10985 class-specifier. */
10986 cp_parser_parse_tentatively (parser);
10987 /* Look for the class-specifier. */
10988 type_spec = cp_parser_class_specifier (parser);
10989 /* If that worked, we're done. */
10990 if (cp_parser_parse_definitely (parser))
10991 {
10992 if (declares_class_or_enum)
10993 *declares_class_or_enum = 2;
10994 if (decl_specs)
10995 cp_parser_set_decl_spec_type (decl_specs,
10996 type_spec,
10997 token->location,
10998 /*user_defined_p=*/true);
10999 return type_spec;
11000 }
11001
11002 /* Fall through. */
11003 elaborated_type_specifier:
11004 /* We're declaring (not defining) a class or enum. */
11005 if (declares_class_or_enum)
11006 *declares_class_or_enum = 1;
11007
11008 /* Fall through. */
11009 case RID_TYPENAME:
11010 /* Look for an elaborated-type-specifier. */
11011 type_spec
11012 = (cp_parser_elaborated_type_specifier
11013 (parser,
11014 decl_specs && decl_specs->specs[(int) ds_friend],
11015 is_declaration));
11016 if (decl_specs)
11017 cp_parser_set_decl_spec_type (decl_specs,
11018 type_spec,
11019 token->location,
11020 /*user_defined_p=*/true);
11021 return type_spec;
11022
11023 case RID_CONST:
11024 ds = ds_const;
11025 if (is_cv_qualifier)
11026 *is_cv_qualifier = true;
11027 break;
11028
11029 case RID_VOLATILE:
11030 ds = ds_volatile;
11031 if (is_cv_qualifier)
11032 *is_cv_qualifier = true;
11033 break;
11034
11035 case RID_RESTRICT:
11036 ds = ds_restrict;
11037 if (is_cv_qualifier)
11038 *is_cv_qualifier = true;
11039 break;
11040
11041 case RID_COMPLEX:
11042 /* The `__complex__' keyword is a GNU extension. */
11043 ds = ds_complex;
11044 break;
11045
11046 default:
11047 break;
11048 }
11049
11050 /* Handle simple keywords. */
11051 if (ds != ds_last)
11052 {
11053 if (decl_specs)
11054 {
11055 ++decl_specs->specs[(int)ds];
11056 decl_specs->any_specifiers_p = true;
11057 }
11058 return cp_lexer_consume_token (parser->lexer)->u.value;
11059 }
11060
11061 /* If we do not already have a type-specifier, assume we are looking
11062 at a simple-type-specifier. */
11063 type_spec = cp_parser_simple_type_specifier (parser,
11064 decl_specs,
11065 flags);
11066
11067 /* If we didn't find a type-specifier, and a type-specifier was not
11068 optional in this context, issue an error message. */
11069 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11070 {
11071 cp_parser_error (parser, "expected type specifier");
11072 return error_mark_node;
11073 }
11074
11075 return type_spec;
11076 }
11077
11078 /* Parse a simple-type-specifier.
11079
11080 simple-type-specifier:
11081 :: [opt] nested-name-specifier [opt] type-name
11082 :: [opt] nested-name-specifier template template-id
11083 char
11084 wchar_t
11085 bool
11086 short
11087 int
11088 long
11089 signed
11090 unsigned
11091 float
11092 double
11093 void
11094
11095 C++0x Extension:
11096
11097 simple-type-specifier:
11098 auto
11099 decltype ( expression )
11100 char16_t
11101 char32_t
11102
11103 GNU Extension:
11104
11105 simple-type-specifier:
11106 __typeof__ unary-expression
11107 __typeof__ ( type-id )
11108
11109 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11110 appropriately updated. */
11111
11112 static tree
11113 cp_parser_simple_type_specifier (cp_parser* parser,
11114 cp_decl_specifier_seq *decl_specs,
11115 cp_parser_flags flags)
11116 {
11117 tree type = NULL_TREE;
11118 cp_token *token;
11119
11120 /* Peek at the next token. */
11121 token = cp_lexer_peek_token (parser->lexer);
11122
11123 /* If we're looking at a keyword, things are easy. */
11124 switch (token->keyword)
11125 {
11126 case RID_CHAR:
11127 if (decl_specs)
11128 decl_specs->explicit_char_p = true;
11129 type = char_type_node;
11130 break;
11131 case RID_CHAR16:
11132 type = char16_type_node;
11133 break;
11134 case RID_CHAR32:
11135 type = char32_type_node;
11136 break;
11137 case RID_WCHAR:
11138 type = wchar_type_node;
11139 break;
11140 case RID_BOOL:
11141 type = boolean_type_node;
11142 break;
11143 case RID_SHORT:
11144 if (decl_specs)
11145 ++decl_specs->specs[(int) ds_short];
11146 type = short_integer_type_node;
11147 break;
11148 case RID_INT:
11149 if (decl_specs)
11150 decl_specs->explicit_int_p = true;
11151 type = integer_type_node;
11152 break;
11153 case RID_LONG:
11154 if (decl_specs)
11155 ++decl_specs->specs[(int) ds_long];
11156 type = long_integer_type_node;
11157 break;
11158 case RID_SIGNED:
11159 if (decl_specs)
11160 ++decl_specs->specs[(int) ds_signed];
11161 type = integer_type_node;
11162 break;
11163 case RID_UNSIGNED:
11164 if (decl_specs)
11165 ++decl_specs->specs[(int) ds_unsigned];
11166 type = unsigned_type_node;
11167 break;
11168 case RID_FLOAT:
11169 type = float_type_node;
11170 break;
11171 case RID_DOUBLE:
11172 type = double_type_node;
11173 break;
11174 case RID_VOID:
11175 type = void_type_node;
11176 break;
11177
11178 case RID_AUTO:
11179 maybe_warn_cpp0x ("C++0x auto");
11180 type = make_auto ();
11181 break;
11182
11183 case RID_DECLTYPE:
11184 /* Parse the `decltype' type. */
11185 type = cp_parser_decltype (parser);
11186
11187 if (decl_specs)
11188 cp_parser_set_decl_spec_type (decl_specs, type,
11189 token->location,
11190 /*user_defined_p=*/true);
11191
11192 return type;
11193
11194 case RID_TYPEOF:
11195 /* Consume the `typeof' token. */
11196 cp_lexer_consume_token (parser->lexer);
11197 /* Parse the operand to `typeof'. */
11198 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11199 /* If it is not already a TYPE, take its type. */
11200 if (!TYPE_P (type))
11201 type = finish_typeof (type);
11202
11203 if (decl_specs)
11204 cp_parser_set_decl_spec_type (decl_specs, type,
11205 token->location,
11206 /*user_defined_p=*/true);
11207
11208 return type;
11209
11210 default:
11211 break;
11212 }
11213
11214 /* If the type-specifier was for a built-in type, we're done. */
11215 if (type)
11216 {
11217 tree id;
11218
11219 /* Record the type. */
11220 if (decl_specs
11221 && (token->keyword != RID_SIGNED
11222 && token->keyword != RID_UNSIGNED
11223 && token->keyword != RID_SHORT
11224 && token->keyword != RID_LONG))
11225 cp_parser_set_decl_spec_type (decl_specs,
11226 type,
11227 token->location,
11228 /*user_defined=*/false);
11229 if (decl_specs)
11230 decl_specs->any_specifiers_p = true;
11231
11232 /* Consume the token. */
11233 id = cp_lexer_consume_token (parser->lexer)->u.value;
11234
11235 /* There is no valid C++ program where a non-template type is
11236 followed by a "<". That usually indicates that the user thought
11237 that the type was a template. */
11238 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11239
11240 return TYPE_NAME (type);
11241 }
11242
11243 /* The type-specifier must be a user-defined type. */
11244 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11245 {
11246 bool qualified_p;
11247 bool global_p;
11248
11249 /* Don't gobble tokens or issue error messages if this is an
11250 optional type-specifier. */
11251 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11252 cp_parser_parse_tentatively (parser);
11253
11254 /* Look for the optional `::' operator. */
11255 global_p
11256 = (cp_parser_global_scope_opt (parser,
11257 /*current_scope_valid_p=*/false)
11258 != NULL_TREE);
11259 /* Look for the nested-name specifier. */
11260 qualified_p
11261 = (cp_parser_nested_name_specifier_opt (parser,
11262 /*typename_keyword_p=*/false,
11263 /*check_dependency_p=*/true,
11264 /*type_p=*/false,
11265 /*is_declaration=*/false)
11266 != NULL_TREE);
11267 token = cp_lexer_peek_token (parser->lexer);
11268 /* If we have seen a nested-name-specifier, and the next token
11269 is `template', then we are using the template-id production. */
11270 if (parser->scope
11271 && cp_parser_optional_template_keyword (parser))
11272 {
11273 /* Look for the template-id. */
11274 type = cp_parser_template_id (parser,
11275 /*template_keyword_p=*/true,
11276 /*check_dependency_p=*/true,
11277 /*is_declaration=*/false);
11278 /* If the template-id did not name a type, we are out of
11279 luck. */
11280 if (TREE_CODE (type) != TYPE_DECL)
11281 {
11282 cp_parser_error (parser, "expected template-id for type");
11283 type = NULL_TREE;
11284 }
11285 }
11286 /* Otherwise, look for a type-name. */
11287 else
11288 type = cp_parser_type_name (parser);
11289 /* Keep track of all name-lookups performed in class scopes. */
11290 if (type
11291 && !global_p
11292 && !qualified_p
11293 && TREE_CODE (type) == TYPE_DECL
11294 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11295 maybe_note_name_used_in_class (DECL_NAME (type), type);
11296 /* If it didn't work out, we don't have a TYPE. */
11297 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11298 && !cp_parser_parse_definitely (parser))
11299 type = NULL_TREE;
11300 if (type && decl_specs)
11301 cp_parser_set_decl_spec_type (decl_specs, type,
11302 token->location,
11303 /*user_defined=*/true);
11304 }
11305
11306 /* If we didn't get a type-name, issue an error message. */
11307 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11308 {
11309 cp_parser_error (parser, "expected type-name");
11310 return error_mark_node;
11311 }
11312
11313 /* There is no valid C++ program where a non-template type is
11314 followed by a "<". That usually indicates that the user thought
11315 that the type was a template. */
11316 if (type && type != error_mark_node)
11317 {
11318 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11319 If it is, then the '<'...'>' enclose protocol names rather than
11320 template arguments, and so everything is fine. */
11321 if (c_dialect_objc ()
11322 && (objc_is_id (type) || objc_is_class_name (type)))
11323 {
11324 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11325 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11326
11327 /* Clobber the "unqualified" type previously entered into
11328 DECL_SPECS with the new, improved protocol-qualified version. */
11329 if (decl_specs)
11330 decl_specs->type = qual_type;
11331
11332 return qual_type;
11333 }
11334
11335 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11336 token->location);
11337 }
11338
11339 return type;
11340 }
11341
11342 /* Parse a type-name.
11343
11344 type-name:
11345 class-name
11346 enum-name
11347 typedef-name
11348
11349 enum-name:
11350 identifier
11351
11352 typedef-name:
11353 identifier
11354
11355 Returns a TYPE_DECL for the type. */
11356
11357 static tree
11358 cp_parser_type_name (cp_parser* parser)
11359 {
11360 tree type_decl;
11361
11362 /* We can't know yet whether it is a class-name or not. */
11363 cp_parser_parse_tentatively (parser);
11364 /* Try a class-name. */
11365 type_decl = cp_parser_class_name (parser,
11366 /*typename_keyword_p=*/false,
11367 /*template_keyword_p=*/false,
11368 none_type,
11369 /*check_dependency_p=*/true,
11370 /*class_head_p=*/false,
11371 /*is_declaration=*/false);
11372 /* If it's not a class-name, keep looking. */
11373 if (!cp_parser_parse_definitely (parser))
11374 {
11375 /* It must be a typedef-name or an enum-name. */
11376 return cp_parser_nonclass_name (parser);
11377 }
11378
11379 return type_decl;
11380 }
11381
11382 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11383
11384 enum-name:
11385 identifier
11386
11387 typedef-name:
11388 identifier
11389
11390 Returns a TYPE_DECL for the type. */
11391
11392 static tree
11393 cp_parser_nonclass_name (cp_parser* parser)
11394 {
11395 tree type_decl;
11396 tree identifier;
11397
11398 cp_token *token = cp_lexer_peek_token (parser->lexer);
11399 identifier = cp_parser_identifier (parser);
11400 if (identifier == error_mark_node)
11401 return error_mark_node;
11402
11403 /* Look up the type-name. */
11404 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11405
11406 if (TREE_CODE (type_decl) != TYPE_DECL
11407 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11408 {
11409 /* See if this is an Objective-C type. */
11410 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11411 tree type = objc_get_protocol_qualified_type (identifier, protos);
11412 if (type)
11413 type_decl = TYPE_NAME (type);
11414 }
11415
11416 /* Issue an error if we did not find a type-name. */
11417 if (TREE_CODE (type_decl) != TYPE_DECL)
11418 {
11419 if (!cp_parser_simulate_error (parser))
11420 cp_parser_name_lookup_error (parser, identifier, type_decl,
11421 "is not a type", token->location);
11422 return error_mark_node;
11423 }
11424 /* Remember that the name was used in the definition of the
11425 current class so that we can check later to see if the
11426 meaning would have been different after the class was
11427 entirely defined. */
11428 else if (type_decl != error_mark_node
11429 && !parser->scope)
11430 maybe_note_name_used_in_class (identifier, type_decl);
11431
11432 return type_decl;
11433 }
11434
11435 /* Parse an elaborated-type-specifier. Note that the grammar given
11436 here incorporates the resolution to DR68.
11437
11438 elaborated-type-specifier:
11439 class-key :: [opt] nested-name-specifier [opt] identifier
11440 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11441 enum-key :: [opt] nested-name-specifier [opt] identifier
11442 typename :: [opt] nested-name-specifier identifier
11443 typename :: [opt] nested-name-specifier template [opt]
11444 template-id
11445
11446 GNU extension:
11447
11448 elaborated-type-specifier:
11449 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11450 class-key attributes :: [opt] nested-name-specifier [opt]
11451 template [opt] template-id
11452 enum attributes :: [opt] nested-name-specifier [opt] identifier
11453
11454 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11455 declared `friend'. If IS_DECLARATION is TRUE, then this
11456 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11457 something is being declared.
11458
11459 Returns the TYPE specified. */
11460
11461 static tree
11462 cp_parser_elaborated_type_specifier (cp_parser* parser,
11463 bool is_friend,
11464 bool is_declaration)
11465 {
11466 enum tag_types tag_type;
11467 tree identifier;
11468 tree type = NULL_TREE;
11469 tree attributes = NULL_TREE;
11470 cp_token *token = NULL;
11471
11472 /* See if we're looking at the `enum' keyword. */
11473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11474 {
11475 /* Consume the `enum' token. */
11476 cp_lexer_consume_token (parser->lexer);
11477 /* Remember that it's an enumeration type. */
11478 tag_type = enum_type;
11479 /* Parse the optional `struct' or `class' key (for C++0x scoped
11480 enums). */
11481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11482 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11483 {
11484 if (cxx_dialect == cxx98)
11485 maybe_warn_cpp0x ("scoped enums");
11486
11487 /* Consume the `struct' or `class'. */
11488 cp_lexer_consume_token (parser->lexer);
11489 }
11490 /* Parse the attributes. */
11491 attributes = cp_parser_attributes_opt (parser);
11492 }
11493 /* Or, it might be `typename'. */
11494 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11495 RID_TYPENAME))
11496 {
11497 /* Consume the `typename' token. */
11498 cp_lexer_consume_token (parser->lexer);
11499 /* Remember that it's a `typename' type. */
11500 tag_type = typename_type;
11501 /* The `typename' keyword is only allowed in templates. */
11502 if (!processing_template_decl)
11503 permerror (input_location, "using %<typename%> outside of template");
11504 }
11505 /* Otherwise it must be a class-key. */
11506 else
11507 {
11508 tag_type = cp_parser_class_key (parser);
11509 if (tag_type == none_type)
11510 return error_mark_node;
11511 /* Parse the attributes. */
11512 attributes = cp_parser_attributes_opt (parser);
11513 }
11514
11515 /* Look for the `::' operator. */
11516 cp_parser_global_scope_opt (parser,
11517 /*current_scope_valid_p=*/false);
11518 /* Look for the nested-name-specifier. */
11519 if (tag_type == typename_type)
11520 {
11521 if (!cp_parser_nested_name_specifier (parser,
11522 /*typename_keyword_p=*/true,
11523 /*check_dependency_p=*/true,
11524 /*type_p=*/true,
11525 is_declaration))
11526 return error_mark_node;
11527 }
11528 else
11529 /* Even though `typename' is not present, the proposed resolution
11530 to Core Issue 180 says that in `class A<T>::B', `B' should be
11531 considered a type-name, even if `A<T>' is dependent. */
11532 cp_parser_nested_name_specifier_opt (parser,
11533 /*typename_keyword_p=*/true,
11534 /*check_dependency_p=*/true,
11535 /*type_p=*/true,
11536 is_declaration);
11537 /* For everything but enumeration types, consider a template-id.
11538 For an enumeration type, consider only a plain identifier. */
11539 if (tag_type != enum_type)
11540 {
11541 bool template_p = false;
11542 tree decl;
11543
11544 /* Allow the `template' keyword. */
11545 template_p = cp_parser_optional_template_keyword (parser);
11546 /* If we didn't see `template', we don't know if there's a
11547 template-id or not. */
11548 if (!template_p)
11549 cp_parser_parse_tentatively (parser);
11550 /* Parse the template-id. */
11551 token = cp_lexer_peek_token (parser->lexer);
11552 decl = cp_parser_template_id (parser, template_p,
11553 /*check_dependency_p=*/true,
11554 is_declaration);
11555 /* If we didn't find a template-id, look for an ordinary
11556 identifier. */
11557 if (!template_p && !cp_parser_parse_definitely (parser))
11558 ;
11559 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11560 in effect, then we must assume that, upon instantiation, the
11561 template will correspond to a class. */
11562 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11563 && tag_type == typename_type)
11564 type = make_typename_type (parser->scope, decl,
11565 typename_type,
11566 /*complain=*/tf_error);
11567 else
11568 type = TREE_TYPE (decl);
11569 }
11570
11571 if (!type)
11572 {
11573 token = cp_lexer_peek_token (parser->lexer);
11574 identifier = cp_parser_identifier (parser);
11575
11576 if (identifier == error_mark_node)
11577 {
11578 parser->scope = NULL_TREE;
11579 return error_mark_node;
11580 }
11581
11582 /* For a `typename', we needn't call xref_tag. */
11583 if (tag_type == typename_type
11584 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11585 return cp_parser_make_typename_type (parser, parser->scope,
11586 identifier,
11587 token->location);
11588 /* Look up a qualified name in the usual way. */
11589 if (parser->scope)
11590 {
11591 tree decl;
11592 tree ambiguous_decls;
11593
11594 decl = cp_parser_lookup_name (parser, identifier,
11595 tag_type,
11596 /*is_template=*/false,
11597 /*is_namespace=*/false,
11598 /*check_dependency=*/true,
11599 &ambiguous_decls,
11600 token->location);
11601
11602 /* If the lookup was ambiguous, an error will already have been
11603 issued. */
11604 if (ambiguous_decls)
11605 return error_mark_node;
11606
11607 /* If we are parsing friend declaration, DECL may be a
11608 TEMPLATE_DECL tree node here. However, we need to check
11609 whether this TEMPLATE_DECL results in valid code. Consider
11610 the following example:
11611
11612 namespace N {
11613 template <class T> class C {};
11614 }
11615 class X {
11616 template <class T> friend class N::C; // #1, valid code
11617 };
11618 template <class T> class Y {
11619 friend class N::C; // #2, invalid code
11620 };
11621
11622 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11623 name lookup of `N::C'. We see that friend declaration must
11624 be template for the code to be valid. Note that
11625 processing_template_decl does not work here since it is
11626 always 1 for the above two cases. */
11627
11628 decl = (cp_parser_maybe_treat_template_as_class
11629 (decl, /*tag_name_p=*/is_friend
11630 && parser->num_template_parameter_lists));
11631
11632 if (TREE_CODE (decl) != TYPE_DECL)
11633 {
11634 cp_parser_diagnose_invalid_type_name (parser,
11635 parser->scope,
11636 identifier,
11637 token->location);
11638 return error_mark_node;
11639 }
11640
11641 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11642 {
11643 bool allow_template = (parser->num_template_parameter_lists
11644 || DECL_SELF_REFERENCE_P (decl));
11645 type = check_elaborated_type_specifier (tag_type, decl,
11646 allow_template);
11647
11648 if (type == error_mark_node)
11649 return error_mark_node;
11650 }
11651
11652 /* Forward declarations of nested types, such as
11653
11654 class C1::C2;
11655 class C1::C2::C3;
11656
11657 are invalid unless all components preceding the final '::'
11658 are complete. If all enclosing types are complete, these
11659 declarations become merely pointless.
11660
11661 Invalid forward declarations of nested types are errors
11662 caught elsewhere in parsing. Those that are pointless arrive
11663 here. */
11664
11665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11666 && !is_friend && !processing_explicit_instantiation)
11667 warning (0, "declaration %qD does not declare anything", decl);
11668
11669 type = TREE_TYPE (decl);
11670 }
11671 else
11672 {
11673 /* An elaborated-type-specifier sometimes introduces a new type and
11674 sometimes names an existing type. Normally, the rule is that it
11675 introduces a new type only if there is not an existing type of
11676 the same name already in scope. For example, given:
11677
11678 struct S {};
11679 void f() { struct S s; }
11680
11681 the `struct S' in the body of `f' is the same `struct S' as in
11682 the global scope; the existing definition is used. However, if
11683 there were no global declaration, this would introduce a new
11684 local class named `S'.
11685
11686 An exception to this rule applies to the following code:
11687
11688 namespace N { struct S; }
11689
11690 Here, the elaborated-type-specifier names a new type
11691 unconditionally; even if there is already an `S' in the
11692 containing scope this declaration names a new type.
11693 This exception only applies if the elaborated-type-specifier
11694 forms the complete declaration:
11695
11696 [class.name]
11697
11698 A declaration consisting solely of `class-key identifier ;' is
11699 either a redeclaration of the name in the current scope or a
11700 forward declaration of the identifier as a class name. It
11701 introduces the name into the current scope.
11702
11703 We are in this situation precisely when the next token is a `;'.
11704
11705 An exception to the exception is that a `friend' declaration does
11706 *not* name a new type; i.e., given:
11707
11708 struct S { friend struct T; };
11709
11710 `T' is not a new type in the scope of `S'.
11711
11712 Also, `new struct S' or `sizeof (struct S)' never results in the
11713 definition of a new type; a new type can only be declared in a
11714 declaration context. */
11715
11716 tag_scope ts;
11717 bool template_p;
11718
11719 if (is_friend)
11720 /* Friends have special name lookup rules. */
11721 ts = ts_within_enclosing_non_class;
11722 else if (is_declaration
11723 && cp_lexer_next_token_is (parser->lexer,
11724 CPP_SEMICOLON))
11725 /* This is a `class-key identifier ;' */
11726 ts = ts_current;
11727 else
11728 ts = ts_global;
11729
11730 template_p =
11731 (parser->num_template_parameter_lists
11732 && (cp_parser_next_token_starts_class_definition_p (parser)
11733 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11734 /* An unqualified name was used to reference this type, so
11735 there were no qualifying templates. */
11736 if (!cp_parser_check_template_parameters (parser,
11737 /*num_templates=*/0,
11738 token->location))
11739 return error_mark_node;
11740 type = xref_tag (tag_type, identifier, ts, template_p);
11741 }
11742 }
11743
11744 if (type == error_mark_node)
11745 return error_mark_node;
11746
11747 /* Allow attributes on forward declarations of classes. */
11748 if (attributes)
11749 {
11750 if (TREE_CODE (type) == TYPENAME_TYPE)
11751 warning (OPT_Wattributes,
11752 "attributes ignored on uninstantiated type");
11753 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11754 && ! processing_explicit_instantiation)
11755 warning (OPT_Wattributes,
11756 "attributes ignored on template instantiation");
11757 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11758 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11759 else
11760 warning (OPT_Wattributes,
11761 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11762 }
11763
11764 if (tag_type != enum_type)
11765 cp_parser_check_class_key (tag_type, type);
11766
11767 /* A "<" cannot follow an elaborated type specifier. If that
11768 happens, the user was probably trying to form a template-id. */
11769 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11770
11771 return type;
11772 }
11773
11774 /* Parse an enum-specifier.
11775
11776 enum-specifier:
11777 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11778
11779 enum-key:
11780 enum
11781 enum class [C++0x]
11782 enum struct [C++0x]
11783
11784 enum-base: [C++0x]
11785 : type-specifier-seq
11786
11787 GNU Extensions:
11788 enum-key attributes[opt] identifier [opt] enum-base [opt]
11789 { enumerator-list [opt] }attributes[opt]
11790
11791 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11792 if the token stream isn't an enum-specifier after all. */
11793
11794 static tree
11795 cp_parser_enum_specifier (cp_parser* parser)
11796 {
11797 tree identifier;
11798 tree type;
11799 tree attributes;
11800 bool scoped_enum_p = false;
11801 bool has_underlying_type = false;
11802 tree underlying_type = NULL_TREE;
11803
11804 /* Parse tentatively so that we can back up if we don't find a
11805 enum-specifier. */
11806 cp_parser_parse_tentatively (parser);
11807
11808 /* Caller guarantees that the current token is 'enum', an identifier
11809 possibly follows, and the token after that is an opening brace.
11810 If we don't have an identifier, fabricate an anonymous name for
11811 the enumeration being defined. */
11812 cp_lexer_consume_token (parser->lexer);
11813
11814 /* Parse the "class" or "struct", which indicates a scoped
11815 enumeration type in C++0x. */
11816 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11817 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11818 {
11819 if (cxx_dialect == cxx98)
11820 maybe_warn_cpp0x ("scoped enums");
11821
11822 /* Consume the `struct' or `class' token. */
11823 cp_lexer_consume_token (parser->lexer);
11824
11825 scoped_enum_p = true;
11826 }
11827
11828 attributes = cp_parser_attributes_opt (parser);
11829
11830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11831 identifier = cp_parser_identifier (parser);
11832 else
11833 identifier = make_anon_name ();
11834
11835 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11836 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11837 {
11838 cp_decl_specifier_seq type_specifiers;
11839
11840 /* At this point this is surely not elaborated type specifier. */
11841 if (!cp_parser_parse_definitely (parser))
11842 return NULL_TREE;
11843
11844 if (cxx_dialect == cxx98)
11845 maybe_warn_cpp0x ("scoped enums");
11846
11847 /* Consume the `:'. */
11848 cp_lexer_consume_token (parser->lexer);
11849
11850 has_underlying_type = true;
11851
11852 /* Parse the type-specifier-seq. */
11853 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11854 &type_specifiers);
11855
11856 /* If that didn't work, stop. */
11857 if (type_specifiers.type != error_mark_node)
11858 {
11859 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11860 /*initialized=*/0, NULL);
11861 if (underlying_type == error_mark_node)
11862 underlying_type = NULL_TREE;
11863 }
11864 }
11865
11866 /* Look for the `{' but don't consume it yet. */
11867 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11868 {
11869 cp_parser_error (parser, "expected %<{%>");
11870 if (has_underlying_type)
11871 return NULL_TREE;
11872 }
11873
11874 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11875 return NULL_TREE;
11876
11877 /* Issue an error message if type-definitions are forbidden here. */
11878 if (!cp_parser_check_type_definition (parser))
11879 type = error_mark_node;
11880 else
11881 /* Create the new type. We do this before consuming the opening
11882 brace so the enum will be recorded as being on the line of its
11883 tag (or the 'enum' keyword, if there is no tag). */
11884 type = start_enum (identifier, underlying_type, scoped_enum_p);
11885
11886 /* Consume the opening brace. */
11887 cp_lexer_consume_token (parser->lexer);
11888
11889 if (type == error_mark_node)
11890 {
11891 cp_parser_skip_to_end_of_block_or_statement (parser);
11892 return error_mark_node;
11893 }
11894
11895 /* If the next token is not '}', then there are some enumerators. */
11896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11897 cp_parser_enumerator_list (parser, type);
11898
11899 /* Consume the final '}'. */
11900 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11901
11902 /* Look for trailing attributes to apply to this enumeration, and
11903 apply them if appropriate. */
11904 if (cp_parser_allow_gnu_extensions_p (parser))
11905 {
11906 tree trailing_attr = cp_parser_attributes_opt (parser);
11907 cplus_decl_attributes (&type,
11908 trailing_attr,
11909 (int) ATTR_FLAG_TYPE_IN_PLACE);
11910 }
11911
11912 /* Finish up the enumeration. */
11913 finish_enum (type);
11914
11915 return type;
11916 }
11917
11918 /* Parse an enumerator-list. The enumerators all have the indicated
11919 TYPE.
11920
11921 enumerator-list:
11922 enumerator-definition
11923 enumerator-list , enumerator-definition */
11924
11925 static void
11926 cp_parser_enumerator_list (cp_parser* parser, tree type)
11927 {
11928 while (true)
11929 {
11930 /* Parse an enumerator-definition. */
11931 cp_parser_enumerator_definition (parser, type);
11932
11933 /* If the next token is not a ',', we've reached the end of
11934 the list. */
11935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11936 break;
11937 /* Otherwise, consume the `,' and keep going. */
11938 cp_lexer_consume_token (parser->lexer);
11939 /* If the next token is a `}', there is a trailing comma. */
11940 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11941 {
11942 if (!in_system_header)
11943 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11944 break;
11945 }
11946 }
11947 }
11948
11949 /* Parse an enumerator-definition. The enumerator has the indicated
11950 TYPE.
11951
11952 enumerator-definition:
11953 enumerator
11954 enumerator = constant-expression
11955
11956 enumerator:
11957 identifier */
11958
11959 static void
11960 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11961 {
11962 tree identifier;
11963 tree value;
11964
11965 /* Look for the identifier. */
11966 identifier = cp_parser_identifier (parser);
11967 if (identifier == error_mark_node)
11968 return;
11969
11970 /* If the next token is an '=', then there is an explicit value. */
11971 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11972 {
11973 /* Consume the `=' token. */
11974 cp_lexer_consume_token (parser->lexer);
11975 /* Parse the value. */
11976 value = cp_parser_constant_expression (parser,
11977 /*allow_non_constant_p=*/false,
11978 NULL);
11979 }
11980 else
11981 value = NULL_TREE;
11982
11983 /* Create the enumerator. */
11984 build_enumerator (identifier, value, type);
11985 }
11986
11987 /* Parse a namespace-name.
11988
11989 namespace-name:
11990 original-namespace-name
11991 namespace-alias
11992
11993 Returns the NAMESPACE_DECL for the namespace. */
11994
11995 static tree
11996 cp_parser_namespace_name (cp_parser* parser)
11997 {
11998 tree identifier;
11999 tree namespace_decl;
12000
12001 cp_token *token = cp_lexer_peek_token (parser->lexer);
12002
12003 /* Get the name of the namespace. */
12004 identifier = cp_parser_identifier (parser);
12005 if (identifier == error_mark_node)
12006 return error_mark_node;
12007
12008 /* Look up the identifier in the currently active scope. Look only
12009 for namespaces, due to:
12010
12011 [basic.lookup.udir]
12012
12013 When looking up a namespace-name in a using-directive or alias
12014 definition, only namespace names are considered.
12015
12016 And:
12017
12018 [basic.lookup.qual]
12019
12020 During the lookup of a name preceding the :: scope resolution
12021 operator, object, function, and enumerator names are ignored.
12022
12023 (Note that cp_parser_qualifying_entity only calls this
12024 function if the token after the name is the scope resolution
12025 operator.) */
12026 namespace_decl = cp_parser_lookup_name (parser, identifier,
12027 none_type,
12028 /*is_template=*/false,
12029 /*is_namespace=*/true,
12030 /*check_dependency=*/true,
12031 /*ambiguous_decls=*/NULL,
12032 token->location);
12033 /* If it's not a namespace, issue an error. */
12034 if (namespace_decl == error_mark_node
12035 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12036 {
12037 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12038 error ("%H%qD is not a namespace-name", &token->location, identifier);
12039 cp_parser_error (parser, "expected namespace-name");
12040 namespace_decl = error_mark_node;
12041 }
12042
12043 return namespace_decl;
12044 }
12045
12046 /* Parse a namespace-definition.
12047
12048 namespace-definition:
12049 named-namespace-definition
12050 unnamed-namespace-definition
12051
12052 named-namespace-definition:
12053 original-namespace-definition
12054 extension-namespace-definition
12055
12056 original-namespace-definition:
12057 namespace identifier { namespace-body }
12058
12059 extension-namespace-definition:
12060 namespace original-namespace-name { namespace-body }
12061
12062 unnamed-namespace-definition:
12063 namespace { namespace-body } */
12064
12065 static void
12066 cp_parser_namespace_definition (cp_parser* parser)
12067 {
12068 tree identifier, attribs;
12069 bool has_visibility;
12070 bool is_inline;
12071
12072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12073 {
12074 is_inline = true;
12075 cp_lexer_consume_token (parser->lexer);
12076 }
12077 else
12078 is_inline = false;
12079
12080 /* Look for the `namespace' keyword. */
12081 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12082
12083 /* Get the name of the namespace. We do not attempt to distinguish
12084 between an original-namespace-definition and an
12085 extension-namespace-definition at this point. The semantic
12086 analysis routines are responsible for that. */
12087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12088 identifier = cp_parser_identifier (parser);
12089 else
12090 identifier = NULL_TREE;
12091
12092 /* Parse any specified attributes. */
12093 attribs = cp_parser_attributes_opt (parser);
12094
12095 /* Look for the `{' to start the namespace. */
12096 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12097 /* Start the namespace. */
12098 push_namespace (identifier);
12099
12100 /* "inline namespace" is equivalent to a stub namespace definition
12101 followed by a strong using directive. */
12102 if (is_inline)
12103 {
12104 tree name_space = current_namespace;
12105 /* Set up namespace association. */
12106 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12107 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12108 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12109 /* Import the contents of the inline namespace. */
12110 pop_namespace ();
12111 do_using_directive (name_space);
12112 push_namespace (identifier);
12113 }
12114
12115 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12116
12117 /* Parse the body of the namespace. */
12118 cp_parser_namespace_body (parser);
12119
12120 #ifdef HANDLE_PRAGMA_VISIBILITY
12121 if (has_visibility)
12122 pop_visibility ();
12123 #endif
12124
12125 /* Finish the namespace. */
12126 pop_namespace ();
12127 /* Look for the final `}'. */
12128 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12129 }
12130
12131 /* Parse a namespace-body.
12132
12133 namespace-body:
12134 declaration-seq [opt] */
12135
12136 static void
12137 cp_parser_namespace_body (cp_parser* parser)
12138 {
12139 cp_parser_declaration_seq_opt (parser);
12140 }
12141
12142 /* Parse a namespace-alias-definition.
12143
12144 namespace-alias-definition:
12145 namespace identifier = qualified-namespace-specifier ; */
12146
12147 static void
12148 cp_parser_namespace_alias_definition (cp_parser* parser)
12149 {
12150 tree identifier;
12151 tree namespace_specifier;
12152
12153 cp_token *token = cp_lexer_peek_token (parser->lexer);
12154
12155 /* Look for the `namespace' keyword. */
12156 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12157 /* Look for the identifier. */
12158 identifier = cp_parser_identifier (parser);
12159 if (identifier == error_mark_node)
12160 return;
12161 /* Look for the `=' token. */
12162 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12163 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12164 {
12165 error ("%H%<namespace%> definition is not allowed here", &token->location);
12166 /* Skip the definition. */
12167 cp_lexer_consume_token (parser->lexer);
12168 if (cp_parser_skip_to_closing_brace (parser))
12169 cp_lexer_consume_token (parser->lexer);
12170 return;
12171 }
12172 cp_parser_require (parser, CPP_EQ, "%<=%>");
12173 /* Look for the qualified-namespace-specifier. */
12174 namespace_specifier
12175 = cp_parser_qualified_namespace_specifier (parser);
12176 /* Look for the `;' token. */
12177 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12178
12179 /* Register the alias in the symbol table. */
12180 do_namespace_alias (identifier, namespace_specifier);
12181 }
12182
12183 /* Parse a qualified-namespace-specifier.
12184
12185 qualified-namespace-specifier:
12186 :: [opt] nested-name-specifier [opt] namespace-name
12187
12188 Returns a NAMESPACE_DECL corresponding to the specified
12189 namespace. */
12190
12191 static tree
12192 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12193 {
12194 /* Look for the optional `::'. */
12195 cp_parser_global_scope_opt (parser,
12196 /*current_scope_valid_p=*/false);
12197
12198 /* Look for the optional nested-name-specifier. */
12199 cp_parser_nested_name_specifier_opt (parser,
12200 /*typename_keyword_p=*/false,
12201 /*check_dependency_p=*/true,
12202 /*type_p=*/false,
12203 /*is_declaration=*/true);
12204
12205 return cp_parser_namespace_name (parser);
12206 }
12207
12208 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12209 access declaration.
12210
12211 using-declaration:
12212 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12213 using :: unqualified-id ;
12214
12215 access-declaration:
12216 qualified-id ;
12217
12218 */
12219
12220 static bool
12221 cp_parser_using_declaration (cp_parser* parser,
12222 bool access_declaration_p)
12223 {
12224 cp_token *token;
12225 bool typename_p = false;
12226 bool global_scope_p;
12227 tree decl;
12228 tree identifier;
12229 tree qscope;
12230
12231 if (access_declaration_p)
12232 cp_parser_parse_tentatively (parser);
12233 else
12234 {
12235 /* Look for the `using' keyword. */
12236 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12237
12238 /* Peek at the next token. */
12239 token = cp_lexer_peek_token (parser->lexer);
12240 /* See if it's `typename'. */
12241 if (token->keyword == RID_TYPENAME)
12242 {
12243 /* Remember that we've seen it. */
12244 typename_p = true;
12245 /* Consume the `typename' token. */
12246 cp_lexer_consume_token (parser->lexer);
12247 }
12248 }
12249
12250 /* Look for the optional global scope qualification. */
12251 global_scope_p
12252 = (cp_parser_global_scope_opt (parser,
12253 /*current_scope_valid_p=*/false)
12254 != NULL_TREE);
12255
12256 /* If we saw `typename', or didn't see `::', then there must be a
12257 nested-name-specifier present. */
12258 if (typename_p || !global_scope_p)
12259 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12260 /*check_dependency_p=*/true,
12261 /*type_p=*/false,
12262 /*is_declaration=*/true);
12263 /* Otherwise, we could be in either of the two productions. In that
12264 case, treat the nested-name-specifier as optional. */
12265 else
12266 qscope = cp_parser_nested_name_specifier_opt (parser,
12267 /*typename_keyword_p=*/false,
12268 /*check_dependency_p=*/true,
12269 /*type_p=*/false,
12270 /*is_declaration=*/true);
12271 if (!qscope)
12272 qscope = global_namespace;
12273
12274 if (access_declaration_p && cp_parser_error_occurred (parser))
12275 /* Something has already gone wrong; there's no need to parse
12276 further. Since an error has occurred, the return value of
12277 cp_parser_parse_definitely will be false, as required. */
12278 return cp_parser_parse_definitely (parser);
12279
12280 token = cp_lexer_peek_token (parser->lexer);
12281 /* Parse the unqualified-id. */
12282 identifier = cp_parser_unqualified_id (parser,
12283 /*template_keyword_p=*/false,
12284 /*check_dependency_p=*/true,
12285 /*declarator_p=*/true,
12286 /*optional_p=*/false);
12287
12288 if (access_declaration_p)
12289 {
12290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12291 cp_parser_simulate_error (parser);
12292 if (!cp_parser_parse_definitely (parser))
12293 return false;
12294 }
12295
12296 /* The function we call to handle a using-declaration is different
12297 depending on what scope we are in. */
12298 if (qscope == error_mark_node || identifier == error_mark_node)
12299 ;
12300 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12301 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12302 /* [namespace.udecl]
12303
12304 A using declaration shall not name a template-id. */
12305 error ("%Ha template-id may not appear in a using-declaration",
12306 &token->location);
12307 else
12308 {
12309 if (at_class_scope_p ())
12310 {
12311 /* Create the USING_DECL. */
12312 decl = do_class_using_decl (parser->scope, identifier);
12313
12314 if (check_for_bare_parameter_packs (decl))
12315 return false;
12316 else
12317 /* Add it to the list of members in this class. */
12318 finish_member_declaration (decl);
12319 }
12320 else
12321 {
12322 decl = cp_parser_lookup_name_simple (parser,
12323 identifier,
12324 token->location);
12325 if (decl == error_mark_node)
12326 cp_parser_name_lookup_error (parser, identifier,
12327 decl, NULL,
12328 token->location);
12329 else if (check_for_bare_parameter_packs (decl))
12330 return false;
12331 else if (!at_namespace_scope_p ())
12332 do_local_using_decl (decl, qscope, identifier);
12333 else
12334 do_toplevel_using_decl (decl, qscope, identifier);
12335 }
12336 }
12337
12338 /* Look for the final `;'. */
12339 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12340
12341 return true;
12342 }
12343
12344 /* Parse a using-directive.
12345
12346 using-directive:
12347 using namespace :: [opt] nested-name-specifier [opt]
12348 namespace-name ; */
12349
12350 static void
12351 cp_parser_using_directive (cp_parser* parser)
12352 {
12353 tree namespace_decl;
12354 tree attribs;
12355
12356 /* Look for the `using' keyword. */
12357 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12358 /* And the `namespace' keyword. */
12359 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12360 /* Look for the optional `::' operator. */
12361 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12362 /* And the optional nested-name-specifier. */
12363 cp_parser_nested_name_specifier_opt (parser,
12364 /*typename_keyword_p=*/false,
12365 /*check_dependency_p=*/true,
12366 /*type_p=*/false,
12367 /*is_declaration=*/true);
12368 /* Get the namespace being used. */
12369 namespace_decl = cp_parser_namespace_name (parser);
12370 /* And any specified attributes. */
12371 attribs = cp_parser_attributes_opt (parser);
12372 /* Update the symbol table. */
12373 parse_using_directive (namespace_decl, attribs);
12374 /* Look for the final `;'. */
12375 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12376 }
12377
12378 /* Parse an asm-definition.
12379
12380 asm-definition:
12381 asm ( string-literal ) ;
12382
12383 GNU Extension:
12384
12385 asm-definition:
12386 asm volatile [opt] ( string-literal ) ;
12387 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12388 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12389 : asm-operand-list [opt] ) ;
12390 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12391 : asm-operand-list [opt]
12392 : asm-operand-list [opt] ) ; */
12393
12394 static void
12395 cp_parser_asm_definition (cp_parser* parser)
12396 {
12397 tree string;
12398 tree outputs = NULL_TREE;
12399 tree inputs = NULL_TREE;
12400 tree clobbers = NULL_TREE;
12401 tree asm_stmt;
12402 bool volatile_p = false;
12403 bool extended_p = false;
12404 bool invalid_inputs_p = false;
12405 bool invalid_outputs_p = false;
12406
12407 /* Look for the `asm' keyword. */
12408 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12409 /* See if the next token is `volatile'. */
12410 if (cp_parser_allow_gnu_extensions_p (parser)
12411 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12412 {
12413 /* Remember that we saw the `volatile' keyword. */
12414 volatile_p = true;
12415 /* Consume the token. */
12416 cp_lexer_consume_token (parser->lexer);
12417 }
12418 /* Look for the opening `('. */
12419 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12420 return;
12421 /* Look for the string. */
12422 string = cp_parser_string_literal (parser, false, false);
12423 if (string == error_mark_node)
12424 {
12425 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12426 /*consume_paren=*/true);
12427 return;
12428 }
12429
12430 /* If we're allowing GNU extensions, check for the extended assembly
12431 syntax. Unfortunately, the `:' tokens need not be separated by
12432 a space in C, and so, for compatibility, we tolerate that here
12433 too. Doing that means that we have to treat the `::' operator as
12434 two `:' tokens. */
12435 if (cp_parser_allow_gnu_extensions_p (parser)
12436 && parser->in_function_body
12437 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12438 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12439 {
12440 bool inputs_p = false;
12441 bool clobbers_p = false;
12442
12443 /* The extended syntax was used. */
12444 extended_p = true;
12445
12446 /* Look for outputs. */
12447 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12448 {
12449 /* Consume the `:'. */
12450 cp_lexer_consume_token (parser->lexer);
12451 /* Parse the output-operands. */
12452 if (cp_lexer_next_token_is_not (parser->lexer,
12453 CPP_COLON)
12454 && cp_lexer_next_token_is_not (parser->lexer,
12455 CPP_SCOPE)
12456 && cp_lexer_next_token_is_not (parser->lexer,
12457 CPP_CLOSE_PAREN))
12458 outputs = cp_parser_asm_operand_list (parser);
12459
12460 if (outputs == error_mark_node)
12461 invalid_outputs_p = true;
12462 }
12463 /* If the next token is `::', there are no outputs, and the
12464 next token is the beginning of the inputs. */
12465 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12466 /* The inputs are coming next. */
12467 inputs_p = true;
12468
12469 /* Look for inputs. */
12470 if (inputs_p
12471 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12472 {
12473 /* Consume the `:' or `::'. */
12474 cp_lexer_consume_token (parser->lexer);
12475 /* Parse the output-operands. */
12476 if (cp_lexer_next_token_is_not (parser->lexer,
12477 CPP_COLON)
12478 && cp_lexer_next_token_is_not (parser->lexer,
12479 CPP_CLOSE_PAREN))
12480 inputs = cp_parser_asm_operand_list (parser);
12481
12482 if (inputs == error_mark_node)
12483 invalid_inputs_p = true;
12484 }
12485 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12486 /* The clobbers are coming next. */
12487 clobbers_p = true;
12488
12489 /* Look for clobbers. */
12490 if (clobbers_p
12491 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12492 {
12493 /* Consume the `:' or `::'. */
12494 cp_lexer_consume_token (parser->lexer);
12495 /* Parse the clobbers. */
12496 if (cp_lexer_next_token_is_not (parser->lexer,
12497 CPP_CLOSE_PAREN))
12498 clobbers = cp_parser_asm_clobber_list (parser);
12499 }
12500 }
12501 /* Look for the closing `)'. */
12502 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12503 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12504 /*consume_paren=*/true);
12505 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12506
12507 if (!invalid_inputs_p && !invalid_outputs_p)
12508 {
12509 /* Create the ASM_EXPR. */
12510 if (parser->in_function_body)
12511 {
12512 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12513 inputs, clobbers);
12514 /* If the extended syntax was not used, mark the ASM_EXPR. */
12515 if (!extended_p)
12516 {
12517 tree temp = asm_stmt;
12518 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12519 temp = TREE_OPERAND (temp, 0);
12520
12521 ASM_INPUT_P (temp) = 1;
12522 }
12523 }
12524 else
12525 cgraph_add_asm_node (string);
12526 }
12527 }
12528
12529 /* Declarators [gram.dcl.decl] */
12530
12531 /* Parse an init-declarator.
12532
12533 init-declarator:
12534 declarator initializer [opt]
12535
12536 GNU Extension:
12537
12538 init-declarator:
12539 declarator asm-specification [opt] attributes [opt] initializer [opt]
12540
12541 function-definition:
12542 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12543 function-body
12544 decl-specifier-seq [opt] declarator function-try-block
12545
12546 GNU Extension:
12547
12548 function-definition:
12549 __extension__ function-definition
12550
12551 The DECL_SPECIFIERS apply to this declarator. Returns a
12552 representation of the entity declared. If MEMBER_P is TRUE, then
12553 this declarator appears in a class scope. The new DECL created by
12554 this declarator is returned.
12555
12556 The CHECKS are access checks that should be performed once we know
12557 what entity is being declared (and, therefore, what classes have
12558 befriended it).
12559
12560 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12561 for a function-definition here as well. If the declarator is a
12562 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12563 be TRUE upon return. By that point, the function-definition will
12564 have been completely parsed.
12565
12566 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12567 is FALSE. */
12568
12569 static tree
12570 cp_parser_init_declarator (cp_parser* parser,
12571 cp_decl_specifier_seq *decl_specifiers,
12572 VEC (deferred_access_check,gc)* checks,
12573 bool function_definition_allowed_p,
12574 bool member_p,
12575 int declares_class_or_enum,
12576 bool* function_definition_p)
12577 {
12578 cp_token *token = NULL, *asm_spec_start_token = NULL,
12579 *attributes_start_token = NULL;
12580 cp_declarator *declarator;
12581 tree prefix_attributes;
12582 tree attributes;
12583 tree asm_specification;
12584 tree initializer;
12585 tree decl = NULL_TREE;
12586 tree scope;
12587 int is_initialized;
12588 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12589 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12590 "(...)". */
12591 enum cpp_ttype initialization_kind;
12592 bool is_direct_init = false;
12593 bool is_non_constant_init;
12594 int ctor_dtor_or_conv_p;
12595 bool friend_p;
12596 tree pushed_scope = NULL;
12597
12598 /* Gather the attributes that were provided with the
12599 decl-specifiers. */
12600 prefix_attributes = decl_specifiers->attributes;
12601
12602 /* Assume that this is not the declarator for a function
12603 definition. */
12604 if (function_definition_p)
12605 *function_definition_p = false;
12606
12607 /* Defer access checks while parsing the declarator; we cannot know
12608 what names are accessible until we know what is being
12609 declared. */
12610 resume_deferring_access_checks ();
12611
12612 /* Parse the declarator. */
12613 token = cp_lexer_peek_token (parser->lexer);
12614 declarator
12615 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12616 &ctor_dtor_or_conv_p,
12617 /*parenthesized_p=*/NULL,
12618 /*member_p=*/false);
12619 /* Gather up the deferred checks. */
12620 stop_deferring_access_checks ();
12621
12622 /* If the DECLARATOR was erroneous, there's no need to go
12623 further. */
12624 if (declarator == cp_error_declarator)
12625 return error_mark_node;
12626
12627 /* Check that the number of template-parameter-lists is OK. */
12628 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12629 token->location))
12630 return error_mark_node;
12631
12632 if (declares_class_or_enum & 2)
12633 cp_parser_check_for_definition_in_return_type (declarator,
12634 decl_specifiers->type,
12635 decl_specifiers->type_location);
12636
12637 /* Figure out what scope the entity declared by the DECLARATOR is
12638 located in. `grokdeclarator' sometimes changes the scope, so
12639 we compute it now. */
12640 scope = get_scope_of_declarator (declarator);
12641
12642 /* If we're allowing GNU extensions, look for an asm-specification
12643 and attributes. */
12644 if (cp_parser_allow_gnu_extensions_p (parser))
12645 {
12646 /* Look for an asm-specification. */
12647 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12648 asm_specification = cp_parser_asm_specification_opt (parser);
12649 /* And attributes. */
12650 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12651 attributes = cp_parser_attributes_opt (parser);
12652 }
12653 else
12654 {
12655 asm_specification = NULL_TREE;
12656 attributes = NULL_TREE;
12657 }
12658
12659 /* Peek at the next token. */
12660 token = cp_lexer_peek_token (parser->lexer);
12661 /* Check to see if the token indicates the start of a
12662 function-definition. */
12663 if (function_declarator_p (declarator)
12664 && cp_parser_token_starts_function_definition_p (token))
12665 {
12666 if (!function_definition_allowed_p)
12667 {
12668 /* If a function-definition should not appear here, issue an
12669 error message. */
12670 cp_parser_error (parser,
12671 "a function-definition is not allowed here");
12672 return error_mark_node;
12673 }
12674 else
12675 {
12676 location_t func_brace_location
12677 = cp_lexer_peek_token (parser->lexer)->location;
12678
12679 /* Neither attributes nor an asm-specification are allowed
12680 on a function-definition. */
12681 if (asm_specification)
12682 error ("%Han asm-specification is not allowed "
12683 "on a function-definition",
12684 &asm_spec_start_token->location);
12685 if (attributes)
12686 error ("%Hattributes are not allowed on a function-definition",
12687 &attributes_start_token->location);
12688 /* This is a function-definition. */
12689 *function_definition_p = true;
12690
12691 /* Parse the function definition. */
12692 if (member_p)
12693 decl = cp_parser_save_member_function_body (parser,
12694 decl_specifiers,
12695 declarator,
12696 prefix_attributes);
12697 else
12698 decl
12699 = (cp_parser_function_definition_from_specifiers_and_declarator
12700 (parser, decl_specifiers, prefix_attributes, declarator));
12701
12702 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12703 {
12704 /* This is where the prologue starts... */
12705 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12706 = func_brace_location;
12707 }
12708
12709 return decl;
12710 }
12711 }
12712
12713 /* [dcl.dcl]
12714
12715 Only in function declarations for constructors, destructors, and
12716 type conversions can the decl-specifier-seq be omitted.
12717
12718 We explicitly postpone this check past the point where we handle
12719 function-definitions because we tolerate function-definitions
12720 that are missing their return types in some modes. */
12721 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12722 {
12723 cp_parser_error (parser,
12724 "expected constructor, destructor, or type conversion");
12725 return error_mark_node;
12726 }
12727
12728 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12729 if (token->type == CPP_EQ
12730 || token->type == CPP_OPEN_PAREN
12731 || token->type == CPP_OPEN_BRACE)
12732 {
12733 is_initialized = SD_INITIALIZED;
12734 initialization_kind = token->type;
12735
12736 if (token->type == CPP_EQ
12737 && function_declarator_p (declarator))
12738 {
12739 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12740 if (t2->keyword == RID_DEFAULT)
12741 is_initialized = SD_DEFAULTED;
12742 else if (t2->keyword == RID_DELETE)
12743 is_initialized = SD_DELETED;
12744 }
12745 }
12746 else
12747 {
12748 /* If the init-declarator isn't initialized and isn't followed by a
12749 `,' or `;', it's not a valid init-declarator. */
12750 if (token->type != CPP_COMMA
12751 && token->type != CPP_SEMICOLON)
12752 {
12753 cp_parser_error (parser, "expected initializer");
12754 return error_mark_node;
12755 }
12756 is_initialized = SD_UNINITIALIZED;
12757 initialization_kind = CPP_EOF;
12758 }
12759
12760 /* Because start_decl has side-effects, we should only call it if we
12761 know we're going ahead. By this point, we know that we cannot
12762 possibly be looking at any other construct. */
12763 cp_parser_commit_to_tentative_parse (parser);
12764
12765 /* If the decl specifiers were bad, issue an error now that we're
12766 sure this was intended to be a declarator. Then continue
12767 declaring the variable(s), as int, to try to cut down on further
12768 errors. */
12769 if (decl_specifiers->any_specifiers_p
12770 && decl_specifiers->type == error_mark_node)
12771 {
12772 cp_parser_error (parser, "invalid type in declaration");
12773 decl_specifiers->type = integer_type_node;
12774 }
12775
12776 /* Check to see whether or not this declaration is a friend. */
12777 friend_p = cp_parser_friend_p (decl_specifiers);
12778
12779 /* Enter the newly declared entry in the symbol table. If we're
12780 processing a declaration in a class-specifier, we wait until
12781 after processing the initializer. */
12782 if (!member_p)
12783 {
12784 if (parser->in_unbraced_linkage_specification_p)
12785 decl_specifiers->storage_class = sc_extern;
12786 decl = start_decl (declarator, decl_specifiers,
12787 is_initialized, attributes, prefix_attributes,
12788 &pushed_scope);
12789 }
12790 else if (scope)
12791 /* Enter the SCOPE. That way unqualified names appearing in the
12792 initializer will be looked up in SCOPE. */
12793 pushed_scope = push_scope (scope);
12794
12795 /* Perform deferred access control checks, now that we know in which
12796 SCOPE the declared entity resides. */
12797 if (!member_p && decl)
12798 {
12799 tree saved_current_function_decl = NULL_TREE;
12800
12801 /* If the entity being declared is a function, pretend that we
12802 are in its scope. If it is a `friend', it may have access to
12803 things that would not otherwise be accessible. */
12804 if (TREE_CODE (decl) == FUNCTION_DECL)
12805 {
12806 saved_current_function_decl = current_function_decl;
12807 current_function_decl = decl;
12808 }
12809
12810 /* Perform access checks for template parameters. */
12811 cp_parser_perform_template_parameter_access_checks (checks);
12812
12813 /* Perform the access control checks for the declarator and the
12814 decl-specifiers. */
12815 perform_deferred_access_checks ();
12816
12817 /* Restore the saved value. */
12818 if (TREE_CODE (decl) == FUNCTION_DECL)
12819 current_function_decl = saved_current_function_decl;
12820 }
12821
12822 /* Parse the initializer. */
12823 initializer = NULL_TREE;
12824 is_direct_init = false;
12825 is_non_constant_init = true;
12826 if (is_initialized)
12827 {
12828 if (function_declarator_p (declarator))
12829 {
12830 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12831 if (initialization_kind == CPP_EQ)
12832 initializer = cp_parser_pure_specifier (parser);
12833 else
12834 {
12835 /* If the declaration was erroneous, we don't really
12836 know what the user intended, so just silently
12837 consume the initializer. */
12838 if (decl != error_mark_node)
12839 error ("%Hinitializer provided for function",
12840 &initializer_start_token->location);
12841 cp_parser_skip_to_closing_parenthesis (parser,
12842 /*recovering=*/true,
12843 /*or_comma=*/false,
12844 /*consume_paren=*/true);
12845 }
12846 }
12847 else
12848 initializer = cp_parser_initializer (parser,
12849 &is_direct_init,
12850 &is_non_constant_init);
12851 }
12852
12853 /* The old parser allows attributes to appear after a parenthesized
12854 initializer. Mark Mitchell proposed removing this functionality
12855 on the GCC mailing lists on 2002-08-13. This parser accepts the
12856 attributes -- but ignores them. */
12857 if (cp_parser_allow_gnu_extensions_p (parser)
12858 && initialization_kind == CPP_OPEN_PAREN)
12859 if (cp_parser_attributes_opt (parser))
12860 warning (OPT_Wattributes,
12861 "attributes after parenthesized initializer ignored");
12862
12863 /* For an in-class declaration, use `grokfield' to create the
12864 declaration. */
12865 if (member_p)
12866 {
12867 if (pushed_scope)
12868 {
12869 pop_scope (pushed_scope);
12870 pushed_scope = false;
12871 }
12872 decl = grokfield (declarator, decl_specifiers,
12873 initializer, !is_non_constant_init,
12874 /*asmspec=*/NULL_TREE,
12875 prefix_attributes);
12876 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12877 cp_parser_save_default_args (parser, decl);
12878 }
12879
12880 /* Finish processing the declaration. But, skip friend
12881 declarations. */
12882 if (!friend_p && decl && decl != error_mark_node)
12883 {
12884 cp_finish_decl (decl,
12885 initializer, !is_non_constant_init,
12886 asm_specification,
12887 /* If the initializer is in parentheses, then this is
12888 a direct-initialization, which means that an
12889 `explicit' constructor is OK. Otherwise, an
12890 `explicit' constructor cannot be used. */
12891 ((is_direct_init || !is_initialized)
12892 ? 0 : LOOKUP_ONLYCONVERTING));
12893 }
12894 else if ((cxx_dialect != cxx98) && friend_p
12895 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12896 /* Core issue #226 (C++0x only): A default template-argument
12897 shall not be specified in a friend class template
12898 declaration. */
12899 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12900 /*is_partial=*/0, /*is_friend_decl=*/1);
12901
12902 if (!friend_p && pushed_scope)
12903 pop_scope (pushed_scope);
12904
12905 return decl;
12906 }
12907
12908 /* Parse a declarator.
12909
12910 declarator:
12911 direct-declarator
12912 ptr-operator declarator
12913
12914 abstract-declarator:
12915 ptr-operator abstract-declarator [opt]
12916 direct-abstract-declarator
12917
12918 GNU Extensions:
12919
12920 declarator:
12921 attributes [opt] direct-declarator
12922 attributes [opt] ptr-operator declarator
12923
12924 abstract-declarator:
12925 attributes [opt] ptr-operator abstract-declarator [opt]
12926 attributes [opt] direct-abstract-declarator
12927
12928 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12929 detect constructor, destructor or conversion operators. It is set
12930 to -1 if the declarator is a name, and +1 if it is a
12931 function. Otherwise it is set to zero. Usually you just want to
12932 test for >0, but internally the negative value is used.
12933
12934 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12935 a decl-specifier-seq unless it declares a constructor, destructor,
12936 or conversion. It might seem that we could check this condition in
12937 semantic analysis, rather than parsing, but that makes it difficult
12938 to handle something like `f()'. We want to notice that there are
12939 no decl-specifiers, and therefore realize that this is an
12940 expression, not a declaration.)
12941
12942 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12943 the declarator is a direct-declarator of the form "(...)".
12944
12945 MEMBER_P is true iff this declarator is a member-declarator. */
12946
12947 static cp_declarator *
12948 cp_parser_declarator (cp_parser* parser,
12949 cp_parser_declarator_kind dcl_kind,
12950 int* ctor_dtor_or_conv_p,
12951 bool* parenthesized_p,
12952 bool member_p)
12953 {
12954 cp_token *token;
12955 cp_declarator *declarator;
12956 enum tree_code code;
12957 cp_cv_quals cv_quals;
12958 tree class_type;
12959 tree attributes = NULL_TREE;
12960
12961 /* Assume this is not a constructor, destructor, or type-conversion
12962 operator. */
12963 if (ctor_dtor_or_conv_p)
12964 *ctor_dtor_or_conv_p = 0;
12965
12966 if (cp_parser_allow_gnu_extensions_p (parser))
12967 attributes = cp_parser_attributes_opt (parser);
12968
12969 /* Peek at the next token. */
12970 token = cp_lexer_peek_token (parser->lexer);
12971
12972 /* Check for the ptr-operator production. */
12973 cp_parser_parse_tentatively (parser);
12974 /* Parse the ptr-operator. */
12975 code = cp_parser_ptr_operator (parser,
12976 &class_type,
12977 &cv_quals);
12978 /* If that worked, then we have a ptr-operator. */
12979 if (cp_parser_parse_definitely (parser))
12980 {
12981 /* If a ptr-operator was found, then this declarator was not
12982 parenthesized. */
12983 if (parenthesized_p)
12984 *parenthesized_p = true;
12985 /* The dependent declarator is optional if we are parsing an
12986 abstract-declarator. */
12987 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12988 cp_parser_parse_tentatively (parser);
12989
12990 /* Parse the dependent declarator. */
12991 declarator = cp_parser_declarator (parser, dcl_kind,
12992 /*ctor_dtor_or_conv_p=*/NULL,
12993 /*parenthesized_p=*/NULL,
12994 /*member_p=*/false);
12995
12996 /* If we are parsing an abstract-declarator, we must handle the
12997 case where the dependent declarator is absent. */
12998 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12999 && !cp_parser_parse_definitely (parser))
13000 declarator = NULL;
13001
13002 declarator = cp_parser_make_indirect_declarator
13003 (code, class_type, cv_quals, declarator);
13004 }
13005 /* Everything else is a direct-declarator. */
13006 else
13007 {
13008 if (parenthesized_p)
13009 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13010 CPP_OPEN_PAREN);
13011 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13012 ctor_dtor_or_conv_p,
13013 member_p);
13014 }
13015
13016 if (attributes && declarator && declarator != cp_error_declarator)
13017 declarator->attributes = attributes;
13018
13019 return declarator;
13020 }
13021
13022 /* Parse a direct-declarator or direct-abstract-declarator.
13023
13024 direct-declarator:
13025 declarator-id
13026 direct-declarator ( parameter-declaration-clause )
13027 cv-qualifier-seq [opt]
13028 exception-specification [opt]
13029 direct-declarator [ constant-expression [opt] ]
13030 ( declarator )
13031
13032 direct-abstract-declarator:
13033 direct-abstract-declarator [opt]
13034 ( parameter-declaration-clause )
13035 cv-qualifier-seq [opt]
13036 exception-specification [opt]
13037 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13038 ( abstract-declarator )
13039
13040 Returns a representation of the declarator. DCL_KIND is
13041 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13042 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13043 we are parsing a direct-declarator. It is
13044 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13045 of ambiguity we prefer an abstract declarator, as per
13046 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13047 cp_parser_declarator. */
13048
13049 static cp_declarator *
13050 cp_parser_direct_declarator (cp_parser* parser,
13051 cp_parser_declarator_kind dcl_kind,
13052 int* ctor_dtor_or_conv_p,
13053 bool member_p)
13054 {
13055 cp_token *token;
13056 cp_declarator *declarator = NULL;
13057 tree scope = NULL_TREE;
13058 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13059 bool saved_in_declarator_p = parser->in_declarator_p;
13060 bool first = true;
13061 tree pushed_scope = NULL_TREE;
13062
13063 while (true)
13064 {
13065 /* Peek at the next token. */
13066 token = cp_lexer_peek_token (parser->lexer);
13067 if (token->type == CPP_OPEN_PAREN)
13068 {
13069 /* This is either a parameter-declaration-clause, or a
13070 parenthesized declarator. When we know we are parsing a
13071 named declarator, it must be a parenthesized declarator
13072 if FIRST is true. For instance, `(int)' is a
13073 parameter-declaration-clause, with an omitted
13074 direct-abstract-declarator. But `((*))', is a
13075 parenthesized abstract declarator. Finally, when T is a
13076 template parameter `(T)' is a
13077 parameter-declaration-clause, and not a parenthesized
13078 named declarator.
13079
13080 We first try and parse a parameter-declaration-clause,
13081 and then try a nested declarator (if FIRST is true).
13082
13083 It is not an error for it not to be a
13084 parameter-declaration-clause, even when FIRST is
13085 false. Consider,
13086
13087 int i (int);
13088 int i (3);
13089
13090 The first is the declaration of a function while the
13091 second is the definition of a variable, including its
13092 initializer.
13093
13094 Having seen only the parenthesis, we cannot know which of
13095 these two alternatives should be selected. Even more
13096 complex are examples like:
13097
13098 int i (int (a));
13099 int i (int (3));
13100
13101 The former is a function-declaration; the latter is a
13102 variable initialization.
13103
13104 Thus again, we try a parameter-declaration-clause, and if
13105 that fails, we back out and return. */
13106
13107 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13108 {
13109 tree params;
13110 unsigned saved_num_template_parameter_lists;
13111 bool is_declarator = false;
13112 tree t;
13113
13114 /* In a member-declarator, the only valid interpretation
13115 of a parenthesis is the start of a
13116 parameter-declaration-clause. (It is invalid to
13117 initialize a static data member with a parenthesized
13118 initializer; only the "=" form of initialization is
13119 permitted.) */
13120 if (!member_p)
13121 cp_parser_parse_tentatively (parser);
13122
13123 /* Consume the `('. */
13124 cp_lexer_consume_token (parser->lexer);
13125 if (first)
13126 {
13127 /* If this is going to be an abstract declarator, we're
13128 in a declarator and we can't have default args. */
13129 parser->default_arg_ok_p = false;
13130 parser->in_declarator_p = true;
13131 }
13132
13133 /* Inside the function parameter list, surrounding
13134 template-parameter-lists do not apply. */
13135 saved_num_template_parameter_lists
13136 = parser->num_template_parameter_lists;
13137 parser->num_template_parameter_lists = 0;
13138
13139 begin_scope (sk_function_parms, NULL_TREE);
13140
13141 /* Parse the parameter-declaration-clause. */
13142 params = cp_parser_parameter_declaration_clause (parser);
13143
13144 parser->num_template_parameter_lists
13145 = saved_num_template_parameter_lists;
13146
13147 /* If all went well, parse the cv-qualifier-seq and the
13148 exception-specification. */
13149 if (member_p || cp_parser_parse_definitely (parser))
13150 {
13151 cp_cv_quals cv_quals;
13152 tree exception_specification;
13153 tree late_return;
13154
13155 is_declarator = true;
13156
13157 if (ctor_dtor_or_conv_p)
13158 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13159 first = false;
13160 /* Consume the `)'. */
13161 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13162
13163 /* Parse the cv-qualifier-seq. */
13164 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13165 /* And the exception-specification. */
13166 exception_specification
13167 = cp_parser_exception_specification_opt (parser);
13168
13169 late_return
13170 = cp_parser_late_return_type_opt (parser);
13171
13172 /* Create the function-declarator. */
13173 declarator = make_call_declarator (declarator,
13174 params,
13175 cv_quals,
13176 exception_specification,
13177 late_return);
13178 /* Any subsequent parameter lists are to do with
13179 return type, so are not those of the declared
13180 function. */
13181 parser->default_arg_ok_p = false;
13182 }
13183
13184 /* Remove the function parms from scope. */
13185 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13186 pop_binding (DECL_NAME (t), t);
13187 leave_scope();
13188
13189 if (is_declarator)
13190 /* Repeat the main loop. */
13191 continue;
13192 }
13193
13194 /* If this is the first, we can try a parenthesized
13195 declarator. */
13196 if (first)
13197 {
13198 bool saved_in_type_id_in_expr_p;
13199
13200 parser->default_arg_ok_p = saved_default_arg_ok_p;
13201 parser->in_declarator_p = saved_in_declarator_p;
13202
13203 /* Consume the `('. */
13204 cp_lexer_consume_token (parser->lexer);
13205 /* Parse the nested declarator. */
13206 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13207 parser->in_type_id_in_expr_p = true;
13208 declarator
13209 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13210 /*parenthesized_p=*/NULL,
13211 member_p);
13212 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13213 first = false;
13214 /* Expect a `)'. */
13215 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13216 declarator = cp_error_declarator;
13217 if (declarator == cp_error_declarator)
13218 break;
13219
13220 goto handle_declarator;
13221 }
13222 /* Otherwise, we must be done. */
13223 else
13224 break;
13225 }
13226 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13227 && token->type == CPP_OPEN_SQUARE)
13228 {
13229 /* Parse an array-declarator. */
13230 tree bounds;
13231
13232 if (ctor_dtor_or_conv_p)
13233 *ctor_dtor_or_conv_p = 0;
13234
13235 first = false;
13236 parser->default_arg_ok_p = false;
13237 parser->in_declarator_p = true;
13238 /* Consume the `['. */
13239 cp_lexer_consume_token (parser->lexer);
13240 /* Peek at the next token. */
13241 token = cp_lexer_peek_token (parser->lexer);
13242 /* If the next token is `]', then there is no
13243 constant-expression. */
13244 if (token->type != CPP_CLOSE_SQUARE)
13245 {
13246 bool non_constant_p;
13247
13248 bounds
13249 = cp_parser_constant_expression (parser,
13250 /*allow_non_constant=*/true,
13251 &non_constant_p);
13252 if (!non_constant_p)
13253 bounds = fold_non_dependent_expr (bounds);
13254 /* Normally, the array bound must be an integral constant
13255 expression. However, as an extension, we allow VLAs
13256 in function scopes. */
13257 else if (!parser->in_function_body)
13258 {
13259 error ("%Harray bound is not an integer constant",
13260 &token->location);
13261 bounds = error_mark_node;
13262 }
13263 }
13264 else
13265 bounds = NULL_TREE;
13266 /* Look for the closing `]'. */
13267 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13268 {
13269 declarator = cp_error_declarator;
13270 break;
13271 }
13272
13273 declarator = make_array_declarator (declarator, bounds);
13274 }
13275 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13276 {
13277 tree qualifying_scope;
13278 tree unqualified_name;
13279 special_function_kind sfk;
13280 bool abstract_ok;
13281 bool pack_expansion_p = false;
13282 cp_token *declarator_id_start_token;
13283
13284 /* Parse a declarator-id */
13285 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13286 if (abstract_ok)
13287 {
13288 cp_parser_parse_tentatively (parser);
13289
13290 /* If we see an ellipsis, we should be looking at a
13291 parameter pack. */
13292 if (token->type == CPP_ELLIPSIS)
13293 {
13294 /* Consume the `...' */
13295 cp_lexer_consume_token (parser->lexer);
13296
13297 pack_expansion_p = true;
13298 }
13299 }
13300
13301 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13302 unqualified_name
13303 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13304 qualifying_scope = parser->scope;
13305 if (abstract_ok)
13306 {
13307 bool okay = false;
13308
13309 if (!unqualified_name && pack_expansion_p)
13310 {
13311 /* Check whether an error occurred. */
13312 okay = !cp_parser_error_occurred (parser);
13313
13314 /* We already consumed the ellipsis to mark a
13315 parameter pack, but we have no way to report it,
13316 so abort the tentative parse. We will be exiting
13317 immediately anyway. */
13318 cp_parser_abort_tentative_parse (parser);
13319 }
13320 else
13321 okay = cp_parser_parse_definitely (parser);
13322
13323 if (!okay)
13324 unqualified_name = error_mark_node;
13325 else if (unqualified_name
13326 && (qualifying_scope
13327 || (TREE_CODE (unqualified_name)
13328 != IDENTIFIER_NODE)))
13329 {
13330 cp_parser_error (parser, "expected unqualified-id");
13331 unqualified_name = error_mark_node;
13332 }
13333 }
13334
13335 if (!unqualified_name)
13336 return NULL;
13337 if (unqualified_name == error_mark_node)
13338 {
13339 declarator = cp_error_declarator;
13340 pack_expansion_p = false;
13341 declarator->parameter_pack_p = false;
13342 break;
13343 }
13344
13345 if (qualifying_scope && at_namespace_scope_p ()
13346 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13347 {
13348 /* In the declaration of a member of a template class
13349 outside of the class itself, the SCOPE will sometimes
13350 be a TYPENAME_TYPE. For example, given:
13351
13352 template <typename T>
13353 int S<T>::R::i = 3;
13354
13355 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13356 this context, we must resolve S<T>::R to an ordinary
13357 type, rather than a typename type.
13358
13359 The reason we normally avoid resolving TYPENAME_TYPEs
13360 is that a specialization of `S' might render
13361 `S<T>::R' not a type. However, if `S' is
13362 specialized, then this `i' will not be used, so there
13363 is no harm in resolving the types here. */
13364 tree type;
13365
13366 /* Resolve the TYPENAME_TYPE. */
13367 type = resolve_typename_type (qualifying_scope,
13368 /*only_current_p=*/false);
13369 /* If that failed, the declarator is invalid. */
13370 if (TREE_CODE (type) == TYPENAME_TYPE)
13371 error ("%H%<%T::%E%> is not a type",
13372 &declarator_id_start_token->location,
13373 TYPE_CONTEXT (qualifying_scope),
13374 TYPE_IDENTIFIER (qualifying_scope));
13375 qualifying_scope = type;
13376 }
13377
13378 sfk = sfk_none;
13379
13380 if (unqualified_name)
13381 {
13382 tree class_type;
13383
13384 if (qualifying_scope
13385 && CLASS_TYPE_P (qualifying_scope))
13386 class_type = qualifying_scope;
13387 else
13388 class_type = current_class_type;
13389
13390 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13391 {
13392 tree name_type = TREE_TYPE (unqualified_name);
13393 if (class_type && same_type_p (name_type, class_type))
13394 {
13395 if (qualifying_scope
13396 && CLASSTYPE_USE_TEMPLATE (name_type))
13397 {
13398 error ("%Hinvalid use of constructor as a template",
13399 &declarator_id_start_token->location);
13400 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13401 "name the constructor in a qualified name",
13402 class_type,
13403 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13404 class_type, name_type);
13405 declarator = cp_error_declarator;
13406 break;
13407 }
13408 else
13409 unqualified_name = constructor_name (class_type);
13410 }
13411 else
13412 {
13413 /* We do not attempt to print the declarator
13414 here because we do not have enough
13415 information about its original syntactic
13416 form. */
13417 cp_parser_error (parser, "invalid declarator");
13418 declarator = cp_error_declarator;
13419 break;
13420 }
13421 }
13422
13423 if (class_type)
13424 {
13425 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13426 sfk = sfk_destructor;
13427 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13428 sfk = sfk_conversion;
13429 else if (/* There's no way to declare a constructor
13430 for an anonymous type, even if the type
13431 got a name for linkage purposes. */
13432 !TYPE_WAS_ANONYMOUS (class_type)
13433 && constructor_name_p (unqualified_name,
13434 class_type))
13435 {
13436 unqualified_name = constructor_name (class_type);
13437 sfk = sfk_constructor;
13438 }
13439
13440 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13441 *ctor_dtor_or_conv_p = -1;
13442 }
13443 }
13444 declarator = make_id_declarator (qualifying_scope,
13445 unqualified_name,
13446 sfk);
13447 declarator->id_loc = token->location;
13448 declarator->parameter_pack_p = pack_expansion_p;
13449
13450 if (pack_expansion_p)
13451 maybe_warn_variadic_templates ();
13452
13453 handle_declarator:;
13454 scope = get_scope_of_declarator (declarator);
13455 if (scope)
13456 /* Any names that appear after the declarator-id for a
13457 member are looked up in the containing scope. */
13458 pushed_scope = push_scope (scope);
13459 parser->in_declarator_p = true;
13460 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13461 || (declarator && declarator->kind == cdk_id))
13462 /* Default args are only allowed on function
13463 declarations. */
13464 parser->default_arg_ok_p = saved_default_arg_ok_p;
13465 else
13466 parser->default_arg_ok_p = false;
13467
13468 first = false;
13469 }
13470 /* We're done. */
13471 else
13472 break;
13473 }
13474
13475 /* For an abstract declarator, we might wind up with nothing at this
13476 point. That's an error; the declarator is not optional. */
13477 if (!declarator)
13478 cp_parser_error (parser, "expected declarator");
13479
13480 /* If we entered a scope, we must exit it now. */
13481 if (pushed_scope)
13482 pop_scope (pushed_scope);
13483
13484 parser->default_arg_ok_p = saved_default_arg_ok_p;
13485 parser->in_declarator_p = saved_in_declarator_p;
13486
13487 return declarator;
13488 }
13489
13490 /* Parse a ptr-operator.
13491
13492 ptr-operator:
13493 * cv-qualifier-seq [opt]
13494 &
13495 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13496
13497 GNU Extension:
13498
13499 ptr-operator:
13500 & cv-qualifier-seq [opt]
13501
13502 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13503 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13504 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13505 filled in with the TYPE containing the member. *CV_QUALS is
13506 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13507 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13508 Note that the tree codes returned by this function have nothing
13509 to do with the types of trees that will be eventually be created
13510 to represent the pointer or reference type being parsed. They are
13511 just constants with suggestive names. */
13512 static enum tree_code
13513 cp_parser_ptr_operator (cp_parser* parser,
13514 tree* type,
13515 cp_cv_quals *cv_quals)
13516 {
13517 enum tree_code code = ERROR_MARK;
13518 cp_token *token;
13519
13520 /* Assume that it's not a pointer-to-member. */
13521 *type = NULL_TREE;
13522 /* And that there are no cv-qualifiers. */
13523 *cv_quals = TYPE_UNQUALIFIED;
13524
13525 /* Peek at the next token. */
13526 token = cp_lexer_peek_token (parser->lexer);
13527
13528 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13529 if (token->type == CPP_MULT)
13530 code = INDIRECT_REF;
13531 else if (token->type == CPP_AND)
13532 code = ADDR_EXPR;
13533 else if ((cxx_dialect != cxx98) &&
13534 token->type == CPP_AND_AND) /* C++0x only */
13535 code = NON_LVALUE_EXPR;
13536
13537 if (code != ERROR_MARK)
13538 {
13539 /* Consume the `*', `&' or `&&'. */
13540 cp_lexer_consume_token (parser->lexer);
13541
13542 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13543 `&', if we are allowing GNU extensions. (The only qualifier
13544 that can legally appear after `&' is `restrict', but that is
13545 enforced during semantic analysis. */
13546 if (code == INDIRECT_REF
13547 || cp_parser_allow_gnu_extensions_p (parser))
13548 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13549 }
13550 else
13551 {
13552 /* Try the pointer-to-member case. */
13553 cp_parser_parse_tentatively (parser);
13554 /* Look for the optional `::' operator. */
13555 cp_parser_global_scope_opt (parser,
13556 /*current_scope_valid_p=*/false);
13557 /* Look for the nested-name specifier. */
13558 token = cp_lexer_peek_token (parser->lexer);
13559 cp_parser_nested_name_specifier (parser,
13560 /*typename_keyword_p=*/false,
13561 /*check_dependency_p=*/true,
13562 /*type_p=*/false,
13563 /*is_declaration=*/false);
13564 /* If we found it, and the next token is a `*', then we are
13565 indeed looking at a pointer-to-member operator. */
13566 if (!cp_parser_error_occurred (parser)
13567 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13568 {
13569 /* Indicate that the `*' operator was used. */
13570 code = INDIRECT_REF;
13571
13572 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13573 error ("%H%qD is a namespace", &token->location, parser->scope);
13574 else
13575 {
13576 /* The type of which the member is a member is given by the
13577 current SCOPE. */
13578 *type = parser->scope;
13579 /* The next name will not be qualified. */
13580 parser->scope = NULL_TREE;
13581 parser->qualifying_scope = NULL_TREE;
13582 parser->object_scope = NULL_TREE;
13583 /* Look for the optional cv-qualifier-seq. */
13584 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13585 }
13586 }
13587 /* If that didn't work we don't have a ptr-operator. */
13588 if (!cp_parser_parse_definitely (parser))
13589 cp_parser_error (parser, "expected ptr-operator");
13590 }
13591
13592 return code;
13593 }
13594
13595 /* Parse an (optional) cv-qualifier-seq.
13596
13597 cv-qualifier-seq:
13598 cv-qualifier cv-qualifier-seq [opt]
13599
13600 cv-qualifier:
13601 const
13602 volatile
13603
13604 GNU Extension:
13605
13606 cv-qualifier:
13607 __restrict__
13608
13609 Returns a bitmask representing the cv-qualifiers. */
13610
13611 static cp_cv_quals
13612 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13613 {
13614 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13615
13616 while (true)
13617 {
13618 cp_token *token;
13619 cp_cv_quals cv_qualifier;
13620
13621 /* Peek at the next token. */
13622 token = cp_lexer_peek_token (parser->lexer);
13623 /* See if it's a cv-qualifier. */
13624 switch (token->keyword)
13625 {
13626 case RID_CONST:
13627 cv_qualifier = TYPE_QUAL_CONST;
13628 break;
13629
13630 case RID_VOLATILE:
13631 cv_qualifier = TYPE_QUAL_VOLATILE;
13632 break;
13633
13634 case RID_RESTRICT:
13635 cv_qualifier = TYPE_QUAL_RESTRICT;
13636 break;
13637
13638 default:
13639 cv_qualifier = TYPE_UNQUALIFIED;
13640 break;
13641 }
13642
13643 if (!cv_qualifier)
13644 break;
13645
13646 if (cv_quals & cv_qualifier)
13647 {
13648 error ("%Hduplicate cv-qualifier", &token->location);
13649 cp_lexer_purge_token (parser->lexer);
13650 }
13651 else
13652 {
13653 cp_lexer_consume_token (parser->lexer);
13654 cv_quals |= cv_qualifier;
13655 }
13656 }
13657
13658 return cv_quals;
13659 }
13660
13661 /* Parse a late-specified return type, if any. This is not a separate
13662 non-terminal, but part of a function declarator, which looks like
13663
13664 -> type-id
13665
13666 Returns the type indicated by the type-id. */
13667
13668 static tree
13669 cp_parser_late_return_type_opt (cp_parser* parser)
13670 {
13671 cp_token *token;
13672
13673 /* Peek at the next token. */
13674 token = cp_lexer_peek_token (parser->lexer);
13675 /* A late-specified return type is indicated by an initial '->'. */
13676 if (token->type != CPP_DEREF)
13677 return NULL_TREE;
13678
13679 /* Consume the ->. */
13680 cp_lexer_consume_token (parser->lexer);
13681
13682 return cp_parser_type_id (parser);
13683 }
13684
13685 /* Parse a declarator-id.
13686
13687 declarator-id:
13688 id-expression
13689 :: [opt] nested-name-specifier [opt] type-name
13690
13691 In the `id-expression' case, the value returned is as for
13692 cp_parser_id_expression if the id-expression was an unqualified-id.
13693 If the id-expression was a qualified-id, then a SCOPE_REF is
13694 returned. The first operand is the scope (either a NAMESPACE_DECL
13695 or TREE_TYPE), but the second is still just a representation of an
13696 unqualified-id. */
13697
13698 static tree
13699 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13700 {
13701 tree id;
13702 /* The expression must be an id-expression. Assume that qualified
13703 names are the names of types so that:
13704
13705 template <class T>
13706 int S<T>::R::i = 3;
13707
13708 will work; we must treat `S<T>::R' as the name of a type.
13709 Similarly, assume that qualified names are templates, where
13710 required, so that:
13711
13712 template <class T>
13713 int S<T>::R<T>::i = 3;
13714
13715 will work, too. */
13716 id = cp_parser_id_expression (parser,
13717 /*template_keyword_p=*/false,
13718 /*check_dependency_p=*/false,
13719 /*template_p=*/NULL,
13720 /*declarator_p=*/true,
13721 optional_p);
13722 if (id && BASELINK_P (id))
13723 id = BASELINK_FUNCTIONS (id);
13724 return id;
13725 }
13726
13727 /* Parse a type-id.
13728
13729 type-id:
13730 type-specifier-seq abstract-declarator [opt]
13731
13732 Returns the TYPE specified. */
13733
13734 static tree
13735 cp_parser_type_id (cp_parser* parser)
13736 {
13737 cp_decl_specifier_seq type_specifier_seq;
13738 cp_declarator *abstract_declarator;
13739
13740 /* Parse the type-specifier-seq. */
13741 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13742 &type_specifier_seq);
13743 if (type_specifier_seq.type == error_mark_node)
13744 return error_mark_node;
13745
13746 /* There might or might not be an abstract declarator. */
13747 cp_parser_parse_tentatively (parser);
13748 /* Look for the declarator. */
13749 abstract_declarator
13750 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13751 /*parenthesized_p=*/NULL,
13752 /*member_p=*/false);
13753 /* Check to see if there really was a declarator. */
13754 if (!cp_parser_parse_definitely (parser))
13755 abstract_declarator = NULL;
13756
13757 if (type_specifier_seq.type
13758 && type_uses_auto (type_specifier_seq.type))
13759 {
13760 error ("invalid use of %<auto%>");
13761 return error_mark_node;
13762 }
13763
13764 return groktypename (&type_specifier_seq, abstract_declarator);
13765 }
13766
13767 /* Parse a type-specifier-seq.
13768
13769 type-specifier-seq:
13770 type-specifier type-specifier-seq [opt]
13771
13772 GNU extension:
13773
13774 type-specifier-seq:
13775 attributes type-specifier-seq [opt]
13776
13777 If IS_CONDITION is true, we are at the start of a "condition",
13778 e.g., we've just seen "if (".
13779
13780 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13781
13782 static void
13783 cp_parser_type_specifier_seq (cp_parser* parser,
13784 bool is_condition,
13785 cp_decl_specifier_seq *type_specifier_seq)
13786 {
13787 bool seen_type_specifier = false;
13788 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13789 cp_token *start_token = NULL;
13790
13791 /* Clear the TYPE_SPECIFIER_SEQ. */
13792 clear_decl_specs (type_specifier_seq);
13793
13794 /* Parse the type-specifiers and attributes. */
13795 while (true)
13796 {
13797 tree type_specifier;
13798 bool is_cv_qualifier;
13799
13800 /* Check for attributes first. */
13801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13802 {
13803 type_specifier_seq->attributes =
13804 chainon (type_specifier_seq->attributes,
13805 cp_parser_attributes_opt (parser));
13806 continue;
13807 }
13808
13809 /* record the token of the beginning of the type specifier seq,
13810 for error reporting purposes*/
13811 if (!start_token)
13812 start_token = cp_lexer_peek_token (parser->lexer);
13813
13814 /* Look for the type-specifier. */
13815 type_specifier = cp_parser_type_specifier (parser,
13816 flags,
13817 type_specifier_seq,
13818 /*is_declaration=*/false,
13819 NULL,
13820 &is_cv_qualifier);
13821 if (!type_specifier)
13822 {
13823 /* If the first type-specifier could not be found, this is not a
13824 type-specifier-seq at all. */
13825 if (!seen_type_specifier)
13826 {
13827 cp_parser_error (parser, "expected type-specifier");
13828 type_specifier_seq->type = error_mark_node;
13829 return;
13830 }
13831 /* If subsequent type-specifiers could not be found, the
13832 type-specifier-seq is complete. */
13833 break;
13834 }
13835
13836 seen_type_specifier = true;
13837 /* The standard says that a condition can be:
13838
13839 type-specifier-seq declarator = assignment-expression
13840
13841 However, given:
13842
13843 struct S {};
13844 if (int S = ...)
13845
13846 we should treat the "S" as a declarator, not as a
13847 type-specifier. The standard doesn't say that explicitly for
13848 type-specifier-seq, but it does say that for
13849 decl-specifier-seq in an ordinary declaration. Perhaps it
13850 would be clearer just to allow a decl-specifier-seq here, and
13851 then add a semantic restriction that if any decl-specifiers
13852 that are not type-specifiers appear, the program is invalid. */
13853 if (is_condition && !is_cv_qualifier)
13854 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13855 }
13856
13857 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13858 }
13859
13860 /* Parse a parameter-declaration-clause.
13861
13862 parameter-declaration-clause:
13863 parameter-declaration-list [opt] ... [opt]
13864 parameter-declaration-list , ...
13865
13866 Returns a representation for the parameter declarations. A return
13867 value of NULL indicates a parameter-declaration-clause consisting
13868 only of an ellipsis. */
13869
13870 static tree
13871 cp_parser_parameter_declaration_clause (cp_parser* parser)
13872 {
13873 tree parameters;
13874 cp_token *token;
13875 bool ellipsis_p;
13876 bool is_error;
13877
13878 /* Peek at the next token. */
13879 token = cp_lexer_peek_token (parser->lexer);
13880 /* Check for trivial parameter-declaration-clauses. */
13881 if (token->type == CPP_ELLIPSIS)
13882 {
13883 /* Consume the `...' token. */
13884 cp_lexer_consume_token (parser->lexer);
13885 return NULL_TREE;
13886 }
13887 else if (token->type == CPP_CLOSE_PAREN)
13888 /* There are no parameters. */
13889 {
13890 #ifndef NO_IMPLICIT_EXTERN_C
13891 if (in_system_header && current_class_type == NULL
13892 && current_lang_name == lang_name_c)
13893 return NULL_TREE;
13894 else
13895 #endif
13896 return void_list_node;
13897 }
13898 /* Check for `(void)', too, which is a special case. */
13899 else if (token->keyword == RID_VOID
13900 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13901 == CPP_CLOSE_PAREN))
13902 {
13903 /* Consume the `void' token. */
13904 cp_lexer_consume_token (parser->lexer);
13905 /* There are no parameters. */
13906 return void_list_node;
13907 }
13908
13909 /* Parse the parameter-declaration-list. */
13910 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13911 /* If a parse error occurred while parsing the
13912 parameter-declaration-list, then the entire
13913 parameter-declaration-clause is erroneous. */
13914 if (is_error)
13915 return NULL;
13916
13917 /* Peek at the next token. */
13918 token = cp_lexer_peek_token (parser->lexer);
13919 /* If it's a `,', the clause should terminate with an ellipsis. */
13920 if (token->type == CPP_COMMA)
13921 {
13922 /* Consume the `,'. */
13923 cp_lexer_consume_token (parser->lexer);
13924 /* Expect an ellipsis. */
13925 ellipsis_p
13926 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13927 }
13928 /* It might also be `...' if the optional trailing `,' was
13929 omitted. */
13930 else if (token->type == CPP_ELLIPSIS)
13931 {
13932 /* Consume the `...' token. */
13933 cp_lexer_consume_token (parser->lexer);
13934 /* And remember that we saw it. */
13935 ellipsis_p = true;
13936 }
13937 else
13938 ellipsis_p = false;
13939
13940 /* Finish the parameter list. */
13941 if (!ellipsis_p)
13942 parameters = chainon (parameters, void_list_node);
13943
13944 return parameters;
13945 }
13946
13947 /* Parse a parameter-declaration-list.
13948
13949 parameter-declaration-list:
13950 parameter-declaration
13951 parameter-declaration-list , parameter-declaration
13952
13953 Returns a representation of the parameter-declaration-list, as for
13954 cp_parser_parameter_declaration_clause. However, the
13955 `void_list_node' is never appended to the list. Upon return,
13956 *IS_ERROR will be true iff an error occurred. */
13957
13958 static tree
13959 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13960 {
13961 tree parameters = NULL_TREE;
13962 tree *tail = &parameters;
13963 bool saved_in_unbraced_linkage_specification_p;
13964
13965 /* Assume all will go well. */
13966 *is_error = false;
13967 /* The special considerations that apply to a function within an
13968 unbraced linkage specifications do not apply to the parameters
13969 to the function. */
13970 saved_in_unbraced_linkage_specification_p
13971 = parser->in_unbraced_linkage_specification_p;
13972 parser->in_unbraced_linkage_specification_p = false;
13973
13974 /* Look for more parameters. */
13975 while (true)
13976 {
13977 cp_parameter_declarator *parameter;
13978 tree decl = error_mark_node;
13979 bool parenthesized_p;
13980 /* Parse the parameter. */
13981 parameter
13982 = cp_parser_parameter_declaration (parser,
13983 /*template_parm_p=*/false,
13984 &parenthesized_p);
13985
13986 /* We don't know yet if the enclosing context is deprecated, so wait
13987 and warn in grokparms if appropriate. */
13988 deprecated_state = DEPRECATED_SUPPRESS;
13989
13990 if (parameter)
13991 decl = grokdeclarator (parameter->declarator,
13992 &parameter->decl_specifiers,
13993 PARM,
13994 parameter->default_argument != NULL_TREE,
13995 &parameter->decl_specifiers.attributes);
13996
13997 deprecated_state = DEPRECATED_NORMAL;
13998
13999 /* If a parse error occurred parsing the parameter declaration,
14000 then the entire parameter-declaration-list is erroneous. */
14001 if (decl == error_mark_node)
14002 {
14003 *is_error = true;
14004 parameters = error_mark_node;
14005 break;
14006 }
14007
14008 if (parameter->decl_specifiers.attributes)
14009 cplus_decl_attributes (&decl,
14010 parameter->decl_specifiers.attributes,
14011 0);
14012 if (DECL_NAME (decl))
14013 decl = pushdecl (decl);
14014
14015 /* Add the new parameter to the list. */
14016 *tail = build_tree_list (parameter->default_argument, decl);
14017 tail = &TREE_CHAIN (*tail);
14018
14019 /* Peek at the next token. */
14020 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14021 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14022 /* These are for Objective-C++ */
14023 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14024 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14025 /* The parameter-declaration-list is complete. */
14026 break;
14027 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14028 {
14029 cp_token *token;
14030
14031 /* Peek at the next token. */
14032 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14033 /* If it's an ellipsis, then the list is complete. */
14034 if (token->type == CPP_ELLIPSIS)
14035 break;
14036 /* Otherwise, there must be more parameters. Consume the
14037 `,'. */
14038 cp_lexer_consume_token (parser->lexer);
14039 /* When parsing something like:
14040
14041 int i(float f, double d)
14042
14043 we can tell after seeing the declaration for "f" that we
14044 are not looking at an initialization of a variable "i",
14045 but rather at the declaration of a function "i".
14046
14047 Due to the fact that the parsing of template arguments
14048 (as specified to a template-id) requires backtracking we
14049 cannot use this technique when inside a template argument
14050 list. */
14051 if (!parser->in_template_argument_list_p
14052 && !parser->in_type_id_in_expr_p
14053 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14054 /* However, a parameter-declaration of the form
14055 "foat(f)" (which is a valid declaration of a
14056 parameter "f") can also be interpreted as an
14057 expression (the conversion of "f" to "float"). */
14058 && !parenthesized_p)
14059 cp_parser_commit_to_tentative_parse (parser);
14060 }
14061 else
14062 {
14063 cp_parser_error (parser, "expected %<,%> or %<...%>");
14064 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14065 cp_parser_skip_to_closing_parenthesis (parser,
14066 /*recovering=*/true,
14067 /*or_comma=*/false,
14068 /*consume_paren=*/false);
14069 break;
14070 }
14071 }
14072
14073 parser->in_unbraced_linkage_specification_p
14074 = saved_in_unbraced_linkage_specification_p;
14075
14076 return parameters;
14077 }
14078
14079 /* Parse a parameter declaration.
14080
14081 parameter-declaration:
14082 decl-specifier-seq ... [opt] declarator
14083 decl-specifier-seq declarator = assignment-expression
14084 decl-specifier-seq ... [opt] abstract-declarator [opt]
14085 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14086
14087 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14088 declares a template parameter. (In that case, a non-nested `>'
14089 token encountered during the parsing of the assignment-expression
14090 is not interpreted as a greater-than operator.)
14091
14092 Returns a representation of the parameter, or NULL if an error
14093 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14094 true iff the declarator is of the form "(p)". */
14095
14096 static cp_parameter_declarator *
14097 cp_parser_parameter_declaration (cp_parser *parser,
14098 bool template_parm_p,
14099 bool *parenthesized_p)
14100 {
14101 int declares_class_or_enum;
14102 bool greater_than_is_operator_p;
14103 cp_decl_specifier_seq decl_specifiers;
14104 cp_declarator *declarator;
14105 tree default_argument;
14106 cp_token *token = NULL, *declarator_token_start = NULL;
14107 const char *saved_message;
14108
14109 /* In a template parameter, `>' is not an operator.
14110
14111 [temp.param]
14112
14113 When parsing a default template-argument for a non-type
14114 template-parameter, the first non-nested `>' is taken as the end
14115 of the template parameter-list rather than a greater-than
14116 operator. */
14117 greater_than_is_operator_p = !template_parm_p;
14118
14119 /* Type definitions may not appear in parameter types. */
14120 saved_message = parser->type_definition_forbidden_message;
14121 parser->type_definition_forbidden_message
14122 = "types may not be defined in parameter types";
14123
14124 /* Parse the declaration-specifiers. */
14125 cp_parser_decl_specifier_seq (parser,
14126 CP_PARSER_FLAGS_NONE,
14127 &decl_specifiers,
14128 &declares_class_or_enum);
14129 /* If an error occurred, there's no reason to attempt to parse the
14130 rest of the declaration. */
14131 if (cp_parser_error_occurred (parser))
14132 {
14133 parser->type_definition_forbidden_message = saved_message;
14134 return NULL;
14135 }
14136
14137 /* Peek at the next token. */
14138 token = cp_lexer_peek_token (parser->lexer);
14139
14140 /* If the next token is a `)', `,', `=', `>', or `...', then there
14141 is no declarator. However, when variadic templates are enabled,
14142 there may be a declarator following `...'. */
14143 if (token->type == CPP_CLOSE_PAREN
14144 || token->type == CPP_COMMA
14145 || token->type == CPP_EQ
14146 || token->type == CPP_GREATER)
14147 {
14148 declarator = NULL;
14149 if (parenthesized_p)
14150 *parenthesized_p = false;
14151 }
14152 /* Otherwise, there should be a declarator. */
14153 else
14154 {
14155 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14156 parser->default_arg_ok_p = false;
14157
14158 /* After seeing a decl-specifier-seq, if the next token is not a
14159 "(", there is no possibility that the code is a valid
14160 expression. Therefore, if parsing tentatively, we commit at
14161 this point. */
14162 if (!parser->in_template_argument_list_p
14163 /* In an expression context, having seen:
14164
14165 (int((char ...
14166
14167 we cannot be sure whether we are looking at a
14168 function-type (taking a "char" as a parameter) or a cast
14169 of some object of type "char" to "int". */
14170 && !parser->in_type_id_in_expr_p
14171 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14172 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14173 cp_parser_commit_to_tentative_parse (parser);
14174 /* Parse the declarator. */
14175 declarator_token_start = token;
14176 declarator = cp_parser_declarator (parser,
14177 CP_PARSER_DECLARATOR_EITHER,
14178 /*ctor_dtor_or_conv_p=*/NULL,
14179 parenthesized_p,
14180 /*member_p=*/false);
14181 parser->default_arg_ok_p = saved_default_arg_ok_p;
14182 /* After the declarator, allow more attributes. */
14183 decl_specifiers.attributes
14184 = chainon (decl_specifiers.attributes,
14185 cp_parser_attributes_opt (parser));
14186 }
14187
14188 /* If the next token is an ellipsis, and we have not seen a
14189 declarator name, and the type of the declarator contains parameter
14190 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14191 a parameter pack expansion expression. Otherwise, leave the
14192 ellipsis for a C-style variadic function. */
14193 token = cp_lexer_peek_token (parser->lexer);
14194 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14195 {
14196 tree type = decl_specifiers.type;
14197
14198 if (type && DECL_P (type))
14199 type = TREE_TYPE (type);
14200
14201 if (type
14202 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14203 && declarator_can_be_parameter_pack (declarator)
14204 && (!declarator || !declarator->parameter_pack_p)
14205 && uses_parameter_packs (type))
14206 {
14207 /* Consume the `...'. */
14208 cp_lexer_consume_token (parser->lexer);
14209 maybe_warn_variadic_templates ();
14210
14211 /* Build a pack expansion type */
14212 if (declarator)
14213 declarator->parameter_pack_p = true;
14214 else
14215 decl_specifiers.type = make_pack_expansion (type);
14216 }
14217 }
14218
14219 /* The restriction on defining new types applies only to the type
14220 of the parameter, not to the default argument. */
14221 parser->type_definition_forbidden_message = saved_message;
14222
14223 /* If the next token is `=', then process a default argument. */
14224 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14225 {
14226 /* Consume the `='. */
14227 cp_lexer_consume_token (parser->lexer);
14228
14229 /* If we are defining a class, then the tokens that make up the
14230 default argument must be saved and processed later. */
14231 if (!template_parm_p && at_class_scope_p ()
14232 && TYPE_BEING_DEFINED (current_class_type))
14233 {
14234 unsigned depth = 0;
14235 int maybe_template_id = 0;
14236 cp_token *first_token;
14237 cp_token *token;
14238
14239 /* Add tokens until we have processed the entire default
14240 argument. We add the range [first_token, token). */
14241 first_token = cp_lexer_peek_token (parser->lexer);
14242 while (true)
14243 {
14244 bool done = false;
14245
14246 /* Peek at the next token. */
14247 token = cp_lexer_peek_token (parser->lexer);
14248 /* What we do depends on what token we have. */
14249 switch (token->type)
14250 {
14251 /* In valid code, a default argument must be
14252 immediately followed by a `,' `)', or `...'. */
14253 case CPP_COMMA:
14254 if (depth == 0 && maybe_template_id)
14255 {
14256 /* If we've seen a '<', we might be in a
14257 template-argument-list. Until Core issue 325 is
14258 resolved, we don't know how this situation ought
14259 to be handled, so try to DTRT. We check whether
14260 what comes after the comma is a valid parameter
14261 declaration list. If it is, then the comma ends
14262 the default argument; otherwise the default
14263 argument continues. */
14264 bool error = false;
14265
14266 /* Set ITALP so cp_parser_parameter_declaration_list
14267 doesn't decide to commit to this parse. */
14268 bool saved_italp = parser->in_template_argument_list_p;
14269 parser->in_template_argument_list_p = true;
14270
14271 cp_parser_parse_tentatively (parser);
14272 cp_lexer_consume_token (parser->lexer);
14273 cp_parser_parameter_declaration_list (parser, &error);
14274 if (!cp_parser_error_occurred (parser) && !error)
14275 done = true;
14276 cp_parser_abort_tentative_parse (parser);
14277
14278 parser->in_template_argument_list_p = saved_italp;
14279 break;
14280 }
14281 case CPP_CLOSE_PAREN:
14282 case CPP_ELLIPSIS:
14283 /* If we run into a non-nested `;', `}', or `]',
14284 then the code is invalid -- but the default
14285 argument is certainly over. */
14286 case CPP_SEMICOLON:
14287 case CPP_CLOSE_BRACE:
14288 case CPP_CLOSE_SQUARE:
14289 if (depth == 0)
14290 done = true;
14291 /* Update DEPTH, if necessary. */
14292 else if (token->type == CPP_CLOSE_PAREN
14293 || token->type == CPP_CLOSE_BRACE
14294 || token->type == CPP_CLOSE_SQUARE)
14295 --depth;
14296 break;
14297
14298 case CPP_OPEN_PAREN:
14299 case CPP_OPEN_SQUARE:
14300 case CPP_OPEN_BRACE:
14301 ++depth;
14302 break;
14303
14304 case CPP_LESS:
14305 if (depth == 0)
14306 /* This might be the comparison operator, or it might
14307 start a template argument list. */
14308 ++maybe_template_id;
14309 break;
14310
14311 case CPP_RSHIFT:
14312 if (cxx_dialect == cxx98)
14313 break;
14314 /* Fall through for C++0x, which treats the `>>'
14315 operator like two `>' tokens in certain
14316 cases. */
14317
14318 case CPP_GREATER:
14319 if (depth == 0)
14320 {
14321 /* This might be an operator, or it might close a
14322 template argument list. But if a previous '<'
14323 started a template argument list, this will have
14324 closed it, so we can't be in one anymore. */
14325 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14326 if (maybe_template_id < 0)
14327 maybe_template_id = 0;
14328 }
14329 break;
14330
14331 /* If we run out of tokens, issue an error message. */
14332 case CPP_EOF:
14333 case CPP_PRAGMA_EOL:
14334 error ("%Hfile ends in default argument", &token->location);
14335 done = true;
14336 break;
14337
14338 case CPP_NAME:
14339 case CPP_SCOPE:
14340 /* In these cases, we should look for template-ids.
14341 For example, if the default argument is
14342 `X<int, double>()', we need to do name lookup to
14343 figure out whether or not `X' is a template; if
14344 so, the `,' does not end the default argument.
14345
14346 That is not yet done. */
14347 break;
14348
14349 default:
14350 break;
14351 }
14352
14353 /* If we've reached the end, stop. */
14354 if (done)
14355 break;
14356
14357 /* Add the token to the token block. */
14358 token = cp_lexer_consume_token (parser->lexer);
14359 }
14360
14361 /* Create a DEFAULT_ARG to represent the unparsed default
14362 argument. */
14363 default_argument = make_node (DEFAULT_ARG);
14364 DEFARG_TOKENS (default_argument)
14365 = cp_token_cache_new (first_token, token);
14366 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14367 }
14368 /* Outside of a class definition, we can just parse the
14369 assignment-expression. */
14370 else
14371 {
14372 token = cp_lexer_peek_token (parser->lexer);
14373 default_argument
14374 = cp_parser_default_argument (parser, template_parm_p);
14375 }
14376
14377 if (!parser->default_arg_ok_p)
14378 {
14379 if (flag_permissive)
14380 warning (0, "deprecated use of default argument for parameter of non-function");
14381 else
14382 {
14383 error ("%Hdefault arguments are only "
14384 "permitted for function parameters",
14385 &token->location);
14386 default_argument = NULL_TREE;
14387 }
14388 }
14389 else if ((declarator && declarator->parameter_pack_p)
14390 || (decl_specifiers.type
14391 && PACK_EXPANSION_P (decl_specifiers.type)))
14392 {
14393 const char* kind = template_parm_p? "template " : "";
14394
14395 /* Find the name of the parameter pack. */
14396 cp_declarator *id_declarator = declarator;
14397 while (id_declarator && id_declarator->kind != cdk_id)
14398 id_declarator = id_declarator->declarator;
14399
14400 if (id_declarator && id_declarator->kind == cdk_id)
14401 error ("%H%sparameter pack %qD cannot have a default argument",
14402 &declarator_token_start->location,
14403 kind, id_declarator->u.id.unqualified_name);
14404 else
14405 error ("%H%sparameter pack cannot have a default argument",
14406 &declarator_token_start->location, kind);
14407
14408 default_argument = NULL_TREE;
14409 }
14410 }
14411 else
14412 default_argument = NULL_TREE;
14413
14414 return make_parameter_declarator (&decl_specifiers,
14415 declarator,
14416 default_argument);
14417 }
14418
14419 /* Parse a default argument and return it.
14420
14421 TEMPLATE_PARM_P is true if this is a default argument for a
14422 non-type template parameter. */
14423 static tree
14424 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14425 {
14426 tree default_argument = NULL_TREE;
14427 bool saved_greater_than_is_operator_p;
14428 bool saved_local_variables_forbidden_p;
14429
14430 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14431 set correctly. */
14432 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14433 parser->greater_than_is_operator_p = !template_parm_p;
14434 /* Local variable names (and the `this' keyword) may not
14435 appear in a default argument. */
14436 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14437 parser->local_variables_forbidden_p = true;
14438 /* The default argument expression may cause implicitly
14439 defined member functions to be synthesized, which will
14440 result in garbage collection. We must treat this
14441 situation as if we were within the body of function so as
14442 to avoid collecting live data on the stack. */
14443 ++function_depth;
14444 /* Parse the assignment-expression. */
14445 if (template_parm_p)
14446 push_deferring_access_checks (dk_no_deferred);
14447 default_argument
14448 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14449 if (template_parm_p)
14450 pop_deferring_access_checks ();
14451 /* Restore saved state. */
14452 --function_depth;
14453 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14454 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14455
14456 return default_argument;
14457 }
14458
14459 /* Parse a function-body.
14460
14461 function-body:
14462 compound_statement */
14463
14464 static void
14465 cp_parser_function_body (cp_parser *parser)
14466 {
14467 cp_parser_compound_statement (parser, NULL, false);
14468 }
14469
14470 /* Parse a ctor-initializer-opt followed by a function-body. Return
14471 true if a ctor-initializer was present. */
14472
14473 static bool
14474 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14475 {
14476 tree body;
14477 bool ctor_initializer_p;
14478
14479 /* Begin the function body. */
14480 body = begin_function_body ();
14481 /* Parse the optional ctor-initializer. */
14482 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14483 /* Parse the function-body. */
14484 cp_parser_function_body (parser);
14485 /* Finish the function body. */
14486 finish_function_body (body);
14487
14488 return ctor_initializer_p;
14489 }
14490
14491 /* Parse an initializer.
14492
14493 initializer:
14494 = initializer-clause
14495 ( expression-list )
14496
14497 Returns an expression representing the initializer. If no
14498 initializer is present, NULL_TREE is returned.
14499
14500 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14501 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14502 set to TRUE if there is no initializer present. If there is an
14503 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14504 is set to true; otherwise it is set to false. */
14505
14506 static tree
14507 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14508 bool* non_constant_p)
14509 {
14510 cp_token *token;
14511 tree init;
14512
14513 /* Peek at the next token. */
14514 token = cp_lexer_peek_token (parser->lexer);
14515
14516 /* Let our caller know whether or not this initializer was
14517 parenthesized. */
14518 *is_direct_init = (token->type != CPP_EQ);
14519 /* Assume that the initializer is constant. */
14520 *non_constant_p = false;
14521
14522 if (token->type == CPP_EQ)
14523 {
14524 /* Consume the `='. */
14525 cp_lexer_consume_token (parser->lexer);
14526 /* Parse the initializer-clause. */
14527 init = cp_parser_initializer_clause (parser, non_constant_p);
14528 }
14529 else if (token->type == CPP_OPEN_PAREN)
14530 init = cp_parser_parenthesized_expression_list (parser, false,
14531 /*cast_p=*/false,
14532 /*allow_expansion_p=*/true,
14533 non_constant_p);
14534 else if (token->type == CPP_OPEN_BRACE)
14535 {
14536 maybe_warn_cpp0x ("extended initializer lists");
14537 init = cp_parser_braced_list (parser, non_constant_p);
14538 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14539 }
14540 else
14541 {
14542 /* Anything else is an error. */
14543 cp_parser_error (parser, "expected initializer");
14544 init = error_mark_node;
14545 }
14546
14547 return init;
14548 }
14549
14550 /* Parse an initializer-clause.
14551
14552 initializer-clause:
14553 assignment-expression
14554 braced-init-list
14555
14556 Returns an expression representing the initializer.
14557
14558 If the `assignment-expression' production is used the value
14559 returned is simply a representation for the expression.
14560
14561 Otherwise, calls cp_parser_braced_list. */
14562
14563 static tree
14564 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14565 {
14566 tree initializer;
14567
14568 /* Assume the expression is constant. */
14569 *non_constant_p = false;
14570
14571 /* If it is not a `{', then we are looking at an
14572 assignment-expression. */
14573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14574 {
14575 initializer
14576 = cp_parser_constant_expression (parser,
14577 /*allow_non_constant_p=*/true,
14578 non_constant_p);
14579 if (!*non_constant_p)
14580 initializer = fold_non_dependent_expr (initializer);
14581 }
14582 else
14583 initializer = cp_parser_braced_list (parser, non_constant_p);
14584
14585 return initializer;
14586 }
14587
14588 /* Parse a brace-enclosed initializer list.
14589
14590 braced-init-list:
14591 { initializer-list , [opt] }
14592 { }
14593
14594 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14595 the elements of the initializer-list (or NULL, if the last
14596 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14597 NULL_TREE. There is no way to detect whether or not the optional
14598 trailing `,' was provided. NON_CONSTANT_P is as for
14599 cp_parser_initializer. */
14600
14601 static tree
14602 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14603 {
14604 tree initializer;
14605
14606 /* Consume the `{' token. */
14607 cp_lexer_consume_token (parser->lexer);
14608 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14609 initializer = make_node (CONSTRUCTOR);
14610 /* If it's not a `}', then there is a non-trivial initializer. */
14611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14612 {
14613 /* Parse the initializer list. */
14614 CONSTRUCTOR_ELTS (initializer)
14615 = cp_parser_initializer_list (parser, non_constant_p);
14616 /* A trailing `,' token is allowed. */
14617 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14618 cp_lexer_consume_token (parser->lexer);
14619 }
14620 /* Now, there should be a trailing `}'. */
14621 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14622 TREE_TYPE (initializer) = init_list_type_node;
14623 return initializer;
14624 }
14625
14626 /* Parse an initializer-list.
14627
14628 initializer-list:
14629 initializer-clause ... [opt]
14630 initializer-list , initializer-clause ... [opt]
14631
14632 GNU Extension:
14633
14634 initializer-list:
14635 identifier : initializer-clause
14636 initializer-list, identifier : initializer-clause
14637
14638 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14639 for the initializer. If the INDEX of the elt is non-NULL, it is the
14640 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14641 as for cp_parser_initializer. */
14642
14643 static VEC(constructor_elt,gc) *
14644 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14645 {
14646 VEC(constructor_elt,gc) *v = NULL;
14647
14648 /* Assume all of the expressions are constant. */
14649 *non_constant_p = false;
14650
14651 /* Parse the rest of the list. */
14652 while (true)
14653 {
14654 cp_token *token;
14655 tree identifier;
14656 tree initializer;
14657 bool clause_non_constant_p;
14658
14659 /* If the next token is an identifier and the following one is a
14660 colon, we are looking at the GNU designated-initializer
14661 syntax. */
14662 if (cp_parser_allow_gnu_extensions_p (parser)
14663 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14664 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14665 {
14666 /* Warn the user that they are using an extension. */
14667 pedwarn (input_location, OPT_pedantic,
14668 "ISO C++ does not allow designated initializers");
14669 /* Consume the identifier. */
14670 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14671 /* Consume the `:'. */
14672 cp_lexer_consume_token (parser->lexer);
14673 }
14674 else
14675 identifier = NULL_TREE;
14676
14677 /* Parse the initializer. */
14678 initializer = cp_parser_initializer_clause (parser,
14679 &clause_non_constant_p);
14680 /* If any clause is non-constant, so is the entire initializer. */
14681 if (clause_non_constant_p)
14682 *non_constant_p = true;
14683
14684 /* If we have an ellipsis, this is an initializer pack
14685 expansion. */
14686 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14687 {
14688 /* Consume the `...'. */
14689 cp_lexer_consume_token (parser->lexer);
14690
14691 /* Turn the initializer into an initializer expansion. */
14692 initializer = make_pack_expansion (initializer);
14693 }
14694
14695 /* Add it to the vector. */
14696 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14697
14698 /* If the next token is not a comma, we have reached the end of
14699 the list. */
14700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14701 break;
14702
14703 /* Peek at the next token. */
14704 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14705 /* If the next token is a `}', then we're still done. An
14706 initializer-clause can have a trailing `,' after the
14707 initializer-list and before the closing `}'. */
14708 if (token->type == CPP_CLOSE_BRACE)
14709 break;
14710
14711 /* Consume the `,' token. */
14712 cp_lexer_consume_token (parser->lexer);
14713 }
14714
14715 return v;
14716 }
14717
14718 /* Classes [gram.class] */
14719
14720 /* Parse a class-name.
14721
14722 class-name:
14723 identifier
14724 template-id
14725
14726 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14727 to indicate that names looked up in dependent types should be
14728 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14729 keyword has been used to indicate that the name that appears next
14730 is a template. TAG_TYPE indicates the explicit tag given before
14731 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14732 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14733 is the class being defined in a class-head.
14734
14735 Returns the TYPE_DECL representing the class. */
14736
14737 static tree
14738 cp_parser_class_name (cp_parser *parser,
14739 bool typename_keyword_p,
14740 bool template_keyword_p,
14741 enum tag_types tag_type,
14742 bool check_dependency_p,
14743 bool class_head_p,
14744 bool is_declaration)
14745 {
14746 tree decl;
14747 tree scope;
14748 bool typename_p;
14749 cp_token *token;
14750 tree identifier = NULL_TREE;
14751
14752 /* All class-names start with an identifier. */
14753 token = cp_lexer_peek_token (parser->lexer);
14754 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14755 {
14756 cp_parser_error (parser, "expected class-name");
14757 return error_mark_node;
14758 }
14759
14760 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14761 to a template-id, so we save it here. */
14762 scope = parser->scope;
14763 if (scope == error_mark_node)
14764 return error_mark_node;
14765
14766 /* Any name names a type if we're following the `typename' keyword
14767 in a qualified name where the enclosing scope is type-dependent. */
14768 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14769 && dependent_type_p (scope));
14770 /* Handle the common case (an identifier, but not a template-id)
14771 efficiently. */
14772 if (token->type == CPP_NAME
14773 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14774 {
14775 cp_token *identifier_token;
14776 bool ambiguous_p;
14777
14778 /* Look for the identifier. */
14779 identifier_token = cp_lexer_peek_token (parser->lexer);
14780 ambiguous_p = identifier_token->ambiguous_p;
14781 identifier = cp_parser_identifier (parser);
14782 /* If the next token isn't an identifier, we are certainly not
14783 looking at a class-name. */
14784 if (identifier == error_mark_node)
14785 decl = error_mark_node;
14786 /* If we know this is a type-name, there's no need to look it
14787 up. */
14788 else if (typename_p)
14789 decl = identifier;
14790 else
14791 {
14792 tree ambiguous_decls;
14793 /* If we already know that this lookup is ambiguous, then
14794 we've already issued an error message; there's no reason
14795 to check again. */
14796 if (ambiguous_p)
14797 {
14798 cp_parser_simulate_error (parser);
14799 return error_mark_node;
14800 }
14801 /* If the next token is a `::', then the name must be a type
14802 name.
14803
14804 [basic.lookup.qual]
14805
14806 During the lookup for a name preceding the :: scope
14807 resolution operator, object, function, and enumerator
14808 names are ignored. */
14809 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14810 tag_type = typename_type;
14811 /* Look up the name. */
14812 decl = cp_parser_lookup_name (parser, identifier,
14813 tag_type,
14814 /*is_template=*/false,
14815 /*is_namespace=*/false,
14816 check_dependency_p,
14817 &ambiguous_decls,
14818 identifier_token->location);
14819 if (ambiguous_decls)
14820 {
14821 error ("%Hreference to %qD is ambiguous",
14822 &identifier_token->location, identifier);
14823 print_candidates (ambiguous_decls);
14824 if (cp_parser_parsing_tentatively (parser))
14825 {
14826 identifier_token->ambiguous_p = true;
14827 cp_parser_simulate_error (parser);
14828 }
14829 return error_mark_node;
14830 }
14831 }
14832 }
14833 else
14834 {
14835 /* Try a template-id. */
14836 decl = cp_parser_template_id (parser, template_keyword_p,
14837 check_dependency_p,
14838 is_declaration);
14839 if (decl == error_mark_node)
14840 return error_mark_node;
14841 }
14842
14843 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14844
14845 /* If this is a typename, create a TYPENAME_TYPE. */
14846 if (typename_p && decl != error_mark_node)
14847 {
14848 decl = make_typename_type (scope, decl, typename_type,
14849 /*complain=*/tf_error);
14850 if (decl != error_mark_node)
14851 decl = TYPE_NAME (decl);
14852 }
14853
14854 /* Check to see that it is really the name of a class. */
14855 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14856 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14857 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14858 /* Situations like this:
14859
14860 template <typename T> struct A {
14861 typename T::template X<int>::I i;
14862 };
14863
14864 are problematic. Is `T::template X<int>' a class-name? The
14865 standard does not seem to be definitive, but there is no other
14866 valid interpretation of the following `::'. Therefore, those
14867 names are considered class-names. */
14868 {
14869 decl = make_typename_type (scope, decl, tag_type, tf_error);
14870 if (decl != error_mark_node)
14871 decl = TYPE_NAME (decl);
14872 }
14873 else if (TREE_CODE (decl) != TYPE_DECL
14874 || TREE_TYPE (decl) == error_mark_node
14875 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14876 decl = error_mark_node;
14877
14878 if (decl == error_mark_node)
14879 cp_parser_error (parser, "expected class-name");
14880 else if (identifier && !parser->scope)
14881 maybe_note_name_used_in_class (identifier, decl);
14882
14883 return decl;
14884 }
14885
14886 /* Parse a class-specifier.
14887
14888 class-specifier:
14889 class-head { member-specification [opt] }
14890
14891 Returns the TREE_TYPE representing the class. */
14892
14893 static tree
14894 cp_parser_class_specifier (cp_parser* parser)
14895 {
14896 cp_token *token;
14897 tree type;
14898 tree attributes = NULL_TREE;
14899 int has_trailing_semicolon;
14900 bool nested_name_specifier_p;
14901 unsigned saved_num_template_parameter_lists;
14902 bool saved_in_function_body;
14903 bool saved_in_unbraced_linkage_specification_p;
14904 tree old_scope = NULL_TREE;
14905 tree scope = NULL_TREE;
14906 tree bases;
14907
14908 push_deferring_access_checks (dk_no_deferred);
14909
14910 /* Parse the class-head. */
14911 type = cp_parser_class_head (parser,
14912 &nested_name_specifier_p,
14913 &attributes,
14914 &bases);
14915 /* If the class-head was a semantic disaster, skip the entire body
14916 of the class. */
14917 if (!type)
14918 {
14919 cp_parser_skip_to_end_of_block_or_statement (parser);
14920 pop_deferring_access_checks ();
14921 return error_mark_node;
14922 }
14923
14924 /* Look for the `{'. */
14925 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14926 {
14927 pop_deferring_access_checks ();
14928 return error_mark_node;
14929 }
14930
14931 /* Process the base classes. If they're invalid, skip the
14932 entire class body. */
14933 if (!xref_basetypes (type, bases))
14934 {
14935 /* Consuming the closing brace yields better error messages
14936 later on. */
14937 if (cp_parser_skip_to_closing_brace (parser))
14938 cp_lexer_consume_token (parser->lexer);
14939 pop_deferring_access_checks ();
14940 return error_mark_node;
14941 }
14942
14943 /* Issue an error message if type-definitions are forbidden here. */
14944 cp_parser_check_type_definition (parser);
14945 /* Remember that we are defining one more class. */
14946 ++parser->num_classes_being_defined;
14947 /* Inside the class, surrounding template-parameter-lists do not
14948 apply. */
14949 saved_num_template_parameter_lists
14950 = parser->num_template_parameter_lists;
14951 parser->num_template_parameter_lists = 0;
14952 /* We are not in a function body. */
14953 saved_in_function_body = parser->in_function_body;
14954 parser->in_function_body = false;
14955 /* We are not immediately inside an extern "lang" block. */
14956 saved_in_unbraced_linkage_specification_p
14957 = parser->in_unbraced_linkage_specification_p;
14958 parser->in_unbraced_linkage_specification_p = false;
14959
14960 /* Start the class. */
14961 if (nested_name_specifier_p)
14962 {
14963 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14964 old_scope = push_inner_scope (scope);
14965 }
14966 type = begin_class_definition (type, attributes);
14967
14968 if (type == error_mark_node)
14969 /* If the type is erroneous, skip the entire body of the class. */
14970 cp_parser_skip_to_closing_brace (parser);
14971 else
14972 /* Parse the member-specification. */
14973 cp_parser_member_specification_opt (parser);
14974
14975 /* Look for the trailing `}'. */
14976 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14977 /* We get better error messages by noticing a common problem: a
14978 missing trailing `;'. */
14979 token = cp_lexer_peek_token (parser->lexer);
14980 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14981 /* Look for trailing attributes to apply to this class. */
14982 if (cp_parser_allow_gnu_extensions_p (parser))
14983 attributes = cp_parser_attributes_opt (parser);
14984 if (type != error_mark_node)
14985 type = finish_struct (type, attributes);
14986 if (nested_name_specifier_p)
14987 pop_inner_scope (old_scope, scope);
14988 /* If this class is not itself within the scope of another class,
14989 then we need to parse the bodies of all of the queued function
14990 definitions. Note that the queued functions defined in a class
14991 are not always processed immediately following the
14992 class-specifier for that class. Consider:
14993
14994 struct A {
14995 struct B { void f() { sizeof (A); } };
14996 };
14997
14998 If `f' were processed before the processing of `A' were
14999 completed, there would be no way to compute the size of `A'.
15000 Note that the nesting we are interested in here is lexical --
15001 not the semantic nesting given by TYPE_CONTEXT. In particular,
15002 for:
15003
15004 struct A { struct B; };
15005 struct A::B { void f() { } };
15006
15007 there is no need to delay the parsing of `A::B::f'. */
15008 if (--parser->num_classes_being_defined == 0)
15009 {
15010 tree queue_entry;
15011 tree fn;
15012 tree class_type = NULL_TREE;
15013 tree pushed_scope = NULL_TREE;
15014
15015 /* In a first pass, parse default arguments to the functions.
15016 Then, in a second pass, parse the bodies of the functions.
15017 This two-phased approach handles cases like:
15018
15019 struct S {
15020 void f() { g(); }
15021 void g(int i = 3);
15022 };
15023
15024 */
15025 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15026 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15027 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15028 TREE_PURPOSE (parser->unparsed_functions_queues)
15029 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15030 {
15031 fn = TREE_VALUE (queue_entry);
15032 /* If there are default arguments that have not yet been processed,
15033 take care of them now. */
15034 if (class_type != TREE_PURPOSE (queue_entry))
15035 {
15036 if (pushed_scope)
15037 pop_scope (pushed_scope);
15038 class_type = TREE_PURPOSE (queue_entry);
15039 pushed_scope = push_scope (class_type);
15040 }
15041 /* Make sure that any template parameters are in scope. */
15042 maybe_begin_member_template_processing (fn);
15043 /* Parse the default argument expressions. */
15044 cp_parser_late_parsing_default_args (parser, fn);
15045 /* Remove any template parameters from the symbol table. */
15046 maybe_end_member_template_processing ();
15047 }
15048 if (pushed_scope)
15049 pop_scope (pushed_scope);
15050 /* Now parse the body of the functions. */
15051 for (TREE_VALUE (parser->unparsed_functions_queues)
15052 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15053 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15054 TREE_VALUE (parser->unparsed_functions_queues)
15055 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15056 {
15057 /* Figure out which function we need to process. */
15058 fn = TREE_VALUE (queue_entry);
15059 /* Parse the function. */
15060 cp_parser_late_parsing_for_member (parser, fn);
15061 }
15062 }
15063
15064 /* Put back any saved access checks. */
15065 pop_deferring_access_checks ();
15066
15067 /* Restore saved state. */
15068 parser->in_function_body = saved_in_function_body;
15069 parser->num_template_parameter_lists
15070 = saved_num_template_parameter_lists;
15071 parser->in_unbraced_linkage_specification_p
15072 = saved_in_unbraced_linkage_specification_p;
15073
15074 return type;
15075 }
15076
15077 /* Parse a class-head.
15078
15079 class-head:
15080 class-key identifier [opt] base-clause [opt]
15081 class-key nested-name-specifier identifier base-clause [opt]
15082 class-key nested-name-specifier [opt] template-id
15083 base-clause [opt]
15084
15085 GNU Extensions:
15086 class-key attributes identifier [opt] base-clause [opt]
15087 class-key attributes nested-name-specifier identifier base-clause [opt]
15088 class-key attributes nested-name-specifier [opt] template-id
15089 base-clause [opt]
15090
15091 Upon return BASES is initialized to the list of base classes (or
15092 NULL, if there are none) in the same form returned by
15093 cp_parser_base_clause.
15094
15095 Returns the TYPE of the indicated class. Sets
15096 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15097 involving a nested-name-specifier was used, and FALSE otherwise.
15098
15099 Returns error_mark_node if this is not a class-head.
15100
15101 Returns NULL_TREE if the class-head is syntactically valid, but
15102 semantically invalid in a way that means we should skip the entire
15103 body of the class. */
15104
15105 static tree
15106 cp_parser_class_head (cp_parser* parser,
15107 bool* nested_name_specifier_p,
15108 tree *attributes_p,
15109 tree *bases)
15110 {
15111 tree nested_name_specifier;
15112 enum tag_types class_key;
15113 tree id = NULL_TREE;
15114 tree type = NULL_TREE;
15115 tree attributes;
15116 bool template_id_p = false;
15117 bool qualified_p = false;
15118 bool invalid_nested_name_p = false;
15119 bool invalid_explicit_specialization_p = false;
15120 tree pushed_scope = NULL_TREE;
15121 unsigned num_templates;
15122 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15123 /* Assume no nested-name-specifier will be present. */
15124 *nested_name_specifier_p = false;
15125 /* Assume no template parameter lists will be used in defining the
15126 type. */
15127 num_templates = 0;
15128
15129 *bases = NULL_TREE;
15130
15131 /* Look for the class-key. */
15132 class_key = cp_parser_class_key (parser);
15133 if (class_key == none_type)
15134 return error_mark_node;
15135
15136 /* Parse the attributes. */
15137 attributes = cp_parser_attributes_opt (parser);
15138
15139 /* If the next token is `::', that is invalid -- but sometimes
15140 people do try to write:
15141
15142 struct ::S {};
15143
15144 Handle this gracefully by accepting the extra qualifier, and then
15145 issuing an error about it later if this really is a
15146 class-head. If it turns out just to be an elaborated type
15147 specifier, remain silent. */
15148 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15149 qualified_p = true;
15150
15151 push_deferring_access_checks (dk_no_check);
15152
15153 /* Determine the name of the class. Begin by looking for an
15154 optional nested-name-specifier. */
15155 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15156 nested_name_specifier
15157 = cp_parser_nested_name_specifier_opt (parser,
15158 /*typename_keyword_p=*/false,
15159 /*check_dependency_p=*/false,
15160 /*type_p=*/false,
15161 /*is_declaration=*/false);
15162 /* If there was a nested-name-specifier, then there *must* be an
15163 identifier. */
15164 if (nested_name_specifier)
15165 {
15166 type_start_token = cp_lexer_peek_token (parser->lexer);
15167 /* Although the grammar says `identifier', it really means
15168 `class-name' or `template-name'. You are only allowed to
15169 define a class that has already been declared with this
15170 syntax.
15171
15172 The proposed resolution for Core Issue 180 says that wherever
15173 you see `class T::X' you should treat `X' as a type-name.
15174
15175 It is OK to define an inaccessible class; for example:
15176
15177 class A { class B; };
15178 class A::B {};
15179
15180 We do not know if we will see a class-name, or a
15181 template-name. We look for a class-name first, in case the
15182 class-name is a template-id; if we looked for the
15183 template-name first we would stop after the template-name. */
15184 cp_parser_parse_tentatively (parser);
15185 type = cp_parser_class_name (parser,
15186 /*typename_keyword_p=*/false,
15187 /*template_keyword_p=*/false,
15188 class_type,
15189 /*check_dependency_p=*/false,
15190 /*class_head_p=*/true,
15191 /*is_declaration=*/false);
15192 /* If that didn't work, ignore the nested-name-specifier. */
15193 if (!cp_parser_parse_definitely (parser))
15194 {
15195 invalid_nested_name_p = true;
15196 type_start_token = cp_lexer_peek_token (parser->lexer);
15197 id = cp_parser_identifier (parser);
15198 if (id == error_mark_node)
15199 id = NULL_TREE;
15200 }
15201 /* If we could not find a corresponding TYPE, treat this
15202 declaration like an unqualified declaration. */
15203 if (type == error_mark_node)
15204 nested_name_specifier = NULL_TREE;
15205 /* Otherwise, count the number of templates used in TYPE and its
15206 containing scopes. */
15207 else
15208 {
15209 tree scope;
15210
15211 for (scope = TREE_TYPE (type);
15212 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15213 scope = (TYPE_P (scope)
15214 ? TYPE_CONTEXT (scope)
15215 : DECL_CONTEXT (scope)))
15216 if (TYPE_P (scope)
15217 && CLASS_TYPE_P (scope)
15218 && CLASSTYPE_TEMPLATE_INFO (scope)
15219 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15220 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15221 ++num_templates;
15222 }
15223 }
15224 /* Otherwise, the identifier is optional. */
15225 else
15226 {
15227 /* We don't know whether what comes next is a template-id,
15228 an identifier, or nothing at all. */
15229 cp_parser_parse_tentatively (parser);
15230 /* Check for a template-id. */
15231 type_start_token = cp_lexer_peek_token (parser->lexer);
15232 id = cp_parser_template_id (parser,
15233 /*template_keyword_p=*/false,
15234 /*check_dependency_p=*/true,
15235 /*is_declaration=*/true);
15236 /* If that didn't work, it could still be an identifier. */
15237 if (!cp_parser_parse_definitely (parser))
15238 {
15239 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15240 {
15241 type_start_token = cp_lexer_peek_token (parser->lexer);
15242 id = cp_parser_identifier (parser);
15243 }
15244 else
15245 id = NULL_TREE;
15246 }
15247 else
15248 {
15249 template_id_p = true;
15250 ++num_templates;
15251 }
15252 }
15253
15254 pop_deferring_access_checks ();
15255
15256 if (id)
15257 cp_parser_check_for_invalid_template_id (parser, id,
15258 type_start_token->location);
15259
15260 /* If it's not a `:' or a `{' then we can't really be looking at a
15261 class-head, since a class-head only appears as part of a
15262 class-specifier. We have to detect this situation before calling
15263 xref_tag, since that has irreversible side-effects. */
15264 if (!cp_parser_next_token_starts_class_definition_p (parser))
15265 {
15266 cp_parser_error (parser, "expected %<{%> or %<:%>");
15267 return error_mark_node;
15268 }
15269
15270 /* At this point, we're going ahead with the class-specifier, even
15271 if some other problem occurs. */
15272 cp_parser_commit_to_tentative_parse (parser);
15273 /* Issue the error about the overly-qualified name now. */
15274 if (qualified_p)
15275 {
15276 cp_parser_error (parser,
15277 "global qualification of class name is invalid");
15278 return error_mark_node;
15279 }
15280 else if (invalid_nested_name_p)
15281 {
15282 cp_parser_error (parser,
15283 "qualified name does not name a class");
15284 return error_mark_node;
15285 }
15286 else if (nested_name_specifier)
15287 {
15288 tree scope;
15289
15290 /* Reject typedef-names in class heads. */
15291 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15292 {
15293 error ("%Hinvalid class name in declaration of %qD",
15294 &type_start_token->location, type);
15295 type = NULL_TREE;
15296 goto done;
15297 }
15298
15299 /* Figure out in what scope the declaration is being placed. */
15300 scope = current_scope ();
15301 /* If that scope does not contain the scope in which the
15302 class was originally declared, the program is invalid. */
15303 if (scope && !is_ancestor (scope, nested_name_specifier))
15304 {
15305 if (at_namespace_scope_p ())
15306 error ("%Hdeclaration of %qD in namespace %qD which does not "
15307 "enclose %qD",
15308 &type_start_token->location,
15309 type, scope, nested_name_specifier);
15310 else
15311 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15312 &type_start_token->location,
15313 type, scope, nested_name_specifier);
15314 type = NULL_TREE;
15315 goto done;
15316 }
15317 /* [dcl.meaning]
15318
15319 A declarator-id shall not be qualified except for the
15320 definition of a ... nested class outside of its class
15321 ... [or] the definition or explicit instantiation of a
15322 class member of a namespace outside of its namespace. */
15323 if (scope == nested_name_specifier)
15324 {
15325 permerror (input_location, "%Hextra qualification not allowed",
15326 &nested_name_specifier_token_start->location);
15327 nested_name_specifier = NULL_TREE;
15328 num_templates = 0;
15329 }
15330 }
15331 /* An explicit-specialization must be preceded by "template <>". If
15332 it is not, try to recover gracefully. */
15333 if (at_namespace_scope_p ()
15334 && parser->num_template_parameter_lists == 0
15335 && template_id_p)
15336 {
15337 error ("%Han explicit specialization must be preceded by %<template <>%>",
15338 &type_start_token->location);
15339 invalid_explicit_specialization_p = true;
15340 /* Take the same action that would have been taken by
15341 cp_parser_explicit_specialization. */
15342 ++parser->num_template_parameter_lists;
15343 begin_specialization ();
15344 }
15345 /* There must be no "return" statements between this point and the
15346 end of this function; set "type "to the correct return value and
15347 use "goto done;" to return. */
15348 /* Make sure that the right number of template parameters were
15349 present. */
15350 if (!cp_parser_check_template_parameters (parser, num_templates,
15351 type_start_token->location))
15352 {
15353 /* If something went wrong, there is no point in even trying to
15354 process the class-definition. */
15355 type = NULL_TREE;
15356 goto done;
15357 }
15358
15359 /* Look up the type. */
15360 if (template_id_p)
15361 {
15362 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15363 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15364 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15365 {
15366 error ("%Hfunction template %qD redeclared as a class template",
15367 &type_start_token->location, id);
15368 type = error_mark_node;
15369 }
15370 else
15371 {
15372 type = TREE_TYPE (id);
15373 type = maybe_process_partial_specialization (type);
15374 }
15375 if (nested_name_specifier)
15376 pushed_scope = push_scope (nested_name_specifier);
15377 }
15378 else if (nested_name_specifier)
15379 {
15380 tree class_type;
15381
15382 /* Given:
15383
15384 template <typename T> struct S { struct T };
15385 template <typename T> struct S<T>::T { };
15386
15387 we will get a TYPENAME_TYPE when processing the definition of
15388 `S::T'. We need to resolve it to the actual type before we
15389 try to define it. */
15390 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15391 {
15392 class_type = resolve_typename_type (TREE_TYPE (type),
15393 /*only_current_p=*/false);
15394 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15395 type = TYPE_NAME (class_type);
15396 else
15397 {
15398 cp_parser_error (parser, "could not resolve typename type");
15399 type = error_mark_node;
15400 }
15401 }
15402
15403 if (maybe_process_partial_specialization (TREE_TYPE (type))
15404 == error_mark_node)
15405 {
15406 type = NULL_TREE;
15407 goto done;
15408 }
15409
15410 class_type = current_class_type;
15411 /* Enter the scope indicated by the nested-name-specifier. */
15412 pushed_scope = push_scope (nested_name_specifier);
15413 /* Get the canonical version of this type. */
15414 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15415 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15416 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15417 {
15418 type = push_template_decl (type);
15419 if (type == error_mark_node)
15420 {
15421 type = NULL_TREE;
15422 goto done;
15423 }
15424 }
15425
15426 type = TREE_TYPE (type);
15427 *nested_name_specifier_p = true;
15428 }
15429 else /* The name is not a nested name. */
15430 {
15431 /* If the class was unnamed, create a dummy name. */
15432 if (!id)
15433 id = make_anon_name ();
15434 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15435 parser->num_template_parameter_lists);
15436 }
15437
15438 /* Indicate whether this class was declared as a `class' or as a
15439 `struct'. */
15440 if (TREE_CODE (type) == RECORD_TYPE)
15441 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15442 cp_parser_check_class_key (class_key, type);
15443
15444 /* If this type was already complete, and we see another definition,
15445 that's an error. */
15446 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15447 {
15448 error ("%Hredefinition of %q#T",
15449 &type_start_token->location, type);
15450 error ("%Hprevious definition of %q+#T",
15451 &type_start_token->location, type);
15452 type = NULL_TREE;
15453 goto done;
15454 }
15455 else if (type == error_mark_node)
15456 type = NULL_TREE;
15457
15458 /* We will have entered the scope containing the class; the names of
15459 base classes should be looked up in that context. For example:
15460
15461 struct A { struct B {}; struct C; };
15462 struct A::C : B {};
15463
15464 is valid. */
15465
15466 /* Get the list of base-classes, if there is one. */
15467 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15468 *bases = cp_parser_base_clause (parser);
15469
15470 done:
15471 /* Leave the scope given by the nested-name-specifier. We will
15472 enter the class scope itself while processing the members. */
15473 if (pushed_scope)
15474 pop_scope (pushed_scope);
15475
15476 if (invalid_explicit_specialization_p)
15477 {
15478 end_specialization ();
15479 --parser->num_template_parameter_lists;
15480 }
15481 *attributes_p = attributes;
15482 return type;
15483 }
15484
15485 /* Parse a class-key.
15486
15487 class-key:
15488 class
15489 struct
15490 union
15491
15492 Returns the kind of class-key specified, or none_type to indicate
15493 error. */
15494
15495 static enum tag_types
15496 cp_parser_class_key (cp_parser* parser)
15497 {
15498 cp_token *token;
15499 enum tag_types tag_type;
15500
15501 /* Look for the class-key. */
15502 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15503 if (!token)
15504 return none_type;
15505
15506 /* Check to see if the TOKEN is a class-key. */
15507 tag_type = cp_parser_token_is_class_key (token);
15508 if (!tag_type)
15509 cp_parser_error (parser, "expected class-key");
15510 return tag_type;
15511 }
15512
15513 /* Parse an (optional) member-specification.
15514
15515 member-specification:
15516 member-declaration member-specification [opt]
15517 access-specifier : member-specification [opt] */
15518
15519 static void
15520 cp_parser_member_specification_opt (cp_parser* parser)
15521 {
15522 while (true)
15523 {
15524 cp_token *token;
15525 enum rid keyword;
15526
15527 /* Peek at the next token. */
15528 token = cp_lexer_peek_token (parser->lexer);
15529 /* If it's a `}', or EOF then we've seen all the members. */
15530 if (token->type == CPP_CLOSE_BRACE
15531 || token->type == CPP_EOF
15532 || token->type == CPP_PRAGMA_EOL)
15533 break;
15534
15535 /* See if this token is a keyword. */
15536 keyword = token->keyword;
15537 switch (keyword)
15538 {
15539 case RID_PUBLIC:
15540 case RID_PROTECTED:
15541 case RID_PRIVATE:
15542 /* Consume the access-specifier. */
15543 cp_lexer_consume_token (parser->lexer);
15544 /* Remember which access-specifier is active. */
15545 current_access_specifier = token->u.value;
15546 /* Look for the `:'. */
15547 cp_parser_require (parser, CPP_COLON, "%<:%>");
15548 break;
15549
15550 default:
15551 /* Accept #pragmas at class scope. */
15552 if (token->type == CPP_PRAGMA)
15553 {
15554 cp_parser_pragma (parser, pragma_external);
15555 break;
15556 }
15557
15558 /* Otherwise, the next construction must be a
15559 member-declaration. */
15560 cp_parser_member_declaration (parser);
15561 }
15562 }
15563 }
15564
15565 /* Parse a member-declaration.
15566
15567 member-declaration:
15568 decl-specifier-seq [opt] member-declarator-list [opt] ;
15569 function-definition ; [opt]
15570 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15571 using-declaration
15572 template-declaration
15573
15574 member-declarator-list:
15575 member-declarator
15576 member-declarator-list , member-declarator
15577
15578 member-declarator:
15579 declarator pure-specifier [opt]
15580 declarator constant-initializer [opt]
15581 identifier [opt] : constant-expression
15582
15583 GNU Extensions:
15584
15585 member-declaration:
15586 __extension__ member-declaration
15587
15588 member-declarator:
15589 declarator attributes [opt] pure-specifier [opt]
15590 declarator attributes [opt] constant-initializer [opt]
15591 identifier [opt] attributes [opt] : constant-expression
15592
15593 C++0x Extensions:
15594
15595 member-declaration:
15596 static_assert-declaration */
15597
15598 static void
15599 cp_parser_member_declaration (cp_parser* parser)
15600 {
15601 cp_decl_specifier_seq decl_specifiers;
15602 tree prefix_attributes;
15603 tree decl;
15604 int declares_class_or_enum;
15605 bool friend_p;
15606 cp_token *token = NULL;
15607 cp_token *decl_spec_token_start = NULL;
15608 cp_token *initializer_token_start = NULL;
15609 int saved_pedantic;
15610
15611 /* Check for the `__extension__' keyword. */
15612 if (cp_parser_extension_opt (parser, &saved_pedantic))
15613 {
15614 /* Recurse. */
15615 cp_parser_member_declaration (parser);
15616 /* Restore the old value of the PEDANTIC flag. */
15617 pedantic = saved_pedantic;
15618
15619 return;
15620 }
15621
15622 /* Check for a template-declaration. */
15623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15624 {
15625 /* An explicit specialization here is an error condition, and we
15626 expect the specialization handler to detect and report this. */
15627 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15628 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15629 cp_parser_explicit_specialization (parser);
15630 else
15631 cp_parser_template_declaration (parser, /*member_p=*/true);
15632
15633 return;
15634 }
15635
15636 /* Check for a using-declaration. */
15637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15638 {
15639 /* Parse the using-declaration. */
15640 cp_parser_using_declaration (parser,
15641 /*access_declaration_p=*/false);
15642 return;
15643 }
15644
15645 /* Check for @defs. */
15646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15647 {
15648 tree ivar, member;
15649 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15650 ivar = ivar_chains;
15651 while (ivar)
15652 {
15653 member = ivar;
15654 ivar = TREE_CHAIN (member);
15655 TREE_CHAIN (member) = NULL_TREE;
15656 finish_member_declaration (member);
15657 }
15658 return;
15659 }
15660
15661 /* If the next token is `static_assert' we have a static assertion. */
15662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15663 {
15664 cp_parser_static_assert (parser, /*member_p=*/true);
15665 return;
15666 }
15667
15668 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15669 return;
15670
15671 /* Parse the decl-specifier-seq. */
15672 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15673 cp_parser_decl_specifier_seq (parser,
15674 CP_PARSER_FLAGS_OPTIONAL,
15675 &decl_specifiers,
15676 &declares_class_or_enum);
15677 prefix_attributes = decl_specifiers.attributes;
15678 decl_specifiers.attributes = NULL_TREE;
15679 /* Check for an invalid type-name. */
15680 if (!decl_specifiers.type
15681 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15682 return;
15683 /* If there is no declarator, then the decl-specifier-seq should
15684 specify a type. */
15685 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15686 {
15687 /* If there was no decl-specifier-seq, and the next token is a
15688 `;', then we have something like:
15689
15690 struct S { ; };
15691
15692 [class.mem]
15693
15694 Each member-declaration shall declare at least one member
15695 name of the class. */
15696 if (!decl_specifiers.any_specifiers_p)
15697 {
15698 cp_token *token = cp_lexer_peek_token (parser->lexer);
15699 if (!in_system_header_at (token->location))
15700 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15701 }
15702 else
15703 {
15704 tree type;
15705
15706 /* See if this declaration is a friend. */
15707 friend_p = cp_parser_friend_p (&decl_specifiers);
15708 /* If there were decl-specifiers, check to see if there was
15709 a class-declaration. */
15710 type = check_tag_decl (&decl_specifiers);
15711 /* Nested classes have already been added to the class, but
15712 a `friend' needs to be explicitly registered. */
15713 if (friend_p)
15714 {
15715 /* If the `friend' keyword was present, the friend must
15716 be introduced with a class-key. */
15717 if (!declares_class_or_enum)
15718 error ("%Ha class-key must be used when declaring a friend",
15719 &decl_spec_token_start->location);
15720 /* In this case:
15721
15722 template <typename T> struct A {
15723 friend struct A<T>::B;
15724 };
15725
15726 A<T>::B will be represented by a TYPENAME_TYPE, and
15727 therefore not recognized by check_tag_decl. */
15728 if (!type
15729 && decl_specifiers.type
15730 && TYPE_P (decl_specifiers.type))
15731 type = decl_specifiers.type;
15732 if (!type || !TYPE_P (type))
15733 error ("%Hfriend declaration does not name a class or "
15734 "function", &decl_spec_token_start->location);
15735 else
15736 make_friend_class (current_class_type, type,
15737 /*complain=*/true);
15738 }
15739 /* If there is no TYPE, an error message will already have
15740 been issued. */
15741 else if (!type || type == error_mark_node)
15742 ;
15743 /* An anonymous aggregate has to be handled specially; such
15744 a declaration really declares a data member (with a
15745 particular type), as opposed to a nested class. */
15746 else if (ANON_AGGR_TYPE_P (type))
15747 {
15748 /* Remove constructors and such from TYPE, now that we
15749 know it is an anonymous aggregate. */
15750 fixup_anonymous_aggr (type);
15751 /* And make the corresponding data member. */
15752 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15753 /* Add it to the class. */
15754 finish_member_declaration (decl);
15755 }
15756 else
15757 cp_parser_check_access_in_redeclaration
15758 (TYPE_NAME (type),
15759 decl_spec_token_start->location);
15760 }
15761 }
15762 else
15763 {
15764 /* See if these declarations will be friends. */
15765 friend_p = cp_parser_friend_p (&decl_specifiers);
15766
15767 /* Keep going until we hit the `;' at the end of the
15768 declaration. */
15769 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15770 {
15771 tree attributes = NULL_TREE;
15772 tree first_attribute;
15773
15774 /* Peek at the next token. */
15775 token = cp_lexer_peek_token (parser->lexer);
15776
15777 /* Check for a bitfield declaration. */
15778 if (token->type == CPP_COLON
15779 || (token->type == CPP_NAME
15780 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15781 == CPP_COLON))
15782 {
15783 tree identifier;
15784 tree width;
15785
15786 /* Get the name of the bitfield. Note that we cannot just
15787 check TOKEN here because it may have been invalidated by
15788 the call to cp_lexer_peek_nth_token above. */
15789 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15790 identifier = cp_parser_identifier (parser);
15791 else
15792 identifier = NULL_TREE;
15793
15794 /* Consume the `:' token. */
15795 cp_lexer_consume_token (parser->lexer);
15796 /* Get the width of the bitfield. */
15797 width
15798 = cp_parser_constant_expression (parser,
15799 /*allow_non_constant=*/false,
15800 NULL);
15801
15802 /* Look for attributes that apply to the bitfield. */
15803 attributes = cp_parser_attributes_opt (parser);
15804 /* Remember which attributes are prefix attributes and
15805 which are not. */
15806 first_attribute = attributes;
15807 /* Combine the attributes. */
15808 attributes = chainon (prefix_attributes, attributes);
15809
15810 /* Create the bitfield declaration. */
15811 decl = grokbitfield (identifier
15812 ? make_id_declarator (NULL_TREE,
15813 identifier,
15814 sfk_none)
15815 : NULL,
15816 &decl_specifiers,
15817 width,
15818 attributes);
15819 }
15820 else
15821 {
15822 cp_declarator *declarator;
15823 tree initializer;
15824 tree asm_specification;
15825 int ctor_dtor_or_conv_p;
15826
15827 /* Parse the declarator. */
15828 declarator
15829 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15830 &ctor_dtor_or_conv_p,
15831 /*parenthesized_p=*/NULL,
15832 /*member_p=*/true);
15833
15834 /* If something went wrong parsing the declarator, make sure
15835 that we at least consume some tokens. */
15836 if (declarator == cp_error_declarator)
15837 {
15838 /* Skip to the end of the statement. */
15839 cp_parser_skip_to_end_of_statement (parser);
15840 /* If the next token is not a semicolon, that is
15841 probably because we just skipped over the body of
15842 a function. So, we consume a semicolon if
15843 present, but do not issue an error message if it
15844 is not present. */
15845 if (cp_lexer_next_token_is (parser->lexer,
15846 CPP_SEMICOLON))
15847 cp_lexer_consume_token (parser->lexer);
15848 return;
15849 }
15850
15851 if (declares_class_or_enum & 2)
15852 cp_parser_check_for_definition_in_return_type
15853 (declarator, decl_specifiers.type,
15854 decl_specifiers.type_location);
15855
15856 /* Look for an asm-specification. */
15857 asm_specification = cp_parser_asm_specification_opt (parser);
15858 /* Look for attributes that apply to the declaration. */
15859 attributes = cp_parser_attributes_opt (parser);
15860 /* Remember which attributes are prefix attributes and
15861 which are not. */
15862 first_attribute = attributes;
15863 /* Combine the attributes. */
15864 attributes = chainon (prefix_attributes, attributes);
15865
15866 /* If it's an `=', then we have a constant-initializer or a
15867 pure-specifier. It is not correct to parse the
15868 initializer before registering the member declaration
15869 since the member declaration should be in scope while
15870 its initializer is processed. However, the rest of the
15871 front end does not yet provide an interface that allows
15872 us to handle this correctly. */
15873 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15874 {
15875 /* In [class.mem]:
15876
15877 A pure-specifier shall be used only in the declaration of
15878 a virtual function.
15879
15880 A member-declarator can contain a constant-initializer
15881 only if it declares a static member of integral or
15882 enumeration type.
15883
15884 Therefore, if the DECLARATOR is for a function, we look
15885 for a pure-specifier; otherwise, we look for a
15886 constant-initializer. When we call `grokfield', it will
15887 perform more stringent semantics checks. */
15888 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15889 if (function_declarator_p (declarator))
15890 initializer = cp_parser_pure_specifier (parser);
15891 else
15892 /* Parse the initializer. */
15893 initializer = cp_parser_constant_initializer (parser);
15894 }
15895 /* Otherwise, there is no initializer. */
15896 else
15897 initializer = NULL_TREE;
15898
15899 /* See if we are probably looking at a function
15900 definition. We are certainly not looking at a
15901 member-declarator. Calling `grokfield' has
15902 side-effects, so we must not do it unless we are sure
15903 that we are looking at a member-declarator. */
15904 if (cp_parser_token_starts_function_definition_p
15905 (cp_lexer_peek_token (parser->lexer)))
15906 {
15907 /* The grammar does not allow a pure-specifier to be
15908 used when a member function is defined. (It is
15909 possible that this fact is an oversight in the
15910 standard, since a pure function may be defined
15911 outside of the class-specifier. */
15912 if (initializer)
15913 error ("%Hpure-specifier on function-definition",
15914 &initializer_token_start->location);
15915 decl = cp_parser_save_member_function_body (parser,
15916 &decl_specifiers,
15917 declarator,
15918 attributes);
15919 /* If the member was not a friend, declare it here. */
15920 if (!friend_p)
15921 finish_member_declaration (decl);
15922 /* Peek at the next token. */
15923 token = cp_lexer_peek_token (parser->lexer);
15924 /* If the next token is a semicolon, consume it. */
15925 if (token->type == CPP_SEMICOLON)
15926 cp_lexer_consume_token (parser->lexer);
15927 return;
15928 }
15929 else
15930 if (declarator->kind == cdk_function)
15931 declarator->id_loc = token->location;
15932 /* Create the declaration. */
15933 decl = grokfield (declarator, &decl_specifiers,
15934 initializer, /*init_const_expr_p=*/true,
15935 asm_specification,
15936 attributes);
15937 }
15938
15939 /* Reset PREFIX_ATTRIBUTES. */
15940 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15941 attributes = TREE_CHAIN (attributes);
15942 if (attributes)
15943 TREE_CHAIN (attributes) = NULL_TREE;
15944
15945 /* If there is any qualification still in effect, clear it
15946 now; we will be starting fresh with the next declarator. */
15947 parser->scope = NULL_TREE;
15948 parser->qualifying_scope = NULL_TREE;
15949 parser->object_scope = NULL_TREE;
15950 /* If it's a `,', then there are more declarators. */
15951 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15952 cp_lexer_consume_token (parser->lexer);
15953 /* If the next token isn't a `;', then we have a parse error. */
15954 else if (cp_lexer_next_token_is_not (parser->lexer,
15955 CPP_SEMICOLON))
15956 {
15957 cp_parser_error (parser, "expected %<;%>");
15958 /* Skip tokens until we find a `;'. */
15959 cp_parser_skip_to_end_of_statement (parser);
15960
15961 break;
15962 }
15963
15964 if (decl)
15965 {
15966 /* Add DECL to the list of members. */
15967 if (!friend_p)
15968 finish_member_declaration (decl);
15969
15970 if (TREE_CODE (decl) == FUNCTION_DECL)
15971 cp_parser_save_default_args (parser, decl);
15972 }
15973 }
15974 }
15975
15976 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15977 }
15978
15979 /* Parse a pure-specifier.
15980
15981 pure-specifier:
15982 = 0
15983
15984 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15985 Otherwise, ERROR_MARK_NODE is returned. */
15986
15987 static tree
15988 cp_parser_pure_specifier (cp_parser* parser)
15989 {
15990 cp_token *token;
15991
15992 /* Look for the `=' token. */
15993 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15994 return error_mark_node;
15995 /* Look for the `0' token. */
15996 token = cp_lexer_consume_token (parser->lexer);
15997
15998 /* Accept = default or = delete in c++0x mode. */
15999 if (token->keyword == RID_DEFAULT
16000 || token->keyword == RID_DELETE)
16001 {
16002 maybe_warn_cpp0x ("defaulted and deleted functions");
16003 return token->u.value;
16004 }
16005
16006 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16007 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16008 {
16009 cp_parser_error (parser,
16010 "invalid pure specifier (only %<= 0%> is allowed)");
16011 cp_parser_skip_to_end_of_statement (parser);
16012 return error_mark_node;
16013 }
16014 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16015 {
16016 error ("%Htemplates may not be %<virtual%>", &token->location);
16017 return error_mark_node;
16018 }
16019
16020 return integer_zero_node;
16021 }
16022
16023 /* Parse a constant-initializer.
16024
16025 constant-initializer:
16026 = constant-expression
16027
16028 Returns a representation of the constant-expression. */
16029
16030 static tree
16031 cp_parser_constant_initializer (cp_parser* parser)
16032 {
16033 /* Look for the `=' token. */
16034 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16035 return error_mark_node;
16036
16037 /* It is invalid to write:
16038
16039 struct S { static const int i = { 7 }; };
16040
16041 */
16042 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16043 {
16044 cp_parser_error (parser,
16045 "a brace-enclosed initializer is not allowed here");
16046 /* Consume the opening brace. */
16047 cp_lexer_consume_token (parser->lexer);
16048 /* Skip the initializer. */
16049 cp_parser_skip_to_closing_brace (parser);
16050 /* Look for the trailing `}'. */
16051 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16052
16053 return error_mark_node;
16054 }
16055
16056 return cp_parser_constant_expression (parser,
16057 /*allow_non_constant=*/false,
16058 NULL);
16059 }
16060
16061 /* Derived classes [gram.class.derived] */
16062
16063 /* Parse a base-clause.
16064
16065 base-clause:
16066 : base-specifier-list
16067
16068 base-specifier-list:
16069 base-specifier ... [opt]
16070 base-specifier-list , base-specifier ... [opt]
16071
16072 Returns a TREE_LIST representing the base-classes, in the order in
16073 which they were declared. The representation of each node is as
16074 described by cp_parser_base_specifier.
16075
16076 In the case that no bases are specified, this function will return
16077 NULL_TREE, not ERROR_MARK_NODE. */
16078
16079 static tree
16080 cp_parser_base_clause (cp_parser* parser)
16081 {
16082 tree bases = NULL_TREE;
16083
16084 /* Look for the `:' that begins the list. */
16085 cp_parser_require (parser, CPP_COLON, "%<:%>");
16086
16087 /* Scan the base-specifier-list. */
16088 while (true)
16089 {
16090 cp_token *token;
16091 tree base;
16092 bool pack_expansion_p = false;
16093
16094 /* Look for the base-specifier. */
16095 base = cp_parser_base_specifier (parser);
16096 /* Look for the (optional) ellipsis. */
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16098 {
16099 /* Consume the `...'. */
16100 cp_lexer_consume_token (parser->lexer);
16101
16102 pack_expansion_p = true;
16103 }
16104
16105 /* Add BASE to the front of the list. */
16106 if (base != error_mark_node)
16107 {
16108 if (pack_expansion_p)
16109 /* Make this a pack expansion type. */
16110 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16111
16112
16113 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16114 {
16115 TREE_CHAIN (base) = bases;
16116 bases = base;
16117 }
16118 }
16119 /* Peek at the next token. */
16120 token = cp_lexer_peek_token (parser->lexer);
16121 /* If it's not a comma, then the list is complete. */
16122 if (token->type != CPP_COMMA)
16123 break;
16124 /* Consume the `,'. */
16125 cp_lexer_consume_token (parser->lexer);
16126 }
16127
16128 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16129 base class had a qualified name. However, the next name that
16130 appears is certainly not qualified. */
16131 parser->scope = NULL_TREE;
16132 parser->qualifying_scope = NULL_TREE;
16133 parser->object_scope = NULL_TREE;
16134
16135 return nreverse (bases);
16136 }
16137
16138 /* Parse a base-specifier.
16139
16140 base-specifier:
16141 :: [opt] nested-name-specifier [opt] class-name
16142 virtual access-specifier [opt] :: [opt] nested-name-specifier
16143 [opt] class-name
16144 access-specifier virtual [opt] :: [opt] nested-name-specifier
16145 [opt] class-name
16146
16147 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16148 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16149 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16150 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16151
16152 static tree
16153 cp_parser_base_specifier (cp_parser* parser)
16154 {
16155 cp_token *token;
16156 bool done = false;
16157 bool virtual_p = false;
16158 bool duplicate_virtual_error_issued_p = false;
16159 bool duplicate_access_error_issued_p = false;
16160 bool class_scope_p, template_p;
16161 tree access = access_default_node;
16162 tree type;
16163
16164 /* Process the optional `virtual' and `access-specifier'. */
16165 while (!done)
16166 {
16167 /* Peek at the next token. */
16168 token = cp_lexer_peek_token (parser->lexer);
16169 /* Process `virtual'. */
16170 switch (token->keyword)
16171 {
16172 case RID_VIRTUAL:
16173 /* If `virtual' appears more than once, issue an error. */
16174 if (virtual_p && !duplicate_virtual_error_issued_p)
16175 {
16176 cp_parser_error (parser,
16177 "%<virtual%> specified more than once in base-specified");
16178 duplicate_virtual_error_issued_p = true;
16179 }
16180
16181 virtual_p = true;
16182
16183 /* Consume the `virtual' token. */
16184 cp_lexer_consume_token (parser->lexer);
16185
16186 break;
16187
16188 case RID_PUBLIC:
16189 case RID_PROTECTED:
16190 case RID_PRIVATE:
16191 /* If more than one access specifier appears, issue an
16192 error. */
16193 if (access != access_default_node
16194 && !duplicate_access_error_issued_p)
16195 {
16196 cp_parser_error (parser,
16197 "more than one access specifier in base-specified");
16198 duplicate_access_error_issued_p = true;
16199 }
16200
16201 access = ridpointers[(int) token->keyword];
16202
16203 /* Consume the access-specifier. */
16204 cp_lexer_consume_token (parser->lexer);
16205
16206 break;
16207
16208 default:
16209 done = true;
16210 break;
16211 }
16212 }
16213 /* It is not uncommon to see programs mechanically, erroneously, use
16214 the 'typename' keyword to denote (dependent) qualified types
16215 as base classes. */
16216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16217 {
16218 token = cp_lexer_peek_token (parser->lexer);
16219 if (!processing_template_decl)
16220 error ("%Hkeyword %<typename%> not allowed outside of templates",
16221 &token->location);
16222 else
16223 error ("%Hkeyword %<typename%> not allowed in this context "
16224 "(the base class is implicitly a type)",
16225 &token->location);
16226 cp_lexer_consume_token (parser->lexer);
16227 }
16228
16229 /* Look for the optional `::' operator. */
16230 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16231 /* Look for the nested-name-specifier. The simplest way to
16232 implement:
16233
16234 [temp.res]
16235
16236 The keyword `typename' is not permitted in a base-specifier or
16237 mem-initializer; in these contexts a qualified name that
16238 depends on a template-parameter is implicitly assumed to be a
16239 type name.
16240
16241 is to pretend that we have seen the `typename' keyword at this
16242 point. */
16243 cp_parser_nested_name_specifier_opt (parser,
16244 /*typename_keyword_p=*/true,
16245 /*check_dependency_p=*/true,
16246 typename_type,
16247 /*is_declaration=*/true);
16248 /* If the base class is given by a qualified name, assume that names
16249 we see are type names or templates, as appropriate. */
16250 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16251 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16252
16253 /* Finally, look for the class-name. */
16254 type = cp_parser_class_name (parser,
16255 class_scope_p,
16256 template_p,
16257 typename_type,
16258 /*check_dependency_p=*/true,
16259 /*class_head_p=*/false,
16260 /*is_declaration=*/true);
16261
16262 if (type == error_mark_node)
16263 return error_mark_node;
16264
16265 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16266 }
16267
16268 /* Exception handling [gram.exception] */
16269
16270 /* Parse an (optional) exception-specification.
16271
16272 exception-specification:
16273 throw ( type-id-list [opt] )
16274
16275 Returns a TREE_LIST representing the exception-specification. The
16276 TREE_VALUE of each node is a type. */
16277
16278 static tree
16279 cp_parser_exception_specification_opt (cp_parser* parser)
16280 {
16281 cp_token *token;
16282 tree type_id_list;
16283
16284 /* Peek at the next token. */
16285 token = cp_lexer_peek_token (parser->lexer);
16286 /* If it's not `throw', then there's no exception-specification. */
16287 if (!cp_parser_is_keyword (token, RID_THROW))
16288 return NULL_TREE;
16289
16290 /* Consume the `throw'. */
16291 cp_lexer_consume_token (parser->lexer);
16292
16293 /* Look for the `('. */
16294 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16295
16296 /* Peek at the next token. */
16297 token = cp_lexer_peek_token (parser->lexer);
16298 /* If it's not a `)', then there is a type-id-list. */
16299 if (token->type != CPP_CLOSE_PAREN)
16300 {
16301 const char *saved_message;
16302
16303 /* Types may not be defined in an exception-specification. */
16304 saved_message = parser->type_definition_forbidden_message;
16305 parser->type_definition_forbidden_message
16306 = "types may not be defined in an exception-specification";
16307 /* Parse the type-id-list. */
16308 type_id_list = cp_parser_type_id_list (parser);
16309 /* Restore the saved message. */
16310 parser->type_definition_forbidden_message = saved_message;
16311 }
16312 else
16313 type_id_list = empty_except_spec;
16314
16315 /* Look for the `)'. */
16316 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16317
16318 return type_id_list;
16319 }
16320
16321 /* Parse an (optional) type-id-list.
16322
16323 type-id-list:
16324 type-id ... [opt]
16325 type-id-list , type-id ... [opt]
16326
16327 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16328 in the order that the types were presented. */
16329
16330 static tree
16331 cp_parser_type_id_list (cp_parser* parser)
16332 {
16333 tree types = NULL_TREE;
16334
16335 while (true)
16336 {
16337 cp_token *token;
16338 tree type;
16339
16340 /* Get the next type-id. */
16341 type = cp_parser_type_id (parser);
16342 /* Parse the optional ellipsis. */
16343 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16344 {
16345 /* Consume the `...'. */
16346 cp_lexer_consume_token (parser->lexer);
16347
16348 /* Turn the type into a pack expansion expression. */
16349 type = make_pack_expansion (type);
16350 }
16351 /* Add it to the list. */
16352 types = add_exception_specifier (types, type, /*complain=*/1);
16353 /* Peek at the next token. */
16354 token = cp_lexer_peek_token (parser->lexer);
16355 /* If it is not a `,', we are done. */
16356 if (token->type != CPP_COMMA)
16357 break;
16358 /* Consume the `,'. */
16359 cp_lexer_consume_token (parser->lexer);
16360 }
16361
16362 return nreverse (types);
16363 }
16364
16365 /* Parse a try-block.
16366
16367 try-block:
16368 try compound-statement handler-seq */
16369
16370 static tree
16371 cp_parser_try_block (cp_parser* parser)
16372 {
16373 tree try_block;
16374
16375 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16376 try_block = begin_try_block ();
16377 cp_parser_compound_statement (parser, NULL, true);
16378 finish_try_block (try_block);
16379 cp_parser_handler_seq (parser);
16380 finish_handler_sequence (try_block);
16381
16382 return try_block;
16383 }
16384
16385 /* Parse a function-try-block.
16386
16387 function-try-block:
16388 try ctor-initializer [opt] function-body handler-seq */
16389
16390 static bool
16391 cp_parser_function_try_block (cp_parser* parser)
16392 {
16393 tree compound_stmt;
16394 tree try_block;
16395 bool ctor_initializer_p;
16396
16397 /* Look for the `try' keyword. */
16398 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16399 return false;
16400 /* Let the rest of the front end know where we are. */
16401 try_block = begin_function_try_block (&compound_stmt);
16402 /* Parse the function-body. */
16403 ctor_initializer_p
16404 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16405 /* We're done with the `try' part. */
16406 finish_function_try_block (try_block);
16407 /* Parse the handlers. */
16408 cp_parser_handler_seq (parser);
16409 /* We're done with the handlers. */
16410 finish_function_handler_sequence (try_block, compound_stmt);
16411
16412 return ctor_initializer_p;
16413 }
16414
16415 /* Parse a handler-seq.
16416
16417 handler-seq:
16418 handler handler-seq [opt] */
16419
16420 static void
16421 cp_parser_handler_seq (cp_parser* parser)
16422 {
16423 while (true)
16424 {
16425 cp_token *token;
16426
16427 /* Parse the handler. */
16428 cp_parser_handler (parser);
16429 /* Peek at the next token. */
16430 token = cp_lexer_peek_token (parser->lexer);
16431 /* If it's not `catch' then there are no more handlers. */
16432 if (!cp_parser_is_keyword (token, RID_CATCH))
16433 break;
16434 }
16435 }
16436
16437 /* Parse a handler.
16438
16439 handler:
16440 catch ( exception-declaration ) compound-statement */
16441
16442 static void
16443 cp_parser_handler (cp_parser* parser)
16444 {
16445 tree handler;
16446 tree declaration;
16447
16448 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16449 handler = begin_handler ();
16450 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16451 declaration = cp_parser_exception_declaration (parser);
16452 finish_handler_parms (declaration, handler);
16453 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16454 cp_parser_compound_statement (parser, NULL, false);
16455 finish_handler (handler);
16456 }
16457
16458 /* Parse an exception-declaration.
16459
16460 exception-declaration:
16461 type-specifier-seq declarator
16462 type-specifier-seq abstract-declarator
16463 type-specifier-seq
16464 ...
16465
16466 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16467 ellipsis variant is used. */
16468
16469 static tree
16470 cp_parser_exception_declaration (cp_parser* parser)
16471 {
16472 cp_decl_specifier_seq type_specifiers;
16473 cp_declarator *declarator;
16474 const char *saved_message;
16475
16476 /* If it's an ellipsis, it's easy to handle. */
16477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16478 {
16479 /* Consume the `...' token. */
16480 cp_lexer_consume_token (parser->lexer);
16481 return NULL_TREE;
16482 }
16483
16484 /* Types may not be defined in exception-declarations. */
16485 saved_message = parser->type_definition_forbidden_message;
16486 parser->type_definition_forbidden_message
16487 = "types may not be defined in exception-declarations";
16488
16489 /* Parse the type-specifier-seq. */
16490 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16491 &type_specifiers);
16492 /* If it's a `)', then there is no declarator. */
16493 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16494 declarator = NULL;
16495 else
16496 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16497 /*ctor_dtor_or_conv_p=*/NULL,
16498 /*parenthesized_p=*/NULL,
16499 /*member_p=*/false);
16500
16501 /* Restore the saved message. */
16502 parser->type_definition_forbidden_message = saved_message;
16503
16504 if (!type_specifiers.any_specifiers_p)
16505 return error_mark_node;
16506
16507 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16508 }
16509
16510 /* Parse a throw-expression.
16511
16512 throw-expression:
16513 throw assignment-expression [opt]
16514
16515 Returns a THROW_EXPR representing the throw-expression. */
16516
16517 static tree
16518 cp_parser_throw_expression (cp_parser* parser)
16519 {
16520 tree expression;
16521 cp_token* token;
16522
16523 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16524 token = cp_lexer_peek_token (parser->lexer);
16525 /* Figure out whether or not there is an assignment-expression
16526 following the "throw" keyword. */
16527 if (token->type == CPP_COMMA
16528 || token->type == CPP_SEMICOLON
16529 || token->type == CPP_CLOSE_PAREN
16530 || token->type == CPP_CLOSE_SQUARE
16531 || token->type == CPP_CLOSE_BRACE
16532 || token->type == CPP_COLON)
16533 expression = NULL_TREE;
16534 else
16535 expression = cp_parser_assignment_expression (parser,
16536 /*cast_p=*/false, NULL);
16537
16538 return build_throw (expression);
16539 }
16540
16541 /* GNU Extensions */
16542
16543 /* Parse an (optional) asm-specification.
16544
16545 asm-specification:
16546 asm ( string-literal )
16547
16548 If the asm-specification is present, returns a STRING_CST
16549 corresponding to the string-literal. Otherwise, returns
16550 NULL_TREE. */
16551
16552 static tree
16553 cp_parser_asm_specification_opt (cp_parser* parser)
16554 {
16555 cp_token *token;
16556 tree asm_specification;
16557
16558 /* Peek at the next token. */
16559 token = cp_lexer_peek_token (parser->lexer);
16560 /* If the next token isn't the `asm' keyword, then there's no
16561 asm-specification. */
16562 if (!cp_parser_is_keyword (token, RID_ASM))
16563 return NULL_TREE;
16564
16565 /* Consume the `asm' token. */
16566 cp_lexer_consume_token (parser->lexer);
16567 /* Look for the `('. */
16568 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16569
16570 /* Look for the string-literal. */
16571 asm_specification = cp_parser_string_literal (parser, false, false);
16572
16573 /* Look for the `)'. */
16574 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16575
16576 return asm_specification;
16577 }
16578
16579 /* Parse an asm-operand-list.
16580
16581 asm-operand-list:
16582 asm-operand
16583 asm-operand-list , asm-operand
16584
16585 asm-operand:
16586 string-literal ( expression )
16587 [ string-literal ] string-literal ( expression )
16588
16589 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16590 each node is the expression. The TREE_PURPOSE is itself a
16591 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16592 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16593 is a STRING_CST for the string literal before the parenthesis. Returns
16594 ERROR_MARK_NODE if any of the operands are invalid. */
16595
16596 static tree
16597 cp_parser_asm_operand_list (cp_parser* parser)
16598 {
16599 tree asm_operands = NULL_TREE;
16600 bool invalid_operands = false;
16601
16602 while (true)
16603 {
16604 tree string_literal;
16605 tree expression;
16606 tree name;
16607
16608 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16609 {
16610 /* Consume the `[' token. */
16611 cp_lexer_consume_token (parser->lexer);
16612 /* Read the operand name. */
16613 name = cp_parser_identifier (parser);
16614 if (name != error_mark_node)
16615 name = build_string (IDENTIFIER_LENGTH (name),
16616 IDENTIFIER_POINTER (name));
16617 /* Look for the closing `]'. */
16618 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16619 }
16620 else
16621 name = NULL_TREE;
16622 /* Look for the string-literal. */
16623 string_literal = cp_parser_string_literal (parser, false, false);
16624
16625 /* Look for the `('. */
16626 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16627 /* Parse the expression. */
16628 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16629 /* Look for the `)'. */
16630 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16631
16632 if (name == error_mark_node
16633 || string_literal == error_mark_node
16634 || expression == error_mark_node)
16635 invalid_operands = true;
16636
16637 /* Add this operand to the list. */
16638 asm_operands = tree_cons (build_tree_list (name, string_literal),
16639 expression,
16640 asm_operands);
16641 /* If the next token is not a `,', there are no more
16642 operands. */
16643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16644 break;
16645 /* Consume the `,'. */
16646 cp_lexer_consume_token (parser->lexer);
16647 }
16648
16649 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16650 }
16651
16652 /* Parse an asm-clobber-list.
16653
16654 asm-clobber-list:
16655 string-literal
16656 asm-clobber-list , string-literal
16657
16658 Returns a TREE_LIST, indicating the clobbers in the order that they
16659 appeared. The TREE_VALUE of each node is a STRING_CST. */
16660
16661 static tree
16662 cp_parser_asm_clobber_list (cp_parser* parser)
16663 {
16664 tree clobbers = NULL_TREE;
16665
16666 while (true)
16667 {
16668 tree string_literal;
16669
16670 /* Look for the string literal. */
16671 string_literal = cp_parser_string_literal (parser, false, false);
16672 /* Add it to the list. */
16673 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16674 /* If the next token is not a `,', then the list is
16675 complete. */
16676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16677 break;
16678 /* Consume the `,' token. */
16679 cp_lexer_consume_token (parser->lexer);
16680 }
16681
16682 return clobbers;
16683 }
16684
16685 /* Parse an (optional) series of attributes.
16686
16687 attributes:
16688 attributes attribute
16689
16690 attribute:
16691 __attribute__ (( attribute-list [opt] ))
16692
16693 The return value is as for cp_parser_attribute_list. */
16694
16695 static tree
16696 cp_parser_attributes_opt (cp_parser* parser)
16697 {
16698 tree attributes = NULL_TREE;
16699
16700 while (true)
16701 {
16702 cp_token *token;
16703 tree attribute_list;
16704
16705 /* Peek at the next token. */
16706 token = cp_lexer_peek_token (parser->lexer);
16707 /* If it's not `__attribute__', then we're done. */
16708 if (token->keyword != RID_ATTRIBUTE)
16709 break;
16710
16711 /* Consume the `__attribute__' keyword. */
16712 cp_lexer_consume_token (parser->lexer);
16713 /* Look for the two `(' tokens. */
16714 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16715 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16716
16717 /* Peek at the next token. */
16718 token = cp_lexer_peek_token (parser->lexer);
16719 if (token->type != CPP_CLOSE_PAREN)
16720 /* Parse the attribute-list. */
16721 attribute_list = cp_parser_attribute_list (parser);
16722 else
16723 /* If the next token is a `)', then there is no attribute
16724 list. */
16725 attribute_list = NULL;
16726
16727 /* Look for the two `)' tokens. */
16728 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16729 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16730
16731 /* Add these new attributes to the list. */
16732 attributes = chainon (attributes, attribute_list);
16733 }
16734
16735 return attributes;
16736 }
16737
16738 /* Parse an attribute-list.
16739
16740 attribute-list:
16741 attribute
16742 attribute-list , attribute
16743
16744 attribute:
16745 identifier
16746 identifier ( identifier )
16747 identifier ( identifier , expression-list )
16748 identifier ( expression-list )
16749
16750 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16751 to an attribute. The TREE_PURPOSE of each node is the identifier
16752 indicating which attribute is in use. The TREE_VALUE represents
16753 the arguments, if any. */
16754
16755 static tree
16756 cp_parser_attribute_list (cp_parser* parser)
16757 {
16758 tree attribute_list = NULL_TREE;
16759 bool save_translate_strings_p = parser->translate_strings_p;
16760
16761 parser->translate_strings_p = false;
16762 while (true)
16763 {
16764 cp_token *token;
16765 tree identifier;
16766 tree attribute;
16767
16768 /* Look for the identifier. We also allow keywords here; for
16769 example `__attribute__ ((const))' is legal. */
16770 token = cp_lexer_peek_token (parser->lexer);
16771 if (token->type == CPP_NAME
16772 || token->type == CPP_KEYWORD)
16773 {
16774 tree arguments = NULL_TREE;
16775
16776 /* Consume the token. */
16777 token = cp_lexer_consume_token (parser->lexer);
16778
16779 /* Save away the identifier that indicates which attribute
16780 this is. */
16781 identifier = token->u.value;
16782 attribute = build_tree_list (identifier, NULL_TREE);
16783
16784 /* Peek at the next token. */
16785 token = cp_lexer_peek_token (parser->lexer);
16786 /* If it's an `(', then parse the attribute arguments. */
16787 if (token->type == CPP_OPEN_PAREN)
16788 {
16789 arguments = cp_parser_parenthesized_expression_list
16790 (parser, true, /*cast_p=*/false,
16791 /*allow_expansion_p=*/false,
16792 /*non_constant_p=*/NULL);
16793 /* Save the arguments away. */
16794 TREE_VALUE (attribute) = arguments;
16795 }
16796
16797 if (arguments != error_mark_node)
16798 {
16799 /* Add this attribute to the list. */
16800 TREE_CHAIN (attribute) = attribute_list;
16801 attribute_list = attribute;
16802 }
16803
16804 token = cp_lexer_peek_token (parser->lexer);
16805 }
16806 /* Now, look for more attributes. If the next token isn't a
16807 `,', we're done. */
16808 if (token->type != CPP_COMMA)
16809 break;
16810
16811 /* Consume the comma and keep going. */
16812 cp_lexer_consume_token (parser->lexer);
16813 }
16814 parser->translate_strings_p = save_translate_strings_p;
16815
16816 /* We built up the list in reverse order. */
16817 return nreverse (attribute_list);
16818 }
16819
16820 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16821 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16822 current value of the PEDANTIC flag, regardless of whether or not
16823 the `__extension__' keyword is present. The caller is responsible
16824 for restoring the value of the PEDANTIC flag. */
16825
16826 static bool
16827 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16828 {
16829 /* Save the old value of the PEDANTIC flag. */
16830 *saved_pedantic = pedantic;
16831
16832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16833 {
16834 /* Consume the `__extension__' token. */
16835 cp_lexer_consume_token (parser->lexer);
16836 /* We're not being pedantic while the `__extension__' keyword is
16837 in effect. */
16838 pedantic = 0;
16839
16840 return true;
16841 }
16842
16843 return false;
16844 }
16845
16846 /* Parse a label declaration.
16847
16848 label-declaration:
16849 __label__ label-declarator-seq ;
16850
16851 label-declarator-seq:
16852 identifier , label-declarator-seq
16853 identifier */
16854
16855 static void
16856 cp_parser_label_declaration (cp_parser* parser)
16857 {
16858 /* Look for the `__label__' keyword. */
16859 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16860
16861 while (true)
16862 {
16863 tree identifier;
16864
16865 /* Look for an identifier. */
16866 identifier = cp_parser_identifier (parser);
16867 /* If we failed, stop. */
16868 if (identifier == error_mark_node)
16869 break;
16870 /* Declare it as a label. */
16871 finish_label_decl (identifier);
16872 /* If the next token is a `;', stop. */
16873 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16874 break;
16875 /* Look for the `,' separating the label declarations. */
16876 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16877 }
16878
16879 /* Look for the final `;'. */
16880 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16881 }
16882
16883 /* Support Functions */
16884
16885 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16886 NAME should have one of the representations used for an
16887 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16888 is returned. If PARSER->SCOPE is a dependent type, then a
16889 SCOPE_REF is returned.
16890
16891 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16892 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16893 was formed. Abstractly, such entities should not be passed to this
16894 function, because they do not need to be looked up, but it is
16895 simpler to check for this special case here, rather than at the
16896 call-sites.
16897
16898 In cases not explicitly covered above, this function returns a
16899 DECL, OVERLOAD, or baselink representing the result of the lookup.
16900 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16901 is returned.
16902
16903 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16904 (e.g., "struct") that was used. In that case bindings that do not
16905 refer to types are ignored.
16906
16907 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16908 ignored.
16909
16910 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16911 are ignored.
16912
16913 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16914 types.
16915
16916 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16917 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16918 NULL_TREE otherwise. */
16919
16920 static tree
16921 cp_parser_lookup_name (cp_parser *parser, tree name,
16922 enum tag_types tag_type,
16923 bool is_template,
16924 bool is_namespace,
16925 bool check_dependency,
16926 tree *ambiguous_decls,
16927 location_t name_location)
16928 {
16929 int flags = 0;
16930 tree decl;
16931 tree object_type = parser->context->object_type;
16932
16933 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16934 flags |= LOOKUP_COMPLAIN;
16935
16936 /* Assume that the lookup will be unambiguous. */
16937 if (ambiguous_decls)
16938 *ambiguous_decls = NULL_TREE;
16939
16940 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16941 no longer valid. Note that if we are parsing tentatively, and
16942 the parse fails, OBJECT_TYPE will be automatically restored. */
16943 parser->context->object_type = NULL_TREE;
16944
16945 if (name == error_mark_node)
16946 return error_mark_node;
16947
16948 /* A template-id has already been resolved; there is no lookup to
16949 do. */
16950 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16951 return name;
16952 if (BASELINK_P (name))
16953 {
16954 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16955 == TEMPLATE_ID_EXPR);
16956 return name;
16957 }
16958
16959 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16960 it should already have been checked to make sure that the name
16961 used matches the type being destroyed. */
16962 if (TREE_CODE (name) == BIT_NOT_EXPR)
16963 {
16964 tree type;
16965
16966 /* Figure out to which type this destructor applies. */
16967 if (parser->scope)
16968 type = parser->scope;
16969 else if (object_type)
16970 type = object_type;
16971 else
16972 type = current_class_type;
16973 /* If that's not a class type, there is no destructor. */
16974 if (!type || !CLASS_TYPE_P (type))
16975 return error_mark_node;
16976 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16977 lazily_declare_fn (sfk_destructor, type);
16978 if (!CLASSTYPE_DESTRUCTORS (type))
16979 return error_mark_node;
16980 /* If it was a class type, return the destructor. */
16981 return CLASSTYPE_DESTRUCTORS (type);
16982 }
16983
16984 /* By this point, the NAME should be an ordinary identifier. If
16985 the id-expression was a qualified name, the qualifying scope is
16986 stored in PARSER->SCOPE at this point. */
16987 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16988
16989 /* Perform the lookup. */
16990 if (parser->scope)
16991 {
16992 bool dependent_p;
16993
16994 if (parser->scope == error_mark_node)
16995 return error_mark_node;
16996
16997 /* If the SCOPE is dependent, the lookup must be deferred until
16998 the template is instantiated -- unless we are explicitly
16999 looking up names in uninstantiated templates. Even then, we
17000 cannot look up the name if the scope is not a class type; it
17001 might, for example, be a template type parameter. */
17002 dependent_p = (TYPE_P (parser->scope)
17003 && !(parser->in_declarator_p
17004 && currently_open_class (parser->scope))
17005 && dependent_type_p (parser->scope));
17006 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17007 && dependent_p)
17008 {
17009 if (tag_type)
17010 {
17011 tree type;
17012
17013 /* The resolution to Core Issue 180 says that `struct
17014 A::B' should be considered a type-name, even if `A'
17015 is dependent. */
17016 type = make_typename_type (parser->scope, name, tag_type,
17017 /*complain=*/tf_error);
17018 decl = TYPE_NAME (type);
17019 }
17020 else if (is_template
17021 && (cp_parser_next_token_ends_template_argument_p (parser)
17022 || cp_lexer_next_token_is (parser->lexer,
17023 CPP_CLOSE_PAREN)))
17024 decl = make_unbound_class_template (parser->scope,
17025 name, NULL_TREE,
17026 /*complain=*/tf_error);
17027 else
17028 decl = build_qualified_name (/*type=*/NULL_TREE,
17029 parser->scope, name,
17030 is_template);
17031 }
17032 else
17033 {
17034 tree pushed_scope = NULL_TREE;
17035
17036 /* If PARSER->SCOPE is a dependent type, then it must be a
17037 class type, and we must not be checking dependencies;
17038 otherwise, we would have processed this lookup above. So
17039 that PARSER->SCOPE is not considered a dependent base by
17040 lookup_member, we must enter the scope here. */
17041 if (dependent_p)
17042 pushed_scope = push_scope (parser->scope);
17043 /* If the PARSER->SCOPE is a template specialization, it
17044 may be instantiated during name lookup. In that case,
17045 errors may be issued. Even if we rollback the current
17046 tentative parse, those errors are valid. */
17047 decl = lookup_qualified_name (parser->scope, name,
17048 tag_type != none_type,
17049 /*complain=*/true);
17050
17051 /* If we have a single function from a using decl, pull it out. */
17052 if (decl
17053 && TREE_CODE (decl) == OVERLOAD
17054 && !really_overloaded_fn (decl))
17055 decl = OVL_FUNCTION (decl);
17056
17057 if (pushed_scope)
17058 pop_scope (pushed_scope);
17059 }
17060 parser->qualifying_scope = parser->scope;
17061 parser->object_scope = NULL_TREE;
17062 }
17063 else if (object_type)
17064 {
17065 tree object_decl = NULL_TREE;
17066 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17067 OBJECT_TYPE is not a class. */
17068 if (CLASS_TYPE_P (object_type))
17069 /* If the OBJECT_TYPE is a template specialization, it may
17070 be instantiated during name lookup. In that case, errors
17071 may be issued. Even if we rollback the current tentative
17072 parse, those errors are valid. */
17073 object_decl = lookup_member (object_type,
17074 name,
17075 /*protect=*/0,
17076 tag_type != none_type);
17077 /* Look it up in the enclosing context, too. */
17078 decl = lookup_name_real (name, tag_type != none_type,
17079 /*nonclass=*/0,
17080 /*block_p=*/true, is_namespace, flags);
17081 parser->object_scope = object_type;
17082 parser->qualifying_scope = NULL_TREE;
17083 if (object_decl)
17084 decl = object_decl;
17085 }
17086 else
17087 {
17088 decl = lookup_name_real (name, tag_type != none_type,
17089 /*nonclass=*/0,
17090 /*block_p=*/true, is_namespace, flags);
17091 parser->qualifying_scope = NULL_TREE;
17092 parser->object_scope = NULL_TREE;
17093 }
17094
17095 /* If the lookup failed, let our caller know. */
17096 if (!decl || decl == error_mark_node)
17097 return error_mark_node;
17098
17099 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17100 if (TREE_CODE (decl) == TREE_LIST)
17101 {
17102 if (ambiguous_decls)
17103 *ambiguous_decls = decl;
17104 /* The error message we have to print is too complicated for
17105 cp_parser_error, so we incorporate its actions directly. */
17106 if (!cp_parser_simulate_error (parser))
17107 {
17108 error ("%Hreference to %qD is ambiguous",
17109 &name_location, name);
17110 print_candidates (decl);
17111 }
17112 return error_mark_node;
17113 }
17114
17115 gcc_assert (DECL_P (decl)
17116 || TREE_CODE (decl) == OVERLOAD
17117 || TREE_CODE (decl) == SCOPE_REF
17118 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17119 || BASELINK_P (decl));
17120
17121 /* If we have resolved the name of a member declaration, check to
17122 see if the declaration is accessible. When the name resolves to
17123 set of overloaded functions, accessibility is checked when
17124 overload resolution is done.
17125
17126 During an explicit instantiation, access is not checked at all,
17127 as per [temp.explicit]. */
17128 if (DECL_P (decl))
17129 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17130
17131 return decl;
17132 }
17133
17134 /* Like cp_parser_lookup_name, but for use in the typical case where
17135 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17136 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17137
17138 static tree
17139 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17140 {
17141 return cp_parser_lookup_name (parser, name,
17142 none_type,
17143 /*is_template=*/false,
17144 /*is_namespace=*/false,
17145 /*check_dependency=*/true,
17146 /*ambiguous_decls=*/NULL,
17147 location);
17148 }
17149
17150 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17151 the current context, return the TYPE_DECL. If TAG_NAME_P is
17152 true, the DECL indicates the class being defined in a class-head,
17153 or declared in an elaborated-type-specifier.
17154
17155 Otherwise, return DECL. */
17156
17157 static tree
17158 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17159 {
17160 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17161 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17162
17163 struct A {
17164 template <typename T> struct B;
17165 };
17166
17167 template <typename T> struct A::B {};
17168
17169 Similarly, in an elaborated-type-specifier:
17170
17171 namespace N { struct X{}; }
17172
17173 struct A {
17174 template <typename T> friend struct N::X;
17175 };
17176
17177 However, if the DECL refers to a class type, and we are in
17178 the scope of the class, then the name lookup automatically
17179 finds the TYPE_DECL created by build_self_reference rather
17180 than a TEMPLATE_DECL. For example, in:
17181
17182 template <class T> struct S {
17183 S s;
17184 };
17185
17186 there is no need to handle such case. */
17187
17188 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17189 return DECL_TEMPLATE_RESULT (decl);
17190
17191 return decl;
17192 }
17193
17194 /* If too many, or too few, template-parameter lists apply to the
17195 declarator, issue an error message. Returns TRUE if all went well,
17196 and FALSE otherwise. */
17197
17198 static bool
17199 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17200 cp_declarator *declarator,
17201 location_t declarator_location)
17202 {
17203 unsigned num_templates;
17204
17205 /* We haven't seen any classes that involve template parameters yet. */
17206 num_templates = 0;
17207
17208 switch (declarator->kind)
17209 {
17210 case cdk_id:
17211 if (declarator->u.id.qualifying_scope)
17212 {
17213 tree scope;
17214 tree member;
17215
17216 scope = declarator->u.id.qualifying_scope;
17217 member = declarator->u.id.unqualified_name;
17218
17219 while (scope && CLASS_TYPE_P (scope))
17220 {
17221 /* You're supposed to have one `template <...>'
17222 for every template class, but you don't need one
17223 for a full specialization. For example:
17224
17225 template <class T> struct S{};
17226 template <> struct S<int> { void f(); };
17227 void S<int>::f () {}
17228
17229 is correct; there shouldn't be a `template <>' for
17230 the definition of `S<int>::f'. */
17231 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17232 /* If SCOPE does not have template information of any
17233 kind, then it is not a template, nor is it nested
17234 within a template. */
17235 break;
17236 if (explicit_class_specialization_p (scope))
17237 break;
17238 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17239 ++num_templates;
17240
17241 scope = TYPE_CONTEXT (scope);
17242 }
17243 }
17244 else if (TREE_CODE (declarator->u.id.unqualified_name)
17245 == TEMPLATE_ID_EXPR)
17246 /* If the DECLARATOR has the form `X<y>' then it uses one
17247 additional level of template parameters. */
17248 ++num_templates;
17249
17250 return cp_parser_check_template_parameters (parser,
17251 num_templates,
17252 declarator_location);
17253
17254 case cdk_function:
17255 case cdk_array:
17256 case cdk_pointer:
17257 case cdk_reference:
17258 case cdk_ptrmem:
17259 return (cp_parser_check_declarator_template_parameters
17260 (parser, declarator->declarator, declarator_location));
17261
17262 case cdk_error:
17263 return true;
17264
17265 default:
17266 gcc_unreachable ();
17267 }
17268 return false;
17269 }
17270
17271 /* NUM_TEMPLATES were used in the current declaration. If that is
17272 invalid, return FALSE and issue an error messages. Otherwise,
17273 return TRUE. */
17274
17275 static bool
17276 cp_parser_check_template_parameters (cp_parser* parser,
17277 unsigned num_templates,
17278 location_t location)
17279 {
17280 /* If there are more template classes than parameter lists, we have
17281 something like:
17282
17283 template <class T> void S<T>::R<T>::f (); */
17284 if (parser->num_template_parameter_lists < num_templates)
17285 {
17286 error ("%Htoo few template-parameter-lists", &location);
17287 return false;
17288 }
17289 /* If there are the same number of template classes and parameter
17290 lists, that's OK. */
17291 if (parser->num_template_parameter_lists == num_templates)
17292 return true;
17293 /* If there are more, but only one more, then we are referring to a
17294 member template. That's OK too. */
17295 if (parser->num_template_parameter_lists == num_templates + 1)
17296 return true;
17297 /* Otherwise, there are too many template parameter lists. We have
17298 something like:
17299
17300 template <class T> template <class U> void S::f(); */
17301 error ("%Htoo many template-parameter-lists", &location);
17302 return false;
17303 }
17304
17305 /* Parse an optional `::' token indicating that the following name is
17306 from the global namespace. If so, PARSER->SCOPE is set to the
17307 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17308 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17309 Returns the new value of PARSER->SCOPE, if the `::' token is
17310 present, and NULL_TREE otherwise. */
17311
17312 static tree
17313 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17314 {
17315 cp_token *token;
17316
17317 /* Peek at the next token. */
17318 token = cp_lexer_peek_token (parser->lexer);
17319 /* If we're looking at a `::' token then we're starting from the
17320 global namespace, not our current location. */
17321 if (token->type == CPP_SCOPE)
17322 {
17323 /* Consume the `::' token. */
17324 cp_lexer_consume_token (parser->lexer);
17325 /* Set the SCOPE so that we know where to start the lookup. */
17326 parser->scope = global_namespace;
17327 parser->qualifying_scope = global_namespace;
17328 parser->object_scope = NULL_TREE;
17329
17330 return parser->scope;
17331 }
17332 else if (!current_scope_valid_p)
17333 {
17334 parser->scope = NULL_TREE;
17335 parser->qualifying_scope = NULL_TREE;
17336 parser->object_scope = NULL_TREE;
17337 }
17338
17339 return NULL_TREE;
17340 }
17341
17342 /* Returns TRUE if the upcoming token sequence is the start of a
17343 constructor declarator. If FRIEND_P is true, the declarator is
17344 preceded by the `friend' specifier. */
17345
17346 static bool
17347 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17348 {
17349 bool constructor_p;
17350 tree type_decl = NULL_TREE;
17351 bool nested_name_p;
17352 cp_token *next_token;
17353
17354 /* The common case is that this is not a constructor declarator, so
17355 try to avoid doing lots of work if at all possible. It's not
17356 valid declare a constructor at function scope. */
17357 if (parser->in_function_body)
17358 return false;
17359 /* And only certain tokens can begin a constructor declarator. */
17360 next_token = cp_lexer_peek_token (parser->lexer);
17361 if (next_token->type != CPP_NAME
17362 && next_token->type != CPP_SCOPE
17363 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17364 && next_token->type != CPP_TEMPLATE_ID)
17365 return false;
17366
17367 /* Parse tentatively; we are going to roll back all of the tokens
17368 consumed here. */
17369 cp_parser_parse_tentatively (parser);
17370 /* Assume that we are looking at a constructor declarator. */
17371 constructor_p = true;
17372
17373 /* Look for the optional `::' operator. */
17374 cp_parser_global_scope_opt (parser,
17375 /*current_scope_valid_p=*/false);
17376 /* Look for the nested-name-specifier. */
17377 nested_name_p
17378 = (cp_parser_nested_name_specifier_opt (parser,
17379 /*typename_keyword_p=*/false,
17380 /*check_dependency_p=*/false,
17381 /*type_p=*/false,
17382 /*is_declaration=*/false)
17383 != NULL_TREE);
17384 /* Outside of a class-specifier, there must be a
17385 nested-name-specifier. */
17386 if (!nested_name_p &&
17387 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17388 || friend_p))
17389 constructor_p = false;
17390 /* If we still think that this might be a constructor-declarator,
17391 look for a class-name. */
17392 if (constructor_p)
17393 {
17394 /* If we have:
17395
17396 template <typename T> struct S { S(); };
17397 template <typename T> S<T>::S ();
17398
17399 we must recognize that the nested `S' names a class.
17400 Similarly, for:
17401
17402 template <typename T> S<T>::S<T> ();
17403
17404 we must recognize that the nested `S' names a template. */
17405 type_decl = cp_parser_class_name (parser,
17406 /*typename_keyword_p=*/false,
17407 /*template_keyword_p=*/false,
17408 none_type,
17409 /*check_dependency_p=*/false,
17410 /*class_head_p=*/false,
17411 /*is_declaration=*/false);
17412 /* If there was no class-name, then this is not a constructor. */
17413 constructor_p = !cp_parser_error_occurred (parser);
17414 }
17415
17416 /* If we're still considering a constructor, we have to see a `(',
17417 to begin the parameter-declaration-clause, followed by either a
17418 `)', an `...', or a decl-specifier. We need to check for a
17419 type-specifier to avoid being fooled into thinking that:
17420
17421 S::S (f) (int);
17422
17423 is a constructor. (It is actually a function named `f' that
17424 takes one parameter (of type `int') and returns a value of type
17425 `S::S'. */
17426 if (constructor_p
17427 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17428 {
17429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17430 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17431 /* A parameter declaration begins with a decl-specifier,
17432 which is either the "attribute" keyword, a storage class
17433 specifier, or (usually) a type-specifier. */
17434 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17435 {
17436 tree type;
17437 tree pushed_scope = NULL_TREE;
17438 unsigned saved_num_template_parameter_lists;
17439
17440 /* Names appearing in the type-specifier should be looked up
17441 in the scope of the class. */
17442 if (current_class_type)
17443 type = NULL_TREE;
17444 else
17445 {
17446 type = TREE_TYPE (type_decl);
17447 if (TREE_CODE (type) == TYPENAME_TYPE)
17448 {
17449 type = resolve_typename_type (type,
17450 /*only_current_p=*/false);
17451 if (TREE_CODE (type) == TYPENAME_TYPE)
17452 {
17453 cp_parser_abort_tentative_parse (parser);
17454 return false;
17455 }
17456 }
17457 pushed_scope = push_scope (type);
17458 }
17459
17460 /* Inside the constructor parameter list, surrounding
17461 template-parameter-lists do not apply. */
17462 saved_num_template_parameter_lists
17463 = parser->num_template_parameter_lists;
17464 parser->num_template_parameter_lists = 0;
17465
17466 /* Look for the type-specifier. */
17467 cp_parser_type_specifier (parser,
17468 CP_PARSER_FLAGS_NONE,
17469 /*decl_specs=*/NULL,
17470 /*is_declarator=*/true,
17471 /*declares_class_or_enum=*/NULL,
17472 /*is_cv_qualifier=*/NULL);
17473
17474 parser->num_template_parameter_lists
17475 = saved_num_template_parameter_lists;
17476
17477 /* Leave the scope of the class. */
17478 if (pushed_scope)
17479 pop_scope (pushed_scope);
17480
17481 constructor_p = !cp_parser_error_occurred (parser);
17482 }
17483 }
17484 else
17485 constructor_p = false;
17486 /* We did not really want to consume any tokens. */
17487 cp_parser_abort_tentative_parse (parser);
17488
17489 return constructor_p;
17490 }
17491
17492 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17493 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17494 they must be performed once we are in the scope of the function.
17495
17496 Returns the function defined. */
17497
17498 static tree
17499 cp_parser_function_definition_from_specifiers_and_declarator
17500 (cp_parser* parser,
17501 cp_decl_specifier_seq *decl_specifiers,
17502 tree attributes,
17503 const cp_declarator *declarator)
17504 {
17505 tree fn;
17506 bool success_p;
17507
17508 /* Begin the function-definition. */
17509 success_p = start_function (decl_specifiers, declarator, attributes);
17510
17511 /* The things we're about to see are not directly qualified by any
17512 template headers we've seen thus far. */
17513 reset_specialization ();
17514
17515 /* If there were names looked up in the decl-specifier-seq that we
17516 did not check, check them now. We must wait until we are in the
17517 scope of the function to perform the checks, since the function
17518 might be a friend. */
17519 perform_deferred_access_checks ();
17520
17521 if (!success_p)
17522 {
17523 /* Skip the entire function. */
17524 cp_parser_skip_to_end_of_block_or_statement (parser);
17525 fn = error_mark_node;
17526 }
17527 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17528 {
17529 /* Seen already, skip it. An error message has already been output. */
17530 cp_parser_skip_to_end_of_block_or_statement (parser);
17531 fn = current_function_decl;
17532 current_function_decl = NULL_TREE;
17533 /* If this is a function from a class, pop the nested class. */
17534 if (current_class_name)
17535 pop_nested_class ();
17536 }
17537 else
17538 fn = cp_parser_function_definition_after_declarator (parser,
17539 /*inline_p=*/false);
17540
17541 return fn;
17542 }
17543
17544 /* Parse the part of a function-definition that follows the
17545 declarator. INLINE_P is TRUE iff this function is an inline
17546 function defined with a class-specifier.
17547
17548 Returns the function defined. */
17549
17550 static tree
17551 cp_parser_function_definition_after_declarator (cp_parser* parser,
17552 bool inline_p)
17553 {
17554 tree fn;
17555 bool ctor_initializer_p = false;
17556 bool saved_in_unbraced_linkage_specification_p;
17557 bool saved_in_function_body;
17558 unsigned saved_num_template_parameter_lists;
17559 cp_token *token;
17560
17561 saved_in_function_body = parser->in_function_body;
17562 parser->in_function_body = true;
17563 /* If the next token is `return', then the code may be trying to
17564 make use of the "named return value" extension that G++ used to
17565 support. */
17566 token = cp_lexer_peek_token (parser->lexer);
17567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17568 {
17569 /* Consume the `return' keyword. */
17570 cp_lexer_consume_token (parser->lexer);
17571 /* Look for the identifier that indicates what value is to be
17572 returned. */
17573 cp_parser_identifier (parser);
17574 /* Issue an error message. */
17575 error ("%Hnamed return values are no longer supported",
17576 &token->location);
17577 /* Skip tokens until we reach the start of the function body. */
17578 while (true)
17579 {
17580 cp_token *token = cp_lexer_peek_token (parser->lexer);
17581 if (token->type == CPP_OPEN_BRACE
17582 || token->type == CPP_EOF
17583 || token->type == CPP_PRAGMA_EOL)
17584 break;
17585 cp_lexer_consume_token (parser->lexer);
17586 }
17587 }
17588 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17589 anything declared inside `f'. */
17590 saved_in_unbraced_linkage_specification_p
17591 = parser->in_unbraced_linkage_specification_p;
17592 parser->in_unbraced_linkage_specification_p = false;
17593 /* Inside the function, surrounding template-parameter-lists do not
17594 apply. */
17595 saved_num_template_parameter_lists
17596 = parser->num_template_parameter_lists;
17597 parser->num_template_parameter_lists = 0;
17598 /* If the next token is `try', then we are looking at a
17599 function-try-block. */
17600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17601 ctor_initializer_p = cp_parser_function_try_block (parser);
17602 /* A function-try-block includes the function-body, so we only do
17603 this next part if we're not processing a function-try-block. */
17604 else
17605 ctor_initializer_p
17606 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17607
17608 /* Finish the function. */
17609 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17610 (inline_p ? 2 : 0));
17611 /* Generate code for it, if necessary. */
17612 expand_or_defer_fn (fn);
17613 /* Restore the saved values. */
17614 parser->in_unbraced_linkage_specification_p
17615 = saved_in_unbraced_linkage_specification_p;
17616 parser->num_template_parameter_lists
17617 = saved_num_template_parameter_lists;
17618 parser->in_function_body = saved_in_function_body;
17619
17620 return fn;
17621 }
17622
17623 /* Parse a template-declaration, assuming that the `export' (and
17624 `extern') keywords, if present, has already been scanned. MEMBER_P
17625 is as for cp_parser_template_declaration. */
17626
17627 static void
17628 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17629 {
17630 tree decl = NULL_TREE;
17631 VEC (deferred_access_check,gc) *checks;
17632 tree parameter_list;
17633 bool friend_p = false;
17634 bool need_lang_pop;
17635 cp_token *token;
17636
17637 /* Look for the `template' keyword. */
17638 token = cp_lexer_peek_token (parser->lexer);
17639 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17640 return;
17641
17642 /* And the `<'. */
17643 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17644 return;
17645 if (at_class_scope_p () && current_function_decl)
17646 {
17647 /* 14.5.2.2 [temp.mem]
17648
17649 A local class shall not have member templates. */
17650 error ("%Hinvalid declaration of member template in local class",
17651 &token->location);
17652 cp_parser_skip_to_end_of_block_or_statement (parser);
17653 return;
17654 }
17655 /* [temp]
17656
17657 A template ... shall not have C linkage. */
17658 if (current_lang_name == lang_name_c)
17659 {
17660 error ("%Htemplate with C linkage", &token->location);
17661 /* Give it C++ linkage to avoid confusing other parts of the
17662 front end. */
17663 push_lang_context (lang_name_cplusplus);
17664 need_lang_pop = true;
17665 }
17666 else
17667 need_lang_pop = false;
17668
17669 /* We cannot perform access checks on the template parameter
17670 declarations until we know what is being declared, just as we
17671 cannot check the decl-specifier list. */
17672 push_deferring_access_checks (dk_deferred);
17673
17674 /* If the next token is `>', then we have an invalid
17675 specialization. Rather than complain about an invalid template
17676 parameter, issue an error message here. */
17677 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17678 {
17679 cp_parser_error (parser, "invalid explicit specialization");
17680 begin_specialization ();
17681 parameter_list = NULL_TREE;
17682 }
17683 else
17684 /* Parse the template parameters. */
17685 parameter_list = cp_parser_template_parameter_list (parser);
17686
17687 /* Get the deferred access checks from the parameter list. These
17688 will be checked once we know what is being declared, as for a
17689 member template the checks must be performed in the scope of the
17690 class containing the member. */
17691 checks = get_deferred_access_checks ();
17692
17693 /* Look for the `>'. */
17694 cp_parser_skip_to_end_of_template_parameter_list (parser);
17695 /* We just processed one more parameter list. */
17696 ++parser->num_template_parameter_lists;
17697 /* If the next token is `template', there are more template
17698 parameters. */
17699 if (cp_lexer_next_token_is_keyword (parser->lexer,
17700 RID_TEMPLATE))
17701 cp_parser_template_declaration_after_export (parser, member_p);
17702 else
17703 {
17704 /* There are no access checks when parsing a template, as we do not
17705 know if a specialization will be a friend. */
17706 push_deferring_access_checks (dk_no_check);
17707 token = cp_lexer_peek_token (parser->lexer);
17708 decl = cp_parser_single_declaration (parser,
17709 checks,
17710 member_p,
17711 /*explicit_specialization_p=*/false,
17712 &friend_p);
17713 pop_deferring_access_checks ();
17714
17715 /* If this is a member template declaration, let the front
17716 end know. */
17717 if (member_p && !friend_p && decl)
17718 {
17719 if (TREE_CODE (decl) == TYPE_DECL)
17720 cp_parser_check_access_in_redeclaration (decl, token->location);
17721
17722 decl = finish_member_template_decl (decl);
17723 }
17724 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17725 make_friend_class (current_class_type, TREE_TYPE (decl),
17726 /*complain=*/true);
17727 }
17728 /* We are done with the current parameter list. */
17729 --parser->num_template_parameter_lists;
17730
17731 pop_deferring_access_checks ();
17732
17733 /* Finish up. */
17734 finish_template_decl (parameter_list);
17735
17736 /* Register member declarations. */
17737 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17738 finish_member_declaration (decl);
17739 /* For the erroneous case of a template with C linkage, we pushed an
17740 implicit C++ linkage scope; exit that scope now. */
17741 if (need_lang_pop)
17742 pop_lang_context ();
17743 /* If DECL is a function template, we must return to parse it later.
17744 (Even though there is no definition, there might be default
17745 arguments that need handling.) */
17746 if (member_p && decl
17747 && (TREE_CODE (decl) == FUNCTION_DECL
17748 || DECL_FUNCTION_TEMPLATE_P (decl)))
17749 TREE_VALUE (parser->unparsed_functions_queues)
17750 = tree_cons (NULL_TREE, decl,
17751 TREE_VALUE (parser->unparsed_functions_queues));
17752 }
17753
17754 /* Perform the deferred access checks from a template-parameter-list.
17755 CHECKS is a TREE_LIST of access checks, as returned by
17756 get_deferred_access_checks. */
17757
17758 static void
17759 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17760 {
17761 ++processing_template_parmlist;
17762 perform_access_checks (checks);
17763 --processing_template_parmlist;
17764 }
17765
17766 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17767 `function-definition' sequence. MEMBER_P is true, this declaration
17768 appears in a class scope.
17769
17770 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17771 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17772
17773 static tree
17774 cp_parser_single_declaration (cp_parser* parser,
17775 VEC (deferred_access_check,gc)* checks,
17776 bool member_p,
17777 bool explicit_specialization_p,
17778 bool* friend_p)
17779 {
17780 int declares_class_or_enum;
17781 tree decl = NULL_TREE;
17782 cp_decl_specifier_seq decl_specifiers;
17783 bool function_definition_p = false;
17784 cp_token *decl_spec_token_start;
17785
17786 /* This function is only used when processing a template
17787 declaration. */
17788 gcc_assert (innermost_scope_kind () == sk_template_parms
17789 || innermost_scope_kind () == sk_template_spec);
17790
17791 /* Defer access checks until we know what is being declared. */
17792 push_deferring_access_checks (dk_deferred);
17793
17794 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17795 alternative. */
17796 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17797 cp_parser_decl_specifier_seq (parser,
17798 CP_PARSER_FLAGS_OPTIONAL,
17799 &decl_specifiers,
17800 &declares_class_or_enum);
17801 if (friend_p)
17802 *friend_p = cp_parser_friend_p (&decl_specifiers);
17803
17804 /* There are no template typedefs. */
17805 if (decl_specifiers.specs[(int) ds_typedef])
17806 {
17807 error ("%Htemplate declaration of %qs",
17808 &decl_spec_token_start->location, "typedef");
17809 decl = error_mark_node;
17810 }
17811
17812 /* Gather up the access checks that occurred the
17813 decl-specifier-seq. */
17814 stop_deferring_access_checks ();
17815
17816 /* Check for the declaration of a template class. */
17817 if (declares_class_or_enum)
17818 {
17819 if (cp_parser_declares_only_class_p (parser))
17820 {
17821 decl = shadow_tag (&decl_specifiers);
17822
17823 /* In this case:
17824
17825 struct C {
17826 friend template <typename T> struct A<T>::B;
17827 };
17828
17829 A<T>::B will be represented by a TYPENAME_TYPE, and
17830 therefore not recognized by shadow_tag. */
17831 if (friend_p && *friend_p
17832 && !decl
17833 && decl_specifiers.type
17834 && TYPE_P (decl_specifiers.type))
17835 decl = decl_specifiers.type;
17836
17837 if (decl && decl != error_mark_node)
17838 decl = TYPE_NAME (decl);
17839 else
17840 decl = error_mark_node;
17841
17842 /* Perform access checks for template parameters. */
17843 cp_parser_perform_template_parameter_access_checks (checks);
17844 }
17845 }
17846 /* If it's not a template class, try for a template function. If
17847 the next token is a `;', then this declaration does not declare
17848 anything. But, if there were errors in the decl-specifiers, then
17849 the error might well have come from an attempted class-specifier.
17850 In that case, there's no need to warn about a missing declarator. */
17851 if (!decl
17852 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17853 || decl_specifiers.type != error_mark_node))
17854 {
17855 decl = cp_parser_init_declarator (parser,
17856 &decl_specifiers,
17857 checks,
17858 /*function_definition_allowed_p=*/true,
17859 member_p,
17860 declares_class_or_enum,
17861 &function_definition_p);
17862
17863 /* 7.1.1-1 [dcl.stc]
17864
17865 A storage-class-specifier shall not be specified in an explicit
17866 specialization... */
17867 if (decl
17868 && explicit_specialization_p
17869 && decl_specifiers.storage_class != sc_none)
17870 {
17871 error ("%Hexplicit template specialization cannot have a storage class",
17872 &decl_spec_token_start->location);
17873 decl = error_mark_node;
17874 }
17875 }
17876
17877 pop_deferring_access_checks ();
17878
17879 /* Clear any current qualification; whatever comes next is the start
17880 of something new. */
17881 parser->scope = NULL_TREE;
17882 parser->qualifying_scope = NULL_TREE;
17883 parser->object_scope = NULL_TREE;
17884 /* Look for a trailing `;' after the declaration. */
17885 if (!function_definition_p
17886 && (decl == error_mark_node
17887 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17888 cp_parser_skip_to_end_of_block_or_statement (parser);
17889
17890 return decl;
17891 }
17892
17893 /* Parse a cast-expression that is not the operand of a unary "&". */
17894
17895 static tree
17896 cp_parser_simple_cast_expression (cp_parser *parser)
17897 {
17898 return cp_parser_cast_expression (parser, /*address_p=*/false,
17899 /*cast_p=*/false, NULL);
17900 }
17901
17902 /* Parse a functional cast to TYPE. Returns an expression
17903 representing the cast. */
17904
17905 static tree
17906 cp_parser_functional_cast (cp_parser* parser, tree type)
17907 {
17908 tree expression_list;
17909 tree cast;
17910 bool nonconst_p;
17911
17912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17913 {
17914 maybe_warn_cpp0x ("extended initializer lists");
17915 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17916 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17917 if (TREE_CODE (type) == TYPE_DECL)
17918 type = TREE_TYPE (type);
17919 return finish_compound_literal (type, expression_list);
17920 }
17921
17922 expression_list
17923 = cp_parser_parenthesized_expression_list (parser, false,
17924 /*cast_p=*/true,
17925 /*allow_expansion_p=*/true,
17926 /*non_constant_p=*/NULL);
17927
17928 cast = build_functional_cast (type, expression_list,
17929 tf_warning_or_error);
17930 /* [expr.const]/1: In an integral constant expression "only type
17931 conversions to integral or enumeration type can be used". */
17932 if (TREE_CODE (type) == TYPE_DECL)
17933 type = TREE_TYPE (type);
17934 if (cast != error_mark_node
17935 && !cast_valid_in_integral_constant_expression_p (type)
17936 && (cp_parser_non_integral_constant_expression
17937 (parser, "a call to a constructor")))
17938 return error_mark_node;
17939 return cast;
17940 }
17941
17942 /* Save the tokens that make up the body of a member function defined
17943 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17944 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17945 specifiers applied to the declaration. Returns the FUNCTION_DECL
17946 for the member function. */
17947
17948 static tree
17949 cp_parser_save_member_function_body (cp_parser* parser,
17950 cp_decl_specifier_seq *decl_specifiers,
17951 cp_declarator *declarator,
17952 tree attributes)
17953 {
17954 cp_token *first;
17955 cp_token *last;
17956 tree fn;
17957
17958 /* Create the function-declaration. */
17959 fn = start_method (decl_specifiers, declarator, attributes);
17960 /* If something went badly wrong, bail out now. */
17961 if (fn == error_mark_node)
17962 {
17963 /* If there's a function-body, skip it. */
17964 if (cp_parser_token_starts_function_definition_p
17965 (cp_lexer_peek_token (parser->lexer)))
17966 cp_parser_skip_to_end_of_block_or_statement (parser);
17967 return error_mark_node;
17968 }
17969
17970 /* Remember it, if there default args to post process. */
17971 cp_parser_save_default_args (parser, fn);
17972
17973 /* Save away the tokens that make up the body of the
17974 function. */
17975 first = parser->lexer->next_token;
17976 /* We can have braced-init-list mem-initializers before the fn body. */
17977 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17978 {
17979 cp_lexer_consume_token (parser->lexer);
17980 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17981 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17982 {
17983 /* cache_group will stop after an un-nested { } pair, too. */
17984 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17985 break;
17986
17987 /* variadic mem-inits have ... after the ')'. */
17988 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17989 cp_lexer_consume_token (parser->lexer);
17990 }
17991 }
17992 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17993 /* Handle function try blocks. */
17994 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17995 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17996 last = parser->lexer->next_token;
17997
17998 /* Save away the inline definition; we will process it when the
17999 class is complete. */
18000 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18001 DECL_PENDING_INLINE_P (fn) = 1;
18002
18003 /* We need to know that this was defined in the class, so that
18004 friend templates are handled correctly. */
18005 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18006
18007 /* We're done with the inline definition. */
18008 finish_method (fn);
18009
18010 /* Add FN to the queue of functions to be parsed later. */
18011 TREE_VALUE (parser->unparsed_functions_queues)
18012 = tree_cons (NULL_TREE, fn,
18013 TREE_VALUE (parser->unparsed_functions_queues));
18014
18015 return fn;
18016 }
18017
18018 /* Parse a template-argument-list, as well as the trailing ">" (but
18019 not the opening ">"). See cp_parser_template_argument_list for the
18020 return value. */
18021
18022 static tree
18023 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18024 {
18025 tree arguments;
18026 tree saved_scope;
18027 tree saved_qualifying_scope;
18028 tree saved_object_scope;
18029 bool saved_greater_than_is_operator_p;
18030 bool saved_skip_evaluation;
18031
18032 /* [temp.names]
18033
18034 When parsing a template-id, the first non-nested `>' is taken as
18035 the end of the template-argument-list rather than a greater-than
18036 operator. */
18037 saved_greater_than_is_operator_p
18038 = parser->greater_than_is_operator_p;
18039 parser->greater_than_is_operator_p = false;
18040 /* Parsing the argument list may modify SCOPE, so we save it
18041 here. */
18042 saved_scope = parser->scope;
18043 saved_qualifying_scope = parser->qualifying_scope;
18044 saved_object_scope = parser->object_scope;
18045 /* We need to evaluate the template arguments, even though this
18046 template-id may be nested within a "sizeof". */
18047 saved_skip_evaluation = skip_evaluation;
18048 skip_evaluation = false;
18049 /* Parse the template-argument-list itself. */
18050 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18051 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18052 arguments = NULL_TREE;
18053 else
18054 arguments = cp_parser_template_argument_list (parser);
18055 /* Look for the `>' that ends the template-argument-list. If we find
18056 a '>>' instead, it's probably just a typo. */
18057 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18058 {
18059 if (cxx_dialect != cxx98)
18060 {
18061 /* In C++0x, a `>>' in a template argument list or cast
18062 expression is considered to be two separate `>'
18063 tokens. So, change the current token to a `>', but don't
18064 consume it: it will be consumed later when the outer
18065 template argument list (or cast expression) is parsed.
18066 Note that this replacement of `>' for `>>' is necessary
18067 even if we are parsing tentatively: in the tentative
18068 case, after calling
18069 cp_parser_enclosed_template_argument_list we will always
18070 throw away all of the template arguments and the first
18071 closing `>', either because the template argument list
18072 was erroneous or because we are replacing those tokens
18073 with a CPP_TEMPLATE_ID token. The second `>' (which will
18074 not have been thrown away) is needed either to close an
18075 outer template argument list or to complete a new-style
18076 cast. */
18077 cp_token *token = cp_lexer_peek_token (parser->lexer);
18078 token->type = CPP_GREATER;
18079 }
18080 else if (!saved_greater_than_is_operator_p)
18081 {
18082 /* If we're in a nested template argument list, the '>>' has
18083 to be a typo for '> >'. We emit the error message, but we
18084 continue parsing and we push a '>' as next token, so that
18085 the argument list will be parsed correctly. Note that the
18086 global source location is still on the token before the
18087 '>>', so we need to say explicitly where we want it. */
18088 cp_token *token = cp_lexer_peek_token (parser->lexer);
18089 error ("%H%<>>%> should be %<> >%> "
18090 "within a nested template argument list",
18091 &token->location);
18092
18093 token->type = CPP_GREATER;
18094 }
18095 else
18096 {
18097 /* If this is not a nested template argument list, the '>>'
18098 is a typo for '>'. Emit an error message and continue.
18099 Same deal about the token location, but here we can get it
18100 right by consuming the '>>' before issuing the diagnostic. */
18101 cp_token *token = cp_lexer_consume_token (parser->lexer);
18102 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18103 "a template argument list", &token->location);
18104 }
18105 }
18106 else
18107 cp_parser_skip_to_end_of_template_parameter_list (parser);
18108 /* The `>' token might be a greater-than operator again now. */
18109 parser->greater_than_is_operator_p
18110 = saved_greater_than_is_operator_p;
18111 /* Restore the SAVED_SCOPE. */
18112 parser->scope = saved_scope;
18113 parser->qualifying_scope = saved_qualifying_scope;
18114 parser->object_scope = saved_object_scope;
18115 skip_evaluation = saved_skip_evaluation;
18116
18117 return arguments;
18118 }
18119
18120 /* MEMBER_FUNCTION is a member function, or a friend. If default
18121 arguments, or the body of the function have not yet been parsed,
18122 parse them now. */
18123
18124 static void
18125 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18126 {
18127 /* If this member is a template, get the underlying
18128 FUNCTION_DECL. */
18129 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18130 member_function = DECL_TEMPLATE_RESULT (member_function);
18131
18132 /* There should not be any class definitions in progress at this
18133 point; the bodies of members are only parsed outside of all class
18134 definitions. */
18135 gcc_assert (parser->num_classes_being_defined == 0);
18136 /* While we're parsing the member functions we might encounter more
18137 classes. We want to handle them right away, but we don't want
18138 them getting mixed up with functions that are currently in the
18139 queue. */
18140 parser->unparsed_functions_queues
18141 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18142
18143 /* Make sure that any template parameters are in scope. */
18144 maybe_begin_member_template_processing (member_function);
18145
18146 /* If the body of the function has not yet been parsed, parse it
18147 now. */
18148 if (DECL_PENDING_INLINE_P (member_function))
18149 {
18150 tree function_scope;
18151 cp_token_cache *tokens;
18152
18153 /* The function is no longer pending; we are processing it. */
18154 tokens = DECL_PENDING_INLINE_INFO (member_function);
18155 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18156 DECL_PENDING_INLINE_P (member_function) = 0;
18157
18158 /* If this is a local class, enter the scope of the containing
18159 function. */
18160 function_scope = current_function_decl;
18161 if (function_scope)
18162 push_function_context ();
18163
18164 /* Push the body of the function onto the lexer stack. */
18165 cp_parser_push_lexer_for_tokens (parser, tokens);
18166
18167 /* Let the front end know that we going to be defining this
18168 function. */
18169 start_preparsed_function (member_function, NULL_TREE,
18170 SF_PRE_PARSED | SF_INCLASS_INLINE);
18171
18172 /* Don't do access checking if it is a templated function. */
18173 if (processing_template_decl)
18174 push_deferring_access_checks (dk_no_check);
18175
18176 /* Now, parse the body of the function. */
18177 cp_parser_function_definition_after_declarator (parser,
18178 /*inline_p=*/true);
18179
18180 if (processing_template_decl)
18181 pop_deferring_access_checks ();
18182
18183 /* Leave the scope of the containing function. */
18184 if (function_scope)
18185 pop_function_context ();
18186 cp_parser_pop_lexer (parser);
18187 }
18188
18189 /* Remove any template parameters from the symbol table. */
18190 maybe_end_member_template_processing ();
18191
18192 /* Restore the queue. */
18193 parser->unparsed_functions_queues
18194 = TREE_CHAIN (parser->unparsed_functions_queues);
18195 }
18196
18197 /* If DECL contains any default args, remember it on the unparsed
18198 functions queue. */
18199
18200 static void
18201 cp_parser_save_default_args (cp_parser* parser, tree decl)
18202 {
18203 tree probe;
18204
18205 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18206 probe;
18207 probe = TREE_CHAIN (probe))
18208 if (TREE_PURPOSE (probe))
18209 {
18210 TREE_PURPOSE (parser->unparsed_functions_queues)
18211 = tree_cons (current_class_type, decl,
18212 TREE_PURPOSE (parser->unparsed_functions_queues));
18213 break;
18214 }
18215 }
18216
18217 /* FN is a FUNCTION_DECL which may contains a parameter with an
18218 unparsed DEFAULT_ARG. Parse the default args now. This function
18219 assumes that the current scope is the scope in which the default
18220 argument should be processed. */
18221
18222 static void
18223 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18224 {
18225 bool saved_local_variables_forbidden_p;
18226 tree parm;
18227
18228 /* While we're parsing the default args, we might (due to the
18229 statement expression extension) encounter more classes. We want
18230 to handle them right away, but we don't want them getting mixed
18231 up with default args that are currently in the queue. */
18232 parser->unparsed_functions_queues
18233 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18234
18235 /* Local variable names (and the `this' keyword) may not appear
18236 in a default argument. */
18237 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18238 parser->local_variables_forbidden_p = true;
18239
18240 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18241 parm;
18242 parm = TREE_CHAIN (parm))
18243 {
18244 cp_token_cache *tokens;
18245 tree default_arg = TREE_PURPOSE (parm);
18246 tree parsed_arg;
18247 VEC(tree,gc) *insts;
18248 tree copy;
18249 unsigned ix;
18250
18251 if (!default_arg)
18252 continue;
18253
18254 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18255 /* This can happen for a friend declaration for a function
18256 already declared with default arguments. */
18257 continue;
18258
18259 /* Push the saved tokens for the default argument onto the parser's
18260 lexer stack. */
18261 tokens = DEFARG_TOKENS (default_arg);
18262 cp_parser_push_lexer_for_tokens (parser, tokens);
18263
18264 /* Parse the assignment-expression. */
18265 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18266
18267 if (!processing_template_decl)
18268 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18269
18270 TREE_PURPOSE (parm) = parsed_arg;
18271
18272 /* Update any instantiations we've already created. */
18273 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18274 VEC_iterate (tree, insts, ix, copy); ix++)
18275 TREE_PURPOSE (copy) = parsed_arg;
18276
18277 /* If the token stream has not been completely used up, then
18278 there was extra junk after the end of the default
18279 argument. */
18280 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18281 cp_parser_error (parser, "expected %<,%>");
18282
18283 /* Revert to the main lexer. */
18284 cp_parser_pop_lexer (parser);
18285 }
18286
18287 /* Make sure no default arg is missing. */
18288 check_default_args (fn);
18289
18290 /* Restore the state of local_variables_forbidden_p. */
18291 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18292
18293 /* Restore the queue. */
18294 parser->unparsed_functions_queues
18295 = TREE_CHAIN (parser->unparsed_functions_queues);
18296 }
18297
18298 /* Parse the operand of `sizeof' (or a similar operator). Returns
18299 either a TYPE or an expression, depending on the form of the
18300 input. The KEYWORD indicates which kind of expression we have
18301 encountered. */
18302
18303 static tree
18304 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18305 {
18306 tree expr = NULL_TREE;
18307 const char *saved_message;
18308 char *tmp;
18309 bool saved_integral_constant_expression_p;
18310 bool saved_non_integral_constant_expression_p;
18311 bool pack_expansion_p = false;
18312
18313 /* Types cannot be defined in a `sizeof' expression. Save away the
18314 old message. */
18315 saved_message = parser->type_definition_forbidden_message;
18316 /* And create the new one. */
18317 tmp = concat ("types may not be defined in %<",
18318 IDENTIFIER_POINTER (ridpointers[keyword]),
18319 "%> expressions", NULL);
18320 parser->type_definition_forbidden_message = tmp;
18321
18322 /* The restrictions on constant-expressions do not apply inside
18323 sizeof expressions. */
18324 saved_integral_constant_expression_p
18325 = parser->integral_constant_expression_p;
18326 saved_non_integral_constant_expression_p
18327 = parser->non_integral_constant_expression_p;
18328 parser->integral_constant_expression_p = false;
18329
18330 /* If it's a `...', then we are computing the length of a parameter
18331 pack. */
18332 if (keyword == RID_SIZEOF
18333 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18334 {
18335 /* Consume the `...'. */
18336 cp_lexer_consume_token (parser->lexer);
18337 maybe_warn_variadic_templates ();
18338
18339 /* Note that this is an expansion. */
18340 pack_expansion_p = true;
18341 }
18342
18343 /* Do not actually evaluate the expression. */
18344 ++skip_evaluation;
18345 /* If it's a `(', then we might be looking at the type-id
18346 construction. */
18347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18348 {
18349 tree type;
18350 bool saved_in_type_id_in_expr_p;
18351
18352 /* We can't be sure yet whether we're looking at a type-id or an
18353 expression. */
18354 cp_parser_parse_tentatively (parser);
18355 /* Consume the `('. */
18356 cp_lexer_consume_token (parser->lexer);
18357 /* Parse the type-id. */
18358 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18359 parser->in_type_id_in_expr_p = true;
18360 type = cp_parser_type_id (parser);
18361 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18362 /* Now, look for the trailing `)'. */
18363 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18364 /* If all went well, then we're done. */
18365 if (cp_parser_parse_definitely (parser))
18366 {
18367 cp_decl_specifier_seq decl_specs;
18368
18369 /* Build a trivial decl-specifier-seq. */
18370 clear_decl_specs (&decl_specs);
18371 decl_specs.type = type;
18372
18373 /* Call grokdeclarator to figure out what type this is. */
18374 expr = grokdeclarator (NULL,
18375 &decl_specs,
18376 TYPENAME,
18377 /*initialized=*/0,
18378 /*attrlist=*/NULL);
18379 }
18380 }
18381
18382 /* If the type-id production did not work out, then we must be
18383 looking at the unary-expression production. */
18384 if (!expr)
18385 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18386 /*cast_p=*/false, NULL);
18387
18388 if (pack_expansion_p)
18389 /* Build a pack expansion. */
18390 expr = make_pack_expansion (expr);
18391
18392 /* Go back to evaluating expressions. */
18393 --skip_evaluation;
18394
18395 /* Free the message we created. */
18396 free (tmp);
18397 /* And restore the old one. */
18398 parser->type_definition_forbidden_message = saved_message;
18399 parser->integral_constant_expression_p
18400 = saved_integral_constant_expression_p;
18401 parser->non_integral_constant_expression_p
18402 = saved_non_integral_constant_expression_p;
18403
18404 return expr;
18405 }
18406
18407 /* If the current declaration has no declarator, return true. */
18408
18409 static bool
18410 cp_parser_declares_only_class_p (cp_parser *parser)
18411 {
18412 /* If the next token is a `;' or a `,' then there is no
18413 declarator. */
18414 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18415 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18416 }
18417
18418 /* Update the DECL_SPECS to reflect the storage class indicated by
18419 KEYWORD. */
18420
18421 static void
18422 cp_parser_set_storage_class (cp_parser *parser,
18423 cp_decl_specifier_seq *decl_specs,
18424 enum rid keyword,
18425 location_t location)
18426 {
18427 cp_storage_class storage_class;
18428
18429 if (parser->in_unbraced_linkage_specification_p)
18430 {
18431 error ("%Hinvalid use of %qD in linkage specification",
18432 &location, ridpointers[keyword]);
18433 return;
18434 }
18435 else if (decl_specs->storage_class != sc_none)
18436 {
18437 decl_specs->conflicting_specifiers_p = true;
18438 return;
18439 }
18440
18441 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18442 && decl_specs->specs[(int) ds_thread])
18443 {
18444 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18445 decl_specs->specs[(int) ds_thread] = 0;
18446 }
18447
18448 switch (keyword)
18449 {
18450 case RID_AUTO:
18451 storage_class = sc_auto;
18452 break;
18453 case RID_REGISTER:
18454 storage_class = sc_register;
18455 break;
18456 case RID_STATIC:
18457 storage_class = sc_static;
18458 break;
18459 case RID_EXTERN:
18460 storage_class = sc_extern;
18461 break;
18462 case RID_MUTABLE:
18463 storage_class = sc_mutable;
18464 break;
18465 default:
18466 gcc_unreachable ();
18467 }
18468 decl_specs->storage_class = storage_class;
18469
18470 /* A storage class specifier cannot be applied alongside a typedef
18471 specifier. If there is a typedef specifier present then set
18472 conflicting_specifiers_p which will trigger an error later
18473 on in grokdeclarator. */
18474 if (decl_specs->specs[(int)ds_typedef])
18475 decl_specs->conflicting_specifiers_p = true;
18476 }
18477
18478 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18479 is true, the type is a user-defined type; otherwise it is a
18480 built-in type specified by a keyword. */
18481
18482 static void
18483 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18484 tree type_spec,
18485 location_t location,
18486 bool user_defined_p)
18487 {
18488 decl_specs->any_specifiers_p = true;
18489
18490 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18491 (with, for example, in "typedef int wchar_t;") we remember that
18492 this is what happened. In system headers, we ignore these
18493 declarations so that G++ can work with system headers that are not
18494 C++-safe. */
18495 if (decl_specs->specs[(int) ds_typedef]
18496 && !user_defined_p
18497 && (type_spec == boolean_type_node
18498 || type_spec == char16_type_node
18499 || type_spec == char32_type_node
18500 || type_spec == wchar_type_node)
18501 && (decl_specs->type
18502 || decl_specs->specs[(int) ds_long]
18503 || decl_specs->specs[(int) ds_short]
18504 || decl_specs->specs[(int) ds_unsigned]
18505 || decl_specs->specs[(int) ds_signed]))
18506 {
18507 decl_specs->redefined_builtin_type = type_spec;
18508 if (!decl_specs->type)
18509 {
18510 decl_specs->type = type_spec;
18511 decl_specs->user_defined_type_p = false;
18512 decl_specs->type_location = location;
18513 }
18514 }
18515 else if (decl_specs->type)
18516 decl_specs->multiple_types_p = true;
18517 else
18518 {
18519 decl_specs->type = type_spec;
18520 decl_specs->user_defined_type_p = user_defined_p;
18521 decl_specs->redefined_builtin_type = NULL_TREE;
18522 decl_specs->type_location = location;
18523 }
18524 }
18525
18526 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18527 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18528
18529 static bool
18530 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18531 {
18532 return decl_specifiers->specs[(int) ds_friend] != 0;
18533 }
18534
18535 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18536 issue an error message indicating that TOKEN_DESC was expected.
18537
18538 Returns the token consumed, if the token had the appropriate type.
18539 Otherwise, returns NULL. */
18540
18541 static cp_token *
18542 cp_parser_require (cp_parser* parser,
18543 enum cpp_ttype type,
18544 const char* token_desc)
18545 {
18546 if (cp_lexer_next_token_is (parser->lexer, type))
18547 return cp_lexer_consume_token (parser->lexer);
18548 else
18549 {
18550 /* Output the MESSAGE -- unless we're parsing tentatively. */
18551 if (!cp_parser_simulate_error (parser))
18552 {
18553 char *message = concat ("expected ", token_desc, NULL);
18554 cp_parser_error (parser, message);
18555 free (message);
18556 }
18557 return NULL;
18558 }
18559 }
18560
18561 /* An error message is produced if the next token is not '>'.
18562 All further tokens are skipped until the desired token is
18563 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18564
18565 static void
18566 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18567 {
18568 /* Current level of '< ... >'. */
18569 unsigned level = 0;
18570 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18571 unsigned nesting_depth = 0;
18572
18573 /* Are we ready, yet? If not, issue error message. */
18574 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18575 return;
18576
18577 /* Skip tokens until the desired token is found. */
18578 while (true)
18579 {
18580 /* Peek at the next token. */
18581 switch (cp_lexer_peek_token (parser->lexer)->type)
18582 {
18583 case CPP_LESS:
18584 if (!nesting_depth)
18585 ++level;
18586 break;
18587
18588 case CPP_RSHIFT:
18589 if (cxx_dialect == cxx98)
18590 /* C++0x views the `>>' operator as two `>' tokens, but
18591 C++98 does not. */
18592 break;
18593 else if (!nesting_depth && level-- == 0)
18594 {
18595 /* We've hit a `>>' where the first `>' closes the
18596 template argument list, and the second `>' is
18597 spurious. Just consume the `>>' and stop; we've
18598 already produced at least one error. */
18599 cp_lexer_consume_token (parser->lexer);
18600 return;
18601 }
18602 /* Fall through for C++0x, so we handle the second `>' in
18603 the `>>'. */
18604
18605 case CPP_GREATER:
18606 if (!nesting_depth && level-- == 0)
18607 {
18608 /* We've reached the token we want, consume it and stop. */
18609 cp_lexer_consume_token (parser->lexer);
18610 return;
18611 }
18612 break;
18613
18614 case CPP_OPEN_PAREN:
18615 case CPP_OPEN_SQUARE:
18616 ++nesting_depth;
18617 break;
18618
18619 case CPP_CLOSE_PAREN:
18620 case CPP_CLOSE_SQUARE:
18621 if (nesting_depth-- == 0)
18622 return;
18623 break;
18624
18625 case CPP_EOF:
18626 case CPP_PRAGMA_EOL:
18627 case CPP_SEMICOLON:
18628 case CPP_OPEN_BRACE:
18629 case CPP_CLOSE_BRACE:
18630 /* The '>' was probably forgotten, don't look further. */
18631 return;
18632
18633 default:
18634 break;
18635 }
18636
18637 /* Consume this token. */
18638 cp_lexer_consume_token (parser->lexer);
18639 }
18640 }
18641
18642 /* If the next token is the indicated keyword, consume it. Otherwise,
18643 issue an error message indicating that TOKEN_DESC was expected.
18644
18645 Returns the token consumed, if the token had the appropriate type.
18646 Otherwise, returns NULL. */
18647
18648 static cp_token *
18649 cp_parser_require_keyword (cp_parser* parser,
18650 enum rid keyword,
18651 const char* token_desc)
18652 {
18653 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18654
18655 if (token && token->keyword != keyword)
18656 {
18657 dyn_string_t error_msg;
18658
18659 /* Format the error message. */
18660 error_msg = dyn_string_new (0);
18661 dyn_string_append_cstr (error_msg, "expected ");
18662 dyn_string_append_cstr (error_msg, token_desc);
18663 cp_parser_error (parser, error_msg->s);
18664 dyn_string_delete (error_msg);
18665 return NULL;
18666 }
18667
18668 return token;
18669 }
18670
18671 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18672 function-definition. */
18673
18674 static bool
18675 cp_parser_token_starts_function_definition_p (cp_token* token)
18676 {
18677 return (/* An ordinary function-body begins with an `{'. */
18678 token->type == CPP_OPEN_BRACE
18679 /* A ctor-initializer begins with a `:'. */
18680 || token->type == CPP_COLON
18681 /* A function-try-block begins with `try'. */
18682 || token->keyword == RID_TRY
18683 /* The named return value extension begins with `return'. */
18684 || token->keyword == RID_RETURN);
18685 }
18686
18687 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18688 definition. */
18689
18690 static bool
18691 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18692 {
18693 cp_token *token;
18694
18695 token = cp_lexer_peek_token (parser->lexer);
18696 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18697 }
18698
18699 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18700 C++0x) ending a template-argument. */
18701
18702 static bool
18703 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18704 {
18705 cp_token *token;
18706
18707 token = cp_lexer_peek_token (parser->lexer);
18708 return (token->type == CPP_COMMA
18709 || token->type == CPP_GREATER
18710 || token->type == CPP_ELLIPSIS
18711 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18712 }
18713
18714 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18715 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18716
18717 static bool
18718 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18719 size_t n)
18720 {
18721 cp_token *token;
18722
18723 token = cp_lexer_peek_nth_token (parser->lexer, n);
18724 if (token->type == CPP_LESS)
18725 return true;
18726 /* Check for the sequence `<::' in the original code. It would be lexed as
18727 `[:', where `[' is a digraph, and there is no whitespace before
18728 `:'. */
18729 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18730 {
18731 cp_token *token2;
18732 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18733 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18734 return true;
18735 }
18736 return false;
18737 }
18738
18739 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18740 or none_type otherwise. */
18741
18742 static enum tag_types
18743 cp_parser_token_is_class_key (cp_token* token)
18744 {
18745 switch (token->keyword)
18746 {
18747 case RID_CLASS:
18748 return class_type;
18749 case RID_STRUCT:
18750 return record_type;
18751 case RID_UNION:
18752 return union_type;
18753
18754 default:
18755 return none_type;
18756 }
18757 }
18758
18759 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18760
18761 static void
18762 cp_parser_check_class_key (enum tag_types class_key, tree type)
18763 {
18764 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18765 permerror (input_location, "%qs tag used in naming %q#T",
18766 class_key == union_type ? "union"
18767 : class_key == record_type ? "struct" : "class",
18768 type);
18769 }
18770
18771 /* Issue an error message if DECL is redeclared with different
18772 access than its original declaration [class.access.spec/3].
18773 This applies to nested classes and nested class templates.
18774 [class.mem/1]. */
18775
18776 static void
18777 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18778 {
18779 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18780 return;
18781
18782 if ((TREE_PRIVATE (decl)
18783 != (current_access_specifier == access_private_node))
18784 || (TREE_PROTECTED (decl)
18785 != (current_access_specifier == access_protected_node)))
18786 error ("%H%qD redeclared with different access", &location, decl);
18787 }
18788
18789 /* Look for the `template' keyword, as a syntactic disambiguator.
18790 Return TRUE iff it is present, in which case it will be
18791 consumed. */
18792
18793 static bool
18794 cp_parser_optional_template_keyword (cp_parser *parser)
18795 {
18796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18797 {
18798 /* The `template' keyword can only be used within templates;
18799 outside templates the parser can always figure out what is a
18800 template and what is not. */
18801 if (!processing_template_decl)
18802 {
18803 cp_token *token = cp_lexer_peek_token (parser->lexer);
18804 error ("%H%<template%> (as a disambiguator) is only allowed "
18805 "within templates", &token->location);
18806 /* If this part of the token stream is rescanned, the same
18807 error message would be generated. So, we purge the token
18808 from the stream. */
18809 cp_lexer_purge_token (parser->lexer);
18810 return false;
18811 }
18812 else
18813 {
18814 /* Consume the `template' keyword. */
18815 cp_lexer_consume_token (parser->lexer);
18816 return true;
18817 }
18818 }
18819
18820 return false;
18821 }
18822
18823 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18824 set PARSER->SCOPE, and perform other related actions. */
18825
18826 static void
18827 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18828 {
18829 int i;
18830 struct tree_check *check_value;
18831 deferred_access_check *chk;
18832 VEC (deferred_access_check,gc) *checks;
18833
18834 /* Get the stored value. */
18835 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18836 /* Perform any access checks that were deferred. */
18837 checks = check_value->checks;
18838 if (checks)
18839 {
18840 for (i = 0 ;
18841 VEC_iterate (deferred_access_check, checks, i, chk) ;
18842 ++i)
18843 {
18844 perform_or_defer_access_check (chk->binfo,
18845 chk->decl,
18846 chk->diag_decl);
18847 }
18848 }
18849 /* Set the scope from the stored value. */
18850 parser->scope = check_value->value;
18851 parser->qualifying_scope = check_value->qualifying_scope;
18852 parser->object_scope = NULL_TREE;
18853 }
18854
18855 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18856 encounter the end of a block before what we were looking for. */
18857
18858 static bool
18859 cp_parser_cache_group (cp_parser *parser,
18860 enum cpp_ttype end,
18861 unsigned depth)
18862 {
18863 while (true)
18864 {
18865 cp_token *token = cp_lexer_peek_token (parser->lexer);
18866
18867 /* Abort a parenthesized expression if we encounter a semicolon. */
18868 if ((end == CPP_CLOSE_PAREN || depth == 0)
18869 && token->type == CPP_SEMICOLON)
18870 return true;
18871 /* If we've reached the end of the file, stop. */
18872 if (token->type == CPP_EOF
18873 || (end != CPP_PRAGMA_EOL
18874 && token->type == CPP_PRAGMA_EOL))
18875 return true;
18876 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18877 /* We've hit the end of an enclosing block, so there's been some
18878 kind of syntax error. */
18879 return true;
18880
18881 /* Consume the token. */
18882 cp_lexer_consume_token (parser->lexer);
18883 /* See if it starts a new group. */
18884 if (token->type == CPP_OPEN_BRACE)
18885 {
18886 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18887 /* In theory this should probably check end == '}', but
18888 cp_parser_save_member_function_body needs it to exit
18889 after either '}' or ')' when called with ')'. */
18890 if (depth == 0)
18891 return false;
18892 }
18893 else if (token->type == CPP_OPEN_PAREN)
18894 {
18895 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18896 if (depth == 0 && end == CPP_CLOSE_PAREN)
18897 return false;
18898 }
18899 else if (token->type == CPP_PRAGMA)
18900 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18901 else if (token->type == end)
18902 return false;
18903 }
18904 }
18905
18906 /* Begin parsing tentatively. We always save tokens while parsing
18907 tentatively so that if the tentative parsing fails we can restore the
18908 tokens. */
18909
18910 static void
18911 cp_parser_parse_tentatively (cp_parser* parser)
18912 {
18913 /* Enter a new parsing context. */
18914 parser->context = cp_parser_context_new (parser->context);
18915 /* Begin saving tokens. */
18916 cp_lexer_save_tokens (parser->lexer);
18917 /* In order to avoid repetitive access control error messages,
18918 access checks are queued up until we are no longer parsing
18919 tentatively. */
18920 push_deferring_access_checks (dk_deferred);
18921 }
18922
18923 /* Commit to the currently active tentative parse. */
18924
18925 static void
18926 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18927 {
18928 cp_parser_context *context;
18929 cp_lexer *lexer;
18930
18931 /* Mark all of the levels as committed. */
18932 lexer = parser->lexer;
18933 for (context = parser->context; context->next; context = context->next)
18934 {
18935 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18936 break;
18937 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18938 while (!cp_lexer_saving_tokens (lexer))
18939 lexer = lexer->next;
18940 cp_lexer_commit_tokens (lexer);
18941 }
18942 }
18943
18944 /* Abort the currently active tentative parse. All consumed tokens
18945 will be rolled back, and no diagnostics will be issued. */
18946
18947 static void
18948 cp_parser_abort_tentative_parse (cp_parser* parser)
18949 {
18950 cp_parser_simulate_error (parser);
18951 /* Now, pretend that we want to see if the construct was
18952 successfully parsed. */
18953 cp_parser_parse_definitely (parser);
18954 }
18955
18956 /* Stop parsing tentatively. If a parse error has occurred, restore the
18957 token stream. Otherwise, commit to the tokens we have consumed.
18958 Returns true if no error occurred; false otherwise. */
18959
18960 static bool
18961 cp_parser_parse_definitely (cp_parser* parser)
18962 {
18963 bool error_occurred;
18964 cp_parser_context *context;
18965
18966 /* Remember whether or not an error occurred, since we are about to
18967 destroy that information. */
18968 error_occurred = cp_parser_error_occurred (parser);
18969 /* Remove the topmost context from the stack. */
18970 context = parser->context;
18971 parser->context = context->next;
18972 /* If no parse errors occurred, commit to the tentative parse. */
18973 if (!error_occurred)
18974 {
18975 /* Commit to the tokens read tentatively, unless that was
18976 already done. */
18977 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18978 cp_lexer_commit_tokens (parser->lexer);
18979
18980 pop_to_parent_deferring_access_checks ();
18981 }
18982 /* Otherwise, if errors occurred, roll back our state so that things
18983 are just as they were before we began the tentative parse. */
18984 else
18985 {
18986 cp_lexer_rollback_tokens (parser->lexer);
18987 pop_deferring_access_checks ();
18988 }
18989 /* Add the context to the front of the free list. */
18990 context->next = cp_parser_context_free_list;
18991 cp_parser_context_free_list = context;
18992
18993 return !error_occurred;
18994 }
18995
18996 /* Returns true if we are parsing tentatively and are not committed to
18997 this tentative parse. */
18998
18999 static bool
19000 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19001 {
19002 return (cp_parser_parsing_tentatively (parser)
19003 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19004 }
19005
19006 /* Returns nonzero iff an error has occurred during the most recent
19007 tentative parse. */
19008
19009 static bool
19010 cp_parser_error_occurred (cp_parser* parser)
19011 {
19012 return (cp_parser_parsing_tentatively (parser)
19013 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19014 }
19015
19016 /* Returns nonzero if GNU extensions are allowed. */
19017
19018 static bool
19019 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19020 {
19021 return parser->allow_gnu_extensions_p;
19022 }
19023 \f
19024 /* Objective-C++ Productions */
19025
19026
19027 /* Parse an Objective-C expression, which feeds into a primary-expression
19028 above.
19029
19030 objc-expression:
19031 objc-message-expression
19032 objc-string-literal
19033 objc-encode-expression
19034 objc-protocol-expression
19035 objc-selector-expression
19036
19037 Returns a tree representation of the expression. */
19038
19039 static tree
19040 cp_parser_objc_expression (cp_parser* parser)
19041 {
19042 /* Try to figure out what kind of declaration is present. */
19043 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19044
19045 switch (kwd->type)
19046 {
19047 case CPP_OPEN_SQUARE:
19048 return cp_parser_objc_message_expression (parser);
19049
19050 case CPP_OBJC_STRING:
19051 kwd = cp_lexer_consume_token (parser->lexer);
19052 return objc_build_string_object (kwd->u.value);
19053
19054 case CPP_KEYWORD:
19055 switch (kwd->keyword)
19056 {
19057 case RID_AT_ENCODE:
19058 return cp_parser_objc_encode_expression (parser);
19059
19060 case RID_AT_PROTOCOL:
19061 return cp_parser_objc_protocol_expression (parser);
19062
19063 case RID_AT_SELECTOR:
19064 return cp_parser_objc_selector_expression (parser);
19065
19066 default:
19067 break;
19068 }
19069 default:
19070 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19071 &kwd->location, kwd->u.value);
19072 cp_parser_skip_to_end_of_block_or_statement (parser);
19073 }
19074
19075 return error_mark_node;
19076 }
19077
19078 /* Parse an Objective-C message expression.
19079
19080 objc-message-expression:
19081 [ objc-message-receiver objc-message-args ]
19082
19083 Returns a representation of an Objective-C message. */
19084
19085 static tree
19086 cp_parser_objc_message_expression (cp_parser* parser)
19087 {
19088 tree receiver, messageargs;
19089
19090 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19091 receiver = cp_parser_objc_message_receiver (parser);
19092 messageargs = cp_parser_objc_message_args (parser);
19093 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19094
19095 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19096 }
19097
19098 /* Parse an objc-message-receiver.
19099
19100 objc-message-receiver:
19101 expression
19102 simple-type-specifier
19103
19104 Returns a representation of the type or expression. */
19105
19106 static tree
19107 cp_parser_objc_message_receiver (cp_parser* parser)
19108 {
19109 tree rcv;
19110
19111 /* An Objective-C message receiver may be either (1) a type
19112 or (2) an expression. */
19113 cp_parser_parse_tentatively (parser);
19114 rcv = cp_parser_expression (parser, false, NULL);
19115
19116 if (cp_parser_parse_definitely (parser))
19117 return rcv;
19118
19119 rcv = cp_parser_simple_type_specifier (parser,
19120 /*decl_specs=*/NULL,
19121 CP_PARSER_FLAGS_NONE);
19122
19123 return objc_get_class_reference (rcv);
19124 }
19125
19126 /* Parse the arguments and selectors comprising an Objective-C message.
19127
19128 objc-message-args:
19129 objc-selector
19130 objc-selector-args
19131 objc-selector-args , objc-comma-args
19132
19133 objc-selector-args:
19134 objc-selector [opt] : assignment-expression
19135 objc-selector-args objc-selector [opt] : assignment-expression
19136
19137 objc-comma-args:
19138 assignment-expression
19139 objc-comma-args , assignment-expression
19140
19141 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19142 selector arguments and TREE_VALUE containing a list of comma
19143 arguments. */
19144
19145 static tree
19146 cp_parser_objc_message_args (cp_parser* parser)
19147 {
19148 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19149 bool maybe_unary_selector_p = true;
19150 cp_token *token = cp_lexer_peek_token (parser->lexer);
19151
19152 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19153 {
19154 tree selector = NULL_TREE, arg;
19155
19156 if (token->type != CPP_COLON)
19157 selector = cp_parser_objc_selector (parser);
19158
19159 /* Detect if we have a unary selector. */
19160 if (maybe_unary_selector_p
19161 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19162 return build_tree_list (selector, NULL_TREE);
19163
19164 maybe_unary_selector_p = false;
19165 cp_parser_require (parser, CPP_COLON, "%<:%>");
19166 arg = cp_parser_assignment_expression (parser, false, NULL);
19167
19168 sel_args
19169 = chainon (sel_args,
19170 build_tree_list (selector, arg));
19171
19172 token = cp_lexer_peek_token (parser->lexer);
19173 }
19174
19175 /* Handle non-selector arguments, if any. */
19176 while (token->type == CPP_COMMA)
19177 {
19178 tree arg;
19179
19180 cp_lexer_consume_token (parser->lexer);
19181 arg = cp_parser_assignment_expression (parser, false, NULL);
19182
19183 addl_args
19184 = chainon (addl_args,
19185 build_tree_list (NULL_TREE, arg));
19186
19187 token = cp_lexer_peek_token (parser->lexer);
19188 }
19189
19190 return build_tree_list (sel_args, addl_args);
19191 }
19192
19193 /* Parse an Objective-C encode expression.
19194
19195 objc-encode-expression:
19196 @encode objc-typename
19197
19198 Returns an encoded representation of the type argument. */
19199
19200 static tree
19201 cp_parser_objc_encode_expression (cp_parser* parser)
19202 {
19203 tree type;
19204 cp_token *token;
19205
19206 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19207 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19208 token = cp_lexer_peek_token (parser->lexer);
19209 type = complete_type (cp_parser_type_id (parser));
19210 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19211
19212 if (!type)
19213 {
19214 error ("%H%<@encode%> must specify a type as an argument",
19215 &token->location);
19216 return error_mark_node;
19217 }
19218
19219 return objc_build_encode_expr (type);
19220 }
19221
19222 /* Parse an Objective-C @defs expression. */
19223
19224 static tree
19225 cp_parser_objc_defs_expression (cp_parser *parser)
19226 {
19227 tree name;
19228
19229 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19230 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19231 name = cp_parser_identifier (parser);
19232 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19233
19234 return objc_get_class_ivars (name);
19235 }
19236
19237 /* Parse an Objective-C protocol expression.
19238
19239 objc-protocol-expression:
19240 @protocol ( identifier )
19241
19242 Returns a representation of the protocol expression. */
19243
19244 static tree
19245 cp_parser_objc_protocol_expression (cp_parser* parser)
19246 {
19247 tree proto;
19248
19249 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19250 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19251 proto = cp_parser_identifier (parser);
19252 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19253
19254 return objc_build_protocol_expr (proto);
19255 }
19256
19257 /* Parse an Objective-C selector expression.
19258
19259 objc-selector-expression:
19260 @selector ( objc-method-signature )
19261
19262 objc-method-signature:
19263 objc-selector
19264 objc-selector-seq
19265
19266 objc-selector-seq:
19267 objc-selector :
19268 objc-selector-seq objc-selector :
19269
19270 Returns a representation of the method selector. */
19271
19272 static tree
19273 cp_parser_objc_selector_expression (cp_parser* parser)
19274 {
19275 tree sel_seq = NULL_TREE;
19276 bool maybe_unary_selector_p = true;
19277 cp_token *token;
19278
19279 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19280 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19281 token = cp_lexer_peek_token (parser->lexer);
19282
19283 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19284 || token->type == CPP_SCOPE)
19285 {
19286 tree selector = NULL_TREE;
19287
19288 if (token->type != CPP_COLON
19289 || token->type == CPP_SCOPE)
19290 selector = cp_parser_objc_selector (parser);
19291
19292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19293 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19294 {
19295 /* Detect if we have a unary selector. */
19296 if (maybe_unary_selector_p)
19297 {
19298 sel_seq = selector;
19299 goto finish_selector;
19300 }
19301 else
19302 {
19303 cp_parser_error (parser, "expected %<:%>");
19304 }
19305 }
19306 maybe_unary_selector_p = false;
19307 token = cp_lexer_consume_token (parser->lexer);
19308
19309 if (token->type == CPP_SCOPE)
19310 {
19311 sel_seq
19312 = chainon (sel_seq,
19313 build_tree_list (selector, NULL_TREE));
19314 sel_seq
19315 = chainon (sel_seq,
19316 build_tree_list (NULL_TREE, NULL_TREE));
19317 }
19318 else
19319 sel_seq
19320 = chainon (sel_seq,
19321 build_tree_list (selector, NULL_TREE));
19322
19323 token = cp_lexer_peek_token (parser->lexer);
19324 }
19325
19326 finish_selector:
19327 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19328
19329 return objc_build_selector_expr (sel_seq);
19330 }
19331
19332 /* Parse a list of identifiers.
19333
19334 objc-identifier-list:
19335 identifier
19336 objc-identifier-list , identifier
19337
19338 Returns a TREE_LIST of identifier nodes. */
19339
19340 static tree
19341 cp_parser_objc_identifier_list (cp_parser* parser)
19342 {
19343 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19344 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19345
19346 while (sep->type == CPP_COMMA)
19347 {
19348 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19349 list = chainon (list,
19350 build_tree_list (NULL_TREE,
19351 cp_parser_identifier (parser)));
19352 sep = cp_lexer_peek_token (parser->lexer);
19353 }
19354
19355 return list;
19356 }
19357
19358 /* Parse an Objective-C alias declaration.
19359
19360 objc-alias-declaration:
19361 @compatibility_alias identifier identifier ;
19362
19363 This function registers the alias mapping with the Objective-C front end.
19364 It returns nothing. */
19365
19366 static void
19367 cp_parser_objc_alias_declaration (cp_parser* parser)
19368 {
19369 tree alias, orig;
19370
19371 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19372 alias = cp_parser_identifier (parser);
19373 orig = cp_parser_identifier (parser);
19374 objc_declare_alias (alias, orig);
19375 cp_parser_consume_semicolon_at_end_of_statement (parser);
19376 }
19377
19378 /* Parse an Objective-C class forward-declaration.
19379
19380 objc-class-declaration:
19381 @class objc-identifier-list ;
19382
19383 The function registers the forward declarations with the Objective-C
19384 front end. It returns nothing. */
19385
19386 static void
19387 cp_parser_objc_class_declaration (cp_parser* parser)
19388 {
19389 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19390 objc_declare_class (cp_parser_objc_identifier_list (parser));
19391 cp_parser_consume_semicolon_at_end_of_statement (parser);
19392 }
19393
19394 /* Parse a list of Objective-C protocol references.
19395
19396 objc-protocol-refs-opt:
19397 objc-protocol-refs [opt]
19398
19399 objc-protocol-refs:
19400 < objc-identifier-list >
19401
19402 Returns a TREE_LIST of identifiers, if any. */
19403
19404 static tree
19405 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19406 {
19407 tree protorefs = NULL_TREE;
19408
19409 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19410 {
19411 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19412 protorefs = cp_parser_objc_identifier_list (parser);
19413 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19414 }
19415
19416 return protorefs;
19417 }
19418
19419 /* Parse a Objective-C visibility specification. */
19420
19421 static void
19422 cp_parser_objc_visibility_spec (cp_parser* parser)
19423 {
19424 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19425
19426 switch (vis->keyword)
19427 {
19428 case RID_AT_PRIVATE:
19429 objc_set_visibility (2);
19430 break;
19431 case RID_AT_PROTECTED:
19432 objc_set_visibility (0);
19433 break;
19434 case RID_AT_PUBLIC:
19435 objc_set_visibility (1);
19436 break;
19437 default:
19438 return;
19439 }
19440
19441 /* Eat '@private'/'@protected'/'@public'. */
19442 cp_lexer_consume_token (parser->lexer);
19443 }
19444
19445 /* Parse an Objective-C method type. */
19446
19447 static void
19448 cp_parser_objc_method_type (cp_parser* parser)
19449 {
19450 objc_set_method_type
19451 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19452 ? PLUS_EXPR
19453 : MINUS_EXPR);
19454 }
19455
19456 /* Parse an Objective-C protocol qualifier. */
19457
19458 static tree
19459 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19460 {
19461 tree quals = NULL_TREE, node;
19462 cp_token *token = cp_lexer_peek_token (parser->lexer);
19463
19464 node = token->u.value;
19465
19466 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19467 && (node == ridpointers [(int) RID_IN]
19468 || node == ridpointers [(int) RID_OUT]
19469 || node == ridpointers [(int) RID_INOUT]
19470 || node == ridpointers [(int) RID_BYCOPY]
19471 || node == ridpointers [(int) RID_BYREF]
19472 || node == ridpointers [(int) RID_ONEWAY]))
19473 {
19474 quals = tree_cons (NULL_TREE, node, quals);
19475 cp_lexer_consume_token (parser->lexer);
19476 token = cp_lexer_peek_token (parser->lexer);
19477 node = token->u.value;
19478 }
19479
19480 return quals;
19481 }
19482
19483 /* Parse an Objective-C typename. */
19484
19485 static tree
19486 cp_parser_objc_typename (cp_parser* parser)
19487 {
19488 tree type_name = NULL_TREE;
19489
19490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19491 {
19492 tree proto_quals, cp_type = NULL_TREE;
19493
19494 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19495 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19496
19497 /* An ObjC type name may consist of just protocol qualifiers, in which
19498 case the type shall default to 'id'. */
19499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19500 cp_type = cp_parser_type_id (parser);
19501
19502 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19503 type_name = build_tree_list (proto_quals, cp_type);
19504 }
19505
19506 return type_name;
19507 }
19508
19509 /* Check to see if TYPE refers to an Objective-C selector name. */
19510
19511 static bool
19512 cp_parser_objc_selector_p (enum cpp_ttype type)
19513 {
19514 return (type == CPP_NAME || type == CPP_KEYWORD
19515 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19516 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19517 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19518 || type == CPP_XOR || type == CPP_XOR_EQ);
19519 }
19520
19521 /* Parse an Objective-C selector. */
19522
19523 static tree
19524 cp_parser_objc_selector (cp_parser* parser)
19525 {
19526 cp_token *token = cp_lexer_consume_token (parser->lexer);
19527
19528 if (!cp_parser_objc_selector_p (token->type))
19529 {
19530 error ("%Hinvalid Objective-C++ selector name", &token->location);
19531 return error_mark_node;
19532 }
19533
19534 /* C++ operator names are allowed to appear in ObjC selectors. */
19535 switch (token->type)
19536 {
19537 case CPP_AND_AND: return get_identifier ("and");
19538 case CPP_AND_EQ: return get_identifier ("and_eq");
19539 case CPP_AND: return get_identifier ("bitand");
19540 case CPP_OR: return get_identifier ("bitor");
19541 case CPP_COMPL: return get_identifier ("compl");
19542 case CPP_NOT: return get_identifier ("not");
19543 case CPP_NOT_EQ: return get_identifier ("not_eq");
19544 case CPP_OR_OR: return get_identifier ("or");
19545 case CPP_OR_EQ: return get_identifier ("or_eq");
19546 case CPP_XOR: return get_identifier ("xor");
19547 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19548 default: return token->u.value;
19549 }
19550 }
19551
19552 /* Parse an Objective-C params list. */
19553
19554 static tree
19555 cp_parser_objc_method_keyword_params (cp_parser* parser)
19556 {
19557 tree params = NULL_TREE;
19558 bool maybe_unary_selector_p = true;
19559 cp_token *token = cp_lexer_peek_token (parser->lexer);
19560
19561 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19562 {
19563 tree selector = NULL_TREE, type_name, identifier;
19564
19565 if (token->type != CPP_COLON)
19566 selector = cp_parser_objc_selector (parser);
19567
19568 /* Detect if we have a unary selector. */
19569 if (maybe_unary_selector_p
19570 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19571 return selector;
19572
19573 maybe_unary_selector_p = false;
19574 cp_parser_require (parser, CPP_COLON, "%<:%>");
19575 type_name = cp_parser_objc_typename (parser);
19576 identifier = cp_parser_identifier (parser);
19577
19578 params
19579 = chainon (params,
19580 objc_build_keyword_decl (selector,
19581 type_name,
19582 identifier));
19583
19584 token = cp_lexer_peek_token (parser->lexer);
19585 }
19586
19587 return params;
19588 }
19589
19590 /* Parse the non-keyword Objective-C params. */
19591
19592 static tree
19593 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19594 {
19595 tree params = make_node (TREE_LIST);
19596 cp_token *token = cp_lexer_peek_token (parser->lexer);
19597 *ellipsisp = false; /* Initially, assume no ellipsis. */
19598
19599 while (token->type == CPP_COMMA)
19600 {
19601 cp_parameter_declarator *parmdecl;
19602 tree parm;
19603
19604 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19605 token = cp_lexer_peek_token (parser->lexer);
19606
19607 if (token->type == CPP_ELLIPSIS)
19608 {
19609 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19610 *ellipsisp = true;
19611 break;
19612 }
19613
19614 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19615 parm = grokdeclarator (parmdecl->declarator,
19616 &parmdecl->decl_specifiers,
19617 PARM, /*initialized=*/0,
19618 /*attrlist=*/NULL);
19619
19620 chainon (params, build_tree_list (NULL_TREE, parm));
19621 token = cp_lexer_peek_token (parser->lexer);
19622 }
19623
19624 return params;
19625 }
19626
19627 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19628
19629 static void
19630 cp_parser_objc_interstitial_code (cp_parser* parser)
19631 {
19632 cp_token *token = cp_lexer_peek_token (parser->lexer);
19633
19634 /* If the next token is `extern' and the following token is a string
19635 literal, then we have a linkage specification. */
19636 if (token->keyword == RID_EXTERN
19637 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19638 cp_parser_linkage_specification (parser);
19639 /* Handle #pragma, if any. */
19640 else if (token->type == CPP_PRAGMA)
19641 cp_parser_pragma (parser, pragma_external);
19642 /* Allow stray semicolons. */
19643 else if (token->type == CPP_SEMICOLON)
19644 cp_lexer_consume_token (parser->lexer);
19645 /* Finally, try to parse a block-declaration, or a function-definition. */
19646 else
19647 cp_parser_block_declaration (parser, /*statement_p=*/false);
19648 }
19649
19650 /* Parse a method signature. */
19651
19652 static tree
19653 cp_parser_objc_method_signature (cp_parser* parser)
19654 {
19655 tree rettype, kwdparms, optparms;
19656 bool ellipsis = false;
19657
19658 cp_parser_objc_method_type (parser);
19659 rettype = cp_parser_objc_typename (parser);
19660 kwdparms = cp_parser_objc_method_keyword_params (parser);
19661 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19662
19663 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19664 }
19665
19666 /* Pars an Objective-C method prototype list. */
19667
19668 static void
19669 cp_parser_objc_method_prototype_list (cp_parser* parser)
19670 {
19671 cp_token *token = cp_lexer_peek_token (parser->lexer);
19672
19673 while (token->keyword != RID_AT_END)
19674 {
19675 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19676 {
19677 objc_add_method_declaration
19678 (cp_parser_objc_method_signature (parser));
19679 cp_parser_consume_semicolon_at_end_of_statement (parser);
19680 }
19681 else
19682 /* Allow for interspersed non-ObjC++ code. */
19683 cp_parser_objc_interstitial_code (parser);
19684
19685 token = cp_lexer_peek_token (parser->lexer);
19686 }
19687
19688 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19689 objc_finish_interface ();
19690 }
19691
19692 /* Parse an Objective-C method definition list. */
19693
19694 static void
19695 cp_parser_objc_method_definition_list (cp_parser* parser)
19696 {
19697 cp_token *token = cp_lexer_peek_token (parser->lexer);
19698
19699 while (token->keyword != RID_AT_END)
19700 {
19701 tree meth;
19702
19703 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19704 {
19705 push_deferring_access_checks (dk_deferred);
19706 objc_start_method_definition
19707 (cp_parser_objc_method_signature (parser));
19708
19709 /* For historical reasons, we accept an optional semicolon. */
19710 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19711 cp_lexer_consume_token (parser->lexer);
19712
19713 perform_deferred_access_checks ();
19714 stop_deferring_access_checks ();
19715 meth = cp_parser_function_definition_after_declarator (parser,
19716 false);
19717 pop_deferring_access_checks ();
19718 objc_finish_method_definition (meth);
19719 }
19720 else
19721 /* Allow for interspersed non-ObjC++ code. */
19722 cp_parser_objc_interstitial_code (parser);
19723
19724 token = cp_lexer_peek_token (parser->lexer);
19725 }
19726
19727 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19728 objc_finish_implementation ();
19729 }
19730
19731 /* Parse Objective-C ivars. */
19732
19733 static void
19734 cp_parser_objc_class_ivars (cp_parser* parser)
19735 {
19736 cp_token *token = cp_lexer_peek_token (parser->lexer);
19737
19738 if (token->type != CPP_OPEN_BRACE)
19739 return; /* No ivars specified. */
19740
19741 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19742 token = cp_lexer_peek_token (parser->lexer);
19743
19744 while (token->type != CPP_CLOSE_BRACE)
19745 {
19746 cp_decl_specifier_seq declspecs;
19747 int decl_class_or_enum_p;
19748 tree prefix_attributes;
19749
19750 cp_parser_objc_visibility_spec (parser);
19751
19752 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19753 break;
19754
19755 cp_parser_decl_specifier_seq (parser,
19756 CP_PARSER_FLAGS_OPTIONAL,
19757 &declspecs,
19758 &decl_class_or_enum_p);
19759 prefix_attributes = declspecs.attributes;
19760 declspecs.attributes = NULL_TREE;
19761
19762 /* Keep going until we hit the `;' at the end of the
19763 declaration. */
19764 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19765 {
19766 tree width = NULL_TREE, attributes, first_attribute, decl;
19767 cp_declarator *declarator = NULL;
19768 int ctor_dtor_or_conv_p;
19769
19770 /* Check for a (possibly unnamed) bitfield declaration. */
19771 token = cp_lexer_peek_token (parser->lexer);
19772 if (token->type == CPP_COLON)
19773 goto eat_colon;
19774
19775 if (token->type == CPP_NAME
19776 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19777 == CPP_COLON))
19778 {
19779 /* Get the name of the bitfield. */
19780 declarator = make_id_declarator (NULL_TREE,
19781 cp_parser_identifier (parser),
19782 sfk_none);
19783
19784 eat_colon:
19785 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19786 /* Get the width of the bitfield. */
19787 width
19788 = cp_parser_constant_expression (parser,
19789 /*allow_non_constant=*/false,
19790 NULL);
19791 }
19792 else
19793 {
19794 /* Parse the declarator. */
19795 declarator
19796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19797 &ctor_dtor_or_conv_p,
19798 /*parenthesized_p=*/NULL,
19799 /*member_p=*/false);
19800 }
19801
19802 /* Look for attributes that apply to the ivar. */
19803 attributes = cp_parser_attributes_opt (parser);
19804 /* Remember which attributes are prefix attributes and
19805 which are not. */
19806 first_attribute = attributes;
19807 /* Combine the attributes. */
19808 attributes = chainon (prefix_attributes, attributes);
19809
19810 if (width)
19811 /* Create the bitfield declaration. */
19812 decl = grokbitfield (declarator, &declspecs,
19813 width,
19814 attributes);
19815 else
19816 decl = grokfield (declarator, &declspecs,
19817 NULL_TREE, /*init_const_expr_p=*/false,
19818 NULL_TREE, attributes);
19819
19820 /* Add the instance variable. */
19821 objc_add_instance_variable (decl);
19822
19823 /* Reset PREFIX_ATTRIBUTES. */
19824 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19825 attributes = TREE_CHAIN (attributes);
19826 if (attributes)
19827 TREE_CHAIN (attributes) = NULL_TREE;
19828
19829 token = cp_lexer_peek_token (parser->lexer);
19830
19831 if (token->type == CPP_COMMA)
19832 {
19833 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19834 continue;
19835 }
19836 break;
19837 }
19838
19839 cp_parser_consume_semicolon_at_end_of_statement (parser);
19840 token = cp_lexer_peek_token (parser->lexer);
19841 }
19842
19843 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19844 /* For historical reasons, we accept an optional semicolon. */
19845 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19846 cp_lexer_consume_token (parser->lexer);
19847 }
19848
19849 /* Parse an Objective-C protocol declaration. */
19850
19851 static void
19852 cp_parser_objc_protocol_declaration (cp_parser* parser)
19853 {
19854 tree proto, protorefs;
19855 cp_token *tok;
19856
19857 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19859 {
19860 tok = cp_lexer_peek_token (parser->lexer);
19861 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19862 goto finish;
19863 }
19864
19865 /* See if we have a forward declaration or a definition. */
19866 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19867
19868 /* Try a forward declaration first. */
19869 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19870 {
19871 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19872 finish:
19873 cp_parser_consume_semicolon_at_end_of_statement (parser);
19874 }
19875
19876 /* Ok, we got a full-fledged definition (or at least should). */
19877 else
19878 {
19879 proto = cp_parser_identifier (parser);
19880 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19881 objc_start_protocol (proto, protorefs);
19882 cp_parser_objc_method_prototype_list (parser);
19883 }
19884 }
19885
19886 /* Parse an Objective-C superclass or category. */
19887
19888 static void
19889 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19890 tree *categ)
19891 {
19892 cp_token *next = cp_lexer_peek_token (parser->lexer);
19893
19894 *super = *categ = NULL_TREE;
19895 if (next->type == CPP_COLON)
19896 {
19897 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19898 *super = cp_parser_identifier (parser);
19899 }
19900 else if (next->type == CPP_OPEN_PAREN)
19901 {
19902 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19903 *categ = cp_parser_identifier (parser);
19904 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19905 }
19906 }
19907
19908 /* Parse an Objective-C class interface. */
19909
19910 static void
19911 cp_parser_objc_class_interface (cp_parser* parser)
19912 {
19913 tree name, super, categ, protos;
19914
19915 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19916 name = cp_parser_identifier (parser);
19917 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19918 protos = cp_parser_objc_protocol_refs_opt (parser);
19919
19920 /* We have either a class or a category on our hands. */
19921 if (categ)
19922 objc_start_category_interface (name, categ, protos);
19923 else
19924 {
19925 objc_start_class_interface (name, super, protos);
19926 /* Handle instance variable declarations, if any. */
19927 cp_parser_objc_class_ivars (parser);
19928 objc_continue_interface ();
19929 }
19930
19931 cp_parser_objc_method_prototype_list (parser);
19932 }
19933
19934 /* Parse an Objective-C class implementation. */
19935
19936 static void
19937 cp_parser_objc_class_implementation (cp_parser* parser)
19938 {
19939 tree name, super, categ;
19940
19941 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19942 name = cp_parser_identifier (parser);
19943 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19944
19945 /* We have either a class or a category on our hands. */
19946 if (categ)
19947 objc_start_category_implementation (name, categ);
19948 else
19949 {
19950 objc_start_class_implementation (name, super);
19951 /* Handle instance variable declarations, if any. */
19952 cp_parser_objc_class_ivars (parser);
19953 objc_continue_implementation ();
19954 }
19955
19956 cp_parser_objc_method_definition_list (parser);
19957 }
19958
19959 /* Consume the @end token and finish off the implementation. */
19960
19961 static void
19962 cp_parser_objc_end_implementation (cp_parser* parser)
19963 {
19964 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19965 objc_finish_implementation ();
19966 }
19967
19968 /* Parse an Objective-C declaration. */
19969
19970 static void
19971 cp_parser_objc_declaration (cp_parser* parser)
19972 {
19973 /* Try to figure out what kind of declaration is present. */
19974 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19975
19976 switch (kwd->keyword)
19977 {
19978 case RID_AT_ALIAS:
19979 cp_parser_objc_alias_declaration (parser);
19980 break;
19981 case RID_AT_CLASS:
19982 cp_parser_objc_class_declaration (parser);
19983 break;
19984 case RID_AT_PROTOCOL:
19985 cp_parser_objc_protocol_declaration (parser);
19986 break;
19987 case RID_AT_INTERFACE:
19988 cp_parser_objc_class_interface (parser);
19989 break;
19990 case RID_AT_IMPLEMENTATION:
19991 cp_parser_objc_class_implementation (parser);
19992 break;
19993 case RID_AT_END:
19994 cp_parser_objc_end_implementation (parser);
19995 break;
19996 default:
19997 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19998 &kwd->location, kwd->u.value);
19999 cp_parser_skip_to_end_of_block_or_statement (parser);
20000 }
20001 }
20002
20003 /* Parse an Objective-C try-catch-finally statement.
20004
20005 objc-try-catch-finally-stmt:
20006 @try compound-statement objc-catch-clause-seq [opt]
20007 objc-finally-clause [opt]
20008
20009 objc-catch-clause-seq:
20010 objc-catch-clause objc-catch-clause-seq [opt]
20011
20012 objc-catch-clause:
20013 @catch ( exception-declaration ) compound-statement
20014
20015 objc-finally-clause
20016 @finally compound-statement
20017
20018 Returns NULL_TREE. */
20019
20020 static tree
20021 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20022 location_t location;
20023 tree stmt;
20024
20025 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20026 location = cp_lexer_peek_token (parser->lexer)->location;
20027 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20028 node, lest it get absorbed into the surrounding block. */
20029 stmt = push_stmt_list ();
20030 cp_parser_compound_statement (parser, NULL, false);
20031 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20032
20033 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20034 {
20035 cp_parameter_declarator *parmdecl;
20036 tree parm;
20037
20038 cp_lexer_consume_token (parser->lexer);
20039 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20040 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20041 parm = grokdeclarator (parmdecl->declarator,
20042 &parmdecl->decl_specifiers,
20043 PARM, /*initialized=*/0,
20044 /*attrlist=*/NULL);
20045 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20046 objc_begin_catch_clause (parm);
20047 cp_parser_compound_statement (parser, NULL, false);
20048 objc_finish_catch_clause ();
20049 }
20050
20051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20052 {
20053 cp_lexer_consume_token (parser->lexer);
20054 location = cp_lexer_peek_token (parser->lexer)->location;
20055 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20056 node, lest it get absorbed into the surrounding block. */
20057 stmt = push_stmt_list ();
20058 cp_parser_compound_statement (parser, NULL, false);
20059 objc_build_finally_clause (location, pop_stmt_list (stmt));
20060 }
20061
20062 return objc_finish_try_stmt ();
20063 }
20064
20065 /* Parse an Objective-C synchronized statement.
20066
20067 objc-synchronized-stmt:
20068 @synchronized ( expression ) compound-statement
20069
20070 Returns NULL_TREE. */
20071
20072 static tree
20073 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20074 location_t location;
20075 tree lock, stmt;
20076
20077 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20078
20079 location = cp_lexer_peek_token (parser->lexer)->location;
20080 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20081 lock = cp_parser_expression (parser, false, NULL);
20082 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20083
20084 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20085 node, lest it get absorbed into the surrounding block. */
20086 stmt = push_stmt_list ();
20087 cp_parser_compound_statement (parser, NULL, false);
20088
20089 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20090 }
20091
20092 /* Parse an Objective-C throw statement.
20093
20094 objc-throw-stmt:
20095 @throw assignment-expression [opt] ;
20096
20097 Returns a constructed '@throw' statement. */
20098
20099 static tree
20100 cp_parser_objc_throw_statement (cp_parser *parser) {
20101 tree expr = NULL_TREE;
20102
20103 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20104
20105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20106 expr = cp_parser_assignment_expression (parser, false, NULL);
20107
20108 cp_parser_consume_semicolon_at_end_of_statement (parser);
20109
20110 return objc_build_throw_stmt (expr);
20111 }
20112
20113 /* Parse an Objective-C statement. */
20114
20115 static tree
20116 cp_parser_objc_statement (cp_parser * parser) {
20117 /* Try to figure out what kind of declaration is present. */
20118 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20119
20120 switch (kwd->keyword)
20121 {
20122 case RID_AT_TRY:
20123 return cp_parser_objc_try_catch_finally_statement (parser);
20124 case RID_AT_SYNCHRONIZED:
20125 return cp_parser_objc_synchronized_statement (parser);
20126 case RID_AT_THROW:
20127 return cp_parser_objc_throw_statement (parser);
20128 default:
20129 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20130 &kwd->location, kwd->u.value);
20131 cp_parser_skip_to_end_of_block_or_statement (parser);
20132 }
20133
20134 return error_mark_node;
20135 }
20136 \f
20137 /* OpenMP 2.5 parsing routines. */
20138
20139 /* Returns name of the next clause.
20140 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20141 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20142 returned and the token is consumed. */
20143
20144 static pragma_omp_clause
20145 cp_parser_omp_clause_name (cp_parser *parser)
20146 {
20147 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20148
20149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20150 result = PRAGMA_OMP_CLAUSE_IF;
20151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20152 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20153 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20154 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20155 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20156 {
20157 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20158 const char *p = IDENTIFIER_POINTER (id);
20159
20160 switch (p[0])
20161 {
20162 case 'c':
20163 if (!strcmp ("collapse", p))
20164 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20165 else if (!strcmp ("copyin", p))
20166 result = PRAGMA_OMP_CLAUSE_COPYIN;
20167 else if (!strcmp ("copyprivate", p))
20168 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20169 break;
20170 case 'f':
20171 if (!strcmp ("firstprivate", p))
20172 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20173 break;
20174 case 'l':
20175 if (!strcmp ("lastprivate", p))
20176 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20177 break;
20178 case 'n':
20179 if (!strcmp ("nowait", p))
20180 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20181 else if (!strcmp ("num_threads", p))
20182 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20183 break;
20184 case 'o':
20185 if (!strcmp ("ordered", p))
20186 result = PRAGMA_OMP_CLAUSE_ORDERED;
20187 break;
20188 case 'r':
20189 if (!strcmp ("reduction", p))
20190 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20191 break;
20192 case 's':
20193 if (!strcmp ("schedule", p))
20194 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20195 else if (!strcmp ("shared", p))
20196 result = PRAGMA_OMP_CLAUSE_SHARED;
20197 break;
20198 case 'u':
20199 if (!strcmp ("untied", p))
20200 result = PRAGMA_OMP_CLAUSE_UNTIED;
20201 break;
20202 }
20203 }
20204
20205 if (result != PRAGMA_OMP_CLAUSE_NONE)
20206 cp_lexer_consume_token (parser->lexer);
20207
20208 return result;
20209 }
20210
20211 /* Validate that a clause of the given type does not already exist. */
20212
20213 static void
20214 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20215 const char *name, location_t location)
20216 {
20217 tree c;
20218
20219 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20220 if (OMP_CLAUSE_CODE (c) == code)
20221 {
20222 error ("%Htoo many %qs clauses", &location, name);
20223 break;
20224 }
20225 }
20226
20227 /* OpenMP 2.5:
20228 variable-list:
20229 identifier
20230 variable-list , identifier
20231
20232 In addition, we match a closing parenthesis. An opening parenthesis
20233 will have been consumed by the caller.
20234
20235 If KIND is nonzero, create the appropriate node and install the decl
20236 in OMP_CLAUSE_DECL and add the node to the head of the list.
20237
20238 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20239 return the list created. */
20240
20241 static tree
20242 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20243 tree list)
20244 {
20245 cp_token *token;
20246 while (1)
20247 {
20248 tree name, decl;
20249
20250 token = cp_lexer_peek_token (parser->lexer);
20251 name = cp_parser_id_expression (parser, /*template_p=*/false,
20252 /*check_dependency_p=*/true,
20253 /*template_p=*/NULL,
20254 /*declarator_p=*/false,
20255 /*optional_p=*/false);
20256 if (name == error_mark_node)
20257 goto skip_comma;
20258
20259 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20260 if (decl == error_mark_node)
20261 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20262 else if (kind != 0)
20263 {
20264 tree u = build_omp_clause (kind);
20265 OMP_CLAUSE_DECL (u) = decl;
20266 OMP_CLAUSE_CHAIN (u) = list;
20267 list = u;
20268 }
20269 else
20270 list = tree_cons (decl, NULL_TREE, list);
20271
20272 get_comma:
20273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20274 break;
20275 cp_lexer_consume_token (parser->lexer);
20276 }
20277
20278 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20279 {
20280 int ending;
20281
20282 /* Try to resync to an unnested comma. Copied from
20283 cp_parser_parenthesized_expression_list. */
20284 skip_comma:
20285 ending = cp_parser_skip_to_closing_parenthesis (parser,
20286 /*recovering=*/true,
20287 /*or_comma=*/true,
20288 /*consume_paren=*/true);
20289 if (ending < 0)
20290 goto get_comma;
20291 }
20292
20293 return list;
20294 }
20295
20296 /* Similarly, but expect leading and trailing parenthesis. This is a very
20297 common case for omp clauses. */
20298
20299 static tree
20300 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20301 {
20302 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20303 return cp_parser_omp_var_list_no_open (parser, kind, list);
20304 return list;
20305 }
20306
20307 /* OpenMP 3.0:
20308 collapse ( constant-expression ) */
20309
20310 static tree
20311 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20312 {
20313 tree c, num;
20314 location_t loc;
20315 HOST_WIDE_INT n;
20316
20317 loc = cp_lexer_peek_token (parser->lexer)->location;
20318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20319 return list;
20320
20321 num = cp_parser_constant_expression (parser, false, NULL);
20322
20323 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20324 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20325 /*or_comma=*/false,
20326 /*consume_paren=*/true);
20327
20328 if (num == error_mark_node)
20329 return list;
20330 num = fold_non_dependent_expr (num);
20331 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20332 || !host_integerp (num, 0)
20333 || (n = tree_low_cst (num, 0)) <= 0
20334 || (int) n != n)
20335 {
20336 error ("%Hcollapse argument needs positive constant integer expression",
20337 &loc);
20338 return list;
20339 }
20340
20341 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20342 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20343 OMP_CLAUSE_CHAIN (c) = list;
20344 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20345
20346 return c;
20347 }
20348
20349 /* OpenMP 2.5:
20350 default ( shared | none ) */
20351
20352 static tree
20353 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20354 {
20355 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20356 tree c;
20357
20358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20359 return list;
20360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20361 {
20362 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20363 const char *p = IDENTIFIER_POINTER (id);
20364
20365 switch (p[0])
20366 {
20367 case 'n':
20368 if (strcmp ("none", p) != 0)
20369 goto invalid_kind;
20370 kind = OMP_CLAUSE_DEFAULT_NONE;
20371 break;
20372
20373 case 's':
20374 if (strcmp ("shared", p) != 0)
20375 goto invalid_kind;
20376 kind = OMP_CLAUSE_DEFAULT_SHARED;
20377 break;
20378
20379 default:
20380 goto invalid_kind;
20381 }
20382
20383 cp_lexer_consume_token (parser->lexer);
20384 }
20385 else
20386 {
20387 invalid_kind:
20388 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20389 }
20390
20391 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20392 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20393 /*or_comma=*/false,
20394 /*consume_paren=*/true);
20395
20396 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20397 return list;
20398
20399 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20400 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20401 OMP_CLAUSE_CHAIN (c) = list;
20402 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20403
20404 return c;
20405 }
20406
20407 /* OpenMP 2.5:
20408 if ( expression ) */
20409
20410 static tree
20411 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20412 {
20413 tree t, c;
20414
20415 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20416 return list;
20417
20418 t = cp_parser_condition (parser);
20419
20420 if (t == error_mark_node
20421 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20423 /*or_comma=*/false,
20424 /*consume_paren=*/true);
20425
20426 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20427
20428 c = build_omp_clause (OMP_CLAUSE_IF);
20429 OMP_CLAUSE_IF_EXPR (c) = t;
20430 OMP_CLAUSE_CHAIN (c) = list;
20431
20432 return c;
20433 }
20434
20435 /* OpenMP 2.5:
20436 nowait */
20437
20438 static tree
20439 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20440 tree list, location_t location)
20441 {
20442 tree c;
20443
20444 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20445
20446 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20447 OMP_CLAUSE_CHAIN (c) = list;
20448 return c;
20449 }
20450
20451 /* OpenMP 2.5:
20452 num_threads ( expression ) */
20453
20454 static tree
20455 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20456 location_t location)
20457 {
20458 tree t, c;
20459
20460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20461 return list;
20462
20463 t = cp_parser_expression (parser, false, NULL);
20464
20465 if (t == error_mark_node
20466 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20468 /*or_comma=*/false,
20469 /*consume_paren=*/true);
20470
20471 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20472 "num_threads", location);
20473
20474 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20475 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20476 OMP_CLAUSE_CHAIN (c) = list;
20477
20478 return c;
20479 }
20480
20481 /* OpenMP 2.5:
20482 ordered */
20483
20484 static tree
20485 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20486 tree list, location_t location)
20487 {
20488 tree c;
20489
20490 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20491 "ordered", location);
20492
20493 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20494 OMP_CLAUSE_CHAIN (c) = list;
20495 return c;
20496 }
20497
20498 /* OpenMP 2.5:
20499 reduction ( reduction-operator : variable-list )
20500
20501 reduction-operator:
20502 One of: + * - & ^ | && || */
20503
20504 static tree
20505 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20506 {
20507 enum tree_code code;
20508 tree nlist, c;
20509
20510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20511 return list;
20512
20513 switch (cp_lexer_peek_token (parser->lexer)->type)
20514 {
20515 case CPP_PLUS:
20516 code = PLUS_EXPR;
20517 break;
20518 case CPP_MULT:
20519 code = MULT_EXPR;
20520 break;
20521 case CPP_MINUS:
20522 code = MINUS_EXPR;
20523 break;
20524 case CPP_AND:
20525 code = BIT_AND_EXPR;
20526 break;
20527 case CPP_XOR:
20528 code = BIT_XOR_EXPR;
20529 break;
20530 case CPP_OR:
20531 code = BIT_IOR_EXPR;
20532 break;
20533 case CPP_AND_AND:
20534 code = TRUTH_ANDIF_EXPR;
20535 break;
20536 case CPP_OR_OR:
20537 code = TRUTH_ORIF_EXPR;
20538 break;
20539 default:
20540 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20541 "%<|%>, %<&&%>, or %<||%>");
20542 resync_fail:
20543 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20544 /*or_comma=*/false,
20545 /*consume_paren=*/true);
20546 return list;
20547 }
20548 cp_lexer_consume_token (parser->lexer);
20549
20550 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20551 goto resync_fail;
20552
20553 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20554 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20555 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20556
20557 return nlist;
20558 }
20559
20560 /* OpenMP 2.5:
20561 schedule ( schedule-kind )
20562 schedule ( schedule-kind , expression )
20563
20564 schedule-kind:
20565 static | dynamic | guided | runtime | auto */
20566
20567 static tree
20568 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20569 {
20570 tree c, t;
20571
20572 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20573 return list;
20574
20575 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20576
20577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20578 {
20579 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20580 const char *p = IDENTIFIER_POINTER (id);
20581
20582 switch (p[0])
20583 {
20584 case 'd':
20585 if (strcmp ("dynamic", p) != 0)
20586 goto invalid_kind;
20587 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20588 break;
20589
20590 case 'g':
20591 if (strcmp ("guided", p) != 0)
20592 goto invalid_kind;
20593 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20594 break;
20595
20596 case 'r':
20597 if (strcmp ("runtime", p) != 0)
20598 goto invalid_kind;
20599 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20600 break;
20601
20602 default:
20603 goto invalid_kind;
20604 }
20605 }
20606 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20607 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20608 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20609 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20610 else
20611 goto invalid_kind;
20612 cp_lexer_consume_token (parser->lexer);
20613
20614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20615 {
20616 cp_token *token;
20617 cp_lexer_consume_token (parser->lexer);
20618
20619 token = cp_lexer_peek_token (parser->lexer);
20620 t = cp_parser_assignment_expression (parser, false, NULL);
20621
20622 if (t == error_mark_node)
20623 goto resync_fail;
20624 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20625 error ("%Hschedule %<runtime%> does not take "
20626 "a %<chunk_size%> parameter", &token->location);
20627 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20628 error ("%Hschedule %<auto%> does not take "
20629 "a %<chunk_size%> parameter", &token->location);
20630 else
20631 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20632
20633 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20634 goto resync_fail;
20635 }
20636 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20637 goto resync_fail;
20638
20639 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20640 OMP_CLAUSE_CHAIN (c) = list;
20641 return c;
20642
20643 invalid_kind:
20644 cp_parser_error (parser, "invalid schedule kind");
20645 resync_fail:
20646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20647 /*or_comma=*/false,
20648 /*consume_paren=*/true);
20649 return list;
20650 }
20651
20652 /* OpenMP 3.0:
20653 untied */
20654
20655 static tree
20656 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20657 tree list, location_t location)
20658 {
20659 tree c;
20660
20661 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20662
20663 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20664 OMP_CLAUSE_CHAIN (c) = list;
20665 return c;
20666 }
20667
20668 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20669 is a bitmask in MASK. Return the list of clauses found; the result
20670 of clause default goes in *pdefault. */
20671
20672 static tree
20673 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20674 const char *where, cp_token *pragma_tok)
20675 {
20676 tree clauses = NULL;
20677 bool first = true;
20678 cp_token *token = NULL;
20679
20680 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20681 {
20682 pragma_omp_clause c_kind;
20683 const char *c_name;
20684 tree prev = clauses;
20685
20686 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20687 cp_lexer_consume_token (parser->lexer);
20688
20689 token = cp_lexer_peek_token (parser->lexer);
20690 c_kind = cp_parser_omp_clause_name (parser);
20691 first = false;
20692
20693 switch (c_kind)
20694 {
20695 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20696 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20697 token->location);
20698 c_name = "collapse";
20699 break;
20700 case PRAGMA_OMP_CLAUSE_COPYIN:
20701 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20702 c_name = "copyin";
20703 break;
20704 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20705 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20706 clauses);
20707 c_name = "copyprivate";
20708 break;
20709 case PRAGMA_OMP_CLAUSE_DEFAULT:
20710 clauses = cp_parser_omp_clause_default (parser, clauses,
20711 token->location);
20712 c_name = "default";
20713 break;
20714 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20715 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20716 clauses);
20717 c_name = "firstprivate";
20718 break;
20719 case PRAGMA_OMP_CLAUSE_IF:
20720 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20721 c_name = "if";
20722 break;
20723 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20724 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20725 clauses);
20726 c_name = "lastprivate";
20727 break;
20728 case PRAGMA_OMP_CLAUSE_NOWAIT:
20729 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20730 c_name = "nowait";
20731 break;
20732 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20733 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20734 token->location);
20735 c_name = "num_threads";
20736 break;
20737 case PRAGMA_OMP_CLAUSE_ORDERED:
20738 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20739 token->location);
20740 c_name = "ordered";
20741 break;
20742 case PRAGMA_OMP_CLAUSE_PRIVATE:
20743 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20744 clauses);
20745 c_name = "private";
20746 break;
20747 case PRAGMA_OMP_CLAUSE_REDUCTION:
20748 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20749 c_name = "reduction";
20750 break;
20751 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20752 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20753 token->location);
20754 c_name = "schedule";
20755 break;
20756 case PRAGMA_OMP_CLAUSE_SHARED:
20757 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20758 clauses);
20759 c_name = "shared";
20760 break;
20761 case PRAGMA_OMP_CLAUSE_UNTIED:
20762 clauses = cp_parser_omp_clause_untied (parser, clauses,
20763 token->location);
20764 c_name = "nowait";
20765 break;
20766 default:
20767 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20768 goto saw_error;
20769 }
20770
20771 if (((mask >> c_kind) & 1) == 0)
20772 {
20773 /* Remove the invalid clause(s) from the list to avoid
20774 confusing the rest of the compiler. */
20775 clauses = prev;
20776 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20777 }
20778 }
20779 saw_error:
20780 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20781 return finish_omp_clauses (clauses);
20782 }
20783
20784 /* OpenMP 2.5:
20785 structured-block:
20786 statement
20787
20788 In practice, we're also interested in adding the statement to an
20789 outer node. So it is convenient if we work around the fact that
20790 cp_parser_statement calls add_stmt. */
20791
20792 static unsigned
20793 cp_parser_begin_omp_structured_block (cp_parser *parser)
20794 {
20795 unsigned save = parser->in_statement;
20796
20797 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20798 This preserves the "not within loop or switch" style error messages
20799 for nonsense cases like
20800 void foo() {
20801 #pragma omp single
20802 break;
20803 }
20804 */
20805 if (parser->in_statement)
20806 parser->in_statement = IN_OMP_BLOCK;
20807
20808 return save;
20809 }
20810
20811 static void
20812 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20813 {
20814 parser->in_statement = save;
20815 }
20816
20817 static tree
20818 cp_parser_omp_structured_block (cp_parser *parser)
20819 {
20820 tree stmt = begin_omp_structured_block ();
20821 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20822
20823 cp_parser_statement (parser, NULL_TREE, false, NULL);
20824
20825 cp_parser_end_omp_structured_block (parser, save);
20826 return finish_omp_structured_block (stmt);
20827 }
20828
20829 /* OpenMP 2.5:
20830 # pragma omp atomic new-line
20831 expression-stmt
20832
20833 expression-stmt:
20834 x binop= expr | x++ | ++x | x-- | --x
20835 binop:
20836 +, *, -, /, &, ^, |, <<, >>
20837
20838 where x is an lvalue expression with scalar type. */
20839
20840 static void
20841 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20842 {
20843 tree lhs, rhs;
20844 enum tree_code code;
20845
20846 cp_parser_require_pragma_eol (parser, pragma_tok);
20847
20848 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20849 /*cast_p=*/false, NULL);
20850 switch (TREE_CODE (lhs))
20851 {
20852 case ERROR_MARK:
20853 goto saw_error;
20854
20855 case PREINCREMENT_EXPR:
20856 case POSTINCREMENT_EXPR:
20857 lhs = TREE_OPERAND (lhs, 0);
20858 code = PLUS_EXPR;
20859 rhs = integer_one_node;
20860 break;
20861
20862 case PREDECREMENT_EXPR:
20863 case POSTDECREMENT_EXPR:
20864 lhs = TREE_OPERAND (lhs, 0);
20865 code = MINUS_EXPR;
20866 rhs = integer_one_node;
20867 break;
20868
20869 default:
20870 switch (cp_lexer_peek_token (parser->lexer)->type)
20871 {
20872 case CPP_MULT_EQ:
20873 code = MULT_EXPR;
20874 break;
20875 case CPP_DIV_EQ:
20876 code = TRUNC_DIV_EXPR;
20877 break;
20878 case CPP_PLUS_EQ:
20879 code = PLUS_EXPR;
20880 break;
20881 case CPP_MINUS_EQ:
20882 code = MINUS_EXPR;
20883 break;
20884 case CPP_LSHIFT_EQ:
20885 code = LSHIFT_EXPR;
20886 break;
20887 case CPP_RSHIFT_EQ:
20888 code = RSHIFT_EXPR;
20889 break;
20890 case CPP_AND_EQ:
20891 code = BIT_AND_EXPR;
20892 break;
20893 case CPP_OR_EQ:
20894 code = BIT_IOR_EXPR;
20895 break;
20896 case CPP_XOR_EQ:
20897 code = BIT_XOR_EXPR;
20898 break;
20899 default:
20900 cp_parser_error (parser,
20901 "invalid operator for %<#pragma omp atomic%>");
20902 goto saw_error;
20903 }
20904 cp_lexer_consume_token (parser->lexer);
20905
20906 rhs = cp_parser_expression (parser, false, NULL);
20907 if (rhs == error_mark_node)
20908 goto saw_error;
20909 break;
20910 }
20911 finish_omp_atomic (code, lhs, rhs);
20912 cp_parser_consume_semicolon_at_end_of_statement (parser);
20913 return;
20914
20915 saw_error:
20916 cp_parser_skip_to_end_of_block_or_statement (parser);
20917 }
20918
20919
20920 /* OpenMP 2.5:
20921 # pragma omp barrier new-line */
20922
20923 static void
20924 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20925 {
20926 cp_parser_require_pragma_eol (parser, pragma_tok);
20927 finish_omp_barrier ();
20928 }
20929
20930 /* OpenMP 2.5:
20931 # pragma omp critical [(name)] new-line
20932 structured-block */
20933
20934 static tree
20935 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20936 {
20937 tree stmt, name = NULL;
20938
20939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20940 {
20941 cp_lexer_consume_token (parser->lexer);
20942
20943 name = cp_parser_identifier (parser);
20944
20945 if (name == error_mark_node
20946 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20948 /*or_comma=*/false,
20949 /*consume_paren=*/true);
20950 if (name == error_mark_node)
20951 name = NULL;
20952 }
20953 cp_parser_require_pragma_eol (parser, pragma_tok);
20954
20955 stmt = cp_parser_omp_structured_block (parser);
20956 return c_finish_omp_critical (stmt, name);
20957 }
20958
20959 /* OpenMP 2.5:
20960 # pragma omp flush flush-vars[opt] new-line
20961
20962 flush-vars:
20963 ( variable-list ) */
20964
20965 static void
20966 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20967 {
20968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20969 (void) cp_parser_omp_var_list (parser, 0, NULL);
20970 cp_parser_require_pragma_eol (parser, pragma_tok);
20971
20972 finish_omp_flush ();
20973 }
20974
20975 /* Helper function, to parse omp for increment expression. */
20976
20977 static tree
20978 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20979 {
20980 tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
20981 enum tree_code op;
20982 cp_token *token;
20983
20984 if (lhs != decl)
20985 {
20986 cp_parser_skip_to_end_of_statement (parser);
20987 return error_mark_node;
20988 }
20989
20990 token = cp_lexer_peek_token (parser->lexer);
20991 op = binops_by_token [token->type].tree_type;
20992 switch (op)
20993 {
20994 case LT_EXPR:
20995 case LE_EXPR:
20996 case GT_EXPR:
20997 case GE_EXPR:
20998 break;
20999 default:
21000 cp_parser_skip_to_end_of_statement (parser);
21001 return error_mark_node;
21002 }
21003
21004 cp_lexer_consume_token (parser->lexer);
21005 rhs = cp_parser_binary_expression (parser, false,
21006 PREC_RELATIONAL_EXPRESSION, NULL);
21007 if (rhs == error_mark_node
21008 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21009 {
21010 cp_parser_skip_to_end_of_statement (parser);
21011 return error_mark_node;
21012 }
21013
21014 return build2 (op, boolean_type_node, lhs, rhs);
21015 }
21016
21017 /* Helper function, to parse omp for increment expression. */
21018
21019 static tree
21020 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21021 {
21022 cp_token *token = cp_lexer_peek_token (parser->lexer);
21023 enum tree_code op;
21024 tree lhs, rhs;
21025 cp_id_kind idk;
21026 bool decl_first;
21027
21028 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21029 {
21030 op = (token->type == CPP_PLUS_PLUS
21031 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21032 cp_lexer_consume_token (parser->lexer);
21033 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21034 if (lhs != decl)
21035 return error_mark_node;
21036 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21037 }
21038
21039 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21040 if (lhs != decl)
21041 return error_mark_node;
21042
21043 token = cp_lexer_peek_token (parser->lexer);
21044 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21045 {
21046 op = (token->type == CPP_PLUS_PLUS
21047 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21048 cp_lexer_consume_token (parser->lexer);
21049 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21050 }
21051
21052 op = cp_parser_assignment_operator_opt (parser);
21053 if (op == ERROR_MARK)
21054 return error_mark_node;
21055
21056 if (op != NOP_EXPR)
21057 {
21058 rhs = cp_parser_assignment_expression (parser, false, NULL);
21059 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21060 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21061 }
21062
21063 lhs = cp_parser_binary_expression (parser, false,
21064 PREC_ADDITIVE_EXPRESSION, NULL);
21065 token = cp_lexer_peek_token (parser->lexer);
21066 decl_first = lhs == decl;
21067 if (decl_first)
21068 lhs = NULL_TREE;
21069 if (token->type != CPP_PLUS
21070 && token->type != CPP_MINUS)
21071 return error_mark_node;
21072
21073 do
21074 {
21075 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21076 cp_lexer_consume_token (parser->lexer);
21077 rhs = cp_parser_binary_expression (parser, false,
21078 PREC_ADDITIVE_EXPRESSION, NULL);
21079 token = cp_lexer_peek_token (parser->lexer);
21080 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21081 {
21082 if (lhs == NULL_TREE)
21083 {
21084 if (op == PLUS_EXPR)
21085 lhs = rhs;
21086 else
21087 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21088 }
21089 else
21090 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21091 NULL, tf_warning_or_error);
21092 }
21093 }
21094 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21095
21096 if (!decl_first)
21097 {
21098 if (rhs != decl || op == MINUS_EXPR)
21099 return error_mark_node;
21100 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21101 }
21102 else
21103 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21104
21105 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21106 }
21107
21108 /* Parse the restricted form of the for statement allowed by OpenMP. */
21109
21110 static tree
21111 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21112 {
21113 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21114 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21115 tree this_pre_body, cl;
21116 location_t loc_first;
21117 bool collapse_err = false;
21118 int i, collapse = 1, nbraces = 0;
21119
21120 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21121 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21122 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21123
21124 gcc_assert (collapse >= 1);
21125
21126 declv = make_tree_vec (collapse);
21127 initv = make_tree_vec (collapse);
21128 condv = make_tree_vec (collapse);
21129 incrv = make_tree_vec (collapse);
21130
21131 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21132
21133 for (i = 0; i < collapse; i++)
21134 {
21135 int bracecount = 0;
21136 bool add_private_clause = false;
21137 location_t loc;
21138
21139 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21140 {
21141 cp_parser_error (parser, "for statement expected");
21142 return NULL;
21143 }
21144 loc = cp_lexer_consume_token (parser->lexer)->location;
21145
21146 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21147 return NULL;
21148
21149 init = decl = real_decl = NULL;
21150 this_pre_body = push_stmt_list ();
21151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21152 {
21153 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21154
21155 init-expr:
21156 var = lb
21157 integer-type var = lb
21158 random-access-iterator-type var = lb
21159 pointer-type var = lb
21160 */
21161 cp_decl_specifier_seq type_specifiers;
21162
21163 /* First, try to parse as an initialized declaration. See
21164 cp_parser_condition, from whence the bulk of this is copied. */
21165
21166 cp_parser_parse_tentatively (parser);
21167 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21168 &type_specifiers);
21169 if (cp_parser_parse_definitely (parser))
21170 {
21171 /* If parsing a type specifier seq succeeded, then this
21172 MUST be a initialized declaration. */
21173 tree asm_specification, attributes;
21174 cp_declarator *declarator;
21175
21176 declarator = cp_parser_declarator (parser,
21177 CP_PARSER_DECLARATOR_NAMED,
21178 /*ctor_dtor_or_conv_p=*/NULL,
21179 /*parenthesized_p=*/NULL,
21180 /*member_p=*/false);
21181 attributes = cp_parser_attributes_opt (parser);
21182 asm_specification = cp_parser_asm_specification_opt (parser);
21183
21184 if (declarator == cp_error_declarator)
21185 cp_parser_skip_to_end_of_statement (parser);
21186
21187 else
21188 {
21189 tree pushed_scope, auto_node;
21190
21191 decl = start_decl (declarator, &type_specifiers,
21192 SD_INITIALIZED, attributes,
21193 /*prefix_attributes=*/NULL_TREE,
21194 &pushed_scope);
21195
21196 auto_node = type_uses_auto (TREE_TYPE (decl));
21197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21198 {
21199 if (cp_lexer_next_token_is (parser->lexer,
21200 CPP_OPEN_PAREN))
21201 error ("parenthesized initialization is not allowed in "
21202 "OpenMP %<for%> loop");
21203 else
21204 /* Trigger an error. */
21205 cp_parser_require (parser, CPP_EQ, "%<=%>");
21206
21207 init = error_mark_node;
21208 cp_parser_skip_to_end_of_statement (parser);
21209 }
21210 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21211 || type_dependent_expression_p (decl)
21212 || auto_node)
21213 {
21214 bool is_direct_init, is_non_constant_init;
21215
21216 init = cp_parser_initializer (parser,
21217 &is_direct_init,
21218 &is_non_constant_init);
21219
21220 if (auto_node && describable_type (init))
21221 {
21222 TREE_TYPE (decl)
21223 = do_auto_deduction (TREE_TYPE (decl), init,
21224 auto_node);
21225
21226 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21227 && !type_dependent_expression_p (decl))
21228 goto non_class;
21229 }
21230
21231 cp_finish_decl (decl, init, !is_non_constant_init,
21232 asm_specification,
21233 LOOKUP_ONLYCONVERTING);
21234 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21235 {
21236 for_block
21237 = tree_cons (NULL, this_pre_body, for_block);
21238 init = NULL_TREE;
21239 }
21240 else
21241 init = pop_stmt_list (this_pre_body);
21242 this_pre_body = NULL_TREE;
21243 }
21244 else
21245 {
21246 /* Consume '='. */
21247 cp_lexer_consume_token (parser->lexer);
21248 init = cp_parser_assignment_expression (parser, false, NULL);
21249
21250 non_class:
21251 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21252 init = error_mark_node;
21253 else
21254 cp_finish_decl (decl, NULL_TREE,
21255 /*init_const_expr_p=*/false,
21256 asm_specification,
21257 LOOKUP_ONLYCONVERTING);
21258 }
21259
21260 if (pushed_scope)
21261 pop_scope (pushed_scope);
21262 }
21263 }
21264 else
21265 {
21266 cp_id_kind idk;
21267 /* If parsing a type specifier sequence failed, then
21268 this MUST be a simple expression. */
21269 cp_parser_parse_tentatively (parser);
21270 decl = cp_parser_primary_expression (parser, false, false,
21271 false, &idk);
21272 if (!cp_parser_error_occurred (parser)
21273 && decl
21274 && DECL_P (decl)
21275 && CLASS_TYPE_P (TREE_TYPE (decl)))
21276 {
21277 tree rhs;
21278
21279 cp_parser_parse_definitely (parser);
21280 cp_parser_require (parser, CPP_EQ, "%<=%>");
21281 rhs = cp_parser_assignment_expression (parser, false, NULL);
21282 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21283 rhs,
21284 tf_warning_or_error));
21285 add_private_clause = true;
21286 }
21287 else
21288 {
21289 decl = NULL;
21290 cp_parser_abort_tentative_parse (parser);
21291 init = cp_parser_expression (parser, false, NULL);
21292 if (init)
21293 {
21294 if (TREE_CODE (init) == MODIFY_EXPR
21295 || TREE_CODE (init) == MODOP_EXPR)
21296 real_decl = TREE_OPERAND (init, 0);
21297 }
21298 }
21299 }
21300 }
21301 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21302 if (this_pre_body)
21303 {
21304 this_pre_body = pop_stmt_list (this_pre_body);
21305 if (pre_body)
21306 {
21307 tree t = pre_body;
21308 pre_body = push_stmt_list ();
21309 add_stmt (t);
21310 add_stmt (this_pre_body);
21311 pre_body = pop_stmt_list (pre_body);
21312 }
21313 else
21314 pre_body = this_pre_body;
21315 }
21316
21317 if (decl)
21318 real_decl = decl;
21319 if (par_clauses != NULL && real_decl != NULL_TREE)
21320 {
21321 tree *c;
21322 for (c = par_clauses; *c ; )
21323 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21324 && OMP_CLAUSE_DECL (*c) == real_decl)
21325 {
21326 error ("%Hiteration variable %qD should not be firstprivate",
21327 &loc, real_decl);
21328 *c = OMP_CLAUSE_CHAIN (*c);
21329 }
21330 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21331 && OMP_CLAUSE_DECL (*c) == real_decl)
21332 {
21333 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21334 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21335 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21336 OMP_CLAUSE_DECL (l) = real_decl;
21337 OMP_CLAUSE_CHAIN (l) = clauses;
21338 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21339 clauses = l;
21340 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21341 CP_OMP_CLAUSE_INFO (*c) = NULL;
21342 add_private_clause = false;
21343 }
21344 else
21345 {
21346 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21347 && OMP_CLAUSE_DECL (*c) == real_decl)
21348 add_private_clause = false;
21349 c = &OMP_CLAUSE_CHAIN (*c);
21350 }
21351 }
21352
21353 if (add_private_clause)
21354 {
21355 tree c;
21356 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21357 {
21358 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21359 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21360 && OMP_CLAUSE_DECL (c) == decl)
21361 break;
21362 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21363 && OMP_CLAUSE_DECL (c) == decl)
21364 error ("%Hiteration variable %qD should not be firstprivate",
21365 &loc, decl);
21366 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21367 && OMP_CLAUSE_DECL (c) == decl)
21368 error ("%Hiteration variable %qD should not be reduction",
21369 &loc, decl);
21370 }
21371 if (c == NULL)
21372 {
21373 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21374 OMP_CLAUSE_DECL (c) = decl;
21375 c = finish_omp_clauses (c);
21376 if (c)
21377 {
21378 OMP_CLAUSE_CHAIN (c) = clauses;
21379 clauses = c;
21380 }
21381 }
21382 }
21383
21384 cond = NULL;
21385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21386 {
21387 /* If decl is an iterator, preserve LHS and RHS of the relational
21388 expr until finish_omp_for. */
21389 if (decl
21390 && (type_dependent_expression_p (decl)
21391 || CLASS_TYPE_P (TREE_TYPE (decl))))
21392 cond = cp_parser_omp_for_cond (parser, decl);
21393 else
21394 cond = cp_parser_condition (parser);
21395 }
21396 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21397
21398 incr = NULL;
21399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21400 {
21401 /* If decl is an iterator, preserve the operator on decl
21402 until finish_omp_for. */
21403 if (decl
21404 && (type_dependent_expression_p (decl)
21405 || CLASS_TYPE_P (TREE_TYPE (decl))))
21406 incr = cp_parser_omp_for_incr (parser, decl);
21407 else
21408 incr = cp_parser_expression (parser, false, NULL);
21409 }
21410
21411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21413 /*or_comma=*/false,
21414 /*consume_paren=*/true);
21415
21416 TREE_VEC_ELT (declv, i) = decl;
21417 TREE_VEC_ELT (initv, i) = init;
21418 TREE_VEC_ELT (condv, i) = cond;
21419 TREE_VEC_ELT (incrv, i) = incr;
21420
21421 if (i == collapse - 1)
21422 break;
21423
21424 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21425 in between the collapsed for loops to be still considered perfectly
21426 nested. Hopefully the final version clarifies this.
21427 For now handle (multiple) {'s and empty statements. */
21428 cp_parser_parse_tentatively (parser);
21429 do
21430 {
21431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21432 break;
21433 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21434 {
21435 cp_lexer_consume_token (parser->lexer);
21436 bracecount++;
21437 }
21438 else if (bracecount
21439 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21440 cp_lexer_consume_token (parser->lexer);
21441 else
21442 {
21443 loc = cp_lexer_peek_token (parser->lexer)->location;
21444 error ("%Hnot enough collapsed for loops", &loc);
21445 collapse_err = true;
21446 cp_parser_abort_tentative_parse (parser);
21447 declv = NULL_TREE;
21448 break;
21449 }
21450 }
21451 while (1);
21452
21453 if (declv)
21454 {
21455 cp_parser_parse_definitely (parser);
21456 nbraces += bracecount;
21457 }
21458 }
21459
21460 /* Note that we saved the original contents of this flag when we entered
21461 the structured block, and so we don't need to re-save it here. */
21462 parser->in_statement = IN_OMP_FOR;
21463
21464 /* Note that the grammar doesn't call for a structured block here,
21465 though the loop as a whole is a structured block. */
21466 body = push_stmt_list ();
21467 cp_parser_statement (parser, NULL_TREE, false, NULL);
21468 body = pop_stmt_list (body);
21469
21470 if (declv == NULL_TREE)
21471 ret = NULL_TREE;
21472 else
21473 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21474 pre_body, clauses);
21475
21476 while (nbraces)
21477 {
21478 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21479 {
21480 cp_lexer_consume_token (parser->lexer);
21481 nbraces--;
21482 }
21483 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21484 cp_lexer_consume_token (parser->lexer);
21485 else
21486 {
21487 if (!collapse_err)
21488 {
21489 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21490 error ("%Hcollapsed loops not perfectly nested", &loc);
21491 }
21492 collapse_err = true;
21493 cp_parser_statement_seq_opt (parser, NULL);
21494 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21495 }
21496 }
21497
21498 while (for_block)
21499 {
21500 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21501 for_block = TREE_CHAIN (for_block);
21502 }
21503
21504 return ret;
21505 }
21506
21507 /* OpenMP 2.5:
21508 #pragma omp for for-clause[optseq] new-line
21509 for-loop */
21510
21511 #define OMP_FOR_CLAUSE_MASK \
21512 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21513 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21514 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21515 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21516 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21517 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21518 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21519 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21520
21521 static tree
21522 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21523 {
21524 tree clauses, sb, ret;
21525 unsigned int save;
21526
21527 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21528 "#pragma omp for", pragma_tok);
21529
21530 sb = begin_omp_structured_block ();
21531 save = cp_parser_begin_omp_structured_block (parser);
21532
21533 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21534
21535 cp_parser_end_omp_structured_block (parser, save);
21536 add_stmt (finish_omp_structured_block (sb));
21537
21538 return ret;
21539 }
21540
21541 /* OpenMP 2.5:
21542 # pragma omp master new-line
21543 structured-block */
21544
21545 static tree
21546 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21547 {
21548 cp_parser_require_pragma_eol (parser, pragma_tok);
21549 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21550 }
21551
21552 /* OpenMP 2.5:
21553 # pragma omp ordered new-line
21554 structured-block */
21555
21556 static tree
21557 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21558 {
21559 cp_parser_require_pragma_eol (parser, pragma_tok);
21560 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21561 }
21562
21563 /* OpenMP 2.5:
21564
21565 section-scope:
21566 { section-sequence }
21567
21568 section-sequence:
21569 section-directive[opt] structured-block
21570 section-sequence section-directive structured-block */
21571
21572 static tree
21573 cp_parser_omp_sections_scope (cp_parser *parser)
21574 {
21575 tree stmt, substmt;
21576 bool error_suppress = false;
21577 cp_token *tok;
21578
21579 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21580 return NULL_TREE;
21581
21582 stmt = push_stmt_list ();
21583
21584 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21585 {
21586 unsigned save;
21587
21588 substmt = begin_omp_structured_block ();
21589 save = cp_parser_begin_omp_structured_block (parser);
21590
21591 while (1)
21592 {
21593 cp_parser_statement (parser, NULL_TREE, false, NULL);
21594
21595 tok = cp_lexer_peek_token (parser->lexer);
21596 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21597 break;
21598 if (tok->type == CPP_CLOSE_BRACE)
21599 break;
21600 if (tok->type == CPP_EOF)
21601 break;
21602 }
21603
21604 cp_parser_end_omp_structured_block (parser, save);
21605 substmt = finish_omp_structured_block (substmt);
21606 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21607 add_stmt (substmt);
21608 }
21609
21610 while (1)
21611 {
21612 tok = cp_lexer_peek_token (parser->lexer);
21613 if (tok->type == CPP_CLOSE_BRACE)
21614 break;
21615 if (tok->type == CPP_EOF)
21616 break;
21617
21618 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21619 {
21620 cp_lexer_consume_token (parser->lexer);
21621 cp_parser_require_pragma_eol (parser, tok);
21622 error_suppress = false;
21623 }
21624 else if (!error_suppress)
21625 {
21626 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21627 error_suppress = true;
21628 }
21629
21630 substmt = cp_parser_omp_structured_block (parser);
21631 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21632 add_stmt (substmt);
21633 }
21634 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21635
21636 substmt = pop_stmt_list (stmt);
21637
21638 stmt = make_node (OMP_SECTIONS);
21639 TREE_TYPE (stmt) = void_type_node;
21640 OMP_SECTIONS_BODY (stmt) = substmt;
21641
21642 add_stmt (stmt);
21643 return stmt;
21644 }
21645
21646 /* OpenMP 2.5:
21647 # pragma omp sections sections-clause[optseq] newline
21648 sections-scope */
21649
21650 #define OMP_SECTIONS_CLAUSE_MASK \
21651 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21652 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21653 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21654 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21655 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21656
21657 static tree
21658 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21659 {
21660 tree clauses, ret;
21661
21662 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21663 "#pragma omp sections", pragma_tok);
21664
21665 ret = cp_parser_omp_sections_scope (parser);
21666 if (ret)
21667 OMP_SECTIONS_CLAUSES (ret) = clauses;
21668
21669 return ret;
21670 }
21671
21672 /* OpenMP 2.5:
21673 # pragma parallel parallel-clause new-line
21674 # pragma parallel for parallel-for-clause new-line
21675 # pragma parallel sections parallel-sections-clause new-line */
21676
21677 #define OMP_PARALLEL_CLAUSE_MASK \
21678 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21679 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21680 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21681 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21682 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21683 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21684 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21685 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21686
21687 static tree
21688 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21689 {
21690 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21691 const char *p_name = "#pragma omp parallel";
21692 tree stmt, clauses, par_clause, ws_clause, block;
21693 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21694 unsigned int save;
21695
21696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21697 {
21698 cp_lexer_consume_token (parser->lexer);
21699 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21700 p_name = "#pragma omp parallel for";
21701 mask |= OMP_FOR_CLAUSE_MASK;
21702 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21703 }
21704 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21705 {
21706 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21707 const char *p = IDENTIFIER_POINTER (id);
21708 if (strcmp (p, "sections") == 0)
21709 {
21710 cp_lexer_consume_token (parser->lexer);
21711 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21712 p_name = "#pragma omp parallel sections";
21713 mask |= OMP_SECTIONS_CLAUSE_MASK;
21714 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21715 }
21716 }
21717
21718 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21719 block = begin_omp_parallel ();
21720 save = cp_parser_begin_omp_structured_block (parser);
21721
21722 switch (p_kind)
21723 {
21724 case PRAGMA_OMP_PARALLEL:
21725 cp_parser_statement (parser, NULL_TREE, false, NULL);
21726 par_clause = clauses;
21727 break;
21728
21729 case PRAGMA_OMP_PARALLEL_FOR:
21730 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21731 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21732 break;
21733
21734 case PRAGMA_OMP_PARALLEL_SECTIONS:
21735 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21736 stmt = cp_parser_omp_sections_scope (parser);
21737 if (stmt)
21738 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21739 break;
21740
21741 default:
21742 gcc_unreachable ();
21743 }
21744
21745 cp_parser_end_omp_structured_block (parser, save);
21746 stmt = finish_omp_parallel (par_clause, block);
21747 if (p_kind != PRAGMA_OMP_PARALLEL)
21748 OMP_PARALLEL_COMBINED (stmt) = 1;
21749 return stmt;
21750 }
21751
21752 /* OpenMP 2.5:
21753 # pragma omp single single-clause[optseq] new-line
21754 structured-block */
21755
21756 #define OMP_SINGLE_CLAUSE_MASK \
21757 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21758 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21759 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21760 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21761
21762 static tree
21763 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21764 {
21765 tree stmt = make_node (OMP_SINGLE);
21766 TREE_TYPE (stmt) = void_type_node;
21767
21768 OMP_SINGLE_CLAUSES (stmt)
21769 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21770 "#pragma omp single", pragma_tok);
21771 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21772
21773 return add_stmt (stmt);
21774 }
21775
21776 /* OpenMP 3.0:
21777 # pragma omp task task-clause[optseq] new-line
21778 structured-block */
21779
21780 #define OMP_TASK_CLAUSE_MASK \
21781 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21782 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21783 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21784 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21785 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21786 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21787
21788 static tree
21789 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21790 {
21791 tree clauses, block;
21792 unsigned int save;
21793
21794 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21795 "#pragma omp task", pragma_tok);
21796 block = begin_omp_task ();
21797 save = cp_parser_begin_omp_structured_block (parser);
21798 cp_parser_statement (parser, NULL_TREE, false, NULL);
21799 cp_parser_end_omp_structured_block (parser, save);
21800 return finish_omp_task (clauses, block);
21801 }
21802
21803 /* OpenMP 3.0:
21804 # pragma omp taskwait new-line */
21805
21806 static void
21807 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21808 {
21809 cp_parser_require_pragma_eol (parser, pragma_tok);
21810 finish_omp_taskwait ();
21811 }
21812
21813 /* OpenMP 2.5:
21814 # pragma omp threadprivate (variable-list) */
21815
21816 static void
21817 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21818 {
21819 tree vars;
21820
21821 vars = cp_parser_omp_var_list (parser, 0, NULL);
21822 cp_parser_require_pragma_eol (parser, pragma_tok);
21823
21824 finish_omp_threadprivate (vars);
21825 }
21826
21827 /* Main entry point to OpenMP statement pragmas. */
21828
21829 static void
21830 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21831 {
21832 tree stmt;
21833
21834 switch (pragma_tok->pragma_kind)
21835 {
21836 case PRAGMA_OMP_ATOMIC:
21837 cp_parser_omp_atomic (parser, pragma_tok);
21838 return;
21839 case PRAGMA_OMP_CRITICAL:
21840 stmt = cp_parser_omp_critical (parser, pragma_tok);
21841 break;
21842 case PRAGMA_OMP_FOR:
21843 stmt = cp_parser_omp_for (parser, pragma_tok);
21844 break;
21845 case PRAGMA_OMP_MASTER:
21846 stmt = cp_parser_omp_master (parser, pragma_tok);
21847 break;
21848 case PRAGMA_OMP_ORDERED:
21849 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21850 break;
21851 case PRAGMA_OMP_PARALLEL:
21852 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21853 break;
21854 case PRAGMA_OMP_SECTIONS:
21855 stmt = cp_parser_omp_sections (parser, pragma_tok);
21856 break;
21857 case PRAGMA_OMP_SINGLE:
21858 stmt = cp_parser_omp_single (parser, pragma_tok);
21859 break;
21860 case PRAGMA_OMP_TASK:
21861 stmt = cp_parser_omp_task (parser, pragma_tok);
21862 break;
21863 default:
21864 gcc_unreachable ();
21865 }
21866
21867 if (stmt)
21868 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21869 }
21870 \f
21871 /* The parser. */
21872
21873 static GTY (()) cp_parser *the_parser;
21874
21875 \f
21876 /* Special handling for the first token or line in the file. The first
21877 thing in the file might be #pragma GCC pch_preprocess, which loads a
21878 PCH file, which is a GC collection point. So we need to handle this
21879 first pragma without benefit of an existing lexer structure.
21880
21881 Always returns one token to the caller in *FIRST_TOKEN. This is
21882 either the true first token of the file, or the first token after
21883 the initial pragma. */
21884
21885 static void
21886 cp_parser_initial_pragma (cp_token *first_token)
21887 {
21888 tree name = NULL;
21889
21890 cp_lexer_get_preprocessor_token (NULL, first_token);
21891 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21892 return;
21893
21894 cp_lexer_get_preprocessor_token (NULL, first_token);
21895 if (first_token->type == CPP_STRING)
21896 {
21897 name = first_token->u.value;
21898
21899 cp_lexer_get_preprocessor_token (NULL, first_token);
21900 if (first_token->type != CPP_PRAGMA_EOL)
21901 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21902 &first_token->location);
21903 }
21904 else
21905 error ("%Hexpected string literal", &first_token->location);
21906
21907 /* Skip to the end of the pragma. */
21908 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21909 cp_lexer_get_preprocessor_token (NULL, first_token);
21910
21911 /* Now actually load the PCH file. */
21912 if (name)
21913 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21914
21915 /* Read one more token to return to our caller. We have to do this
21916 after reading the PCH file in, since its pointers have to be
21917 live. */
21918 cp_lexer_get_preprocessor_token (NULL, first_token);
21919 }
21920
21921 /* Normal parsing of a pragma token. Here we can (and must) use the
21922 regular lexer. */
21923
21924 static bool
21925 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21926 {
21927 cp_token *pragma_tok;
21928 unsigned int id;
21929
21930 pragma_tok = cp_lexer_consume_token (parser->lexer);
21931 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21932 parser->lexer->in_pragma = true;
21933
21934 id = pragma_tok->pragma_kind;
21935 switch (id)
21936 {
21937 case PRAGMA_GCC_PCH_PREPROCESS:
21938 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21939 &pragma_tok->location);
21940 break;
21941
21942 case PRAGMA_OMP_BARRIER:
21943 switch (context)
21944 {
21945 case pragma_compound:
21946 cp_parser_omp_barrier (parser, pragma_tok);
21947 return false;
21948 case pragma_stmt:
21949 error ("%H%<#pragma omp barrier%> may only be "
21950 "used in compound statements", &pragma_tok->location);
21951 break;
21952 default:
21953 goto bad_stmt;
21954 }
21955 break;
21956
21957 case PRAGMA_OMP_FLUSH:
21958 switch (context)
21959 {
21960 case pragma_compound:
21961 cp_parser_omp_flush (parser, pragma_tok);
21962 return false;
21963 case pragma_stmt:
21964 error ("%H%<#pragma omp flush%> may only be "
21965 "used in compound statements", &pragma_tok->location);
21966 break;
21967 default:
21968 goto bad_stmt;
21969 }
21970 break;
21971
21972 case PRAGMA_OMP_TASKWAIT:
21973 switch (context)
21974 {
21975 case pragma_compound:
21976 cp_parser_omp_taskwait (parser, pragma_tok);
21977 return false;
21978 case pragma_stmt:
21979 error ("%H%<#pragma omp taskwait%> may only be "
21980 "used in compound statements",
21981 &pragma_tok->location);
21982 break;
21983 default:
21984 goto bad_stmt;
21985 }
21986 break;
21987
21988 case PRAGMA_OMP_THREADPRIVATE:
21989 cp_parser_omp_threadprivate (parser, pragma_tok);
21990 return false;
21991
21992 case PRAGMA_OMP_ATOMIC:
21993 case PRAGMA_OMP_CRITICAL:
21994 case PRAGMA_OMP_FOR:
21995 case PRAGMA_OMP_MASTER:
21996 case PRAGMA_OMP_ORDERED:
21997 case PRAGMA_OMP_PARALLEL:
21998 case PRAGMA_OMP_SECTIONS:
21999 case PRAGMA_OMP_SINGLE:
22000 case PRAGMA_OMP_TASK:
22001 if (context == pragma_external)
22002 goto bad_stmt;
22003 cp_parser_omp_construct (parser, pragma_tok);
22004 return true;
22005
22006 case PRAGMA_OMP_SECTION:
22007 error ("%H%<#pragma omp section%> may only be used in "
22008 "%<#pragma omp sections%> construct", &pragma_tok->location);
22009 break;
22010
22011 default:
22012 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22013 c_invoke_pragma_handler (id);
22014 break;
22015
22016 bad_stmt:
22017 cp_parser_error (parser, "expected declaration specifiers");
22018 break;
22019 }
22020
22021 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22022 return false;
22023 }
22024
22025 /* The interface the pragma parsers have to the lexer. */
22026
22027 enum cpp_ttype
22028 pragma_lex (tree *value)
22029 {
22030 cp_token *tok;
22031 enum cpp_ttype ret;
22032
22033 tok = cp_lexer_peek_token (the_parser->lexer);
22034
22035 ret = tok->type;
22036 *value = tok->u.value;
22037
22038 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22039 ret = CPP_EOF;
22040 else if (ret == CPP_STRING)
22041 *value = cp_parser_string_literal (the_parser, false, false);
22042 else
22043 {
22044 cp_lexer_consume_token (the_parser->lexer);
22045 if (ret == CPP_KEYWORD)
22046 ret = CPP_NAME;
22047 }
22048
22049 return ret;
22050 }
22051
22052 \f
22053 /* External interface. */
22054
22055 /* Parse one entire translation unit. */
22056
22057 void
22058 c_parse_file (void)
22059 {
22060 bool error_occurred;
22061 static bool already_called = false;
22062
22063 if (already_called)
22064 {
22065 sorry ("inter-module optimizations not implemented for C++");
22066 return;
22067 }
22068 already_called = true;
22069
22070 the_parser = cp_parser_new ();
22071 push_deferring_access_checks (flag_access_control
22072 ? dk_no_deferred : dk_no_check);
22073 error_occurred = cp_parser_translation_unit (the_parser);
22074 the_parser = NULL;
22075 }
22076
22077 #include "gt-cp-parser.h"