bf742eea079bd771c6ed3f9cc6bf34664f84974e
[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);
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);
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);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625 (cp_parser *);
1626 static tree cp_parser_expression
1627 (cp_parser *, bool);
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);
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);
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 type_decl
3884 = cp_parser_class_name (parser,
3885 /*typename_keyword_p=*/false,
3886 /*template_keyword_p=*/false,
3887 none_type,
3888 /*check_dependency=*/false,
3889 /*class_head_p=*/false,
3890 declarator_p);
3891 }
3892 /* If an error occurred, assume that the name of the
3893 destructor is the same as the name of the qualifying
3894 class. That allows us to keep parsing after running
3895 into ill-formed destructor names. */
3896 if (type_decl == error_mark_node && scope)
3897 return build_nt (BIT_NOT_EXPR, scope);
3898 else if (type_decl == error_mark_node)
3899 return error_mark_node;
3900
3901 /* Check that destructor name and scope match. */
3902 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3903 {
3904 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3905 error ("%Hdeclaration of %<~%T%> as member of %qT",
3906 &token->location, type_decl, scope);
3907 cp_parser_simulate_error (parser);
3908 return error_mark_node;
3909 }
3910
3911 /* [class.dtor]
3912
3913 A typedef-name that names a class shall not be used as the
3914 identifier in the declarator for a destructor declaration. */
3915 if (declarator_p
3916 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3917 && !DECL_SELF_REFERENCE_P (type_decl)
3918 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3919 error ("%Htypedef-name %qD used as destructor declarator",
3920 &token->location, type_decl);
3921
3922 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3923 }
3924
3925 case CPP_KEYWORD:
3926 if (token->keyword == RID_OPERATOR)
3927 {
3928 tree id;
3929
3930 /* This could be a template-id, so we try that first. */
3931 cp_parser_parse_tentatively (parser);
3932 /* Try a template-id. */
3933 id = cp_parser_template_id (parser, template_keyword_p,
3934 /*check_dependency_p=*/true,
3935 declarator_p);
3936 /* If that worked, we're done. */
3937 if (cp_parser_parse_definitely (parser))
3938 return id;
3939 /* We still don't know whether we're looking at an
3940 operator-function-id or a conversion-function-id. */
3941 cp_parser_parse_tentatively (parser);
3942 /* Try an operator-function-id. */
3943 id = cp_parser_operator_function_id (parser);
3944 /* If that didn't work, try a conversion-function-id. */
3945 if (!cp_parser_parse_definitely (parser))
3946 id = cp_parser_conversion_function_id (parser);
3947
3948 return id;
3949 }
3950 /* Fall through. */
3951
3952 default:
3953 if (optional_p)
3954 return NULL_TREE;
3955 cp_parser_error (parser, "expected unqualified-id");
3956 return error_mark_node;
3957 }
3958 }
3959
3960 /* Parse an (optional) nested-name-specifier.
3961
3962 nested-name-specifier: [C++98]
3963 class-or-namespace-name :: nested-name-specifier [opt]
3964 class-or-namespace-name :: template nested-name-specifier [opt]
3965
3966 nested-name-specifier: [C++0x]
3967 type-name ::
3968 namespace-name ::
3969 nested-name-specifier identifier ::
3970 nested-name-specifier template [opt] simple-template-id ::
3971
3972 PARSER->SCOPE should be set appropriately before this function is
3973 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3974 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3975 in name lookups.
3976
3977 Sets PARSER->SCOPE to the class (TYPE) or namespace
3978 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3979 it unchanged if there is no nested-name-specifier. Returns the new
3980 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3981
3982 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3983 part of a declaration and/or decl-specifier. */
3984
3985 static tree
3986 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3987 bool typename_keyword_p,
3988 bool check_dependency_p,
3989 bool type_p,
3990 bool is_declaration)
3991 {
3992 bool success = false;
3993 cp_token_position start = 0;
3994 cp_token *token;
3995
3996 /* Remember where the nested-name-specifier starts. */
3997 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3998 {
3999 start = cp_lexer_token_position (parser->lexer, false);
4000 push_deferring_access_checks (dk_deferred);
4001 }
4002
4003 while (true)
4004 {
4005 tree new_scope;
4006 tree old_scope;
4007 tree saved_qualifying_scope;
4008 bool template_keyword_p;
4009
4010 /* Spot cases that cannot be the beginning of a
4011 nested-name-specifier. */
4012 token = cp_lexer_peek_token (parser->lexer);
4013
4014 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4015 the already parsed nested-name-specifier. */
4016 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4017 {
4018 /* Grab the nested-name-specifier and continue the loop. */
4019 cp_parser_pre_parsed_nested_name_specifier (parser);
4020 /* If we originally encountered this nested-name-specifier
4021 with IS_DECLARATION set to false, we will not have
4022 resolved TYPENAME_TYPEs, so we must do so here. */
4023 if (is_declaration
4024 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4025 {
4026 new_scope = resolve_typename_type (parser->scope,
4027 /*only_current_p=*/false);
4028 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4029 parser->scope = new_scope;
4030 }
4031 success = true;
4032 continue;
4033 }
4034
4035 /* Spot cases that cannot be the beginning of a
4036 nested-name-specifier. On the second and subsequent times
4037 through the loop, we look for the `template' keyword. */
4038 if (success && token->keyword == RID_TEMPLATE)
4039 ;
4040 /* A template-id can start a nested-name-specifier. */
4041 else if (token->type == CPP_TEMPLATE_ID)
4042 ;
4043 else
4044 {
4045 /* If the next token is not an identifier, then it is
4046 definitely not a type-name or namespace-name. */
4047 if (token->type != CPP_NAME)
4048 break;
4049 /* If the following token is neither a `<' (to begin a
4050 template-id), nor a `::', then we are not looking at a
4051 nested-name-specifier. */
4052 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4053 if (token->type != CPP_SCOPE
4054 && !cp_parser_nth_token_starts_template_argument_list_p
4055 (parser, 2))
4056 break;
4057 }
4058
4059 /* The nested-name-specifier is optional, so we parse
4060 tentatively. */
4061 cp_parser_parse_tentatively (parser);
4062
4063 /* Look for the optional `template' keyword, if this isn't the
4064 first time through the loop. */
4065 if (success)
4066 template_keyword_p = cp_parser_optional_template_keyword (parser);
4067 else
4068 template_keyword_p = false;
4069
4070 /* Save the old scope since the name lookup we are about to do
4071 might destroy it. */
4072 old_scope = parser->scope;
4073 saved_qualifying_scope = parser->qualifying_scope;
4074 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4075 look up names in "X<T>::I" in order to determine that "Y" is
4076 a template. So, if we have a typename at this point, we make
4077 an effort to look through it. */
4078 if (is_declaration
4079 && !typename_keyword_p
4080 && parser->scope
4081 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4082 parser->scope = resolve_typename_type (parser->scope,
4083 /*only_current_p=*/false);
4084 /* Parse the qualifying entity. */
4085 new_scope
4086 = cp_parser_qualifying_entity (parser,
4087 typename_keyword_p,
4088 template_keyword_p,
4089 check_dependency_p,
4090 type_p,
4091 is_declaration);
4092 /* Look for the `::' token. */
4093 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4094
4095 /* If we found what we wanted, we keep going; otherwise, we're
4096 done. */
4097 if (!cp_parser_parse_definitely (parser))
4098 {
4099 bool error_p = false;
4100
4101 /* Restore the OLD_SCOPE since it was valid before the
4102 failed attempt at finding the last
4103 class-or-namespace-name. */
4104 parser->scope = old_scope;
4105 parser->qualifying_scope = saved_qualifying_scope;
4106 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4107 break;
4108 /* If the next token is an identifier, and the one after
4109 that is a `::', then any valid interpretation would have
4110 found a class-or-namespace-name. */
4111 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4112 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4113 == CPP_SCOPE)
4114 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4115 != CPP_COMPL))
4116 {
4117 token = cp_lexer_consume_token (parser->lexer);
4118 if (!error_p)
4119 {
4120 if (!token->ambiguous_p)
4121 {
4122 tree decl;
4123 tree ambiguous_decls;
4124
4125 decl = cp_parser_lookup_name (parser, token->u.value,
4126 none_type,
4127 /*is_template=*/false,
4128 /*is_namespace=*/false,
4129 /*check_dependency=*/true,
4130 &ambiguous_decls,
4131 token->location);
4132 if (TREE_CODE (decl) == TEMPLATE_DECL)
4133 error ("%H%qD used without template parameters",
4134 &token->location, decl);
4135 else if (ambiguous_decls)
4136 {
4137 error ("%Hreference to %qD is ambiguous",
4138 &token->location, token->u.value);
4139 print_candidates (ambiguous_decls);
4140 decl = error_mark_node;
4141 }
4142 else
4143 {
4144 const char* msg = "is not a class or namespace";
4145 if (cxx_dialect != cxx98)
4146 msg = "is not a class, namespace, or enumeration";
4147 cp_parser_name_lookup_error
4148 (parser, token->u.value, decl, msg,
4149 token->location);
4150 }
4151 }
4152 parser->scope = error_mark_node;
4153 error_p = true;
4154 /* Treat this as a successful nested-name-specifier
4155 due to:
4156
4157 [basic.lookup.qual]
4158
4159 If the name found is not a class-name (clause
4160 _class_) or namespace-name (_namespace.def_), the
4161 program is ill-formed. */
4162 success = true;
4163 }
4164 cp_lexer_consume_token (parser->lexer);
4165 }
4166 break;
4167 }
4168 /* We've found one valid nested-name-specifier. */
4169 success = true;
4170 /* Name lookup always gives us a DECL. */
4171 if (TREE_CODE (new_scope) == TYPE_DECL)
4172 new_scope = TREE_TYPE (new_scope);
4173 /* Uses of "template" must be followed by actual templates. */
4174 if (template_keyword_p
4175 && !(CLASS_TYPE_P (new_scope)
4176 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4177 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4178 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4179 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4180 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4181 == TEMPLATE_ID_EXPR)))
4182 permerror (input_location, TYPE_P (new_scope)
4183 ? "%qT is not a template"
4184 : "%qD is not a template",
4185 new_scope);
4186 /* If it is a class scope, try to complete it; we are about to
4187 be looking up names inside the class. */
4188 if (TYPE_P (new_scope)
4189 /* Since checking types for dependency can be expensive,
4190 avoid doing it if the type is already complete. */
4191 && !COMPLETE_TYPE_P (new_scope)
4192 /* Do not try to complete dependent types. */
4193 && !dependent_type_p (new_scope))
4194 {
4195 new_scope = complete_type (new_scope);
4196 /* If it is a typedef to current class, use the current
4197 class instead, as the typedef won't have any names inside
4198 it yet. */
4199 if (!COMPLETE_TYPE_P (new_scope)
4200 && currently_open_class (new_scope))
4201 new_scope = TYPE_MAIN_VARIANT (new_scope);
4202 }
4203 /* Make sure we look in the right scope the next time through
4204 the loop. */
4205 parser->scope = new_scope;
4206 }
4207
4208 /* If parsing tentatively, replace the sequence of tokens that makes
4209 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4210 token. That way, should we re-parse the token stream, we will
4211 not have to repeat the effort required to do the parse, nor will
4212 we issue duplicate error messages. */
4213 if (success && start)
4214 {
4215 cp_token *token;
4216
4217 token = cp_lexer_token_at (parser->lexer, start);
4218 /* Reset the contents of the START token. */
4219 token->type = CPP_NESTED_NAME_SPECIFIER;
4220 /* Retrieve any deferred checks. Do not pop this access checks yet
4221 so the memory will not be reclaimed during token replacing below. */
4222 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4223 token->u.tree_check_value->value = parser->scope;
4224 token->u.tree_check_value->checks = get_deferred_access_checks ();
4225 token->u.tree_check_value->qualifying_scope =
4226 parser->qualifying_scope;
4227 token->keyword = RID_MAX;
4228
4229 /* Purge all subsequent tokens. */
4230 cp_lexer_purge_tokens_after (parser->lexer, start);
4231 }
4232
4233 if (start)
4234 pop_to_parent_deferring_access_checks ();
4235
4236 return success ? parser->scope : NULL_TREE;
4237 }
4238
4239 /* Parse a nested-name-specifier. See
4240 cp_parser_nested_name_specifier_opt for details. This function
4241 behaves identically, except that it will an issue an error if no
4242 nested-name-specifier is present. */
4243
4244 static tree
4245 cp_parser_nested_name_specifier (cp_parser *parser,
4246 bool typename_keyword_p,
4247 bool check_dependency_p,
4248 bool type_p,
4249 bool is_declaration)
4250 {
4251 tree scope;
4252
4253 /* Look for the nested-name-specifier. */
4254 scope = cp_parser_nested_name_specifier_opt (parser,
4255 typename_keyword_p,
4256 check_dependency_p,
4257 type_p,
4258 is_declaration);
4259 /* If it was not present, issue an error message. */
4260 if (!scope)
4261 {
4262 cp_parser_error (parser, "expected nested-name-specifier");
4263 parser->scope = NULL_TREE;
4264 }
4265
4266 return scope;
4267 }
4268
4269 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4270 this is either a class-name or a namespace-name (which corresponds
4271 to the class-or-namespace-name production in the grammar). For
4272 C++0x, it can also be a type-name that refers to an enumeration
4273 type.
4274
4275 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4276 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4277 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4278 TYPE_P is TRUE iff the next name should be taken as a class-name,
4279 even the same name is declared to be another entity in the same
4280 scope.
4281
4282 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4283 specified by the class-or-namespace-name. If neither is found the
4284 ERROR_MARK_NODE is returned. */
4285
4286 static tree
4287 cp_parser_qualifying_entity (cp_parser *parser,
4288 bool typename_keyword_p,
4289 bool template_keyword_p,
4290 bool check_dependency_p,
4291 bool type_p,
4292 bool is_declaration)
4293 {
4294 tree saved_scope;
4295 tree saved_qualifying_scope;
4296 tree saved_object_scope;
4297 tree scope;
4298 bool only_class_p;
4299 bool successful_parse_p;
4300
4301 /* Before we try to parse the class-name, we must save away the
4302 current PARSER->SCOPE since cp_parser_class_name will destroy
4303 it. */
4304 saved_scope = parser->scope;
4305 saved_qualifying_scope = parser->qualifying_scope;
4306 saved_object_scope = parser->object_scope;
4307 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4308 there is no need to look for a namespace-name. */
4309 only_class_p = template_keyword_p
4310 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4311 if (!only_class_p)
4312 cp_parser_parse_tentatively (parser);
4313 scope = cp_parser_class_name (parser,
4314 typename_keyword_p,
4315 template_keyword_p,
4316 type_p ? class_type : none_type,
4317 check_dependency_p,
4318 /*class_head_p=*/false,
4319 is_declaration);
4320 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4321 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4322 if (!only_class_p
4323 && cxx_dialect != cxx98
4324 && !successful_parse_p)
4325 {
4326 /* Restore the saved scope. */
4327 parser->scope = saved_scope;
4328 parser->qualifying_scope = saved_qualifying_scope;
4329 parser->object_scope = saved_object_scope;
4330
4331 /* Parse tentatively. */
4332 cp_parser_parse_tentatively (parser);
4333
4334 /* Parse a typedef-name or enum-name. */
4335 scope = cp_parser_nonclass_name (parser);
4336 successful_parse_p = cp_parser_parse_definitely (parser);
4337 }
4338 /* If that didn't work, try for a namespace-name. */
4339 if (!only_class_p && !successful_parse_p)
4340 {
4341 /* Restore the saved scope. */
4342 parser->scope = saved_scope;
4343 parser->qualifying_scope = saved_qualifying_scope;
4344 parser->object_scope = saved_object_scope;
4345 /* If we are not looking at an identifier followed by the scope
4346 resolution operator, then this is not part of a
4347 nested-name-specifier. (Note that this function is only used
4348 to parse the components of a nested-name-specifier.) */
4349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4350 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4351 return error_mark_node;
4352 scope = cp_parser_namespace_name (parser);
4353 }
4354
4355 return scope;
4356 }
4357
4358 /* Parse a postfix-expression.
4359
4360 postfix-expression:
4361 primary-expression
4362 postfix-expression [ expression ]
4363 postfix-expression ( expression-list [opt] )
4364 simple-type-specifier ( expression-list [opt] )
4365 typename :: [opt] nested-name-specifier identifier
4366 ( expression-list [opt] )
4367 typename :: [opt] nested-name-specifier template [opt] template-id
4368 ( expression-list [opt] )
4369 postfix-expression . template [opt] id-expression
4370 postfix-expression -> template [opt] id-expression
4371 postfix-expression . pseudo-destructor-name
4372 postfix-expression -> pseudo-destructor-name
4373 postfix-expression ++
4374 postfix-expression --
4375 dynamic_cast < type-id > ( expression )
4376 static_cast < type-id > ( expression )
4377 reinterpret_cast < type-id > ( expression )
4378 const_cast < type-id > ( expression )
4379 typeid ( expression )
4380 typeid ( type-id )
4381
4382 GNU Extension:
4383
4384 postfix-expression:
4385 ( type-id ) { initializer-list , [opt] }
4386
4387 This extension is a GNU version of the C99 compound-literal
4388 construct. (The C99 grammar uses `type-name' instead of `type-id',
4389 but they are essentially the same concept.)
4390
4391 If ADDRESS_P is true, the postfix expression is the operand of the
4392 `&' operator. CAST_P is true if this expression is the target of a
4393 cast.
4394
4395 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4396 class member access expressions [expr.ref].
4397
4398 Returns a representation of the expression. */
4399
4400 static tree
4401 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4402 bool member_access_only_p)
4403 {
4404 cp_token *token;
4405 enum rid keyword;
4406 cp_id_kind idk = CP_ID_KIND_NONE;
4407 tree postfix_expression = NULL_TREE;
4408 bool is_member_access = false;
4409
4410 /* Peek at the next token. */
4411 token = cp_lexer_peek_token (parser->lexer);
4412 /* Some of the productions are determined by keywords. */
4413 keyword = token->keyword;
4414 switch (keyword)
4415 {
4416 case RID_DYNCAST:
4417 case RID_STATCAST:
4418 case RID_REINTCAST:
4419 case RID_CONSTCAST:
4420 {
4421 tree type;
4422 tree expression;
4423 const char *saved_message;
4424
4425 /* All of these can be handled in the same way from the point
4426 of view of parsing. Begin by consuming the token
4427 identifying the cast. */
4428 cp_lexer_consume_token (parser->lexer);
4429
4430 /* New types cannot be defined in the cast. */
4431 saved_message = parser->type_definition_forbidden_message;
4432 parser->type_definition_forbidden_message
4433 = "types may not be defined in casts";
4434
4435 /* Look for the opening `<'. */
4436 cp_parser_require (parser, CPP_LESS, "%<<%>");
4437 /* Parse the type to which we are casting. */
4438 type = cp_parser_type_id (parser);
4439 /* Look for the closing `>'. */
4440 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4441 /* Restore the old message. */
4442 parser->type_definition_forbidden_message = saved_message;
4443
4444 /* And the expression which is being cast. */
4445 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4446 expression = cp_parser_expression (parser, /*cast_p=*/true);
4447 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4448
4449 /* Only type conversions to integral or enumeration types
4450 can be used in constant-expressions. */
4451 if (!cast_valid_in_integral_constant_expression_p (type)
4452 && (cp_parser_non_integral_constant_expression
4453 (parser,
4454 "a cast to a type other than an integral or "
4455 "enumeration type")))
4456 return error_mark_node;
4457
4458 switch (keyword)
4459 {
4460 case RID_DYNCAST:
4461 postfix_expression
4462 = build_dynamic_cast (type, expression, tf_warning_or_error);
4463 break;
4464 case RID_STATCAST:
4465 postfix_expression
4466 = build_static_cast (type, expression, tf_warning_or_error);
4467 break;
4468 case RID_REINTCAST:
4469 postfix_expression
4470 = build_reinterpret_cast (type, expression,
4471 tf_warning_or_error);
4472 break;
4473 case RID_CONSTCAST:
4474 postfix_expression
4475 = build_const_cast (type, expression, tf_warning_or_error);
4476 break;
4477 default:
4478 gcc_unreachable ();
4479 }
4480 }
4481 break;
4482
4483 case RID_TYPEID:
4484 {
4485 tree type;
4486 const char *saved_message;
4487 bool saved_in_type_id_in_expr_p;
4488
4489 /* Consume the `typeid' token. */
4490 cp_lexer_consume_token (parser->lexer);
4491 /* Look for the `(' token. */
4492 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4493 /* Types cannot be defined in a `typeid' expression. */
4494 saved_message = parser->type_definition_forbidden_message;
4495 parser->type_definition_forbidden_message
4496 = "types may not be defined in a %<typeid%> expression";
4497 /* We can't be sure yet whether we're looking at a type-id or an
4498 expression. */
4499 cp_parser_parse_tentatively (parser);
4500 /* Try a type-id first. */
4501 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4502 parser->in_type_id_in_expr_p = true;
4503 type = cp_parser_type_id (parser);
4504 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4505 /* Look for the `)' token. Otherwise, we can't be sure that
4506 we're not looking at an expression: consider `typeid (int
4507 (3))', for example. */
4508 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4509 /* If all went well, simply lookup the type-id. */
4510 if (cp_parser_parse_definitely (parser))
4511 postfix_expression = get_typeid (type);
4512 /* Otherwise, fall back to the expression variant. */
4513 else
4514 {
4515 tree expression;
4516
4517 /* Look for an expression. */
4518 expression = cp_parser_expression (parser, /*cast_p=*/false);
4519 /* Compute its typeid. */
4520 postfix_expression = build_typeid (expression);
4521 /* Look for the `)' token. */
4522 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4523 }
4524 /* Restore the saved message. */
4525 parser->type_definition_forbidden_message = saved_message;
4526 /* `typeid' may not appear in an integral constant expression. */
4527 if (cp_parser_non_integral_constant_expression(parser,
4528 "%<typeid%> operator"))
4529 return error_mark_node;
4530 }
4531 break;
4532
4533 case RID_TYPENAME:
4534 {
4535 tree type;
4536 /* The syntax permitted here is the same permitted for an
4537 elaborated-type-specifier. */
4538 type = cp_parser_elaborated_type_specifier (parser,
4539 /*is_friend=*/false,
4540 /*is_declaration=*/false);
4541 postfix_expression = cp_parser_functional_cast (parser, type);
4542 }
4543 break;
4544
4545 default:
4546 {
4547 tree type;
4548
4549 /* If the next thing is a simple-type-specifier, we may be
4550 looking at a functional cast. We could also be looking at
4551 an id-expression. So, we try the functional cast, and if
4552 that doesn't work we fall back to the primary-expression. */
4553 cp_parser_parse_tentatively (parser);
4554 /* Look for the simple-type-specifier. */
4555 type = cp_parser_simple_type_specifier (parser,
4556 /*decl_specs=*/NULL,
4557 CP_PARSER_FLAGS_NONE);
4558 /* Parse the cast itself. */
4559 if (!cp_parser_error_occurred (parser))
4560 postfix_expression
4561 = cp_parser_functional_cast (parser, type);
4562 /* If that worked, we're done. */
4563 if (cp_parser_parse_definitely (parser))
4564 break;
4565
4566 /* If the functional-cast didn't work out, try a
4567 compound-literal. */
4568 if (cp_parser_allow_gnu_extensions_p (parser)
4569 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4570 {
4571 VEC(constructor_elt,gc) *initializer_list = NULL;
4572 bool saved_in_type_id_in_expr_p;
4573
4574 cp_parser_parse_tentatively (parser);
4575 /* Consume the `('. */
4576 cp_lexer_consume_token (parser->lexer);
4577 /* Parse the type. */
4578 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4579 parser->in_type_id_in_expr_p = true;
4580 type = cp_parser_type_id (parser);
4581 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4582 /* Look for the `)'. */
4583 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4584 /* Look for the `{'. */
4585 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4586 /* If things aren't going well, there's no need to
4587 keep going. */
4588 if (!cp_parser_error_occurred (parser))
4589 {
4590 bool non_constant_p;
4591 /* Parse the initializer-list. */
4592 initializer_list
4593 = cp_parser_initializer_list (parser, &non_constant_p);
4594 /* Allow a trailing `,'. */
4595 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4596 cp_lexer_consume_token (parser->lexer);
4597 /* Look for the final `}'. */
4598 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4599 }
4600 /* If that worked, we're definitely looking at a
4601 compound-literal expression. */
4602 if (cp_parser_parse_definitely (parser))
4603 {
4604 /* Warn the user that a compound literal is not
4605 allowed in standard C++. */
4606 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4607 /* For simplicity, we disallow compound literals in
4608 constant-expressions. We could
4609 allow compound literals of integer type, whose
4610 initializer was a constant, in constant
4611 expressions. Permitting that usage, as a further
4612 extension, would not change the meaning of any
4613 currently accepted programs. (Of course, as
4614 compound literals are not part of ISO C++, the
4615 standard has nothing to say.) */
4616 if (cp_parser_non_integral_constant_expression
4617 (parser, "non-constant compound literals"))
4618 {
4619 postfix_expression = error_mark_node;
4620 break;
4621 }
4622 /* Form the representation of the compound-literal. */
4623 postfix_expression
4624 = (finish_compound_literal
4625 (type, build_constructor (init_list_type_node,
4626 initializer_list)));
4627 break;
4628 }
4629 }
4630
4631 /* It must be a primary-expression. */
4632 postfix_expression
4633 = cp_parser_primary_expression (parser, address_p, cast_p,
4634 /*template_arg_p=*/false,
4635 &idk);
4636 }
4637 break;
4638 }
4639
4640 /* Keep looping until the postfix-expression is complete. */
4641 while (true)
4642 {
4643 if (idk == CP_ID_KIND_UNQUALIFIED
4644 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4645 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4646 /* It is not a Koenig lookup function call. */
4647 postfix_expression
4648 = unqualified_name_lookup_error (postfix_expression);
4649
4650 /* Peek at the next token. */
4651 token = cp_lexer_peek_token (parser->lexer);
4652
4653 switch (token->type)
4654 {
4655 case CPP_OPEN_SQUARE:
4656 postfix_expression
4657 = cp_parser_postfix_open_square_expression (parser,
4658 postfix_expression,
4659 false);
4660 idk = CP_ID_KIND_NONE;
4661 is_member_access = false;
4662 break;
4663
4664 case CPP_OPEN_PAREN:
4665 /* postfix-expression ( expression-list [opt] ) */
4666 {
4667 bool koenig_p;
4668 bool is_builtin_constant_p;
4669 bool saved_integral_constant_expression_p = false;
4670 bool saved_non_integral_constant_expression_p = false;
4671 tree args;
4672
4673 is_member_access = false;
4674
4675 is_builtin_constant_p
4676 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4677 if (is_builtin_constant_p)
4678 {
4679 /* The whole point of __builtin_constant_p is to allow
4680 non-constant expressions to appear as arguments. */
4681 saved_integral_constant_expression_p
4682 = parser->integral_constant_expression_p;
4683 saved_non_integral_constant_expression_p
4684 = parser->non_integral_constant_expression_p;
4685 parser->integral_constant_expression_p = false;
4686 }
4687 args = (cp_parser_parenthesized_expression_list
4688 (parser, /*is_attribute_list=*/false,
4689 /*cast_p=*/false, /*allow_expansion_p=*/true,
4690 /*non_constant_p=*/NULL));
4691 if (is_builtin_constant_p)
4692 {
4693 parser->integral_constant_expression_p
4694 = saved_integral_constant_expression_p;
4695 parser->non_integral_constant_expression_p
4696 = saved_non_integral_constant_expression_p;
4697 }
4698
4699 if (args == error_mark_node)
4700 {
4701 postfix_expression = error_mark_node;
4702 break;
4703 }
4704
4705 /* Function calls are not permitted in
4706 constant-expressions. */
4707 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4708 && cp_parser_non_integral_constant_expression (parser,
4709 "a function call"))
4710 {
4711 postfix_expression = error_mark_node;
4712 break;
4713 }
4714
4715 koenig_p = false;
4716 if (idk == CP_ID_KIND_UNQUALIFIED)
4717 {
4718 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4719 {
4720 if (args)
4721 {
4722 koenig_p = true;
4723 postfix_expression
4724 = perform_koenig_lookup (postfix_expression, args);
4725 }
4726 else
4727 postfix_expression
4728 = unqualified_fn_lookup_error (postfix_expression);
4729 }
4730 /* We do not perform argument-dependent lookup if
4731 normal lookup finds a non-function, in accordance
4732 with the expected resolution of DR 218. */
4733 else if (args && is_overloaded_fn (postfix_expression))
4734 {
4735 tree fn = get_first_fn (postfix_expression);
4736
4737 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4738 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4739
4740 /* Only do argument dependent lookup if regular
4741 lookup does not find a set of member functions.
4742 [basic.lookup.koenig]/2a */
4743 if (!DECL_FUNCTION_MEMBER_P (fn))
4744 {
4745 koenig_p = true;
4746 postfix_expression
4747 = perform_koenig_lookup (postfix_expression, args);
4748 }
4749 }
4750 }
4751
4752 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4753 {
4754 tree instance = TREE_OPERAND (postfix_expression, 0);
4755 tree fn = TREE_OPERAND (postfix_expression, 1);
4756
4757 if (processing_template_decl
4758 && (type_dependent_expression_p (instance)
4759 || (!BASELINK_P (fn)
4760 && TREE_CODE (fn) != FIELD_DECL)
4761 || type_dependent_expression_p (fn)
4762 || any_type_dependent_arguments_p (args)))
4763 {
4764 postfix_expression
4765 = build_nt_call_list (postfix_expression, args);
4766 break;
4767 }
4768
4769 if (BASELINK_P (fn))
4770 postfix_expression
4771 = (build_new_method_call
4772 (instance, fn, args, NULL_TREE,
4773 (idk == CP_ID_KIND_QUALIFIED
4774 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4775 /*fn_p=*/NULL,
4776 tf_warning_or_error));
4777 else
4778 postfix_expression
4779 = finish_call_expr (postfix_expression, args,
4780 /*disallow_virtual=*/false,
4781 /*koenig_p=*/false,
4782 tf_warning_or_error);
4783 }
4784 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4785 || TREE_CODE (postfix_expression) == MEMBER_REF
4786 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4787 postfix_expression = (build_offset_ref_call_from_tree
4788 (postfix_expression, args));
4789 else if (idk == CP_ID_KIND_QUALIFIED)
4790 /* A call to a static class member, or a namespace-scope
4791 function. */
4792 postfix_expression
4793 = finish_call_expr (postfix_expression, args,
4794 /*disallow_virtual=*/true,
4795 koenig_p,
4796 tf_warning_or_error);
4797 else
4798 /* All other function calls. */
4799 postfix_expression
4800 = finish_call_expr (postfix_expression, args,
4801 /*disallow_virtual=*/false,
4802 koenig_p,
4803 tf_warning_or_error);
4804
4805 if (warn_disallowed_functions)
4806 warn_if_disallowed_function_p (postfix_expression);
4807
4808 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4809 idk = CP_ID_KIND_NONE;
4810 }
4811 break;
4812
4813 case CPP_DOT:
4814 case CPP_DEREF:
4815 /* postfix-expression . template [opt] id-expression
4816 postfix-expression . pseudo-destructor-name
4817 postfix-expression -> template [opt] id-expression
4818 postfix-expression -> pseudo-destructor-name */
4819
4820 /* Consume the `.' or `->' operator. */
4821 cp_lexer_consume_token (parser->lexer);
4822
4823 postfix_expression
4824 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4825 postfix_expression,
4826 false, &idk,
4827 token->location);
4828
4829 is_member_access = true;
4830 break;
4831
4832 case CPP_PLUS_PLUS:
4833 /* postfix-expression ++ */
4834 /* Consume the `++' token. */
4835 cp_lexer_consume_token (parser->lexer);
4836 /* Generate a representation for the complete expression. */
4837 postfix_expression
4838 = finish_increment_expr (postfix_expression,
4839 POSTINCREMENT_EXPR);
4840 /* Increments may not appear in constant-expressions. */
4841 if (cp_parser_non_integral_constant_expression (parser,
4842 "an increment"))
4843 postfix_expression = error_mark_node;
4844 idk = CP_ID_KIND_NONE;
4845 is_member_access = false;
4846 break;
4847
4848 case CPP_MINUS_MINUS:
4849 /* postfix-expression -- */
4850 /* Consume the `--' token. */
4851 cp_lexer_consume_token (parser->lexer);
4852 /* Generate a representation for the complete expression. */
4853 postfix_expression
4854 = finish_increment_expr (postfix_expression,
4855 POSTDECREMENT_EXPR);
4856 /* Decrements may not appear in constant-expressions. */
4857 if (cp_parser_non_integral_constant_expression (parser,
4858 "a decrement"))
4859 postfix_expression = error_mark_node;
4860 idk = CP_ID_KIND_NONE;
4861 is_member_access = false;
4862 break;
4863
4864 default:
4865 if (member_access_only_p)
4866 return is_member_access? postfix_expression : error_mark_node;
4867 else
4868 return postfix_expression;
4869 }
4870 }
4871
4872 /* We should never get here. */
4873 gcc_unreachable ();
4874 return error_mark_node;
4875 }
4876
4877 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4878 by cp_parser_builtin_offsetof. We're looking for
4879
4880 postfix-expression [ expression ]
4881
4882 FOR_OFFSETOF is set if we're being called in that context, which
4883 changes how we deal with integer constant expressions. */
4884
4885 static tree
4886 cp_parser_postfix_open_square_expression (cp_parser *parser,
4887 tree postfix_expression,
4888 bool for_offsetof)
4889 {
4890 tree index;
4891
4892 /* Consume the `[' token. */
4893 cp_lexer_consume_token (parser->lexer);
4894
4895 /* Parse the index expression. */
4896 /* ??? For offsetof, there is a question of what to allow here. If
4897 offsetof is not being used in an integral constant expression context,
4898 then we *could* get the right answer by computing the value at runtime.
4899 If we are in an integral constant expression context, then we might
4900 could accept any constant expression; hard to say without analysis.
4901 Rather than open the barn door too wide right away, allow only integer
4902 constant expressions here. */
4903 if (for_offsetof)
4904 index = cp_parser_constant_expression (parser, false, NULL);
4905 else
4906 index = cp_parser_expression (parser, /*cast_p=*/false);
4907
4908 /* Look for the closing `]'. */
4909 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4910
4911 /* Build the ARRAY_REF. */
4912 postfix_expression = grok_array_decl (postfix_expression, index);
4913
4914 /* When not doing offsetof, array references are not permitted in
4915 constant-expressions. */
4916 if (!for_offsetof
4917 && (cp_parser_non_integral_constant_expression
4918 (parser, "an array reference")))
4919 postfix_expression = error_mark_node;
4920
4921 return postfix_expression;
4922 }
4923
4924 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4925 by cp_parser_builtin_offsetof. We're looking for
4926
4927 postfix-expression . template [opt] id-expression
4928 postfix-expression . pseudo-destructor-name
4929 postfix-expression -> template [opt] id-expression
4930 postfix-expression -> pseudo-destructor-name
4931
4932 FOR_OFFSETOF is set if we're being called in that context. That sorta
4933 limits what of the above we'll actually accept, but nevermind.
4934 TOKEN_TYPE is the "." or "->" token, which will already have been
4935 removed from the stream. */
4936
4937 static tree
4938 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4939 enum cpp_ttype token_type,
4940 tree postfix_expression,
4941 bool for_offsetof, cp_id_kind *idk,
4942 location_t location)
4943 {
4944 tree name;
4945 bool dependent_p;
4946 bool pseudo_destructor_p;
4947 tree scope = NULL_TREE;
4948
4949 /* If this is a `->' operator, dereference the pointer. */
4950 if (token_type == CPP_DEREF)
4951 postfix_expression = build_x_arrow (postfix_expression);
4952 /* Check to see whether or not the expression is type-dependent. */
4953 dependent_p = type_dependent_expression_p (postfix_expression);
4954 /* The identifier following the `->' or `.' is not qualified. */
4955 parser->scope = NULL_TREE;
4956 parser->qualifying_scope = NULL_TREE;
4957 parser->object_scope = NULL_TREE;
4958 *idk = CP_ID_KIND_NONE;
4959 /* Enter the scope corresponding to the type of the object
4960 given by the POSTFIX_EXPRESSION. */
4961 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4962 {
4963 scope = TREE_TYPE (postfix_expression);
4964 /* According to the standard, no expression should ever have
4965 reference type. Unfortunately, we do not currently match
4966 the standard in this respect in that our internal representation
4967 of an expression may have reference type even when the standard
4968 says it does not. Therefore, we have to manually obtain the
4969 underlying type here. */
4970 scope = non_reference (scope);
4971 /* The type of the POSTFIX_EXPRESSION must be complete. */
4972 if (scope == unknown_type_node)
4973 {
4974 error ("%H%qE does not have class type", &location, postfix_expression);
4975 scope = NULL_TREE;
4976 }
4977 else
4978 scope = complete_type_or_else (scope, NULL_TREE);
4979 /* Let the name lookup machinery know that we are processing a
4980 class member access expression. */
4981 parser->context->object_type = scope;
4982 /* If something went wrong, we want to be able to discern that case,
4983 as opposed to the case where there was no SCOPE due to the type
4984 of expression being dependent. */
4985 if (!scope)
4986 scope = error_mark_node;
4987 /* If the SCOPE was erroneous, make the various semantic analysis
4988 functions exit quickly -- and without issuing additional error
4989 messages. */
4990 if (scope == error_mark_node)
4991 postfix_expression = error_mark_node;
4992 }
4993
4994 /* Assume this expression is not a pseudo-destructor access. */
4995 pseudo_destructor_p = false;
4996
4997 /* If the SCOPE is a scalar type, then, if this is a valid program,
4998 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4999 is type dependent, it can be pseudo-destructor-name or something else.
5000 Try to parse it as pseudo-destructor-name first. */
5001 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5002 {
5003 tree s;
5004 tree type;
5005
5006 cp_parser_parse_tentatively (parser);
5007 /* Parse the pseudo-destructor-name. */
5008 s = NULL_TREE;
5009 cp_parser_pseudo_destructor_name (parser, &s, &type);
5010 if (dependent_p
5011 && (cp_parser_error_occurred (parser)
5012 || TREE_CODE (type) != TYPE_DECL
5013 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5014 cp_parser_abort_tentative_parse (parser);
5015 else if (cp_parser_parse_definitely (parser))
5016 {
5017 pseudo_destructor_p = true;
5018 postfix_expression
5019 = finish_pseudo_destructor_expr (postfix_expression,
5020 s, TREE_TYPE (type));
5021 }
5022 }
5023
5024 if (!pseudo_destructor_p)
5025 {
5026 /* If the SCOPE is not a scalar type, we are looking at an
5027 ordinary class member access expression, rather than a
5028 pseudo-destructor-name. */
5029 bool template_p;
5030 cp_token *token = cp_lexer_peek_token (parser->lexer);
5031 /* Parse the id-expression. */
5032 name = (cp_parser_id_expression
5033 (parser,
5034 cp_parser_optional_template_keyword (parser),
5035 /*check_dependency_p=*/true,
5036 &template_p,
5037 /*declarator_p=*/false,
5038 /*optional_p=*/false));
5039 /* In general, build a SCOPE_REF if the member name is qualified.
5040 However, if the name was not dependent and has already been
5041 resolved; there is no need to build the SCOPE_REF. For example;
5042
5043 struct X { void f(); };
5044 template <typename T> void f(T* t) { t->X::f(); }
5045
5046 Even though "t" is dependent, "X::f" is not and has been resolved
5047 to a BASELINK; there is no need to include scope information. */
5048
5049 /* But we do need to remember that there was an explicit scope for
5050 virtual function calls. */
5051 if (parser->scope)
5052 *idk = CP_ID_KIND_QUALIFIED;
5053
5054 /* If the name is a template-id that names a type, we will get a
5055 TYPE_DECL here. That is invalid code. */
5056 if (TREE_CODE (name) == TYPE_DECL)
5057 {
5058 error ("%Hinvalid use of %qD", &token->location, name);
5059 postfix_expression = error_mark_node;
5060 }
5061 else
5062 {
5063 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5064 {
5065 name = build_qualified_name (/*type=*/NULL_TREE,
5066 parser->scope,
5067 name,
5068 template_p);
5069 parser->scope = NULL_TREE;
5070 parser->qualifying_scope = NULL_TREE;
5071 parser->object_scope = NULL_TREE;
5072 }
5073 if (scope && name && BASELINK_P (name))
5074 adjust_result_of_qualified_name_lookup
5075 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5076 postfix_expression
5077 = finish_class_member_access_expr (postfix_expression, name,
5078 template_p,
5079 tf_warning_or_error);
5080 }
5081 }
5082
5083 /* We no longer need to look up names in the scope of the object on
5084 the left-hand side of the `.' or `->' operator. */
5085 parser->context->object_type = NULL_TREE;
5086
5087 /* Outside of offsetof, these operators may not appear in
5088 constant-expressions. */
5089 if (!for_offsetof
5090 && (cp_parser_non_integral_constant_expression
5091 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5092 postfix_expression = error_mark_node;
5093
5094 return postfix_expression;
5095 }
5096
5097 /* Parse a parenthesized expression-list.
5098
5099 expression-list:
5100 assignment-expression
5101 expression-list, assignment-expression
5102
5103 attribute-list:
5104 expression-list
5105 identifier
5106 identifier, expression-list
5107
5108 CAST_P is true if this expression is the target of a cast.
5109
5110 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5111 argument pack.
5112
5113 Returns a TREE_LIST. The TREE_VALUE of each node is a
5114 representation of an assignment-expression. Note that a TREE_LIST
5115 is returned even if there is only a single expression in the list.
5116 error_mark_node is returned if the ( and or ) are
5117 missing. NULL_TREE is returned on no expressions. The parentheses
5118 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5119 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5120 indicates whether or not all of the expressions in the list were
5121 constant. */
5122
5123 static tree
5124 cp_parser_parenthesized_expression_list (cp_parser* parser,
5125 bool is_attribute_list,
5126 bool cast_p,
5127 bool allow_expansion_p,
5128 bool *non_constant_p)
5129 {
5130 tree expression_list = NULL_TREE;
5131 bool fold_expr_p = is_attribute_list;
5132 tree identifier = NULL_TREE;
5133 bool saved_greater_than_is_operator_p;
5134
5135 /* Assume all the expressions will be constant. */
5136 if (non_constant_p)
5137 *non_constant_p = false;
5138
5139 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5140 return error_mark_node;
5141
5142 /* Within a parenthesized expression, a `>' token is always
5143 the greater-than operator. */
5144 saved_greater_than_is_operator_p
5145 = parser->greater_than_is_operator_p;
5146 parser->greater_than_is_operator_p = true;
5147
5148 /* Consume expressions until there are no more. */
5149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5150 while (true)
5151 {
5152 tree expr;
5153
5154 /* At the beginning of attribute lists, check to see if the
5155 next token is an identifier. */
5156 if (is_attribute_list
5157 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5158 {
5159 cp_token *token;
5160
5161 /* Consume the identifier. */
5162 token = cp_lexer_consume_token (parser->lexer);
5163 /* Save the identifier. */
5164 identifier = token->u.value;
5165 }
5166 else
5167 {
5168 bool expr_non_constant_p;
5169
5170 /* Parse the next assignment-expression. */
5171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5172 {
5173 /* A braced-init-list. */
5174 maybe_warn_cpp0x ("extended initializer lists");
5175 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5176 if (non_constant_p && expr_non_constant_p)
5177 *non_constant_p = true;
5178 }
5179 else if (non_constant_p)
5180 {
5181 expr = (cp_parser_constant_expression
5182 (parser, /*allow_non_constant_p=*/true,
5183 &expr_non_constant_p));
5184 if (expr_non_constant_p)
5185 *non_constant_p = true;
5186 }
5187 else
5188 expr = cp_parser_assignment_expression (parser, cast_p);
5189
5190 if (fold_expr_p)
5191 expr = fold_non_dependent_expr (expr);
5192
5193 /* If we have an ellipsis, then this is an expression
5194 expansion. */
5195 if (allow_expansion_p
5196 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5197 {
5198 /* Consume the `...'. */
5199 cp_lexer_consume_token (parser->lexer);
5200
5201 /* Build the argument pack. */
5202 expr = make_pack_expansion (expr);
5203 }
5204
5205 /* Add it to the list. We add error_mark_node
5206 expressions to the list, so that we can still tell if
5207 the correct form for a parenthesized expression-list
5208 is found. That gives better errors. */
5209 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5210
5211 if (expr == error_mark_node)
5212 goto skip_comma;
5213 }
5214
5215 /* After the first item, attribute lists look the same as
5216 expression lists. */
5217 is_attribute_list = false;
5218
5219 get_comma:;
5220 /* If the next token isn't a `,', then we are done. */
5221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5222 break;
5223
5224 /* Otherwise, consume the `,' and keep going. */
5225 cp_lexer_consume_token (parser->lexer);
5226 }
5227
5228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5229 {
5230 int ending;
5231
5232 skip_comma:;
5233 /* We try and resync to an unnested comma, as that will give the
5234 user better diagnostics. */
5235 ending = cp_parser_skip_to_closing_parenthesis (parser,
5236 /*recovering=*/true,
5237 /*or_comma=*/true,
5238 /*consume_paren=*/true);
5239 if (ending < 0)
5240 goto get_comma;
5241 if (!ending)
5242 {
5243 parser->greater_than_is_operator_p
5244 = saved_greater_than_is_operator_p;
5245 return error_mark_node;
5246 }
5247 }
5248
5249 parser->greater_than_is_operator_p
5250 = saved_greater_than_is_operator_p;
5251
5252 /* We built up the list in reverse order so we must reverse it now. */
5253 expression_list = nreverse (expression_list);
5254 if (identifier)
5255 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5256
5257 return expression_list;
5258 }
5259
5260 /* Parse a pseudo-destructor-name.
5261
5262 pseudo-destructor-name:
5263 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5264 :: [opt] nested-name-specifier template template-id :: ~ type-name
5265 :: [opt] nested-name-specifier [opt] ~ type-name
5266
5267 If either of the first two productions is used, sets *SCOPE to the
5268 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5269 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5270 or ERROR_MARK_NODE if the parse fails. */
5271
5272 static void
5273 cp_parser_pseudo_destructor_name (cp_parser* parser,
5274 tree* scope,
5275 tree* type)
5276 {
5277 bool nested_name_specifier_p;
5278
5279 /* Assume that things will not work out. */
5280 *type = error_mark_node;
5281
5282 /* Look for the optional `::' operator. */
5283 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5284 /* Look for the optional nested-name-specifier. */
5285 nested_name_specifier_p
5286 = (cp_parser_nested_name_specifier_opt (parser,
5287 /*typename_keyword_p=*/false,
5288 /*check_dependency_p=*/true,
5289 /*type_p=*/false,
5290 /*is_declaration=*/false)
5291 != NULL_TREE);
5292 /* Now, if we saw a nested-name-specifier, we might be doing the
5293 second production. */
5294 if (nested_name_specifier_p
5295 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5296 {
5297 /* Consume the `template' keyword. */
5298 cp_lexer_consume_token (parser->lexer);
5299 /* Parse the template-id. */
5300 cp_parser_template_id (parser,
5301 /*template_keyword_p=*/true,
5302 /*check_dependency_p=*/false,
5303 /*is_declaration=*/true);
5304 /* Look for the `::' token. */
5305 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5306 }
5307 /* If the next token is not a `~', then there might be some
5308 additional qualification. */
5309 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5310 {
5311 /* At this point, we're looking for "type-name :: ~". The type-name
5312 must not be a class-name, since this is a pseudo-destructor. So,
5313 it must be either an enum-name, or a typedef-name -- both of which
5314 are just identifiers. So, we peek ahead to check that the "::"
5315 and "~" tokens are present; if they are not, then we can avoid
5316 calling type_name. */
5317 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5318 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5319 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5320 {
5321 cp_parser_error (parser, "non-scalar type");
5322 return;
5323 }
5324
5325 /* Look for the type-name. */
5326 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5327 if (*scope == error_mark_node)
5328 return;
5329
5330 /* Look for the `::' token. */
5331 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5332 }
5333 else
5334 *scope = NULL_TREE;
5335
5336 /* Look for the `~'. */
5337 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5338 /* Look for the type-name again. We are not responsible for
5339 checking that it matches the first type-name. */
5340 *type = cp_parser_nonclass_name (parser);
5341 }
5342
5343 /* Parse a unary-expression.
5344
5345 unary-expression:
5346 postfix-expression
5347 ++ cast-expression
5348 -- cast-expression
5349 unary-operator cast-expression
5350 sizeof unary-expression
5351 sizeof ( type-id )
5352 new-expression
5353 delete-expression
5354
5355 GNU Extensions:
5356
5357 unary-expression:
5358 __extension__ cast-expression
5359 __alignof__ unary-expression
5360 __alignof__ ( type-id )
5361 __real__ cast-expression
5362 __imag__ cast-expression
5363 && identifier
5364
5365 ADDRESS_P is true iff the unary-expression is appearing as the
5366 operand of the `&' operator. CAST_P is true if this expression is
5367 the target of a cast.
5368
5369 Returns a representation of the expression. */
5370
5371 static tree
5372 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5373 {
5374 cp_token *token;
5375 enum tree_code unary_operator;
5376
5377 /* Peek at the next token. */
5378 token = cp_lexer_peek_token (parser->lexer);
5379 /* Some keywords give away the kind of expression. */
5380 if (token->type == CPP_KEYWORD)
5381 {
5382 enum rid keyword = token->keyword;
5383
5384 switch (keyword)
5385 {
5386 case RID_ALIGNOF:
5387 case RID_SIZEOF:
5388 {
5389 tree operand;
5390 enum tree_code op;
5391
5392 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5393 /* Consume the token. */
5394 cp_lexer_consume_token (parser->lexer);
5395 /* Parse the operand. */
5396 operand = cp_parser_sizeof_operand (parser, keyword);
5397
5398 if (TYPE_P (operand))
5399 return cxx_sizeof_or_alignof_type (operand, op, true);
5400 else
5401 return cxx_sizeof_or_alignof_expr (operand, op, true);
5402 }
5403
5404 case RID_NEW:
5405 return cp_parser_new_expression (parser);
5406
5407 case RID_DELETE:
5408 return cp_parser_delete_expression (parser);
5409
5410 case RID_EXTENSION:
5411 {
5412 /* The saved value of the PEDANTIC flag. */
5413 int saved_pedantic;
5414 tree expr;
5415
5416 /* Save away the PEDANTIC flag. */
5417 cp_parser_extension_opt (parser, &saved_pedantic);
5418 /* Parse the cast-expression. */
5419 expr = cp_parser_simple_cast_expression (parser);
5420 /* Restore the PEDANTIC flag. */
5421 pedantic = saved_pedantic;
5422
5423 return expr;
5424 }
5425
5426 case RID_REALPART:
5427 case RID_IMAGPART:
5428 {
5429 tree expression;
5430
5431 /* Consume the `__real__' or `__imag__' token. */
5432 cp_lexer_consume_token (parser->lexer);
5433 /* Parse the cast-expression. */
5434 expression = cp_parser_simple_cast_expression (parser);
5435 /* Create the complete representation. */
5436 return build_x_unary_op ((keyword == RID_REALPART
5437 ? REALPART_EXPR : IMAGPART_EXPR),
5438 expression,
5439 tf_warning_or_error);
5440 }
5441 break;
5442
5443 default:
5444 break;
5445 }
5446 }
5447
5448 /* Look for the `:: new' and `:: delete', which also signal the
5449 beginning of a new-expression, or delete-expression,
5450 respectively. If the next token is `::', then it might be one of
5451 these. */
5452 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5453 {
5454 enum rid keyword;
5455
5456 /* See if the token after the `::' is one of the keywords in
5457 which we're interested. */
5458 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5459 /* If it's `new', we have a new-expression. */
5460 if (keyword == RID_NEW)
5461 return cp_parser_new_expression (parser);
5462 /* Similarly, for `delete'. */
5463 else if (keyword == RID_DELETE)
5464 return cp_parser_delete_expression (parser);
5465 }
5466
5467 /* Look for a unary operator. */
5468 unary_operator = cp_parser_unary_operator (token);
5469 /* The `++' and `--' operators can be handled similarly, even though
5470 they are not technically unary-operators in the grammar. */
5471 if (unary_operator == ERROR_MARK)
5472 {
5473 if (token->type == CPP_PLUS_PLUS)
5474 unary_operator = PREINCREMENT_EXPR;
5475 else if (token->type == CPP_MINUS_MINUS)
5476 unary_operator = PREDECREMENT_EXPR;
5477 /* Handle the GNU address-of-label extension. */
5478 else if (cp_parser_allow_gnu_extensions_p (parser)
5479 && token->type == CPP_AND_AND)
5480 {
5481 tree identifier;
5482 tree expression;
5483 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5484
5485 /* Consume the '&&' token. */
5486 cp_lexer_consume_token (parser->lexer);
5487 /* Look for the identifier. */
5488 identifier = cp_parser_identifier (parser);
5489 /* Create an expression representing the address. */
5490 expression = finish_label_address_expr (identifier, loc);
5491 if (cp_parser_non_integral_constant_expression (parser,
5492 "the address of a label"))
5493 expression = error_mark_node;
5494 return expression;
5495 }
5496 }
5497 if (unary_operator != ERROR_MARK)
5498 {
5499 tree cast_expression;
5500 tree expression = error_mark_node;
5501 const char *non_constant_p = NULL;
5502
5503 /* Consume the operator token. */
5504 token = cp_lexer_consume_token (parser->lexer);
5505 /* Parse the cast-expression. */
5506 cast_expression
5507 = cp_parser_cast_expression (parser,
5508 unary_operator == ADDR_EXPR,
5509 /*cast_p=*/false);
5510 /* Now, build an appropriate representation. */
5511 switch (unary_operator)
5512 {
5513 case INDIRECT_REF:
5514 non_constant_p = "%<*%>";
5515 expression = build_x_indirect_ref (cast_expression, "unary *",
5516 tf_warning_or_error);
5517 break;
5518
5519 case ADDR_EXPR:
5520 non_constant_p = "%<&%>";
5521 /* Fall through. */
5522 case BIT_NOT_EXPR:
5523 expression = build_x_unary_op (unary_operator, cast_expression,
5524 tf_warning_or_error);
5525 break;
5526
5527 case PREINCREMENT_EXPR:
5528 case PREDECREMENT_EXPR:
5529 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5530 ? "%<++%>" : "%<--%>");
5531 /* Fall through. */
5532 case UNARY_PLUS_EXPR:
5533 case NEGATE_EXPR:
5534 case TRUTH_NOT_EXPR:
5535 expression = finish_unary_op_expr (unary_operator, cast_expression);
5536 break;
5537
5538 default:
5539 gcc_unreachable ();
5540 }
5541
5542 if (non_constant_p
5543 && cp_parser_non_integral_constant_expression (parser,
5544 non_constant_p))
5545 expression = error_mark_node;
5546
5547 return expression;
5548 }
5549
5550 return cp_parser_postfix_expression (parser, address_p, cast_p,
5551 /*member_access_only_p=*/false);
5552 }
5553
5554 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5555 unary-operator, the corresponding tree code is returned. */
5556
5557 static enum tree_code
5558 cp_parser_unary_operator (cp_token* token)
5559 {
5560 switch (token->type)
5561 {
5562 case CPP_MULT:
5563 return INDIRECT_REF;
5564
5565 case CPP_AND:
5566 return ADDR_EXPR;
5567
5568 case CPP_PLUS:
5569 return UNARY_PLUS_EXPR;
5570
5571 case CPP_MINUS:
5572 return NEGATE_EXPR;
5573
5574 case CPP_NOT:
5575 return TRUTH_NOT_EXPR;
5576
5577 case CPP_COMPL:
5578 return BIT_NOT_EXPR;
5579
5580 default:
5581 return ERROR_MARK;
5582 }
5583 }
5584
5585 /* Parse a new-expression.
5586
5587 new-expression:
5588 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5589 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5590
5591 Returns a representation of the expression. */
5592
5593 static tree
5594 cp_parser_new_expression (cp_parser* parser)
5595 {
5596 bool global_scope_p;
5597 tree placement;
5598 tree type;
5599 tree initializer;
5600 tree nelts;
5601
5602 /* Look for the optional `::' operator. */
5603 global_scope_p
5604 = (cp_parser_global_scope_opt (parser,
5605 /*current_scope_valid_p=*/false)
5606 != NULL_TREE);
5607 /* Look for the `new' operator. */
5608 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5609 /* There's no easy way to tell a new-placement from the
5610 `( type-id )' construct. */
5611 cp_parser_parse_tentatively (parser);
5612 /* Look for a new-placement. */
5613 placement = cp_parser_new_placement (parser);
5614 /* If that didn't work out, there's no new-placement. */
5615 if (!cp_parser_parse_definitely (parser))
5616 placement = NULL_TREE;
5617
5618 /* If the next token is a `(', then we have a parenthesized
5619 type-id. */
5620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5621 {
5622 cp_token *token;
5623 /* Consume the `('. */
5624 cp_lexer_consume_token (parser->lexer);
5625 /* Parse the type-id. */
5626 type = cp_parser_type_id (parser);
5627 /* Look for the closing `)'. */
5628 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5629 token = cp_lexer_peek_token (parser->lexer);
5630 /* There should not be a direct-new-declarator in this production,
5631 but GCC used to allowed this, so we check and emit a sensible error
5632 message for this case. */
5633 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5634 {
5635 error ("%Harray bound forbidden after parenthesized type-id",
5636 &token->location);
5637 inform (token->location,
5638 "try removing the parentheses around the type-id");
5639 cp_parser_direct_new_declarator (parser);
5640 }
5641 nelts = NULL_TREE;
5642 }
5643 /* Otherwise, there must be a new-type-id. */
5644 else
5645 type = cp_parser_new_type_id (parser, &nelts);
5646
5647 /* If the next token is a `(' or '{', then we have a new-initializer. */
5648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5649 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5650 initializer = cp_parser_new_initializer (parser);
5651 else
5652 initializer = NULL_TREE;
5653
5654 /* A new-expression may not appear in an integral constant
5655 expression. */
5656 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5657 return error_mark_node;
5658
5659 /* Create a representation of the new-expression. */
5660 return build_new (placement, type, nelts, initializer, global_scope_p,
5661 tf_warning_or_error);
5662 }
5663
5664 /* Parse a new-placement.
5665
5666 new-placement:
5667 ( expression-list )
5668
5669 Returns the same representation as for an expression-list. */
5670
5671 static tree
5672 cp_parser_new_placement (cp_parser* parser)
5673 {
5674 tree expression_list;
5675
5676 /* Parse the expression-list. */
5677 expression_list = (cp_parser_parenthesized_expression_list
5678 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5679 /*non_constant_p=*/NULL));
5680
5681 return expression_list;
5682 }
5683
5684 /* Parse a new-type-id.
5685
5686 new-type-id:
5687 type-specifier-seq new-declarator [opt]
5688
5689 Returns the TYPE allocated. If the new-type-id indicates an array
5690 type, *NELTS is set to the number of elements in the last array
5691 bound; the TYPE will not include the last array bound. */
5692
5693 static tree
5694 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5695 {
5696 cp_decl_specifier_seq type_specifier_seq;
5697 cp_declarator *new_declarator;
5698 cp_declarator *declarator;
5699 cp_declarator *outer_declarator;
5700 const char *saved_message;
5701 tree type;
5702
5703 /* The type-specifier sequence must not contain type definitions.
5704 (It cannot contain declarations of new types either, but if they
5705 are not definitions we will catch that because they are not
5706 complete.) */
5707 saved_message = parser->type_definition_forbidden_message;
5708 parser->type_definition_forbidden_message
5709 = "types may not be defined in a new-type-id";
5710 /* Parse the type-specifier-seq. */
5711 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5712 &type_specifier_seq);
5713 /* Restore the old message. */
5714 parser->type_definition_forbidden_message = saved_message;
5715 /* Parse the new-declarator. */
5716 new_declarator = cp_parser_new_declarator_opt (parser);
5717
5718 /* Determine the number of elements in the last array dimension, if
5719 any. */
5720 *nelts = NULL_TREE;
5721 /* Skip down to the last array dimension. */
5722 declarator = new_declarator;
5723 outer_declarator = NULL;
5724 while (declarator && (declarator->kind == cdk_pointer
5725 || declarator->kind == cdk_ptrmem))
5726 {
5727 outer_declarator = declarator;
5728 declarator = declarator->declarator;
5729 }
5730 while (declarator
5731 && declarator->kind == cdk_array
5732 && declarator->declarator
5733 && declarator->declarator->kind == cdk_array)
5734 {
5735 outer_declarator = declarator;
5736 declarator = declarator->declarator;
5737 }
5738
5739 if (declarator && declarator->kind == cdk_array)
5740 {
5741 *nelts = declarator->u.array.bounds;
5742 if (*nelts == error_mark_node)
5743 *nelts = integer_one_node;
5744
5745 if (outer_declarator)
5746 outer_declarator->declarator = declarator->declarator;
5747 else
5748 new_declarator = NULL;
5749 }
5750
5751 type = groktypename (&type_specifier_seq, new_declarator);
5752 return type;
5753 }
5754
5755 /* Parse an (optional) new-declarator.
5756
5757 new-declarator:
5758 ptr-operator new-declarator [opt]
5759 direct-new-declarator
5760
5761 Returns the declarator. */
5762
5763 static cp_declarator *
5764 cp_parser_new_declarator_opt (cp_parser* parser)
5765 {
5766 enum tree_code code;
5767 tree type;
5768 cp_cv_quals cv_quals;
5769
5770 /* We don't know if there's a ptr-operator next, or not. */
5771 cp_parser_parse_tentatively (parser);
5772 /* Look for a ptr-operator. */
5773 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5774 /* If that worked, look for more new-declarators. */
5775 if (cp_parser_parse_definitely (parser))
5776 {
5777 cp_declarator *declarator;
5778
5779 /* Parse another optional declarator. */
5780 declarator = cp_parser_new_declarator_opt (parser);
5781
5782 return cp_parser_make_indirect_declarator
5783 (code, type, cv_quals, declarator);
5784 }
5785
5786 /* If the next token is a `[', there is a direct-new-declarator. */
5787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5788 return cp_parser_direct_new_declarator (parser);
5789
5790 return NULL;
5791 }
5792
5793 /* Parse a direct-new-declarator.
5794
5795 direct-new-declarator:
5796 [ expression ]
5797 direct-new-declarator [constant-expression]
5798
5799 */
5800
5801 static cp_declarator *
5802 cp_parser_direct_new_declarator (cp_parser* parser)
5803 {
5804 cp_declarator *declarator = NULL;
5805
5806 while (true)
5807 {
5808 tree expression;
5809
5810 /* Look for the opening `['. */
5811 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5812 /* The first expression is not required to be constant. */
5813 if (!declarator)
5814 {
5815 cp_token *token = cp_lexer_peek_token (parser->lexer);
5816 expression = cp_parser_expression (parser, /*cast_p=*/false);
5817 /* The standard requires that the expression have integral
5818 type. DR 74 adds enumeration types. We believe that the
5819 real intent is that these expressions be handled like the
5820 expression in a `switch' condition, which also allows
5821 classes with a single conversion to integral or
5822 enumeration type. */
5823 if (!processing_template_decl)
5824 {
5825 expression
5826 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5827 expression,
5828 /*complain=*/true);
5829 if (!expression)
5830 {
5831 error ("%Hexpression in new-declarator must have integral "
5832 "or enumeration type", &token->location);
5833 expression = error_mark_node;
5834 }
5835 }
5836 }
5837 /* But all the other expressions must be. */
5838 else
5839 expression
5840 = cp_parser_constant_expression (parser,
5841 /*allow_non_constant=*/false,
5842 NULL);
5843 /* Look for the closing `]'. */
5844 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5845
5846 /* Add this bound to the declarator. */
5847 declarator = make_array_declarator (declarator, expression);
5848
5849 /* If the next token is not a `[', then there are no more
5850 bounds. */
5851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5852 break;
5853 }
5854
5855 return declarator;
5856 }
5857
5858 /* Parse a new-initializer.
5859
5860 new-initializer:
5861 ( expression-list [opt] )
5862 braced-init-list
5863
5864 Returns a representation of the expression-list. If there is no
5865 expression-list, VOID_ZERO_NODE is returned. */
5866
5867 static tree
5868 cp_parser_new_initializer (cp_parser* parser)
5869 {
5870 tree expression_list;
5871
5872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5873 {
5874 bool expr_non_constant_p;
5875 maybe_warn_cpp0x ("extended initializer lists");
5876 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5877 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5878 expression_list = build_tree_list (NULL_TREE, expression_list);
5879 }
5880 else
5881 expression_list = (cp_parser_parenthesized_expression_list
5882 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5883 /*non_constant_p=*/NULL));
5884 if (!expression_list)
5885 expression_list = void_zero_node;
5886
5887 return expression_list;
5888 }
5889
5890 /* Parse a delete-expression.
5891
5892 delete-expression:
5893 :: [opt] delete cast-expression
5894 :: [opt] delete [ ] cast-expression
5895
5896 Returns a representation of the expression. */
5897
5898 static tree
5899 cp_parser_delete_expression (cp_parser* parser)
5900 {
5901 bool global_scope_p;
5902 bool array_p;
5903 tree expression;
5904
5905 /* Look for the optional `::' operator. */
5906 global_scope_p
5907 = (cp_parser_global_scope_opt (parser,
5908 /*current_scope_valid_p=*/false)
5909 != NULL_TREE);
5910 /* Look for the `delete' keyword. */
5911 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5912 /* See if the array syntax is in use. */
5913 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5914 {
5915 /* Consume the `[' token. */
5916 cp_lexer_consume_token (parser->lexer);
5917 /* Look for the `]' token. */
5918 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5919 /* Remember that this is the `[]' construct. */
5920 array_p = true;
5921 }
5922 else
5923 array_p = false;
5924
5925 /* Parse the cast-expression. */
5926 expression = cp_parser_simple_cast_expression (parser);
5927
5928 /* A delete-expression may not appear in an integral constant
5929 expression. */
5930 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5931 return error_mark_node;
5932
5933 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5934 }
5935
5936 /* Returns true if TOKEN may start a cast-expression and false
5937 otherwise. */
5938
5939 static bool
5940 cp_parser_token_starts_cast_expression (cp_token *token)
5941 {
5942 switch (token->type)
5943 {
5944 case CPP_COMMA:
5945 case CPP_SEMICOLON:
5946 case CPP_QUERY:
5947 case CPP_COLON:
5948 case CPP_CLOSE_SQUARE:
5949 case CPP_CLOSE_PAREN:
5950 case CPP_CLOSE_BRACE:
5951 case CPP_DOT:
5952 case CPP_DOT_STAR:
5953 case CPP_DEREF:
5954 case CPP_DEREF_STAR:
5955 case CPP_DIV:
5956 case CPP_MOD:
5957 case CPP_LSHIFT:
5958 case CPP_RSHIFT:
5959 case CPP_LESS:
5960 case CPP_GREATER:
5961 case CPP_LESS_EQ:
5962 case CPP_GREATER_EQ:
5963 case CPP_EQ_EQ:
5964 case CPP_NOT_EQ:
5965 case CPP_EQ:
5966 case CPP_MULT_EQ:
5967 case CPP_DIV_EQ:
5968 case CPP_MOD_EQ:
5969 case CPP_PLUS_EQ:
5970 case CPP_MINUS_EQ:
5971 case CPP_RSHIFT_EQ:
5972 case CPP_LSHIFT_EQ:
5973 case CPP_AND_EQ:
5974 case CPP_XOR_EQ:
5975 case CPP_OR_EQ:
5976 case CPP_XOR:
5977 case CPP_OR:
5978 case CPP_OR_OR:
5979 case CPP_EOF:
5980 return false;
5981
5982 /* '[' may start a primary-expression in obj-c++. */
5983 case CPP_OPEN_SQUARE:
5984 return c_dialect_objc ();
5985
5986 default:
5987 return true;
5988 }
5989 }
5990
5991 /* Parse a cast-expression.
5992
5993 cast-expression:
5994 unary-expression
5995 ( type-id ) cast-expression
5996
5997 ADDRESS_P is true iff the unary-expression is appearing as the
5998 operand of the `&' operator. CAST_P is true if this expression is
5999 the target of a cast.
6000
6001 Returns a representation of the expression. */
6002
6003 static tree
6004 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
6005 {
6006 /* If it's a `(', then we might be looking at a cast. */
6007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6008 {
6009 tree type = NULL_TREE;
6010 tree expr = NULL_TREE;
6011 bool compound_literal_p;
6012 const char *saved_message;
6013
6014 /* There's no way to know yet whether or not this is a cast.
6015 For example, `(int (3))' is a unary-expression, while `(int)
6016 3' is a cast. So, we resort to parsing tentatively. */
6017 cp_parser_parse_tentatively (parser);
6018 /* Types may not be defined in a cast. */
6019 saved_message = parser->type_definition_forbidden_message;
6020 parser->type_definition_forbidden_message
6021 = "types may not be defined in casts";
6022 /* Consume the `('. */
6023 cp_lexer_consume_token (parser->lexer);
6024 /* A very tricky bit is that `(struct S) { 3 }' is a
6025 compound-literal (which we permit in C++ as an extension).
6026 But, that construct is not a cast-expression -- it is a
6027 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6028 is legal; if the compound-literal were a cast-expression,
6029 you'd need an extra set of parentheses.) But, if we parse
6030 the type-id, and it happens to be a class-specifier, then we
6031 will commit to the parse at that point, because we cannot
6032 undo the action that is done when creating a new class. So,
6033 then we cannot back up and do a postfix-expression.
6034
6035 Therefore, we scan ahead to the closing `)', and check to see
6036 if the token after the `)' is a `{'. If so, we are not
6037 looking at a cast-expression.
6038
6039 Save tokens so that we can put them back. */
6040 cp_lexer_save_tokens (parser->lexer);
6041 /* Skip tokens until the next token is a closing parenthesis.
6042 If we find the closing `)', and the next token is a `{', then
6043 we are looking at a compound-literal. */
6044 compound_literal_p
6045 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6046 /*consume_paren=*/true)
6047 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6048 /* Roll back the tokens we skipped. */
6049 cp_lexer_rollback_tokens (parser->lexer);
6050 /* If we were looking at a compound-literal, simulate an error
6051 so that the call to cp_parser_parse_definitely below will
6052 fail. */
6053 if (compound_literal_p)
6054 cp_parser_simulate_error (parser);
6055 else
6056 {
6057 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6058 parser->in_type_id_in_expr_p = true;
6059 /* Look for the type-id. */
6060 type = cp_parser_type_id (parser);
6061 /* Look for the closing `)'. */
6062 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6064 }
6065
6066 /* Restore the saved message. */
6067 parser->type_definition_forbidden_message = saved_message;
6068
6069 /* At this point this can only be either a cast or a
6070 parenthesized ctor such as `(T ())' that looks like a cast to
6071 function returning T. */
6072 if (!cp_parser_error_occurred (parser)
6073 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6074 (parser->lexer)))
6075 {
6076 cp_parser_parse_definitely (parser);
6077 expr = cp_parser_cast_expression (parser,
6078 /*address_p=*/false,
6079 /*cast_p=*/true);
6080
6081 /* Warn about old-style casts, if so requested. */
6082 if (warn_old_style_cast
6083 && !in_system_header
6084 && !VOID_TYPE_P (type)
6085 && current_lang_name != lang_name_c)
6086 warning (OPT_Wold_style_cast, "use of old-style cast");
6087
6088 /* Only type conversions to integral or enumeration types
6089 can be used in constant-expressions. */
6090 if (!cast_valid_in_integral_constant_expression_p (type)
6091 && (cp_parser_non_integral_constant_expression
6092 (parser,
6093 "a cast to a type other than an integral or "
6094 "enumeration type")))
6095 return error_mark_node;
6096
6097 /* Perform the cast. */
6098 expr = build_c_cast (type, expr);
6099 return expr;
6100 }
6101 else
6102 cp_parser_abort_tentative_parse (parser);
6103 }
6104
6105 /* If we get here, then it's not a cast, so it must be a
6106 unary-expression. */
6107 return cp_parser_unary_expression (parser, address_p, cast_p);
6108 }
6109
6110 /* Parse a binary expression of the general form:
6111
6112 pm-expression:
6113 cast-expression
6114 pm-expression .* cast-expression
6115 pm-expression ->* cast-expression
6116
6117 multiplicative-expression:
6118 pm-expression
6119 multiplicative-expression * pm-expression
6120 multiplicative-expression / pm-expression
6121 multiplicative-expression % pm-expression
6122
6123 additive-expression:
6124 multiplicative-expression
6125 additive-expression + multiplicative-expression
6126 additive-expression - multiplicative-expression
6127
6128 shift-expression:
6129 additive-expression
6130 shift-expression << additive-expression
6131 shift-expression >> additive-expression
6132
6133 relational-expression:
6134 shift-expression
6135 relational-expression < shift-expression
6136 relational-expression > shift-expression
6137 relational-expression <= shift-expression
6138 relational-expression >= shift-expression
6139
6140 GNU Extension:
6141
6142 relational-expression:
6143 relational-expression <? shift-expression
6144 relational-expression >? shift-expression
6145
6146 equality-expression:
6147 relational-expression
6148 equality-expression == relational-expression
6149 equality-expression != relational-expression
6150
6151 and-expression:
6152 equality-expression
6153 and-expression & equality-expression
6154
6155 exclusive-or-expression:
6156 and-expression
6157 exclusive-or-expression ^ and-expression
6158
6159 inclusive-or-expression:
6160 exclusive-or-expression
6161 inclusive-or-expression | exclusive-or-expression
6162
6163 logical-and-expression:
6164 inclusive-or-expression
6165 logical-and-expression && inclusive-or-expression
6166
6167 logical-or-expression:
6168 logical-and-expression
6169 logical-or-expression || logical-and-expression
6170
6171 All these are implemented with a single function like:
6172
6173 binary-expression:
6174 simple-cast-expression
6175 binary-expression <token> binary-expression
6176
6177 CAST_P is true if this expression is the target of a cast.
6178
6179 The binops_by_token map is used to get the tree codes for each <token> type.
6180 binary-expressions are associated according to a precedence table. */
6181
6182 #define TOKEN_PRECEDENCE(token) \
6183 (((token->type == CPP_GREATER \
6184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6185 && !parser->greater_than_is_operator_p) \
6186 ? PREC_NOT_OPERATOR \
6187 : binops_by_token[token->type].prec)
6188
6189 static tree
6190 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6191 enum cp_parser_prec prec)
6192 {
6193 cp_parser_expression_stack stack;
6194 cp_parser_expression_stack_entry *sp = &stack[0];
6195 tree lhs, rhs;
6196 cp_token *token;
6197 enum tree_code tree_type, lhs_type, rhs_type;
6198 enum cp_parser_prec new_prec, lookahead_prec;
6199 bool overloaded_p;
6200
6201 /* Parse the first expression. */
6202 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6203 lhs_type = ERROR_MARK;
6204
6205 for (;;)
6206 {
6207 /* Get an operator token. */
6208 token = cp_lexer_peek_token (parser->lexer);
6209
6210 if (warn_cxx0x_compat
6211 && token->type == CPP_RSHIFT
6212 && !parser->greater_than_is_operator_p)
6213 {
6214 warning (OPT_Wc__0x_compat,
6215 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6216 &token->location);
6217 warning (OPT_Wc__0x_compat,
6218 "suggest parentheses around %<>>%> expression");
6219 }
6220
6221 new_prec = TOKEN_PRECEDENCE (token);
6222
6223 /* Popping an entry off the stack means we completed a subexpression:
6224 - either we found a token which is not an operator (`>' where it is not
6225 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6226 will happen repeatedly;
6227 - or, we found an operator which has lower priority. This is the case
6228 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6229 parsing `3 * 4'. */
6230 if (new_prec <= prec)
6231 {
6232 if (sp == stack)
6233 break;
6234 else
6235 goto pop;
6236 }
6237
6238 get_rhs:
6239 tree_type = binops_by_token[token->type].tree_type;
6240
6241 /* We used the operator token. */
6242 cp_lexer_consume_token (parser->lexer);
6243
6244 /* Extract another operand. It may be the RHS of this expression
6245 or the LHS of a new, higher priority expression. */
6246 rhs = cp_parser_simple_cast_expression (parser);
6247 rhs_type = ERROR_MARK;
6248
6249 /* Get another operator token. Look up its precedence to avoid
6250 building a useless (immediately popped) stack entry for common
6251 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6252 token = cp_lexer_peek_token (parser->lexer);
6253 lookahead_prec = TOKEN_PRECEDENCE (token);
6254 if (lookahead_prec > new_prec)
6255 {
6256 /* ... and prepare to parse the RHS of the new, higher priority
6257 expression. Since precedence levels on the stack are
6258 monotonically increasing, we do not have to care about
6259 stack overflows. */
6260 sp->prec = prec;
6261 sp->tree_type = tree_type;
6262 sp->lhs = lhs;
6263 sp->lhs_type = lhs_type;
6264 sp++;
6265 lhs = rhs;
6266 lhs_type = rhs_type;
6267 prec = new_prec;
6268 new_prec = lookahead_prec;
6269 goto get_rhs;
6270
6271 pop:
6272 /* If the stack is not empty, we have parsed into LHS the right side
6273 (`4' in the example above) of an expression we had suspended.
6274 We can use the information on the stack to recover the LHS (`3')
6275 from the stack together with the tree code (`MULT_EXPR'), and
6276 the precedence of the higher level subexpression
6277 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6278 which will be used to actually build the additive expression. */
6279 --sp;
6280 prec = sp->prec;
6281 tree_type = sp->tree_type;
6282 rhs = lhs;
6283 rhs_type = lhs_type;
6284 lhs = sp->lhs;
6285 lhs_type = sp->lhs_type;
6286 }
6287
6288 overloaded_p = false;
6289 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6290 ERROR_MARK for everything that is not a binary expression.
6291 This makes warn_about_parentheses miss some warnings that
6292 involve unary operators. For unary expressions we should
6293 pass the correct tree_code unless the unary expression was
6294 surrounded by parentheses.
6295 */
6296 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6297 &overloaded_p, tf_warning_or_error);
6298 lhs_type = tree_type;
6299
6300 /* If the binary operator required the use of an overloaded operator,
6301 then this expression cannot be an integral constant-expression.
6302 An overloaded operator can be used even if both operands are
6303 otherwise permissible in an integral constant-expression if at
6304 least one of the operands is of enumeration type. */
6305
6306 if (overloaded_p
6307 && (cp_parser_non_integral_constant_expression
6308 (parser, "calls to overloaded operators")))
6309 return error_mark_node;
6310 }
6311
6312 return lhs;
6313 }
6314
6315
6316 /* Parse the `? expression : assignment-expression' part of a
6317 conditional-expression. The LOGICAL_OR_EXPR is the
6318 logical-or-expression that started the conditional-expression.
6319 Returns a representation of the entire conditional-expression.
6320
6321 This routine is used by cp_parser_assignment_expression.
6322
6323 ? expression : assignment-expression
6324
6325 GNU Extensions:
6326
6327 ? : assignment-expression */
6328
6329 static tree
6330 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6331 {
6332 tree expr;
6333 tree assignment_expr;
6334
6335 /* Consume the `?' token. */
6336 cp_lexer_consume_token (parser->lexer);
6337 if (cp_parser_allow_gnu_extensions_p (parser)
6338 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6339 /* Implicit true clause. */
6340 expr = NULL_TREE;
6341 else
6342 /* Parse the expression. */
6343 expr = cp_parser_expression (parser, /*cast_p=*/false);
6344
6345 /* The next token should be a `:'. */
6346 cp_parser_require (parser, CPP_COLON, "%<:%>");
6347 /* Parse the assignment-expression. */
6348 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6349
6350 /* Build the conditional-expression. */
6351 return build_x_conditional_expr (logical_or_expr,
6352 expr,
6353 assignment_expr,
6354 tf_warning_or_error);
6355 }
6356
6357 /* Parse an assignment-expression.
6358
6359 assignment-expression:
6360 conditional-expression
6361 logical-or-expression assignment-operator assignment_expression
6362 throw-expression
6363
6364 CAST_P is true if this expression is the target of a cast.
6365
6366 Returns a representation for the expression. */
6367
6368 static tree
6369 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6370 {
6371 tree expr;
6372
6373 /* If the next token is the `throw' keyword, then we're looking at
6374 a throw-expression. */
6375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6376 expr = cp_parser_throw_expression (parser);
6377 /* Otherwise, it must be that we are looking at a
6378 logical-or-expression. */
6379 else
6380 {
6381 /* Parse the binary expressions (logical-or-expression). */
6382 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6383 /* If the next token is a `?' then we're actually looking at a
6384 conditional-expression. */
6385 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6386 return cp_parser_question_colon_clause (parser, expr);
6387 else
6388 {
6389 enum tree_code assignment_operator;
6390
6391 /* If it's an assignment-operator, we're using the second
6392 production. */
6393 assignment_operator
6394 = cp_parser_assignment_operator_opt (parser);
6395 if (assignment_operator != ERROR_MARK)
6396 {
6397 bool non_constant_p;
6398
6399 /* Parse the right-hand side of the assignment. */
6400 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6401
6402 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6403 maybe_warn_cpp0x ("extended initializer lists");
6404
6405 /* An assignment may not appear in a
6406 constant-expression. */
6407 if (cp_parser_non_integral_constant_expression (parser,
6408 "an assignment"))
6409 return error_mark_node;
6410 /* Build the assignment expression. */
6411 expr = build_x_modify_expr (expr,
6412 assignment_operator,
6413 rhs,
6414 tf_warning_or_error);
6415 }
6416 }
6417 }
6418
6419 return expr;
6420 }
6421
6422 /* Parse an (optional) assignment-operator.
6423
6424 assignment-operator: one of
6425 = *= /= %= += -= >>= <<= &= ^= |=
6426
6427 GNU Extension:
6428
6429 assignment-operator: one of
6430 <?= >?=
6431
6432 If the next token is an assignment operator, the corresponding tree
6433 code is returned, and the token is consumed. For example, for
6434 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6435 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6436 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6437 operator, ERROR_MARK is returned. */
6438
6439 static enum tree_code
6440 cp_parser_assignment_operator_opt (cp_parser* parser)
6441 {
6442 enum tree_code op;
6443 cp_token *token;
6444
6445 /* Peek at the next token. */
6446 token = cp_lexer_peek_token (parser->lexer);
6447
6448 switch (token->type)
6449 {
6450 case CPP_EQ:
6451 op = NOP_EXPR;
6452 break;
6453
6454 case CPP_MULT_EQ:
6455 op = MULT_EXPR;
6456 break;
6457
6458 case CPP_DIV_EQ:
6459 op = TRUNC_DIV_EXPR;
6460 break;
6461
6462 case CPP_MOD_EQ:
6463 op = TRUNC_MOD_EXPR;
6464 break;
6465
6466 case CPP_PLUS_EQ:
6467 op = PLUS_EXPR;
6468 break;
6469
6470 case CPP_MINUS_EQ:
6471 op = MINUS_EXPR;
6472 break;
6473
6474 case CPP_RSHIFT_EQ:
6475 op = RSHIFT_EXPR;
6476 break;
6477
6478 case CPP_LSHIFT_EQ:
6479 op = LSHIFT_EXPR;
6480 break;
6481
6482 case CPP_AND_EQ:
6483 op = BIT_AND_EXPR;
6484 break;
6485
6486 case CPP_XOR_EQ:
6487 op = BIT_XOR_EXPR;
6488 break;
6489
6490 case CPP_OR_EQ:
6491 op = BIT_IOR_EXPR;
6492 break;
6493
6494 default:
6495 /* Nothing else is an assignment operator. */
6496 op = ERROR_MARK;
6497 }
6498
6499 /* If it was an assignment operator, consume it. */
6500 if (op != ERROR_MARK)
6501 cp_lexer_consume_token (parser->lexer);
6502
6503 return op;
6504 }
6505
6506 /* Parse an expression.
6507
6508 expression:
6509 assignment-expression
6510 expression , assignment-expression
6511
6512 CAST_P is true if this expression is the target of a cast.
6513
6514 Returns a representation of the expression. */
6515
6516 static tree
6517 cp_parser_expression (cp_parser* parser, bool cast_p)
6518 {
6519 tree expression = NULL_TREE;
6520
6521 while (true)
6522 {
6523 tree assignment_expression;
6524
6525 /* Parse the next assignment-expression. */
6526 assignment_expression
6527 = cp_parser_assignment_expression (parser, cast_p);
6528 /* If this is the first assignment-expression, we can just
6529 save it away. */
6530 if (!expression)
6531 expression = assignment_expression;
6532 else
6533 expression = build_x_compound_expr (expression,
6534 assignment_expression,
6535 tf_warning_or_error);
6536 /* If the next token is not a comma, then we are done with the
6537 expression. */
6538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6539 break;
6540 /* Consume the `,'. */
6541 cp_lexer_consume_token (parser->lexer);
6542 /* A comma operator cannot appear in a constant-expression. */
6543 if (cp_parser_non_integral_constant_expression (parser,
6544 "a comma operator"))
6545 expression = error_mark_node;
6546 }
6547
6548 return expression;
6549 }
6550
6551 /* Parse a constant-expression.
6552
6553 constant-expression:
6554 conditional-expression
6555
6556 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6557 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6558 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6559 is false, NON_CONSTANT_P should be NULL. */
6560
6561 static tree
6562 cp_parser_constant_expression (cp_parser* parser,
6563 bool allow_non_constant_p,
6564 bool *non_constant_p)
6565 {
6566 bool saved_integral_constant_expression_p;
6567 bool saved_allow_non_integral_constant_expression_p;
6568 bool saved_non_integral_constant_expression_p;
6569 tree expression;
6570
6571 /* It might seem that we could simply parse the
6572 conditional-expression, and then check to see if it were
6573 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6574 one that the compiler can figure out is constant, possibly after
6575 doing some simplifications or optimizations. The standard has a
6576 precise definition of constant-expression, and we must honor
6577 that, even though it is somewhat more restrictive.
6578
6579 For example:
6580
6581 int i[(2, 3)];
6582
6583 is not a legal declaration, because `(2, 3)' is not a
6584 constant-expression. The `,' operator is forbidden in a
6585 constant-expression. However, GCC's constant-folding machinery
6586 will fold this operation to an INTEGER_CST for `3'. */
6587
6588 /* Save the old settings. */
6589 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6590 saved_allow_non_integral_constant_expression_p
6591 = parser->allow_non_integral_constant_expression_p;
6592 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6593 /* We are now parsing a constant-expression. */
6594 parser->integral_constant_expression_p = true;
6595 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6596 parser->non_integral_constant_expression_p = false;
6597 /* Although the grammar says "conditional-expression", we parse an
6598 "assignment-expression", which also permits "throw-expression"
6599 and the use of assignment operators. In the case that
6600 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6601 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6602 actually essential that we look for an assignment-expression.
6603 For example, cp_parser_initializer_clauses uses this function to
6604 determine whether a particular assignment-expression is in fact
6605 constant. */
6606 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6607 /* Restore the old settings. */
6608 parser->integral_constant_expression_p
6609 = saved_integral_constant_expression_p;
6610 parser->allow_non_integral_constant_expression_p
6611 = saved_allow_non_integral_constant_expression_p;
6612 if (allow_non_constant_p)
6613 *non_constant_p = parser->non_integral_constant_expression_p;
6614 else if (parser->non_integral_constant_expression_p)
6615 expression = error_mark_node;
6616 parser->non_integral_constant_expression_p
6617 = saved_non_integral_constant_expression_p;
6618
6619 return expression;
6620 }
6621
6622 /* Parse __builtin_offsetof.
6623
6624 offsetof-expression:
6625 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6626
6627 offsetof-member-designator:
6628 id-expression
6629 | offsetof-member-designator "." id-expression
6630 | offsetof-member-designator "[" expression "]"
6631 | offsetof-member-designator "->" id-expression */
6632
6633 static tree
6634 cp_parser_builtin_offsetof (cp_parser *parser)
6635 {
6636 int save_ice_p, save_non_ice_p;
6637 tree type, expr;
6638 cp_id_kind dummy;
6639 cp_token *token;
6640
6641 /* We're about to accept non-integral-constant things, but will
6642 definitely yield an integral constant expression. Save and
6643 restore these values around our local parsing. */
6644 save_ice_p = parser->integral_constant_expression_p;
6645 save_non_ice_p = parser->non_integral_constant_expression_p;
6646
6647 /* Consume the "__builtin_offsetof" token. */
6648 cp_lexer_consume_token (parser->lexer);
6649 /* Consume the opening `('. */
6650 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6651 /* Parse the type-id. */
6652 type = cp_parser_type_id (parser);
6653 /* Look for the `,'. */
6654 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6655 token = cp_lexer_peek_token (parser->lexer);
6656
6657 /* Build the (type *)null that begins the traditional offsetof macro. */
6658 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6659 tf_warning_or_error);
6660
6661 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6662 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6663 true, &dummy, token->location);
6664 while (true)
6665 {
6666 token = cp_lexer_peek_token (parser->lexer);
6667 switch (token->type)
6668 {
6669 case CPP_OPEN_SQUARE:
6670 /* offsetof-member-designator "[" expression "]" */
6671 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6672 break;
6673
6674 case CPP_DEREF:
6675 /* offsetof-member-designator "->" identifier */
6676 expr = grok_array_decl (expr, integer_zero_node);
6677 /* FALLTHRU */
6678
6679 case CPP_DOT:
6680 /* offsetof-member-designator "." identifier */
6681 cp_lexer_consume_token (parser->lexer);
6682 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6683 expr, true, &dummy,
6684 token->location);
6685 break;
6686
6687 case CPP_CLOSE_PAREN:
6688 /* Consume the ")" token. */
6689 cp_lexer_consume_token (parser->lexer);
6690 goto success;
6691
6692 default:
6693 /* Error. We know the following require will fail, but
6694 that gives the proper error message. */
6695 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6696 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6697 expr = error_mark_node;
6698 goto failure;
6699 }
6700 }
6701
6702 success:
6703 /* If we're processing a template, we can't finish the semantics yet.
6704 Otherwise we can fold the entire expression now. */
6705 if (processing_template_decl)
6706 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6707 else
6708 expr = finish_offsetof (expr);
6709
6710 failure:
6711 parser->integral_constant_expression_p = save_ice_p;
6712 parser->non_integral_constant_expression_p = save_non_ice_p;
6713
6714 return expr;
6715 }
6716
6717 /* Parse a trait expression. */
6718
6719 static tree
6720 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6721 {
6722 cp_trait_kind kind;
6723 tree type1, type2 = NULL_TREE;
6724 bool binary = false;
6725 cp_decl_specifier_seq decl_specs;
6726
6727 switch (keyword)
6728 {
6729 case RID_HAS_NOTHROW_ASSIGN:
6730 kind = CPTK_HAS_NOTHROW_ASSIGN;
6731 break;
6732 case RID_HAS_NOTHROW_CONSTRUCTOR:
6733 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6734 break;
6735 case RID_HAS_NOTHROW_COPY:
6736 kind = CPTK_HAS_NOTHROW_COPY;
6737 break;
6738 case RID_HAS_TRIVIAL_ASSIGN:
6739 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6740 break;
6741 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6742 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6743 break;
6744 case RID_HAS_TRIVIAL_COPY:
6745 kind = CPTK_HAS_TRIVIAL_COPY;
6746 break;
6747 case RID_HAS_TRIVIAL_DESTRUCTOR:
6748 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6749 break;
6750 case RID_HAS_VIRTUAL_DESTRUCTOR:
6751 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6752 break;
6753 case RID_IS_ABSTRACT:
6754 kind = CPTK_IS_ABSTRACT;
6755 break;
6756 case RID_IS_BASE_OF:
6757 kind = CPTK_IS_BASE_OF;
6758 binary = true;
6759 break;
6760 case RID_IS_CLASS:
6761 kind = CPTK_IS_CLASS;
6762 break;
6763 case RID_IS_CONVERTIBLE_TO:
6764 kind = CPTK_IS_CONVERTIBLE_TO;
6765 binary = true;
6766 break;
6767 case RID_IS_EMPTY:
6768 kind = CPTK_IS_EMPTY;
6769 break;
6770 case RID_IS_ENUM:
6771 kind = CPTK_IS_ENUM;
6772 break;
6773 case RID_IS_POD:
6774 kind = CPTK_IS_POD;
6775 break;
6776 case RID_IS_POLYMORPHIC:
6777 kind = CPTK_IS_POLYMORPHIC;
6778 break;
6779 case RID_IS_UNION:
6780 kind = CPTK_IS_UNION;
6781 break;
6782 default:
6783 gcc_unreachable ();
6784 }
6785
6786 /* Consume the token. */
6787 cp_lexer_consume_token (parser->lexer);
6788
6789 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6790
6791 type1 = cp_parser_type_id (parser);
6792
6793 if (type1 == error_mark_node)
6794 return error_mark_node;
6795
6796 /* Build a trivial decl-specifier-seq. */
6797 clear_decl_specs (&decl_specs);
6798 decl_specs.type = type1;
6799
6800 /* Call grokdeclarator to figure out what type this is. */
6801 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6802 /*initialized=*/0, /*attrlist=*/NULL);
6803
6804 if (binary)
6805 {
6806 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6807
6808 type2 = cp_parser_type_id (parser);
6809
6810 if (type2 == error_mark_node)
6811 return error_mark_node;
6812
6813 /* Build a trivial decl-specifier-seq. */
6814 clear_decl_specs (&decl_specs);
6815 decl_specs.type = type2;
6816
6817 /* Call grokdeclarator to figure out what type this is. */
6818 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6819 /*initialized=*/0, /*attrlist=*/NULL);
6820 }
6821
6822 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6823
6824 /* Complete the trait expression, which may mean either processing
6825 the trait expr now or saving it for template instantiation. */
6826 return finish_trait_expr (kind, type1, type2);
6827 }
6828
6829 /* Statements [gram.stmt.stmt] */
6830
6831 /* Parse a statement.
6832
6833 statement:
6834 labeled-statement
6835 expression-statement
6836 compound-statement
6837 selection-statement
6838 iteration-statement
6839 jump-statement
6840 declaration-statement
6841 try-block
6842
6843 IN_COMPOUND is true when the statement is nested inside a
6844 cp_parser_compound_statement; this matters for certain pragmas.
6845
6846 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6847 is a (possibly labeled) if statement which is not enclosed in braces
6848 and has an else clause. This is used to implement -Wparentheses. */
6849
6850 static void
6851 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6852 bool in_compound, bool *if_p)
6853 {
6854 tree statement;
6855 cp_token *token;
6856 location_t statement_location;
6857
6858 restart:
6859 if (if_p != NULL)
6860 *if_p = false;
6861 /* There is no statement yet. */
6862 statement = NULL_TREE;
6863 /* Peek at the next token. */
6864 token = cp_lexer_peek_token (parser->lexer);
6865 /* Remember the location of the first token in the statement. */
6866 statement_location = token->location;
6867 /* If this is a keyword, then that will often determine what kind of
6868 statement we have. */
6869 if (token->type == CPP_KEYWORD)
6870 {
6871 enum rid keyword = token->keyword;
6872
6873 switch (keyword)
6874 {
6875 case RID_CASE:
6876 case RID_DEFAULT:
6877 /* Looks like a labeled-statement with a case label.
6878 Parse the label, and then use tail recursion to parse
6879 the statement. */
6880 cp_parser_label_for_labeled_statement (parser);
6881 goto restart;
6882
6883 case RID_IF:
6884 case RID_SWITCH:
6885 statement = cp_parser_selection_statement (parser, if_p);
6886 break;
6887
6888 case RID_WHILE:
6889 case RID_DO:
6890 case RID_FOR:
6891 statement = cp_parser_iteration_statement (parser);
6892 break;
6893
6894 case RID_BREAK:
6895 case RID_CONTINUE:
6896 case RID_RETURN:
6897 case RID_GOTO:
6898 statement = cp_parser_jump_statement (parser);
6899 break;
6900
6901 /* Objective-C++ exception-handling constructs. */
6902 case RID_AT_TRY:
6903 case RID_AT_CATCH:
6904 case RID_AT_FINALLY:
6905 case RID_AT_SYNCHRONIZED:
6906 case RID_AT_THROW:
6907 statement = cp_parser_objc_statement (parser);
6908 break;
6909
6910 case RID_TRY:
6911 statement = cp_parser_try_block (parser);
6912 break;
6913
6914 case RID_NAMESPACE:
6915 /* This must be a namespace alias definition. */
6916 cp_parser_declaration_statement (parser);
6917 return;
6918
6919 default:
6920 /* It might be a keyword like `int' that can start a
6921 declaration-statement. */
6922 break;
6923 }
6924 }
6925 else if (token->type == CPP_NAME)
6926 {
6927 /* If the next token is a `:', then we are looking at a
6928 labeled-statement. */
6929 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6930 if (token->type == CPP_COLON)
6931 {
6932 /* Looks like a labeled-statement with an ordinary label.
6933 Parse the label, and then use tail recursion to parse
6934 the statement. */
6935 cp_parser_label_for_labeled_statement (parser);
6936 goto restart;
6937 }
6938 }
6939 /* Anything that starts with a `{' must be a compound-statement. */
6940 else if (token->type == CPP_OPEN_BRACE)
6941 statement = cp_parser_compound_statement (parser, NULL, false);
6942 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6943 a statement all its own. */
6944 else if (token->type == CPP_PRAGMA)
6945 {
6946 /* Only certain OpenMP pragmas are attached to statements, and thus
6947 are considered statements themselves. All others are not. In
6948 the context of a compound, accept the pragma as a "statement" and
6949 return so that we can check for a close brace. Otherwise we
6950 require a real statement and must go back and read one. */
6951 if (in_compound)
6952 cp_parser_pragma (parser, pragma_compound);
6953 else if (!cp_parser_pragma (parser, pragma_stmt))
6954 goto restart;
6955 return;
6956 }
6957 else if (token->type == CPP_EOF)
6958 {
6959 cp_parser_error (parser, "expected statement");
6960 return;
6961 }
6962
6963 /* Everything else must be a declaration-statement or an
6964 expression-statement. Try for the declaration-statement
6965 first, unless we are looking at a `;', in which case we know that
6966 we have an expression-statement. */
6967 if (!statement)
6968 {
6969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6970 {
6971 cp_parser_parse_tentatively (parser);
6972 /* Try to parse the declaration-statement. */
6973 cp_parser_declaration_statement (parser);
6974 /* If that worked, we're done. */
6975 if (cp_parser_parse_definitely (parser))
6976 return;
6977 }
6978 /* Look for an expression-statement instead. */
6979 statement = cp_parser_expression_statement (parser, in_statement_expr);
6980 }
6981
6982 /* Set the line number for the statement. */
6983 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6984 SET_EXPR_LOCATION (statement, statement_location);
6985 }
6986
6987 /* Parse the label for a labeled-statement, i.e.
6988
6989 identifier :
6990 case constant-expression :
6991 default :
6992
6993 GNU Extension:
6994 case constant-expression ... constant-expression : statement
6995
6996 When a label is parsed without errors, the label is added to the
6997 parse tree by the finish_* functions, so this function doesn't
6998 have to return the label. */
6999
7000 static void
7001 cp_parser_label_for_labeled_statement (cp_parser* parser)
7002 {
7003 cp_token *token;
7004
7005 /* The next token should be an identifier. */
7006 token = cp_lexer_peek_token (parser->lexer);
7007 if (token->type != CPP_NAME
7008 && token->type != CPP_KEYWORD)
7009 {
7010 cp_parser_error (parser, "expected labeled-statement");
7011 return;
7012 }
7013
7014 switch (token->keyword)
7015 {
7016 case RID_CASE:
7017 {
7018 tree expr, expr_hi;
7019 cp_token *ellipsis;
7020
7021 /* Consume the `case' token. */
7022 cp_lexer_consume_token (parser->lexer);
7023 /* Parse the constant-expression. */
7024 expr = cp_parser_constant_expression (parser,
7025 /*allow_non_constant_p=*/false,
7026 NULL);
7027
7028 ellipsis = cp_lexer_peek_token (parser->lexer);
7029 if (ellipsis->type == CPP_ELLIPSIS)
7030 {
7031 /* Consume the `...' token. */
7032 cp_lexer_consume_token (parser->lexer);
7033 expr_hi =
7034 cp_parser_constant_expression (parser,
7035 /*allow_non_constant_p=*/false,
7036 NULL);
7037 /* We don't need to emit warnings here, as the common code
7038 will do this for us. */
7039 }
7040 else
7041 expr_hi = NULL_TREE;
7042
7043 if (parser->in_switch_statement_p)
7044 finish_case_label (expr, expr_hi);
7045 else
7046 error ("%Hcase label %qE not within a switch statement",
7047 &token->location, expr);
7048 }
7049 break;
7050
7051 case RID_DEFAULT:
7052 /* Consume the `default' token. */
7053 cp_lexer_consume_token (parser->lexer);
7054
7055 if (parser->in_switch_statement_p)
7056 finish_case_label (NULL_TREE, NULL_TREE);
7057 else
7058 error ("%Hcase label not within a switch statement", &token->location);
7059 break;
7060
7061 default:
7062 /* Anything else must be an ordinary label. */
7063 finish_label_stmt (cp_parser_identifier (parser));
7064 break;
7065 }
7066
7067 /* Require the `:' token. */
7068 cp_parser_require (parser, CPP_COLON, "%<:%>");
7069 }
7070
7071 /* Parse an expression-statement.
7072
7073 expression-statement:
7074 expression [opt] ;
7075
7076 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7077 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7078 indicates whether this expression-statement is part of an
7079 expression statement. */
7080
7081 static tree
7082 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7083 {
7084 tree statement = NULL_TREE;
7085
7086 /* If the next token is a ';', then there is no expression
7087 statement. */
7088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7089 statement = cp_parser_expression (parser, /*cast_p=*/false);
7090
7091 /* Consume the final `;'. */
7092 cp_parser_consume_semicolon_at_end_of_statement (parser);
7093
7094 if (in_statement_expr
7095 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7096 /* This is the final expression statement of a statement
7097 expression. */
7098 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7099 else if (statement)
7100 statement = finish_expr_stmt (statement);
7101 else
7102 finish_stmt ();
7103
7104 return statement;
7105 }
7106
7107 /* Parse a compound-statement.
7108
7109 compound-statement:
7110 { statement-seq [opt] }
7111
7112 GNU extension:
7113
7114 compound-statement:
7115 { label-declaration-seq [opt] statement-seq [opt] }
7116
7117 label-declaration-seq:
7118 label-declaration
7119 label-declaration-seq label-declaration
7120
7121 Returns a tree representing the statement. */
7122
7123 static tree
7124 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7125 bool in_try)
7126 {
7127 tree compound_stmt;
7128
7129 /* Consume the `{'. */
7130 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7131 return error_mark_node;
7132 /* Begin the compound-statement. */
7133 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7134 /* If the next keyword is `__label__' we have a label declaration. */
7135 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7136 cp_parser_label_declaration (parser);
7137 /* Parse an (optional) statement-seq. */
7138 cp_parser_statement_seq_opt (parser, in_statement_expr);
7139 /* Finish the compound-statement. */
7140 finish_compound_stmt (compound_stmt);
7141 /* Consume the `}'. */
7142 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7143
7144 return compound_stmt;
7145 }
7146
7147 /* Parse an (optional) statement-seq.
7148
7149 statement-seq:
7150 statement
7151 statement-seq [opt] statement */
7152
7153 static void
7154 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7155 {
7156 /* Scan statements until there aren't any more. */
7157 while (true)
7158 {
7159 cp_token *token = cp_lexer_peek_token (parser->lexer);
7160
7161 /* If we're looking at a `}', then we've run out of statements. */
7162 if (token->type == CPP_CLOSE_BRACE
7163 || token->type == CPP_EOF
7164 || token->type == CPP_PRAGMA_EOL)
7165 break;
7166
7167 /* If we are in a compound statement and find 'else' then
7168 something went wrong. */
7169 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7170 {
7171 if (parser->in_statement & IN_IF_STMT)
7172 break;
7173 else
7174 {
7175 token = cp_lexer_consume_token (parser->lexer);
7176 error ("%H%<else%> without a previous %<if%>", &token->location);
7177 }
7178 }
7179
7180 /* Parse the statement. */
7181 cp_parser_statement (parser, in_statement_expr, true, NULL);
7182 }
7183 }
7184
7185 /* Parse a selection-statement.
7186
7187 selection-statement:
7188 if ( condition ) statement
7189 if ( condition ) statement else statement
7190 switch ( condition ) statement
7191
7192 Returns the new IF_STMT or SWITCH_STMT.
7193
7194 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7195 is a (possibly labeled) if statement which is not enclosed in
7196 braces and has an else clause. This is used to implement
7197 -Wparentheses. */
7198
7199 static tree
7200 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7201 {
7202 cp_token *token;
7203 enum rid keyword;
7204
7205 if (if_p != NULL)
7206 *if_p = false;
7207
7208 /* Peek at the next token. */
7209 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7210
7211 /* See what kind of keyword it is. */
7212 keyword = token->keyword;
7213 switch (keyword)
7214 {
7215 case RID_IF:
7216 case RID_SWITCH:
7217 {
7218 tree statement;
7219 tree condition;
7220
7221 /* Look for the `('. */
7222 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7223 {
7224 cp_parser_skip_to_end_of_statement (parser);
7225 return error_mark_node;
7226 }
7227
7228 /* Begin the selection-statement. */
7229 if (keyword == RID_IF)
7230 statement = begin_if_stmt ();
7231 else
7232 statement = begin_switch_stmt ();
7233
7234 /* Parse the condition. */
7235 condition = cp_parser_condition (parser);
7236 /* Look for the `)'. */
7237 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7238 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7239 /*consume_paren=*/true);
7240
7241 if (keyword == RID_IF)
7242 {
7243 bool nested_if;
7244 unsigned char in_statement;
7245
7246 /* Add the condition. */
7247 finish_if_stmt_cond (condition, statement);
7248
7249 /* Parse the then-clause. */
7250 in_statement = parser->in_statement;
7251 parser->in_statement |= IN_IF_STMT;
7252 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7253 {
7254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7255 add_stmt (build_empty_stmt ());
7256 cp_lexer_consume_token (parser->lexer);
7257 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7258 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7259 "empty body in an %<if%> statement");
7260 nested_if = false;
7261 }
7262 else
7263 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7264 parser->in_statement = in_statement;
7265
7266 finish_then_clause (statement);
7267
7268 /* If the next token is `else', parse the else-clause. */
7269 if (cp_lexer_next_token_is_keyword (parser->lexer,
7270 RID_ELSE))
7271 {
7272 /* Consume the `else' keyword. */
7273 cp_lexer_consume_token (parser->lexer);
7274 begin_else_clause (statement);
7275 /* Parse the else-clause. */
7276 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7277 {
7278 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7279 OPT_Wempty_body, "suggest braces around "
7280 "empty body in an %<else%> statement");
7281 add_stmt (build_empty_stmt ());
7282 cp_lexer_consume_token (parser->lexer);
7283 }
7284 else
7285 cp_parser_implicitly_scoped_statement (parser, NULL);
7286
7287 finish_else_clause (statement);
7288
7289 /* If we are currently parsing a then-clause, then
7290 IF_P will not be NULL. We set it to true to
7291 indicate that this if statement has an else clause.
7292 This may trigger the Wparentheses warning below
7293 when we get back up to the parent if statement. */
7294 if (if_p != NULL)
7295 *if_p = true;
7296 }
7297 else
7298 {
7299 /* This if statement does not have an else clause. If
7300 NESTED_IF is true, then the then-clause is an if
7301 statement which does have an else clause. We warn
7302 about the potential ambiguity. */
7303 if (nested_if)
7304 warning (OPT_Wparentheses,
7305 ("%Hsuggest explicit braces "
7306 "to avoid ambiguous %<else%>"),
7307 EXPR_LOCUS (statement));
7308 }
7309
7310 /* Now we're all done with the if-statement. */
7311 finish_if_stmt (statement);
7312 }
7313 else
7314 {
7315 bool in_switch_statement_p;
7316 unsigned char in_statement;
7317
7318 /* Add the condition. */
7319 finish_switch_cond (condition, statement);
7320
7321 /* Parse the body of the switch-statement. */
7322 in_switch_statement_p = parser->in_switch_statement_p;
7323 in_statement = parser->in_statement;
7324 parser->in_switch_statement_p = true;
7325 parser->in_statement |= IN_SWITCH_STMT;
7326 cp_parser_implicitly_scoped_statement (parser, NULL);
7327 parser->in_switch_statement_p = in_switch_statement_p;
7328 parser->in_statement = in_statement;
7329
7330 /* Now we're all done with the switch-statement. */
7331 finish_switch_stmt (statement);
7332 }
7333
7334 return statement;
7335 }
7336 break;
7337
7338 default:
7339 cp_parser_error (parser, "expected selection-statement");
7340 return error_mark_node;
7341 }
7342 }
7343
7344 /* Parse a condition.
7345
7346 condition:
7347 expression
7348 type-specifier-seq declarator = initializer-clause
7349 type-specifier-seq declarator braced-init-list
7350
7351 GNU Extension:
7352
7353 condition:
7354 type-specifier-seq declarator asm-specification [opt]
7355 attributes [opt] = assignment-expression
7356
7357 Returns the expression that should be tested. */
7358
7359 static tree
7360 cp_parser_condition (cp_parser* parser)
7361 {
7362 cp_decl_specifier_seq type_specifiers;
7363 const char *saved_message;
7364
7365 /* Try the declaration first. */
7366 cp_parser_parse_tentatively (parser);
7367 /* New types are not allowed in the type-specifier-seq for a
7368 condition. */
7369 saved_message = parser->type_definition_forbidden_message;
7370 parser->type_definition_forbidden_message
7371 = "types may not be defined in conditions";
7372 /* Parse the type-specifier-seq. */
7373 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7374 &type_specifiers);
7375 /* Restore the saved message. */
7376 parser->type_definition_forbidden_message = saved_message;
7377 /* If all is well, we might be looking at a declaration. */
7378 if (!cp_parser_error_occurred (parser))
7379 {
7380 tree decl;
7381 tree asm_specification;
7382 tree attributes;
7383 cp_declarator *declarator;
7384 tree initializer = NULL_TREE;
7385
7386 /* Parse the declarator. */
7387 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7388 /*ctor_dtor_or_conv_p=*/NULL,
7389 /*parenthesized_p=*/NULL,
7390 /*member_p=*/false);
7391 /* Parse the attributes. */
7392 attributes = cp_parser_attributes_opt (parser);
7393 /* Parse the asm-specification. */
7394 asm_specification = cp_parser_asm_specification_opt (parser);
7395 /* If the next token is not an `=' or '{', then we might still be
7396 looking at an expression. For example:
7397
7398 if (A(a).x)
7399
7400 looks like a decl-specifier-seq and a declarator -- but then
7401 there is no `=', so this is an expression. */
7402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7403 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7404 cp_parser_simulate_error (parser);
7405
7406 /* If we did see an `=' or '{', then we are looking at a declaration
7407 for sure. */
7408 if (cp_parser_parse_definitely (parser))
7409 {
7410 tree pushed_scope;
7411 bool non_constant_p;
7412 bool flags = LOOKUP_ONLYCONVERTING;
7413
7414 /* Create the declaration. */
7415 decl = start_decl (declarator, &type_specifiers,
7416 /*initialized_p=*/true,
7417 attributes, /*prefix_attributes=*/NULL_TREE,
7418 &pushed_scope);
7419
7420 /* Parse the initializer. */
7421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7422 {
7423 initializer = cp_parser_braced_list (parser, &non_constant_p);
7424 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7425 flags = 0;
7426 }
7427 else
7428 {
7429 /* Consume the `='. */
7430 cp_parser_require (parser, CPP_EQ, "%<=%>");
7431 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7432 }
7433 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7434 maybe_warn_cpp0x ("extended initializer lists");
7435
7436 if (!non_constant_p)
7437 initializer = fold_non_dependent_expr (initializer);
7438
7439 /* Process the initializer. */
7440 cp_finish_decl (decl,
7441 initializer, !non_constant_p,
7442 asm_specification,
7443 flags);
7444
7445 if (pushed_scope)
7446 pop_scope (pushed_scope);
7447
7448 return convert_from_reference (decl);
7449 }
7450 }
7451 /* If we didn't even get past the declarator successfully, we are
7452 definitely not looking at a declaration. */
7453 else
7454 cp_parser_abort_tentative_parse (parser);
7455
7456 /* Otherwise, we are looking at an expression. */
7457 return cp_parser_expression (parser, /*cast_p=*/false);
7458 }
7459
7460 /* Parse an iteration-statement.
7461
7462 iteration-statement:
7463 while ( condition ) statement
7464 do statement while ( expression ) ;
7465 for ( for-init-statement condition [opt] ; expression [opt] )
7466 statement
7467
7468 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7469
7470 static tree
7471 cp_parser_iteration_statement (cp_parser* parser)
7472 {
7473 cp_token *token;
7474 enum rid keyword;
7475 tree statement;
7476 unsigned char in_statement;
7477
7478 /* Peek at the next token. */
7479 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7480 if (!token)
7481 return error_mark_node;
7482
7483 /* Remember whether or not we are already within an iteration
7484 statement. */
7485 in_statement = parser->in_statement;
7486
7487 /* See what kind of keyword it is. */
7488 keyword = token->keyword;
7489 switch (keyword)
7490 {
7491 case RID_WHILE:
7492 {
7493 tree condition;
7494
7495 /* Begin the while-statement. */
7496 statement = begin_while_stmt ();
7497 /* Look for the `('. */
7498 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7499 /* Parse the condition. */
7500 condition = cp_parser_condition (parser);
7501 finish_while_stmt_cond (condition, statement);
7502 /* Look for the `)'. */
7503 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7504 /* Parse the dependent statement. */
7505 parser->in_statement = IN_ITERATION_STMT;
7506 cp_parser_already_scoped_statement (parser);
7507 parser->in_statement = in_statement;
7508 /* We're done with the while-statement. */
7509 finish_while_stmt (statement);
7510 }
7511 break;
7512
7513 case RID_DO:
7514 {
7515 tree expression;
7516
7517 /* Begin the do-statement. */
7518 statement = begin_do_stmt ();
7519 /* Parse the body of the do-statement. */
7520 parser->in_statement = IN_ITERATION_STMT;
7521 cp_parser_implicitly_scoped_statement (parser, NULL);
7522 parser->in_statement = in_statement;
7523 finish_do_body (statement);
7524 /* Look for the `while' keyword. */
7525 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7526 /* Look for the `('. */
7527 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7528 /* Parse the expression. */
7529 expression = cp_parser_expression (parser, /*cast_p=*/false);
7530 /* We're done with the do-statement. */
7531 finish_do_stmt (expression, statement);
7532 /* Look for the `)'. */
7533 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7534 /* Look for the `;'. */
7535 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7536 }
7537 break;
7538
7539 case RID_FOR:
7540 {
7541 tree condition = NULL_TREE;
7542 tree expression = NULL_TREE;
7543
7544 /* Begin the for-statement. */
7545 statement = begin_for_stmt ();
7546 /* Look for the `('. */
7547 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7548 /* Parse the initialization. */
7549 cp_parser_for_init_statement (parser);
7550 finish_for_init_stmt (statement);
7551
7552 /* If there's a condition, process it. */
7553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7554 condition = cp_parser_condition (parser);
7555 finish_for_cond (condition, statement);
7556 /* Look for the `;'. */
7557 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7558
7559 /* If there's an expression, process it. */
7560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7561 expression = cp_parser_expression (parser, /*cast_p=*/false);
7562 finish_for_expr (expression, statement);
7563 /* Look for the `)'. */
7564 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7565
7566 /* Parse the body of the for-statement. */
7567 parser->in_statement = IN_ITERATION_STMT;
7568 cp_parser_already_scoped_statement (parser);
7569 parser->in_statement = in_statement;
7570
7571 /* We're done with the for-statement. */
7572 finish_for_stmt (statement);
7573 }
7574 break;
7575
7576 default:
7577 cp_parser_error (parser, "expected iteration-statement");
7578 statement = error_mark_node;
7579 break;
7580 }
7581
7582 return statement;
7583 }
7584
7585 /* Parse a for-init-statement.
7586
7587 for-init-statement:
7588 expression-statement
7589 simple-declaration */
7590
7591 static void
7592 cp_parser_for_init_statement (cp_parser* parser)
7593 {
7594 /* If the next token is a `;', then we have an empty
7595 expression-statement. Grammatically, this is also a
7596 simple-declaration, but an invalid one, because it does not
7597 declare anything. Therefore, if we did not handle this case
7598 specially, we would issue an error message about an invalid
7599 declaration. */
7600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7601 {
7602 /* We're going to speculatively look for a declaration, falling back
7603 to an expression, if necessary. */
7604 cp_parser_parse_tentatively (parser);
7605 /* Parse the declaration. */
7606 cp_parser_simple_declaration (parser,
7607 /*function_definition_allowed_p=*/false);
7608 /* If the tentative parse failed, then we shall need to look for an
7609 expression-statement. */
7610 if (cp_parser_parse_definitely (parser))
7611 return;
7612 }
7613
7614 cp_parser_expression_statement (parser, false);
7615 }
7616
7617 /* Parse a jump-statement.
7618
7619 jump-statement:
7620 break ;
7621 continue ;
7622 return expression [opt] ;
7623 return braced-init-list ;
7624 goto identifier ;
7625
7626 GNU extension:
7627
7628 jump-statement:
7629 goto * expression ;
7630
7631 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7632
7633 static tree
7634 cp_parser_jump_statement (cp_parser* parser)
7635 {
7636 tree statement = error_mark_node;
7637 cp_token *token;
7638 enum rid keyword;
7639 unsigned char in_statement;
7640
7641 /* Peek at the next token. */
7642 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7643 if (!token)
7644 return error_mark_node;
7645
7646 /* See what kind of keyword it is. */
7647 keyword = token->keyword;
7648 switch (keyword)
7649 {
7650 case RID_BREAK:
7651 in_statement = parser->in_statement & ~IN_IF_STMT;
7652 switch (in_statement)
7653 {
7654 case 0:
7655 error ("%Hbreak statement not within loop or switch", &token->location);
7656 break;
7657 default:
7658 gcc_assert ((in_statement & IN_SWITCH_STMT)
7659 || in_statement == IN_ITERATION_STMT);
7660 statement = finish_break_stmt ();
7661 break;
7662 case IN_OMP_BLOCK:
7663 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7664 break;
7665 case IN_OMP_FOR:
7666 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7667 break;
7668 }
7669 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7670 break;
7671
7672 case RID_CONTINUE:
7673 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7674 {
7675 case 0:
7676 error ("%Hcontinue statement not within a loop", &token->location);
7677 break;
7678 case IN_ITERATION_STMT:
7679 case IN_OMP_FOR:
7680 statement = finish_continue_stmt ();
7681 break;
7682 case IN_OMP_BLOCK:
7683 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7684 break;
7685 default:
7686 gcc_unreachable ();
7687 }
7688 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7689 break;
7690
7691 case RID_RETURN:
7692 {
7693 tree expr;
7694 bool expr_non_constant_p;
7695
7696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7697 {
7698 maybe_warn_cpp0x ("extended initializer lists");
7699 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7700 }
7701 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7702 expr = cp_parser_expression (parser, /*cast_p=*/false);
7703 else
7704 /* If the next token is a `;', then there is no
7705 expression. */
7706 expr = NULL_TREE;
7707 /* Build the return-statement. */
7708 statement = finish_return_stmt (expr);
7709 /* Look for the final `;'. */
7710 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7711 }
7712 break;
7713
7714 case RID_GOTO:
7715 /* Create the goto-statement. */
7716 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7717 {
7718 /* Issue a warning about this use of a GNU extension. */
7719 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7720 /* Consume the '*' token. */
7721 cp_lexer_consume_token (parser->lexer);
7722 /* Parse the dependent expression. */
7723 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7724 }
7725 else
7726 finish_goto_stmt (cp_parser_identifier (parser));
7727 /* Look for the final `;'. */
7728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7729 break;
7730
7731 default:
7732 cp_parser_error (parser, "expected jump-statement");
7733 break;
7734 }
7735
7736 return statement;
7737 }
7738
7739 /* Parse a declaration-statement.
7740
7741 declaration-statement:
7742 block-declaration */
7743
7744 static void
7745 cp_parser_declaration_statement (cp_parser* parser)
7746 {
7747 void *p;
7748
7749 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7750 p = obstack_alloc (&declarator_obstack, 0);
7751
7752 /* Parse the block-declaration. */
7753 cp_parser_block_declaration (parser, /*statement_p=*/true);
7754
7755 /* Free any declarators allocated. */
7756 obstack_free (&declarator_obstack, p);
7757
7758 /* Finish off the statement. */
7759 finish_stmt ();
7760 }
7761
7762 /* Some dependent statements (like `if (cond) statement'), are
7763 implicitly in their own scope. In other words, if the statement is
7764 a single statement (as opposed to a compound-statement), it is
7765 none-the-less treated as if it were enclosed in braces. Any
7766 declarations appearing in the dependent statement are out of scope
7767 after control passes that point. This function parses a statement,
7768 but ensures that is in its own scope, even if it is not a
7769 compound-statement.
7770
7771 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7772 is a (possibly labeled) if statement which is not enclosed in
7773 braces and has an else clause. This is used to implement
7774 -Wparentheses.
7775
7776 Returns the new statement. */
7777
7778 static tree
7779 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7780 {
7781 tree statement;
7782
7783 if (if_p != NULL)
7784 *if_p = false;
7785
7786 /* Mark if () ; with a special NOP_EXPR. */
7787 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7788 {
7789 cp_lexer_consume_token (parser->lexer);
7790 statement = add_stmt (build_empty_stmt ());
7791 }
7792 /* if a compound is opened, we simply parse the statement directly. */
7793 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7794 statement = cp_parser_compound_statement (parser, NULL, false);
7795 /* If the token is not a `{', then we must take special action. */
7796 else
7797 {
7798 /* Create a compound-statement. */
7799 statement = begin_compound_stmt (0);
7800 /* Parse the dependent-statement. */
7801 cp_parser_statement (parser, NULL_TREE, false, if_p);
7802 /* Finish the dummy compound-statement. */
7803 finish_compound_stmt (statement);
7804 }
7805
7806 /* Return the statement. */
7807 return statement;
7808 }
7809
7810 /* For some dependent statements (like `while (cond) statement'), we
7811 have already created a scope. Therefore, even if the dependent
7812 statement is a compound-statement, we do not want to create another
7813 scope. */
7814
7815 static void
7816 cp_parser_already_scoped_statement (cp_parser* parser)
7817 {
7818 /* If the token is a `{', then we must take special action. */
7819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7820 cp_parser_statement (parser, NULL_TREE, false, NULL);
7821 else
7822 {
7823 /* Avoid calling cp_parser_compound_statement, so that we
7824 don't create a new scope. Do everything else by hand. */
7825 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7826 cp_parser_statement_seq_opt (parser, NULL_TREE);
7827 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7828 }
7829 }
7830
7831 /* Declarations [gram.dcl.dcl] */
7832
7833 /* Parse an optional declaration-sequence.
7834
7835 declaration-seq:
7836 declaration
7837 declaration-seq declaration */
7838
7839 static void
7840 cp_parser_declaration_seq_opt (cp_parser* parser)
7841 {
7842 while (true)
7843 {
7844 cp_token *token;
7845
7846 token = cp_lexer_peek_token (parser->lexer);
7847
7848 if (token->type == CPP_CLOSE_BRACE
7849 || token->type == CPP_EOF
7850 || token->type == CPP_PRAGMA_EOL)
7851 break;
7852
7853 if (token->type == CPP_SEMICOLON)
7854 {
7855 /* A declaration consisting of a single semicolon is
7856 invalid. Allow it unless we're being pedantic. */
7857 cp_lexer_consume_token (parser->lexer);
7858 if (!in_system_header)
7859 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7860 continue;
7861 }
7862
7863 /* If we're entering or exiting a region that's implicitly
7864 extern "C", modify the lang context appropriately. */
7865 if (!parser->implicit_extern_c && token->implicit_extern_c)
7866 {
7867 push_lang_context (lang_name_c);
7868 parser->implicit_extern_c = true;
7869 }
7870 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7871 {
7872 pop_lang_context ();
7873 parser->implicit_extern_c = false;
7874 }
7875
7876 if (token->type == CPP_PRAGMA)
7877 {
7878 /* A top-level declaration can consist solely of a #pragma.
7879 A nested declaration cannot, so this is done here and not
7880 in cp_parser_declaration. (A #pragma at block scope is
7881 handled in cp_parser_statement.) */
7882 cp_parser_pragma (parser, pragma_external);
7883 continue;
7884 }
7885
7886 /* Parse the declaration itself. */
7887 cp_parser_declaration (parser);
7888 }
7889 }
7890
7891 /* Parse a declaration.
7892
7893 declaration:
7894 block-declaration
7895 function-definition
7896 template-declaration
7897 explicit-instantiation
7898 explicit-specialization
7899 linkage-specification
7900 namespace-definition
7901
7902 GNU extension:
7903
7904 declaration:
7905 __extension__ declaration */
7906
7907 static void
7908 cp_parser_declaration (cp_parser* parser)
7909 {
7910 cp_token token1;
7911 cp_token token2;
7912 int saved_pedantic;
7913 void *p;
7914
7915 /* Check for the `__extension__' keyword. */
7916 if (cp_parser_extension_opt (parser, &saved_pedantic))
7917 {
7918 /* Parse the qualified declaration. */
7919 cp_parser_declaration (parser);
7920 /* Restore the PEDANTIC flag. */
7921 pedantic = saved_pedantic;
7922
7923 return;
7924 }
7925
7926 /* Try to figure out what kind of declaration is present. */
7927 token1 = *cp_lexer_peek_token (parser->lexer);
7928
7929 if (token1.type != CPP_EOF)
7930 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7931 else
7932 {
7933 token2.type = CPP_EOF;
7934 token2.keyword = RID_MAX;
7935 }
7936
7937 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7938 p = obstack_alloc (&declarator_obstack, 0);
7939
7940 /* If the next token is `extern' and the following token is a string
7941 literal, then we have a linkage specification. */
7942 if (token1.keyword == RID_EXTERN
7943 && cp_parser_is_string_literal (&token2))
7944 cp_parser_linkage_specification (parser);
7945 /* If the next token is `template', then we have either a template
7946 declaration, an explicit instantiation, or an explicit
7947 specialization. */
7948 else if (token1.keyword == RID_TEMPLATE)
7949 {
7950 /* `template <>' indicates a template specialization. */
7951 if (token2.type == CPP_LESS
7952 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7953 cp_parser_explicit_specialization (parser);
7954 /* `template <' indicates a template declaration. */
7955 else if (token2.type == CPP_LESS)
7956 cp_parser_template_declaration (parser, /*member_p=*/false);
7957 /* Anything else must be an explicit instantiation. */
7958 else
7959 cp_parser_explicit_instantiation (parser);
7960 }
7961 /* If the next token is `export', then we have a template
7962 declaration. */
7963 else if (token1.keyword == RID_EXPORT)
7964 cp_parser_template_declaration (parser, /*member_p=*/false);
7965 /* If the next token is `extern', 'static' or 'inline' and the one
7966 after that is `template', we have a GNU extended explicit
7967 instantiation directive. */
7968 else if (cp_parser_allow_gnu_extensions_p (parser)
7969 && (token1.keyword == RID_EXTERN
7970 || token1.keyword == RID_STATIC
7971 || token1.keyword == RID_INLINE)
7972 && token2.keyword == RID_TEMPLATE)
7973 cp_parser_explicit_instantiation (parser);
7974 /* If the next token is `namespace', check for a named or unnamed
7975 namespace definition. */
7976 else if (token1.keyword == RID_NAMESPACE
7977 && (/* A named namespace definition. */
7978 (token2.type == CPP_NAME
7979 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7980 != CPP_EQ))
7981 /* An unnamed namespace definition. */
7982 || token2.type == CPP_OPEN_BRACE
7983 || token2.keyword == RID_ATTRIBUTE))
7984 cp_parser_namespace_definition (parser);
7985 /* An inline (associated) namespace definition. */
7986 else if (token1.keyword == RID_INLINE
7987 && token2.keyword == RID_NAMESPACE)
7988 cp_parser_namespace_definition (parser);
7989 /* Objective-C++ declaration/definition. */
7990 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7991 cp_parser_objc_declaration (parser);
7992 /* We must have either a block declaration or a function
7993 definition. */
7994 else
7995 /* Try to parse a block-declaration, or a function-definition. */
7996 cp_parser_block_declaration (parser, /*statement_p=*/false);
7997
7998 /* Free any declarators allocated. */
7999 obstack_free (&declarator_obstack, p);
8000 }
8001
8002 /* Parse a block-declaration.
8003
8004 block-declaration:
8005 simple-declaration
8006 asm-definition
8007 namespace-alias-definition
8008 using-declaration
8009 using-directive
8010
8011 GNU Extension:
8012
8013 block-declaration:
8014 __extension__ block-declaration
8015
8016 C++0x Extension:
8017
8018 block-declaration:
8019 static_assert-declaration
8020
8021 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8022 part of a declaration-statement. */
8023
8024 static void
8025 cp_parser_block_declaration (cp_parser *parser,
8026 bool statement_p)
8027 {
8028 cp_token *token1;
8029 int saved_pedantic;
8030
8031 /* Check for the `__extension__' keyword. */
8032 if (cp_parser_extension_opt (parser, &saved_pedantic))
8033 {
8034 /* Parse the qualified declaration. */
8035 cp_parser_block_declaration (parser, statement_p);
8036 /* Restore the PEDANTIC flag. */
8037 pedantic = saved_pedantic;
8038
8039 return;
8040 }
8041
8042 /* Peek at the next token to figure out which kind of declaration is
8043 present. */
8044 token1 = cp_lexer_peek_token (parser->lexer);
8045
8046 /* If the next keyword is `asm', we have an asm-definition. */
8047 if (token1->keyword == RID_ASM)
8048 {
8049 if (statement_p)
8050 cp_parser_commit_to_tentative_parse (parser);
8051 cp_parser_asm_definition (parser);
8052 }
8053 /* If the next keyword is `namespace', we have a
8054 namespace-alias-definition. */
8055 else if (token1->keyword == RID_NAMESPACE)
8056 cp_parser_namespace_alias_definition (parser);
8057 /* If the next keyword is `using', we have either a
8058 using-declaration or a using-directive. */
8059 else if (token1->keyword == RID_USING)
8060 {
8061 cp_token *token2;
8062
8063 if (statement_p)
8064 cp_parser_commit_to_tentative_parse (parser);
8065 /* If the token after `using' is `namespace', then we have a
8066 using-directive. */
8067 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8068 if (token2->keyword == RID_NAMESPACE)
8069 cp_parser_using_directive (parser);
8070 /* Otherwise, it's a using-declaration. */
8071 else
8072 cp_parser_using_declaration (parser,
8073 /*access_declaration_p=*/false);
8074 }
8075 /* If the next keyword is `__label__' we have a misplaced label
8076 declaration. */
8077 else if (token1->keyword == RID_LABEL)
8078 {
8079 cp_lexer_consume_token (parser->lexer);
8080 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8081 cp_parser_skip_to_end_of_statement (parser);
8082 /* If the next token is now a `;', consume it. */
8083 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8084 cp_lexer_consume_token (parser->lexer);
8085 }
8086 /* If the next token is `static_assert' we have a static assertion. */
8087 else if (token1->keyword == RID_STATIC_ASSERT)
8088 cp_parser_static_assert (parser, /*member_p=*/false);
8089 /* Anything else must be a simple-declaration. */
8090 else
8091 cp_parser_simple_declaration (parser, !statement_p);
8092 }
8093
8094 /* Parse a simple-declaration.
8095
8096 simple-declaration:
8097 decl-specifier-seq [opt] init-declarator-list [opt] ;
8098
8099 init-declarator-list:
8100 init-declarator
8101 init-declarator-list , init-declarator
8102
8103 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8104 function-definition as a simple-declaration. */
8105
8106 static void
8107 cp_parser_simple_declaration (cp_parser* parser,
8108 bool function_definition_allowed_p)
8109 {
8110 cp_decl_specifier_seq decl_specifiers;
8111 int declares_class_or_enum;
8112 bool saw_declarator;
8113
8114 /* Defer access checks until we know what is being declared; the
8115 checks for names appearing in the decl-specifier-seq should be
8116 done as if we were in the scope of the thing being declared. */
8117 push_deferring_access_checks (dk_deferred);
8118
8119 /* Parse the decl-specifier-seq. We have to keep track of whether
8120 or not the decl-specifier-seq declares a named class or
8121 enumeration type, since that is the only case in which the
8122 init-declarator-list is allowed to be empty.
8123
8124 [dcl.dcl]
8125
8126 In a simple-declaration, the optional init-declarator-list can be
8127 omitted only when declaring a class or enumeration, that is when
8128 the decl-specifier-seq contains either a class-specifier, an
8129 elaborated-type-specifier, or an enum-specifier. */
8130 cp_parser_decl_specifier_seq (parser,
8131 CP_PARSER_FLAGS_OPTIONAL,
8132 &decl_specifiers,
8133 &declares_class_or_enum);
8134 /* We no longer need to defer access checks. */
8135 stop_deferring_access_checks ();
8136
8137 /* In a block scope, a valid declaration must always have a
8138 decl-specifier-seq. By not trying to parse declarators, we can
8139 resolve the declaration/expression ambiguity more quickly. */
8140 if (!function_definition_allowed_p
8141 && !decl_specifiers.any_specifiers_p)
8142 {
8143 cp_parser_error (parser, "expected declaration");
8144 goto done;
8145 }
8146
8147 /* If the next two tokens are both identifiers, the code is
8148 erroneous. The usual cause of this situation is code like:
8149
8150 T t;
8151
8152 where "T" should name a type -- but does not. */
8153 if (!decl_specifiers.type
8154 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8155 {
8156 /* If parsing tentatively, we should commit; we really are
8157 looking at a declaration. */
8158 cp_parser_commit_to_tentative_parse (parser);
8159 /* Give up. */
8160 goto done;
8161 }
8162
8163 /* If we have seen at least one decl-specifier, and the next token
8164 is not a parenthesis, then we must be looking at a declaration.
8165 (After "int (" we might be looking at a functional cast.) */
8166 if (decl_specifiers.any_specifiers_p
8167 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8168 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8169 && !cp_parser_error_occurred (parser))
8170 cp_parser_commit_to_tentative_parse (parser);
8171
8172 /* Keep going until we hit the `;' at the end of the simple
8173 declaration. */
8174 saw_declarator = false;
8175 while (cp_lexer_next_token_is_not (parser->lexer,
8176 CPP_SEMICOLON))
8177 {
8178 cp_token *token;
8179 bool function_definition_p;
8180 tree decl;
8181
8182 if (saw_declarator)
8183 {
8184 /* If we are processing next declarator, coma is expected */
8185 token = cp_lexer_peek_token (parser->lexer);
8186 gcc_assert (token->type == CPP_COMMA);
8187 cp_lexer_consume_token (parser->lexer);
8188 }
8189 else
8190 saw_declarator = true;
8191
8192 /* Parse the init-declarator. */
8193 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8194 /*checks=*/NULL,
8195 function_definition_allowed_p,
8196 /*member_p=*/false,
8197 declares_class_or_enum,
8198 &function_definition_p);
8199 /* If an error occurred while parsing tentatively, exit quickly.
8200 (That usually happens when in the body of a function; each
8201 statement is treated as a declaration-statement until proven
8202 otherwise.) */
8203 if (cp_parser_error_occurred (parser))
8204 goto done;
8205 /* Handle function definitions specially. */
8206 if (function_definition_p)
8207 {
8208 /* If the next token is a `,', then we are probably
8209 processing something like:
8210
8211 void f() {}, *p;
8212
8213 which is erroneous. */
8214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8215 {
8216 cp_token *token = cp_lexer_peek_token (parser->lexer);
8217 error ("%Hmixing declarations and function-definitions is forbidden",
8218 &token->location);
8219 }
8220 /* Otherwise, we're done with the list of declarators. */
8221 else
8222 {
8223 pop_deferring_access_checks ();
8224 return;
8225 }
8226 }
8227 /* The next token should be either a `,' or a `;'. */
8228 token = cp_lexer_peek_token (parser->lexer);
8229 /* If it's a `,', there are more declarators to come. */
8230 if (token->type == CPP_COMMA)
8231 /* will be consumed next time around */;
8232 /* If it's a `;', we are done. */
8233 else if (token->type == CPP_SEMICOLON)
8234 break;
8235 /* Anything else is an error. */
8236 else
8237 {
8238 /* If we have already issued an error message we don't need
8239 to issue another one. */
8240 if (decl != error_mark_node
8241 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8242 cp_parser_error (parser, "expected %<,%> or %<;%>");
8243 /* Skip tokens until we reach the end of the statement. */
8244 cp_parser_skip_to_end_of_statement (parser);
8245 /* If the next token is now a `;', consume it. */
8246 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8247 cp_lexer_consume_token (parser->lexer);
8248 goto done;
8249 }
8250 /* After the first time around, a function-definition is not
8251 allowed -- even if it was OK at first. For example:
8252
8253 int i, f() {}
8254
8255 is not valid. */
8256 function_definition_allowed_p = false;
8257 }
8258
8259 /* Issue an error message if no declarators are present, and the
8260 decl-specifier-seq does not itself declare a class or
8261 enumeration. */
8262 if (!saw_declarator)
8263 {
8264 if (cp_parser_declares_only_class_p (parser))
8265 shadow_tag (&decl_specifiers);
8266 /* Perform any deferred access checks. */
8267 perform_deferred_access_checks ();
8268 }
8269
8270 /* Consume the `;'. */
8271 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8272
8273 done:
8274 pop_deferring_access_checks ();
8275 }
8276
8277 /* Parse a decl-specifier-seq.
8278
8279 decl-specifier-seq:
8280 decl-specifier-seq [opt] decl-specifier
8281
8282 decl-specifier:
8283 storage-class-specifier
8284 type-specifier
8285 function-specifier
8286 friend
8287 typedef
8288
8289 GNU Extension:
8290
8291 decl-specifier:
8292 attributes
8293
8294 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8295
8296 The parser flags FLAGS is used to control type-specifier parsing.
8297
8298 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8299 flags:
8300
8301 1: one of the decl-specifiers is an elaborated-type-specifier
8302 (i.e., a type declaration)
8303 2: one of the decl-specifiers is an enum-specifier or a
8304 class-specifier (i.e., a type definition)
8305
8306 */
8307
8308 static void
8309 cp_parser_decl_specifier_seq (cp_parser* parser,
8310 cp_parser_flags flags,
8311 cp_decl_specifier_seq *decl_specs,
8312 int* declares_class_or_enum)
8313 {
8314 bool constructor_possible_p = !parser->in_declarator_p;
8315 cp_token *start_token = NULL;
8316
8317 /* Clear DECL_SPECS. */
8318 clear_decl_specs (decl_specs);
8319
8320 /* Assume no class or enumeration type is declared. */
8321 *declares_class_or_enum = 0;
8322
8323 /* Keep reading specifiers until there are no more to read. */
8324 while (true)
8325 {
8326 bool constructor_p;
8327 bool found_decl_spec;
8328 cp_token *token;
8329
8330 /* Peek at the next token. */
8331 token = cp_lexer_peek_token (parser->lexer);
8332
8333 /* Save the first token of the decl spec list for error
8334 reporting. */
8335 if (!start_token)
8336 start_token = token;
8337 /* Handle attributes. */
8338 if (token->keyword == RID_ATTRIBUTE)
8339 {
8340 /* Parse the attributes. */
8341 decl_specs->attributes
8342 = chainon (decl_specs->attributes,
8343 cp_parser_attributes_opt (parser));
8344 continue;
8345 }
8346 /* Assume we will find a decl-specifier keyword. */
8347 found_decl_spec = true;
8348 /* If the next token is an appropriate keyword, we can simply
8349 add it to the list. */
8350 switch (token->keyword)
8351 {
8352 /* decl-specifier:
8353 friend */
8354 case RID_FRIEND:
8355 if (!at_class_scope_p ())
8356 {
8357 error ("%H%<friend%> used outside of class", &token->location);
8358 cp_lexer_purge_token (parser->lexer);
8359 }
8360 else
8361 {
8362 ++decl_specs->specs[(int) ds_friend];
8363 /* Consume the token. */
8364 cp_lexer_consume_token (parser->lexer);
8365 }
8366 break;
8367
8368 /* function-specifier:
8369 inline
8370 virtual
8371 explicit */
8372 case RID_INLINE:
8373 case RID_VIRTUAL:
8374 case RID_EXPLICIT:
8375 cp_parser_function_specifier_opt (parser, decl_specs);
8376 break;
8377
8378 /* decl-specifier:
8379 typedef */
8380 case RID_TYPEDEF:
8381 ++decl_specs->specs[(int) ds_typedef];
8382 /* Consume the token. */
8383 cp_lexer_consume_token (parser->lexer);
8384 /* A constructor declarator cannot appear in a typedef. */
8385 constructor_possible_p = false;
8386 /* The "typedef" keyword can only occur in a declaration; we
8387 may as well commit at this point. */
8388 cp_parser_commit_to_tentative_parse (parser);
8389
8390 if (decl_specs->storage_class != sc_none)
8391 decl_specs->conflicting_specifiers_p = true;
8392 break;
8393
8394 /* storage-class-specifier:
8395 auto
8396 register
8397 static
8398 extern
8399 mutable
8400
8401 GNU Extension:
8402 thread */
8403 case RID_AUTO:
8404 if (cxx_dialect == cxx98)
8405 {
8406 /* Consume the token. */
8407 cp_lexer_consume_token (parser->lexer);
8408
8409 /* Complain about `auto' as a storage specifier, if
8410 we're complaining about C++0x compatibility. */
8411 warning
8412 (OPT_Wc__0x_compat,
8413 "%H%<auto%> will change meaning in C++0x; please remove it",
8414 &token->location);
8415
8416 /* Set the storage class anyway. */
8417 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8418 token->location);
8419 }
8420 else
8421 /* C++0x auto type-specifier. */
8422 found_decl_spec = false;
8423 break;
8424
8425 case RID_REGISTER:
8426 case RID_STATIC:
8427 case RID_EXTERN:
8428 case RID_MUTABLE:
8429 /* Consume the token. */
8430 cp_lexer_consume_token (parser->lexer);
8431 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8432 token->location);
8433 break;
8434 case RID_THREAD:
8435 /* Consume the token. */
8436 cp_lexer_consume_token (parser->lexer);
8437 ++decl_specs->specs[(int) ds_thread];
8438 break;
8439
8440 default:
8441 /* We did not yet find a decl-specifier yet. */
8442 found_decl_spec = false;
8443 break;
8444 }
8445
8446 /* Constructors are a special case. The `S' in `S()' is not a
8447 decl-specifier; it is the beginning of the declarator. */
8448 constructor_p
8449 = (!found_decl_spec
8450 && constructor_possible_p
8451 && (cp_parser_constructor_declarator_p
8452 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8453
8454 /* If we don't have a DECL_SPEC yet, then we must be looking at
8455 a type-specifier. */
8456 if (!found_decl_spec && !constructor_p)
8457 {
8458 int decl_spec_declares_class_or_enum;
8459 bool is_cv_qualifier;
8460 tree type_spec;
8461
8462 type_spec
8463 = cp_parser_type_specifier (parser, flags,
8464 decl_specs,
8465 /*is_declaration=*/true,
8466 &decl_spec_declares_class_or_enum,
8467 &is_cv_qualifier);
8468 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8469
8470 /* If this type-specifier referenced a user-defined type
8471 (a typedef, class-name, etc.), then we can't allow any
8472 more such type-specifiers henceforth.
8473
8474 [dcl.spec]
8475
8476 The longest sequence of decl-specifiers that could
8477 possibly be a type name is taken as the
8478 decl-specifier-seq of a declaration. The sequence shall
8479 be self-consistent as described below.
8480
8481 [dcl.type]
8482
8483 As a general rule, at most one type-specifier is allowed
8484 in the complete decl-specifier-seq of a declaration. The
8485 only exceptions are the following:
8486
8487 -- const or volatile can be combined with any other
8488 type-specifier.
8489
8490 -- signed or unsigned can be combined with char, long,
8491 short, or int.
8492
8493 -- ..
8494
8495 Example:
8496
8497 typedef char* Pc;
8498 void g (const int Pc);
8499
8500 Here, Pc is *not* part of the decl-specifier seq; it's
8501 the declarator. Therefore, once we see a type-specifier
8502 (other than a cv-qualifier), we forbid any additional
8503 user-defined types. We *do* still allow things like `int
8504 int' to be considered a decl-specifier-seq, and issue the
8505 error message later. */
8506 if (type_spec && !is_cv_qualifier)
8507 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8508 /* A constructor declarator cannot follow a type-specifier. */
8509 if (type_spec)
8510 {
8511 constructor_possible_p = false;
8512 found_decl_spec = true;
8513 }
8514 }
8515
8516 /* If we still do not have a DECL_SPEC, then there are no more
8517 decl-specifiers. */
8518 if (!found_decl_spec)
8519 break;
8520
8521 decl_specs->any_specifiers_p = true;
8522 /* After we see one decl-specifier, further decl-specifiers are
8523 always optional. */
8524 flags |= CP_PARSER_FLAGS_OPTIONAL;
8525 }
8526
8527 cp_parser_check_decl_spec (decl_specs, start_token->location);
8528
8529 /* Don't allow a friend specifier with a class definition. */
8530 if (decl_specs->specs[(int) ds_friend] != 0
8531 && (*declares_class_or_enum & 2))
8532 error ("%Hclass definition may not be declared a friend",
8533 &start_token->location);
8534 }
8535
8536 /* Parse an (optional) storage-class-specifier.
8537
8538 storage-class-specifier:
8539 auto
8540 register
8541 static
8542 extern
8543 mutable
8544
8545 GNU Extension:
8546
8547 storage-class-specifier:
8548 thread
8549
8550 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8551
8552 static tree
8553 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8554 {
8555 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8556 {
8557 case RID_AUTO:
8558 if (cxx_dialect != cxx98)
8559 return NULL_TREE;
8560 /* Fall through for C++98. */
8561
8562 case RID_REGISTER:
8563 case RID_STATIC:
8564 case RID_EXTERN:
8565 case RID_MUTABLE:
8566 case RID_THREAD:
8567 /* Consume the token. */
8568 return cp_lexer_consume_token (parser->lexer)->u.value;
8569
8570 default:
8571 return NULL_TREE;
8572 }
8573 }
8574
8575 /* Parse an (optional) function-specifier.
8576
8577 function-specifier:
8578 inline
8579 virtual
8580 explicit
8581
8582 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8583 Updates DECL_SPECS, if it is non-NULL. */
8584
8585 static tree
8586 cp_parser_function_specifier_opt (cp_parser* parser,
8587 cp_decl_specifier_seq *decl_specs)
8588 {
8589 cp_token *token = cp_lexer_peek_token (parser->lexer);
8590 switch (token->keyword)
8591 {
8592 case RID_INLINE:
8593 if (decl_specs)
8594 ++decl_specs->specs[(int) ds_inline];
8595 break;
8596
8597 case RID_VIRTUAL:
8598 /* 14.5.2.3 [temp.mem]
8599
8600 A member function template shall not be virtual. */
8601 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8602 error ("%Htemplates may not be %<virtual%>", &token->location);
8603 else if (decl_specs)
8604 ++decl_specs->specs[(int) ds_virtual];
8605 break;
8606
8607 case RID_EXPLICIT:
8608 if (decl_specs)
8609 ++decl_specs->specs[(int) ds_explicit];
8610 break;
8611
8612 default:
8613 return NULL_TREE;
8614 }
8615
8616 /* Consume the token. */
8617 return cp_lexer_consume_token (parser->lexer)->u.value;
8618 }
8619
8620 /* Parse a linkage-specification.
8621
8622 linkage-specification:
8623 extern string-literal { declaration-seq [opt] }
8624 extern string-literal declaration */
8625
8626 static void
8627 cp_parser_linkage_specification (cp_parser* parser)
8628 {
8629 tree linkage;
8630
8631 /* Look for the `extern' keyword. */
8632 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8633
8634 /* Look for the string-literal. */
8635 linkage = cp_parser_string_literal (parser, false, false);
8636
8637 /* Transform the literal into an identifier. If the literal is a
8638 wide-character string, or contains embedded NULs, then we can't
8639 handle it as the user wants. */
8640 if (strlen (TREE_STRING_POINTER (linkage))
8641 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8642 {
8643 cp_parser_error (parser, "invalid linkage-specification");
8644 /* Assume C++ linkage. */
8645 linkage = lang_name_cplusplus;
8646 }
8647 else
8648 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8649
8650 /* We're now using the new linkage. */
8651 push_lang_context (linkage);
8652
8653 /* If the next token is a `{', then we're using the first
8654 production. */
8655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8656 {
8657 /* Consume the `{' token. */
8658 cp_lexer_consume_token (parser->lexer);
8659 /* Parse the declarations. */
8660 cp_parser_declaration_seq_opt (parser);
8661 /* Look for the closing `}'. */
8662 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8663 }
8664 /* Otherwise, there's just one declaration. */
8665 else
8666 {
8667 bool saved_in_unbraced_linkage_specification_p;
8668
8669 saved_in_unbraced_linkage_specification_p
8670 = parser->in_unbraced_linkage_specification_p;
8671 parser->in_unbraced_linkage_specification_p = true;
8672 cp_parser_declaration (parser);
8673 parser->in_unbraced_linkage_specification_p
8674 = saved_in_unbraced_linkage_specification_p;
8675 }
8676
8677 /* We're done with the linkage-specification. */
8678 pop_lang_context ();
8679 }
8680
8681 /* Parse a static_assert-declaration.
8682
8683 static_assert-declaration:
8684 static_assert ( constant-expression , string-literal ) ;
8685
8686 If MEMBER_P, this static_assert is a class member. */
8687
8688 static void
8689 cp_parser_static_assert(cp_parser *parser, bool member_p)
8690 {
8691 tree condition;
8692 tree message;
8693 cp_token *token;
8694 location_t saved_loc;
8695
8696 /* Peek at the `static_assert' token so we can keep track of exactly
8697 where the static assertion started. */
8698 token = cp_lexer_peek_token (parser->lexer);
8699 saved_loc = token->location;
8700
8701 /* Look for the `static_assert' keyword. */
8702 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8703 "%<static_assert%>"))
8704 return;
8705
8706 /* We know we are in a static assertion; commit to any tentative
8707 parse. */
8708 if (cp_parser_parsing_tentatively (parser))
8709 cp_parser_commit_to_tentative_parse (parser);
8710
8711 /* Parse the `(' starting the static assertion condition. */
8712 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8713
8714 /* Parse the constant-expression. */
8715 condition =
8716 cp_parser_constant_expression (parser,
8717 /*allow_non_constant_p=*/false,
8718 /*non_constant_p=*/NULL);
8719
8720 /* Parse the separating `,'. */
8721 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8722
8723 /* Parse the string-literal message. */
8724 message = cp_parser_string_literal (parser,
8725 /*translate=*/false,
8726 /*wide_ok=*/true);
8727
8728 /* A `)' completes the static assertion. */
8729 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8730 cp_parser_skip_to_closing_parenthesis (parser,
8731 /*recovering=*/true,
8732 /*or_comma=*/false,
8733 /*consume_paren=*/true);
8734
8735 /* A semicolon terminates the declaration. */
8736 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8737
8738 /* Complete the static assertion, which may mean either processing
8739 the static assert now or saving it for template instantiation. */
8740 finish_static_assert (condition, message, saved_loc, member_p);
8741 }
8742
8743 /* Parse a `decltype' type. Returns the type.
8744
8745 simple-type-specifier:
8746 decltype ( expression ) */
8747
8748 static tree
8749 cp_parser_decltype (cp_parser *parser)
8750 {
8751 tree expr;
8752 bool id_expression_or_member_access_p = false;
8753 const char *saved_message;
8754 bool saved_integral_constant_expression_p;
8755 bool saved_non_integral_constant_expression_p;
8756 cp_token *id_expr_start_token;
8757
8758 /* Look for the `decltype' token. */
8759 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8760 return error_mark_node;
8761
8762 /* Types cannot be defined in a `decltype' expression. Save away the
8763 old message. */
8764 saved_message = parser->type_definition_forbidden_message;
8765
8766 /* And create the new one. */
8767 parser->type_definition_forbidden_message
8768 = "types may not be defined in %<decltype%> expressions";
8769
8770 /* The restrictions on constant-expressions do not apply inside
8771 decltype expressions. */
8772 saved_integral_constant_expression_p
8773 = parser->integral_constant_expression_p;
8774 saved_non_integral_constant_expression_p
8775 = parser->non_integral_constant_expression_p;
8776 parser->integral_constant_expression_p = false;
8777
8778 /* Do not actually evaluate the expression. */
8779 ++skip_evaluation;
8780
8781 /* Parse the opening `('. */
8782 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8783 return error_mark_node;
8784
8785 /* First, try parsing an id-expression. */
8786 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8787 cp_parser_parse_tentatively (parser);
8788 expr = cp_parser_id_expression (parser,
8789 /*template_keyword_p=*/false,
8790 /*check_dependency_p=*/true,
8791 /*template_p=*/NULL,
8792 /*declarator_p=*/false,
8793 /*optional_p=*/false);
8794
8795 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8796 {
8797 bool non_integral_constant_expression_p = false;
8798 tree id_expression = expr;
8799 cp_id_kind idk;
8800 const char *error_msg;
8801
8802 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8803 /* Lookup the name we got back from the id-expression. */
8804 expr = cp_parser_lookup_name (parser, expr,
8805 none_type,
8806 /*is_template=*/false,
8807 /*is_namespace=*/false,
8808 /*check_dependency=*/true,
8809 /*ambiguous_decls=*/NULL,
8810 id_expr_start_token->location);
8811
8812 if (expr
8813 && expr != error_mark_node
8814 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8815 && TREE_CODE (expr) != TYPE_DECL
8816 && (TREE_CODE (expr) != BIT_NOT_EXPR
8817 || !TYPE_P (TREE_OPERAND (expr, 0)))
8818 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8819 {
8820 /* Complete lookup of the id-expression. */
8821 expr = (finish_id_expression
8822 (id_expression, expr, parser->scope, &idk,
8823 /*integral_constant_expression_p=*/false,
8824 /*allow_non_integral_constant_expression_p=*/true,
8825 &non_integral_constant_expression_p,
8826 /*template_p=*/false,
8827 /*done=*/true,
8828 /*address_p=*/false,
8829 /*template_arg_p=*/false,
8830 &error_msg,
8831 id_expr_start_token->location));
8832
8833 if (expr == error_mark_node)
8834 /* We found an id-expression, but it was something that we
8835 should not have found. This is an error, not something
8836 we can recover from, so note that we found an
8837 id-expression and we'll recover as gracefully as
8838 possible. */
8839 id_expression_or_member_access_p = true;
8840 }
8841
8842 if (expr
8843 && expr != error_mark_node
8844 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8845 /* We have an id-expression. */
8846 id_expression_or_member_access_p = true;
8847 }
8848
8849 if (!id_expression_or_member_access_p)
8850 {
8851 /* Abort the id-expression parse. */
8852 cp_parser_abort_tentative_parse (parser);
8853
8854 /* Parsing tentatively, again. */
8855 cp_parser_parse_tentatively (parser);
8856
8857 /* Parse a class member access. */
8858 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8859 /*cast_p=*/false,
8860 /*member_access_only_p=*/true);
8861
8862 if (expr
8863 && expr != error_mark_node
8864 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8865 /* We have an id-expression. */
8866 id_expression_or_member_access_p = true;
8867 }
8868
8869 if (id_expression_or_member_access_p)
8870 /* We have parsed the complete id-expression or member access. */
8871 cp_parser_parse_definitely (parser);
8872 else
8873 {
8874 /* Abort our attempt to parse an id-expression or member access
8875 expression. */
8876 cp_parser_abort_tentative_parse (parser);
8877
8878 /* Parse a full expression. */
8879 expr = cp_parser_expression (parser, /*cast_p=*/false);
8880 }
8881
8882 /* Go back to evaluating expressions. */
8883 --skip_evaluation;
8884
8885 /* Restore the old message and the integral constant expression
8886 flags. */
8887 parser->type_definition_forbidden_message = saved_message;
8888 parser->integral_constant_expression_p
8889 = saved_integral_constant_expression_p;
8890 parser->non_integral_constant_expression_p
8891 = saved_non_integral_constant_expression_p;
8892
8893 if (expr == error_mark_node)
8894 {
8895 /* Skip everything up to the closing `)'. */
8896 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8897 /*consume_paren=*/true);
8898 return error_mark_node;
8899 }
8900
8901 /* Parse to the closing `)'. */
8902 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8903 {
8904 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8905 /*consume_paren=*/true);
8906 return error_mark_node;
8907 }
8908
8909 return finish_decltype_type (expr, id_expression_or_member_access_p);
8910 }
8911
8912 /* Special member functions [gram.special] */
8913
8914 /* Parse a conversion-function-id.
8915
8916 conversion-function-id:
8917 operator conversion-type-id
8918
8919 Returns an IDENTIFIER_NODE representing the operator. */
8920
8921 static tree
8922 cp_parser_conversion_function_id (cp_parser* parser)
8923 {
8924 tree type;
8925 tree saved_scope;
8926 tree saved_qualifying_scope;
8927 tree saved_object_scope;
8928 tree pushed_scope = NULL_TREE;
8929
8930 /* Look for the `operator' token. */
8931 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8932 return error_mark_node;
8933 /* When we parse the conversion-type-id, the current scope will be
8934 reset. However, we need that information in able to look up the
8935 conversion function later, so we save it here. */
8936 saved_scope = parser->scope;
8937 saved_qualifying_scope = parser->qualifying_scope;
8938 saved_object_scope = parser->object_scope;
8939 /* We must enter the scope of the class so that the names of
8940 entities declared within the class are available in the
8941 conversion-type-id. For example, consider:
8942
8943 struct S {
8944 typedef int I;
8945 operator I();
8946 };
8947
8948 S::operator I() { ... }
8949
8950 In order to see that `I' is a type-name in the definition, we
8951 must be in the scope of `S'. */
8952 if (saved_scope)
8953 pushed_scope = push_scope (saved_scope);
8954 /* Parse the conversion-type-id. */
8955 type = cp_parser_conversion_type_id (parser);
8956 /* Leave the scope of the class, if any. */
8957 if (pushed_scope)
8958 pop_scope (pushed_scope);
8959 /* Restore the saved scope. */
8960 parser->scope = saved_scope;
8961 parser->qualifying_scope = saved_qualifying_scope;
8962 parser->object_scope = saved_object_scope;
8963 /* If the TYPE is invalid, indicate failure. */
8964 if (type == error_mark_node)
8965 return error_mark_node;
8966 return mangle_conv_op_name_for_type (type);
8967 }
8968
8969 /* Parse a conversion-type-id:
8970
8971 conversion-type-id:
8972 type-specifier-seq conversion-declarator [opt]
8973
8974 Returns the TYPE specified. */
8975
8976 static tree
8977 cp_parser_conversion_type_id (cp_parser* parser)
8978 {
8979 tree attributes;
8980 cp_decl_specifier_seq type_specifiers;
8981 cp_declarator *declarator;
8982 tree type_specified;
8983
8984 /* Parse the attributes. */
8985 attributes = cp_parser_attributes_opt (parser);
8986 /* Parse the type-specifiers. */
8987 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8988 &type_specifiers);
8989 /* If that didn't work, stop. */
8990 if (type_specifiers.type == error_mark_node)
8991 return error_mark_node;
8992 /* Parse the conversion-declarator. */
8993 declarator = cp_parser_conversion_declarator_opt (parser);
8994
8995 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8996 /*initialized=*/0, &attributes);
8997 if (attributes)
8998 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8999
9000 /* Don't give this error when parsing tentatively. This happens to
9001 work because we always parse this definitively once. */
9002 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9003 && type_uses_auto (type_specified))
9004 {
9005 error ("invalid use of %<auto%> in conversion operator");
9006 return error_mark_node;
9007 }
9008
9009 return type_specified;
9010 }
9011
9012 /* Parse an (optional) conversion-declarator.
9013
9014 conversion-declarator:
9015 ptr-operator conversion-declarator [opt]
9016
9017 */
9018
9019 static cp_declarator *
9020 cp_parser_conversion_declarator_opt (cp_parser* parser)
9021 {
9022 enum tree_code code;
9023 tree class_type;
9024 cp_cv_quals cv_quals;
9025
9026 /* We don't know if there's a ptr-operator next, or not. */
9027 cp_parser_parse_tentatively (parser);
9028 /* Try the ptr-operator. */
9029 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9030 /* If it worked, look for more conversion-declarators. */
9031 if (cp_parser_parse_definitely (parser))
9032 {
9033 cp_declarator *declarator;
9034
9035 /* Parse another optional declarator. */
9036 declarator = cp_parser_conversion_declarator_opt (parser);
9037
9038 return cp_parser_make_indirect_declarator
9039 (code, class_type, cv_quals, declarator);
9040 }
9041
9042 return NULL;
9043 }
9044
9045 /* Parse an (optional) ctor-initializer.
9046
9047 ctor-initializer:
9048 : mem-initializer-list
9049
9050 Returns TRUE iff the ctor-initializer was actually present. */
9051
9052 static bool
9053 cp_parser_ctor_initializer_opt (cp_parser* parser)
9054 {
9055 /* If the next token is not a `:', then there is no
9056 ctor-initializer. */
9057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9058 {
9059 /* Do default initialization of any bases and members. */
9060 if (DECL_CONSTRUCTOR_P (current_function_decl))
9061 finish_mem_initializers (NULL_TREE);
9062
9063 return false;
9064 }
9065
9066 /* Consume the `:' token. */
9067 cp_lexer_consume_token (parser->lexer);
9068 /* And the mem-initializer-list. */
9069 cp_parser_mem_initializer_list (parser);
9070
9071 return true;
9072 }
9073
9074 /* Parse a mem-initializer-list.
9075
9076 mem-initializer-list:
9077 mem-initializer ... [opt]
9078 mem-initializer ... [opt] , mem-initializer-list */
9079
9080 static void
9081 cp_parser_mem_initializer_list (cp_parser* parser)
9082 {
9083 tree mem_initializer_list = NULL_TREE;
9084 cp_token *token = cp_lexer_peek_token (parser->lexer);
9085
9086 /* Let the semantic analysis code know that we are starting the
9087 mem-initializer-list. */
9088 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9089 error ("%Honly constructors take base initializers",
9090 &token->location);
9091
9092 /* Loop through the list. */
9093 while (true)
9094 {
9095 tree mem_initializer;
9096
9097 token = cp_lexer_peek_token (parser->lexer);
9098 /* Parse the mem-initializer. */
9099 mem_initializer = cp_parser_mem_initializer (parser);
9100 /* If the next token is a `...', we're expanding member initializers. */
9101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9102 {
9103 /* Consume the `...'. */
9104 cp_lexer_consume_token (parser->lexer);
9105
9106 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9107 can be expanded but members cannot. */
9108 if (mem_initializer != error_mark_node
9109 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9110 {
9111 error ("%Hcannot expand initializer for member %<%D%>",
9112 &token->location, TREE_PURPOSE (mem_initializer));
9113 mem_initializer = error_mark_node;
9114 }
9115
9116 /* Construct the pack expansion type. */
9117 if (mem_initializer != error_mark_node)
9118 mem_initializer = make_pack_expansion (mem_initializer);
9119 }
9120 /* Add it to the list, unless it was erroneous. */
9121 if (mem_initializer != error_mark_node)
9122 {
9123 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9124 mem_initializer_list = mem_initializer;
9125 }
9126 /* If the next token is not a `,', we're done. */
9127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9128 break;
9129 /* Consume the `,' token. */
9130 cp_lexer_consume_token (parser->lexer);
9131 }
9132
9133 /* Perform semantic analysis. */
9134 if (DECL_CONSTRUCTOR_P (current_function_decl))
9135 finish_mem_initializers (mem_initializer_list);
9136 }
9137
9138 /* Parse a mem-initializer.
9139
9140 mem-initializer:
9141 mem-initializer-id ( expression-list [opt] )
9142 mem-initializer-id braced-init-list
9143
9144 GNU extension:
9145
9146 mem-initializer:
9147 ( expression-list [opt] )
9148
9149 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9150 class) or FIELD_DECL (for a non-static data member) to initialize;
9151 the TREE_VALUE is the expression-list. An empty initialization
9152 list is represented by void_list_node. */
9153
9154 static tree
9155 cp_parser_mem_initializer (cp_parser* parser)
9156 {
9157 tree mem_initializer_id;
9158 tree expression_list;
9159 tree member;
9160 cp_token *token = cp_lexer_peek_token (parser->lexer);
9161
9162 /* Find out what is being initialized. */
9163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9164 {
9165 permerror (token->location,
9166 "anachronistic old-style base class initializer");
9167 mem_initializer_id = NULL_TREE;
9168 }
9169 else
9170 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9171 member = expand_member_init (mem_initializer_id);
9172 if (member && !DECL_P (member))
9173 in_base_initializer = 1;
9174
9175 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9176 {
9177 bool expr_non_constant_p;
9178 maybe_warn_cpp0x ("extended initializer lists");
9179 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9180 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9181 expression_list = build_tree_list (NULL_TREE, expression_list);
9182 }
9183 else
9184 expression_list
9185 = cp_parser_parenthesized_expression_list (parser, false,
9186 /*cast_p=*/false,
9187 /*allow_expansion_p=*/true,
9188 /*non_constant_p=*/NULL);
9189 if (expression_list == error_mark_node)
9190 return error_mark_node;
9191 if (!expression_list)
9192 expression_list = void_type_node;
9193
9194 in_base_initializer = 0;
9195
9196 return member ? build_tree_list (member, expression_list) : error_mark_node;
9197 }
9198
9199 /* Parse a mem-initializer-id.
9200
9201 mem-initializer-id:
9202 :: [opt] nested-name-specifier [opt] class-name
9203 identifier
9204
9205 Returns a TYPE indicating the class to be initializer for the first
9206 production. Returns an IDENTIFIER_NODE indicating the data member
9207 to be initialized for the second production. */
9208
9209 static tree
9210 cp_parser_mem_initializer_id (cp_parser* parser)
9211 {
9212 bool global_scope_p;
9213 bool nested_name_specifier_p;
9214 bool template_p = false;
9215 tree id;
9216
9217 cp_token *token = cp_lexer_peek_token (parser->lexer);
9218
9219 /* `typename' is not allowed in this context ([temp.res]). */
9220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9221 {
9222 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9223 "member initializer is implicitly a type)",
9224 &token->location);
9225 cp_lexer_consume_token (parser->lexer);
9226 }
9227 /* Look for the optional `::' operator. */
9228 global_scope_p
9229 = (cp_parser_global_scope_opt (parser,
9230 /*current_scope_valid_p=*/false)
9231 != NULL_TREE);
9232 /* Look for the optional nested-name-specifier. The simplest way to
9233 implement:
9234
9235 [temp.res]
9236
9237 The keyword `typename' is not permitted in a base-specifier or
9238 mem-initializer; in these contexts a qualified name that
9239 depends on a template-parameter is implicitly assumed to be a
9240 type name.
9241
9242 is to assume that we have seen the `typename' keyword at this
9243 point. */
9244 nested_name_specifier_p
9245 = (cp_parser_nested_name_specifier_opt (parser,
9246 /*typename_keyword_p=*/true,
9247 /*check_dependency_p=*/true,
9248 /*type_p=*/true,
9249 /*is_declaration=*/true)
9250 != NULL_TREE);
9251 if (nested_name_specifier_p)
9252 template_p = cp_parser_optional_template_keyword (parser);
9253 /* If there is a `::' operator or a nested-name-specifier, then we
9254 are definitely looking for a class-name. */
9255 if (global_scope_p || nested_name_specifier_p)
9256 return cp_parser_class_name (parser,
9257 /*typename_keyword_p=*/true,
9258 /*template_keyword_p=*/template_p,
9259 none_type,
9260 /*check_dependency_p=*/true,
9261 /*class_head_p=*/false,
9262 /*is_declaration=*/true);
9263 /* Otherwise, we could also be looking for an ordinary identifier. */
9264 cp_parser_parse_tentatively (parser);
9265 /* Try a class-name. */
9266 id = cp_parser_class_name (parser,
9267 /*typename_keyword_p=*/true,
9268 /*template_keyword_p=*/false,
9269 none_type,
9270 /*check_dependency_p=*/true,
9271 /*class_head_p=*/false,
9272 /*is_declaration=*/true);
9273 /* If we found one, we're done. */
9274 if (cp_parser_parse_definitely (parser))
9275 return id;
9276 /* Otherwise, look for an ordinary identifier. */
9277 return cp_parser_identifier (parser);
9278 }
9279
9280 /* Overloading [gram.over] */
9281
9282 /* Parse an operator-function-id.
9283
9284 operator-function-id:
9285 operator operator
9286
9287 Returns an IDENTIFIER_NODE for the operator which is a
9288 human-readable spelling of the identifier, e.g., `operator +'. */
9289
9290 static tree
9291 cp_parser_operator_function_id (cp_parser* parser)
9292 {
9293 /* Look for the `operator' keyword. */
9294 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9295 return error_mark_node;
9296 /* And then the name of the operator itself. */
9297 return cp_parser_operator (parser);
9298 }
9299
9300 /* Parse an operator.
9301
9302 operator:
9303 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9304 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9305 || ++ -- , ->* -> () []
9306
9307 GNU Extensions:
9308
9309 operator:
9310 <? >? <?= >?=
9311
9312 Returns an IDENTIFIER_NODE for the operator which is a
9313 human-readable spelling of the identifier, e.g., `operator +'. */
9314
9315 static tree
9316 cp_parser_operator (cp_parser* parser)
9317 {
9318 tree id = NULL_TREE;
9319 cp_token *token;
9320
9321 /* Peek at the next token. */
9322 token = cp_lexer_peek_token (parser->lexer);
9323 /* Figure out which operator we have. */
9324 switch (token->type)
9325 {
9326 case CPP_KEYWORD:
9327 {
9328 enum tree_code op;
9329
9330 /* The keyword should be either `new' or `delete'. */
9331 if (token->keyword == RID_NEW)
9332 op = NEW_EXPR;
9333 else if (token->keyword == RID_DELETE)
9334 op = DELETE_EXPR;
9335 else
9336 break;
9337
9338 /* Consume the `new' or `delete' token. */
9339 cp_lexer_consume_token (parser->lexer);
9340
9341 /* Peek at the next token. */
9342 token = cp_lexer_peek_token (parser->lexer);
9343 /* If it's a `[' token then this is the array variant of the
9344 operator. */
9345 if (token->type == CPP_OPEN_SQUARE)
9346 {
9347 /* Consume the `[' token. */
9348 cp_lexer_consume_token (parser->lexer);
9349 /* Look for the `]' token. */
9350 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9351 id = ansi_opname (op == NEW_EXPR
9352 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9353 }
9354 /* Otherwise, we have the non-array variant. */
9355 else
9356 id = ansi_opname (op);
9357
9358 return id;
9359 }
9360
9361 case CPP_PLUS:
9362 id = ansi_opname (PLUS_EXPR);
9363 break;
9364
9365 case CPP_MINUS:
9366 id = ansi_opname (MINUS_EXPR);
9367 break;
9368
9369 case CPP_MULT:
9370 id = ansi_opname (MULT_EXPR);
9371 break;
9372
9373 case CPP_DIV:
9374 id = ansi_opname (TRUNC_DIV_EXPR);
9375 break;
9376
9377 case CPP_MOD:
9378 id = ansi_opname (TRUNC_MOD_EXPR);
9379 break;
9380
9381 case CPP_XOR:
9382 id = ansi_opname (BIT_XOR_EXPR);
9383 break;
9384
9385 case CPP_AND:
9386 id = ansi_opname (BIT_AND_EXPR);
9387 break;
9388
9389 case CPP_OR:
9390 id = ansi_opname (BIT_IOR_EXPR);
9391 break;
9392
9393 case CPP_COMPL:
9394 id = ansi_opname (BIT_NOT_EXPR);
9395 break;
9396
9397 case CPP_NOT:
9398 id = ansi_opname (TRUTH_NOT_EXPR);
9399 break;
9400
9401 case CPP_EQ:
9402 id = ansi_assopname (NOP_EXPR);
9403 break;
9404
9405 case CPP_LESS:
9406 id = ansi_opname (LT_EXPR);
9407 break;
9408
9409 case CPP_GREATER:
9410 id = ansi_opname (GT_EXPR);
9411 break;
9412
9413 case CPP_PLUS_EQ:
9414 id = ansi_assopname (PLUS_EXPR);
9415 break;
9416
9417 case CPP_MINUS_EQ:
9418 id = ansi_assopname (MINUS_EXPR);
9419 break;
9420
9421 case CPP_MULT_EQ:
9422 id = ansi_assopname (MULT_EXPR);
9423 break;
9424
9425 case CPP_DIV_EQ:
9426 id = ansi_assopname (TRUNC_DIV_EXPR);
9427 break;
9428
9429 case CPP_MOD_EQ:
9430 id = ansi_assopname (TRUNC_MOD_EXPR);
9431 break;
9432
9433 case CPP_XOR_EQ:
9434 id = ansi_assopname (BIT_XOR_EXPR);
9435 break;
9436
9437 case CPP_AND_EQ:
9438 id = ansi_assopname (BIT_AND_EXPR);
9439 break;
9440
9441 case CPP_OR_EQ:
9442 id = ansi_assopname (BIT_IOR_EXPR);
9443 break;
9444
9445 case CPP_LSHIFT:
9446 id = ansi_opname (LSHIFT_EXPR);
9447 break;
9448
9449 case CPP_RSHIFT:
9450 id = ansi_opname (RSHIFT_EXPR);
9451 break;
9452
9453 case CPP_LSHIFT_EQ:
9454 id = ansi_assopname (LSHIFT_EXPR);
9455 break;
9456
9457 case CPP_RSHIFT_EQ:
9458 id = ansi_assopname (RSHIFT_EXPR);
9459 break;
9460
9461 case CPP_EQ_EQ:
9462 id = ansi_opname (EQ_EXPR);
9463 break;
9464
9465 case CPP_NOT_EQ:
9466 id = ansi_opname (NE_EXPR);
9467 break;
9468
9469 case CPP_LESS_EQ:
9470 id = ansi_opname (LE_EXPR);
9471 break;
9472
9473 case CPP_GREATER_EQ:
9474 id = ansi_opname (GE_EXPR);
9475 break;
9476
9477 case CPP_AND_AND:
9478 id = ansi_opname (TRUTH_ANDIF_EXPR);
9479 break;
9480
9481 case CPP_OR_OR:
9482 id = ansi_opname (TRUTH_ORIF_EXPR);
9483 break;
9484
9485 case CPP_PLUS_PLUS:
9486 id = ansi_opname (POSTINCREMENT_EXPR);
9487 break;
9488
9489 case CPP_MINUS_MINUS:
9490 id = ansi_opname (PREDECREMENT_EXPR);
9491 break;
9492
9493 case CPP_COMMA:
9494 id = ansi_opname (COMPOUND_EXPR);
9495 break;
9496
9497 case CPP_DEREF_STAR:
9498 id = ansi_opname (MEMBER_REF);
9499 break;
9500
9501 case CPP_DEREF:
9502 id = ansi_opname (COMPONENT_REF);
9503 break;
9504
9505 case CPP_OPEN_PAREN:
9506 /* Consume the `('. */
9507 cp_lexer_consume_token (parser->lexer);
9508 /* Look for the matching `)'. */
9509 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9510 return ansi_opname (CALL_EXPR);
9511
9512 case CPP_OPEN_SQUARE:
9513 /* Consume the `['. */
9514 cp_lexer_consume_token (parser->lexer);
9515 /* Look for the matching `]'. */
9516 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9517 return ansi_opname (ARRAY_REF);
9518
9519 default:
9520 /* Anything else is an error. */
9521 break;
9522 }
9523
9524 /* If we have selected an identifier, we need to consume the
9525 operator token. */
9526 if (id)
9527 cp_lexer_consume_token (parser->lexer);
9528 /* Otherwise, no valid operator name was present. */
9529 else
9530 {
9531 cp_parser_error (parser, "expected operator");
9532 id = error_mark_node;
9533 }
9534
9535 return id;
9536 }
9537
9538 /* Parse a template-declaration.
9539
9540 template-declaration:
9541 export [opt] template < template-parameter-list > declaration
9542
9543 If MEMBER_P is TRUE, this template-declaration occurs within a
9544 class-specifier.
9545
9546 The grammar rule given by the standard isn't correct. What
9547 is really meant is:
9548
9549 template-declaration:
9550 export [opt] template-parameter-list-seq
9551 decl-specifier-seq [opt] init-declarator [opt] ;
9552 export [opt] template-parameter-list-seq
9553 function-definition
9554
9555 template-parameter-list-seq:
9556 template-parameter-list-seq [opt]
9557 template < template-parameter-list > */
9558
9559 static void
9560 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9561 {
9562 /* Check for `export'. */
9563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9564 {
9565 /* Consume the `export' token. */
9566 cp_lexer_consume_token (parser->lexer);
9567 /* Warn that we do not support `export'. */
9568 warning (0, "keyword %<export%> not implemented, and will be ignored");
9569 }
9570
9571 cp_parser_template_declaration_after_export (parser, member_p);
9572 }
9573
9574 /* Parse a template-parameter-list.
9575
9576 template-parameter-list:
9577 template-parameter
9578 template-parameter-list , template-parameter
9579
9580 Returns a TREE_LIST. Each node represents a template parameter.
9581 The nodes are connected via their TREE_CHAINs. */
9582
9583 static tree
9584 cp_parser_template_parameter_list (cp_parser* parser)
9585 {
9586 tree parameter_list = NULL_TREE;
9587
9588 begin_template_parm_list ();
9589 while (true)
9590 {
9591 tree parameter;
9592 bool is_non_type;
9593 bool is_parameter_pack;
9594
9595 /* Parse the template-parameter. */
9596 parameter = cp_parser_template_parameter (parser,
9597 &is_non_type,
9598 &is_parameter_pack);
9599 /* Add it to the list. */
9600 if (parameter != error_mark_node)
9601 parameter_list = process_template_parm (parameter_list,
9602 parameter,
9603 is_non_type,
9604 is_parameter_pack);
9605 else
9606 {
9607 tree err_parm = build_tree_list (parameter, parameter);
9608 TREE_VALUE (err_parm) = error_mark_node;
9609 parameter_list = chainon (parameter_list, err_parm);
9610 }
9611
9612 /* If the next token is not a `,', we're done. */
9613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9614 break;
9615 /* Otherwise, consume the `,' token. */
9616 cp_lexer_consume_token (parser->lexer);
9617 }
9618
9619 return end_template_parm_list (parameter_list);
9620 }
9621
9622 /* Parse a template-parameter.
9623
9624 template-parameter:
9625 type-parameter
9626 parameter-declaration
9627
9628 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9629 the parameter. The TREE_PURPOSE is the default value, if any.
9630 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9631 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9632 set to true iff this parameter is a parameter pack. */
9633
9634 static tree
9635 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9636 bool *is_parameter_pack)
9637 {
9638 cp_token *token;
9639 cp_parameter_declarator *parameter_declarator;
9640 cp_declarator *id_declarator;
9641 tree parm;
9642
9643 /* Assume it is a type parameter or a template parameter. */
9644 *is_non_type = false;
9645 /* Assume it not a parameter pack. */
9646 *is_parameter_pack = false;
9647 /* Peek at the next token. */
9648 token = cp_lexer_peek_token (parser->lexer);
9649 /* If it is `class' or `template', we have a type-parameter. */
9650 if (token->keyword == RID_TEMPLATE)
9651 return cp_parser_type_parameter (parser, is_parameter_pack);
9652 /* If it is `class' or `typename' we do not know yet whether it is a
9653 type parameter or a non-type parameter. Consider:
9654
9655 template <typename T, typename T::X X> ...
9656
9657 or:
9658
9659 template <class C, class D*> ...
9660
9661 Here, the first parameter is a type parameter, and the second is
9662 a non-type parameter. We can tell by looking at the token after
9663 the identifier -- if it is a `,', `=', or `>' then we have a type
9664 parameter. */
9665 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9666 {
9667 /* Peek at the token after `class' or `typename'. */
9668 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9669 /* If it's an ellipsis, we have a template type parameter
9670 pack. */
9671 if (token->type == CPP_ELLIPSIS)
9672 return cp_parser_type_parameter (parser, is_parameter_pack);
9673 /* If it's an identifier, skip it. */
9674 if (token->type == CPP_NAME)
9675 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9676 /* Now, see if the token looks like the end of a template
9677 parameter. */
9678 if (token->type == CPP_COMMA
9679 || token->type == CPP_EQ
9680 || token->type == CPP_GREATER)
9681 return cp_parser_type_parameter (parser, is_parameter_pack);
9682 }
9683
9684 /* Otherwise, it is a non-type parameter.
9685
9686 [temp.param]
9687
9688 When parsing a default template-argument for a non-type
9689 template-parameter, the first non-nested `>' is taken as the end
9690 of the template parameter-list rather than a greater-than
9691 operator. */
9692 *is_non_type = true;
9693 parameter_declarator
9694 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9695 /*parenthesized_p=*/NULL);
9696
9697 /* If the parameter declaration is marked as a parameter pack, set
9698 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9699 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9700 grokdeclarator. */
9701 if (parameter_declarator
9702 && parameter_declarator->declarator
9703 && parameter_declarator->declarator->parameter_pack_p)
9704 {
9705 *is_parameter_pack = true;
9706 parameter_declarator->declarator->parameter_pack_p = false;
9707 }
9708
9709 /* If the next token is an ellipsis, and we don't already have it
9710 marked as a parameter pack, then we have a parameter pack (that
9711 has no declarator). */
9712 if (!*is_parameter_pack
9713 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9714 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9715 {
9716 /* Consume the `...'. */
9717 cp_lexer_consume_token (parser->lexer);
9718 maybe_warn_variadic_templates ();
9719
9720 *is_parameter_pack = true;
9721 }
9722 /* We might end up with a pack expansion as the type of the non-type
9723 template parameter, in which case this is a non-type template
9724 parameter pack. */
9725 else if (parameter_declarator
9726 && parameter_declarator->decl_specifiers.type
9727 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9728 {
9729 *is_parameter_pack = true;
9730 parameter_declarator->decl_specifiers.type =
9731 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9732 }
9733
9734 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9735 {
9736 /* Parameter packs cannot have default arguments. However, a
9737 user may try to do so, so we'll parse them and give an
9738 appropriate diagnostic here. */
9739
9740 /* Consume the `='. */
9741 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9742 cp_lexer_consume_token (parser->lexer);
9743
9744 /* Find the name of the parameter pack. */
9745 id_declarator = parameter_declarator->declarator;
9746 while (id_declarator && id_declarator->kind != cdk_id)
9747 id_declarator = id_declarator->declarator;
9748
9749 if (id_declarator && id_declarator->kind == cdk_id)
9750 error ("%Htemplate parameter pack %qD cannot have a default argument",
9751 &start_token->location, id_declarator->u.id.unqualified_name);
9752 else
9753 error ("%Htemplate parameter pack cannot have a default argument",
9754 &start_token->location);
9755
9756 /* Parse the default argument, but throw away the result. */
9757 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9758 }
9759
9760 parm = grokdeclarator (parameter_declarator->declarator,
9761 &parameter_declarator->decl_specifiers,
9762 PARM, /*initialized=*/0,
9763 /*attrlist=*/NULL);
9764 if (parm == error_mark_node)
9765 return error_mark_node;
9766
9767 return build_tree_list (parameter_declarator->default_argument, parm);
9768 }
9769
9770 /* Parse a type-parameter.
9771
9772 type-parameter:
9773 class identifier [opt]
9774 class identifier [opt] = type-id
9775 typename identifier [opt]
9776 typename identifier [opt] = type-id
9777 template < template-parameter-list > class identifier [opt]
9778 template < template-parameter-list > class identifier [opt]
9779 = id-expression
9780
9781 GNU Extension (variadic templates):
9782
9783 type-parameter:
9784 class ... identifier [opt]
9785 typename ... identifier [opt]
9786
9787 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9788 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9789 the declaration of the parameter.
9790
9791 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9792
9793 static tree
9794 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9795 {
9796 cp_token *token;
9797 tree parameter;
9798
9799 /* Look for a keyword to tell us what kind of parameter this is. */
9800 token = cp_parser_require (parser, CPP_KEYWORD,
9801 "%<class%>, %<typename%>, or %<template%>");
9802 if (!token)
9803 return error_mark_node;
9804
9805 switch (token->keyword)
9806 {
9807 case RID_CLASS:
9808 case RID_TYPENAME:
9809 {
9810 tree identifier;
9811 tree default_argument;
9812
9813 /* If the next token is an ellipsis, we have a template
9814 argument pack. */
9815 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9816 {
9817 /* Consume the `...' token. */
9818 cp_lexer_consume_token (parser->lexer);
9819 maybe_warn_variadic_templates ();
9820
9821 *is_parameter_pack = true;
9822 }
9823
9824 /* If the next token is an identifier, then it names the
9825 parameter. */
9826 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9827 identifier = cp_parser_identifier (parser);
9828 else
9829 identifier = NULL_TREE;
9830
9831 /* Create the parameter. */
9832 parameter = finish_template_type_parm (class_type_node, identifier);
9833
9834 /* If the next token is an `=', we have a default argument. */
9835 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9836 {
9837 /* Consume the `=' token. */
9838 cp_lexer_consume_token (parser->lexer);
9839 /* Parse the default-argument. */
9840 push_deferring_access_checks (dk_no_deferred);
9841 default_argument = cp_parser_type_id (parser);
9842
9843 /* Template parameter packs cannot have default
9844 arguments. */
9845 if (*is_parameter_pack)
9846 {
9847 if (identifier)
9848 error ("%Htemplate parameter pack %qD cannot have a "
9849 "default argument", &token->location, identifier);
9850 else
9851 error ("%Htemplate parameter packs cannot have "
9852 "default arguments", &token->location);
9853 default_argument = NULL_TREE;
9854 }
9855 pop_deferring_access_checks ();
9856 }
9857 else
9858 default_argument = NULL_TREE;
9859
9860 /* Create the combined representation of the parameter and the
9861 default argument. */
9862 parameter = build_tree_list (default_argument, parameter);
9863 }
9864 break;
9865
9866 case RID_TEMPLATE:
9867 {
9868 tree parameter_list;
9869 tree identifier;
9870 tree default_argument;
9871
9872 /* Look for the `<'. */
9873 cp_parser_require (parser, CPP_LESS, "%<<%>");
9874 /* Parse the template-parameter-list. */
9875 parameter_list = cp_parser_template_parameter_list (parser);
9876 /* Look for the `>'. */
9877 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9878 /* Look for the `class' keyword. */
9879 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9880 /* If the next token is an ellipsis, we have a template
9881 argument pack. */
9882 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9883 {
9884 /* Consume the `...' token. */
9885 cp_lexer_consume_token (parser->lexer);
9886 maybe_warn_variadic_templates ();
9887
9888 *is_parameter_pack = true;
9889 }
9890 /* If the next token is an `=', then there is a
9891 default-argument. If the next token is a `>', we are at
9892 the end of the parameter-list. If the next token is a `,',
9893 then we are at the end of this parameter. */
9894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9895 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9896 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9897 {
9898 identifier = cp_parser_identifier (parser);
9899 /* Treat invalid names as if the parameter were nameless. */
9900 if (identifier == error_mark_node)
9901 identifier = NULL_TREE;
9902 }
9903 else
9904 identifier = NULL_TREE;
9905
9906 /* Create the template parameter. */
9907 parameter = finish_template_template_parm (class_type_node,
9908 identifier);
9909
9910 /* If the next token is an `=', then there is a
9911 default-argument. */
9912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9913 {
9914 bool is_template;
9915
9916 /* Consume the `='. */
9917 cp_lexer_consume_token (parser->lexer);
9918 /* Parse the id-expression. */
9919 push_deferring_access_checks (dk_no_deferred);
9920 /* save token before parsing the id-expression, for error
9921 reporting */
9922 token = cp_lexer_peek_token (parser->lexer);
9923 default_argument
9924 = cp_parser_id_expression (parser,
9925 /*template_keyword_p=*/false,
9926 /*check_dependency_p=*/true,
9927 /*template_p=*/&is_template,
9928 /*declarator_p=*/false,
9929 /*optional_p=*/false);
9930 if (TREE_CODE (default_argument) == TYPE_DECL)
9931 /* If the id-expression was a template-id that refers to
9932 a template-class, we already have the declaration here,
9933 so no further lookup is needed. */
9934 ;
9935 else
9936 /* Look up the name. */
9937 default_argument
9938 = cp_parser_lookup_name (parser, default_argument,
9939 none_type,
9940 /*is_template=*/is_template,
9941 /*is_namespace=*/false,
9942 /*check_dependency=*/true,
9943 /*ambiguous_decls=*/NULL,
9944 token->location);
9945 /* See if the default argument is valid. */
9946 default_argument
9947 = check_template_template_default_arg (default_argument);
9948
9949 /* Template parameter packs cannot have default
9950 arguments. */
9951 if (*is_parameter_pack)
9952 {
9953 if (identifier)
9954 error ("%Htemplate parameter pack %qD cannot "
9955 "have a default argument",
9956 &token->location, identifier);
9957 else
9958 error ("%Htemplate parameter packs cannot "
9959 "have default arguments",
9960 &token->location);
9961 default_argument = NULL_TREE;
9962 }
9963 pop_deferring_access_checks ();
9964 }
9965 else
9966 default_argument = NULL_TREE;
9967
9968 /* Create the combined representation of the parameter and the
9969 default argument. */
9970 parameter = build_tree_list (default_argument, parameter);
9971 }
9972 break;
9973
9974 default:
9975 gcc_unreachable ();
9976 break;
9977 }
9978
9979 return parameter;
9980 }
9981
9982 /* Parse a template-id.
9983
9984 template-id:
9985 template-name < template-argument-list [opt] >
9986
9987 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9988 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9989 returned. Otherwise, if the template-name names a function, or set
9990 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9991 names a class, returns a TYPE_DECL for the specialization.
9992
9993 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9994 uninstantiated templates. */
9995
9996 static tree
9997 cp_parser_template_id (cp_parser *parser,
9998 bool template_keyword_p,
9999 bool check_dependency_p,
10000 bool is_declaration)
10001 {
10002 int i;
10003 tree templ;
10004 tree arguments;
10005 tree template_id;
10006 cp_token_position start_of_id = 0;
10007 deferred_access_check *chk;
10008 VEC (deferred_access_check,gc) *access_check;
10009 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10010 bool is_identifier;
10011
10012 /* If the next token corresponds to a template-id, there is no need
10013 to reparse it. */
10014 next_token = cp_lexer_peek_token (parser->lexer);
10015 if (next_token->type == CPP_TEMPLATE_ID)
10016 {
10017 struct tree_check *check_value;
10018
10019 /* Get the stored value. */
10020 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10021 /* Perform any access checks that were deferred. */
10022 access_check = check_value->checks;
10023 if (access_check)
10024 {
10025 for (i = 0 ;
10026 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10027 ++i)
10028 {
10029 perform_or_defer_access_check (chk->binfo,
10030 chk->decl,
10031 chk->diag_decl);
10032 }
10033 }
10034 /* Return the stored value. */
10035 return check_value->value;
10036 }
10037
10038 /* Avoid performing name lookup if there is no possibility of
10039 finding a template-id. */
10040 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10041 || (next_token->type == CPP_NAME
10042 && !cp_parser_nth_token_starts_template_argument_list_p
10043 (parser, 2)))
10044 {
10045 cp_parser_error (parser, "expected template-id");
10046 return error_mark_node;
10047 }
10048
10049 /* Remember where the template-id starts. */
10050 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10051 start_of_id = cp_lexer_token_position (parser->lexer, false);
10052
10053 push_deferring_access_checks (dk_deferred);
10054
10055 /* Parse the template-name. */
10056 is_identifier = false;
10057 token = cp_lexer_peek_token (parser->lexer);
10058 templ = cp_parser_template_name (parser, template_keyword_p,
10059 check_dependency_p,
10060 is_declaration,
10061 &is_identifier);
10062 if (templ == error_mark_node || is_identifier)
10063 {
10064 pop_deferring_access_checks ();
10065 return templ;
10066 }
10067
10068 /* If we find the sequence `[:' after a template-name, it's probably
10069 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10070 parse correctly the argument list. */
10071 next_token = cp_lexer_peek_token (parser->lexer);
10072 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10073 if (next_token->type == CPP_OPEN_SQUARE
10074 && next_token->flags & DIGRAPH
10075 && next_token_2->type == CPP_COLON
10076 && !(next_token_2->flags & PREV_WHITE))
10077 {
10078 cp_parser_parse_tentatively (parser);
10079 /* Change `:' into `::'. */
10080 next_token_2->type = CPP_SCOPE;
10081 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10082 CPP_LESS. */
10083 cp_lexer_consume_token (parser->lexer);
10084
10085 /* Parse the arguments. */
10086 arguments = cp_parser_enclosed_template_argument_list (parser);
10087 if (!cp_parser_parse_definitely (parser))
10088 {
10089 /* If we couldn't parse an argument list, then we revert our changes
10090 and return simply an error. Maybe this is not a template-id
10091 after all. */
10092 next_token_2->type = CPP_COLON;
10093 cp_parser_error (parser, "expected %<<%>");
10094 pop_deferring_access_checks ();
10095 return error_mark_node;
10096 }
10097 /* Otherwise, emit an error about the invalid digraph, but continue
10098 parsing because we got our argument list. */
10099 if (permerror (next_token->location,
10100 "%<<::%> cannot begin a template-argument list"))
10101 {
10102 static bool hint = false;
10103 inform (next_token->location,
10104 "%<<:%> is an alternate spelling for %<[%>."
10105 " Insert whitespace between %<<%> and %<::%>");
10106 if (!hint && !flag_permissive)
10107 {
10108 inform (next_token->location, "(if you use %<-fpermissive%>"
10109 " G++ will accept your code)");
10110 hint = true;
10111 }
10112 }
10113 }
10114 else
10115 {
10116 /* Look for the `<' that starts the template-argument-list. */
10117 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10118 {
10119 pop_deferring_access_checks ();
10120 return error_mark_node;
10121 }
10122 /* Parse the arguments. */
10123 arguments = cp_parser_enclosed_template_argument_list (parser);
10124 }
10125
10126 /* Build a representation of the specialization. */
10127 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10128 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10129 else if (DECL_CLASS_TEMPLATE_P (templ)
10130 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10131 {
10132 bool entering_scope;
10133 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10134 template (rather than some instantiation thereof) only if
10135 is not nested within some other construct. For example, in
10136 "template <typename T> void f(T) { A<T>::", A<T> is just an
10137 instantiation of A. */
10138 entering_scope = (template_parm_scope_p ()
10139 && cp_lexer_next_token_is (parser->lexer,
10140 CPP_SCOPE));
10141 template_id
10142 = finish_template_type (templ, arguments, entering_scope);
10143 }
10144 else
10145 {
10146 /* If it's not a class-template or a template-template, it should be
10147 a function-template. */
10148 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10149 || TREE_CODE (templ) == OVERLOAD
10150 || BASELINK_P (templ)));
10151
10152 template_id = lookup_template_function (templ, arguments);
10153 }
10154
10155 /* If parsing tentatively, replace the sequence of tokens that makes
10156 up the template-id with a CPP_TEMPLATE_ID token. That way,
10157 should we re-parse the token stream, we will not have to repeat
10158 the effort required to do the parse, nor will we issue duplicate
10159 error messages about problems during instantiation of the
10160 template. */
10161 if (start_of_id)
10162 {
10163 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10164
10165 /* Reset the contents of the START_OF_ID token. */
10166 token->type = CPP_TEMPLATE_ID;
10167 /* Retrieve any deferred checks. Do not pop this access checks yet
10168 so the memory will not be reclaimed during token replacing below. */
10169 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10170 token->u.tree_check_value->value = template_id;
10171 token->u.tree_check_value->checks = get_deferred_access_checks ();
10172 token->keyword = RID_MAX;
10173
10174 /* Purge all subsequent tokens. */
10175 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10176
10177 /* ??? Can we actually assume that, if template_id ==
10178 error_mark_node, we will have issued a diagnostic to the
10179 user, as opposed to simply marking the tentative parse as
10180 failed? */
10181 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10182 error ("%Hparse error in template argument list",
10183 &token->location);
10184 }
10185
10186 pop_deferring_access_checks ();
10187 return template_id;
10188 }
10189
10190 /* Parse a template-name.
10191
10192 template-name:
10193 identifier
10194
10195 The standard should actually say:
10196
10197 template-name:
10198 identifier
10199 operator-function-id
10200
10201 A defect report has been filed about this issue.
10202
10203 A conversion-function-id cannot be a template name because they cannot
10204 be part of a template-id. In fact, looking at this code:
10205
10206 a.operator K<int>()
10207
10208 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10209 It is impossible to call a templated conversion-function-id with an
10210 explicit argument list, since the only allowed template parameter is
10211 the type to which it is converting.
10212
10213 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10214 `template' keyword, in a construction like:
10215
10216 T::template f<3>()
10217
10218 In that case `f' is taken to be a template-name, even though there
10219 is no way of knowing for sure.
10220
10221 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10222 name refers to a set of overloaded functions, at least one of which
10223 is a template, or an IDENTIFIER_NODE with the name of the template,
10224 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10225 names are looked up inside uninstantiated templates. */
10226
10227 static tree
10228 cp_parser_template_name (cp_parser* parser,
10229 bool template_keyword_p,
10230 bool check_dependency_p,
10231 bool is_declaration,
10232 bool *is_identifier)
10233 {
10234 tree identifier;
10235 tree decl;
10236 tree fns;
10237 cp_token *token = cp_lexer_peek_token (parser->lexer);
10238
10239 /* If the next token is `operator', then we have either an
10240 operator-function-id or a conversion-function-id. */
10241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10242 {
10243 /* We don't know whether we're looking at an
10244 operator-function-id or a conversion-function-id. */
10245 cp_parser_parse_tentatively (parser);
10246 /* Try an operator-function-id. */
10247 identifier = cp_parser_operator_function_id (parser);
10248 /* If that didn't work, try a conversion-function-id. */
10249 if (!cp_parser_parse_definitely (parser))
10250 {
10251 cp_parser_error (parser, "expected template-name");
10252 return error_mark_node;
10253 }
10254 }
10255 /* Look for the identifier. */
10256 else
10257 identifier = cp_parser_identifier (parser);
10258
10259 /* If we didn't find an identifier, we don't have a template-id. */
10260 if (identifier == error_mark_node)
10261 return error_mark_node;
10262
10263 /* If the name immediately followed the `template' keyword, then it
10264 is a template-name. However, if the next token is not `<', then
10265 we do not treat it as a template-name, since it is not being used
10266 as part of a template-id. This enables us to handle constructs
10267 like:
10268
10269 template <typename T> struct S { S(); };
10270 template <typename T> S<T>::S();
10271
10272 correctly. We would treat `S' as a template -- if it were `S<T>'
10273 -- but we do not if there is no `<'. */
10274
10275 if (processing_template_decl
10276 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10277 {
10278 /* In a declaration, in a dependent context, we pretend that the
10279 "template" keyword was present in order to improve error
10280 recovery. For example, given:
10281
10282 template <typename T> void f(T::X<int>);
10283
10284 we want to treat "X<int>" as a template-id. */
10285 if (is_declaration
10286 && !template_keyword_p
10287 && parser->scope && TYPE_P (parser->scope)
10288 && check_dependency_p
10289 && dependent_type_p (parser->scope)
10290 /* Do not do this for dtors (or ctors), since they never
10291 need the template keyword before their name. */
10292 && !constructor_name_p (identifier, parser->scope))
10293 {
10294 cp_token_position start = 0;
10295
10296 /* Explain what went wrong. */
10297 error ("%Hnon-template %qD used as template",
10298 &token->location, identifier);
10299 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10300 parser->scope, identifier);
10301 /* If parsing tentatively, find the location of the "<" token. */
10302 if (cp_parser_simulate_error (parser))
10303 start = cp_lexer_token_position (parser->lexer, true);
10304 /* Parse the template arguments so that we can issue error
10305 messages about them. */
10306 cp_lexer_consume_token (parser->lexer);
10307 cp_parser_enclosed_template_argument_list (parser);
10308 /* Skip tokens until we find a good place from which to
10309 continue parsing. */
10310 cp_parser_skip_to_closing_parenthesis (parser,
10311 /*recovering=*/true,
10312 /*or_comma=*/true,
10313 /*consume_paren=*/false);
10314 /* If parsing tentatively, permanently remove the
10315 template argument list. That will prevent duplicate
10316 error messages from being issued about the missing
10317 "template" keyword. */
10318 if (start)
10319 cp_lexer_purge_tokens_after (parser->lexer, start);
10320 if (is_identifier)
10321 *is_identifier = true;
10322 return identifier;
10323 }
10324
10325 /* If the "template" keyword is present, then there is generally
10326 no point in doing name-lookup, so we just return IDENTIFIER.
10327 But, if the qualifying scope is non-dependent then we can
10328 (and must) do name-lookup normally. */
10329 if (template_keyword_p
10330 && (!parser->scope
10331 || (TYPE_P (parser->scope)
10332 && dependent_type_p (parser->scope))))
10333 return identifier;
10334 }
10335
10336 /* Look up the name. */
10337 decl = cp_parser_lookup_name (parser, identifier,
10338 none_type,
10339 /*is_template=*/false,
10340 /*is_namespace=*/false,
10341 check_dependency_p,
10342 /*ambiguous_decls=*/NULL,
10343 token->location);
10344 decl = maybe_get_template_decl_from_type_decl (decl);
10345
10346 /* If DECL is a template, then the name was a template-name. */
10347 if (TREE_CODE (decl) == TEMPLATE_DECL)
10348 ;
10349 else
10350 {
10351 tree fn = NULL_TREE;
10352
10353 /* The standard does not explicitly indicate whether a name that
10354 names a set of overloaded declarations, some of which are
10355 templates, is a template-name. However, such a name should
10356 be a template-name; otherwise, there is no way to form a
10357 template-id for the overloaded templates. */
10358 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10359 if (TREE_CODE (fns) == OVERLOAD)
10360 for (fn = fns; fn; fn = OVL_NEXT (fn))
10361 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10362 break;
10363
10364 if (!fn)
10365 {
10366 /* The name does not name a template. */
10367 cp_parser_error (parser, "expected template-name");
10368 return error_mark_node;
10369 }
10370 }
10371
10372 /* If DECL is dependent, and refers to a function, then just return
10373 its name; we will look it up again during template instantiation. */
10374 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10375 {
10376 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10377 if (TYPE_P (scope) && dependent_type_p (scope))
10378 return identifier;
10379 }
10380
10381 return decl;
10382 }
10383
10384 /* Parse a template-argument-list.
10385
10386 template-argument-list:
10387 template-argument ... [opt]
10388 template-argument-list , template-argument ... [opt]
10389
10390 Returns a TREE_VEC containing the arguments. */
10391
10392 static tree
10393 cp_parser_template_argument_list (cp_parser* parser)
10394 {
10395 tree fixed_args[10];
10396 unsigned n_args = 0;
10397 unsigned alloced = 10;
10398 tree *arg_ary = fixed_args;
10399 tree vec;
10400 bool saved_in_template_argument_list_p;
10401 bool saved_ice_p;
10402 bool saved_non_ice_p;
10403
10404 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10405 parser->in_template_argument_list_p = true;
10406 /* Even if the template-id appears in an integral
10407 constant-expression, the contents of the argument list do
10408 not. */
10409 saved_ice_p = parser->integral_constant_expression_p;
10410 parser->integral_constant_expression_p = false;
10411 saved_non_ice_p = parser->non_integral_constant_expression_p;
10412 parser->non_integral_constant_expression_p = false;
10413 /* Parse the arguments. */
10414 do
10415 {
10416 tree argument;
10417
10418 if (n_args)
10419 /* Consume the comma. */
10420 cp_lexer_consume_token (parser->lexer);
10421
10422 /* Parse the template-argument. */
10423 argument = cp_parser_template_argument (parser);
10424
10425 /* If the next token is an ellipsis, we're expanding a template
10426 argument pack. */
10427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10428 {
10429 /* Consume the `...' token. */
10430 cp_lexer_consume_token (parser->lexer);
10431
10432 /* Make the argument into a TYPE_PACK_EXPANSION or
10433 EXPR_PACK_EXPANSION. */
10434 argument = make_pack_expansion (argument);
10435 }
10436
10437 if (n_args == alloced)
10438 {
10439 alloced *= 2;
10440
10441 if (arg_ary == fixed_args)
10442 {
10443 arg_ary = XNEWVEC (tree, alloced);
10444 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10445 }
10446 else
10447 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10448 }
10449 arg_ary[n_args++] = argument;
10450 }
10451 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10452
10453 vec = make_tree_vec (n_args);
10454
10455 while (n_args--)
10456 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10457
10458 if (arg_ary != fixed_args)
10459 free (arg_ary);
10460 parser->non_integral_constant_expression_p = saved_non_ice_p;
10461 parser->integral_constant_expression_p = saved_ice_p;
10462 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10463 return vec;
10464 }
10465
10466 /* Parse a template-argument.
10467
10468 template-argument:
10469 assignment-expression
10470 type-id
10471 id-expression
10472
10473 The representation is that of an assignment-expression, type-id, or
10474 id-expression -- except that the qualified id-expression is
10475 evaluated, so that the value returned is either a DECL or an
10476 OVERLOAD.
10477
10478 Although the standard says "assignment-expression", it forbids
10479 throw-expressions or assignments in the template argument.
10480 Therefore, we use "conditional-expression" instead. */
10481
10482 static tree
10483 cp_parser_template_argument (cp_parser* parser)
10484 {
10485 tree argument;
10486 bool template_p;
10487 bool address_p;
10488 bool maybe_type_id = false;
10489 cp_token *token = NULL, *argument_start_token = NULL;
10490 cp_id_kind idk;
10491
10492 /* There's really no way to know what we're looking at, so we just
10493 try each alternative in order.
10494
10495 [temp.arg]
10496
10497 In a template-argument, an ambiguity between a type-id and an
10498 expression is resolved to a type-id, regardless of the form of
10499 the corresponding template-parameter.
10500
10501 Therefore, we try a type-id first. */
10502 cp_parser_parse_tentatively (parser);
10503 argument = cp_parser_type_id (parser);
10504 /* If there was no error parsing the type-id but the next token is a
10505 '>>', our behavior depends on which dialect of C++ we're
10506 parsing. In C++98, we probably found a typo for '> >'. But there
10507 are type-id which are also valid expressions. For instance:
10508
10509 struct X { int operator >> (int); };
10510 template <int V> struct Foo {};
10511 Foo<X () >> 5> r;
10512
10513 Here 'X()' is a valid type-id of a function type, but the user just
10514 wanted to write the expression "X() >> 5". Thus, we remember that we
10515 found a valid type-id, but we still try to parse the argument as an
10516 expression to see what happens.
10517
10518 In C++0x, the '>>' will be considered two separate '>'
10519 tokens. */
10520 if (!cp_parser_error_occurred (parser)
10521 && cxx_dialect == cxx98
10522 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10523 {
10524 maybe_type_id = true;
10525 cp_parser_abort_tentative_parse (parser);
10526 }
10527 else
10528 {
10529 /* If the next token isn't a `,' or a `>', then this argument wasn't
10530 really finished. This means that the argument is not a valid
10531 type-id. */
10532 if (!cp_parser_next_token_ends_template_argument_p (parser))
10533 cp_parser_error (parser, "expected template-argument");
10534 /* If that worked, we're done. */
10535 if (cp_parser_parse_definitely (parser))
10536 return argument;
10537 }
10538 /* We're still not sure what the argument will be. */
10539 cp_parser_parse_tentatively (parser);
10540 /* Try a template. */
10541 argument_start_token = cp_lexer_peek_token (parser->lexer);
10542 argument = cp_parser_id_expression (parser,
10543 /*template_keyword_p=*/false,
10544 /*check_dependency_p=*/true,
10545 &template_p,
10546 /*declarator_p=*/false,
10547 /*optional_p=*/false);
10548 /* If the next token isn't a `,' or a `>', then this argument wasn't
10549 really finished. */
10550 if (!cp_parser_next_token_ends_template_argument_p (parser))
10551 cp_parser_error (parser, "expected template-argument");
10552 if (!cp_parser_error_occurred (parser))
10553 {
10554 /* Figure out what is being referred to. If the id-expression
10555 was for a class template specialization, then we will have a
10556 TYPE_DECL at this point. There is no need to do name lookup
10557 at this point in that case. */
10558 if (TREE_CODE (argument) != TYPE_DECL)
10559 argument = cp_parser_lookup_name (parser, argument,
10560 none_type,
10561 /*is_template=*/template_p,
10562 /*is_namespace=*/false,
10563 /*check_dependency=*/true,
10564 /*ambiguous_decls=*/NULL,
10565 argument_start_token->location);
10566 if (TREE_CODE (argument) != TEMPLATE_DECL
10567 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10568 cp_parser_error (parser, "expected template-name");
10569 }
10570 if (cp_parser_parse_definitely (parser))
10571 return argument;
10572 /* It must be a non-type argument. There permitted cases are given
10573 in [temp.arg.nontype]:
10574
10575 -- an integral constant-expression of integral or enumeration
10576 type; or
10577
10578 -- the name of a non-type template-parameter; or
10579
10580 -- the name of an object or function with external linkage...
10581
10582 -- the address of an object or function with external linkage...
10583
10584 -- a pointer to member... */
10585 /* Look for a non-type template parameter. */
10586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10587 {
10588 cp_parser_parse_tentatively (parser);
10589 argument = cp_parser_primary_expression (parser,
10590 /*address_p=*/false,
10591 /*cast_p=*/false,
10592 /*template_arg_p=*/true,
10593 &idk);
10594 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10595 || !cp_parser_next_token_ends_template_argument_p (parser))
10596 cp_parser_simulate_error (parser);
10597 if (cp_parser_parse_definitely (parser))
10598 return argument;
10599 }
10600
10601 /* If the next token is "&", the argument must be the address of an
10602 object or function with external linkage. */
10603 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10604 if (address_p)
10605 cp_lexer_consume_token (parser->lexer);
10606 /* See if we might have an id-expression. */
10607 token = cp_lexer_peek_token (parser->lexer);
10608 if (token->type == CPP_NAME
10609 || token->keyword == RID_OPERATOR
10610 || token->type == CPP_SCOPE
10611 || token->type == CPP_TEMPLATE_ID
10612 || token->type == CPP_NESTED_NAME_SPECIFIER)
10613 {
10614 cp_parser_parse_tentatively (parser);
10615 argument = cp_parser_primary_expression (parser,
10616 address_p,
10617 /*cast_p=*/false,
10618 /*template_arg_p=*/true,
10619 &idk);
10620 if (cp_parser_error_occurred (parser)
10621 || !cp_parser_next_token_ends_template_argument_p (parser))
10622 cp_parser_abort_tentative_parse (parser);
10623 else
10624 {
10625 if (TREE_CODE (argument) == INDIRECT_REF)
10626 {
10627 gcc_assert (REFERENCE_REF_P (argument));
10628 argument = TREE_OPERAND (argument, 0);
10629 }
10630
10631 if (TREE_CODE (argument) == VAR_DECL)
10632 {
10633 /* A variable without external linkage might still be a
10634 valid constant-expression, so no error is issued here
10635 if the external-linkage check fails. */
10636 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10637 cp_parser_simulate_error (parser);
10638 }
10639 else if (is_overloaded_fn (argument))
10640 /* All overloaded functions are allowed; if the external
10641 linkage test does not pass, an error will be issued
10642 later. */
10643 ;
10644 else if (address_p
10645 && (TREE_CODE (argument) == OFFSET_REF
10646 || TREE_CODE (argument) == SCOPE_REF))
10647 /* A pointer-to-member. */
10648 ;
10649 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10650 ;
10651 else
10652 cp_parser_simulate_error (parser);
10653
10654 if (cp_parser_parse_definitely (parser))
10655 {
10656 if (address_p)
10657 argument = build_x_unary_op (ADDR_EXPR, argument,
10658 tf_warning_or_error);
10659 return argument;
10660 }
10661 }
10662 }
10663 /* If the argument started with "&", there are no other valid
10664 alternatives at this point. */
10665 if (address_p)
10666 {
10667 cp_parser_error (parser, "invalid non-type template argument");
10668 return error_mark_node;
10669 }
10670
10671 /* If the argument wasn't successfully parsed as a type-id followed
10672 by '>>', the argument can only be a constant expression now.
10673 Otherwise, we try parsing the constant-expression tentatively,
10674 because the argument could really be a type-id. */
10675 if (maybe_type_id)
10676 cp_parser_parse_tentatively (parser);
10677 argument = cp_parser_constant_expression (parser,
10678 /*allow_non_constant_p=*/false,
10679 /*non_constant_p=*/NULL);
10680 argument = fold_non_dependent_expr (argument);
10681 if (!maybe_type_id)
10682 return argument;
10683 if (!cp_parser_next_token_ends_template_argument_p (parser))
10684 cp_parser_error (parser, "expected template-argument");
10685 if (cp_parser_parse_definitely (parser))
10686 return argument;
10687 /* We did our best to parse the argument as a non type-id, but that
10688 was the only alternative that matched (albeit with a '>' after
10689 it). We can assume it's just a typo from the user, and a
10690 diagnostic will then be issued. */
10691 return cp_parser_type_id (parser);
10692 }
10693
10694 /* Parse an explicit-instantiation.
10695
10696 explicit-instantiation:
10697 template declaration
10698
10699 Although the standard says `declaration', what it really means is:
10700
10701 explicit-instantiation:
10702 template decl-specifier-seq [opt] declarator [opt] ;
10703
10704 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10705 supposed to be allowed. A defect report has been filed about this
10706 issue.
10707
10708 GNU Extension:
10709
10710 explicit-instantiation:
10711 storage-class-specifier template
10712 decl-specifier-seq [opt] declarator [opt] ;
10713 function-specifier template
10714 decl-specifier-seq [opt] declarator [opt] ; */
10715
10716 static void
10717 cp_parser_explicit_instantiation (cp_parser* parser)
10718 {
10719 int declares_class_or_enum;
10720 cp_decl_specifier_seq decl_specifiers;
10721 tree extension_specifier = NULL_TREE;
10722 cp_token *token;
10723
10724 /* Look for an (optional) storage-class-specifier or
10725 function-specifier. */
10726 if (cp_parser_allow_gnu_extensions_p (parser))
10727 {
10728 extension_specifier
10729 = cp_parser_storage_class_specifier_opt (parser);
10730 if (!extension_specifier)
10731 extension_specifier
10732 = cp_parser_function_specifier_opt (parser,
10733 /*decl_specs=*/NULL);
10734 }
10735
10736 /* Look for the `template' keyword. */
10737 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10738 /* Let the front end know that we are processing an explicit
10739 instantiation. */
10740 begin_explicit_instantiation ();
10741 /* [temp.explicit] says that we are supposed to ignore access
10742 control while processing explicit instantiation directives. */
10743 push_deferring_access_checks (dk_no_check);
10744 /* Parse a decl-specifier-seq. */
10745 token = cp_lexer_peek_token (parser->lexer);
10746 cp_parser_decl_specifier_seq (parser,
10747 CP_PARSER_FLAGS_OPTIONAL,
10748 &decl_specifiers,
10749 &declares_class_or_enum);
10750 /* If there was exactly one decl-specifier, and it declared a class,
10751 and there's no declarator, then we have an explicit type
10752 instantiation. */
10753 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10754 {
10755 tree type;
10756
10757 type = check_tag_decl (&decl_specifiers);
10758 /* Turn access control back on for names used during
10759 template instantiation. */
10760 pop_deferring_access_checks ();
10761 if (type)
10762 do_type_instantiation (type, extension_specifier,
10763 /*complain=*/tf_error);
10764 }
10765 else
10766 {
10767 cp_declarator *declarator;
10768 tree decl;
10769
10770 /* Parse the declarator. */
10771 declarator
10772 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10773 /*ctor_dtor_or_conv_p=*/NULL,
10774 /*parenthesized_p=*/NULL,
10775 /*member_p=*/false);
10776 if (declares_class_or_enum & 2)
10777 cp_parser_check_for_definition_in_return_type (declarator,
10778 decl_specifiers.type,
10779 decl_specifiers.type_location);
10780 if (declarator != cp_error_declarator)
10781 {
10782 decl = grokdeclarator (declarator, &decl_specifiers,
10783 NORMAL, 0, &decl_specifiers.attributes);
10784 /* Turn access control back on for names used during
10785 template instantiation. */
10786 pop_deferring_access_checks ();
10787 /* Do the explicit instantiation. */
10788 do_decl_instantiation (decl, extension_specifier);
10789 }
10790 else
10791 {
10792 pop_deferring_access_checks ();
10793 /* Skip the body of the explicit instantiation. */
10794 cp_parser_skip_to_end_of_statement (parser);
10795 }
10796 }
10797 /* We're done with the instantiation. */
10798 end_explicit_instantiation ();
10799
10800 cp_parser_consume_semicolon_at_end_of_statement (parser);
10801 }
10802
10803 /* Parse an explicit-specialization.
10804
10805 explicit-specialization:
10806 template < > declaration
10807
10808 Although the standard says `declaration', what it really means is:
10809
10810 explicit-specialization:
10811 template <> decl-specifier [opt] init-declarator [opt] ;
10812 template <> function-definition
10813 template <> explicit-specialization
10814 template <> template-declaration */
10815
10816 static void
10817 cp_parser_explicit_specialization (cp_parser* parser)
10818 {
10819 bool need_lang_pop;
10820 cp_token *token = cp_lexer_peek_token (parser->lexer);
10821
10822 /* Look for the `template' keyword. */
10823 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10824 /* Look for the `<'. */
10825 cp_parser_require (parser, CPP_LESS, "%<<%>");
10826 /* Look for the `>'. */
10827 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10828 /* We have processed another parameter list. */
10829 ++parser->num_template_parameter_lists;
10830 /* [temp]
10831
10832 A template ... explicit specialization ... shall not have C
10833 linkage. */
10834 if (current_lang_name == lang_name_c)
10835 {
10836 error ("%Htemplate specialization with C linkage", &token->location);
10837 /* Give it C++ linkage to avoid confusing other parts of the
10838 front end. */
10839 push_lang_context (lang_name_cplusplus);
10840 need_lang_pop = true;
10841 }
10842 else
10843 need_lang_pop = false;
10844 /* Let the front end know that we are beginning a specialization. */
10845 if (!begin_specialization ())
10846 {
10847 end_specialization ();
10848 cp_parser_skip_to_end_of_block_or_statement (parser);
10849 return;
10850 }
10851
10852 /* If the next keyword is `template', we need to figure out whether
10853 or not we're looking a template-declaration. */
10854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10855 {
10856 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10857 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10858 cp_parser_template_declaration_after_export (parser,
10859 /*member_p=*/false);
10860 else
10861 cp_parser_explicit_specialization (parser);
10862 }
10863 else
10864 /* Parse the dependent declaration. */
10865 cp_parser_single_declaration (parser,
10866 /*checks=*/NULL,
10867 /*member_p=*/false,
10868 /*explicit_specialization_p=*/true,
10869 /*friend_p=*/NULL);
10870 /* We're done with the specialization. */
10871 end_specialization ();
10872 /* For the erroneous case of a template with C linkage, we pushed an
10873 implicit C++ linkage scope; exit that scope now. */
10874 if (need_lang_pop)
10875 pop_lang_context ();
10876 /* We're done with this parameter list. */
10877 --parser->num_template_parameter_lists;
10878 }
10879
10880 /* Parse a type-specifier.
10881
10882 type-specifier:
10883 simple-type-specifier
10884 class-specifier
10885 enum-specifier
10886 elaborated-type-specifier
10887 cv-qualifier
10888
10889 GNU Extension:
10890
10891 type-specifier:
10892 __complex__
10893
10894 Returns a representation of the type-specifier. For a
10895 class-specifier, enum-specifier, or elaborated-type-specifier, a
10896 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10897
10898 The parser flags FLAGS is used to control type-specifier parsing.
10899
10900 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10901 in a decl-specifier-seq.
10902
10903 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10904 class-specifier, enum-specifier, or elaborated-type-specifier, then
10905 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10906 if a type is declared; 2 if it is defined. Otherwise, it is set to
10907 zero.
10908
10909 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10910 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10911 is set to FALSE. */
10912
10913 static tree
10914 cp_parser_type_specifier (cp_parser* parser,
10915 cp_parser_flags flags,
10916 cp_decl_specifier_seq *decl_specs,
10917 bool is_declaration,
10918 int* declares_class_or_enum,
10919 bool* is_cv_qualifier)
10920 {
10921 tree type_spec = NULL_TREE;
10922 cp_token *token;
10923 enum rid keyword;
10924 cp_decl_spec ds = ds_last;
10925
10926 /* Assume this type-specifier does not declare a new type. */
10927 if (declares_class_or_enum)
10928 *declares_class_or_enum = 0;
10929 /* And that it does not specify a cv-qualifier. */
10930 if (is_cv_qualifier)
10931 *is_cv_qualifier = false;
10932 /* Peek at the next token. */
10933 token = cp_lexer_peek_token (parser->lexer);
10934
10935 /* If we're looking at a keyword, we can use that to guide the
10936 production we choose. */
10937 keyword = token->keyword;
10938 switch (keyword)
10939 {
10940 case RID_ENUM:
10941 /* Look for the enum-specifier. */
10942 type_spec = cp_parser_enum_specifier (parser);
10943 /* If that worked, we're done. */
10944 if (type_spec)
10945 {
10946 if (declares_class_or_enum)
10947 *declares_class_or_enum = 2;
10948 if (decl_specs)
10949 cp_parser_set_decl_spec_type (decl_specs,
10950 type_spec,
10951 token->location,
10952 /*user_defined_p=*/true);
10953 return type_spec;
10954 }
10955 else
10956 goto elaborated_type_specifier;
10957
10958 /* Any of these indicate either a class-specifier, or an
10959 elaborated-type-specifier. */
10960 case RID_CLASS:
10961 case RID_STRUCT:
10962 case RID_UNION:
10963 /* Parse tentatively so that we can back up if we don't find a
10964 class-specifier. */
10965 cp_parser_parse_tentatively (parser);
10966 /* Look for the class-specifier. */
10967 type_spec = cp_parser_class_specifier (parser);
10968 /* If that worked, we're done. */
10969 if (cp_parser_parse_definitely (parser))
10970 {
10971 if (declares_class_or_enum)
10972 *declares_class_or_enum = 2;
10973 if (decl_specs)
10974 cp_parser_set_decl_spec_type (decl_specs,
10975 type_spec,
10976 token->location,
10977 /*user_defined_p=*/true);
10978 return type_spec;
10979 }
10980
10981 /* Fall through. */
10982 elaborated_type_specifier:
10983 /* We're declaring (not defining) a class or enum. */
10984 if (declares_class_or_enum)
10985 *declares_class_or_enum = 1;
10986
10987 /* Fall through. */
10988 case RID_TYPENAME:
10989 /* Look for an elaborated-type-specifier. */
10990 type_spec
10991 = (cp_parser_elaborated_type_specifier
10992 (parser,
10993 decl_specs && decl_specs->specs[(int) ds_friend],
10994 is_declaration));
10995 if (decl_specs)
10996 cp_parser_set_decl_spec_type (decl_specs,
10997 type_spec,
10998 token->location,
10999 /*user_defined_p=*/true);
11000 return type_spec;
11001
11002 case RID_CONST:
11003 ds = ds_const;
11004 if (is_cv_qualifier)
11005 *is_cv_qualifier = true;
11006 break;
11007
11008 case RID_VOLATILE:
11009 ds = ds_volatile;
11010 if (is_cv_qualifier)
11011 *is_cv_qualifier = true;
11012 break;
11013
11014 case RID_RESTRICT:
11015 ds = ds_restrict;
11016 if (is_cv_qualifier)
11017 *is_cv_qualifier = true;
11018 break;
11019
11020 case RID_COMPLEX:
11021 /* The `__complex__' keyword is a GNU extension. */
11022 ds = ds_complex;
11023 break;
11024
11025 default:
11026 break;
11027 }
11028
11029 /* Handle simple keywords. */
11030 if (ds != ds_last)
11031 {
11032 if (decl_specs)
11033 {
11034 ++decl_specs->specs[(int)ds];
11035 decl_specs->any_specifiers_p = true;
11036 }
11037 return cp_lexer_consume_token (parser->lexer)->u.value;
11038 }
11039
11040 /* If we do not already have a type-specifier, assume we are looking
11041 at a simple-type-specifier. */
11042 type_spec = cp_parser_simple_type_specifier (parser,
11043 decl_specs,
11044 flags);
11045
11046 /* If we didn't find a type-specifier, and a type-specifier was not
11047 optional in this context, issue an error message. */
11048 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11049 {
11050 cp_parser_error (parser, "expected type specifier");
11051 return error_mark_node;
11052 }
11053
11054 return type_spec;
11055 }
11056
11057 /* Parse a simple-type-specifier.
11058
11059 simple-type-specifier:
11060 :: [opt] nested-name-specifier [opt] type-name
11061 :: [opt] nested-name-specifier template template-id
11062 char
11063 wchar_t
11064 bool
11065 short
11066 int
11067 long
11068 signed
11069 unsigned
11070 float
11071 double
11072 void
11073
11074 C++0x Extension:
11075
11076 simple-type-specifier:
11077 auto
11078 decltype ( expression )
11079 char16_t
11080 char32_t
11081
11082 GNU Extension:
11083
11084 simple-type-specifier:
11085 __typeof__ unary-expression
11086 __typeof__ ( type-id )
11087
11088 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11089 appropriately updated. */
11090
11091 static tree
11092 cp_parser_simple_type_specifier (cp_parser* parser,
11093 cp_decl_specifier_seq *decl_specs,
11094 cp_parser_flags flags)
11095 {
11096 tree type = NULL_TREE;
11097 cp_token *token;
11098
11099 /* Peek at the next token. */
11100 token = cp_lexer_peek_token (parser->lexer);
11101
11102 /* If we're looking at a keyword, things are easy. */
11103 switch (token->keyword)
11104 {
11105 case RID_CHAR:
11106 if (decl_specs)
11107 decl_specs->explicit_char_p = true;
11108 type = char_type_node;
11109 break;
11110 case RID_CHAR16:
11111 type = char16_type_node;
11112 break;
11113 case RID_CHAR32:
11114 type = char32_type_node;
11115 break;
11116 case RID_WCHAR:
11117 type = wchar_type_node;
11118 break;
11119 case RID_BOOL:
11120 type = boolean_type_node;
11121 break;
11122 case RID_SHORT:
11123 if (decl_specs)
11124 ++decl_specs->specs[(int) ds_short];
11125 type = short_integer_type_node;
11126 break;
11127 case RID_INT:
11128 if (decl_specs)
11129 decl_specs->explicit_int_p = true;
11130 type = integer_type_node;
11131 break;
11132 case RID_LONG:
11133 if (decl_specs)
11134 ++decl_specs->specs[(int) ds_long];
11135 type = long_integer_type_node;
11136 break;
11137 case RID_SIGNED:
11138 if (decl_specs)
11139 ++decl_specs->specs[(int) ds_signed];
11140 type = integer_type_node;
11141 break;
11142 case RID_UNSIGNED:
11143 if (decl_specs)
11144 ++decl_specs->specs[(int) ds_unsigned];
11145 type = unsigned_type_node;
11146 break;
11147 case RID_FLOAT:
11148 type = float_type_node;
11149 break;
11150 case RID_DOUBLE:
11151 type = double_type_node;
11152 break;
11153 case RID_VOID:
11154 type = void_type_node;
11155 break;
11156
11157 case RID_AUTO:
11158 maybe_warn_cpp0x ("C++0x auto");
11159 type = make_auto ();
11160 break;
11161
11162 case RID_DECLTYPE:
11163 /* Parse the `decltype' type. */
11164 type = cp_parser_decltype (parser);
11165
11166 if (decl_specs)
11167 cp_parser_set_decl_spec_type (decl_specs, type,
11168 token->location,
11169 /*user_defined_p=*/true);
11170
11171 return type;
11172
11173 case RID_TYPEOF:
11174 /* Consume the `typeof' token. */
11175 cp_lexer_consume_token (parser->lexer);
11176 /* Parse the operand to `typeof'. */
11177 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11178 /* If it is not already a TYPE, take its type. */
11179 if (!TYPE_P (type))
11180 type = finish_typeof (type);
11181
11182 if (decl_specs)
11183 cp_parser_set_decl_spec_type (decl_specs, type,
11184 token->location,
11185 /*user_defined_p=*/true);
11186
11187 return type;
11188
11189 default:
11190 break;
11191 }
11192
11193 /* If the type-specifier was for a built-in type, we're done. */
11194 if (type)
11195 {
11196 tree id;
11197
11198 /* Record the type. */
11199 if (decl_specs
11200 && (token->keyword != RID_SIGNED
11201 && token->keyword != RID_UNSIGNED
11202 && token->keyword != RID_SHORT
11203 && token->keyword != RID_LONG))
11204 cp_parser_set_decl_spec_type (decl_specs,
11205 type,
11206 token->location,
11207 /*user_defined=*/false);
11208 if (decl_specs)
11209 decl_specs->any_specifiers_p = true;
11210
11211 /* Consume the token. */
11212 id = cp_lexer_consume_token (parser->lexer)->u.value;
11213
11214 /* There is no valid C++ program where a non-template type is
11215 followed by a "<". That usually indicates that the user thought
11216 that the type was a template. */
11217 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11218
11219 return TYPE_NAME (type);
11220 }
11221
11222 /* The type-specifier must be a user-defined type. */
11223 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11224 {
11225 bool qualified_p;
11226 bool global_p;
11227
11228 /* Don't gobble tokens or issue error messages if this is an
11229 optional type-specifier. */
11230 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11231 cp_parser_parse_tentatively (parser);
11232
11233 /* Look for the optional `::' operator. */
11234 global_p
11235 = (cp_parser_global_scope_opt (parser,
11236 /*current_scope_valid_p=*/false)
11237 != NULL_TREE);
11238 /* Look for the nested-name specifier. */
11239 qualified_p
11240 = (cp_parser_nested_name_specifier_opt (parser,
11241 /*typename_keyword_p=*/false,
11242 /*check_dependency_p=*/true,
11243 /*type_p=*/false,
11244 /*is_declaration=*/false)
11245 != NULL_TREE);
11246 token = cp_lexer_peek_token (parser->lexer);
11247 /* If we have seen a nested-name-specifier, and the next token
11248 is `template', then we are using the template-id production. */
11249 if (parser->scope
11250 && cp_parser_optional_template_keyword (parser))
11251 {
11252 /* Look for the template-id. */
11253 type = cp_parser_template_id (parser,
11254 /*template_keyword_p=*/true,
11255 /*check_dependency_p=*/true,
11256 /*is_declaration=*/false);
11257 /* If the template-id did not name a type, we are out of
11258 luck. */
11259 if (TREE_CODE (type) != TYPE_DECL)
11260 {
11261 cp_parser_error (parser, "expected template-id for type");
11262 type = NULL_TREE;
11263 }
11264 }
11265 /* Otherwise, look for a type-name. */
11266 else
11267 type = cp_parser_type_name (parser);
11268 /* Keep track of all name-lookups performed in class scopes. */
11269 if (type
11270 && !global_p
11271 && !qualified_p
11272 && TREE_CODE (type) == TYPE_DECL
11273 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11274 maybe_note_name_used_in_class (DECL_NAME (type), type);
11275 /* If it didn't work out, we don't have a TYPE. */
11276 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11277 && !cp_parser_parse_definitely (parser))
11278 type = NULL_TREE;
11279 if (type && decl_specs)
11280 cp_parser_set_decl_spec_type (decl_specs, type,
11281 token->location,
11282 /*user_defined=*/true);
11283 }
11284
11285 /* If we didn't get a type-name, issue an error message. */
11286 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11287 {
11288 cp_parser_error (parser, "expected type-name");
11289 return error_mark_node;
11290 }
11291
11292 /* There is no valid C++ program where a non-template type is
11293 followed by a "<". That usually indicates that the user thought
11294 that the type was a template. */
11295 if (type && type != error_mark_node)
11296 {
11297 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11298 If it is, then the '<'...'>' enclose protocol names rather than
11299 template arguments, and so everything is fine. */
11300 if (c_dialect_objc ()
11301 && (objc_is_id (type) || objc_is_class_name (type)))
11302 {
11303 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11304 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11305
11306 /* Clobber the "unqualified" type previously entered into
11307 DECL_SPECS with the new, improved protocol-qualified version. */
11308 if (decl_specs)
11309 decl_specs->type = qual_type;
11310
11311 return qual_type;
11312 }
11313
11314 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11315 token->location);
11316 }
11317
11318 return type;
11319 }
11320
11321 /* Parse a type-name.
11322
11323 type-name:
11324 class-name
11325 enum-name
11326 typedef-name
11327
11328 enum-name:
11329 identifier
11330
11331 typedef-name:
11332 identifier
11333
11334 Returns a TYPE_DECL for the type. */
11335
11336 static tree
11337 cp_parser_type_name (cp_parser* parser)
11338 {
11339 tree type_decl;
11340
11341 /* We can't know yet whether it is a class-name or not. */
11342 cp_parser_parse_tentatively (parser);
11343 /* Try a class-name. */
11344 type_decl = cp_parser_class_name (parser,
11345 /*typename_keyword_p=*/false,
11346 /*template_keyword_p=*/false,
11347 none_type,
11348 /*check_dependency_p=*/true,
11349 /*class_head_p=*/false,
11350 /*is_declaration=*/false);
11351 /* If it's not a class-name, keep looking. */
11352 if (!cp_parser_parse_definitely (parser))
11353 {
11354 /* It must be a typedef-name or an enum-name. */
11355 return cp_parser_nonclass_name (parser);
11356 }
11357
11358 return type_decl;
11359 }
11360
11361 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11362
11363 enum-name:
11364 identifier
11365
11366 typedef-name:
11367 identifier
11368
11369 Returns a TYPE_DECL for the type. */
11370
11371 static tree
11372 cp_parser_nonclass_name (cp_parser* parser)
11373 {
11374 tree type_decl;
11375 tree identifier;
11376
11377 cp_token *token = cp_lexer_peek_token (parser->lexer);
11378 identifier = cp_parser_identifier (parser);
11379 if (identifier == error_mark_node)
11380 return error_mark_node;
11381
11382 /* Look up the type-name. */
11383 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11384
11385 if (TREE_CODE (type_decl) != TYPE_DECL
11386 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11387 {
11388 /* See if this is an Objective-C type. */
11389 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11390 tree type = objc_get_protocol_qualified_type (identifier, protos);
11391 if (type)
11392 type_decl = TYPE_NAME (type);
11393 }
11394
11395 /* Issue an error if we did not find a type-name. */
11396 if (TREE_CODE (type_decl) != TYPE_DECL)
11397 {
11398 if (!cp_parser_simulate_error (parser))
11399 cp_parser_name_lookup_error (parser, identifier, type_decl,
11400 "is not a type", token->location);
11401 return error_mark_node;
11402 }
11403 /* Remember that the name was used in the definition of the
11404 current class so that we can check later to see if the
11405 meaning would have been different after the class was
11406 entirely defined. */
11407 else if (type_decl != error_mark_node
11408 && !parser->scope)
11409 maybe_note_name_used_in_class (identifier, type_decl);
11410
11411 return type_decl;
11412 }
11413
11414 /* Parse an elaborated-type-specifier. Note that the grammar given
11415 here incorporates the resolution to DR68.
11416
11417 elaborated-type-specifier:
11418 class-key :: [opt] nested-name-specifier [opt] identifier
11419 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11420 enum-key :: [opt] nested-name-specifier [opt] identifier
11421 typename :: [opt] nested-name-specifier identifier
11422 typename :: [opt] nested-name-specifier template [opt]
11423 template-id
11424
11425 GNU extension:
11426
11427 elaborated-type-specifier:
11428 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11429 class-key attributes :: [opt] nested-name-specifier [opt]
11430 template [opt] template-id
11431 enum attributes :: [opt] nested-name-specifier [opt] identifier
11432
11433 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11434 declared `friend'. If IS_DECLARATION is TRUE, then this
11435 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11436 something is being declared.
11437
11438 Returns the TYPE specified. */
11439
11440 static tree
11441 cp_parser_elaborated_type_specifier (cp_parser* parser,
11442 bool is_friend,
11443 bool is_declaration)
11444 {
11445 enum tag_types tag_type;
11446 tree identifier;
11447 tree type = NULL_TREE;
11448 tree attributes = NULL_TREE;
11449 cp_token *token = NULL;
11450
11451 /* See if we're looking at the `enum' keyword. */
11452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11453 {
11454 /* Consume the `enum' token. */
11455 cp_lexer_consume_token (parser->lexer);
11456 /* Remember that it's an enumeration type. */
11457 tag_type = enum_type;
11458 /* Parse the optional `struct' or `class' key (for C++0x scoped
11459 enums). */
11460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11461 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11462 {
11463 if (cxx_dialect == cxx98)
11464 maybe_warn_cpp0x ("scoped enums");
11465
11466 /* Consume the `struct' or `class'. */
11467 cp_lexer_consume_token (parser->lexer);
11468 }
11469 /* Parse the attributes. */
11470 attributes = cp_parser_attributes_opt (parser);
11471 }
11472 /* Or, it might be `typename'. */
11473 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11474 RID_TYPENAME))
11475 {
11476 /* Consume the `typename' token. */
11477 cp_lexer_consume_token (parser->lexer);
11478 /* Remember that it's a `typename' type. */
11479 tag_type = typename_type;
11480 /* The `typename' keyword is only allowed in templates. */
11481 if (!processing_template_decl)
11482 permerror (input_location, "using %<typename%> outside of template");
11483 }
11484 /* Otherwise it must be a class-key. */
11485 else
11486 {
11487 tag_type = cp_parser_class_key (parser);
11488 if (tag_type == none_type)
11489 return error_mark_node;
11490 /* Parse the attributes. */
11491 attributes = cp_parser_attributes_opt (parser);
11492 }
11493
11494 /* Look for the `::' operator. */
11495 cp_parser_global_scope_opt (parser,
11496 /*current_scope_valid_p=*/false);
11497 /* Look for the nested-name-specifier. */
11498 if (tag_type == typename_type)
11499 {
11500 if (!cp_parser_nested_name_specifier (parser,
11501 /*typename_keyword_p=*/true,
11502 /*check_dependency_p=*/true,
11503 /*type_p=*/true,
11504 is_declaration))
11505 return error_mark_node;
11506 }
11507 else
11508 /* Even though `typename' is not present, the proposed resolution
11509 to Core Issue 180 says that in `class A<T>::B', `B' should be
11510 considered a type-name, even if `A<T>' is dependent. */
11511 cp_parser_nested_name_specifier_opt (parser,
11512 /*typename_keyword_p=*/true,
11513 /*check_dependency_p=*/true,
11514 /*type_p=*/true,
11515 is_declaration);
11516 /* For everything but enumeration types, consider a template-id.
11517 For an enumeration type, consider only a plain identifier. */
11518 if (tag_type != enum_type)
11519 {
11520 bool template_p = false;
11521 tree decl;
11522
11523 /* Allow the `template' keyword. */
11524 template_p = cp_parser_optional_template_keyword (parser);
11525 /* If we didn't see `template', we don't know if there's a
11526 template-id or not. */
11527 if (!template_p)
11528 cp_parser_parse_tentatively (parser);
11529 /* Parse the template-id. */
11530 token = cp_lexer_peek_token (parser->lexer);
11531 decl = cp_parser_template_id (parser, template_p,
11532 /*check_dependency_p=*/true,
11533 is_declaration);
11534 /* If we didn't find a template-id, look for an ordinary
11535 identifier. */
11536 if (!template_p && !cp_parser_parse_definitely (parser))
11537 ;
11538 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11539 in effect, then we must assume that, upon instantiation, the
11540 template will correspond to a class. */
11541 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11542 && tag_type == typename_type)
11543 type = make_typename_type (parser->scope, decl,
11544 typename_type,
11545 /*complain=*/tf_error);
11546 else
11547 type = TREE_TYPE (decl);
11548 }
11549
11550 if (!type)
11551 {
11552 token = cp_lexer_peek_token (parser->lexer);
11553 identifier = cp_parser_identifier (parser);
11554
11555 if (identifier == error_mark_node)
11556 {
11557 parser->scope = NULL_TREE;
11558 return error_mark_node;
11559 }
11560
11561 /* For a `typename', we needn't call xref_tag. */
11562 if (tag_type == typename_type
11563 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11564 return cp_parser_make_typename_type (parser, parser->scope,
11565 identifier,
11566 token->location);
11567 /* Look up a qualified name in the usual way. */
11568 if (parser->scope)
11569 {
11570 tree decl;
11571 tree ambiguous_decls;
11572
11573 decl = cp_parser_lookup_name (parser, identifier,
11574 tag_type,
11575 /*is_template=*/false,
11576 /*is_namespace=*/false,
11577 /*check_dependency=*/true,
11578 &ambiguous_decls,
11579 token->location);
11580
11581 /* If the lookup was ambiguous, an error will already have been
11582 issued. */
11583 if (ambiguous_decls)
11584 return error_mark_node;
11585
11586 /* If we are parsing friend declaration, DECL may be a
11587 TEMPLATE_DECL tree node here. However, we need to check
11588 whether this TEMPLATE_DECL results in valid code. Consider
11589 the following example:
11590
11591 namespace N {
11592 template <class T> class C {};
11593 }
11594 class X {
11595 template <class T> friend class N::C; // #1, valid code
11596 };
11597 template <class T> class Y {
11598 friend class N::C; // #2, invalid code
11599 };
11600
11601 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11602 name lookup of `N::C'. We see that friend declaration must
11603 be template for the code to be valid. Note that
11604 processing_template_decl does not work here since it is
11605 always 1 for the above two cases. */
11606
11607 decl = (cp_parser_maybe_treat_template_as_class
11608 (decl, /*tag_name_p=*/is_friend
11609 && parser->num_template_parameter_lists));
11610
11611 if (TREE_CODE (decl) != TYPE_DECL)
11612 {
11613 cp_parser_diagnose_invalid_type_name (parser,
11614 parser->scope,
11615 identifier,
11616 token->location);
11617 return error_mark_node;
11618 }
11619
11620 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11621 {
11622 bool allow_template = (parser->num_template_parameter_lists
11623 || DECL_SELF_REFERENCE_P (decl));
11624 type = check_elaborated_type_specifier (tag_type, decl,
11625 allow_template);
11626
11627 if (type == error_mark_node)
11628 return error_mark_node;
11629 }
11630
11631 /* Forward declarations of nested types, such as
11632
11633 class C1::C2;
11634 class C1::C2::C3;
11635
11636 are invalid unless all components preceding the final '::'
11637 are complete. If all enclosing types are complete, these
11638 declarations become merely pointless.
11639
11640 Invalid forward declarations of nested types are errors
11641 caught elsewhere in parsing. Those that are pointless arrive
11642 here. */
11643
11644 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11645 && !is_friend && !processing_explicit_instantiation)
11646 warning (0, "declaration %qD does not declare anything", decl);
11647
11648 type = TREE_TYPE (decl);
11649 }
11650 else
11651 {
11652 /* An elaborated-type-specifier sometimes introduces a new type and
11653 sometimes names an existing type. Normally, the rule is that it
11654 introduces a new type only if there is not an existing type of
11655 the same name already in scope. For example, given:
11656
11657 struct S {};
11658 void f() { struct S s; }
11659
11660 the `struct S' in the body of `f' is the same `struct S' as in
11661 the global scope; the existing definition is used. However, if
11662 there were no global declaration, this would introduce a new
11663 local class named `S'.
11664
11665 An exception to this rule applies to the following code:
11666
11667 namespace N { struct S; }
11668
11669 Here, the elaborated-type-specifier names a new type
11670 unconditionally; even if there is already an `S' in the
11671 containing scope this declaration names a new type.
11672 This exception only applies if the elaborated-type-specifier
11673 forms the complete declaration:
11674
11675 [class.name]
11676
11677 A declaration consisting solely of `class-key identifier ;' is
11678 either a redeclaration of the name in the current scope or a
11679 forward declaration of the identifier as a class name. It
11680 introduces the name into the current scope.
11681
11682 We are in this situation precisely when the next token is a `;'.
11683
11684 An exception to the exception is that a `friend' declaration does
11685 *not* name a new type; i.e., given:
11686
11687 struct S { friend struct T; };
11688
11689 `T' is not a new type in the scope of `S'.
11690
11691 Also, `new struct S' or `sizeof (struct S)' never results in the
11692 definition of a new type; a new type can only be declared in a
11693 declaration context. */
11694
11695 tag_scope ts;
11696 bool template_p;
11697
11698 if (is_friend)
11699 /* Friends have special name lookup rules. */
11700 ts = ts_within_enclosing_non_class;
11701 else if (is_declaration
11702 && cp_lexer_next_token_is (parser->lexer,
11703 CPP_SEMICOLON))
11704 /* This is a `class-key identifier ;' */
11705 ts = ts_current;
11706 else
11707 ts = ts_global;
11708
11709 template_p =
11710 (parser->num_template_parameter_lists
11711 && (cp_parser_next_token_starts_class_definition_p (parser)
11712 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11713 /* An unqualified name was used to reference this type, so
11714 there were no qualifying templates. */
11715 if (!cp_parser_check_template_parameters (parser,
11716 /*num_templates=*/0,
11717 token->location))
11718 return error_mark_node;
11719 type = xref_tag (tag_type, identifier, ts, template_p);
11720 }
11721 }
11722
11723 if (type == error_mark_node)
11724 return error_mark_node;
11725
11726 /* Allow attributes on forward declarations of classes. */
11727 if (attributes)
11728 {
11729 if (TREE_CODE (type) == TYPENAME_TYPE)
11730 warning (OPT_Wattributes,
11731 "attributes ignored on uninstantiated type");
11732 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11733 && ! processing_explicit_instantiation)
11734 warning (OPT_Wattributes,
11735 "attributes ignored on template instantiation");
11736 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11737 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11738 else
11739 warning (OPT_Wattributes,
11740 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11741 }
11742
11743 if (tag_type != enum_type)
11744 cp_parser_check_class_key (tag_type, type);
11745
11746 /* A "<" cannot follow an elaborated type specifier. If that
11747 happens, the user was probably trying to form a template-id. */
11748 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11749
11750 return type;
11751 }
11752
11753 /* Parse an enum-specifier.
11754
11755 enum-specifier:
11756 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11757
11758 enum-key:
11759 enum
11760 enum class [C++0x]
11761 enum struct [C++0x]
11762
11763 enum-base: [C++0x]
11764 : type-specifier-seq
11765
11766 GNU Extensions:
11767 enum-key attributes[opt] identifier [opt] enum-base [opt]
11768 { enumerator-list [opt] }attributes[opt]
11769
11770 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11771 if the token stream isn't an enum-specifier after all. */
11772
11773 static tree
11774 cp_parser_enum_specifier (cp_parser* parser)
11775 {
11776 tree identifier;
11777 tree type;
11778 tree attributes;
11779 bool scoped_enum_p = false;
11780 bool has_underlying_type = false;
11781 tree underlying_type = NULL_TREE;
11782
11783 /* Parse tentatively so that we can back up if we don't find a
11784 enum-specifier. */
11785 cp_parser_parse_tentatively (parser);
11786
11787 /* Caller guarantees that the current token is 'enum', an identifier
11788 possibly follows, and the token after that is an opening brace.
11789 If we don't have an identifier, fabricate an anonymous name for
11790 the enumeration being defined. */
11791 cp_lexer_consume_token (parser->lexer);
11792
11793 /* Parse the "class" or "struct", which indicates a scoped
11794 enumeration type in C++0x. */
11795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11796 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11797 {
11798 if (cxx_dialect == cxx98)
11799 maybe_warn_cpp0x ("scoped enums");
11800
11801 /* Consume the `struct' or `class' token. */
11802 cp_lexer_consume_token (parser->lexer);
11803
11804 scoped_enum_p = true;
11805 }
11806
11807 attributes = cp_parser_attributes_opt (parser);
11808
11809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11810 identifier = cp_parser_identifier (parser);
11811 else
11812 identifier = make_anon_name ();
11813
11814 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11815 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11816 {
11817 cp_decl_specifier_seq type_specifiers;
11818
11819 /* At this point this is surely not elaborated type specifier. */
11820 if (!cp_parser_parse_definitely (parser))
11821 return NULL_TREE;
11822
11823 if (cxx_dialect == cxx98)
11824 maybe_warn_cpp0x ("scoped enums");
11825
11826 /* Consume the `:'. */
11827 cp_lexer_consume_token (parser->lexer);
11828
11829 has_underlying_type = true;
11830
11831 /* Parse the type-specifier-seq. */
11832 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11833 &type_specifiers);
11834
11835 /* If that didn't work, stop. */
11836 if (type_specifiers.type != error_mark_node)
11837 {
11838 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11839 /*initialized=*/0, NULL);
11840 if (underlying_type == error_mark_node)
11841 underlying_type = NULL_TREE;
11842 }
11843 }
11844
11845 /* Look for the `{' but don't consume it yet. */
11846 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11847 {
11848 cp_parser_error (parser, "expected %<{%>");
11849 if (has_underlying_type)
11850 return NULL_TREE;
11851 }
11852
11853 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11854 return NULL_TREE;
11855
11856 /* Issue an error message if type-definitions are forbidden here. */
11857 if (!cp_parser_check_type_definition (parser))
11858 type = error_mark_node;
11859 else
11860 /* Create the new type. We do this before consuming the opening
11861 brace so the enum will be recorded as being on the line of its
11862 tag (or the 'enum' keyword, if there is no tag). */
11863 type = start_enum (identifier, underlying_type, scoped_enum_p);
11864
11865 /* Consume the opening brace. */
11866 cp_lexer_consume_token (parser->lexer);
11867
11868 if (type == error_mark_node)
11869 {
11870 cp_parser_skip_to_end_of_block_or_statement (parser);
11871 return error_mark_node;
11872 }
11873
11874 /* If the next token is not '}', then there are some enumerators. */
11875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11876 cp_parser_enumerator_list (parser, type);
11877
11878 /* Consume the final '}'. */
11879 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11880
11881 /* Look for trailing attributes to apply to this enumeration, and
11882 apply them if appropriate. */
11883 if (cp_parser_allow_gnu_extensions_p (parser))
11884 {
11885 tree trailing_attr = cp_parser_attributes_opt (parser);
11886 cplus_decl_attributes (&type,
11887 trailing_attr,
11888 (int) ATTR_FLAG_TYPE_IN_PLACE);
11889 }
11890
11891 /* Finish up the enumeration. */
11892 finish_enum (type);
11893
11894 return type;
11895 }
11896
11897 /* Parse an enumerator-list. The enumerators all have the indicated
11898 TYPE.
11899
11900 enumerator-list:
11901 enumerator-definition
11902 enumerator-list , enumerator-definition */
11903
11904 static void
11905 cp_parser_enumerator_list (cp_parser* parser, tree type)
11906 {
11907 while (true)
11908 {
11909 /* Parse an enumerator-definition. */
11910 cp_parser_enumerator_definition (parser, type);
11911
11912 /* If the next token is not a ',', we've reached the end of
11913 the list. */
11914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11915 break;
11916 /* Otherwise, consume the `,' and keep going. */
11917 cp_lexer_consume_token (parser->lexer);
11918 /* If the next token is a `}', there is a trailing comma. */
11919 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11920 {
11921 if (!in_system_header)
11922 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11923 break;
11924 }
11925 }
11926 }
11927
11928 /* Parse an enumerator-definition. The enumerator has the indicated
11929 TYPE.
11930
11931 enumerator-definition:
11932 enumerator
11933 enumerator = constant-expression
11934
11935 enumerator:
11936 identifier */
11937
11938 static void
11939 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11940 {
11941 tree identifier;
11942 tree value;
11943
11944 /* Look for the identifier. */
11945 identifier = cp_parser_identifier (parser);
11946 if (identifier == error_mark_node)
11947 return;
11948
11949 /* If the next token is an '=', then there is an explicit value. */
11950 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11951 {
11952 /* Consume the `=' token. */
11953 cp_lexer_consume_token (parser->lexer);
11954 /* Parse the value. */
11955 value = cp_parser_constant_expression (parser,
11956 /*allow_non_constant_p=*/false,
11957 NULL);
11958 }
11959 else
11960 value = NULL_TREE;
11961
11962 /* Create the enumerator. */
11963 build_enumerator (identifier, value, type);
11964 }
11965
11966 /* Parse a namespace-name.
11967
11968 namespace-name:
11969 original-namespace-name
11970 namespace-alias
11971
11972 Returns the NAMESPACE_DECL for the namespace. */
11973
11974 static tree
11975 cp_parser_namespace_name (cp_parser* parser)
11976 {
11977 tree identifier;
11978 tree namespace_decl;
11979
11980 cp_token *token = cp_lexer_peek_token (parser->lexer);
11981
11982 /* Get the name of the namespace. */
11983 identifier = cp_parser_identifier (parser);
11984 if (identifier == error_mark_node)
11985 return error_mark_node;
11986
11987 /* Look up the identifier in the currently active scope. Look only
11988 for namespaces, due to:
11989
11990 [basic.lookup.udir]
11991
11992 When looking up a namespace-name in a using-directive or alias
11993 definition, only namespace names are considered.
11994
11995 And:
11996
11997 [basic.lookup.qual]
11998
11999 During the lookup of a name preceding the :: scope resolution
12000 operator, object, function, and enumerator names are ignored.
12001
12002 (Note that cp_parser_qualifying_entity only calls this
12003 function if the token after the name is the scope resolution
12004 operator.) */
12005 namespace_decl = cp_parser_lookup_name (parser, identifier,
12006 none_type,
12007 /*is_template=*/false,
12008 /*is_namespace=*/true,
12009 /*check_dependency=*/true,
12010 /*ambiguous_decls=*/NULL,
12011 token->location);
12012 /* If it's not a namespace, issue an error. */
12013 if (namespace_decl == error_mark_node
12014 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12015 {
12016 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12017 error ("%H%qD is not a namespace-name", &token->location, identifier);
12018 cp_parser_error (parser, "expected namespace-name");
12019 namespace_decl = error_mark_node;
12020 }
12021
12022 return namespace_decl;
12023 }
12024
12025 /* Parse a namespace-definition.
12026
12027 namespace-definition:
12028 named-namespace-definition
12029 unnamed-namespace-definition
12030
12031 named-namespace-definition:
12032 original-namespace-definition
12033 extension-namespace-definition
12034
12035 original-namespace-definition:
12036 namespace identifier { namespace-body }
12037
12038 extension-namespace-definition:
12039 namespace original-namespace-name { namespace-body }
12040
12041 unnamed-namespace-definition:
12042 namespace { namespace-body } */
12043
12044 static void
12045 cp_parser_namespace_definition (cp_parser* parser)
12046 {
12047 tree identifier, attribs;
12048 bool has_visibility;
12049 bool is_inline;
12050
12051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12052 {
12053 is_inline = true;
12054 cp_lexer_consume_token (parser->lexer);
12055 }
12056 else
12057 is_inline = false;
12058
12059 /* Look for the `namespace' keyword. */
12060 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12061
12062 /* Get the name of the namespace. We do not attempt to distinguish
12063 between an original-namespace-definition and an
12064 extension-namespace-definition at this point. The semantic
12065 analysis routines are responsible for that. */
12066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12067 identifier = cp_parser_identifier (parser);
12068 else
12069 identifier = NULL_TREE;
12070
12071 /* Parse any specified attributes. */
12072 attribs = cp_parser_attributes_opt (parser);
12073
12074 /* Look for the `{' to start the namespace. */
12075 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12076 /* Start the namespace. */
12077 push_namespace (identifier);
12078
12079 /* "inline namespace" is equivalent to a stub namespace definition
12080 followed by a strong using directive. */
12081 if (is_inline)
12082 {
12083 tree name_space = current_namespace;
12084 /* Set up namespace association. */
12085 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12086 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12087 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12088 /* Import the contents of the inline namespace. */
12089 pop_namespace ();
12090 do_using_directive (name_space);
12091 push_namespace (identifier);
12092 }
12093
12094 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12095
12096 /* Parse the body of the namespace. */
12097 cp_parser_namespace_body (parser);
12098
12099 #ifdef HANDLE_PRAGMA_VISIBILITY
12100 if (has_visibility)
12101 pop_visibility ();
12102 #endif
12103
12104 /* Finish the namespace. */
12105 pop_namespace ();
12106 /* Look for the final `}'. */
12107 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12108 }
12109
12110 /* Parse a namespace-body.
12111
12112 namespace-body:
12113 declaration-seq [opt] */
12114
12115 static void
12116 cp_parser_namespace_body (cp_parser* parser)
12117 {
12118 cp_parser_declaration_seq_opt (parser);
12119 }
12120
12121 /* Parse a namespace-alias-definition.
12122
12123 namespace-alias-definition:
12124 namespace identifier = qualified-namespace-specifier ; */
12125
12126 static void
12127 cp_parser_namespace_alias_definition (cp_parser* parser)
12128 {
12129 tree identifier;
12130 tree namespace_specifier;
12131
12132 cp_token *token = cp_lexer_peek_token (parser->lexer);
12133
12134 /* Look for the `namespace' keyword. */
12135 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12136 /* Look for the identifier. */
12137 identifier = cp_parser_identifier (parser);
12138 if (identifier == error_mark_node)
12139 return;
12140 /* Look for the `=' token. */
12141 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12142 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12143 {
12144 error ("%H%<namespace%> definition is not allowed here", &token->location);
12145 /* Skip the definition. */
12146 cp_lexer_consume_token (parser->lexer);
12147 if (cp_parser_skip_to_closing_brace (parser))
12148 cp_lexer_consume_token (parser->lexer);
12149 return;
12150 }
12151 cp_parser_require (parser, CPP_EQ, "%<=%>");
12152 /* Look for the qualified-namespace-specifier. */
12153 namespace_specifier
12154 = cp_parser_qualified_namespace_specifier (parser);
12155 /* Look for the `;' token. */
12156 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12157
12158 /* Register the alias in the symbol table. */
12159 do_namespace_alias (identifier, namespace_specifier);
12160 }
12161
12162 /* Parse a qualified-namespace-specifier.
12163
12164 qualified-namespace-specifier:
12165 :: [opt] nested-name-specifier [opt] namespace-name
12166
12167 Returns a NAMESPACE_DECL corresponding to the specified
12168 namespace. */
12169
12170 static tree
12171 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12172 {
12173 /* Look for the optional `::'. */
12174 cp_parser_global_scope_opt (parser,
12175 /*current_scope_valid_p=*/false);
12176
12177 /* Look for the optional nested-name-specifier. */
12178 cp_parser_nested_name_specifier_opt (parser,
12179 /*typename_keyword_p=*/false,
12180 /*check_dependency_p=*/true,
12181 /*type_p=*/false,
12182 /*is_declaration=*/true);
12183
12184 return cp_parser_namespace_name (parser);
12185 }
12186
12187 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12188 access declaration.
12189
12190 using-declaration:
12191 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12192 using :: unqualified-id ;
12193
12194 access-declaration:
12195 qualified-id ;
12196
12197 */
12198
12199 static bool
12200 cp_parser_using_declaration (cp_parser* parser,
12201 bool access_declaration_p)
12202 {
12203 cp_token *token;
12204 bool typename_p = false;
12205 bool global_scope_p;
12206 tree decl;
12207 tree identifier;
12208 tree qscope;
12209
12210 if (access_declaration_p)
12211 cp_parser_parse_tentatively (parser);
12212 else
12213 {
12214 /* Look for the `using' keyword. */
12215 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12216
12217 /* Peek at the next token. */
12218 token = cp_lexer_peek_token (parser->lexer);
12219 /* See if it's `typename'. */
12220 if (token->keyword == RID_TYPENAME)
12221 {
12222 /* Remember that we've seen it. */
12223 typename_p = true;
12224 /* Consume the `typename' token. */
12225 cp_lexer_consume_token (parser->lexer);
12226 }
12227 }
12228
12229 /* Look for the optional global scope qualification. */
12230 global_scope_p
12231 = (cp_parser_global_scope_opt (parser,
12232 /*current_scope_valid_p=*/false)
12233 != NULL_TREE);
12234
12235 /* If we saw `typename', or didn't see `::', then there must be a
12236 nested-name-specifier present. */
12237 if (typename_p || !global_scope_p)
12238 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12239 /*check_dependency_p=*/true,
12240 /*type_p=*/false,
12241 /*is_declaration=*/true);
12242 /* Otherwise, we could be in either of the two productions. In that
12243 case, treat the nested-name-specifier as optional. */
12244 else
12245 qscope = cp_parser_nested_name_specifier_opt (parser,
12246 /*typename_keyword_p=*/false,
12247 /*check_dependency_p=*/true,
12248 /*type_p=*/false,
12249 /*is_declaration=*/true);
12250 if (!qscope)
12251 qscope = global_namespace;
12252
12253 if (access_declaration_p && cp_parser_error_occurred (parser))
12254 /* Something has already gone wrong; there's no need to parse
12255 further. Since an error has occurred, the return value of
12256 cp_parser_parse_definitely will be false, as required. */
12257 return cp_parser_parse_definitely (parser);
12258
12259 token = cp_lexer_peek_token (parser->lexer);
12260 /* Parse the unqualified-id. */
12261 identifier = cp_parser_unqualified_id (parser,
12262 /*template_keyword_p=*/false,
12263 /*check_dependency_p=*/true,
12264 /*declarator_p=*/true,
12265 /*optional_p=*/false);
12266
12267 if (access_declaration_p)
12268 {
12269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12270 cp_parser_simulate_error (parser);
12271 if (!cp_parser_parse_definitely (parser))
12272 return false;
12273 }
12274
12275 /* The function we call to handle a using-declaration is different
12276 depending on what scope we are in. */
12277 if (qscope == error_mark_node || identifier == error_mark_node)
12278 ;
12279 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12280 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12281 /* [namespace.udecl]
12282
12283 A using declaration shall not name a template-id. */
12284 error ("%Ha template-id may not appear in a using-declaration",
12285 &token->location);
12286 else
12287 {
12288 if (at_class_scope_p ())
12289 {
12290 /* Create the USING_DECL. */
12291 decl = do_class_using_decl (parser->scope, identifier);
12292
12293 if (check_for_bare_parameter_packs (decl))
12294 return false;
12295 else
12296 /* Add it to the list of members in this class. */
12297 finish_member_declaration (decl);
12298 }
12299 else
12300 {
12301 decl = cp_parser_lookup_name_simple (parser,
12302 identifier,
12303 token->location);
12304 if (decl == error_mark_node)
12305 cp_parser_name_lookup_error (parser, identifier,
12306 decl, NULL,
12307 token->location);
12308 else if (check_for_bare_parameter_packs (decl))
12309 return false;
12310 else if (!at_namespace_scope_p ())
12311 do_local_using_decl (decl, qscope, identifier);
12312 else
12313 do_toplevel_using_decl (decl, qscope, identifier);
12314 }
12315 }
12316
12317 /* Look for the final `;'. */
12318 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12319
12320 return true;
12321 }
12322
12323 /* Parse a using-directive.
12324
12325 using-directive:
12326 using namespace :: [opt] nested-name-specifier [opt]
12327 namespace-name ; */
12328
12329 static void
12330 cp_parser_using_directive (cp_parser* parser)
12331 {
12332 tree namespace_decl;
12333 tree attribs;
12334
12335 /* Look for the `using' keyword. */
12336 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12337 /* And the `namespace' keyword. */
12338 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12339 /* Look for the optional `::' operator. */
12340 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12341 /* And the optional nested-name-specifier. */
12342 cp_parser_nested_name_specifier_opt (parser,
12343 /*typename_keyword_p=*/false,
12344 /*check_dependency_p=*/true,
12345 /*type_p=*/false,
12346 /*is_declaration=*/true);
12347 /* Get the namespace being used. */
12348 namespace_decl = cp_parser_namespace_name (parser);
12349 /* And any specified attributes. */
12350 attribs = cp_parser_attributes_opt (parser);
12351 /* Update the symbol table. */
12352 parse_using_directive (namespace_decl, attribs);
12353 /* Look for the final `;'. */
12354 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12355 }
12356
12357 /* Parse an asm-definition.
12358
12359 asm-definition:
12360 asm ( string-literal ) ;
12361
12362 GNU Extension:
12363
12364 asm-definition:
12365 asm volatile [opt] ( string-literal ) ;
12366 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12368 : asm-operand-list [opt] ) ;
12369 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12370 : asm-operand-list [opt]
12371 : asm-operand-list [opt] ) ; */
12372
12373 static void
12374 cp_parser_asm_definition (cp_parser* parser)
12375 {
12376 tree string;
12377 tree outputs = NULL_TREE;
12378 tree inputs = NULL_TREE;
12379 tree clobbers = NULL_TREE;
12380 tree asm_stmt;
12381 bool volatile_p = false;
12382 bool extended_p = false;
12383 bool invalid_inputs_p = false;
12384 bool invalid_outputs_p = false;
12385
12386 /* Look for the `asm' keyword. */
12387 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12388 /* See if the next token is `volatile'. */
12389 if (cp_parser_allow_gnu_extensions_p (parser)
12390 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12391 {
12392 /* Remember that we saw the `volatile' keyword. */
12393 volatile_p = true;
12394 /* Consume the token. */
12395 cp_lexer_consume_token (parser->lexer);
12396 }
12397 /* Look for the opening `('. */
12398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12399 return;
12400 /* Look for the string. */
12401 string = cp_parser_string_literal (parser, false, false);
12402 if (string == error_mark_node)
12403 {
12404 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12405 /*consume_paren=*/true);
12406 return;
12407 }
12408
12409 /* If we're allowing GNU extensions, check for the extended assembly
12410 syntax. Unfortunately, the `:' tokens need not be separated by
12411 a space in C, and so, for compatibility, we tolerate that here
12412 too. Doing that means that we have to treat the `::' operator as
12413 two `:' tokens. */
12414 if (cp_parser_allow_gnu_extensions_p (parser)
12415 && parser->in_function_body
12416 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12417 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12418 {
12419 bool inputs_p = false;
12420 bool clobbers_p = false;
12421
12422 /* The extended syntax was used. */
12423 extended_p = true;
12424
12425 /* Look for outputs. */
12426 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12427 {
12428 /* Consume the `:'. */
12429 cp_lexer_consume_token (parser->lexer);
12430 /* Parse the output-operands. */
12431 if (cp_lexer_next_token_is_not (parser->lexer,
12432 CPP_COLON)
12433 && cp_lexer_next_token_is_not (parser->lexer,
12434 CPP_SCOPE)
12435 && cp_lexer_next_token_is_not (parser->lexer,
12436 CPP_CLOSE_PAREN))
12437 outputs = cp_parser_asm_operand_list (parser);
12438
12439 if (outputs == error_mark_node)
12440 invalid_outputs_p = true;
12441 }
12442 /* If the next token is `::', there are no outputs, and the
12443 next token is the beginning of the inputs. */
12444 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12445 /* The inputs are coming next. */
12446 inputs_p = true;
12447
12448 /* Look for inputs. */
12449 if (inputs_p
12450 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12451 {
12452 /* Consume the `:' or `::'. */
12453 cp_lexer_consume_token (parser->lexer);
12454 /* Parse the output-operands. */
12455 if (cp_lexer_next_token_is_not (parser->lexer,
12456 CPP_COLON)
12457 && cp_lexer_next_token_is_not (parser->lexer,
12458 CPP_CLOSE_PAREN))
12459 inputs = cp_parser_asm_operand_list (parser);
12460
12461 if (inputs == error_mark_node)
12462 invalid_inputs_p = true;
12463 }
12464 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12465 /* The clobbers are coming next. */
12466 clobbers_p = true;
12467
12468 /* Look for clobbers. */
12469 if (clobbers_p
12470 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12471 {
12472 /* Consume the `:' or `::'. */
12473 cp_lexer_consume_token (parser->lexer);
12474 /* Parse the clobbers. */
12475 if (cp_lexer_next_token_is_not (parser->lexer,
12476 CPP_CLOSE_PAREN))
12477 clobbers = cp_parser_asm_clobber_list (parser);
12478 }
12479 }
12480 /* Look for the closing `)'. */
12481 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12482 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12483 /*consume_paren=*/true);
12484 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12485
12486 if (!invalid_inputs_p && !invalid_outputs_p)
12487 {
12488 /* Create the ASM_EXPR. */
12489 if (parser->in_function_body)
12490 {
12491 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12492 inputs, clobbers);
12493 /* If the extended syntax was not used, mark the ASM_EXPR. */
12494 if (!extended_p)
12495 {
12496 tree temp = asm_stmt;
12497 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12498 temp = TREE_OPERAND (temp, 0);
12499
12500 ASM_INPUT_P (temp) = 1;
12501 }
12502 }
12503 else
12504 cgraph_add_asm_node (string);
12505 }
12506 }
12507
12508 /* Declarators [gram.dcl.decl] */
12509
12510 /* Parse an init-declarator.
12511
12512 init-declarator:
12513 declarator initializer [opt]
12514
12515 GNU Extension:
12516
12517 init-declarator:
12518 declarator asm-specification [opt] attributes [opt] initializer [opt]
12519
12520 function-definition:
12521 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12522 function-body
12523 decl-specifier-seq [opt] declarator function-try-block
12524
12525 GNU Extension:
12526
12527 function-definition:
12528 __extension__ function-definition
12529
12530 The DECL_SPECIFIERS apply to this declarator. Returns a
12531 representation of the entity declared. If MEMBER_P is TRUE, then
12532 this declarator appears in a class scope. The new DECL created by
12533 this declarator is returned.
12534
12535 The CHECKS are access checks that should be performed once we know
12536 what entity is being declared (and, therefore, what classes have
12537 befriended it).
12538
12539 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12540 for a function-definition here as well. If the declarator is a
12541 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12542 be TRUE upon return. By that point, the function-definition will
12543 have been completely parsed.
12544
12545 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12546 is FALSE. */
12547
12548 static tree
12549 cp_parser_init_declarator (cp_parser* parser,
12550 cp_decl_specifier_seq *decl_specifiers,
12551 VEC (deferred_access_check,gc)* checks,
12552 bool function_definition_allowed_p,
12553 bool member_p,
12554 int declares_class_or_enum,
12555 bool* function_definition_p)
12556 {
12557 cp_token *token = NULL, *asm_spec_start_token = NULL,
12558 *attributes_start_token = NULL;
12559 cp_declarator *declarator;
12560 tree prefix_attributes;
12561 tree attributes;
12562 tree asm_specification;
12563 tree initializer;
12564 tree decl = NULL_TREE;
12565 tree scope;
12566 int is_initialized;
12567 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12568 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12569 "(...)". */
12570 enum cpp_ttype initialization_kind;
12571 bool is_direct_init = false;
12572 bool is_non_constant_init;
12573 int ctor_dtor_or_conv_p;
12574 bool friend_p;
12575 tree pushed_scope = NULL;
12576
12577 /* Gather the attributes that were provided with the
12578 decl-specifiers. */
12579 prefix_attributes = decl_specifiers->attributes;
12580
12581 /* Assume that this is not the declarator for a function
12582 definition. */
12583 if (function_definition_p)
12584 *function_definition_p = false;
12585
12586 /* Defer access checks while parsing the declarator; we cannot know
12587 what names are accessible until we know what is being
12588 declared. */
12589 resume_deferring_access_checks ();
12590
12591 /* Parse the declarator. */
12592 token = cp_lexer_peek_token (parser->lexer);
12593 declarator
12594 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12595 &ctor_dtor_or_conv_p,
12596 /*parenthesized_p=*/NULL,
12597 /*member_p=*/false);
12598 /* Gather up the deferred checks. */
12599 stop_deferring_access_checks ();
12600
12601 /* If the DECLARATOR was erroneous, there's no need to go
12602 further. */
12603 if (declarator == cp_error_declarator)
12604 return error_mark_node;
12605
12606 /* Check that the number of template-parameter-lists is OK. */
12607 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12608 token->location))
12609 return error_mark_node;
12610
12611 if (declares_class_or_enum & 2)
12612 cp_parser_check_for_definition_in_return_type (declarator,
12613 decl_specifiers->type,
12614 decl_specifiers->type_location);
12615
12616 /* Figure out what scope the entity declared by the DECLARATOR is
12617 located in. `grokdeclarator' sometimes changes the scope, so
12618 we compute it now. */
12619 scope = get_scope_of_declarator (declarator);
12620
12621 /* If we're allowing GNU extensions, look for an asm-specification
12622 and attributes. */
12623 if (cp_parser_allow_gnu_extensions_p (parser))
12624 {
12625 /* Look for an asm-specification. */
12626 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12627 asm_specification = cp_parser_asm_specification_opt (parser);
12628 /* And attributes. */
12629 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12630 attributes = cp_parser_attributes_opt (parser);
12631 }
12632 else
12633 {
12634 asm_specification = NULL_TREE;
12635 attributes = NULL_TREE;
12636 }
12637
12638 /* Peek at the next token. */
12639 token = cp_lexer_peek_token (parser->lexer);
12640 /* Check to see if the token indicates the start of a
12641 function-definition. */
12642 if (function_declarator_p (declarator)
12643 && cp_parser_token_starts_function_definition_p (token))
12644 {
12645 if (!function_definition_allowed_p)
12646 {
12647 /* If a function-definition should not appear here, issue an
12648 error message. */
12649 cp_parser_error (parser,
12650 "a function-definition is not allowed here");
12651 return error_mark_node;
12652 }
12653 else
12654 {
12655 location_t func_brace_location
12656 = cp_lexer_peek_token (parser->lexer)->location;
12657
12658 /* Neither attributes nor an asm-specification are allowed
12659 on a function-definition. */
12660 if (asm_specification)
12661 error ("%Han asm-specification is not allowed "
12662 "on a function-definition",
12663 &asm_spec_start_token->location);
12664 if (attributes)
12665 error ("%Hattributes are not allowed on a function-definition",
12666 &attributes_start_token->location);
12667 /* This is a function-definition. */
12668 *function_definition_p = true;
12669
12670 /* Parse the function definition. */
12671 if (member_p)
12672 decl = cp_parser_save_member_function_body (parser,
12673 decl_specifiers,
12674 declarator,
12675 prefix_attributes);
12676 else
12677 decl
12678 = (cp_parser_function_definition_from_specifiers_and_declarator
12679 (parser, decl_specifiers, prefix_attributes, declarator));
12680
12681 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12682 {
12683 /* This is where the prologue starts... */
12684 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12685 = func_brace_location;
12686 }
12687
12688 return decl;
12689 }
12690 }
12691
12692 /* [dcl.dcl]
12693
12694 Only in function declarations for constructors, destructors, and
12695 type conversions can the decl-specifier-seq be omitted.
12696
12697 We explicitly postpone this check past the point where we handle
12698 function-definitions because we tolerate function-definitions
12699 that are missing their return types in some modes. */
12700 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12701 {
12702 cp_parser_error (parser,
12703 "expected constructor, destructor, or type conversion");
12704 return error_mark_node;
12705 }
12706
12707 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12708 if (token->type == CPP_EQ
12709 || token->type == CPP_OPEN_PAREN
12710 || token->type == CPP_OPEN_BRACE)
12711 {
12712 is_initialized = SD_INITIALIZED;
12713 initialization_kind = token->type;
12714
12715 if (token->type == CPP_EQ
12716 && function_declarator_p (declarator))
12717 {
12718 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12719 if (t2->keyword == RID_DEFAULT)
12720 is_initialized = SD_DEFAULTED;
12721 else if (t2->keyword == RID_DELETE)
12722 is_initialized = SD_DELETED;
12723 }
12724 }
12725 else
12726 {
12727 /* If the init-declarator isn't initialized and isn't followed by a
12728 `,' or `;', it's not a valid init-declarator. */
12729 if (token->type != CPP_COMMA
12730 && token->type != CPP_SEMICOLON)
12731 {
12732 cp_parser_error (parser, "expected initializer");
12733 return error_mark_node;
12734 }
12735 is_initialized = SD_UNINITIALIZED;
12736 initialization_kind = CPP_EOF;
12737 }
12738
12739 /* Because start_decl has side-effects, we should only call it if we
12740 know we're going ahead. By this point, we know that we cannot
12741 possibly be looking at any other construct. */
12742 cp_parser_commit_to_tentative_parse (parser);
12743
12744 /* If the decl specifiers were bad, issue an error now that we're
12745 sure this was intended to be a declarator. Then continue
12746 declaring the variable(s), as int, to try to cut down on further
12747 errors. */
12748 if (decl_specifiers->any_specifiers_p
12749 && decl_specifiers->type == error_mark_node)
12750 {
12751 cp_parser_error (parser, "invalid type in declaration");
12752 decl_specifiers->type = integer_type_node;
12753 }
12754
12755 /* Check to see whether or not this declaration is a friend. */
12756 friend_p = cp_parser_friend_p (decl_specifiers);
12757
12758 /* Enter the newly declared entry in the symbol table. If we're
12759 processing a declaration in a class-specifier, we wait until
12760 after processing the initializer. */
12761 if (!member_p)
12762 {
12763 if (parser->in_unbraced_linkage_specification_p)
12764 decl_specifiers->storage_class = sc_extern;
12765 decl = start_decl (declarator, decl_specifiers,
12766 is_initialized, attributes, prefix_attributes,
12767 &pushed_scope);
12768 }
12769 else if (scope)
12770 /* Enter the SCOPE. That way unqualified names appearing in the
12771 initializer will be looked up in SCOPE. */
12772 pushed_scope = push_scope (scope);
12773
12774 /* Perform deferred access control checks, now that we know in which
12775 SCOPE the declared entity resides. */
12776 if (!member_p && decl)
12777 {
12778 tree saved_current_function_decl = NULL_TREE;
12779
12780 /* If the entity being declared is a function, pretend that we
12781 are in its scope. If it is a `friend', it may have access to
12782 things that would not otherwise be accessible. */
12783 if (TREE_CODE (decl) == FUNCTION_DECL)
12784 {
12785 saved_current_function_decl = current_function_decl;
12786 current_function_decl = decl;
12787 }
12788
12789 /* Perform access checks for template parameters. */
12790 cp_parser_perform_template_parameter_access_checks (checks);
12791
12792 /* Perform the access control checks for the declarator and the
12793 decl-specifiers. */
12794 perform_deferred_access_checks ();
12795
12796 /* Restore the saved value. */
12797 if (TREE_CODE (decl) == FUNCTION_DECL)
12798 current_function_decl = saved_current_function_decl;
12799 }
12800
12801 /* Parse the initializer. */
12802 initializer = NULL_TREE;
12803 is_direct_init = false;
12804 is_non_constant_init = true;
12805 if (is_initialized)
12806 {
12807 if (function_declarator_p (declarator))
12808 {
12809 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12810 if (initialization_kind == CPP_EQ)
12811 initializer = cp_parser_pure_specifier (parser);
12812 else
12813 {
12814 /* If the declaration was erroneous, we don't really
12815 know what the user intended, so just silently
12816 consume the initializer. */
12817 if (decl != error_mark_node)
12818 error ("%Hinitializer provided for function",
12819 &initializer_start_token->location);
12820 cp_parser_skip_to_closing_parenthesis (parser,
12821 /*recovering=*/true,
12822 /*or_comma=*/false,
12823 /*consume_paren=*/true);
12824 }
12825 }
12826 else
12827 initializer = cp_parser_initializer (parser,
12828 &is_direct_init,
12829 &is_non_constant_init);
12830 }
12831
12832 /* The old parser allows attributes to appear after a parenthesized
12833 initializer. Mark Mitchell proposed removing this functionality
12834 on the GCC mailing lists on 2002-08-13. This parser accepts the
12835 attributes -- but ignores them. */
12836 if (cp_parser_allow_gnu_extensions_p (parser)
12837 && initialization_kind == CPP_OPEN_PAREN)
12838 if (cp_parser_attributes_opt (parser))
12839 warning (OPT_Wattributes,
12840 "attributes after parenthesized initializer ignored");
12841
12842 /* For an in-class declaration, use `grokfield' to create the
12843 declaration. */
12844 if (member_p)
12845 {
12846 if (pushed_scope)
12847 {
12848 pop_scope (pushed_scope);
12849 pushed_scope = false;
12850 }
12851 decl = grokfield (declarator, decl_specifiers,
12852 initializer, !is_non_constant_init,
12853 /*asmspec=*/NULL_TREE,
12854 prefix_attributes);
12855 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12856 cp_parser_save_default_args (parser, decl);
12857 }
12858
12859 /* Finish processing the declaration. But, skip friend
12860 declarations. */
12861 if (!friend_p && decl && decl != error_mark_node)
12862 {
12863 cp_finish_decl (decl,
12864 initializer, !is_non_constant_init,
12865 asm_specification,
12866 /* If the initializer is in parentheses, then this is
12867 a direct-initialization, which means that an
12868 `explicit' constructor is OK. Otherwise, an
12869 `explicit' constructor cannot be used. */
12870 ((is_direct_init || !is_initialized)
12871 ? 0 : LOOKUP_ONLYCONVERTING));
12872 }
12873 else if ((cxx_dialect != cxx98) && friend_p
12874 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12875 /* Core issue #226 (C++0x only): A default template-argument
12876 shall not be specified in a friend class template
12877 declaration. */
12878 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12879 /*is_partial=*/0, /*is_friend_decl=*/1);
12880
12881 if (!friend_p && pushed_scope)
12882 pop_scope (pushed_scope);
12883
12884 return decl;
12885 }
12886
12887 /* Parse a declarator.
12888
12889 declarator:
12890 direct-declarator
12891 ptr-operator declarator
12892
12893 abstract-declarator:
12894 ptr-operator abstract-declarator [opt]
12895 direct-abstract-declarator
12896
12897 GNU Extensions:
12898
12899 declarator:
12900 attributes [opt] direct-declarator
12901 attributes [opt] ptr-operator declarator
12902
12903 abstract-declarator:
12904 attributes [opt] ptr-operator abstract-declarator [opt]
12905 attributes [opt] direct-abstract-declarator
12906
12907 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12908 detect constructor, destructor or conversion operators. It is set
12909 to -1 if the declarator is a name, and +1 if it is a
12910 function. Otherwise it is set to zero. Usually you just want to
12911 test for >0, but internally the negative value is used.
12912
12913 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12914 a decl-specifier-seq unless it declares a constructor, destructor,
12915 or conversion. It might seem that we could check this condition in
12916 semantic analysis, rather than parsing, but that makes it difficult
12917 to handle something like `f()'. We want to notice that there are
12918 no decl-specifiers, and therefore realize that this is an
12919 expression, not a declaration.)
12920
12921 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12922 the declarator is a direct-declarator of the form "(...)".
12923
12924 MEMBER_P is true iff this declarator is a member-declarator. */
12925
12926 static cp_declarator *
12927 cp_parser_declarator (cp_parser* parser,
12928 cp_parser_declarator_kind dcl_kind,
12929 int* ctor_dtor_or_conv_p,
12930 bool* parenthesized_p,
12931 bool member_p)
12932 {
12933 cp_token *token;
12934 cp_declarator *declarator;
12935 enum tree_code code;
12936 cp_cv_quals cv_quals;
12937 tree class_type;
12938 tree attributes = NULL_TREE;
12939
12940 /* Assume this is not a constructor, destructor, or type-conversion
12941 operator. */
12942 if (ctor_dtor_or_conv_p)
12943 *ctor_dtor_or_conv_p = 0;
12944
12945 if (cp_parser_allow_gnu_extensions_p (parser))
12946 attributes = cp_parser_attributes_opt (parser);
12947
12948 /* Peek at the next token. */
12949 token = cp_lexer_peek_token (parser->lexer);
12950
12951 /* Check for the ptr-operator production. */
12952 cp_parser_parse_tentatively (parser);
12953 /* Parse the ptr-operator. */
12954 code = cp_parser_ptr_operator (parser,
12955 &class_type,
12956 &cv_quals);
12957 /* If that worked, then we have a ptr-operator. */
12958 if (cp_parser_parse_definitely (parser))
12959 {
12960 /* If a ptr-operator was found, then this declarator was not
12961 parenthesized. */
12962 if (parenthesized_p)
12963 *parenthesized_p = true;
12964 /* The dependent declarator is optional if we are parsing an
12965 abstract-declarator. */
12966 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12967 cp_parser_parse_tentatively (parser);
12968
12969 /* Parse the dependent declarator. */
12970 declarator = cp_parser_declarator (parser, dcl_kind,
12971 /*ctor_dtor_or_conv_p=*/NULL,
12972 /*parenthesized_p=*/NULL,
12973 /*member_p=*/false);
12974
12975 /* If we are parsing an abstract-declarator, we must handle the
12976 case where the dependent declarator is absent. */
12977 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12978 && !cp_parser_parse_definitely (parser))
12979 declarator = NULL;
12980
12981 declarator = cp_parser_make_indirect_declarator
12982 (code, class_type, cv_quals, declarator);
12983 }
12984 /* Everything else is a direct-declarator. */
12985 else
12986 {
12987 if (parenthesized_p)
12988 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12989 CPP_OPEN_PAREN);
12990 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12991 ctor_dtor_or_conv_p,
12992 member_p);
12993 }
12994
12995 if (attributes && declarator && declarator != cp_error_declarator)
12996 declarator->attributes = attributes;
12997
12998 return declarator;
12999 }
13000
13001 /* Parse a direct-declarator or direct-abstract-declarator.
13002
13003 direct-declarator:
13004 declarator-id
13005 direct-declarator ( parameter-declaration-clause )
13006 cv-qualifier-seq [opt]
13007 exception-specification [opt]
13008 direct-declarator [ constant-expression [opt] ]
13009 ( declarator )
13010
13011 direct-abstract-declarator:
13012 direct-abstract-declarator [opt]
13013 ( parameter-declaration-clause )
13014 cv-qualifier-seq [opt]
13015 exception-specification [opt]
13016 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13017 ( abstract-declarator )
13018
13019 Returns a representation of the declarator. DCL_KIND is
13020 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13021 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13022 we are parsing a direct-declarator. It is
13023 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13024 of ambiguity we prefer an abstract declarator, as per
13025 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13026 cp_parser_declarator. */
13027
13028 static cp_declarator *
13029 cp_parser_direct_declarator (cp_parser* parser,
13030 cp_parser_declarator_kind dcl_kind,
13031 int* ctor_dtor_or_conv_p,
13032 bool member_p)
13033 {
13034 cp_token *token;
13035 cp_declarator *declarator = NULL;
13036 tree scope = NULL_TREE;
13037 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13038 bool saved_in_declarator_p = parser->in_declarator_p;
13039 bool first = true;
13040 tree pushed_scope = NULL_TREE;
13041
13042 while (true)
13043 {
13044 /* Peek at the next token. */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 if (token->type == CPP_OPEN_PAREN)
13047 {
13048 /* This is either a parameter-declaration-clause, or a
13049 parenthesized declarator. When we know we are parsing a
13050 named declarator, it must be a parenthesized declarator
13051 if FIRST is true. For instance, `(int)' is a
13052 parameter-declaration-clause, with an omitted
13053 direct-abstract-declarator. But `((*))', is a
13054 parenthesized abstract declarator. Finally, when T is a
13055 template parameter `(T)' is a
13056 parameter-declaration-clause, and not a parenthesized
13057 named declarator.
13058
13059 We first try and parse a parameter-declaration-clause,
13060 and then try a nested declarator (if FIRST is true).
13061
13062 It is not an error for it not to be a
13063 parameter-declaration-clause, even when FIRST is
13064 false. Consider,
13065
13066 int i (int);
13067 int i (3);
13068
13069 The first is the declaration of a function while the
13070 second is the definition of a variable, including its
13071 initializer.
13072
13073 Having seen only the parenthesis, we cannot know which of
13074 these two alternatives should be selected. Even more
13075 complex are examples like:
13076
13077 int i (int (a));
13078 int i (int (3));
13079
13080 The former is a function-declaration; the latter is a
13081 variable initialization.
13082
13083 Thus again, we try a parameter-declaration-clause, and if
13084 that fails, we back out and return. */
13085
13086 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13087 {
13088 tree params;
13089 unsigned saved_num_template_parameter_lists;
13090 bool is_declarator = false;
13091 tree t;
13092
13093 /* In a member-declarator, the only valid interpretation
13094 of a parenthesis is the start of a
13095 parameter-declaration-clause. (It is invalid to
13096 initialize a static data member with a parenthesized
13097 initializer; only the "=" form of initialization is
13098 permitted.) */
13099 if (!member_p)
13100 cp_parser_parse_tentatively (parser);
13101
13102 /* Consume the `('. */
13103 cp_lexer_consume_token (parser->lexer);
13104 if (first)
13105 {
13106 /* If this is going to be an abstract declarator, we're
13107 in a declarator and we can't have default args. */
13108 parser->default_arg_ok_p = false;
13109 parser->in_declarator_p = true;
13110 }
13111
13112 /* Inside the function parameter list, surrounding
13113 template-parameter-lists do not apply. */
13114 saved_num_template_parameter_lists
13115 = parser->num_template_parameter_lists;
13116 parser->num_template_parameter_lists = 0;
13117
13118 begin_scope (sk_function_parms, NULL_TREE);
13119
13120 /* Parse the parameter-declaration-clause. */
13121 params = cp_parser_parameter_declaration_clause (parser);
13122
13123 parser->num_template_parameter_lists
13124 = saved_num_template_parameter_lists;
13125
13126 /* If all went well, parse the cv-qualifier-seq and the
13127 exception-specification. */
13128 if (member_p || cp_parser_parse_definitely (parser))
13129 {
13130 cp_cv_quals cv_quals;
13131 tree exception_specification;
13132 tree late_return;
13133
13134 is_declarator = true;
13135
13136 if (ctor_dtor_or_conv_p)
13137 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13138 first = false;
13139 /* Consume the `)'. */
13140 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13141
13142 /* Parse the cv-qualifier-seq. */
13143 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13144 /* And the exception-specification. */
13145 exception_specification
13146 = cp_parser_exception_specification_opt (parser);
13147
13148 late_return
13149 = cp_parser_late_return_type_opt (parser);
13150
13151 /* Create the function-declarator. */
13152 declarator = make_call_declarator (declarator,
13153 params,
13154 cv_quals,
13155 exception_specification,
13156 late_return);
13157 /* Any subsequent parameter lists are to do with
13158 return type, so are not those of the declared
13159 function. */
13160 parser->default_arg_ok_p = false;
13161 }
13162
13163 /* Remove the function parms from scope. */
13164 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13165 pop_binding (DECL_NAME (t), t);
13166 leave_scope();
13167
13168 if (is_declarator)
13169 /* Repeat the main loop. */
13170 continue;
13171 }
13172
13173 /* If this is the first, we can try a parenthesized
13174 declarator. */
13175 if (first)
13176 {
13177 bool saved_in_type_id_in_expr_p;
13178
13179 parser->default_arg_ok_p = saved_default_arg_ok_p;
13180 parser->in_declarator_p = saved_in_declarator_p;
13181
13182 /* Consume the `('. */
13183 cp_lexer_consume_token (parser->lexer);
13184 /* Parse the nested declarator. */
13185 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13186 parser->in_type_id_in_expr_p = true;
13187 declarator
13188 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13189 /*parenthesized_p=*/NULL,
13190 member_p);
13191 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13192 first = false;
13193 /* Expect a `)'. */
13194 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13195 declarator = cp_error_declarator;
13196 if (declarator == cp_error_declarator)
13197 break;
13198
13199 goto handle_declarator;
13200 }
13201 /* Otherwise, we must be done. */
13202 else
13203 break;
13204 }
13205 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13206 && token->type == CPP_OPEN_SQUARE)
13207 {
13208 /* Parse an array-declarator. */
13209 tree bounds;
13210
13211 if (ctor_dtor_or_conv_p)
13212 *ctor_dtor_or_conv_p = 0;
13213
13214 first = false;
13215 parser->default_arg_ok_p = false;
13216 parser->in_declarator_p = true;
13217 /* Consume the `['. */
13218 cp_lexer_consume_token (parser->lexer);
13219 /* Peek at the next token. */
13220 token = cp_lexer_peek_token (parser->lexer);
13221 /* If the next token is `]', then there is no
13222 constant-expression. */
13223 if (token->type != CPP_CLOSE_SQUARE)
13224 {
13225 bool non_constant_p;
13226
13227 bounds
13228 = cp_parser_constant_expression (parser,
13229 /*allow_non_constant=*/true,
13230 &non_constant_p);
13231 if (!non_constant_p)
13232 bounds = fold_non_dependent_expr (bounds);
13233 /* Normally, the array bound must be an integral constant
13234 expression. However, as an extension, we allow VLAs
13235 in function scopes. */
13236 else if (!parser->in_function_body)
13237 {
13238 error ("%Harray bound is not an integer constant",
13239 &token->location);
13240 bounds = error_mark_node;
13241 }
13242 }
13243 else
13244 bounds = NULL_TREE;
13245 /* Look for the closing `]'. */
13246 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13247 {
13248 declarator = cp_error_declarator;
13249 break;
13250 }
13251
13252 declarator = make_array_declarator (declarator, bounds);
13253 }
13254 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13255 {
13256 tree qualifying_scope;
13257 tree unqualified_name;
13258 special_function_kind sfk;
13259 bool abstract_ok;
13260 bool pack_expansion_p = false;
13261 cp_token *declarator_id_start_token;
13262
13263 /* Parse a declarator-id */
13264 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13265 if (abstract_ok)
13266 {
13267 cp_parser_parse_tentatively (parser);
13268
13269 /* If we see an ellipsis, we should be looking at a
13270 parameter pack. */
13271 if (token->type == CPP_ELLIPSIS)
13272 {
13273 /* Consume the `...' */
13274 cp_lexer_consume_token (parser->lexer);
13275
13276 pack_expansion_p = true;
13277 }
13278 }
13279
13280 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13281 unqualified_name
13282 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13283 qualifying_scope = parser->scope;
13284 if (abstract_ok)
13285 {
13286 bool okay = false;
13287
13288 if (!unqualified_name && pack_expansion_p)
13289 {
13290 /* Check whether an error occurred. */
13291 okay = !cp_parser_error_occurred (parser);
13292
13293 /* We already consumed the ellipsis to mark a
13294 parameter pack, but we have no way to report it,
13295 so abort the tentative parse. We will be exiting
13296 immediately anyway. */
13297 cp_parser_abort_tentative_parse (parser);
13298 }
13299 else
13300 okay = cp_parser_parse_definitely (parser);
13301
13302 if (!okay)
13303 unqualified_name = error_mark_node;
13304 else if (unqualified_name
13305 && (qualifying_scope
13306 || (TREE_CODE (unqualified_name)
13307 != IDENTIFIER_NODE)))
13308 {
13309 cp_parser_error (parser, "expected unqualified-id");
13310 unqualified_name = error_mark_node;
13311 }
13312 }
13313
13314 if (!unqualified_name)
13315 return NULL;
13316 if (unqualified_name == error_mark_node)
13317 {
13318 declarator = cp_error_declarator;
13319 pack_expansion_p = false;
13320 declarator->parameter_pack_p = false;
13321 break;
13322 }
13323
13324 if (qualifying_scope && at_namespace_scope_p ()
13325 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13326 {
13327 /* In the declaration of a member of a template class
13328 outside of the class itself, the SCOPE will sometimes
13329 be a TYPENAME_TYPE. For example, given:
13330
13331 template <typename T>
13332 int S<T>::R::i = 3;
13333
13334 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13335 this context, we must resolve S<T>::R to an ordinary
13336 type, rather than a typename type.
13337
13338 The reason we normally avoid resolving TYPENAME_TYPEs
13339 is that a specialization of `S' might render
13340 `S<T>::R' not a type. However, if `S' is
13341 specialized, then this `i' will not be used, so there
13342 is no harm in resolving the types here. */
13343 tree type;
13344
13345 /* Resolve the TYPENAME_TYPE. */
13346 type = resolve_typename_type (qualifying_scope,
13347 /*only_current_p=*/false);
13348 /* If that failed, the declarator is invalid. */
13349 if (TREE_CODE (type) == TYPENAME_TYPE)
13350 error ("%H%<%T::%E%> is not a type",
13351 &declarator_id_start_token->location,
13352 TYPE_CONTEXT (qualifying_scope),
13353 TYPE_IDENTIFIER (qualifying_scope));
13354 qualifying_scope = type;
13355 }
13356
13357 sfk = sfk_none;
13358
13359 if (unqualified_name)
13360 {
13361 tree class_type;
13362
13363 if (qualifying_scope
13364 && CLASS_TYPE_P (qualifying_scope))
13365 class_type = qualifying_scope;
13366 else
13367 class_type = current_class_type;
13368
13369 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13370 {
13371 tree name_type = TREE_TYPE (unqualified_name);
13372 if (class_type && same_type_p (name_type, class_type))
13373 {
13374 if (qualifying_scope
13375 && CLASSTYPE_USE_TEMPLATE (name_type))
13376 {
13377 error ("%Hinvalid use of constructor as a template",
13378 &declarator_id_start_token->location);
13379 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13380 "name the constructor in a qualified name",
13381 class_type,
13382 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13383 class_type, name_type);
13384 declarator = cp_error_declarator;
13385 break;
13386 }
13387 else
13388 unqualified_name = constructor_name (class_type);
13389 }
13390 else
13391 {
13392 /* We do not attempt to print the declarator
13393 here because we do not have enough
13394 information about its original syntactic
13395 form. */
13396 cp_parser_error (parser, "invalid declarator");
13397 declarator = cp_error_declarator;
13398 break;
13399 }
13400 }
13401
13402 if (class_type)
13403 {
13404 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13405 sfk = sfk_destructor;
13406 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13407 sfk = sfk_conversion;
13408 else if (/* There's no way to declare a constructor
13409 for an anonymous type, even if the type
13410 got a name for linkage purposes. */
13411 !TYPE_WAS_ANONYMOUS (class_type)
13412 && constructor_name_p (unqualified_name,
13413 class_type))
13414 {
13415 unqualified_name = constructor_name (class_type);
13416 sfk = sfk_constructor;
13417 }
13418
13419 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13420 *ctor_dtor_or_conv_p = -1;
13421 }
13422 }
13423 declarator = make_id_declarator (qualifying_scope,
13424 unqualified_name,
13425 sfk);
13426 declarator->id_loc = token->location;
13427 declarator->parameter_pack_p = pack_expansion_p;
13428
13429 if (pack_expansion_p)
13430 maybe_warn_variadic_templates ();
13431
13432 handle_declarator:;
13433 scope = get_scope_of_declarator (declarator);
13434 if (scope)
13435 /* Any names that appear after the declarator-id for a
13436 member are looked up in the containing scope. */
13437 pushed_scope = push_scope (scope);
13438 parser->in_declarator_p = true;
13439 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13440 || (declarator && declarator->kind == cdk_id))
13441 /* Default args are only allowed on function
13442 declarations. */
13443 parser->default_arg_ok_p = saved_default_arg_ok_p;
13444 else
13445 parser->default_arg_ok_p = false;
13446
13447 first = false;
13448 }
13449 /* We're done. */
13450 else
13451 break;
13452 }
13453
13454 /* For an abstract declarator, we might wind up with nothing at this
13455 point. That's an error; the declarator is not optional. */
13456 if (!declarator)
13457 cp_parser_error (parser, "expected declarator");
13458
13459 /* If we entered a scope, we must exit it now. */
13460 if (pushed_scope)
13461 pop_scope (pushed_scope);
13462
13463 parser->default_arg_ok_p = saved_default_arg_ok_p;
13464 parser->in_declarator_p = saved_in_declarator_p;
13465
13466 return declarator;
13467 }
13468
13469 /* Parse a ptr-operator.
13470
13471 ptr-operator:
13472 * cv-qualifier-seq [opt]
13473 &
13474 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13475
13476 GNU Extension:
13477
13478 ptr-operator:
13479 & cv-qualifier-seq [opt]
13480
13481 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13482 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13483 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13484 filled in with the TYPE containing the member. *CV_QUALS is
13485 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13486 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13487 Note that the tree codes returned by this function have nothing
13488 to do with the types of trees that will be eventually be created
13489 to represent the pointer or reference type being parsed. They are
13490 just constants with suggestive names. */
13491 static enum tree_code
13492 cp_parser_ptr_operator (cp_parser* parser,
13493 tree* type,
13494 cp_cv_quals *cv_quals)
13495 {
13496 enum tree_code code = ERROR_MARK;
13497 cp_token *token;
13498
13499 /* Assume that it's not a pointer-to-member. */
13500 *type = NULL_TREE;
13501 /* And that there are no cv-qualifiers. */
13502 *cv_quals = TYPE_UNQUALIFIED;
13503
13504 /* Peek at the next token. */
13505 token = cp_lexer_peek_token (parser->lexer);
13506
13507 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13508 if (token->type == CPP_MULT)
13509 code = INDIRECT_REF;
13510 else if (token->type == CPP_AND)
13511 code = ADDR_EXPR;
13512 else if ((cxx_dialect != cxx98) &&
13513 token->type == CPP_AND_AND) /* C++0x only */
13514 code = NON_LVALUE_EXPR;
13515
13516 if (code != ERROR_MARK)
13517 {
13518 /* Consume the `*', `&' or `&&'. */
13519 cp_lexer_consume_token (parser->lexer);
13520
13521 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13522 `&', if we are allowing GNU extensions. (The only qualifier
13523 that can legally appear after `&' is `restrict', but that is
13524 enforced during semantic analysis. */
13525 if (code == INDIRECT_REF
13526 || cp_parser_allow_gnu_extensions_p (parser))
13527 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13528 }
13529 else
13530 {
13531 /* Try the pointer-to-member case. */
13532 cp_parser_parse_tentatively (parser);
13533 /* Look for the optional `::' operator. */
13534 cp_parser_global_scope_opt (parser,
13535 /*current_scope_valid_p=*/false);
13536 /* Look for the nested-name specifier. */
13537 token = cp_lexer_peek_token (parser->lexer);
13538 cp_parser_nested_name_specifier (parser,
13539 /*typename_keyword_p=*/false,
13540 /*check_dependency_p=*/true,
13541 /*type_p=*/false,
13542 /*is_declaration=*/false);
13543 /* If we found it, and the next token is a `*', then we are
13544 indeed looking at a pointer-to-member operator. */
13545 if (!cp_parser_error_occurred (parser)
13546 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13547 {
13548 /* Indicate that the `*' operator was used. */
13549 code = INDIRECT_REF;
13550
13551 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13552 error ("%H%qD is a namespace", &token->location, parser->scope);
13553 else
13554 {
13555 /* The type of which the member is a member is given by the
13556 current SCOPE. */
13557 *type = parser->scope;
13558 /* The next name will not be qualified. */
13559 parser->scope = NULL_TREE;
13560 parser->qualifying_scope = NULL_TREE;
13561 parser->object_scope = NULL_TREE;
13562 /* Look for the optional cv-qualifier-seq. */
13563 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13564 }
13565 }
13566 /* If that didn't work we don't have a ptr-operator. */
13567 if (!cp_parser_parse_definitely (parser))
13568 cp_parser_error (parser, "expected ptr-operator");
13569 }
13570
13571 return code;
13572 }
13573
13574 /* Parse an (optional) cv-qualifier-seq.
13575
13576 cv-qualifier-seq:
13577 cv-qualifier cv-qualifier-seq [opt]
13578
13579 cv-qualifier:
13580 const
13581 volatile
13582
13583 GNU Extension:
13584
13585 cv-qualifier:
13586 __restrict__
13587
13588 Returns a bitmask representing the cv-qualifiers. */
13589
13590 static cp_cv_quals
13591 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13592 {
13593 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13594
13595 while (true)
13596 {
13597 cp_token *token;
13598 cp_cv_quals cv_qualifier;
13599
13600 /* Peek at the next token. */
13601 token = cp_lexer_peek_token (parser->lexer);
13602 /* See if it's a cv-qualifier. */
13603 switch (token->keyword)
13604 {
13605 case RID_CONST:
13606 cv_qualifier = TYPE_QUAL_CONST;
13607 break;
13608
13609 case RID_VOLATILE:
13610 cv_qualifier = TYPE_QUAL_VOLATILE;
13611 break;
13612
13613 case RID_RESTRICT:
13614 cv_qualifier = TYPE_QUAL_RESTRICT;
13615 break;
13616
13617 default:
13618 cv_qualifier = TYPE_UNQUALIFIED;
13619 break;
13620 }
13621
13622 if (!cv_qualifier)
13623 break;
13624
13625 if (cv_quals & cv_qualifier)
13626 {
13627 error ("%Hduplicate cv-qualifier", &token->location);
13628 cp_lexer_purge_token (parser->lexer);
13629 }
13630 else
13631 {
13632 cp_lexer_consume_token (parser->lexer);
13633 cv_quals |= cv_qualifier;
13634 }
13635 }
13636
13637 return cv_quals;
13638 }
13639
13640 /* Parse a late-specified return type, if any. This is not a separate
13641 non-terminal, but part of a function declarator, which looks like
13642
13643 -> type-id
13644
13645 Returns the type indicated by the type-id. */
13646
13647 static tree
13648 cp_parser_late_return_type_opt (cp_parser* parser)
13649 {
13650 cp_token *token;
13651
13652 /* Peek at the next token. */
13653 token = cp_lexer_peek_token (parser->lexer);
13654 /* A late-specified return type is indicated by an initial '->'. */
13655 if (token->type != CPP_DEREF)
13656 return NULL_TREE;
13657
13658 /* Consume the ->. */
13659 cp_lexer_consume_token (parser->lexer);
13660
13661 return cp_parser_type_id (parser);
13662 }
13663
13664 /* Parse a declarator-id.
13665
13666 declarator-id:
13667 id-expression
13668 :: [opt] nested-name-specifier [opt] type-name
13669
13670 In the `id-expression' case, the value returned is as for
13671 cp_parser_id_expression if the id-expression was an unqualified-id.
13672 If the id-expression was a qualified-id, then a SCOPE_REF is
13673 returned. The first operand is the scope (either a NAMESPACE_DECL
13674 or TREE_TYPE), but the second is still just a representation of an
13675 unqualified-id. */
13676
13677 static tree
13678 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13679 {
13680 tree id;
13681 /* The expression must be an id-expression. Assume that qualified
13682 names are the names of types so that:
13683
13684 template <class T>
13685 int S<T>::R::i = 3;
13686
13687 will work; we must treat `S<T>::R' as the name of a type.
13688 Similarly, assume that qualified names are templates, where
13689 required, so that:
13690
13691 template <class T>
13692 int S<T>::R<T>::i = 3;
13693
13694 will work, too. */
13695 id = cp_parser_id_expression (parser,
13696 /*template_keyword_p=*/false,
13697 /*check_dependency_p=*/false,
13698 /*template_p=*/NULL,
13699 /*declarator_p=*/true,
13700 optional_p);
13701 if (id && BASELINK_P (id))
13702 id = BASELINK_FUNCTIONS (id);
13703 return id;
13704 }
13705
13706 /* Parse a type-id.
13707
13708 type-id:
13709 type-specifier-seq abstract-declarator [opt]
13710
13711 Returns the TYPE specified. */
13712
13713 static tree
13714 cp_parser_type_id (cp_parser* parser)
13715 {
13716 cp_decl_specifier_seq type_specifier_seq;
13717 cp_declarator *abstract_declarator;
13718
13719 /* Parse the type-specifier-seq. */
13720 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13721 &type_specifier_seq);
13722 if (type_specifier_seq.type == error_mark_node)
13723 return error_mark_node;
13724
13725 /* There might or might not be an abstract declarator. */
13726 cp_parser_parse_tentatively (parser);
13727 /* Look for the declarator. */
13728 abstract_declarator
13729 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13730 /*parenthesized_p=*/NULL,
13731 /*member_p=*/false);
13732 /* Check to see if there really was a declarator. */
13733 if (!cp_parser_parse_definitely (parser))
13734 abstract_declarator = NULL;
13735
13736 if (type_specifier_seq.type
13737 && type_uses_auto (type_specifier_seq.type))
13738 {
13739 error ("invalid use of %<auto%>");
13740 return error_mark_node;
13741 }
13742
13743 return groktypename (&type_specifier_seq, abstract_declarator);
13744 }
13745
13746 /* Parse a type-specifier-seq.
13747
13748 type-specifier-seq:
13749 type-specifier type-specifier-seq [opt]
13750
13751 GNU extension:
13752
13753 type-specifier-seq:
13754 attributes type-specifier-seq [opt]
13755
13756 If IS_CONDITION is true, we are at the start of a "condition",
13757 e.g., we've just seen "if (".
13758
13759 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13760
13761 static void
13762 cp_parser_type_specifier_seq (cp_parser* parser,
13763 bool is_condition,
13764 cp_decl_specifier_seq *type_specifier_seq)
13765 {
13766 bool seen_type_specifier = false;
13767 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13768 cp_token *start_token = NULL;
13769
13770 /* Clear the TYPE_SPECIFIER_SEQ. */
13771 clear_decl_specs (type_specifier_seq);
13772
13773 /* Parse the type-specifiers and attributes. */
13774 while (true)
13775 {
13776 tree type_specifier;
13777 bool is_cv_qualifier;
13778
13779 /* Check for attributes first. */
13780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13781 {
13782 type_specifier_seq->attributes =
13783 chainon (type_specifier_seq->attributes,
13784 cp_parser_attributes_opt (parser));
13785 continue;
13786 }
13787
13788 /* record the token of the beginning of the type specifier seq,
13789 for error reporting purposes*/
13790 if (!start_token)
13791 start_token = cp_lexer_peek_token (parser->lexer);
13792
13793 /* Look for the type-specifier. */
13794 type_specifier = cp_parser_type_specifier (parser,
13795 flags,
13796 type_specifier_seq,
13797 /*is_declaration=*/false,
13798 NULL,
13799 &is_cv_qualifier);
13800 if (!type_specifier)
13801 {
13802 /* If the first type-specifier could not be found, this is not a
13803 type-specifier-seq at all. */
13804 if (!seen_type_specifier)
13805 {
13806 cp_parser_error (parser, "expected type-specifier");
13807 type_specifier_seq->type = error_mark_node;
13808 return;
13809 }
13810 /* If subsequent type-specifiers could not be found, the
13811 type-specifier-seq is complete. */
13812 break;
13813 }
13814
13815 seen_type_specifier = true;
13816 /* The standard says that a condition can be:
13817
13818 type-specifier-seq declarator = assignment-expression
13819
13820 However, given:
13821
13822 struct S {};
13823 if (int S = ...)
13824
13825 we should treat the "S" as a declarator, not as a
13826 type-specifier. The standard doesn't say that explicitly for
13827 type-specifier-seq, but it does say that for
13828 decl-specifier-seq in an ordinary declaration. Perhaps it
13829 would be clearer just to allow a decl-specifier-seq here, and
13830 then add a semantic restriction that if any decl-specifiers
13831 that are not type-specifiers appear, the program is invalid. */
13832 if (is_condition && !is_cv_qualifier)
13833 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13834 }
13835
13836 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13837 }
13838
13839 /* Parse a parameter-declaration-clause.
13840
13841 parameter-declaration-clause:
13842 parameter-declaration-list [opt] ... [opt]
13843 parameter-declaration-list , ...
13844
13845 Returns a representation for the parameter declarations. A return
13846 value of NULL indicates a parameter-declaration-clause consisting
13847 only of an ellipsis. */
13848
13849 static tree
13850 cp_parser_parameter_declaration_clause (cp_parser* parser)
13851 {
13852 tree parameters;
13853 cp_token *token;
13854 bool ellipsis_p;
13855 bool is_error;
13856
13857 /* Peek at the next token. */
13858 token = cp_lexer_peek_token (parser->lexer);
13859 /* Check for trivial parameter-declaration-clauses. */
13860 if (token->type == CPP_ELLIPSIS)
13861 {
13862 /* Consume the `...' token. */
13863 cp_lexer_consume_token (parser->lexer);
13864 return NULL_TREE;
13865 }
13866 else if (token->type == CPP_CLOSE_PAREN)
13867 /* There are no parameters. */
13868 {
13869 #ifndef NO_IMPLICIT_EXTERN_C
13870 if (in_system_header && current_class_type == NULL
13871 && current_lang_name == lang_name_c)
13872 return NULL_TREE;
13873 else
13874 #endif
13875 return void_list_node;
13876 }
13877 /* Check for `(void)', too, which is a special case. */
13878 else if (token->keyword == RID_VOID
13879 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13880 == CPP_CLOSE_PAREN))
13881 {
13882 /* Consume the `void' token. */
13883 cp_lexer_consume_token (parser->lexer);
13884 /* There are no parameters. */
13885 return void_list_node;
13886 }
13887
13888 /* Parse the parameter-declaration-list. */
13889 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13890 /* If a parse error occurred while parsing the
13891 parameter-declaration-list, then the entire
13892 parameter-declaration-clause is erroneous. */
13893 if (is_error)
13894 return NULL;
13895
13896 /* Peek at the next token. */
13897 token = cp_lexer_peek_token (parser->lexer);
13898 /* If it's a `,', the clause should terminate with an ellipsis. */
13899 if (token->type == CPP_COMMA)
13900 {
13901 /* Consume the `,'. */
13902 cp_lexer_consume_token (parser->lexer);
13903 /* Expect an ellipsis. */
13904 ellipsis_p
13905 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13906 }
13907 /* It might also be `...' if the optional trailing `,' was
13908 omitted. */
13909 else if (token->type == CPP_ELLIPSIS)
13910 {
13911 /* Consume the `...' token. */
13912 cp_lexer_consume_token (parser->lexer);
13913 /* And remember that we saw it. */
13914 ellipsis_p = true;
13915 }
13916 else
13917 ellipsis_p = false;
13918
13919 /* Finish the parameter list. */
13920 if (!ellipsis_p)
13921 parameters = chainon (parameters, void_list_node);
13922
13923 return parameters;
13924 }
13925
13926 /* Parse a parameter-declaration-list.
13927
13928 parameter-declaration-list:
13929 parameter-declaration
13930 parameter-declaration-list , parameter-declaration
13931
13932 Returns a representation of the parameter-declaration-list, as for
13933 cp_parser_parameter_declaration_clause. However, the
13934 `void_list_node' is never appended to the list. Upon return,
13935 *IS_ERROR will be true iff an error occurred. */
13936
13937 static tree
13938 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13939 {
13940 tree parameters = NULL_TREE;
13941 tree *tail = &parameters;
13942 bool saved_in_unbraced_linkage_specification_p;
13943
13944 /* Assume all will go well. */
13945 *is_error = false;
13946 /* The special considerations that apply to a function within an
13947 unbraced linkage specifications do not apply to the parameters
13948 to the function. */
13949 saved_in_unbraced_linkage_specification_p
13950 = parser->in_unbraced_linkage_specification_p;
13951 parser->in_unbraced_linkage_specification_p = false;
13952
13953 /* Look for more parameters. */
13954 while (true)
13955 {
13956 cp_parameter_declarator *parameter;
13957 tree decl = error_mark_node;
13958 bool parenthesized_p;
13959 /* Parse the parameter. */
13960 parameter
13961 = cp_parser_parameter_declaration (parser,
13962 /*template_parm_p=*/false,
13963 &parenthesized_p);
13964
13965 /* We don't know yet if the enclosing context is deprecated, so wait
13966 and warn in grokparms if appropriate. */
13967 deprecated_state = DEPRECATED_SUPPRESS;
13968
13969 if (parameter)
13970 decl = grokdeclarator (parameter->declarator,
13971 &parameter->decl_specifiers,
13972 PARM,
13973 parameter->default_argument != NULL_TREE,
13974 &parameter->decl_specifiers.attributes);
13975
13976 deprecated_state = DEPRECATED_NORMAL;
13977
13978 /* If a parse error occurred parsing the parameter declaration,
13979 then the entire parameter-declaration-list is erroneous. */
13980 if (decl == error_mark_node)
13981 {
13982 *is_error = true;
13983 parameters = error_mark_node;
13984 break;
13985 }
13986
13987 if (parameter->decl_specifiers.attributes)
13988 cplus_decl_attributes (&decl,
13989 parameter->decl_specifiers.attributes,
13990 0);
13991 if (DECL_NAME (decl))
13992 decl = pushdecl (decl);
13993
13994 /* Add the new parameter to the list. */
13995 *tail = build_tree_list (parameter->default_argument, decl);
13996 tail = &TREE_CHAIN (*tail);
13997
13998 /* Peek at the next token. */
13999 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14000 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14001 /* These are for Objective-C++ */
14002 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14003 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14004 /* The parameter-declaration-list is complete. */
14005 break;
14006 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14007 {
14008 cp_token *token;
14009
14010 /* Peek at the next token. */
14011 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14012 /* If it's an ellipsis, then the list is complete. */
14013 if (token->type == CPP_ELLIPSIS)
14014 break;
14015 /* Otherwise, there must be more parameters. Consume the
14016 `,'. */
14017 cp_lexer_consume_token (parser->lexer);
14018 /* When parsing something like:
14019
14020 int i(float f, double d)
14021
14022 we can tell after seeing the declaration for "f" that we
14023 are not looking at an initialization of a variable "i",
14024 but rather at the declaration of a function "i".
14025
14026 Due to the fact that the parsing of template arguments
14027 (as specified to a template-id) requires backtracking we
14028 cannot use this technique when inside a template argument
14029 list. */
14030 if (!parser->in_template_argument_list_p
14031 && !parser->in_type_id_in_expr_p
14032 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14033 /* However, a parameter-declaration of the form
14034 "foat(f)" (which is a valid declaration of a
14035 parameter "f") can also be interpreted as an
14036 expression (the conversion of "f" to "float"). */
14037 && !parenthesized_p)
14038 cp_parser_commit_to_tentative_parse (parser);
14039 }
14040 else
14041 {
14042 cp_parser_error (parser, "expected %<,%> or %<...%>");
14043 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14044 cp_parser_skip_to_closing_parenthesis (parser,
14045 /*recovering=*/true,
14046 /*or_comma=*/false,
14047 /*consume_paren=*/false);
14048 break;
14049 }
14050 }
14051
14052 parser->in_unbraced_linkage_specification_p
14053 = saved_in_unbraced_linkage_specification_p;
14054
14055 return parameters;
14056 }
14057
14058 /* Parse a parameter declaration.
14059
14060 parameter-declaration:
14061 decl-specifier-seq ... [opt] declarator
14062 decl-specifier-seq declarator = assignment-expression
14063 decl-specifier-seq ... [opt] abstract-declarator [opt]
14064 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14065
14066 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14067 declares a template parameter. (In that case, a non-nested `>'
14068 token encountered during the parsing of the assignment-expression
14069 is not interpreted as a greater-than operator.)
14070
14071 Returns a representation of the parameter, or NULL if an error
14072 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14073 true iff the declarator is of the form "(p)". */
14074
14075 static cp_parameter_declarator *
14076 cp_parser_parameter_declaration (cp_parser *parser,
14077 bool template_parm_p,
14078 bool *parenthesized_p)
14079 {
14080 int declares_class_or_enum;
14081 bool greater_than_is_operator_p;
14082 cp_decl_specifier_seq decl_specifiers;
14083 cp_declarator *declarator;
14084 tree default_argument;
14085 cp_token *token = NULL, *declarator_token_start = NULL;
14086 const char *saved_message;
14087
14088 /* In a template parameter, `>' is not an operator.
14089
14090 [temp.param]
14091
14092 When parsing a default template-argument for a non-type
14093 template-parameter, the first non-nested `>' is taken as the end
14094 of the template parameter-list rather than a greater-than
14095 operator. */
14096 greater_than_is_operator_p = !template_parm_p;
14097
14098 /* Type definitions may not appear in parameter types. */
14099 saved_message = parser->type_definition_forbidden_message;
14100 parser->type_definition_forbidden_message
14101 = "types may not be defined in parameter types";
14102
14103 /* Parse the declaration-specifiers. */
14104 cp_parser_decl_specifier_seq (parser,
14105 CP_PARSER_FLAGS_NONE,
14106 &decl_specifiers,
14107 &declares_class_or_enum);
14108 /* If an error occurred, there's no reason to attempt to parse the
14109 rest of the declaration. */
14110 if (cp_parser_error_occurred (parser))
14111 {
14112 parser->type_definition_forbidden_message = saved_message;
14113 return NULL;
14114 }
14115
14116 /* Peek at the next token. */
14117 token = cp_lexer_peek_token (parser->lexer);
14118
14119 /* If the next token is a `)', `,', `=', `>', or `...', then there
14120 is no declarator. However, when variadic templates are enabled,
14121 there may be a declarator following `...'. */
14122 if (token->type == CPP_CLOSE_PAREN
14123 || token->type == CPP_COMMA
14124 || token->type == CPP_EQ
14125 || token->type == CPP_GREATER)
14126 {
14127 declarator = NULL;
14128 if (parenthesized_p)
14129 *parenthesized_p = false;
14130 }
14131 /* Otherwise, there should be a declarator. */
14132 else
14133 {
14134 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14135 parser->default_arg_ok_p = false;
14136
14137 /* After seeing a decl-specifier-seq, if the next token is not a
14138 "(", there is no possibility that the code is a valid
14139 expression. Therefore, if parsing tentatively, we commit at
14140 this point. */
14141 if (!parser->in_template_argument_list_p
14142 /* In an expression context, having seen:
14143
14144 (int((char ...
14145
14146 we cannot be sure whether we are looking at a
14147 function-type (taking a "char" as a parameter) or a cast
14148 of some object of type "char" to "int". */
14149 && !parser->in_type_id_in_expr_p
14150 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14151 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14152 cp_parser_commit_to_tentative_parse (parser);
14153 /* Parse the declarator. */
14154 declarator_token_start = token;
14155 declarator = cp_parser_declarator (parser,
14156 CP_PARSER_DECLARATOR_EITHER,
14157 /*ctor_dtor_or_conv_p=*/NULL,
14158 parenthesized_p,
14159 /*member_p=*/false);
14160 parser->default_arg_ok_p = saved_default_arg_ok_p;
14161 /* After the declarator, allow more attributes. */
14162 decl_specifiers.attributes
14163 = chainon (decl_specifiers.attributes,
14164 cp_parser_attributes_opt (parser));
14165 }
14166
14167 /* If the next token is an ellipsis, and we have not seen a
14168 declarator name, and the type of the declarator contains parameter
14169 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14170 a parameter pack expansion expression. Otherwise, leave the
14171 ellipsis for a C-style variadic function. */
14172 token = cp_lexer_peek_token (parser->lexer);
14173 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14174 {
14175 tree type = decl_specifiers.type;
14176
14177 if (type && DECL_P (type))
14178 type = TREE_TYPE (type);
14179
14180 if (type
14181 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14182 && declarator_can_be_parameter_pack (declarator)
14183 && (!declarator || !declarator->parameter_pack_p)
14184 && uses_parameter_packs (type))
14185 {
14186 /* Consume the `...'. */
14187 cp_lexer_consume_token (parser->lexer);
14188 maybe_warn_variadic_templates ();
14189
14190 /* Build a pack expansion type */
14191 if (declarator)
14192 declarator->parameter_pack_p = true;
14193 else
14194 decl_specifiers.type = make_pack_expansion (type);
14195 }
14196 }
14197
14198 /* The restriction on defining new types applies only to the type
14199 of the parameter, not to the default argument. */
14200 parser->type_definition_forbidden_message = saved_message;
14201
14202 /* If the next token is `=', then process a default argument. */
14203 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14204 {
14205 /* Consume the `='. */
14206 cp_lexer_consume_token (parser->lexer);
14207
14208 /* If we are defining a class, then the tokens that make up the
14209 default argument must be saved and processed later. */
14210 if (!template_parm_p && at_class_scope_p ()
14211 && TYPE_BEING_DEFINED (current_class_type))
14212 {
14213 unsigned depth = 0;
14214 int maybe_template_id = 0;
14215 cp_token *first_token;
14216 cp_token *token;
14217
14218 /* Add tokens until we have processed the entire default
14219 argument. We add the range [first_token, token). */
14220 first_token = cp_lexer_peek_token (parser->lexer);
14221 while (true)
14222 {
14223 bool done = false;
14224
14225 /* Peek at the next token. */
14226 token = cp_lexer_peek_token (parser->lexer);
14227 /* What we do depends on what token we have. */
14228 switch (token->type)
14229 {
14230 /* In valid code, a default argument must be
14231 immediately followed by a `,' `)', or `...'. */
14232 case CPP_COMMA:
14233 if (depth == 0 && maybe_template_id)
14234 {
14235 /* If we've seen a '<', we might be in a
14236 template-argument-list. Until Core issue 325 is
14237 resolved, we don't know how this situation ought
14238 to be handled, so try to DTRT. We check whether
14239 what comes after the comma is a valid parameter
14240 declaration list. If it is, then the comma ends
14241 the default argument; otherwise the default
14242 argument continues. */
14243 bool error = false;
14244
14245 /* Set ITALP so cp_parser_parameter_declaration_list
14246 doesn't decide to commit to this parse. */
14247 bool saved_italp = parser->in_template_argument_list_p;
14248 parser->in_template_argument_list_p = true;
14249
14250 cp_parser_parse_tentatively (parser);
14251 cp_lexer_consume_token (parser->lexer);
14252 cp_parser_parameter_declaration_list (parser, &error);
14253 if (!cp_parser_error_occurred (parser) && !error)
14254 done = true;
14255 cp_parser_abort_tentative_parse (parser);
14256
14257 parser->in_template_argument_list_p = saved_italp;
14258 break;
14259 }
14260 case CPP_CLOSE_PAREN:
14261 case CPP_ELLIPSIS:
14262 /* If we run into a non-nested `;', `}', or `]',
14263 then the code is invalid -- but the default
14264 argument is certainly over. */
14265 case CPP_SEMICOLON:
14266 case CPP_CLOSE_BRACE:
14267 case CPP_CLOSE_SQUARE:
14268 if (depth == 0)
14269 done = true;
14270 /* Update DEPTH, if necessary. */
14271 else if (token->type == CPP_CLOSE_PAREN
14272 || token->type == CPP_CLOSE_BRACE
14273 || token->type == CPP_CLOSE_SQUARE)
14274 --depth;
14275 break;
14276
14277 case CPP_OPEN_PAREN:
14278 case CPP_OPEN_SQUARE:
14279 case CPP_OPEN_BRACE:
14280 ++depth;
14281 break;
14282
14283 case CPP_LESS:
14284 if (depth == 0)
14285 /* This might be the comparison operator, or it might
14286 start a template argument list. */
14287 ++maybe_template_id;
14288 break;
14289
14290 case CPP_RSHIFT:
14291 if (cxx_dialect == cxx98)
14292 break;
14293 /* Fall through for C++0x, which treats the `>>'
14294 operator like two `>' tokens in certain
14295 cases. */
14296
14297 case CPP_GREATER:
14298 if (depth == 0)
14299 {
14300 /* This might be an operator, or it might close a
14301 template argument list. But if a previous '<'
14302 started a template argument list, this will have
14303 closed it, so we can't be in one anymore. */
14304 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14305 if (maybe_template_id < 0)
14306 maybe_template_id = 0;
14307 }
14308 break;
14309
14310 /* If we run out of tokens, issue an error message. */
14311 case CPP_EOF:
14312 case CPP_PRAGMA_EOL:
14313 error ("%Hfile ends in default argument", &token->location);
14314 done = true;
14315 break;
14316
14317 case CPP_NAME:
14318 case CPP_SCOPE:
14319 /* In these cases, we should look for template-ids.
14320 For example, if the default argument is
14321 `X<int, double>()', we need to do name lookup to
14322 figure out whether or not `X' is a template; if
14323 so, the `,' does not end the default argument.
14324
14325 That is not yet done. */
14326 break;
14327
14328 default:
14329 break;
14330 }
14331
14332 /* If we've reached the end, stop. */
14333 if (done)
14334 break;
14335
14336 /* Add the token to the token block. */
14337 token = cp_lexer_consume_token (parser->lexer);
14338 }
14339
14340 /* Create a DEFAULT_ARG to represent the unparsed default
14341 argument. */
14342 default_argument = make_node (DEFAULT_ARG);
14343 DEFARG_TOKENS (default_argument)
14344 = cp_token_cache_new (first_token, token);
14345 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14346 }
14347 /* Outside of a class definition, we can just parse the
14348 assignment-expression. */
14349 else
14350 {
14351 token = cp_lexer_peek_token (parser->lexer);
14352 default_argument
14353 = cp_parser_default_argument (parser, template_parm_p);
14354 }
14355
14356 if (!parser->default_arg_ok_p)
14357 {
14358 if (flag_permissive)
14359 warning (0, "deprecated use of default argument for parameter of non-function");
14360 else
14361 {
14362 error ("%Hdefault arguments are only "
14363 "permitted for function parameters",
14364 &token->location);
14365 default_argument = NULL_TREE;
14366 }
14367 }
14368 else if ((declarator && declarator->parameter_pack_p)
14369 || (decl_specifiers.type
14370 && PACK_EXPANSION_P (decl_specifiers.type)))
14371 {
14372 const char* kind = template_parm_p? "template " : "";
14373
14374 /* Find the name of the parameter pack. */
14375 cp_declarator *id_declarator = declarator;
14376 while (id_declarator && id_declarator->kind != cdk_id)
14377 id_declarator = id_declarator->declarator;
14378
14379 if (id_declarator && id_declarator->kind == cdk_id)
14380 error ("%H%sparameter pack %qD cannot have a default argument",
14381 &declarator_token_start->location,
14382 kind, id_declarator->u.id.unqualified_name);
14383 else
14384 error ("%H%sparameter pack cannot have a default argument",
14385 &declarator_token_start->location, kind);
14386
14387 default_argument = NULL_TREE;
14388 }
14389 }
14390 else
14391 default_argument = NULL_TREE;
14392
14393 return make_parameter_declarator (&decl_specifiers,
14394 declarator,
14395 default_argument);
14396 }
14397
14398 /* Parse a default argument and return it.
14399
14400 TEMPLATE_PARM_P is true if this is a default argument for a
14401 non-type template parameter. */
14402 static tree
14403 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14404 {
14405 tree default_argument = NULL_TREE;
14406 bool saved_greater_than_is_operator_p;
14407 bool saved_local_variables_forbidden_p;
14408
14409 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14410 set correctly. */
14411 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14412 parser->greater_than_is_operator_p = !template_parm_p;
14413 /* Local variable names (and the `this' keyword) may not
14414 appear in a default argument. */
14415 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14416 parser->local_variables_forbidden_p = true;
14417 /* The default argument expression may cause implicitly
14418 defined member functions to be synthesized, which will
14419 result in garbage collection. We must treat this
14420 situation as if we were within the body of function so as
14421 to avoid collecting live data on the stack. */
14422 ++function_depth;
14423 /* Parse the assignment-expression. */
14424 if (template_parm_p)
14425 push_deferring_access_checks (dk_no_deferred);
14426 default_argument
14427 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14428 if (template_parm_p)
14429 pop_deferring_access_checks ();
14430 /* Restore saved state. */
14431 --function_depth;
14432 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14433 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14434
14435 return default_argument;
14436 }
14437
14438 /* Parse a function-body.
14439
14440 function-body:
14441 compound_statement */
14442
14443 static void
14444 cp_parser_function_body (cp_parser *parser)
14445 {
14446 cp_parser_compound_statement (parser, NULL, false);
14447 }
14448
14449 /* Parse a ctor-initializer-opt followed by a function-body. Return
14450 true if a ctor-initializer was present. */
14451
14452 static bool
14453 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14454 {
14455 tree body;
14456 bool ctor_initializer_p;
14457
14458 /* Begin the function body. */
14459 body = begin_function_body ();
14460 /* Parse the optional ctor-initializer. */
14461 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14462 /* Parse the function-body. */
14463 cp_parser_function_body (parser);
14464 /* Finish the function body. */
14465 finish_function_body (body);
14466
14467 return ctor_initializer_p;
14468 }
14469
14470 /* Parse an initializer.
14471
14472 initializer:
14473 = initializer-clause
14474 ( expression-list )
14475
14476 Returns an expression representing the initializer. If no
14477 initializer is present, NULL_TREE is returned.
14478
14479 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14480 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14481 set to TRUE if there is no initializer present. If there is an
14482 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14483 is set to true; otherwise it is set to false. */
14484
14485 static tree
14486 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14487 bool* non_constant_p)
14488 {
14489 cp_token *token;
14490 tree init;
14491
14492 /* Peek at the next token. */
14493 token = cp_lexer_peek_token (parser->lexer);
14494
14495 /* Let our caller know whether or not this initializer was
14496 parenthesized. */
14497 *is_direct_init = (token->type != CPP_EQ);
14498 /* Assume that the initializer is constant. */
14499 *non_constant_p = false;
14500
14501 if (token->type == CPP_EQ)
14502 {
14503 /* Consume the `='. */
14504 cp_lexer_consume_token (parser->lexer);
14505 /* Parse the initializer-clause. */
14506 init = cp_parser_initializer_clause (parser, non_constant_p);
14507 }
14508 else if (token->type == CPP_OPEN_PAREN)
14509 init = cp_parser_parenthesized_expression_list (parser, false,
14510 /*cast_p=*/false,
14511 /*allow_expansion_p=*/true,
14512 non_constant_p);
14513 else if (token->type == CPP_OPEN_BRACE)
14514 {
14515 maybe_warn_cpp0x ("extended initializer lists");
14516 init = cp_parser_braced_list (parser, non_constant_p);
14517 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14518 }
14519 else
14520 {
14521 /* Anything else is an error. */
14522 cp_parser_error (parser, "expected initializer");
14523 init = error_mark_node;
14524 }
14525
14526 return init;
14527 }
14528
14529 /* Parse an initializer-clause.
14530
14531 initializer-clause:
14532 assignment-expression
14533 braced-init-list
14534
14535 Returns an expression representing the initializer.
14536
14537 If the `assignment-expression' production is used the value
14538 returned is simply a representation for the expression.
14539
14540 Otherwise, calls cp_parser_braced_list. */
14541
14542 static tree
14543 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14544 {
14545 tree initializer;
14546
14547 /* Assume the expression is constant. */
14548 *non_constant_p = false;
14549
14550 /* If it is not a `{', then we are looking at an
14551 assignment-expression. */
14552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14553 {
14554 initializer
14555 = cp_parser_constant_expression (parser,
14556 /*allow_non_constant_p=*/true,
14557 non_constant_p);
14558 if (!*non_constant_p)
14559 initializer = fold_non_dependent_expr (initializer);
14560 }
14561 else
14562 initializer = cp_parser_braced_list (parser, non_constant_p);
14563
14564 return initializer;
14565 }
14566
14567 /* Parse a brace-enclosed initializer list.
14568
14569 braced-init-list:
14570 { initializer-list , [opt] }
14571 { }
14572
14573 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14574 the elements of the initializer-list (or NULL, if the last
14575 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14576 NULL_TREE. There is no way to detect whether or not the optional
14577 trailing `,' was provided. NON_CONSTANT_P is as for
14578 cp_parser_initializer. */
14579
14580 static tree
14581 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14582 {
14583 tree initializer;
14584
14585 /* Consume the `{' token. */
14586 cp_lexer_consume_token (parser->lexer);
14587 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14588 initializer = make_node (CONSTRUCTOR);
14589 /* If it's not a `}', then there is a non-trivial initializer. */
14590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14591 {
14592 /* Parse the initializer list. */
14593 CONSTRUCTOR_ELTS (initializer)
14594 = cp_parser_initializer_list (parser, non_constant_p);
14595 /* A trailing `,' token is allowed. */
14596 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14597 cp_lexer_consume_token (parser->lexer);
14598 }
14599 /* Now, there should be a trailing `}'. */
14600 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14601 TREE_TYPE (initializer) = init_list_type_node;
14602 return initializer;
14603 }
14604
14605 /* Parse an initializer-list.
14606
14607 initializer-list:
14608 initializer-clause ... [opt]
14609 initializer-list , initializer-clause ... [opt]
14610
14611 GNU Extension:
14612
14613 initializer-list:
14614 identifier : initializer-clause
14615 initializer-list, identifier : initializer-clause
14616
14617 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14618 for the initializer. If the INDEX of the elt is non-NULL, it is the
14619 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14620 as for cp_parser_initializer. */
14621
14622 static VEC(constructor_elt,gc) *
14623 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14624 {
14625 VEC(constructor_elt,gc) *v = NULL;
14626
14627 /* Assume all of the expressions are constant. */
14628 *non_constant_p = false;
14629
14630 /* Parse the rest of the list. */
14631 while (true)
14632 {
14633 cp_token *token;
14634 tree identifier;
14635 tree initializer;
14636 bool clause_non_constant_p;
14637
14638 /* If the next token is an identifier and the following one is a
14639 colon, we are looking at the GNU designated-initializer
14640 syntax. */
14641 if (cp_parser_allow_gnu_extensions_p (parser)
14642 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14643 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14644 {
14645 /* Warn the user that they are using an extension. */
14646 pedwarn (input_location, OPT_pedantic,
14647 "ISO C++ does not allow designated initializers");
14648 /* Consume the identifier. */
14649 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14650 /* Consume the `:'. */
14651 cp_lexer_consume_token (parser->lexer);
14652 }
14653 else
14654 identifier = NULL_TREE;
14655
14656 /* Parse the initializer. */
14657 initializer = cp_parser_initializer_clause (parser,
14658 &clause_non_constant_p);
14659 /* If any clause is non-constant, so is the entire initializer. */
14660 if (clause_non_constant_p)
14661 *non_constant_p = true;
14662
14663 /* If we have an ellipsis, this is an initializer pack
14664 expansion. */
14665 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14666 {
14667 /* Consume the `...'. */
14668 cp_lexer_consume_token (parser->lexer);
14669
14670 /* Turn the initializer into an initializer expansion. */
14671 initializer = make_pack_expansion (initializer);
14672 }
14673
14674 /* Add it to the vector. */
14675 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14676
14677 /* If the next token is not a comma, we have reached the end of
14678 the list. */
14679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14680 break;
14681
14682 /* Peek at the next token. */
14683 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14684 /* If the next token is a `}', then we're still done. An
14685 initializer-clause can have a trailing `,' after the
14686 initializer-list and before the closing `}'. */
14687 if (token->type == CPP_CLOSE_BRACE)
14688 break;
14689
14690 /* Consume the `,' token. */
14691 cp_lexer_consume_token (parser->lexer);
14692 }
14693
14694 return v;
14695 }
14696
14697 /* Classes [gram.class] */
14698
14699 /* Parse a class-name.
14700
14701 class-name:
14702 identifier
14703 template-id
14704
14705 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14706 to indicate that names looked up in dependent types should be
14707 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14708 keyword has been used to indicate that the name that appears next
14709 is a template. TAG_TYPE indicates the explicit tag given before
14710 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14711 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14712 is the class being defined in a class-head.
14713
14714 Returns the TYPE_DECL representing the class. */
14715
14716 static tree
14717 cp_parser_class_name (cp_parser *parser,
14718 bool typename_keyword_p,
14719 bool template_keyword_p,
14720 enum tag_types tag_type,
14721 bool check_dependency_p,
14722 bool class_head_p,
14723 bool is_declaration)
14724 {
14725 tree decl;
14726 tree scope;
14727 bool typename_p;
14728 cp_token *token;
14729 tree identifier = NULL_TREE;
14730
14731 /* All class-names start with an identifier. */
14732 token = cp_lexer_peek_token (parser->lexer);
14733 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14734 {
14735 cp_parser_error (parser, "expected class-name");
14736 return error_mark_node;
14737 }
14738
14739 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14740 to a template-id, so we save it here. */
14741 scope = parser->scope;
14742 if (scope == error_mark_node)
14743 return error_mark_node;
14744
14745 /* Any name names a type if we're following the `typename' keyword
14746 in a qualified name where the enclosing scope is type-dependent. */
14747 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14748 && dependent_type_p (scope));
14749 /* Handle the common case (an identifier, but not a template-id)
14750 efficiently. */
14751 if (token->type == CPP_NAME
14752 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14753 {
14754 cp_token *identifier_token;
14755 bool ambiguous_p;
14756
14757 /* Look for the identifier. */
14758 identifier_token = cp_lexer_peek_token (parser->lexer);
14759 ambiguous_p = identifier_token->ambiguous_p;
14760 identifier = cp_parser_identifier (parser);
14761 /* If the next token isn't an identifier, we are certainly not
14762 looking at a class-name. */
14763 if (identifier == error_mark_node)
14764 decl = error_mark_node;
14765 /* If we know this is a type-name, there's no need to look it
14766 up. */
14767 else if (typename_p)
14768 decl = identifier;
14769 else
14770 {
14771 tree ambiguous_decls;
14772 /* If we already know that this lookup is ambiguous, then
14773 we've already issued an error message; there's no reason
14774 to check again. */
14775 if (ambiguous_p)
14776 {
14777 cp_parser_simulate_error (parser);
14778 return error_mark_node;
14779 }
14780 /* If the next token is a `::', then the name must be a type
14781 name.
14782
14783 [basic.lookup.qual]
14784
14785 During the lookup for a name preceding the :: scope
14786 resolution operator, object, function, and enumerator
14787 names are ignored. */
14788 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14789 tag_type = typename_type;
14790 /* Look up the name. */
14791 decl = cp_parser_lookup_name (parser, identifier,
14792 tag_type,
14793 /*is_template=*/false,
14794 /*is_namespace=*/false,
14795 check_dependency_p,
14796 &ambiguous_decls,
14797 identifier_token->location);
14798 if (ambiguous_decls)
14799 {
14800 error ("%Hreference to %qD is ambiguous",
14801 &identifier_token->location, identifier);
14802 print_candidates (ambiguous_decls);
14803 if (cp_parser_parsing_tentatively (parser))
14804 {
14805 identifier_token->ambiguous_p = true;
14806 cp_parser_simulate_error (parser);
14807 }
14808 return error_mark_node;
14809 }
14810 }
14811 }
14812 else
14813 {
14814 /* Try a template-id. */
14815 decl = cp_parser_template_id (parser, template_keyword_p,
14816 check_dependency_p,
14817 is_declaration);
14818 if (decl == error_mark_node)
14819 return error_mark_node;
14820 }
14821
14822 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14823
14824 /* If this is a typename, create a TYPENAME_TYPE. */
14825 if (typename_p && decl != error_mark_node)
14826 {
14827 decl = make_typename_type (scope, decl, typename_type,
14828 /*complain=*/tf_error);
14829 if (decl != error_mark_node)
14830 decl = TYPE_NAME (decl);
14831 }
14832
14833 /* Check to see that it is really the name of a class. */
14834 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14835 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14836 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14837 /* Situations like this:
14838
14839 template <typename T> struct A {
14840 typename T::template X<int>::I i;
14841 };
14842
14843 are problematic. Is `T::template X<int>' a class-name? The
14844 standard does not seem to be definitive, but there is no other
14845 valid interpretation of the following `::'. Therefore, those
14846 names are considered class-names. */
14847 {
14848 decl = make_typename_type (scope, decl, tag_type, tf_error);
14849 if (decl != error_mark_node)
14850 decl = TYPE_NAME (decl);
14851 }
14852 else if (TREE_CODE (decl) != TYPE_DECL
14853 || TREE_TYPE (decl) == error_mark_node
14854 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14855 decl = error_mark_node;
14856
14857 if (decl == error_mark_node)
14858 cp_parser_error (parser, "expected class-name");
14859 else if (identifier && !parser->scope)
14860 maybe_note_name_used_in_class (identifier, decl);
14861
14862 return decl;
14863 }
14864
14865 /* Parse a class-specifier.
14866
14867 class-specifier:
14868 class-head { member-specification [opt] }
14869
14870 Returns the TREE_TYPE representing the class. */
14871
14872 static tree
14873 cp_parser_class_specifier (cp_parser* parser)
14874 {
14875 cp_token *token;
14876 tree type;
14877 tree attributes = NULL_TREE;
14878 int has_trailing_semicolon;
14879 bool nested_name_specifier_p;
14880 unsigned saved_num_template_parameter_lists;
14881 bool saved_in_function_body;
14882 bool saved_in_unbraced_linkage_specification_p;
14883 tree old_scope = NULL_TREE;
14884 tree scope = NULL_TREE;
14885 tree bases;
14886
14887 push_deferring_access_checks (dk_no_deferred);
14888
14889 /* Parse the class-head. */
14890 type = cp_parser_class_head (parser,
14891 &nested_name_specifier_p,
14892 &attributes,
14893 &bases);
14894 /* If the class-head was a semantic disaster, skip the entire body
14895 of the class. */
14896 if (!type)
14897 {
14898 cp_parser_skip_to_end_of_block_or_statement (parser);
14899 pop_deferring_access_checks ();
14900 return error_mark_node;
14901 }
14902
14903 /* Look for the `{'. */
14904 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14905 {
14906 pop_deferring_access_checks ();
14907 return error_mark_node;
14908 }
14909
14910 /* Process the base classes. If they're invalid, skip the
14911 entire class body. */
14912 if (!xref_basetypes (type, bases))
14913 {
14914 /* Consuming the closing brace yields better error messages
14915 later on. */
14916 if (cp_parser_skip_to_closing_brace (parser))
14917 cp_lexer_consume_token (parser->lexer);
14918 pop_deferring_access_checks ();
14919 return error_mark_node;
14920 }
14921
14922 /* Issue an error message if type-definitions are forbidden here. */
14923 cp_parser_check_type_definition (parser);
14924 /* Remember that we are defining one more class. */
14925 ++parser->num_classes_being_defined;
14926 /* Inside the class, surrounding template-parameter-lists do not
14927 apply. */
14928 saved_num_template_parameter_lists
14929 = parser->num_template_parameter_lists;
14930 parser->num_template_parameter_lists = 0;
14931 /* We are not in a function body. */
14932 saved_in_function_body = parser->in_function_body;
14933 parser->in_function_body = false;
14934 /* We are not immediately inside an extern "lang" block. */
14935 saved_in_unbraced_linkage_specification_p
14936 = parser->in_unbraced_linkage_specification_p;
14937 parser->in_unbraced_linkage_specification_p = false;
14938
14939 /* Start the class. */
14940 if (nested_name_specifier_p)
14941 {
14942 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14943 old_scope = push_inner_scope (scope);
14944 }
14945 type = begin_class_definition (type, attributes);
14946
14947 if (type == error_mark_node)
14948 /* If the type is erroneous, skip the entire body of the class. */
14949 cp_parser_skip_to_closing_brace (parser);
14950 else
14951 /* Parse the member-specification. */
14952 cp_parser_member_specification_opt (parser);
14953
14954 /* Look for the trailing `}'. */
14955 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14956 /* We get better error messages by noticing a common problem: a
14957 missing trailing `;'. */
14958 token = cp_lexer_peek_token (parser->lexer);
14959 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14960 /* Look for trailing attributes to apply to this class. */
14961 if (cp_parser_allow_gnu_extensions_p (parser))
14962 attributes = cp_parser_attributes_opt (parser);
14963 if (type != error_mark_node)
14964 type = finish_struct (type, attributes);
14965 if (nested_name_specifier_p)
14966 pop_inner_scope (old_scope, scope);
14967 /* If this class is not itself within the scope of another class,
14968 then we need to parse the bodies of all of the queued function
14969 definitions. Note that the queued functions defined in a class
14970 are not always processed immediately following the
14971 class-specifier for that class. Consider:
14972
14973 struct A {
14974 struct B { void f() { sizeof (A); } };
14975 };
14976
14977 If `f' were processed before the processing of `A' were
14978 completed, there would be no way to compute the size of `A'.
14979 Note that the nesting we are interested in here is lexical --
14980 not the semantic nesting given by TYPE_CONTEXT. In particular,
14981 for:
14982
14983 struct A { struct B; };
14984 struct A::B { void f() { } };
14985
14986 there is no need to delay the parsing of `A::B::f'. */
14987 if (--parser->num_classes_being_defined == 0)
14988 {
14989 tree queue_entry;
14990 tree fn;
14991 tree class_type = NULL_TREE;
14992 tree pushed_scope = NULL_TREE;
14993
14994 /* In a first pass, parse default arguments to the functions.
14995 Then, in a second pass, parse the bodies of the functions.
14996 This two-phased approach handles cases like:
14997
14998 struct S {
14999 void f() { g(); }
15000 void g(int i = 3);
15001 };
15002
15003 */
15004 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15005 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15006 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15007 TREE_PURPOSE (parser->unparsed_functions_queues)
15008 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15009 {
15010 fn = TREE_VALUE (queue_entry);
15011 /* If there are default arguments that have not yet been processed,
15012 take care of them now. */
15013 if (class_type != TREE_PURPOSE (queue_entry))
15014 {
15015 if (pushed_scope)
15016 pop_scope (pushed_scope);
15017 class_type = TREE_PURPOSE (queue_entry);
15018 pushed_scope = push_scope (class_type);
15019 }
15020 /* Make sure that any template parameters are in scope. */
15021 maybe_begin_member_template_processing (fn);
15022 /* Parse the default argument expressions. */
15023 cp_parser_late_parsing_default_args (parser, fn);
15024 /* Remove any template parameters from the symbol table. */
15025 maybe_end_member_template_processing ();
15026 }
15027 if (pushed_scope)
15028 pop_scope (pushed_scope);
15029 /* Now parse the body of the functions. */
15030 for (TREE_VALUE (parser->unparsed_functions_queues)
15031 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15032 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15033 TREE_VALUE (parser->unparsed_functions_queues)
15034 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15035 {
15036 /* Figure out which function we need to process. */
15037 fn = TREE_VALUE (queue_entry);
15038 /* Parse the function. */
15039 cp_parser_late_parsing_for_member (parser, fn);
15040 }
15041 }
15042
15043 /* Put back any saved access checks. */
15044 pop_deferring_access_checks ();
15045
15046 /* Restore saved state. */
15047 parser->in_function_body = saved_in_function_body;
15048 parser->num_template_parameter_lists
15049 = saved_num_template_parameter_lists;
15050 parser->in_unbraced_linkage_specification_p
15051 = saved_in_unbraced_linkage_specification_p;
15052
15053 return type;
15054 }
15055
15056 /* Parse a class-head.
15057
15058 class-head:
15059 class-key identifier [opt] base-clause [opt]
15060 class-key nested-name-specifier identifier base-clause [opt]
15061 class-key nested-name-specifier [opt] template-id
15062 base-clause [opt]
15063
15064 GNU Extensions:
15065 class-key attributes identifier [opt] base-clause [opt]
15066 class-key attributes nested-name-specifier identifier base-clause [opt]
15067 class-key attributes nested-name-specifier [opt] template-id
15068 base-clause [opt]
15069
15070 Upon return BASES is initialized to the list of base classes (or
15071 NULL, if there are none) in the same form returned by
15072 cp_parser_base_clause.
15073
15074 Returns the TYPE of the indicated class. Sets
15075 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15076 involving a nested-name-specifier was used, and FALSE otherwise.
15077
15078 Returns error_mark_node if this is not a class-head.
15079
15080 Returns NULL_TREE if the class-head is syntactically valid, but
15081 semantically invalid in a way that means we should skip the entire
15082 body of the class. */
15083
15084 static tree
15085 cp_parser_class_head (cp_parser* parser,
15086 bool* nested_name_specifier_p,
15087 tree *attributes_p,
15088 tree *bases)
15089 {
15090 tree nested_name_specifier;
15091 enum tag_types class_key;
15092 tree id = NULL_TREE;
15093 tree type = NULL_TREE;
15094 tree attributes;
15095 bool template_id_p = false;
15096 bool qualified_p = false;
15097 bool invalid_nested_name_p = false;
15098 bool invalid_explicit_specialization_p = false;
15099 tree pushed_scope = NULL_TREE;
15100 unsigned num_templates;
15101 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15102 /* Assume no nested-name-specifier will be present. */
15103 *nested_name_specifier_p = false;
15104 /* Assume no template parameter lists will be used in defining the
15105 type. */
15106 num_templates = 0;
15107
15108 *bases = NULL_TREE;
15109
15110 /* Look for the class-key. */
15111 class_key = cp_parser_class_key (parser);
15112 if (class_key == none_type)
15113 return error_mark_node;
15114
15115 /* Parse the attributes. */
15116 attributes = cp_parser_attributes_opt (parser);
15117
15118 /* If the next token is `::', that is invalid -- but sometimes
15119 people do try to write:
15120
15121 struct ::S {};
15122
15123 Handle this gracefully by accepting the extra qualifier, and then
15124 issuing an error about it later if this really is a
15125 class-head. If it turns out just to be an elaborated type
15126 specifier, remain silent. */
15127 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15128 qualified_p = true;
15129
15130 push_deferring_access_checks (dk_no_check);
15131
15132 /* Determine the name of the class. Begin by looking for an
15133 optional nested-name-specifier. */
15134 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15135 nested_name_specifier
15136 = cp_parser_nested_name_specifier_opt (parser,
15137 /*typename_keyword_p=*/false,
15138 /*check_dependency_p=*/false,
15139 /*type_p=*/false,
15140 /*is_declaration=*/false);
15141 /* If there was a nested-name-specifier, then there *must* be an
15142 identifier. */
15143 if (nested_name_specifier)
15144 {
15145 type_start_token = cp_lexer_peek_token (parser->lexer);
15146 /* Although the grammar says `identifier', it really means
15147 `class-name' or `template-name'. You are only allowed to
15148 define a class that has already been declared with this
15149 syntax.
15150
15151 The proposed resolution for Core Issue 180 says that wherever
15152 you see `class T::X' you should treat `X' as a type-name.
15153
15154 It is OK to define an inaccessible class; for example:
15155
15156 class A { class B; };
15157 class A::B {};
15158
15159 We do not know if we will see a class-name, or a
15160 template-name. We look for a class-name first, in case the
15161 class-name is a template-id; if we looked for the
15162 template-name first we would stop after the template-name. */
15163 cp_parser_parse_tentatively (parser);
15164 type = cp_parser_class_name (parser,
15165 /*typename_keyword_p=*/false,
15166 /*template_keyword_p=*/false,
15167 class_type,
15168 /*check_dependency_p=*/false,
15169 /*class_head_p=*/true,
15170 /*is_declaration=*/false);
15171 /* If that didn't work, ignore the nested-name-specifier. */
15172 if (!cp_parser_parse_definitely (parser))
15173 {
15174 invalid_nested_name_p = true;
15175 type_start_token = cp_lexer_peek_token (parser->lexer);
15176 id = cp_parser_identifier (parser);
15177 if (id == error_mark_node)
15178 id = NULL_TREE;
15179 }
15180 /* If we could not find a corresponding TYPE, treat this
15181 declaration like an unqualified declaration. */
15182 if (type == error_mark_node)
15183 nested_name_specifier = NULL_TREE;
15184 /* Otherwise, count the number of templates used in TYPE and its
15185 containing scopes. */
15186 else
15187 {
15188 tree scope;
15189
15190 for (scope = TREE_TYPE (type);
15191 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15192 scope = (TYPE_P (scope)
15193 ? TYPE_CONTEXT (scope)
15194 : DECL_CONTEXT (scope)))
15195 if (TYPE_P (scope)
15196 && CLASS_TYPE_P (scope)
15197 && CLASSTYPE_TEMPLATE_INFO (scope)
15198 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15199 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15200 ++num_templates;
15201 }
15202 }
15203 /* Otherwise, the identifier is optional. */
15204 else
15205 {
15206 /* We don't know whether what comes next is a template-id,
15207 an identifier, or nothing at all. */
15208 cp_parser_parse_tentatively (parser);
15209 /* Check for a template-id. */
15210 type_start_token = cp_lexer_peek_token (parser->lexer);
15211 id = cp_parser_template_id (parser,
15212 /*template_keyword_p=*/false,
15213 /*check_dependency_p=*/true,
15214 /*is_declaration=*/true);
15215 /* If that didn't work, it could still be an identifier. */
15216 if (!cp_parser_parse_definitely (parser))
15217 {
15218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15219 {
15220 type_start_token = cp_lexer_peek_token (parser->lexer);
15221 id = cp_parser_identifier (parser);
15222 }
15223 else
15224 id = NULL_TREE;
15225 }
15226 else
15227 {
15228 template_id_p = true;
15229 ++num_templates;
15230 }
15231 }
15232
15233 pop_deferring_access_checks ();
15234
15235 if (id)
15236 cp_parser_check_for_invalid_template_id (parser, id,
15237 type_start_token->location);
15238
15239 /* If it's not a `:' or a `{' then we can't really be looking at a
15240 class-head, since a class-head only appears as part of a
15241 class-specifier. We have to detect this situation before calling
15242 xref_tag, since that has irreversible side-effects. */
15243 if (!cp_parser_next_token_starts_class_definition_p (parser))
15244 {
15245 cp_parser_error (parser, "expected %<{%> or %<:%>");
15246 return error_mark_node;
15247 }
15248
15249 /* At this point, we're going ahead with the class-specifier, even
15250 if some other problem occurs. */
15251 cp_parser_commit_to_tentative_parse (parser);
15252 /* Issue the error about the overly-qualified name now. */
15253 if (qualified_p)
15254 {
15255 cp_parser_error (parser,
15256 "global qualification of class name is invalid");
15257 return error_mark_node;
15258 }
15259 else if (invalid_nested_name_p)
15260 {
15261 cp_parser_error (parser,
15262 "qualified name does not name a class");
15263 return error_mark_node;
15264 }
15265 else if (nested_name_specifier)
15266 {
15267 tree scope;
15268
15269 /* Reject typedef-names in class heads. */
15270 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15271 {
15272 error ("%Hinvalid class name in declaration of %qD",
15273 &type_start_token->location, type);
15274 type = NULL_TREE;
15275 goto done;
15276 }
15277
15278 /* Figure out in what scope the declaration is being placed. */
15279 scope = current_scope ();
15280 /* If that scope does not contain the scope in which the
15281 class was originally declared, the program is invalid. */
15282 if (scope && !is_ancestor (scope, nested_name_specifier))
15283 {
15284 if (at_namespace_scope_p ())
15285 error ("%Hdeclaration of %qD in namespace %qD which does not "
15286 "enclose %qD",
15287 &type_start_token->location,
15288 type, scope, nested_name_specifier);
15289 else
15290 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15291 &type_start_token->location,
15292 type, scope, nested_name_specifier);
15293 type = NULL_TREE;
15294 goto done;
15295 }
15296 /* [dcl.meaning]
15297
15298 A declarator-id shall not be qualified except for the
15299 definition of a ... nested class outside of its class
15300 ... [or] the definition or explicit instantiation of a
15301 class member of a namespace outside of its namespace. */
15302 if (scope == nested_name_specifier)
15303 {
15304 permerror (input_location, "%Hextra qualification not allowed",
15305 &nested_name_specifier_token_start->location);
15306 nested_name_specifier = NULL_TREE;
15307 num_templates = 0;
15308 }
15309 }
15310 /* An explicit-specialization must be preceded by "template <>". If
15311 it is not, try to recover gracefully. */
15312 if (at_namespace_scope_p ()
15313 && parser->num_template_parameter_lists == 0
15314 && template_id_p)
15315 {
15316 error ("%Han explicit specialization must be preceded by %<template <>%>",
15317 &type_start_token->location);
15318 invalid_explicit_specialization_p = true;
15319 /* Take the same action that would have been taken by
15320 cp_parser_explicit_specialization. */
15321 ++parser->num_template_parameter_lists;
15322 begin_specialization ();
15323 }
15324 /* There must be no "return" statements between this point and the
15325 end of this function; set "type "to the correct return value and
15326 use "goto done;" to return. */
15327 /* Make sure that the right number of template parameters were
15328 present. */
15329 if (!cp_parser_check_template_parameters (parser, num_templates,
15330 type_start_token->location))
15331 {
15332 /* If something went wrong, there is no point in even trying to
15333 process the class-definition. */
15334 type = NULL_TREE;
15335 goto done;
15336 }
15337
15338 /* Look up the type. */
15339 if (template_id_p)
15340 {
15341 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15342 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15343 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15344 {
15345 error ("%Hfunction template %qD redeclared as a class template",
15346 &type_start_token->location, id);
15347 type = error_mark_node;
15348 }
15349 else
15350 {
15351 type = TREE_TYPE (id);
15352 type = maybe_process_partial_specialization (type);
15353 }
15354 if (nested_name_specifier)
15355 pushed_scope = push_scope (nested_name_specifier);
15356 }
15357 else if (nested_name_specifier)
15358 {
15359 tree class_type;
15360
15361 /* Given:
15362
15363 template <typename T> struct S { struct T };
15364 template <typename T> struct S<T>::T { };
15365
15366 we will get a TYPENAME_TYPE when processing the definition of
15367 `S::T'. We need to resolve it to the actual type before we
15368 try to define it. */
15369 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15370 {
15371 class_type = resolve_typename_type (TREE_TYPE (type),
15372 /*only_current_p=*/false);
15373 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15374 type = TYPE_NAME (class_type);
15375 else
15376 {
15377 cp_parser_error (parser, "could not resolve typename type");
15378 type = error_mark_node;
15379 }
15380 }
15381
15382 if (maybe_process_partial_specialization (TREE_TYPE (type))
15383 == error_mark_node)
15384 {
15385 type = NULL_TREE;
15386 goto done;
15387 }
15388
15389 class_type = current_class_type;
15390 /* Enter the scope indicated by the nested-name-specifier. */
15391 pushed_scope = push_scope (nested_name_specifier);
15392 /* Get the canonical version of this type. */
15393 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15394 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15395 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15396 {
15397 type = push_template_decl (type);
15398 if (type == error_mark_node)
15399 {
15400 type = NULL_TREE;
15401 goto done;
15402 }
15403 }
15404
15405 type = TREE_TYPE (type);
15406 *nested_name_specifier_p = true;
15407 }
15408 else /* The name is not a nested name. */
15409 {
15410 /* If the class was unnamed, create a dummy name. */
15411 if (!id)
15412 id = make_anon_name ();
15413 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15414 parser->num_template_parameter_lists);
15415 }
15416
15417 /* Indicate whether this class was declared as a `class' or as a
15418 `struct'. */
15419 if (TREE_CODE (type) == RECORD_TYPE)
15420 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15421 cp_parser_check_class_key (class_key, type);
15422
15423 /* If this type was already complete, and we see another definition,
15424 that's an error. */
15425 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15426 {
15427 error ("%Hredefinition of %q#T",
15428 &type_start_token->location, type);
15429 error ("%Hprevious definition of %q+#T",
15430 &type_start_token->location, type);
15431 type = NULL_TREE;
15432 goto done;
15433 }
15434 else if (type == error_mark_node)
15435 type = NULL_TREE;
15436
15437 /* We will have entered the scope containing the class; the names of
15438 base classes should be looked up in that context. For example:
15439
15440 struct A { struct B {}; struct C; };
15441 struct A::C : B {};
15442
15443 is valid. */
15444
15445 /* Get the list of base-classes, if there is one. */
15446 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15447 *bases = cp_parser_base_clause (parser);
15448
15449 done:
15450 /* Leave the scope given by the nested-name-specifier. We will
15451 enter the class scope itself while processing the members. */
15452 if (pushed_scope)
15453 pop_scope (pushed_scope);
15454
15455 if (invalid_explicit_specialization_p)
15456 {
15457 end_specialization ();
15458 --parser->num_template_parameter_lists;
15459 }
15460 *attributes_p = attributes;
15461 return type;
15462 }
15463
15464 /* Parse a class-key.
15465
15466 class-key:
15467 class
15468 struct
15469 union
15470
15471 Returns the kind of class-key specified, or none_type to indicate
15472 error. */
15473
15474 static enum tag_types
15475 cp_parser_class_key (cp_parser* parser)
15476 {
15477 cp_token *token;
15478 enum tag_types tag_type;
15479
15480 /* Look for the class-key. */
15481 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15482 if (!token)
15483 return none_type;
15484
15485 /* Check to see if the TOKEN is a class-key. */
15486 tag_type = cp_parser_token_is_class_key (token);
15487 if (!tag_type)
15488 cp_parser_error (parser, "expected class-key");
15489 return tag_type;
15490 }
15491
15492 /* Parse an (optional) member-specification.
15493
15494 member-specification:
15495 member-declaration member-specification [opt]
15496 access-specifier : member-specification [opt] */
15497
15498 static void
15499 cp_parser_member_specification_opt (cp_parser* parser)
15500 {
15501 while (true)
15502 {
15503 cp_token *token;
15504 enum rid keyword;
15505
15506 /* Peek at the next token. */
15507 token = cp_lexer_peek_token (parser->lexer);
15508 /* If it's a `}', or EOF then we've seen all the members. */
15509 if (token->type == CPP_CLOSE_BRACE
15510 || token->type == CPP_EOF
15511 || token->type == CPP_PRAGMA_EOL)
15512 break;
15513
15514 /* See if this token is a keyword. */
15515 keyword = token->keyword;
15516 switch (keyword)
15517 {
15518 case RID_PUBLIC:
15519 case RID_PROTECTED:
15520 case RID_PRIVATE:
15521 /* Consume the access-specifier. */
15522 cp_lexer_consume_token (parser->lexer);
15523 /* Remember which access-specifier is active. */
15524 current_access_specifier = token->u.value;
15525 /* Look for the `:'. */
15526 cp_parser_require (parser, CPP_COLON, "%<:%>");
15527 break;
15528
15529 default:
15530 /* Accept #pragmas at class scope. */
15531 if (token->type == CPP_PRAGMA)
15532 {
15533 cp_parser_pragma (parser, pragma_external);
15534 break;
15535 }
15536
15537 /* Otherwise, the next construction must be a
15538 member-declaration. */
15539 cp_parser_member_declaration (parser);
15540 }
15541 }
15542 }
15543
15544 /* Parse a member-declaration.
15545
15546 member-declaration:
15547 decl-specifier-seq [opt] member-declarator-list [opt] ;
15548 function-definition ; [opt]
15549 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15550 using-declaration
15551 template-declaration
15552
15553 member-declarator-list:
15554 member-declarator
15555 member-declarator-list , member-declarator
15556
15557 member-declarator:
15558 declarator pure-specifier [opt]
15559 declarator constant-initializer [opt]
15560 identifier [opt] : constant-expression
15561
15562 GNU Extensions:
15563
15564 member-declaration:
15565 __extension__ member-declaration
15566
15567 member-declarator:
15568 declarator attributes [opt] pure-specifier [opt]
15569 declarator attributes [opt] constant-initializer [opt]
15570 identifier [opt] attributes [opt] : constant-expression
15571
15572 C++0x Extensions:
15573
15574 member-declaration:
15575 static_assert-declaration */
15576
15577 static void
15578 cp_parser_member_declaration (cp_parser* parser)
15579 {
15580 cp_decl_specifier_seq decl_specifiers;
15581 tree prefix_attributes;
15582 tree decl;
15583 int declares_class_or_enum;
15584 bool friend_p;
15585 cp_token *token = NULL;
15586 cp_token *decl_spec_token_start = NULL;
15587 cp_token *initializer_token_start = NULL;
15588 int saved_pedantic;
15589
15590 /* Check for the `__extension__' keyword. */
15591 if (cp_parser_extension_opt (parser, &saved_pedantic))
15592 {
15593 /* Recurse. */
15594 cp_parser_member_declaration (parser);
15595 /* Restore the old value of the PEDANTIC flag. */
15596 pedantic = saved_pedantic;
15597
15598 return;
15599 }
15600
15601 /* Check for a template-declaration. */
15602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15603 {
15604 /* An explicit specialization here is an error condition, and we
15605 expect the specialization handler to detect and report this. */
15606 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15608 cp_parser_explicit_specialization (parser);
15609 else
15610 cp_parser_template_declaration (parser, /*member_p=*/true);
15611
15612 return;
15613 }
15614
15615 /* Check for a using-declaration. */
15616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15617 {
15618 /* Parse the using-declaration. */
15619 cp_parser_using_declaration (parser,
15620 /*access_declaration_p=*/false);
15621 return;
15622 }
15623
15624 /* Check for @defs. */
15625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15626 {
15627 tree ivar, member;
15628 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15629 ivar = ivar_chains;
15630 while (ivar)
15631 {
15632 member = ivar;
15633 ivar = TREE_CHAIN (member);
15634 TREE_CHAIN (member) = NULL_TREE;
15635 finish_member_declaration (member);
15636 }
15637 return;
15638 }
15639
15640 /* If the next token is `static_assert' we have a static assertion. */
15641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15642 {
15643 cp_parser_static_assert (parser, /*member_p=*/true);
15644 return;
15645 }
15646
15647 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15648 return;
15649
15650 /* Parse the decl-specifier-seq. */
15651 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15652 cp_parser_decl_specifier_seq (parser,
15653 CP_PARSER_FLAGS_OPTIONAL,
15654 &decl_specifiers,
15655 &declares_class_or_enum);
15656 prefix_attributes = decl_specifiers.attributes;
15657 decl_specifiers.attributes = NULL_TREE;
15658 /* Check for an invalid type-name. */
15659 if (!decl_specifiers.type
15660 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15661 return;
15662 /* If there is no declarator, then the decl-specifier-seq should
15663 specify a type. */
15664 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15665 {
15666 /* If there was no decl-specifier-seq, and the next token is a
15667 `;', then we have something like:
15668
15669 struct S { ; };
15670
15671 [class.mem]
15672
15673 Each member-declaration shall declare at least one member
15674 name of the class. */
15675 if (!decl_specifiers.any_specifiers_p)
15676 {
15677 cp_token *token = cp_lexer_peek_token (parser->lexer);
15678 if (!in_system_header_at (token->location))
15679 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15680 }
15681 else
15682 {
15683 tree type;
15684
15685 /* See if this declaration is a friend. */
15686 friend_p = cp_parser_friend_p (&decl_specifiers);
15687 /* If there were decl-specifiers, check to see if there was
15688 a class-declaration. */
15689 type = check_tag_decl (&decl_specifiers);
15690 /* Nested classes have already been added to the class, but
15691 a `friend' needs to be explicitly registered. */
15692 if (friend_p)
15693 {
15694 /* If the `friend' keyword was present, the friend must
15695 be introduced with a class-key. */
15696 if (!declares_class_or_enum)
15697 error ("%Ha class-key must be used when declaring a friend",
15698 &decl_spec_token_start->location);
15699 /* In this case:
15700
15701 template <typename T> struct A {
15702 friend struct A<T>::B;
15703 };
15704
15705 A<T>::B will be represented by a TYPENAME_TYPE, and
15706 therefore not recognized by check_tag_decl. */
15707 if (!type
15708 && decl_specifiers.type
15709 && TYPE_P (decl_specifiers.type))
15710 type = decl_specifiers.type;
15711 if (!type || !TYPE_P (type))
15712 error ("%Hfriend declaration does not name a class or "
15713 "function", &decl_spec_token_start->location);
15714 else
15715 make_friend_class (current_class_type, type,
15716 /*complain=*/true);
15717 }
15718 /* If there is no TYPE, an error message will already have
15719 been issued. */
15720 else if (!type || type == error_mark_node)
15721 ;
15722 /* An anonymous aggregate has to be handled specially; such
15723 a declaration really declares a data member (with a
15724 particular type), as opposed to a nested class. */
15725 else if (ANON_AGGR_TYPE_P (type))
15726 {
15727 /* Remove constructors and such from TYPE, now that we
15728 know it is an anonymous aggregate. */
15729 fixup_anonymous_aggr (type);
15730 /* And make the corresponding data member. */
15731 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15732 /* Add it to the class. */
15733 finish_member_declaration (decl);
15734 }
15735 else
15736 cp_parser_check_access_in_redeclaration
15737 (TYPE_NAME (type),
15738 decl_spec_token_start->location);
15739 }
15740 }
15741 else
15742 {
15743 /* See if these declarations will be friends. */
15744 friend_p = cp_parser_friend_p (&decl_specifiers);
15745
15746 /* Keep going until we hit the `;' at the end of the
15747 declaration. */
15748 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15749 {
15750 tree attributes = NULL_TREE;
15751 tree first_attribute;
15752
15753 /* Peek at the next token. */
15754 token = cp_lexer_peek_token (parser->lexer);
15755
15756 /* Check for a bitfield declaration. */
15757 if (token->type == CPP_COLON
15758 || (token->type == CPP_NAME
15759 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15760 == CPP_COLON))
15761 {
15762 tree identifier;
15763 tree width;
15764
15765 /* Get the name of the bitfield. Note that we cannot just
15766 check TOKEN here because it may have been invalidated by
15767 the call to cp_lexer_peek_nth_token above. */
15768 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15769 identifier = cp_parser_identifier (parser);
15770 else
15771 identifier = NULL_TREE;
15772
15773 /* Consume the `:' token. */
15774 cp_lexer_consume_token (parser->lexer);
15775 /* Get the width of the bitfield. */
15776 width
15777 = cp_parser_constant_expression (parser,
15778 /*allow_non_constant=*/false,
15779 NULL);
15780
15781 /* Look for attributes that apply to the bitfield. */
15782 attributes = cp_parser_attributes_opt (parser);
15783 /* Remember which attributes are prefix attributes and
15784 which are not. */
15785 first_attribute = attributes;
15786 /* Combine the attributes. */
15787 attributes = chainon (prefix_attributes, attributes);
15788
15789 /* Create the bitfield declaration. */
15790 decl = grokbitfield (identifier
15791 ? make_id_declarator (NULL_TREE,
15792 identifier,
15793 sfk_none)
15794 : NULL,
15795 &decl_specifiers,
15796 width,
15797 attributes);
15798 }
15799 else
15800 {
15801 cp_declarator *declarator;
15802 tree initializer;
15803 tree asm_specification;
15804 int ctor_dtor_or_conv_p;
15805
15806 /* Parse the declarator. */
15807 declarator
15808 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15809 &ctor_dtor_or_conv_p,
15810 /*parenthesized_p=*/NULL,
15811 /*member_p=*/true);
15812
15813 /* If something went wrong parsing the declarator, make sure
15814 that we at least consume some tokens. */
15815 if (declarator == cp_error_declarator)
15816 {
15817 /* Skip to the end of the statement. */
15818 cp_parser_skip_to_end_of_statement (parser);
15819 /* If the next token is not a semicolon, that is
15820 probably because we just skipped over the body of
15821 a function. So, we consume a semicolon if
15822 present, but do not issue an error message if it
15823 is not present. */
15824 if (cp_lexer_next_token_is (parser->lexer,
15825 CPP_SEMICOLON))
15826 cp_lexer_consume_token (parser->lexer);
15827 return;
15828 }
15829
15830 if (declares_class_or_enum & 2)
15831 cp_parser_check_for_definition_in_return_type
15832 (declarator, decl_specifiers.type,
15833 decl_specifiers.type_location);
15834
15835 /* Look for an asm-specification. */
15836 asm_specification = cp_parser_asm_specification_opt (parser);
15837 /* Look for attributes that apply to the declaration. */
15838 attributes = cp_parser_attributes_opt (parser);
15839 /* Remember which attributes are prefix attributes and
15840 which are not. */
15841 first_attribute = attributes;
15842 /* Combine the attributes. */
15843 attributes = chainon (prefix_attributes, attributes);
15844
15845 /* If it's an `=', then we have a constant-initializer or a
15846 pure-specifier. It is not correct to parse the
15847 initializer before registering the member declaration
15848 since the member declaration should be in scope while
15849 its initializer is processed. However, the rest of the
15850 front end does not yet provide an interface that allows
15851 us to handle this correctly. */
15852 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15853 {
15854 /* In [class.mem]:
15855
15856 A pure-specifier shall be used only in the declaration of
15857 a virtual function.
15858
15859 A member-declarator can contain a constant-initializer
15860 only if it declares a static member of integral or
15861 enumeration type.
15862
15863 Therefore, if the DECLARATOR is for a function, we look
15864 for a pure-specifier; otherwise, we look for a
15865 constant-initializer. When we call `grokfield', it will
15866 perform more stringent semantics checks. */
15867 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15868 if (function_declarator_p (declarator))
15869 initializer = cp_parser_pure_specifier (parser);
15870 else
15871 /* Parse the initializer. */
15872 initializer = cp_parser_constant_initializer (parser);
15873 }
15874 /* Otherwise, there is no initializer. */
15875 else
15876 initializer = NULL_TREE;
15877
15878 /* See if we are probably looking at a function
15879 definition. We are certainly not looking at a
15880 member-declarator. Calling `grokfield' has
15881 side-effects, so we must not do it unless we are sure
15882 that we are looking at a member-declarator. */
15883 if (cp_parser_token_starts_function_definition_p
15884 (cp_lexer_peek_token (parser->lexer)))
15885 {
15886 /* The grammar does not allow a pure-specifier to be
15887 used when a member function is defined. (It is
15888 possible that this fact is an oversight in the
15889 standard, since a pure function may be defined
15890 outside of the class-specifier. */
15891 if (initializer)
15892 error ("%Hpure-specifier on function-definition",
15893 &initializer_token_start->location);
15894 decl = cp_parser_save_member_function_body (parser,
15895 &decl_specifiers,
15896 declarator,
15897 attributes);
15898 /* If the member was not a friend, declare it here. */
15899 if (!friend_p)
15900 finish_member_declaration (decl);
15901 /* Peek at the next token. */
15902 token = cp_lexer_peek_token (parser->lexer);
15903 /* If the next token is a semicolon, consume it. */
15904 if (token->type == CPP_SEMICOLON)
15905 cp_lexer_consume_token (parser->lexer);
15906 return;
15907 }
15908 else
15909 if (declarator->kind == cdk_function)
15910 declarator->id_loc = token->location;
15911 /* Create the declaration. */
15912 decl = grokfield (declarator, &decl_specifiers,
15913 initializer, /*init_const_expr_p=*/true,
15914 asm_specification,
15915 attributes);
15916 }
15917
15918 /* Reset PREFIX_ATTRIBUTES. */
15919 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15920 attributes = TREE_CHAIN (attributes);
15921 if (attributes)
15922 TREE_CHAIN (attributes) = NULL_TREE;
15923
15924 /* If there is any qualification still in effect, clear it
15925 now; we will be starting fresh with the next declarator. */
15926 parser->scope = NULL_TREE;
15927 parser->qualifying_scope = NULL_TREE;
15928 parser->object_scope = NULL_TREE;
15929 /* If it's a `,', then there are more declarators. */
15930 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15931 cp_lexer_consume_token (parser->lexer);
15932 /* If the next token isn't a `;', then we have a parse error. */
15933 else if (cp_lexer_next_token_is_not (parser->lexer,
15934 CPP_SEMICOLON))
15935 {
15936 cp_parser_error (parser, "expected %<;%>");
15937 /* Skip tokens until we find a `;'. */
15938 cp_parser_skip_to_end_of_statement (parser);
15939
15940 break;
15941 }
15942
15943 if (decl)
15944 {
15945 /* Add DECL to the list of members. */
15946 if (!friend_p)
15947 finish_member_declaration (decl);
15948
15949 if (TREE_CODE (decl) == FUNCTION_DECL)
15950 cp_parser_save_default_args (parser, decl);
15951 }
15952 }
15953 }
15954
15955 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15956 }
15957
15958 /* Parse a pure-specifier.
15959
15960 pure-specifier:
15961 = 0
15962
15963 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15964 Otherwise, ERROR_MARK_NODE is returned. */
15965
15966 static tree
15967 cp_parser_pure_specifier (cp_parser* parser)
15968 {
15969 cp_token *token;
15970
15971 /* Look for the `=' token. */
15972 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15973 return error_mark_node;
15974 /* Look for the `0' token. */
15975 token = cp_lexer_consume_token (parser->lexer);
15976
15977 /* Accept = default or = delete in c++0x mode. */
15978 if (token->keyword == RID_DEFAULT
15979 || token->keyword == RID_DELETE)
15980 {
15981 maybe_warn_cpp0x ("defaulted and deleted functions");
15982 return token->u.value;
15983 }
15984
15985 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15986 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15987 {
15988 cp_parser_error (parser,
15989 "invalid pure specifier (only %<= 0%> is allowed)");
15990 cp_parser_skip_to_end_of_statement (parser);
15991 return error_mark_node;
15992 }
15993 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15994 {
15995 error ("%Htemplates may not be %<virtual%>", &token->location);
15996 return error_mark_node;
15997 }
15998
15999 return integer_zero_node;
16000 }
16001
16002 /* Parse a constant-initializer.
16003
16004 constant-initializer:
16005 = constant-expression
16006
16007 Returns a representation of the constant-expression. */
16008
16009 static tree
16010 cp_parser_constant_initializer (cp_parser* parser)
16011 {
16012 /* Look for the `=' token. */
16013 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16014 return error_mark_node;
16015
16016 /* It is invalid to write:
16017
16018 struct S { static const int i = { 7 }; };
16019
16020 */
16021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16022 {
16023 cp_parser_error (parser,
16024 "a brace-enclosed initializer is not allowed here");
16025 /* Consume the opening brace. */
16026 cp_lexer_consume_token (parser->lexer);
16027 /* Skip the initializer. */
16028 cp_parser_skip_to_closing_brace (parser);
16029 /* Look for the trailing `}'. */
16030 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16031
16032 return error_mark_node;
16033 }
16034
16035 return cp_parser_constant_expression (parser,
16036 /*allow_non_constant=*/false,
16037 NULL);
16038 }
16039
16040 /* Derived classes [gram.class.derived] */
16041
16042 /* Parse a base-clause.
16043
16044 base-clause:
16045 : base-specifier-list
16046
16047 base-specifier-list:
16048 base-specifier ... [opt]
16049 base-specifier-list , base-specifier ... [opt]
16050
16051 Returns a TREE_LIST representing the base-classes, in the order in
16052 which they were declared. The representation of each node is as
16053 described by cp_parser_base_specifier.
16054
16055 In the case that no bases are specified, this function will return
16056 NULL_TREE, not ERROR_MARK_NODE. */
16057
16058 static tree
16059 cp_parser_base_clause (cp_parser* parser)
16060 {
16061 tree bases = NULL_TREE;
16062
16063 /* Look for the `:' that begins the list. */
16064 cp_parser_require (parser, CPP_COLON, "%<:%>");
16065
16066 /* Scan the base-specifier-list. */
16067 while (true)
16068 {
16069 cp_token *token;
16070 tree base;
16071 bool pack_expansion_p = false;
16072
16073 /* Look for the base-specifier. */
16074 base = cp_parser_base_specifier (parser);
16075 /* Look for the (optional) ellipsis. */
16076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16077 {
16078 /* Consume the `...'. */
16079 cp_lexer_consume_token (parser->lexer);
16080
16081 pack_expansion_p = true;
16082 }
16083
16084 /* Add BASE to the front of the list. */
16085 if (base != error_mark_node)
16086 {
16087 if (pack_expansion_p)
16088 /* Make this a pack expansion type. */
16089 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16090
16091
16092 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16093 {
16094 TREE_CHAIN (base) = bases;
16095 bases = base;
16096 }
16097 }
16098 /* Peek at the next token. */
16099 token = cp_lexer_peek_token (parser->lexer);
16100 /* If it's not a comma, then the list is complete. */
16101 if (token->type != CPP_COMMA)
16102 break;
16103 /* Consume the `,'. */
16104 cp_lexer_consume_token (parser->lexer);
16105 }
16106
16107 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16108 base class had a qualified name. However, the next name that
16109 appears is certainly not qualified. */
16110 parser->scope = NULL_TREE;
16111 parser->qualifying_scope = NULL_TREE;
16112 parser->object_scope = NULL_TREE;
16113
16114 return nreverse (bases);
16115 }
16116
16117 /* Parse a base-specifier.
16118
16119 base-specifier:
16120 :: [opt] nested-name-specifier [opt] class-name
16121 virtual access-specifier [opt] :: [opt] nested-name-specifier
16122 [opt] class-name
16123 access-specifier virtual [opt] :: [opt] nested-name-specifier
16124 [opt] class-name
16125
16126 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16127 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16128 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16129 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16130
16131 static tree
16132 cp_parser_base_specifier (cp_parser* parser)
16133 {
16134 cp_token *token;
16135 bool done = false;
16136 bool virtual_p = false;
16137 bool duplicate_virtual_error_issued_p = false;
16138 bool duplicate_access_error_issued_p = false;
16139 bool class_scope_p, template_p;
16140 tree access = access_default_node;
16141 tree type;
16142
16143 /* Process the optional `virtual' and `access-specifier'. */
16144 while (!done)
16145 {
16146 /* Peek at the next token. */
16147 token = cp_lexer_peek_token (parser->lexer);
16148 /* Process `virtual'. */
16149 switch (token->keyword)
16150 {
16151 case RID_VIRTUAL:
16152 /* If `virtual' appears more than once, issue an error. */
16153 if (virtual_p && !duplicate_virtual_error_issued_p)
16154 {
16155 cp_parser_error (parser,
16156 "%<virtual%> specified more than once in base-specified");
16157 duplicate_virtual_error_issued_p = true;
16158 }
16159
16160 virtual_p = true;
16161
16162 /* Consume the `virtual' token. */
16163 cp_lexer_consume_token (parser->lexer);
16164
16165 break;
16166
16167 case RID_PUBLIC:
16168 case RID_PROTECTED:
16169 case RID_PRIVATE:
16170 /* If more than one access specifier appears, issue an
16171 error. */
16172 if (access != access_default_node
16173 && !duplicate_access_error_issued_p)
16174 {
16175 cp_parser_error (parser,
16176 "more than one access specifier in base-specified");
16177 duplicate_access_error_issued_p = true;
16178 }
16179
16180 access = ridpointers[(int) token->keyword];
16181
16182 /* Consume the access-specifier. */
16183 cp_lexer_consume_token (parser->lexer);
16184
16185 break;
16186
16187 default:
16188 done = true;
16189 break;
16190 }
16191 }
16192 /* It is not uncommon to see programs mechanically, erroneously, use
16193 the 'typename' keyword to denote (dependent) qualified types
16194 as base classes. */
16195 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16196 {
16197 token = cp_lexer_peek_token (parser->lexer);
16198 if (!processing_template_decl)
16199 error ("%Hkeyword %<typename%> not allowed outside of templates",
16200 &token->location);
16201 else
16202 error ("%Hkeyword %<typename%> not allowed in this context "
16203 "(the base class is implicitly a type)",
16204 &token->location);
16205 cp_lexer_consume_token (parser->lexer);
16206 }
16207
16208 /* Look for the optional `::' operator. */
16209 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16210 /* Look for the nested-name-specifier. The simplest way to
16211 implement:
16212
16213 [temp.res]
16214
16215 The keyword `typename' is not permitted in a base-specifier or
16216 mem-initializer; in these contexts a qualified name that
16217 depends on a template-parameter is implicitly assumed to be a
16218 type name.
16219
16220 is to pretend that we have seen the `typename' keyword at this
16221 point. */
16222 cp_parser_nested_name_specifier_opt (parser,
16223 /*typename_keyword_p=*/true,
16224 /*check_dependency_p=*/true,
16225 typename_type,
16226 /*is_declaration=*/true);
16227 /* If the base class is given by a qualified name, assume that names
16228 we see are type names or templates, as appropriate. */
16229 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16230 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16231
16232 /* Finally, look for the class-name. */
16233 type = cp_parser_class_name (parser,
16234 class_scope_p,
16235 template_p,
16236 typename_type,
16237 /*check_dependency_p=*/true,
16238 /*class_head_p=*/false,
16239 /*is_declaration=*/true);
16240
16241 if (type == error_mark_node)
16242 return error_mark_node;
16243
16244 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16245 }
16246
16247 /* Exception handling [gram.exception] */
16248
16249 /* Parse an (optional) exception-specification.
16250
16251 exception-specification:
16252 throw ( type-id-list [opt] )
16253
16254 Returns a TREE_LIST representing the exception-specification. The
16255 TREE_VALUE of each node is a type. */
16256
16257 static tree
16258 cp_parser_exception_specification_opt (cp_parser* parser)
16259 {
16260 cp_token *token;
16261 tree type_id_list;
16262
16263 /* Peek at the next token. */
16264 token = cp_lexer_peek_token (parser->lexer);
16265 /* If it's not `throw', then there's no exception-specification. */
16266 if (!cp_parser_is_keyword (token, RID_THROW))
16267 return NULL_TREE;
16268
16269 /* Consume the `throw'. */
16270 cp_lexer_consume_token (parser->lexer);
16271
16272 /* Look for the `('. */
16273 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16274
16275 /* Peek at the next token. */
16276 token = cp_lexer_peek_token (parser->lexer);
16277 /* If it's not a `)', then there is a type-id-list. */
16278 if (token->type != CPP_CLOSE_PAREN)
16279 {
16280 const char *saved_message;
16281
16282 /* Types may not be defined in an exception-specification. */
16283 saved_message = parser->type_definition_forbidden_message;
16284 parser->type_definition_forbidden_message
16285 = "types may not be defined in an exception-specification";
16286 /* Parse the type-id-list. */
16287 type_id_list = cp_parser_type_id_list (parser);
16288 /* Restore the saved message. */
16289 parser->type_definition_forbidden_message = saved_message;
16290 }
16291 else
16292 type_id_list = empty_except_spec;
16293
16294 /* Look for the `)'. */
16295 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16296
16297 return type_id_list;
16298 }
16299
16300 /* Parse an (optional) type-id-list.
16301
16302 type-id-list:
16303 type-id ... [opt]
16304 type-id-list , type-id ... [opt]
16305
16306 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16307 in the order that the types were presented. */
16308
16309 static tree
16310 cp_parser_type_id_list (cp_parser* parser)
16311 {
16312 tree types = NULL_TREE;
16313
16314 while (true)
16315 {
16316 cp_token *token;
16317 tree type;
16318
16319 /* Get the next type-id. */
16320 type = cp_parser_type_id (parser);
16321 /* Parse the optional ellipsis. */
16322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16323 {
16324 /* Consume the `...'. */
16325 cp_lexer_consume_token (parser->lexer);
16326
16327 /* Turn the type into a pack expansion expression. */
16328 type = make_pack_expansion (type);
16329 }
16330 /* Add it to the list. */
16331 types = add_exception_specifier (types, type, /*complain=*/1);
16332 /* Peek at the next token. */
16333 token = cp_lexer_peek_token (parser->lexer);
16334 /* If it is not a `,', we are done. */
16335 if (token->type != CPP_COMMA)
16336 break;
16337 /* Consume the `,'. */
16338 cp_lexer_consume_token (parser->lexer);
16339 }
16340
16341 return nreverse (types);
16342 }
16343
16344 /* Parse a try-block.
16345
16346 try-block:
16347 try compound-statement handler-seq */
16348
16349 static tree
16350 cp_parser_try_block (cp_parser* parser)
16351 {
16352 tree try_block;
16353
16354 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16355 try_block = begin_try_block ();
16356 cp_parser_compound_statement (parser, NULL, true);
16357 finish_try_block (try_block);
16358 cp_parser_handler_seq (parser);
16359 finish_handler_sequence (try_block);
16360
16361 return try_block;
16362 }
16363
16364 /* Parse a function-try-block.
16365
16366 function-try-block:
16367 try ctor-initializer [opt] function-body handler-seq */
16368
16369 static bool
16370 cp_parser_function_try_block (cp_parser* parser)
16371 {
16372 tree compound_stmt;
16373 tree try_block;
16374 bool ctor_initializer_p;
16375
16376 /* Look for the `try' keyword. */
16377 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16378 return false;
16379 /* Let the rest of the front end know where we are. */
16380 try_block = begin_function_try_block (&compound_stmt);
16381 /* Parse the function-body. */
16382 ctor_initializer_p
16383 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16384 /* We're done with the `try' part. */
16385 finish_function_try_block (try_block);
16386 /* Parse the handlers. */
16387 cp_parser_handler_seq (parser);
16388 /* We're done with the handlers. */
16389 finish_function_handler_sequence (try_block, compound_stmt);
16390
16391 return ctor_initializer_p;
16392 }
16393
16394 /* Parse a handler-seq.
16395
16396 handler-seq:
16397 handler handler-seq [opt] */
16398
16399 static void
16400 cp_parser_handler_seq (cp_parser* parser)
16401 {
16402 while (true)
16403 {
16404 cp_token *token;
16405
16406 /* Parse the handler. */
16407 cp_parser_handler (parser);
16408 /* Peek at the next token. */
16409 token = cp_lexer_peek_token (parser->lexer);
16410 /* If it's not `catch' then there are no more handlers. */
16411 if (!cp_parser_is_keyword (token, RID_CATCH))
16412 break;
16413 }
16414 }
16415
16416 /* Parse a handler.
16417
16418 handler:
16419 catch ( exception-declaration ) compound-statement */
16420
16421 static void
16422 cp_parser_handler (cp_parser* parser)
16423 {
16424 tree handler;
16425 tree declaration;
16426
16427 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16428 handler = begin_handler ();
16429 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16430 declaration = cp_parser_exception_declaration (parser);
16431 finish_handler_parms (declaration, handler);
16432 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16433 cp_parser_compound_statement (parser, NULL, false);
16434 finish_handler (handler);
16435 }
16436
16437 /* Parse an exception-declaration.
16438
16439 exception-declaration:
16440 type-specifier-seq declarator
16441 type-specifier-seq abstract-declarator
16442 type-specifier-seq
16443 ...
16444
16445 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16446 ellipsis variant is used. */
16447
16448 static tree
16449 cp_parser_exception_declaration (cp_parser* parser)
16450 {
16451 cp_decl_specifier_seq type_specifiers;
16452 cp_declarator *declarator;
16453 const char *saved_message;
16454
16455 /* If it's an ellipsis, it's easy to handle. */
16456 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16457 {
16458 /* Consume the `...' token. */
16459 cp_lexer_consume_token (parser->lexer);
16460 return NULL_TREE;
16461 }
16462
16463 /* Types may not be defined in exception-declarations. */
16464 saved_message = parser->type_definition_forbidden_message;
16465 parser->type_definition_forbidden_message
16466 = "types may not be defined in exception-declarations";
16467
16468 /* Parse the type-specifier-seq. */
16469 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16470 &type_specifiers);
16471 /* If it's a `)', then there is no declarator. */
16472 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16473 declarator = NULL;
16474 else
16475 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16476 /*ctor_dtor_or_conv_p=*/NULL,
16477 /*parenthesized_p=*/NULL,
16478 /*member_p=*/false);
16479
16480 /* Restore the saved message. */
16481 parser->type_definition_forbidden_message = saved_message;
16482
16483 if (!type_specifiers.any_specifiers_p)
16484 return error_mark_node;
16485
16486 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16487 }
16488
16489 /* Parse a throw-expression.
16490
16491 throw-expression:
16492 throw assignment-expression [opt]
16493
16494 Returns a THROW_EXPR representing the throw-expression. */
16495
16496 static tree
16497 cp_parser_throw_expression (cp_parser* parser)
16498 {
16499 tree expression;
16500 cp_token* token;
16501
16502 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16503 token = cp_lexer_peek_token (parser->lexer);
16504 /* Figure out whether or not there is an assignment-expression
16505 following the "throw" keyword. */
16506 if (token->type == CPP_COMMA
16507 || token->type == CPP_SEMICOLON
16508 || token->type == CPP_CLOSE_PAREN
16509 || token->type == CPP_CLOSE_SQUARE
16510 || token->type == CPP_CLOSE_BRACE
16511 || token->type == CPP_COLON)
16512 expression = NULL_TREE;
16513 else
16514 expression = cp_parser_assignment_expression (parser,
16515 /*cast_p=*/false);
16516
16517 return build_throw (expression);
16518 }
16519
16520 /* GNU Extensions */
16521
16522 /* Parse an (optional) asm-specification.
16523
16524 asm-specification:
16525 asm ( string-literal )
16526
16527 If the asm-specification is present, returns a STRING_CST
16528 corresponding to the string-literal. Otherwise, returns
16529 NULL_TREE. */
16530
16531 static tree
16532 cp_parser_asm_specification_opt (cp_parser* parser)
16533 {
16534 cp_token *token;
16535 tree asm_specification;
16536
16537 /* Peek at the next token. */
16538 token = cp_lexer_peek_token (parser->lexer);
16539 /* If the next token isn't the `asm' keyword, then there's no
16540 asm-specification. */
16541 if (!cp_parser_is_keyword (token, RID_ASM))
16542 return NULL_TREE;
16543
16544 /* Consume the `asm' token. */
16545 cp_lexer_consume_token (parser->lexer);
16546 /* Look for the `('. */
16547 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16548
16549 /* Look for the string-literal. */
16550 asm_specification = cp_parser_string_literal (parser, false, false);
16551
16552 /* Look for the `)'. */
16553 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16554
16555 return asm_specification;
16556 }
16557
16558 /* Parse an asm-operand-list.
16559
16560 asm-operand-list:
16561 asm-operand
16562 asm-operand-list , asm-operand
16563
16564 asm-operand:
16565 string-literal ( expression )
16566 [ string-literal ] string-literal ( expression )
16567
16568 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16569 each node is the expression. The TREE_PURPOSE is itself a
16570 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16571 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16572 is a STRING_CST for the string literal before the parenthesis. Returns
16573 ERROR_MARK_NODE if any of the operands are invalid. */
16574
16575 static tree
16576 cp_parser_asm_operand_list (cp_parser* parser)
16577 {
16578 tree asm_operands = NULL_TREE;
16579 bool invalid_operands = false;
16580
16581 while (true)
16582 {
16583 tree string_literal;
16584 tree expression;
16585 tree name;
16586
16587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16588 {
16589 /* Consume the `[' token. */
16590 cp_lexer_consume_token (parser->lexer);
16591 /* Read the operand name. */
16592 name = cp_parser_identifier (parser);
16593 if (name != error_mark_node)
16594 name = build_string (IDENTIFIER_LENGTH (name),
16595 IDENTIFIER_POINTER (name));
16596 /* Look for the closing `]'. */
16597 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16598 }
16599 else
16600 name = NULL_TREE;
16601 /* Look for the string-literal. */
16602 string_literal = cp_parser_string_literal (parser, false, false);
16603
16604 /* Look for the `('. */
16605 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16606 /* Parse the expression. */
16607 expression = cp_parser_expression (parser, /*cast_p=*/false);
16608 /* Look for the `)'. */
16609 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16610
16611 if (name == error_mark_node
16612 || string_literal == error_mark_node
16613 || expression == error_mark_node)
16614 invalid_operands = true;
16615
16616 /* Add this operand to the list. */
16617 asm_operands = tree_cons (build_tree_list (name, string_literal),
16618 expression,
16619 asm_operands);
16620 /* If the next token is not a `,', there are no more
16621 operands. */
16622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16623 break;
16624 /* Consume the `,'. */
16625 cp_lexer_consume_token (parser->lexer);
16626 }
16627
16628 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16629 }
16630
16631 /* Parse an asm-clobber-list.
16632
16633 asm-clobber-list:
16634 string-literal
16635 asm-clobber-list , string-literal
16636
16637 Returns a TREE_LIST, indicating the clobbers in the order that they
16638 appeared. The TREE_VALUE of each node is a STRING_CST. */
16639
16640 static tree
16641 cp_parser_asm_clobber_list (cp_parser* parser)
16642 {
16643 tree clobbers = NULL_TREE;
16644
16645 while (true)
16646 {
16647 tree string_literal;
16648
16649 /* Look for the string literal. */
16650 string_literal = cp_parser_string_literal (parser, false, false);
16651 /* Add it to the list. */
16652 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16653 /* If the next token is not a `,', then the list is
16654 complete. */
16655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16656 break;
16657 /* Consume the `,' token. */
16658 cp_lexer_consume_token (parser->lexer);
16659 }
16660
16661 return clobbers;
16662 }
16663
16664 /* Parse an (optional) series of attributes.
16665
16666 attributes:
16667 attributes attribute
16668
16669 attribute:
16670 __attribute__ (( attribute-list [opt] ))
16671
16672 The return value is as for cp_parser_attribute_list. */
16673
16674 static tree
16675 cp_parser_attributes_opt (cp_parser* parser)
16676 {
16677 tree attributes = NULL_TREE;
16678
16679 while (true)
16680 {
16681 cp_token *token;
16682 tree attribute_list;
16683
16684 /* Peek at the next token. */
16685 token = cp_lexer_peek_token (parser->lexer);
16686 /* If it's not `__attribute__', then we're done. */
16687 if (token->keyword != RID_ATTRIBUTE)
16688 break;
16689
16690 /* Consume the `__attribute__' keyword. */
16691 cp_lexer_consume_token (parser->lexer);
16692 /* Look for the two `(' tokens. */
16693 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16694 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16695
16696 /* Peek at the next token. */
16697 token = cp_lexer_peek_token (parser->lexer);
16698 if (token->type != CPP_CLOSE_PAREN)
16699 /* Parse the attribute-list. */
16700 attribute_list = cp_parser_attribute_list (parser);
16701 else
16702 /* If the next token is a `)', then there is no attribute
16703 list. */
16704 attribute_list = NULL;
16705
16706 /* Look for the two `)' tokens. */
16707 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16708 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16709
16710 /* Add these new attributes to the list. */
16711 attributes = chainon (attributes, attribute_list);
16712 }
16713
16714 return attributes;
16715 }
16716
16717 /* Parse an attribute-list.
16718
16719 attribute-list:
16720 attribute
16721 attribute-list , attribute
16722
16723 attribute:
16724 identifier
16725 identifier ( identifier )
16726 identifier ( identifier , expression-list )
16727 identifier ( expression-list )
16728
16729 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16730 to an attribute. The TREE_PURPOSE of each node is the identifier
16731 indicating which attribute is in use. The TREE_VALUE represents
16732 the arguments, if any. */
16733
16734 static tree
16735 cp_parser_attribute_list (cp_parser* parser)
16736 {
16737 tree attribute_list = NULL_TREE;
16738 bool save_translate_strings_p = parser->translate_strings_p;
16739
16740 parser->translate_strings_p = false;
16741 while (true)
16742 {
16743 cp_token *token;
16744 tree identifier;
16745 tree attribute;
16746
16747 /* Look for the identifier. We also allow keywords here; for
16748 example `__attribute__ ((const))' is legal. */
16749 token = cp_lexer_peek_token (parser->lexer);
16750 if (token->type == CPP_NAME
16751 || token->type == CPP_KEYWORD)
16752 {
16753 tree arguments = NULL_TREE;
16754
16755 /* Consume the token. */
16756 token = cp_lexer_consume_token (parser->lexer);
16757
16758 /* Save away the identifier that indicates which attribute
16759 this is. */
16760 identifier = token->u.value;
16761 attribute = build_tree_list (identifier, NULL_TREE);
16762
16763 /* Peek at the next token. */
16764 token = cp_lexer_peek_token (parser->lexer);
16765 /* If it's an `(', then parse the attribute arguments. */
16766 if (token->type == CPP_OPEN_PAREN)
16767 {
16768 arguments = cp_parser_parenthesized_expression_list
16769 (parser, true, /*cast_p=*/false,
16770 /*allow_expansion_p=*/false,
16771 /*non_constant_p=*/NULL);
16772 /* Save the arguments away. */
16773 TREE_VALUE (attribute) = arguments;
16774 }
16775
16776 if (arguments != error_mark_node)
16777 {
16778 /* Add this attribute to the list. */
16779 TREE_CHAIN (attribute) = attribute_list;
16780 attribute_list = attribute;
16781 }
16782
16783 token = cp_lexer_peek_token (parser->lexer);
16784 }
16785 /* Now, look for more attributes. If the next token isn't a
16786 `,', we're done. */
16787 if (token->type != CPP_COMMA)
16788 break;
16789
16790 /* Consume the comma and keep going. */
16791 cp_lexer_consume_token (parser->lexer);
16792 }
16793 parser->translate_strings_p = save_translate_strings_p;
16794
16795 /* We built up the list in reverse order. */
16796 return nreverse (attribute_list);
16797 }
16798
16799 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16800 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16801 current value of the PEDANTIC flag, regardless of whether or not
16802 the `__extension__' keyword is present. The caller is responsible
16803 for restoring the value of the PEDANTIC flag. */
16804
16805 static bool
16806 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16807 {
16808 /* Save the old value of the PEDANTIC flag. */
16809 *saved_pedantic = pedantic;
16810
16811 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16812 {
16813 /* Consume the `__extension__' token. */
16814 cp_lexer_consume_token (parser->lexer);
16815 /* We're not being pedantic while the `__extension__' keyword is
16816 in effect. */
16817 pedantic = 0;
16818
16819 return true;
16820 }
16821
16822 return false;
16823 }
16824
16825 /* Parse a label declaration.
16826
16827 label-declaration:
16828 __label__ label-declarator-seq ;
16829
16830 label-declarator-seq:
16831 identifier , label-declarator-seq
16832 identifier */
16833
16834 static void
16835 cp_parser_label_declaration (cp_parser* parser)
16836 {
16837 /* Look for the `__label__' keyword. */
16838 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16839
16840 while (true)
16841 {
16842 tree identifier;
16843
16844 /* Look for an identifier. */
16845 identifier = cp_parser_identifier (parser);
16846 /* If we failed, stop. */
16847 if (identifier == error_mark_node)
16848 break;
16849 /* Declare it as a label. */
16850 finish_label_decl (identifier);
16851 /* If the next token is a `;', stop. */
16852 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16853 break;
16854 /* Look for the `,' separating the label declarations. */
16855 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16856 }
16857
16858 /* Look for the final `;'. */
16859 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16860 }
16861
16862 /* Support Functions */
16863
16864 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16865 NAME should have one of the representations used for an
16866 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16867 is returned. If PARSER->SCOPE is a dependent type, then a
16868 SCOPE_REF is returned.
16869
16870 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16871 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16872 was formed. Abstractly, such entities should not be passed to this
16873 function, because they do not need to be looked up, but it is
16874 simpler to check for this special case here, rather than at the
16875 call-sites.
16876
16877 In cases not explicitly covered above, this function returns a
16878 DECL, OVERLOAD, or baselink representing the result of the lookup.
16879 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16880 is returned.
16881
16882 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16883 (e.g., "struct") that was used. In that case bindings that do not
16884 refer to types are ignored.
16885
16886 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16887 ignored.
16888
16889 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16890 are ignored.
16891
16892 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16893 types.
16894
16895 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16896 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16897 NULL_TREE otherwise. */
16898
16899 static tree
16900 cp_parser_lookup_name (cp_parser *parser, tree name,
16901 enum tag_types tag_type,
16902 bool is_template,
16903 bool is_namespace,
16904 bool check_dependency,
16905 tree *ambiguous_decls,
16906 location_t name_location)
16907 {
16908 int flags = 0;
16909 tree decl;
16910 tree object_type = parser->context->object_type;
16911
16912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16913 flags |= LOOKUP_COMPLAIN;
16914
16915 /* Assume that the lookup will be unambiguous. */
16916 if (ambiguous_decls)
16917 *ambiguous_decls = NULL_TREE;
16918
16919 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16920 no longer valid. Note that if we are parsing tentatively, and
16921 the parse fails, OBJECT_TYPE will be automatically restored. */
16922 parser->context->object_type = NULL_TREE;
16923
16924 if (name == error_mark_node)
16925 return error_mark_node;
16926
16927 /* A template-id has already been resolved; there is no lookup to
16928 do. */
16929 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16930 return name;
16931 if (BASELINK_P (name))
16932 {
16933 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16934 == TEMPLATE_ID_EXPR);
16935 return name;
16936 }
16937
16938 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16939 it should already have been checked to make sure that the name
16940 used matches the type being destroyed. */
16941 if (TREE_CODE (name) == BIT_NOT_EXPR)
16942 {
16943 tree type;
16944
16945 /* Figure out to which type this destructor applies. */
16946 if (parser->scope)
16947 type = parser->scope;
16948 else if (object_type)
16949 type = object_type;
16950 else
16951 type = current_class_type;
16952 /* If that's not a class type, there is no destructor. */
16953 if (!type || !CLASS_TYPE_P (type))
16954 return error_mark_node;
16955 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16956 lazily_declare_fn (sfk_destructor, type);
16957 if (!CLASSTYPE_DESTRUCTORS (type))
16958 return error_mark_node;
16959 /* If it was a class type, return the destructor. */
16960 return CLASSTYPE_DESTRUCTORS (type);
16961 }
16962
16963 /* By this point, the NAME should be an ordinary identifier. If
16964 the id-expression was a qualified name, the qualifying scope is
16965 stored in PARSER->SCOPE at this point. */
16966 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16967
16968 /* Perform the lookup. */
16969 if (parser->scope)
16970 {
16971 bool dependent_p;
16972
16973 if (parser->scope == error_mark_node)
16974 return error_mark_node;
16975
16976 /* If the SCOPE is dependent, the lookup must be deferred until
16977 the template is instantiated -- unless we are explicitly
16978 looking up names in uninstantiated templates. Even then, we
16979 cannot look up the name if the scope is not a class type; it
16980 might, for example, be a template type parameter. */
16981 dependent_p = (TYPE_P (parser->scope)
16982 && !(parser->in_declarator_p
16983 && currently_open_class (parser->scope))
16984 && dependent_type_p (parser->scope));
16985 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16986 && dependent_p)
16987 {
16988 if (tag_type)
16989 {
16990 tree type;
16991
16992 /* The resolution to Core Issue 180 says that `struct
16993 A::B' should be considered a type-name, even if `A'
16994 is dependent. */
16995 type = make_typename_type (parser->scope, name, tag_type,
16996 /*complain=*/tf_error);
16997 decl = TYPE_NAME (type);
16998 }
16999 else if (is_template
17000 && (cp_parser_next_token_ends_template_argument_p (parser)
17001 || cp_lexer_next_token_is (parser->lexer,
17002 CPP_CLOSE_PAREN)))
17003 decl = make_unbound_class_template (parser->scope,
17004 name, NULL_TREE,
17005 /*complain=*/tf_error);
17006 else
17007 decl = build_qualified_name (/*type=*/NULL_TREE,
17008 parser->scope, name,
17009 is_template);
17010 }
17011 else
17012 {
17013 tree pushed_scope = NULL_TREE;
17014
17015 /* If PARSER->SCOPE is a dependent type, then it must be a
17016 class type, and we must not be checking dependencies;
17017 otherwise, we would have processed this lookup above. So
17018 that PARSER->SCOPE is not considered a dependent base by
17019 lookup_member, we must enter the scope here. */
17020 if (dependent_p)
17021 pushed_scope = push_scope (parser->scope);
17022 /* If the PARSER->SCOPE is a template specialization, it
17023 may be instantiated during name lookup. In that case,
17024 errors may be issued. Even if we rollback the current
17025 tentative parse, those errors are valid. */
17026 decl = lookup_qualified_name (parser->scope, name,
17027 tag_type != none_type,
17028 /*complain=*/true);
17029
17030 /* If we have a single function from a using decl, pull it out. */
17031 if (decl
17032 && TREE_CODE (decl) == OVERLOAD
17033 && !really_overloaded_fn (decl))
17034 decl = OVL_FUNCTION (decl);
17035
17036 if (pushed_scope)
17037 pop_scope (pushed_scope);
17038 }
17039 parser->qualifying_scope = parser->scope;
17040 parser->object_scope = NULL_TREE;
17041 }
17042 else if (object_type)
17043 {
17044 tree object_decl = NULL_TREE;
17045 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17046 OBJECT_TYPE is not a class. */
17047 if (CLASS_TYPE_P (object_type))
17048 /* If the OBJECT_TYPE is a template specialization, it may
17049 be instantiated during name lookup. In that case, errors
17050 may be issued. Even if we rollback the current tentative
17051 parse, those errors are valid. */
17052 object_decl = lookup_member (object_type,
17053 name,
17054 /*protect=*/0,
17055 tag_type != none_type);
17056 /* Look it up in the enclosing context, too. */
17057 decl = lookup_name_real (name, tag_type != none_type,
17058 /*nonclass=*/0,
17059 /*block_p=*/true, is_namespace, flags);
17060 parser->object_scope = object_type;
17061 parser->qualifying_scope = NULL_TREE;
17062 if (object_decl)
17063 decl = object_decl;
17064 }
17065 else
17066 {
17067 decl = lookup_name_real (name, tag_type != none_type,
17068 /*nonclass=*/0,
17069 /*block_p=*/true, is_namespace, flags);
17070 parser->qualifying_scope = NULL_TREE;
17071 parser->object_scope = NULL_TREE;
17072 }
17073
17074 /* If the lookup failed, let our caller know. */
17075 if (!decl || decl == error_mark_node)
17076 return error_mark_node;
17077
17078 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17079 if (TREE_CODE (decl) == TREE_LIST)
17080 {
17081 if (ambiguous_decls)
17082 *ambiguous_decls = decl;
17083 /* The error message we have to print is too complicated for
17084 cp_parser_error, so we incorporate its actions directly. */
17085 if (!cp_parser_simulate_error (parser))
17086 {
17087 error ("%Hreference to %qD is ambiguous",
17088 &name_location, name);
17089 print_candidates (decl);
17090 }
17091 return error_mark_node;
17092 }
17093
17094 gcc_assert (DECL_P (decl)
17095 || TREE_CODE (decl) == OVERLOAD
17096 || TREE_CODE (decl) == SCOPE_REF
17097 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17098 || BASELINK_P (decl));
17099
17100 /* If we have resolved the name of a member declaration, check to
17101 see if the declaration is accessible. When the name resolves to
17102 set of overloaded functions, accessibility is checked when
17103 overload resolution is done.
17104
17105 During an explicit instantiation, access is not checked at all,
17106 as per [temp.explicit]. */
17107 if (DECL_P (decl))
17108 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17109
17110 return decl;
17111 }
17112
17113 /* Like cp_parser_lookup_name, but for use in the typical case where
17114 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17115 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17116
17117 static tree
17118 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17119 {
17120 return cp_parser_lookup_name (parser, name,
17121 none_type,
17122 /*is_template=*/false,
17123 /*is_namespace=*/false,
17124 /*check_dependency=*/true,
17125 /*ambiguous_decls=*/NULL,
17126 location);
17127 }
17128
17129 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17130 the current context, return the TYPE_DECL. If TAG_NAME_P is
17131 true, the DECL indicates the class being defined in a class-head,
17132 or declared in an elaborated-type-specifier.
17133
17134 Otherwise, return DECL. */
17135
17136 static tree
17137 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17138 {
17139 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17140 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17141
17142 struct A {
17143 template <typename T> struct B;
17144 };
17145
17146 template <typename T> struct A::B {};
17147
17148 Similarly, in an elaborated-type-specifier:
17149
17150 namespace N { struct X{}; }
17151
17152 struct A {
17153 template <typename T> friend struct N::X;
17154 };
17155
17156 However, if the DECL refers to a class type, and we are in
17157 the scope of the class, then the name lookup automatically
17158 finds the TYPE_DECL created by build_self_reference rather
17159 than a TEMPLATE_DECL. For example, in:
17160
17161 template <class T> struct S {
17162 S s;
17163 };
17164
17165 there is no need to handle such case. */
17166
17167 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17168 return DECL_TEMPLATE_RESULT (decl);
17169
17170 return decl;
17171 }
17172
17173 /* If too many, or too few, template-parameter lists apply to the
17174 declarator, issue an error message. Returns TRUE if all went well,
17175 and FALSE otherwise. */
17176
17177 static bool
17178 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17179 cp_declarator *declarator,
17180 location_t declarator_location)
17181 {
17182 unsigned num_templates;
17183
17184 /* We haven't seen any classes that involve template parameters yet. */
17185 num_templates = 0;
17186
17187 switch (declarator->kind)
17188 {
17189 case cdk_id:
17190 if (declarator->u.id.qualifying_scope)
17191 {
17192 tree scope;
17193 tree member;
17194
17195 scope = declarator->u.id.qualifying_scope;
17196 member = declarator->u.id.unqualified_name;
17197
17198 while (scope && CLASS_TYPE_P (scope))
17199 {
17200 /* You're supposed to have one `template <...>'
17201 for every template class, but you don't need one
17202 for a full specialization. For example:
17203
17204 template <class T> struct S{};
17205 template <> struct S<int> { void f(); };
17206 void S<int>::f () {}
17207
17208 is correct; there shouldn't be a `template <>' for
17209 the definition of `S<int>::f'. */
17210 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17211 /* If SCOPE does not have template information of any
17212 kind, then it is not a template, nor is it nested
17213 within a template. */
17214 break;
17215 if (explicit_class_specialization_p (scope))
17216 break;
17217 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17218 ++num_templates;
17219
17220 scope = TYPE_CONTEXT (scope);
17221 }
17222 }
17223 else if (TREE_CODE (declarator->u.id.unqualified_name)
17224 == TEMPLATE_ID_EXPR)
17225 /* If the DECLARATOR has the form `X<y>' then it uses one
17226 additional level of template parameters. */
17227 ++num_templates;
17228
17229 return cp_parser_check_template_parameters (parser,
17230 num_templates,
17231 declarator_location);
17232
17233 case cdk_function:
17234 case cdk_array:
17235 case cdk_pointer:
17236 case cdk_reference:
17237 case cdk_ptrmem:
17238 return (cp_parser_check_declarator_template_parameters
17239 (parser, declarator->declarator, declarator_location));
17240
17241 case cdk_error:
17242 return true;
17243
17244 default:
17245 gcc_unreachable ();
17246 }
17247 return false;
17248 }
17249
17250 /* NUM_TEMPLATES were used in the current declaration. If that is
17251 invalid, return FALSE and issue an error messages. Otherwise,
17252 return TRUE. */
17253
17254 static bool
17255 cp_parser_check_template_parameters (cp_parser* parser,
17256 unsigned num_templates,
17257 location_t location)
17258 {
17259 /* If there are more template classes than parameter lists, we have
17260 something like:
17261
17262 template <class T> void S<T>::R<T>::f (); */
17263 if (parser->num_template_parameter_lists < num_templates)
17264 {
17265 error ("%Htoo few template-parameter-lists", &location);
17266 return false;
17267 }
17268 /* If there are the same number of template classes and parameter
17269 lists, that's OK. */
17270 if (parser->num_template_parameter_lists == num_templates)
17271 return true;
17272 /* If there are more, but only one more, then we are referring to a
17273 member template. That's OK too. */
17274 if (parser->num_template_parameter_lists == num_templates + 1)
17275 return true;
17276 /* Otherwise, there are too many template parameter lists. We have
17277 something like:
17278
17279 template <class T> template <class U> void S::f(); */
17280 error ("%Htoo many template-parameter-lists", &location);
17281 return false;
17282 }
17283
17284 /* Parse an optional `::' token indicating that the following name is
17285 from the global namespace. If so, PARSER->SCOPE is set to the
17286 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17287 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17288 Returns the new value of PARSER->SCOPE, if the `::' token is
17289 present, and NULL_TREE otherwise. */
17290
17291 static tree
17292 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17293 {
17294 cp_token *token;
17295
17296 /* Peek at the next token. */
17297 token = cp_lexer_peek_token (parser->lexer);
17298 /* If we're looking at a `::' token then we're starting from the
17299 global namespace, not our current location. */
17300 if (token->type == CPP_SCOPE)
17301 {
17302 /* Consume the `::' token. */
17303 cp_lexer_consume_token (parser->lexer);
17304 /* Set the SCOPE so that we know where to start the lookup. */
17305 parser->scope = global_namespace;
17306 parser->qualifying_scope = global_namespace;
17307 parser->object_scope = NULL_TREE;
17308
17309 return parser->scope;
17310 }
17311 else if (!current_scope_valid_p)
17312 {
17313 parser->scope = NULL_TREE;
17314 parser->qualifying_scope = NULL_TREE;
17315 parser->object_scope = NULL_TREE;
17316 }
17317
17318 return NULL_TREE;
17319 }
17320
17321 /* Returns TRUE if the upcoming token sequence is the start of a
17322 constructor declarator. If FRIEND_P is true, the declarator is
17323 preceded by the `friend' specifier. */
17324
17325 static bool
17326 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17327 {
17328 bool constructor_p;
17329 tree type_decl = NULL_TREE;
17330 bool nested_name_p;
17331 cp_token *next_token;
17332
17333 /* The common case is that this is not a constructor declarator, so
17334 try to avoid doing lots of work if at all possible. It's not
17335 valid declare a constructor at function scope. */
17336 if (parser->in_function_body)
17337 return false;
17338 /* And only certain tokens can begin a constructor declarator. */
17339 next_token = cp_lexer_peek_token (parser->lexer);
17340 if (next_token->type != CPP_NAME
17341 && next_token->type != CPP_SCOPE
17342 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17343 && next_token->type != CPP_TEMPLATE_ID)
17344 return false;
17345
17346 /* Parse tentatively; we are going to roll back all of the tokens
17347 consumed here. */
17348 cp_parser_parse_tentatively (parser);
17349 /* Assume that we are looking at a constructor declarator. */
17350 constructor_p = true;
17351
17352 /* Look for the optional `::' operator. */
17353 cp_parser_global_scope_opt (parser,
17354 /*current_scope_valid_p=*/false);
17355 /* Look for the nested-name-specifier. */
17356 nested_name_p
17357 = (cp_parser_nested_name_specifier_opt (parser,
17358 /*typename_keyword_p=*/false,
17359 /*check_dependency_p=*/false,
17360 /*type_p=*/false,
17361 /*is_declaration=*/false)
17362 != NULL_TREE);
17363 /* Outside of a class-specifier, there must be a
17364 nested-name-specifier. */
17365 if (!nested_name_p &&
17366 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17367 || friend_p))
17368 constructor_p = false;
17369 /* If we still think that this might be a constructor-declarator,
17370 look for a class-name. */
17371 if (constructor_p)
17372 {
17373 /* If we have:
17374
17375 template <typename T> struct S { S(); };
17376 template <typename T> S<T>::S ();
17377
17378 we must recognize that the nested `S' names a class.
17379 Similarly, for:
17380
17381 template <typename T> S<T>::S<T> ();
17382
17383 we must recognize that the nested `S' names a template. */
17384 type_decl = cp_parser_class_name (parser,
17385 /*typename_keyword_p=*/false,
17386 /*template_keyword_p=*/false,
17387 none_type,
17388 /*check_dependency_p=*/false,
17389 /*class_head_p=*/false,
17390 /*is_declaration=*/false);
17391 /* If there was no class-name, then this is not a constructor. */
17392 constructor_p = !cp_parser_error_occurred (parser);
17393 }
17394
17395 /* If we're still considering a constructor, we have to see a `(',
17396 to begin the parameter-declaration-clause, followed by either a
17397 `)', an `...', or a decl-specifier. We need to check for a
17398 type-specifier to avoid being fooled into thinking that:
17399
17400 S::S (f) (int);
17401
17402 is a constructor. (It is actually a function named `f' that
17403 takes one parameter (of type `int') and returns a value of type
17404 `S::S'. */
17405 if (constructor_p
17406 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17407 {
17408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17409 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17410 /* A parameter declaration begins with a decl-specifier,
17411 which is either the "attribute" keyword, a storage class
17412 specifier, or (usually) a type-specifier. */
17413 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17414 {
17415 tree type;
17416 tree pushed_scope = NULL_TREE;
17417 unsigned saved_num_template_parameter_lists;
17418
17419 /* Names appearing in the type-specifier should be looked up
17420 in the scope of the class. */
17421 if (current_class_type)
17422 type = NULL_TREE;
17423 else
17424 {
17425 type = TREE_TYPE (type_decl);
17426 if (TREE_CODE (type) == TYPENAME_TYPE)
17427 {
17428 type = resolve_typename_type (type,
17429 /*only_current_p=*/false);
17430 if (TREE_CODE (type) == TYPENAME_TYPE)
17431 {
17432 cp_parser_abort_tentative_parse (parser);
17433 return false;
17434 }
17435 }
17436 pushed_scope = push_scope (type);
17437 }
17438
17439 /* Inside the constructor parameter list, surrounding
17440 template-parameter-lists do not apply. */
17441 saved_num_template_parameter_lists
17442 = parser->num_template_parameter_lists;
17443 parser->num_template_parameter_lists = 0;
17444
17445 /* Look for the type-specifier. */
17446 cp_parser_type_specifier (parser,
17447 CP_PARSER_FLAGS_NONE,
17448 /*decl_specs=*/NULL,
17449 /*is_declarator=*/true,
17450 /*declares_class_or_enum=*/NULL,
17451 /*is_cv_qualifier=*/NULL);
17452
17453 parser->num_template_parameter_lists
17454 = saved_num_template_parameter_lists;
17455
17456 /* Leave the scope of the class. */
17457 if (pushed_scope)
17458 pop_scope (pushed_scope);
17459
17460 constructor_p = !cp_parser_error_occurred (parser);
17461 }
17462 }
17463 else
17464 constructor_p = false;
17465 /* We did not really want to consume any tokens. */
17466 cp_parser_abort_tentative_parse (parser);
17467
17468 return constructor_p;
17469 }
17470
17471 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17472 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17473 they must be performed once we are in the scope of the function.
17474
17475 Returns the function defined. */
17476
17477 static tree
17478 cp_parser_function_definition_from_specifiers_and_declarator
17479 (cp_parser* parser,
17480 cp_decl_specifier_seq *decl_specifiers,
17481 tree attributes,
17482 const cp_declarator *declarator)
17483 {
17484 tree fn;
17485 bool success_p;
17486
17487 /* Begin the function-definition. */
17488 success_p = start_function (decl_specifiers, declarator, attributes);
17489
17490 /* The things we're about to see are not directly qualified by any
17491 template headers we've seen thus far. */
17492 reset_specialization ();
17493
17494 /* If there were names looked up in the decl-specifier-seq that we
17495 did not check, check them now. We must wait until we are in the
17496 scope of the function to perform the checks, since the function
17497 might be a friend. */
17498 perform_deferred_access_checks ();
17499
17500 if (!success_p)
17501 {
17502 /* Skip the entire function. */
17503 cp_parser_skip_to_end_of_block_or_statement (parser);
17504 fn = error_mark_node;
17505 }
17506 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17507 {
17508 /* Seen already, skip it. An error message has already been output. */
17509 cp_parser_skip_to_end_of_block_or_statement (parser);
17510 fn = current_function_decl;
17511 current_function_decl = NULL_TREE;
17512 /* If this is a function from a class, pop the nested class. */
17513 if (current_class_name)
17514 pop_nested_class ();
17515 }
17516 else
17517 fn = cp_parser_function_definition_after_declarator (parser,
17518 /*inline_p=*/false);
17519
17520 return fn;
17521 }
17522
17523 /* Parse the part of a function-definition that follows the
17524 declarator. INLINE_P is TRUE iff this function is an inline
17525 function defined with a class-specifier.
17526
17527 Returns the function defined. */
17528
17529 static tree
17530 cp_parser_function_definition_after_declarator (cp_parser* parser,
17531 bool inline_p)
17532 {
17533 tree fn;
17534 bool ctor_initializer_p = false;
17535 bool saved_in_unbraced_linkage_specification_p;
17536 bool saved_in_function_body;
17537 unsigned saved_num_template_parameter_lists;
17538 cp_token *token;
17539
17540 saved_in_function_body = parser->in_function_body;
17541 parser->in_function_body = true;
17542 /* If the next token is `return', then the code may be trying to
17543 make use of the "named return value" extension that G++ used to
17544 support. */
17545 token = cp_lexer_peek_token (parser->lexer);
17546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17547 {
17548 /* Consume the `return' keyword. */
17549 cp_lexer_consume_token (parser->lexer);
17550 /* Look for the identifier that indicates what value is to be
17551 returned. */
17552 cp_parser_identifier (parser);
17553 /* Issue an error message. */
17554 error ("%Hnamed return values are no longer supported",
17555 &token->location);
17556 /* Skip tokens until we reach the start of the function body. */
17557 while (true)
17558 {
17559 cp_token *token = cp_lexer_peek_token (parser->lexer);
17560 if (token->type == CPP_OPEN_BRACE
17561 || token->type == CPP_EOF
17562 || token->type == CPP_PRAGMA_EOL)
17563 break;
17564 cp_lexer_consume_token (parser->lexer);
17565 }
17566 }
17567 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17568 anything declared inside `f'. */
17569 saved_in_unbraced_linkage_specification_p
17570 = parser->in_unbraced_linkage_specification_p;
17571 parser->in_unbraced_linkage_specification_p = false;
17572 /* Inside the function, surrounding template-parameter-lists do not
17573 apply. */
17574 saved_num_template_parameter_lists
17575 = parser->num_template_parameter_lists;
17576 parser->num_template_parameter_lists = 0;
17577 /* If the next token is `try', then we are looking at a
17578 function-try-block. */
17579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17580 ctor_initializer_p = cp_parser_function_try_block (parser);
17581 /* A function-try-block includes the function-body, so we only do
17582 this next part if we're not processing a function-try-block. */
17583 else
17584 ctor_initializer_p
17585 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17586
17587 /* Finish the function. */
17588 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17589 (inline_p ? 2 : 0));
17590 /* Generate code for it, if necessary. */
17591 expand_or_defer_fn (fn);
17592 /* Restore the saved values. */
17593 parser->in_unbraced_linkage_specification_p
17594 = saved_in_unbraced_linkage_specification_p;
17595 parser->num_template_parameter_lists
17596 = saved_num_template_parameter_lists;
17597 parser->in_function_body = saved_in_function_body;
17598
17599 return fn;
17600 }
17601
17602 /* Parse a template-declaration, assuming that the `export' (and
17603 `extern') keywords, if present, has already been scanned. MEMBER_P
17604 is as for cp_parser_template_declaration. */
17605
17606 static void
17607 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17608 {
17609 tree decl = NULL_TREE;
17610 VEC (deferred_access_check,gc) *checks;
17611 tree parameter_list;
17612 bool friend_p = false;
17613 bool need_lang_pop;
17614 cp_token *token;
17615
17616 /* Look for the `template' keyword. */
17617 token = cp_lexer_peek_token (parser->lexer);
17618 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17619 return;
17620
17621 /* And the `<'. */
17622 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17623 return;
17624 if (at_class_scope_p () && current_function_decl)
17625 {
17626 /* 14.5.2.2 [temp.mem]
17627
17628 A local class shall not have member templates. */
17629 error ("%Hinvalid declaration of member template in local class",
17630 &token->location);
17631 cp_parser_skip_to_end_of_block_or_statement (parser);
17632 return;
17633 }
17634 /* [temp]
17635
17636 A template ... shall not have C linkage. */
17637 if (current_lang_name == lang_name_c)
17638 {
17639 error ("%Htemplate with C linkage", &token->location);
17640 /* Give it C++ linkage to avoid confusing other parts of the
17641 front end. */
17642 push_lang_context (lang_name_cplusplus);
17643 need_lang_pop = true;
17644 }
17645 else
17646 need_lang_pop = false;
17647
17648 /* We cannot perform access checks on the template parameter
17649 declarations until we know what is being declared, just as we
17650 cannot check the decl-specifier list. */
17651 push_deferring_access_checks (dk_deferred);
17652
17653 /* If the next token is `>', then we have an invalid
17654 specialization. Rather than complain about an invalid template
17655 parameter, issue an error message here. */
17656 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17657 {
17658 cp_parser_error (parser, "invalid explicit specialization");
17659 begin_specialization ();
17660 parameter_list = NULL_TREE;
17661 }
17662 else
17663 /* Parse the template parameters. */
17664 parameter_list = cp_parser_template_parameter_list (parser);
17665
17666 /* Get the deferred access checks from the parameter list. These
17667 will be checked once we know what is being declared, as for a
17668 member template the checks must be performed in the scope of the
17669 class containing the member. */
17670 checks = get_deferred_access_checks ();
17671
17672 /* Look for the `>'. */
17673 cp_parser_skip_to_end_of_template_parameter_list (parser);
17674 /* We just processed one more parameter list. */
17675 ++parser->num_template_parameter_lists;
17676 /* If the next token is `template', there are more template
17677 parameters. */
17678 if (cp_lexer_next_token_is_keyword (parser->lexer,
17679 RID_TEMPLATE))
17680 cp_parser_template_declaration_after_export (parser, member_p);
17681 else
17682 {
17683 /* There are no access checks when parsing a template, as we do not
17684 know if a specialization will be a friend. */
17685 push_deferring_access_checks (dk_no_check);
17686 token = cp_lexer_peek_token (parser->lexer);
17687 decl = cp_parser_single_declaration (parser,
17688 checks,
17689 member_p,
17690 /*explicit_specialization_p=*/false,
17691 &friend_p);
17692 pop_deferring_access_checks ();
17693
17694 /* If this is a member template declaration, let the front
17695 end know. */
17696 if (member_p && !friend_p && decl)
17697 {
17698 if (TREE_CODE (decl) == TYPE_DECL)
17699 cp_parser_check_access_in_redeclaration (decl, token->location);
17700
17701 decl = finish_member_template_decl (decl);
17702 }
17703 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17704 make_friend_class (current_class_type, TREE_TYPE (decl),
17705 /*complain=*/true);
17706 }
17707 /* We are done with the current parameter list. */
17708 --parser->num_template_parameter_lists;
17709
17710 pop_deferring_access_checks ();
17711
17712 /* Finish up. */
17713 finish_template_decl (parameter_list);
17714
17715 /* Register member declarations. */
17716 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17717 finish_member_declaration (decl);
17718 /* For the erroneous case of a template with C linkage, we pushed an
17719 implicit C++ linkage scope; exit that scope now. */
17720 if (need_lang_pop)
17721 pop_lang_context ();
17722 /* If DECL is a function template, we must return to parse it later.
17723 (Even though there is no definition, there might be default
17724 arguments that need handling.) */
17725 if (member_p && decl
17726 && (TREE_CODE (decl) == FUNCTION_DECL
17727 || DECL_FUNCTION_TEMPLATE_P (decl)))
17728 TREE_VALUE (parser->unparsed_functions_queues)
17729 = tree_cons (NULL_TREE, decl,
17730 TREE_VALUE (parser->unparsed_functions_queues));
17731 }
17732
17733 /* Perform the deferred access checks from a template-parameter-list.
17734 CHECKS is a TREE_LIST of access checks, as returned by
17735 get_deferred_access_checks. */
17736
17737 static void
17738 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17739 {
17740 ++processing_template_parmlist;
17741 perform_access_checks (checks);
17742 --processing_template_parmlist;
17743 }
17744
17745 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17746 `function-definition' sequence. MEMBER_P is true, this declaration
17747 appears in a class scope.
17748
17749 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17750 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17751
17752 static tree
17753 cp_parser_single_declaration (cp_parser* parser,
17754 VEC (deferred_access_check,gc)* checks,
17755 bool member_p,
17756 bool explicit_specialization_p,
17757 bool* friend_p)
17758 {
17759 int declares_class_or_enum;
17760 tree decl = NULL_TREE;
17761 cp_decl_specifier_seq decl_specifiers;
17762 bool function_definition_p = false;
17763 cp_token *decl_spec_token_start;
17764
17765 /* This function is only used when processing a template
17766 declaration. */
17767 gcc_assert (innermost_scope_kind () == sk_template_parms
17768 || innermost_scope_kind () == sk_template_spec);
17769
17770 /* Defer access checks until we know what is being declared. */
17771 push_deferring_access_checks (dk_deferred);
17772
17773 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17774 alternative. */
17775 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17776 cp_parser_decl_specifier_seq (parser,
17777 CP_PARSER_FLAGS_OPTIONAL,
17778 &decl_specifiers,
17779 &declares_class_or_enum);
17780 if (friend_p)
17781 *friend_p = cp_parser_friend_p (&decl_specifiers);
17782
17783 /* There are no template typedefs. */
17784 if (decl_specifiers.specs[(int) ds_typedef])
17785 {
17786 error ("%Htemplate declaration of %qs",
17787 &decl_spec_token_start->location, "typedef");
17788 decl = error_mark_node;
17789 }
17790
17791 /* Gather up the access checks that occurred the
17792 decl-specifier-seq. */
17793 stop_deferring_access_checks ();
17794
17795 /* Check for the declaration of a template class. */
17796 if (declares_class_or_enum)
17797 {
17798 if (cp_parser_declares_only_class_p (parser))
17799 {
17800 decl = shadow_tag (&decl_specifiers);
17801
17802 /* In this case:
17803
17804 struct C {
17805 friend template <typename T> struct A<T>::B;
17806 };
17807
17808 A<T>::B will be represented by a TYPENAME_TYPE, and
17809 therefore not recognized by shadow_tag. */
17810 if (friend_p && *friend_p
17811 && !decl
17812 && decl_specifiers.type
17813 && TYPE_P (decl_specifiers.type))
17814 decl = decl_specifiers.type;
17815
17816 if (decl && decl != error_mark_node)
17817 decl = TYPE_NAME (decl);
17818 else
17819 decl = error_mark_node;
17820
17821 /* Perform access checks for template parameters. */
17822 cp_parser_perform_template_parameter_access_checks (checks);
17823 }
17824 }
17825 /* If it's not a template class, try for a template function. If
17826 the next token is a `;', then this declaration does not declare
17827 anything. But, if there were errors in the decl-specifiers, then
17828 the error might well have come from an attempted class-specifier.
17829 In that case, there's no need to warn about a missing declarator. */
17830 if (!decl
17831 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17832 || decl_specifiers.type != error_mark_node))
17833 {
17834 decl = cp_parser_init_declarator (parser,
17835 &decl_specifiers,
17836 checks,
17837 /*function_definition_allowed_p=*/true,
17838 member_p,
17839 declares_class_or_enum,
17840 &function_definition_p);
17841
17842 /* 7.1.1-1 [dcl.stc]
17843
17844 A storage-class-specifier shall not be specified in an explicit
17845 specialization... */
17846 if (decl
17847 && explicit_specialization_p
17848 && decl_specifiers.storage_class != sc_none)
17849 {
17850 error ("%Hexplicit template specialization cannot have a storage class",
17851 &decl_spec_token_start->location);
17852 decl = error_mark_node;
17853 }
17854 }
17855
17856 pop_deferring_access_checks ();
17857
17858 /* Clear any current qualification; whatever comes next is the start
17859 of something new. */
17860 parser->scope = NULL_TREE;
17861 parser->qualifying_scope = NULL_TREE;
17862 parser->object_scope = NULL_TREE;
17863 /* Look for a trailing `;' after the declaration. */
17864 if (!function_definition_p
17865 && (decl == error_mark_node
17866 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17867 cp_parser_skip_to_end_of_block_or_statement (parser);
17868
17869 return decl;
17870 }
17871
17872 /* Parse a cast-expression that is not the operand of a unary "&". */
17873
17874 static tree
17875 cp_parser_simple_cast_expression (cp_parser *parser)
17876 {
17877 return cp_parser_cast_expression (parser, /*address_p=*/false,
17878 /*cast_p=*/false);
17879 }
17880
17881 /* Parse a functional cast to TYPE. Returns an expression
17882 representing the cast. */
17883
17884 static tree
17885 cp_parser_functional_cast (cp_parser* parser, tree type)
17886 {
17887 tree expression_list;
17888 tree cast;
17889 bool nonconst_p;
17890
17891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17892 {
17893 maybe_warn_cpp0x ("extended initializer lists");
17894 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17895 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17896 if (TREE_CODE (type) == TYPE_DECL)
17897 type = TREE_TYPE (type);
17898 return finish_compound_literal (type, expression_list);
17899 }
17900
17901 expression_list
17902 = cp_parser_parenthesized_expression_list (parser, false,
17903 /*cast_p=*/true,
17904 /*allow_expansion_p=*/true,
17905 /*non_constant_p=*/NULL);
17906
17907 cast = build_functional_cast (type, expression_list,
17908 tf_warning_or_error);
17909 /* [expr.const]/1: In an integral constant expression "only type
17910 conversions to integral or enumeration type can be used". */
17911 if (TREE_CODE (type) == TYPE_DECL)
17912 type = TREE_TYPE (type);
17913 if (cast != error_mark_node
17914 && !cast_valid_in_integral_constant_expression_p (type)
17915 && (cp_parser_non_integral_constant_expression
17916 (parser, "a call to a constructor")))
17917 return error_mark_node;
17918 return cast;
17919 }
17920
17921 /* Save the tokens that make up the body of a member function defined
17922 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17923 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17924 specifiers applied to the declaration. Returns the FUNCTION_DECL
17925 for the member function. */
17926
17927 static tree
17928 cp_parser_save_member_function_body (cp_parser* parser,
17929 cp_decl_specifier_seq *decl_specifiers,
17930 cp_declarator *declarator,
17931 tree attributes)
17932 {
17933 cp_token *first;
17934 cp_token *last;
17935 tree fn;
17936
17937 /* Create the function-declaration. */
17938 fn = start_method (decl_specifiers, declarator, attributes);
17939 /* If something went badly wrong, bail out now. */
17940 if (fn == error_mark_node)
17941 {
17942 /* If there's a function-body, skip it. */
17943 if (cp_parser_token_starts_function_definition_p
17944 (cp_lexer_peek_token (parser->lexer)))
17945 cp_parser_skip_to_end_of_block_or_statement (parser);
17946 return error_mark_node;
17947 }
17948
17949 /* Remember it, if there default args to post process. */
17950 cp_parser_save_default_args (parser, fn);
17951
17952 /* Save away the tokens that make up the body of the
17953 function. */
17954 first = parser->lexer->next_token;
17955 /* We can have braced-init-list mem-initializers before the fn body. */
17956 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17957 {
17958 cp_lexer_consume_token (parser->lexer);
17959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17960 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17961 {
17962 /* cache_group will stop after an un-nested { } pair, too. */
17963 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17964 break;
17965
17966 /* variadic mem-inits have ... after the ')'. */
17967 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17968 cp_lexer_consume_token (parser->lexer);
17969 }
17970 }
17971 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17972 /* Handle function try blocks. */
17973 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17974 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17975 last = parser->lexer->next_token;
17976
17977 /* Save away the inline definition; we will process it when the
17978 class is complete. */
17979 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17980 DECL_PENDING_INLINE_P (fn) = 1;
17981
17982 /* We need to know that this was defined in the class, so that
17983 friend templates are handled correctly. */
17984 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17985
17986 /* We're done with the inline definition. */
17987 finish_method (fn);
17988
17989 /* Add FN to the queue of functions to be parsed later. */
17990 TREE_VALUE (parser->unparsed_functions_queues)
17991 = tree_cons (NULL_TREE, fn,
17992 TREE_VALUE (parser->unparsed_functions_queues));
17993
17994 return fn;
17995 }
17996
17997 /* Parse a template-argument-list, as well as the trailing ">" (but
17998 not the opening ">"). See cp_parser_template_argument_list for the
17999 return value. */
18000
18001 static tree
18002 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18003 {
18004 tree arguments;
18005 tree saved_scope;
18006 tree saved_qualifying_scope;
18007 tree saved_object_scope;
18008 bool saved_greater_than_is_operator_p;
18009 bool saved_skip_evaluation;
18010
18011 /* [temp.names]
18012
18013 When parsing a template-id, the first non-nested `>' is taken as
18014 the end of the template-argument-list rather than a greater-than
18015 operator. */
18016 saved_greater_than_is_operator_p
18017 = parser->greater_than_is_operator_p;
18018 parser->greater_than_is_operator_p = false;
18019 /* Parsing the argument list may modify SCOPE, so we save it
18020 here. */
18021 saved_scope = parser->scope;
18022 saved_qualifying_scope = parser->qualifying_scope;
18023 saved_object_scope = parser->object_scope;
18024 /* We need to evaluate the template arguments, even though this
18025 template-id may be nested within a "sizeof". */
18026 saved_skip_evaluation = skip_evaluation;
18027 skip_evaluation = false;
18028 /* Parse the template-argument-list itself. */
18029 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18030 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18031 arguments = NULL_TREE;
18032 else
18033 arguments = cp_parser_template_argument_list (parser);
18034 /* Look for the `>' that ends the template-argument-list. If we find
18035 a '>>' instead, it's probably just a typo. */
18036 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18037 {
18038 if (cxx_dialect != cxx98)
18039 {
18040 /* In C++0x, a `>>' in a template argument list or cast
18041 expression is considered to be two separate `>'
18042 tokens. So, change the current token to a `>', but don't
18043 consume it: it will be consumed later when the outer
18044 template argument list (or cast expression) is parsed.
18045 Note that this replacement of `>' for `>>' is necessary
18046 even if we are parsing tentatively: in the tentative
18047 case, after calling
18048 cp_parser_enclosed_template_argument_list we will always
18049 throw away all of the template arguments and the first
18050 closing `>', either because the template argument list
18051 was erroneous or because we are replacing those tokens
18052 with a CPP_TEMPLATE_ID token. The second `>' (which will
18053 not have been thrown away) is needed either to close an
18054 outer template argument list or to complete a new-style
18055 cast. */
18056 cp_token *token = cp_lexer_peek_token (parser->lexer);
18057 token->type = CPP_GREATER;
18058 }
18059 else if (!saved_greater_than_is_operator_p)
18060 {
18061 /* If we're in a nested template argument list, the '>>' has
18062 to be a typo for '> >'. We emit the error message, but we
18063 continue parsing and we push a '>' as next token, so that
18064 the argument list will be parsed correctly. Note that the
18065 global source location is still on the token before the
18066 '>>', so we need to say explicitly where we want it. */
18067 cp_token *token = cp_lexer_peek_token (parser->lexer);
18068 error ("%H%<>>%> should be %<> >%> "
18069 "within a nested template argument list",
18070 &token->location);
18071
18072 token->type = CPP_GREATER;
18073 }
18074 else
18075 {
18076 /* If this is not a nested template argument list, the '>>'
18077 is a typo for '>'. Emit an error message and continue.
18078 Same deal about the token location, but here we can get it
18079 right by consuming the '>>' before issuing the diagnostic. */
18080 cp_token *token = cp_lexer_consume_token (parser->lexer);
18081 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18082 "a template argument list", &token->location);
18083 }
18084 }
18085 else
18086 cp_parser_skip_to_end_of_template_parameter_list (parser);
18087 /* The `>' token might be a greater-than operator again now. */
18088 parser->greater_than_is_operator_p
18089 = saved_greater_than_is_operator_p;
18090 /* Restore the SAVED_SCOPE. */
18091 parser->scope = saved_scope;
18092 parser->qualifying_scope = saved_qualifying_scope;
18093 parser->object_scope = saved_object_scope;
18094 skip_evaluation = saved_skip_evaluation;
18095
18096 return arguments;
18097 }
18098
18099 /* MEMBER_FUNCTION is a member function, or a friend. If default
18100 arguments, or the body of the function have not yet been parsed,
18101 parse them now. */
18102
18103 static void
18104 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18105 {
18106 /* If this member is a template, get the underlying
18107 FUNCTION_DECL. */
18108 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18109 member_function = DECL_TEMPLATE_RESULT (member_function);
18110
18111 /* There should not be any class definitions in progress at this
18112 point; the bodies of members are only parsed outside of all class
18113 definitions. */
18114 gcc_assert (parser->num_classes_being_defined == 0);
18115 /* While we're parsing the member functions we might encounter more
18116 classes. We want to handle them right away, but we don't want
18117 them getting mixed up with functions that are currently in the
18118 queue. */
18119 parser->unparsed_functions_queues
18120 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18121
18122 /* Make sure that any template parameters are in scope. */
18123 maybe_begin_member_template_processing (member_function);
18124
18125 /* If the body of the function has not yet been parsed, parse it
18126 now. */
18127 if (DECL_PENDING_INLINE_P (member_function))
18128 {
18129 tree function_scope;
18130 cp_token_cache *tokens;
18131
18132 /* The function is no longer pending; we are processing it. */
18133 tokens = DECL_PENDING_INLINE_INFO (member_function);
18134 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18135 DECL_PENDING_INLINE_P (member_function) = 0;
18136
18137 /* If this is a local class, enter the scope of the containing
18138 function. */
18139 function_scope = current_function_decl;
18140 if (function_scope)
18141 push_function_context ();
18142
18143 /* Push the body of the function onto the lexer stack. */
18144 cp_parser_push_lexer_for_tokens (parser, tokens);
18145
18146 /* Let the front end know that we going to be defining this
18147 function. */
18148 start_preparsed_function (member_function, NULL_TREE,
18149 SF_PRE_PARSED | SF_INCLASS_INLINE);
18150
18151 /* Don't do access checking if it is a templated function. */
18152 if (processing_template_decl)
18153 push_deferring_access_checks (dk_no_check);
18154
18155 /* Now, parse the body of the function. */
18156 cp_parser_function_definition_after_declarator (parser,
18157 /*inline_p=*/true);
18158
18159 if (processing_template_decl)
18160 pop_deferring_access_checks ();
18161
18162 /* Leave the scope of the containing function. */
18163 if (function_scope)
18164 pop_function_context ();
18165 cp_parser_pop_lexer (parser);
18166 }
18167
18168 /* Remove any template parameters from the symbol table. */
18169 maybe_end_member_template_processing ();
18170
18171 /* Restore the queue. */
18172 parser->unparsed_functions_queues
18173 = TREE_CHAIN (parser->unparsed_functions_queues);
18174 }
18175
18176 /* If DECL contains any default args, remember it on the unparsed
18177 functions queue. */
18178
18179 static void
18180 cp_parser_save_default_args (cp_parser* parser, tree decl)
18181 {
18182 tree probe;
18183
18184 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18185 probe;
18186 probe = TREE_CHAIN (probe))
18187 if (TREE_PURPOSE (probe))
18188 {
18189 TREE_PURPOSE (parser->unparsed_functions_queues)
18190 = tree_cons (current_class_type, decl,
18191 TREE_PURPOSE (parser->unparsed_functions_queues));
18192 break;
18193 }
18194 }
18195
18196 /* FN is a FUNCTION_DECL which may contains a parameter with an
18197 unparsed DEFAULT_ARG. Parse the default args now. This function
18198 assumes that the current scope is the scope in which the default
18199 argument should be processed. */
18200
18201 static void
18202 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18203 {
18204 bool saved_local_variables_forbidden_p;
18205 tree parm;
18206
18207 /* While we're parsing the default args, we might (due to the
18208 statement expression extension) encounter more classes. We want
18209 to handle them right away, but we don't want them getting mixed
18210 up with default args that are currently in the queue. */
18211 parser->unparsed_functions_queues
18212 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18213
18214 /* Local variable names (and the `this' keyword) may not appear
18215 in a default argument. */
18216 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18217 parser->local_variables_forbidden_p = true;
18218
18219 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18220 parm;
18221 parm = TREE_CHAIN (parm))
18222 {
18223 cp_token_cache *tokens;
18224 tree default_arg = TREE_PURPOSE (parm);
18225 tree parsed_arg;
18226 VEC(tree,gc) *insts;
18227 tree copy;
18228 unsigned ix;
18229
18230 if (!default_arg)
18231 continue;
18232
18233 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18234 /* This can happen for a friend declaration for a function
18235 already declared with default arguments. */
18236 continue;
18237
18238 /* Push the saved tokens for the default argument onto the parser's
18239 lexer stack. */
18240 tokens = DEFARG_TOKENS (default_arg);
18241 cp_parser_push_lexer_for_tokens (parser, tokens);
18242
18243 /* Parse the assignment-expression. */
18244 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18245
18246 if (!processing_template_decl)
18247 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18248
18249 TREE_PURPOSE (parm) = parsed_arg;
18250
18251 /* Update any instantiations we've already created. */
18252 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18253 VEC_iterate (tree, insts, ix, copy); ix++)
18254 TREE_PURPOSE (copy) = parsed_arg;
18255
18256 /* If the token stream has not been completely used up, then
18257 there was extra junk after the end of the default
18258 argument. */
18259 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18260 cp_parser_error (parser, "expected %<,%>");
18261
18262 /* Revert to the main lexer. */
18263 cp_parser_pop_lexer (parser);
18264 }
18265
18266 /* Make sure no default arg is missing. */
18267 check_default_args (fn);
18268
18269 /* Restore the state of local_variables_forbidden_p. */
18270 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18271
18272 /* Restore the queue. */
18273 parser->unparsed_functions_queues
18274 = TREE_CHAIN (parser->unparsed_functions_queues);
18275 }
18276
18277 /* Parse the operand of `sizeof' (or a similar operator). Returns
18278 either a TYPE or an expression, depending on the form of the
18279 input. The KEYWORD indicates which kind of expression we have
18280 encountered. */
18281
18282 static tree
18283 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18284 {
18285 tree expr = NULL_TREE;
18286 const char *saved_message;
18287 char *tmp;
18288 bool saved_integral_constant_expression_p;
18289 bool saved_non_integral_constant_expression_p;
18290 bool pack_expansion_p = false;
18291
18292 /* Types cannot be defined in a `sizeof' expression. Save away the
18293 old message. */
18294 saved_message = parser->type_definition_forbidden_message;
18295 /* And create the new one. */
18296 tmp = concat ("types may not be defined in %<",
18297 IDENTIFIER_POINTER (ridpointers[keyword]),
18298 "%> expressions", NULL);
18299 parser->type_definition_forbidden_message = tmp;
18300
18301 /* The restrictions on constant-expressions do not apply inside
18302 sizeof expressions. */
18303 saved_integral_constant_expression_p
18304 = parser->integral_constant_expression_p;
18305 saved_non_integral_constant_expression_p
18306 = parser->non_integral_constant_expression_p;
18307 parser->integral_constant_expression_p = false;
18308
18309 /* If it's a `...', then we are computing the length of a parameter
18310 pack. */
18311 if (keyword == RID_SIZEOF
18312 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18313 {
18314 /* Consume the `...'. */
18315 cp_lexer_consume_token (parser->lexer);
18316 maybe_warn_variadic_templates ();
18317
18318 /* Note that this is an expansion. */
18319 pack_expansion_p = true;
18320 }
18321
18322 /* Do not actually evaluate the expression. */
18323 ++skip_evaluation;
18324 /* If it's a `(', then we might be looking at the type-id
18325 construction. */
18326 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18327 {
18328 tree type;
18329 bool saved_in_type_id_in_expr_p;
18330
18331 /* We can't be sure yet whether we're looking at a type-id or an
18332 expression. */
18333 cp_parser_parse_tentatively (parser);
18334 /* Consume the `('. */
18335 cp_lexer_consume_token (parser->lexer);
18336 /* Parse the type-id. */
18337 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18338 parser->in_type_id_in_expr_p = true;
18339 type = cp_parser_type_id (parser);
18340 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18341 /* Now, look for the trailing `)'. */
18342 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18343 /* If all went well, then we're done. */
18344 if (cp_parser_parse_definitely (parser))
18345 {
18346 cp_decl_specifier_seq decl_specs;
18347
18348 /* Build a trivial decl-specifier-seq. */
18349 clear_decl_specs (&decl_specs);
18350 decl_specs.type = type;
18351
18352 /* Call grokdeclarator to figure out what type this is. */
18353 expr = grokdeclarator (NULL,
18354 &decl_specs,
18355 TYPENAME,
18356 /*initialized=*/0,
18357 /*attrlist=*/NULL);
18358 }
18359 }
18360
18361 /* If the type-id production did not work out, then we must be
18362 looking at the unary-expression production. */
18363 if (!expr)
18364 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18365 /*cast_p=*/false);
18366
18367 if (pack_expansion_p)
18368 /* Build a pack expansion. */
18369 expr = make_pack_expansion (expr);
18370
18371 /* Go back to evaluating expressions. */
18372 --skip_evaluation;
18373
18374 /* Free the message we created. */
18375 free (tmp);
18376 /* And restore the old one. */
18377 parser->type_definition_forbidden_message = saved_message;
18378 parser->integral_constant_expression_p
18379 = saved_integral_constant_expression_p;
18380 parser->non_integral_constant_expression_p
18381 = saved_non_integral_constant_expression_p;
18382
18383 return expr;
18384 }
18385
18386 /* If the current declaration has no declarator, return true. */
18387
18388 static bool
18389 cp_parser_declares_only_class_p (cp_parser *parser)
18390 {
18391 /* If the next token is a `;' or a `,' then there is no
18392 declarator. */
18393 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18394 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18395 }
18396
18397 /* Update the DECL_SPECS to reflect the storage class indicated by
18398 KEYWORD. */
18399
18400 static void
18401 cp_parser_set_storage_class (cp_parser *parser,
18402 cp_decl_specifier_seq *decl_specs,
18403 enum rid keyword,
18404 location_t location)
18405 {
18406 cp_storage_class storage_class;
18407
18408 if (parser->in_unbraced_linkage_specification_p)
18409 {
18410 error ("%Hinvalid use of %qD in linkage specification",
18411 &location, ridpointers[keyword]);
18412 return;
18413 }
18414 else if (decl_specs->storage_class != sc_none)
18415 {
18416 decl_specs->conflicting_specifiers_p = true;
18417 return;
18418 }
18419
18420 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18421 && decl_specs->specs[(int) ds_thread])
18422 {
18423 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18424 decl_specs->specs[(int) ds_thread] = 0;
18425 }
18426
18427 switch (keyword)
18428 {
18429 case RID_AUTO:
18430 storage_class = sc_auto;
18431 break;
18432 case RID_REGISTER:
18433 storage_class = sc_register;
18434 break;
18435 case RID_STATIC:
18436 storage_class = sc_static;
18437 break;
18438 case RID_EXTERN:
18439 storage_class = sc_extern;
18440 break;
18441 case RID_MUTABLE:
18442 storage_class = sc_mutable;
18443 break;
18444 default:
18445 gcc_unreachable ();
18446 }
18447 decl_specs->storage_class = storage_class;
18448
18449 /* A storage class specifier cannot be applied alongside a typedef
18450 specifier. If there is a typedef specifier present then set
18451 conflicting_specifiers_p which will trigger an error later
18452 on in grokdeclarator. */
18453 if (decl_specs->specs[(int)ds_typedef])
18454 decl_specs->conflicting_specifiers_p = true;
18455 }
18456
18457 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18458 is true, the type is a user-defined type; otherwise it is a
18459 built-in type specified by a keyword. */
18460
18461 static void
18462 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18463 tree type_spec,
18464 location_t location,
18465 bool user_defined_p)
18466 {
18467 decl_specs->any_specifiers_p = true;
18468
18469 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18470 (with, for example, in "typedef int wchar_t;") we remember that
18471 this is what happened. In system headers, we ignore these
18472 declarations so that G++ can work with system headers that are not
18473 C++-safe. */
18474 if (decl_specs->specs[(int) ds_typedef]
18475 && !user_defined_p
18476 && (type_spec == boolean_type_node
18477 || type_spec == char16_type_node
18478 || type_spec == char32_type_node
18479 || type_spec == wchar_type_node)
18480 && (decl_specs->type
18481 || decl_specs->specs[(int) ds_long]
18482 || decl_specs->specs[(int) ds_short]
18483 || decl_specs->specs[(int) ds_unsigned]
18484 || decl_specs->specs[(int) ds_signed]))
18485 {
18486 decl_specs->redefined_builtin_type = type_spec;
18487 if (!decl_specs->type)
18488 {
18489 decl_specs->type = type_spec;
18490 decl_specs->user_defined_type_p = false;
18491 decl_specs->type_location = location;
18492 }
18493 }
18494 else if (decl_specs->type)
18495 decl_specs->multiple_types_p = true;
18496 else
18497 {
18498 decl_specs->type = type_spec;
18499 decl_specs->user_defined_type_p = user_defined_p;
18500 decl_specs->redefined_builtin_type = NULL_TREE;
18501 decl_specs->type_location = location;
18502 }
18503 }
18504
18505 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18506 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18507
18508 static bool
18509 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18510 {
18511 return decl_specifiers->specs[(int) ds_friend] != 0;
18512 }
18513
18514 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18515 issue an error message indicating that TOKEN_DESC was expected.
18516
18517 Returns the token consumed, if the token had the appropriate type.
18518 Otherwise, returns NULL. */
18519
18520 static cp_token *
18521 cp_parser_require (cp_parser* parser,
18522 enum cpp_ttype type,
18523 const char* token_desc)
18524 {
18525 if (cp_lexer_next_token_is (parser->lexer, type))
18526 return cp_lexer_consume_token (parser->lexer);
18527 else
18528 {
18529 /* Output the MESSAGE -- unless we're parsing tentatively. */
18530 if (!cp_parser_simulate_error (parser))
18531 {
18532 char *message = concat ("expected ", token_desc, NULL);
18533 cp_parser_error (parser, message);
18534 free (message);
18535 }
18536 return NULL;
18537 }
18538 }
18539
18540 /* An error message is produced if the next token is not '>'.
18541 All further tokens are skipped until the desired token is
18542 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18543
18544 static void
18545 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18546 {
18547 /* Current level of '< ... >'. */
18548 unsigned level = 0;
18549 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18550 unsigned nesting_depth = 0;
18551
18552 /* Are we ready, yet? If not, issue error message. */
18553 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18554 return;
18555
18556 /* Skip tokens until the desired token is found. */
18557 while (true)
18558 {
18559 /* Peek at the next token. */
18560 switch (cp_lexer_peek_token (parser->lexer)->type)
18561 {
18562 case CPP_LESS:
18563 if (!nesting_depth)
18564 ++level;
18565 break;
18566
18567 case CPP_RSHIFT:
18568 if (cxx_dialect == cxx98)
18569 /* C++0x views the `>>' operator as two `>' tokens, but
18570 C++98 does not. */
18571 break;
18572 else if (!nesting_depth && level-- == 0)
18573 {
18574 /* We've hit a `>>' where the first `>' closes the
18575 template argument list, and the second `>' is
18576 spurious. Just consume the `>>' and stop; we've
18577 already produced at least one error. */
18578 cp_lexer_consume_token (parser->lexer);
18579 return;
18580 }
18581 /* Fall through for C++0x, so we handle the second `>' in
18582 the `>>'. */
18583
18584 case CPP_GREATER:
18585 if (!nesting_depth && level-- == 0)
18586 {
18587 /* We've reached the token we want, consume it and stop. */
18588 cp_lexer_consume_token (parser->lexer);
18589 return;
18590 }
18591 break;
18592
18593 case CPP_OPEN_PAREN:
18594 case CPP_OPEN_SQUARE:
18595 ++nesting_depth;
18596 break;
18597
18598 case CPP_CLOSE_PAREN:
18599 case CPP_CLOSE_SQUARE:
18600 if (nesting_depth-- == 0)
18601 return;
18602 break;
18603
18604 case CPP_EOF:
18605 case CPP_PRAGMA_EOL:
18606 case CPP_SEMICOLON:
18607 case CPP_OPEN_BRACE:
18608 case CPP_CLOSE_BRACE:
18609 /* The '>' was probably forgotten, don't look further. */
18610 return;
18611
18612 default:
18613 break;
18614 }
18615
18616 /* Consume this token. */
18617 cp_lexer_consume_token (parser->lexer);
18618 }
18619 }
18620
18621 /* If the next token is the indicated keyword, consume it. Otherwise,
18622 issue an error message indicating that TOKEN_DESC was expected.
18623
18624 Returns the token consumed, if the token had the appropriate type.
18625 Otherwise, returns NULL. */
18626
18627 static cp_token *
18628 cp_parser_require_keyword (cp_parser* parser,
18629 enum rid keyword,
18630 const char* token_desc)
18631 {
18632 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18633
18634 if (token && token->keyword != keyword)
18635 {
18636 dyn_string_t error_msg;
18637
18638 /* Format the error message. */
18639 error_msg = dyn_string_new (0);
18640 dyn_string_append_cstr (error_msg, "expected ");
18641 dyn_string_append_cstr (error_msg, token_desc);
18642 cp_parser_error (parser, error_msg->s);
18643 dyn_string_delete (error_msg);
18644 return NULL;
18645 }
18646
18647 return token;
18648 }
18649
18650 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18651 function-definition. */
18652
18653 static bool
18654 cp_parser_token_starts_function_definition_p (cp_token* token)
18655 {
18656 return (/* An ordinary function-body begins with an `{'. */
18657 token->type == CPP_OPEN_BRACE
18658 /* A ctor-initializer begins with a `:'. */
18659 || token->type == CPP_COLON
18660 /* A function-try-block begins with `try'. */
18661 || token->keyword == RID_TRY
18662 /* The named return value extension begins with `return'. */
18663 || token->keyword == RID_RETURN);
18664 }
18665
18666 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18667 definition. */
18668
18669 static bool
18670 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18671 {
18672 cp_token *token;
18673
18674 token = cp_lexer_peek_token (parser->lexer);
18675 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18676 }
18677
18678 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18679 C++0x) ending a template-argument. */
18680
18681 static bool
18682 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18683 {
18684 cp_token *token;
18685
18686 token = cp_lexer_peek_token (parser->lexer);
18687 return (token->type == CPP_COMMA
18688 || token->type == CPP_GREATER
18689 || token->type == CPP_ELLIPSIS
18690 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18691 }
18692
18693 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18694 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18695
18696 static bool
18697 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18698 size_t n)
18699 {
18700 cp_token *token;
18701
18702 token = cp_lexer_peek_nth_token (parser->lexer, n);
18703 if (token->type == CPP_LESS)
18704 return true;
18705 /* Check for the sequence `<::' in the original code. It would be lexed as
18706 `[:', where `[' is a digraph, and there is no whitespace before
18707 `:'. */
18708 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18709 {
18710 cp_token *token2;
18711 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18712 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18713 return true;
18714 }
18715 return false;
18716 }
18717
18718 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18719 or none_type otherwise. */
18720
18721 static enum tag_types
18722 cp_parser_token_is_class_key (cp_token* token)
18723 {
18724 switch (token->keyword)
18725 {
18726 case RID_CLASS:
18727 return class_type;
18728 case RID_STRUCT:
18729 return record_type;
18730 case RID_UNION:
18731 return union_type;
18732
18733 default:
18734 return none_type;
18735 }
18736 }
18737
18738 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18739
18740 static void
18741 cp_parser_check_class_key (enum tag_types class_key, tree type)
18742 {
18743 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18744 permerror (input_location, "%qs tag used in naming %q#T",
18745 class_key == union_type ? "union"
18746 : class_key == record_type ? "struct" : "class",
18747 type);
18748 }
18749
18750 /* Issue an error message if DECL is redeclared with different
18751 access than its original declaration [class.access.spec/3].
18752 This applies to nested classes and nested class templates.
18753 [class.mem/1]. */
18754
18755 static void
18756 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18757 {
18758 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18759 return;
18760
18761 if ((TREE_PRIVATE (decl)
18762 != (current_access_specifier == access_private_node))
18763 || (TREE_PROTECTED (decl)
18764 != (current_access_specifier == access_protected_node)))
18765 error ("%H%qD redeclared with different access", &location, decl);
18766 }
18767
18768 /* Look for the `template' keyword, as a syntactic disambiguator.
18769 Return TRUE iff it is present, in which case it will be
18770 consumed. */
18771
18772 static bool
18773 cp_parser_optional_template_keyword (cp_parser *parser)
18774 {
18775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18776 {
18777 /* The `template' keyword can only be used within templates;
18778 outside templates the parser can always figure out what is a
18779 template and what is not. */
18780 if (!processing_template_decl)
18781 {
18782 cp_token *token = cp_lexer_peek_token (parser->lexer);
18783 error ("%H%<template%> (as a disambiguator) is only allowed "
18784 "within templates", &token->location);
18785 /* If this part of the token stream is rescanned, the same
18786 error message would be generated. So, we purge the token
18787 from the stream. */
18788 cp_lexer_purge_token (parser->lexer);
18789 return false;
18790 }
18791 else
18792 {
18793 /* Consume the `template' keyword. */
18794 cp_lexer_consume_token (parser->lexer);
18795 return true;
18796 }
18797 }
18798
18799 return false;
18800 }
18801
18802 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18803 set PARSER->SCOPE, and perform other related actions. */
18804
18805 static void
18806 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18807 {
18808 int i;
18809 struct tree_check *check_value;
18810 deferred_access_check *chk;
18811 VEC (deferred_access_check,gc) *checks;
18812
18813 /* Get the stored value. */
18814 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18815 /* Perform any access checks that were deferred. */
18816 checks = check_value->checks;
18817 if (checks)
18818 {
18819 for (i = 0 ;
18820 VEC_iterate (deferred_access_check, checks, i, chk) ;
18821 ++i)
18822 {
18823 perform_or_defer_access_check (chk->binfo,
18824 chk->decl,
18825 chk->diag_decl);
18826 }
18827 }
18828 /* Set the scope from the stored value. */
18829 parser->scope = check_value->value;
18830 parser->qualifying_scope = check_value->qualifying_scope;
18831 parser->object_scope = NULL_TREE;
18832 }
18833
18834 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18835 encounter the end of a block before what we were looking for. */
18836
18837 static bool
18838 cp_parser_cache_group (cp_parser *parser,
18839 enum cpp_ttype end,
18840 unsigned depth)
18841 {
18842 while (true)
18843 {
18844 cp_token *token = cp_lexer_peek_token (parser->lexer);
18845
18846 /* Abort a parenthesized expression if we encounter a semicolon. */
18847 if ((end == CPP_CLOSE_PAREN || depth == 0)
18848 && token->type == CPP_SEMICOLON)
18849 return true;
18850 /* If we've reached the end of the file, stop. */
18851 if (token->type == CPP_EOF
18852 || (end != CPP_PRAGMA_EOL
18853 && token->type == CPP_PRAGMA_EOL))
18854 return true;
18855 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18856 /* We've hit the end of an enclosing block, so there's been some
18857 kind of syntax error. */
18858 return true;
18859
18860 /* Consume the token. */
18861 cp_lexer_consume_token (parser->lexer);
18862 /* See if it starts a new group. */
18863 if (token->type == CPP_OPEN_BRACE)
18864 {
18865 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18866 /* In theory this should probably check end == '}', but
18867 cp_parser_save_member_function_body needs it to exit
18868 after either '}' or ')' when called with ')'. */
18869 if (depth == 0)
18870 return false;
18871 }
18872 else if (token->type == CPP_OPEN_PAREN)
18873 {
18874 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18875 if (depth == 0 && end == CPP_CLOSE_PAREN)
18876 return false;
18877 }
18878 else if (token->type == CPP_PRAGMA)
18879 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18880 else if (token->type == end)
18881 return false;
18882 }
18883 }
18884
18885 /* Begin parsing tentatively. We always save tokens while parsing
18886 tentatively so that if the tentative parsing fails we can restore the
18887 tokens. */
18888
18889 static void
18890 cp_parser_parse_tentatively (cp_parser* parser)
18891 {
18892 /* Enter a new parsing context. */
18893 parser->context = cp_parser_context_new (parser->context);
18894 /* Begin saving tokens. */
18895 cp_lexer_save_tokens (parser->lexer);
18896 /* In order to avoid repetitive access control error messages,
18897 access checks are queued up until we are no longer parsing
18898 tentatively. */
18899 push_deferring_access_checks (dk_deferred);
18900 }
18901
18902 /* Commit to the currently active tentative parse. */
18903
18904 static void
18905 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18906 {
18907 cp_parser_context *context;
18908 cp_lexer *lexer;
18909
18910 /* Mark all of the levels as committed. */
18911 lexer = parser->lexer;
18912 for (context = parser->context; context->next; context = context->next)
18913 {
18914 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18915 break;
18916 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18917 while (!cp_lexer_saving_tokens (lexer))
18918 lexer = lexer->next;
18919 cp_lexer_commit_tokens (lexer);
18920 }
18921 }
18922
18923 /* Abort the currently active tentative parse. All consumed tokens
18924 will be rolled back, and no diagnostics will be issued. */
18925
18926 static void
18927 cp_parser_abort_tentative_parse (cp_parser* parser)
18928 {
18929 cp_parser_simulate_error (parser);
18930 /* Now, pretend that we want to see if the construct was
18931 successfully parsed. */
18932 cp_parser_parse_definitely (parser);
18933 }
18934
18935 /* Stop parsing tentatively. If a parse error has occurred, restore the
18936 token stream. Otherwise, commit to the tokens we have consumed.
18937 Returns true if no error occurred; false otherwise. */
18938
18939 static bool
18940 cp_parser_parse_definitely (cp_parser* parser)
18941 {
18942 bool error_occurred;
18943 cp_parser_context *context;
18944
18945 /* Remember whether or not an error occurred, since we are about to
18946 destroy that information. */
18947 error_occurred = cp_parser_error_occurred (parser);
18948 /* Remove the topmost context from the stack. */
18949 context = parser->context;
18950 parser->context = context->next;
18951 /* If no parse errors occurred, commit to the tentative parse. */
18952 if (!error_occurred)
18953 {
18954 /* Commit to the tokens read tentatively, unless that was
18955 already done. */
18956 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18957 cp_lexer_commit_tokens (parser->lexer);
18958
18959 pop_to_parent_deferring_access_checks ();
18960 }
18961 /* Otherwise, if errors occurred, roll back our state so that things
18962 are just as they were before we began the tentative parse. */
18963 else
18964 {
18965 cp_lexer_rollback_tokens (parser->lexer);
18966 pop_deferring_access_checks ();
18967 }
18968 /* Add the context to the front of the free list. */
18969 context->next = cp_parser_context_free_list;
18970 cp_parser_context_free_list = context;
18971
18972 return !error_occurred;
18973 }
18974
18975 /* Returns true if we are parsing tentatively and are not committed to
18976 this tentative parse. */
18977
18978 static bool
18979 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18980 {
18981 return (cp_parser_parsing_tentatively (parser)
18982 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18983 }
18984
18985 /* Returns nonzero iff an error has occurred during the most recent
18986 tentative parse. */
18987
18988 static bool
18989 cp_parser_error_occurred (cp_parser* parser)
18990 {
18991 return (cp_parser_parsing_tentatively (parser)
18992 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18993 }
18994
18995 /* Returns nonzero if GNU extensions are allowed. */
18996
18997 static bool
18998 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18999 {
19000 return parser->allow_gnu_extensions_p;
19001 }
19002 \f
19003 /* Objective-C++ Productions */
19004
19005
19006 /* Parse an Objective-C expression, which feeds into a primary-expression
19007 above.
19008
19009 objc-expression:
19010 objc-message-expression
19011 objc-string-literal
19012 objc-encode-expression
19013 objc-protocol-expression
19014 objc-selector-expression
19015
19016 Returns a tree representation of the expression. */
19017
19018 static tree
19019 cp_parser_objc_expression (cp_parser* parser)
19020 {
19021 /* Try to figure out what kind of declaration is present. */
19022 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19023
19024 switch (kwd->type)
19025 {
19026 case CPP_OPEN_SQUARE:
19027 return cp_parser_objc_message_expression (parser);
19028
19029 case CPP_OBJC_STRING:
19030 kwd = cp_lexer_consume_token (parser->lexer);
19031 return objc_build_string_object (kwd->u.value);
19032
19033 case CPP_KEYWORD:
19034 switch (kwd->keyword)
19035 {
19036 case RID_AT_ENCODE:
19037 return cp_parser_objc_encode_expression (parser);
19038
19039 case RID_AT_PROTOCOL:
19040 return cp_parser_objc_protocol_expression (parser);
19041
19042 case RID_AT_SELECTOR:
19043 return cp_parser_objc_selector_expression (parser);
19044
19045 default:
19046 break;
19047 }
19048 default:
19049 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19050 &kwd->location, kwd->u.value);
19051 cp_parser_skip_to_end_of_block_or_statement (parser);
19052 }
19053
19054 return error_mark_node;
19055 }
19056
19057 /* Parse an Objective-C message expression.
19058
19059 objc-message-expression:
19060 [ objc-message-receiver objc-message-args ]
19061
19062 Returns a representation of an Objective-C message. */
19063
19064 static tree
19065 cp_parser_objc_message_expression (cp_parser* parser)
19066 {
19067 tree receiver, messageargs;
19068
19069 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19070 receiver = cp_parser_objc_message_receiver (parser);
19071 messageargs = cp_parser_objc_message_args (parser);
19072 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19073
19074 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19075 }
19076
19077 /* Parse an objc-message-receiver.
19078
19079 objc-message-receiver:
19080 expression
19081 simple-type-specifier
19082
19083 Returns a representation of the type or expression. */
19084
19085 static tree
19086 cp_parser_objc_message_receiver (cp_parser* parser)
19087 {
19088 tree rcv;
19089
19090 /* An Objective-C message receiver may be either (1) a type
19091 or (2) an expression. */
19092 cp_parser_parse_tentatively (parser);
19093 rcv = cp_parser_expression (parser, false);
19094
19095 if (cp_parser_parse_definitely (parser))
19096 return rcv;
19097
19098 rcv = cp_parser_simple_type_specifier (parser,
19099 /*decl_specs=*/NULL,
19100 CP_PARSER_FLAGS_NONE);
19101
19102 return objc_get_class_reference (rcv);
19103 }
19104
19105 /* Parse the arguments and selectors comprising an Objective-C message.
19106
19107 objc-message-args:
19108 objc-selector
19109 objc-selector-args
19110 objc-selector-args , objc-comma-args
19111
19112 objc-selector-args:
19113 objc-selector [opt] : assignment-expression
19114 objc-selector-args objc-selector [opt] : assignment-expression
19115
19116 objc-comma-args:
19117 assignment-expression
19118 objc-comma-args , assignment-expression
19119
19120 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19121 selector arguments and TREE_VALUE containing a list of comma
19122 arguments. */
19123
19124 static tree
19125 cp_parser_objc_message_args (cp_parser* parser)
19126 {
19127 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19128 bool maybe_unary_selector_p = true;
19129 cp_token *token = cp_lexer_peek_token (parser->lexer);
19130
19131 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19132 {
19133 tree selector = NULL_TREE, arg;
19134
19135 if (token->type != CPP_COLON)
19136 selector = cp_parser_objc_selector (parser);
19137
19138 /* Detect if we have a unary selector. */
19139 if (maybe_unary_selector_p
19140 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19141 return build_tree_list (selector, NULL_TREE);
19142
19143 maybe_unary_selector_p = false;
19144 cp_parser_require (parser, CPP_COLON, "%<:%>");
19145 arg = cp_parser_assignment_expression (parser, false);
19146
19147 sel_args
19148 = chainon (sel_args,
19149 build_tree_list (selector, arg));
19150
19151 token = cp_lexer_peek_token (parser->lexer);
19152 }
19153
19154 /* Handle non-selector arguments, if any. */
19155 while (token->type == CPP_COMMA)
19156 {
19157 tree arg;
19158
19159 cp_lexer_consume_token (parser->lexer);
19160 arg = cp_parser_assignment_expression (parser, false);
19161
19162 addl_args
19163 = chainon (addl_args,
19164 build_tree_list (NULL_TREE, arg));
19165
19166 token = cp_lexer_peek_token (parser->lexer);
19167 }
19168
19169 return build_tree_list (sel_args, addl_args);
19170 }
19171
19172 /* Parse an Objective-C encode expression.
19173
19174 objc-encode-expression:
19175 @encode objc-typename
19176
19177 Returns an encoded representation of the type argument. */
19178
19179 static tree
19180 cp_parser_objc_encode_expression (cp_parser* parser)
19181 {
19182 tree type;
19183 cp_token *token;
19184
19185 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19186 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19187 token = cp_lexer_peek_token (parser->lexer);
19188 type = complete_type (cp_parser_type_id (parser));
19189 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19190
19191 if (!type)
19192 {
19193 error ("%H%<@encode%> must specify a type as an argument",
19194 &token->location);
19195 return error_mark_node;
19196 }
19197
19198 return objc_build_encode_expr (type);
19199 }
19200
19201 /* Parse an Objective-C @defs expression. */
19202
19203 static tree
19204 cp_parser_objc_defs_expression (cp_parser *parser)
19205 {
19206 tree name;
19207
19208 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19209 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19210 name = cp_parser_identifier (parser);
19211 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19212
19213 return objc_get_class_ivars (name);
19214 }
19215
19216 /* Parse an Objective-C protocol expression.
19217
19218 objc-protocol-expression:
19219 @protocol ( identifier )
19220
19221 Returns a representation of the protocol expression. */
19222
19223 static tree
19224 cp_parser_objc_protocol_expression (cp_parser* parser)
19225 {
19226 tree proto;
19227
19228 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19229 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19230 proto = cp_parser_identifier (parser);
19231 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19232
19233 return objc_build_protocol_expr (proto);
19234 }
19235
19236 /* Parse an Objective-C selector expression.
19237
19238 objc-selector-expression:
19239 @selector ( objc-method-signature )
19240
19241 objc-method-signature:
19242 objc-selector
19243 objc-selector-seq
19244
19245 objc-selector-seq:
19246 objc-selector :
19247 objc-selector-seq objc-selector :
19248
19249 Returns a representation of the method selector. */
19250
19251 static tree
19252 cp_parser_objc_selector_expression (cp_parser* parser)
19253 {
19254 tree sel_seq = NULL_TREE;
19255 bool maybe_unary_selector_p = true;
19256 cp_token *token;
19257
19258 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19259 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19260 token = cp_lexer_peek_token (parser->lexer);
19261
19262 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19263 || token->type == CPP_SCOPE)
19264 {
19265 tree selector = NULL_TREE;
19266
19267 if (token->type != CPP_COLON
19268 || token->type == CPP_SCOPE)
19269 selector = cp_parser_objc_selector (parser);
19270
19271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19272 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19273 {
19274 /* Detect if we have a unary selector. */
19275 if (maybe_unary_selector_p)
19276 {
19277 sel_seq = selector;
19278 goto finish_selector;
19279 }
19280 else
19281 {
19282 cp_parser_error (parser, "expected %<:%>");
19283 }
19284 }
19285 maybe_unary_selector_p = false;
19286 token = cp_lexer_consume_token (parser->lexer);
19287
19288 if (token->type == CPP_SCOPE)
19289 {
19290 sel_seq
19291 = chainon (sel_seq,
19292 build_tree_list (selector, NULL_TREE));
19293 sel_seq
19294 = chainon (sel_seq,
19295 build_tree_list (NULL_TREE, NULL_TREE));
19296 }
19297 else
19298 sel_seq
19299 = chainon (sel_seq,
19300 build_tree_list (selector, NULL_TREE));
19301
19302 token = cp_lexer_peek_token (parser->lexer);
19303 }
19304
19305 finish_selector:
19306 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19307
19308 return objc_build_selector_expr (sel_seq);
19309 }
19310
19311 /* Parse a list of identifiers.
19312
19313 objc-identifier-list:
19314 identifier
19315 objc-identifier-list , identifier
19316
19317 Returns a TREE_LIST of identifier nodes. */
19318
19319 static tree
19320 cp_parser_objc_identifier_list (cp_parser* parser)
19321 {
19322 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19323 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19324
19325 while (sep->type == CPP_COMMA)
19326 {
19327 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19328 list = chainon (list,
19329 build_tree_list (NULL_TREE,
19330 cp_parser_identifier (parser)));
19331 sep = cp_lexer_peek_token (parser->lexer);
19332 }
19333
19334 return list;
19335 }
19336
19337 /* Parse an Objective-C alias declaration.
19338
19339 objc-alias-declaration:
19340 @compatibility_alias identifier identifier ;
19341
19342 This function registers the alias mapping with the Objective-C front end.
19343 It returns nothing. */
19344
19345 static void
19346 cp_parser_objc_alias_declaration (cp_parser* parser)
19347 {
19348 tree alias, orig;
19349
19350 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19351 alias = cp_parser_identifier (parser);
19352 orig = cp_parser_identifier (parser);
19353 objc_declare_alias (alias, orig);
19354 cp_parser_consume_semicolon_at_end_of_statement (parser);
19355 }
19356
19357 /* Parse an Objective-C class forward-declaration.
19358
19359 objc-class-declaration:
19360 @class objc-identifier-list ;
19361
19362 The function registers the forward declarations with the Objective-C
19363 front end. It returns nothing. */
19364
19365 static void
19366 cp_parser_objc_class_declaration (cp_parser* parser)
19367 {
19368 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19369 objc_declare_class (cp_parser_objc_identifier_list (parser));
19370 cp_parser_consume_semicolon_at_end_of_statement (parser);
19371 }
19372
19373 /* Parse a list of Objective-C protocol references.
19374
19375 objc-protocol-refs-opt:
19376 objc-protocol-refs [opt]
19377
19378 objc-protocol-refs:
19379 < objc-identifier-list >
19380
19381 Returns a TREE_LIST of identifiers, if any. */
19382
19383 static tree
19384 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19385 {
19386 tree protorefs = NULL_TREE;
19387
19388 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19389 {
19390 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19391 protorefs = cp_parser_objc_identifier_list (parser);
19392 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19393 }
19394
19395 return protorefs;
19396 }
19397
19398 /* Parse a Objective-C visibility specification. */
19399
19400 static void
19401 cp_parser_objc_visibility_spec (cp_parser* parser)
19402 {
19403 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19404
19405 switch (vis->keyword)
19406 {
19407 case RID_AT_PRIVATE:
19408 objc_set_visibility (2);
19409 break;
19410 case RID_AT_PROTECTED:
19411 objc_set_visibility (0);
19412 break;
19413 case RID_AT_PUBLIC:
19414 objc_set_visibility (1);
19415 break;
19416 default:
19417 return;
19418 }
19419
19420 /* Eat '@private'/'@protected'/'@public'. */
19421 cp_lexer_consume_token (parser->lexer);
19422 }
19423
19424 /* Parse an Objective-C method type. */
19425
19426 static void
19427 cp_parser_objc_method_type (cp_parser* parser)
19428 {
19429 objc_set_method_type
19430 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19431 ? PLUS_EXPR
19432 : MINUS_EXPR);
19433 }
19434
19435 /* Parse an Objective-C protocol qualifier. */
19436
19437 static tree
19438 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19439 {
19440 tree quals = NULL_TREE, node;
19441 cp_token *token = cp_lexer_peek_token (parser->lexer);
19442
19443 node = token->u.value;
19444
19445 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19446 && (node == ridpointers [(int) RID_IN]
19447 || node == ridpointers [(int) RID_OUT]
19448 || node == ridpointers [(int) RID_INOUT]
19449 || node == ridpointers [(int) RID_BYCOPY]
19450 || node == ridpointers [(int) RID_BYREF]
19451 || node == ridpointers [(int) RID_ONEWAY]))
19452 {
19453 quals = tree_cons (NULL_TREE, node, quals);
19454 cp_lexer_consume_token (parser->lexer);
19455 token = cp_lexer_peek_token (parser->lexer);
19456 node = token->u.value;
19457 }
19458
19459 return quals;
19460 }
19461
19462 /* Parse an Objective-C typename. */
19463
19464 static tree
19465 cp_parser_objc_typename (cp_parser* parser)
19466 {
19467 tree type_name = NULL_TREE;
19468
19469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19470 {
19471 tree proto_quals, cp_type = NULL_TREE;
19472
19473 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19474 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19475
19476 /* An ObjC type name may consist of just protocol qualifiers, in which
19477 case the type shall default to 'id'. */
19478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19479 cp_type = cp_parser_type_id (parser);
19480
19481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19482 type_name = build_tree_list (proto_quals, cp_type);
19483 }
19484
19485 return type_name;
19486 }
19487
19488 /* Check to see if TYPE refers to an Objective-C selector name. */
19489
19490 static bool
19491 cp_parser_objc_selector_p (enum cpp_ttype type)
19492 {
19493 return (type == CPP_NAME || type == CPP_KEYWORD
19494 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19495 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19496 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19497 || type == CPP_XOR || type == CPP_XOR_EQ);
19498 }
19499
19500 /* Parse an Objective-C selector. */
19501
19502 static tree
19503 cp_parser_objc_selector (cp_parser* parser)
19504 {
19505 cp_token *token = cp_lexer_consume_token (parser->lexer);
19506
19507 if (!cp_parser_objc_selector_p (token->type))
19508 {
19509 error ("%Hinvalid Objective-C++ selector name", &token->location);
19510 return error_mark_node;
19511 }
19512
19513 /* C++ operator names are allowed to appear in ObjC selectors. */
19514 switch (token->type)
19515 {
19516 case CPP_AND_AND: return get_identifier ("and");
19517 case CPP_AND_EQ: return get_identifier ("and_eq");
19518 case CPP_AND: return get_identifier ("bitand");
19519 case CPP_OR: return get_identifier ("bitor");
19520 case CPP_COMPL: return get_identifier ("compl");
19521 case CPP_NOT: return get_identifier ("not");
19522 case CPP_NOT_EQ: return get_identifier ("not_eq");
19523 case CPP_OR_OR: return get_identifier ("or");
19524 case CPP_OR_EQ: return get_identifier ("or_eq");
19525 case CPP_XOR: return get_identifier ("xor");
19526 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19527 default: return token->u.value;
19528 }
19529 }
19530
19531 /* Parse an Objective-C params list. */
19532
19533 static tree
19534 cp_parser_objc_method_keyword_params (cp_parser* parser)
19535 {
19536 tree params = NULL_TREE;
19537 bool maybe_unary_selector_p = true;
19538 cp_token *token = cp_lexer_peek_token (parser->lexer);
19539
19540 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19541 {
19542 tree selector = NULL_TREE, type_name, identifier;
19543
19544 if (token->type != CPP_COLON)
19545 selector = cp_parser_objc_selector (parser);
19546
19547 /* Detect if we have a unary selector. */
19548 if (maybe_unary_selector_p
19549 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19550 return selector;
19551
19552 maybe_unary_selector_p = false;
19553 cp_parser_require (parser, CPP_COLON, "%<:%>");
19554 type_name = cp_parser_objc_typename (parser);
19555 identifier = cp_parser_identifier (parser);
19556
19557 params
19558 = chainon (params,
19559 objc_build_keyword_decl (selector,
19560 type_name,
19561 identifier));
19562
19563 token = cp_lexer_peek_token (parser->lexer);
19564 }
19565
19566 return params;
19567 }
19568
19569 /* Parse the non-keyword Objective-C params. */
19570
19571 static tree
19572 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19573 {
19574 tree params = make_node (TREE_LIST);
19575 cp_token *token = cp_lexer_peek_token (parser->lexer);
19576 *ellipsisp = false; /* Initially, assume no ellipsis. */
19577
19578 while (token->type == CPP_COMMA)
19579 {
19580 cp_parameter_declarator *parmdecl;
19581 tree parm;
19582
19583 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19584 token = cp_lexer_peek_token (parser->lexer);
19585
19586 if (token->type == CPP_ELLIPSIS)
19587 {
19588 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19589 *ellipsisp = true;
19590 break;
19591 }
19592
19593 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19594 parm = grokdeclarator (parmdecl->declarator,
19595 &parmdecl->decl_specifiers,
19596 PARM, /*initialized=*/0,
19597 /*attrlist=*/NULL);
19598
19599 chainon (params, build_tree_list (NULL_TREE, parm));
19600 token = cp_lexer_peek_token (parser->lexer);
19601 }
19602
19603 return params;
19604 }
19605
19606 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19607
19608 static void
19609 cp_parser_objc_interstitial_code (cp_parser* parser)
19610 {
19611 cp_token *token = cp_lexer_peek_token (parser->lexer);
19612
19613 /* If the next token is `extern' and the following token is a string
19614 literal, then we have a linkage specification. */
19615 if (token->keyword == RID_EXTERN
19616 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19617 cp_parser_linkage_specification (parser);
19618 /* Handle #pragma, if any. */
19619 else if (token->type == CPP_PRAGMA)
19620 cp_parser_pragma (parser, pragma_external);
19621 /* Allow stray semicolons. */
19622 else if (token->type == CPP_SEMICOLON)
19623 cp_lexer_consume_token (parser->lexer);
19624 /* Finally, try to parse a block-declaration, or a function-definition. */
19625 else
19626 cp_parser_block_declaration (parser, /*statement_p=*/false);
19627 }
19628
19629 /* Parse a method signature. */
19630
19631 static tree
19632 cp_parser_objc_method_signature (cp_parser* parser)
19633 {
19634 tree rettype, kwdparms, optparms;
19635 bool ellipsis = false;
19636
19637 cp_parser_objc_method_type (parser);
19638 rettype = cp_parser_objc_typename (parser);
19639 kwdparms = cp_parser_objc_method_keyword_params (parser);
19640 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19641
19642 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19643 }
19644
19645 /* Pars an Objective-C method prototype list. */
19646
19647 static void
19648 cp_parser_objc_method_prototype_list (cp_parser* parser)
19649 {
19650 cp_token *token = cp_lexer_peek_token (parser->lexer);
19651
19652 while (token->keyword != RID_AT_END)
19653 {
19654 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19655 {
19656 objc_add_method_declaration
19657 (cp_parser_objc_method_signature (parser));
19658 cp_parser_consume_semicolon_at_end_of_statement (parser);
19659 }
19660 else
19661 /* Allow for interspersed non-ObjC++ code. */
19662 cp_parser_objc_interstitial_code (parser);
19663
19664 token = cp_lexer_peek_token (parser->lexer);
19665 }
19666
19667 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19668 objc_finish_interface ();
19669 }
19670
19671 /* Parse an Objective-C method definition list. */
19672
19673 static void
19674 cp_parser_objc_method_definition_list (cp_parser* parser)
19675 {
19676 cp_token *token = cp_lexer_peek_token (parser->lexer);
19677
19678 while (token->keyword != RID_AT_END)
19679 {
19680 tree meth;
19681
19682 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19683 {
19684 push_deferring_access_checks (dk_deferred);
19685 objc_start_method_definition
19686 (cp_parser_objc_method_signature (parser));
19687
19688 /* For historical reasons, we accept an optional semicolon. */
19689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19690 cp_lexer_consume_token (parser->lexer);
19691
19692 perform_deferred_access_checks ();
19693 stop_deferring_access_checks ();
19694 meth = cp_parser_function_definition_after_declarator (parser,
19695 false);
19696 pop_deferring_access_checks ();
19697 objc_finish_method_definition (meth);
19698 }
19699 else
19700 /* Allow for interspersed non-ObjC++ code. */
19701 cp_parser_objc_interstitial_code (parser);
19702
19703 token = cp_lexer_peek_token (parser->lexer);
19704 }
19705
19706 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19707 objc_finish_implementation ();
19708 }
19709
19710 /* Parse Objective-C ivars. */
19711
19712 static void
19713 cp_parser_objc_class_ivars (cp_parser* parser)
19714 {
19715 cp_token *token = cp_lexer_peek_token (parser->lexer);
19716
19717 if (token->type != CPP_OPEN_BRACE)
19718 return; /* No ivars specified. */
19719
19720 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19721 token = cp_lexer_peek_token (parser->lexer);
19722
19723 while (token->type != CPP_CLOSE_BRACE)
19724 {
19725 cp_decl_specifier_seq declspecs;
19726 int decl_class_or_enum_p;
19727 tree prefix_attributes;
19728
19729 cp_parser_objc_visibility_spec (parser);
19730
19731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19732 break;
19733
19734 cp_parser_decl_specifier_seq (parser,
19735 CP_PARSER_FLAGS_OPTIONAL,
19736 &declspecs,
19737 &decl_class_or_enum_p);
19738 prefix_attributes = declspecs.attributes;
19739 declspecs.attributes = NULL_TREE;
19740
19741 /* Keep going until we hit the `;' at the end of the
19742 declaration. */
19743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19744 {
19745 tree width = NULL_TREE, attributes, first_attribute, decl;
19746 cp_declarator *declarator = NULL;
19747 int ctor_dtor_or_conv_p;
19748
19749 /* Check for a (possibly unnamed) bitfield declaration. */
19750 token = cp_lexer_peek_token (parser->lexer);
19751 if (token->type == CPP_COLON)
19752 goto eat_colon;
19753
19754 if (token->type == CPP_NAME
19755 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19756 == CPP_COLON))
19757 {
19758 /* Get the name of the bitfield. */
19759 declarator = make_id_declarator (NULL_TREE,
19760 cp_parser_identifier (parser),
19761 sfk_none);
19762
19763 eat_colon:
19764 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19765 /* Get the width of the bitfield. */
19766 width
19767 = cp_parser_constant_expression (parser,
19768 /*allow_non_constant=*/false,
19769 NULL);
19770 }
19771 else
19772 {
19773 /* Parse the declarator. */
19774 declarator
19775 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19776 &ctor_dtor_or_conv_p,
19777 /*parenthesized_p=*/NULL,
19778 /*member_p=*/false);
19779 }
19780
19781 /* Look for attributes that apply to the ivar. */
19782 attributes = cp_parser_attributes_opt (parser);
19783 /* Remember which attributes are prefix attributes and
19784 which are not. */
19785 first_attribute = attributes;
19786 /* Combine the attributes. */
19787 attributes = chainon (prefix_attributes, attributes);
19788
19789 if (width)
19790 /* Create the bitfield declaration. */
19791 decl = grokbitfield (declarator, &declspecs,
19792 width,
19793 attributes);
19794 else
19795 decl = grokfield (declarator, &declspecs,
19796 NULL_TREE, /*init_const_expr_p=*/false,
19797 NULL_TREE, attributes);
19798
19799 /* Add the instance variable. */
19800 objc_add_instance_variable (decl);
19801
19802 /* Reset PREFIX_ATTRIBUTES. */
19803 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19804 attributes = TREE_CHAIN (attributes);
19805 if (attributes)
19806 TREE_CHAIN (attributes) = NULL_TREE;
19807
19808 token = cp_lexer_peek_token (parser->lexer);
19809
19810 if (token->type == CPP_COMMA)
19811 {
19812 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19813 continue;
19814 }
19815 break;
19816 }
19817
19818 cp_parser_consume_semicolon_at_end_of_statement (parser);
19819 token = cp_lexer_peek_token (parser->lexer);
19820 }
19821
19822 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19823 /* For historical reasons, we accept an optional semicolon. */
19824 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19825 cp_lexer_consume_token (parser->lexer);
19826 }
19827
19828 /* Parse an Objective-C protocol declaration. */
19829
19830 static void
19831 cp_parser_objc_protocol_declaration (cp_parser* parser)
19832 {
19833 tree proto, protorefs;
19834 cp_token *tok;
19835
19836 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19838 {
19839 tok = cp_lexer_peek_token (parser->lexer);
19840 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19841 goto finish;
19842 }
19843
19844 /* See if we have a forward declaration or a definition. */
19845 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19846
19847 /* Try a forward declaration first. */
19848 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19849 {
19850 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19851 finish:
19852 cp_parser_consume_semicolon_at_end_of_statement (parser);
19853 }
19854
19855 /* Ok, we got a full-fledged definition (or at least should). */
19856 else
19857 {
19858 proto = cp_parser_identifier (parser);
19859 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19860 objc_start_protocol (proto, protorefs);
19861 cp_parser_objc_method_prototype_list (parser);
19862 }
19863 }
19864
19865 /* Parse an Objective-C superclass or category. */
19866
19867 static void
19868 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19869 tree *categ)
19870 {
19871 cp_token *next = cp_lexer_peek_token (parser->lexer);
19872
19873 *super = *categ = NULL_TREE;
19874 if (next->type == CPP_COLON)
19875 {
19876 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19877 *super = cp_parser_identifier (parser);
19878 }
19879 else if (next->type == CPP_OPEN_PAREN)
19880 {
19881 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19882 *categ = cp_parser_identifier (parser);
19883 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19884 }
19885 }
19886
19887 /* Parse an Objective-C class interface. */
19888
19889 static void
19890 cp_parser_objc_class_interface (cp_parser* parser)
19891 {
19892 tree name, super, categ, protos;
19893
19894 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19895 name = cp_parser_identifier (parser);
19896 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19897 protos = cp_parser_objc_protocol_refs_opt (parser);
19898
19899 /* We have either a class or a category on our hands. */
19900 if (categ)
19901 objc_start_category_interface (name, categ, protos);
19902 else
19903 {
19904 objc_start_class_interface (name, super, protos);
19905 /* Handle instance variable declarations, if any. */
19906 cp_parser_objc_class_ivars (parser);
19907 objc_continue_interface ();
19908 }
19909
19910 cp_parser_objc_method_prototype_list (parser);
19911 }
19912
19913 /* Parse an Objective-C class implementation. */
19914
19915 static void
19916 cp_parser_objc_class_implementation (cp_parser* parser)
19917 {
19918 tree name, super, categ;
19919
19920 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19921 name = cp_parser_identifier (parser);
19922 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19923
19924 /* We have either a class or a category on our hands. */
19925 if (categ)
19926 objc_start_category_implementation (name, categ);
19927 else
19928 {
19929 objc_start_class_implementation (name, super);
19930 /* Handle instance variable declarations, if any. */
19931 cp_parser_objc_class_ivars (parser);
19932 objc_continue_implementation ();
19933 }
19934
19935 cp_parser_objc_method_definition_list (parser);
19936 }
19937
19938 /* Consume the @end token and finish off the implementation. */
19939
19940 static void
19941 cp_parser_objc_end_implementation (cp_parser* parser)
19942 {
19943 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19944 objc_finish_implementation ();
19945 }
19946
19947 /* Parse an Objective-C declaration. */
19948
19949 static void
19950 cp_parser_objc_declaration (cp_parser* parser)
19951 {
19952 /* Try to figure out what kind of declaration is present. */
19953 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19954
19955 switch (kwd->keyword)
19956 {
19957 case RID_AT_ALIAS:
19958 cp_parser_objc_alias_declaration (parser);
19959 break;
19960 case RID_AT_CLASS:
19961 cp_parser_objc_class_declaration (parser);
19962 break;
19963 case RID_AT_PROTOCOL:
19964 cp_parser_objc_protocol_declaration (parser);
19965 break;
19966 case RID_AT_INTERFACE:
19967 cp_parser_objc_class_interface (parser);
19968 break;
19969 case RID_AT_IMPLEMENTATION:
19970 cp_parser_objc_class_implementation (parser);
19971 break;
19972 case RID_AT_END:
19973 cp_parser_objc_end_implementation (parser);
19974 break;
19975 default:
19976 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19977 &kwd->location, kwd->u.value);
19978 cp_parser_skip_to_end_of_block_or_statement (parser);
19979 }
19980 }
19981
19982 /* Parse an Objective-C try-catch-finally statement.
19983
19984 objc-try-catch-finally-stmt:
19985 @try compound-statement objc-catch-clause-seq [opt]
19986 objc-finally-clause [opt]
19987
19988 objc-catch-clause-seq:
19989 objc-catch-clause objc-catch-clause-seq [opt]
19990
19991 objc-catch-clause:
19992 @catch ( exception-declaration ) compound-statement
19993
19994 objc-finally-clause
19995 @finally compound-statement
19996
19997 Returns NULL_TREE. */
19998
19999 static tree
20000 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20001 location_t location;
20002 tree stmt;
20003
20004 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20005 location = cp_lexer_peek_token (parser->lexer)->location;
20006 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20007 node, lest it get absorbed into the surrounding block. */
20008 stmt = push_stmt_list ();
20009 cp_parser_compound_statement (parser, NULL, false);
20010 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20011
20012 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20013 {
20014 cp_parameter_declarator *parmdecl;
20015 tree parm;
20016
20017 cp_lexer_consume_token (parser->lexer);
20018 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20019 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20020 parm = grokdeclarator (parmdecl->declarator,
20021 &parmdecl->decl_specifiers,
20022 PARM, /*initialized=*/0,
20023 /*attrlist=*/NULL);
20024 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20025 objc_begin_catch_clause (parm);
20026 cp_parser_compound_statement (parser, NULL, false);
20027 objc_finish_catch_clause ();
20028 }
20029
20030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20031 {
20032 cp_lexer_consume_token (parser->lexer);
20033 location = cp_lexer_peek_token (parser->lexer)->location;
20034 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20035 node, lest it get absorbed into the surrounding block. */
20036 stmt = push_stmt_list ();
20037 cp_parser_compound_statement (parser, NULL, false);
20038 objc_build_finally_clause (location, pop_stmt_list (stmt));
20039 }
20040
20041 return objc_finish_try_stmt ();
20042 }
20043
20044 /* Parse an Objective-C synchronized statement.
20045
20046 objc-synchronized-stmt:
20047 @synchronized ( expression ) compound-statement
20048
20049 Returns NULL_TREE. */
20050
20051 static tree
20052 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20053 location_t location;
20054 tree lock, stmt;
20055
20056 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20057
20058 location = cp_lexer_peek_token (parser->lexer)->location;
20059 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20060 lock = cp_parser_expression (parser, false);
20061 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20062
20063 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20064 node, lest it get absorbed into the surrounding block. */
20065 stmt = push_stmt_list ();
20066 cp_parser_compound_statement (parser, NULL, false);
20067
20068 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20069 }
20070
20071 /* Parse an Objective-C throw statement.
20072
20073 objc-throw-stmt:
20074 @throw assignment-expression [opt] ;
20075
20076 Returns a constructed '@throw' statement. */
20077
20078 static tree
20079 cp_parser_objc_throw_statement (cp_parser *parser) {
20080 tree expr = NULL_TREE;
20081
20082 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20083
20084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20085 expr = cp_parser_assignment_expression (parser, false);
20086
20087 cp_parser_consume_semicolon_at_end_of_statement (parser);
20088
20089 return objc_build_throw_stmt (expr);
20090 }
20091
20092 /* Parse an Objective-C statement. */
20093
20094 static tree
20095 cp_parser_objc_statement (cp_parser * parser) {
20096 /* Try to figure out what kind of declaration is present. */
20097 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20098
20099 switch (kwd->keyword)
20100 {
20101 case RID_AT_TRY:
20102 return cp_parser_objc_try_catch_finally_statement (parser);
20103 case RID_AT_SYNCHRONIZED:
20104 return cp_parser_objc_synchronized_statement (parser);
20105 case RID_AT_THROW:
20106 return cp_parser_objc_throw_statement (parser);
20107 default:
20108 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20109 &kwd->location, kwd->u.value);
20110 cp_parser_skip_to_end_of_block_or_statement (parser);
20111 }
20112
20113 return error_mark_node;
20114 }
20115 \f
20116 /* OpenMP 2.5 parsing routines. */
20117
20118 /* Returns name of the next clause.
20119 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20120 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20121 returned and the token is consumed. */
20122
20123 static pragma_omp_clause
20124 cp_parser_omp_clause_name (cp_parser *parser)
20125 {
20126 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20127
20128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20129 result = PRAGMA_OMP_CLAUSE_IF;
20130 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20131 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20132 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20133 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20134 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20135 {
20136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20137 const char *p = IDENTIFIER_POINTER (id);
20138
20139 switch (p[0])
20140 {
20141 case 'c':
20142 if (!strcmp ("collapse", p))
20143 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20144 else if (!strcmp ("copyin", p))
20145 result = PRAGMA_OMP_CLAUSE_COPYIN;
20146 else if (!strcmp ("copyprivate", p))
20147 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20148 break;
20149 case 'f':
20150 if (!strcmp ("firstprivate", p))
20151 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20152 break;
20153 case 'l':
20154 if (!strcmp ("lastprivate", p))
20155 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20156 break;
20157 case 'n':
20158 if (!strcmp ("nowait", p))
20159 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20160 else if (!strcmp ("num_threads", p))
20161 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20162 break;
20163 case 'o':
20164 if (!strcmp ("ordered", p))
20165 result = PRAGMA_OMP_CLAUSE_ORDERED;
20166 break;
20167 case 'r':
20168 if (!strcmp ("reduction", p))
20169 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20170 break;
20171 case 's':
20172 if (!strcmp ("schedule", p))
20173 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20174 else if (!strcmp ("shared", p))
20175 result = PRAGMA_OMP_CLAUSE_SHARED;
20176 break;
20177 case 'u':
20178 if (!strcmp ("untied", p))
20179 result = PRAGMA_OMP_CLAUSE_UNTIED;
20180 break;
20181 }
20182 }
20183
20184 if (result != PRAGMA_OMP_CLAUSE_NONE)
20185 cp_lexer_consume_token (parser->lexer);
20186
20187 return result;
20188 }
20189
20190 /* Validate that a clause of the given type does not already exist. */
20191
20192 static void
20193 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20194 const char *name, location_t location)
20195 {
20196 tree c;
20197
20198 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20199 if (OMP_CLAUSE_CODE (c) == code)
20200 {
20201 error ("%Htoo many %qs clauses", &location, name);
20202 break;
20203 }
20204 }
20205
20206 /* OpenMP 2.5:
20207 variable-list:
20208 identifier
20209 variable-list , identifier
20210
20211 In addition, we match a closing parenthesis. An opening parenthesis
20212 will have been consumed by the caller.
20213
20214 If KIND is nonzero, create the appropriate node and install the decl
20215 in OMP_CLAUSE_DECL and add the node to the head of the list.
20216
20217 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20218 return the list created. */
20219
20220 static tree
20221 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20222 tree list)
20223 {
20224 cp_token *token;
20225 while (1)
20226 {
20227 tree name, decl;
20228
20229 token = cp_lexer_peek_token (parser->lexer);
20230 name = cp_parser_id_expression (parser, /*template_p=*/false,
20231 /*check_dependency_p=*/true,
20232 /*template_p=*/NULL,
20233 /*declarator_p=*/false,
20234 /*optional_p=*/false);
20235 if (name == error_mark_node)
20236 goto skip_comma;
20237
20238 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20239 if (decl == error_mark_node)
20240 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20241 else if (kind != 0)
20242 {
20243 tree u = build_omp_clause (kind);
20244 OMP_CLAUSE_DECL (u) = decl;
20245 OMP_CLAUSE_CHAIN (u) = list;
20246 list = u;
20247 }
20248 else
20249 list = tree_cons (decl, NULL_TREE, list);
20250
20251 get_comma:
20252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20253 break;
20254 cp_lexer_consume_token (parser->lexer);
20255 }
20256
20257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20258 {
20259 int ending;
20260
20261 /* Try to resync to an unnested comma. Copied from
20262 cp_parser_parenthesized_expression_list. */
20263 skip_comma:
20264 ending = cp_parser_skip_to_closing_parenthesis (parser,
20265 /*recovering=*/true,
20266 /*or_comma=*/true,
20267 /*consume_paren=*/true);
20268 if (ending < 0)
20269 goto get_comma;
20270 }
20271
20272 return list;
20273 }
20274
20275 /* Similarly, but expect leading and trailing parenthesis. This is a very
20276 common case for omp clauses. */
20277
20278 static tree
20279 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20280 {
20281 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20282 return cp_parser_omp_var_list_no_open (parser, kind, list);
20283 return list;
20284 }
20285
20286 /* OpenMP 3.0:
20287 collapse ( constant-expression ) */
20288
20289 static tree
20290 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20291 {
20292 tree c, num;
20293 location_t loc;
20294 HOST_WIDE_INT n;
20295
20296 loc = cp_lexer_peek_token (parser->lexer)->location;
20297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20298 return list;
20299
20300 num = cp_parser_constant_expression (parser, false, NULL);
20301
20302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20303 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20304 /*or_comma=*/false,
20305 /*consume_paren=*/true);
20306
20307 if (num == error_mark_node)
20308 return list;
20309 num = fold_non_dependent_expr (num);
20310 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20311 || !host_integerp (num, 0)
20312 || (n = tree_low_cst (num, 0)) <= 0
20313 || (int) n != n)
20314 {
20315 error ("%Hcollapse argument needs positive constant integer expression",
20316 &loc);
20317 return list;
20318 }
20319
20320 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20321 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20322 OMP_CLAUSE_CHAIN (c) = list;
20323 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20324
20325 return c;
20326 }
20327
20328 /* OpenMP 2.5:
20329 default ( shared | none ) */
20330
20331 static tree
20332 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20333 {
20334 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20335 tree c;
20336
20337 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20338 return list;
20339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20340 {
20341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20342 const char *p = IDENTIFIER_POINTER (id);
20343
20344 switch (p[0])
20345 {
20346 case 'n':
20347 if (strcmp ("none", p) != 0)
20348 goto invalid_kind;
20349 kind = OMP_CLAUSE_DEFAULT_NONE;
20350 break;
20351
20352 case 's':
20353 if (strcmp ("shared", p) != 0)
20354 goto invalid_kind;
20355 kind = OMP_CLAUSE_DEFAULT_SHARED;
20356 break;
20357
20358 default:
20359 goto invalid_kind;
20360 }
20361
20362 cp_lexer_consume_token (parser->lexer);
20363 }
20364 else
20365 {
20366 invalid_kind:
20367 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20368 }
20369
20370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20372 /*or_comma=*/false,
20373 /*consume_paren=*/true);
20374
20375 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20376 return list;
20377
20378 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20379 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20380 OMP_CLAUSE_CHAIN (c) = list;
20381 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20382
20383 return c;
20384 }
20385
20386 /* OpenMP 2.5:
20387 if ( expression ) */
20388
20389 static tree
20390 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20391 {
20392 tree t, c;
20393
20394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20395 return list;
20396
20397 t = cp_parser_condition (parser);
20398
20399 if (t == error_mark_node
20400 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20402 /*or_comma=*/false,
20403 /*consume_paren=*/true);
20404
20405 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20406
20407 c = build_omp_clause (OMP_CLAUSE_IF);
20408 OMP_CLAUSE_IF_EXPR (c) = t;
20409 OMP_CLAUSE_CHAIN (c) = list;
20410
20411 return c;
20412 }
20413
20414 /* OpenMP 2.5:
20415 nowait */
20416
20417 static tree
20418 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20419 tree list, location_t location)
20420 {
20421 tree c;
20422
20423 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20424
20425 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20426 OMP_CLAUSE_CHAIN (c) = list;
20427 return c;
20428 }
20429
20430 /* OpenMP 2.5:
20431 num_threads ( expression ) */
20432
20433 static tree
20434 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20435 location_t location)
20436 {
20437 tree t, c;
20438
20439 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20440 return list;
20441
20442 t = cp_parser_expression (parser, false);
20443
20444 if (t == error_mark_node
20445 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20447 /*or_comma=*/false,
20448 /*consume_paren=*/true);
20449
20450 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20451 "num_threads", location);
20452
20453 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20454 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20455 OMP_CLAUSE_CHAIN (c) = list;
20456
20457 return c;
20458 }
20459
20460 /* OpenMP 2.5:
20461 ordered */
20462
20463 static tree
20464 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20465 tree list, location_t location)
20466 {
20467 tree c;
20468
20469 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20470 "ordered", location);
20471
20472 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20473 OMP_CLAUSE_CHAIN (c) = list;
20474 return c;
20475 }
20476
20477 /* OpenMP 2.5:
20478 reduction ( reduction-operator : variable-list )
20479
20480 reduction-operator:
20481 One of: + * - & ^ | && || */
20482
20483 static tree
20484 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20485 {
20486 enum tree_code code;
20487 tree nlist, c;
20488
20489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20490 return list;
20491
20492 switch (cp_lexer_peek_token (parser->lexer)->type)
20493 {
20494 case CPP_PLUS:
20495 code = PLUS_EXPR;
20496 break;
20497 case CPP_MULT:
20498 code = MULT_EXPR;
20499 break;
20500 case CPP_MINUS:
20501 code = MINUS_EXPR;
20502 break;
20503 case CPP_AND:
20504 code = BIT_AND_EXPR;
20505 break;
20506 case CPP_XOR:
20507 code = BIT_XOR_EXPR;
20508 break;
20509 case CPP_OR:
20510 code = BIT_IOR_EXPR;
20511 break;
20512 case CPP_AND_AND:
20513 code = TRUTH_ANDIF_EXPR;
20514 break;
20515 case CPP_OR_OR:
20516 code = TRUTH_ORIF_EXPR;
20517 break;
20518 default:
20519 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20520 "%<|%>, %<&&%>, or %<||%>");
20521 resync_fail:
20522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20523 /*or_comma=*/false,
20524 /*consume_paren=*/true);
20525 return list;
20526 }
20527 cp_lexer_consume_token (parser->lexer);
20528
20529 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20530 goto resync_fail;
20531
20532 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20533 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20534 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20535
20536 return nlist;
20537 }
20538
20539 /* OpenMP 2.5:
20540 schedule ( schedule-kind )
20541 schedule ( schedule-kind , expression )
20542
20543 schedule-kind:
20544 static | dynamic | guided | runtime | auto */
20545
20546 static tree
20547 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20548 {
20549 tree c, t;
20550
20551 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20552 return list;
20553
20554 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20555
20556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20557 {
20558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20559 const char *p = IDENTIFIER_POINTER (id);
20560
20561 switch (p[0])
20562 {
20563 case 'd':
20564 if (strcmp ("dynamic", p) != 0)
20565 goto invalid_kind;
20566 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20567 break;
20568
20569 case 'g':
20570 if (strcmp ("guided", p) != 0)
20571 goto invalid_kind;
20572 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20573 break;
20574
20575 case 'r':
20576 if (strcmp ("runtime", p) != 0)
20577 goto invalid_kind;
20578 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20579 break;
20580
20581 default:
20582 goto invalid_kind;
20583 }
20584 }
20585 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20586 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20588 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20589 else
20590 goto invalid_kind;
20591 cp_lexer_consume_token (parser->lexer);
20592
20593 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20594 {
20595 cp_token *token;
20596 cp_lexer_consume_token (parser->lexer);
20597
20598 token = cp_lexer_peek_token (parser->lexer);
20599 t = cp_parser_assignment_expression (parser, false);
20600
20601 if (t == error_mark_node)
20602 goto resync_fail;
20603 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20604 error ("%Hschedule %<runtime%> does not take "
20605 "a %<chunk_size%> parameter", &token->location);
20606 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20607 error ("%Hschedule %<auto%> does not take "
20608 "a %<chunk_size%> parameter", &token->location);
20609 else
20610 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20611
20612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20613 goto resync_fail;
20614 }
20615 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20616 goto resync_fail;
20617
20618 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20619 OMP_CLAUSE_CHAIN (c) = list;
20620 return c;
20621
20622 invalid_kind:
20623 cp_parser_error (parser, "invalid schedule kind");
20624 resync_fail:
20625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20626 /*or_comma=*/false,
20627 /*consume_paren=*/true);
20628 return list;
20629 }
20630
20631 /* OpenMP 3.0:
20632 untied */
20633
20634 static tree
20635 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20636 tree list, location_t location)
20637 {
20638 tree c;
20639
20640 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20641
20642 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20643 OMP_CLAUSE_CHAIN (c) = list;
20644 return c;
20645 }
20646
20647 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20648 is a bitmask in MASK. Return the list of clauses found; the result
20649 of clause default goes in *pdefault. */
20650
20651 static tree
20652 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20653 const char *where, cp_token *pragma_tok)
20654 {
20655 tree clauses = NULL;
20656 bool first = true;
20657 cp_token *token = NULL;
20658
20659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20660 {
20661 pragma_omp_clause c_kind;
20662 const char *c_name;
20663 tree prev = clauses;
20664
20665 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20666 cp_lexer_consume_token (parser->lexer);
20667
20668 token = cp_lexer_peek_token (parser->lexer);
20669 c_kind = cp_parser_omp_clause_name (parser);
20670 first = false;
20671
20672 switch (c_kind)
20673 {
20674 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20675 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20676 token->location);
20677 c_name = "collapse";
20678 break;
20679 case PRAGMA_OMP_CLAUSE_COPYIN:
20680 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20681 c_name = "copyin";
20682 break;
20683 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20685 clauses);
20686 c_name = "copyprivate";
20687 break;
20688 case PRAGMA_OMP_CLAUSE_DEFAULT:
20689 clauses = cp_parser_omp_clause_default (parser, clauses,
20690 token->location);
20691 c_name = "default";
20692 break;
20693 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20695 clauses);
20696 c_name = "firstprivate";
20697 break;
20698 case PRAGMA_OMP_CLAUSE_IF:
20699 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20700 c_name = "if";
20701 break;
20702 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20703 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20704 clauses);
20705 c_name = "lastprivate";
20706 break;
20707 case PRAGMA_OMP_CLAUSE_NOWAIT:
20708 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20709 c_name = "nowait";
20710 break;
20711 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20712 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20713 token->location);
20714 c_name = "num_threads";
20715 break;
20716 case PRAGMA_OMP_CLAUSE_ORDERED:
20717 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20718 token->location);
20719 c_name = "ordered";
20720 break;
20721 case PRAGMA_OMP_CLAUSE_PRIVATE:
20722 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20723 clauses);
20724 c_name = "private";
20725 break;
20726 case PRAGMA_OMP_CLAUSE_REDUCTION:
20727 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20728 c_name = "reduction";
20729 break;
20730 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20731 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20732 token->location);
20733 c_name = "schedule";
20734 break;
20735 case PRAGMA_OMP_CLAUSE_SHARED:
20736 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20737 clauses);
20738 c_name = "shared";
20739 break;
20740 case PRAGMA_OMP_CLAUSE_UNTIED:
20741 clauses = cp_parser_omp_clause_untied (parser, clauses,
20742 token->location);
20743 c_name = "nowait";
20744 break;
20745 default:
20746 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20747 goto saw_error;
20748 }
20749
20750 if (((mask >> c_kind) & 1) == 0)
20751 {
20752 /* Remove the invalid clause(s) from the list to avoid
20753 confusing the rest of the compiler. */
20754 clauses = prev;
20755 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20756 }
20757 }
20758 saw_error:
20759 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20760 return finish_omp_clauses (clauses);
20761 }
20762
20763 /* OpenMP 2.5:
20764 structured-block:
20765 statement
20766
20767 In practice, we're also interested in adding the statement to an
20768 outer node. So it is convenient if we work around the fact that
20769 cp_parser_statement calls add_stmt. */
20770
20771 static unsigned
20772 cp_parser_begin_omp_structured_block (cp_parser *parser)
20773 {
20774 unsigned save = parser->in_statement;
20775
20776 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20777 This preserves the "not within loop or switch" style error messages
20778 for nonsense cases like
20779 void foo() {
20780 #pragma omp single
20781 break;
20782 }
20783 */
20784 if (parser->in_statement)
20785 parser->in_statement = IN_OMP_BLOCK;
20786
20787 return save;
20788 }
20789
20790 static void
20791 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20792 {
20793 parser->in_statement = save;
20794 }
20795
20796 static tree
20797 cp_parser_omp_structured_block (cp_parser *parser)
20798 {
20799 tree stmt = begin_omp_structured_block ();
20800 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20801
20802 cp_parser_statement (parser, NULL_TREE, false, NULL);
20803
20804 cp_parser_end_omp_structured_block (parser, save);
20805 return finish_omp_structured_block (stmt);
20806 }
20807
20808 /* OpenMP 2.5:
20809 # pragma omp atomic new-line
20810 expression-stmt
20811
20812 expression-stmt:
20813 x binop= expr | x++ | ++x | x-- | --x
20814 binop:
20815 +, *, -, /, &, ^, |, <<, >>
20816
20817 where x is an lvalue expression with scalar type. */
20818
20819 static void
20820 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20821 {
20822 tree lhs, rhs;
20823 enum tree_code code;
20824
20825 cp_parser_require_pragma_eol (parser, pragma_tok);
20826
20827 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20828 /*cast_p=*/false);
20829 switch (TREE_CODE (lhs))
20830 {
20831 case ERROR_MARK:
20832 goto saw_error;
20833
20834 case PREINCREMENT_EXPR:
20835 case POSTINCREMENT_EXPR:
20836 lhs = TREE_OPERAND (lhs, 0);
20837 code = PLUS_EXPR;
20838 rhs = integer_one_node;
20839 break;
20840
20841 case PREDECREMENT_EXPR:
20842 case POSTDECREMENT_EXPR:
20843 lhs = TREE_OPERAND (lhs, 0);
20844 code = MINUS_EXPR;
20845 rhs = integer_one_node;
20846 break;
20847
20848 default:
20849 switch (cp_lexer_peek_token (parser->lexer)->type)
20850 {
20851 case CPP_MULT_EQ:
20852 code = MULT_EXPR;
20853 break;
20854 case CPP_DIV_EQ:
20855 code = TRUNC_DIV_EXPR;
20856 break;
20857 case CPP_PLUS_EQ:
20858 code = PLUS_EXPR;
20859 break;
20860 case CPP_MINUS_EQ:
20861 code = MINUS_EXPR;
20862 break;
20863 case CPP_LSHIFT_EQ:
20864 code = LSHIFT_EXPR;
20865 break;
20866 case CPP_RSHIFT_EQ:
20867 code = RSHIFT_EXPR;
20868 break;
20869 case CPP_AND_EQ:
20870 code = BIT_AND_EXPR;
20871 break;
20872 case CPP_OR_EQ:
20873 code = BIT_IOR_EXPR;
20874 break;
20875 case CPP_XOR_EQ:
20876 code = BIT_XOR_EXPR;
20877 break;
20878 default:
20879 cp_parser_error (parser,
20880 "invalid operator for %<#pragma omp atomic%>");
20881 goto saw_error;
20882 }
20883 cp_lexer_consume_token (parser->lexer);
20884
20885 rhs = cp_parser_expression (parser, false);
20886 if (rhs == error_mark_node)
20887 goto saw_error;
20888 break;
20889 }
20890 finish_omp_atomic (code, lhs, rhs);
20891 cp_parser_consume_semicolon_at_end_of_statement (parser);
20892 return;
20893
20894 saw_error:
20895 cp_parser_skip_to_end_of_block_or_statement (parser);
20896 }
20897
20898
20899 /* OpenMP 2.5:
20900 # pragma omp barrier new-line */
20901
20902 static void
20903 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20904 {
20905 cp_parser_require_pragma_eol (parser, pragma_tok);
20906 finish_omp_barrier ();
20907 }
20908
20909 /* OpenMP 2.5:
20910 # pragma omp critical [(name)] new-line
20911 structured-block */
20912
20913 static tree
20914 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20915 {
20916 tree stmt, name = NULL;
20917
20918 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20919 {
20920 cp_lexer_consume_token (parser->lexer);
20921
20922 name = cp_parser_identifier (parser);
20923
20924 if (name == error_mark_node
20925 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20927 /*or_comma=*/false,
20928 /*consume_paren=*/true);
20929 if (name == error_mark_node)
20930 name = NULL;
20931 }
20932 cp_parser_require_pragma_eol (parser, pragma_tok);
20933
20934 stmt = cp_parser_omp_structured_block (parser);
20935 return c_finish_omp_critical (stmt, name);
20936 }
20937
20938 /* OpenMP 2.5:
20939 # pragma omp flush flush-vars[opt] new-line
20940
20941 flush-vars:
20942 ( variable-list ) */
20943
20944 static void
20945 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20946 {
20947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20948 (void) cp_parser_omp_var_list (parser, 0, NULL);
20949 cp_parser_require_pragma_eol (parser, pragma_tok);
20950
20951 finish_omp_flush ();
20952 }
20953
20954 /* Helper function, to parse omp for increment expression. */
20955
20956 static tree
20957 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20958 {
20959 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20960 enum tree_code op;
20961 cp_token *token;
20962
20963 if (lhs != decl)
20964 {
20965 cp_parser_skip_to_end_of_statement (parser);
20966 return error_mark_node;
20967 }
20968
20969 token = cp_lexer_peek_token (parser->lexer);
20970 op = binops_by_token [token->type].tree_type;
20971 switch (op)
20972 {
20973 case LT_EXPR:
20974 case LE_EXPR:
20975 case GT_EXPR:
20976 case GE_EXPR:
20977 break;
20978 default:
20979 cp_parser_skip_to_end_of_statement (parser);
20980 return error_mark_node;
20981 }
20982
20983 cp_lexer_consume_token (parser->lexer);
20984 rhs = cp_parser_binary_expression (parser, false,
20985 PREC_RELATIONAL_EXPRESSION);
20986 if (rhs == error_mark_node
20987 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20988 {
20989 cp_parser_skip_to_end_of_statement (parser);
20990 return error_mark_node;
20991 }
20992
20993 return build2 (op, boolean_type_node, lhs, rhs);
20994 }
20995
20996 /* Helper function, to parse omp for increment expression. */
20997
20998 static tree
20999 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21000 {
21001 cp_token *token = cp_lexer_peek_token (parser->lexer);
21002 enum tree_code op;
21003 tree lhs, rhs;
21004 cp_id_kind idk;
21005 bool decl_first;
21006
21007 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21008 {
21009 op = (token->type == CPP_PLUS_PLUS
21010 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21011 cp_lexer_consume_token (parser->lexer);
21012 lhs = cp_parser_cast_expression (parser, false, false);
21013 if (lhs != decl)
21014 return error_mark_node;
21015 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21016 }
21017
21018 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21019 if (lhs != decl)
21020 return error_mark_node;
21021
21022 token = cp_lexer_peek_token (parser->lexer);
21023 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21024 {
21025 op = (token->type == CPP_PLUS_PLUS
21026 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21027 cp_lexer_consume_token (parser->lexer);
21028 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21029 }
21030
21031 op = cp_parser_assignment_operator_opt (parser);
21032 if (op == ERROR_MARK)
21033 return error_mark_node;
21034
21035 if (op != NOP_EXPR)
21036 {
21037 rhs = cp_parser_assignment_expression (parser, false);
21038 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21039 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21040 }
21041
21042 lhs = cp_parser_binary_expression (parser, false,
21043 PREC_ADDITIVE_EXPRESSION);
21044 token = cp_lexer_peek_token (parser->lexer);
21045 decl_first = lhs == decl;
21046 if (decl_first)
21047 lhs = NULL_TREE;
21048 if (token->type != CPP_PLUS
21049 && token->type != CPP_MINUS)
21050 return error_mark_node;
21051
21052 do
21053 {
21054 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21055 cp_lexer_consume_token (parser->lexer);
21056 rhs = cp_parser_binary_expression (parser, false,
21057 PREC_ADDITIVE_EXPRESSION);
21058 token = cp_lexer_peek_token (parser->lexer);
21059 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21060 {
21061 if (lhs == NULL_TREE)
21062 {
21063 if (op == PLUS_EXPR)
21064 lhs = rhs;
21065 else
21066 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21067 }
21068 else
21069 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21070 NULL, tf_warning_or_error);
21071 }
21072 }
21073 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21074
21075 if (!decl_first)
21076 {
21077 if (rhs != decl || op == MINUS_EXPR)
21078 return error_mark_node;
21079 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21080 }
21081 else
21082 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21083
21084 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21085 }
21086
21087 /* Parse the restricted form of the for statement allowed by OpenMP. */
21088
21089 static tree
21090 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21091 {
21092 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21093 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21094 tree this_pre_body, cl;
21095 location_t loc_first;
21096 bool collapse_err = false;
21097 int i, collapse = 1, nbraces = 0;
21098
21099 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21100 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21101 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21102
21103 gcc_assert (collapse >= 1);
21104
21105 declv = make_tree_vec (collapse);
21106 initv = make_tree_vec (collapse);
21107 condv = make_tree_vec (collapse);
21108 incrv = make_tree_vec (collapse);
21109
21110 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21111
21112 for (i = 0; i < collapse; i++)
21113 {
21114 int bracecount = 0;
21115 bool add_private_clause = false;
21116 location_t loc;
21117
21118 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21119 {
21120 cp_parser_error (parser, "for statement expected");
21121 return NULL;
21122 }
21123 loc = cp_lexer_consume_token (parser->lexer)->location;
21124
21125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21126 return NULL;
21127
21128 init = decl = real_decl = NULL;
21129 this_pre_body = push_stmt_list ();
21130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21131 {
21132 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21133
21134 init-expr:
21135 var = lb
21136 integer-type var = lb
21137 random-access-iterator-type var = lb
21138 pointer-type var = lb
21139 */
21140 cp_decl_specifier_seq type_specifiers;
21141
21142 /* First, try to parse as an initialized declaration. See
21143 cp_parser_condition, from whence the bulk of this is copied. */
21144
21145 cp_parser_parse_tentatively (parser);
21146 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21147 &type_specifiers);
21148 if (cp_parser_parse_definitely (parser))
21149 {
21150 /* If parsing a type specifier seq succeeded, then this
21151 MUST be a initialized declaration. */
21152 tree asm_specification, attributes;
21153 cp_declarator *declarator;
21154
21155 declarator = cp_parser_declarator (parser,
21156 CP_PARSER_DECLARATOR_NAMED,
21157 /*ctor_dtor_or_conv_p=*/NULL,
21158 /*parenthesized_p=*/NULL,
21159 /*member_p=*/false);
21160 attributes = cp_parser_attributes_opt (parser);
21161 asm_specification = cp_parser_asm_specification_opt (parser);
21162
21163 if (declarator == cp_error_declarator)
21164 cp_parser_skip_to_end_of_statement (parser);
21165
21166 else
21167 {
21168 tree pushed_scope, auto_node;
21169
21170 decl = start_decl (declarator, &type_specifiers,
21171 SD_INITIALIZED, attributes,
21172 /*prefix_attributes=*/NULL_TREE,
21173 &pushed_scope);
21174
21175 auto_node = type_uses_auto (TREE_TYPE (decl));
21176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21177 {
21178 if (cp_lexer_next_token_is (parser->lexer,
21179 CPP_OPEN_PAREN))
21180 error ("parenthesized initialization is not allowed in "
21181 "OpenMP %<for%> loop");
21182 else
21183 /* Trigger an error. */
21184 cp_parser_require (parser, CPP_EQ, "%<=%>");
21185
21186 init = error_mark_node;
21187 cp_parser_skip_to_end_of_statement (parser);
21188 }
21189 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21190 || type_dependent_expression_p (decl)
21191 || auto_node)
21192 {
21193 bool is_direct_init, is_non_constant_init;
21194
21195 init = cp_parser_initializer (parser,
21196 &is_direct_init,
21197 &is_non_constant_init);
21198
21199 if (auto_node && describable_type (init))
21200 {
21201 TREE_TYPE (decl)
21202 = do_auto_deduction (TREE_TYPE (decl), init,
21203 auto_node);
21204
21205 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21206 && !type_dependent_expression_p (decl))
21207 goto non_class;
21208 }
21209
21210 cp_finish_decl (decl, init, !is_non_constant_init,
21211 asm_specification,
21212 LOOKUP_ONLYCONVERTING);
21213 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21214 {
21215 for_block
21216 = tree_cons (NULL, this_pre_body, for_block);
21217 init = NULL_TREE;
21218 }
21219 else
21220 init = pop_stmt_list (this_pre_body);
21221 this_pre_body = NULL_TREE;
21222 }
21223 else
21224 {
21225 /* Consume '='. */
21226 cp_lexer_consume_token (parser->lexer);
21227 init = cp_parser_assignment_expression (parser, false);
21228
21229 non_class:
21230 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21231 init = error_mark_node;
21232 else
21233 cp_finish_decl (decl, NULL_TREE,
21234 /*init_const_expr_p=*/false,
21235 asm_specification,
21236 LOOKUP_ONLYCONVERTING);
21237 }
21238
21239 if (pushed_scope)
21240 pop_scope (pushed_scope);
21241 }
21242 }
21243 else
21244 {
21245 cp_id_kind idk;
21246 /* If parsing a type specifier sequence failed, then
21247 this MUST be a simple expression. */
21248 cp_parser_parse_tentatively (parser);
21249 decl = cp_parser_primary_expression (parser, false, false,
21250 false, &idk);
21251 if (!cp_parser_error_occurred (parser)
21252 && decl
21253 && DECL_P (decl)
21254 && CLASS_TYPE_P (TREE_TYPE (decl)))
21255 {
21256 tree rhs;
21257
21258 cp_parser_parse_definitely (parser);
21259 cp_parser_require (parser, CPP_EQ, "%<=%>");
21260 rhs = cp_parser_assignment_expression (parser, false);
21261 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21262 rhs,
21263 tf_warning_or_error));
21264 add_private_clause = true;
21265 }
21266 else
21267 {
21268 decl = NULL;
21269 cp_parser_abort_tentative_parse (parser);
21270 init = cp_parser_expression (parser, false);
21271 if (init)
21272 {
21273 if (TREE_CODE (init) == MODIFY_EXPR
21274 || TREE_CODE (init) == MODOP_EXPR)
21275 real_decl = TREE_OPERAND (init, 0);
21276 }
21277 }
21278 }
21279 }
21280 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21281 if (this_pre_body)
21282 {
21283 this_pre_body = pop_stmt_list (this_pre_body);
21284 if (pre_body)
21285 {
21286 tree t = pre_body;
21287 pre_body = push_stmt_list ();
21288 add_stmt (t);
21289 add_stmt (this_pre_body);
21290 pre_body = pop_stmt_list (pre_body);
21291 }
21292 else
21293 pre_body = this_pre_body;
21294 }
21295
21296 if (decl)
21297 real_decl = decl;
21298 if (par_clauses != NULL && real_decl != NULL_TREE)
21299 {
21300 tree *c;
21301 for (c = par_clauses; *c ; )
21302 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21303 && OMP_CLAUSE_DECL (*c) == real_decl)
21304 {
21305 error ("%Hiteration variable %qD should not be firstprivate",
21306 &loc, real_decl);
21307 *c = OMP_CLAUSE_CHAIN (*c);
21308 }
21309 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21310 && OMP_CLAUSE_DECL (*c) == real_decl)
21311 {
21312 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21313 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21314 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21315 OMP_CLAUSE_DECL (l) = real_decl;
21316 OMP_CLAUSE_CHAIN (l) = clauses;
21317 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21318 clauses = l;
21319 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21320 CP_OMP_CLAUSE_INFO (*c) = NULL;
21321 add_private_clause = false;
21322 }
21323 else
21324 {
21325 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21326 && OMP_CLAUSE_DECL (*c) == real_decl)
21327 add_private_clause = false;
21328 c = &OMP_CLAUSE_CHAIN (*c);
21329 }
21330 }
21331
21332 if (add_private_clause)
21333 {
21334 tree c;
21335 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21336 {
21337 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21338 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21339 && OMP_CLAUSE_DECL (c) == decl)
21340 break;
21341 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21342 && OMP_CLAUSE_DECL (c) == decl)
21343 error ("%Hiteration variable %qD should not be firstprivate",
21344 &loc, decl);
21345 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21346 && OMP_CLAUSE_DECL (c) == decl)
21347 error ("%Hiteration variable %qD should not be reduction",
21348 &loc, decl);
21349 }
21350 if (c == NULL)
21351 {
21352 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21353 OMP_CLAUSE_DECL (c) = decl;
21354 c = finish_omp_clauses (c);
21355 if (c)
21356 {
21357 OMP_CLAUSE_CHAIN (c) = clauses;
21358 clauses = c;
21359 }
21360 }
21361 }
21362
21363 cond = NULL;
21364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21365 {
21366 /* If decl is an iterator, preserve LHS and RHS of the relational
21367 expr until finish_omp_for. */
21368 if (decl
21369 && (type_dependent_expression_p (decl)
21370 || CLASS_TYPE_P (TREE_TYPE (decl))))
21371 cond = cp_parser_omp_for_cond (parser, decl);
21372 else
21373 cond = cp_parser_condition (parser);
21374 }
21375 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21376
21377 incr = NULL;
21378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21379 {
21380 /* If decl is an iterator, preserve the operator on decl
21381 until finish_omp_for. */
21382 if (decl
21383 && (type_dependent_expression_p (decl)
21384 || CLASS_TYPE_P (TREE_TYPE (decl))))
21385 incr = cp_parser_omp_for_incr (parser, decl);
21386 else
21387 incr = cp_parser_expression (parser, false);
21388 }
21389
21390 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21391 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21392 /*or_comma=*/false,
21393 /*consume_paren=*/true);
21394
21395 TREE_VEC_ELT (declv, i) = decl;
21396 TREE_VEC_ELT (initv, i) = init;
21397 TREE_VEC_ELT (condv, i) = cond;
21398 TREE_VEC_ELT (incrv, i) = incr;
21399
21400 if (i == collapse - 1)
21401 break;
21402
21403 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21404 in between the collapsed for loops to be still considered perfectly
21405 nested. Hopefully the final version clarifies this.
21406 For now handle (multiple) {'s and empty statements. */
21407 cp_parser_parse_tentatively (parser);
21408 do
21409 {
21410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21411 break;
21412 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21413 {
21414 cp_lexer_consume_token (parser->lexer);
21415 bracecount++;
21416 }
21417 else if (bracecount
21418 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21419 cp_lexer_consume_token (parser->lexer);
21420 else
21421 {
21422 loc = cp_lexer_peek_token (parser->lexer)->location;
21423 error ("%Hnot enough collapsed for loops", &loc);
21424 collapse_err = true;
21425 cp_parser_abort_tentative_parse (parser);
21426 declv = NULL_TREE;
21427 break;
21428 }
21429 }
21430 while (1);
21431
21432 if (declv)
21433 {
21434 cp_parser_parse_definitely (parser);
21435 nbraces += bracecount;
21436 }
21437 }
21438
21439 /* Note that we saved the original contents of this flag when we entered
21440 the structured block, and so we don't need to re-save it here. */
21441 parser->in_statement = IN_OMP_FOR;
21442
21443 /* Note that the grammar doesn't call for a structured block here,
21444 though the loop as a whole is a structured block. */
21445 body = push_stmt_list ();
21446 cp_parser_statement (parser, NULL_TREE, false, NULL);
21447 body = pop_stmt_list (body);
21448
21449 if (declv == NULL_TREE)
21450 ret = NULL_TREE;
21451 else
21452 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21453 pre_body, clauses);
21454
21455 while (nbraces)
21456 {
21457 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21458 {
21459 cp_lexer_consume_token (parser->lexer);
21460 nbraces--;
21461 }
21462 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21463 cp_lexer_consume_token (parser->lexer);
21464 else
21465 {
21466 if (!collapse_err)
21467 {
21468 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21469 error ("%Hcollapsed loops not perfectly nested", &loc);
21470 }
21471 collapse_err = true;
21472 cp_parser_statement_seq_opt (parser, NULL);
21473 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21474 }
21475 }
21476
21477 while (for_block)
21478 {
21479 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21480 for_block = TREE_CHAIN (for_block);
21481 }
21482
21483 return ret;
21484 }
21485
21486 /* OpenMP 2.5:
21487 #pragma omp for for-clause[optseq] new-line
21488 for-loop */
21489
21490 #define OMP_FOR_CLAUSE_MASK \
21491 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21492 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21493 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21494 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21495 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21496 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21497 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21498 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21499
21500 static tree
21501 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21502 {
21503 tree clauses, sb, ret;
21504 unsigned int save;
21505
21506 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21507 "#pragma omp for", pragma_tok);
21508
21509 sb = begin_omp_structured_block ();
21510 save = cp_parser_begin_omp_structured_block (parser);
21511
21512 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21513
21514 cp_parser_end_omp_structured_block (parser, save);
21515 add_stmt (finish_omp_structured_block (sb));
21516
21517 return ret;
21518 }
21519
21520 /* OpenMP 2.5:
21521 # pragma omp master new-line
21522 structured-block */
21523
21524 static tree
21525 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21526 {
21527 cp_parser_require_pragma_eol (parser, pragma_tok);
21528 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21529 }
21530
21531 /* OpenMP 2.5:
21532 # pragma omp ordered new-line
21533 structured-block */
21534
21535 static tree
21536 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21537 {
21538 cp_parser_require_pragma_eol (parser, pragma_tok);
21539 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21540 }
21541
21542 /* OpenMP 2.5:
21543
21544 section-scope:
21545 { section-sequence }
21546
21547 section-sequence:
21548 section-directive[opt] structured-block
21549 section-sequence section-directive structured-block */
21550
21551 static tree
21552 cp_parser_omp_sections_scope (cp_parser *parser)
21553 {
21554 tree stmt, substmt;
21555 bool error_suppress = false;
21556 cp_token *tok;
21557
21558 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21559 return NULL_TREE;
21560
21561 stmt = push_stmt_list ();
21562
21563 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21564 {
21565 unsigned save;
21566
21567 substmt = begin_omp_structured_block ();
21568 save = cp_parser_begin_omp_structured_block (parser);
21569
21570 while (1)
21571 {
21572 cp_parser_statement (parser, NULL_TREE, false, NULL);
21573
21574 tok = cp_lexer_peek_token (parser->lexer);
21575 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21576 break;
21577 if (tok->type == CPP_CLOSE_BRACE)
21578 break;
21579 if (tok->type == CPP_EOF)
21580 break;
21581 }
21582
21583 cp_parser_end_omp_structured_block (parser, save);
21584 substmt = finish_omp_structured_block (substmt);
21585 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21586 add_stmt (substmt);
21587 }
21588
21589 while (1)
21590 {
21591 tok = cp_lexer_peek_token (parser->lexer);
21592 if (tok->type == CPP_CLOSE_BRACE)
21593 break;
21594 if (tok->type == CPP_EOF)
21595 break;
21596
21597 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21598 {
21599 cp_lexer_consume_token (parser->lexer);
21600 cp_parser_require_pragma_eol (parser, tok);
21601 error_suppress = false;
21602 }
21603 else if (!error_suppress)
21604 {
21605 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21606 error_suppress = true;
21607 }
21608
21609 substmt = cp_parser_omp_structured_block (parser);
21610 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21611 add_stmt (substmt);
21612 }
21613 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21614
21615 substmt = pop_stmt_list (stmt);
21616
21617 stmt = make_node (OMP_SECTIONS);
21618 TREE_TYPE (stmt) = void_type_node;
21619 OMP_SECTIONS_BODY (stmt) = substmt;
21620
21621 add_stmt (stmt);
21622 return stmt;
21623 }
21624
21625 /* OpenMP 2.5:
21626 # pragma omp sections sections-clause[optseq] newline
21627 sections-scope */
21628
21629 #define OMP_SECTIONS_CLAUSE_MASK \
21630 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21631 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21632 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21633 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21634 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21635
21636 static tree
21637 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21638 {
21639 tree clauses, ret;
21640
21641 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21642 "#pragma omp sections", pragma_tok);
21643
21644 ret = cp_parser_omp_sections_scope (parser);
21645 if (ret)
21646 OMP_SECTIONS_CLAUSES (ret) = clauses;
21647
21648 return ret;
21649 }
21650
21651 /* OpenMP 2.5:
21652 # pragma parallel parallel-clause new-line
21653 # pragma parallel for parallel-for-clause new-line
21654 # pragma parallel sections parallel-sections-clause new-line */
21655
21656 #define OMP_PARALLEL_CLAUSE_MASK \
21657 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21658 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21659 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21660 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21661 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21662 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21663 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21664 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21665
21666 static tree
21667 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21668 {
21669 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21670 const char *p_name = "#pragma omp parallel";
21671 tree stmt, clauses, par_clause, ws_clause, block;
21672 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21673 unsigned int save;
21674
21675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21676 {
21677 cp_lexer_consume_token (parser->lexer);
21678 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21679 p_name = "#pragma omp parallel for";
21680 mask |= OMP_FOR_CLAUSE_MASK;
21681 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21682 }
21683 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21684 {
21685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21686 const char *p = IDENTIFIER_POINTER (id);
21687 if (strcmp (p, "sections") == 0)
21688 {
21689 cp_lexer_consume_token (parser->lexer);
21690 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21691 p_name = "#pragma omp parallel sections";
21692 mask |= OMP_SECTIONS_CLAUSE_MASK;
21693 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21694 }
21695 }
21696
21697 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21698 block = begin_omp_parallel ();
21699 save = cp_parser_begin_omp_structured_block (parser);
21700
21701 switch (p_kind)
21702 {
21703 case PRAGMA_OMP_PARALLEL:
21704 cp_parser_statement (parser, NULL_TREE, false, NULL);
21705 par_clause = clauses;
21706 break;
21707
21708 case PRAGMA_OMP_PARALLEL_FOR:
21709 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21710 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21711 break;
21712
21713 case PRAGMA_OMP_PARALLEL_SECTIONS:
21714 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21715 stmt = cp_parser_omp_sections_scope (parser);
21716 if (stmt)
21717 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21718 break;
21719
21720 default:
21721 gcc_unreachable ();
21722 }
21723
21724 cp_parser_end_omp_structured_block (parser, save);
21725 stmt = finish_omp_parallel (par_clause, block);
21726 if (p_kind != PRAGMA_OMP_PARALLEL)
21727 OMP_PARALLEL_COMBINED (stmt) = 1;
21728 return stmt;
21729 }
21730
21731 /* OpenMP 2.5:
21732 # pragma omp single single-clause[optseq] new-line
21733 structured-block */
21734
21735 #define OMP_SINGLE_CLAUSE_MASK \
21736 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21737 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21738 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21739 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21740
21741 static tree
21742 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21743 {
21744 tree stmt = make_node (OMP_SINGLE);
21745 TREE_TYPE (stmt) = void_type_node;
21746
21747 OMP_SINGLE_CLAUSES (stmt)
21748 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21749 "#pragma omp single", pragma_tok);
21750 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21751
21752 return add_stmt (stmt);
21753 }
21754
21755 /* OpenMP 3.0:
21756 # pragma omp task task-clause[optseq] new-line
21757 structured-block */
21758
21759 #define OMP_TASK_CLAUSE_MASK \
21760 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21761 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21762 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21763 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21764 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21765 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21766
21767 static tree
21768 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21769 {
21770 tree clauses, block;
21771 unsigned int save;
21772
21773 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21774 "#pragma omp task", pragma_tok);
21775 block = begin_omp_task ();
21776 save = cp_parser_begin_omp_structured_block (parser);
21777 cp_parser_statement (parser, NULL_TREE, false, NULL);
21778 cp_parser_end_omp_structured_block (parser, save);
21779 return finish_omp_task (clauses, block);
21780 }
21781
21782 /* OpenMP 3.0:
21783 # pragma omp taskwait new-line */
21784
21785 static void
21786 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21787 {
21788 cp_parser_require_pragma_eol (parser, pragma_tok);
21789 finish_omp_taskwait ();
21790 }
21791
21792 /* OpenMP 2.5:
21793 # pragma omp threadprivate (variable-list) */
21794
21795 static void
21796 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21797 {
21798 tree vars;
21799
21800 vars = cp_parser_omp_var_list (parser, 0, NULL);
21801 cp_parser_require_pragma_eol (parser, pragma_tok);
21802
21803 finish_omp_threadprivate (vars);
21804 }
21805
21806 /* Main entry point to OpenMP statement pragmas. */
21807
21808 static void
21809 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21810 {
21811 tree stmt;
21812
21813 switch (pragma_tok->pragma_kind)
21814 {
21815 case PRAGMA_OMP_ATOMIC:
21816 cp_parser_omp_atomic (parser, pragma_tok);
21817 return;
21818 case PRAGMA_OMP_CRITICAL:
21819 stmt = cp_parser_omp_critical (parser, pragma_tok);
21820 break;
21821 case PRAGMA_OMP_FOR:
21822 stmt = cp_parser_omp_for (parser, pragma_tok);
21823 break;
21824 case PRAGMA_OMP_MASTER:
21825 stmt = cp_parser_omp_master (parser, pragma_tok);
21826 break;
21827 case PRAGMA_OMP_ORDERED:
21828 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21829 break;
21830 case PRAGMA_OMP_PARALLEL:
21831 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21832 break;
21833 case PRAGMA_OMP_SECTIONS:
21834 stmt = cp_parser_omp_sections (parser, pragma_tok);
21835 break;
21836 case PRAGMA_OMP_SINGLE:
21837 stmt = cp_parser_omp_single (parser, pragma_tok);
21838 break;
21839 case PRAGMA_OMP_TASK:
21840 stmt = cp_parser_omp_task (parser, pragma_tok);
21841 break;
21842 default:
21843 gcc_unreachable ();
21844 }
21845
21846 if (stmt)
21847 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21848 }
21849 \f
21850 /* The parser. */
21851
21852 static GTY (()) cp_parser *the_parser;
21853
21854 \f
21855 /* Special handling for the first token or line in the file. The first
21856 thing in the file might be #pragma GCC pch_preprocess, which loads a
21857 PCH file, which is a GC collection point. So we need to handle this
21858 first pragma without benefit of an existing lexer structure.
21859
21860 Always returns one token to the caller in *FIRST_TOKEN. This is
21861 either the true first token of the file, or the first token after
21862 the initial pragma. */
21863
21864 static void
21865 cp_parser_initial_pragma (cp_token *first_token)
21866 {
21867 tree name = NULL;
21868
21869 cp_lexer_get_preprocessor_token (NULL, first_token);
21870 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21871 return;
21872
21873 cp_lexer_get_preprocessor_token (NULL, first_token);
21874 if (first_token->type == CPP_STRING)
21875 {
21876 name = first_token->u.value;
21877
21878 cp_lexer_get_preprocessor_token (NULL, first_token);
21879 if (first_token->type != CPP_PRAGMA_EOL)
21880 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21881 &first_token->location);
21882 }
21883 else
21884 error ("%Hexpected string literal", &first_token->location);
21885
21886 /* Skip to the end of the pragma. */
21887 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21888 cp_lexer_get_preprocessor_token (NULL, first_token);
21889
21890 /* Now actually load the PCH file. */
21891 if (name)
21892 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21893
21894 /* Read one more token to return to our caller. We have to do this
21895 after reading the PCH file in, since its pointers have to be
21896 live. */
21897 cp_lexer_get_preprocessor_token (NULL, first_token);
21898 }
21899
21900 /* Normal parsing of a pragma token. Here we can (and must) use the
21901 regular lexer. */
21902
21903 static bool
21904 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21905 {
21906 cp_token *pragma_tok;
21907 unsigned int id;
21908
21909 pragma_tok = cp_lexer_consume_token (parser->lexer);
21910 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21911 parser->lexer->in_pragma = true;
21912
21913 id = pragma_tok->pragma_kind;
21914 switch (id)
21915 {
21916 case PRAGMA_GCC_PCH_PREPROCESS:
21917 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21918 &pragma_tok->location);
21919 break;
21920
21921 case PRAGMA_OMP_BARRIER:
21922 switch (context)
21923 {
21924 case pragma_compound:
21925 cp_parser_omp_barrier (parser, pragma_tok);
21926 return false;
21927 case pragma_stmt:
21928 error ("%H%<#pragma omp barrier%> may only be "
21929 "used in compound statements", &pragma_tok->location);
21930 break;
21931 default:
21932 goto bad_stmt;
21933 }
21934 break;
21935
21936 case PRAGMA_OMP_FLUSH:
21937 switch (context)
21938 {
21939 case pragma_compound:
21940 cp_parser_omp_flush (parser, pragma_tok);
21941 return false;
21942 case pragma_stmt:
21943 error ("%H%<#pragma omp flush%> may only be "
21944 "used in compound statements", &pragma_tok->location);
21945 break;
21946 default:
21947 goto bad_stmt;
21948 }
21949 break;
21950
21951 case PRAGMA_OMP_TASKWAIT:
21952 switch (context)
21953 {
21954 case pragma_compound:
21955 cp_parser_omp_taskwait (parser, pragma_tok);
21956 return false;
21957 case pragma_stmt:
21958 error ("%H%<#pragma omp taskwait%> may only be "
21959 "used in compound statements",
21960 &pragma_tok->location);
21961 break;
21962 default:
21963 goto bad_stmt;
21964 }
21965 break;
21966
21967 case PRAGMA_OMP_THREADPRIVATE:
21968 cp_parser_omp_threadprivate (parser, pragma_tok);
21969 return false;
21970
21971 case PRAGMA_OMP_ATOMIC:
21972 case PRAGMA_OMP_CRITICAL:
21973 case PRAGMA_OMP_FOR:
21974 case PRAGMA_OMP_MASTER:
21975 case PRAGMA_OMP_ORDERED:
21976 case PRAGMA_OMP_PARALLEL:
21977 case PRAGMA_OMP_SECTIONS:
21978 case PRAGMA_OMP_SINGLE:
21979 case PRAGMA_OMP_TASK:
21980 if (context == pragma_external)
21981 goto bad_stmt;
21982 cp_parser_omp_construct (parser, pragma_tok);
21983 return true;
21984
21985 case PRAGMA_OMP_SECTION:
21986 error ("%H%<#pragma omp section%> may only be used in "
21987 "%<#pragma omp sections%> construct", &pragma_tok->location);
21988 break;
21989
21990 default:
21991 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21992 c_invoke_pragma_handler (id);
21993 break;
21994
21995 bad_stmt:
21996 cp_parser_error (parser, "expected declaration specifiers");
21997 break;
21998 }
21999
22000 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22001 return false;
22002 }
22003
22004 /* The interface the pragma parsers have to the lexer. */
22005
22006 enum cpp_ttype
22007 pragma_lex (tree *value)
22008 {
22009 cp_token *tok;
22010 enum cpp_ttype ret;
22011
22012 tok = cp_lexer_peek_token (the_parser->lexer);
22013
22014 ret = tok->type;
22015 *value = tok->u.value;
22016
22017 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22018 ret = CPP_EOF;
22019 else if (ret == CPP_STRING)
22020 *value = cp_parser_string_literal (the_parser, false, false);
22021 else
22022 {
22023 cp_lexer_consume_token (the_parser->lexer);
22024 if (ret == CPP_KEYWORD)
22025 ret = CPP_NAME;
22026 }
22027
22028 return ret;
22029 }
22030
22031 \f
22032 /* External interface. */
22033
22034 /* Parse one entire translation unit. */
22035
22036 void
22037 c_parse_file (void)
22038 {
22039 bool error_occurred;
22040 static bool already_called = false;
22041
22042 if (already_called)
22043 {
22044 sorry ("inter-module optimizations not implemented for C++");
22045 return;
22046 }
22047 already_called = true;
22048
22049 the_parser = cp_parser_new ();
22050 push_deferring_access_checks (flag_access_control
22051 ? dk_no_deferred : dk_no_check);
22052 error_occurred = cp_parser_translation_unit (the_parser);
22053 the_parser = NULL;
22054 }
22055
22056 #include "gt-cp-parser.h"