526a9a3acefbf3895a75484f790fd2344eccd1a6
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer. */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
46
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
49
50 struct tree_check GTY(())
51 {
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
59 };
60
61 /* A C++ token. */
62
63 typedef struct cp_token GTY (())
64 {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
88 location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99 0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
105 it is created. */
106
107 typedef struct cp_lexer GTY (())
108 {
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
113 buffer. */
114 size_t buffer_length;
115
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
119
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
123
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
127 tokens. */
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
132
133 /* True if we should output debugging information. */
134 bool debugging_p;
135
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
138 bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
145 a lexer. */
146
147 typedef struct cp_token_cache GTY(())
148 {
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
151
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes. */
157
158 static cp_lexer *cp_lexer_new_main
159 (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163 (cp_lexer *);
164 static int cp_lexer_saving_tokens
165 (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167 (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173 (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183 (cp_lexer *);
184 static void cp_lexer_purge_token
185 (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189 (cp_lexer *);
190 static void cp_lexer_commit_tokens
191 (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193 (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198 (cp_lexer *);
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217 (cp_token *);
218
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables. */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255 preprocessor. */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260 cp_token first_token;
261 cp_lexer *lexer;
262 cp_token *pos;
263 size_t alloc;
264 size_t space;
265 cp_token *buffer;
266
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
271
272 c_common_no_more_pch ();
273
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
283
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
287
288 /* Put the first token in the buffer. */
289 space = alloc;
290 pos = buffer;
291 *pos = first_token;
292
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
295 {
296 pos++;
297 if (!--space)
298 {
299 space = alloc;
300 alloc *= 2;
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
303 }
304 cp_lexer_get_preprocessor_token (lexer, pos);
305 }
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
335
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
343
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
346 }
347
348 /* Frees all resources associated with LEXER. */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output. */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366 return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376 return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382 return pos;
383 }
384
385 /* nonzero if we are presently saving tokens. */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401 static int is_extern_c = 0;
402
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
409
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
416
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
419 {
420 if (C_IS_RESERVED_WORD (token->u.value))
421 {
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
431 }
432 else
433 {
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437 {
438 /* Warn about the C++0x keyword (but still treat it as
439 an identifier). */
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
443
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
447 }
448
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
451 }
452 }
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
455 {
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
458 {
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
468 }
469 }
470 else if (token->type == CPP_PRAGMA)
471 {
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
475 }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN. */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482 if (token->type != CPP_EOF)
483 {
484 input_location = token->location;
485 }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489 consume it. */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494 if (cp_lexer_debugging_p (lexer))
495 {
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
499 }
500 return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE. */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508 return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE. */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516 return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD. */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD. */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier. */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540 cp_token *token;
541
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
544 {
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
547 case RID_AUTO:
548 /* Storage classes. */
549 case RID_REGISTER:
550 case RID_STATIC:
551 case RID_EXTERN:
552 case RID_MUTABLE:
553 case RID_THREAD:
554 /* Elaborated type specifiers. */
555 case RID_ENUM:
556 case RID_CLASS:
557 case RID_STRUCT:
558 case RID_UNION:
559 case RID_TYPENAME:
560 /* Simple type specifiers. */
561 case RID_CHAR:
562 case RID_CHAR16:
563 case RID_CHAR32:
564 case RID_WCHAR:
565 case RID_BOOL:
566 case RID_SHORT:
567 case RID_INT:
568 case RID_LONG:
569 case RID_SIGNED:
570 case RID_UNSIGNED:
571 case RID_FLOAT:
572 case RID_DOUBLE:
573 case RID_VOID:
574 /* GNU extensions. */
575 case RID_ATTRIBUTE:
576 case RID_TYPEOF:
577 /* C++0x extensions. */
578 case RID_DECLTYPE:
579 return true;
580
581 default:
582 return false;
583 }
584 }
585
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595 cp_token *token;
596
597 /* N is 1-based, not zero-based. */
598 gcc_assert (n > 0);
599
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604 --n;
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
607 while (n != 0)
608 {
609 ++token;
610 if (token == lexer->last_token)
611 {
612 token = &eof_token;
613 break;
614 }
615
616 if (token->type != CPP_PURGED)
617 --n;
618 }
619
620 if (cp_lexer_debugging_p (lexer))
621 {
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
624 }
625
626 return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635 cp_token *token = lexer->next_token;
636
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640 do
641 {
642 lexer->next_token++;
643 if (lexer->next_token == lexer->last_token)
644 {
645 lexer->next_token = &eof_token;
646 break;
647 }
648
649 }
650 while (lexer->next_token->type == CPP_PURGED);
651
652 cp_lexer_set_source_position_from_token (token);
653
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
656 {
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
660 }
661
662 return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
667 token. */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672 cp_token *tok = lexer->next_token;
673
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
679
680 do
681 {
682 tok++;
683 if (tok == lexer->last_token)
684 {
685 tok = &eof_token;
686 break;
687 }
688 }
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700 cp_token *peek = lexer->next_token;
701
702 if (peek == &eof_token)
703 peek = lexer->last_token;
704
705 gcc_assert (tok < peek);
706
707 for ( tok += 1; tok != peek; tok += 1)
708 {
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
713 }
714 }
715
716 /* Begin saving tokens. All tokens consumed after this point will be
717 preserved. */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved. */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739 VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM. */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768 TTYPE_TABLE
769 #undef OP
770 #undef TK
771 /* C++ parser token types - see "Manifest constants", above. */
772 "KEYWORD",
773 "TEMPLATE_ID",
774 "NESTED_NAME_SPECIFIER",
775 "PURGED"
776 };
777
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
782
783 /* For some tokens, print the associated data. */
784 switch (token->type)
785 {
786 case CPP_KEYWORD:
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790 break;
791 /* else fall through */
792 case CPP_NAME:
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
794 break;
795
796 case CPP_STRING:
797 case CPP_STRING16:
798 case CPP_STRING32:
799 case CPP_WSTRING:
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801 break;
802
803 default:
804 break;
805 }
806 }
807
808 /* Start emitting debugging information. */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813 lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information. */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821 lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens. */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
833 cache->last = last;
834 return cache;
835 }
836
837 \f
838 /* Decl-specifiers. */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators. */
849
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872 allocated. */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool. */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880 return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889 cp_declarator *declarator;
890
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
896
897 return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
904 is, if any. */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
909 {
910 cp_declarator *declarator;
911
912 /* It is valid to write:
913
914 class C { void f(); };
915 typedef C D;
916 void D::f();
917
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
921 well. */
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
933
934 return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944 cp_declarator *declarator;
945
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
950 if (target)
951 {
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
954 }
955 else
956 declarator->parameter_pack_p = false;
957
958 return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references. */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965 bool rvalue_ref)
966 {
967 cp_declarator *declarator;
968
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
973 if (target)
974 {
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
977 }
978 else
979 declarator->parameter_pack_p = false;
980
981 return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
990 {
991 cp_declarator *declarator;
992
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
997
998 if (pointee)
999 {
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1002 }
1003 else
1004 declarator->parameter_pack_p = false;
1005
1006 return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016 tree parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification,
1019 tree late_return_type)
1020 {
1021 cp_declarator *declarator;
1022
1023 declarator = make_declarator (cdk_function);
1024 declarator->declarator = target;
1025 declarator->u.function.parameters = parms;
1026 declarator->u.function.qualifiers = cv_qualifiers;
1027 declarator->u.function.exception_specification = exception_specification;
1028 declarator->u.function.late_return_type = late_return_type;
1029 if (target)
1030 {
1031 declarator->parameter_pack_p = target->parameter_pack_p;
1032 target->parameter_pack_p = false;
1033 }
1034 else
1035 declarator->parameter_pack_p = false;
1036
1037 return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046 cp_declarator *declarator;
1047
1048 declarator = make_declarator (cdk_array);
1049 declarator->declarator = element;
1050 declarator->u.array.bounds = bounds;
1051 if (element)
1052 {
1053 declarator->parameter_pack_p = element->parameter_pack_p;
1054 element->parameter_pack_p = false;
1055 }
1056 else
1057 declarator->parameter_pack_p = false;
1058
1059 return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1064 static bool
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1071 bool found = false;
1072 while (declarator && !found)
1073 {
1074 switch ((int)declarator->kind)
1075 {
1076 case cdk_id:
1077 case cdk_array:
1078 found = true;
1079 break;
1080
1081 case cdk_error:
1082 return true;
1083
1084 default:
1085 declarator = declarator->declarator;
1086 break;
1087 }
1088 }
1089
1090 return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100 cp_declarator *declarator,
1101 tree default_argument)
1102 {
1103 cp_parameter_declarator *parameter;
1104
1105 parameter = ((cp_parameter_declarator *)
1106 alloc_declarator (sizeof (cp_parameter_declarator)));
1107 parameter->next = NULL;
1108 if (decl_specifiers)
1109 parameter->decl_specifiers = *decl_specifiers;
1110 else
1111 clear_decl_specs (&parameter->decl_specifiers);
1112 parameter->declarator = declarator;
1113 parameter->default_argument = default_argument;
1114 parameter->ellipsis_p = false;
1115
1116 return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124 while (declarator)
1125 {
1126 if (declarator->kind == cdk_function
1127 && declarator->declarator->kind == cdk_id)
1128 return true;
1129 if (declarator->kind == cdk_id
1130 || declarator->kind == cdk_error)
1131 return false;
1132 declarator = declarator->declarator;
1133 }
1134 return false;
1135 }
1136
1137 /* The parser. */
1138
1139 /* Overview
1140 --------
1141
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1149
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1152 code processed.
1153
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1162
1163 Methodology
1164 -----------
1165
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1182
1183 Future Improvements
1184 -------------------
1185
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1190 case. */
1191
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1194
1195 typedef enum cp_parser_flags
1196 {
1197 /* No flags. */
1198 CP_PARSER_FLAGS_NONE = 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse. */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1221
1222 enum cp_parser_prec
1223 {
1224 PREC_NOT_OPERATOR,
1225 PREC_LOGICAL_OR_EXPRESSION,
1226 PREC_LOGICAL_AND_EXPRESSION,
1227 PREC_INCLUSIVE_OR_EXPRESSION,
1228 PREC_EXCLUSIVE_OR_EXPRESSION,
1229 PREC_AND_EXPRESSION,
1230 PREC_EQUALITY_EXPRESSION,
1231 PREC_RELATIONAL_EXPRESSION,
1232 PREC_SHIFT_EXPRESSION,
1233 PREC_ADDITIVE_EXPRESSION,
1234 PREC_MULTIPLICATIVE_EXPRESSION,
1235 PREC_PM_EXPRESSION,
1236 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244 /* The token type. */
1245 enum cpp_ttype token_type;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse. */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR,
1260 /* We are committed to this tentative parse, whether or not an error
1261 has occurred. */
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267 /* Left hand side of the binary operation we are currently
1268 parsing. */
1269 tree lhs;
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type;
1275 /* Precedence of the binary operation we are parsing. */
1276 int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1281 increasing. */
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288 /* If this is a tentative parsing context, the status of the
1289 tentative parse. */
1290 enum cp_parser_status_kind status;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1295 tree object_type;
1296
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes. */
1302
1303 /* Constructors and destructors. */
1304
1305 static cp_parser_context *cp_parser_context_new
1306 (cp_parser_context *);
1307
1308 /* Class variables. */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1314 cp_parser_new. */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1351 for speed. */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors. */
1355
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362 cp_parser_context *context;
1363
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list != NULL)
1366 {
1367 /* Pull the first entry from the free list. */
1368 context = cp_parser_context_free_list;
1369 cp_parser_context_free_list = context->next;
1370 memset (context, 0, sizeof (*context));
1371 }
1372 else
1373 context = GGC_CNEW (cp_parser_context);
1374
1375 /* No errors have occurred yet in this context. */
1376 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1379 if (next)
1380 {
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context->object_type = next->object_type;
1384 /* Thread the stack. */
1385 context->next = next;
1386 }
1387
1388 return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser. */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395 /* The lexer from which we are obtaining tokens. */
1396 cp_lexer *lexer;
1397
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1411 tree scope;
1412
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1418 tree object_scope;
1419 tree qualifying_scope;
1420
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1423
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context *context;
1431
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p;
1435
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p;
1442
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p;
1447
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p;
1451
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p;
1457
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p;
1461
1462 /* TRUE if local variable names and `this' are forbidden in the
1463 current context. */
1464 bool local_variables_forbidden_p;
1465
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1468 declaration'. */
1469 bool in_unbraced_linkage_specification_p;
1470
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p;
1474
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p;
1477
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement;
1489
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p;
1495
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1498 alternatives. */
1499 bool in_type_id_in_expr_p;
1500
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c;
1504
1505 /* TRUE if strings in expressions should be translated to the execution
1506 character set. */
1507 bool translate_strings_p;
1508
1509 /* TRUE if we are presently parsing the body of a function, but not
1510 a local class. */
1511 bool in_function_body;
1512
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message;
1517
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1523 order.
1524
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1528 processing.
1529
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues;
1533
1534 /* The number of classes whose definitions are currently in
1535 progress. */
1536 unsigned num_classes_being_defined;
1537
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes. */
1544
1545 /* Constructors and destructors. */
1546
1547 static cp_parser *cp_parser_new
1548 (void);
1549
1550 /* Routines to parse various constructs.
1551
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1561
1562 /* Lexical conventions [gram.lex] */
1563
1564 static tree cp_parser_identifier
1565 (cp_parser *);
1566 static tree cp_parser_string_literal
1567 (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic] */
1570
1571 static bool cp_parser_translation_unit
1572 (cp_parser *);
1573
1574 /* Expressions [gram.expr] */
1575
1576 static tree cp_parser_primary_expression
1577 (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579 (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser *, bool, bool, bool, cp_id_kind *);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599 (cp_parser *, bool, bool, cp_id_kind *);
1600 static enum tree_code cp_parser_unary_operator
1601 (cp_token *);
1602 static tree cp_parser_new_expression
1603 (cp_parser *);
1604 static tree cp_parser_new_placement
1605 (cp_parser *);
1606 static tree cp_parser_new_type_id
1607 (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609 (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611 (cp_parser *);
1612 static tree cp_parser_new_initializer
1613 (cp_parser *);
1614 static tree cp_parser_delete_expression
1615 (cp_parser *);
1616 static tree cp_parser_cast_expression
1617 (cp_parser *, bool, bool, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625 (cp_parser *);
1626 static tree cp_parser_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static tree cp_parser_constant_expression
1629 (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631 (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt] */
1634
1635 static void cp_parser_statement
1636 (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638 (cp_parser *);
1639 static tree cp_parser_expression_statement
1640 (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642 (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646 (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648 (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650 (cp_parser *);
1651 static void cp_parser_for_init_statement
1652 (cp_parser *);
1653 static tree cp_parser_jump_statement
1654 (cp_parser *);
1655 static void cp_parser_declaration_statement
1656 (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661 (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666 (cp_parser *);
1667 static void cp_parser_declaration
1668 (cp_parser *);
1669 static void cp_parser_block_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676 (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681 int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685 (cp_parser *);
1686 static tree cp_parser_nonclass_name
1687 (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691 (cp_parser *);
1692 static void cp_parser_enumerator_list
1693 (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697 (cp_parser *);
1698 static void cp_parser_namespace_definition
1699 (cp_parser *);
1700 static void cp_parser_namespace_body
1701 (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703 (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705 (cp_parser *);
1706 static bool cp_parser_using_declaration
1707 (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709 (cp_parser *);
1710 static void cp_parser_asm_definition
1711 (cp_parser *);
1712 static void cp_parser_linkage_specification
1713 (cp_parser *);
1714 static void cp_parser_static_assert
1715 (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717 (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730 (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732 (cp_parser *);
1733 static tree cp_parser_declarator_id
1734 (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736 (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740 (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744 (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser *, bool);
1747 static void cp_parser_function_body
1748 (cp_parser *);
1749 static tree cp_parser_initializer
1750 (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756 (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759 (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766 (cp_parser *);
1767 static tree cp_parser_class_head
1768 (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770 (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772 (cp_parser *);
1773 static void cp_parser_member_declaration
1774 (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776 (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778 (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783 (cp_parser *);
1784 static tree cp_parser_base_specifier
1785 (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790 (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792 (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794 (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796 (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798 (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800 (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802 (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807 (cp_parser *);
1808 static tree cp_parser_operator
1809 (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814 (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816 (cp_parser *);
1817 static tree cp_parser_template_parameter
1818 (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826 (cp_parser *);
1827 static tree cp_parser_template_argument
1828 (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830 (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832 (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837 (cp_parser *);
1838 static bool cp_parser_function_try_block
1839 (cp_parser *);
1840 static void cp_parser_handler_seq
1841 (cp_parser *);
1842 static void cp_parser_handler
1843 (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845 (cp_parser *);
1846 static tree cp_parser_throw_expression
1847 (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849 (cp_parser *);
1850 static tree cp_parser_type_id_list
1851 (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856 (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858 (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860 (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862 (cp_parser *);
1863 static tree cp_parser_attribute_list
1864 (cp_parser *);
1865 static bool cp_parser_extension_opt
1866 (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868 (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872 (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877 (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879 (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881 (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883 (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885 (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887 (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889 (cp_parser *);
1890 static tree cp_parser_objc_expression
1891 (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893 (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895 (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897 (cp_parser *);
1898 static void cp_parser_objc_declaration
1899 (cp_parser *);
1900 static tree cp_parser_objc_statement
1901 (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910 (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916 (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936 (cp_parser *);
1937 static void cp_parser_save_default_args
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946 (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948 (cp_parser *);
1949 static void cp_parser_set_storage_class
1950 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956 (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958 (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960 (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962 (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964 (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968 (cp_token *);
1969 static void cp_parser_check_class_key
1970 (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974 (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976 (cp_parser *);
1977 static bool cp_parser_cache_group
1978 (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980 (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982 (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984 (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986 (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988 (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990 (cp_parser *);
1991 static void cp_parser_error
1992 (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996 (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998 (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008 (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012 (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014 (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016 (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018 (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020 (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024 (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026 (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028 (cp_token *);
2029 static bool cp_parser_is_keyword
2030 (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively. */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041 return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal. */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049 return (token->type == CPP_STRING ||
2050 token->type == CPP_STRING16 ||
2051 token->type == CPP_STRING32 ||
2052 token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060 return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2067 OTHER-TOKEN". */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072 if (!cp_parser_simulate_error (parser))
2073 {
2074 cp_token *token = cp_lexer_peek_token (parser->lexer);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token);
2078
2079 if (token->type == CPP_PRAGMA)
2080 {
2081 error ("%H%<#pragma%> is not allowed here", &token->location);
2082 cp_parser_skip_to_pragma_eol (parser, token);
2083 return;
2084 }
2085
2086 c_parse_error (message,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2089 identifiers. */
2090 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091 token->u.value);
2092 }
2093 }
2094
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102 tree name,
2103 tree decl,
2104 const char* desired,
2105 location_t location)
2106 {
2107 /* If name lookup completely failed, tell the user that NAME was not
2108 declared. */
2109 if (decl == error_mark_node)
2110 {
2111 if (parser->scope && parser->scope != global_namespace)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location, parser->scope, name);
2114 else if (parser->scope == global_namespace)
2115 error ("%H%<::%E%> has not been declared", &location, name);
2116 else if (parser->object_scope
2117 && !CLASS_TYPE_P (parser->object_scope))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location, name, parser->object_scope);
2120 else if (parser->object_scope)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location, parser->object_scope, name);
2123 else
2124 error ("%H%qE has not been declared", &location, name);
2125 }
2126 else if (parser->scope && parser->scope != global_namespace)
2127 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128 else if (parser->scope == global_namespace)
2129 error ("%H%<::%E%> %s", &location, name, desired);
2130 else
2131 error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142 {
2143 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144 return true;
2145 }
2146 return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers. */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153 location_t location)
2154 {
2155 cp_decl_spec ds;
2156
2157 for (ds = ds_first; ds != ds_last; ++ds)
2158 {
2159 unsigned count = decl_specs->specs[(int)ds];
2160 if (count < 2)
2161 continue;
2162 /* The "long" specifier is a special case because of "long long". */
2163 if (ds == ds_long)
2164 {
2165 if (count > 2)
2166 error ("%H%<long long long%> is too long for GCC", &location);
2167 else if (pedantic && !in_system_header && warn_long_long
2168 && cxx_dialect == cxx98)
2169 pedwarn (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2171 }
2172 else if (count > 1)
2173 {
2174 static const char *const decl_spec_names[] = {
2175 "signed",
2176 "unsigned",
2177 "short",
2178 "long",
2179 "const",
2180 "volatile",
2181 "restrict",
2182 "inline",
2183 "virtual",
2184 "explicit",
2185 "friend",
2186 "typedef",
2187 "__complex",
2188 "__thread"
2189 };
2190 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191 }
2192 }
2193 }
2194
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2197 issued. */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2204 {
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2208 return false;
2209 }
2210 return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2222 {
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2226 while (declarator
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2231 if (declarator
2232 && declarator->kind == cdk_function)
2233 {
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2237 type);
2238 }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2250 {
2251 cp_token_position start = 0;
2252
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254 {
2255 if (TYPE_P (type))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2259 else
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2270 if (start)
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2272 }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2282 const char *thing)
2283 {
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2286 {
2287 if (!parser->allow_non_integral_constant_expression_p)
2288 {
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2293 NULL);
2294 error (message);
2295 free (message);
2296 return true;
2297 }
2298 }
2299 return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2312 {
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2323 &location, decl);
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2329 &location);
2330 else if (!parser->scope)
2331 {
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2336
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2339
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2343 {
2344 tree b;
2345
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347 b;
2348 b = TREE_CHAIN (b))
2349 {
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2353 {
2354 tree field;
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2360 field;
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2364 {
2365 inform (location,
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2368 break;
2369 }
2370 if (field)
2371 break;
2372 }
2373 }
2374 }
2375 }
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2379 {
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2386 else
2387 gcc_unreachable ();
2388 }
2389 cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2395
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405 tree id;
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2422 {
2423 cp_parser_abort_tentative_parse (parser);
2424 return false;
2425 }
2426 if (!cp_parser_parse_definitely (parser))
2427 return false;
2428
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2435 return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445 bool recovering,
2446 bool or_comma,
2447 bool consume_paren)
2448 {
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2451
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454 return 0;
2455
2456 while (true)
2457 {
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460 switch (token->type)
2461 {
2462 case CPP_EOF:
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2465 return 0;
2466
2467 case CPP_SEMICOLON:
2468 /* This matches the processing in skip_to_end_of_statement. */
2469 if (!brace_depth)
2470 return 0;
2471 break;
2472
2473 case CPP_OPEN_BRACE:
2474 ++brace_depth;
2475 break;
2476 case CPP_CLOSE_BRACE:
2477 if (!brace_depth--)
2478 return 0;
2479 break;
2480
2481 case CPP_COMMA:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2483 return -1;
2484 break;
2485
2486 case CPP_OPEN_PAREN:
2487 if (!brace_depth)
2488 ++paren_depth;
2489 break;
2490
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2493 {
2494 if (consume_paren)
2495 cp_lexer_consume_token (parser->lexer);
2496 return 1;
2497 }
2498 break;
2499
2500 default:
2501 break;
2502 }
2503
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2506 }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516 unsigned nesting_depth = 0;
2517
2518 while (true)
2519 {
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522 switch (token->type)
2523 {
2524 case CPP_EOF:
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2527 return;
2528
2529 case CPP_SEMICOLON:
2530 /* If the next token is a `;', we have reached the end of the
2531 statement. */
2532 if (!nesting_depth)
2533 return;
2534 break;
2535
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2539
2540 { 3 + }
2541
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2545 return;
2546
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2549 That way given:
2550
2551 void f g () { ... }
2552 typedef int I;
2553
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2556 declaration. */
2557 if (--nesting_depth == 0)
2558 {
2559 cp_lexer_consume_token (parser->lexer);
2560 return;
2561 }
2562
2563 case CPP_OPEN_BRACE:
2564 ++nesting_depth;
2565 break;
2566
2567 default:
2568 break;
2569 }
2570
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2573 }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585 {
2586 /* If there is additional (erroneous) input, skip to the end of
2587 the statement. */
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2592 }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601 int nesting_depth = 0;
2602
2603 while (nesting_depth >= 0)
2604 {
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607 switch (token->type)
2608 {
2609 case CPP_EOF:
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2612 return;
2613
2614 case CPP_SEMICOLON:
2615 /* Stop if this is an unnested ';'. */
2616 if (!nesting_depth)
2617 nesting_depth = -1;
2618 break;
2619
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2622 nesting level. */
2623 nesting_depth--;
2624 if (!nesting_depth)
2625 nesting_depth = -1;
2626 break;
2627
2628 case CPP_OPEN_BRACE:
2629 /* Nest. */
2630 nesting_depth++;
2631 break;
2632
2633 default:
2634 break;
2635 }
2636
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2639 }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2644 false otherwise. */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649 unsigned nesting_depth = 0;
2650
2651 while (true)
2652 {
2653 cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655 switch (token->type)
2656 {
2657 case CPP_EOF:
2658 case CPP_PRAGMA_EOL:
2659 /* If we've run out of tokens, stop. */
2660 return false;
2661
2662 case CPP_CLOSE_BRACE:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth-- == 0)
2666 return true;
2667 break;
2668
2669 case CPP_OPEN_BRACE:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2672 ++nesting_depth;
2673 break;
2674
2675 default:
2676 break;
2677 }
2678
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser->lexer);
2681 }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2686 sequence. */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691 cp_token *token;
2692
2693 parser->lexer->in_pragma = false;
2694
2695 do
2696 token = cp_lexer_consume_token (parser->lexer);
2697 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709 parser->lexer->in_pragma = false;
2710 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720 tree id, location_t id_location)
2721 {
2722 tree result;
2723 if (TREE_CODE (id) == IDENTIFIER_NODE)
2724 {
2725 result = make_typename_type (scope, id, typename_type,
2726 /*complain=*/tf_none);
2727 if (result == error_mark_node)
2728 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729 return result;
2730 }
2731 return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741 cp_cv_quals cv_qualifiers,
2742 cp_declarator *target)
2743 {
2744 if (code == ERROR_MARK)
2745 return cp_error_declarator;
2746
2747 if (code == INDIRECT_REF)
2748 if (class_type == NULL_TREE)
2749 return make_pointer_declarator (cv_qualifiers, target);
2750 else
2751 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, false);
2754 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, true);
2756 gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser. */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764 cp_parser *parser;
2765 cp_lexer *lexer;
2766 unsigned i;
2767
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer = cp_lexer_new_main ();
2771
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775 binops_by_token[binops[i].token_type] = binops[i];
2776
2777 parser = GGC_CNEW (cp_parser);
2778 parser->lexer = lexer;
2779 parser->context = cp_parser_context_new (NULL);
2780
2781 /* For now, we always accept GNU extensions. */
2782 parser->allow_gnu_extensions_p = 1;
2783
2784 /* The `>' token is a greater-than operator, not the end of a
2785 template-id. */
2786 parser->greater_than_is_operator_p = true;
2787
2788 parser->default_arg_ok_p = true;
2789
2790 /* We are not parsing a constant-expression. */
2791 parser->integral_constant_expression_p = false;
2792 parser->allow_non_integral_constant_expression_p = false;
2793 parser->non_integral_constant_expression_p = false;
2794
2795 /* Local variable names are not forbidden. */
2796 parser->local_variables_forbidden_p = false;
2797
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser->in_unbraced_linkage_specification_p = false;
2800
2801 /* We are not processing a declarator. */
2802 parser->in_declarator_p = false;
2803
2804 /* We are not processing a template-argument-list. */
2805 parser->in_template_argument_list_p = false;
2806
2807 /* We are not in an iteration statement. */
2808 parser->in_statement = 0;
2809
2810 /* We are not in a switch statement. */
2811 parser->in_switch_statement_p = false;
2812
2813 /* We are not parsing a type-id inside an expression. */
2814 parser->in_type_id_in_expr_p = false;
2815
2816 /* Declarations aren't implicitly extern "C". */
2817 parser->implicit_extern_c = false;
2818
2819 /* String literals should be translated to the execution character set. */
2820 parser->translate_strings_p = true;
2821
2822 /* We are not parsing a function body. */
2823 parser->in_function_body = false;
2824
2825 /* The unparsed function queue is empty. */
2826 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828 /* There are no classes being defined. */
2829 parser->num_classes_being_defined = 0;
2830
2831 /* No template parameters apply. */
2832 parser->num_template_parameter_lists = 0;
2833
2834 return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845 lexer->next = parser->lexer;
2846 parser->lexer = lexer;
2847
2848 /* Move the current source position to that of the first token in the
2849 new lexer. */
2850 cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858 cp_lexer *lexer = parser->lexer;
2859 parser->lexer = lexer->next;
2860 cp_lexer_destroy (lexer);
2861
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex] */
2868
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2870 identifier. */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875 cp_token *token;
2876
2877 /* Look for the identifier. */
2878 token = cp_parser_require (parser, CPP_NAME, "identifier");
2879 /* Return the value. */
2880 return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2887 invalid here.
2888
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2893
2894 This code is largely lifted from lex_string() in c-lex.c.
2895
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900 tree value;
2901 size_t count;
2902 struct obstack str_ob;
2903 cpp_string str, istr, *strs;
2904 cp_token *tok;
2905 enum cpp_ttype type;
2906
2907 tok = cp_lexer_peek_token (parser->lexer);
2908 if (!cp_parser_is_string_literal (tok))
2909 {
2910 cp_parser_error (parser, "expected string-literal");
2911 return error_mark_node;
2912 }
2913
2914 type = tok->type;
2915
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920 {
2921 cp_lexer_consume_token (parser->lexer);
2922
2923 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924 str.len = TREE_STRING_LENGTH (tok->u.value);
2925 count = 1;
2926
2927 strs = &str;
2928 }
2929 else
2930 {
2931 gcc_obstack_init (&str_ob);
2932 count = 0;
2933
2934 do
2935 {
2936 cp_lexer_consume_token (parser->lexer);
2937 count++;
2938 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939 str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941 if (type != tok->type)
2942 {
2943 if (type == CPP_STRING)
2944 type = tok->type;
2945 else if (tok->type != CPP_STRING)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok->location);
2948 }
2949
2950 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952 tok = cp_lexer_peek_token (parser->lexer);
2953 }
2954 while (cp_parser_is_string_literal (tok));
2955
2956 strs = (cpp_string *) obstack_finish (&str_ob);
2957 }
2958
2959 if (type != CPP_STRING && !wide_ok)
2960 {
2961 cp_parser_error (parser, "a wide string is invalid in this context");
2962 type = CPP_STRING;
2963 }
2964
2965 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966 (parse_in, strs, count, &istr, type))
2967 {
2968 value = build_string (istr.len, (const char *)istr.text);
2969 free (CONST_CAST (unsigned char *, istr.text));
2970
2971 switch (type)
2972 {
2973 default:
2974 case CPP_STRING:
2975 TREE_TYPE (value) = char_array_type_node;
2976 break;
2977 case CPP_STRING16:
2978 TREE_TYPE (value) = char16_array_type_node;
2979 break;
2980 case CPP_STRING32:
2981 TREE_TYPE (value) = char32_array_type_node;
2982 break;
2983 case CPP_WSTRING:
2984 TREE_TYPE (value) = wchar_array_type_node;
2985 break;
2986 }
2987
2988 value = fix_string_type (value);
2989 }
2990 else
2991 /* cpp_interpret_string has issued an error. */
2992 value = error_mark_node;
2993
2994 if (count > 1)
2995 obstack_free (&str_ob, 0);
2996
2997 return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic] */
3002
3003 /* Parse a translation-unit.
3004
3005 translation-unit:
3006 declaration-seq [opt]
3007
3008 Returns TRUE if all went well. */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013 /* The address of the first non-permanent object on the declarator
3014 obstack. */
3015 static void *declarator_obstack_base;
3016
3017 bool success;
3018
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator)
3021 {
3022 gcc_obstack_init (&declarator_obstack);
3023 /* Create the error declarator. */
3024 cp_error_declarator = make_declarator (cdk_error);
3025 /* Create the empty parameter list. */
3026 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029 }
3030
3031 cp_parser_declaration_seq_opt (parser);
3032
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035 {
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser->lexer);
3038 parser->lexer = NULL;
3039
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser->implicit_extern_c)
3043 {
3044 pop_lang_context ();
3045 parser->implicit_extern_c = false;
3046 }
3047
3048 /* Finish up. */
3049 finish_translation_unit ();
3050
3051 success = true;
3052 }
3053 else
3054 {
3055 cp_parser_error (parser, "expected declaration");
3056 success = false;
3057 }
3058
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack)
3061 == declarator_obstack_base);
3062
3063 /* All went well. */
3064 return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071 primary-expression:
3072 literal
3073 this
3074 ( expression )
3075 id-expression
3076
3077 GNU Extensions:
3078
3079 primary-expression:
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3083
3084 C++ Extensions:
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3102
3103 Objective-C++ Extension:
3104
3105 primary-expression:
3106 objc-expression
3107
3108 literal:
3109 __null
3110
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3115
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121 bool address_p,
3122 bool cast_p,
3123 bool template_arg_p,
3124 cp_id_kind *idk)
3125 {
3126 cp_token *token = NULL;
3127
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk = CP_ID_KIND_NONE;
3130
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133 switch (token->type)
3134 {
3135 /* literal:
3136 integer-literal
3137 character-literal
3138 floating-literal
3139 string-literal
3140 boolean-literal */
3141 case CPP_CHAR:
3142 case CPP_CHAR16:
3143 case CPP_CHAR32:
3144 case CPP_WCHAR:
3145 case CPP_NUMBER:
3146 token = cp_lexer_consume_token (parser->lexer);
3147 /* Floating-point literals are only allowed in an integral
3148 constant expression if they are cast to an integral or
3149 enumeration type. */
3150 if (TREE_CODE (token->u.value) == REAL_CST
3151 && parser->integral_constant_expression_p
3152 && pedantic)
3153 {
3154 /* CAST_P will be set even in invalid code like "int(2.7 +
3155 ...)". Therefore, we have to check that the next token
3156 is sure to end the cast. */
3157 if (cast_p)
3158 {
3159 cp_token *next_token;
3160
3161 next_token = cp_lexer_peek_token (parser->lexer);
3162 if (/* The comma at the end of an
3163 enumerator-definition. */
3164 next_token->type != CPP_COMMA
3165 /* The curly brace at the end of an enum-specifier. */
3166 && next_token->type != CPP_CLOSE_BRACE
3167 /* The end of a statement. */
3168 && next_token->type != CPP_SEMICOLON
3169 /* The end of the cast-expression. */
3170 && next_token->type != CPP_CLOSE_PAREN
3171 /* The end of an array bound. */
3172 && next_token->type != CPP_CLOSE_SQUARE
3173 /* The closing ">" in a template-argument-list. */
3174 && (next_token->type != CPP_GREATER
3175 || parser->greater_than_is_operator_p)
3176 /* C++0x only: A ">>" treated like two ">" tokens,
3177 in a template-argument-list. */
3178 && (next_token->type != CPP_RSHIFT
3179 || (cxx_dialect == cxx98)
3180 || parser->greater_than_is_operator_p))
3181 cast_p = false;
3182 }
3183
3184 /* If we are within a cast, then the constraint that the
3185 cast is to an integral or enumeration type will be
3186 checked at that point. If we are not within a cast, then
3187 this code is invalid. */
3188 if (!cast_p)
3189 cp_parser_non_integral_constant_expression
3190 (parser, "floating-point literal");
3191 }
3192 return token->u.value;
3193
3194 case CPP_STRING:
3195 case CPP_STRING16:
3196 case CPP_STRING32:
3197 case CPP_WSTRING:
3198 /* ??? Should wide strings be allowed when parser->translate_strings_p
3199 is false (i.e. in attributes)? If not, we can kill the third
3200 argument to cp_parser_string_literal. */
3201 return cp_parser_string_literal (parser,
3202 parser->translate_strings_p,
3203 true);
3204
3205 case CPP_OPEN_PAREN:
3206 {
3207 tree expr;
3208 bool saved_greater_than_is_operator_p;
3209
3210 /* Consume the `('. */
3211 cp_lexer_consume_token (parser->lexer);
3212 /* Within a parenthesized expression, a `>' token is always
3213 the greater-than operator. */
3214 saved_greater_than_is_operator_p
3215 = parser->greater_than_is_operator_p;
3216 parser->greater_than_is_operator_p = true;
3217 /* If we see `( { ' then we are looking at the beginning of
3218 a GNU statement-expression. */
3219 if (cp_parser_allow_gnu_extensions_p (parser)
3220 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221 {
3222 /* Statement-expressions are not allowed by the standard. */
3223 pedwarn (token->location, OPT_pedantic,
3224 "ISO C++ forbids braced-groups within expressions");
3225
3226 /* And they're not allowed outside of a function-body; you
3227 cannot, for example, write:
3228
3229 int i = ({ int j = 3; j + 1; });
3230
3231 at class or namespace scope. */
3232 if (!parser->in_function_body
3233 || parser->in_template_argument_list_p)
3234 {
3235 error ("%Hstatement-expressions are not allowed outside "
3236 "functions nor in template-argument lists",
3237 &token->location);
3238 cp_parser_skip_to_end_of_block_or_statement (parser);
3239 expr = error_mark_node;
3240 }
3241 else
3242 {
3243 /* Start the statement-expression. */
3244 expr = begin_stmt_expr ();
3245 /* Parse the compound-statement. */
3246 cp_parser_compound_statement (parser, expr, false);
3247 /* Finish up. */
3248 expr = finish_stmt_expr (expr, false);
3249 }
3250 }
3251 else
3252 {
3253 /* Parse the parenthesized expression. */
3254 expr = cp_parser_expression (parser, cast_p, idk);
3255 /* Let the front end know that this expression was
3256 enclosed in parentheses. This matters in case, for
3257 example, the expression is of the form `A::B', since
3258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259 not. */
3260 finish_parenthesized_expr (expr);
3261 }
3262 /* The `>' token might be the end of a template-id or
3263 template-parameter-list now. */
3264 parser->greater_than_is_operator_p
3265 = saved_greater_than_is_operator_p;
3266 /* Consume the `)'. */
3267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268 cp_parser_skip_to_end_of_statement (parser);
3269
3270 return expr;
3271 }
3272
3273 case CPP_KEYWORD:
3274 switch (token->keyword)
3275 {
3276 /* These two are the boolean literals. */
3277 case RID_TRUE:
3278 cp_lexer_consume_token (parser->lexer);
3279 return boolean_true_node;
3280 case RID_FALSE:
3281 cp_lexer_consume_token (parser->lexer);
3282 return boolean_false_node;
3283
3284 /* The `__null' literal. */
3285 case RID_NULL:
3286 cp_lexer_consume_token (parser->lexer);
3287 return null_node;
3288
3289 /* Recognize the `this' keyword. */
3290 case RID_THIS:
3291 cp_lexer_consume_token (parser->lexer);
3292 if (parser->local_variables_forbidden_p)
3293 {
3294 error ("%H%<this%> may not be used in this context",
3295 &token->location);
3296 return error_mark_node;
3297 }
3298 /* Pointers cannot appear in constant-expressions. */
3299 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300 return error_mark_node;
3301 return finish_this_expr ();
3302
3303 /* The `operator' keyword can be the beginning of an
3304 id-expression. */
3305 case RID_OPERATOR:
3306 goto id_expression;
3307
3308 case RID_FUNCTION_NAME:
3309 case RID_PRETTY_FUNCTION_NAME:
3310 case RID_C99_FUNCTION_NAME:
3311 {
3312 const char *name;
3313
3314 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3315 __func__ are the names of variables -- but they are
3316 treated specially. Therefore, they are handled here,
3317 rather than relying on the generic id-expression logic
3318 below. Grammatically, these names are id-expressions.
3319
3320 Consume the token. */
3321 token = cp_lexer_consume_token (parser->lexer);
3322
3323 switch (token->keyword)
3324 {
3325 case RID_FUNCTION_NAME:
3326 name = "%<__FUNCTION__%>";
3327 break;
3328 case RID_PRETTY_FUNCTION_NAME:
3329 name = "%<__PRETTY_FUNCTION__%>";
3330 break;
3331 case RID_C99_FUNCTION_NAME:
3332 name = "%<__func__%>";
3333 break;
3334 default:
3335 gcc_unreachable ();
3336 }
3337
3338 if (cp_parser_non_integral_constant_expression (parser, name))
3339 return error_mark_node;
3340
3341 /* Look up the name. */
3342 return finish_fname (token->u.value);
3343 }
3344
3345 case RID_VA_ARG:
3346 {
3347 tree expression;
3348 tree type;
3349
3350 /* The `__builtin_va_arg' construct is used to handle
3351 `va_arg'. Consume the `__builtin_va_arg' token. */
3352 cp_lexer_consume_token (parser->lexer);
3353 /* Look for the opening `('. */
3354 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3355 /* Now, parse the assignment-expression. */
3356 expression = cp_parser_assignment_expression (parser,
3357 /*cast_p=*/false, NULL);
3358 /* Look for the `,'. */
3359 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3360 /* Parse the type-id. */
3361 type = cp_parser_type_id (parser);
3362 /* Look for the closing `)'. */
3363 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3364 /* Using `va_arg' in a constant-expression is not
3365 allowed. */
3366 if (cp_parser_non_integral_constant_expression (parser,
3367 "%<va_arg%>"))
3368 return error_mark_node;
3369 return build_x_va_arg (expression, type);
3370 }
3371
3372 case RID_OFFSETOF:
3373 return cp_parser_builtin_offsetof (parser);
3374
3375 case RID_HAS_NOTHROW_ASSIGN:
3376 case RID_HAS_NOTHROW_CONSTRUCTOR:
3377 case RID_HAS_NOTHROW_COPY:
3378 case RID_HAS_TRIVIAL_ASSIGN:
3379 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3380 case RID_HAS_TRIVIAL_COPY:
3381 case RID_HAS_TRIVIAL_DESTRUCTOR:
3382 case RID_HAS_VIRTUAL_DESTRUCTOR:
3383 case RID_IS_ABSTRACT:
3384 case RID_IS_BASE_OF:
3385 case RID_IS_CLASS:
3386 case RID_IS_CONVERTIBLE_TO:
3387 case RID_IS_EMPTY:
3388 case RID_IS_ENUM:
3389 case RID_IS_POD:
3390 case RID_IS_POLYMORPHIC:
3391 case RID_IS_UNION:
3392 return cp_parser_trait_expr (parser, token->keyword);
3393
3394 /* Objective-C++ expressions. */
3395 case RID_AT_ENCODE:
3396 case RID_AT_PROTOCOL:
3397 case RID_AT_SELECTOR:
3398 return cp_parser_objc_expression (parser);
3399
3400 default:
3401 cp_parser_error (parser, "expected primary-expression");
3402 return error_mark_node;
3403 }
3404
3405 /* An id-expression can start with either an identifier, a
3406 `::' as the beginning of a qualified-id, or the "operator"
3407 keyword. */
3408 case CPP_NAME:
3409 case CPP_SCOPE:
3410 case CPP_TEMPLATE_ID:
3411 case CPP_NESTED_NAME_SPECIFIER:
3412 {
3413 tree id_expression;
3414 tree decl;
3415 const char *error_msg;
3416 bool template_p;
3417 bool done;
3418 cp_token *id_expr_token;
3419
3420 id_expression:
3421 /* Parse the id-expression. */
3422 id_expression
3423 = cp_parser_id_expression (parser,
3424 /*template_keyword_p=*/false,
3425 /*check_dependency_p=*/true,
3426 &template_p,
3427 /*declarator_p=*/false,
3428 /*optional_p=*/false);
3429 if (id_expression == error_mark_node)
3430 return error_mark_node;
3431 id_expr_token = token;
3432 token = cp_lexer_peek_token (parser->lexer);
3433 done = (token->type != CPP_OPEN_SQUARE
3434 && token->type != CPP_OPEN_PAREN
3435 && token->type != CPP_DOT
3436 && token->type != CPP_DEREF
3437 && token->type != CPP_PLUS_PLUS
3438 && token->type != CPP_MINUS_MINUS);
3439 /* If we have a template-id, then no further lookup is
3440 required. If the template-id was for a template-class, we
3441 will sometimes have a TYPE_DECL at this point. */
3442 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3443 || TREE_CODE (id_expression) == TYPE_DECL)
3444 decl = id_expression;
3445 /* Look up the name. */
3446 else
3447 {
3448 tree ambiguous_decls;
3449
3450 decl = cp_parser_lookup_name (parser, id_expression,
3451 none_type,
3452 template_p,
3453 /*is_namespace=*/false,
3454 /*check_dependency=*/true,
3455 &ambiguous_decls,
3456 id_expr_token->location);
3457 /* If the lookup was ambiguous, an error will already have
3458 been issued. */
3459 if (ambiguous_decls)
3460 return error_mark_node;
3461
3462 /* In Objective-C++, an instance variable (ivar) may be preferred
3463 to whatever cp_parser_lookup_name() found. */
3464 decl = objc_lookup_ivar (decl, id_expression);
3465
3466 /* If name lookup gives us a SCOPE_REF, then the
3467 qualifying scope was dependent. */
3468 if (TREE_CODE (decl) == SCOPE_REF)
3469 {
3470 /* At this point, we do not know if DECL is a valid
3471 integral constant expression. We assume that it is
3472 in fact such an expression, so that code like:
3473
3474 template <int N> struct A {
3475 int a[B<N>::i];
3476 };
3477
3478 is accepted. At template-instantiation time, we
3479 will check that B<N>::i is actually a constant. */
3480 return decl;
3481 }
3482 /* Check to see if DECL is a local variable in a context
3483 where that is forbidden. */
3484 if (parser->local_variables_forbidden_p
3485 && local_variable_p (decl))
3486 {
3487 /* It might be that we only found DECL because we are
3488 trying to be generous with pre-ISO scoping rules.
3489 For example, consider:
3490
3491 int i;
3492 void g() {
3493 for (int i = 0; i < 10; ++i) {}
3494 extern void f(int j = i);
3495 }
3496
3497 Here, name look up will originally find the out
3498 of scope `i'. We need to issue a warning message,
3499 but then use the global `i'. */
3500 decl = check_for_out_of_scope_variable (decl);
3501 if (local_variable_p (decl))
3502 {
3503 error ("%Hlocal variable %qD may not appear in this context",
3504 &id_expr_token->location, decl);
3505 return error_mark_node;
3506 }
3507 }
3508 }
3509
3510 decl = (finish_id_expression
3511 (id_expression, decl, parser->scope,
3512 idk,
3513 parser->integral_constant_expression_p,
3514 parser->allow_non_integral_constant_expression_p,
3515 &parser->non_integral_constant_expression_p,
3516 template_p, done, address_p,
3517 template_arg_p,
3518 &error_msg,
3519 id_expr_token->location));
3520 if (error_msg)
3521 cp_parser_error (parser, error_msg);
3522 return decl;
3523 }
3524
3525 /* Anything else is an error. */
3526 default:
3527 /* ...unless we have an Objective-C++ message or string literal,
3528 that is. */
3529 if (c_dialect_objc ()
3530 && (token->type == CPP_OPEN_SQUARE
3531 || token->type == CPP_OBJC_STRING))
3532 return cp_parser_objc_expression (parser);
3533
3534 cp_parser_error (parser, "expected primary-expression");
3535 return error_mark_node;
3536 }
3537 }
3538
3539 /* Parse an id-expression.
3540
3541 id-expression:
3542 unqualified-id
3543 qualified-id
3544
3545 qualified-id:
3546 :: [opt] nested-name-specifier template [opt] unqualified-id
3547 :: identifier
3548 :: operator-function-id
3549 :: template-id
3550
3551 Return a representation of the unqualified portion of the
3552 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3553 a `::' or nested-name-specifier.
3554
3555 Often, if the id-expression was a qualified-id, the caller will
3556 want to make a SCOPE_REF to represent the qualified-id. This
3557 function does not do this in order to avoid wastefully creating
3558 SCOPE_REFs when they are not required.
3559
3560 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3561 `template' keyword.
3562
3563 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3564 uninstantiated templates.
3565
3566 If *TEMPLATE_P is non-NULL, it is set to true iff the
3567 `template' keyword is used to explicitly indicate that the entity
3568 named is a template.
3569
3570 If DECLARATOR_P is true, the id-expression is appearing as part of
3571 a declarator, rather than as part of an expression. */
3572
3573 static tree
3574 cp_parser_id_expression (cp_parser *parser,
3575 bool template_keyword_p,
3576 bool check_dependency_p,
3577 bool *template_p,
3578 bool declarator_p,
3579 bool optional_p)
3580 {
3581 bool global_scope_p;
3582 bool nested_name_specifier_p;
3583
3584 /* Assume the `template' keyword was not used. */
3585 if (template_p)
3586 *template_p = template_keyword_p;
3587
3588 /* Look for the optional `::' operator. */
3589 global_scope_p
3590 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3591 != NULL_TREE);
3592 /* Look for the optional nested-name-specifier. */
3593 nested_name_specifier_p
3594 = (cp_parser_nested_name_specifier_opt (parser,
3595 /*typename_keyword_p=*/false,
3596 check_dependency_p,
3597 /*type_p=*/false,
3598 declarator_p)
3599 != NULL_TREE);
3600 /* If there is a nested-name-specifier, then we are looking at
3601 the first qualified-id production. */
3602 if (nested_name_specifier_p)
3603 {
3604 tree saved_scope;
3605 tree saved_object_scope;
3606 tree saved_qualifying_scope;
3607 tree unqualified_id;
3608 bool is_template;
3609
3610 /* See if the next token is the `template' keyword. */
3611 if (!template_p)
3612 template_p = &is_template;
3613 *template_p = cp_parser_optional_template_keyword (parser);
3614 /* Name lookup we do during the processing of the
3615 unqualified-id might obliterate SCOPE. */
3616 saved_scope = parser->scope;
3617 saved_object_scope = parser->object_scope;
3618 saved_qualifying_scope = parser->qualifying_scope;
3619 /* Process the final unqualified-id. */
3620 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3621 check_dependency_p,
3622 declarator_p,
3623 /*optional_p=*/false);
3624 /* Restore the SAVED_SCOPE for our caller. */
3625 parser->scope = saved_scope;
3626 parser->object_scope = saved_object_scope;
3627 parser->qualifying_scope = saved_qualifying_scope;
3628
3629 return unqualified_id;
3630 }
3631 /* Otherwise, if we are in global scope, then we are looking at one
3632 of the other qualified-id productions. */
3633 else if (global_scope_p)
3634 {
3635 cp_token *token;
3636 tree id;
3637
3638 /* Peek at the next token. */
3639 token = cp_lexer_peek_token (parser->lexer);
3640
3641 /* If it's an identifier, and the next token is not a "<", then
3642 we can avoid the template-id case. This is an optimization
3643 for this common case. */
3644 if (token->type == CPP_NAME
3645 && !cp_parser_nth_token_starts_template_argument_list_p
3646 (parser, 2))
3647 return cp_parser_identifier (parser);
3648
3649 cp_parser_parse_tentatively (parser);
3650 /* Try a template-id. */
3651 id = cp_parser_template_id (parser,
3652 /*template_keyword_p=*/false,
3653 /*check_dependency_p=*/true,
3654 declarator_p);
3655 /* If that worked, we're done. */
3656 if (cp_parser_parse_definitely (parser))
3657 return id;
3658
3659 /* Peek at the next token. (Changes in the token buffer may
3660 have invalidated the pointer obtained above.) */
3661 token = cp_lexer_peek_token (parser->lexer);
3662
3663 switch (token->type)
3664 {
3665 case CPP_NAME:
3666 return cp_parser_identifier (parser);
3667
3668 case CPP_KEYWORD:
3669 if (token->keyword == RID_OPERATOR)
3670 return cp_parser_operator_function_id (parser);
3671 /* Fall through. */
3672
3673 default:
3674 cp_parser_error (parser, "expected id-expression");
3675 return error_mark_node;
3676 }
3677 }
3678 else
3679 return cp_parser_unqualified_id (parser, template_keyword_p,
3680 /*check_dependency_p=*/true,
3681 declarator_p,
3682 optional_p);
3683 }
3684
3685 /* Parse an unqualified-id.
3686
3687 unqualified-id:
3688 identifier
3689 operator-function-id
3690 conversion-function-id
3691 ~ class-name
3692 template-id
3693
3694 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3695 keyword, in a construct like `A::template ...'.
3696
3697 Returns a representation of unqualified-id. For the `identifier'
3698 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3699 production a BIT_NOT_EXPR is returned; the operand of the
3700 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3701 other productions, see the documentation accompanying the
3702 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3703 names are looked up in uninstantiated templates. If DECLARATOR_P
3704 is true, the unqualified-id is appearing as part of a declarator,
3705 rather than as part of an expression. */
3706
3707 static tree
3708 cp_parser_unqualified_id (cp_parser* parser,
3709 bool template_keyword_p,
3710 bool check_dependency_p,
3711 bool declarator_p,
3712 bool optional_p)
3713 {
3714 cp_token *token;
3715
3716 /* Peek at the next token. */
3717 token = cp_lexer_peek_token (parser->lexer);
3718
3719 switch (token->type)
3720 {
3721 case CPP_NAME:
3722 {
3723 tree id;
3724
3725 /* We don't know yet whether or not this will be a
3726 template-id. */
3727 cp_parser_parse_tentatively (parser);
3728 /* Try a template-id. */
3729 id = cp_parser_template_id (parser, template_keyword_p,
3730 check_dependency_p,
3731 declarator_p);
3732 /* If it worked, we're done. */
3733 if (cp_parser_parse_definitely (parser))
3734 return id;
3735 /* Otherwise, it's an ordinary identifier. */
3736 return cp_parser_identifier (parser);
3737 }
3738
3739 case CPP_TEMPLATE_ID:
3740 return cp_parser_template_id (parser, template_keyword_p,
3741 check_dependency_p,
3742 declarator_p);
3743
3744 case CPP_COMPL:
3745 {
3746 tree type_decl;
3747 tree qualifying_scope;
3748 tree object_scope;
3749 tree scope;
3750 bool done;
3751
3752 /* Consume the `~' token. */
3753 cp_lexer_consume_token (parser->lexer);
3754 /* Parse the class-name. The standard, as written, seems to
3755 say that:
3756
3757 template <typename T> struct S { ~S (); };
3758 template <typename T> S<T>::~S() {}
3759
3760 is invalid, since `~' must be followed by a class-name, but
3761 `S<T>' is dependent, and so not known to be a class.
3762 That's not right; we need to look in uninstantiated
3763 templates. A further complication arises from:
3764
3765 template <typename T> void f(T t) {
3766 t.T::~T();
3767 }
3768
3769 Here, it is not possible to look up `T' in the scope of `T'
3770 itself. We must look in both the current scope, and the
3771 scope of the containing complete expression.
3772
3773 Yet another issue is:
3774
3775 struct S {
3776 int S;
3777 ~S();
3778 };
3779
3780 S::~S() {}
3781
3782 The standard does not seem to say that the `S' in `~S'
3783 should refer to the type `S' and not the data member
3784 `S::S'. */
3785
3786 /* DR 244 says that we look up the name after the "~" in the
3787 same scope as we looked up the qualifying name. That idea
3788 isn't fully worked out; it's more complicated than that. */
3789 scope = parser->scope;
3790 object_scope = parser->object_scope;
3791 qualifying_scope = parser->qualifying_scope;
3792
3793 /* Check for invalid scopes. */
3794 if (scope == error_mark_node)
3795 {
3796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3797 cp_lexer_consume_token (parser->lexer);
3798 return error_mark_node;
3799 }
3800 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3801 {
3802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3803 error ("%Hscope %qT before %<~%> is not a class-name",
3804 &token->location, scope);
3805 cp_parser_simulate_error (parser);
3806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3807 cp_lexer_consume_token (parser->lexer);
3808 return error_mark_node;
3809 }
3810 gcc_assert (!scope || TYPE_P (scope));
3811
3812 /* If the name is of the form "X::~X" it's OK. */
3813 token = cp_lexer_peek_token (parser->lexer);
3814 if (scope
3815 && token->type == CPP_NAME
3816 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3817 == CPP_OPEN_PAREN)
3818 && constructor_name_p (token->u.value, scope))
3819 {
3820 cp_lexer_consume_token (parser->lexer);
3821 return build_nt (BIT_NOT_EXPR, scope);
3822 }
3823
3824 /* If there was an explicit qualification (S::~T), first look
3825 in the scope given by the qualification (i.e., S). */
3826 done = false;
3827 type_decl = NULL_TREE;
3828 if (scope)
3829 {
3830 cp_parser_parse_tentatively (parser);
3831 type_decl = cp_parser_class_name (parser,
3832 /*typename_keyword_p=*/false,
3833 /*template_keyword_p=*/false,
3834 none_type,
3835 /*check_dependency=*/false,
3836 /*class_head_p=*/false,
3837 declarator_p);
3838 if (cp_parser_parse_definitely (parser))
3839 done = true;
3840 }
3841 /* In "N::S::~S", look in "N" as well. */
3842 if (!done && scope && qualifying_scope)
3843 {
3844 cp_parser_parse_tentatively (parser);
3845 parser->scope = qualifying_scope;
3846 parser->object_scope = NULL_TREE;
3847 parser->qualifying_scope = NULL_TREE;
3848 type_decl
3849 = cp_parser_class_name (parser,
3850 /*typename_keyword_p=*/false,
3851 /*template_keyword_p=*/false,
3852 none_type,
3853 /*check_dependency=*/false,
3854 /*class_head_p=*/false,
3855 declarator_p);
3856 if (cp_parser_parse_definitely (parser))
3857 done = true;
3858 }
3859 /* In "p->S::~T", look in the scope given by "*p" as well. */
3860 else if (!done && object_scope)
3861 {
3862 cp_parser_parse_tentatively (parser);
3863 parser->scope = object_scope;
3864 parser->object_scope = NULL_TREE;
3865 parser->qualifying_scope = NULL_TREE;
3866 type_decl
3867 = cp_parser_class_name (parser,
3868 /*typename_keyword_p=*/false,
3869 /*template_keyword_p=*/false,
3870 none_type,
3871 /*check_dependency=*/false,
3872 /*class_head_p=*/false,
3873 declarator_p);
3874 if (cp_parser_parse_definitely (parser))
3875 done = true;
3876 }
3877 /* Look in the surrounding context. */
3878 if (!done)
3879 {
3880 parser->scope = NULL_TREE;
3881 parser->object_scope = NULL_TREE;
3882 parser->qualifying_scope = NULL_TREE;
3883 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 cp_id_kind * pidk_return)
4404 {
4405 cp_token *token;
4406 enum rid keyword;
4407 cp_id_kind idk = CP_ID_KIND_NONE;
4408 tree postfix_expression = NULL_TREE;
4409 bool is_member_access = false;
4410
4411 /* Peek at the next token. */
4412 token = cp_lexer_peek_token (parser->lexer);
4413 /* Some of the productions are determined by keywords. */
4414 keyword = token->keyword;
4415 switch (keyword)
4416 {
4417 case RID_DYNCAST:
4418 case RID_STATCAST:
4419 case RID_REINTCAST:
4420 case RID_CONSTCAST:
4421 {
4422 tree type;
4423 tree expression;
4424 const char *saved_message;
4425
4426 /* All of these can be handled in the same way from the point
4427 of view of parsing. Begin by consuming the token
4428 identifying the cast. */
4429 cp_lexer_consume_token (parser->lexer);
4430
4431 /* New types cannot be defined in the cast. */
4432 saved_message = parser->type_definition_forbidden_message;
4433 parser->type_definition_forbidden_message
4434 = "types may not be defined in casts";
4435
4436 /* Look for the opening `<'. */
4437 cp_parser_require (parser, CPP_LESS, "%<<%>");
4438 /* Parse the type to which we are casting. */
4439 type = cp_parser_type_id (parser);
4440 /* Look for the closing `>'. */
4441 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4442 /* Restore the old message. */
4443 parser->type_definition_forbidden_message = saved_message;
4444
4445 /* And the expression which is being cast. */
4446 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4447 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4448 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4449
4450 /* Only type conversions to integral or enumeration types
4451 can be used in constant-expressions. */
4452 if (!cast_valid_in_integral_constant_expression_p (type)
4453 && (cp_parser_non_integral_constant_expression
4454 (parser,
4455 "a cast to a type other than an integral or "
4456 "enumeration type")))
4457 return error_mark_node;
4458
4459 switch (keyword)
4460 {
4461 case RID_DYNCAST:
4462 postfix_expression
4463 = build_dynamic_cast (type, expression, tf_warning_or_error);
4464 break;
4465 case RID_STATCAST:
4466 postfix_expression
4467 = build_static_cast (type, expression, tf_warning_or_error);
4468 break;
4469 case RID_REINTCAST:
4470 postfix_expression
4471 = build_reinterpret_cast (type, expression,
4472 tf_warning_or_error);
4473 break;
4474 case RID_CONSTCAST:
4475 postfix_expression
4476 = build_const_cast (type, expression, tf_warning_or_error);
4477 break;
4478 default:
4479 gcc_unreachable ();
4480 }
4481 }
4482 break;
4483
4484 case RID_TYPEID:
4485 {
4486 tree type;
4487 const char *saved_message;
4488 bool saved_in_type_id_in_expr_p;
4489
4490 /* Consume the `typeid' token. */
4491 cp_lexer_consume_token (parser->lexer);
4492 /* Look for the `(' token. */
4493 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4494 /* Types cannot be defined in a `typeid' expression. */
4495 saved_message = parser->type_definition_forbidden_message;
4496 parser->type_definition_forbidden_message
4497 = "types may not be defined in a %<typeid%> expression";
4498 /* We can't be sure yet whether we're looking at a type-id or an
4499 expression. */
4500 cp_parser_parse_tentatively (parser);
4501 /* Try a type-id first. */
4502 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4503 parser->in_type_id_in_expr_p = true;
4504 type = cp_parser_type_id (parser);
4505 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4506 /* Look for the `)' token. Otherwise, we can't be sure that
4507 we're not looking at an expression: consider `typeid (int
4508 (3))', for example. */
4509 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4510 /* If all went well, simply lookup the type-id. */
4511 if (cp_parser_parse_definitely (parser))
4512 postfix_expression = get_typeid (type);
4513 /* Otherwise, fall back to the expression variant. */
4514 else
4515 {
4516 tree expression;
4517
4518 /* Look for an expression. */
4519 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4520 /* Compute its typeid. */
4521 postfix_expression = build_typeid (expression);
4522 /* Look for the `)' token. */
4523 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4524 }
4525 /* Restore the saved message. */
4526 parser->type_definition_forbidden_message = saved_message;
4527 /* `typeid' may not appear in an integral constant expression. */
4528 if (cp_parser_non_integral_constant_expression(parser,
4529 "%<typeid%> operator"))
4530 return error_mark_node;
4531 }
4532 break;
4533
4534 case RID_TYPENAME:
4535 {
4536 tree type;
4537 /* The syntax permitted here is the same permitted for an
4538 elaborated-type-specifier. */
4539 type = cp_parser_elaborated_type_specifier (parser,
4540 /*is_friend=*/false,
4541 /*is_declaration=*/false);
4542 postfix_expression = cp_parser_functional_cast (parser, type);
4543 }
4544 break;
4545
4546 default:
4547 {
4548 tree type;
4549
4550 /* If the next thing is a simple-type-specifier, we may be
4551 looking at a functional cast. We could also be looking at
4552 an id-expression. So, we try the functional cast, and if
4553 that doesn't work we fall back to the primary-expression. */
4554 cp_parser_parse_tentatively (parser);
4555 /* Look for the simple-type-specifier. */
4556 type = cp_parser_simple_type_specifier (parser,
4557 /*decl_specs=*/NULL,
4558 CP_PARSER_FLAGS_NONE);
4559 /* Parse the cast itself. */
4560 if (!cp_parser_error_occurred (parser))
4561 postfix_expression
4562 = cp_parser_functional_cast (parser, type);
4563 /* If that worked, we're done. */
4564 if (cp_parser_parse_definitely (parser))
4565 break;
4566
4567 /* If the functional-cast didn't work out, try a
4568 compound-literal. */
4569 if (cp_parser_allow_gnu_extensions_p (parser)
4570 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4571 {
4572 VEC(constructor_elt,gc) *initializer_list = NULL;
4573 bool saved_in_type_id_in_expr_p;
4574
4575 cp_parser_parse_tentatively (parser);
4576 /* Consume the `('. */
4577 cp_lexer_consume_token (parser->lexer);
4578 /* Parse the type. */
4579 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4580 parser->in_type_id_in_expr_p = true;
4581 type = cp_parser_type_id (parser);
4582 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4583 /* Look for the `)'. */
4584 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4585 /* Look for the `{'. */
4586 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4587 /* If things aren't going well, there's no need to
4588 keep going. */
4589 if (!cp_parser_error_occurred (parser))
4590 {
4591 bool non_constant_p;
4592 /* Parse the initializer-list. */
4593 initializer_list
4594 = cp_parser_initializer_list (parser, &non_constant_p);
4595 /* Allow a trailing `,'. */
4596 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4597 cp_lexer_consume_token (parser->lexer);
4598 /* Look for the final `}'. */
4599 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4600 }
4601 /* If that worked, we're definitely looking at a
4602 compound-literal expression. */
4603 if (cp_parser_parse_definitely (parser))
4604 {
4605 /* Warn the user that a compound literal is not
4606 allowed in standard C++. */
4607 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4608 /* For simplicity, we disallow compound literals in
4609 constant-expressions. We could
4610 allow compound literals of integer type, whose
4611 initializer was a constant, in constant
4612 expressions. Permitting that usage, as a further
4613 extension, would not change the meaning of any
4614 currently accepted programs. (Of course, as
4615 compound literals are not part of ISO C++, the
4616 standard has nothing to say.) */
4617 if (cp_parser_non_integral_constant_expression
4618 (parser, "non-constant compound literals"))
4619 {
4620 postfix_expression = error_mark_node;
4621 break;
4622 }
4623 /* Form the representation of the compound-literal. */
4624 postfix_expression
4625 = (finish_compound_literal
4626 (type, build_constructor (init_list_type_node,
4627 initializer_list)));
4628 break;
4629 }
4630 }
4631
4632 /* It must be a primary-expression. */
4633 postfix_expression
4634 = cp_parser_primary_expression (parser, address_p, cast_p,
4635 /*template_arg_p=*/false,
4636 &idk);
4637 }
4638 break;
4639 }
4640
4641 /* Keep looping until the postfix-expression is complete. */
4642 while (true)
4643 {
4644 if (idk == CP_ID_KIND_UNQUALIFIED
4645 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4646 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4647 /* It is not a Koenig lookup function call. */
4648 postfix_expression
4649 = unqualified_name_lookup_error (postfix_expression);
4650
4651 /* Peek at the next token. */
4652 token = cp_lexer_peek_token (parser->lexer);
4653
4654 switch (token->type)
4655 {
4656 case CPP_OPEN_SQUARE:
4657 postfix_expression
4658 = cp_parser_postfix_open_square_expression (parser,
4659 postfix_expression,
4660 false);
4661 idk = CP_ID_KIND_NONE;
4662 is_member_access = false;
4663 break;
4664
4665 case CPP_OPEN_PAREN:
4666 /* postfix-expression ( expression-list [opt] ) */
4667 {
4668 bool koenig_p;
4669 bool is_builtin_constant_p;
4670 bool saved_integral_constant_expression_p = false;
4671 bool saved_non_integral_constant_expression_p = false;
4672 tree args;
4673
4674 is_member_access = false;
4675
4676 is_builtin_constant_p
4677 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4678 if (is_builtin_constant_p)
4679 {
4680 /* The whole point of __builtin_constant_p is to allow
4681 non-constant expressions to appear as arguments. */
4682 saved_integral_constant_expression_p
4683 = parser->integral_constant_expression_p;
4684 saved_non_integral_constant_expression_p
4685 = parser->non_integral_constant_expression_p;
4686 parser->integral_constant_expression_p = false;
4687 }
4688 args = (cp_parser_parenthesized_expression_list
4689 (parser, /*is_attribute_list=*/false,
4690 /*cast_p=*/false, /*allow_expansion_p=*/true,
4691 /*non_constant_p=*/NULL));
4692 if (is_builtin_constant_p)
4693 {
4694 parser->integral_constant_expression_p
4695 = saved_integral_constant_expression_p;
4696 parser->non_integral_constant_expression_p
4697 = saved_non_integral_constant_expression_p;
4698 }
4699
4700 if (args == error_mark_node)
4701 {
4702 postfix_expression = error_mark_node;
4703 break;
4704 }
4705
4706 /* Function calls are not permitted in
4707 constant-expressions. */
4708 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4709 && cp_parser_non_integral_constant_expression (parser,
4710 "a function call"))
4711 {
4712 postfix_expression = error_mark_node;
4713 break;
4714 }
4715
4716 koenig_p = false;
4717 if (idk == CP_ID_KIND_UNQUALIFIED)
4718 {
4719 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4720 {
4721 if (args)
4722 {
4723 koenig_p = true;
4724 postfix_expression
4725 = perform_koenig_lookup (postfix_expression, args);
4726 }
4727 else
4728 postfix_expression
4729 = unqualified_fn_lookup_error (postfix_expression);
4730 }
4731 /* We do not perform argument-dependent lookup if
4732 normal lookup finds a non-function, in accordance
4733 with the expected resolution of DR 218. */
4734 else if (args && is_overloaded_fn (postfix_expression))
4735 {
4736 tree fn = get_first_fn (postfix_expression);
4737
4738 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4739 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4740
4741 /* Only do argument dependent lookup if regular
4742 lookup does not find a set of member functions.
4743 [basic.lookup.koenig]/2a */
4744 if (!DECL_FUNCTION_MEMBER_P (fn))
4745 {
4746 koenig_p = true;
4747 postfix_expression
4748 = perform_koenig_lookup (postfix_expression, args);
4749 }
4750 }
4751 }
4752
4753 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4754 {
4755 tree instance = TREE_OPERAND (postfix_expression, 0);
4756 tree fn = TREE_OPERAND (postfix_expression, 1);
4757
4758 if (processing_template_decl
4759 && (type_dependent_expression_p (instance)
4760 || (!BASELINK_P (fn)
4761 && TREE_CODE (fn) != FIELD_DECL)
4762 || type_dependent_expression_p (fn)
4763 || any_type_dependent_arguments_p (args)))
4764 {
4765 postfix_expression
4766 = build_nt_call_list (postfix_expression, args);
4767 break;
4768 }
4769
4770 if (BASELINK_P (fn))
4771 {
4772 postfix_expression
4773 = (build_new_method_call
4774 (instance, fn, args, NULL_TREE,
4775 (idk == CP_ID_KIND_QUALIFIED
4776 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4777 /*fn_p=*/NULL,
4778 tf_warning_or_error));
4779 }
4780 else
4781 postfix_expression
4782 = finish_call_expr (postfix_expression, args,
4783 /*disallow_virtual=*/false,
4784 /*koenig_p=*/false,
4785 tf_warning_or_error);
4786 }
4787 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4788 || TREE_CODE (postfix_expression) == MEMBER_REF
4789 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4790 postfix_expression = (build_offset_ref_call_from_tree
4791 (postfix_expression, args));
4792 else if (idk == CP_ID_KIND_QUALIFIED)
4793 /* A call to a static class member, or a namespace-scope
4794 function. */
4795 postfix_expression
4796 = finish_call_expr (postfix_expression, args,
4797 /*disallow_virtual=*/true,
4798 koenig_p,
4799 tf_warning_or_error);
4800 else
4801 /* All other function calls. */
4802 postfix_expression
4803 = finish_call_expr (postfix_expression, args,
4804 /*disallow_virtual=*/false,
4805 koenig_p,
4806 tf_warning_or_error);
4807
4808 if (warn_disallowed_functions)
4809 warn_if_disallowed_function_p (postfix_expression);
4810
4811 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4812 idk = CP_ID_KIND_NONE;
4813 }
4814 break;
4815
4816 case CPP_DOT:
4817 case CPP_DEREF:
4818 /* postfix-expression . template [opt] id-expression
4819 postfix-expression . pseudo-destructor-name
4820 postfix-expression -> template [opt] id-expression
4821 postfix-expression -> pseudo-destructor-name */
4822
4823 /* Consume the `.' or `->' operator. */
4824 cp_lexer_consume_token (parser->lexer);
4825
4826 postfix_expression
4827 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4828 postfix_expression,
4829 false, &idk,
4830 token->location);
4831
4832 is_member_access = true;
4833 break;
4834
4835 case CPP_PLUS_PLUS:
4836 /* postfix-expression ++ */
4837 /* Consume the `++' token. */
4838 cp_lexer_consume_token (parser->lexer);
4839 /* Generate a representation for the complete expression. */
4840 postfix_expression
4841 = finish_increment_expr (postfix_expression,
4842 POSTINCREMENT_EXPR);
4843 /* Increments may not appear in constant-expressions. */
4844 if (cp_parser_non_integral_constant_expression (parser,
4845 "an increment"))
4846 postfix_expression = error_mark_node;
4847 idk = CP_ID_KIND_NONE;
4848 is_member_access = false;
4849 break;
4850
4851 case CPP_MINUS_MINUS:
4852 /* postfix-expression -- */
4853 /* Consume the `--' token. */
4854 cp_lexer_consume_token (parser->lexer);
4855 /* Generate a representation for the complete expression. */
4856 postfix_expression
4857 = finish_increment_expr (postfix_expression,
4858 POSTDECREMENT_EXPR);
4859 /* Decrements may not appear in constant-expressions. */
4860 if (cp_parser_non_integral_constant_expression (parser,
4861 "a decrement"))
4862 postfix_expression = error_mark_node;
4863 idk = CP_ID_KIND_NONE;
4864 is_member_access = false;
4865 break;
4866
4867 default:
4868 if (pidk_return != NULL)
4869 * pidk_return = idk;
4870 if (member_access_only_p)
4871 return is_member_access? postfix_expression : error_mark_node;
4872 else
4873 return postfix_expression;
4874 }
4875 }
4876
4877 /* We should never get here. */
4878 gcc_unreachable ();
4879 return error_mark_node;
4880 }
4881
4882 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4883 by cp_parser_builtin_offsetof. We're looking for
4884
4885 postfix-expression [ expression ]
4886
4887 FOR_OFFSETOF is set if we're being called in that context, which
4888 changes how we deal with integer constant expressions. */
4889
4890 static tree
4891 cp_parser_postfix_open_square_expression (cp_parser *parser,
4892 tree postfix_expression,
4893 bool for_offsetof)
4894 {
4895 tree index;
4896
4897 /* Consume the `[' token. */
4898 cp_lexer_consume_token (parser->lexer);
4899
4900 /* Parse the index expression. */
4901 /* ??? For offsetof, there is a question of what to allow here. If
4902 offsetof is not being used in an integral constant expression context,
4903 then we *could* get the right answer by computing the value at runtime.
4904 If we are in an integral constant expression context, then we might
4905 could accept any constant expression; hard to say without analysis.
4906 Rather than open the barn door too wide right away, allow only integer
4907 constant expressions here. */
4908 if (for_offsetof)
4909 index = cp_parser_constant_expression (parser, false, NULL);
4910 else
4911 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4912
4913 /* Look for the closing `]'. */
4914 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4915
4916 /* Build the ARRAY_REF. */
4917 postfix_expression = grok_array_decl (postfix_expression, index);
4918
4919 /* When not doing offsetof, array references are not permitted in
4920 constant-expressions. */
4921 if (!for_offsetof
4922 && (cp_parser_non_integral_constant_expression
4923 (parser, "an array reference")))
4924 postfix_expression = error_mark_node;
4925
4926 return postfix_expression;
4927 }
4928
4929 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4930 by cp_parser_builtin_offsetof. We're looking for
4931
4932 postfix-expression . template [opt] id-expression
4933 postfix-expression . pseudo-destructor-name
4934 postfix-expression -> template [opt] id-expression
4935 postfix-expression -> pseudo-destructor-name
4936
4937 FOR_OFFSETOF is set if we're being called in that context. That sorta
4938 limits what of the above we'll actually accept, but nevermind.
4939 TOKEN_TYPE is the "." or "->" token, which will already have been
4940 removed from the stream. */
4941
4942 static tree
4943 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4944 enum cpp_ttype token_type,
4945 tree postfix_expression,
4946 bool for_offsetof, cp_id_kind *idk,
4947 location_t location)
4948 {
4949 tree name;
4950 bool dependent_p;
4951 bool pseudo_destructor_p;
4952 tree scope = NULL_TREE;
4953
4954 /* If this is a `->' operator, dereference the pointer. */
4955 if (token_type == CPP_DEREF)
4956 postfix_expression = build_x_arrow (postfix_expression);
4957 /* Check to see whether or not the expression is type-dependent. */
4958 dependent_p = type_dependent_expression_p (postfix_expression);
4959 /* The identifier following the `->' or `.' is not qualified. */
4960 parser->scope = NULL_TREE;
4961 parser->qualifying_scope = NULL_TREE;
4962 parser->object_scope = NULL_TREE;
4963 *idk = CP_ID_KIND_NONE;
4964
4965 /* Enter the scope corresponding to the type of the object
4966 given by the POSTFIX_EXPRESSION. */
4967 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4968 {
4969 scope = TREE_TYPE (postfix_expression);
4970 /* According to the standard, no expression should ever have
4971 reference type. Unfortunately, we do not currently match
4972 the standard in this respect in that our internal representation
4973 of an expression may have reference type even when the standard
4974 says it does not. Therefore, we have to manually obtain the
4975 underlying type here. */
4976 scope = non_reference (scope);
4977 /* The type of the POSTFIX_EXPRESSION must be complete. */
4978 if (scope == unknown_type_node)
4979 {
4980 error ("%H%qE does not have class type", &location, postfix_expression);
4981 scope = NULL_TREE;
4982 }
4983 else
4984 scope = complete_type_or_else (scope, NULL_TREE);
4985 /* Let the name lookup machinery know that we are processing a
4986 class member access expression. */
4987 parser->context->object_type = scope;
4988 /* If something went wrong, we want to be able to discern that case,
4989 as opposed to the case where there was no SCOPE due to the type
4990 of expression being dependent. */
4991 if (!scope)
4992 scope = error_mark_node;
4993 /* If the SCOPE was erroneous, make the various semantic analysis
4994 functions exit quickly -- and without issuing additional error
4995 messages. */
4996 if (scope == error_mark_node)
4997 postfix_expression = error_mark_node;
4998 }
4999
5000 /* Assume this expression is not a pseudo-destructor access. */
5001 pseudo_destructor_p = false;
5002
5003 /* If the SCOPE is a scalar type, then, if this is a valid program,
5004 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5005 is type dependent, it can be pseudo-destructor-name or something else.
5006 Try to parse it as pseudo-destructor-name first. */
5007 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5008 {
5009 tree s;
5010 tree type;
5011
5012 cp_parser_parse_tentatively (parser);
5013 /* Parse the pseudo-destructor-name. */
5014 s = NULL_TREE;
5015 cp_parser_pseudo_destructor_name (parser, &s, &type);
5016 if (dependent_p
5017 && (cp_parser_error_occurred (parser)
5018 || TREE_CODE (type) != TYPE_DECL
5019 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5020 cp_parser_abort_tentative_parse (parser);
5021 else if (cp_parser_parse_definitely (parser))
5022 {
5023 pseudo_destructor_p = true;
5024 postfix_expression
5025 = finish_pseudo_destructor_expr (postfix_expression,
5026 s, TREE_TYPE (type));
5027 }
5028 }
5029
5030 if (!pseudo_destructor_p)
5031 {
5032 /* If the SCOPE is not a scalar type, we are looking at an
5033 ordinary class member access expression, rather than a
5034 pseudo-destructor-name. */
5035 bool template_p;
5036 cp_token *token = cp_lexer_peek_token (parser->lexer);
5037 /* Parse the id-expression. */
5038 name = (cp_parser_id_expression
5039 (parser,
5040 cp_parser_optional_template_keyword (parser),
5041 /*check_dependency_p=*/true,
5042 &template_p,
5043 /*declarator_p=*/false,
5044 /*optional_p=*/false));
5045 /* In general, build a SCOPE_REF if the member name is qualified.
5046 However, if the name was not dependent and has already been
5047 resolved; there is no need to build the SCOPE_REF. For example;
5048
5049 struct X { void f(); };
5050 template <typename T> void f(T* t) { t->X::f(); }
5051
5052 Even though "t" is dependent, "X::f" is not and has been resolved
5053 to a BASELINK; there is no need to include scope information. */
5054
5055 /* But we do need to remember that there was an explicit scope for
5056 virtual function calls. */
5057 if (parser->scope)
5058 *idk = CP_ID_KIND_QUALIFIED;
5059
5060 /* If the name is a template-id that names a type, we will get a
5061 TYPE_DECL here. That is invalid code. */
5062 if (TREE_CODE (name) == TYPE_DECL)
5063 {
5064 error ("%Hinvalid use of %qD", &token->location, name);
5065 postfix_expression = error_mark_node;
5066 }
5067 else
5068 {
5069 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5070 {
5071 name = build_qualified_name (/*type=*/NULL_TREE,
5072 parser->scope,
5073 name,
5074 template_p);
5075 parser->scope = NULL_TREE;
5076 parser->qualifying_scope = NULL_TREE;
5077 parser->object_scope = NULL_TREE;
5078 }
5079 if (scope && name && BASELINK_P (name))
5080 adjust_result_of_qualified_name_lookup
5081 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5082 postfix_expression
5083 = finish_class_member_access_expr (postfix_expression, name,
5084 template_p,
5085 tf_warning_or_error);
5086 }
5087 }
5088
5089 /* We no longer need to look up names in the scope of the object on
5090 the left-hand side of the `.' or `->' operator. */
5091 parser->context->object_type = NULL_TREE;
5092
5093 /* Outside of offsetof, these operators may not appear in
5094 constant-expressions. */
5095 if (!for_offsetof
5096 && (cp_parser_non_integral_constant_expression
5097 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5098 postfix_expression = error_mark_node;
5099
5100 return postfix_expression;
5101 }
5102
5103 /* Parse a parenthesized expression-list.
5104
5105 expression-list:
5106 assignment-expression
5107 expression-list, assignment-expression
5108
5109 attribute-list:
5110 expression-list
5111 identifier
5112 identifier, expression-list
5113
5114 CAST_P is true if this expression is the target of a cast.
5115
5116 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5117 argument pack.
5118
5119 Returns a TREE_LIST. The TREE_VALUE of each node is a
5120 representation of an assignment-expression. Note that a TREE_LIST
5121 is returned even if there is only a single expression in the list.
5122 error_mark_node is returned if the ( and or ) are
5123 missing. NULL_TREE is returned on no expressions. The parentheses
5124 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5125 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5126 indicates whether or not all of the expressions in the list were
5127 constant. */
5128
5129 static tree
5130 cp_parser_parenthesized_expression_list (cp_parser* parser,
5131 bool is_attribute_list,
5132 bool cast_p,
5133 bool allow_expansion_p,
5134 bool *non_constant_p)
5135 {
5136 tree expression_list = NULL_TREE;
5137 bool fold_expr_p = is_attribute_list;
5138 tree identifier = NULL_TREE;
5139 bool saved_greater_than_is_operator_p;
5140
5141 /* Assume all the expressions will be constant. */
5142 if (non_constant_p)
5143 *non_constant_p = false;
5144
5145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5146 return error_mark_node;
5147
5148 /* Within a parenthesized expression, a `>' token is always
5149 the greater-than operator. */
5150 saved_greater_than_is_operator_p
5151 = parser->greater_than_is_operator_p;
5152 parser->greater_than_is_operator_p = true;
5153
5154 /* Consume expressions until there are no more. */
5155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5156 while (true)
5157 {
5158 tree expr;
5159
5160 /* At the beginning of attribute lists, check to see if the
5161 next token is an identifier. */
5162 if (is_attribute_list
5163 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5164 {
5165 cp_token *token;
5166
5167 /* Consume the identifier. */
5168 token = cp_lexer_consume_token (parser->lexer);
5169 /* Save the identifier. */
5170 identifier = token->u.value;
5171 }
5172 else
5173 {
5174 bool expr_non_constant_p;
5175
5176 /* Parse the next assignment-expression. */
5177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5178 {
5179 /* A braced-init-list. */
5180 maybe_warn_cpp0x ("extended initializer lists");
5181 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5182 if (non_constant_p && expr_non_constant_p)
5183 *non_constant_p = true;
5184 }
5185 else if (non_constant_p)
5186 {
5187 expr = (cp_parser_constant_expression
5188 (parser, /*allow_non_constant_p=*/true,
5189 &expr_non_constant_p));
5190 if (expr_non_constant_p)
5191 *non_constant_p = true;
5192 }
5193 else
5194 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5195
5196 if (fold_expr_p)
5197 expr = fold_non_dependent_expr (expr);
5198
5199 /* If we have an ellipsis, then this is an expression
5200 expansion. */
5201 if (allow_expansion_p
5202 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5203 {
5204 /* Consume the `...'. */
5205 cp_lexer_consume_token (parser->lexer);
5206
5207 /* Build the argument pack. */
5208 expr = make_pack_expansion (expr);
5209 }
5210
5211 /* Add it to the list. We add error_mark_node
5212 expressions to the list, so that we can still tell if
5213 the correct form for a parenthesized expression-list
5214 is found. That gives better errors. */
5215 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5216
5217 if (expr == error_mark_node)
5218 goto skip_comma;
5219 }
5220
5221 /* After the first item, attribute lists look the same as
5222 expression lists. */
5223 is_attribute_list = false;
5224
5225 get_comma:;
5226 /* If the next token isn't a `,', then we are done. */
5227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5228 break;
5229
5230 /* Otherwise, consume the `,' and keep going. */
5231 cp_lexer_consume_token (parser->lexer);
5232 }
5233
5234 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5235 {
5236 int ending;
5237
5238 skip_comma:;
5239 /* We try and resync to an unnested comma, as that will give the
5240 user better diagnostics. */
5241 ending = cp_parser_skip_to_closing_parenthesis (parser,
5242 /*recovering=*/true,
5243 /*or_comma=*/true,
5244 /*consume_paren=*/true);
5245 if (ending < 0)
5246 goto get_comma;
5247 if (!ending)
5248 {
5249 parser->greater_than_is_operator_p
5250 = saved_greater_than_is_operator_p;
5251 return error_mark_node;
5252 }
5253 }
5254
5255 parser->greater_than_is_operator_p
5256 = saved_greater_than_is_operator_p;
5257
5258 /* We built up the list in reverse order so we must reverse it now. */
5259 expression_list = nreverse (expression_list);
5260 if (identifier)
5261 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5262
5263 return expression_list;
5264 }
5265
5266 /* Parse a pseudo-destructor-name.
5267
5268 pseudo-destructor-name:
5269 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5270 :: [opt] nested-name-specifier template template-id :: ~ type-name
5271 :: [opt] nested-name-specifier [opt] ~ type-name
5272
5273 If either of the first two productions is used, sets *SCOPE to the
5274 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5275 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5276 or ERROR_MARK_NODE if the parse fails. */
5277
5278 static void
5279 cp_parser_pseudo_destructor_name (cp_parser* parser,
5280 tree* scope,
5281 tree* type)
5282 {
5283 bool nested_name_specifier_p;
5284
5285 /* Assume that things will not work out. */
5286 *type = error_mark_node;
5287
5288 /* Look for the optional `::' operator. */
5289 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5290 /* Look for the optional nested-name-specifier. */
5291 nested_name_specifier_p
5292 = (cp_parser_nested_name_specifier_opt (parser,
5293 /*typename_keyword_p=*/false,
5294 /*check_dependency_p=*/true,
5295 /*type_p=*/false,
5296 /*is_declaration=*/false)
5297 != NULL_TREE);
5298 /* Now, if we saw a nested-name-specifier, we might be doing the
5299 second production. */
5300 if (nested_name_specifier_p
5301 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5302 {
5303 /* Consume the `template' keyword. */
5304 cp_lexer_consume_token (parser->lexer);
5305 /* Parse the template-id. */
5306 cp_parser_template_id (parser,
5307 /*template_keyword_p=*/true,
5308 /*check_dependency_p=*/false,
5309 /*is_declaration=*/true);
5310 /* Look for the `::' token. */
5311 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5312 }
5313 /* If the next token is not a `~', then there might be some
5314 additional qualification. */
5315 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5316 {
5317 /* At this point, we're looking for "type-name :: ~". The type-name
5318 must not be a class-name, since this is a pseudo-destructor. So,
5319 it must be either an enum-name, or a typedef-name -- both of which
5320 are just identifiers. So, we peek ahead to check that the "::"
5321 and "~" tokens are present; if they are not, then we can avoid
5322 calling type_name. */
5323 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5324 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5325 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5326 {
5327 cp_parser_error (parser, "non-scalar type");
5328 return;
5329 }
5330
5331 /* Look for the type-name. */
5332 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5333 if (*scope == error_mark_node)
5334 return;
5335
5336 /* Look for the `::' token. */
5337 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5338 }
5339 else
5340 *scope = NULL_TREE;
5341
5342 /* Look for the `~'. */
5343 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5344 /* Look for the type-name again. We are not responsible for
5345 checking that it matches the first type-name. */
5346 *type = cp_parser_nonclass_name (parser);
5347 }
5348
5349 /* Parse a unary-expression.
5350
5351 unary-expression:
5352 postfix-expression
5353 ++ cast-expression
5354 -- cast-expression
5355 unary-operator cast-expression
5356 sizeof unary-expression
5357 sizeof ( type-id )
5358 new-expression
5359 delete-expression
5360
5361 GNU Extensions:
5362
5363 unary-expression:
5364 __extension__ cast-expression
5365 __alignof__ unary-expression
5366 __alignof__ ( type-id )
5367 __real__ cast-expression
5368 __imag__ cast-expression
5369 && identifier
5370
5371 ADDRESS_P is true iff the unary-expression is appearing as the
5372 operand of the `&' operator. CAST_P is true if this expression is
5373 the target of a cast.
5374
5375 Returns a representation of the expression. */
5376
5377 static tree
5378 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5379 cp_id_kind * pidk)
5380 {
5381 cp_token *token;
5382 enum tree_code unary_operator;
5383
5384 /* Peek at the next token. */
5385 token = cp_lexer_peek_token (parser->lexer);
5386 /* Some keywords give away the kind of expression. */
5387 if (token->type == CPP_KEYWORD)
5388 {
5389 enum rid keyword = token->keyword;
5390
5391 switch (keyword)
5392 {
5393 case RID_ALIGNOF:
5394 case RID_SIZEOF:
5395 {
5396 tree operand;
5397 enum tree_code op;
5398
5399 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5400 /* Consume the token. */
5401 cp_lexer_consume_token (parser->lexer);
5402 /* Parse the operand. */
5403 operand = cp_parser_sizeof_operand (parser, keyword);
5404
5405 if (TYPE_P (operand))
5406 return cxx_sizeof_or_alignof_type (operand, op, true);
5407 else
5408 return cxx_sizeof_or_alignof_expr (operand, op, true);
5409 }
5410
5411 case RID_NEW:
5412 return cp_parser_new_expression (parser);
5413
5414 case RID_DELETE:
5415 return cp_parser_delete_expression (parser);
5416
5417 case RID_EXTENSION:
5418 {
5419 /* The saved value of the PEDANTIC flag. */
5420 int saved_pedantic;
5421 tree expr;
5422
5423 /* Save away the PEDANTIC flag. */
5424 cp_parser_extension_opt (parser, &saved_pedantic);
5425 /* Parse the cast-expression. */
5426 expr = cp_parser_simple_cast_expression (parser);
5427 /* Restore the PEDANTIC flag. */
5428 pedantic = saved_pedantic;
5429
5430 return expr;
5431 }
5432
5433 case RID_REALPART:
5434 case RID_IMAGPART:
5435 {
5436 tree expression;
5437
5438 /* Consume the `__real__' or `__imag__' token. */
5439 cp_lexer_consume_token (parser->lexer);
5440 /* Parse the cast-expression. */
5441 expression = cp_parser_simple_cast_expression (parser);
5442 /* Create the complete representation. */
5443 return build_x_unary_op ((keyword == RID_REALPART
5444 ? REALPART_EXPR : IMAGPART_EXPR),
5445 expression,
5446 tf_warning_or_error);
5447 }
5448 break;
5449
5450 default:
5451 break;
5452 }
5453 }
5454
5455 /* Look for the `:: new' and `:: delete', which also signal the
5456 beginning of a new-expression, or delete-expression,
5457 respectively. If the next token is `::', then it might be one of
5458 these. */
5459 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5460 {
5461 enum rid keyword;
5462
5463 /* See if the token after the `::' is one of the keywords in
5464 which we're interested. */
5465 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5466 /* If it's `new', we have a new-expression. */
5467 if (keyword == RID_NEW)
5468 return cp_parser_new_expression (parser);
5469 /* Similarly, for `delete'. */
5470 else if (keyword == RID_DELETE)
5471 return cp_parser_delete_expression (parser);
5472 }
5473
5474 /* Look for a unary operator. */
5475 unary_operator = cp_parser_unary_operator (token);
5476 /* The `++' and `--' operators can be handled similarly, even though
5477 they are not technically unary-operators in the grammar. */
5478 if (unary_operator == ERROR_MARK)
5479 {
5480 if (token->type == CPP_PLUS_PLUS)
5481 unary_operator = PREINCREMENT_EXPR;
5482 else if (token->type == CPP_MINUS_MINUS)
5483 unary_operator = PREDECREMENT_EXPR;
5484 /* Handle the GNU address-of-label extension. */
5485 else if (cp_parser_allow_gnu_extensions_p (parser)
5486 && token->type == CPP_AND_AND)
5487 {
5488 tree identifier;
5489 tree expression;
5490 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5491
5492 /* Consume the '&&' token. */
5493 cp_lexer_consume_token (parser->lexer);
5494 /* Look for the identifier. */
5495 identifier = cp_parser_identifier (parser);
5496 /* Create an expression representing the address. */
5497 expression = finish_label_address_expr (identifier, loc);
5498 if (cp_parser_non_integral_constant_expression (parser,
5499 "the address of a label"))
5500 expression = error_mark_node;
5501 return expression;
5502 }
5503 }
5504 if (unary_operator != ERROR_MARK)
5505 {
5506 tree cast_expression;
5507 tree expression = error_mark_node;
5508 const char *non_constant_p = NULL;
5509
5510 /* Consume the operator token. */
5511 token = cp_lexer_consume_token (parser->lexer);
5512 /* Parse the cast-expression. */
5513 cast_expression
5514 = cp_parser_cast_expression (parser,
5515 unary_operator == ADDR_EXPR,
5516 /*cast_p=*/false, pidk);
5517 /* Now, build an appropriate representation. */
5518 switch (unary_operator)
5519 {
5520 case INDIRECT_REF:
5521 non_constant_p = "%<*%>";
5522 expression = build_x_indirect_ref (cast_expression, "unary *",
5523 tf_warning_or_error);
5524 break;
5525
5526 case ADDR_EXPR:
5527 non_constant_p = "%<&%>";
5528 /* Fall through. */
5529 case BIT_NOT_EXPR:
5530 expression = build_x_unary_op (unary_operator, cast_expression,
5531 tf_warning_or_error);
5532 break;
5533
5534 case PREINCREMENT_EXPR:
5535 case PREDECREMENT_EXPR:
5536 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5537 ? "%<++%>" : "%<--%>");
5538 /* Fall through. */
5539 case UNARY_PLUS_EXPR:
5540 case NEGATE_EXPR:
5541 case TRUTH_NOT_EXPR:
5542 expression = finish_unary_op_expr (unary_operator, cast_expression);
5543 break;
5544
5545 default:
5546 gcc_unreachable ();
5547 }
5548
5549 if (non_constant_p
5550 && cp_parser_non_integral_constant_expression (parser,
5551 non_constant_p))
5552 expression = error_mark_node;
5553
5554 return expression;
5555 }
5556
5557 return cp_parser_postfix_expression (parser, address_p, cast_p,
5558 /*member_access_only_p=*/false,
5559 pidk);
5560 }
5561
5562 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5563 unary-operator, the corresponding tree code is returned. */
5564
5565 static enum tree_code
5566 cp_parser_unary_operator (cp_token* token)
5567 {
5568 switch (token->type)
5569 {
5570 case CPP_MULT:
5571 return INDIRECT_REF;
5572
5573 case CPP_AND:
5574 return ADDR_EXPR;
5575
5576 case CPP_PLUS:
5577 return UNARY_PLUS_EXPR;
5578
5579 case CPP_MINUS:
5580 return NEGATE_EXPR;
5581
5582 case CPP_NOT:
5583 return TRUTH_NOT_EXPR;
5584
5585 case CPP_COMPL:
5586 return BIT_NOT_EXPR;
5587
5588 default:
5589 return ERROR_MARK;
5590 }
5591 }
5592
5593 /* Parse a new-expression.
5594
5595 new-expression:
5596 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5597 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5598
5599 Returns a representation of the expression. */
5600
5601 static tree
5602 cp_parser_new_expression (cp_parser* parser)
5603 {
5604 bool global_scope_p;
5605 tree placement;
5606 tree type;
5607 tree initializer;
5608 tree nelts;
5609
5610 /* Look for the optional `::' operator. */
5611 global_scope_p
5612 = (cp_parser_global_scope_opt (parser,
5613 /*current_scope_valid_p=*/false)
5614 != NULL_TREE);
5615 /* Look for the `new' operator. */
5616 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5617 /* There's no easy way to tell a new-placement from the
5618 `( type-id )' construct. */
5619 cp_parser_parse_tentatively (parser);
5620 /* Look for a new-placement. */
5621 placement = cp_parser_new_placement (parser);
5622 /* If that didn't work out, there's no new-placement. */
5623 if (!cp_parser_parse_definitely (parser))
5624 placement = NULL_TREE;
5625
5626 /* If the next token is a `(', then we have a parenthesized
5627 type-id. */
5628 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5629 {
5630 cp_token *token;
5631 /* Consume the `('. */
5632 cp_lexer_consume_token (parser->lexer);
5633 /* Parse the type-id. */
5634 type = cp_parser_type_id (parser);
5635 /* Look for the closing `)'. */
5636 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5637 token = cp_lexer_peek_token (parser->lexer);
5638 /* There should not be a direct-new-declarator in this production,
5639 but GCC used to allowed this, so we check and emit a sensible error
5640 message for this case. */
5641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5642 {
5643 error ("%Harray bound forbidden after parenthesized type-id",
5644 &token->location);
5645 inform (token->location,
5646 "try removing the parentheses around the type-id");
5647 cp_parser_direct_new_declarator (parser);
5648 }
5649 nelts = NULL_TREE;
5650 }
5651 /* Otherwise, there must be a new-type-id. */
5652 else
5653 type = cp_parser_new_type_id (parser, &nelts);
5654
5655 /* If the next token is a `(' or '{', then we have a new-initializer. */
5656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5657 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5658 initializer = cp_parser_new_initializer (parser);
5659 else
5660 initializer = NULL_TREE;
5661
5662 /* A new-expression may not appear in an integral constant
5663 expression. */
5664 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5665 return error_mark_node;
5666
5667 /* Create a representation of the new-expression. */
5668 return build_new (placement, type, nelts, initializer, global_scope_p,
5669 tf_warning_or_error);
5670 }
5671
5672 /* Parse a new-placement.
5673
5674 new-placement:
5675 ( expression-list )
5676
5677 Returns the same representation as for an expression-list. */
5678
5679 static tree
5680 cp_parser_new_placement (cp_parser* parser)
5681 {
5682 tree expression_list;
5683
5684 /* Parse the expression-list. */
5685 expression_list = (cp_parser_parenthesized_expression_list
5686 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5687 /*non_constant_p=*/NULL));
5688
5689 return expression_list;
5690 }
5691
5692 /* Parse a new-type-id.
5693
5694 new-type-id:
5695 type-specifier-seq new-declarator [opt]
5696
5697 Returns the TYPE allocated. If the new-type-id indicates an array
5698 type, *NELTS is set to the number of elements in the last array
5699 bound; the TYPE will not include the last array bound. */
5700
5701 static tree
5702 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5703 {
5704 cp_decl_specifier_seq type_specifier_seq;
5705 cp_declarator *new_declarator;
5706 cp_declarator *declarator;
5707 cp_declarator *outer_declarator;
5708 const char *saved_message;
5709 tree type;
5710
5711 /* The type-specifier sequence must not contain type definitions.
5712 (It cannot contain declarations of new types either, but if they
5713 are not definitions we will catch that because they are not
5714 complete.) */
5715 saved_message = parser->type_definition_forbidden_message;
5716 parser->type_definition_forbidden_message
5717 = "types may not be defined in a new-type-id";
5718 /* Parse the type-specifier-seq. */
5719 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5720 &type_specifier_seq);
5721 /* Restore the old message. */
5722 parser->type_definition_forbidden_message = saved_message;
5723 /* Parse the new-declarator. */
5724 new_declarator = cp_parser_new_declarator_opt (parser);
5725
5726 /* Determine the number of elements in the last array dimension, if
5727 any. */
5728 *nelts = NULL_TREE;
5729 /* Skip down to the last array dimension. */
5730 declarator = new_declarator;
5731 outer_declarator = NULL;
5732 while (declarator && (declarator->kind == cdk_pointer
5733 || declarator->kind == cdk_ptrmem))
5734 {
5735 outer_declarator = declarator;
5736 declarator = declarator->declarator;
5737 }
5738 while (declarator
5739 && declarator->kind == cdk_array
5740 && declarator->declarator
5741 && declarator->declarator->kind == cdk_array)
5742 {
5743 outer_declarator = declarator;
5744 declarator = declarator->declarator;
5745 }
5746
5747 if (declarator && declarator->kind == cdk_array)
5748 {
5749 *nelts = declarator->u.array.bounds;
5750 if (*nelts == error_mark_node)
5751 *nelts = integer_one_node;
5752
5753 if (outer_declarator)
5754 outer_declarator->declarator = declarator->declarator;
5755 else
5756 new_declarator = NULL;
5757 }
5758
5759 type = groktypename (&type_specifier_seq, new_declarator);
5760 return type;
5761 }
5762
5763 /* Parse an (optional) new-declarator.
5764
5765 new-declarator:
5766 ptr-operator new-declarator [opt]
5767 direct-new-declarator
5768
5769 Returns the declarator. */
5770
5771 static cp_declarator *
5772 cp_parser_new_declarator_opt (cp_parser* parser)
5773 {
5774 enum tree_code code;
5775 tree type;
5776 cp_cv_quals cv_quals;
5777
5778 /* We don't know if there's a ptr-operator next, or not. */
5779 cp_parser_parse_tentatively (parser);
5780 /* Look for a ptr-operator. */
5781 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5782 /* If that worked, look for more new-declarators. */
5783 if (cp_parser_parse_definitely (parser))
5784 {
5785 cp_declarator *declarator;
5786
5787 /* Parse another optional declarator. */
5788 declarator = cp_parser_new_declarator_opt (parser);
5789
5790 return cp_parser_make_indirect_declarator
5791 (code, type, cv_quals, declarator);
5792 }
5793
5794 /* If the next token is a `[', there is a direct-new-declarator. */
5795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5796 return cp_parser_direct_new_declarator (parser);
5797
5798 return NULL;
5799 }
5800
5801 /* Parse a direct-new-declarator.
5802
5803 direct-new-declarator:
5804 [ expression ]
5805 direct-new-declarator [constant-expression]
5806
5807 */
5808
5809 static cp_declarator *
5810 cp_parser_direct_new_declarator (cp_parser* parser)
5811 {
5812 cp_declarator *declarator = NULL;
5813
5814 while (true)
5815 {
5816 tree expression;
5817
5818 /* Look for the opening `['. */
5819 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5820 /* The first expression is not required to be constant. */
5821 if (!declarator)
5822 {
5823 cp_token *token = cp_lexer_peek_token (parser->lexer);
5824 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5825 /* The standard requires that the expression have integral
5826 type. DR 74 adds enumeration types. We believe that the
5827 real intent is that these expressions be handled like the
5828 expression in a `switch' condition, which also allows
5829 classes with a single conversion to integral or
5830 enumeration type. */
5831 if (!processing_template_decl)
5832 {
5833 expression
5834 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5835 expression,
5836 /*complain=*/true);
5837 if (!expression)
5838 {
5839 error ("%Hexpression in new-declarator must have integral "
5840 "or enumeration type", &token->location);
5841 expression = error_mark_node;
5842 }
5843 }
5844 }
5845 /* But all the other expressions must be. */
5846 else
5847 expression
5848 = cp_parser_constant_expression (parser,
5849 /*allow_non_constant=*/false,
5850 NULL);
5851 /* Look for the closing `]'. */
5852 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5853
5854 /* Add this bound to the declarator. */
5855 declarator = make_array_declarator (declarator, expression);
5856
5857 /* If the next token is not a `[', then there are no more
5858 bounds. */
5859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5860 break;
5861 }
5862
5863 return declarator;
5864 }
5865
5866 /* Parse a new-initializer.
5867
5868 new-initializer:
5869 ( expression-list [opt] )
5870 braced-init-list
5871
5872 Returns a representation of the expression-list. If there is no
5873 expression-list, VOID_ZERO_NODE is returned. */
5874
5875 static tree
5876 cp_parser_new_initializer (cp_parser* parser)
5877 {
5878 tree expression_list;
5879
5880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5881 {
5882 bool expr_non_constant_p;
5883 maybe_warn_cpp0x ("extended initializer lists");
5884 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5885 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5886 expression_list = build_tree_list (NULL_TREE, expression_list);
5887 }
5888 else
5889 expression_list = (cp_parser_parenthesized_expression_list
5890 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5891 /*non_constant_p=*/NULL));
5892 if (!expression_list)
5893 expression_list = void_zero_node;
5894
5895 return expression_list;
5896 }
5897
5898 /* Parse a delete-expression.
5899
5900 delete-expression:
5901 :: [opt] delete cast-expression
5902 :: [opt] delete [ ] cast-expression
5903
5904 Returns a representation of the expression. */
5905
5906 static tree
5907 cp_parser_delete_expression (cp_parser* parser)
5908 {
5909 bool global_scope_p;
5910 bool array_p;
5911 tree expression;
5912
5913 /* Look for the optional `::' operator. */
5914 global_scope_p
5915 = (cp_parser_global_scope_opt (parser,
5916 /*current_scope_valid_p=*/false)
5917 != NULL_TREE);
5918 /* Look for the `delete' keyword. */
5919 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5920 /* See if the array syntax is in use. */
5921 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5922 {
5923 /* Consume the `[' token. */
5924 cp_lexer_consume_token (parser->lexer);
5925 /* Look for the `]' token. */
5926 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5927 /* Remember that this is the `[]' construct. */
5928 array_p = true;
5929 }
5930 else
5931 array_p = false;
5932
5933 /* Parse the cast-expression. */
5934 expression = cp_parser_simple_cast_expression (parser);
5935
5936 /* A delete-expression may not appear in an integral constant
5937 expression. */
5938 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5939 return error_mark_node;
5940
5941 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5942 }
5943
5944 /* Returns true if TOKEN may start a cast-expression and false
5945 otherwise. */
5946
5947 static bool
5948 cp_parser_token_starts_cast_expression (cp_token *token)
5949 {
5950 switch (token->type)
5951 {
5952 case CPP_COMMA:
5953 case CPP_SEMICOLON:
5954 case CPP_QUERY:
5955 case CPP_COLON:
5956 case CPP_CLOSE_SQUARE:
5957 case CPP_CLOSE_PAREN:
5958 case CPP_CLOSE_BRACE:
5959 case CPP_DOT:
5960 case CPP_DOT_STAR:
5961 case CPP_DEREF:
5962 case CPP_DEREF_STAR:
5963 case CPP_DIV:
5964 case CPP_MOD:
5965 case CPP_LSHIFT:
5966 case CPP_RSHIFT:
5967 case CPP_LESS:
5968 case CPP_GREATER:
5969 case CPP_LESS_EQ:
5970 case CPP_GREATER_EQ:
5971 case CPP_EQ_EQ:
5972 case CPP_NOT_EQ:
5973 case CPP_EQ:
5974 case CPP_MULT_EQ:
5975 case CPP_DIV_EQ:
5976 case CPP_MOD_EQ:
5977 case CPP_PLUS_EQ:
5978 case CPP_MINUS_EQ:
5979 case CPP_RSHIFT_EQ:
5980 case CPP_LSHIFT_EQ:
5981 case CPP_AND_EQ:
5982 case CPP_XOR_EQ:
5983 case CPP_OR_EQ:
5984 case CPP_XOR:
5985 case CPP_OR:
5986 case CPP_OR_OR:
5987 case CPP_EOF:
5988 return false;
5989
5990 /* '[' may start a primary-expression in obj-c++. */
5991 case CPP_OPEN_SQUARE:
5992 return c_dialect_objc ();
5993
5994 default:
5995 return true;
5996 }
5997 }
5998
5999 /* Parse a cast-expression.
6000
6001 cast-expression:
6002 unary-expression
6003 ( type-id ) cast-expression
6004
6005 ADDRESS_P is true iff the unary-expression is appearing as the
6006 operand of the `&' operator. CAST_P is true if this expression is
6007 the target of a cast.
6008
6009 Returns a representation of the expression. */
6010
6011 static tree
6012 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6013 cp_id_kind * pidk)
6014 {
6015 /* If it's a `(', then we might be looking at a cast. */
6016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6017 {
6018 tree type = NULL_TREE;
6019 tree expr = NULL_TREE;
6020 bool compound_literal_p;
6021 const char *saved_message;
6022
6023 /* There's no way to know yet whether or not this is a cast.
6024 For example, `(int (3))' is a unary-expression, while `(int)
6025 3' is a cast. So, we resort to parsing tentatively. */
6026 cp_parser_parse_tentatively (parser);
6027 /* Types may not be defined in a cast. */
6028 saved_message = parser->type_definition_forbidden_message;
6029 parser->type_definition_forbidden_message
6030 = "types may not be defined in casts";
6031 /* Consume the `('. */
6032 cp_lexer_consume_token (parser->lexer);
6033 /* A very tricky bit is that `(struct S) { 3 }' is a
6034 compound-literal (which we permit in C++ as an extension).
6035 But, that construct is not a cast-expression -- it is a
6036 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6037 is legal; if the compound-literal were a cast-expression,
6038 you'd need an extra set of parentheses.) But, if we parse
6039 the type-id, and it happens to be a class-specifier, then we
6040 will commit to the parse at that point, because we cannot
6041 undo the action that is done when creating a new class. So,
6042 then we cannot back up and do a postfix-expression.
6043
6044 Therefore, we scan ahead to the closing `)', and check to see
6045 if the token after the `)' is a `{'. If so, we are not
6046 looking at a cast-expression.
6047
6048 Save tokens so that we can put them back. */
6049 cp_lexer_save_tokens (parser->lexer);
6050 /* Skip tokens until the next token is a closing parenthesis.
6051 If we find the closing `)', and the next token is a `{', then
6052 we are looking at a compound-literal. */
6053 compound_literal_p
6054 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6055 /*consume_paren=*/true)
6056 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6057 /* Roll back the tokens we skipped. */
6058 cp_lexer_rollback_tokens (parser->lexer);
6059 /* If we were looking at a compound-literal, simulate an error
6060 so that the call to cp_parser_parse_definitely below will
6061 fail. */
6062 if (compound_literal_p)
6063 cp_parser_simulate_error (parser);
6064 else
6065 {
6066 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6067 parser->in_type_id_in_expr_p = true;
6068 /* Look for the type-id. */
6069 type = cp_parser_type_id (parser);
6070 /* Look for the closing `)'. */
6071 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6072 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6073 }
6074
6075 /* Restore the saved message. */
6076 parser->type_definition_forbidden_message = saved_message;
6077
6078 /* At this point this can only be either a cast or a
6079 parenthesized ctor such as `(T ())' that looks like a cast to
6080 function returning T. */
6081 if (!cp_parser_error_occurred (parser)
6082 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6083 (parser->lexer)))
6084 {
6085 cp_parser_parse_definitely (parser);
6086 expr = cp_parser_cast_expression (parser,
6087 /*address_p=*/false,
6088 /*cast_p=*/true, pidk);
6089
6090 /* Warn about old-style casts, if so requested. */
6091 if (warn_old_style_cast
6092 && !in_system_header
6093 && !VOID_TYPE_P (type)
6094 && current_lang_name != lang_name_c)
6095 warning (OPT_Wold_style_cast, "use of old-style cast");
6096
6097 /* Only type conversions to integral or enumeration types
6098 can be used in constant-expressions. */
6099 if (!cast_valid_in_integral_constant_expression_p (type)
6100 && (cp_parser_non_integral_constant_expression
6101 (parser,
6102 "a cast to a type other than an integral or "
6103 "enumeration type")))
6104 return error_mark_node;
6105
6106 /* Perform the cast. */
6107 expr = build_c_cast (type, expr);
6108 return expr;
6109 }
6110 else
6111 cp_parser_abort_tentative_parse (parser);
6112 }
6113
6114 /* If we get here, then it's not a cast, so it must be a
6115 unary-expression. */
6116 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6117 }
6118
6119 /* Parse a binary expression of the general form:
6120
6121 pm-expression:
6122 cast-expression
6123 pm-expression .* cast-expression
6124 pm-expression ->* cast-expression
6125
6126 multiplicative-expression:
6127 pm-expression
6128 multiplicative-expression * pm-expression
6129 multiplicative-expression / pm-expression
6130 multiplicative-expression % pm-expression
6131
6132 additive-expression:
6133 multiplicative-expression
6134 additive-expression + multiplicative-expression
6135 additive-expression - multiplicative-expression
6136
6137 shift-expression:
6138 additive-expression
6139 shift-expression << additive-expression
6140 shift-expression >> additive-expression
6141
6142 relational-expression:
6143 shift-expression
6144 relational-expression < shift-expression
6145 relational-expression > shift-expression
6146 relational-expression <= shift-expression
6147 relational-expression >= shift-expression
6148
6149 GNU Extension:
6150
6151 relational-expression:
6152 relational-expression <? shift-expression
6153 relational-expression >? shift-expression
6154
6155 equality-expression:
6156 relational-expression
6157 equality-expression == relational-expression
6158 equality-expression != relational-expression
6159
6160 and-expression:
6161 equality-expression
6162 and-expression & equality-expression
6163
6164 exclusive-or-expression:
6165 and-expression
6166 exclusive-or-expression ^ and-expression
6167
6168 inclusive-or-expression:
6169 exclusive-or-expression
6170 inclusive-or-expression | exclusive-or-expression
6171
6172 logical-and-expression:
6173 inclusive-or-expression
6174 logical-and-expression && inclusive-or-expression
6175
6176 logical-or-expression:
6177 logical-and-expression
6178 logical-or-expression || logical-and-expression
6179
6180 All these are implemented with a single function like:
6181
6182 binary-expression:
6183 simple-cast-expression
6184 binary-expression <token> binary-expression
6185
6186 CAST_P is true if this expression is the target of a cast.
6187
6188 The binops_by_token map is used to get the tree codes for each <token> type.
6189 binary-expressions are associated according to a precedence table. */
6190
6191 #define TOKEN_PRECEDENCE(token) \
6192 (((token->type == CPP_GREATER \
6193 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6194 && !parser->greater_than_is_operator_p) \
6195 ? PREC_NOT_OPERATOR \
6196 : binops_by_token[token->type].prec)
6197
6198 static tree
6199 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6200 enum cp_parser_prec prec,
6201 cp_id_kind * pidk)
6202 {
6203 cp_parser_expression_stack stack;
6204 cp_parser_expression_stack_entry *sp = &stack[0];
6205 tree lhs, rhs;
6206 cp_token *token;
6207 enum tree_code tree_type, lhs_type, rhs_type;
6208 enum cp_parser_prec new_prec, lookahead_prec;
6209 bool overloaded_p;
6210
6211 /* Parse the first expression. */
6212 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6213 lhs_type = ERROR_MARK;
6214
6215 for (;;)
6216 {
6217 /* Get an operator token. */
6218 token = cp_lexer_peek_token (parser->lexer);
6219
6220 if (warn_cxx0x_compat
6221 && token->type == CPP_RSHIFT
6222 && !parser->greater_than_is_operator_p)
6223 {
6224 warning (OPT_Wc__0x_compat,
6225 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6226 &token->location);
6227 warning (OPT_Wc__0x_compat,
6228 "suggest parentheses around %<>>%> expression");
6229 }
6230
6231 new_prec = TOKEN_PRECEDENCE (token);
6232
6233 /* Popping an entry off the stack means we completed a subexpression:
6234 - either we found a token which is not an operator (`>' where it is not
6235 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6236 will happen repeatedly;
6237 - or, we found an operator which has lower priority. This is the case
6238 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6239 parsing `3 * 4'. */
6240 if (new_prec <= prec)
6241 {
6242 if (sp == stack)
6243 break;
6244 else
6245 goto pop;
6246 }
6247
6248 get_rhs:
6249 tree_type = binops_by_token[token->type].tree_type;
6250
6251 /* We used the operator token. */
6252 cp_lexer_consume_token (parser->lexer);
6253
6254 /* Extract another operand. It may be the RHS of this expression
6255 or the LHS of a new, higher priority expression. */
6256 rhs = cp_parser_simple_cast_expression (parser);
6257 rhs_type = ERROR_MARK;
6258
6259 /* Get another operator token. Look up its precedence to avoid
6260 building a useless (immediately popped) stack entry for common
6261 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6262 token = cp_lexer_peek_token (parser->lexer);
6263 lookahead_prec = TOKEN_PRECEDENCE (token);
6264 if (lookahead_prec > new_prec)
6265 {
6266 /* ... and prepare to parse the RHS of the new, higher priority
6267 expression. Since precedence levels on the stack are
6268 monotonically increasing, we do not have to care about
6269 stack overflows. */
6270 sp->prec = prec;
6271 sp->tree_type = tree_type;
6272 sp->lhs = lhs;
6273 sp->lhs_type = lhs_type;
6274 sp++;
6275 lhs = rhs;
6276 lhs_type = rhs_type;
6277 prec = new_prec;
6278 new_prec = lookahead_prec;
6279 goto get_rhs;
6280
6281 pop:
6282 /* If the stack is not empty, we have parsed into LHS the right side
6283 (`4' in the example above) of an expression we had suspended.
6284 We can use the information on the stack to recover the LHS (`3')
6285 from the stack together with the tree code (`MULT_EXPR'), and
6286 the precedence of the higher level subexpression
6287 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6288 which will be used to actually build the additive expression. */
6289 --sp;
6290 prec = sp->prec;
6291 tree_type = sp->tree_type;
6292 rhs = lhs;
6293 rhs_type = lhs_type;
6294 lhs = sp->lhs;
6295 lhs_type = sp->lhs_type;
6296 }
6297
6298 overloaded_p = false;
6299 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6300 ERROR_MARK for everything that is not a binary expression.
6301 This makes warn_about_parentheses miss some warnings that
6302 involve unary operators. For unary expressions we should
6303 pass the correct tree_code unless the unary expression was
6304 surrounded by parentheses.
6305 */
6306 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6307 &overloaded_p, tf_warning_or_error);
6308 lhs_type = tree_type;
6309
6310 /* If the binary operator required the use of an overloaded operator,
6311 then this expression cannot be an integral constant-expression.
6312 An overloaded operator can be used even if both operands are
6313 otherwise permissible in an integral constant-expression if at
6314 least one of the operands is of enumeration type. */
6315
6316 if (overloaded_p
6317 && (cp_parser_non_integral_constant_expression
6318 (parser, "calls to overloaded operators")))
6319 return error_mark_node;
6320 }
6321
6322 return lhs;
6323 }
6324
6325
6326 /* Parse the `? expression : assignment-expression' part of a
6327 conditional-expression. The LOGICAL_OR_EXPR is the
6328 logical-or-expression that started the conditional-expression.
6329 Returns a representation of the entire conditional-expression.
6330
6331 This routine is used by cp_parser_assignment_expression.
6332
6333 ? expression : assignment-expression
6334
6335 GNU Extensions:
6336
6337 ? : assignment-expression */
6338
6339 static tree
6340 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6341 {
6342 tree expr;
6343 tree assignment_expr;
6344
6345 /* Consume the `?' token. */
6346 cp_lexer_consume_token (parser->lexer);
6347 if (cp_parser_allow_gnu_extensions_p (parser)
6348 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6349 /* Implicit true clause. */
6350 expr = NULL_TREE;
6351 else
6352 /* Parse the expression. */
6353 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6354
6355 /* The next token should be a `:'. */
6356 cp_parser_require (parser, CPP_COLON, "%<:%>");
6357 /* Parse the assignment-expression. */
6358 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6359
6360 /* Build the conditional-expression. */
6361 return build_x_conditional_expr (logical_or_expr,
6362 expr,
6363 assignment_expr,
6364 tf_warning_or_error);
6365 }
6366
6367 /* Parse an assignment-expression.
6368
6369 assignment-expression:
6370 conditional-expression
6371 logical-or-expression assignment-operator assignment_expression
6372 throw-expression
6373
6374 CAST_P is true if this expression is the target of a cast.
6375
6376 Returns a representation for the expression. */
6377
6378 static tree
6379 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6380 cp_id_kind * pidk)
6381 {
6382 tree expr;
6383
6384 /* If the next token is the `throw' keyword, then we're looking at
6385 a throw-expression. */
6386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6387 expr = cp_parser_throw_expression (parser);
6388 /* Otherwise, it must be that we are looking at a
6389 logical-or-expression. */
6390 else
6391 {
6392 /* Parse the binary expressions (logical-or-expression). */
6393 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6394 /* If the next token is a `?' then we're actually looking at a
6395 conditional-expression. */
6396 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6397 return cp_parser_question_colon_clause (parser, expr);
6398 else
6399 {
6400 enum tree_code assignment_operator;
6401
6402 /* If it's an assignment-operator, we're using the second
6403 production. */
6404 assignment_operator
6405 = cp_parser_assignment_operator_opt (parser);
6406 if (assignment_operator != ERROR_MARK)
6407 {
6408 bool non_constant_p;
6409
6410 /* Parse the right-hand side of the assignment. */
6411 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6412
6413 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6414 maybe_warn_cpp0x ("extended initializer lists");
6415
6416 /* An assignment may not appear in a
6417 constant-expression. */
6418 if (cp_parser_non_integral_constant_expression (parser,
6419 "an assignment"))
6420 return error_mark_node;
6421 /* Build the assignment expression. */
6422 expr = build_x_modify_expr (expr,
6423 assignment_operator,
6424 rhs,
6425 tf_warning_or_error);
6426 }
6427 }
6428 }
6429
6430 return expr;
6431 }
6432
6433 /* Parse an (optional) assignment-operator.
6434
6435 assignment-operator: one of
6436 = *= /= %= += -= >>= <<= &= ^= |=
6437
6438 GNU Extension:
6439
6440 assignment-operator: one of
6441 <?= >?=
6442
6443 If the next token is an assignment operator, the corresponding tree
6444 code is returned, and the token is consumed. For example, for
6445 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6446 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6447 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6448 operator, ERROR_MARK is returned. */
6449
6450 static enum tree_code
6451 cp_parser_assignment_operator_opt (cp_parser* parser)
6452 {
6453 enum tree_code op;
6454 cp_token *token;
6455
6456 /* Peek at the next token. */
6457 token = cp_lexer_peek_token (parser->lexer);
6458
6459 switch (token->type)
6460 {
6461 case CPP_EQ:
6462 op = NOP_EXPR;
6463 break;
6464
6465 case CPP_MULT_EQ:
6466 op = MULT_EXPR;
6467 break;
6468
6469 case CPP_DIV_EQ:
6470 op = TRUNC_DIV_EXPR;
6471 break;
6472
6473 case CPP_MOD_EQ:
6474 op = TRUNC_MOD_EXPR;
6475 break;
6476
6477 case CPP_PLUS_EQ:
6478 op = PLUS_EXPR;
6479 break;
6480
6481 case CPP_MINUS_EQ:
6482 op = MINUS_EXPR;
6483 break;
6484
6485 case CPP_RSHIFT_EQ:
6486 op = RSHIFT_EXPR;
6487 break;
6488
6489 case CPP_LSHIFT_EQ:
6490 op = LSHIFT_EXPR;
6491 break;
6492
6493 case CPP_AND_EQ:
6494 op = BIT_AND_EXPR;
6495 break;
6496
6497 case CPP_XOR_EQ:
6498 op = BIT_XOR_EXPR;
6499 break;
6500
6501 case CPP_OR_EQ:
6502 op = BIT_IOR_EXPR;
6503 break;
6504
6505 default:
6506 /* Nothing else is an assignment operator. */
6507 op = ERROR_MARK;
6508 }
6509
6510 /* If it was an assignment operator, consume it. */
6511 if (op != ERROR_MARK)
6512 cp_lexer_consume_token (parser->lexer);
6513
6514 return op;
6515 }
6516
6517 /* Parse an expression.
6518
6519 expression:
6520 assignment-expression
6521 expression , assignment-expression
6522
6523 CAST_P is true if this expression is the target of a cast.
6524
6525 Returns a representation of the expression. */
6526
6527 static tree
6528 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6529 {
6530 tree expression = NULL_TREE;
6531
6532 while (true)
6533 {
6534 tree assignment_expression;
6535
6536 /* Parse the next assignment-expression. */
6537 assignment_expression
6538 = cp_parser_assignment_expression (parser, cast_p, pidk);
6539 /* If this is the first assignment-expression, we can just
6540 save it away. */
6541 if (!expression)
6542 expression = assignment_expression;
6543 else
6544 expression = build_x_compound_expr (expression,
6545 assignment_expression,
6546 tf_warning_or_error);
6547 /* If the next token is not a comma, then we are done with the
6548 expression. */
6549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6550 break;
6551 /* Consume the `,'. */
6552 cp_lexer_consume_token (parser->lexer);
6553 /* A comma operator cannot appear in a constant-expression. */
6554 if (cp_parser_non_integral_constant_expression (parser,
6555 "a comma operator"))
6556 expression = error_mark_node;
6557 }
6558
6559 return expression;
6560 }
6561
6562 /* Parse a constant-expression.
6563
6564 constant-expression:
6565 conditional-expression
6566
6567 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6568 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6569 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6570 is false, NON_CONSTANT_P should be NULL. */
6571
6572 static tree
6573 cp_parser_constant_expression (cp_parser* parser,
6574 bool allow_non_constant_p,
6575 bool *non_constant_p)
6576 {
6577 bool saved_integral_constant_expression_p;
6578 bool saved_allow_non_integral_constant_expression_p;
6579 bool saved_non_integral_constant_expression_p;
6580 tree expression;
6581
6582 /* It might seem that we could simply parse the
6583 conditional-expression, and then check to see if it were
6584 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6585 one that the compiler can figure out is constant, possibly after
6586 doing some simplifications or optimizations. The standard has a
6587 precise definition of constant-expression, and we must honor
6588 that, even though it is somewhat more restrictive.
6589
6590 For example:
6591
6592 int i[(2, 3)];
6593
6594 is not a legal declaration, because `(2, 3)' is not a
6595 constant-expression. The `,' operator is forbidden in a
6596 constant-expression. However, GCC's constant-folding machinery
6597 will fold this operation to an INTEGER_CST for `3'. */
6598
6599 /* Save the old settings. */
6600 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6601 saved_allow_non_integral_constant_expression_p
6602 = parser->allow_non_integral_constant_expression_p;
6603 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6604 /* We are now parsing a constant-expression. */
6605 parser->integral_constant_expression_p = true;
6606 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6607 parser->non_integral_constant_expression_p = false;
6608 /* Although the grammar says "conditional-expression", we parse an
6609 "assignment-expression", which also permits "throw-expression"
6610 and the use of assignment operators. In the case that
6611 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6612 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6613 actually essential that we look for an assignment-expression.
6614 For example, cp_parser_initializer_clauses uses this function to
6615 determine whether a particular assignment-expression is in fact
6616 constant. */
6617 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6618 /* Restore the old settings. */
6619 parser->integral_constant_expression_p
6620 = saved_integral_constant_expression_p;
6621 parser->allow_non_integral_constant_expression_p
6622 = saved_allow_non_integral_constant_expression_p;
6623 if (allow_non_constant_p)
6624 *non_constant_p = parser->non_integral_constant_expression_p;
6625 else if (parser->non_integral_constant_expression_p)
6626 expression = error_mark_node;
6627 parser->non_integral_constant_expression_p
6628 = saved_non_integral_constant_expression_p;
6629
6630 return expression;
6631 }
6632
6633 /* Parse __builtin_offsetof.
6634
6635 offsetof-expression:
6636 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6637
6638 offsetof-member-designator:
6639 id-expression
6640 | offsetof-member-designator "." id-expression
6641 | offsetof-member-designator "[" expression "]"
6642 | offsetof-member-designator "->" id-expression */
6643
6644 static tree
6645 cp_parser_builtin_offsetof (cp_parser *parser)
6646 {
6647 int save_ice_p, save_non_ice_p;
6648 tree type, expr;
6649 cp_id_kind dummy;
6650 cp_token *token;
6651
6652 /* We're about to accept non-integral-constant things, but will
6653 definitely yield an integral constant expression. Save and
6654 restore these values around our local parsing. */
6655 save_ice_p = parser->integral_constant_expression_p;
6656 save_non_ice_p = parser->non_integral_constant_expression_p;
6657
6658 /* Consume the "__builtin_offsetof" token. */
6659 cp_lexer_consume_token (parser->lexer);
6660 /* Consume the opening `('. */
6661 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6662 /* Parse the type-id. */
6663 type = cp_parser_type_id (parser);
6664 /* Look for the `,'. */
6665 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6666 token = cp_lexer_peek_token (parser->lexer);
6667
6668 /* Build the (type *)null that begins the traditional offsetof macro. */
6669 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6670 tf_warning_or_error);
6671
6672 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6673 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6674 true, &dummy, token->location);
6675 while (true)
6676 {
6677 token = cp_lexer_peek_token (parser->lexer);
6678 switch (token->type)
6679 {
6680 case CPP_OPEN_SQUARE:
6681 /* offsetof-member-designator "[" expression "]" */
6682 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6683 break;
6684
6685 case CPP_DEREF:
6686 /* offsetof-member-designator "->" identifier */
6687 expr = grok_array_decl (expr, integer_zero_node);
6688 /* FALLTHRU */
6689
6690 case CPP_DOT:
6691 /* offsetof-member-designator "." identifier */
6692 cp_lexer_consume_token (parser->lexer);
6693 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6694 expr, true, &dummy,
6695 token->location);
6696 break;
6697
6698 case CPP_CLOSE_PAREN:
6699 /* Consume the ")" token. */
6700 cp_lexer_consume_token (parser->lexer);
6701 goto success;
6702
6703 default:
6704 /* Error. We know the following require will fail, but
6705 that gives the proper error message. */
6706 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6707 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6708 expr = error_mark_node;
6709 goto failure;
6710 }
6711 }
6712
6713 success:
6714 /* If we're processing a template, we can't finish the semantics yet.
6715 Otherwise we can fold the entire expression now. */
6716 if (processing_template_decl)
6717 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6718 else
6719 expr = finish_offsetof (expr);
6720
6721 failure:
6722 parser->integral_constant_expression_p = save_ice_p;
6723 parser->non_integral_constant_expression_p = save_non_ice_p;
6724
6725 return expr;
6726 }
6727
6728 /* Parse a trait expression. */
6729
6730 static tree
6731 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6732 {
6733 cp_trait_kind kind;
6734 tree type1, type2 = NULL_TREE;
6735 bool binary = false;
6736 cp_decl_specifier_seq decl_specs;
6737
6738 switch (keyword)
6739 {
6740 case RID_HAS_NOTHROW_ASSIGN:
6741 kind = CPTK_HAS_NOTHROW_ASSIGN;
6742 break;
6743 case RID_HAS_NOTHROW_CONSTRUCTOR:
6744 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6745 break;
6746 case RID_HAS_NOTHROW_COPY:
6747 kind = CPTK_HAS_NOTHROW_COPY;
6748 break;
6749 case RID_HAS_TRIVIAL_ASSIGN:
6750 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6751 break;
6752 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6753 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6754 break;
6755 case RID_HAS_TRIVIAL_COPY:
6756 kind = CPTK_HAS_TRIVIAL_COPY;
6757 break;
6758 case RID_HAS_TRIVIAL_DESTRUCTOR:
6759 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6760 break;
6761 case RID_HAS_VIRTUAL_DESTRUCTOR:
6762 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6763 break;
6764 case RID_IS_ABSTRACT:
6765 kind = CPTK_IS_ABSTRACT;
6766 break;
6767 case RID_IS_BASE_OF:
6768 kind = CPTK_IS_BASE_OF;
6769 binary = true;
6770 break;
6771 case RID_IS_CLASS:
6772 kind = CPTK_IS_CLASS;
6773 break;
6774 case RID_IS_CONVERTIBLE_TO:
6775 kind = CPTK_IS_CONVERTIBLE_TO;
6776 binary = true;
6777 break;
6778 case RID_IS_EMPTY:
6779 kind = CPTK_IS_EMPTY;
6780 break;
6781 case RID_IS_ENUM:
6782 kind = CPTK_IS_ENUM;
6783 break;
6784 case RID_IS_POD:
6785 kind = CPTK_IS_POD;
6786 break;
6787 case RID_IS_POLYMORPHIC:
6788 kind = CPTK_IS_POLYMORPHIC;
6789 break;
6790 case RID_IS_UNION:
6791 kind = CPTK_IS_UNION;
6792 break;
6793 default:
6794 gcc_unreachable ();
6795 }
6796
6797 /* Consume the token. */
6798 cp_lexer_consume_token (parser->lexer);
6799
6800 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6801
6802 type1 = cp_parser_type_id (parser);
6803
6804 if (type1 == error_mark_node)
6805 return error_mark_node;
6806
6807 /* Build a trivial decl-specifier-seq. */
6808 clear_decl_specs (&decl_specs);
6809 decl_specs.type = type1;
6810
6811 /* Call grokdeclarator to figure out what type this is. */
6812 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6813 /*initialized=*/0, /*attrlist=*/NULL);
6814
6815 if (binary)
6816 {
6817 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6818
6819 type2 = cp_parser_type_id (parser);
6820
6821 if (type2 == error_mark_node)
6822 return error_mark_node;
6823
6824 /* Build a trivial decl-specifier-seq. */
6825 clear_decl_specs (&decl_specs);
6826 decl_specs.type = type2;
6827
6828 /* Call grokdeclarator to figure out what type this is. */
6829 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6830 /*initialized=*/0, /*attrlist=*/NULL);
6831 }
6832
6833 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6834
6835 /* Complete the trait expression, which may mean either processing
6836 the trait expr now or saving it for template instantiation. */
6837 return finish_trait_expr (kind, type1, type2);
6838 }
6839
6840 /* Statements [gram.stmt.stmt] */
6841
6842 /* Parse a statement.
6843
6844 statement:
6845 labeled-statement
6846 expression-statement
6847 compound-statement
6848 selection-statement
6849 iteration-statement
6850 jump-statement
6851 declaration-statement
6852 try-block
6853
6854 IN_COMPOUND is true when the statement is nested inside a
6855 cp_parser_compound_statement; this matters for certain pragmas.
6856
6857 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6858 is a (possibly labeled) if statement which is not enclosed in braces
6859 and has an else clause. This is used to implement -Wparentheses. */
6860
6861 static void
6862 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6863 bool in_compound, bool *if_p)
6864 {
6865 tree statement;
6866 cp_token *token;
6867 location_t statement_location;
6868
6869 restart:
6870 if (if_p != NULL)
6871 *if_p = false;
6872 /* There is no statement yet. */
6873 statement = NULL_TREE;
6874 /* Peek at the next token. */
6875 token = cp_lexer_peek_token (parser->lexer);
6876 /* Remember the location of the first token in the statement. */
6877 statement_location = token->location;
6878 /* If this is a keyword, then that will often determine what kind of
6879 statement we have. */
6880 if (token->type == CPP_KEYWORD)
6881 {
6882 enum rid keyword = token->keyword;
6883
6884 switch (keyword)
6885 {
6886 case RID_CASE:
6887 case RID_DEFAULT:
6888 /* Looks like a labeled-statement with a case label.
6889 Parse the label, and then use tail recursion to parse
6890 the statement. */
6891 cp_parser_label_for_labeled_statement (parser);
6892 goto restart;
6893
6894 case RID_IF:
6895 case RID_SWITCH:
6896 statement = cp_parser_selection_statement (parser, if_p);
6897 break;
6898
6899 case RID_WHILE:
6900 case RID_DO:
6901 case RID_FOR:
6902 statement = cp_parser_iteration_statement (parser);
6903 break;
6904
6905 case RID_BREAK:
6906 case RID_CONTINUE:
6907 case RID_RETURN:
6908 case RID_GOTO:
6909 statement = cp_parser_jump_statement (parser);
6910 break;
6911
6912 /* Objective-C++ exception-handling constructs. */
6913 case RID_AT_TRY:
6914 case RID_AT_CATCH:
6915 case RID_AT_FINALLY:
6916 case RID_AT_SYNCHRONIZED:
6917 case RID_AT_THROW:
6918 statement = cp_parser_objc_statement (parser);
6919 break;
6920
6921 case RID_TRY:
6922 statement = cp_parser_try_block (parser);
6923 break;
6924
6925 case RID_NAMESPACE:
6926 /* This must be a namespace alias definition. */
6927 cp_parser_declaration_statement (parser);
6928 return;
6929
6930 default:
6931 /* It might be a keyword like `int' that can start a
6932 declaration-statement. */
6933 break;
6934 }
6935 }
6936 else if (token->type == CPP_NAME)
6937 {
6938 /* If the next token is a `:', then we are looking at a
6939 labeled-statement. */
6940 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6941 if (token->type == CPP_COLON)
6942 {
6943 /* Looks like a labeled-statement with an ordinary label.
6944 Parse the label, and then use tail recursion to parse
6945 the statement. */
6946 cp_parser_label_for_labeled_statement (parser);
6947 goto restart;
6948 }
6949 }
6950 /* Anything that starts with a `{' must be a compound-statement. */
6951 else if (token->type == CPP_OPEN_BRACE)
6952 statement = cp_parser_compound_statement (parser, NULL, false);
6953 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6954 a statement all its own. */
6955 else if (token->type == CPP_PRAGMA)
6956 {
6957 /* Only certain OpenMP pragmas are attached to statements, and thus
6958 are considered statements themselves. All others are not. In
6959 the context of a compound, accept the pragma as a "statement" and
6960 return so that we can check for a close brace. Otherwise we
6961 require a real statement and must go back and read one. */
6962 if (in_compound)
6963 cp_parser_pragma (parser, pragma_compound);
6964 else if (!cp_parser_pragma (parser, pragma_stmt))
6965 goto restart;
6966 return;
6967 }
6968 else if (token->type == CPP_EOF)
6969 {
6970 cp_parser_error (parser, "expected statement");
6971 return;
6972 }
6973
6974 /* Everything else must be a declaration-statement or an
6975 expression-statement. Try for the declaration-statement
6976 first, unless we are looking at a `;', in which case we know that
6977 we have an expression-statement. */
6978 if (!statement)
6979 {
6980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6981 {
6982 cp_parser_parse_tentatively (parser);
6983 /* Try to parse the declaration-statement. */
6984 cp_parser_declaration_statement (parser);
6985 /* If that worked, we're done. */
6986 if (cp_parser_parse_definitely (parser))
6987 return;
6988 }
6989 /* Look for an expression-statement instead. */
6990 statement = cp_parser_expression_statement (parser, in_statement_expr);
6991 }
6992
6993 /* Set the line number for the statement. */
6994 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6995 SET_EXPR_LOCATION (statement, statement_location);
6996 }
6997
6998 /* Parse the label for a labeled-statement, i.e.
6999
7000 identifier :
7001 case constant-expression :
7002 default :
7003
7004 GNU Extension:
7005 case constant-expression ... constant-expression : statement
7006
7007 When a label is parsed without errors, the label is added to the
7008 parse tree by the finish_* functions, so this function doesn't
7009 have to return the label. */
7010
7011 static void
7012 cp_parser_label_for_labeled_statement (cp_parser* parser)
7013 {
7014 cp_token *token;
7015
7016 /* The next token should be an identifier. */
7017 token = cp_lexer_peek_token (parser->lexer);
7018 if (token->type != CPP_NAME
7019 && token->type != CPP_KEYWORD)
7020 {
7021 cp_parser_error (parser, "expected labeled-statement");
7022 return;
7023 }
7024
7025 switch (token->keyword)
7026 {
7027 case RID_CASE:
7028 {
7029 tree expr, expr_hi;
7030 cp_token *ellipsis;
7031
7032 /* Consume the `case' token. */
7033 cp_lexer_consume_token (parser->lexer);
7034 /* Parse the constant-expression. */
7035 expr = cp_parser_constant_expression (parser,
7036 /*allow_non_constant_p=*/false,
7037 NULL);
7038
7039 ellipsis = cp_lexer_peek_token (parser->lexer);
7040 if (ellipsis->type == CPP_ELLIPSIS)
7041 {
7042 /* Consume the `...' token. */
7043 cp_lexer_consume_token (parser->lexer);
7044 expr_hi =
7045 cp_parser_constant_expression (parser,
7046 /*allow_non_constant_p=*/false,
7047 NULL);
7048 /* We don't need to emit warnings here, as the common code
7049 will do this for us. */
7050 }
7051 else
7052 expr_hi = NULL_TREE;
7053
7054 if (parser->in_switch_statement_p)
7055 finish_case_label (expr, expr_hi);
7056 else
7057 error ("%Hcase label %qE not within a switch statement",
7058 &token->location, expr);
7059 }
7060 break;
7061
7062 case RID_DEFAULT:
7063 /* Consume the `default' token. */
7064 cp_lexer_consume_token (parser->lexer);
7065
7066 if (parser->in_switch_statement_p)
7067 finish_case_label (NULL_TREE, NULL_TREE);
7068 else
7069 error ("%Hcase label not within a switch statement", &token->location);
7070 break;
7071
7072 default:
7073 /* Anything else must be an ordinary label. */
7074 finish_label_stmt (cp_parser_identifier (parser));
7075 break;
7076 }
7077
7078 /* Require the `:' token. */
7079 cp_parser_require (parser, CPP_COLON, "%<:%>");
7080 }
7081
7082 /* Parse an expression-statement.
7083
7084 expression-statement:
7085 expression [opt] ;
7086
7087 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7088 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7089 indicates whether this expression-statement is part of an
7090 expression statement. */
7091
7092 static tree
7093 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7094 {
7095 tree statement = NULL_TREE;
7096
7097 /* If the next token is a ';', then there is no expression
7098 statement. */
7099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7100 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7101
7102 /* Consume the final `;'. */
7103 cp_parser_consume_semicolon_at_end_of_statement (parser);
7104
7105 if (in_statement_expr
7106 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7107 /* This is the final expression statement of a statement
7108 expression. */
7109 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7110 else if (statement)
7111 statement = finish_expr_stmt (statement);
7112 else
7113 finish_stmt ();
7114
7115 return statement;
7116 }
7117
7118 /* Parse a compound-statement.
7119
7120 compound-statement:
7121 { statement-seq [opt] }
7122
7123 GNU extension:
7124
7125 compound-statement:
7126 { label-declaration-seq [opt] statement-seq [opt] }
7127
7128 label-declaration-seq:
7129 label-declaration
7130 label-declaration-seq label-declaration
7131
7132 Returns a tree representing the statement. */
7133
7134 static tree
7135 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7136 bool in_try)
7137 {
7138 tree compound_stmt;
7139
7140 /* Consume the `{'. */
7141 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7142 return error_mark_node;
7143 /* Begin the compound-statement. */
7144 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7145 /* If the next keyword is `__label__' we have a label declaration. */
7146 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7147 cp_parser_label_declaration (parser);
7148 /* Parse an (optional) statement-seq. */
7149 cp_parser_statement_seq_opt (parser, in_statement_expr);
7150 /* Finish the compound-statement. */
7151 finish_compound_stmt (compound_stmt);
7152 /* Consume the `}'. */
7153 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7154
7155 return compound_stmt;
7156 }
7157
7158 /* Parse an (optional) statement-seq.
7159
7160 statement-seq:
7161 statement
7162 statement-seq [opt] statement */
7163
7164 static void
7165 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7166 {
7167 /* Scan statements until there aren't any more. */
7168 while (true)
7169 {
7170 cp_token *token = cp_lexer_peek_token (parser->lexer);
7171
7172 /* If we're looking at a `}', then we've run out of statements. */
7173 if (token->type == CPP_CLOSE_BRACE
7174 || token->type == CPP_EOF
7175 || token->type == CPP_PRAGMA_EOL)
7176 break;
7177
7178 /* If we are in a compound statement and find 'else' then
7179 something went wrong. */
7180 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7181 {
7182 if (parser->in_statement & IN_IF_STMT)
7183 break;
7184 else
7185 {
7186 token = cp_lexer_consume_token (parser->lexer);
7187 error ("%H%<else%> without a previous %<if%>", &token->location);
7188 }
7189 }
7190
7191 /* Parse the statement. */
7192 cp_parser_statement (parser, in_statement_expr, true, NULL);
7193 }
7194 }
7195
7196 /* Parse a selection-statement.
7197
7198 selection-statement:
7199 if ( condition ) statement
7200 if ( condition ) statement else statement
7201 switch ( condition ) statement
7202
7203 Returns the new IF_STMT or SWITCH_STMT.
7204
7205 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7206 is a (possibly labeled) if statement which is not enclosed in
7207 braces and has an else clause. This is used to implement
7208 -Wparentheses. */
7209
7210 static tree
7211 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7212 {
7213 cp_token *token;
7214 enum rid keyword;
7215
7216 if (if_p != NULL)
7217 *if_p = false;
7218
7219 /* Peek at the next token. */
7220 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7221
7222 /* See what kind of keyword it is. */
7223 keyword = token->keyword;
7224 switch (keyword)
7225 {
7226 case RID_IF:
7227 case RID_SWITCH:
7228 {
7229 tree statement;
7230 tree condition;
7231
7232 /* Look for the `('. */
7233 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7234 {
7235 cp_parser_skip_to_end_of_statement (parser);
7236 return error_mark_node;
7237 }
7238
7239 /* Begin the selection-statement. */
7240 if (keyword == RID_IF)
7241 statement = begin_if_stmt ();
7242 else
7243 statement = begin_switch_stmt ();
7244
7245 /* Parse the condition. */
7246 condition = cp_parser_condition (parser);
7247 /* Look for the `)'. */
7248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7249 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7250 /*consume_paren=*/true);
7251
7252 if (keyword == RID_IF)
7253 {
7254 bool nested_if;
7255 unsigned char in_statement;
7256
7257 /* Add the condition. */
7258 finish_if_stmt_cond (condition, statement);
7259
7260 /* Parse the then-clause. */
7261 in_statement = parser->in_statement;
7262 parser->in_statement |= IN_IF_STMT;
7263 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7264 {
7265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7266 add_stmt (build_empty_stmt ());
7267 cp_lexer_consume_token (parser->lexer);
7268 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7269 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7270 "empty body in an %<if%> statement");
7271 nested_if = false;
7272 }
7273 else
7274 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7275 parser->in_statement = in_statement;
7276
7277 finish_then_clause (statement);
7278
7279 /* If the next token is `else', parse the else-clause. */
7280 if (cp_lexer_next_token_is_keyword (parser->lexer,
7281 RID_ELSE))
7282 {
7283 /* Consume the `else' keyword. */
7284 cp_lexer_consume_token (parser->lexer);
7285 begin_else_clause (statement);
7286 /* Parse the else-clause. */
7287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7288 {
7289 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7290 OPT_Wempty_body, "suggest braces around "
7291 "empty body in an %<else%> statement");
7292 add_stmt (build_empty_stmt ());
7293 cp_lexer_consume_token (parser->lexer);
7294 }
7295 else
7296 cp_parser_implicitly_scoped_statement (parser, NULL);
7297
7298 finish_else_clause (statement);
7299
7300 /* If we are currently parsing a then-clause, then
7301 IF_P will not be NULL. We set it to true to
7302 indicate that this if statement has an else clause.
7303 This may trigger the Wparentheses warning below
7304 when we get back up to the parent if statement. */
7305 if (if_p != NULL)
7306 *if_p = true;
7307 }
7308 else
7309 {
7310 /* This if statement does not have an else clause. If
7311 NESTED_IF is true, then the then-clause is an if
7312 statement which does have an else clause. We warn
7313 about the potential ambiguity. */
7314 if (nested_if)
7315 warning (OPT_Wparentheses,
7316 ("%Hsuggest explicit braces "
7317 "to avoid ambiguous %<else%>"),
7318 EXPR_LOCUS (statement));
7319 }
7320
7321 /* Now we're all done with the if-statement. */
7322 finish_if_stmt (statement);
7323 }
7324 else
7325 {
7326 bool in_switch_statement_p;
7327 unsigned char in_statement;
7328
7329 /* Add the condition. */
7330 finish_switch_cond (condition, statement);
7331
7332 /* Parse the body of the switch-statement. */
7333 in_switch_statement_p = parser->in_switch_statement_p;
7334 in_statement = parser->in_statement;
7335 parser->in_switch_statement_p = true;
7336 parser->in_statement |= IN_SWITCH_STMT;
7337 cp_parser_implicitly_scoped_statement (parser, NULL);
7338 parser->in_switch_statement_p = in_switch_statement_p;
7339 parser->in_statement = in_statement;
7340
7341 /* Now we're all done with the switch-statement. */
7342 finish_switch_stmt (statement);
7343 }
7344
7345 return statement;
7346 }
7347 break;
7348
7349 default:
7350 cp_parser_error (parser, "expected selection-statement");
7351 return error_mark_node;
7352 }
7353 }
7354
7355 /* Parse a condition.
7356
7357 condition:
7358 expression
7359 type-specifier-seq declarator = initializer-clause
7360 type-specifier-seq declarator braced-init-list
7361
7362 GNU Extension:
7363
7364 condition:
7365 type-specifier-seq declarator asm-specification [opt]
7366 attributes [opt] = assignment-expression
7367
7368 Returns the expression that should be tested. */
7369
7370 static tree
7371 cp_parser_condition (cp_parser* parser)
7372 {
7373 cp_decl_specifier_seq type_specifiers;
7374 const char *saved_message;
7375
7376 /* Try the declaration first. */
7377 cp_parser_parse_tentatively (parser);
7378 /* New types are not allowed in the type-specifier-seq for a
7379 condition. */
7380 saved_message = parser->type_definition_forbidden_message;
7381 parser->type_definition_forbidden_message
7382 = "types may not be defined in conditions";
7383 /* Parse the type-specifier-seq. */
7384 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7385 &type_specifiers);
7386 /* Restore the saved message. */
7387 parser->type_definition_forbidden_message = saved_message;
7388 /* If all is well, we might be looking at a declaration. */
7389 if (!cp_parser_error_occurred (parser))
7390 {
7391 tree decl;
7392 tree asm_specification;
7393 tree attributes;
7394 cp_declarator *declarator;
7395 tree initializer = NULL_TREE;
7396
7397 /* Parse the declarator. */
7398 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7399 /*ctor_dtor_or_conv_p=*/NULL,
7400 /*parenthesized_p=*/NULL,
7401 /*member_p=*/false);
7402 /* Parse the attributes. */
7403 attributes = cp_parser_attributes_opt (parser);
7404 /* Parse the asm-specification. */
7405 asm_specification = cp_parser_asm_specification_opt (parser);
7406 /* If the next token is not an `=' or '{', then we might still be
7407 looking at an expression. For example:
7408
7409 if (A(a).x)
7410
7411 looks like a decl-specifier-seq and a declarator -- but then
7412 there is no `=', so this is an expression. */
7413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7414 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7415 cp_parser_simulate_error (parser);
7416
7417 /* If we did see an `=' or '{', then we are looking at a declaration
7418 for sure. */
7419 if (cp_parser_parse_definitely (parser))
7420 {
7421 tree pushed_scope;
7422 bool non_constant_p;
7423 bool flags = LOOKUP_ONLYCONVERTING;
7424
7425 /* Create the declaration. */
7426 decl = start_decl (declarator, &type_specifiers,
7427 /*initialized_p=*/true,
7428 attributes, /*prefix_attributes=*/NULL_TREE,
7429 &pushed_scope);
7430
7431 /* Parse the initializer. */
7432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7433 {
7434 initializer = cp_parser_braced_list (parser, &non_constant_p);
7435 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7436 flags = 0;
7437 }
7438 else
7439 {
7440 /* Consume the `='. */
7441 cp_parser_require (parser, CPP_EQ, "%<=%>");
7442 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7443 }
7444 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7445 maybe_warn_cpp0x ("extended initializer lists");
7446
7447 if (!non_constant_p)
7448 initializer = fold_non_dependent_expr (initializer);
7449
7450 /* Process the initializer. */
7451 cp_finish_decl (decl,
7452 initializer, !non_constant_p,
7453 asm_specification,
7454 flags);
7455
7456 if (pushed_scope)
7457 pop_scope (pushed_scope);
7458
7459 return convert_from_reference (decl);
7460 }
7461 }
7462 /* If we didn't even get past the declarator successfully, we are
7463 definitely not looking at a declaration. */
7464 else
7465 cp_parser_abort_tentative_parse (parser);
7466
7467 /* Otherwise, we are looking at an expression. */
7468 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7469 }
7470
7471 /* Parse an iteration-statement.
7472
7473 iteration-statement:
7474 while ( condition ) statement
7475 do statement while ( expression ) ;
7476 for ( for-init-statement condition [opt] ; expression [opt] )
7477 statement
7478
7479 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7480
7481 static tree
7482 cp_parser_iteration_statement (cp_parser* parser)
7483 {
7484 cp_token *token;
7485 enum rid keyword;
7486 tree statement;
7487 unsigned char in_statement;
7488
7489 /* Peek at the next token. */
7490 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7491 if (!token)
7492 return error_mark_node;
7493
7494 /* Remember whether or not we are already within an iteration
7495 statement. */
7496 in_statement = parser->in_statement;
7497
7498 /* See what kind of keyword it is. */
7499 keyword = token->keyword;
7500 switch (keyword)
7501 {
7502 case RID_WHILE:
7503 {
7504 tree condition;
7505
7506 /* Begin the while-statement. */
7507 statement = begin_while_stmt ();
7508 /* Look for the `('. */
7509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7510 /* Parse the condition. */
7511 condition = cp_parser_condition (parser);
7512 finish_while_stmt_cond (condition, statement);
7513 /* Look for the `)'. */
7514 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7515 /* Parse the dependent statement. */
7516 parser->in_statement = IN_ITERATION_STMT;
7517 cp_parser_already_scoped_statement (parser);
7518 parser->in_statement = in_statement;
7519 /* We're done with the while-statement. */
7520 finish_while_stmt (statement);
7521 }
7522 break;
7523
7524 case RID_DO:
7525 {
7526 tree expression;
7527
7528 /* Begin the do-statement. */
7529 statement = begin_do_stmt ();
7530 /* Parse the body of the do-statement. */
7531 parser->in_statement = IN_ITERATION_STMT;
7532 cp_parser_implicitly_scoped_statement (parser, NULL);
7533 parser->in_statement = in_statement;
7534 finish_do_body (statement);
7535 /* Look for the `while' keyword. */
7536 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7537 /* Look for the `('. */
7538 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7539 /* Parse the expression. */
7540 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7541 /* We're done with the do-statement. */
7542 finish_do_stmt (expression, statement);
7543 /* Look for the `)'. */
7544 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7545 /* Look for the `;'. */
7546 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7547 }
7548 break;
7549
7550 case RID_FOR:
7551 {
7552 tree condition = NULL_TREE;
7553 tree expression = NULL_TREE;
7554
7555 /* Begin the for-statement. */
7556 statement = begin_for_stmt ();
7557 /* Look for the `('. */
7558 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7559 /* Parse the initialization. */
7560 cp_parser_for_init_statement (parser);
7561 finish_for_init_stmt (statement);
7562
7563 /* If there's a condition, process it. */
7564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7565 condition = cp_parser_condition (parser);
7566 finish_for_cond (condition, statement);
7567 /* Look for the `;'. */
7568 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7569
7570 /* If there's an expression, process it. */
7571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7572 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7573 finish_for_expr (expression, statement);
7574 /* Look for the `)'. */
7575 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7576
7577 /* Parse the body of the for-statement. */
7578 parser->in_statement = IN_ITERATION_STMT;
7579 cp_parser_already_scoped_statement (parser);
7580 parser->in_statement = in_statement;
7581
7582 /* We're done with the for-statement. */
7583 finish_for_stmt (statement);
7584 }
7585 break;
7586
7587 default:
7588 cp_parser_error (parser, "expected iteration-statement");
7589 statement = error_mark_node;
7590 break;
7591 }
7592
7593 return statement;
7594 }
7595
7596 /* Parse a for-init-statement.
7597
7598 for-init-statement:
7599 expression-statement
7600 simple-declaration */
7601
7602 static void
7603 cp_parser_for_init_statement (cp_parser* parser)
7604 {
7605 /* If the next token is a `;', then we have an empty
7606 expression-statement. Grammatically, this is also a
7607 simple-declaration, but an invalid one, because it does not
7608 declare anything. Therefore, if we did not handle this case
7609 specially, we would issue an error message about an invalid
7610 declaration. */
7611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7612 {
7613 /* We're going to speculatively look for a declaration, falling back
7614 to an expression, if necessary. */
7615 cp_parser_parse_tentatively (parser);
7616 /* Parse the declaration. */
7617 cp_parser_simple_declaration (parser,
7618 /*function_definition_allowed_p=*/false);
7619 /* If the tentative parse failed, then we shall need to look for an
7620 expression-statement. */
7621 if (cp_parser_parse_definitely (parser))
7622 return;
7623 }
7624
7625 cp_parser_expression_statement (parser, false);
7626 }
7627
7628 /* Parse a jump-statement.
7629
7630 jump-statement:
7631 break ;
7632 continue ;
7633 return expression [opt] ;
7634 return braced-init-list ;
7635 goto identifier ;
7636
7637 GNU extension:
7638
7639 jump-statement:
7640 goto * expression ;
7641
7642 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7643
7644 static tree
7645 cp_parser_jump_statement (cp_parser* parser)
7646 {
7647 tree statement = error_mark_node;
7648 cp_token *token;
7649 enum rid keyword;
7650 unsigned char in_statement;
7651
7652 /* Peek at the next token. */
7653 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7654 if (!token)
7655 return error_mark_node;
7656
7657 /* See what kind of keyword it is. */
7658 keyword = token->keyword;
7659 switch (keyword)
7660 {
7661 case RID_BREAK:
7662 in_statement = parser->in_statement & ~IN_IF_STMT;
7663 switch (in_statement)
7664 {
7665 case 0:
7666 error ("%Hbreak statement not within loop or switch", &token->location);
7667 break;
7668 default:
7669 gcc_assert ((in_statement & IN_SWITCH_STMT)
7670 || in_statement == IN_ITERATION_STMT);
7671 statement = finish_break_stmt ();
7672 break;
7673 case IN_OMP_BLOCK:
7674 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7675 break;
7676 case IN_OMP_FOR:
7677 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7678 break;
7679 }
7680 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7681 break;
7682
7683 case RID_CONTINUE:
7684 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7685 {
7686 case 0:
7687 error ("%Hcontinue statement not within a loop", &token->location);
7688 break;
7689 case IN_ITERATION_STMT:
7690 case IN_OMP_FOR:
7691 statement = finish_continue_stmt ();
7692 break;
7693 case IN_OMP_BLOCK:
7694 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7695 break;
7696 default:
7697 gcc_unreachable ();
7698 }
7699 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7700 break;
7701
7702 case RID_RETURN:
7703 {
7704 tree expr;
7705 bool expr_non_constant_p;
7706
7707 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7708 {
7709 maybe_warn_cpp0x ("extended initializer lists");
7710 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7711 }
7712 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7713 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7714 else
7715 /* If the next token is a `;', then there is no
7716 expression. */
7717 expr = NULL_TREE;
7718 /* Build the return-statement. */
7719 statement = finish_return_stmt (expr);
7720 /* Look for the final `;'. */
7721 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7722 }
7723 break;
7724
7725 case RID_GOTO:
7726 /* Create the goto-statement. */
7727 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7728 {
7729 /* Issue a warning about this use of a GNU extension. */
7730 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7731 /* Consume the '*' token. */
7732 cp_lexer_consume_token (parser->lexer);
7733 /* Parse the dependent expression. */
7734 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7735 }
7736 else
7737 finish_goto_stmt (cp_parser_identifier (parser));
7738 /* Look for the final `;'. */
7739 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7740 break;
7741
7742 default:
7743 cp_parser_error (parser, "expected jump-statement");
7744 break;
7745 }
7746
7747 return statement;
7748 }
7749
7750 /* Parse a declaration-statement.
7751
7752 declaration-statement:
7753 block-declaration */
7754
7755 static void
7756 cp_parser_declaration_statement (cp_parser* parser)
7757 {
7758 void *p;
7759
7760 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7761 p = obstack_alloc (&declarator_obstack, 0);
7762
7763 /* Parse the block-declaration. */
7764 cp_parser_block_declaration (parser, /*statement_p=*/true);
7765
7766 /* Free any declarators allocated. */
7767 obstack_free (&declarator_obstack, p);
7768
7769 /* Finish off the statement. */
7770 finish_stmt ();
7771 }
7772
7773 /* Some dependent statements (like `if (cond) statement'), are
7774 implicitly in their own scope. In other words, if the statement is
7775 a single statement (as opposed to a compound-statement), it is
7776 none-the-less treated as if it were enclosed in braces. Any
7777 declarations appearing in the dependent statement are out of scope
7778 after control passes that point. This function parses a statement,
7779 but ensures that is in its own scope, even if it is not a
7780 compound-statement.
7781
7782 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7783 is a (possibly labeled) if statement which is not enclosed in
7784 braces and has an else clause. This is used to implement
7785 -Wparentheses.
7786
7787 Returns the new statement. */
7788
7789 static tree
7790 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7791 {
7792 tree statement;
7793
7794 if (if_p != NULL)
7795 *if_p = false;
7796
7797 /* Mark if () ; with a special NOP_EXPR. */
7798 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7799 {
7800 cp_lexer_consume_token (parser->lexer);
7801 statement = add_stmt (build_empty_stmt ());
7802 }
7803 /* if a compound is opened, we simply parse the statement directly. */
7804 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7805 statement = cp_parser_compound_statement (parser, NULL, false);
7806 /* If the token is not a `{', then we must take special action. */
7807 else
7808 {
7809 /* Create a compound-statement. */
7810 statement = begin_compound_stmt (0);
7811 /* Parse the dependent-statement. */
7812 cp_parser_statement (parser, NULL_TREE, false, if_p);
7813 /* Finish the dummy compound-statement. */
7814 finish_compound_stmt (statement);
7815 }
7816
7817 /* Return the statement. */
7818 return statement;
7819 }
7820
7821 /* For some dependent statements (like `while (cond) statement'), we
7822 have already created a scope. Therefore, even if the dependent
7823 statement is a compound-statement, we do not want to create another
7824 scope. */
7825
7826 static void
7827 cp_parser_already_scoped_statement (cp_parser* parser)
7828 {
7829 /* If the token is a `{', then we must take special action. */
7830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7831 cp_parser_statement (parser, NULL_TREE, false, NULL);
7832 else
7833 {
7834 /* Avoid calling cp_parser_compound_statement, so that we
7835 don't create a new scope. Do everything else by hand. */
7836 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7837 cp_parser_statement_seq_opt (parser, NULL_TREE);
7838 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7839 }
7840 }
7841
7842 /* Declarations [gram.dcl.dcl] */
7843
7844 /* Parse an optional declaration-sequence.
7845
7846 declaration-seq:
7847 declaration
7848 declaration-seq declaration */
7849
7850 static void
7851 cp_parser_declaration_seq_opt (cp_parser* parser)
7852 {
7853 while (true)
7854 {
7855 cp_token *token;
7856
7857 token = cp_lexer_peek_token (parser->lexer);
7858
7859 if (token->type == CPP_CLOSE_BRACE
7860 || token->type == CPP_EOF
7861 || token->type == CPP_PRAGMA_EOL)
7862 break;
7863
7864 if (token->type == CPP_SEMICOLON)
7865 {
7866 /* A declaration consisting of a single semicolon is
7867 invalid. Allow it unless we're being pedantic. */
7868 cp_lexer_consume_token (parser->lexer);
7869 if (!in_system_header)
7870 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7871 continue;
7872 }
7873
7874 /* If we're entering or exiting a region that's implicitly
7875 extern "C", modify the lang context appropriately. */
7876 if (!parser->implicit_extern_c && token->implicit_extern_c)
7877 {
7878 push_lang_context (lang_name_c);
7879 parser->implicit_extern_c = true;
7880 }
7881 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7882 {
7883 pop_lang_context ();
7884 parser->implicit_extern_c = false;
7885 }
7886
7887 if (token->type == CPP_PRAGMA)
7888 {
7889 /* A top-level declaration can consist solely of a #pragma.
7890 A nested declaration cannot, so this is done here and not
7891 in cp_parser_declaration. (A #pragma at block scope is
7892 handled in cp_parser_statement.) */
7893 cp_parser_pragma (parser, pragma_external);
7894 continue;
7895 }
7896
7897 /* Parse the declaration itself. */
7898 cp_parser_declaration (parser);
7899 }
7900 }
7901
7902 /* Parse a declaration.
7903
7904 declaration:
7905 block-declaration
7906 function-definition
7907 template-declaration
7908 explicit-instantiation
7909 explicit-specialization
7910 linkage-specification
7911 namespace-definition
7912
7913 GNU extension:
7914
7915 declaration:
7916 __extension__ declaration */
7917
7918 static void
7919 cp_parser_declaration (cp_parser* parser)
7920 {
7921 cp_token token1;
7922 cp_token token2;
7923 int saved_pedantic;
7924 void *p;
7925
7926 /* Check for the `__extension__' keyword. */
7927 if (cp_parser_extension_opt (parser, &saved_pedantic))
7928 {
7929 /* Parse the qualified declaration. */
7930 cp_parser_declaration (parser);
7931 /* Restore the PEDANTIC flag. */
7932 pedantic = saved_pedantic;
7933
7934 return;
7935 }
7936
7937 /* Try to figure out what kind of declaration is present. */
7938 token1 = *cp_lexer_peek_token (parser->lexer);
7939
7940 if (token1.type != CPP_EOF)
7941 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7942 else
7943 {
7944 token2.type = CPP_EOF;
7945 token2.keyword = RID_MAX;
7946 }
7947
7948 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7949 p = obstack_alloc (&declarator_obstack, 0);
7950
7951 /* If the next token is `extern' and the following token is a string
7952 literal, then we have a linkage specification. */
7953 if (token1.keyword == RID_EXTERN
7954 && cp_parser_is_string_literal (&token2))
7955 cp_parser_linkage_specification (parser);
7956 /* If the next token is `template', then we have either a template
7957 declaration, an explicit instantiation, or an explicit
7958 specialization. */
7959 else if (token1.keyword == RID_TEMPLATE)
7960 {
7961 /* `template <>' indicates a template specialization. */
7962 if (token2.type == CPP_LESS
7963 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7964 cp_parser_explicit_specialization (parser);
7965 /* `template <' indicates a template declaration. */
7966 else if (token2.type == CPP_LESS)
7967 cp_parser_template_declaration (parser, /*member_p=*/false);
7968 /* Anything else must be an explicit instantiation. */
7969 else
7970 cp_parser_explicit_instantiation (parser);
7971 }
7972 /* If the next token is `export', then we have a template
7973 declaration. */
7974 else if (token1.keyword == RID_EXPORT)
7975 cp_parser_template_declaration (parser, /*member_p=*/false);
7976 /* If the next token is `extern', 'static' or 'inline' and the one
7977 after that is `template', we have a GNU extended explicit
7978 instantiation directive. */
7979 else if (cp_parser_allow_gnu_extensions_p (parser)
7980 && (token1.keyword == RID_EXTERN
7981 || token1.keyword == RID_STATIC
7982 || token1.keyword == RID_INLINE)
7983 && token2.keyword == RID_TEMPLATE)
7984 cp_parser_explicit_instantiation (parser);
7985 /* If the next token is `namespace', check for a named or unnamed
7986 namespace definition. */
7987 else if (token1.keyword == RID_NAMESPACE
7988 && (/* A named namespace definition. */
7989 (token2.type == CPP_NAME
7990 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7991 != CPP_EQ))
7992 /* An unnamed namespace definition. */
7993 || token2.type == CPP_OPEN_BRACE
7994 || token2.keyword == RID_ATTRIBUTE))
7995 cp_parser_namespace_definition (parser);
7996 /* An inline (associated) namespace definition. */
7997 else if (token1.keyword == RID_INLINE
7998 && token2.keyword == RID_NAMESPACE)
7999 cp_parser_namespace_definition (parser);
8000 /* Objective-C++ declaration/definition. */
8001 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8002 cp_parser_objc_declaration (parser);
8003 /* We must have either a block declaration or a function
8004 definition. */
8005 else
8006 /* Try to parse a block-declaration, or a function-definition. */
8007 cp_parser_block_declaration (parser, /*statement_p=*/false);
8008
8009 /* Free any declarators allocated. */
8010 obstack_free (&declarator_obstack, p);
8011 }
8012
8013 /* Parse a block-declaration.
8014
8015 block-declaration:
8016 simple-declaration
8017 asm-definition
8018 namespace-alias-definition
8019 using-declaration
8020 using-directive
8021
8022 GNU Extension:
8023
8024 block-declaration:
8025 __extension__ block-declaration
8026
8027 C++0x Extension:
8028
8029 block-declaration:
8030 static_assert-declaration
8031
8032 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8033 part of a declaration-statement. */
8034
8035 static void
8036 cp_parser_block_declaration (cp_parser *parser,
8037 bool statement_p)
8038 {
8039 cp_token *token1;
8040 int saved_pedantic;
8041
8042 /* Check for the `__extension__' keyword. */
8043 if (cp_parser_extension_opt (parser, &saved_pedantic))
8044 {
8045 /* Parse the qualified declaration. */
8046 cp_parser_block_declaration (parser, statement_p);
8047 /* Restore the PEDANTIC flag. */
8048 pedantic = saved_pedantic;
8049
8050 return;
8051 }
8052
8053 /* Peek at the next token to figure out which kind of declaration is
8054 present. */
8055 token1 = cp_lexer_peek_token (parser->lexer);
8056
8057 /* If the next keyword is `asm', we have an asm-definition. */
8058 if (token1->keyword == RID_ASM)
8059 {
8060 if (statement_p)
8061 cp_parser_commit_to_tentative_parse (parser);
8062 cp_parser_asm_definition (parser);
8063 }
8064 /* If the next keyword is `namespace', we have a
8065 namespace-alias-definition. */
8066 else if (token1->keyword == RID_NAMESPACE)
8067 cp_parser_namespace_alias_definition (parser);
8068 /* If the next keyword is `using', we have either a
8069 using-declaration or a using-directive. */
8070 else if (token1->keyword == RID_USING)
8071 {
8072 cp_token *token2;
8073
8074 if (statement_p)
8075 cp_parser_commit_to_tentative_parse (parser);
8076 /* If the token after `using' is `namespace', then we have a
8077 using-directive. */
8078 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8079 if (token2->keyword == RID_NAMESPACE)
8080 cp_parser_using_directive (parser);
8081 /* Otherwise, it's a using-declaration. */
8082 else
8083 cp_parser_using_declaration (parser,
8084 /*access_declaration_p=*/false);
8085 }
8086 /* If the next keyword is `__label__' we have a misplaced label
8087 declaration. */
8088 else if (token1->keyword == RID_LABEL)
8089 {
8090 cp_lexer_consume_token (parser->lexer);
8091 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8092 cp_parser_skip_to_end_of_statement (parser);
8093 /* If the next token is now a `;', consume it. */
8094 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8095 cp_lexer_consume_token (parser->lexer);
8096 }
8097 /* If the next token is `static_assert' we have a static assertion. */
8098 else if (token1->keyword == RID_STATIC_ASSERT)
8099 cp_parser_static_assert (parser, /*member_p=*/false);
8100 /* Anything else must be a simple-declaration. */
8101 else
8102 cp_parser_simple_declaration (parser, !statement_p);
8103 }
8104
8105 /* Parse a simple-declaration.
8106
8107 simple-declaration:
8108 decl-specifier-seq [opt] init-declarator-list [opt] ;
8109
8110 init-declarator-list:
8111 init-declarator
8112 init-declarator-list , init-declarator
8113
8114 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8115 function-definition as a simple-declaration. */
8116
8117 static void
8118 cp_parser_simple_declaration (cp_parser* parser,
8119 bool function_definition_allowed_p)
8120 {
8121 cp_decl_specifier_seq decl_specifiers;
8122 int declares_class_or_enum;
8123 bool saw_declarator;
8124
8125 /* Defer access checks until we know what is being declared; the
8126 checks for names appearing in the decl-specifier-seq should be
8127 done as if we were in the scope of the thing being declared. */
8128 push_deferring_access_checks (dk_deferred);
8129
8130 /* Parse the decl-specifier-seq. We have to keep track of whether
8131 or not the decl-specifier-seq declares a named class or
8132 enumeration type, since that is the only case in which the
8133 init-declarator-list is allowed to be empty.
8134
8135 [dcl.dcl]
8136
8137 In a simple-declaration, the optional init-declarator-list can be
8138 omitted only when declaring a class or enumeration, that is when
8139 the decl-specifier-seq contains either a class-specifier, an
8140 elaborated-type-specifier, or an enum-specifier. */
8141 cp_parser_decl_specifier_seq (parser,
8142 CP_PARSER_FLAGS_OPTIONAL,
8143 &decl_specifiers,
8144 &declares_class_or_enum);
8145 /* We no longer need to defer access checks. */
8146 stop_deferring_access_checks ();
8147
8148 /* In a block scope, a valid declaration must always have a
8149 decl-specifier-seq. By not trying to parse declarators, we can
8150 resolve the declaration/expression ambiguity more quickly. */
8151 if (!function_definition_allowed_p
8152 && !decl_specifiers.any_specifiers_p)
8153 {
8154 cp_parser_error (parser, "expected declaration");
8155 goto done;
8156 }
8157
8158 /* If the next two tokens are both identifiers, the code is
8159 erroneous. The usual cause of this situation is code like:
8160
8161 T t;
8162
8163 where "T" should name a type -- but does not. */
8164 if (!decl_specifiers.type
8165 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8166 {
8167 /* If parsing tentatively, we should commit; we really are
8168 looking at a declaration. */
8169 cp_parser_commit_to_tentative_parse (parser);
8170 /* Give up. */
8171 goto done;
8172 }
8173
8174 /* If we have seen at least one decl-specifier, and the next token
8175 is not a parenthesis, then we must be looking at a declaration.
8176 (After "int (" we might be looking at a functional cast.) */
8177 if (decl_specifiers.any_specifiers_p
8178 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8179 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8180 && !cp_parser_error_occurred (parser))
8181 cp_parser_commit_to_tentative_parse (parser);
8182
8183 /* Keep going until we hit the `;' at the end of the simple
8184 declaration. */
8185 saw_declarator = false;
8186 while (cp_lexer_next_token_is_not (parser->lexer,
8187 CPP_SEMICOLON))
8188 {
8189 cp_token *token;
8190 bool function_definition_p;
8191 tree decl;
8192
8193 if (saw_declarator)
8194 {
8195 /* If we are processing next declarator, coma is expected */
8196 token = cp_lexer_peek_token (parser->lexer);
8197 gcc_assert (token->type == CPP_COMMA);
8198 cp_lexer_consume_token (parser->lexer);
8199 }
8200 else
8201 saw_declarator = true;
8202
8203 /* Parse the init-declarator. */
8204 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8205 /*checks=*/NULL,
8206 function_definition_allowed_p,
8207 /*member_p=*/false,
8208 declares_class_or_enum,
8209 &function_definition_p);
8210 /* If an error occurred while parsing tentatively, exit quickly.
8211 (That usually happens when in the body of a function; each
8212 statement is treated as a declaration-statement until proven
8213 otherwise.) */
8214 if (cp_parser_error_occurred (parser))
8215 goto done;
8216 /* Handle function definitions specially. */
8217 if (function_definition_p)
8218 {
8219 /* If the next token is a `,', then we are probably
8220 processing something like:
8221
8222 void f() {}, *p;
8223
8224 which is erroneous. */
8225 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8226 {
8227 cp_token *token = cp_lexer_peek_token (parser->lexer);
8228 error ("%Hmixing declarations and function-definitions is forbidden",
8229 &token->location);
8230 }
8231 /* Otherwise, we're done with the list of declarators. */
8232 else
8233 {
8234 pop_deferring_access_checks ();
8235 return;
8236 }
8237 }
8238 /* The next token should be either a `,' or a `;'. */
8239 token = cp_lexer_peek_token (parser->lexer);
8240 /* If it's a `,', there are more declarators to come. */
8241 if (token->type == CPP_COMMA)
8242 /* will be consumed next time around */;
8243 /* If it's a `;', we are done. */
8244 else if (token->type == CPP_SEMICOLON)
8245 break;
8246 /* Anything else is an error. */
8247 else
8248 {
8249 /* If we have already issued an error message we don't need
8250 to issue another one. */
8251 if (decl != error_mark_node
8252 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8253 cp_parser_error (parser, "expected %<,%> or %<;%>");
8254 /* Skip tokens until we reach the end of the statement. */
8255 cp_parser_skip_to_end_of_statement (parser);
8256 /* If the next token is now a `;', consume it. */
8257 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8258 cp_lexer_consume_token (parser->lexer);
8259 goto done;
8260 }
8261 /* After the first time around, a function-definition is not
8262 allowed -- even if it was OK at first. For example:
8263
8264 int i, f() {}
8265
8266 is not valid. */
8267 function_definition_allowed_p = false;
8268 }
8269
8270 /* Issue an error message if no declarators are present, and the
8271 decl-specifier-seq does not itself declare a class or
8272 enumeration. */
8273 if (!saw_declarator)
8274 {
8275 if (cp_parser_declares_only_class_p (parser))
8276 shadow_tag (&decl_specifiers);
8277 /* Perform any deferred access checks. */
8278 perform_deferred_access_checks ();
8279 }
8280
8281 /* Consume the `;'. */
8282 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8283
8284 done:
8285 pop_deferring_access_checks ();
8286 }
8287
8288 /* Parse a decl-specifier-seq.
8289
8290 decl-specifier-seq:
8291 decl-specifier-seq [opt] decl-specifier
8292
8293 decl-specifier:
8294 storage-class-specifier
8295 type-specifier
8296 function-specifier
8297 friend
8298 typedef
8299
8300 GNU Extension:
8301
8302 decl-specifier:
8303 attributes
8304
8305 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8306
8307 The parser flags FLAGS is used to control type-specifier parsing.
8308
8309 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8310 flags:
8311
8312 1: one of the decl-specifiers is an elaborated-type-specifier
8313 (i.e., a type declaration)
8314 2: one of the decl-specifiers is an enum-specifier or a
8315 class-specifier (i.e., a type definition)
8316
8317 */
8318
8319 static void
8320 cp_parser_decl_specifier_seq (cp_parser* parser,
8321 cp_parser_flags flags,
8322 cp_decl_specifier_seq *decl_specs,
8323 int* declares_class_or_enum)
8324 {
8325 bool constructor_possible_p = !parser->in_declarator_p;
8326 cp_token *start_token = NULL;
8327
8328 /* Clear DECL_SPECS. */
8329 clear_decl_specs (decl_specs);
8330
8331 /* Assume no class or enumeration type is declared. */
8332 *declares_class_or_enum = 0;
8333
8334 /* Keep reading specifiers until there are no more to read. */
8335 while (true)
8336 {
8337 bool constructor_p;
8338 bool found_decl_spec;
8339 cp_token *token;
8340
8341 /* Peek at the next token. */
8342 token = cp_lexer_peek_token (parser->lexer);
8343
8344 /* Save the first token of the decl spec list for error
8345 reporting. */
8346 if (!start_token)
8347 start_token = token;
8348 /* Handle attributes. */
8349 if (token->keyword == RID_ATTRIBUTE)
8350 {
8351 /* Parse the attributes. */
8352 decl_specs->attributes
8353 = chainon (decl_specs->attributes,
8354 cp_parser_attributes_opt (parser));
8355 continue;
8356 }
8357 /* Assume we will find a decl-specifier keyword. */
8358 found_decl_spec = true;
8359 /* If the next token is an appropriate keyword, we can simply
8360 add it to the list. */
8361 switch (token->keyword)
8362 {
8363 /* decl-specifier:
8364 friend */
8365 case RID_FRIEND:
8366 if (!at_class_scope_p ())
8367 {
8368 error ("%H%<friend%> used outside of class", &token->location);
8369 cp_lexer_purge_token (parser->lexer);
8370 }
8371 else
8372 {
8373 ++decl_specs->specs[(int) ds_friend];
8374 /* Consume the token. */
8375 cp_lexer_consume_token (parser->lexer);
8376 }
8377 break;
8378
8379 /* function-specifier:
8380 inline
8381 virtual
8382 explicit */
8383 case RID_INLINE:
8384 case RID_VIRTUAL:
8385 case RID_EXPLICIT:
8386 cp_parser_function_specifier_opt (parser, decl_specs);
8387 break;
8388
8389 /* decl-specifier:
8390 typedef */
8391 case RID_TYPEDEF:
8392 ++decl_specs->specs[(int) ds_typedef];
8393 /* Consume the token. */
8394 cp_lexer_consume_token (parser->lexer);
8395 /* A constructor declarator cannot appear in a typedef. */
8396 constructor_possible_p = false;
8397 /* The "typedef" keyword can only occur in a declaration; we
8398 may as well commit at this point. */
8399 cp_parser_commit_to_tentative_parse (parser);
8400
8401 if (decl_specs->storage_class != sc_none)
8402 decl_specs->conflicting_specifiers_p = true;
8403 break;
8404
8405 /* storage-class-specifier:
8406 auto
8407 register
8408 static
8409 extern
8410 mutable
8411
8412 GNU Extension:
8413 thread */
8414 case RID_AUTO:
8415 if (cxx_dialect == cxx98)
8416 {
8417 /* Consume the token. */
8418 cp_lexer_consume_token (parser->lexer);
8419
8420 /* Complain about `auto' as a storage specifier, if
8421 we're complaining about C++0x compatibility. */
8422 warning
8423 (OPT_Wc__0x_compat,
8424 "%H%<auto%> will change meaning in C++0x; please remove it",
8425 &token->location);
8426
8427 /* Set the storage class anyway. */
8428 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8429 token->location);
8430 }
8431 else
8432 /* C++0x auto type-specifier. */
8433 found_decl_spec = false;
8434 break;
8435
8436 case RID_REGISTER:
8437 case RID_STATIC:
8438 case RID_EXTERN:
8439 case RID_MUTABLE:
8440 /* Consume the token. */
8441 cp_lexer_consume_token (parser->lexer);
8442 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8443 token->location);
8444 break;
8445 case RID_THREAD:
8446 /* Consume the token. */
8447 cp_lexer_consume_token (parser->lexer);
8448 ++decl_specs->specs[(int) ds_thread];
8449 break;
8450
8451 default:
8452 /* We did not yet find a decl-specifier yet. */
8453 found_decl_spec = false;
8454 break;
8455 }
8456
8457 /* Constructors are a special case. The `S' in `S()' is not a
8458 decl-specifier; it is the beginning of the declarator. */
8459 constructor_p
8460 = (!found_decl_spec
8461 && constructor_possible_p
8462 && (cp_parser_constructor_declarator_p
8463 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8464
8465 /* If we don't have a DECL_SPEC yet, then we must be looking at
8466 a type-specifier. */
8467 if (!found_decl_spec && !constructor_p)
8468 {
8469 int decl_spec_declares_class_or_enum;
8470 bool is_cv_qualifier;
8471 tree type_spec;
8472
8473 type_spec
8474 = cp_parser_type_specifier (parser, flags,
8475 decl_specs,
8476 /*is_declaration=*/true,
8477 &decl_spec_declares_class_or_enum,
8478 &is_cv_qualifier);
8479 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8480
8481 /* If this type-specifier referenced a user-defined type
8482 (a typedef, class-name, etc.), then we can't allow any
8483 more such type-specifiers henceforth.
8484
8485 [dcl.spec]
8486
8487 The longest sequence of decl-specifiers that could
8488 possibly be a type name is taken as the
8489 decl-specifier-seq of a declaration. The sequence shall
8490 be self-consistent as described below.
8491
8492 [dcl.type]
8493
8494 As a general rule, at most one type-specifier is allowed
8495 in the complete decl-specifier-seq of a declaration. The
8496 only exceptions are the following:
8497
8498 -- const or volatile can be combined with any other
8499 type-specifier.
8500
8501 -- signed or unsigned can be combined with char, long,
8502 short, or int.
8503
8504 -- ..
8505
8506 Example:
8507
8508 typedef char* Pc;
8509 void g (const int Pc);
8510
8511 Here, Pc is *not* part of the decl-specifier seq; it's
8512 the declarator. Therefore, once we see a type-specifier
8513 (other than a cv-qualifier), we forbid any additional
8514 user-defined types. We *do* still allow things like `int
8515 int' to be considered a decl-specifier-seq, and issue the
8516 error message later. */
8517 if (type_spec && !is_cv_qualifier)
8518 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8519 /* A constructor declarator cannot follow a type-specifier. */
8520 if (type_spec)
8521 {
8522 constructor_possible_p = false;
8523 found_decl_spec = true;
8524 }
8525 }
8526
8527 /* If we still do not have a DECL_SPEC, then there are no more
8528 decl-specifiers. */
8529 if (!found_decl_spec)
8530 break;
8531
8532 decl_specs->any_specifiers_p = true;
8533 /* After we see one decl-specifier, further decl-specifiers are
8534 always optional. */
8535 flags |= CP_PARSER_FLAGS_OPTIONAL;
8536 }
8537
8538 cp_parser_check_decl_spec (decl_specs, start_token->location);
8539
8540 /* Don't allow a friend specifier with a class definition. */
8541 if (decl_specs->specs[(int) ds_friend] != 0
8542 && (*declares_class_or_enum & 2))
8543 error ("%Hclass definition may not be declared a friend",
8544 &start_token->location);
8545 }
8546
8547 /* Parse an (optional) storage-class-specifier.
8548
8549 storage-class-specifier:
8550 auto
8551 register
8552 static
8553 extern
8554 mutable
8555
8556 GNU Extension:
8557
8558 storage-class-specifier:
8559 thread
8560
8561 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8562
8563 static tree
8564 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8565 {
8566 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8567 {
8568 case RID_AUTO:
8569 if (cxx_dialect != cxx98)
8570 return NULL_TREE;
8571 /* Fall through for C++98. */
8572
8573 case RID_REGISTER:
8574 case RID_STATIC:
8575 case RID_EXTERN:
8576 case RID_MUTABLE:
8577 case RID_THREAD:
8578 /* Consume the token. */
8579 return cp_lexer_consume_token (parser->lexer)->u.value;
8580
8581 default:
8582 return NULL_TREE;
8583 }
8584 }
8585
8586 /* Parse an (optional) function-specifier.
8587
8588 function-specifier:
8589 inline
8590 virtual
8591 explicit
8592
8593 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8594 Updates DECL_SPECS, if it is non-NULL. */
8595
8596 static tree
8597 cp_parser_function_specifier_opt (cp_parser* parser,
8598 cp_decl_specifier_seq *decl_specs)
8599 {
8600 cp_token *token = cp_lexer_peek_token (parser->lexer);
8601 switch (token->keyword)
8602 {
8603 case RID_INLINE:
8604 if (decl_specs)
8605 ++decl_specs->specs[(int) ds_inline];
8606 break;
8607
8608 case RID_VIRTUAL:
8609 /* 14.5.2.3 [temp.mem]
8610
8611 A member function template shall not be virtual. */
8612 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8613 error ("%Htemplates may not be %<virtual%>", &token->location);
8614 else if (decl_specs)
8615 ++decl_specs->specs[(int) ds_virtual];
8616 break;
8617
8618 case RID_EXPLICIT:
8619 if (decl_specs)
8620 ++decl_specs->specs[(int) ds_explicit];
8621 break;
8622
8623 default:
8624 return NULL_TREE;
8625 }
8626
8627 /* Consume the token. */
8628 return cp_lexer_consume_token (parser->lexer)->u.value;
8629 }
8630
8631 /* Parse a linkage-specification.
8632
8633 linkage-specification:
8634 extern string-literal { declaration-seq [opt] }
8635 extern string-literal declaration */
8636
8637 static void
8638 cp_parser_linkage_specification (cp_parser* parser)
8639 {
8640 tree linkage;
8641
8642 /* Look for the `extern' keyword. */
8643 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8644
8645 /* Look for the string-literal. */
8646 linkage = cp_parser_string_literal (parser, false, false);
8647
8648 /* Transform the literal into an identifier. If the literal is a
8649 wide-character string, or contains embedded NULs, then we can't
8650 handle it as the user wants. */
8651 if (strlen (TREE_STRING_POINTER (linkage))
8652 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8653 {
8654 cp_parser_error (parser, "invalid linkage-specification");
8655 /* Assume C++ linkage. */
8656 linkage = lang_name_cplusplus;
8657 }
8658 else
8659 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8660
8661 /* We're now using the new linkage. */
8662 push_lang_context (linkage);
8663
8664 /* If the next token is a `{', then we're using the first
8665 production. */
8666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8667 {
8668 /* Consume the `{' token. */
8669 cp_lexer_consume_token (parser->lexer);
8670 /* Parse the declarations. */
8671 cp_parser_declaration_seq_opt (parser);
8672 /* Look for the closing `}'. */
8673 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8674 }
8675 /* Otherwise, there's just one declaration. */
8676 else
8677 {
8678 bool saved_in_unbraced_linkage_specification_p;
8679
8680 saved_in_unbraced_linkage_specification_p
8681 = parser->in_unbraced_linkage_specification_p;
8682 parser->in_unbraced_linkage_specification_p = true;
8683 cp_parser_declaration (parser);
8684 parser->in_unbraced_linkage_specification_p
8685 = saved_in_unbraced_linkage_specification_p;
8686 }
8687
8688 /* We're done with the linkage-specification. */
8689 pop_lang_context ();
8690 }
8691
8692 /* Parse a static_assert-declaration.
8693
8694 static_assert-declaration:
8695 static_assert ( constant-expression , string-literal ) ;
8696
8697 If MEMBER_P, this static_assert is a class member. */
8698
8699 static void
8700 cp_parser_static_assert(cp_parser *parser, bool member_p)
8701 {
8702 tree condition;
8703 tree message;
8704 cp_token *token;
8705 location_t saved_loc;
8706
8707 /* Peek at the `static_assert' token so we can keep track of exactly
8708 where the static assertion started. */
8709 token = cp_lexer_peek_token (parser->lexer);
8710 saved_loc = token->location;
8711
8712 /* Look for the `static_assert' keyword. */
8713 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8714 "%<static_assert%>"))
8715 return;
8716
8717 /* We know we are in a static assertion; commit to any tentative
8718 parse. */
8719 if (cp_parser_parsing_tentatively (parser))
8720 cp_parser_commit_to_tentative_parse (parser);
8721
8722 /* Parse the `(' starting the static assertion condition. */
8723 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8724
8725 /* Parse the constant-expression. */
8726 condition =
8727 cp_parser_constant_expression (parser,
8728 /*allow_non_constant_p=*/false,
8729 /*non_constant_p=*/NULL);
8730
8731 /* Parse the separating `,'. */
8732 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8733
8734 /* Parse the string-literal message. */
8735 message = cp_parser_string_literal (parser,
8736 /*translate=*/false,
8737 /*wide_ok=*/true);
8738
8739 /* A `)' completes the static assertion. */
8740 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8741 cp_parser_skip_to_closing_parenthesis (parser,
8742 /*recovering=*/true,
8743 /*or_comma=*/false,
8744 /*consume_paren=*/true);
8745
8746 /* A semicolon terminates the declaration. */
8747 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8748
8749 /* Complete the static assertion, which may mean either processing
8750 the static assert now or saving it for template instantiation. */
8751 finish_static_assert (condition, message, saved_loc, member_p);
8752 }
8753
8754 /* Parse a `decltype' type. Returns the type.
8755
8756 simple-type-specifier:
8757 decltype ( expression ) */
8758
8759 static tree
8760 cp_parser_decltype (cp_parser *parser)
8761 {
8762 tree expr;
8763 bool id_expression_or_member_access_p = false;
8764 const char *saved_message;
8765 bool saved_integral_constant_expression_p;
8766 bool saved_non_integral_constant_expression_p;
8767 cp_token *id_expr_start_token;
8768
8769 /* Look for the `decltype' token. */
8770 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8771 return error_mark_node;
8772
8773 /* Types cannot be defined in a `decltype' expression. Save away the
8774 old message. */
8775 saved_message = parser->type_definition_forbidden_message;
8776
8777 /* And create the new one. */
8778 parser->type_definition_forbidden_message
8779 = "types may not be defined in %<decltype%> expressions";
8780
8781 /* The restrictions on constant-expressions do not apply inside
8782 decltype expressions. */
8783 saved_integral_constant_expression_p
8784 = parser->integral_constant_expression_p;
8785 saved_non_integral_constant_expression_p
8786 = parser->non_integral_constant_expression_p;
8787 parser->integral_constant_expression_p = false;
8788
8789 /* Do not actually evaluate the expression. */
8790 ++skip_evaluation;
8791
8792 /* Parse the opening `('. */
8793 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8794 return error_mark_node;
8795
8796 /* First, try parsing an id-expression. */
8797 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8798 cp_parser_parse_tentatively (parser);
8799 expr = cp_parser_id_expression (parser,
8800 /*template_keyword_p=*/false,
8801 /*check_dependency_p=*/true,
8802 /*template_p=*/NULL,
8803 /*declarator_p=*/false,
8804 /*optional_p=*/false);
8805
8806 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8807 {
8808 bool non_integral_constant_expression_p = false;
8809 tree id_expression = expr;
8810 cp_id_kind idk;
8811 const char *error_msg;
8812
8813 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8814 /* Lookup the name we got back from the id-expression. */
8815 expr = cp_parser_lookup_name (parser, expr,
8816 none_type,
8817 /*is_template=*/false,
8818 /*is_namespace=*/false,
8819 /*check_dependency=*/true,
8820 /*ambiguous_decls=*/NULL,
8821 id_expr_start_token->location);
8822
8823 if (expr
8824 && expr != error_mark_node
8825 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8826 && TREE_CODE (expr) != TYPE_DECL
8827 && (TREE_CODE (expr) != BIT_NOT_EXPR
8828 || !TYPE_P (TREE_OPERAND (expr, 0)))
8829 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8830 {
8831 /* Complete lookup of the id-expression. */
8832 expr = (finish_id_expression
8833 (id_expression, expr, parser->scope, &idk,
8834 /*integral_constant_expression_p=*/false,
8835 /*allow_non_integral_constant_expression_p=*/true,
8836 &non_integral_constant_expression_p,
8837 /*template_p=*/false,
8838 /*done=*/true,
8839 /*address_p=*/false,
8840 /*template_arg_p=*/false,
8841 &error_msg,
8842 id_expr_start_token->location));
8843
8844 if (expr == error_mark_node)
8845 /* We found an id-expression, but it was something that we
8846 should not have found. This is an error, not something
8847 we can recover from, so note that we found an
8848 id-expression and we'll recover as gracefully as
8849 possible. */
8850 id_expression_or_member_access_p = true;
8851 }
8852
8853 if (expr
8854 && expr != error_mark_node
8855 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8856 /* We have an id-expression. */
8857 id_expression_or_member_access_p = true;
8858 }
8859
8860 if (!id_expression_or_member_access_p)
8861 {
8862 /* Abort the id-expression parse. */
8863 cp_parser_abort_tentative_parse (parser);
8864
8865 /* Parsing tentatively, again. */
8866 cp_parser_parse_tentatively (parser);
8867
8868 /* Parse a class member access. */
8869 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8870 /*cast_p=*/false,
8871 /*member_access_only_p=*/true, NULL);
8872
8873 if (expr
8874 && expr != error_mark_node
8875 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8876 /* We have an id-expression. */
8877 id_expression_or_member_access_p = true;
8878 }
8879
8880 if (id_expression_or_member_access_p)
8881 /* We have parsed the complete id-expression or member access. */
8882 cp_parser_parse_definitely (parser);
8883 else
8884 {
8885 /* Abort our attempt to parse an id-expression or member access
8886 expression. */
8887 cp_parser_abort_tentative_parse (parser);
8888
8889 /* Parse a full expression. */
8890 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8891 }
8892
8893 /* Go back to evaluating expressions. */
8894 --skip_evaluation;
8895
8896 /* Restore the old message and the integral constant expression
8897 flags. */
8898 parser->type_definition_forbidden_message = saved_message;
8899 parser->integral_constant_expression_p
8900 = saved_integral_constant_expression_p;
8901 parser->non_integral_constant_expression_p
8902 = saved_non_integral_constant_expression_p;
8903
8904 if (expr == error_mark_node)
8905 {
8906 /* Skip everything up to the closing `)'. */
8907 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8908 /*consume_paren=*/true);
8909 return error_mark_node;
8910 }
8911
8912 /* Parse to the closing `)'. */
8913 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8914 {
8915 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8916 /*consume_paren=*/true);
8917 return error_mark_node;
8918 }
8919
8920 return finish_decltype_type (expr, id_expression_or_member_access_p);
8921 }
8922
8923 /* Special member functions [gram.special] */
8924
8925 /* Parse a conversion-function-id.
8926
8927 conversion-function-id:
8928 operator conversion-type-id
8929
8930 Returns an IDENTIFIER_NODE representing the operator. */
8931
8932 static tree
8933 cp_parser_conversion_function_id (cp_parser* parser)
8934 {
8935 tree type;
8936 tree saved_scope;
8937 tree saved_qualifying_scope;
8938 tree saved_object_scope;
8939 tree pushed_scope = NULL_TREE;
8940
8941 /* Look for the `operator' token. */
8942 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8943 return error_mark_node;
8944 /* When we parse the conversion-type-id, the current scope will be
8945 reset. However, we need that information in able to look up the
8946 conversion function later, so we save it here. */
8947 saved_scope = parser->scope;
8948 saved_qualifying_scope = parser->qualifying_scope;
8949 saved_object_scope = parser->object_scope;
8950 /* We must enter the scope of the class so that the names of
8951 entities declared within the class are available in the
8952 conversion-type-id. For example, consider:
8953
8954 struct S {
8955 typedef int I;
8956 operator I();
8957 };
8958
8959 S::operator I() { ... }
8960
8961 In order to see that `I' is a type-name in the definition, we
8962 must be in the scope of `S'. */
8963 if (saved_scope)
8964 pushed_scope = push_scope (saved_scope);
8965 /* Parse the conversion-type-id. */
8966 type = cp_parser_conversion_type_id (parser);
8967 /* Leave the scope of the class, if any. */
8968 if (pushed_scope)
8969 pop_scope (pushed_scope);
8970 /* Restore the saved scope. */
8971 parser->scope = saved_scope;
8972 parser->qualifying_scope = saved_qualifying_scope;
8973 parser->object_scope = saved_object_scope;
8974 /* If the TYPE is invalid, indicate failure. */
8975 if (type == error_mark_node)
8976 return error_mark_node;
8977 return mangle_conv_op_name_for_type (type);
8978 }
8979
8980 /* Parse a conversion-type-id:
8981
8982 conversion-type-id:
8983 type-specifier-seq conversion-declarator [opt]
8984
8985 Returns the TYPE specified. */
8986
8987 static tree
8988 cp_parser_conversion_type_id (cp_parser* parser)
8989 {
8990 tree attributes;
8991 cp_decl_specifier_seq type_specifiers;
8992 cp_declarator *declarator;
8993 tree type_specified;
8994
8995 /* Parse the attributes. */
8996 attributes = cp_parser_attributes_opt (parser);
8997 /* Parse the type-specifiers. */
8998 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8999 &type_specifiers);
9000 /* If that didn't work, stop. */
9001 if (type_specifiers.type == error_mark_node)
9002 return error_mark_node;
9003 /* Parse the conversion-declarator. */
9004 declarator = cp_parser_conversion_declarator_opt (parser);
9005
9006 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9007 /*initialized=*/0, &attributes);
9008 if (attributes)
9009 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9010
9011 /* Don't give this error when parsing tentatively. This happens to
9012 work because we always parse this definitively once. */
9013 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9014 && type_uses_auto (type_specified))
9015 {
9016 error ("invalid use of %<auto%> in conversion operator");
9017 return error_mark_node;
9018 }
9019
9020 return type_specified;
9021 }
9022
9023 /* Parse an (optional) conversion-declarator.
9024
9025 conversion-declarator:
9026 ptr-operator conversion-declarator [opt]
9027
9028 */
9029
9030 static cp_declarator *
9031 cp_parser_conversion_declarator_opt (cp_parser* parser)
9032 {
9033 enum tree_code code;
9034 tree class_type;
9035 cp_cv_quals cv_quals;
9036
9037 /* We don't know if there's a ptr-operator next, or not. */
9038 cp_parser_parse_tentatively (parser);
9039 /* Try the ptr-operator. */
9040 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9041 /* If it worked, look for more conversion-declarators. */
9042 if (cp_parser_parse_definitely (parser))
9043 {
9044 cp_declarator *declarator;
9045
9046 /* Parse another optional declarator. */
9047 declarator = cp_parser_conversion_declarator_opt (parser);
9048
9049 return cp_parser_make_indirect_declarator
9050 (code, class_type, cv_quals, declarator);
9051 }
9052
9053 return NULL;
9054 }
9055
9056 /* Parse an (optional) ctor-initializer.
9057
9058 ctor-initializer:
9059 : mem-initializer-list
9060
9061 Returns TRUE iff the ctor-initializer was actually present. */
9062
9063 static bool
9064 cp_parser_ctor_initializer_opt (cp_parser* parser)
9065 {
9066 /* If the next token is not a `:', then there is no
9067 ctor-initializer. */
9068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9069 {
9070 /* Do default initialization of any bases and members. */
9071 if (DECL_CONSTRUCTOR_P (current_function_decl))
9072 finish_mem_initializers (NULL_TREE);
9073
9074 return false;
9075 }
9076
9077 /* Consume the `:' token. */
9078 cp_lexer_consume_token (parser->lexer);
9079 /* And the mem-initializer-list. */
9080 cp_parser_mem_initializer_list (parser);
9081
9082 return true;
9083 }
9084
9085 /* Parse a mem-initializer-list.
9086
9087 mem-initializer-list:
9088 mem-initializer ... [opt]
9089 mem-initializer ... [opt] , mem-initializer-list */
9090
9091 static void
9092 cp_parser_mem_initializer_list (cp_parser* parser)
9093 {
9094 tree mem_initializer_list = NULL_TREE;
9095 cp_token *token = cp_lexer_peek_token (parser->lexer);
9096
9097 /* Let the semantic analysis code know that we are starting the
9098 mem-initializer-list. */
9099 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9100 error ("%Honly constructors take base initializers",
9101 &token->location);
9102
9103 /* Loop through the list. */
9104 while (true)
9105 {
9106 tree mem_initializer;
9107
9108 token = cp_lexer_peek_token (parser->lexer);
9109 /* Parse the mem-initializer. */
9110 mem_initializer = cp_parser_mem_initializer (parser);
9111 /* If the next token is a `...', we're expanding member initializers. */
9112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9113 {
9114 /* Consume the `...'. */
9115 cp_lexer_consume_token (parser->lexer);
9116
9117 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9118 can be expanded but members cannot. */
9119 if (mem_initializer != error_mark_node
9120 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9121 {
9122 error ("%Hcannot expand initializer for member %<%D%>",
9123 &token->location, TREE_PURPOSE (mem_initializer));
9124 mem_initializer = error_mark_node;
9125 }
9126
9127 /* Construct the pack expansion type. */
9128 if (mem_initializer != error_mark_node)
9129 mem_initializer = make_pack_expansion (mem_initializer);
9130 }
9131 /* Add it to the list, unless it was erroneous. */
9132 if (mem_initializer != error_mark_node)
9133 {
9134 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9135 mem_initializer_list = mem_initializer;
9136 }
9137 /* If the next token is not a `,', we're done. */
9138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9139 break;
9140 /* Consume the `,' token. */
9141 cp_lexer_consume_token (parser->lexer);
9142 }
9143
9144 /* Perform semantic analysis. */
9145 if (DECL_CONSTRUCTOR_P (current_function_decl))
9146 finish_mem_initializers (mem_initializer_list);
9147 }
9148
9149 /* Parse a mem-initializer.
9150
9151 mem-initializer:
9152 mem-initializer-id ( expression-list [opt] )
9153 mem-initializer-id braced-init-list
9154
9155 GNU extension:
9156
9157 mem-initializer:
9158 ( expression-list [opt] )
9159
9160 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9161 class) or FIELD_DECL (for a non-static data member) to initialize;
9162 the TREE_VALUE is the expression-list. An empty initialization
9163 list is represented by void_list_node. */
9164
9165 static tree
9166 cp_parser_mem_initializer (cp_parser* parser)
9167 {
9168 tree mem_initializer_id;
9169 tree expression_list;
9170 tree member;
9171 cp_token *token = cp_lexer_peek_token (parser->lexer);
9172
9173 /* Find out what is being initialized. */
9174 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9175 {
9176 permerror (token->location,
9177 "anachronistic old-style base class initializer");
9178 mem_initializer_id = NULL_TREE;
9179 }
9180 else
9181 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9182 member = expand_member_init (mem_initializer_id);
9183 if (member && !DECL_P (member))
9184 in_base_initializer = 1;
9185
9186 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9187 {
9188 bool expr_non_constant_p;
9189 maybe_warn_cpp0x ("extended initializer lists");
9190 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9191 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9192 expression_list = build_tree_list (NULL_TREE, expression_list);
9193 }
9194 else
9195 expression_list
9196 = cp_parser_parenthesized_expression_list (parser, false,
9197 /*cast_p=*/false,
9198 /*allow_expansion_p=*/true,
9199 /*non_constant_p=*/NULL);
9200 if (expression_list == error_mark_node)
9201 return error_mark_node;
9202 if (!expression_list)
9203 expression_list = void_type_node;
9204
9205 in_base_initializer = 0;
9206
9207 return member ? build_tree_list (member, expression_list) : error_mark_node;
9208 }
9209
9210 /* Parse a mem-initializer-id.
9211
9212 mem-initializer-id:
9213 :: [opt] nested-name-specifier [opt] class-name
9214 identifier
9215
9216 Returns a TYPE indicating the class to be initializer for the first
9217 production. Returns an IDENTIFIER_NODE indicating the data member
9218 to be initialized for the second production. */
9219
9220 static tree
9221 cp_parser_mem_initializer_id (cp_parser* parser)
9222 {
9223 bool global_scope_p;
9224 bool nested_name_specifier_p;
9225 bool template_p = false;
9226 tree id;
9227
9228 cp_token *token = cp_lexer_peek_token (parser->lexer);
9229
9230 /* `typename' is not allowed in this context ([temp.res]). */
9231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9232 {
9233 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9234 "member initializer is implicitly a type)",
9235 &token->location);
9236 cp_lexer_consume_token (parser->lexer);
9237 }
9238 /* Look for the optional `::' operator. */
9239 global_scope_p
9240 = (cp_parser_global_scope_opt (parser,
9241 /*current_scope_valid_p=*/false)
9242 != NULL_TREE);
9243 /* Look for the optional nested-name-specifier. The simplest way to
9244 implement:
9245
9246 [temp.res]
9247
9248 The keyword `typename' is not permitted in a base-specifier or
9249 mem-initializer; in these contexts a qualified name that
9250 depends on a template-parameter is implicitly assumed to be a
9251 type name.
9252
9253 is to assume that we have seen the `typename' keyword at this
9254 point. */
9255 nested_name_specifier_p
9256 = (cp_parser_nested_name_specifier_opt (parser,
9257 /*typename_keyword_p=*/true,
9258 /*check_dependency_p=*/true,
9259 /*type_p=*/true,
9260 /*is_declaration=*/true)
9261 != NULL_TREE);
9262 if (nested_name_specifier_p)
9263 template_p = cp_parser_optional_template_keyword (parser);
9264 /* If there is a `::' operator or a nested-name-specifier, then we
9265 are definitely looking for a class-name. */
9266 if (global_scope_p || nested_name_specifier_p)
9267 return cp_parser_class_name (parser,
9268 /*typename_keyword_p=*/true,
9269 /*template_keyword_p=*/template_p,
9270 none_type,
9271 /*check_dependency_p=*/true,
9272 /*class_head_p=*/false,
9273 /*is_declaration=*/true);
9274 /* Otherwise, we could also be looking for an ordinary identifier. */
9275 cp_parser_parse_tentatively (parser);
9276 /* Try a class-name. */
9277 id = cp_parser_class_name (parser,
9278 /*typename_keyword_p=*/true,
9279 /*template_keyword_p=*/false,
9280 none_type,
9281 /*check_dependency_p=*/true,
9282 /*class_head_p=*/false,
9283 /*is_declaration=*/true);
9284 /* If we found one, we're done. */
9285 if (cp_parser_parse_definitely (parser))
9286 return id;
9287 /* Otherwise, look for an ordinary identifier. */
9288 return cp_parser_identifier (parser);
9289 }
9290
9291 /* Overloading [gram.over] */
9292
9293 /* Parse an operator-function-id.
9294
9295 operator-function-id:
9296 operator operator
9297
9298 Returns an IDENTIFIER_NODE for the operator which is a
9299 human-readable spelling of the identifier, e.g., `operator +'. */
9300
9301 static tree
9302 cp_parser_operator_function_id (cp_parser* parser)
9303 {
9304 /* Look for the `operator' keyword. */
9305 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9306 return error_mark_node;
9307 /* And then the name of the operator itself. */
9308 return cp_parser_operator (parser);
9309 }
9310
9311 /* Parse an operator.
9312
9313 operator:
9314 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9315 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9316 || ++ -- , ->* -> () []
9317
9318 GNU Extensions:
9319
9320 operator:
9321 <? >? <?= >?=
9322
9323 Returns an IDENTIFIER_NODE for the operator which is a
9324 human-readable spelling of the identifier, e.g., `operator +'. */
9325
9326 static tree
9327 cp_parser_operator (cp_parser* parser)
9328 {
9329 tree id = NULL_TREE;
9330 cp_token *token;
9331
9332 /* Peek at the next token. */
9333 token = cp_lexer_peek_token (parser->lexer);
9334 /* Figure out which operator we have. */
9335 switch (token->type)
9336 {
9337 case CPP_KEYWORD:
9338 {
9339 enum tree_code op;
9340
9341 /* The keyword should be either `new' or `delete'. */
9342 if (token->keyword == RID_NEW)
9343 op = NEW_EXPR;
9344 else if (token->keyword == RID_DELETE)
9345 op = DELETE_EXPR;
9346 else
9347 break;
9348
9349 /* Consume the `new' or `delete' token. */
9350 cp_lexer_consume_token (parser->lexer);
9351
9352 /* Peek at the next token. */
9353 token = cp_lexer_peek_token (parser->lexer);
9354 /* If it's a `[' token then this is the array variant of the
9355 operator. */
9356 if (token->type == CPP_OPEN_SQUARE)
9357 {
9358 /* Consume the `[' token. */
9359 cp_lexer_consume_token (parser->lexer);
9360 /* Look for the `]' token. */
9361 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9362 id = ansi_opname (op == NEW_EXPR
9363 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9364 }
9365 /* Otherwise, we have the non-array variant. */
9366 else
9367 id = ansi_opname (op);
9368
9369 return id;
9370 }
9371
9372 case CPP_PLUS:
9373 id = ansi_opname (PLUS_EXPR);
9374 break;
9375
9376 case CPP_MINUS:
9377 id = ansi_opname (MINUS_EXPR);
9378 break;
9379
9380 case CPP_MULT:
9381 id = ansi_opname (MULT_EXPR);
9382 break;
9383
9384 case CPP_DIV:
9385 id = ansi_opname (TRUNC_DIV_EXPR);
9386 break;
9387
9388 case CPP_MOD:
9389 id = ansi_opname (TRUNC_MOD_EXPR);
9390 break;
9391
9392 case CPP_XOR:
9393 id = ansi_opname (BIT_XOR_EXPR);
9394 break;
9395
9396 case CPP_AND:
9397 id = ansi_opname (BIT_AND_EXPR);
9398 break;
9399
9400 case CPP_OR:
9401 id = ansi_opname (BIT_IOR_EXPR);
9402 break;
9403
9404 case CPP_COMPL:
9405 id = ansi_opname (BIT_NOT_EXPR);
9406 break;
9407
9408 case CPP_NOT:
9409 id = ansi_opname (TRUTH_NOT_EXPR);
9410 break;
9411
9412 case CPP_EQ:
9413 id = ansi_assopname (NOP_EXPR);
9414 break;
9415
9416 case CPP_LESS:
9417 id = ansi_opname (LT_EXPR);
9418 break;
9419
9420 case CPP_GREATER:
9421 id = ansi_opname (GT_EXPR);
9422 break;
9423
9424 case CPP_PLUS_EQ:
9425 id = ansi_assopname (PLUS_EXPR);
9426 break;
9427
9428 case CPP_MINUS_EQ:
9429 id = ansi_assopname (MINUS_EXPR);
9430 break;
9431
9432 case CPP_MULT_EQ:
9433 id = ansi_assopname (MULT_EXPR);
9434 break;
9435
9436 case CPP_DIV_EQ:
9437 id = ansi_assopname (TRUNC_DIV_EXPR);
9438 break;
9439
9440 case CPP_MOD_EQ:
9441 id = ansi_assopname (TRUNC_MOD_EXPR);
9442 break;
9443
9444 case CPP_XOR_EQ:
9445 id = ansi_assopname (BIT_XOR_EXPR);
9446 break;
9447
9448 case CPP_AND_EQ:
9449 id = ansi_assopname (BIT_AND_EXPR);
9450 break;
9451
9452 case CPP_OR_EQ:
9453 id = ansi_assopname (BIT_IOR_EXPR);
9454 break;
9455
9456 case CPP_LSHIFT:
9457 id = ansi_opname (LSHIFT_EXPR);
9458 break;
9459
9460 case CPP_RSHIFT:
9461 id = ansi_opname (RSHIFT_EXPR);
9462 break;
9463
9464 case CPP_LSHIFT_EQ:
9465 id = ansi_assopname (LSHIFT_EXPR);
9466 break;
9467
9468 case CPP_RSHIFT_EQ:
9469 id = ansi_assopname (RSHIFT_EXPR);
9470 break;
9471
9472 case CPP_EQ_EQ:
9473 id = ansi_opname (EQ_EXPR);
9474 break;
9475
9476 case CPP_NOT_EQ:
9477 id = ansi_opname (NE_EXPR);
9478 break;
9479
9480 case CPP_LESS_EQ:
9481 id = ansi_opname (LE_EXPR);
9482 break;
9483
9484 case CPP_GREATER_EQ:
9485 id = ansi_opname (GE_EXPR);
9486 break;
9487
9488 case CPP_AND_AND:
9489 id = ansi_opname (TRUTH_ANDIF_EXPR);
9490 break;
9491
9492 case CPP_OR_OR:
9493 id = ansi_opname (TRUTH_ORIF_EXPR);
9494 break;
9495
9496 case CPP_PLUS_PLUS:
9497 id = ansi_opname (POSTINCREMENT_EXPR);
9498 break;
9499
9500 case CPP_MINUS_MINUS:
9501 id = ansi_opname (PREDECREMENT_EXPR);
9502 break;
9503
9504 case CPP_COMMA:
9505 id = ansi_opname (COMPOUND_EXPR);
9506 break;
9507
9508 case CPP_DEREF_STAR:
9509 id = ansi_opname (MEMBER_REF);
9510 break;
9511
9512 case CPP_DEREF:
9513 id = ansi_opname (COMPONENT_REF);
9514 break;
9515
9516 case CPP_OPEN_PAREN:
9517 /* Consume the `('. */
9518 cp_lexer_consume_token (parser->lexer);
9519 /* Look for the matching `)'. */
9520 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9521 return ansi_opname (CALL_EXPR);
9522
9523 case CPP_OPEN_SQUARE:
9524 /* Consume the `['. */
9525 cp_lexer_consume_token (parser->lexer);
9526 /* Look for the matching `]'. */
9527 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9528 return ansi_opname (ARRAY_REF);
9529
9530 default:
9531 /* Anything else is an error. */
9532 break;
9533 }
9534
9535 /* If we have selected an identifier, we need to consume the
9536 operator token. */
9537 if (id)
9538 cp_lexer_consume_token (parser->lexer);
9539 /* Otherwise, no valid operator name was present. */
9540 else
9541 {
9542 cp_parser_error (parser, "expected operator");
9543 id = error_mark_node;
9544 }
9545
9546 return id;
9547 }
9548
9549 /* Parse a template-declaration.
9550
9551 template-declaration:
9552 export [opt] template < template-parameter-list > declaration
9553
9554 If MEMBER_P is TRUE, this template-declaration occurs within a
9555 class-specifier.
9556
9557 The grammar rule given by the standard isn't correct. What
9558 is really meant is:
9559
9560 template-declaration:
9561 export [opt] template-parameter-list-seq
9562 decl-specifier-seq [opt] init-declarator [opt] ;
9563 export [opt] template-parameter-list-seq
9564 function-definition
9565
9566 template-parameter-list-seq:
9567 template-parameter-list-seq [opt]
9568 template < template-parameter-list > */
9569
9570 static void
9571 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9572 {
9573 /* Check for `export'. */
9574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9575 {
9576 /* Consume the `export' token. */
9577 cp_lexer_consume_token (parser->lexer);
9578 /* Warn that we do not support `export'. */
9579 warning (0, "keyword %<export%> not implemented, and will be ignored");
9580 }
9581
9582 cp_parser_template_declaration_after_export (parser, member_p);
9583 }
9584
9585 /* Parse a template-parameter-list.
9586
9587 template-parameter-list:
9588 template-parameter
9589 template-parameter-list , template-parameter
9590
9591 Returns a TREE_LIST. Each node represents a template parameter.
9592 The nodes are connected via their TREE_CHAINs. */
9593
9594 static tree
9595 cp_parser_template_parameter_list (cp_parser* parser)
9596 {
9597 tree parameter_list = NULL_TREE;
9598
9599 begin_template_parm_list ();
9600 while (true)
9601 {
9602 tree parameter;
9603 bool is_non_type;
9604 bool is_parameter_pack;
9605
9606 /* Parse the template-parameter. */
9607 parameter = cp_parser_template_parameter (parser,
9608 &is_non_type,
9609 &is_parameter_pack);
9610 /* Add it to the list. */
9611 if (parameter != error_mark_node)
9612 parameter_list = process_template_parm (parameter_list,
9613 parameter,
9614 is_non_type,
9615 is_parameter_pack);
9616 else
9617 {
9618 tree err_parm = build_tree_list (parameter, parameter);
9619 TREE_VALUE (err_parm) = error_mark_node;
9620 parameter_list = chainon (parameter_list, err_parm);
9621 }
9622
9623 /* If the next token is not a `,', we're done. */
9624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9625 break;
9626 /* Otherwise, consume the `,' token. */
9627 cp_lexer_consume_token (parser->lexer);
9628 }
9629
9630 return end_template_parm_list (parameter_list);
9631 }
9632
9633 /* Parse a template-parameter.
9634
9635 template-parameter:
9636 type-parameter
9637 parameter-declaration
9638
9639 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9640 the parameter. The TREE_PURPOSE is the default value, if any.
9641 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9642 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9643 set to true iff this parameter is a parameter pack. */
9644
9645 static tree
9646 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9647 bool *is_parameter_pack)
9648 {
9649 cp_token *token;
9650 cp_parameter_declarator *parameter_declarator;
9651 cp_declarator *id_declarator;
9652 tree parm;
9653
9654 /* Assume it is a type parameter or a template parameter. */
9655 *is_non_type = false;
9656 /* Assume it not a parameter pack. */
9657 *is_parameter_pack = false;
9658 /* Peek at the next token. */
9659 token = cp_lexer_peek_token (parser->lexer);
9660 /* If it is `class' or `template', we have a type-parameter. */
9661 if (token->keyword == RID_TEMPLATE)
9662 return cp_parser_type_parameter (parser, is_parameter_pack);
9663 /* If it is `class' or `typename' we do not know yet whether it is a
9664 type parameter or a non-type parameter. Consider:
9665
9666 template <typename T, typename T::X X> ...
9667
9668 or:
9669
9670 template <class C, class D*> ...
9671
9672 Here, the first parameter is a type parameter, and the second is
9673 a non-type parameter. We can tell by looking at the token after
9674 the identifier -- if it is a `,', `=', or `>' then we have a type
9675 parameter. */
9676 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9677 {
9678 /* Peek at the token after `class' or `typename'. */
9679 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9680 /* If it's an ellipsis, we have a template type parameter
9681 pack. */
9682 if (token->type == CPP_ELLIPSIS)
9683 return cp_parser_type_parameter (parser, is_parameter_pack);
9684 /* If it's an identifier, skip it. */
9685 if (token->type == CPP_NAME)
9686 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9687 /* Now, see if the token looks like the end of a template
9688 parameter. */
9689 if (token->type == CPP_COMMA
9690 || token->type == CPP_EQ
9691 || token->type == CPP_GREATER)
9692 return cp_parser_type_parameter (parser, is_parameter_pack);
9693 }
9694
9695 /* Otherwise, it is a non-type parameter.
9696
9697 [temp.param]
9698
9699 When parsing a default template-argument for a non-type
9700 template-parameter, the first non-nested `>' is taken as the end
9701 of the template parameter-list rather than a greater-than
9702 operator. */
9703 *is_non_type = true;
9704 parameter_declarator
9705 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9706 /*parenthesized_p=*/NULL);
9707
9708 /* If the parameter declaration is marked as a parameter pack, set
9709 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9710 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9711 grokdeclarator. */
9712 if (parameter_declarator
9713 && parameter_declarator->declarator
9714 && parameter_declarator->declarator->parameter_pack_p)
9715 {
9716 *is_parameter_pack = true;
9717 parameter_declarator->declarator->parameter_pack_p = false;
9718 }
9719
9720 /* If the next token is an ellipsis, and we don't already have it
9721 marked as a parameter pack, then we have a parameter pack (that
9722 has no declarator). */
9723 if (!*is_parameter_pack
9724 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9725 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9726 {
9727 /* Consume the `...'. */
9728 cp_lexer_consume_token (parser->lexer);
9729 maybe_warn_variadic_templates ();
9730
9731 *is_parameter_pack = true;
9732 }
9733 /* We might end up with a pack expansion as the type of the non-type
9734 template parameter, in which case this is a non-type template
9735 parameter pack. */
9736 else if (parameter_declarator
9737 && parameter_declarator->decl_specifiers.type
9738 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9739 {
9740 *is_parameter_pack = true;
9741 parameter_declarator->decl_specifiers.type =
9742 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9743 }
9744
9745 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9746 {
9747 /* Parameter packs cannot have default arguments. However, a
9748 user may try to do so, so we'll parse them and give an
9749 appropriate diagnostic here. */
9750
9751 /* Consume the `='. */
9752 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9753 cp_lexer_consume_token (parser->lexer);
9754
9755 /* Find the name of the parameter pack. */
9756 id_declarator = parameter_declarator->declarator;
9757 while (id_declarator && id_declarator->kind != cdk_id)
9758 id_declarator = id_declarator->declarator;
9759
9760 if (id_declarator && id_declarator->kind == cdk_id)
9761 error ("%Htemplate parameter pack %qD cannot have a default argument",
9762 &start_token->location, id_declarator->u.id.unqualified_name);
9763 else
9764 error ("%Htemplate parameter pack cannot have a default argument",
9765 &start_token->location);
9766
9767 /* Parse the default argument, but throw away the result. */
9768 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9769 }
9770
9771 parm = grokdeclarator (parameter_declarator->declarator,
9772 &parameter_declarator->decl_specifiers,
9773 PARM, /*initialized=*/0,
9774 /*attrlist=*/NULL);
9775 if (parm == error_mark_node)
9776 return error_mark_node;
9777
9778 return build_tree_list (parameter_declarator->default_argument, parm);
9779 }
9780
9781 /* Parse a type-parameter.
9782
9783 type-parameter:
9784 class identifier [opt]
9785 class identifier [opt] = type-id
9786 typename identifier [opt]
9787 typename identifier [opt] = type-id
9788 template < template-parameter-list > class identifier [opt]
9789 template < template-parameter-list > class identifier [opt]
9790 = id-expression
9791
9792 GNU Extension (variadic templates):
9793
9794 type-parameter:
9795 class ... identifier [opt]
9796 typename ... identifier [opt]
9797
9798 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9799 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9800 the declaration of the parameter.
9801
9802 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9803
9804 static tree
9805 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9806 {
9807 cp_token *token;
9808 tree parameter;
9809
9810 /* Look for a keyword to tell us what kind of parameter this is. */
9811 token = cp_parser_require (parser, CPP_KEYWORD,
9812 "%<class%>, %<typename%>, or %<template%>");
9813 if (!token)
9814 return error_mark_node;
9815
9816 switch (token->keyword)
9817 {
9818 case RID_CLASS:
9819 case RID_TYPENAME:
9820 {
9821 tree identifier;
9822 tree default_argument;
9823
9824 /* If the next token is an ellipsis, we have a template
9825 argument pack. */
9826 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9827 {
9828 /* Consume the `...' token. */
9829 cp_lexer_consume_token (parser->lexer);
9830 maybe_warn_variadic_templates ();
9831
9832 *is_parameter_pack = true;
9833 }
9834
9835 /* If the next token is an identifier, then it names the
9836 parameter. */
9837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9838 identifier = cp_parser_identifier (parser);
9839 else
9840 identifier = NULL_TREE;
9841
9842 /* Create the parameter. */
9843 parameter = finish_template_type_parm (class_type_node, identifier);
9844
9845 /* If the next token is an `=', we have a default argument. */
9846 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9847 {
9848 /* Consume the `=' token. */
9849 cp_lexer_consume_token (parser->lexer);
9850 /* Parse the default-argument. */
9851 push_deferring_access_checks (dk_no_deferred);
9852 default_argument = cp_parser_type_id (parser);
9853
9854 /* Template parameter packs cannot have default
9855 arguments. */
9856 if (*is_parameter_pack)
9857 {
9858 if (identifier)
9859 error ("%Htemplate parameter pack %qD cannot have a "
9860 "default argument", &token->location, identifier);
9861 else
9862 error ("%Htemplate parameter packs cannot have "
9863 "default arguments", &token->location);
9864 default_argument = NULL_TREE;
9865 }
9866 pop_deferring_access_checks ();
9867 }
9868 else
9869 default_argument = NULL_TREE;
9870
9871 /* Create the combined representation of the parameter and the
9872 default argument. */
9873 parameter = build_tree_list (default_argument, parameter);
9874 }
9875 break;
9876
9877 case RID_TEMPLATE:
9878 {
9879 tree parameter_list;
9880 tree identifier;
9881 tree default_argument;
9882
9883 /* Look for the `<'. */
9884 cp_parser_require (parser, CPP_LESS, "%<<%>");
9885 /* Parse the template-parameter-list. */
9886 parameter_list = cp_parser_template_parameter_list (parser);
9887 /* Look for the `>'. */
9888 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9889 /* Look for the `class' keyword. */
9890 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9891 /* If the next token is an ellipsis, we have a template
9892 argument pack. */
9893 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9894 {
9895 /* Consume the `...' token. */
9896 cp_lexer_consume_token (parser->lexer);
9897 maybe_warn_variadic_templates ();
9898
9899 *is_parameter_pack = true;
9900 }
9901 /* If the next token is an `=', then there is a
9902 default-argument. If the next token is a `>', we are at
9903 the end of the parameter-list. If the next token is a `,',
9904 then we are at the end of this parameter. */
9905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9906 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9907 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9908 {
9909 identifier = cp_parser_identifier (parser);
9910 /* Treat invalid names as if the parameter were nameless. */
9911 if (identifier == error_mark_node)
9912 identifier = NULL_TREE;
9913 }
9914 else
9915 identifier = NULL_TREE;
9916
9917 /* Create the template parameter. */
9918 parameter = finish_template_template_parm (class_type_node,
9919 identifier);
9920
9921 /* If the next token is an `=', then there is a
9922 default-argument. */
9923 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9924 {
9925 bool is_template;
9926
9927 /* Consume the `='. */
9928 cp_lexer_consume_token (parser->lexer);
9929 /* Parse the id-expression. */
9930 push_deferring_access_checks (dk_no_deferred);
9931 /* save token before parsing the id-expression, for error
9932 reporting */
9933 token = cp_lexer_peek_token (parser->lexer);
9934 default_argument
9935 = cp_parser_id_expression (parser,
9936 /*template_keyword_p=*/false,
9937 /*check_dependency_p=*/true,
9938 /*template_p=*/&is_template,
9939 /*declarator_p=*/false,
9940 /*optional_p=*/false);
9941 if (TREE_CODE (default_argument) == TYPE_DECL)
9942 /* If the id-expression was a template-id that refers to
9943 a template-class, we already have the declaration here,
9944 so no further lookup is needed. */
9945 ;
9946 else
9947 /* Look up the name. */
9948 default_argument
9949 = cp_parser_lookup_name (parser, default_argument,
9950 none_type,
9951 /*is_template=*/is_template,
9952 /*is_namespace=*/false,
9953 /*check_dependency=*/true,
9954 /*ambiguous_decls=*/NULL,
9955 token->location);
9956 /* See if the default argument is valid. */
9957 default_argument
9958 = check_template_template_default_arg (default_argument);
9959
9960 /* Template parameter packs cannot have default
9961 arguments. */
9962 if (*is_parameter_pack)
9963 {
9964 if (identifier)
9965 error ("%Htemplate parameter pack %qD cannot "
9966 "have a default argument",
9967 &token->location, identifier);
9968 else
9969 error ("%Htemplate parameter packs cannot "
9970 "have default arguments",
9971 &token->location);
9972 default_argument = NULL_TREE;
9973 }
9974 pop_deferring_access_checks ();
9975 }
9976 else
9977 default_argument = NULL_TREE;
9978
9979 /* Create the combined representation of the parameter and the
9980 default argument. */
9981 parameter = build_tree_list (default_argument, parameter);
9982 }
9983 break;
9984
9985 default:
9986 gcc_unreachable ();
9987 break;
9988 }
9989
9990 return parameter;
9991 }
9992
9993 /* Parse a template-id.
9994
9995 template-id:
9996 template-name < template-argument-list [opt] >
9997
9998 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9999 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10000 returned. Otherwise, if the template-name names a function, or set
10001 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10002 names a class, returns a TYPE_DECL for the specialization.
10003
10004 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10005 uninstantiated templates. */
10006
10007 static tree
10008 cp_parser_template_id (cp_parser *parser,
10009 bool template_keyword_p,
10010 bool check_dependency_p,
10011 bool is_declaration)
10012 {
10013 int i;
10014 tree templ;
10015 tree arguments;
10016 tree template_id;
10017 cp_token_position start_of_id = 0;
10018 deferred_access_check *chk;
10019 VEC (deferred_access_check,gc) *access_check;
10020 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10021 bool is_identifier;
10022
10023 /* If the next token corresponds to a template-id, there is no need
10024 to reparse it. */
10025 next_token = cp_lexer_peek_token (parser->lexer);
10026 if (next_token->type == CPP_TEMPLATE_ID)
10027 {
10028 struct tree_check *check_value;
10029
10030 /* Get the stored value. */
10031 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10032 /* Perform any access checks that were deferred. */
10033 access_check = check_value->checks;
10034 if (access_check)
10035 {
10036 for (i = 0 ;
10037 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10038 ++i)
10039 {
10040 perform_or_defer_access_check (chk->binfo,
10041 chk->decl,
10042 chk->diag_decl);
10043 }
10044 }
10045 /* Return the stored value. */
10046 return check_value->value;
10047 }
10048
10049 /* Avoid performing name lookup if there is no possibility of
10050 finding a template-id. */
10051 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10052 || (next_token->type == CPP_NAME
10053 && !cp_parser_nth_token_starts_template_argument_list_p
10054 (parser, 2)))
10055 {
10056 cp_parser_error (parser, "expected template-id");
10057 return error_mark_node;
10058 }
10059
10060 /* Remember where the template-id starts. */
10061 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10062 start_of_id = cp_lexer_token_position (parser->lexer, false);
10063
10064 push_deferring_access_checks (dk_deferred);
10065
10066 /* Parse the template-name. */
10067 is_identifier = false;
10068 token = cp_lexer_peek_token (parser->lexer);
10069 templ = cp_parser_template_name (parser, template_keyword_p,
10070 check_dependency_p,
10071 is_declaration,
10072 &is_identifier);
10073 if (templ == error_mark_node || is_identifier)
10074 {
10075 pop_deferring_access_checks ();
10076 return templ;
10077 }
10078
10079 /* If we find the sequence `[:' after a template-name, it's probably
10080 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10081 parse correctly the argument list. */
10082 next_token = cp_lexer_peek_token (parser->lexer);
10083 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10084 if (next_token->type == CPP_OPEN_SQUARE
10085 && next_token->flags & DIGRAPH
10086 && next_token_2->type == CPP_COLON
10087 && !(next_token_2->flags & PREV_WHITE))
10088 {
10089 cp_parser_parse_tentatively (parser);
10090 /* Change `:' into `::'. */
10091 next_token_2->type = CPP_SCOPE;
10092 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10093 CPP_LESS. */
10094 cp_lexer_consume_token (parser->lexer);
10095
10096 /* Parse the arguments. */
10097 arguments = cp_parser_enclosed_template_argument_list (parser);
10098 if (!cp_parser_parse_definitely (parser))
10099 {
10100 /* If we couldn't parse an argument list, then we revert our changes
10101 and return simply an error. Maybe this is not a template-id
10102 after all. */
10103 next_token_2->type = CPP_COLON;
10104 cp_parser_error (parser, "expected %<<%>");
10105 pop_deferring_access_checks ();
10106 return error_mark_node;
10107 }
10108 /* Otherwise, emit an error about the invalid digraph, but continue
10109 parsing because we got our argument list. */
10110 if (permerror (next_token->location,
10111 "%<<::%> cannot begin a template-argument list"))
10112 {
10113 static bool hint = false;
10114 inform (next_token->location,
10115 "%<<:%> is an alternate spelling for %<[%>."
10116 " Insert whitespace between %<<%> and %<::%>");
10117 if (!hint && !flag_permissive)
10118 {
10119 inform (next_token->location, "(if you use %<-fpermissive%>"
10120 " G++ will accept your code)");
10121 hint = true;
10122 }
10123 }
10124 }
10125 else
10126 {
10127 /* Look for the `<' that starts the template-argument-list. */
10128 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10129 {
10130 pop_deferring_access_checks ();
10131 return error_mark_node;
10132 }
10133 /* Parse the arguments. */
10134 arguments = cp_parser_enclosed_template_argument_list (parser);
10135 }
10136
10137 /* Build a representation of the specialization. */
10138 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10139 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10140 else if (DECL_CLASS_TEMPLATE_P (templ)
10141 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10142 {
10143 bool entering_scope;
10144 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10145 template (rather than some instantiation thereof) only if
10146 is not nested within some other construct. For example, in
10147 "template <typename T> void f(T) { A<T>::", A<T> is just an
10148 instantiation of A. */
10149 entering_scope = (template_parm_scope_p ()
10150 && cp_lexer_next_token_is (parser->lexer,
10151 CPP_SCOPE));
10152 template_id
10153 = finish_template_type (templ, arguments, entering_scope);
10154 }
10155 else
10156 {
10157 /* If it's not a class-template or a template-template, it should be
10158 a function-template. */
10159 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10160 || TREE_CODE (templ) == OVERLOAD
10161 || BASELINK_P (templ)));
10162
10163 template_id = lookup_template_function (templ, arguments);
10164 }
10165
10166 /* If parsing tentatively, replace the sequence of tokens that makes
10167 up the template-id with a CPP_TEMPLATE_ID token. That way,
10168 should we re-parse the token stream, we will not have to repeat
10169 the effort required to do the parse, nor will we issue duplicate
10170 error messages about problems during instantiation of the
10171 template. */
10172 if (start_of_id)
10173 {
10174 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10175
10176 /* Reset the contents of the START_OF_ID token. */
10177 token->type = CPP_TEMPLATE_ID;
10178 /* Retrieve any deferred checks. Do not pop this access checks yet
10179 so the memory will not be reclaimed during token replacing below. */
10180 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10181 token->u.tree_check_value->value = template_id;
10182 token->u.tree_check_value->checks = get_deferred_access_checks ();
10183 token->keyword = RID_MAX;
10184
10185 /* Purge all subsequent tokens. */
10186 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10187
10188 /* ??? Can we actually assume that, if template_id ==
10189 error_mark_node, we will have issued a diagnostic to the
10190 user, as opposed to simply marking the tentative parse as
10191 failed? */
10192 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10193 error ("%Hparse error in template argument list",
10194 &token->location);
10195 }
10196
10197 pop_deferring_access_checks ();
10198 return template_id;
10199 }
10200
10201 /* Parse a template-name.
10202
10203 template-name:
10204 identifier
10205
10206 The standard should actually say:
10207
10208 template-name:
10209 identifier
10210 operator-function-id
10211
10212 A defect report has been filed about this issue.
10213
10214 A conversion-function-id cannot be a template name because they cannot
10215 be part of a template-id. In fact, looking at this code:
10216
10217 a.operator K<int>()
10218
10219 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10220 It is impossible to call a templated conversion-function-id with an
10221 explicit argument list, since the only allowed template parameter is
10222 the type to which it is converting.
10223
10224 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10225 `template' keyword, in a construction like:
10226
10227 T::template f<3>()
10228
10229 In that case `f' is taken to be a template-name, even though there
10230 is no way of knowing for sure.
10231
10232 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10233 name refers to a set of overloaded functions, at least one of which
10234 is a template, or an IDENTIFIER_NODE with the name of the template,
10235 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10236 names are looked up inside uninstantiated templates. */
10237
10238 static tree
10239 cp_parser_template_name (cp_parser* parser,
10240 bool template_keyword_p,
10241 bool check_dependency_p,
10242 bool is_declaration,
10243 bool *is_identifier)
10244 {
10245 tree identifier;
10246 tree decl;
10247 tree fns;
10248 cp_token *token = cp_lexer_peek_token (parser->lexer);
10249
10250 /* If the next token is `operator', then we have either an
10251 operator-function-id or a conversion-function-id. */
10252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10253 {
10254 /* We don't know whether we're looking at an
10255 operator-function-id or a conversion-function-id. */
10256 cp_parser_parse_tentatively (parser);
10257 /* Try an operator-function-id. */
10258 identifier = cp_parser_operator_function_id (parser);
10259 /* If that didn't work, try a conversion-function-id. */
10260 if (!cp_parser_parse_definitely (parser))
10261 {
10262 cp_parser_error (parser, "expected template-name");
10263 return error_mark_node;
10264 }
10265 }
10266 /* Look for the identifier. */
10267 else
10268 identifier = cp_parser_identifier (parser);
10269
10270 /* If we didn't find an identifier, we don't have a template-id. */
10271 if (identifier == error_mark_node)
10272 return error_mark_node;
10273
10274 /* If the name immediately followed the `template' keyword, then it
10275 is a template-name. However, if the next token is not `<', then
10276 we do not treat it as a template-name, since it is not being used
10277 as part of a template-id. This enables us to handle constructs
10278 like:
10279
10280 template <typename T> struct S { S(); };
10281 template <typename T> S<T>::S();
10282
10283 correctly. We would treat `S' as a template -- if it were `S<T>'
10284 -- but we do not if there is no `<'. */
10285
10286 if (processing_template_decl
10287 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10288 {
10289 /* In a declaration, in a dependent context, we pretend that the
10290 "template" keyword was present in order to improve error
10291 recovery. For example, given:
10292
10293 template <typename T> void f(T::X<int>);
10294
10295 we want to treat "X<int>" as a template-id. */
10296 if (is_declaration
10297 && !template_keyword_p
10298 && parser->scope && TYPE_P (parser->scope)
10299 && check_dependency_p
10300 && dependent_type_p (parser->scope)
10301 /* Do not do this for dtors (or ctors), since they never
10302 need the template keyword before their name. */
10303 && !constructor_name_p (identifier, parser->scope))
10304 {
10305 cp_token_position start = 0;
10306
10307 /* Explain what went wrong. */
10308 error ("%Hnon-template %qD used as template",
10309 &token->location, identifier);
10310 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10311 parser->scope, identifier);
10312 /* If parsing tentatively, find the location of the "<" token. */
10313 if (cp_parser_simulate_error (parser))
10314 start = cp_lexer_token_position (parser->lexer, true);
10315 /* Parse the template arguments so that we can issue error
10316 messages about them. */
10317 cp_lexer_consume_token (parser->lexer);
10318 cp_parser_enclosed_template_argument_list (parser);
10319 /* Skip tokens until we find a good place from which to
10320 continue parsing. */
10321 cp_parser_skip_to_closing_parenthesis (parser,
10322 /*recovering=*/true,
10323 /*or_comma=*/true,
10324 /*consume_paren=*/false);
10325 /* If parsing tentatively, permanently remove the
10326 template argument list. That will prevent duplicate
10327 error messages from being issued about the missing
10328 "template" keyword. */
10329 if (start)
10330 cp_lexer_purge_tokens_after (parser->lexer, start);
10331 if (is_identifier)
10332 *is_identifier = true;
10333 return identifier;
10334 }
10335
10336 /* If the "template" keyword is present, then there is generally
10337 no point in doing name-lookup, so we just return IDENTIFIER.
10338 But, if the qualifying scope is non-dependent then we can
10339 (and must) do name-lookup normally. */
10340 if (template_keyword_p
10341 && (!parser->scope
10342 || (TYPE_P (parser->scope)
10343 && dependent_type_p (parser->scope))))
10344 return identifier;
10345 }
10346
10347 /* Look up the name. */
10348 decl = cp_parser_lookup_name (parser, identifier,
10349 none_type,
10350 /*is_template=*/false,
10351 /*is_namespace=*/false,
10352 check_dependency_p,
10353 /*ambiguous_decls=*/NULL,
10354 token->location);
10355 decl = maybe_get_template_decl_from_type_decl (decl);
10356
10357 /* If DECL is a template, then the name was a template-name. */
10358 if (TREE_CODE (decl) == TEMPLATE_DECL)
10359 ;
10360 else
10361 {
10362 tree fn = NULL_TREE;
10363
10364 /* The standard does not explicitly indicate whether a name that
10365 names a set of overloaded declarations, some of which are
10366 templates, is a template-name. However, such a name should
10367 be a template-name; otherwise, there is no way to form a
10368 template-id for the overloaded templates. */
10369 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10370 if (TREE_CODE (fns) == OVERLOAD)
10371 for (fn = fns; fn; fn = OVL_NEXT (fn))
10372 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10373 break;
10374
10375 if (!fn)
10376 {
10377 /* The name does not name a template. */
10378 cp_parser_error (parser, "expected template-name");
10379 return error_mark_node;
10380 }
10381 }
10382
10383 /* If DECL is dependent, and refers to a function, then just return
10384 its name; we will look it up again during template instantiation. */
10385 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10386 {
10387 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10388 if (TYPE_P (scope) && dependent_type_p (scope))
10389 return identifier;
10390 }
10391
10392 return decl;
10393 }
10394
10395 /* Parse a template-argument-list.
10396
10397 template-argument-list:
10398 template-argument ... [opt]
10399 template-argument-list , template-argument ... [opt]
10400
10401 Returns a TREE_VEC containing the arguments. */
10402
10403 static tree
10404 cp_parser_template_argument_list (cp_parser* parser)
10405 {
10406 tree fixed_args[10];
10407 unsigned n_args = 0;
10408 unsigned alloced = 10;
10409 tree *arg_ary = fixed_args;
10410 tree vec;
10411 bool saved_in_template_argument_list_p;
10412 bool saved_ice_p;
10413 bool saved_non_ice_p;
10414
10415 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10416 parser->in_template_argument_list_p = true;
10417 /* Even if the template-id appears in an integral
10418 constant-expression, the contents of the argument list do
10419 not. */
10420 saved_ice_p = parser->integral_constant_expression_p;
10421 parser->integral_constant_expression_p = false;
10422 saved_non_ice_p = parser->non_integral_constant_expression_p;
10423 parser->non_integral_constant_expression_p = false;
10424 /* Parse the arguments. */
10425 do
10426 {
10427 tree argument;
10428
10429 if (n_args)
10430 /* Consume the comma. */
10431 cp_lexer_consume_token (parser->lexer);
10432
10433 /* Parse the template-argument. */
10434 argument = cp_parser_template_argument (parser);
10435
10436 /* If the next token is an ellipsis, we're expanding a template
10437 argument pack. */
10438 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10439 {
10440 /* Consume the `...' token. */
10441 cp_lexer_consume_token (parser->lexer);
10442
10443 /* Make the argument into a TYPE_PACK_EXPANSION or
10444 EXPR_PACK_EXPANSION. */
10445 argument = make_pack_expansion (argument);
10446 }
10447
10448 if (n_args == alloced)
10449 {
10450 alloced *= 2;
10451
10452 if (arg_ary == fixed_args)
10453 {
10454 arg_ary = XNEWVEC (tree, alloced);
10455 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10456 }
10457 else
10458 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10459 }
10460 arg_ary[n_args++] = argument;
10461 }
10462 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10463
10464 vec = make_tree_vec (n_args);
10465
10466 while (n_args--)
10467 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10468
10469 if (arg_ary != fixed_args)
10470 free (arg_ary);
10471 parser->non_integral_constant_expression_p = saved_non_ice_p;
10472 parser->integral_constant_expression_p = saved_ice_p;
10473 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10474 return vec;
10475 }
10476
10477 /* Parse a template-argument.
10478
10479 template-argument:
10480 assignment-expression
10481 type-id
10482 id-expression
10483
10484 The representation is that of an assignment-expression, type-id, or
10485 id-expression -- except that the qualified id-expression is
10486 evaluated, so that the value returned is either a DECL or an
10487 OVERLOAD.
10488
10489 Although the standard says "assignment-expression", it forbids
10490 throw-expressions or assignments in the template argument.
10491 Therefore, we use "conditional-expression" instead. */
10492
10493 static tree
10494 cp_parser_template_argument (cp_parser* parser)
10495 {
10496 tree argument;
10497 bool template_p;
10498 bool address_p;
10499 bool maybe_type_id = false;
10500 cp_token *token = NULL, *argument_start_token = NULL;
10501 cp_id_kind idk;
10502
10503 /* There's really no way to know what we're looking at, so we just
10504 try each alternative in order.
10505
10506 [temp.arg]
10507
10508 In a template-argument, an ambiguity between a type-id and an
10509 expression is resolved to a type-id, regardless of the form of
10510 the corresponding template-parameter.
10511
10512 Therefore, we try a type-id first. */
10513 cp_parser_parse_tentatively (parser);
10514 argument = cp_parser_type_id (parser);
10515 /* If there was no error parsing the type-id but the next token is a
10516 '>>', our behavior depends on which dialect of C++ we're
10517 parsing. In C++98, we probably found a typo for '> >'. But there
10518 are type-id which are also valid expressions. For instance:
10519
10520 struct X { int operator >> (int); };
10521 template <int V> struct Foo {};
10522 Foo<X () >> 5> r;
10523
10524 Here 'X()' is a valid type-id of a function type, but the user just
10525 wanted to write the expression "X() >> 5". Thus, we remember that we
10526 found a valid type-id, but we still try to parse the argument as an
10527 expression to see what happens.
10528
10529 In C++0x, the '>>' will be considered two separate '>'
10530 tokens. */
10531 if (!cp_parser_error_occurred (parser)
10532 && cxx_dialect == cxx98
10533 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10534 {
10535 maybe_type_id = true;
10536 cp_parser_abort_tentative_parse (parser);
10537 }
10538 else
10539 {
10540 /* If the next token isn't a `,' or a `>', then this argument wasn't
10541 really finished. This means that the argument is not a valid
10542 type-id. */
10543 if (!cp_parser_next_token_ends_template_argument_p (parser))
10544 cp_parser_error (parser, "expected template-argument");
10545 /* If that worked, we're done. */
10546 if (cp_parser_parse_definitely (parser))
10547 return argument;
10548 }
10549 /* We're still not sure what the argument will be. */
10550 cp_parser_parse_tentatively (parser);
10551 /* Try a template. */
10552 argument_start_token = cp_lexer_peek_token (parser->lexer);
10553 argument = cp_parser_id_expression (parser,
10554 /*template_keyword_p=*/false,
10555 /*check_dependency_p=*/true,
10556 &template_p,
10557 /*declarator_p=*/false,
10558 /*optional_p=*/false);
10559 /* If the next token isn't a `,' or a `>', then this argument wasn't
10560 really finished. */
10561 if (!cp_parser_next_token_ends_template_argument_p (parser))
10562 cp_parser_error (parser, "expected template-argument");
10563 if (!cp_parser_error_occurred (parser))
10564 {
10565 /* Figure out what is being referred to. If the id-expression
10566 was for a class template specialization, then we will have a
10567 TYPE_DECL at this point. There is no need to do name lookup
10568 at this point in that case. */
10569 if (TREE_CODE (argument) != TYPE_DECL)
10570 argument = cp_parser_lookup_name (parser, argument,
10571 none_type,
10572 /*is_template=*/template_p,
10573 /*is_namespace=*/false,
10574 /*check_dependency=*/true,
10575 /*ambiguous_decls=*/NULL,
10576 argument_start_token->location);
10577 if (TREE_CODE (argument) != TEMPLATE_DECL
10578 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10579 cp_parser_error (parser, "expected template-name");
10580 }
10581 if (cp_parser_parse_definitely (parser))
10582 return argument;
10583 /* It must be a non-type argument. There permitted cases are given
10584 in [temp.arg.nontype]:
10585
10586 -- an integral constant-expression of integral or enumeration
10587 type; or
10588
10589 -- the name of a non-type template-parameter; or
10590
10591 -- the name of an object or function with external linkage...
10592
10593 -- the address of an object or function with external linkage...
10594
10595 -- a pointer to member... */
10596 /* Look for a non-type template parameter. */
10597 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10598 {
10599 cp_parser_parse_tentatively (parser);
10600 argument = cp_parser_primary_expression (parser,
10601 /*address_p=*/false,
10602 /*cast_p=*/false,
10603 /*template_arg_p=*/true,
10604 &idk);
10605 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10606 || !cp_parser_next_token_ends_template_argument_p (parser))
10607 cp_parser_simulate_error (parser);
10608 if (cp_parser_parse_definitely (parser))
10609 return argument;
10610 }
10611
10612 /* If the next token is "&", the argument must be the address of an
10613 object or function with external linkage. */
10614 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10615 if (address_p)
10616 cp_lexer_consume_token (parser->lexer);
10617 /* See if we might have an id-expression. */
10618 token = cp_lexer_peek_token (parser->lexer);
10619 if (token->type == CPP_NAME
10620 || token->keyword == RID_OPERATOR
10621 || token->type == CPP_SCOPE
10622 || token->type == CPP_TEMPLATE_ID
10623 || token->type == CPP_NESTED_NAME_SPECIFIER)
10624 {
10625 cp_parser_parse_tentatively (parser);
10626 argument = cp_parser_primary_expression (parser,
10627 address_p,
10628 /*cast_p=*/false,
10629 /*template_arg_p=*/true,
10630 &idk);
10631 if (cp_parser_error_occurred (parser)
10632 || !cp_parser_next_token_ends_template_argument_p (parser))
10633 cp_parser_abort_tentative_parse (parser);
10634 else
10635 {
10636 if (TREE_CODE (argument) == INDIRECT_REF)
10637 {
10638 gcc_assert (REFERENCE_REF_P (argument));
10639 argument = TREE_OPERAND (argument, 0);
10640 }
10641
10642 if (TREE_CODE (argument) == VAR_DECL)
10643 {
10644 /* A variable without external linkage might still be a
10645 valid constant-expression, so no error is issued here
10646 if the external-linkage check fails. */
10647 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10648 cp_parser_simulate_error (parser);
10649 }
10650 else if (is_overloaded_fn (argument))
10651 /* All overloaded functions are allowed; if the external
10652 linkage test does not pass, an error will be issued
10653 later. */
10654 ;
10655 else if (address_p
10656 && (TREE_CODE (argument) == OFFSET_REF
10657 || TREE_CODE (argument) == SCOPE_REF))
10658 /* A pointer-to-member. */
10659 ;
10660 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10661 ;
10662 else
10663 cp_parser_simulate_error (parser);
10664
10665 if (cp_parser_parse_definitely (parser))
10666 {
10667 if (address_p)
10668 argument = build_x_unary_op (ADDR_EXPR, argument,
10669 tf_warning_or_error);
10670 return argument;
10671 }
10672 }
10673 }
10674 /* If the argument started with "&", there are no other valid
10675 alternatives at this point. */
10676 if (address_p)
10677 {
10678 cp_parser_error (parser, "invalid non-type template argument");
10679 return error_mark_node;
10680 }
10681
10682 /* If the argument wasn't successfully parsed as a type-id followed
10683 by '>>', the argument can only be a constant expression now.
10684 Otherwise, we try parsing the constant-expression tentatively,
10685 because the argument could really be a type-id. */
10686 if (maybe_type_id)
10687 cp_parser_parse_tentatively (parser);
10688 argument = cp_parser_constant_expression (parser,
10689 /*allow_non_constant_p=*/false,
10690 /*non_constant_p=*/NULL);
10691 argument = fold_non_dependent_expr (argument);
10692 if (!maybe_type_id)
10693 return argument;
10694 if (!cp_parser_next_token_ends_template_argument_p (parser))
10695 cp_parser_error (parser, "expected template-argument");
10696 if (cp_parser_parse_definitely (parser))
10697 return argument;
10698 /* We did our best to parse the argument as a non type-id, but that
10699 was the only alternative that matched (albeit with a '>' after
10700 it). We can assume it's just a typo from the user, and a
10701 diagnostic will then be issued. */
10702 return cp_parser_type_id (parser);
10703 }
10704
10705 /* Parse an explicit-instantiation.
10706
10707 explicit-instantiation:
10708 template declaration
10709
10710 Although the standard says `declaration', what it really means is:
10711
10712 explicit-instantiation:
10713 template decl-specifier-seq [opt] declarator [opt] ;
10714
10715 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10716 supposed to be allowed. A defect report has been filed about this
10717 issue.
10718
10719 GNU Extension:
10720
10721 explicit-instantiation:
10722 storage-class-specifier template
10723 decl-specifier-seq [opt] declarator [opt] ;
10724 function-specifier template
10725 decl-specifier-seq [opt] declarator [opt] ; */
10726
10727 static void
10728 cp_parser_explicit_instantiation (cp_parser* parser)
10729 {
10730 int declares_class_or_enum;
10731 cp_decl_specifier_seq decl_specifiers;
10732 tree extension_specifier = NULL_TREE;
10733 cp_token *token;
10734
10735 /* Look for an (optional) storage-class-specifier or
10736 function-specifier. */
10737 if (cp_parser_allow_gnu_extensions_p (parser))
10738 {
10739 extension_specifier
10740 = cp_parser_storage_class_specifier_opt (parser);
10741 if (!extension_specifier)
10742 extension_specifier
10743 = cp_parser_function_specifier_opt (parser,
10744 /*decl_specs=*/NULL);
10745 }
10746
10747 /* Look for the `template' keyword. */
10748 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10749 /* Let the front end know that we are processing an explicit
10750 instantiation. */
10751 begin_explicit_instantiation ();
10752 /* [temp.explicit] says that we are supposed to ignore access
10753 control while processing explicit instantiation directives. */
10754 push_deferring_access_checks (dk_no_check);
10755 /* Parse a decl-specifier-seq. */
10756 token = cp_lexer_peek_token (parser->lexer);
10757 cp_parser_decl_specifier_seq (parser,
10758 CP_PARSER_FLAGS_OPTIONAL,
10759 &decl_specifiers,
10760 &declares_class_or_enum);
10761 /* If there was exactly one decl-specifier, and it declared a class,
10762 and there's no declarator, then we have an explicit type
10763 instantiation. */
10764 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10765 {
10766 tree type;
10767
10768 type = check_tag_decl (&decl_specifiers);
10769 /* Turn access control back on for names used during
10770 template instantiation. */
10771 pop_deferring_access_checks ();
10772 if (type)
10773 do_type_instantiation (type, extension_specifier,
10774 /*complain=*/tf_error);
10775 }
10776 else
10777 {
10778 cp_declarator *declarator;
10779 tree decl;
10780
10781 /* Parse the declarator. */
10782 declarator
10783 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10784 /*ctor_dtor_or_conv_p=*/NULL,
10785 /*parenthesized_p=*/NULL,
10786 /*member_p=*/false);
10787 if (declares_class_or_enum & 2)
10788 cp_parser_check_for_definition_in_return_type (declarator,
10789 decl_specifiers.type,
10790 decl_specifiers.type_location);
10791 if (declarator != cp_error_declarator)
10792 {
10793 decl = grokdeclarator (declarator, &decl_specifiers,
10794 NORMAL, 0, &decl_specifiers.attributes);
10795 /* Turn access control back on for names used during
10796 template instantiation. */
10797 pop_deferring_access_checks ();
10798 /* Do the explicit instantiation. */
10799 do_decl_instantiation (decl, extension_specifier);
10800 }
10801 else
10802 {
10803 pop_deferring_access_checks ();
10804 /* Skip the body of the explicit instantiation. */
10805 cp_parser_skip_to_end_of_statement (parser);
10806 }
10807 }
10808 /* We're done with the instantiation. */
10809 end_explicit_instantiation ();
10810
10811 cp_parser_consume_semicolon_at_end_of_statement (parser);
10812 }
10813
10814 /* Parse an explicit-specialization.
10815
10816 explicit-specialization:
10817 template < > declaration
10818
10819 Although the standard says `declaration', what it really means is:
10820
10821 explicit-specialization:
10822 template <> decl-specifier [opt] init-declarator [opt] ;
10823 template <> function-definition
10824 template <> explicit-specialization
10825 template <> template-declaration */
10826
10827 static void
10828 cp_parser_explicit_specialization (cp_parser* parser)
10829 {
10830 bool need_lang_pop;
10831 cp_token *token = cp_lexer_peek_token (parser->lexer);
10832
10833 /* Look for the `template' keyword. */
10834 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10835 /* Look for the `<'. */
10836 cp_parser_require (parser, CPP_LESS, "%<<%>");
10837 /* Look for the `>'. */
10838 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10839 /* We have processed another parameter list. */
10840 ++parser->num_template_parameter_lists;
10841 /* [temp]
10842
10843 A template ... explicit specialization ... shall not have C
10844 linkage. */
10845 if (current_lang_name == lang_name_c)
10846 {
10847 error ("%Htemplate specialization with C linkage", &token->location);
10848 /* Give it C++ linkage to avoid confusing other parts of the
10849 front end. */
10850 push_lang_context (lang_name_cplusplus);
10851 need_lang_pop = true;
10852 }
10853 else
10854 need_lang_pop = false;
10855 /* Let the front end know that we are beginning a specialization. */
10856 if (!begin_specialization ())
10857 {
10858 end_specialization ();
10859 cp_parser_skip_to_end_of_block_or_statement (parser);
10860 return;
10861 }
10862
10863 /* If the next keyword is `template', we need to figure out whether
10864 or not we're looking a template-declaration. */
10865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10866 {
10867 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10868 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10869 cp_parser_template_declaration_after_export (parser,
10870 /*member_p=*/false);
10871 else
10872 cp_parser_explicit_specialization (parser);
10873 }
10874 else
10875 /* Parse the dependent declaration. */
10876 cp_parser_single_declaration (parser,
10877 /*checks=*/NULL,
10878 /*member_p=*/false,
10879 /*explicit_specialization_p=*/true,
10880 /*friend_p=*/NULL);
10881 /* We're done with the specialization. */
10882 end_specialization ();
10883 /* For the erroneous case of a template with C linkage, we pushed an
10884 implicit C++ linkage scope; exit that scope now. */
10885 if (need_lang_pop)
10886 pop_lang_context ();
10887 /* We're done with this parameter list. */
10888 --parser->num_template_parameter_lists;
10889 }
10890
10891 /* Parse a type-specifier.
10892
10893 type-specifier:
10894 simple-type-specifier
10895 class-specifier
10896 enum-specifier
10897 elaborated-type-specifier
10898 cv-qualifier
10899
10900 GNU Extension:
10901
10902 type-specifier:
10903 __complex__
10904
10905 Returns a representation of the type-specifier. For a
10906 class-specifier, enum-specifier, or elaborated-type-specifier, a
10907 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10908
10909 The parser flags FLAGS is used to control type-specifier parsing.
10910
10911 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10912 in a decl-specifier-seq.
10913
10914 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10915 class-specifier, enum-specifier, or elaborated-type-specifier, then
10916 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10917 if a type is declared; 2 if it is defined. Otherwise, it is set to
10918 zero.
10919
10920 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10921 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10922 is set to FALSE. */
10923
10924 static tree
10925 cp_parser_type_specifier (cp_parser* parser,
10926 cp_parser_flags flags,
10927 cp_decl_specifier_seq *decl_specs,
10928 bool is_declaration,
10929 int* declares_class_or_enum,
10930 bool* is_cv_qualifier)
10931 {
10932 tree type_spec = NULL_TREE;
10933 cp_token *token;
10934 enum rid keyword;
10935 cp_decl_spec ds = ds_last;
10936
10937 /* Assume this type-specifier does not declare a new type. */
10938 if (declares_class_or_enum)
10939 *declares_class_or_enum = 0;
10940 /* And that it does not specify a cv-qualifier. */
10941 if (is_cv_qualifier)
10942 *is_cv_qualifier = false;
10943 /* Peek at the next token. */
10944 token = cp_lexer_peek_token (parser->lexer);
10945
10946 /* If we're looking at a keyword, we can use that to guide the
10947 production we choose. */
10948 keyword = token->keyword;
10949 switch (keyword)
10950 {
10951 case RID_ENUM:
10952 /* Look for the enum-specifier. */
10953 type_spec = cp_parser_enum_specifier (parser);
10954 /* If that worked, we're done. */
10955 if (type_spec)
10956 {
10957 if (declares_class_or_enum)
10958 *declares_class_or_enum = 2;
10959 if (decl_specs)
10960 cp_parser_set_decl_spec_type (decl_specs,
10961 type_spec,
10962 token->location,
10963 /*user_defined_p=*/true);
10964 return type_spec;
10965 }
10966 else
10967 goto elaborated_type_specifier;
10968
10969 /* Any of these indicate either a class-specifier, or an
10970 elaborated-type-specifier. */
10971 case RID_CLASS:
10972 case RID_STRUCT:
10973 case RID_UNION:
10974 /* Parse tentatively so that we can back up if we don't find a
10975 class-specifier. */
10976 cp_parser_parse_tentatively (parser);
10977 /* Look for the class-specifier. */
10978 type_spec = cp_parser_class_specifier (parser);
10979 /* If that worked, we're done. */
10980 if (cp_parser_parse_definitely (parser))
10981 {
10982 if (declares_class_or_enum)
10983 *declares_class_or_enum = 2;
10984 if (decl_specs)
10985 cp_parser_set_decl_spec_type (decl_specs,
10986 type_spec,
10987 token->location,
10988 /*user_defined_p=*/true);
10989 return type_spec;
10990 }
10991
10992 /* Fall through. */
10993 elaborated_type_specifier:
10994 /* We're declaring (not defining) a class or enum. */
10995 if (declares_class_or_enum)
10996 *declares_class_or_enum = 1;
10997
10998 /* Fall through. */
10999 case RID_TYPENAME:
11000 /* Look for an elaborated-type-specifier. */
11001 type_spec
11002 = (cp_parser_elaborated_type_specifier
11003 (parser,
11004 decl_specs && decl_specs->specs[(int) ds_friend],
11005 is_declaration));
11006 if (decl_specs)
11007 cp_parser_set_decl_spec_type (decl_specs,
11008 type_spec,
11009 token->location,
11010 /*user_defined_p=*/true);
11011 return type_spec;
11012
11013 case RID_CONST:
11014 ds = ds_const;
11015 if (is_cv_qualifier)
11016 *is_cv_qualifier = true;
11017 break;
11018
11019 case RID_VOLATILE:
11020 ds = ds_volatile;
11021 if (is_cv_qualifier)
11022 *is_cv_qualifier = true;
11023 break;
11024
11025 case RID_RESTRICT:
11026 ds = ds_restrict;
11027 if (is_cv_qualifier)
11028 *is_cv_qualifier = true;
11029 break;
11030
11031 case RID_COMPLEX:
11032 /* The `__complex__' keyword is a GNU extension. */
11033 ds = ds_complex;
11034 break;
11035
11036 default:
11037 break;
11038 }
11039
11040 /* Handle simple keywords. */
11041 if (ds != ds_last)
11042 {
11043 if (decl_specs)
11044 {
11045 ++decl_specs->specs[(int)ds];
11046 decl_specs->any_specifiers_p = true;
11047 }
11048 return cp_lexer_consume_token (parser->lexer)->u.value;
11049 }
11050
11051 /* If we do not already have a type-specifier, assume we are looking
11052 at a simple-type-specifier. */
11053 type_spec = cp_parser_simple_type_specifier (parser,
11054 decl_specs,
11055 flags);
11056
11057 /* If we didn't find a type-specifier, and a type-specifier was not
11058 optional in this context, issue an error message. */
11059 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11060 {
11061 cp_parser_error (parser, "expected type specifier");
11062 return error_mark_node;
11063 }
11064
11065 return type_spec;
11066 }
11067
11068 /* Parse a simple-type-specifier.
11069
11070 simple-type-specifier:
11071 :: [opt] nested-name-specifier [opt] type-name
11072 :: [opt] nested-name-specifier template template-id
11073 char
11074 wchar_t
11075 bool
11076 short
11077 int
11078 long
11079 signed
11080 unsigned
11081 float
11082 double
11083 void
11084
11085 C++0x Extension:
11086
11087 simple-type-specifier:
11088 auto
11089 decltype ( expression )
11090 char16_t
11091 char32_t
11092
11093 GNU Extension:
11094
11095 simple-type-specifier:
11096 __typeof__ unary-expression
11097 __typeof__ ( type-id )
11098
11099 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11100 appropriately updated. */
11101
11102 static tree
11103 cp_parser_simple_type_specifier (cp_parser* parser,
11104 cp_decl_specifier_seq *decl_specs,
11105 cp_parser_flags flags)
11106 {
11107 tree type = NULL_TREE;
11108 cp_token *token;
11109
11110 /* Peek at the next token. */
11111 token = cp_lexer_peek_token (parser->lexer);
11112
11113 /* If we're looking at a keyword, things are easy. */
11114 switch (token->keyword)
11115 {
11116 case RID_CHAR:
11117 if (decl_specs)
11118 decl_specs->explicit_char_p = true;
11119 type = char_type_node;
11120 break;
11121 case RID_CHAR16:
11122 type = char16_type_node;
11123 break;
11124 case RID_CHAR32:
11125 type = char32_type_node;
11126 break;
11127 case RID_WCHAR:
11128 type = wchar_type_node;
11129 break;
11130 case RID_BOOL:
11131 type = boolean_type_node;
11132 break;
11133 case RID_SHORT:
11134 if (decl_specs)
11135 ++decl_specs->specs[(int) ds_short];
11136 type = short_integer_type_node;
11137 break;
11138 case RID_INT:
11139 if (decl_specs)
11140 decl_specs->explicit_int_p = true;
11141 type = integer_type_node;
11142 break;
11143 case RID_LONG:
11144 if (decl_specs)
11145 ++decl_specs->specs[(int) ds_long];
11146 type = long_integer_type_node;
11147 break;
11148 case RID_SIGNED:
11149 if (decl_specs)
11150 ++decl_specs->specs[(int) ds_signed];
11151 type = integer_type_node;
11152 break;
11153 case RID_UNSIGNED:
11154 if (decl_specs)
11155 ++decl_specs->specs[(int) ds_unsigned];
11156 type = unsigned_type_node;
11157 break;
11158 case RID_FLOAT:
11159 type = float_type_node;
11160 break;
11161 case RID_DOUBLE:
11162 type = double_type_node;
11163 break;
11164 case RID_VOID:
11165 type = void_type_node;
11166 break;
11167
11168 case RID_AUTO:
11169 maybe_warn_cpp0x ("C++0x auto");
11170 type = make_auto ();
11171 break;
11172
11173 case RID_DECLTYPE:
11174 /* Parse the `decltype' type. */
11175 type = cp_parser_decltype (parser);
11176
11177 if (decl_specs)
11178 cp_parser_set_decl_spec_type (decl_specs, type,
11179 token->location,
11180 /*user_defined_p=*/true);
11181
11182 return type;
11183
11184 case RID_TYPEOF:
11185 /* Consume the `typeof' token. */
11186 cp_lexer_consume_token (parser->lexer);
11187 /* Parse the operand to `typeof'. */
11188 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11189 /* If it is not already a TYPE, take its type. */
11190 if (!TYPE_P (type))
11191 type = finish_typeof (type);
11192
11193 if (decl_specs)
11194 cp_parser_set_decl_spec_type (decl_specs, type,
11195 token->location,
11196 /*user_defined_p=*/true);
11197
11198 return type;
11199
11200 default:
11201 break;
11202 }
11203
11204 /* If the type-specifier was for a built-in type, we're done. */
11205 if (type)
11206 {
11207 tree id;
11208
11209 /* Record the type. */
11210 if (decl_specs
11211 && (token->keyword != RID_SIGNED
11212 && token->keyword != RID_UNSIGNED
11213 && token->keyword != RID_SHORT
11214 && token->keyword != RID_LONG))
11215 cp_parser_set_decl_spec_type (decl_specs,
11216 type,
11217 token->location,
11218 /*user_defined=*/false);
11219 if (decl_specs)
11220 decl_specs->any_specifiers_p = true;
11221
11222 /* Consume the token. */
11223 id = cp_lexer_consume_token (parser->lexer)->u.value;
11224
11225 /* There is no valid C++ program where a non-template type is
11226 followed by a "<". That usually indicates that the user thought
11227 that the type was a template. */
11228 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11229
11230 return TYPE_NAME (type);
11231 }
11232
11233 /* The type-specifier must be a user-defined type. */
11234 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11235 {
11236 bool qualified_p;
11237 bool global_p;
11238
11239 /* Don't gobble tokens or issue error messages if this is an
11240 optional type-specifier. */
11241 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11242 cp_parser_parse_tentatively (parser);
11243
11244 /* Look for the optional `::' operator. */
11245 global_p
11246 = (cp_parser_global_scope_opt (parser,
11247 /*current_scope_valid_p=*/false)
11248 != NULL_TREE);
11249 /* Look for the nested-name specifier. */
11250 qualified_p
11251 = (cp_parser_nested_name_specifier_opt (parser,
11252 /*typename_keyword_p=*/false,
11253 /*check_dependency_p=*/true,
11254 /*type_p=*/false,
11255 /*is_declaration=*/false)
11256 != NULL_TREE);
11257 token = cp_lexer_peek_token (parser->lexer);
11258 /* If we have seen a nested-name-specifier, and the next token
11259 is `template', then we are using the template-id production. */
11260 if (parser->scope
11261 && cp_parser_optional_template_keyword (parser))
11262 {
11263 /* Look for the template-id. */
11264 type = cp_parser_template_id (parser,
11265 /*template_keyword_p=*/true,
11266 /*check_dependency_p=*/true,
11267 /*is_declaration=*/false);
11268 /* If the template-id did not name a type, we are out of
11269 luck. */
11270 if (TREE_CODE (type) != TYPE_DECL)
11271 {
11272 cp_parser_error (parser, "expected template-id for type");
11273 type = NULL_TREE;
11274 }
11275 }
11276 /* Otherwise, look for a type-name. */
11277 else
11278 type = cp_parser_type_name (parser);
11279 /* Keep track of all name-lookups performed in class scopes. */
11280 if (type
11281 && !global_p
11282 && !qualified_p
11283 && TREE_CODE (type) == TYPE_DECL
11284 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11285 maybe_note_name_used_in_class (DECL_NAME (type), type);
11286 /* If it didn't work out, we don't have a TYPE. */
11287 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11288 && !cp_parser_parse_definitely (parser))
11289 type = NULL_TREE;
11290 if (type && decl_specs)
11291 cp_parser_set_decl_spec_type (decl_specs, type,
11292 token->location,
11293 /*user_defined=*/true);
11294 }
11295
11296 /* If we didn't get a type-name, issue an error message. */
11297 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11298 {
11299 cp_parser_error (parser, "expected type-name");
11300 return error_mark_node;
11301 }
11302
11303 /* There is no valid C++ program where a non-template type is
11304 followed by a "<". That usually indicates that the user thought
11305 that the type was a template. */
11306 if (type && type != error_mark_node)
11307 {
11308 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11309 If it is, then the '<'...'>' enclose protocol names rather than
11310 template arguments, and so everything is fine. */
11311 if (c_dialect_objc ()
11312 && (objc_is_id (type) || objc_is_class_name (type)))
11313 {
11314 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11315 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11316
11317 /* Clobber the "unqualified" type previously entered into
11318 DECL_SPECS with the new, improved protocol-qualified version. */
11319 if (decl_specs)
11320 decl_specs->type = qual_type;
11321
11322 return qual_type;
11323 }
11324
11325 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11326 token->location);
11327 }
11328
11329 return type;
11330 }
11331
11332 /* Parse a type-name.
11333
11334 type-name:
11335 class-name
11336 enum-name
11337 typedef-name
11338
11339 enum-name:
11340 identifier
11341
11342 typedef-name:
11343 identifier
11344
11345 Returns a TYPE_DECL for the type. */
11346
11347 static tree
11348 cp_parser_type_name (cp_parser* parser)
11349 {
11350 tree type_decl;
11351
11352 /* We can't know yet whether it is a class-name or not. */
11353 cp_parser_parse_tentatively (parser);
11354 /* Try a class-name. */
11355 type_decl = cp_parser_class_name (parser,
11356 /*typename_keyword_p=*/false,
11357 /*template_keyword_p=*/false,
11358 none_type,
11359 /*check_dependency_p=*/true,
11360 /*class_head_p=*/false,
11361 /*is_declaration=*/false);
11362 /* If it's not a class-name, keep looking. */
11363 if (!cp_parser_parse_definitely (parser))
11364 {
11365 /* It must be a typedef-name or an enum-name. */
11366 return cp_parser_nonclass_name (parser);
11367 }
11368
11369 return type_decl;
11370 }
11371
11372 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11373
11374 enum-name:
11375 identifier
11376
11377 typedef-name:
11378 identifier
11379
11380 Returns a TYPE_DECL for the type. */
11381
11382 static tree
11383 cp_parser_nonclass_name (cp_parser* parser)
11384 {
11385 tree type_decl;
11386 tree identifier;
11387
11388 cp_token *token = cp_lexer_peek_token (parser->lexer);
11389 identifier = cp_parser_identifier (parser);
11390 if (identifier == error_mark_node)
11391 return error_mark_node;
11392
11393 /* Look up the type-name. */
11394 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11395
11396 if (TREE_CODE (type_decl) != TYPE_DECL
11397 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11398 {
11399 /* See if this is an Objective-C type. */
11400 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11401 tree type = objc_get_protocol_qualified_type (identifier, protos);
11402 if (type)
11403 type_decl = TYPE_NAME (type);
11404 }
11405
11406 /* Issue an error if we did not find a type-name. */
11407 if (TREE_CODE (type_decl) != TYPE_DECL)
11408 {
11409 if (!cp_parser_simulate_error (parser))
11410 cp_parser_name_lookup_error (parser, identifier, type_decl,
11411 "is not a type", token->location);
11412 return error_mark_node;
11413 }
11414 /* Remember that the name was used in the definition of the
11415 current class so that we can check later to see if the
11416 meaning would have been different after the class was
11417 entirely defined. */
11418 else if (type_decl != error_mark_node
11419 && !parser->scope)
11420 maybe_note_name_used_in_class (identifier, type_decl);
11421
11422 return type_decl;
11423 }
11424
11425 /* Parse an elaborated-type-specifier. Note that the grammar given
11426 here incorporates the resolution to DR68.
11427
11428 elaborated-type-specifier:
11429 class-key :: [opt] nested-name-specifier [opt] identifier
11430 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11431 enum-key :: [opt] nested-name-specifier [opt] identifier
11432 typename :: [opt] nested-name-specifier identifier
11433 typename :: [opt] nested-name-specifier template [opt]
11434 template-id
11435
11436 GNU extension:
11437
11438 elaborated-type-specifier:
11439 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11440 class-key attributes :: [opt] nested-name-specifier [opt]
11441 template [opt] template-id
11442 enum attributes :: [opt] nested-name-specifier [opt] identifier
11443
11444 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11445 declared `friend'. If IS_DECLARATION is TRUE, then this
11446 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11447 something is being declared.
11448
11449 Returns the TYPE specified. */
11450
11451 static tree
11452 cp_parser_elaborated_type_specifier (cp_parser* parser,
11453 bool is_friend,
11454 bool is_declaration)
11455 {
11456 enum tag_types tag_type;
11457 tree identifier;
11458 tree type = NULL_TREE;
11459 tree attributes = NULL_TREE;
11460 cp_token *token = NULL;
11461
11462 /* See if we're looking at the `enum' keyword. */
11463 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11464 {
11465 /* Consume the `enum' token. */
11466 cp_lexer_consume_token (parser->lexer);
11467 /* Remember that it's an enumeration type. */
11468 tag_type = enum_type;
11469 /* Parse the optional `struct' or `class' key (for C++0x scoped
11470 enums). */
11471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11472 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11473 {
11474 if (cxx_dialect == cxx98)
11475 maybe_warn_cpp0x ("scoped enums");
11476
11477 /* Consume the `struct' or `class'. */
11478 cp_lexer_consume_token (parser->lexer);
11479 }
11480 /* Parse the attributes. */
11481 attributes = cp_parser_attributes_opt (parser);
11482 }
11483 /* Or, it might be `typename'. */
11484 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11485 RID_TYPENAME))
11486 {
11487 /* Consume the `typename' token. */
11488 cp_lexer_consume_token (parser->lexer);
11489 /* Remember that it's a `typename' type. */
11490 tag_type = typename_type;
11491 /* The `typename' keyword is only allowed in templates. */
11492 if (!processing_template_decl)
11493 permerror (input_location, "using %<typename%> outside of template");
11494 }
11495 /* Otherwise it must be a class-key. */
11496 else
11497 {
11498 tag_type = cp_parser_class_key (parser);
11499 if (tag_type == none_type)
11500 return error_mark_node;
11501 /* Parse the attributes. */
11502 attributes = cp_parser_attributes_opt (parser);
11503 }
11504
11505 /* Look for the `::' operator. */
11506 cp_parser_global_scope_opt (parser,
11507 /*current_scope_valid_p=*/false);
11508 /* Look for the nested-name-specifier. */
11509 if (tag_type == typename_type)
11510 {
11511 if (!cp_parser_nested_name_specifier (parser,
11512 /*typename_keyword_p=*/true,
11513 /*check_dependency_p=*/true,
11514 /*type_p=*/true,
11515 is_declaration))
11516 return error_mark_node;
11517 }
11518 else
11519 /* Even though `typename' is not present, the proposed resolution
11520 to Core Issue 180 says that in `class A<T>::B', `B' should be
11521 considered a type-name, even if `A<T>' is dependent. */
11522 cp_parser_nested_name_specifier_opt (parser,
11523 /*typename_keyword_p=*/true,
11524 /*check_dependency_p=*/true,
11525 /*type_p=*/true,
11526 is_declaration);
11527 /* For everything but enumeration types, consider a template-id.
11528 For an enumeration type, consider only a plain identifier. */
11529 if (tag_type != enum_type)
11530 {
11531 bool template_p = false;
11532 tree decl;
11533
11534 /* Allow the `template' keyword. */
11535 template_p = cp_parser_optional_template_keyword (parser);
11536 /* If we didn't see `template', we don't know if there's a
11537 template-id or not. */
11538 if (!template_p)
11539 cp_parser_parse_tentatively (parser);
11540 /* Parse the template-id. */
11541 token = cp_lexer_peek_token (parser->lexer);
11542 decl = cp_parser_template_id (parser, template_p,
11543 /*check_dependency_p=*/true,
11544 is_declaration);
11545 /* If we didn't find a template-id, look for an ordinary
11546 identifier. */
11547 if (!template_p && !cp_parser_parse_definitely (parser))
11548 ;
11549 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11550 in effect, then we must assume that, upon instantiation, the
11551 template will correspond to a class. */
11552 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11553 && tag_type == typename_type)
11554 type = make_typename_type (parser->scope, decl,
11555 typename_type,
11556 /*complain=*/tf_error);
11557 else
11558 type = TREE_TYPE (decl);
11559 }
11560
11561 if (!type)
11562 {
11563 token = cp_lexer_peek_token (parser->lexer);
11564 identifier = cp_parser_identifier (parser);
11565
11566 if (identifier == error_mark_node)
11567 {
11568 parser->scope = NULL_TREE;
11569 return error_mark_node;
11570 }
11571
11572 /* For a `typename', we needn't call xref_tag. */
11573 if (tag_type == typename_type
11574 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11575 return cp_parser_make_typename_type (parser, parser->scope,
11576 identifier,
11577 token->location);
11578 /* Look up a qualified name in the usual way. */
11579 if (parser->scope)
11580 {
11581 tree decl;
11582 tree ambiguous_decls;
11583
11584 decl = cp_parser_lookup_name (parser, identifier,
11585 tag_type,
11586 /*is_template=*/false,
11587 /*is_namespace=*/false,
11588 /*check_dependency=*/true,
11589 &ambiguous_decls,
11590 token->location);
11591
11592 /* If the lookup was ambiguous, an error will already have been
11593 issued. */
11594 if (ambiguous_decls)
11595 return error_mark_node;
11596
11597 /* If we are parsing friend declaration, DECL may be a
11598 TEMPLATE_DECL tree node here. However, we need to check
11599 whether this TEMPLATE_DECL results in valid code. Consider
11600 the following example:
11601
11602 namespace N {
11603 template <class T> class C {};
11604 }
11605 class X {
11606 template <class T> friend class N::C; // #1, valid code
11607 };
11608 template <class T> class Y {
11609 friend class N::C; // #2, invalid code
11610 };
11611
11612 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11613 name lookup of `N::C'. We see that friend declaration must
11614 be template for the code to be valid. Note that
11615 processing_template_decl does not work here since it is
11616 always 1 for the above two cases. */
11617
11618 decl = (cp_parser_maybe_treat_template_as_class
11619 (decl, /*tag_name_p=*/is_friend
11620 && parser->num_template_parameter_lists));
11621
11622 if (TREE_CODE (decl) != TYPE_DECL)
11623 {
11624 cp_parser_diagnose_invalid_type_name (parser,
11625 parser->scope,
11626 identifier,
11627 token->location);
11628 return error_mark_node;
11629 }
11630
11631 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11632 {
11633 bool allow_template = (parser->num_template_parameter_lists
11634 || DECL_SELF_REFERENCE_P (decl));
11635 type = check_elaborated_type_specifier (tag_type, decl,
11636 allow_template);
11637
11638 if (type == error_mark_node)
11639 return error_mark_node;
11640 }
11641
11642 /* Forward declarations of nested types, such as
11643
11644 class C1::C2;
11645 class C1::C2::C3;
11646
11647 are invalid unless all components preceding the final '::'
11648 are complete. If all enclosing types are complete, these
11649 declarations become merely pointless.
11650
11651 Invalid forward declarations of nested types are errors
11652 caught elsewhere in parsing. Those that are pointless arrive
11653 here. */
11654
11655 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11656 && !is_friend && !processing_explicit_instantiation)
11657 warning (0, "declaration %qD does not declare anything", decl);
11658
11659 type = TREE_TYPE (decl);
11660 }
11661 else
11662 {
11663 /* An elaborated-type-specifier sometimes introduces a new type and
11664 sometimes names an existing type. Normally, the rule is that it
11665 introduces a new type only if there is not an existing type of
11666 the same name already in scope. For example, given:
11667
11668 struct S {};
11669 void f() { struct S s; }
11670
11671 the `struct S' in the body of `f' is the same `struct S' as in
11672 the global scope; the existing definition is used. However, if
11673 there were no global declaration, this would introduce a new
11674 local class named `S'.
11675
11676 An exception to this rule applies to the following code:
11677
11678 namespace N { struct S; }
11679
11680 Here, the elaborated-type-specifier names a new type
11681 unconditionally; even if there is already an `S' in the
11682 containing scope this declaration names a new type.
11683 This exception only applies if the elaborated-type-specifier
11684 forms the complete declaration:
11685
11686 [class.name]
11687
11688 A declaration consisting solely of `class-key identifier ;' is
11689 either a redeclaration of the name in the current scope or a
11690 forward declaration of the identifier as a class name. It
11691 introduces the name into the current scope.
11692
11693 We are in this situation precisely when the next token is a `;'.
11694
11695 An exception to the exception is that a `friend' declaration does
11696 *not* name a new type; i.e., given:
11697
11698 struct S { friend struct T; };
11699
11700 `T' is not a new type in the scope of `S'.
11701
11702 Also, `new struct S' or `sizeof (struct S)' never results in the
11703 definition of a new type; a new type can only be declared in a
11704 declaration context. */
11705
11706 tag_scope ts;
11707 bool template_p;
11708
11709 if (is_friend)
11710 /* Friends have special name lookup rules. */
11711 ts = ts_within_enclosing_non_class;
11712 else if (is_declaration
11713 && cp_lexer_next_token_is (parser->lexer,
11714 CPP_SEMICOLON))
11715 /* This is a `class-key identifier ;' */
11716 ts = ts_current;
11717 else
11718 ts = ts_global;
11719
11720 template_p =
11721 (parser->num_template_parameter_lists
11722 && (cp_parser_next_token_starts_class_definition_p (parser)
11723 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11724 /* An unqualified name was used to reference this type, so
11725 there were no qualifying templates. */
11726 if (!cp_parser_check_template_parameters (parser,
11727 /*num_templates=*/0,
11728 token->location))
11729 return error_mark_node;
11730 type = xref_tag (tag_type, identifier, ts, template_p);
11731 }
11732 }
11733
11734 if (type == error_mark_node)
11735 return error_mark_node;
11736
11737 /* Allow attributes on forward declarations of classes. */
11738 if (attributes)
11739 {
11740 if (TREE_CODE (type) == TYPENAME_TYPE)
11741 warning (OPT_Wattributes,
11742 "attributes ignored on uninstantiated type");
11743 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11744 && ! processing_explicit_instantiation)
11745 warning (OPT_Wattributes,
11746 "attributes ignored on template instantiation");
11747 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11748 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11749 else
11750 warning (OPT_Wattributes,
11751 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11752 }
11753
11754 if (tag_type != enum_type)
11755 cp_parser_check_class_key (tag_type, type);
11756
11757 /* A "<" cannot follow an elaborated type specifier. If that
11758 happens, the user was probably trying to form a template-id. */
11759 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11760
11761 return type;
11762 }
11763
11764 /* Parse an enum-specifier.
11765
11766 enum-specifier:
11767 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11768
11769 enum-key:
11770 enum
11771 enum class [C++0x]
11772 enum struct [C++0x]
11773
11774 enum-base: [C++0x]
11775 : type-specifier-seq
11776
11777 GNU Extensions:
11778 enum-key attributes[opt] identifier [opt] enum-base [opt]
11779 { enumerator-list [opt] }attributes[opt]
11780
11781 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11782 if the token stream isn't an enum-specifier after all. */
11783
11784 static tree
11785 cp_parser_enum_specifier (cp_parser* parser)
11786 {
11787 tree identifier;
11788 tree type;
11789 tree attributes;
11790 bool scoped_enum_p = false;
11791 bool has_underlying_type = false;
11792 tree underlying_type = NULL_TREE;
11793
11794 /* Parse tentatively so that we can back up if we don't find a
11795 enum-specifier. */
11796 cp_parser_parse_tentatively (parser);
11797
11798 /* Caller guarantees that the current token is 'enum', an identifier
11799 possibly follows, and the token after that is an opening brace.
11800 If we don't have an identifier, fabricate an anonymous name for
11801 the enumeration being defined. */
11802 cp_lexer_consume_token (parser->lexer);
11803
11804 /* Parse the "class" or "struct", which indicates a scoped
11805 enumeration type in C++0x. */
11806 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11807 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11808 {
11809 if (cxx_dialect == cxx98)
11810 maybe_warn_cpp0x ("scoped enums");
11811
11812 /* Consume the `struct' or `class' token. */
11813 cp_lexer_consume_token (parser->lexer);
11814
11815 scoped_enum_p = true;
11816 }
11817
11818 attributes = cp_parser_attributes_opt (parser);
11819
11820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11821 identifier = cp_parser_identifier (parser);
11822 else
11823 identifier = make_anon_name ();
11824
11825 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11826 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11827 {
11828 cp_decl_specifier_seq type_specifiers;
11829
11830 /* At this point this is surely not elaborated type specifier. */
11831 if (!cp_parser_parse_definitely (parser))
11832 return NULL_TREE;
11833
11834 if (cxx_dialect == cxx98)
11835 maybe_warn_cpp0x ("scoped enums");
11836
11837 /* Consume the `:'. */
11838 cp_lexer_consume_token (parser->lexer);
11839
11840 has_underlying_type = true;
11841
11842 /* Parse the type-specifier-seq. */
11843 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11844 &type_specifiers);
11845
11846 /* If that didn't work, stop. */
11847 if (type_specifiers.type != error_mark_node)
11848 {
11849 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11850 /*initialized=*/0, NULL);
11851 if (underlying_type == error_mark_node)
11852 underlying_type = NULL_TREE;
11853 }
11854 }
11855
11856 /* Look for the `{' but don't consume it yet. */
11857 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11858 {
11859 cp_parser_error (parser, "expected %<{%>");
11860 if (has_underlying_type)
11861 return NULL_TREE;
11862 }
11863
11864 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11865 return NULL_TREE;
11866
11867 /* Issue an error message if type-definitions are forbidden here. */
11868 if (!cp_parser_check_type_definition (parser))
11869 type = error_mark_node;
11870 else
11871 /* Create the new type. We do this before consuming the opening
11872 brace so the enum will be recorded as being on the line of its
11873 tag (or the 'enum' keyword, if there is no tag). */
11874 type = start_enum (identifier, underlying_type, scoped_enum_p);
11875
11876 /* Consume the opening brace. */
11877 cp_lexer_consume_token (parser->lexer);
11878
11879 if (type == error_mark_node)
11880 {
11881 cp_parser_skip_to_end_of_block_or_statement (parser);
11882 return error_mark_node;
11883 }
11884
11885 /* If the next token is not '}', then there are some enumerators. */
11886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11887 cp_parser_enumerator_list (parser, type);
11888
11889 /* Consume the final '}'. */
11890 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11891
11892 /* Look for trailing attributes to apply to this enumeration, and
11893 apply them if appropriate. */
11894 if (cp_parser_allow_gnu_extensions_p (parser))
11895 {
11896 tree trailing_attr = cp_parser_attributes_opt (parser);
11897 cplus_decl_attributes (&type,
11898 trailing_attr,
11899 (int) ATTR_FLAG_TYPE_IN_PLACE);
11900 }
11901
11902 /* Finish up the enumeration. */
11903 finish_enum (type);
11904
11905 return type;
11906 }
11907
11908 /* Parse an enumerator-list. The enumerators all have the indicated
11909 TYPE.
11910
11911 enumerator-list:
11912 enumerator-definition
11913 enumerator-list , enumerator-definition */
11914
11915 static void
11916 cp_parser_enumerator_list (cp_parser* parser, tree type)
11917 {
11918 while (true)
11919 {
11920 /* Parse an enumerator-definition. */
11921 cp_parser_enumerator_definition (parser, type);
11922
11923 /* If the next token is not a ',', we've reached the end of
11924 the list. */
11925 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11926 break;
11927 /* Otherwise, consume the `,' and keep going. */
11928 cp_lexer_consume_token (parser->lexer);
11929 /* If the next token is a `}', there is a trailing comma. */
11930 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11931 {
11932 if (!in_system_header)
11933 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11934 break;
11935 }
11936 }
11937 }
11938
11939 /* Parse an enumerator-definition. The enumerator has the indicated
11940 TYPE.
11941
11942 enumerator-definition:
11943 enumerator
11944 enumerator = constant-expression
11945
11946 enumerator:
11947 identifier */
11948
11949 static void
11950 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11951 {
11952 tree identifier;
11953 tree value;
11954
11955 /* Look for the identifier. */
11956 identifier = cp_parser_identifier (parser);
11957 if (identifier == error_mark_node)
11958 return;
11959
11960 /* If the next token is an '=', then there is an explicit value. */
11961 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11962 {
11963 /* Consume the `=' token. */
11964 cp_lexer_consume_token (parser->lexer);
11965 /* Parse the value. */
11966 value = cp_parser_constant_expression (parser,
11967 /*allow_non_constant_p=*/false,
11968 NULL);
11969 }
11970 else
11971 value = NULL_TREE;
11972
11973 /* Create the enumerator. */
11974 build_enumerator (identifier, value, type);
11975 }
11976
11977 /* Parse a namespace-name.
11978
11979 namespace-name:
11980 original-namespace-name
11981 namespace-alias
11982
11983 Returns the NAMESPACE_DECL for the namespace. */
11984
11985 static tree
11986 cp_parser_namespace_name (cp_parser* parser)
11987 {
11988 tree identifier;
11989 tree namespace_decl;
11990
11991 cp_token *token = cp_lexer_peek_token (parser->lexer);
11992
11993 /* Get the name of the namespace. */
11994 identifier = cp_parser_identifier (parser);
11995 if (identifier == error_mark_node)
11996 return error_mark_node;
11997
11998 /* Look up the identifier in the currently active scope. Look only
11999 for namespaces, due to:
12000
12001 [basic.lookup.udir]
12002
12003 When looking up a namespace-name in a using-directive or alias
12004 definition, only namespace names are considered.
12005
12006 And:
12007
12008 [basic.lookup.qual]
12009
12010 During the lookup of a name preceding the :: scope resolution
12011 operator, object, function, and enumerator names are ignored.
12012
12013 (Note that cp_parser_qualifying_entity only calls this
12014 function if the token after the name is the scope resolution
12015 operator.) */
12016 namespace_decl = cp_parser_lookup_name (parser, identifier,
12017 none_type,
12018 /*is_template=*/false,
12019 /*is_namespace=*/true,
12020 /*check_dependency=*/true,
12021 /*ambiguous_decls=*/NULL,
12022 token->location);
12023 /* If it's not a namespace, issue an error. */
12024 if (namespace_decl == error_mark_node
12025 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12026 {
12027 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12028 error ("%H%qD is not a namespace-name", &token->location, identifier);
12029 cp_parser_error (parser, "expected namespace-name");
12030 namespace_decl = error_mark_node;
12031 }
12032
12033 return namespace_decl;
12034 }
12035
12036 /* Parse a namespace-definition.
12037
12038 namespace-definition:
12039 named-namespace-definition
12040 unnamed-namespace-definition
12041
12042 named-namespace-definition:
12043 original-namespace-definition
12044 extension-namespace-definition
12045
12046 original-namespace-definition:
12047 namespace identifier { namespace-body }
12048
12049 extension-namespace-definition:
12050 namespace original-namespace-name { namespace-body }
12051
12052 unnamed-namespace-definition:
12053 namespace { namespace-body } */
12054
12055 static void
12056 cp_parser_namespace_definition (cp_parser* parser)
12057 {
12058 tree identifier, attribs;
12059 bool has_visibility;
12060 bool is_inline;
12061
12062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12063 {
12064 is_inline = true;
12065 cp_lexer_consume_token (parser->lexer);
12066 }
12067 else
12068 is_inline = false;
12069
12070 /* Look for the `namespace' keyword. */
12071 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12072
12073 /* Get the name of the namespace. We do not attempt to distinguish
12074 between an original-namespace-definition and an
12075 extension-namespace-definition at this point. The semantic
12076 analysis routines are responsible for that. */
12077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12078 identifier = cp_parser_identifier (parser);
12079 else
12080 identifier = NULL_TREE;
12081
12082 /* Parse any specified attributes. */
12083 attribs = cp_parser_attributes_opt (parser);
12084
12085 /* Look for the `{' to start the namespace. */
12086 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12087 /* Start the namespace. */
12088 push_namespace (identifier);
12089
12090 /* "inline namespace" is equivalent to a stub namespace definition
12091 followed by a strong using directive. */
12092 if (is_inline)
12093 {
12094 tree name_space = current_namespace;
12095 /* Set up namespace association. */
12096 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12097 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12098 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12099 /* Import the contents of the inline namespace. */
12100 pop_namespace ();
12101 do_using_directive (name_space);
12102 push_namespace (identifier);
12103 }
12104
12105 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12106
12107 /* Parse the body of the namespace. */
12108 cp_parser_namespace_body (parser);
12109
12110 #ifdef HANDLE_PRAGMA_VISIBILITY
12111 if (has_visibility)
12112 pop_visibility ();
12113 #endif
12114
12115 /* Finish the namespace. */
12116 pop_namespace ();
12117 /* Look for the final `}'. */
12118 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12119 }
12120
12121 /* Parse a namespace-body.
12122
12123 namespace-body:
12124 declaration-seq [opt] */
12125
12126 static void
12127 cp_parser_namespace_body (cp_parser* parser)
12128 {
12129 cp_parser_declaration_seq_opt (parser);
12130 }
12131
12132 /* Parse a namespace-alias-definition.
12133
12134 namespace-alias-definition:
12135 namespace identifier = qualified-namespace-specifier ; */
12136
12137 static void
12138 cp_parser_namespace_alias_definition (cp_parser* parser)
12139 {
12140 tree identifier;
12141 tree namespace_specifier;
12142
12143 cp_token *token = cp_lexer_peek_token (parser->lexer);
12144
12145 /* Look for the `namespace' keyword. */
12146 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12147 /* Look for the identifier. */
12148 identifier = cp_parser_identifier (parser);
12149 if (identifier == error_mark_node)
12150 return;
12151 /* Look for the `=' token. */
12152 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12153 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12154 {
12155 error ("%H%<namespace%> definition is not allowed here", &token->location);
12156 /* Skip the definition. */
12157 cp_lexer_consume_token (parser->lexer);
12158 if (cp_parser_skip_to_closing_brace (parser))
12159 cp_lexer_consume_token (parser->lexer);
12160 return;
12161 }
12162 cp_parser_require (parser, CPP_EQ, "%<=%>");
12163 /* Look for the qualified-namespace-specifier. */
12164 namespace_specifier
12165 = cp_parser_qualified_namespace_specifier (parser);
12166 /* Look for the `;' token. */
12167 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12168
12169 /* Register the alias in the symbol table. */
12170 do_namespace_alias (identifier, namespace_specifier);
12171 }
12172
12173 /* Parse a qualified-namespace-specifier.
12174
12175 qualified-namespace-specifier:
12176 :: [opt] nested-name-specifier [opt] namespace-name
12177
12178 Returns a NAMESPACE_DECL corresponding to the specified
12179 namespace. */
12180
12181 static tree
12182 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12183 {
12184 /* Look for the optional `::'. */
12185 cp_parser_global_scope_opt (parser,
12186 /*current_scope_valid_p=*/false);
12187
12188 /* Look for the optional nested-name-specifier. */
12189 cp_parser_nested_name_specifier_opt (parser,
12190 /*typename_keyword_p=*/false,
12191 /*check_dependency_p=*/true,
12192 /*type_p=*/false,
12193 /*is_declaration=*/true);
12194
12195 return cp_parser_namespace_name (parser);
12196 }
12197
12198 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12199 access declaration.
12200
12201 using-declaration:
12202 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12203 using :: unqualified-id ;
12204
12205 access-declaration:
12206 qualified-id ;
12207
12208 */
12209
12210 static bool
12211 cp_parser_using_declaration (cp_parser* parser,
12212 bool access_declaration_p)
12213 {
12214 cp_token *token;
12215 bool typename_p = false;
12216 bool global_scope_p;
12217 tree decl;
12218 tree identifier;
12219 tree qscope;
12220
12221 if (access_declaration_p)
12222 cp_parser_parse_tentatively (parser);
12223 else
12224 {
12225 /* Look for the `using' keyword. */
12226 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12227
12228 /* Peek at the next token. */
12229 token = cp_lexer_peek_token (parser->lexer);
12230 /* See if it's `typename'. */
12231 if (token->keyword == RID_TYPENAME)
12232 {
12233 /* Remember that we've seen it. */
12234 typename_p = true;
12235 /* Consume the `typename' token. */
12236 cp_lexer_consume_token (parser->lexer);
12237 }
12238 }
12239
12240 /* Look for the optional global scope qualification. */
12241 global_scope_p
12242 = (cp_parser_global_scope_opt (parser,
12243 /*current_scope_valid_p=*/false)
12244 != NULL_TREE);
12245
12246 /* If we saw `typename', or didn't see `::', then there must be a
12247 nested-name-specifier present. */
12248 if (typename_p || !global_scope_p)
12249 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12250 /*check_dependency_p=*/true,
12251 /*type_p=*/false,
12252 /*is_declaration=*/true);
12253 /* Otherwise, we could be in either of the two productions. In that
12254 case, treat the nested-name-specifier as optional. */
12255 else
12256 qscope = cp_parser_nested_name_specifier_opt (parser,
12257 /*typename_keyword_p=*/false,
12258 /*check_dependency_p=*/true,
12259 /*type_p=*/false,
12260 /*is_declaration=*/true);
12261 if (!qscope)
12262 qscope = global_namespace;
12263
12264 if (access_declaration_p && cp_parser_error_occurred (parser))
12265 /* Something has already gone wrong; there's no need to parse
12266 further. Since an error has occurred, the return value of
12267 cp_parser_parse_definitely will be false, as required. */
12268 return cp_parser_parse_definitely (parser);
12269
12270 token = cp_lexer_peek_token (parser->lexer);
12271 /* Parse the unqualified-id. */
12272 identifier = cp_parser_unqualified_id (parser,
12273 /*template_keyword_p=*/false,
12274 /*check_dependency_p=*/true,
12275 /*declarator_p=*/true,
12276 /*optional_p=*/false);
12277
12278 if (access_declaration_p)
12279 {
12280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12281 cp_parser_simulate_error (parser);
12282 if (!cp_parser_parse_definitely (parser))
12283 return false;
12284 }
12285
12286 /* The function we call to handle a using-declaration is different
12287 depending on what scope we are in. */
12288 if (qscope == error_mark_node || identifier == error_mark_node)
12289 ;
12290 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12291 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12292 /* [namespace.udecl]
12293
12294 A using declaration shall not name a template-id. */
12295 error ("%Ha template-id may not appear in a using-declaration",
12296 &token->location);
12297 else
12298 {
12299 if (at_class_scope_p ())
12300 {
12301 /* Create the USING_DECL. */
12302 decl = do_class_using_decl (parser->scope, identifier);
12303
12304 if (check_for_bare_parameter_packs (decl))
12305 return false;
12306 else
12307 /* Add it to the list of members in this class. */
12308 finish_member_declaration (decl);
12309 }
12310 else
12311 {
12312 decl = cp_parser_lookup_name_simple (parser,
12313 identifier,
12314 token->location);
12315 if (decl == error_mark_node)
12316 cp_parser_name_lookup_error (parser, identifier,
12317 decl, NULL,
12318 token->location);
12319 else if (check_for_bare_parameter_packs (decl))
12320 return false;
12321 else if (!at_namespace_scope_p ())
12322 do_local_using_decl (decl, qscope, identifier);
12323 else
12324 do_toplevel_using_decl (decl, qscope, identifier);
12325 }
12326 }
12327
12328 /* Look for the final `;'. */
12329 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12330
12331 return true;
12332 }
12333
12334 /* Parse a using-directive.
12335
12336 using-directive:
12337 using namespace :: [opt] nested-name-specifier [opt]
12338 namespace-name ; */
12339
12340 static void
12341 cp_parser_using_directive (cp_parser* parser)
12342 {
12343 tree namespace_decl;
12344 tree attribs;
12345
12346 /* Look for the `using' keyword. */
12347 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12348 /* And the `namespace' keyword. */
12349 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12350 /* Look for the optional `::' operator. */
12351 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12352 /* And the optional nested-name-specifier. */
12353 cp_parser_nested_name_specifier_opt (parser,
12354 /*typename_keyword_p=*/false,
12355 /*check_dependency_p=*/true,
12356 /*type_p=*/false,
12357 /*is_declaration=*/true);
12358 /* Get the namespace being used. */
12359 namespace_decl = cp_parser_namespace_name (parser);
12360 /* And any specified attributes. */
12361 attribs = cp_parser_attributes_opt (parser);
12362 /* Update the symbol table. */
12363 parse_using_directive (namespace_decl, attribs);
12364 /* Look for the final `;'. */
12365 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12366 }
12367
12368 /* Parse an asm-definition.
12369
12370 asm-definition:
12371 asm ( string-literal ) ;
12372
12373 GNU Extension:
12374
12375 asm-definition:
12376 asm volatile [opt] ( string-literal ) ;
12377 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12378 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12379 : asm-operand-list [opt] ) ;
12380 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12381 : asm-operand-list [opt]
12382 : asm-operand-list [opt] ) ; */
12383
12384 static void
12385 cp_parser_asm_definition (cp_parser* parser)
12386 {
12387 tree string;
12388 tree outputs = NULL_TREE;
12389 tree inputs = NULL_TREE;
12390 tree clobbers = NULL_TREE;
12391 tree asm_stmt;
12392 bool volatile_p = false;
12393 bool extended_p = false;
12394 bool invalid_inputs_p = false;
12395 bool invalid_outputs_p = false;
12396
12397 /* Look for the `asm' keyword. */
12398 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12399 /* See if the next token is `volatile'. */
12400 if (cp_parser_allow_gnu_extensions_p (parser)
12401 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12402 {
12403 /* Remember that we saw the `volatile' keyword. */
12404 volatile_p = true;
12405 /* Consume the token. */
12406 cp_lexer_consume_token (parser->lexer);
12407 }
12408 /* Look for the opening `('. */
12409 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12410 return;
12411 /* Look for the string. */
12412 string = cp_parser_string_literal (parser, false, false);
12413 if (string == error_mark_node)
12414 {
12415 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12416 /*consume_paren=*/true);
12417 return;
12418 }
12419
12420 /* If we're allowing GNU extensions, check for the extended assembly
12421 syntax. Unfortunately, the `:' tokens need not be separated by
12422 a space in C, and so, for compatibility, we tolerate that here
12423 too. Doing that means that we have to treat the `::' operator as
12424 two `:' tokens. */
12425 if (cp_parser_allow_gnu_extensions_p (parser)
12426 && parser->in_function_body
12427 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12428 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12429 {
12430 bool inputs_p = false;
12431 bool clobbers_p = false;
12432
12433 /* The extended syntax was used. */
12434 extended_p = true;
12435
12436 /* Look for outputs. */
12437 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12438 {
12439 /* Consume the `:'. */
12440 cp_lexer_consume_token (parser->lexer);
12441 /* Parse the output-operands. */
12442 if (cp_lexer_next_token_is_not (parser->lexer,
12443 CPP_COLON)
12444 && cp_lexer_next_token_is_not (parser->lexer,
12445 CPP_SCOPE)
12446 && cp_lexer_next_token_is_not (parser->lexer,
12447 CPP_CLOSE_PAREN))
12448 outputs = cp_parser_asm_operand_list (parser);
12449
12450 if (outputs == error_mark_node)
12451 invalid_outputs_p = true;
12452 }
12453 /* If the next token is `::', there are no outputs, and the
12454 next token is the beginning of the inputs. */
12455 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12456 /* The inputs are coming next. */
12457 inputs_p = true;
12458
12459 /* Look for inputs. */
12460 if (inputs_p
12461 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12462 {
12463 /* Consume the `:' or `::'. */
12464 cp_lexer_consume_token (parser->lexer);
12465 /* Parse the output-operands. */
12466 if (cp_lexer_next_token_is_not (parser->lexer,
12467 CPP_COLON)
12468 && cp_lexer_next_token_is_not (parser->lexer,
12469 CPP_CLOSE_PAREN))
12470 inputs = cp_parser_asm_operand_list (parser);
12471
12472 if (inputs == error_mark_node)
12473 invalid_inputs_p = true;
12474 }
12475 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12476 /* The clobbers are coming next. */
12477 clobbers_p = true;
12478
12479 /* Look for clobbers. */
12480 if (clobbers_p
12481 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12482 {
12483 /* Consume the `:' or `::'. */
12484 cp_lexer_consume_token (parser->lexer);
12485 /* Parse the clobbers. */
12486 if (cp_lexer_next_token_is_not (parser->lexer,
12487 CPP_CLOSE_PAREN))
12488 clobbers = cp_parser_asm_clobber_list (parser);
12489 }
12490 }
12491 /* Look for the closing `)'. */
12492 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12493 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12494 /*consume_paren=*/true);
12495 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12496
12497 if (!invalid_inputs_p && !invalid_outputs_p)
12498 {
12499 /* Create the ASM_EXPR. */
12500 if (parser->in_function_body)
12501 {
12502 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12503 inputs, clobbers);
12504 /* If the extended syntax was not used, mark the ASM_EXPR. */
12505 if (!extended_p)
12506 {
12507 tree temp = asm_stmt;
12508 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12509 temp = TREE_OPERAND (temp, 0);
12510
12511 ASM_INPUT_P (temp) = 1;
12512 }
12513 }
12514 else
12515 cgraph_add_asm_node (string);
12516 }
12517 }
12518
12519 /* Declarators [gram.dcl.decl] */
12520
12521 /* Parse an init-declarator.
12522
12523 init-declarator:
12524 declarator initializer [opt]
12525
12526 GNU Extension:
12527
12528 init-declarator:
12529 declarator asm-specification [opt] attributes [opt] initializer [opt]
12530
12531 function-definition:
12532 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12533 function-body
12534 decl-specifier-seq [opt] declarator function-try-block
12535
12536 GNU Extension:
12537
12538 function-definition:
12539 __extension__ function-definition
12540
12541 The DECL_SPECIFIERS apply to this declarator. Returns a
12542 representation of the entity declared. If MEMBER_P is TRUE, then
12543 this declarator appears in a class scope. The new DECL created by
12544 this declarator is returned.
12545
12546 The CHECKS are access checks that should be performed once we know
12547 what entity is being declared (and, therefore, what classes have
12548 befriended it).
12549
12550 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12551 for a function-definition here as well. If the declarator is a
12552 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12553 be TRUE upon return. By that point, the function-definition will
12554 have been completely parsed.
12555
12556 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12557 is FALSE. */
12558
12559 static tree
12560 cp_parser_init_declarator (cp_parser* parser,
12561 cp_decl_specifier_seq *decl_specifiers,
12562 VEC (deferred_access_check,gc)* checks,
12563 bool function_definition_allowed_p,
12564 bool member_p,
12565 int declares_class_or_enum,
12566 bool* function_definition_p)
12567 {
12568 cp_token *token = NULL, *asm_spec_start_token = NULL,
12569 *attributes_start_token = NULL;
12570 cp_declarator *declarator;
12571 tree prefix_attributes;
12572 tree attributes;
12573 tree asm_specification;
12574 tree initializer;
12575 tree decl = NULL_TREE;
12576 tree scope;
12577 int is_initialized;
12578 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12579 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12580 "(...)". */
12581 enum cpp_ttype initialization_kind;
12582 bool is_direct_init = false;
12583 bool is_non_constant_init;
12584 int ctor_dtor_or_conv_p;
12585 bool friend_p;
12586 tree pushed_scope = NULL;
12587
12588 /* Gather the attributes that were provided with the
12589 decl-specifiers. */
12590 prefix_attributes = decl_specifiers->attributes;
12591
12592 /* Assume that this is not the declarator for a function
12593 definition. */
12594 if (function_definition_p)
12595 *function_definition_p = false;
12596
12597 /* Defer access checks while parsing the declarator; we cannot know
12598 what names are accessible until we know what is being
12599 declared. */
12600 resume_deferring_access_checks ();
12601
12602 /* Parse the declarator. */
12603 token = cp_lexer_peek_token (parser->lexer);
12604 declarator
12605 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12606 &ctor_dtor_or_conv_p,
12607 /*parenthesized_p=*/NULL,
12608 /*member_p=*/false);
12609 /* Gather up the deferred checks. */
12610 stop_deferring_access_checks ();
12611
12612 /* If the DECLARATOR was erroneous, there's no need to go
12613 further. */
12614 if (declarator == cp_error_declarator)
12615 return error_mark_node;
12616
12617 /* Check that the number of template-parameter-lists is OK. */
12618 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12619 token->location))
12620 return error_mark_node;
12621
12622 if (declares_class_or_enum & 2)
12623 cp_parser_check_for_definition_in_return_type (declarator,
12624 decl_specifiers->type,
12625 decl_specifiers->type_location);
12626
12627 /* Figure out what scope the entity declared by the DECLARATOR is
12628 located in. `grokdeclarator' sometimes changes the scope, so
12629 we compute it now. */
12630 scope = get_scope_of_declarator (declarator);
12631
12632 /* If we're allowing GNU extensions, look for an asm-specification
12633 and attributes. */
12634 if (cp_parser_allow_gnu_extensions_p (parser))
12635 {
12636 /* Look for an asm-specification. */
12637 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12638 asm_specification = cp_parser_asm_specification_opt (parser);
12639 /* And attributes. */
12640 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12641 attributes = cp_parser_attributes_opt (parser);
12642 }
12643 else
12644 {
12645 asm_specification = NULL_TREE;
12646 attributes = NULL_TREE;
12647 }
12648
12649 /* Peek at the next token. */
12650 token = cp_lexer_peek_token (parser->lexer);
12651 /* Check to see if the token indicates the start of a
12652 function-definition. */
12653 if (function_declarator_p (declarator)
12654 && cp_parser_token_starts_function_definition_p (token))
12655 {
12656 if (!function_definition_allowed_p)
12657 {
12658 /* If a function-definition should not appear here, issue an
12659 error message. */
12660 cp_parser_error (parser,
12661 "a function-definition is not allowed here");
12662 return error_mark_node;
12663 }
12664 else
12665 {
12666 location_t func_brace_location
12667 = cp_lexer_peek_token (parser->lexer)->location;
12668
12669 /* Neither attributes nor an asm-specification are allowed
12670 on a function-definition. */
12671 if (asm_specification)
12672 error ("%Han asm-specification is not allowed "
12673 "on a function-definition",
12674 &asm_spec_start_token->location);
12675 if (attributes)
12676 error ("%Hattributes are not allowed on a function-definition",
12677 &attributes_start_token->location);
12678 /* This is a function-definition. */
12679 *function_definition_p = true;
12680
12681 /* Parse the function definition. */
12682 if (member_p)
12683 decl = cp_parser_save_member_function_body (parser,
12684 decl_specifiers,
12685 declarator,
12686 prefix_attributes);
12687 else
12688 decl
12689 = (cp_parser_function_definition_from_specifiers_and_declarator
12690 (parser, decl_specifiers, prefix_attributes, declarator));
12691
12692 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12693 {
12694 /* This is where the prologue starts... */
12695 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12696 = func_brace_location;
12697 }
12698
12699 return decl;
12700 }
12701 }
12702
12703 /* [dcl.dcl]
12704
12705 Only in function declarations for constructors, destructors, and
12706 type conversions can the decl-specifier-seq be omitted.
12707
12708 We explicitly postpone this check past the point where we handle
12709 function-definitions because we tolerate function-definitions
12710 that are missing their return types in some modes. */
12711 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12712 {
12713 cp_parser_error (parser,
12714 "expected constructor, destructor, or type conversion");
12715 return error_mark_node;
12716 }
12717
12718 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12719 if (token->type == CPP_EQ
12720 || token->type == CPP_OPEN_PAREN
12721 || token->type == CPP_OPEN_BRACE)
12722 {
12723 is_initialized = SD_INITIALIZED;
12724 initialization_kind = token->type;
12725
12726 if (token->type == CPP_EQ
12727 && function_declarator_p (declarator))
12728 {
12729 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12730 if (t2->keyword == RID_DEFAULT)
12731 is_initialized = SD_DEFAULTED;
12732 else if (t2->keyword == RID_DELETE)
12733 is_initialized = SD_DELETED;
12734 }
12735 }
12736 else
12737 {
12738 /* If the init-declarator isn't initialized and isn't followed by a
12739 `,' or `;', it's not a valid init-declarator. */
12740 if (token->type != CPP_COMMA
12741 && token->type != CPP_SEMICOLON)
12742 {
12743 cp_parser_error (parser, "expected initializer");
12744 return error_mark_node;
12745 }
12746 is_initialized = SD_UNINITIALIZED;
12747 initialization_kind = CPP_EOF;
12748 }
12749
12750 /* Because start_decl has side-effects, we should only call it if we
12751 know we're going ahead. By this point, we know that we cannot
12752 possibly be looking at any other construct. */
12753 cp_parser_commit_to_tentative_parse (parser);
12754
12755 /* If the decl specifiers were bad, issue an error now that we're
12756 sure this was intended to be a declarator. Then continue
12757 declaring the variable(s), as int, to try to cut down on further
12758 errors. */
12759 if (decl_specifiers->any_specifiers_p
12760 && decl_specifiers->type == error_mark_node)
12761 {
12762 cp_parser_error (parser, "invalid type in declaration");
12763 decl_specifiers->type = integer_type_node;
12764 }
12765
12766 /* Check to see whether or not this declaration is a friend. */
12767 friend_p = cp_parser_friend_p (decl_specifiers);
12768
12769 /* Enter the newly declared entry in the symbol table. If we're
12770 processing a declaration in a class-specifier, we wait until
12771 after processing the initializer. */
12772 if (!member_p)
12773 {
12774 if (parser->in_unbraced_linkage_specification_p)
12775 decl_specifiers->storage_class = sc_extern;
12776 decl = start_decl (declarator, decl_specifiers,
12777 is_initialized, attributes, prefix_attributes,
12778 &pushed_scope);
12779 }
12780 else if (scope)
12781 /* Enter the SCOPE. That way unqualified names appearing in the
12782 initializer will be looked up in SCOPE. */
12783 pushed_scope = push_scope (scope);
12784
12785 /* Perform deferred access control checks, now that we know in which
12786 SCOPE the declared entity resides. */
12787 if (!member_p && decl)
12788 {
12789 tree saved_current_function_decl = NULL_TREE;
12790
12791 /* If the entity being declared is a function, pretend that we
12792 are in its scope. If it is a `friend', it may have access to
12793 things that would not otherwise be accessible. */
12794 if (TREE_CODE (decl) == FUNCTION_DECL)
12795 {
12796 saved_current_function_decl = current_function_decl;
12797 current_function_decl = decl;
12798 }
12799
12800 /* Perform access checks for template parameters. */
12801 cp_parser_perform_template_parameter_access_checks (checks);
12802
12803 /* Perform the access control checks for the declarator and the
12804 decl-specifiers. */
12805 perform_deferred_access_checks ();
12806
12807 /* Restore the saved value. */
12808 if (TREE_CODE (decl) == FUNCTION_DECL)
12809 current_function_decl = saved_current_function_decl;
12810 }
12811
12812 /* Parse the initializer. */
12813 initializer = NULL_TREE;
12814 is_direct_init = false;
12815 is_non_constant_init = true;
12816 if (is_initialized)
12817 {
12818 if (function_declarator_p (declarator))
12819 {
12820 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12821 if (initialization_kind == CPP_EQ)
12822 initializer = cp_parser_pure_specifier (parser);
12823 else
12824 {
12825 /* If the declaration was erroneous, we don't really
12826 know what the user intended, so just silently
12827 consume the initializer. */
12828 if (decl != error_mark_node)
12829 error ("%Hinitializer provided for function",
12830 &initializer_start_token->location);
12831 cp_parser_skip_to_closing_parenthesis (parser,
12832 /*recovering=*/true,
12833 /*or_comma=*/false,
12834 /*consume_paren=*/true);
12835 }
12836 }
12837 else
12838 initializer = cp_parser_initializer (parser,
12839 &is_direct_init,
12840 &is_non_constant_init);
12841 }
12842
12843 /* The old parser allows attributes to appear after a parenthesized
12844 initializer. Mark Mitchell proposed removing this functionality
12845 on the GCC mailing lists on 2002-08-13. This parser accepts the
12846 attributes -- but ignores them. */
12847 if (cp_parser_allow_gnu_extensions_p (parser)
12848 && initialization_kind == CPP_OPEN_PAREN)
12849 if (cp_parser_attributes_opt (parser))
12850 warning (OPT_Wattributes,
12851 "attributes after parenthesized initializer ignored");
12852
12853 /* For an in-class declaration, use `grokfield' to create the
12854 declaration. */
12855 if (member_p)
12856 {
12857 if (pushed_scope)
12858 {
12859 pop_scope (pushed_scope);
12860 pushed_scope = false;
12861 }
12862 decl = grokfield (declarator, decl_specifiers,
12863 initializer, !is_non_constant_init,
12864 /*asmspec=*/NULL_TREE,
12865 prefix_attributes);
12866 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12867 cp_parser_save_default_args (parser, decl);
12868 }
12869
12870 /* Finish processing the declaration. But, skip friend
12871 declarations. */
12872 if (!friend_p && decl && decl != error_mark_node)
12873 {
12874 cp_finish_decl (decl,
12875 initializer, !is_non_constant_init,
12876 asm_specification,
12877 /* If the initializer is in parentheses, then this is
12878 a direct-initialization, which means that an
12879 `explicit' constructor is OK. Otherwise, an
12880 `explicit' constructor cannot be used. */
12881 ((is_direct_init || !is_initialized)
12882 ? 0 : LOOKUP_ONLYCONVERTING));
12883 }
12884 else if ((cxx_dialect != cxx98) && friend_p
12885 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12886 /* Core issue #226 (C++0x only): A default template-argument
12887 shall not be specified in a friend class template
12888 declaration. */
12889 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12890 /*is_partial=*/0, /*is_friend_decl=*/1);
12891
12892 if (!friend_p && pushed_scope)
12893 pop_scope (pushed_scope);
12894
12895 return decl;
12896 }
12897
12898 /* Parse a declarator.
12899
12900 declarator:
12901 direct-declarator
12902 ptr-operator declarator
12903
12904 abstract-declarator:
12905 ptr-operator abstract-declarator [opt]
12906 direct-abstract-declarator
12907
12908 GNU Extensions:
12909
12910 declarator:
12911 attributes [opt] direct-declarator
12912 attributes [opt] ptr-operator declarator
12913
12914 abstract-declarator:
12915 attributes [opt] ptr-operator abstract-declarator [opt]
12916 attributes [opt] direct-abstract-declarator
12917
12918 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12919 detect constructor, destructor or conversion operators. It is set
12920 to -1 if the declarator is a name, and +1 if it is a
12921 function. Otherwise it is set to zero. Usually you just want to
12922 test for >0, but internally the negative value is used.
12923
12924 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12925 a decl-specifier-seq unless it declares a constructor, destructor,
12926 or conversion. It might seem that we could check this condition in
12927 semantic analysis, rather than parsing, but that makes it difficult
12928 to handle something like `f()'. We want to notice that there are
12929 no decl-specifiers, and therefore realize that this is an
12930 expression, not a declaration.)
12931
12932 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12933 the declarator is a direct-declarator of the form "(...)".
12934
12935 MEMBER_P is true iff this declarator is a member-declarator. */
12936
12937 static cp_declarator *
12938 cp_parser_declarator (cp_parser* parser,
12939 cp_parser_declarator_kind dcl_kind,
12940 int* ctor_dtor_or_conv_p,
12941 bool* parenthesized_p,
12942 bool member_p)
12943 {
12944 cp_token *token;
12945 cp_declarator *declarator;
12946 enum tree_code code;
12947 cp_cv_quals cv_quals;
12948 tree class_type;
12949 tree attributes = NULL_TREE;
12950
12951 /* Assume this is not a constructor, destructor, or type-conversion
12952 operator. */
12953 if (ctor_dtor_or_conv_p)
12954 *ctor_dtor_or_conv_p = 0;
12955
12956 if (cp_parser_allow_gnu_extensions_p (parser))
12957 attributes = cp_parser_attributes_opt (parser);
12958
12959 /* Peek at the next token. */
12960 token = cp_lexer_peek_token (parser->lexer);
12961
12962 /* Check for the ptr-operator production. */
12963 cp_parser_parse_tentatively (parser);
12964 /* Parse the ptr-operator. */
12965 code = cp_parser_ptr_operator (parser,
12966 &class_type,
12967 &cv_quals);
12968 /* If that worked, then we have a ptr-operator. */
12969 if (cp_parser_parse_definitely (parser))
12970 {
12971 /* If a ptr-operator was found, then this declarator was not
12972 parenthesized. */
12973 if (parenthesized_p)
12974 *parenthesized_p = true;
12975 /* The dependent declarator is optional if we are parsing an
12976 abstract-declarator. */
12977 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12978 cp_parser_parse_tentatively (parser);
12979
12980 /* Parse the dependent declarator. */
12981 declarator = cp_parser_declarator (parser, dcl_kind,
12982 /*ctor_dtor_or_conv_p=*/NULL,
12983 /*parenthesized_p=*/NULL,
12984 /*member_p=*/false);
12985
12986 /* If we are parsing an abstract-declarator, we must handle the
12987 case where the dependent declarator is absent. */
12988 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12989 && !cp_parser_parse_definitely (parser))
12990 declarator = NULL;
12991
12992 declarator = cp_parser_make_indirect_declarator
12993 (code, class_type, cv_quals, declarator);
12994 }
12995 /* Everything else is a direct-declarator. */
12996 else
12997 {
12998 if (parenthesized_p)
12999 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13000 CPP_OPEN_PAREN);
13001 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13002 ctor_dtor_or_conv_p,
13003 member_p);
13004 }
13005
13006 if (attributes && declarator && declarator != cp_error_declarator)
13007 declarator->attributes = attributes;
13008
13009 return declarator;
13010 }
13011
13012 /* Parse a direct-declarator or direct-abstract-declarator.
13013
13014 direct-declarator:
13015 declarator-id
13016 direct-declarator ( parameter-declaration-clause )
13017 cv-qualifier-seq [opt]
13018 exception-specification [opt]
13019 direct-declarator [ constant-expression [opt] ]
13020 ( declarator )
13021
13022 direct-abstract-declarator:
13023 direct-abstract-declarator [opt]
13024 ( parameter-declaration-clause )
13025 cv-qualifier-seq [opt]
13026 exception-specification [opt]
13027 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13028 ( abstract-declarator )
13029
13030 Returns a representation of the declarator. DCL_KIND is
13031 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13032 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13033 we are parsing a direct-declarator. It is
13034 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13035 of ambiguity we prefer an abstract declarator, as per
13036 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13037 cp_parser_declarator. */
13038
13039 static cp_declarator *
13040 cp_parser_direct_declarator (cp_parser* parser,
13041 cp_parser_declarator_kind dcl_kind,
13042 int* ctor_dtor_or_conv_p,
13043 bool member_p)
13044 {
13045 cp_token *token;
13046 cp_declarator *declarator = NULL;
13047 tree scope = NULL_TREE;
13048 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13049 bool saved_in_declarator_p = parser->in_declarator_p;
13050 bool first = true;
13051 tree pushed_scope = NULL_TREE;
13052
13053 while (true)
13054 {
13055 /* Peek at the next token. */
13056 token = cp_lexer_peek_token (parser->lexer);
13057 if (token->type == CPP_OPEN_PAREN)
13058 {
13059 /* This is either a parameter-declaration-clause, or a
13060 parenthesized declarator. When we know we are parsing a
13061 named declarator, it must be a parenthesized declarator
13062 if FIRST is true. For instance, `(int)' is a
13063 parameter-declaration-clause, with an omitted
13064 direct-abstract-declarator. But `((*))', is a
13065 parenthesized abstract declarator. Finally, when T is a
13066 template parameter `(T)' is a
13067 parameter-declaration-clause, and not a parenthesized
13068 named declarator.
13069
13070 We first try and parse a parameter-declaration-clause,
13071 and then try a nested declarator (if FIRST is true).
13072
13073 It is not an error for it not to be a
13074 parameter-declaration-clause, even when FIRST is
13075 false. Consider,
13076
13077 int i (int);
13078 int i (3);
13079
13080 The first is the declaration of a function while the
13081 second is the definition of a variable, including its
13082 initializer.
13083
13084 Having seen only the parenthesis, we cannot know which of
13085 these two alternatives should be selected. Even more
13086 complex are examples like:
13087
13088 int i (int (a));
13089 int i (int (3));
13090
13091 The former is a function-declaration; the latter is a
13092 variable initialization.
13093
13094 Thus again, we try a parameter-declaration-clause, and if
13095 that fails, we back out and return. */
13096
13097 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13098 {
13099 tree params;
13100 unsigned saved_num_template_parameter_lists;
13101 bool is_declarator = false;
13102 tree t;
13103
13104 /* In a member-declarator, the only valid interpretation
13105 of a parenthesis is the start of a
13106 parameter-declaration-clause. (It is invalid to
13107 initialize a static data member with a parenthesized
13108 initializer; only the "=" form of initialization is
13109 permitted.) */
13110 if (!member_p)
13111 cp_parser_parse_tentatively (parser);
13112
13113 /* Consume the `('. */
13114 cp_lexer_consume_token (parser->lexer);
13115 if (first)
13116 {
13117 /* If this is going to be an abstract declarator, we're
13118 in a declarator and we can't have default args. */
13119 parser->default_arg_ok_p = false;
13120 parser->in_declarator_p = true;
13121 }
13122
13123 /* Inside the function parameter list, surrounding
13124 template-parameter-lists do not apply. */
13125 saved_num_template_parameter_lists
13126 = parser->num_template_parameter_lists;
13127 parser->num_template_parameter_lists = 0;
13128
13129 begin_scope (sk_function_parms, NULL_TREE);
13130
13131 /* Parse the parameter-declaration-clause. */
13132 params = cp_parser_parameter_declaration_clause (parser);
13133
13134 parser->num_template_parameter_lists
13135 = saved_num_template_parameter_lists;
13136
13137 /* If all went well, parse the cv-qualifier-seq and the
13138 exception-specification. */
13139 if (member_p || cp_parser_parse_definitely (parser))
13140 {
13141 cp_cv_quals cv_quals;
13142 tree exception_specification;
13143 tree late_return;
13144
13145 is_declarator = true;
13146
13147 if (ctor_dtor_or_conv_p)
13148 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13149 first = false;
13150 /* Consume the `)'. */
13151 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13152
13153 /* Parse the cv-qualifier-seq. */
13154 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13155 /* And the exception-specification. */
13156 exception_specification
13157 = cp_parser_exception_specification_opt (parser);
13158
13159 late_return
13160 = cp_parser_late_return_type_opt (parser);
13161
13162 /* Create the function-declarator. */
13163 declarator = make_call_declarator (declarator,
13164 params,
13165 cv_quals,
13166 exception_specification,
13167 late_return);
13168 /* Any subsequent parameter lists are to do with
13169 return type, so are not those of the declared
13170 function. */
13171 parser->default_arg_ok_p = false;
13172 }
13173
13174 /* Remove the function parms from scope. */
13175 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13176 pop_binding (DECL_NAME (t), t);
13177 leave_scope();
13178
13179 if (is_declarator)
13180 /* Repeat the main loop. */
13181 continue;
13182 }
13183
13184 /* If this is the first, we can try a parenthesized
13185 declarator. */
13186 if (first)
13187 {
13188 bool saved_in_type_id_in_expr_p;
13189
13190 parser->default_arg_ok_p = saved_default_arg_ok_p;
13191 parser->in_declarator_p = saved_in_declarator_p;
13192
13193 /* Consume the `('. */
13194 cp_lexer_consume_token (parser->lexer);
13195 /* Parse the nested declarator. */
13196 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13197 parser->in_type_id_in_expr_p = true;
13198 declarator
13199 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13200 /*parenthesized_p=*/NULL,
13201 member_p);
13202 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13203 first = false;
13204 /* Expect a `)'. */
13205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13206 declarator = cp_error_declarator;
13207 if (declarator == cp_error_declarator)
13208 break;
13209
13210 goto handle_declarator;
13211 }
13212 /* Otherwise, we must be done. */
13213 else
13214 break;
13215 }
13216 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13217 && token->type == CPP_OPEN_SQUARE)
13218 {
13219 /* Parse an array-declarator. */
13220 tree bounds;
13221
13222 if (ctor_dtor_or_conv_p)
13223 *ctor_dtor_or_conv_p = 0;
13224
13225 first = false;
13226 parser->default_arg_ok_p = false;
13227 parser->in_declarator_p = true;
13228 /* Consume the `['. */
13229 cp_lexer_consume_token (parser->lexer);
13230 /* Peek at the next token. */
13231 token = cp_lexer_peek_token (parser->lexer);
13232 /* If the next token is `]', then there is no
13233 constant-expression. */
13234 if (token->type != CPP_CLOSE_SQUARE)
13235 {
13236 bool non_constant_p;
13237
13238 bounds
13239 = cp_parser_constant_expression (parser,
13240 /*allow_non_constant=*/true,
13241 &non_constant_p);
13242 if (!non_constant_p)
13243 bounds = fold_non_dependent_expr (bounds);
13244 /* Normally, the array bound must be an integral constant
13245 expression. However, as an extension, we allow VLAs
13246 in function scopes. */
13247 else if (!parser->in_function_body)
13248 {
13249 error ("%Harray bound is not an integer constant",
13250 &token->location);
13251 bounds = error_mark_node;
13252 }
13253 }
13254 else
13255 bounds = NULL_TREE;
13256 /* Look for the closing `]'. */
13257 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13258 {
13259 declarator = cp_error_declarator;
13260 break;
13261 }
13262
13263 declarator = make_array_declarator (declarator, bounds);
13264 }
13265 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13266 {
13267 tree qualifying_scope;
13268 tree unqualified_name;
13269 special_function_kind sfk;
13270 bool abstract_ok;
13271 bool pack_expansion_p = false;
13272 cp_token *declarator_id_start_token;
13273
13274 /* Parse a declarator-id */
13275 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13276 if (abstract_ok)
13277 {
13278 cp_parser_parse_tentatively (parser);
13279
13280 /* If we see an ellipsis, we should be looking at a
13281 parameter pack. */
13282 if (token->type == CPP_ELLIPSIS)
13283 {
13284 /* Consume the `...' */
13285 cp_lexer_consume_token (parser->lexer);
13286
13287 pack_expansion_p = true;
13288 }
13289 }
13290
13291 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13292 unqualified_name
13293 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13294 qualifying_scope = parser->scope;
13295 if (abstract_ok)
13296 {
13297 bool okay = false;
13298
13299 if (!unqualified_name && pack_expansion_p)
13300 {
13301 /* Check whether an error occurred. */
13302 okay = !cp_parser_error_occurred (parser);
13303
13304 /* We already consumed the ellipsis to mark a
13305 parameter pack, but we have no way to report it,
13306 so abort the tentative parse. We will be exiting
13307 immediately anyway. */
13308 cp_parser_abort_tentative_parse (parser);
13309 }
13310 else
13311 okay = cp_parser_parse_definitely (parser);
13312
13313 if (!okay)
13314 unqualified_name = error_mark_node;
13315 else if (unqualified_name
13316 && (qualifying_scope
13317 || (TREE_CODE (unqualified_name)
13318 != IDENTIFIER_NODE)))
13319 {
13320 cp_parser_error (parser, "expected unqualified-id");
13321 unqualified_name = error_mark_node;
13322 }
13323 }
13324
13325 if (!unqualified_name)
13326 return NULL;
13327 if (unqualified_name == error_mark_node)
13328 {
13329 declarator = cp_error_declarator;
13330 pack_expansion_p = false;
13331 declarator->parameter_pack_p = false;
13332 break;
13333 }
13334
13335 if (qualifying_scope && at_namespace_scope_p ()
13336 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13337 {
13338 /* In the declaration of a member of a template class
13339 outside of the class itself, the SCOPE will sometimes
13340 be a TYPENAME_TYPE. For example, given:
13341
13342 template <typename T>
13343 int S<T>::R::i = 3;
13344
13345 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13346 this context, we must resolve S<T>::R to an ordinary
13347 type, rather than a typename type.
13348
13349 The reason we normally avoid resolving TYPENAME_TYPEs
13350 is that a specialization of `S' might render
13351 `S<T>::R' not a type. However, if `S' is
13352 specialized, then this `i' will not be used, so there
13353 is no harm in resolving the types here. */
13354 tree type;
13355
13356 /* Resolve the TYPENAME_TYPE. */
13357 type = resolve_typename_type (qualifying_scope,
13358 /*only_current_p=*/false);
13359 /* If that failed, the declarator is invalid. */
13360 if (TREE_CODE (type) == TYPENAME_TYPE)
13361 error ("%H%<%T::%E%> is not a type",
13362 &declarator_id_start_token->location,
13363 TYPE_CONTEXT (qualifying_scope),
13364 TYPE_IDENTIFIER (qualifying_scope));
13365 qualifying_scope = type;
13366 }
13367
13368 sfk = sfk_none;
13369
13370 if (unqualified_name)
13371 {
13372 tree class_type;
13373
13374 if (qualifying_scope
13375 && CLASS_TYPE_P (qualifying_scope))
13376 class_type = qualifying_scope;
13377 else
13378 class_type = current_class_type;
13379
13380 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13381 {
13382 tree name_type = TREE_TYPE (unqualified_name);
13383 if (class_type && same_type_p (name_type, class_type))
13384 {
13385 if (qualifying_scope
13386 && CLASSTYPE_USE_TEMPLATE (name_type))
13387 {
13388 error ("%Hinvalid use of constructor as a template",
13389 &declarator_id_start_token->location);
13390 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13391 "name the constructor in a qualified name",
13392 class_type,
13393 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13394 class_type, name_type);
13395 declarator = cp_error_declarator;
13396 break;
13397 }
13398 else
13399 unqualified_name = constructor_name (class_type);
13400 }
13401 else
13402 {
13403 /* We do not attempt to print the declarator
13404 here because we do not have enough
13405 information about its original syntactic
13406 form. */
13407 cp_parser_error (parser, "invalid declarator");
13408 declarator = cp_error_declarator;
13409 break;
13410 }
13411 }
13412
13413 if (class_type)
13414 {
13415 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13416 sfk = sfk_destructor;
13417 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13418 sfk = sfk_conversion;
13419 else if (/* There's no way to declare a constructor
13420 for an anonymous type, even if the type
13421 got a name for linkage purposes. */
13422 !TYPE_WAS_ANONYMOUS (class_type)
13423 && constructor_name_p (unqualified_name,
13424 class_type))
13425 {
13426 unqualified_name = constructor_name (class_type);
13427 sfk = sfk_constructor;
13428 }
13429
13430 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13431 *ctor_dtor_or_conv_p = -1;
13432 }
13433 }
13434 declarator = make_id_declarator (qualifying_scope,
13435 unqualified_name,
13436 sfk);
13437 declarator->id_loc = token->location;
13438 declarator->parameter_pack_p = pack_expansion_p;
13439
13440 if (pack_expansion_p)
13441 maybe_warn_variadic_templates ();
13442
13443 handle_declarator:;
13444 scope = get_scope_of_declarator (declarator);
13445 if (scope)
13446 /* Any names that appear after the declarator-id for a
13447 member are looked up in the containing scope. */
13448 pushed_scope = push_scope (scope);
13449 parser->in_declarator_p = true;
13450 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13451 || (declarator && declarator->kind == cdk_id))
13452 /* Default args are only allowed on function
13453 declarations. */
13454 parser->default_arg_ok_p = saved_default_arg_ok_p;
13455 else
13456 parser->default_arg_ok_p = false;
13457
13458 first = false;
13459 }
13460 /* We're done. */
13461 else
13462 break;
13463 }
13464
13465 /* For an abstract declarator, we might wind up with nothing at this
13466 point. That's an error; the declarator is not optional. */
13467 if (!declarator)
13468 cp_parser_error (parser, "expected declarator");
13469
13470 /* If we entered a scope, we must exit it now. */
13471 if (pushed_scope)
13472 pop_scope (pushed_scope);
13473
13474 parser->default_arg_ok_p = saved_default_arg_ok_p;
13475 parser->in_declarator_p = saved_in_declarator_p;
13476
13477 return declarator;
13478 }
13479
13480 /* Parse a ptr-operator.
13481
13482 ptr-operator:
13483 * cv-qualifier-seq [opt]
13484 &
13485 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13486
13487 GNU Extension:
13488
13489 ptr-operator:
13490 & cv-qualifier-seq [opt]
13491
13492 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13493 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13494 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13495 filled in with the TYPE containing the member. *CV_QUALS is
13496 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13497 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13498 Note that the tree codes returned by this function have nothing
13499 to do with the types of trees that will be eventually be created
13500 to represent the pointer or reference type being parsed. They are
13501 just constants with suggestive names. */
13502 static enum tree_code
13503 cp_parser_ptr_operator (cp_parser* parser,
13504 tree* type,
13505 cp_cv_quals *cv_quals)
13506 {
13507 enum tree_code code = ERROR_MARK;
13508 cp_token *token;
13509
13510 /* Assume that it's not a pointer-to-member. */
13511 *type = NULL_TREE;
13512 /* And that there are no cv-qualifiers. */
13513 *cv_quals = TYPE_UNQUALIFIED;
13514
13515 /* Peek at the next token. */
13516 token = cp_lexer_peek_token (parser->lexer);
13517
13518 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13519 if (token->type == CPP_MULT)
13520 code = INDIRECT_REF;
13521 else if (token->type == CPP_AND)
13522 code = ADDR_EXPR;
13523 else if ((cxx_dialect != cxx98) &&
13524 token->type == CPP_AND_AND) /* C++0x only */
13525 code = NON_LVALUE_EXPR;
13526
13527 if (code != ERROR_MARK)
13528 {
13529 /* Consume the `*', `&' or `&&'. */
13530 cp_lexer_consume_token (parser->lexer);
13531
13532 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13533 `&', if we are allowing GNU extensions. (The only qualifier
13534 that can legally appear after `&' is `restrict', but that is
13535 enforced during semantic analysis. */
13536 if (code == INDIRECT_REF
13537 || cp_parser_allow_gnu_extensions_p (parser))
13538 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13539 }
13540 else
13541 {
13542 /* Try the pointer-to-member case. */
13543 cp_parser_parse_tentatively (parser);
13544 /* Look for the optional `::' operator. */
13545 cp_parser_global_scope_opt (parser,
13546 /*current_scope_valid_p=*/false);
13547 /* Look for the nested-name specifier. */
13548 token = cp_lexer_peek_token (parser->lexer);
13549 cp_parser_nested_name_specifier (parser,
13550 /*typename_keyword_p=*/false,
13551 /*check_dependency_p=*/true,
13552 /*type_p=*/false,
13553 /*is_declaration=*/false);
13554 /* If we found it, and the next token is a `*', then we are
13555 indeed looking at a pointer-to-member operator. */
13556 if (!cp_parser_error_occurred (parser)
13557 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13558 {
13559 /* Indicate that the `*' operator was used. */
13560 code = INDIRECT_REF;
13561
13562 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13563 error ("%H%qD is a namespace", &token->location, parser->scope);
13564 else
13565 {
13566 /* The type of which the member is a member is given by the
13567 current SCOPE. */
13568 *type = parser->scope;
13569 /* The next name will not be qualified. */
13570 parser->scope = NULL_TREE;
13571 parser->qualifying_scope = NULL_TREE;
13572 parser->object_scope = NULL_TREE;
13573 /* Look for the optional cv-qualifier-seq. */
13574 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13575 }
13576 }
13577 /* If that didn't work we don't have a ptr-operator. */
13578 if (!cp_parser_parse_definitely (parser))
13579 cp_parser_error (parser, "expected ptr-operator");
13580 }
13581
13582 return code;
13583 }
13584
13585 /* Parse an (optional) cv-qualifier-seq.
13586
13587 cv-qualifier-seq:
13588 cv-qualifier cv-qualifier-seq [opt]
13589
13590 cv-qualifier:
13591 const
13592 volatile
13593
13594 GNU Extension:
13595
13596 cv-qualifier:
13597 __restrict__
13598
13599 Returns a bitmask representing the cv-qualifiers. */
13600
13601 static cp_cv_quals
13602 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13603 {
13604 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13605
13606 while (true)
13607 {
13608 cp_token *token;
13609 cp_cv_quals cv_qualifier;
13610
13611 /* Peek at the next token. */
13612 token = cp_lexer_peek_token (parser->lexer);
13613 /* See if it's a cv-qualifier. */
13614 switch (token->keyword)
13615 {
13616 case RID_CONST:
13617 cv_qualifier = TYPE_QUAL_CONST;
13618 break;
13619
13620 case RID_VOLATILE:
13621 cv_qualifier = TYPE_QUAL_VOLATILE;
13622 break;
13623
13624 case RID_RESTRICT:
13625 cv_qualifier = TYPE_QUAL_RESTRICT;
13626 break;
13627
13628 default:
13629 cv_qualifier = TYPE_UNQUALIFIED;
13630 break;
13631 }
13632
13633 if (!cv_qualifier)
13634 break;
13635
13636 if (cv_quals & cv_qualifier)
13637 {
13638 error ("%Hduplicate cv-qualifier", &token->location);
13639 cp_lexer_purge_token (parser->lexer);
13640 }
13641 else
13642 {
13643 cp_lexer_consume_token (parser->lexer);
13644 cv_quals |= cv_qualifier;
13645 }
13646 }
13647
13648 return cv_quals;
13649 }
13650
13651 /* Parse a late-specified return type, if any. This is not a separate
13652 non-terminal, but part of a function declarator, which looks like
13653
13654 -> type-id
13655
13656 Returns the type indicated by the type-id. */
13657
13658 static tree
13659 cp_parser_late_return_type_opt (cp_parser* parser)
13660 {
13661 cp_token *token;
13662
13663 /* Peek at the next token. */
13664 token = cp_lexer_peek_token (parser->lexer);
13665 /* A late-specified return type is indicated by an initial '->'. */
13666 if (token->type != CPP_DEREF)
13667 return NULL_TREE;
13668
13669 /* Consume the ->. */
13670 cp_lexer_consume_token (parser->lexer);
13671
13672 return cp_parser_type_id (parser);
13673 }
13674
13675 /* Parse a declarator-id.
13676
13677 declarator-id:
13678 id-expression
13679 :: [opt] nested-name-specifier [opt] type-name
13680
13681 In the `id-expression' case, the value returned is as for
13682 cp_parser_id_expression if the id-expression was an unqualified-id.
13683 If the id-expression was a qualified-id, then a SCOPE_REF is
13684 returned. The first operand is the scope (either a NAMESPACE_DECL
13685 or TREE_TYPE), but the second is still just a representation of an
13686 unqualified-id. */
13687
13688 static tree
13689 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13690 {
13691 tree id;
13692 /* The expression must be an id-expression. Assume that qualified
13693 names are the names of types so that:
13694
13695 template <class T>
13696 int S<T>::R::i = 3;
13697
13698 will work; we must treat `S<T>::R' as the name of a type.
13699 Similarly, assume that qualified names are templates, where
13700 required, so that:
13701
13702 template <class T>
13703 int S<T>::R<T>::i = 3;
13704
13705 will work, too. */
13706 id = cp_parser_id_expression (parser,
13707 /*template_keyword_p=*/false,
13708 /*check_dependency_p=*/false,
13709 /*template_p=*/NULL,
13710 /*declarator_p=*/true,
13711 optional_p);
13712 if (id && BASELINK_P (id))
13713 id = BASELINK_FUNCTIONS (id);
13714 return id;
13715 }
13716
13717 /* Parse a type-id.
13718
13719 type-id:
13720 type-specifier-seq abstract-declarator [opt]
13721
13722 Returns the TYPE specified. */
13723
13724 static tree
13725 cp_parser_type_id (cp_parser* parser)
13726 {
13727 cp_decl_specifier_seq type_specifier_seq;
13728 cp_declarator *abstract_declarator;
13729
13730 /* Parse the type-specifier-seq. */
13731 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13732 &type_specifier_seq);
13733 if (type_specifier_seq.type == error_mark_node)
13734 return error_mark_node;
13735
13736 /* There might or might not be an abstract declarator. */
13737 cp_parser_parse_tentatively (parser);
13738 /* Look for the declarator. */
13739 abstract_declarator
13740 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13741 /*parenthesized_p=*/NULL,
13742 /*member_p=*/false);
13743 /* Check to see if there really was a declarator. */
13744 if (!cp_parser_parse_definitely (parser))
13745 abstract_declarator = NULL;
13746
13747 if (type_specifier_seq.type
13748 && type_uses_auto (type_specifier_seq.type))
13749 {
13750 error ("invalid use of %<auto%>");
13751 return error_mark_node;
13752 }
13753
13754 return groktypename (&type_specifier_seq, abstract_declarator);
13755 }
13756
13757 /* Parse a type-specifier-seq.
13758
13759 type-specifier-seq:
13760 type-specifier type-specifier-seq [opt]
13761
13762 GNU extension:
13763
13764 type-specifier-seq:
13765 attributes type-specifier-seq [opt]
13766
13767 If IS_CONDITION is true, we are at the start of a "condition",
13768 e.g., we've just seen "if (".
13769
13770 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13771
13772 static void
13773 cp_parser_type_specifier_seq (cp_parser* parser,
13774 bool is_condition,
13775 cp_decl_specifier_seq *type_specifier_seq)
13776 {
13777 bool seen_type_specifier = false;
13778 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13779 cp_token *start_token = NULL;
13780
13781 /* Clear the TYPE_SPECIFIER_SEQ. */
13782 clear_decl_specs (type_specifier_seq);
13783
13784 /* Parse the type-specifiers and attributes. */
13785 while (true)
13786 {
13787 tree type_specifier;
13788 bool is_cv_qualifier;
13789
13790 /* Check for attributes first. */
13791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13792 {
13793 type_specifier_seq->attributes =
13794 chainon (type_specifier_seq->attributes,
13795 cp_parser_attributes_opt (parser));
13796 continue;
13797 }
13798
13799 /* record the token of the beginning of the type specifier seq,
13800 for error reporting purposes*/
13801 if (!start_token)
13802 start_token = cp_lexer_peek_token (parser->lexer);
13803
13804 /* Look for the type-specifier. */
13805 type_specifier = cp_parser_type_specifier (parser,
13806 flags,
13807 type_specifier_seq,
13808 /*is_declaration=*/false,
13809 NULL,
13810 &is_cv_qualifier);
13811 if (!type_specifier)
13812 {
13813 /* If the first type-specifier could not be found, this is not a
13814 type-specifier-seq at all. */
13815 if (!seen_type_specifier)
13816 {
13817 cp_parser_error (parser, "expected type-specifier");
13818 type_specifier_seq->type = error_mark_node;
13819 return;
13820 }
13821 /* If subsequent type-specifiers could not be found, the
13822 type-specifier-seq is complete. */
13823 break;
13824 }
13825
13826 seen_type_specifier = true;
13827 /* The standard says that a condition can be:
13828
13829 type-specifier-seq declarator = assignment-expression
13830
13831 However, given:
13832
13833 struct S {};
13834 if (int S = ...)
13835
13836 we should treat the "S" as a declarator, not as a
13837 type-specifier. The standard doesn't say that explicitly for
13838 type-specifier-seq, but it does say that for
13839 decl-specifier-seq in an ordinary declaration. Perhaps it
13840 would be clearer just to allow a decl-specifier-seq here, and
13841 then add a semantic restriction that if any decl-specifiers
13842 that are not type-specifiers appear, the program is invalid. */
13843 if (is_condition && !is_cv_qualifier)
13844 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13845 }
13846
13847 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13848 }
13849
13850 /* Parse a parameter-declaration-clause.
13851
13852 parameter-declaration-clause:
13853 parameter-declaration-list [opt] ... [opt]
13854 parameter-declaration-list , ...
13855
13856 Returns a representation for the parameter declarations. A return
13857 value of NULL indicates a parameter-declaration-clause consisting
13858 only of an ellipsis. */
13859
13860 static tree
13861 cp_parser_parameter_declaration_clause (cp_parser* parser)
13862 {
13863 tree parameters;
13864 cp_token *token;
13865 bool ellipsis_p;
13866 bool is_error;
13867
13868 /* Peek at the next token. */
13869 token = cp_lexer_peek_token (parser->lexer);
13870 /* Check for trivial parameter-declaration-clauses. */
13871 if (token->type == CPP_ELLIPSIS)
13872 {
13873 /* Consume the `...' token. */
13874 cp_lexer_consume_token (parser->lexer);
13875 return NULL_TREE;
13876 }
13877 else if (token->type == CPP_CLOSE_PAREN)
13878 /* There are no parameters. */
13879 {
13880 #ifndef NO_IMPLICIT_EXTERN_C
13881 if (in_system_header && current_class_type == NULL
13882 && current_lang_name == lang_name_c)
13883 return NULL_TREE;
13884 else
13885 #endif
13886 return void_list_node;
13887 }
13888 /* Check for `(void)', too, which is a special case. */
13889 else if (token->keyword == RID_VOID
13890 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13891 == CPP_CLOSE_PAREN))
13892 {
13893 /* Consume the `void' token. */
13894 cp_lexer_consume_token (parser->lexer);
13895 /* There are no parameters. */
13896 return void_list_node;
13897 }
13898
13899 /* Parse the parameter-declaration-list. */
13900 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13901 /* If a parse error occurred while parsing the
13902 parameter-declaration-list, then the entire
13903 parameter-declaration-clause is erroneous. */
13904 if (is_error)
13905 return NULL;
13906
13907 /* Peek at the next token. */
13908 token = cp_lexer_peek_token (parser->lexer);
13909 /* If it's a `,', the clause should terminate with an ellipsis. */
13910 if (token->type == CPP_COMMA)
13911 {
13912 /* Consume the `,'. */
13913 cp_lexer_consume_token (parser->lexer);
13914 /* Expect an ellipsis. */
13915 ellipsis_p
13916 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13917 }
13918 /* It might also be `...' if the optional trailing `,' was
13919 omitted. */
13920 else if (token->type == CPP_ELLIPSIS)
13921 {
13922 /* Consume the `...' token. */
13923 cp_lexer_consume_token (parser->lexer);
13924 /* And remember that we saw it. */
13925 ellipsis_p = true;
13926 }
13927 else
13928 ellipsis_p = false;
13929
13930 /* Finish the parameter list. */
13931 if (!ellipsis_p)
13932 parameters = chainon (parameters, void_list_node);
13933
13934 return parameters;
13935 }
13936
13937 /* Parse a parameter-declaration-list.
13938
13939 parameter-declaration-list:
13940 parameter-declaration
13941 parameter-declaration-list , parameter-declaration
13942
13943 Returns a representation of the parameter-declaration-list, as for
13944 cp_parser_parameter_declaration_clause. However, the
13945 `void_list_node' is never appended to the list. Upon return,
13946 *IS_ERROR will be true iff an error occurred. */
13947
13948 static tree
13949 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13950 {
13951 tree parameters = NULL_TREE;
13952 tree *tail = &parameters;
13953 bool saved_in_unbraced_linkage_specification_p;
13954
13955 /* Assume all will go well. */
13956 *is_error = false;
13957 /* The special considerations that apply to a function within an
13958 unbraced linkage specifications do not apply to the parameters
13959 to the function. */
13960 saved_in_unbraced_linkage_specification_p
13961 = parser->in_unbraced_linkage_specification_p;
13962 parser->in_unbraced_linkage_specification_p = false;
13963
13964 /* Look for more parameters. */
13965 while (true)
13966 {
13967 cp_parameter_declarator *parameter;
13968 tree decl = error_mark_node;
13969 bool parenthesized_p;
13970 /* Parse the parameter. */
13971 parameter
13972 = cp_parser_parameter_declaration (parser,
13973 /*template_parm_p=*/false,
13974 &parenthesized_p);
13975
13976 /* We don't know yet if the enclosing context is deprecated, so wait
13977 and warn in grokparms if appropriate. */
13978 deprecated_state = DEPRECATED_SUPPRESS;
13979
13980 if (parameter)
13981 decl = grokdeclarator (parameter->declarator,
13982 &parameter->decl_specifiers,
13983 PARM,
13984 parameter->default_argument != NULL_TREE,
13985 &parameter->decl_specifiers.attributes);
13986
13987 deprecated_state = DEPRECATED_NORMAL;
13988
13989 /* If a parse error occurred parsing the parameter declaration,
13990 then the entire parameter-declaration-list is erroneous. */
13991 if (decl == error_mark_node)
13992 {
13993 *is_error = true;
13994 parameters = error_mark_node;
13995 break;
13996 }
13997
13998 if (parameter->decl_specifiers.attributes)
13999 cplus_decl_attributes (&decl,
14000 parameter->decl_specifiers.attributes,
14001 0);
14002 if (DECL_NAME (decl))
14003 decl = pushdecl (decl);
14004
14005 /* Add the new parameter to the list. */
14006 *tail = build_tree_list (parameter->default_argument, decl);
14007 tail = &TREE_CHAIN (*tail);
14008
14009 /* Peek at the next token. */
14010 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14011 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14012 /* These are for Objective-C++ */
14013 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14014 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14015 /* The parameter-declaration-list is complete. */
14016 break;
14017 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14018 {
14019 cp_token *token;
14020
14021 /* Peek at the next token. */
14022 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14023 /* If it's an ellipsis, then the list is complete. */
14024 if (token->type == CPP_ELLIPSIS)
14025 break;
14026 /* Otherwise, there must be more parameters. Consume the
14027 `,'. */
14028 cp_lexer_consume_token (parser->lexer);
14029 /* When parsing something like:
14030
14031 int i(float f, double d)
14032
14033 we can tell after seeing the declaration for "f" that we
14034 are not looking at an initialization of a variable "i",
14035 but rather at the declaration of a function "i".
14036
14037 Due to the fact that the parsing of template arguments
14038 (as specified to a template-id) requires backtracking we
14039 cannot use this technique when inside a template argument
14040 list. */
14041 if (!parser->in_template_argument_list_p
14042 && !parser->in_type_id_in_expr_p
14043 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14044 /* However, a parameter-declaration of the form
14045 "foat(f)" (which is a valid declaration of a
14046 parameter "f") can also be interpreted as an
14047 expression (the conversion of "f" to "float"). */
14048 && !parenthesized_p)
14049 cp_parser_commit_to_tentative_parse (parser);
14050 }
14051 else
14052 {
14053 cp_parser_error (parser, "expected %<,%> or %<...%>");
14054 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14055 cp_parser_skip_to_closing_parenthesis (parser,
14056 /*recovering=*/true,
14057 /*or_comma=*/false,
14058 /*consume_paren=*/false);
14059 break;
14060 }
14061 }
14062
14063 parser->in_unbraced_linkage_specification_p
14064 = saved_in_unbraced_linkage_specification_p;
14065
14066 return parameters;
14067 }
14068
14069 /* Parse a parameter declaration.
14070
14071 parameter-declaration:
14072 decl-specifier-seq ... [opt] declarator
14073 decl-specifier-seq declarator = assignment-expression
14074 decl-specifier-seq ... [opt] abstract-declarator [opt]
14075 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14076
14077 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14078 declares a template parameter. (In that case, a non-nested `>'
14079 token encountered during the parsing of the assignment-expression
14080 is not interpreted as a greater-than operator.)
14081
14082 Returns a representation of the parameter, or NULL if an error
14083 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14084 true iff the declarator is of the form "(p)". */
14085
14086 static cp_parameter_declarator *
14087 cp_parser_parameter_declaration (cp_parser *parser,
14088 bool template_parm_p,
14089 bool *parenthesized_p)
14090 {
14091 int declares_class_or_enum;
14092 bool greater_than_is_operator_p;
14093 cp_decl_specifier_seq decl_specifiers;
14094 cp_declarator *declarator;
14095 tree default_argument;
14096 cp_token *token = NULL, *declarator_token_start = NULL;
14097 const char *saved_message;
14098
14099 /* In a template parameter, `>' is not an operator.
14100
14101 [temp.param]
14102
14103 When parsing a default template-argument for a non-type
14104 template-parameter, the first non-nested `>' is taken as the end
14105 of the template parameter-list rather than a greater-than
14106 operator. */
14107 greater_than_is_operator_p = !template_parm_p;
14108
14109 /* Type definitions may not appear in parameter types. */
14110 saved_message = parser->type_definition_forbidden_message;
14111 parser->type_definition_forbidden_message
14112 = "types may not be defined in parameter types";
14113
14114 /* Parse the declaration-specifiers. */
14115 cp_parser_decl_specifier_seq (parser,
14116 CP_PARSER_FLAGS_NONE,
14117 &decl_specifiers,
14118 &declares_class_or_enum);
14119 /* If an error occurred, there's no reason to attempt to parse the
14120 rest of the declaration. */
14121 if (cp_parser_error_occurred (parser))
14122 {
14123 parser->type_definition_forbidden_message = saved_message;
14124 return NULL;
14125 }
14126
14127 /* Peek at the next token. */
14128 token = cp_lexer_peek_token (parser->lexer);
14129
14130 /* If the next token is a `)', `,', `=', `>', or `...', then there
14131 is no declarator. However, when variadic templates are enabled,
14132 there may be a declarator following `...'. */
14133 if (token->type == CPP_CLOSE_PAREN
14134 || token->type == CPP_COMMA
14135 || token->type == CPP_EQ
14136 || token->type == CPP_GREATER)
14137 {
14138 declarator = NULL;
14139 if (parenthesized_p)
14140 *parenthesized_p = false;
14141 }
14142 /* Otherwise, there should be a declarator. */
14143 else
14144 {
14145 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14146 parser->default_arg_ok_p = false;
14147
14148 /* After seeing a decl-specifier-seq, if the next token is not a
14149 "(", there is no possibility that the code is a valid
14150 expression. Therefore, if parsing tentatively, we commit at
14151 this point. */
14152 if (!parser->in_template_argument_list_p
14153 /* In an expression context, having seen:
14154
14155 (int((char ...
14156
14157 we cannot be sure whether we are looking at a
14158 function-type (taking a "char" as a parameter) or a cast
14159 of some object of type "char" to "int". */
14160 && !parser->in_type_id_in_expr_p
14161 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14162 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14163 cp_parser_commit_to_tentative_parse (parser);
14164 /* Parse the declarator. */
14165 declarator_token_start = token;
14166 declarator = cp_parser_declarator (parser,
14167 CP_PARSER_DECLARATOR_EITHER,
14168 /*ctor_dtor_or_conv_p=*/NULL,
14169 parenthesized_p,
14170 /*member_p=*/false);
14171 parser->default_arg_ok_p = saved_default_arg_ok_p;
14172 /* After the declarator, allow more attributes. */
14173 decl_specifiers.attributes
14174 = chainon (decl_specifiers.attributes,
14175 cp_parser_attributes_opt (parser));
14176 }
14177
14178 /* If the next token is an ellipsis, and we have not seen a
14179 declarator name, and the type of the declarator contains parameter
14180 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14181 a parameter pack expansion expression. Otherwise, leave the
14182 ellipsis for a C-style variadic function. */
14183 token = cp_lexer_peek_token (parser->lexer);
14184 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14185 {
14186 tree type = decl_specifiers.type;
14187
14188 if (type && DECL_P (type))
14189 type = TREE_TYPE (type);
14190
14191 if (type
14192 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14193 && declarator_can_be_parameter_pack (declarator)
14194 && (!declarator || !declarator->parameter_pack_p)
14195 && uses_parameter_packs (type))
14196 {
14197 /* Consume the `...'. */
14198 cp_lexer_consume_token (parser->lexer);
14199 maybe_warn_variadic_templates ();
14200
14201 /* Build a pack expansion type */
14202 if (declarator)
14203 declarator->parameter_pack_p = true;
14204 else
14205 decl_specifiers.type = make_pack_expansion (type);
14206 }
14207 }
14208
14209 /* The restriction on defining new types applies only to the type
14210 of the parameter, not to the default argument. */
14211 parser->type_definition_forbidden_message = saved_message;
14212
14213 /* If the next token is `=', then process a default argument. */
14214 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14215 {
14216 /* Consume the `='. */
14217 cp_lexer_consume_token (parser->lexer);
14218
14219 /* If we are defining a class, then the tokens that make up the
14220 default argument must be saved and processed later. */
14221 if (!template_parm_p && at_class_scope_p ()
14222 && TYPE_BEING_DEFINED (current_class_type))
14223 {
14224 unsigned depth = 0;
14225 int maybe_template_id = 0;
14226 cp_token *first_token;
14227 cp_token *token;
14228
14229 /* Add tokens until we have processed the entire default
14230 argument. We add the range [first_token, token). */
14231 first_token = cp_lexer_peek_token (parser->lexer);
14232 while (true)
14233 {
14234 bool done = false;
14235
14236 /* Peek at the next token. */
14237 token = cp_lexer_peek_token (parser->lexer);
14238 /* What we do depends on what token we have. */
14239 switch (token->type)
14240 {
14241 /* In valid code, a default argument must be
14242 immediately followed by a `,' `)', or `...'. */
14243 case CPP_COMMA:
14244 if (depth == 0 && maybe_template_id)
14245 {
14246 /* If we've seen a '<', we might be in a
14247 template-argument-list. Until Core issue 325 is
14248 resolved, we don't know how this situation ought
14249 to be handled, so try to DTRT. We check whether
14250 what comes after the comma is a valid parameter
14251 declaration list. If it is, then the comma ends
14252 the default argument; otherwise the default
14253 argument continues. */
14254 bool error = false;
14255
14256 /* Set ITALP so cp_parser_parameter_declaration_list
14257 doesn't decide to commit to this parse. */
14258 bool saved_italp = parser->in_template_argument_list_p;
14259 parser->in_template_argument_list_p = true;
14260
14261 cp_parser_parse_tentatively (parser);
14262 cp_lexer_consume_token (parser->lexer);
14263 cp_parser_parameter_declaration_list (parser, &error);
14264 if (!cp_parser_error_occurred (parser) && !error)
14265 done = true;
14266 cp_parser_abort_tentative_parse (parser);
14267
14268 parser->in_template_argument_list_p = saved_italp;
14269 break;
14270 }
14271 case CPP_CLOSE_PAREN:
14272 case CPP_ELLIPSIS:
14273 /* If we run into a non-nested `;', `}', or `]',
14274 then the code is invalid -- but the default
14275 argument is certainly over. */
14276 case CPP_SEMICOLON:
14277 case CPP_CLOSE_BRACE:
14278 case CPP_CLOSE_SQUARE:
14279 if (depth == 0)
14280 done = true;
14281 /* Update DEPTH, if necessary. */
14282 else if (token->type == CPP_CLOSE_PAREN
14283 || token->type == CPP_CLOSE_BRACE
14284 || token->type == CPP_CLOSE_SQUARE)
14285 --depth;
14286 break;
14287
14288 case CPP_OPEN_PAREN:
14289 case CPP_OPEN_SQUARE:
14290 case CPP_OPEN_BRACE:
14291 ++depth;
14292 break;
14293
14294 case CPP_LESS:
14295 if (depth == 0)
14296 /* This might be the comparison operator, or it might
14297 start a template argument list. */
14298 ++maybe_template_id;
14299 break;
14300
14301 case CPP_RSHIFT:
14302 if (cxx_dialect == cxx98)
14303 break;
14304 /* Fall through for C++0x, which treats the `>>'
14305 operator like two `>' tokens in certain
14306 cases. */
14307
14308 case CPP_GREATER:
14309 if (depth == 0)
14310 {
14311 /* This might be an operator, or it might close a
14312 template argument list. But if a previous '<'
14313 started a template argument list, this will have
14314 closed it, so we can't be in one anymore. */
14315 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14316 if (maybe_template_id < 0)
14317 maybe_template_id = 0;
14318 }
14319 break;
14320
14321 /* If we run out of tokens, issue an error message. */
14322 case CPP_EOF:
14323 case CPP_PRAGMA_EOL:
14324 error ("%Hfile ends in default argument", &token->location);
14325 done = true;
14326 break;
14327
14328 case CPP_NAME:
14329 case CPP_SCOPE:
14330 /* In these cases, we should look for template-ids.
14331 For example, if the default argument is
14332 `X<int, double>()', we need to do name lookup to
14333 figure out whether or not `X' is a template; if
14334 so, the `,' does not end the default argument.
14335
14336 That is not yet done. */
14337 break;
14338
14339 default:
14340 break;
14341 }
14342
14343 /* If we've reached the end, stop. */
14344 if (done)
14345 break;
14346
14347 /* Add the token to the token block. */
14348 token = cp_lexer_consume_token (parser->lexer);
14349 }
14350
14351 /* Create a DEFAULT_ARG to represent the unparsed default
14352 argument. */
14353 default_argument = make_node (DEFAULT_ARG);
14354 DEFARG_TOKENS (default_argument)
14355 = cp_token_cache_new (first_token, token);
14356 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14357 }
14358 /* Outside of a class definition, we can just parse the
14359 assignment-expression. */
14360 else
14361 {
14362 token = cp_lexer_peek_token (parser->lexer);
14363 default_argument
14364 = cp_parser_default_argument (parser, template_parm_p);
14365 }
14366
14367 if (!parser->default_arg_ok_p)
14368 {
14369 if (flag_permissive)
14370 warning (0, "deprecated use of default argument for parameter of non-function");
14371 else
14372 {
14373 error ("%Hdefault arguments are only "
14374 "permitted for function parameters",
14375 &token->location);
14376 default_argument = NULL_TREE;
14377 }
14378 }
14379 else if ((declarator && declarator->parameter_pack_p)
14380 || (decl_specifiers.type
14381 && PACK_EXPANSION_P (decl_specifiers.type)))
14382 {
14383 const char* kind = template_parm_p? "template " : "";
14384
14385 /* Find the name of the parameter pack. */
14386 cp_declarator *id_declarator = declarator;
14387 while (id_declarator && id_declarator->kind != cdk_id)
14388 id_declarator = id_declarator->declarator;
14389
14390 if (id_declarator && id_declarator->kind == cdk_id)
14391 error ("%H%sparameter pack %qD cannot have a default argument",
14392 &declarator_token_start->location,
14393 kind, id_declarator->u.id.unqualified_name);
14394 else
14395 error ("%H%sparameter pack cannot have a default argument",
14396 &declarator_token_start->location, kind);
14397
14398 default_argument = NULL_TREE;
14399 }
14400 }
14401 else
14402 default_argument = NULL_TREE;
14403
14404 return make_parameter_declarator (&decl_specifiers,
14405 declarator,
14406 default_argument);
14407 }
14408
14409 /* Parse a default argument and return it.
14410
14411 TEMPLATE_PARM_P is true if this is a default argument for a
14412 non-type template parameter. */
14413 static tree
14414 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14415 {
14416 tree default_argument = NULL_TREE;
14417 bool saved_greater_than_is_operator_p;
14418 bool saved_local_variables_forbidden_p;
14419
14420 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14421 set correctly. */
14422 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14423 parser->greater_than_is_operator_p = !template_parm_p;
14424 /* Local variable names (and the `this' keyword) may not
14425 appear in a default argument. */
14426 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14427 parser->local_variables_forbidden_p = true;
14428 /* The default argument expression may cause implicitly
14429 defined member functions to be synthesized, which will
14430 result in garbage collection. We must treat this
14431 situation as if we were within the body of function so as
14432 to avoid collecting live data on the stack. */
14433 ++function_depth;
14434 /* Parse the assignment-expression. */
14435 if (template_parm_p)
14436 push_deferring_access_checks (dk_no_deferred);
14437 default_argument
14438 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14439 if (template_parm_p)
14440 pop_deferring_access_checks ();
14441 /* Restore saved state. */
14442 --function_depth;
14443 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14444 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14445
14446 return default_argument;
14447 }
14448
14449 /* Parse a function-body.
14450
14451 function-body:
14452 compound_statement */
14453
14454 static void
14455 cp_parser_function_body (cp_parser *parser)
14456 {
14457 cp_parser_compound_statement (parser, NULL, false);
14458 }
14459
14460 /* Parse a ctor-initializer-opt followed by a function-body. Return
14461 true if a ctor-initializer was present. */
14462
14463 static bool
14464 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14465 {
14466 tree body;
14467 bool ctor_initializer_p;
14468
14469 /* Begin the function body. */
14470 body = begin_function_body ();
14471 /* Parse the optional ctor-initializer. */
14472 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14473 /* Parse the function-body. */
14474 cp_parser_function_body (parser);
14475 /* Finish the function body. */
14476 finish_function_body (body);
14477
14478 return ctor_initializer_p;
14479 }
14480
14481 /* Parse an initializer.
14482
14483 initializer:
14484 = initializer-clause
14485 ( expression-list )
14486
14487 Returns an expression representing the initializer. If no
14488 initializer is present, NULL_TREE is returned.
14489
14490 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14491 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14492 set to TRUE if there is no initializer present. If there is an
14493 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14494 is set to true; otherwise it is set to false. */
14495
14496 static tree
14497 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14498 bool* non_constant_p)
14499 {
14500 cp_token *token;
14501 tree init;
14502
14503 /* Peek at the next token. */
14504 token = cp_lexer_peek_token (parser->lexer);
14505
14506 /* Let our caller know whether or not this initializer was
14507 parenthesized. */
14508 *is_direct_init = (token->type != CPP_EQ);
14509 /* Assume that the initializer is constant. */
14510 *non_constant_p = false;
14511
14512 if (token->type == CPP_EQ)
14513 {
14514 /* Consume the `='. */
14515 cp_lexer_consume_token (parser->lexer);
14516 /* Parse the initializer-clause. */
14517 init = cp_parser_initializer_clause (parser, non_constant_p);
14518 }
14519 else if (token->type == CPP_OPEN_PAREN)
14520 init = cp_parser_parenthesized_expression_list (parser, false,
14521 /*cast_p=*/false,
14522 /*allow_expansion_p=*/true,
14523 non_constant_p);
14524 else if (token->type == CPP_OPEN_BRACE)
14525 {
14526 maybe_warn_cpp0x ("extended initializer lists");
14527 init = cp_parser_braced_list (parser, non_constant_p);
14528 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14529 }
14530 else
14531 {
14532 /* Anything else is an error. */
14533 cp_parser_error (parser, "expected initializer");
14534 init = error_mark_node;
14535 }
14536
14537 return init;
14538 }
14539
14540 /* Parse an initializer-clause.
14541
14542 initializer-clause:
14543 assignment-expression
14544 braced-init-list
14545
14546 Returns an expression representing the initializer.
14547
14548 If the `assignment-expression' production is used the value
14549 returned is simply a representation for the expression.
14550
14551 Otherwise, calls cp_parser_braced_list. */
14552
14553 static tree
14554 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14555 {
14556 tree initializer;
14557
14558 /* Assume the expression is constant. */
14559 *non_constant_p = false;
14560
14561 /* If it is not a `{', then we are looking at an
14562 assignment-expression. */
14563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14564 {
14565 initializer
14566 = cp_parser_constant_expression (parser,
14567 /*allow_non_constant_p=*/true,
14568 non_constant_p);
14569 if (!*non_constant_p)
14570 initializer = fold_non_dependent_expr (initializer);
14571 }
14572 else
14573 initializer = cp_parser_braced_list (parser, non_constant_p);
14574
14575 return initializer;
14576 }
14577
14578 /* Parse a brace-enclosed initializer list.
14579
14580 braced-init-list:
14581 { initializer-list , [opt] }
14582 { }
14583
14584 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14585 the elements of the initializer-list (or NULL, if the last
14586 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14587 NULL_TREE. There is no way to detect whether or not the optional
14588 trailing `,' was provided. NON_CONSTANT_P is as for
14589 cp_parser_initializer. */
14590
14591 static tree
14592 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14593 {
14594 tree initializer;
14595
14596 /* Consume the `{' token. */
14597 cp_lexer_consume_token (parser->lexer);
14598 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14599 initializer = make_node (CONSTRUCTOR);
14600 /* If it's not a `}', then there is a non-trivial initializer. */
14601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14602 {
14603 /* Parse the initializer list. */
14604 CONSTRUCTOR_ELTS (initializer)
14605 = cp_parser_initializer_list (parser, non_constant_p);
14606 /* A trailing `,' token is allowed. */
14607 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14608 cp_lexer_consume_token (parser->lexer);
14609 }
14610 /* Now, there should be a trailing `}'. */
14611 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14612 TREE_TYPE (initializer) = init_list_type_node;
14613 return initializer;
14614 }
14615
14616 /* Parse an initializer-list.
14617
14618 initializer-list:
14619 initializer-clause ... [opt]
14620 initializer-list , initializer-clause ... [opt]
14621
14622 GNU Extension:
14623
14624 initializer-list:
14625 identifier : initializer-clause
14626 initializer-list, identifier : initializer-clause
14627
14628 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14629 for the initializer. If the INDEX of the elt is non-NULL, it is the
14630 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14631 as for cp_parser_initializer. */
14632
14633 static VEC(constructor_elt,gc) *
14634 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14635 {
14636 VEC(constructor_elt,gc) *v = NULL;
14637
14638 /* Assume all of the expressions are constant. */
14639 *non_constant_p = false;
14640
14641 /* Parse the rest of the list. */
14642 while (true)
14643 {
14644 cp_token *token;
14645 tree identifier;
14646 tree initializer;
14647 bool clause_non_constant_p;
14648
14649 /* If the next token is an identifier and the following one is a
14650 colon, we are looking at the GNU designated-initializer
14651 syntax. */
14652 if (cp_parser_allow_gnu_extensions_p (parser)
14653 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14654 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14655 {
14656 /* Warn the user that they are using an extension. */
14657 pedwarn (input_location, OPT_pedantic,
14658 "ISO C++ does not allow designated initializers");
14659 /* Consume the identifier. */
14660 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14661 /* Consume the `:'. */
14662 cp_lexer_consume_token (parser->lexer);
14663 }
14664 else
14665 identifier = NULL_TREE;
14666
14667 /* Parse the initializer. */
14668 initializer = cp_parser_initializer_clause (parser,
14669 &clause_non_constant_p);
14670 /* If any clause is non-constant, so is the entire initializer. */
14671 if (clause_non_constant_p)
14672 *non_constant_p = true;
14673
14674 /* If we have an ellipsis, this is an initializer pack
14675 expansion. */
14676 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14677 {
14678 /* Consume the `...'. */
14679 cp_lexer_consume_token (parser->lexer);
14680
14681 /* Turn the initializer into an initializer expansion. */
14682 initializer = make_pack_expansion (initializer);
14683 }
14684
14685 /* Add it to the vector. */
14686 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14687
14688 /* If the next token is not a comma, we have reached the end of
14689 the list. */
14690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14691 break;
14692
14693 /* Peek at the next token. */
14694 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14695 /* If the next token is a `}', then we're still done. An
14696 initializer-clause can have a trailing `,' after the
14697 initializer-list and before the closing `}'. */
14698 if (token->type == CPP_CLOSE_BRACE)
14699 break;
14700
14701 /* Consume the `,' token. */
14702 cp_lexer_consume_token (parser->lexer);
14703 }
14704
14705 return v;
14706 }
14707
14708 /* Classes [gram.class] */
14709
14710 /* Parse a class-name.
14711
14712 class-name:
14713 identifier
14714 template-id
14715
14716 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14717 to indicate that names looked up in dependent types should be
14718 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14719 keyword has been used to indicate that the name that appears next
14720 is a template. TAG_TYPE indicates the explicit tag given before
14721 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14722 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14723 is the class being defined in a class-head.
14724
14725 Returns the TYPE_DECL representing the class. */
14726
14727 static tree
14728 cp_parser_class_name (cp_parser *parser,
14729 bool typename_keyword_p,
14730 bool template_keyword_p,
14731 enum tag_types tag_type,
14732 bool check_dependency_p,
14733 bool class_head_p,
14734 bool is_declaration)
14735 {
14736 tree decl;
14737 tree scope;
14738 bool typename_p;
14739 cp_token *token;
14740 tree identifier = NULL_TREE;
14741
14742 /* All class-names start with an identifier. */
14743 token = cp_lexer_peek_token (parser->lexer);
14744 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14745 {
14746 cp_parser_error (parser, "expected class-name");
14747 return error_mark_node;
14748 }
14749
14750 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14751 to a template-id, so we save it here. */
14752 scope = parser->scope;
14753 if (scope == error_mark_node)
14754 return error_mark_node;
14755
14756 /* Any name names a type if we're following the `typename' keyword
14757 in a qualified name where the enclosing scope is type-dependent. */
14758 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14759 && dependent_type_p (scope));
14760 /* Handle the common case (an identifier, but not a template-id)
14761 efficiently. */
14762 if (token->type == CPP_NAME
14763 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14764 {
14765 cp_token *identifier_token;
14766 bool ambiguous_p;
14767
14768 /* Look for the identifier. */
14769 identifier_token = cp_lexer_peek_token (parser->lexer);
14770 ambiguous_p = identifier_token->ambiguous_p;
14771 identifier = cp_parser_identifier (parser);
14772 /* If the next token isn't an identifier, we are certainly not
14773 looking at a class-name. */
14774 if (identifier == error_mark_node)
14775 decl = error_mark_node;
14776 /* If we know this is a type-name, there's no need to look it
14777 up. */
14778 else if (typename_p)
14779 decl = identifier;
14780 else
14781 {
14782 tree ambiguous_decls;
14783 /* If we already know that this lookup is ambiguous, then
14784 we've already issued an error message; there's no reason
14785 to check again. */
14786 if (ambiguous_p)
14787 {
14788 cp_parser_simulate_error (parser);
14789 return error_mark_node;
14790 }
14791 /* If the next token is a `::', then the name must be a type
14792 name.
14793
14794 [basic.lookup.qual]
14795
14796 During the lookup for a name preceding the :: scope
14797 resolution operator, object, function, and enumerator
14798 names are ignored. */
14799 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14800 tag_type = typename_type;
14801 /* Look up the name. */
14802 decl = cp_parser_lookup_name (parser, identifier,
14803 tag_type,
14804 /*is_template=*/false,
14805 /*is_namespace=*/false,
14806 check_dependency_p,
14807 &ambiguous_decls,
14808 identifier_token->location);
14809 if (ambiguous_decls)
14810 {
14811 error ("%Hreference to %qD is ambiguous",
14812 &identifier_token->location, identifier);
14813 print_candidates (ambiguous_decls);
14814 if (cp_parser_parsing_tentatively (parser))
14815 {
14816 identifier_token->ambiguous_p = true;
14817 cp_parser_simulate_error (parser);
14818 }
14819 return error_mark_node;
14820 }
14821 }
14822 }
14823 else
14824 {
14825 /* Try a template-id. */
14826 decl = cp_parser_template_id (parser, template_keyword_p,
14827 check_dependency_p,
14828 is_declaration);
14829 if (decl == error_mark_node)
14830 return error_mark_node;
14831 }
14832
14833 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14834
14835 /* If this is a typename, create a TYPENAME_TYPE. */
14836 if (typename_p && decl != error_mark_node)
14837 {
14838 decl = make_typename_type (scope, decl, typename_type,
14839 /*complain=*/tf_error);
14840 if (decl != error_mark_node)
14841 decl = TYPE_NAME (decl);
14842 }
14843
14844 /* Check to see that it is really the name of a class. */
14845 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14846 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14847 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14848 /* Situations like this:
14849
14850 template <typename T> struct A {
14851 typename T::template X<int>::I i;
14852 };
14853
14854 are problematic. Is `T::template X<int>' a class-name? The
14855 standard does not seem to be definitive, but there is no other
14856 valid interpretation of the following `::'. Therefore, those
14857 names are considered class-names. */
14858 {
14859 decl = make_typename_type (scope, decl, tag_type, tf_error);
14860 if (decl != error_mark_node)
14861 decl = TYPE_NAME (decl);
14862 }
14863 else if (TREE_CODE (decl) != TYPE_DECL
14864 || TREE_TYPE (decl) == error_mark_node
14865 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14866 decl = error_mark_node;
14867
14868 if (decl == error_mark_node)
14869 cp_parser_error (parser, "expected class-name");
14870 else if (identifier && !parser->scope)
14871 maybe_note_name_used_in_class (identifier, decl);
14872
14873 return decl;
14874 }
14875
14876 /* Parse a class-specifier.
14877
14878 class-specifier:
14879 class-head { member-specification [opt] }
14880
14881 Returns the TREE_TYPE representing the class. */
14882
14883 static tree
14884 cp_parser_class_specifier (cp_parser* parser)
14885 {
14886 cp_token *token;
14887 tree type;
14888 tree attributes = NULL_TREE;
14889 int has_trailing_semicolon;
14890 bool nested_name_specifier_p;
14891 unsigned saved_num_template_parameter_lists;
14892 bool saved_in_function_body;
14893 bool saved_in_unbraced_linkage_specification_p;
14894 tree old_scope = NULL_TREE;
14895 tree scope = NULL_TREE;
14896 tree bases;
14897
14898 push_deferring_access_checks (dk_no_deferred);
14899
14900 /* Parse the class-head. */
14901 type = cp_parser_class_head (parser,
14902 &nested_name_specifier_p,
14903 &attributes,
14904 &bases);
14905 /* If the class-head was a semantic disaster, skip the entire body
14906 of the class. */
14907 if (!type)
14908 {
14909 cp_parser_skip_to_end_of_block_or_statement (parser);
14910 pop_deferring_access_checks ();
14911 return error_mark_node;
14912 }
14913
14914 /* Look for the `{'. */
14915 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14916 {
14917 pop_deferring_access_checks ();
14918 return error_mark_node;
14919 }
14920
14921 /* Process the base classes. If they're invalid, skip the
14922 entire class body. */
14923 if (!xref_basetypes (type, bases))
14924 {
14925 /* Consuming the closing brace yields better error messages
14926 later on. */
14927 if (cp_parser_skip_to_closing_brace (parser))
14928 cp_lexer_consume_token (parser->lexer);
14929 pop_deferring_access_checks ();
14930 return error_mark_node;
14931 }
14932
14933 /* Issue an error message if type-definitions are forbidden here. */
14934 cp_parser_check_type_definition (parser);
14935 /* Remember that we are defining one more class. */
14936 ++parser->num_classes_being_defined;
14937 /* Inside the class, surrounding template-parameter-lists do not
14938 apply. */
14939 saved_num_template_parameter_lists
14940 = parser->num_template_parameter_lists;
14941 parser->num_template_parameter_lists = 0;
14942 /* We are not in a function body. */
14943 saved_in_function_body = parser->in_function_body;
14944 parser->in_function_body = false;
14945 /* We are not immediately inside an extern "lang" block. */
14946 saved_in_unbraced_linkage_specification_p
14947 = parser->in_unbraced_linkage_specification_p;
14948 parser->in_unbraced_linkage_specification_p = false;
14949
14950 /* Start the class. */
14951 if (nested_name_specifier_p)
14952 {
14953 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14954 old_scope = push_inner_scope (scope);
14955 }
14956 type = begin_class_definition (type, attributes);
14957
14958 if (type == error_mark_node)
14959 /* If the type is erroneous, skip the entire body of the class. */
14960 cp_parser_skip_to_closing_brace (parser);
14961 else
14962 /* Parse the member-specification. */
14963 cp_parser_member_specification_opt (parser);
14964
14965 /* Look for the trailing `}'. */
14966 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14967 /* We get better error messages by noticing a common problem: a
14968 missing trailing `;'. */
14969 token = cp_lexer_peek_token (parser->lexer);
14970 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14971 /* Look for trailing attributes to apply to this class. */
14972 if (cp_parser_allow_gnu_extensions_p (parser))
14973 attributes = cp_parser_attributes_opt (parser);
14974 if (type != error_mark_node)
14975 type = finish_struct (type, attributes);
14976 if (nested_name_specifier_p)
14977 pop_inner_scope (old_scope, scope);
14978 /* If this class is not itself within the scope of another class,
14979 then we need to parse the bodies of all of the queued function
14980 definitions. Note that the queued functions defined in a class
14981 are not always processed immediately following the
14982 class-specifier for that class. Consider:
14983
14984 struct A {
14985 struct B { void f() { sizeof (A); } };
14986 };
14987
14988 If `f' were processed before the processing of `A' were
14989 completed, there would be no way to compute the size of `A'.
14990 Note that the nesting we are interested in here is lexical --
14991 not the semantic nesting given by TYPE_CONTEXT. In particular,
14992 for:
14993
14994 struct A { struct B; };
14995 struct A::B { void f() { } };
14996
14997 there is no need to delay the parsing of `A::B::f'. */
14998 if (--parser->num_classes_being_defined == 0)
14999 {
15000 tree queue_entry;
15001 tree fn;
15002 tree class_type = NULL_TREE;
15003 tree pushed_scope = NULL_TREE;
15004
15005 /* In a first pass, parse default arguments to the functions.
15006 Then, in a second pass, parse the bodies of the functions.
15007 This two-phased approach handles cases like:
15008
15009 struct S {
15010 void f() { g(); }
15011 void g(int i = 3);
15012 };
15013
15014 */
15015 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15016 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15017 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15018 TREE_PURPOSE (parser->unparsed_functions_queues)
15019 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15020 {
15021 fn = TREE_VALUE (queue_entry);
15022 /* If there are default arguments that have not yet been processed,
15023 take care of them now. */
15024 if (class_type != TREE_PURPOSE (queue_entry))
15025 {
15026 if (pushed_scope)
15027 pop_scope (pushed_scope);
15028 class_type = TREE_PURPOSE (queue_entry);
15029 pushed_scope = push_scope (class_type);
15030 }
15031 /* Make sure that any template parameters are in scope. */
15032 maybe_begin_member_template_processing (fn);
15033 /* Parse the default argument expressions. */
15034 cp_parser_late_parsing_default_args (parser, fn);
15035 /* Remove any template parameters from the symbol table. */
15036 maybe_end_member_template_processing ();
15037 }
15038 if (pushed_scope)
15039 pop_scope (pushed_scope);
15040 /* Now parse the body of the functions. */
15041 for (TREE_VALUE (parser->unparsed_functions_queues)
15042 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15043 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15044 TREE_VALUE (parser->unparsed_functions_queues)
15045 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15046 {
15047 /* Figure out which function we need to process. */
15048 fn = TREE_VALUE (queue_entry);
15049 /* Parse the function. */
15050 cp_parser_late_parsing_for_member (parser, fn);
15051 }
15052 }
15053
15054 /* Put back any saved access checks. */
15055 pop_deferring_access_checks ();
15056
15057 /* Restore saved state. */
15058 parser->in_function_body = saved_in_function_body;
15059 parser->num_template_parameter_lists
15060 = saved_num_template_parameter_lists;
15061 parser->in_unbraced_linkage_specification_p
15062 = saved_in_unbraced_linkage_specification_p;
15063
15064 return type;
15065 }
15066
15067 /* Parse a class-head.
15068
15069 class-head:
15070 class-key identifier [opt] base-clause [opt]
15071 class-key nested-name-specifier identifier base-clause [opt]
15072 class-key nested-name-specifier [opt] template-id
15073 base-clause [opt]
15074
15075 GNU Extensions:
15076 class-key attributes identifier [opt] base-clause [opt]
15077 class-key attributes nested-name-specifier identifier base-clause [opt]
15078 class-key attributes nested-name-specifier [opt] template-id
15079 base-clause [opt]
15080
15081 Upon return BASES is initialized to the list of base classes (or
15082 NULL, if there are none) in the same form returned by
15083 cp_parser_base_clause.
15084
15085 Returns the TYPE of the indicated class. Sets
15086 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15087 involving a nested-name-specifier was used, and FALSE otherwise.
15088
15089 Returns error_mark_node if this is not a class-head.
15090
15091 Returns NULL_TREE if the class-head is syntactically valid, but
15092 semantically invalid in a way that means we should skip the entire
15093 body of the class. */
15094
15095 static tree
15096 cp_parser_class_head (cp_parser* parser,
15097 bool* nested_name_specifier_p,
15098 tree *attributes_p,
15099 tree *bases)
15100 {
15101 tree nested_name_specifier;
15102 enum tag_types class_key;
15103 tree id = NULL_TREE;
15104 tree type = NULL_TREE;
15105 tree attributes;
15106 bool template_id_p = false;
15107 bool qualified_p = false;
15108 bool invalid_nested_name_p = false;
15109 bool invalid_explicit_specialization_p = false;
15110 tree pushed_scope = NULL_TREE;
15111 unsigned num_templates;
15112 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15113 /* Assume no nested-name-specifier will be present. */
15114 *nested_name_specifier_p = false;
15115 /* Assume no template parameter lists will be used in defining the
15116 type. */
15117 num_templates = 0;
15118
15119 *bases = NULL_TREE;
15120
15121 /* Look for the class-key. */
15122 class_key = cp_parser_class_key (parser);
15123 if (class_key == none_type)
15124 return error_mark_node;
15125
15126 /* Parse the attributes. */
15127 attributes = cp_parser_attributes_opt (parser);
15128
15129 /* If the next token is `::', that is invalid -- but sometimes
15130 people do try to write:
15131
15132 struct ::S {};
15133
15134 Handle this gracefully by accepting the extra qualifier, and then
15135 issuing an error about it later if this really is a
15136 class-head. If it turns out just to be an elaborated type
15137 specifier, remain silent. */
15138 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15139 qualified_p = true;
15140
15141 push_deferring_access_checks (dk_no_check);
15142
15143 /* Determine the name of the class. Begin by looking for an
15144 optional nested-name-specifier. */
15145 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15146 nested_name_specifier
15147 = cp_parser_nested_name_specifier_opt (parser,
15148 /*typename_keyword_p=*/false,
15149 /*check_dependency_p=*/false,
15150 /*type_p=*/false,
15151 /*is_declaration=*/false);
15152 /* If there was a nested-name-specifier, then there *must* be an
15153 identifier. */
15154 if (nested_name_specifier)
15155 {
15156 type_start_token = cp_lexer_peek_token (parser->lexer);
15157 /* Although the grammar says `identifier', it really means
15158 `class-name' or `template-name'. You are only allowed to
15159 define a class that has already been declared with this
15160 syntax.
15161
15162 The proposed resolution for Core Issue 180 says that wherever
15163 you see `class T::X' you should treat `X' as a type-name.
15164
15165 It is OK to define an inaccessible class; for example:
15166
15167 class A { class B; };
15168 class A::B {};
15169
15170 We do not know if we will see a class-name, or a
15171 template-name. We look for a class-name first, in case the
15172 class-name is a template-id; if we looked for the
15173 template-name first we would stop after the template-name. */
15174 cp_parser_parse_tentatively (parser);
15175 type = cp_parser_class_name (parser,
15176 /*typename_keyword_p=*/false,
15177 /*template_keyword_p=*/false,
15178 class_type,
15179 /*check_dependency_p=*/false,
15180 /*class_head_p=*/true,
15181 /*is_declaration=*/false);
15182 /* If that didn't work, ignore the nested-name-specifier. */
15183 if (!cp_parser_parse_definitely (parser))
15184 {
15185 invalid_nested_name_p = true;
15186 type_start_token = cp_lexer_peek_token (parser->lexer);
15187 id = cp_parser_identifier (parser);
15188 if (id == error_mark_node)
15189 id = NULL_TREE;
15190 }
15191 /* If we could not find a corresponding TYPE, treat this
15192 declaration like an unqualified declaration. */
15193 if (type == error_mark_node)
15194 nested_name_specifier = NULL_TREE;
15195 /* Otherwise, count the number of templates used in TYPE and its
15196 containing scopes. */
15197 else
15198 {
15199 tree scope;
15200
15201 for (scope = TREE_TYPE (type);
15202 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15203 scope = (TYPE_P (scope)
15204 ? TYPE_CONTEXT (scope)
15205 : DECL_CONTEXT (scope)))
15206 if (TYPE_P (scope)
15207 && CLASS_TYPE_P (scope)
15208 && CLASSTYPE_TEMPLATE_INFO (scope)
15209 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15210 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15211 ++num_templates;
15212 }
15213 }
15214 /* Otherwise, the identifier is optional. */
15215 else
15216 {
15217 /* We don't know whether what comes next is a template-id,
15218 an identifier, or nothing at all. */
15219 cp_parser_parse_tentatively (parser);
15220 /* Check for a template-id. */
15221 type_start_token = cp_lexer_peek_token (parser->lexer);
15222 id = cp_parser_template_id (parser,
15223 /*template_keyword_p=*/false,
15224 /*check_dependency_p=*/true,
15225 /*is_declaration=*/true);
15226 /* If that didn't work, it could still be an identifier. */
15227 if (!cp_parser_parse_definitely (parser))
15228 {
15229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15230 {
15231 type_start_token = cp_lexer_peek_token (parser->lexer);
15232 id = cp_parser_identifier (parser);
15233 }
15234 else
15235 id = NULL_TREE;
15236 }
15237 else
15238 {
15239 template_id_p = true;
15240 ++num_templates;
15241 }
15242 }
15243
15244 pop_deferring_access_checks ();
15245
15246 if (id)
15247 cp_parser_check_for_invalid_template_id (parser, id,
15248 type_start_token->location);
15249
15250 /* If it's not a `:' or a `{' then we can't really be looking at a
15251 class-head, since a class-head only appears as part of a
15252 class-specifier. We have to detect this situation before calling
15253 xref_tag, since that has irreversible side-effects. */
15254 if (!cp_parser_next_token_starts_class_definition_p (parser))
15255 {
15256 cp_parser_error (parser, "expected %<{%> or %<:%>");
15257 return error_mark_node;
15258 }
15259
15260 /* At this point, we're going ahead with the class-specifier, even
15261 if some other problem occurs. */
15262 cp_parser_commit_to_tentative_parse (parser);
15263 /* Issue the error about the overly-qualified name now. */
15264 if (qualified_p)
15265 {
15266 cp_parser_error (parser,
15267 "global qualification of class name is invalid");
15268 return error_mark_node;
15269 }
15270 else if (invalid_nested_name_p)
15271 {
15272 cp_parser_error (parser,
15273 "qualified name does not name a class");
15274 return error_mark_node;
15275 }
15276 else if (nested_name_specifier)
15277 {
15278 tree scope;
15279
15280 /* Reject typedef-names in class heads. */
15281 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15282 {
15283 error ("%Hinvalid class name in declaration of %qD",
15284 &type_start_token->location, type);
15285 type = NULL_TREE;
15286 goto done;
15287 }
15288
15289 /* Figure out in what scope the declaration is being placed. */
15290 scope = current_scope ();
15291 /* If that scope does not contain the scope in which the
15292 class was originally declared, the program is invalid. */
15293 if (scope && !is_ancestor (scope, nested_name_specifier))
15294 {
15295 if (at_namespace_scope_p ())
15296 error ("%Hdeclaration of %qD in namespace %qD which does not "
15297 "enclose %qD",
15298 &type_start_token->location,
15299 type, scope, nested_name_specifier);
15300 else
15301 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15302 &type_start_token->location,
15303 type, scope, nested_name_specifier);
15304 type = NULL_TREE;
15305 goto done;
15306 }
15307 /* [dcl.meaning]
15308
15309 A declarator-id shall not be qualified except for the
15310 definition of a ... nested class outside of its class
15311 ... [or] the definition or explicit instantiation of a
15312 class member of a namespace outside of its namespace. */
15313 if (scope == nested_name_specifier)
15314 {
15315 permerror (input_location, "%Hextra qualification not allowed",
15316 &nested_name_specifier_token_start->location);
15317 nested_name_specifier = NULL_TREE;
15318 num_templates = 0;
15319 }
15320 }
15321 /* An explicit-specialization must be preceded by "template <>". If
15322 it is not, try to recover gracefully. */
15323 if (at_namespace_scope_p ()
15324 && parser->num_template_parameter_lists == 0
15325 && template_id_p)
15326 {
15327 error ("%Han explicit specialization must be preceded by %<template <>%>",
15328 &type_start_token->location);
15329 invalid_explicit_specialization_p = true;
15330 /* Take the same action that would have been taken by
15331 cp_parser_explicit_specialization. */
15332 ++parser->num_template_parameter_lists;
15333 begin_specialization ();
15334 }
15335 /* There must be no "return" statements between this point and the
15336 end of this function; set "type "to the correct return value and
15337 use "goto done;" to return. */
15338 /* Make sure that the right number of template parameters were
15339 present. */
15340 if (!cp_parser_check_template_parameters (parser, num_templates,
15341 type_start_token->location))
15342 {
15343 /* If something went wrong, there is no point in even trying to
15344 process the class-definition. */
15345 type = NULL_TREE;
15346 goto done;
15347 }
15348
15349 /* Look up the type. */
15350 if (template_id_p)
15351 {
15352 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15353 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15354 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15355 {
15356 error ("%Hfunction template %qD redeclared as a class template",
15357 &type_start_token->location, id);
15358 type = error_mark_node;
15359 }
15360 else
15361 {
15362 type = TREE_TYPE (id);
15363 type = maybe_process_partial_specialization (type);
15364 }
15365 if (nested_name_specifier)
15366 pushed_scope = push_scope (nested_name_specifier);
15367 }
15368 else if (nested_name_specifier)
15369 {
15370 tree class_type;
15371
15372 /* Given:
15373
15374 template <typename T> struct S { struct T };
15375 template <typename T> struct S<T>::T { };
15376
15377 we will get a TYPENAME_TYPE when processing the definition of
15378 `S::T'. We need to resolve it to the actual type before we
15379 try to define it. */
15380 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15381 {
15382 class_type = resolve_typename_type (TREE_TYPE (type),
15383 /*only_current_p=*/false);
15384 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15385 type = TYPE_NAME (class_type);
15386 else
15387 {
15388 cp_parser_error (parser, "could not resolve typename type");
15389 type = error_mark_node;
15390 }
15391 }
15392
15393 if (maybe_process_partial_specialization (TREE_TYPE (type))
15394 == error_mark_node)
15395 {
15396 type = NULL_TREE;
15397 goto done;
15398 }
15399
15400 class_type = current_class_type;
15401 /* Enter the scope indicated by the nested-name-specifier. */
15402 pushed_scope = push_scope (nested_name_specifier);
15403 /* Get the canonical version of this type. */
15404 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15405 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15406 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15407 {
15408 type = push_template_decl (type);
15409 if (type == error_mark_node)
15410 {
15411 type = NULL_TREE;
15412 goto done;
15413 }
15414 }
15415
15416 type = TREE_TYPE (type);
15417 *nested_name_specifier_p = true;
15418 }
15419 else /* The name is not a nested name. */
15420 {
15421 /* If the class was unnamed, create a dummy name. */
15422 if (!id)
15423 id = make_anon_name ();
15424 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15425 parser->num_template_parameter_lists);
15426 }
15427
15428 /* Indicate whether this class was declared as a `class' or as a
15429 `struct'. */
15430 if (TREE_CODE (type) == RECORD_TYPE)
15431 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15432 cp_parser_check_class_key (class_key, type);
15433
15434 /* If this type was already complete, and we see another definition,
15435 that's an error. */
15436 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15437 {
15438 error ("%Hredefinition of %q#T",
15439 &type_start_token->location, type);
15440 error ("%Hprevious definition of %q+#T",
15441 &type_start_token->location, type);
15442 type = NULL_TREE;
15443 goto done;
15444 }
15445 else if (type == error_mark_node)
15446 type = NULL_TREE;
15447
15448 /* We will have entered the scope containing the class; the names of
15449 base classes should be looked up in that context. For example:
15450
15451 struct A { struct B {}; struct C; };
15452 struct A::C : B {};
15453
15454 is valid. */
15455
15456 /* Get the list of base-classes, if there is one. */
15457 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15458 *bases = cp_parser_base_clause (parser);
15459
15460 done:
15461 /* Leave the scope given by the nested-name-specifier. We will
15462 enter the class scope itself while processing the members. */
15463 if (pushed_scope)
15464 pop_scope (pushed_scope);
15465
15466 if (invalid_explicit_specialization_p)
15467 {
15468 end_specialization ();
15469 --parser->num_template_parameter_lists;
15470 }
15471 *attributes_p = attributes;
15472 return type;
15473 }
15474
15475 /* Parse a class-key.
15476
15477 class-key:
15478 class
15479 struct
15480 union
15481
15482 Returns the kind of class-key specified, or none_type to indicate
15483 error. */
15484
15485 static enum tag_types
15486 cp_parser_class_key (cp_parser* parser)
15487 {
15488 cp_token *token;
15489 enum tag_types tag_type;
15490
15491 /* Look for the class-key. */
15492 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15493 if (!token)
15494 return none_type;
15495
15496 /* Check to see if the TOKEN is a class-key. */
15497 tag_type = cp_parser_token_is_class_key (token);
15498 if (!tag_type)
15499 cp_parser_error (parser, "expected class-key");
15500 return tag_type;
15501 }
15502
15503 /* Parse an (optional) member-specification.
15504
15505 member-specification:
15506 member-declaration member-specification [opt]
15507 access-specifier : member-specification [opt] */
15508
15509 static void
15510 cp_parser_member_specification_opt (cp_parser* parser)
15511 {
15512 while (true)
15513 {
15514 cp_token *token;
15515 enum rid keyword;
15516
15517 /* Peek at the next token. */
15518 token = cp_lexer_peek_token (parser->lexer);
15519 /* If it's a `}', or EOF then we've seen all the members. */
15520 if (token->type == CPP_CLOSE_BRACE
15521 || token->type == CPP_EOF
15522 || token->type == CPP_PRAGMA_EOL)
15523 break;
15524
15525 /* See if this token is a keyword. */
15526 keyword = token->keyword;
15527 switch (keyword)
15528 {
15529 case RID_PUBLIC:
15530 case RID_PROTECTED:
15531 case RID_PRIVATE:
15532 /* Consume the access-specifier. */
15533 cp_lexer_consume_token (parser->lexer);
15534 /* Remember which access-specifier is active. */
15535 current_access_specifier = token->u.value;
15536 /* Look for the `:'. */
15537 cp_parser_require (parser, CPP_COLON, "%<:%>");
15538 break;
15539
15540 default:
15541 /* Accept #pragmas at class scope. */
15542 if (token->type == CPP_PRAGMA)
15543 {
15544 cp_parser_pragma (parser, pragma_external);
15545 break;
15546 }
15547
15548 /* Otherwise, the next construction must be a
15549 member-declaration. */
15550 cp_parser_member_declaration (parser);
15551 }
15552 }
15553 }
15554
15555 /* Parse a member-declaration.
15556
15557 member-declaration:
15558 decl-specifier-seq [opt] member-declarator-list [opt] ;
15559 function-definition ; [opt]
15560 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15561 using-declaration
15562 template-declaration
15563
15564 member-declarator-list:
15565 member-declarator
15566 member-declarator-list , member-declarator
15567
15568 member-declarator:
15569 declarator pure-specifier [opt]
15570 declarator constant-initializer [opt]
15571 identifier [opt] : constant-expression
15572
15573 GNU Extensions:
15574
15575 member-declaration:
15576 __extension__ member-declaration
15577
15578 member-declarator:
15579 declarator attributes [opt] pure-specifier [opt]
15580 declarator attributes [opt] constant-initializer [opt]
15581 identifier [opt] attributes [opt] : constant-expression
15582
15583 C++0x Extensions:
15584
15585 member-declaration:
15586 static_assert-declaration */
15587
15588 static void
15589 cp_parser_member_declaration (cp_parser* parser)
15590 {
15591 cp_decl_specifier_seq decl_specifiers;
15592 tree prefix_attributes;
15593 tree decl;
15594 int declares_class_or_enum;
15595 bool friend_p;
15596 cp_token *token = NULL;
15597 cp_token *decl_spec_token_start = NULL;
15598 cp_token *initializer_token_start = NULL;
15599 int saved_pedantic;
15600
15601 /* Check for the `__extension__' keyword. */
15602 if (cp_parser_extension_opt (parser, &saved_pedantic))
15603 {
15604 /* Recurse. */
15605 cp_parser_member_declaration (parser);
15606 /* Restore the old value of the PEDANTIC flag. */
15607 pedantic = saved_pedantic;
15608
15609 return;
15610 }
15611
15612 /* Check for a template-declaration. */
15613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15614 {
15615 /* An explicit specialization here is an error condition, and we
15616 expect the specialization handler to detect and report this. */
15617 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15618 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15619 cp_parser_explicit_specialization (parser);
15620 else
15621 cp_parser_template_declaration (parser, /*member_p=*/true);
15622
15623 return;
15624 }
15625
15626 /* Check for a using-declaration. */
15627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15628 {
15629 /* Parse the using-declaration. */
15630 cp_parser_using_declaration (parser,
15631 /*access_declaration_p=*/false);
15632 return;
15633 }
15634
15635 /* Check for @defs. */
15636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15637 {
15638 tree ivar, member;
15639 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15640 ivar = ivar_chains;
15641 while (ivar)
15642 {
15643 member = ivar;
15644 ivar = TREE_CHAIN (member);
15645 TREE_CHAIN (member) = NULL_TREE;
15646 finish_member_declaration (member);
15647 }
15648 return;
15649 }
15650
15651 /* If the next token is `static_assert' we have a static assertion. */
15652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15653 {
15654 cp_parser_static_assert (parser, /*member_p=*/true);
15655 return;
15656 }
15657
15658 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15659 return;
15660
15661 /* Parse the decl-specifier-seq. */
15662 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15663 cp_parser_decl_specifier_seq (parser,
15664 CP_PARSER_FLAGS_OPTIONAL,
15665 &decl_specifiers,
15666 &declares_class_or_enum);
15667 prefix_attributes = decl_specifiers.attributes;
15668 decl_specifiers.attributes = NULL_TREE;
15669 /* Check for an invalid type-name. */
15670 if (!decl_specifiers.type
15671 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15672 return;
15673 /* If there is no declarator, then the decl-specifier-seq should
15674 specify a type. */
15675 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15676 {
15677 /* If there was no decl-specifier-seq, and the next token is a
15678 `;', then we have something like:
15679
15680 struct S { ; };
15681
15682 [class.mem]
15683
15684 Each member-declaration shall declare at least one member
15685 name of the class. */
15686 if (!decl_specifiers.any_specifiers_p)
15687 {
15688 cp_token *token = cp_lexer_peek_token (parser->lexer);
15689 if (!in_system_header_at (token->location))
15690 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15691 }
15692 else
15693 {
15694 tree type;
15695
15696 /* See if this declaration is a friend. */
15697 friend_p = cp_parser_friend_p (&decl_specifiers);
15698 /* If there were decl-specifiers, check to see if there was
15699 a class-declaration. */
15700 type = check_tag_decl (&decl_specifiers);
15701 /* Nested classes have already been added to the class, but
15702 a `friend' needs to be explicitly registered. */
15703 if (friend_p)
15704 {
15705 /* If the `friend' keyword was present, the friend must
15706 be introduced with a class-key. */
15707 if (!declares_class_or_enum)
15708 error ("%Ha class-key must be used when declaring a friend",
15709 &decl_spec_token_start->location);
15710 /* In this case:
15711
15712 template <typename T> struct A {
15713 friend struct A<T>::B;
15714 };
15715
15716 A<T>::B will be represented by a TYPENAME_TYPE, and
15717 therefore not recognized by check_tag_decl. */
15718 if (!type
15719 && decl_specifiers.type
15720 && TYPE_P (decl_specifiers.type))
15721 type = decl_specifiers.type;
15722 if (!type || !TYPE_P (type))
15723 error ("%Hfriend declaration does not name a class or "
15724 "function", &decl_spec_token_start->location);
15725 else
15726 make_friend_class (current_class_type, type,
15727 /*complain=*/true);
15728 }
15729 /* If there is no TYPE, an error message will already have
15730 been issued. */
15731 else if (!type || type == error_mark_node)
15732 ;
15733 /* An anonymous aggregate has to be handled specially; such
15734 a declaration really declares a data member (with a
15735 particular type), as opposed to a nested class. */
15736 else if (ANON_AGGR_TYPE_P (type))
15737 {
15738 /* Remove constructors and such from TYPE, now that we
15739 know it is an anonymous aggregate. */
15740 fixup_anonymous_aggr (type);
15741 /* And make the corresponding data member. */
15742 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15743 /* Add it to the class. */
15744 finish_member_declaration (decl);
15745 }
15746 else
15747 cp_parser_check_access_in_redeclaration
15748 (TYPE_NAME (type),
15749 decl_spec_token_start->location);
15750 }
15751 }
15752 else
15753 {
15754 /* See if these declarations will be friends. */
15755 friend_p = cp_parser_friend_p (&decl_specifiers);
15756
15757 /* Keep going until we hit the `;' at the end of the
15758 declaration. */
15759 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15760 {
15761 tree attributes = NULL_TREE;
15762 tree first_attribute;
15763
15764 /* Peek at the next token. */
15765 token = cp_lexer_peek_token (parser->lexer);
15766
15767 /* Check for a bitfield declaration. */
15768 if (token->type == CPP_COLON
15769 || (token->type == CPP_NAME
15770 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15771 == CPP_COLON))
15772 {
15773 tree identifier;
15774 tree width;
15775
15776 /* Get the name of the bitfield. Note that we cannot just
15777 check TOKEN here because it may have been invalidated by
15778 the call to cp_lexer_peek_nth_token above. */
15779 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15780 identifier = cp_parser_identifier (parser);
15781 else
15782 identifier = NULL_TREE;
15783
15784 /* Consume the `:' token. */
15785 cp_lexer_consume_token (parser->lexer);
15786 /* Get the width of the bitfield. */
15787 width
15788 = cp_parser_constant_expression (parser,
15789 /*allow_non_constant=*/false,
15790 NULL);
15791
15792 /* Look for attributes that apply to the bitfield. */
15793 attributes = cp_parser_attributes_opt (parser);
15794 /* Remember which attributes are prefix attributes and
15795 which are not. */
15796 first_attribute = attributes;
15797 /* Combine the attributes. */
15798 attributes = chainon (prefix_attributes, attributes);
15799
15800 /* Create the bitfield declaration. */
15801 decl = grokbitfield (identifier
15802 ? make_id_declarator (NULL_TREE,
15803 identifier,
15804 sfk_none)
15805 : NULL,
15806 &decl_specifiers,
15807 width,
15808 attributes);
15809 }
15810 else
15811 {
15812 cp_declarator *declarator;
15813 tree initializer;
15814 tree asm_specification;
15815 int ctor_dtor_or_conv_p;
15816
15817 /* Parse the declarator. */
15818 declarator
15819 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15820 &ctor_dtor_or_conv_p,
15821 /*parenthesized_p=*/NULL,
15822 /*member_p=*/true);
15823
15824 /* If something went wrong parsing the declarator, make sure
15825 that we at least consume some tokens. */
15826 if (declarator == cp_error_declarator)
15827 {
15828 /* Skip to the end of the statement. */
15829 cp_parser_skip_to_end_of_statement (parser);
15830 /* If the next token is not a semicolon, that is
15831 probably because we just skipped over the body of
15832 a function. So, we consume a semicolon if
15833 present, but do not issue an error message if it
15834 is not present. */
15835 if (cp_lexer_next_token_is (parser->lexer,
15836 CPP_SEMICOLON))
15837 cp_lexer_consume_token (parser->lexer);
15838 return;
15839 }
15840
15841 if (declares_class_or_enum & 2)
15842 cp_parser_check_for_definition_in_return_type
15843 (declarator, decl_specifiers.type,
15844 decl_specifiers.type_location);
15845
15846 /* Look for an asm-specification. */
15847 asm_specification = cp_parser_asm_specification_opt (parser);
15848 /* Look for attributes that apply to the declaration. */
15849 attributes = cp_parser_attributes_opt (parser);
15850 /* Remember which attributes are prefix attributes and
15851 which are not. */
15852 first_attribute = attributes;
15853 /* Combine the attributes. */
15854 attributes = chainon (prefix_attributes, attributes);
15855
15856 /* If it's an `=', then we have a constant-initializer or a
15857 pure-specifier. It is not correct to parse the
15858 initializer before registering the member declaration
15859 since the member declaration should be in scope while
15860 its initializer is processed. However, the rest of the
15861 front end does not yet provide an interface that allows
15862 us to handle this correctly. */
15863 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15864 {
15865 /* In [class.mem]:
15866
15867 A pure-specifier shall be used only in the declaration of
15868 a virtual function.
15869
15870 A member-declarator can contain a constant-initializer
15871 only if it declares a static member of integral or
15872 enumeration type.
15873
15874 Therefore, if the DECLARATOR is for a function, we look
15875 for a pure-specifier; otherwise, we look for a
15876 constant-initializer. When we call `grokfield', it will
15877 perform more stringent semantics checks. */
15878 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15879 if (function_declarator_p (declarator))
15880 initializer = cp_parser_pure_specifier (parser);
15881 else
15882 /* Parse the initializer. */
15883 initializer = cp_parser_constant_initializer (parser);
15884 }
15885 /* Otherwise, there is no initializer. */
15886 else
15887 initializer = NULL_TREE;
15888
15889 /* See if we are probably looking at a function
15890 definition. We are certainly not looking at a
15891 member-declarator. Calling `grokfield' has
15892 side-effects, so we must not do it unless we are sure
15893 that we are looking at a member-declarator. */
15894 if (cp_parser_token_starts_function_definition_p
15895 (cp_lexer_peek_token (parser->lexer)))
15896 {
15897 /* The grammar does not allow a pure-specifier to be
15898 used when a member function is defined. (It is
15899 possible that this fact is an oversight in the
15900 standard, since a pure function may be defined
15901 outside of the class-specifier. */
15902 if (initializer)
15903 error ("%Hpure-specifier on function-definition",
15904 &initializer_token_start->location);
15905 decl = cp_parser_save_member_function_body (parser,
15906 &decl_specifiers,
15907 declarator,
15908 attributes);
15909 /* If the member was not a friend, declare it here. */
15910 if (!friend_p)
15911 finish_member_declaration (decl);
15912 /* Peek at the next token. */
15913 token = cp_lexer_peek_token (parser->lexer);
15914 /* If the next token is a semicolon, consume it. */
15915 if (token->type == CPP_SEMICOLON)
15916 cp_lexer_consume_token (parser->lexer);
15917 return;
15918 }
15919 else
15920 if (declarator->kind == cdk_function)
15921 declarator->id_loc = token->location;
15922 /* Create the declaration. */
15923 decl = grokfield (declarator, &decl_specifiers,
15924 initializer, /*init_const_expr_p=*/true,
15925 asm_specification,
15926 attributes);
15927 }
15928
15929 /* Reset PREFIX_ATTRIBUTES. */
15930 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15931 attributes = TREE_CHAIN (attributes);
15932 if (attributes)
15933 TREE_CHAIN (attributes) = NULL_TREE;
15934
15935 /* If there is any qualification still in effect, clear it
15936 now; we will be starting fresh with the next declarator. */
15937 parser->scope = NULL_TREE;
15938 parser->qualifying_scope = NULL_TREE;
15939 parser->object_scope = NULL_TREE;
15940 /* If it's a `,', then there are more declarators. */
15941 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15942 cp_lexer_consume_token (parser->lexer);
15943 /* If the next token isn't a `;', then we have a parse error. */
15944 else if (cp_lexer_next_token_is_not (parser->lexer,
15945 CPP_SEMICOLON))
15946 {
15947 cp_parser_error (parser, "expected %<;%>");
15948 /* Skip tokens until we find a `;'. */
15949 cp_parser_skip_to_end_of_statement (parser);
15950
15951 break;
15952 }
15953
15954 if (decl)
15955 {
15956 /* Add DECL to the list of members. */
15957 if (!friend_p)
15958 finish_member_declaration (decl);
15959
15960 if (TREE_CODE (decl) == FUNCTION_DECL)
15961 cp_parser_save_default_args (parser, decl);
15962 }
15963 }
15964 }
15965
15966 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15967 }
15968
15969 /* Parse a pure-specifier.
15970
15971 pure-specifier:
15972 = 0
15973
15974 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15975 Otherwise, ERROR_MARK_NODE is returned. */
15976
15977 static tree
15978 cp_parser_pure_specifier (cp_parser* parser)
15979 {
15980 cp_token *token;
15981
15982 /* Look for the `=' token. */
15983 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15984 return error_mark_node;
15985 /* Look for the `0' token. */
15986 token = cp_lexer_consume_token (parser->lexer);
15987
15988 /* Accept = default or = delete in c++0x mode. */
15989 if (token->keyword == RID_DEFAULT
15990 || token->keyword == RID_DELETE)
15991 {
15992 maybe_warn_cpp0x ("defaulted and deleted functions");
15993 return token->u.value;
15994 }
15995
15996 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15997 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15998 {
15999 cp_parser_error (parser,
16000 "invalid pure specifier (only %<= 0%> is allowed)");
16001 cp_parser_skip_to_end_of_statement (parser);
16002 return error_mark_node;
16003 }
16004 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16005 {
16006 error ("%Htemplates may not be %<virtual%>", &token->location);
16007 return error_mark_node;
16008 }
16009
16010 return integer_zero_node;
16011 }
16012
16013 /* Parse a constant-initializer.
16014
16015 constant-initializer:
16016 = constant-expression
16017
16018 Returns a representation of the constant-expression. */
16019
16020 static tree
16021 cp_parser_constant_initializer (cp_parser* parser)
16022 {
16023 /* Look for the `=' token. */
16024 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16025 return error_mark_node;
16026
16027 /* It is invalid to write:
16028
16029 struct S { static const int i = { 7 }; };
16030
16031 */
16032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16033 {
16034 cp_parser_error (parser,
16035 "a brace-enclosed initializer is not allowed here");
16036 /* Consume the opening brace. */
16037 cp_lexer_consume_token (parser->lexer);
16038 /* Skip the initializer. */
16039 cp_parser_skip_to_closing_brace (parser);
16040 /* Look for the trailing `}'. */
16041 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16042
16043 return error_mark_node;
16044 }
16045
16046 return cp_parser_constant_expression (parser,
16047 /*allow_non_constant=*/false,
16048 NULL);
16049 }
16050
16051 /* Derived classes [gram.class.derived] */
16052
16053 /* Parse a base-clause.
16054
16055 base-clause:
16056 : base-specifier-list
16057
16058 base-specifier-list:
16059 base-specifier ... [opt]
16060 base-specifier-list , base-specifier ... [opt]
16061
16062 Returns a TREE_LIST representing the base-classes, in the order in
16063 which they were declared. The representation of each node is as
16064 described by cp_parser_base_specifier.
16065
16066 In the case that no bases are specified, this function will return
16067 NULL_TREE, not ERROR_MARK_NODE. */
16068
16069 static tree
16070 cp_parser_base_clause (cp_parser* parser)
16071 {
16072 tree bases = NULL_TREE;
16073
16074 /* Look for the `:' that begins the list. */
16075 cp_parser_require (parser, CPP_COLON, "%<:%>");
16076
16077 /* Scan the base-specifier-list. */
16078 while (true)
16079 {
16080 cp_token *token;
16081 tree base;
16082 bool pack_expansion_p = false;
16083
16084 /* Look for the base-specifier. */
16085 base = cp_parser_base_specifier (parser);
16086 /* Look for the (optional) ellipsis. */
16087 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16088 {
16089 /* Consume the `...'. */
16090 cp_lexer_consume_token (parser->lexer);
16091
16092 pack_expansion_p = true;
16093 }
16094
16095 /* Add BASE to the front of the list. */
16096 if (base != error_mark_node)
16097 {
16098 if (pack_expansion_p)
16099 /* Make this a pack expansion type. */
16100 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16101
16102
16103 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16104 {
16105 TREE_CHAIN (base) = bases;
16106 bases = base;
16107 }
16108 }
16109 /* Peek at the next token. */
16110 token = cp_lexer_peek_token (parser->lexer);
16111 /* If it's not a comma, then the list is complete. */
16112 if (token->type != CPP_COMMA)
16113 break;
16114 /* Consume the `,'. */
16115 cp_lexer_consume_token (parser->lexer);
16116 }
16117
16118 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16119 base class had a qualified name. However, the next name that
16120 appears is certainly not qualified. */
16121 parser->scope = NULL_TREE;
16122 parser->qualifying_scope = NULL_TREE;
16123 parser->object_scope = NULL_TREE;
16124
16125 return nreverse (bases);
16126 }
16127
16128 /* Parse a base-specifier.
16129
16130 base-specifier:
16131 :: [opt] nested-name-specifier [opt] class-name
16132 virtual access-specifier [opt] :: [opt] nested-name-specifier
16133 [opt] class-name
16134 access-specifier virtual [opt] :: [opt] nested-name-specifier
16135 [opt] class-name
16136
16137 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16138 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16139 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16140 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16141
16142 static tree
16143 cp_parser_base_specifier (cp_parser* parser)
16144 {
16145 cp_token *token;
16146 bool done = false;
16147 bool virtual_p = false;
16148 bool duplicate_virtual_error_issued_p = false;
16149 bool duplicate_access_error_issued_p = false;
16150 bool class_scope_p, template_p;
16151 tree access = access_default_node;
16152 tree type;
16153
16154 /* Process the optional `virtual' and `access-specifier'. */
16155 while (!done)
16156 {
16157 /* Peek at the next token. */
16158 token = cp_lexer_peek_token (parser->lexer);
16159 /* Process `virtual'. */
16160 switch (token->keyword)
16161 {
16162 case RID_VIRTUAL:
16163 /* If `virtual' appears more than once, issue an error. */
16164 if (virtual_p && !duplicate_virtual_error_issued_p)
16165 {
16166 cp_parser_error (parser,
16167 "%<virtual%> specified more than once in base-specified");
16168 duplicate_virtual_error_issued_p = true;
16169 }
16170
16171 virtual_p = true;
16172
16173 /* Consume the `virtual' token. */
16174 cp_lexer_consume_token (parser->lexer);
16175
16176 break;
16177
16178 case RID_PUBLIC:
16179 case RID_PROTECTED:
16180 case RID_PRIVATE:
16181 /* If more than one access specifier appears, issue an
16182 error. */
16183 if (access != access_default_node
16184 && !duplicate_access_error_issued_p)
16185 {
16186 cp_parser_error (parser,
16187 "more than one access specifier in base-specified");
16188 duplicate_access_error_issued_p = true;
16189 }
16190
16191 access = ridpointers[(int) token->keyword];
16192
16193 /* Consume the access-specifier. */
16194 cp_lexer_consume_token (parser->lexer);
16195
16196 break;
16197
16198 default:
16199 done = true;
16200 break;
16201 }
16202 }
16203 /* It is not uncommon to see programs mechanically, erroneously, use
16204 the 'typename' keyword to denote (dependent) qualified types
16205 as base classes. */
16206 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16207 {
16208 token = cp_lexer_peek_token (parser->lexer);
16209 if (!processing_template_decl)
16210 error ("%Hkeyword %<typename%> not allowed outside of templates",
16211 &token->location);
16212 else
16213 error ("%Hkeyword %<typename%> not allowed in this context "
16214 "(the base class is implicitly a type)",
16215 &token->location);
16216 cp_lexer_consume_token (parser->lexer);
16217 }
16218
16219 /* Look for the optional `::' operator. */
16220 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16221 /* Look for the nested-name-specifier. The simplest way to
16222 implement:
16223
16224 [temp.res]
16225
16226 The keyword `typename' is not permitted in a base-specifier or
16227 mem-initializer; in these contexts a qualified name that
16228 depends on a template-parameter is implicitly assumed to be a
16229 type name.
16230
16231 is to pretend that we have seen the `typename' keyword at this
16232 point. */
16233 cp_parser_nested_name_specifier_opt (parser,
16234 /*typename_keyword_p=*/true,
16235 /*check_dependency_p=*/true,
16236 typename_type,
16237 /*is_declaration=*/true);
16238 /* If the base class is given by a qualified name, assume that names
16239 we see are type names or templates, as appropriate. */
16240 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16241 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16242
16243 /* Finally, look for the class-name. */
16244 type = cp_parser_class_name (parser,
16245 class_scope_p,
16246 template_p,
16247 typename_type,
16248 /*check_dependency_p=*/true,
16249 /*class_head_p=*/false,
16250 /*is_declaration=*/true);
16251
16252 if (type == error_mark_node)
16253 return error_mark_node;
16254
16255 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16256 }
16257
16258 /* Exception handling [gram.exception] */
16259
16260 /* Parse an (optional) exception-specification.
16261
16262 exception-specification:
16263 throw ( type-id-list [opt] )
16264
16265 Returns a TREE_LIST representing the exception-specification. The
16266 TREE_VALUE of each node is a type. */
16267
16268 static tree
16269 cp_parser_exception_specification_opt (cp_parser* parser)
16270 {
16271 cp_token *token;
16272 tree type_id_list;
16273
16274 /* Peek at the next token. */
16275 token = cp_lexer_peek_token (parser->lexer);
16276 /* If it's not `throw', then there's no exception-specification. */
16277 if (!cp_parser_is_keyword (token, RID_THROW))
16278 return NULL_TREE;
16279
16280 /* Consume the `throw'. */
16281 cp_lexer_consume_token (parser->lexer);
16282
16283 /* Look for the `('. */
16284 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16285
16286 /* Peek at the next token. */
16287 token = cp_lexer_peek_token (parser->lexer);
16288 /* If it's not a `)', then there is a type-id-list. */
16289 if (token->type != CPP_CLOSE_PAREN)
16290 {
16291 const char *saved_message;
16292
16293 /* Types may not be defined in an exception-specification. */
16294 saved_message = parser->type_definition_forbidden_message;
16295 parser->type_definition_forbidden_message
16296 = "types may not be defined in an exception-specification";
16297 /* Parse the type-id-list. */
16298 type_id_list = cp_parser_type_id_list (parser);
16299 /* Restore the saved message. */
16300 parser->type_definition_forbidden_message = saved_message;
16301 }
16302 else
16303 type_id_list = empty_except_spec;
16304
16305 /* Look for the `)'. */
16306 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16307
16308 return type_id_list;
16309 }
16310
16311 /* Parse an (optional) type-id-list.
16312
16313 type-id-list:
16314 type-id ... [opt]
16315 type-id-list , type-id ... [opt]
16316
16317 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16318 in the order that the types were presented. */
16319
16320 static tree
16321 cp_parser_type_id_list (cp_parser* parser)
16322 {
16323 tree types = NULL_TREE;
16324
16325 while (true)
16326 {
16327 cp_token *token;
16328 tree type;
16329
16330 /* Get the next type-id. */
16331 type = cp_parser_type_id (parser);
16332 /* Parse the optional ellipsis. */
16333 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16334 {
16335 /* Consume the `...'. */
16336 cp_lexer_consume_token (parser->lexer);
16337
16338 /* Turn the type into a pack expansion expression. */
16339 type = make_pack_expansion (type);
16340 }
16341 /* Add it to the list. */
16342 types = add_exception_specifier (types, type, /*complain=*/1);
16343 /* Peek at the next token. */
16344 token = cp_lexer_peek_token (parser->lexer);
16345 /* If it is not a `,', we are done. */
16346 if (token->type != CPP_COMMA)
16347 break;
16348 /* Consume the `,'. */
16349 cp_lexer_consume_token (parser->lexer);
16350 }
16351
16352 return nreverse (types);
16353 }
16354
16355 /* Parse a try-block.
16356
16357 try-block:
16358 try compound-statement handler-seq */
16359
16360 static tree
16361 cp_parser_try_block (cp_parser* parser)
16362 {
16363 tree try_block;
16364
16365 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16366 try_block = begin_try_block ();
16367 cp_parser_compound_statement (parser, NULL, true);
16368 finish_try_block (try_block);
16369 cp_parser_handler_seq (parser);
16370 finish_handler_sequence (try_block);
16371
16372 return try_block;
16373 }
16374
16375 /* Parse a function-try-block.
16376
16377 function-try-block:
16378 try ctor-initializer [opt] function-body handler-seq */
16379
16380 static bool
16381 cp_parser_function_try_block (cp_parser* parser)
16382 {
16383 tree compound_stmt;
16384 tree try_block;
16385 bool ctor_initializer_p;
16386
16387 /* Look for the `try' keyword. */
16388 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16389 return false;
16390 /* Let the rest of the front end know where we are. */
16391 try_block = begin_function_try_block (&compound_stmt);
16392 /* Parse the function-body. */
16393 ctor_initializer_p
16394 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16395 /* We're done with the `try' part. */
16396 finish_function_try_block (try_block);
16397 /* Parse the handlers. */
16398 cp_parser_handler_seq (parser);
16399 /* We're done with the handlers. */
16400 finish_function_handler_sequence (try_block, compound_stmt);
16401
16402 return ctor_initializer_p;
16403 }
16404
16405 /* Parse a handler-seq.
16406
16407 handler-seq:
16408 handler handler-seq [opt] */
16409
16410 static void
16411 cp_parser_handler_seq (cp_parser* parser)
16412 {
16413 while (true)
16414 {
16415 cp_token *token;
16416
16417 /* Parse the handler. */
16418 cp_parser_handler (parser);
16419 /* Peek at the next token. */
16420 token = cp_lexer_peek_token (parser->lexer);
16421 /* If it's not `catch' then there are no more handlers. */
16422 if (!cp_parser_is_keyword (token, RID_CATCH))
16423 break;
16424 }
16425 }
16426
16427 /* Parse a handler.
16428
16429 handler:
16430 catch ( exception-declaration ) compound-statement */
16431
16432 static void
16433 cp_parser_handler (cp_parser* parser)
16434 {
16435 tree handler;
16436 tree declaration;
16437
16438 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16439 handler = begin_handler ();
16440 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16441 declaration = cp_parser_exception_declaration (parser);
16442 finish_handler_parms (declaration, handler);
16443 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16444 cp_parser_compound_statement (parser, NULL, false);
16445 finish_handler (handler);
16446 }
16447
16448 /* Parse an exception-declaration.
16449
16450 exception-declaration:
16451 type-specifier-seq declarator
16452 type-specifier-seq abstract-declarator
16453 type-specifier-seq
16454 ...
16455
16456 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16457 ellipsis variant is used. */
16458
16459 static tree
16460 cp_parser_exception_declaration (cp_parser* parser)
16461 {
16462 cp_decl_specifier_seq type_specifiers;
16463 cp_declarator *declarator;
16464 const char *saved_message;
16465
16466 /* If it's an ellipsis, it's easy to handle. */
16467 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16468 {
16469 /* Consume the `...' token. */
16470 cp_lexer_consume_token (parser->lexer);
16471 return NULL_TREE;
16472 }
16473
16474 /* Types may not be defined in exception-declarations. */
16475 saved_message = parser->type_definition_forbidden_message;
16476 parser->type_definition_forbidden_message
16477 = "types may not be defined in exception-declarations";
16478
16479 /* Parse the type-specifier-seq. */
16480 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16481 &type_specifiers);
16482 /* If it's a `)', then there is no declarator. */
16483 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16484 declarator = NULL;
16485 else
16486 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16487 /*ctor_dtor_or_conv_p=*/NULL,
16488 /*parenthesized_p=*/NULL,
16489 /*member_p=*/false);
16490
16491 /* Restore the saved message. */
16492 parser->type_definition_forbidden_message = saved_message;
16493
16494 if (!type_specifiers.any_specifiers_p)
16495 return error_mark_node;
16496
16497 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16498 }
16499
16500 /* Parse a throw-expression.
16501
16502 throw-expression:
16503 throw assignment-expression [opt]
16504
16505 Returns a THROW_EXPR representing the throw-expression. */
16506
16507 static tree
16508 cp_parser_throw_expression (cp_parser* parser)
16509 {
16510 tree expression;
16511 cp_token* token;
16512
16513 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16514 token = cp_lexer_peek_token (parser->lexer);
16515 /* Figure out whether or not there is an assignment-expression
16516 following the "throw" keyword. */
16517 if (token->type == CPP_COMMA
16518 || token->type == CPP_SEMICOLON
16519 || token->type == CPP_CLOSE_PAREN
16520 || token->type == CPP_CLOSE_SQUARE
16521 || token->type == CPP_CLOSE_BRACE
16522 || token->type == CPP_COLON)
16523 expression = NULL_TREE;
16524 else
16525 expression = cp_parser_assignment_expression (parser,
16526 /*cast_p=*/false, NULL);
16527
16528 return build_throw (expression);
16529 }
16530
16531 /* GNU Extensions */
16532
16533 /* Parse an (optional) asm-specification.
16534
16535 asm-specification:
16536 asm ( string-literal )
16537
16538 If the asm-specification is present, returns a STRING_CST
16539 corresponding to the string-literal. Otherwise, returns
16540 NULL_TREE. */
16541
16542 static tree
16543 cp_parser_asm_specification_opt (cp_parser* parser)
16544 {
16545 cp_token *token;
16546 tree asm_specification;
16547
16548 /* Peek at the next token. */
16549 token = cp_lexer_peek_token (parser->lexer);
16550 /* If the next token isn't the `asm' keyword, then there's no
16551 asm-specification. */
16552 if (!cp_parser_is_keyword (token, RID_ASM))
16553 return NULL_TREE;
16554
16555 /* Consume the `asm' token. */
16556 cp_lexer_consume_token (parser->lexer);
16557 /* Look for the `('. */
16558 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16559
16560 /* Look for the string-literal. */
16561 asm_specification = cp_parser_string_literal (parser, false, false);
16562
16563 /* Look for the `)'. */
16564 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16565
16566 return asm_specification;
16567 }
16568
16569 /* Parse an asm-operand-list.
16570
16571 asm-operand-list:
16572 asm-operand
16573 asm-operand-list , asm-operand
16574
16575 asm-operand:
16576 string-literal ( expression )
16577 [ string-literal ] string-literal ( expression )
16578
16579 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16580 each node is the expression. The TREE_PURPOSE is itself a
16581 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16582 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16583 is a STRING_CST for the string literal before the parenthesis. Returns
16584 ERROR_MARK_NODE if any of the operands are invalid. */
16585
16586 static tree
16587 cp_parser_asm_operand_list (cp_parser* parser)
16588 {
16589 tree asm_operands = NULL_TREE;
16590 bool invalid_operands = false;
16591
16592 while (true)
16593 {
16594 tree string_literal;
16595 tree expression;
16596 tree name;
16597
16598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16599 {
16600 /* Consume the `[' token. */
16601 cp_lexer_consume_token (parser->lexer);
16602 /* Read the operand name. */
16603 name = cp_parser_identifier (parser);
16604 if (name != error_mark_node)
16605 name = build_string (IDENTIFIER_LENGTH (name),
16606 IDENTIFIER_POINTER (name));
16607 /* Look for the closing `]'. */
16608 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16609 }
16610 else
16611 name = NULL_TREE;
16612 /* Look for the string-literal. */
16613 string_literal = cp_parser_string_literal (parser, false, false);
16614
16615 /* Look for the `('. */
16616 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16617 /* Parse the expression. */
16618 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16619 /* Look for the `)'. */
16620 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16621
16622 if (name == error_mark_node
16623 || string_literal == error_mark_node
16624 || expression == error_mark_node)
16625 invalid_operands = true;
16626
16627 /* Add this operand to the list. */
16628 asm_operands = tree_cons (build_tree_list (name, string_literal),
16629 expression,
16630 asm_operands);
16631 /* If the next token is not a `,', there are no more
16632 operands. */
16633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16634 break;
16635 /* Consume the `,'. */
16636 cp_lexer_consume_token (parser->lexer);
16637 }
16638
16639 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16640 }
16641
16642 /* Parse an asm-clobber-list.
16643
16644 asm-clobber-list:
16645 string-literal
16646 asm-clobber-list , string-literal
16647
16648 Returns a TREE_LIST, indicating the clobbers in the order that they
16649 appeared. The TREE_VALUE of each node is a STRING_CST. */
16650
16651 static tree
16652 cp_parser_asm_clobber_list (cp_parser* parser)
16653 {
16654 tree clobbers = NULL_TREE;
16655
16656 while (true)
16657 {
16658 tree string_literal;
16659
16660 /* Look for the string literal. */
16661 string_literal = cp_parser_string_literal (parser, false, false);
16662 /* Add it to the list. */
16663 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16664 /* If the next token is not a `,', then the list is
16665 complete. */
16666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16667 break;
16668 /* Consume the `,' token. */
16669 cp_lexer_consume_token (parser->lexer);
16670 }
16671
16672 return clobbers;
16673 }
16674
16675 /* Parse an (optional) series of attributes.
16676
16677 attributes:
16678 attributes attribute
16679
16680 attribute:
16681 __attribute__ (( attribute-list [opt] ))
16682
16683 The return value is as for cp_parser_attribute_list. */
16684
16685 static tree
16686 cp_parser_attributes_opt (cp_parser* parser)
16687 {
16688 tree attributes = NULL_TREE;
16689
16690 while (true)
16691 {
16692 cp_token *token;
16693 tree attribute_list;
16694
16695 /* Peek at the next token. */
16696 token = cp_lexer_peek_token (parser->lexer);
16697 /* If it's not `__attribute__', then we're done. */
16698 if (token->keyword != RID_ATTRIBUTE)
16699 break;
16700
16701 /* Consume the `__attribute__' keyword. */
16702 cp_lexer_consume_token (parser->lexer);
16703 /* Look for the two `(' tokens. */
16704 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16705 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16706
16707 /* Peek at the next token. */
16708 token = cp_lexer_peek_token (parser->lexer);
16709 if (token->type != CPP_CLOSE_PAREN)
16710 /* Parse the attribute-list. */
16711 attribute_list = cp_parser_attribute_list (parser);
16712 else
16713 /* If the next token is a `)', then there is no attribute
16714 list. */
16715 attribute_list = NULL;
16716
16717 /* Look for the two `)' tokens. */
16718 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16719 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16720
16721 /* Add these new attributes to the list. */
16722 attributes = chainon (attributes, attribute_list);
16723 }
16724
16725 return attributes;
16726 }
16727
16728 /* Parse an attribute-list.
16729
16730 attribute-list:
16731 attribute
16732 attribute-list , attribute
16733
16734 attribute:
16735 identifier
16736 identifier ( identifier )
16737 identifier ( identifier , expression-list )
16738 identifier ( expression-list )
16739
16740 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16741 to an attribute. The TREE_PURPOSE of each node is the identifier
16742 indicating which attribute is in use. The TREE_VALUE represents
16743 the arguments, if any. */
16744
16745 static tree
16746 cp_parser_attribute_list (cp_parser* parser)
16747 {
16748 tree attribute_list = NULL_TREE;
16749 bool save_translate_strings_p = parser->translate_strings_p;
16750
16751 parser->translate_strings_p = false;
16752 while (true)
16753 {
16754 cp_token *token;
16755 tree identifier;
16756 tree attribute;
16757
16758 /* Look for the identifier. We also allow keywords here; for
16759 example `__attribute__ ((const))' is legal. */
16760 token = cp_lexer_peek_token (parser->lexer);
16761 if (token->type == CPP_NAME
16762 || token->type == CPP_KEYWORD)
16763 {
16764 tree arguments = NULL_TREE;
16765
16766 /* Consume the token. */
16767 token = cp_lexer_consume_token (parser->lexer);
16768
16769 /* Save away the identifier that indicates which attribute
16770 this is. */
16771 identifier = token->u.value;
16772 attribute = build_tree_list (identifier, NULL_TREE);
16773
16774 /* Peek at the next token. */
16775 token = cp_lexer_peek_token (parser->lexer);
16776 /* If it's an `(', then parse the attribute arguments. */
16777 if (token->type == CPP_OPEN_PAREN)
16778 {
16779 arguments = cp_parser_parenthesized_expression_list
16780 (parser, true, /*cast_p=*/false,
16781 /*allow_expansion_p=*/false,
16782 /*non_constant_p=*/NULL);
16783 /* Save the arguments away. */
16784 TREE_VALUE (attribute) = arguments;
16785 }
16786
16787 if (arguments != error_mark_node)
16788 {
16789 /* Add this attribute to the list. */
16790 TREE_CHAIN (attribute) = attribute_list;
16791 attribute_list = attribute;
16792 }
16793
16794 token = cp_lexer_peek_token (parser->lexer);
16795 }
16796 /* Now, look for more attributes. If the next token isn't a
16797 `,', we're done. */
16798 if (token->type != CPP_COMMA)
16799 break;
16800
16801 /* Consume the comma and keep going. */
16802 cp_lexer_consume_token (parser->lexer);
16803 }
16804 parser->translate_strings_p = save_translate_strings_p;
16805
16806 /* We built up the list in reverse order. */
16807 return nreverse (attribute_list);
16808 }
16809
16810 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16811 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16812 current value of the PEDANTIC flag, regardless of whether or not
16813 the `__extension__' keyword is present. The caller is responsible
16814 for restoring the value of the PEDANTIC flag. */
16815
16816 static bool
16817 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16818 {
16819 /* Save the old value of the PEDANTIC flag. */
16820 *saved_pedantic = pedantic;
16821
16822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16823 {
16824 /* Consume the `__extension__' token. */
16825 cp_lexer_consume_token (parser->lexer);
16826 /* We're not being pedantic while the `__extension__' keyword is
16827 in effect. */
16828 pedantic = 0;
16829
16830 return true;
16831 }
16832
16833 return false;
16834 }
16835
16836 /* Parse a label declaration.
16837
16838 label-declaration:
16839 __label__ label-declarator-seq ;
16840
16841 label-declarator-seq:
16842 identifier , label-declarator-seq
16843 identifier */
16844
16845 static void
16846 cp_parser_label_declaration (cp_parser* parser)
16847 {
16848 /* Look for the `__label__' keyword. */
16849 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16850
16851 while (true)
16852 {
16853 tree identifier;
16854
16855 /* Look for an identifier. */
16856 identifier = cp_parser_identifier (parser);
16857 /* If we failed, stop. */
16858 if (identifier == error_mark_node)
16859 break;
16860 /* Declare it as a label. */
16861 finish_label_decl (identifier);
16862 /* If the next token is a `;', stop. */
16863 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16864 break;
16865 /* Look for the `,' separating the label declarations. */
16866 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16867 }
16868
16869 /* Look for the final `;'. */
16870 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16871 }
16872
16873 /* Support Functions */
16874
16875 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16876 NAME should have one of the representations used for an
16877 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16878 is returned. If PARSER->SCOPE is a dependent type, then a
16879 SCOPE_REF is returned.
16880
16881 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16882 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16883 was formed. Abstractly, such entities should not be passed to this
16884 function, because they do not need to be looked up, but it is
16885 simpler to check for this special case here, rather than at the
16886 call-sites.
16887
16888 In cases not explicitly covered above, this function returns a
16889 DECL, OVERLOAD, or baselink representing the result of the lookup.
16890 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16891 is returned.
16892
16893 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16894 (e.g., "struct") that was used. In that case bindings that do not
16895 refer to types are ignored.
16896
16897 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16898 ignored.
16899
16900 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16901 are ignored.
16902
16903 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16904 types.
16905
16906 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16907 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16908 NULL_TREE otherwise. */
16909
16910 static tree
16911 cp_parser_lookup_name (cp_parser *parser, tree name,
16912 enum tag_types tag_type,
16913 bool is_template,
16914 bool is_namespace,
16915 bool check_dependency,
16916 tree *ambiguous_decls,
16917 location_t name_location)
16918 {
16919 int flags = 0;
16920 tree decl;
16921 tree object_type = parser->context->object_type;
16922
16923 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16924 flags |= LOOKUP_COMPLAIN;
16925
16926 /* Assume that the lookup will be unambiguous. */
16927 if (ambiguous_decls)
16928 *ambiguous_decls = NULL_TREE;
16929
16930 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16931 no longer valid. Note that if we are parsing tentatively, and
16932 the parse fails, OBJECT_TYPE will be automatically restored. */
16933 parser->context->object_type = NULL_TREE;
16934
16935 if (name == error_mark_node)
16936 return error_mark_node;
16937
16938 /* A template-id has already been resolved; there is no lookup to
16939 do. */
16940 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16941 return name;
16942 if (BASELINK_P (name))
16943 {
16944 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16945 == TEMPLATE_ID_EXPR);
16946 return name;
16947 }
16948
16949 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16950 it should already have been checked to make sure that the name
16951 used matches the type being destroyed. */
16952 if (TREE_CODE (name) == BIT_NOT_EXPR)
16953 {
16954 tree type;
16955
16956 /* Figure out to which type this destructor applies. */
16957 if (parser->scope)
16958 type = parser->scope;
16959 else if (object_type)
16960 type = object_type;
16961 else
16962 type = current_class_type;
16963 /* If that's not a class type, there is no destructor. */
16964 if (!type || !CLASS_TYPE_P (type))
16965 return error_mark_node;
16966 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16967 lazily_declare_fn (sfk_destructor, type);
16968 if (!CLASSTYPE_DESTRUCTORS (type))
16969 return error_mark_node;
16970 /* If it was a class type, return the destructor. */
16971 return CLASSTYPE_DESTRUCTORS (type);
16972 }
16973
16974 /* By this point, the NAME should be an ordinary identifier. If
16975 the id-expression was a qualified name, the qualifying scope is
16976 stored in PARSER->SCOPE at this point. */
16977 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16978
16979 /* Perform the lookup. */
16980 if (parser->scope)
16981 {
16982 bool dependent_p;
16983
16984 if (parser->scope == error_mark_node)
16985 return error_mark_node;
16986
16987 /* If the SCOPE is dependent, the lookup must be deferred until
16988 the template is instantiated -- unless we are explicitly
16989 looking up names in uninstantiated templates. Even then, we
16990 cannot look up the name if the scope is not a class type; it
16991 might, for example, be a template type parameter. */
16992 dependent_p = (TYPE_P (parser->scope)
16993 && !(parser->in_declarator_p
16994 && currently_open_class (parser->scope))
16995 && dependent_type_p (parser->scope));
16996 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16997 && dependent_p)
16998 {
16999 if (tag_type)
17000 {
17001 tree type;
17002
17003 /* The resolution to Core Issue 180 says that `struct
17004 A::B' should be considered a type-name, even if `A'
17005 is dependent. */
17006 type = make_typename_type (parser->scope, name, tag_type,
17007 /*complain=*/tf_error);
17008 decl = TYPE_NAME (type);
17009 }
17010 else if (is_template
17011 && (cp_parser_next_token_ends_template_argument_p (parser)
17012 || cp_lexer_next_token_is (parser->lexer,
17013 CPP_CLOSE_PAREN)))
17014 decl = make_unbound_class_template (parser->scope,
17015 name, NULL_TREE,
17016 /*complain=*/tf_error);
17017 else
17018 decl = build_qualified_name (/*type=*/NULL_TREE,
17019 parser->scope, name,
17020 is_template);
17021 }
17022 else
17023 {
17024 tree pushed_scope = NULL_TREE;
17025
17026 /* If PARSER->SCOPE is a dependent type, then it must be a
17027 class type, and we must not be checking dependencies;
17028 otherwise, we would have processed this lookup above. So
17029 that PARSER->SCOPE is not considered a dependent base by
17030 lookup_member, we must enter the scope here. */
17031 if (dependent_p)
17032 pushed_scope = push_scope (parser->scope);
17033 /* If the PARSER->SCOPE is a template specialization, it
17034 may be instantiated during name lookup. In that case,
17035 errors may be issued. Even if we rollback the current
17036 tentative parse, those errors are valid. */
17037 decl = lookup_qualified_name (parser->scope, name,
17038 tag_type != none_type,
17039 /*complain=*/true);
17040
17041 /* If we have a single function from a using decl, pull it out. */
17042 if (decl
17043 && TREE_CODE (decl) == OVERLOAD
17044 && !really_overloaded_fn (decl))
17045 decl = OVL_FUNCTION (decl);
17046
17047 if (pushed_scope)
17048 pop_scope (pushed_scope);
17049 }
17050 parser->qualifying_scope = parser->scope;
17051 parser->object_scope = NULL_TREE;
17052 }
17053 else if (object_type)
17054 {
17055 tree object_decl = NULL_TREE;
17056 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17057 OBJECT_TYPE is not a class. */
17058 if (CLASS_TYPE_P (object_type))
17059 /* If the OBJECT_TYPE is a template specialization, it may
17060 be instantiated during name lookup. In that case, errors
17061 may be issued. Even if we rollback the current tentative
17062 parse, those errors are valid. */
17063 object_decl = lookup_member (object_type,
17064 name,
17065 /*protect=*/0,
17066 tag_type != none_type);
17067 /* Look it up in the enclosing context, too. */
17068 decl = lookup_name_real (name, tag_type != none_type,
17069 /*nonclass=*/0,
17070 /*block_p=*/true, is_namespace, flags);
17071 parser->object_scope = object_type;
17072 parser->qualifying_scope = NULL_TREE;
17073 if (object_decl)
17074 decl = object_decl;
17075 }
17076 else
17077 {
17078 decl = lookup_name_real (name, tag_type != none_type,
17079 /*nonclass=*/0,
17080 /*block_p=*/true, is_namespace, flags);
17081 parser->qualifying_scope = NULL_TREE;
17082 parser->object_scope = NULL_TREE;
17083 }
17084
17085 /* If the lookup failed, let our caller know. */
17086 if (!decl || decl == error_mark_node)
17087 return error_mark_node;
17088
17089 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17090 if (TREE_CODE (decl) == TREE_LIST)
17091 {
17092 if (ambiguous_decls)
17093 *ambiguous_decls = decl;
17094 /* The error message we have to print is too complicated for
17095 cp_parser_error, so we incorporate its actions directly. */
17096 if (!cp_parser_simulate_error (parser))
17097 {
17098 error ("%Hreference to %qD is ambiguous",
17099 &name_location, name);
17100 print_candidates (decl);
17101 }
17102 return error_mark_node;
17103 }
17104
17105 gcc_assert (DECL_P (decl)
17106 || TREE_CODE (decl) == OVERLOAD
17107 || TREE_CODE (decl) == SCOPE_REF
17108 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17109 || BASELINK_P (decl));
17110
17111 /* If we have resolved the name of a member declaration, check to
17112 see if the declaration is accessible. When the name resolves to
17113 set of overloaded functions, accessibility is checked when
17114 overload resolution is done.
17115
17116 During an explicit instantiation, access is not checked at all,
17117 as per [temp.explicit]. */
17118 if (DECL_P (decl))
17119 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17120
17121 return decl;
17122 }
17123
17124 /* Like cp_parser_lookup_name, but for use in the typical case where
17125 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17126 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17127
17128 static tree
17129 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17130 {
17131 return cp_parser_lookup_name (parser, name,
17132 none_type,
17133 /*is_template=*/false,
17134 /*is_namespace=*/false,
17135 /*check_dependency=*/true,
17136 /*ambiguous_decls=*/NULL,
17137 location);
17138 }
17139
17140 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17141 the current context, return the TYPE_DECL. If TAG_NAME_P is
17142 true, the DECL indicates the class being defined in a class-head,
17143 or declared in an elaborated-type-specifier.
17144
17145 Otherwise, return DECL. */
17146
17147 static tree
17148 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17149 {
17150 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17151 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17152
17153 struct A {
17154 template <typename T> struct B;
17155 };
17156
17157 template <typename T> struct A::B {};
17158
17159 Similarly, in an elaborated-type-specifier:
17160
17161 namespace N { struct X{}; }
17162
17163 struct A {
17164 template <typename T> friend struct N::X;
17165 };
17166
17167 However, if the DECL refers to a class type, and we are in
17168 the scope of the class, then the name lookup automatically
17169 finds the TYPE_DECL created by build_self_reference rather
17170 than a TEMPLATE_DECL. For example, in:
17171
17172 template <class T> struct S {
17173 S s;
17174 };
17175
17176 there is no need to handle such case. */
17177
17178 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17179 return DECL_TEMPLATE_RESULT (decl);
17180
17181 return decl;
17182 }
17183
17184 /* If too many, or too few, template-parameter lists apply to the
17185 declarator, issue an error message. Returns TRUE if all went well,
17186 and FALSE otherwise. */
17187
17188 static bool
17189 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17190 cp_declarator *declarator,
17191 location_t declarator_location)
17192 {
17193 unsigned num_templates;
17194
17195 /* We haven't seen any classes that involve template parameters yet. */
17196 num_templates = 0;
17197
17198 switch (declarator->kind)
17199 {
17200 case cdk_id:
17201 if (declarator->u.id.qualifying_scope)
17202 {
17203 tree scope;
17204 tree member;
17205
17206 scope = declarator->u.id.qualifying_scope;
17207 member = declarator->u.id.unqualified_name;
17208
17209 while (scope && CLASS_TYPE_P (scope))
17210 {
17211 /* You're supposed to have one `template <...>'
17212 for every template class, but you don't need one
17213 for a full specialization. For example:
17214
17215 template <class T> struct S{};
17216 template <> struct S<int> { void f(); };
17217 void S<int>::f () {}
17218
17219 is correct; there shouldn't be a `template <>' for
17220 the definition of `S<int>::f'. */
17221 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17222 /* If SCOPE does not have template information of any
17223 kind, then it is not a template, nor is it nested
17224 within a template. */
17225 break;
17226 if (explicit_class_specialization_p (scope))
17227 break;
17228 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17229 ++num_templates;
17230
17231 scope = TYPE_CONTEXT (scope);
17232 }
17233 }
17234 else if (TREE_CODE (declarator->u.id.unqualified_name)
17235 == TEMPLATE_ID_EXPR)
17236 /* If the DECLARATOR has the form `X<y>' then it uses one
17237 additional level of template parameters. */
17238 ++num_templates;
17239
17240 return cp_parser_check_template_parameters (parser,
17241 num_templates,
17242 declarator_location);
17243
17244 case cdk_function:
17245 case cdk_array:
17246 case cdk_pointer:
17247 case cdk_reference:
17248 case cdk_ptrmem:
17249 return (cp_parser_check_declarator_template_parameters
17250 (parser, declarator->declarator, declarator_location));
17251
17252 case cdk_error:
17253 return true;
17254
17255 default:
17256 gcc_unreachable ();
17257 }
17258 return false;
17259 }
17260
17261 /* NUM_TEMPLATES were used in the current declaration. If that is
17262 invalid, return FALSE and issue an error messages. Otherwise,
17263 return TRUE. */
17264
17265 static bool
17266 cp_parser_check_template_parameters (cp_parser* parser,
17267 unsigned num_templates,
17268 location_t location)
17269 {
17270 /* If there are more template classes than parameter lists, we have
17271 something like:
17272
17273 template <class T> void S<T>::R<T>::f (); */
17274 if (parser->num_template_parameter_lists < num_templates)
17275 {
17276 error ("%Htoo few template-parameter-lists", &location);
17277 return false;
17278 }
17279 /* If there are the same number of template classes and parameter
17280 lists, that's OK. */
17281 if (parser->num_template_parameter_lists == num_templates)
17282 return true;
17283 /* If there are more, but only one more, then we are referring to a
17284 member template. That's OK too. */
17285 if (parser->num_template_parameter_lists == num_templates + 1)
17286 return true;
17287 /* Otherwise, there are too many template parameter lists. We have
17288 something like:
17289
17290 template <class T> template <class U> void S::f(); */
17291 error ("%Htoo many template-parameter-lists", &location);
17292 return false;
17293 }
17294
17295 /* Parse an optional `::' token indicating that the following name is
17296 from the global namespace. If so, PARSER->SCOPE is set to the
17297 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17298 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17299 Returns the new value of PARSER->SCOPE, if the `::' token is
17300 present, and NULL_TREE otherwise. */
17301
17302 static tree
17303 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17304 {
17305 cp_token *token;
17306
17307 /* Peek at the next token. */
17308 token = cp_lexer_peek_token (parser->lexer);
17309 /* If we're looking at a `::' token then we're starting from the
17310 global namespace, not our current location. */
17311 if (token->type == CPP_SCOPE)
17312 {
17313 /* Consume the `::' token. */
17314 cp_lexer_consume_token (parser->lexer);
17315 /* Set the SCOPE so that we know where to start the lookup. */
17316 parser->scope = global_namespace;
17317 parser->qualifying_scope = global_namespace;
17318 parser->object_scope = NULL_TREE;
17319
17320 return parser->scope;
17321 }
17322 else if (!current_scope_valid_p)
17323 {
17324 parser->scope = NULL_TREE;
17325 parser->qualifying_scope = NULL_TREE;
17326 parser->object_scope = NULL_TREE;
17327 }
17328
17329 return NULL_TREE;
17330 }
17331
17332 /* Returns TRUE if the upcoming token sequence is the start of a
17333 constructor declarator. If FRIEND_P is true, the declarator is
17334 preceded by the `friend' specifier. */
17335
17336 static bool
17337 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17338 {
17339 bool constructor_p;
17340 tree type_decl = NULL_TREE;
17341 bool nested_name_p;
17342 cp_token *next_token;
17343
17344 /* The common case is that this is not a constructor declarator, so
17345 try to avoid doing lots of work if at all possible. It's not
17346 valid declare a constructor at function scope. */
17347 if (parser->in_function_body)
17348 return false;
17349 /* And only certain tokens can begin a constructor declarator. */
17350 next_token = cp_lexer_peek_token (parser->lexer);
17351 if (next_token->type != CPP_NAME
17352 && next_token->type != CPP_SCOPE
17353 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17354 && next_token->type != CPP_TEMPLATE_ID)
17355 return false;
17356
17357 /* Parse tentatively; we are going to roll back all of the tokens
17358 consumed here. */
17359 cp_parser_parse_tentatively (parser);
17360 /* Assume that we are looking at a constructor declarator. */
17361 constructor_p = true;
17362
17363 /* Look for the optional `::' operator. */
17364 cp_parser_global_scope_opt (parser,
17365 /*current_scope_valid_p=*/false);
17366 /* Look for the nested-name-specifier. */
17367 nested_name_p
17368 = (cp_parser_nested_name_specifier_opt (parser,
17369 /*typename_keyword_p=*/false,
17370 /*check_dependency_p=*/false,
17371 /*type_p=*/false,
17372 /*is_declaration=*/false)
17373 != NULL_TREE);
17374 /* Outside of a class-specifier, there must be a
17375 nested-name-specifier. */
17376 if (!nested_name_p &&
17377 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17378 || friend_p))
17379 constructor_p = false;
17380 /* If we still think that this might be a constructor-declarator,
17381 look for a class-name. */
17382 if (constructor_p)
17383 {
17384 /* If we have:
17385
17386 template <typename T> struct S { S(); };
17387 template <typename T> S<T>::S ();
17388
17389 we must recognize that the nested `S' names a class.
17390 Similarly, for:
17391
17392 template <typename T> S<T>::S<T> ();
17393
17394 we must recognize that the nested `S' names a template. */
17395 type_decl = cp_parser_class_name (parser,
17396 /*typename_keyword_p=*/false,
17397 /*template_keyword_p=*/false,
17398 none_type,
17399 /*check_dependency_p=*/false,
17400 /*class_head_p=*/false,
17401 /*is_declaration=*/false);
17402 /* If there was no class-name, then this is not a constructor. */
17403 constructor_p = !cp_parser_error_occurred (parser);
17404 }
17405
17406 /* If we're still considering a constructor, we have to see a `(',
17407 to begin the parameter-declaration-clause, followed by either a
17408 `)', an `...', or a decl-specifier. We need to check for a
17409 type-specifier to avoid being fooled into thinking that:
17410
17411 S::S (f) (int);
17412
17413 is a constructor. (It is actually a function named `f' that
17414 takes one parameter (of type `int') and returns a value of type
17415 `S::S'. */
17416 if (constructor_p
17417 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17418 {
17419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17420 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17421 /* A parameter declaration begins with a decl-specifier,
17422 which is either the "attribute" keyword, a storage class
17423 specifier, or (usually) a type-specifier. */
17424 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17425 {
17426 tree type;
17427 tree pushed_scope = NULL_TREE;
17428 unsigned saved_num_template_parameter_lists;
17429
17430 /* Names appearing in the type-specifier should be looked up
17431 in the scope of the class. */
17432 if (current_class_type)
17433 type = NULL_TREE;
17434 else
17435 {
17436 type = TREE_TYPE (type_decl);
17437 if (TREE_CODE (type) == TYPENAME_TYPE)
17438 {
17439 type = resolve_typename_type (type,
17440 /*only_current_p=*/false);
17441 if (TREE_CODE (type) == TYPENAME_TYPE)
17442 {
17443 cp_parser_abort_tentative_parse (parser);
17444 return false;
17445 }
17446 }
17447 pushed_scope = push_scope (type);
17448 }
17449
17450 /* Inside the constructor parameter list, surrounding
17451 template-parameter-lists do not apply. */
17452 saved_num_template_parameter_lists
17453 = parser->num_template_parameter_lists;
17454 parser->num_template_parameter_lists = 0;
17455
17456 /* Look for the type-specifier. */
17457 cp_parser_type_specifier (parser,
17458 CP_PARSER_FLAGS_NONE,
17459 /*decl_specs=*/NULL,
17460 /*is_declarator=*/true,
17461 /*declares_class_or_enum=*/NULL,
17462 /*is_cv_qualifier=*/NULL);
17463
17464 parser->num_template_parameter_lists
17465 = saved_num_template_parameter_lists;
17466
17467 /* Leave the scope of the class. */
17468 if (pushed_scope)
17469 pop_scope (pushed_scope);
17470
17471 constructor_p = !cp_parser_error_occurred (parser);
17472 }
17473 }
17474 else
17475 constructor_p = false;
17476 /* We did not really want to consume any tokens. */
17477 cp_parser_abort_tentative_parse (parser);
17478
17479 return constructor_p;
17480 }
17481
17482 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17483 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17484 they must be performed once we are in the scope of the function.
17485
17486 Returns the function defined. */
17487
17488 static tree
17489 cp_parser_function_definition_from_specifiers_and_declarator
17490 (cp_parser* parser,
17491 cp_decl_specifier_seq *decl_specifiers,
17492 tree attributes,
17493 const cp_declarator *declarator)
17494 {
17495 tree fn;
17496 bool success_p;
17497
17498 /* Begin the function-definition. */
17499 success_p = start_function (decl_specifiers, declarator, attributes);
17500
17501 /* The things we're about to see are not directly qualified by any
17502 template headers we've seen thus far. */
17503 reset_specialization ();
17504
17505 /* If there were names looked up in the decl-specifier-seq that we
17506 did not check, check them now. We must wait until we are in the
17507 scope of the function to perform the checks, since the function
17508 might be a friend. */
17509 perform_deferred_access_checks ();
17510
17511 if (!success_p)
17512 {
17513 /* Skip the entire function. */
17514 cp_parser_skip_to_end_of_block_or_statement (parser);
17515 fn = error_mark_node;
17516 }
17517 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17518 {
17519 /* Seen already, skip it. An error message has already been output. */
17520 cp_parser_skip_to_end_of_block_or_statement (parser);
17521 fn = current_function_decl;
17522 current_function_decl = NULL_TREE;
17523 /* If this is a function from a class, pop the nested class. */
17524 if (current_class_name)
17525 pop_nested_class ();
17526 }
17527 else
17528 fn = cp_parser_function_definition_after_declarator (parser,
17529 /*inline_p=*/false);
17530
17531 return fn;
17532 }
17533
17534 /* Parse the part of a function-definition that follows the
17535 declarator. INLINE_P is TRUE iff this function is an inline
17536 function defined with a class-specifier.
17537
17538 Returns the function defined. */
17539
17540 static tree
17541 cp_parser_function_definition_after_declarator (cp_parser* parser,
17542 bool inline_p)
17543 {
17544 tree fn;
17545 bool ctor_initializer_p = false;
17546 bool saved_in_unbraced_linkage_specification_p;
17547 bool saved_in_function_body;
17548 unsigned saved_num_template_parameter_lists;
17549 cp_token *token;
17550
17551 saved_in_function_body = parser->in_function_body;
17552 parser->in_function_body = true;
17553 /* If the next token is `return', then the code may be trying to
17554 make use of the "named return value" extension that G++ used to
17555 support. */
17556 token = cp_lexer_peek_token (parser->lexer);
17557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17558 {
17559 /* Consume the `return' keyword. */
17560 cp_lexer_consume_token (parser->lexer);
17561 /* Look for the identifier that indicates what value is to be
17562 returned. */
17563 cp_parser_identifier (parser);
17564 /* Issue an error message. */
17565 error ("%Hnamed return values are no longer supported",
17566 &token->location);
17567 /* Skip tokens until we reach the start of the function body. */
17568 while (true)
17569 {
17570 cp_token *token = cp_lexer_peek_token (parser->lexer);
17571 if (token->type == CPP_OPEN_BRACE
17572 || token->type == CPP_EOF
17573 || token->type == CPP_PRAGMA_EOL)
17574 break;
17575 cp_lexer_consume_token (parser->lexer);
17576 }
17577 }
17578 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17579 anything declared inside `f'. */
17580 saved_in_unbraced_linkage_specification_p
17581 = parser->in_unbraced_linkage_specification_p;
17582 parser->in_unbraced_linkage_specification_p = false;
17583 /* Inside the function, surrounding template-parameter-lists do not
17584 apply. */
17585 saved_num_template_parameter_lists
17586 = parser->num_template_parameter_lists;
17587 parser->num_template_parameter_lists = 0;
17588 /* If the next token is `try', then we are looking at a
17589 function-try-block. */
17590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17591 ctor_initializer_p = cp_parser_function_try_block (parser);
17592 /* A function-try-block includes the function-body, so we only do
17593 this next part if we're not processing a function-try-block. */
17594 else
17595 ctor_initializer_p
17596 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17597
17598 /* Finish the function. */
17599 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17600 (inline_p ? 2 : 0));
17601 /* Generate code for it, if necessary. */
17602 expand_or_defer_fn (fn);
17603 /* Restore the saved values. */
17604 parser->in_unbraced_linkage_specification_p
17605 = saved_in_unbraced_linkage_specification_p;
17606 parser->num_template_parameter_lists
17607 = saved_num_template_parameter_lists;
17608 parser->in_function_body = saved_in_function_body;
17609
17610 return fn;
17611 }
17612
17613 /* Parse a template-declaration, assuming that the `export' (and
17614 `extern') keywords, if present, has already been scanned. MEMBER_P
17615 is as for cp_parser_template_declaration. */
17616
17617 static void
17618 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17619 {
17620 tree decl = NULL_TREE;
17621 VEC (deferred_access_check,gc) *checks;
17622 tree parameter_list;
17623 bool friend_p = false;
17624 bool need_lang_pop;
17625 cp_token *token;
17626
17627 /* Look for the `template' keyword. */
17628 token = cp_lexer_peek_token (parser->lexer);
17629 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17630 return;
17631
17632 /* And the `<'. */
17633 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17634 return;
17635 if (at_class_scope_p () && current_function_decl)
17636 {
17637 /* 14.5.2.2 [temp.mem]
17638
17639 A local class shall not have member templates. */
17640 error ("%Hinvalid declaration of member template in local class",
17641 &token->location);
17642 cp_parser_skip_to_end_of_block_or_statement (parser);
17643 return;
17644 }
17645 /* [temp]
17646
17647 A template ... shall not have C linkage. */
17648 if (current_lang_name == lang_name_c)
17649 {
17650 error ("%Htemplate with C linkage", &token->location);
17651 /* Give it C++ linkage to avoid confusing other parts of the
17652 front end. */
17653 push_lang_context (lang_name_cplusplus);
17654 need_lang_pop = true;
17655 }
17656 else
17657 need_lang_pop = false;
17658
17659 /* We cannot perform access checks on the template parameter
17660 declarations until we know what is being declared, just as we
17661 cannot check the decl-specifier list. */
17662 push_deferring_access_checks (dk_deferred);
17663
17664 /* If the next token is `>', then we have an invalid
17665 specialization. Rather than complain about an invalid template
17666 parameter, issue an error message here. */
17667 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17668 {
17669 cp_parser_error (parser, "invalid explicit specialization");
17670 begin_specialization ();
17671 parameter_list = NULL_TREE;
17672 }
17673 else
17674 /* Parse the template parameters. */
17675 parameter_list = cp_parser_template_parameter_list (parser);
17676
17677 /* Get the deferred access checks from the parameter list. These
17678 will be checked once we know what is being declared, as for a
17679 member template the checks must be performed in the scope of the
17680 class containing the member. */
17681 checks = get_deferred_access_checks ();
17682
17683 /* Look for the `>'. */
17684 cp_parser_skip_to_end_of_template_parameter_list (parser);
17685 /* We just processed one more parameter list. */
17686 ++parser->num_template_parameter_lists;
17687 /* If the next token is `template', there are more template
17688 parameters. */
17689 if (cp_lexer_next_token_is_keyword (parser->lexer,
17690 RID_TEMPLATE))
17691 cp_parser_template_declaration_after_export (parser, member_p);
17692 else
17693 {
17694 /* There are no access checks when parsing a template, as we do not
17695 know if a specialization will be a friend. */
17696 push_deferring_access_checks (dk_no_check);
17697 token = cp_lexer_peek_token (parser->lexer);
17698 decl = cp_parser_single_declaration (parser,
17699 checks,
17700 member_p,
17701 /*explicit_specialization_p=*/false,
17702 &friend_p);
17703 pop_deferring_access_checks ();
17704
17705 /* If this is a member template declaration, let the front
17706 end know. */
17707 if (member_p && !friend_p && decl)
17708 {
17709 if (TREE_CODE (decl) == TYPE_DECL)
17710 cp_parser_check_access_in_redeclaration (decl, token->location);
17711
17712 decl = finish_member_template_decl (decl);
17713 }
17714 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17715 make_friend_class (current_class_type, TREE_TYPE (decl),
17716 /*complain=*/true);
17717 }
17718 /* We are done with the current parameter list. */
17719 --parser->num_template_parameter_lists;
17720
17721 pop_deferring_access_checks ();
17722
17723 /* Finish up. */
17724 finish_template_decl (parameter_list);
17725
17726 /* Register member declarations. */
17727 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17728 finish_member_declaration (decl);
17729 /* For the erroneous case of a template with C linkage, we pushed an
17730 implicit C++ linkage scope; exit that scope now. */
17731 if (need_lang_pop)
17732 pop_lang_context ();
17733 /* If DECL is a function template, we must return to parse it later.
17734 (Even though there is no definition, there might be default
17735 arguments that need handling.) */
17736 if (member_p && decl
17737 && (TREE_CODE (decl) == FUNCTION_DECL
17738 || DECL_FUNCTION_TEMPLATE_P (decl)))
17739 TREE_VALUE (parser->unparsed_functions_queues)
17740 = tree_cons (NULL_TREE, decl,
17741 TREE_VALUE (parser->unparsed_functions_queues));
17742 }
17743
17744 /* Perform the deferred access checks from a template-parameter-list.
17745 CHECKS is a TREE_LIST of access checks, as returned by
17746 get_deferred_access_checks. */
17747
17748 static void
17749 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17750 {
17751 ++processing_template_parmlist;
17752 perform_access_checks (checks);
17753 --processing_template_parmlist;
17754 }
17755
17756 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17757 `function-definition' sequence. MEMBER_P is true, this declaration
17758 appears in a class scope.
17759
17760 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17761 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17762
17763 static tree
17764 cp_parser_single_declaration (cp_parser* parser,
17765 VEC (deferred_access_check,gc)* checks,
17766 bool member_p,
17767 bool explicit_specialization_p,
17768 bool* friend_p)
17769 {
17770 int declares_class_or_enum;
17771 tree decl = NULL_TREE;
17772 cp_decl_specifier_seq decl_specifiers;
17773 bool function_definition_p = false;
17774 cp_token *decl_spec_token_start;
17775
17776 /* This function is only used when processing a template
17777 declaration. */
17778 gcc_assert (innermost_scope_kind () == sk_template_parms
17779 || innermost_scope_kind () == sk_template_spec);
17780
17781 /* Defer access checks until we know what is being declared. */
17782 push_deferring_access_checks (dk_deferred);
17783
17784 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17785 alternative. */
17786 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17787 cp_parser_decl_specifier_seq (parser,
17788 CP_PARSER_FLAGS_OPTIONAL,
17789 &decl_specifiers,
17790 &declares_class_or_enum);
17791 if (friend_p)
17792 *friend_p = cp_parser_friend_p (&decl_specifiers);
17793
17794 /* There are no template typedefs. */
17795 if (decl_specifiers.specs[(int) ds_typedef])
17796 {
17797 error ("%Htemplate declaration of %qs",
17798 &decl_spec_token_start->location, "typedef");
17799 decl = error_mark_node;
17800 }
17801
17802 /* Gather up the access checks that occurred the
17803 decl-specifier-seq. */
17804 stop_deferring_access_checks ();
17805
17806 /* Check for the declaration of a template class. */
17807 if (declares_class_or_enum)
17808 {
17809 if (cp_parser_declares_only_class_p (parser))
17810 {
17811 decl = shadow_tag (&decl_specifiers);
17812
17813 /* In this case:
17814
17815 struct C {
17816 friend template <typename T> struct A<T>::B;
17817 };
17818
17819 A<T>::B will be represented by a TYPENAME_TYPE, and
17820 therefore not recognized by shadow_tag. */
17821 if (friend_p && *friend_p
17822 && !decl
17823 && decl_specifiers.type
17824 && TYPE_P (decl_specifiers.type))
17825 decl = decl_specifiers.type;
17826
17827 if (decl && decl != error_mark_node)
17828 decl = TYPE_NAME (decl);
17829 else
17830 decl = error_mark_node;
17831
17832 /* Perform access checks for template parameters. */
17833 cp_parser_perform_template_parameter_access_checks (checks);
17834 }
17835 }
17836 /* If it's not a template class, try for a template function. If
17837 the next token is a `;', then this declaration does not declare
17838 anything. But, if there were errors in the decl-specifiers, then
17839 the error might well have come from an attempted class-specifier.
17840 In that case, there's no need to warn about a missing declarator. */
17841 if (!decl
17842 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17843 || decl_specifiers.type != error_mark_node))
17844 {
17845 decl = cp_parser_init_declarator (parser,
17846 &decl_specifiers,
17847 checks,
17848 /*function_definition_allowed_p=*/true,
17849 member_p,
17850 declares_class_or_enum,
17851 &function_definition_p);
17852
17853 /* 7.1.1-1 [dcl.stc]
17854
17855 A storage-class-specifier shall not be specified in an explicit
17856 specialization... */
17857 if (decl
17858 && explicit_specialization_p
17859 && decl_specifiers.storage_class != sc_none)
17860 {
17861 error ("%Hexplicit template specialization cannot have a storage class",
17862 &decl_spec_token_start->location);
17863 decl = error_mark_node;
17864 }
17865 }
17866
17867 pop_deferring_access_checks ();
17868
17869 /* Clear any current qualification; whatever comes next is the start
17870 of something new. */
17871 parser->scope = NULL_TREE;
17872 parser->qualifying_scope = NULL_TREE;
17873 parser->object_scope = NULL_TREE;
17874 /* Look for a trailing `;' after the declaration. */
17875 if (!function_definition_p
17876 && (decl == error_mark_node
17877 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17878 cp_parser_skip_to_end_of_block_or_statement (parser);
17879
17880 return decl;
17881 }
17882
17883 /* Parse a cast-expression that is not the operand of a unary "&". */
17884
17885 static tree
17886 cp_parser_simple_cast_expression (cp_parser *parser)
17887 {
17888 return cp_parser_cast_expression (parser, /*address_p=*/false,
17889 /*cast_p=*/false, NULL);
17890 }
17891
17892 /* Parse a functional cast to TYPE. Returns an expression
17893 representing the cast. */
17894
17895 static tree
17896 cp_parser_functional_cast (cp_parser* parser, tree type)
17897 {
17898 tree expression_list;
17899 tree cast;
17900 bool nonconst_p;
17901
17902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17903 {
17904 maybe_warn_cpp0x ("extended initializer lists");
17905 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17906 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17907 if (TREE_CODE (type) == TYPE_DECL)
17908 type = TREE_TYPE (type);
17909 return finish_compound_literal (type, expression_list);
17910 }
17911
17912 expression_list
17913 = cp_parser_parenthesized_expression_list (parser, false,
17914 /*cast_p=*/true,
17915 /*allow_expansion_p=*/true,
17916 /*non_constant_p=*/NULL);
17917
17918 cast = build_functional_cast (type, expression_list,
17919 tf_warning_or_error);
17920 /* [expr.const]/1: In an integral constant expression "only type
17921 conversions to integral or enumeration type can be used". */
17922 if (TREE_CODE (type) == TYPE_DECL)
17923 type = TREE_TYPE (type);
17924 if (cast != error_mark_node
17925 && !cast_valid_in_integral_constant_expression_p (type)
17926 && (cp_parser_non_integral_constant_expression
17927 (parser, "a call to a constructor")))
17928 return error_mark_node;
17929 return cast;
17930 }
17931
17932 /* Save the tokens that make up the body of a member function defined
17933 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17934 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17935 specifiers applied to the declaration. Returns the FUNCTION_DECL
17936 for the member function. */
17937
17938 static tree
17939 cp_parser_save_member_function_body (cp_parser* parser,
17940 cp_decl_specifier_seq *decl_specifiers,
17941 cp_declarator *declarator,
17942 tree attributes)
17943 {
17944 cp_token *first;
17945 cp_token *last;
17946 tree fn;
17947
17948 /* Create the function-declaration. */
17949 fn = start_method (decl_specifiers, declarator, attributes);
17950 /* If something went badly wrong, bail out now. */
17951 if (fn == error_mark_node)
17952 {
17953 /* If there's a function-body, skip it. */
17954 if (cp_parser_token_starts_function_definition_p
17955 (cp_lexer_peek_token (parser->lexer)))
17956 cp_parser_skip_to_end_of_block_or_statement (parser);
17957 return error_mark_node;
17958 }
17959
17960 /* Remember it, if there default args to post process. */
17961 cp_parser_save_default_args (parser, fn);
17962
17963 /* Save away the tokens that make up the body of the
17964 function. */
17965 first = parser->lexer->next_token;
17966 /* We can have braced-init-list mem-initializers before the fn body. */
17967 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17968 {
17969 cp_lexer_consume_token (parser->lexer);
17970 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17971 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17972 {
17973 /* cache_group will stop after an un-nested { } pair, too. */
17974 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17975 break;
17976
17977 /* variadic mem-inits have ... after the ')'. */
17978 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17979 cp_lexer_consume_token (parser->lexer);
17980 }
17981 }
17982 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17983 /* Handle function try blocks. */
17984 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17985 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17986 last = parser->lexer->next_token;
17987
17988 /* Save away the inline definition; we will process it when the
17989 class is complete. */
17990 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17991 DECL_PENDING_INLINE_P (fn) = 1;
17992
17993 /* We need to know that this was defined in the class, so that
17994 friend templates are handled correctly. */
17995 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17996
17997 /* We're done with the inline definition. */
17998 finish_method (fn);
17999
18000 /* Add FN to the queue of functions to be parsed later. */
18001 TREE_VALUE (parser->unparsed_functions_queues)
18002 = tree_cons (NULL_TREE, fn,
18003 TREE_VALUE (parser->unparsed_functions_queues));
18004
18005 return fn;
18006 }
18007
18008 /* Parse a template-argument-list, as well as the trailing ">" (but
18009 not the opening ">"). See cp_parser_template_argument_list for the
18010 return value. */
18011
18012 static tree
18013 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18014 {
18015 tree arguments;
18016 tree saved_scope;
18017 tree saved_qualifying_scope;
18018 tree saved_object_scope;
18019 bool saved_greater_than_is_operator_p;
18020 bool saved_skip_evaluation;
18021
18022 /* [temp.names]
18023
18024 When parsing a template-id, the first non-nested `>' is taken as
18025 the end of the template-argument-list rather than a greater-than
18026 operator. */
18027 saved_greater_than_is_operator_p
18028 = parser->greater_than_is_operator_p;
18029 parser->greater_than_is_operator_p = false;
18030 /* Parsing the argument list may modify SCOPE, so we save it
18031 here. */
18032 saved_scope = parser->scope;
18033 saved_qualifying_scope = parser->qualifying_scope;
18034 saved_object_scope = parser->object_scope;
18035 /* We need to evaluate the template arguments, even though this
18036 template-id may be nested within a "sizeof". */
18037 saved_skip_evaluation = skip_evaluation;
18038 skip_evaluation = false;
18039 /* Parse the template-argument-list itself. */
18040 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18041 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18042 arguments = NULL_TREE;
18043 else
18044 arguments = cp_parser_template_argument_list (parser);
18045 /* Look for the `>' that ends the template-argument-list. If we find
18046 a '>>' instead, it's probably just a typo. */
18047 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18048 {
18049 if (cxx_dialect != cxx98)
18050 {
18051 /* In C++0x, a `>>' in a template argument list or cast
18052 expression is considered to be two separate `>'
18053 tokens. So, change the current token to a `>', but don't
18054 consume it: it will be consumed later when the outer
18055 template argument list (or cast expression) is parsed.
18056 Note that this replacement of `>' for `>>' is necessary
18057 even if we are parsing tentatively: in the tentative
18058 case, after calling
18059 cp_parser_enclosed_template_argument_list we will always
18060 throw away all of the template arguments and the first
18061 closing `>', either because the template argument list
18062 was erroneous or because we are replacing those tokens
18063 with a CPP_TEMPLATE_ID token. The second `>' (which will
18064 not have been thrown away) is needed either to close an
18065 outer template argument list or to complete a new-style
18066 cast. */
18067 cp_token *token = cp_lexer_peek_token (parser->lexer);
18068 token->type = CPP_GREATER;
18069 }
18070 else if (!saved_greater_than_is_operator_p)
18071 {
18072 /* If we're in a nested template argument list, the '>>' has
18073 to be a typo for '> >'. We emit the error message, but we
18074 continue parsing and we push a '>' as next token, so that
18075 the argument list will be parsed correctly. Note that the
18076 global source location is still on the token before the
18077 '>>', so we need to say explicitly where we want it. */
18078 cp_token *token = cp_lexer_peek_token (parser->lexer);
18079 error ("%H%<>>%> should be %<> >%> "
18080 "within a nested template argument list",
18081 &token->location);
18082
18083 token->type = CPP_GREATER;
18084 }
18085 else
18086 {
18087 /* If this is not a nested template argument list, the '>>'
18088 is a typo for '>'. Emit an error message and continue.
18089 Same deal about the token location, but here we can get it
18090 right by consuming the '>>' before issuing the diagnostic. */
18091 cp_token *token = cp_lexer_consume_token (parser->lexer);
18092 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18093 "a template argument list", &token->location);
18094 }
18095 }
18096 else
18097 cp_parser_skip_to_end_of_template_parameter_list (parser);
18098 /* The `>' token might be a greater-than operator again now. */
18099 parser->greater_than_is_operator_p
18100 = saved_greater_than_is_operator_p;
18101 /* Restore the SAVED_SCOPE. */
18102 parser->scope = saved_scope;
18103 parser->qualifying_scope = saved_qualifying_scope;
18104 parser->object_scope = saved_object_scope;
18105 skip_evaluation = saved_skip_evaluation;
18106
18107 return arguments;
18108 }
18109
18110 /* MEMBER_FUNCTION is a member function, or a friend. If default
18111 arguments, or the body of the function have not yet been parsed,
18112 parse them now. */
18113
18114 static void
18115 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18116 {
18117 /* If this member is a template, get the underlying
18118 FUNCTION_DECL. */
18119 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18120 member_function = DECL_TEMPLATE_RESULT (member_function);
18121
18122 /* There should not be any class definitions in progress at this
18123 point; the bodies of members are only parsed outside of all class
18124 definitions. */
18125 gcc_assert (parser->num_classes_being_defined == 0);
18126 /* While we're parsing the member functions we might encounter more
18127 classes. We want to handle them right away, but we don't want
18128 them getting mixed up with functions that are currently in the
18129 queue. */
18130 parser->unparsed_functions_queues
18131 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18132
18133 /* Make sure that any template parameters are in scope. */
18134 maybe_begin_member_template_processing (member_function);
18135
18136 /* If the body of the function has not yet been parsed, parse it
18137 now. */
18138 if (DECL_PENDING_INLINE_P (member_function))
18139 {
18140 tree function_scope;
18141 cp_token_cache *tokens;
18142
18143 /* The function is no longer pending; we are processing it. */
18144 tokens = DECL_PENDING_INLINE_INFO (member_function);
18145 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18146 DECL_PENDING_INLINE_P (member_function) = 0;
18147
18148 /* If this is a local class, enter the scope of the containing
18149 function. */
18150 function_scope = current_function_decl;
18151 if (function_scope)
18152 push_function_context ();
18153
18154 /* Push the body of the function onto the lexer stack. */
18155 cp_parser_push_lexer_for_tokens (parser, tokens);
18156
18157 /* Let the front end know that we going to be defining this
18158 function. */
18159 start_preparsed_function (member_function, NULL_TREE,
18160 SF_PRE_PARSED | SF_INCLASS_INLINE);
18161
18162 /* Don't do access checking if it is a templated function. */
18163 if (processing_template_decl)
18164 push_deferring_access_checks (dk_no_check);
18165
18166 /* Now, parse the body of the function. */
18167 cp_parser_function_definition_after_declarator (parser,
18168 /*inline_p=*/true);
18169
18170 if (processing_template_decl)
18171 pop_deferring_access_checks ();
18172
18173 /* Leave the scope of the containing function. */
18174 if (function_scope)
18175 pop_function_context ();
18176 cp_parser_pop_lexer (parser);
18177 }
18178
18179 /* Remove any template parameters from the symbol table. */
18180 maybe_end_member_template_processing ();
18181
18182 /* Restore the queue. */
18183 parser->unparsed_functions_queues
18184 = TREE_CHAIN (parser->unparsed_functions_queues);
18185 }
18186
18187 /* If DECL contains any default args, remember it on the unparsed
18188 functions queue. */
18189
18190 static void
18191 cp_parser_save_default_args (cp_parser* parser, tree decl)
18192 {
18193 tree probe;
18194
18195 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18196 probe;
18197 probe = TREE_CHAIN (probe))
18198 if (TREE_PURPOSE (probe))
18199 {
18200 TREE_PURPOSE (parser->unparsed_functions_queues)
18201 = tree_cons (current_class_type, decl,
18202 TREE_PURPOSE (parser->unparsed_functions_queues));
18203 break;
18204 }
18205 }
18206
18207 /* FN is a FUNCTION_DECL which may contains a parameter with an
18208 unparsed DEFAULT_ARG. Parse the default args now. This function
18209 assumes that the current scope is the scope in which the default
18210 argument should be processed. */
18211
18212 static void
18213 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18214 {
18215 bool saved_local_variables_forbidden_p;
18216 tree parm;
18217
18218 /* While we're parsing the default args, we might (due to the
18219 statement expression extension) encounter more classes. We want
18220 to handle them right away, but we don't want them getting mixed
18221 up with default args that are currently in the queue. */
18222 parser->unparsed_functions_queues
18223 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18224
18225 /* Local variable names (and the `this' keyword) may not appear
18226 in a default argument. */
18227 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18228 parser->local_variables_forbidden_p = true;
18229
18230 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18231 parm;
18232 parm = TREE_CHAIN (parm))
18233 {
18234 cp_token_cache *tokens;
18235 tree default_arg = TREE_PURPOSE (parm);
18236 tree parsed_arg;
18237 VEC(tree,gc) *insts;
18238 tree copy;
18239 unsigned ix;
18240
18241 if (!default_arg)
18242 continue;
18243
18244 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18245 /* This can happen for a friend declaration for a function
18246 already declared with default arguments. */
18247 continue;
18248
18249 /* Push the saved tokens for the default argument onto the parser's
18250 lexer stack. */
18251 tokens = DEFARG_TOKENS (default_arg);
18252 cp_parser_push_lexer_for_tokens (parser, tokens);
18253
18254 /* Parse the assignment-expression. */
18255 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18256
18257 if (!processing_template_decl)
18258 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18259
18260 TREE_PURPOSE (parm) = parsed_arg;
18261
18262 /* Update any instantiations we've already created. */
18263 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18264 VEC_iterate (tree, insts, ix, copy); ix++)
18265 TREE_PURPOSE (copy) = parsed_arg;
18266
18267 /* If the token stream has not been completely used up, then
18268 there was extra junk after the end of the default
18269 argument. */
18270 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18271 cp_parser_error (parser, "expected %<,%>");
18272
18273 /* Revert to the main lexer. */
18274 cp_parser_pop_lexer (parser);
18275 }
18276
18277 /* Make sure no default arg is missing. */
18278 check_default_args (fn);
18279
18280 /* Restore the state of local_variables_forbidden_p. */
18281 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18282
18283 /* Restore the queue. */
18284 parser->unparsed_functions_queues
18285 = TREE_CHAIN (parser->unparsed_functions_queues);
18286 }
18287
18288 /* Parse the operand of `sizeof' (or a similar operator). Returns
18289 either a TYPE or an expression, depending on the form of the
18290 input. The KEYWORD indicates which kind of expression we have
18291 encountered. */
18292
18293 static tree
18294 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18295 {
18296 tree expr = NULL_TREE;
18297 const char *saved_message;
18298 char *tmp;
18299 bool saved_integral_constant_expression_p;
18300 bool saved_non_integral_constant_expression_p;
18301 bool pack_expansion_p = false;
18302
18303 /* Types cannot be defined in a `sizeof' expression. Save away the
18304 old message. */
18305 saved_message = parser->type_definition_forbidden_message;
18306 /* And create the new one. */
18307 tmp = concat ("types may not be defined in %<",
18308 IDENTIFIER_POINTER (ridpointers[keyword]),
18309 "%> expressions", NULL);
18310 parser->type_definition_forbidden_message = tmp;
18311
18312 /* The restrictions on constant-expressions do not apply inside
18313 sizeof expressions. */
18314 saved_integral_constant_expression_p
18315 = parser->integral_constant_expression_p;
18316 saved_non_integral_constant_expression_p
18317 = parser->non_integral_constant_expression_p;
18318 parser->integral_constant_expression_p = false;
18319
18320 /* If it's a `...', then we are computing the length of a parameter
18321 pack. */
18322 if (keyword == RID_SIZEOF
18323 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18324 {
18325 /* Consume the `...'. */
18326 cp_lexer_consume_token (parser->lexer);
18327 maybe_warn_variadic_templates ();
18328
18329 /* Note that this is an expansion. */
18330 pack_expansion_p = true;
18331 }
18332
18333 /* Do not actually evaluate the expression. */
18334 ++skip_evaluation;
18335 /* If it's a `(', then we might be looking at the type-id
18336 construction. */
18337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18338 {
18339 tree type;
18340 bool saved_in_type_id_in_expr_p;
18341
18342 /* We can't be sure yet whether we're looking at a type-id or an
18343 expression. */
18344 cp_parser_parse_tentatively (parser);
18345 /* Consume the `('. */
18346 cp_lexer_consume_token (parser->lexer);
18347 /* Parse the type-id. */
18348 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18349 parser->in_type_id_in_expr_p = true;
18350 type = cp_parser_type_id (parser);
18351 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18352 /* Now, look for the trailing `)'. */
18353 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18354 /* If all went well, then we're done. */
18355 if (cp_parser_parse_definitely (parser))
18356 {
18357 cp_decl_specifier_seq decl_specs;
18358
18359 /* Build a trivial decl-specifier-seq. */
18360 clear_decl_specs (&decl_specs);
18361 decl_specs.type = type;
18362
18363 /* Call grokdeclarator to figure out what type this is. */
18364 expr = grokdeclarator (NULL,
18365 &decl_specs,
18366 TYPENAME,
18367 /*initialized=*/0,
18368 /*attrlist=*/NULL);
18369 }
18370 }
18371
18372 /* If the type-id production did not work out, then we must be
18373 looking at the unary-expression production. */
18374 if (!expr)
18375 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18376 /*cast_p=*/false, NULL);
18377
18378 if (pack_expansion_p)
18379 /* Build a pack expansion. */
18380 expr = make_pack_expansion (expr);
18381
18382 /* Go back to evaluating expressions. */
18383 --skip_evaluation;
18384
18385 /* Free the message we created. */
18386 free (tmp);
18387 /* And restore the old one. */
18388 parser->type_definition_forbidden_message = saved_message;
18389 parser->integral_constant_expression_p
18390 = saved_integral_constant_expression_p;
18391 parser->non_integral_constant_expression_p
18392 = saved_non_integral_constant_expression_p;
18393
18394 return expr;
18395 }
18396
18397 /* If the current declaration has no declarator, return true. */
18398
18399 static bool
18400 cp_parser_declares_only_class_p (cp_parser *parser)
18401 {
18402 /* If the next token is a `;' or a `,' then there is no
18403 declarator. */
18404 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18405 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18406 }
18407
18408 /* Update the DECL_SPECS to reflect the storage class indicated by
18409 KEYWORD. */
18410
18411 static void
18412 cp_parser_set_storage_class (cp_parser *parser,
18413 cp_decl_specifier_seq *decl_specs,
18414 enum rid keyword,
18415 location_t location)
18416 {
18417 cp_storage_class storage_class;
18418
18419 if (parser->in_unbraced_linkage_specification_p)
18420 {
18421 error ("%Hinvalid use of %qD in linkage specification",
18422 &location, ridpointers[keyword]);
18423 return;
18424 }
18425 else if (decl_specs->storage_class != sc_none)
18426 {
18427 decl_specs->conflicting_specifiers_p = true;
18428 return;
18429 }
18430
18431 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18432 && decl_specs->specs[(int) ds_thread])
18433 {
18434 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18435 decl_specs->specs[(int) ds_thread] = 0;
18436 }
18437
18438 switch (keyword)
18439 {
18440 case RID_AUTO:
18441 storage_class = sc_auto;
18442 break;
18443 case RID_REGISTER:
18444 storage_class = sc_register;
18445 break;
18446 case RID_STATIC:
18447 storage_class = sc_static;
18448 break;
18449 case RID_EXTERN:
18450 storage_class = sc_extern;
18451 break;
18452 case RID_MUTABLE:
18453 storage_class = sc_mutable;
18454 break;
18455 default:
18456 gcc_unreachable ();
18457 }
18458 decl_specs->storage_class = storage_class;
18459
18460 /* A storage class specifier cannot be applied alongside a typedef
18461 specifier. If there is a typedef specifier present then set
18462 conflicting_specifiers_p which will trigger an error later
18463 on in grokdeclarator. */
18464 if (decl_specs->specs[(int)ds_typedef])
18465 decl_specs->conflicting_specifiers_p = true;
18466 }
18467
18468 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18469 is true, the type is a user-defined type; otherwise it is a
18470 built-in type specified by a keyword. */
18471
18472 static void
18473 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18474 tree type_spec,
18475 location_t location,
18476 bool user_defined_p)
18477 {
18478 decl_specs->any_specifiers_p = true;
18479
18480 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18481 (with, for example, in "typedef int wchar_t;") we remember that
18482 this is what happened. In system headers, we ignore these
18483 declarations so that G++ can work with system headers that are not
18484 C++-safe. */
18485 if (decl_specs->specs[(int) ds_typedef]
18486 && !user_defined_p
18487 && (type_spec == boolean_type_node
18488 || type_spec == char16_type_node
18489 || type_spec == char32_type_node
18490 || type_spec == wchar_type_node)
18491 && (decl_specs->type
18492 || decl_specs->specs[(int) ds_long]
18493 || decl_specs->specs[(int) ds_short]
18494 || decl_specs->specs[(int) ds_unsigned]
18495 || decl_specs->specs[(int) ds_signed]))
18496 {
18497 decl_specs->redefined_builtin_type = type_spec;
18498 if (!decl_specs->type)
18499 {
18500 decl_specs->type = type_spec;
18501 decl_specs->user_defined_type_p = false;
18502 decl_specs->type_location = location;
18503 }
18504 }
18505 else if (decl_specs->type)
18506 decl_specs->multiple_types_p = true;
18507 else
18508 {
18509 decl_specs->type = type_spec;
18510 decl_specs->user_defined_type_p = user_defined_p;
18511 decl_specs->redefined_builtin_type = NULL_TREE;
18512 decl_specs->type_location = location;
18513 }
18514 }
18515
18516 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18517 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18518
18519 static bool
18520 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18521 {
18522 return decl_specifiers->specs[(int) ds_friend] != 0;
18523 }
18524
18525 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18526 issue an error message indicating that TOKEN_DESC was expected.
18527
18528 Returns the token consumed, if the token had the appropriate type.
18529 Otherwise, returns NULL. */
18530
18531 static cp_token *
18532 cp_parser_require (cp_parser* parser,
18533 enum cpp_ttype type,
18534 const char* token_desc)
18535 {
18536 if (cp_lexer_next_token_is (parser->lexer, type))
18537 return cp_lexer_consume_token (parser->lexer);
18538 else
18539 {
18540 /* Output the MESSAGE -- unless we're parsing tentatively. */
18541 if (!cp_parser_simulate_error (parser))
18542 {
18543 char *message = concat ("expected ", token_desc, NULL);
18544 cp_parser_error (parser, message);
18545 free (message);
18546 }
18547 return NULL;
18548 }
18549 }
18550
18551 /* An error message is produced if the next token is not '>'.
18552 All further tokens are skipped until the desired token is
18553 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18554
18555 static void
18556 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18557 {
18558 /* Current level of '< ... >'. */
18559 unsigned level = 0;
18560 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18561 unsigned nesting_depth = 0;
18562
18563 /* Are we ready, yet? If not, issue error message. */
18564 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18565 return;
18566
18567 /* Skip tokens until the desired token is found. */
18568 while (true)
18569 {
18570 /* Peek at the next token. */
18571 switch (cp_lexer_peek_token (parser->lexer)->type)
18572 {
18573 case CPP_LESS:
18574 if (!nesting_depth)
18575 ++level;
18576 break;
18577
18578 case CPP_RSHIFT:
18579 if (cxx_dialect == cxx98)
18580 /* C++0x views the `>>' operator as two `>' tokens, but
18581 C++98 does not. */
18582 break;
18583 else if (!nesting_depth && level-- == 0)
18584 {
18585 /* We've hit a `>>' where the first `>' closes the
18586 template argument list, and the second `>' is
18587 spurious. Just consume the `>>' and stop; we've
18588 already produced at least one error. */
18589 cp_lexer_consume_token (parser->lexer);
18590 return;
18591 }
18592 /* Fall through for C++0x, so we handle the second `>' in
18593 the `>>'. */
18594
18595 case CPP_GREATER:
18596 if (!nesting_depth && level-- == 0)
18597 {
18598 /* We've reached the token we want, consume it and stop. */
18599 cp_lexer_consume_token (parser->lexer);
18600 return;
18601 }
18602 break;
18603
18604 case CPP_OPEN_PAREN:
18605 case CPP_OPEN_SQUARE:
18606 ++nesting_depth;
18607 break;
18608
18609 case CPP_CLOSE_PAREN:
18610 case CPP_CLOSE_SQUARE:
18611 if (nesting_depth-- == 0)
18612 return;
18613 break;
18614
18615 case CPP_EOF:
18616 case CPP_PRAGMA_EOL:
18617 case CPP_SEMICOLON:
18618 case CPP_OPEN_BRACE:
18619 case CPP_CLOSE_BRACE:
18620 /* The '>' was probably forgotten, don't look further. */
18621 return;
18622
18623 default:
18624 break;
18625 }
18626
18627 /* Consume this token. */
18628 cp_lexer_consume_token (parser->lexer);
18629 }
18630 }
18631
18632 /* If the next token is the indicated keyword, consume it. Otherwise,
18633 issue an error message indicating that TOKEN_DESC was expected.
18634
18635 Returns the token consumed, if the token had the appropriate type.
18636 Otherwise, returns NULL. */
18637
18638 static cp_token *
18639 cp_parser_require_keyword (cp_parser* parser,
18640 enum rid keyword,
18641 const char* token_desc)
18642 {
18643 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18644
18645 if (token && token->keyword != keyword)
18646 {
18647 dyn_string_t error_msg;
18648
18649 /* Format the error message. */
18650 error_msg = dyn_string_new (0);
18651 dyn_string_append_cstr (error_msg, "expected ");
18652 dyn_string_append_cstr (error_msg, token_desc);
18653 cp_parser_error (parser, error_msg->s);
18654 dyn_string_delete (error_msg);
18655 return NULL;
18656 }
18657
18658 return token;
18659 }
18660
18661 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18662 function-definition. */
18663
18664 static bool
18665 cp_parser_token_starts_function_definition_p (cp_token* token)
18666 {
18667 return (/* An ordinary function-body begins with an `{'. */
18668 token->type == CPP_OPEN_BRACE
18669 /* A ctor-initializer begins with a `:'. */
18670 || token->type == CPP_COLON
18671 /* A function-try-block begins with `try'. */
18672 || token->keyword == RID_TRY
18673 /* The named return value extension begins with `return'. */
18674 || token->keyword == RID_RETURN);
18675 }
18676
18677 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18678 definition. */
18679
18680 static bool
18681 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18682 {
18683 cp_token *token;
18684
18685 token = cp_lexer_peek_token (parser->lexer);
18686 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18687 }
18688
18689 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18690 C++0x) ending a template-argument. */
18691
18692 static bool
18693 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18694 {
18695 cp_token *token;
18696
18697 token = cp_lexer_peek_token (parser->lexer);
18698 return (token->type == CPP_COMMA
18699 || token->type == CPP_GREATER
18700 || token->type == CPP_ELLIPSIS
18701 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18702 }
18703
18704 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18705 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18706
18707 static bool
18708 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18709 size_t n)
18710 {
18711 cp_token *token;
18712
18713 token = cp_lexer_peek_nth_token (parser->lexer, n);
18714 if (token->type == CPP_LESS)
18715 return true;
18716 /* Check for the sequence `<::' in the original code. It would be lexed as
18717 `[:', where `[' is a digraph, and there is no whitespace before
18718 `:'. */
18719 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18720 {
18721 cp_token *token2;
18722 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18723 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18724 return true;
18725 }
18726 return false;
18727 }
18728
18729 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18730 or none_type otherwise. */
18731
18732 static enum tag_types
18733 cp_parser_token_is_class_key (cp_token* token)
18734 {
18735 switch (token->keyword)
18736 {
18737 case RID_CLASS:
18738 return class_type;
18739 case RID_STRUCT:
18740 return record_type;
18741 case RID_UNION:
18742 return union_type;
18743
18744 default:
18745 return none_type;
18746 }
18747 }
18748
18749 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18750
18751 static void
18752 cp_parser_check_class_key (enum tag_types class_key, tree type)
18753 {
18754 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18755 permerror (input_location, "%qs tag used in naming %q#T",
18756 class_key == union_type ? "union"
18757 : class_key == record_type ? "struct" : "class",
18758 type);
18759 }
18760
18761 /* Issue an error message if DECL is redeclared with different
18762 access than its original declaration [class.access.spec/3].
18763 This applies to nested classes and nested class templates.
18764 [class.mem/1]. */
18765
18766 static void
18767 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18768 {
18769 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18770 return;
18771
18772 if ((TREE_PRIVATE (decl)
18773 != (current_access_specifier == access_private_node))
18774 || (TREE_PROTECTED (decl)
18775 != (current_access_specifier == access_protected_node)))
18776 error ("%H%qD redeclared with different access", &location, decl);
18777 }
18778
18779 /* Look for the `template' keyword, as a syntactic disambiguator.
18780 Return TRUE iff it is present, in which case it will be
18781 consumed. */
18782
18783 static bool
18784 cp_parser_optional_template_keyword (cp_parser *parser)
18785 {
18786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18787 {
18788 /* The `template' keyword can only be used within templates;
18789 outside templates the parser can always figure out what is a
18790 template and what is not. */
18791 if (!processing_template_decl)
18792 {
18793 cp_token *token = cp_lexer_peek_token (parser->lexer);
18794 error ("%H%<template%> (as a disambiguator) is only allowed "
18795 "within templates", &token->location);
18796 /* If this part of the token stream is rescanned, the same
18797 error message would be generated. So, we purge the token
18798 from the stream. */
18799 cp_lexer_purge_token (parser->lexer);
18800 return false;
18801 }
18802 else
18803 {
18804 /* Consume the `template' keyword. */
18805 cp_lexer_consume_token (parser->lexer);
18806 return true;
18807 }
18808 }
18809
18810 return false;
18811 }
18812
18813 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18814 set PARSER->SCOPE, and perform other related actions. */
18815
18816 static void
18817 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18818 {
18819 int i;
18820 struct tree_check *check_value;
18821 deferred_access_check *chk;
18822 VEC (deferred_access_check,gc) *checks;
18823
18824 /* Get the stored value. */
18825 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18826 /* Perform any access checks that were deferred. */
18827 checks = check_value->checks;
18828 if (checks)
18829 {
18830 for (i = 0 ;
18831 VEC_iterate (deferred_access_check, checks, i, chk) ;
18832 ++i)
18833 {
18834 perform_or_defer_access_check (chk->binfo,
18835 chk->decl,
18836 chk->diag_decl);
18837 }
18838 }
18839 /* Set the scope from the stored value. */
18840 parser->scope = check_value->value;
18841 parser->qualifying_scope = check_value->qualifying_scope;
18842 parser->object_scope = NULL_TREE;
18843 }
18844
18845 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18846 encounter the end of a block before what we were looking for. */
18847
18848 static bool
18849 cp_parser_cache_group (cp_parser *parser,
18850 enum cpp_ttype end,
18851 unsigned depth)
18852 {
18853 while (true)
18854 {
18855 cp_token *token = cp_lexer_peek_token (parser->lexer);
18856
18857 /* Abort a parenthesized expression if we encounter a semicolon. */
18858 if ((end == CPP_CLOSE_PAREN || depth == 0)
18859 && token->type == CPP_SEMICOLON)
18860 return true;
18861 /* If we've reached the end of the file, stop. */
18862 if (token->type == CPP_EOF
18863 || (end != CPP_PRAGMA_EOL
18864 && token->type == CPP_PRAGMA_EOL))
18865 return true;
18866 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18867 /* We've hit the end of an enclosing block, so there's been some
18868 kind of syntax error. */
18869 return true;
18870
18871 /* Consume the token. */
18872 cp_lexer_consume_token (parser->lexer);
18873 /* See if it starts a new group. */
18874 if (token->type == CPP_OPEN_BRACE)
18875 {
18876 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18877 /* In theory this should probably check end == '}', but
18878 cp_parser_save_member_function_body needs it to exit
18879 after either '}' or ')' when called with ')'. */
18880 if (depth == 0)
18881 return false;
18882 }
18883 else if (token->type == CPP_OPEN_PAREN)
18884 {
18885 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18886 if (depth == 0 && end == CPP_CLOSE_PAREN)
18887 return false;
18888 }
18889 else if (token->type == CPP_PRAGMA)
18890 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18891 else if (token->type == end)
18892 return false;
18893 }
18894 }
18895
18896 /* Begin parsing tentatively. We always save tokens while parsing
18897 tentatively so that if the tentative parsing fails we can restore the
18898 tokens. */
18899
18900 static void
18901 cp_parser_parse_tentatively (cp_parser* parser)
18902 {
18903 /* Enter a new parsing context. */
18904 parser->context = cp_parser_context_new (parser->context);
18905 /* Begin saving tokens. */
18906 cp_lexer_save_tokens (parser->lexer);
18907 /* In order to avoid repetitive access control error messages,
18908 access checks are queued up until we are no longer parsing
18909 tentatively. */
18910 push_deferring_access_checks (dk_deferred);
18911 }
18912
18913 /* Commit to the currently active tentative parse. */
18914
18915 static void
18916 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18917 {
18918 cp_parser_context *context;
18919 cp_lexer *lexer;
18920
18921 /* Mark all of the levels as committed. */
18922 lexer = parser->lexer;
18923 for (context = parser->context; context->next; context = context->next)
18924 {
18925 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18926 break;
18927 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18928 while (!cp_lexer_saving_tokens (lexer))
18929 lexer = lexer->next;
18930 cp_lexer_commit_tokens (lexer);
18931 }
18932 }
18933
18934 /* Abort the currently active tentative parse. All consumed tokens
18935 will be rolled back, and no diagnostics will be issued. */
18936
18937 static void
18938 cp_parser_abort_tentative_parse (cp_parser* parser)
18939 {
18940 cp_parser_simulate_error (parser);
18941 /* Now, pretend that we want to see if the construct was
18942 successfully parsed. */
18943 cp_parser_parse_definitely (parser);
18944 }
18945
18946 /* Stop parsing tentatively. If a parse error has occurred, restore the
18947 token stream. Otherwise, commit to the tokens we have consumed.
18948 Returns true if no error occurred; false otherwise. */
18949
18950 static bool
18951 cp_parser_parse_definitely (cp_parser* parser)
18952 {
18953 bool error_occurred;
18954 cp_parser_context *context;
18955
18956 /* Remember whether or not an error occurred, since we are about to
18957 destroy that information. */
18958 error_occurred = cp_parser_error_occurred (parser);
18959 /* Remove the topmost context from the stack. */
18960 context = parser->context;
18961 parser->context = context->next;
18962 /* If no parse errors occurred, commit to the tentative parse. */
18963 if (!error_occurred)
18964 {
18965 /* Commit to the tokens read tentatively, unless that was
18966 already done. */
18967 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18968 cp_lexer_commit_tokens (parser->lexer);
18969
18970 pop_to_parent_deferring_access_checks ();
18971 }
18972 /* Otherwise, if errors occurred, roll back our state so that things
18973 are just as they were before we began the tentative parse. */
18974 else
18975 {
18976 cp_lexer_rollback_tokens (parser->lexer);
18977 pop_deferring_access_checks ();
18978 }
18979 /* Add the context to the front of the free list. */
18980 context->next = cp_parser_context_free_list;
18981 cp_parser_context_free_list = context;
18982
18983 return !error_occurred;
18984 }
18985
18986 /* Returns true if we are parsing tentatively and are not committed to
18987 this tentative parse. */
18988
18989 static bool
18990 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18991 {
18992 return (cp_parser_parsing_tentatively (parser)
18993 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18994 }
18995
18996 /* Returns nonzero iff an error has occurred during the most recent
18997 tentative parse. */
18998
18999 static bool
19000 cp_parser_error_occurred (cp_parser* parser)
19001 {
19002 return (cp_parser_parsing_tentatively (parser)
19003 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19004 }
19005
19006 /* Returns nonzero if GNU extensions are allowed. */
19007
19008 static bool
19009 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19010 {
19011 return parser->allow_gnu_extensions_p;
19012 }
19013 \f
19014 /* Objective-C++ Productions */
19015
19016
19017 /* Parse an Objective-C expression, which feeds into a primary-expression
19018 above.
19019
19020 objc-expression:
19021 objc-message-expression
19022 objc-string-literal
19023 objc-encode-expression
19024 objc-protocol-expression
19025 objc-selector-expression
19026
19027 Returns a tree representation of the expression. */
19028
19029 static tree
19030 cp_parser_objc_expression (cp_parser* parser)
19031 {
19032 /* Try to figure out what kind of declaration is present. */
19033 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19034
19035 switch (kwd->type)
19036 {
19037 case CPP_OPEN_SQUARE:
19038 return cp_parser_objc_message_expression (parser);
19039
19040 case CPP_OBJC_STRING:
19041 kwd = cp_lexer_consume_token (parser->lexer);
19042 return objc_build_string_object (kwd->u.value);
19043
19044 case CPP_KEYWORD:
19045 switch (kwd->keyword)
19046 {
19047 case RID_AT_ENCODE:
19048 return cp_parser_objc_encode_expression (parser);
19049
19050 case RID_AT_PROTOCOL:
19051 return cp_parser_objc_protocol_expression (parser);
19052
19053 case RID_AT_SELECTOR:
19054 return cp_parser_objc_selector_expression (parser);
19055
19056 default:
19057 break;
19058 }
19059 default:
19060 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19061 &kwd->location, kwd->u.value);
19062 cp_parser_skip_to_end_of_block_or_statement (parser);
19063 }
19064
19065 return error_mark_node;
19066 }
19067
19068 /* Parse an Objective-C message expression.
19069
19070 objc-message-expression:
19071 [ objc-message-receiver objc-message-args ]
19072
19073 Returns a representation of an Objective-C message. */
19074
19075 static tree
19076 cp_parser_objc_message_expression (cp_parser* parser)
19077 {
19078 tree receiver, messageargs;
19079
19080 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19081 receiver = cp_parser_objc_message_receiver (parser);
19082 messageargs = cp_parser_objc_message_args (parser);
19083 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19084
19085 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19086 }
19087
19088 /* Parse an objc-message-receiver.
19089
19090 objc-message-receiver:
19091 expression
19092 simple-type-specifier
19093
19094 Returns a representation of the type or expression. */
19095
19096 static tree
19097 cp_parser_objc_message_receiver (cp_parser* parser)
19098 {
19099 tree rcv;
19100
19101 /* An Objective-C message receiver may be either (1) a type
19102 or (2) an expression. */
19103 cp_parser_parse_tentatively (parser);
19104 rcv = cp_parser_expression (parser, false, NULL);
19105
19106 if (cp_parser_parse_definitely (parser))
19107 return rcv;
19108
19109 rcv = cp_parser_simple_type_specifier (parser,
19110 /*decl_specs=*/NULL,
19111 CP_PARSER_FLAGS_NONE);
19112
19113 return objc_get_class_reference (rcv);
19114 }
19115
19116 /* Parse the arguments and selectors comprising an Objective-C message.
19117
19118 objc-message-args:
19119 objc-selector
19120 objc-selector-args
19121 objc-selector-args , objc-comma-args
19122
19123 objc-selector-args:
19124 objc-selector [opt] : assignment-expression
19125 objc-selector-args objc-selector [opt] : assignment-expression
19126
19127 objc-comma-args:
19128 assignment-expression
19129 objc-comma-args , assignment-expression
19130
19131 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19132 selector arguments and TREE_VALUE containing a list of comma
19133 arguments. */
19134
19135 static tree
19136 cp_parser_objc_message_args (cp_parser* parser)
19137 {
19138 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19139 bool maybe_unary_selector_p = true;
19140 cp_token *token = cp_lexer_peek_token (parser->lexer);
19141
19142 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19143 {
19144 tree selector = NULL_TREE, arg;
19145
19146 if (token->type != CPP_COLON)
19147 selector = cp_parser_objc_selector (parser);
19148
19149 /* Detect if we have a unary selector. */
19150 if (maybe_unary_selector_p
19151 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19152 return build_tree_list (selector, NULL_TREE);
19153
19154 maybe_unary_selector_p = false;
19155 cp_parser_require (parser, CPP_COLON, "%<:%>");
19156 arg = cp_parser_assignment_expression (parser, false, NULL);
19157
19158 sel_args
19159 = chainon (sel_args,
19160 build_tree_list (selector, arg));
19161
19162 token = cp_lexer_peek_token (parser->lexer);
19163 }
19164
19165 /* Handle non-selector arguments, if any. */
19166 while (token->type == CPP_COMMA)
19167 {
19168 tree arg;
19169
19170 cp_lexer_consume_token (parser->lexer);
19171 arg = cp_parser_assignment_expression (parser, false, NULL);
19172
19173 addl_args
19174 = chainon (addl_args,
19175 build_tree_list (NULL_TREE, arg));
19176
19177 token = cp_lexer_peek_token (parser->lexer);
19178 }
19179
19180 return build_tree_list (sel_args, addl_args);
19181 }
19182
19183 /* Parse an Objective-C encode expression.
19184
19185 objc-encode-expression:
19186 @encode objc-typename
19187
19188 Returns an encoded representation of the type argument. */
19189
19190 static tree
19191 cp_parser_objc_encode_expression (cp_parser* parser)
19192 {
19193 tree type;
19194 cp_token *token;
19195
19196 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19197 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19198 token = cp_lexer_peek_token (parser->lexer);
19199 type = complete_type (cp_parser_type_id (parser));
19200 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19201
19202 if (!type)
19203 {
19204 error ("%H%<@encode%> must specify a type as an argument",
19205 &token->location);
19206 return error_mark_node;
19207 }
19208
19209 return objc_build_encode_expr (type);
19210 }
19211
19212 /* Parse an Objective-C @defs expression. */
19213
19214 static tree
19215 cp_parser_objc_defs_expression (cp_parser *parser)
19216 {
19217 tree name;
19218
19219 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19220 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19221 name = cp_parser_identifier (parser);
19222 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19223
19224 return objc_get_class_ivars (name);
19225 }
19226
19227 /* Parse an Objective-C protocol expression.
19228
19229 objc-protocol-expression:
19230 @protocol ( identifier )
19231
19232 Returns a representation of the protocol expression. */
19233
19234 static tree
19235 cp_parser_objc_protocol_expression (cp_parser* parser)
19236 {
19237 tree proto;
19238
19239 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19240 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19241 proto = cp_parser_identifier (parser);
19242 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19243
19244 return objc_build_protocol_expr (proto);
19245 }
19246
19247 /* Parse an Objective-C selector expression.
19248
19249 objc-selector-expression:
19250 @selector ( objc-method-signature )
19251
19252 objc-method-signature:
19253 objc-selector
19254 objc-selector-seq
19255
19256 objc-selector-seq:
19257 objc-selector :
19258 objc-selector-seq objc-selector :
19259
19260 Returns a representation of the method selector. */
19261
19262 static tree
19263 cp_parser_objc_selector_expression (cp_parser* parser)
19264 {
19265 tree sel_seq = NULL_TREE;
19266 bool maybe_unary_selector_p = true;
19267 cp_token *token;
19268
19269 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19270 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19271 token = cp_lexer_peek_token (parser->lexer);
19272
19273 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19274 || token->type == CPP_SCOPE)
19275 {
19276 tree selector = NULL_TREE;
19277
19278 if (token->type != CPP_COLON
19279 || token->type == CPP_SCOPE)
19280 selector = cp_parser_objc_selector (parser);
19281
19282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19283 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19284 {
19285 /* Detect if we have a unary selector. */
19286 if (maybe_unary_selector_p)
19287 {
19288 sel_seq = selector;
19289 goto finish_selector;
19290 }
19291 else
19292 {
19293 cp_parser_error (parser, "expected %<:%>");
19294 }
19295 }
19296 maybe_unary_selector_p = false;
19297 token = cp_lexer_consume_token (parser->lexer);
19298
19299 if (token->type == CPP_SCOPE)
19300 {
19301 sel_seq
19302 = chainon (sel_seq,
19303 build_tree_list (selector, NULL_TREE));
19304 sel_seq
19305 = chainon (sel_seq,
19306 build_tree_list (NULL_TREE, NULL_TREE));
19307 }
19308 else
19309 sel_seq
19310 = chainon (sel_seq,
19311 build_tree_list (selector, NULL_TREE));
19312
19313 token = cp_lexer_peek_token (parser->lexer);
19314 }
19315
19316 finish_selector:
19317 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19318
19319 return objc_build_selector_expr (sel_seq);
19320 }
19321
19322 /* Parse a list of identifiers.
19323
19324 objc-identifier-list:
19325 identifier
19326 objc-identifier-list , identifier
19327
19328 Returns a TREE_LIST of identifier nodes. */
19329
19330 static tree
19331 cp_parser_objc_identifier_list (cp_parser* parser)
19332 {
19333 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19334 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19335
19336 while (sep->type == CPP_COMMA)
19337 {
19338 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19339 list = chainon (list,
19340 build_tree_list (NULL_TREE,
19341 cp_parser_identifier (parser)));
19342 sep = cp_lexer_peek_token (parser->lexer);
19343 }
19344
19345 return list;
19346 }
19347
19348 /* Parse an Objective-C alias declaration.
19349
19350 objc-alias-declaration:
19351 @compatibility_alias identifier identifier ;
19352
19353 This function registers the alias mapping with the Objective-C front end.
19354 It returns nothing. */
19355
19356 static void
19357 cp_parser_objc_alias_declaration (cp_parser* parser)
19358 {
19359 tree alias, orig;
19360
19361 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19362 alias = cp_parser_identifier (parser);
19363 orig = cp_parser_identifier (parser);
19364 objc_declare_alias (alias, orig);
19365 cp_parser_consume_semicolon_at_end_of_statement (parser);
19366 }
19367
19368 /* Parse an Objective-C class forward-declaration.
19369
19370 objc-class-declaration:
19371 @class objc-identifier-list ;
19372
19373 The function registers the forward declarations with the Objective-C
19374 front end. It returns nothing. */
19375
19376 static void
19377 cp_parser_objc_class_declaration (cp_parser* parser)
19378 {
19379 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19380 objc_declare_class (cp_parser_objc_identifier_list (parser));
19381 cp_parser_consume_semicolon_at_end_of_statement (parser);
19382 }
19383
19384 /* Parse a list of Objective-C protocol references.
19385
19386 objc-protocol-refs-opt:
19387 objc-protocol-refs [opt]
19388
19389 objc-protocol-refs:
19390 < objc-identifier-list >
19391
19392 Returns a TREE_LIST of identifiers, if any. */
19393
19394 static tree
19395 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19396 {
19397 tree protorefs = NULL_TREE;
19398
19399 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19400 {
19401 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19402 protorefs = cp_parser_objc_identifier_list (parser);
19403 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19404 }
19405
19406 return protorefs;
19407 }
19408
19409 /* Parse a Objective-C visibility specification. */
19410
19411 static void
19412 cp_parser_objc_visibility_spec (cp_parser* parser)
19413 {
19414 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19415
19416 switch (vis->keyword)
19417 {
19418 case RID_AT_PRIVATE:
19419 objc_set_visibility (2);
19420 break;
19421 case RID_AT_PROTECTED:
19422 objc_set_visibility (0);
19423 break;
19424 case RID_AT_PUBLIC:
19425 objc_set_visibility (1);
19426 break;
19427 default:
19428 return;
19429 }
19430
19431 /* Eat '@private'/'@protected'/'@public'. */
19432 cp_lexer_consume_token (parser->lexer);
19433 }
19434
19435 /* Parse an Objective-C method type. */
19436
19437 static void
19438 cp_parser_objc_method_type (cp_parser* parser)
19439 {
19440 objc_set_method_type
19441 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19442 ? PLUS_EXPR
19443 : MINUS_EXPR);
19444 }
19445
19446 /* Parse an Objective-C protocol qualifier. */
19447
19448 static tree
19449 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19450 {
19451 tree quals = NULL_TREE, node;
19452 cp_token *token = cp_lexer_peek_token (parser->lexer);
19453
19454 node = token->u.value;
19455
19456 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19457 && (node == ridpointers [(int) RID_IN]
19458 || node == ridpointers [(int) RID_OUT]
19459 || node == ridpointers [(int) RID_INOUT]
19460 || node == ridpointers [(int) RID_BYCOPY]
19461 || node == ridpointers [(int) RID_BYREF]
19462 || node == ridpointers [(int) RID_ONEWAY]))
19463 {
19464 quals = tree_cons (NULL_TREE, node, quals);
19465 cp_lexer_consume_token (parser->lexer);
19466 token = cp_lexer_peek_token (parser->lexer);
19467 node = token->u.value;
19468 }
19469
19470 return quals;
19471 }
19472
19473 /* Parse an Objective-C typename. */
19474
19475 static tree
19476 cp_parser_objc_typename (cp_parser* parser)
19477 {
19478 tree type_name = NULL_TREE;
19479
19480 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19481 {
19482 tree proto_quals, cp_type = NULL_TREE;
19483
19484 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19485 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19486
19487 /* An ObjC type name may consist of just protocol qualifiers, in which
19488 case the type shall default to 'id'. */
19489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19490 cp_type = cp_parser_type_id (parser);
19491
19492 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19493 type_name = build_tree_list (proto_quals, cp_type);
19494 }
19495
19496 return type_name;
19497 }
19498
19499 /* Check to see if TYPE refers to an Objective-C selector name. */
19500
19501 static bool
19502 cp_parser_objc_selector_p (enum cpp_ttype type)
19503 {
19504 return (type == CPP_NAME || type == CPP_KEYWORD
19505 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19506 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19507 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19508 || type == CPP_XOR || type == CPP_XOR_EQ);
19509 }
19510
19511 /* Parse an Objective-C selector. */
19512
19513 static tree
19514 cp_parser_objc_selector (cp_parser* parser)
19515 {
19516 cp_token *token = cp_lexer_consume_token (parser->lexer);
19517
19518 if (!cp_parser_objc_selector_p (token->type))
19519 {
19520 error ("%Hinvalid Objective-C++ selector name", &token->location);
19521 return error_mark_node;
19522 }
19523
19524 /* C++ operator names are allowed to appear in ObjC selectors. */
19525 switch (token->type)
19526 {
19527 case CPP_AND_AND: return get_identifier ("and");
19528 case CPP_AND_EQ: return get_identifier ("and_eq");
19529 case CPP_AND: return get_identifier ("bitand");
19530 case CPP_OR: return get_identifier ("bitor");
19531 case CPP_COMPL: return get_identifier ("compl");
19532 case CPP_NOT: return get_identifier ("not");
19533 case CPP_NOT_EQ: return get_identifier ("not_eq");
19534 case CPP_OR_OR: return get_identifier ("or");
19535 case CPP_OR_EQ: return get_identifier ("or_eq");
19536 case CPP_XOR: return get_identifier ("xor");
19537 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19538 default: return token->u.value;
19539 }
19540 }
19541
19542 /* Parse an Objective-C params list. */
19543
19544 static tree
19545 cp_parser_objc_method_keyword_params (cp_parser* parser)
19546 {
19547 tree params = NULL_TREE;
19548 bool maybe_unary_selector_p = true;
19549 cp_token *token = cp_lexer_peek_token (parser->lexer);
19550
19551 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19552 {
19553 tree selector = NULL_TREE, type_name, identifier;
19554
19555 if (token->type != CPP_COLON)
19556 selector = cp_parser_objc_selector (parser);
19557
19558 /* Detect if we have a unary selector. */
19559 if (maybe_unary_selector_p
19560 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19561 return selector;
19562
19563 maybe_unary_selector_p = false;
19564 cp_parser_require (parser, CPP_COLON, "%<:%>");
19565 type_name = cp_parser_objc_typename (parser);
19566 identifier = cp_parser_identifier (parser);
19567
19568 params
19569 = chainon (params,
19570 objc_build_keyword_decl (selector,
19571 type_name,
19572 identifier));
19573
19574 token = cp_lexer_peek_token (parser->lexer);
19575 }
19576
19577 return params;
19578 }
19579
19580 /* Parse the non-keyword Objective-C params. */
19581
19582 static tree
19583 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19584 {
19585 tree params = make_node (TREE_LIST);
19586 cp_token *token = cp_lexer_peek_token (parser->lexer);
19587 *ellipsisp = false; /* Initially, assume no ellipsis. */
19588
19589 while (token->type == CPP_COMMA)
19590 {
19591 cp_parameter_declarator *parmdecl;
19592 tree parm;
19593
19594 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19595 token = cp_lexer_peek_token (parser->lexer);
19596
19597 if (token->type == CPP_ELLIPSIS)
19598 {
19599 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19600 *ellipsisp = true;
19601 break;
19602 }
19603
19604 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19605 parm = grokdeclarator (parmdecl->declarator,
19606 &parmdecl->decl_specifiers,
19607 PARM, /*initialized=*/0,
19608 /*attrlist=*/NULL);
19609
19610 chainon (params, build_tree_list (NULL_TREE, parm));
19611 token = cp_lexer_peek_token (parser->lexer);
19612 }
19613
19614 return params;
19615 }
19616
19617 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19618
19619 static void
19620 cp_parser_objc_interstitial_code (cp_parser* parser)
19621 {
19622 cp_token *token = cp_lexer_peek_token (parser->lexer);
19623
19624 /* If the next token is `extern' and the following token is a string
19625 literal, then we have a linkage specification. */
19626 if (token->keyword == RID_EXTERN
19627 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19628 cp_parser_linkage_specification (parser);
19629 /* Handle #pragma, if any. */
19630 else if (token->type == CPP_PRAGMA)
19631 cp_parser_pragma (parser, pragma_external);
19632 /* Allow stray semicolons. */
19633 else if (token->type == CPP_SEMICOLON)
19634 cp_lexer_consume_token (parser->lexer);
19635 /* Finally, try to parse a block-declaration, or a function-definition. */
19636 else
19637 cp_parser_block_declaration (parser, /*statement_p=*/false);
19638 }
19639
19640 /* Parse a method signature. */
19641
19642 static tree
19643 cp_parser_objc_method_signature (cp_parser* parser)
19644 {
19645 tree rettype, kwdparms, optparms;
19646 bool ellipsis = false;
19647
19648 cp_parser_objc_method_type (parser);
19649 rettype = cp_parser_objc_typename (parser);
19650 kwdparms = cp_parser_objc_method_keyword_params (parser);
19651 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19652
19653 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19654 }
19655
19656 /* Pars an Objective-C method prototype list. */
19657
19658 static void
19659 cp_parser_objc_method_prototype_list (cp_parser* parser)
19660 {
19661 cp_token *token = cp_lexer_peek_token (parser->lexer);
19662
19663 while (token->keyword != RID_AT_END)
19664 {
19665 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19666 {
19667 objc_add_method_declaration
19668 (cp_parser_objc_method_signature (parser));
19669 cp_parser_consume_semicolon_at_end_of_statement (parser);
19670 }
19671 else
19672 /* Allow for interspersed non-ObjC++ code. */
19673 cp_parser_objc_interstitial_code (parser);
19674
19675 token = cp_lexer_peek_token (parser->lexer);
19676 }
19677
19678 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19679 objc_finish_interface ();
19680 }
19681
19682 /* Parse an Objective-C method definition list. */
19683
19684 static void
19685 cp_parser_objc_method_definition_list (cp_parser* parser)
19686 {
19687 cp_token *token = cp_lexer_peek_token (parser->lexer);
19688
19689 while (token->keyword != RID_AT_END)
19690 {
19691 tree meth;
19692
19693 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19694 {
19695 push_deferring_access_checks (dk_deferred);
19696 objc_start_method_definition
19697 (cp_parser_objc_method_signature (parser));
19698
19699 /* For historical reasons, we accept an optional semicolon. */
19700 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19701 cp_lexer_consume_token (parser->lexer);
19702
19703 perform_deferred_access_checks ();
19704 stop_deferring_access_checks ();
19705 meth = cp_parser_function_definition_after_declarator (parser,
19706 false);
19707 pop_deferring_access_checks ();
19708 objc_finish_method_definition (meth);
19709 }
19710 else
19711 /* Allow for interspersed non-ObjC++ code. */
19712 cp_parser_objc_interstitial_code (parser);
19713
19714 token = cp_lexer_peek_token (parser->lexer);
19715 }
19716
19717 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19718 objc_finish_implementation ();
19719 }
19720
19721 /* Parse Objective-C ivars. */
19722
19723 static void
19724 cp_parser_objc_class_ivars (cp_parser* parser)
19725 {
19726 cp_token *token = cp_lexer_peek_token (parser->lexer);
19727
19728 if (token->type != CPP_OPEN_BRACE)
19729 return; /* No ivars specified. */
19730
19731 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19732 token = cp_lexer_peek_token (parser->lexer);
19733
19734 while (token->type != CPP_CLOSE_BRACE)
19735 {
19736 cp_decl_specifier_seq declspecs;
19737 int decl_class_or_enum_p;
19738 tree prefix_attributes;
19739
19740 cp_parser_objc_visibility_spec (parser);
19741
19742 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19743 break;
19744
19745 cp_parser_decl_specifier_seq (parser,
19746 CP_PARSER_FLAGS_OPTIONAL,
19747 &declspecs,
19748 &decl_class_or_enum_p);
19749 prefix_attributes = declspecs.attributes;
19750 declspecs.attributes = NULL_TREE;
19751
19752 /* Keep going until we hit the `;' at the end of the
19753 declaration. */
19754 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19755 {
19756 tree width = NULL_TREE, attributes, first_attribute, decl;
19757 cp_declarator *declarator = NULL;
19758 int ctor_dtor_or_conv_p;
19759
19760 /* Check for a (possibly unnamed) bitfield declaration. */
19761 token = cp_lexer_peek_token (parser->lexer);
19762 if (token->type == CPP_COLON)
19763 goto eat_colon;
19764
19765 if (token->type == CPP_NAME
19766 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19767 == CPP_COLON))
19768 {
19769 /* Get the name of the bitfield. */
19770 declarator = make_id_declarator (NULL_TREE,
19771 cp_parser_identifier (parser),
19772 sfk_none);
19773
19774 eat_colon:
19775 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19776 /* Get the width of the bitfield. */
19777 width
19778 = cp_parser_constant_expression (parser,
19779 /*allow_non_constant=*/false,
19780 NULL);
19781 }
19782 else
19783 {
19784 /* Parse the declarator. */
19785 declarator
19786 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19787 &ctor_dtor_or_conv_p,
19788 /*parenthesized_p=*/NULL,
19789 /*member_p=*/false);
19790 }
19791
19792 /* Look for attributes that apply to the ivar. */
19793 attributes = cp_parser_attributes_opt (parser);
19794 /* Remember which attributes are prefix attributes and
19795 which are not. */
19796 first_attribute = attributes;
19797 /* Combine the attributes. */
19798 attributes = chainon (prefix_attributes, attributes);
19799
19800 if (width)
19801 /* Create the bitfield declaration. */
19802 decl = grokbitfield (declarator, &declspecs,
19803 width,
19804 attributes);
19805 else
19806 decl = grokfield (declarator, &declspecs,
19807 NULL_TREE, /*init_const_expr_p=*/false,
19808 NULL_TREE, attributes);
19809
19810 /* Add the instance variable. */
19811 objc_add_instance_variable (decl);
19812
19813 /* Reset PREFIX_ATTRIBUTES. */
19814 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19815 attributes = TREE_CHAIN (attributes);
19816 if (attributes)
19817 TREE_CHAIN (attributes) = NULL_TREE;
19818
19819 token = cp_lexer_peek_token (parser->lexer);
19820
19821 if (token->type == CPP_COMMA)
19822 {
19823 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19824 continue;
19825 }
19826 break;
19827 }
19828
19829 cp_parser_consume_semicolon_at_end_of_statement (parser);
19830 token = cp_lexer_peek_token (parser->lexer);
19831 }
19832
19833 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19834 /* For historical reasons, we accept an optional semicolon. */
19835 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19836 cp_lexer_consume_token (parser->lexer);
19837 }
19838
19839 /* Parse an Objective-C protocol declaration. */
19840
19841 static void
19842 cp_parser_objc_protocol_declaration (cp_parser* parser)
19843 {
19844 tree proto, protorefs;
19845 cp_token *tok;
19846
19847 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19849 {
19850 tok = cp_lexer_peek_token (parser->lexer);
19851 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19852 goto finish;
19853 }
19854
19855 /* See if we have a forward declaration or a definition. */
19856 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19857
19858 /* Try a forward declaration first. */
19859 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19860 {
19861 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19862 finish:
19863 cp_parser_consume_semicolon_at_end_of_statement (parser);
19864 }
19865
19866 /* Ok, we got a full-fledged definition (or at least should). */
19867 else
19868 {
19869 proto = cp_parser_identifier (parser);
19870 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19871 objc_start_protocol (proto, protorefs);
19872 cp_parser_objc_method_prototype_list (parser);
19873 }
19874 }
19875
19876 /* Parse an Objective-C superclass or category. */
19877
19878 static void
19879 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19880 tree *categ)
19881 {
19882 cp_token *next = cp_lexer_peek_token (parser->lexer);
19883
19884 *super = *categ = NULL_TREE;
19885 if (next->type == CPP_COLON)
19886 {
19887 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19888 *super = cp_parser_identifier (parser);
19889 }
19890 else if (next->type == CPP_OPEN_PAREN)
19891 {
19892 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19893 *categ = cp_parser_identifier (parser);
19894 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19895 }
19896 }
19897
19898 /* Parse an Objective-C class interface. */
19899
19900 static void
19901 cp_parser_objc_class_interface (cp_parser* parser)
19902 {
19903 tree name, super, categ, protos;
19904
19905 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19906 name = cp_parser_identifier (parser);
19907 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19908 protos = cp_parser_objc_protocol_refs_opt (parser);
19909
19910 /* We have either a class or a category on our hands. */
19911 if (categ)
19912 objc_start_category_interface (name, categ, protos);
19913 else
19914 {
19915 objc_start_class_interface (name, super, protos);
19916 /* Handle instance variable declarations, if any. */
19917 cp_parser_objc_class_ivars (parser);
19918 objc_continue_interface ();
19919 }
19920
19921 cp_parser_objc_method_prototype_list (parser);
19922 }
19923
19924 /* Parse an Objective-C class implementation. */
19925
19926 static void
19927 cp_parser_objc_class_implementation (cp_parser* parser)
19928 {
19929 tree name, super, categ;
19930
19931 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19932 name = cp_parser_identifier (parser);
19933 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19934
19935 /* We have either a class or a category on our hands. */
19936 if (categ)
19937 objc_start_category_implementation (name, categ);
19938 else
19939 {
19940 objc_start_class_implementation (name, super);
19941 /* Handle instance variable declarations, if any. */
19942 cp_parser_objc_class_ivars (parser);
19943 objc_continue_implementation ();
19944 }
19945
19946 cp_parser_objc_method_definition_list (parser);
19947 }
19948
19949 /* Consume the @end token and finish off the implementation. */
19950
19951 static void
19952 cp_parser_objc_end_implementation (cp_parser* parser)
19953 {
19954 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19955 objc_finish_implementation ();
19956 }
19957
19958 /* Parse an Objective-C declaration. */
19959
19960 static void
19961 cp_parser_objc_declaration (cp_parser* parser)
19962 {
19963 /* Try to figure out what kind of declaration is present. */
19964 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19965
19966 switch (kwd->keyword)
19967 {
19968 case RID_AT_ALIAS:
19969 cp_parser_objc_alias_declaration (parser);
19970 break;
19971 case RID_AT_CLASS:
19972 cp_parser_objc_class_declaration (parser);
19973 break;
19974 case RID_AT_PROTOCOL:
19975 cp_parser_objc_protocol_declaration (parser);
19976 break;
19977 case RID_AT_INTERFACE:
19978 cp_parser_objc_class_interface (parser);
19979 break;
19980 case RID_AT_IMPLEMENTATION:
19981 cp_parser_objc_class_implementation (parser);
19982 break;
19983 case RID_AT_END:
19984 cp_parser_objc_end_implementation (parser);
19985 break;
19986 default:
19987 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19988 &kwd->location, kwd->u.value);
19989 cp_parser_skip_to_end_of_block_or_statement (parser);
19990 }
19991 }
19992
19993 /* Parse an Objective-C try-catch-finally statement.
19994
19995 objc-try-catch-finally-stmt:
19996 @try compound-statement objc-catch-clause-seq [opt]
19997 objc-finally-clause [opt]
19998
19999 objc-catch-clause-seq:
20000 objc-catch-clause objc-catch-clause-seq [opt]
20001
20002 objc-catch-clause:
20003 @catch ( exception-declaration ) compound-statement
20004
20005 objc-finally-clause
20006 @finally compound-statement
20007
20008 Returns NULL_TREE. */
20009
20010 static tree
20011 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20012 location_t location;
20013 tree stmt;
20014
20015 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20016 location = cp_lexer_peek_token (parser->lexer)->location;
20017 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20018 node, lest it get absorbed into the surrounding block. */
20019 stmt = push_stmt_list ();
20020 cp_parser_compound_statement (parser, NULL, false);
20021 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20022
20023 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20024 {
20025 cp_parameter_declarator *parmdecl;
20026 tree parm;
20027
20028 cp_lexer_consume_token (parser->lexer);
20029 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20030 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20031 parm = grokdeclarator (parmdecl->declarator,
20032 &parmdecl->decl_specifiers,
20033 PARM, /*initialized=*/0,
20034 /*attrlist=*/NULL);
20035 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20036 objc_begin_catch_clause (parm);
20037 cp_parser_compound_statement (parser, NULL, false);
20038 objc_finish_catch_clause ();
20039 }
20040
20041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20042 {
20043 cp_lexer_consume_token (parser->lexer);
20044 location = cp_lexer_peek_token (parser->lexer)->location;
20045 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20046 node, lest it get absorbed into the surrounding block. */
20047 stmt = push_stmt_list ();
20048 cp_parser_compound_statement (parser, NULL, false);
20049 objc_build_finally_clause (location, pop_stmt_list (stmt));
20050 }
20051
20052 return objc_finish_try_stmt ();
20053 }
20054
20055 /* Parse an Objective-C synchronized statement.
20056
20057 objc-synchronized-stmt:
20058 @synchronized ( expression ) compound-statement
20059
20060 Returns NULL_TREE. */
20061
20062 static tree
20063 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20064 location_t location;
20065 tree lock, stmt;
20066
20067 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20068
20069 location = cp_lexer_peek_token (parser->lexer)->location;
20070 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20071 lock = cp_parser_expression (parser, false, NULL);
20072 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20073
20074 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20075 node, lest it get absorbed into the surrounding block. */
20076 stmt = push_stmt_list ();
20077 cp_parser_compound_statement (parser, NULL, false);
20078
20079 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20080 }
20081
20082 /* Parse an Objective-C throw statement.
20083
20084 objc-throw-stmt:
20085 @throw assignment-expression [opt] ;
20086
20087 Returns a constructed '@throw' statement. */
20088
20089 static tree
20090 cp_parser_objc_throw_statement (cp_parser *parser) {
20091 tree expr = NULL_TREE;
20092
20093 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20094
20095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20096 expr = cp_parser_assignment_expression (parser, false, NULL);
20097
20098 cp_parser_consume_semicolon_at_end_of_statement (parser);
20099
20100 return objc_build_throw_stmt (expr);
20101 }
20102
20103 /* Parse an Objective-C statement. */
20104
20105 static tree
20106 cp_parser_objc_statement (cp_parser * parser) {
20107 /* Try to figure out what kind of declaration is present. */
20108 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20109
20110 switch (kwd->keyword)
20111 {
20112 case RID_AT_TRY:
20113 return cp_parser_objc_try_catch_finally_statement (parser);
20114 case RID_AT_SYNCHRONIZED:
20115 return cp_parser_objc_synchronized_statement (parser);
20116 case RID_AT_THROW:
20117 return cp_parser_objc_throw_statement (parser);
20118 default:
20119 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20120 &kwd->location, kwd->u.value);
20121 cp_parser_skip_to_end_of_block_or_statement (parser);
20122 }
20123
20124 return error_mark_node;
20125 }
20126 \f
20127 /* OpenMP 2.5 parsing routines. */
20128
20129 /* Returns name of the next clause.
20130 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20131 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20132 returned and the token is consumed. */
20133
20134 static pragma_omp_clause
20135 cp_parser_omp_clause_name (cp_parser *parser)
20136 {
20137 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20138
20139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20140 result = PRAGMA_OMP_CLAUSE_IF;
20141 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20142 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20143 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20144 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20145 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20146 {
20147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20148 const char *p = IDENTIFIER_POINTER (id);
20149
20150 switch (p[0])
20151 {
20152 case 'c':
20153 if (!strcmp ("collapse", p))
20154 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20155 else if (!strcmp ("copyin", p))
20156 result = PRAGMA_OMP_CLAUSE_COPYIN;
20157 else if (!strcmp ("copyprivate", p))
20158 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20159 break;
20160 case 'f':
20161 if (!strcmp ("firstprivate", p))
20162 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20163 break;
20164 case 'l':
20165 if (!strcmp ("lastprivate", p))
20166 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20167 break;
20168 case 'n':
20169 if (!strcmp ("nowait", p))
20170 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20171 else if (!strcmp ("num_threads", p))
20172 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20173 break;
20174 case 'o':
20175 if (!strcmp ("ordered", p))
20176 result = PRAGMA_OMP_CLAUSE_ORDERED;
20177 break;
20178 case 'r':
20179 if (!strcmp ("reduction", p))
20180 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20181 break;
20182 case 's':
20183 if (!strcmp ("schedule", p))
20184 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20185 else if (!strcmp ("shared", p))
20186 result = PRAGMA_OMP_CLAUSE_SHARED;
20187 break;
20188 case 'u':
20189 if (!strcmp ("untied", p))
20190 result = PRAGMA_OMP_CLAUSE_UNTIED;
20191 break;
20192 }
20193 }
20194
20195 if (result != PRAGMA_OMP_CLAUSE_NONE)
20196 cp_lexer_consume_token (parser->lexer);
20197
20198 return result;
20199 }
20200
20201 /* Validate that a clause of the given type does not already exist. */
20202
20203 static void
20204 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20205 const char *name, location_t location)
20206 {
20207 tree c;
20208
20209 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20210 if (OMP_CLAUSE_CODE (c) == code)
20211 {
20212 error ("%Htoo many %qs clauses", &location, name);
20213 break;
20214 }
20215 }
20216
20217 /* OpenMP 2.5:
20218 variable-list:
20219 identifier
20220 variable-list , identifier
20221
20222 In addition, we match a closing parenthesis. An opening parenthesis
20223 will have been consumed by the caller.
20224
20225 If KIND is nonzero, create the appropriate node and install the decl
20226 in OMP_CLAUSE_DECL and add the node to the head of the list.
20227
20228 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20229 return the list created. */
20230
20231 static tree
20232 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20233 tree list)
20234 {
20235 cp_token *token;
20236 while (1)
20237 {
20238 tree name, decl;
20239
20240 token = cp_lexer_peek_token (parser->lexer);
20241 name = cp_parser_id_expression (parser, /*template_p=*/false,
20242 /*check_dependency_p=*/true,
20243 /*template_p=*/NULL,
20244 /*declarator_p=*/false,
20245 /*optional_p=*/false);
20246 if (name == error_mark_node)
20247 goto skip_comma;
20248
20249 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20250 if (decl == error_mark_node)
20251 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20252 else if (kind != 0)
20253 {
20254 tree u = build_omp_clause (kind);
20255 OMP_CLAUSE_DECL (u) = decl;
20256 OMP_CLAUSE_CHAIN (u) = list;
20257 list = u;
20258 }
20259 else
20260 list = tree_cons (decl, NULL_TREE, list);
20261
20262 get_comma:
20263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20264 break;
20265 cp_lexer_consume_token (parser->lexer);
20266 }
20267
20268 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20269 {
20270 int ending;
20271
20272 /* Try to resync to an unnested comma. Copied from
20273 cp_parser_parenthesized_expression_list. */
20274 skip_comma:
20275 ending = cp_parser_skip_to_closing_parenthesis (parser,
20276 /*recovering=*/true,
20277 /*or_comma=*/true,
20278 /*consume_paren=*/true);
20279 if (ending < 0)
20280 goto get_comma;
20281 }
20282
20283 return list;
20284 }
20285
20286 /* Similarly, but expect leading and trailing parenthesis. This is a very
20287 common case for omp clauses. */
20288
20289 static tree
20290 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20291 {
20292 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20293 return cp_parser_omp_var_list_no_open (parser, kind, list);
20294 return list;
20295 }
20296
20297 /* OpenMP 3.0:
20298 collapse ( constant-expression ) */
20299
20300 static tree
20301 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20302 {
20303 tree c, num;
20304 location_t loc;
20305 HOST_WIDE_INT n;
20306
20307 loc = cp_lexer_peek_token (parser->lexer)->location;
20308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20309 return list;
20310
20311 num = cp_parser_constant_expression (parser, false, NULL);
20312
20313 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20315 /*or_comma=*/false,
20316 /*consume_paren=*/true);
20317
20318 if (num == error_mark_node)
20319 return list;
20320 num = fold_non_dependent_expr (num);
20321 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20322 || !host_integerp (num, 0)
20323 || (n = tree_low_cst (num, 0)) <= 0
20324 || (int) n != n)
20325 {
20326 error ("%Hcollapse argument needs positive constant integer expression",
20327 &loc);
20328 return list;
20329 }
20330
20331 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20332 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20333 OMP_CLAUSE_CHAIN (c) = list;
20334 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20335
20336 return c;
20337 }
20338
20339 /* OpenMP 2.5:
20340 default ( shared | none ) */
20341
20342 static tree
20343 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20344 {
20345 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20346 tree c;
20347
20348 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20349 return list;
20350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20351 {
20352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20353 const char *p = IDENTIFIER_POINTER (id);
20354
20355 switch (p[0])
20356 {
20357 case 'n':
20358 if (strcmp ("none", p) != 0)
20359 goto invalid_kind;
20360 kind = OMP_CLAUSE_DEFAULT_NONE;
20361 break;
20362
20363 case 's':
20364 if (strcmp ("shared", p) != 0)
20365 goto invalid_kind;
20366 kind = OMP_CLAUSE_DEFAULT_SHARED;
20367 break;
20368
20369 default:
20370 goto invalid_kind;
20371 }
20372
20373 cp_lexer_consume_token (parser->lexer);
20374 }
20375 else
20376 {
20377 invalid_kind:
20378 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20379 }
20380
20381 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20382 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20383 /*or_comma=*/false,
20384 /*consume_paren=*/true);
20385
20386 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20387 return list;
20388
20389 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20390 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20391 OMP_CLAUSE_CHAIN (c) = list;
20392 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20393
20394 return c;
20395 }
20396
20397 /* OpenMP 2.5:
20398 if ( expression ) */
20399
20400 static tree
20401 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20402 {
20403 tree t, c;
20404
20405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20406 return list;
20407
20408 t = cp_parser_condition (parser);
20409
20410 if (t == error_mark_node
20411 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20413 /*or_comma=*/false,
20414 /*consume_paren=*/true);
20415
20416 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20417
20418 c = build_omp_clause (OMP_CLAUSE_IF);
20419 OMP_CLAUSE_IF_EXPR (c) = t;
20420 OMP_CLAUSE_CHAIN (c) = list;
20421
20422 return c;
20423 }
20424
20425 /* OpenMP 2.5:
20426 nowait */
20427
20428 static tree
20429 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20430 tree list, location_t location)
20431 {
20432 tree c;
20433
20434 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20435
20436 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20437 OMP_CLAUSE_CHAIN (c) = list;
20438 return c;
20439 }
20440
20441 /* OpenMP 2.5:
20442 num_threads ( expression ) */
20443
20444 static tree
20445 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20446 location_t location)
20447 {
20448 tree t, c;
20449
20450 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20451 return list;
20452
20453 t = cp_parser_expression (parser, false, NULL);
20454
20455 if (t == error_mark_node
20456 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20457 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20458 /*or_comma=*/false,
20459 /*consume_paren=*/true);
20460
20461 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20462 "num_threads", location);
20463
20464 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20465 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20466 OMP_CLAUSE_CHAIN (c) = list;
20467
20468 return c;
20469 }
20470
20471 /* OpenMP 2.5:
20472 ordered */
20473
20474 static tree
20475 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20476 tree list, location_t location)
20477 {
20478 tree c;
20479
20480 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20481 "ordered", location);
20482
20483 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20484 OMP_CLAUSE_CHAIN (c) = list;
20485 return c;
20486 }
20487
20488 /* OpenMP 2.5:
20489 reduction ( reduction-operator : variable-list )
20490
20491 reduction-operator:
20492 One of: + * - & ^ | && || */
20493
20494 static tree
20495 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20496 {
20497 enum tree_code code;
20498 tree nlist, c;
20499
20500 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20501 return list;
20502
20503 switch (cp_lexer_peek_token (parser->lexer)->type)
20504 {
20505 case CPP_PLUS:
20506 code = PLUS_EXPR;
20507 break;
20508 case CPP_MULT:
20509 code = MULT_EXPR;
20510 break;
20511 case CPP_MINUS:
20512 code = MINUS_EXPR;
20513 break;
20514 case CPP_AND:
20515 code = BIT_AND_EXPR;
20516 break;
20517 case CPP_XOR:
20518 code = BIT_XOR_EXPR;
20519 break;
20520 case CPP_OR:
20521 code = BIT_IOR_EXPR;
20522 break;
20523 case CPP_AND_AND:
20524 code = TRUTH_ANDIF_EXPR;
20525 break;
20526 case CPP_OR_OR:
20527 code = TRUTH_ORIF_EXPR;
20528 break;
20529 default:
20530 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20531 "%<|%>, %<&&%>, or %<||%>");
20532 resync_fail:
20533 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20534 /*or_comma=*/false,
20535 /*consume_paren=*/true);
20536 return list;
20537 }
20538 cp_lexer_consume_token (parser->lexer);
20539
20540 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20541 goto resync_fail;
20542
20543 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20544 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20545 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20546
20547 return nlist;
20548 }
20549
20550 /* OpenMP 2.5:
20551 schedule ( schedule-kind )
20552 schedule ( schedule-kind , expression )
20553
20554 schedule-kind:
20555 static | dynamic | guided | runtime | auto */
20556
20557 static tree
20558 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20559 {
20560 tree c, t;
20561
20562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20563 return list;
20564
20565 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20566
20567 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20568 {
20569 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20570 const char *p = IDENTIFIER_POINTER (id);
20571
20572 switch (p[0])
20573 {
20574 case 'd':
20575 if (strcmp ("dynamic", p) != 0)
20576 goto invalid_kind;
20577 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20578 break;
20579
20580 case 'g':
20581 if (strcmp ("guided", p) != 0)
20582 goto invalid_kind;
20583 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20584 break;
20585
20586 case 'r':
20587 if (strcmp ("runtime", p) != 0)
20588 goto invalid_kind;
20589 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20590 break;
20591
20592 default:
20593 goto invalid_kind;
20594 }
20595 }
20596 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20597 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20598 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20599 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20600 else
20601 goto invalid_kind;
20602 cp_lexer_consume_token (parser->lexer);
20603
20604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20605 {
20606 cp_token *token;
20607 cp_lexer_consume_token (parser->lexer);
20608
20609 token = cp_lexer_peek_token (parser->lexer);
20610 t = cp_parser_assignment_expression (parser, false, NULL);
20611
20612 if (t == error_mark_node)
20613 goto resync_fail;
20614 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20615 error ("%Hschedule %<runtime%> does not take "
20616 "a %<chunk_size%> parameter", &token->location);
20617 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20618 error ("%Hschedule %<auto%> does not take "
20619 "a %<chunk_size%> parameter", &token->location);
20620 else
20621 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20622
20623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20624 goto resync_fail;
20625 }
20626 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20627 goto resync_fail;
20628
20629 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20630 OMP_CLAUSE_CHAIN (c) = list;
20631 return c;
20632
20633 invalid_kind:
20634 cp_parser_error (parser, "invalid schedule kind");
20635 resync_fail:
20636 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20637 /*or_comma=*/false,
20638 /*consume_paren=*/true);
20639 return list;
20640 }
20641
20642 /* OpenMP 3.0:
20643 untied */
20644
20645 static tree
20646 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20647 tree list, location_t location)
20648 {
20649 tree c;
20650
20651 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20652
20653 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20654 OMP_CLAUSE_CHAIN (c) = list;
20655 return c;
20656 }
20657
20658 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20659 is a bitmask in MASK. Return the list of clauses found; the result
20660 of clause default goes in *pdefault. */
20661
20662 static tree
20663 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20664 const char *where, cp_token *pragma_tok)
20665 {
20666 tree clauses = NULL;
20667 bool first = true;
20668 cp_token *token = NULL;
20669
20670 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20671 {
20672 pragma_omp_clause c_kind;
20673 const char *c_name;
20674 tree prev = clauses;
20675
20676 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20677 cp_lexer_consume_token (parser->lexer);
20678
20679 token = cp_lexer_peek_token (parser->lexer);
20680 c_kind = cp_parser_omp_clause_name (parser);
20681 first = false;
20682
20683 switch (c_kind)
20684 {
20685 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20686 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20687 token->location);
20688 c_name = "collapse";
20689 break;
20690 case PRAGMA_OMP_CLAUSE_COPYIN:
20691 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20692 c_name = "copyin";
20693 break;
20694 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20695 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20696 clauses);
20697 c_name = "copyprivate";
20698 break;
20699 case PRAGMA_OMP_CLAUSE_DEFAULT:
20700 clauses = cp_parser_omp_clause_default (parser, clauses,
20701 token->location);
20702 c_name = "default";
20703 break;
20704 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20705 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20706 clauses);
20707 c_name = "firstprivate";
20708 break;
20709 case PRAGMA_OMP_CLAUSE_IF:
20710 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20711 c_name = "if";
20712 break;
20713 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20714 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20715 clauses);
20716 c_name = "lastprivate";
20717 break;
20718 case PRAGMA_OMP_CLAUSE_NOWAIT:
20719 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20720 c_name = "nowait";
20721 break;
20722 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20723 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20724 token->location);
20725 c_name = "num_threads";
20726 break;
20727 case PRAGMA_OMP_CLAUSE_ORDERED:
20728 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20729 token->location);
20730 c_name = "ordered";
20731 break;
20732 case PRAGMA_OMP_CLAUSE_PRIVATE:
20733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20734 clauses);
20735 c_name = "private";
20736 break;
20737 case PRAGMA_OMP_CLAUSE_REDUCTION:
20738 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20739 c_name = "reduction";
20740 break;
20741 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20742 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20743 token->location);
20744 c_name = "schedule";
20745 break;
20746 case PRAGMA_OMP_CLAUSE_SHARED:
20747 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20748 clauses);
20749 c_name = "shared";
20750 break;
20751 case PRAGMA_OMP_CLAUSE_UNTIED:
20752 clauses = cp_parser_omp_clause_untied (parser, clauses,
20753 token->location);
20754 c_name = "nowait";
20755 break;
20756 default:
20757 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20758 goto saw_error;
20759 }
20760
20761 if (((mask >> c_kind) & 1) == 0)
20762 {
20763 /* Remove the invalid clause(s) from the list to avoid
20764 confusing the rest of the compiler. */
20765 clauses = prev;
20766 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20767 }
20768 }
20769 saw_error:
20770 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20771 return finish_omp_clauses (clauses);
20772 }
20773
20774 /* OpenMP 2.5:
20775 structured-block:
20776 statement
20777
20778 In practice, we're also interested in adding the statement to an
20779 outer node. So it is convenient if we work around the fact that
20780 cp_parser_statement calls add_stmt. */
20781
20782 static unsigned
20783 cp_parser_begin_omp_structured_block (cp_parser *parser)
20784 {
20785 unsigned save = parser->in_statement;
20786
20787 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20788 This preserves the "not within loop or switch" style error messages
20789 for nonsense cases like
20790 void foo() {
20791 #pragma omp single
20792 break;
20793 }
20794 */
20795 if (parser->in_statement)
20796 parser->in_statement = IN_OMP_BLOCK;
20797
20798 return save;
20799 }
20800
20801 static void
20802 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20803 {
20804 parser->in_statement = save;
20805 }
20806
20807 static tree
20808 cp_parser_omp_structured_block (cp_parser *parser)
20809 {
20810 tree stmt = begin_omp_structured_block ();
20811 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20812
20813 cp_parser_statement (parser, NULL_TREE, false, NULL);
20814
20815 cp_parser_end_omp_structured_block (parser, save);
20816 return finish_omp_structured_block (stmt);
20817 }
20818
20819 /* OpenMP 2.5:
20820 # pragma omp atomic new-line
20821 expression-stmt
20822
20823 expression-stmt:
20824 x binop= expr | x++ | ++x | x-- | --x
20825 binop:
20826 +, *, -, /, &, ^, |, <<, >>
20827
20828 where x is an lvalue expression with scalar type. */
20829
20830 static void
20831 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20832 {
20833 tree lhs, rhs;
20834 enum tree_code code;
20835
20836 cp_parser_require_pragma_eol (parser, pragma_tok);
20837
20838 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20839 /*cast_p=*/false, NULL);
20840 switch (TREE_CODE (lhs))
20841 {
20842 case ERROR_MARK:
20843 goto saw_error;
20844
20845 case PREINCREMENT_EXPR:
20846 case POSTINCREMENT_EXPR:
20847 lhs = TREE_OPERAND (lhs, 0);
20848 code = PLUS_EXPR;
20849 rhs = integer_one_node;
20850 break;
20851
20852 case PREDECREMENT_EXPR:
20853 case POSTDECREMENT_EXPR:
20854 lhs = TREE_OPERAND (lhs, 0);
20855 code = MINUS_EXPR;
20856 rhs = integer_one_node;
20857 break;
20858
20859 default:
20860 switch (cp_lexer_peek_token (parser->lexer)->type)
20861 {
20862 case CPP_MULT_EQ:
20863 code = MULT_EXPR;
20864 break;
20865 case CPP_DIV_EQ:
20866 code = TRUNC_DIV_EXPR;
20867 break;
20868 case CPP_PLUS_EQ:
20869 code = PLUS_EXPR;
20870 break;
20871 case CPP_MINUS_EQ:
20872 code = MINUS_EXPR;
20873 break;
20874 case CPP_LSHIFT_EQ:
20875 code = LSHIFT_EXPR;
20876 break;
20877 case CPP_RSHIFT_EQ:
20878 code = RSHIFT_EXPR;
20879 break;
20880 case CPP_AND_EQ:
20881 code = BIT_AND_EXPR;
20882 break;
20883 case CPP_OR_EQ:
20884 code = BIT_IOR_EXPR;
20885 break;
20886 case CPP_XOR_EQ:
20887 code = BIT_XOR_EXPR;
20888 break;
20889 default:
20890 cp_parser_error (parser,
20891 "invalid operator for %<#pragma omp atomic%>");
20892 goto saw_error;
20893 }
20894 cp_lexer_consume_token (parser->lexer);
20895
20896 rhs = cp_parser_expression (parser, false, NULL);
20897 if (rhs == error_mark_node)
20898 goto saw_error;
20899 break;
20900 }
20901 finish_omp_atomic (code, lhs, rhs);
20902 cp_parser_consume_semicolon_at_end_of_statement (parser);
20903 return;
20904
20905 saw_error:
20906 cp_parser_skip_to_end_of_block_or_statement (parser);
20907 }
20908
20909
20910 /* OpenMP 2.5:
20911 # pragma omp barrier new-line */
20912
20913 static void
20914 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20915 {
20916 cp_parser_require_pragma_eol (parser, pragma_tok);
20917 finish_omp_barrier ();
20918 }
20919
20920 /* OpenMP 2.5:
20921 # pragma omp critical [(name)] new-line
20922 structured-block */
20923
20924 static tree
20925 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20926 {
20927 tree stmt, name = NULL;
20928
20929 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20930 {
20931 cp_lexer_consume_token (parser->lexer);
20932
20933 name = cp_parser_identifier (parser);
20934
20935 if (name == error_mark_node
20936 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20937 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20938 /*or_comma=*/false,
20939 /*consume_paren=*/true);
20940 if (name == error_mark_node)
20941 name = NULL;
20942 }
20943 cp_parser_require_pragma_eol (parser, pragma_tok);
20944
20945 stmt = cp_parser_omp_structured_block (parser);
20946 return c_finish_omp_critical (stmt, name);
20947 }
20948
20949 /* OpenMP 2.5:
20950 # pragma omp flush flush-vars[opt] new-line
20951
20952 flush-vars:
20953 ( variable-list ) */
20954
20955 static void
20956 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20957 {
20958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20959 (void) cp_parser_omp_var_list (parser, 0, NULL);
20960 cp_parser_require_pragma_eol (parser, pragma_tok);
20961
20962 finish_omp_flush ();
20963 }
20964
20965 /* Helper function, to parse omp for increment expression. */
20966
20967 static tree
20968 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20969 {
20970 tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
20971 enum tree_code op;
20972 cp_token *token;
20973
20974 if (lhs != decl)
20975 {
20976 cp_parser_skip_to_end_of_statement (parser);
20977 return error_mark_node;
20978 }
20979
20980 token = cp_lexer_peek_token (parser->lexer);
20981 op = binops_by_token [token->type].tree_type;
20982 switch (op)
20983 {
20984 case LT_EXPR:
20985 case LE_EXPR:
20986 case GT_EXPR:
20987 case GE_EXPR:
20988 break;
20989 default:
20990 cp_parser_skip_to_end_of_statement (parser);
20991 return error_mark_node;
20992 }
20993
20994 cp_lexer_consume_token (parser->lexer);
20995 rhs = cp_parser_binary_expression (parser, false,
20996 PREC_RELATIONAL_EXPRESSION, NULL);
20997 if (rhs == error_mark_node
20998 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20999 {
21000 cp_parser_skip_to_end_of_statement (parser);
21001 return error_mark_node;
21002 }
21003
21004 return build2 (op, boolean_type_node, lhs, rhs);
21005 }
21006
21007 /* Helper function, to parse omp for increment expression. */
21008
21009 static tree
21010 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21011 {
21012 cp_token *token = cp_lexer_peek_token (parser->lexer);
21013 enum tree_code op;
21014 tree lhs, rhs;
21015 cp_id_kind idk;
21016 bool decl_first;
21017
21018 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21019 {
21020 op = (token->type == CPP_PLUS_PLUS
21021 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21022 cp_lexer_consume_token (parser->lexer);
21023 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21024 if (lhs != decl)
21025 return error_mark_node;
21026 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21027 }
21028
21029 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21030 if (lhs != decl)
21031 return error_mark_node;
21032
21033 token = cp_lexer_peek_token (parser->lexer);
21034 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21035 {
21036 op = (token->type == CPP_PLUS_PLUS
21037 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21038 cp_lexer_consume_token (parser->lexer);
21039 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21040 }
21041
21042 op = cp_parser_assignment_operator_opt (parser);
21043 if (op == ERROR_MARK)
21044 return error_mark_node;
21045
21046 if (op != NOP_EXPR)
21047 {
21048 rhs = cp_parser_assignment_expression (parser, false, NULL);
21049 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21050 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21051 }
21052
21053 lhs = cp_parser_binary_expression (parser, false,
21054 PREC_ADDITIVE_EXPRESSION, NULL);
21055 token = cp_lexer_peek_token (parser->lexer);
21056 decl_first = lhs == decl;
21057 if (decl_first)
21058 lhs = NULL_TREE;
21059 if (token->type != CPP_PLUS
21060 && token->type != CPP_MINUS)
21061 return error_mark_node;
21062
21063 do
21064 {
21065 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21066 cp_lexer_consume_token (parser->lexer);
21067 rhs = cp_parser_binary_expression (parser, false,
21068 PREC_ADDITIVE_EXPRESSION, NULL);
21069 token = cp_lexer_peek_token (parser->lexer);
21070 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21071 {
21072 if (lhs == NULL_TREE)
21073 {
21074 if (op == PLUS_EXPR)
21075 lhs = rhs;
21076 else
21077 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21078 }
21079 else
21080 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21081 NULL, tf_warning_or_error);
21082 }
21083 }
21084 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21085
21086 if (!decl_first)
21087 {
21088 if (rhs != decl || op == MINUS_EXPR)
21089 return error_mark_node;
21090 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21091 }
21092 else
21093 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21094
21095 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21096 }
21097
21098 /* Parse the restricted form of the for statement allowed by OpenMP. */
21099
21100 static tree
21101 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21102 {
21103 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21104 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21105 tree this_pre_body, cl;
21106 location_t loc_first;
21107 bool collapse_err = false;
21108 int i, collapse = 1, nbraces = 0;
21109
21110 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21111 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21112 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21113
21114 gcc_assert (collapse >= 1);
21115
21116 declv = make_tree_vec (collapse);
21117 initv = make_tree_vec (collapse);
21118 condv = make_tree_vec (collapse);
21119 incrv = make_tree_vec (collapse);
21120
21121 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21122
21123 for (i = 0; i < collapse; i++)
21124 {
21125 int bracecount = 0;
21126 bool add_private_clause = false;
21127 location_t loc;
21128
21129 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21130 {
21131 cp_parser_error (parser, "for statement expected");
21132 return NULL;
21133 }
21134 loc = cp_lexer_consume_token (parser->lexer)->location;
21135
21136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21137 return NULL;
21138
21139 init = decl = real_decl = NULL;
21140 this_pre_body = push_stmt_list ();
21141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21142 {
21143 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21144
21145 init-expr:
21146 var = lb
21147 integer-type var = lb
21148 random-access-iterator-type var = lb
21149 pointer-type var = lb
21150 */
21151 cp_decl_specifier_seq type_specifiers;
21152
21153 /* First, try to parse as an initialized declaration. See
21154 cp_parser_condition, from whence the bulk of this is copied. */
21155
21156 cp_parser_parse_tentatively (parser);
21157 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21158 &type_specifiers);
21159 if (cp_parser_parse_definitely (parser))
21160 {
21161 /* If parsing a type specifier seq succeeded, then this
21162 MUST be a initialized declaration. */
21163 tree asm_specification, attributes;
21164 cp_declarator *declarator;
21165
21166 declarator = cp_parser_declarator (parser,
21167 CP_PARSER_DECLARATOR_NAMED,
21168 /*ctor_dtor_or_conv_p=*/NULL,
21169 /*parenthesized_p=*/NULL,
21170 /*member_p=*/false);
21171 attributes = cp_parser_attributes_opt (parser);
21172 asm_specification = cp_parser_asm_specification_opt (parser);
21173
21174 if (declarator == cp_error_declarator)
21175 cp_parser_skip_to_end_of_statement (parser);
21176
21177 else
21178 {
21179 tree pushed_scope, auto_node;
21180
21181 decl = start_decl (declarator, &type_specifiers,
21182 SD_INITIALIZED, attributes,
21183 /*prefix_attributes=*/NULL_TREE,
21184 &pushed_scope);
21185
21186 auto_node = type_uses_auto (TREE_TYPE (decl));
21187 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21188 {
21189 if (cp_lexer_next_token_is (parser->lexer,
21190 CPP_OPEN_PAREN))
21191 error ("parenthesized initialization is not allowed in "
21192 "OpenMP %<for%> loop");
21193 else
21194 /* Trigger an error. */
21195 cp_parser_require (parser, CPP_EQ, "%<=%>");
21196
21197 init = error_mark_node;
21198 cp_parser_skip_to_end_of_statement (parser);
21199 }
21200 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21201 || type_dependent_expression_p (decl)
21202 || auto_node)
21203 {
21204 bool is_direct_init, is_non_constant_init;
21205
21206 init = cp_parser_initializer (parser,
21207 &is_direct_init,
21208 &is_non_constant_init);
21209
21210 if (auto_node && describable_type (init))
21211 {
21212 TREE_TYPE (decl)
21213 = do_auto_deduction (TREE_TYPE (decl), init,
21214 auto_node);
21215
21216 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21217 && !type_dependent_expression_p (decl))
21218 goto non_class;
21219 }
21220
21221 cp_finish_decl (decl, init, !is_non_constant_init,
21222 asm_specification,
21223 LOOKUP_ONLYCONVERTING);
21224 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21225 {
21226 for_block
21227 = tree_cons (NULL, this_pre_body, for_block);
21228 init = NULL_TREE;
21229 }
21230 else
21231 init = pop_stmt_list (this_pre_body);
21232 this_pre_body = NULL_TREE;
21233 }
21234 else
21235 {
21236 /* Consume '='. */
21237 cp_lexer_consume_token (parser->lexer);
21238 init = cp_parser_assignment_expression (parser, false, NULL);
21239
21240 non_class:
21241 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21242 init = error_mark_node;
21243 else
21244 cp_finish_decl (decl, NULL_TREE,
21245 /*init_const_expr_p=*/false,
21246 asm_specification,
21247 LOOKUP_ONLYCONVERTING);
21248 }
21249
21250 if (pushed_scope)
21251 pop_scope (pushed_scope);
21252 }
21253 }
21254 else
21255 {
21256 cp_id_kind idk;
21257 /* If parsing a type specifier sequence failed, then
21258 this MUST be a simple expression. */
21259 cp_parser_parse_tentatively (parser);
21260 decl = cp_parser_primary_expression (parser, false, false,
21261 false, &idk);
21262 if (!cp_parser_error_occurred (parser)
21263 && decl
21264 && DECL_P (decl)
21265 && CLASS_TYPE_P (TREE_TYPE (decl)))
21266 {
21267 tree rhs;
21268
21269 cp_parser_parse_definitely (parser);
21270 cp_parser_require (parser, CPP_EQ, "%<=%>");
21271 rhs = cp_parser_assignment_expression (parser, false, NULL);
21272 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21273 rhs,
21274 tf_warning_or_error));
21275 add_private_clause = true;
21276 }
21277 else
21278 {
21279 decl = NULL;
21280 cp_parser_abort_tentative_parse (parser);
21281 init = cp_parser_expression (parser, false, NULL);
21282 if (init)
21283 {
21284 if (TREE_CODE (init) == MODIFY_EXPR
21285 || TREE_CODE (init) == MODOP_EXPR)
21286 real_decl = TREE_OPERAND (init, 0);
21287 }
21288 }
21289 }
21290 }
21291 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21292 if (this_pre_body)
21293 {
21294 this_pre_body = pop_stmt_list (this_pre_body);
21295 if (pre_body)
21296 {
21297 tree t = pre_body;
21298 pre_body = push_stmt_list ();
21299 add_stmt (t);
21300 add_stmt (this_pre_body);
21301 pre_body = pop_stmt_list (pre_body);
21302 }
21303 else
21304 pre_body = this_pre_body;
21305 }
21306
21307 if (decl)
21308 real_decl = decl;
21309 if (par_clauses != NULL && real_decl != NULL_TREE)
21310 {
21311 tree *c;
21312 for (c = par_clauses; *c ; )
21313 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21314 && OMP_CLAUSE_DECL (*c) == real_decl)
21315 {
21316 error ("%Hiteration variable %qD should not be firstprivate",
21317 &loc, real_decl);
21318 *c = OMP_CLAUSE_CHAIN (*c);
21319 }
21320 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21321 && OMP_CLAUSE_DECL (*c) == real_decl)
21322 {
21323 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21324 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21325 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21326 OMP_CLAUSE_DECL (l) = real_decl;
21327 OMP_CLAUSE_CHAIN (l) = clauses;
21328 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21329 clauses = l;
21330 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21331 CP_OMP_CLAUSE_INFO (*c) = NULL;
21332 add_private_clause = false;
21333 }
21334 else
21335 {
21336 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21337 && OMP_CLAUSE_DECL (*c) == real_decl)
21338 add_private_clause = false;
21339 c = &OMP_CLAUSE_CHAIN (*c);
21340 }
21341 }
21342
21343 if (add_private_clause)
21344 {
21345 tree c;
21346 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21347 {
21348 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21349 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21350 && OMP_CLAUSE_DECL (c) == decl)
21351 break;
21352 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21353 && OMP_CLAUSE_DECL (c) == decl)
21354 error ("%Hiteration variable %qD should not be firstprivate",
21355 &loc, decl);
21356 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21357 && OMP_CLAUSE_DECL (c) == decl)
21358 error ("%Hiteration variable %qD should not be reduction",
21359 &loc, decl);
21360 }
21361 if (c == NULL)
21362 {
21363 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21364 OMP_CLAUSE_DECL (c) = decl;
21365 c = finish_omp_clauses (c);
21366 if (c)
21367 {
21368 OMP_CLAUSE_CHAIN (c) = clauses;
21369 clauses = c;
21370 }
21371 }
21372 }
21373
21374 cond = NULL;
21375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21376 {
21377 /* If decl is an iterator, preserve LHS and RHS of the relational
21378 expr until finish_omp_for. */
21379 if (decl
21380 && (type_dependent_expression_p (decl)
21381 || CLASS_TYPE_P (TREE_TYPE (decl))))
21382 cond = cp_parser_omp_for_cond (parser, decl);
21383 else
21384 cond = cp_parser_condition (parser);
21385 }
21386 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21387
21388 incr = NULL;
21389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21390 {
21391 /* If decl is an iterator, preserve the operator on decl
21392 until finish_omp_for. */
21393 if (decl
21394 && (type_dependent_expression_p (decl)
21395 || CLASS_TYPE_P (TREE_TYPE (decl))))
21396 incr = cp_parser_omp_for_incr (parser, decl);
21397 else
21398 incr = cp_parser_expression (parser, false, NULL);
21399 }
21400
21401 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21402 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21403 /*or_comma=*/false,
21404 /*consume_paren=*/true);
21405
21406 TREE_VEC_ELT (declv, i) = decl;
21407 TREE_VEC_ELT (initv, i) = init;
21408 TREE_VEC_ELT (condv, i) = cond;
21409 TREE_VEC_ELT (incrv, i) = incr;
21410
21411 if (i == collapse - 1)
21412 break;
21413
21414 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21415 in between the collapsed for loops to be still considered perfectly
21416 nested. Hopefully the final version clarifies this.
21417 For now handle (multiple) {'s and empty statements. */
21418 cp_parser_parse_tentatively (parser);
21419 do
21420 {
21421 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21422 break;
21423 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21424 {
21425 cp_lexer_consume_token (parser->lexer);
21426 bracecount++;
21427 }
21428 else if (bracecount
21429 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21430 cp_lexer_consume_token (parser->lexer);
21431 else
21432 {
21433 loc = cp_lexer_peek_token (parser->lexer)->location;
21434 error ("%Hnot enough collapsed for loops", &loc);
21435 collapse_err = true;
21436 cp_parser_abort_tentative_parse (parser);
21437 declv = NULL_TREE;
21438 break;
21439 }
21440 }
21441 while (1);
21442
21443 if (declv)
21444 {
21445 cp_parser_parse_definitely (parser);
21446 nbraces += bracecount;
21447 }
21448 }
21449
21450 /* Note that we saved the original contents of this flag when we entered
21451 the structured block, and so we don't need to re-save it here. */
21452 parser->in_statement = IN_OMP_FOR;
21453
21454 /* Note that the grammar doesn't call for a structured block here,
21455 though the loop as a whole is a structured block. */
21456 body = push_stmt_list ();
21457 cp_parser_statement (parser, NULL_TREE, false, NULL);
21458 body = pop_stmt_list (body);
21459
21460 if (declv == NULL_TREE)
21461 ret = NULL_TREE;
21462 else
21463 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21464 pre_body, clauses);
21465
21466 while (nbraces)
21467 {
21468 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21469 {
21470 cp_lexer_consume_token (parser->lexer);
21471 nbraces--;
21472 }
21473 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21474 cp_lexer_consume_token (parser->lexer);
21475 else
21476 {
21477 if (!collapse_err)
21478 {
21479 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21480 error ("%Hcollapsed loops not perfectly nested", &loc);
21481 }
21482 collapse_err = true;
21483 cp_parser_statement_seq_opt (parser, NULL);
21484 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21485 }
21486 }
21487
21488 while (for_block)
21489 {
21490 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21491 for_block = TREE_CHAIN (for_block);
21492 }
21493
21494 return ret;
21495 }
21496
21497 /* OpenMP 2.5:
21498 #pragma omp for for-clause[optseq] new-line
21499 for-loop */
21500
21501 #define OMP_FOR_CLAUSE_MASK \
21502 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21503 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21504 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21505 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21506 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21507 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21508 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21509 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21510
21511 static tree
21512 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21513 {
21514 tree clauses, sb, ret;
21515 unsigned int save;
21516
21517 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21518 "#pragma omp for", pragma_tok);
21519
21520 sb = begin_omp_structured_block ();
21521 save = cp_parser_begin_omp_structured_block (parser);
21522
21523 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21524
21525 cp_parser_end_omp_structured_block (parser, save);
21526 add_stmt (finish_omp_structured_block (sb));
21527
21528 return ret;
21529 }
21530
21531 /* OpenMP 2.5:
21532 # pragma omp master new-line
21533 structured-block */
21534
21535 static tree
21536 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21537 {
21538 cp_parser_require_pragma_eol (parser, pragma_tok);
21539 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21540 }
21541
21542 /* OpenMP 2.5:
21543 # pragma omp ordered new-line
21544 structured-block */
21545
21546 static tree
21547 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21548 {
21549 cp_parser_require_pragma_eol (parser, pragma_tok);
21550 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21551 }
21552
21553 /* OpenMP 2.5:
21554
21555 section-scope:
21556 { section-sequence }
21557
21558 section-sequence:
21559 section-directive[opt] structured-block
21560 section-sequence section-directive structured-block */
21561
21562 static tree
21563 cp_parser_omp_sections_scope (cp_parser *parser)
21564 {
21565 tree stmt, substmt;
21566 bool error_suppress = false;
21567 cp_token *tok;
21568
21569 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21570 return NULL_TREE;
21571
21572 stmt = push_stmt_list ();
21573
21574 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21575 {
21576 unsigned save;
21577
21578 substmt = begin_omp_structured_block ();
21579 save = cp_parser_begin_omp_structured_block (parser);
21580
21581 while (1)
21582 {
21583 cp_parser_statement (parser, NULL_TREE, false, NULL);
21584
21585 tok = cp_lexer_peek_token (parser->lexer);
21586 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21587 break;
21588 if (tok->type == CPP_CLOSE_BRACE)
21589 break;
21590 if (tok->type == CPP_EOF)
21591 break;
21592 }
21593
21594 cp_parser_end_omp_structured_block (parser, save);
21595 substmt = finish_omp_structured_block (substmt);
21596 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21597 add_stmt (substmt);
21598 }
21599
21600 while (1)
21601 {
21602 tok = cp_lexer_peek_token (parser->lexer);
21603 if (tok->type == CPP_CLOSE_BRACE)
21604 break;
21605 if (tok->type == CPP_EOF)
21606 break;
21607
21608 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21609 {
21610 cp_lexer_consume_token (parser->lexer);
21611 cp_parser_require_pragma_eol (parser, tok);
21612 error_suppress = false;
21613 }
21614 else if (!error_suppress)
21615 {
21616 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21617 error_suppress = true;
21618 }
21619
21620 substmt = cp_parser_omp_structured_block (parser);
21621 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21622 add_stmt (substmt);
21623 }
21624 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21625
21626 substmt = pop_stmt_list (stmt);
21627
21628 stmt = make_node (OMP_SECTIONS);
21629 TREE_TYPE (stmt) = void_type_node;
21630 OMP_SECTIONS_BODY (stmt) = substmt;
21631
21632 add_stmt (stmt);
21633 return stmt;
21634 }
21635
21636 /* OpenMP 2.5:
21637 # pragma omp sections sections-clause[optseq] newline
21638 sections-scope */
21639
21640 #define OMP_SECTIONS_CLAUSE_MASK \
21641 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21642 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21643 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21644 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21645 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21646
21647 static tree
21648 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21649 {
21650 tree clauses, ret;
21651
21652 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21653 "#pragma omp sections", pragma_tok);
21654
21655 ret = cp_parser_omp_sections_scope (parser);
21656 if (ret)
21657 OMP_SECTIONS_CLAUSES (ret) = clauses;
21658
21659 return ret;
21660 }
21661
21662 /* OpenMP 2.5:
21663 # pragma parallel parallel-clause new-line
21664 # pragma parallel for parallel-for-clause new-line
21665 # pragma parallel sections parallel-sections-clause new-line */
21666
21667 #define OMP_PARALLEL_CLAUSE_MASK \
21668 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21669 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21670 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21671 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21672 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21673 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21674 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21675 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21676
21677 static tree
21678 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21679 {
21680 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21681 const char *p_name = "#pragma omp parallel";
21682 tree stmt, clauses, par_clause, ws_clause, block;
21683 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21684 unsigned int save;
21685
21686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21687 {
21688 cp_lexer_consume_token (parser->lexer);
21689 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21690 p_name = "#pragma omp parallel for";
21691 mask |= OMP_FOR_CLAUSE_MASK;
21692 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21693 }
21694 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21695 {
21696 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21697 const char *p = IDENTIFIER_POINTER (id);
21698 if (strcmp (p, "sections") == 0)
21699 {
21700 cp_lexer_consume_token (parser->lexer);
21701 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21702 p_name = "#pragma omp parallel sections";
21703 mask |= OMP_SECTIONS_CLAUSE_MASK;
21704 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21705 }
21706 }
21707
21708 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21709 block = begin_omp_parallel ();
21710 save = cp_parser_begin_omp_structured_block (parser);
21711
21712 switch (p_kind)
21713 {
21714 case PRAGMA_OMP_PARALLEL:
21715 cp_parser_statement (parser, NULL_TREE, false, NULL);
21716 par_clause = clauses;
21717 break;
21718
21719 case PRAGMA_OMP_PARALLEL_FOR:
21720 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21721 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21722 break;
21723
21724 case PRAGMA_OMP_PARALLEL_SECTIONS:
21725 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21726 stmt = cp_parser_omp_sections_scope (parser);
21727 if (stmt)
21728 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21729 break;
21730
21731 default:
21732 gcc_unreachable ();
21733 }
21734
21735 cp_parser_end_omp_structured_block (parser, save);
21736 stmt = finish_omp_parallel (par_clause, block);
21737 if (p_kind != PRAGMA_OMP_PARALLEL)
21738 OMP_PARALLEL_COMBINED (stmt) = 1;
21739 return stmt;
21740 }
21741
21742 /* OpenMP 2.5:
21743 # pragma omp single single-clause[optseq] new-line
21744 structured-block */
21745
21746 #define OMP_SINGLE_CLAUSE_MASK \
21747 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21748 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21749 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21750 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21751
21752 static tree
21753 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21754 {
21755 tree stmt = make_node (OMP_SINGLE);
21756 TREE_TYPE (stmt) = void_type_node;
21757
21758 OMP_SINGLE_CLAUSES (stmt)
21759 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21760 "#pragma omp single", pragma_tok);
21761 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21762
21763 return add_stmt (stmt);
21764 }
21765
21766 /* OpenMP 3.0:
21767 # pragma omp task task-clause[optseq] new-line
21768 structured-block */
21769
21770 #define OMP_TASK_CLAUSE_MASK \
21771 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21772 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21773 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21774 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21775 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21776 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21777
21778 static tree
21779 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21780 {
21781 tree clauses, block;
21782 unsigned int save;
21783
21784 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21785 "#pragma omp task", pragma_tok);
21786 block = begin_omp_task ();
21787 save = cp_parser_begin_omp_structured_block (parser);
21788 cp_parser_statement (parser, NULL_TREE, false, NULL);
21789 cp_parser_end_omp_structured_block (parser, save);
21790 return finish_omp_task (clauses, block);
21791 }
21792
21793 /* OpenMP 3.0:
21794 # pragma omp taskwait new-line */
21795
21796 static void
21797 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21798 {
21799 cp_parser_require_pragma_eol (parser, pragma_tok);
21800 finish_omp_taskwait ();
21801 }
21802
21803 /* OpenMP 2.5:
21804 # pragma omp threadprivate (variable-list) */
21805
21806 static void
21807 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21808 {
21809 tree vars;
21810
21811 vars = cp_parser_omp_var_list (parser, 0, NULL);
21812 cp_parser_require_pragma_eol (parser, pragma_tok);
21813
21814 finish_omp_threadprivate (vars);
21815 }
21816
21817 /* Main entry point to OpenMP statement pragmas. */
21818
21819 static void
21820 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21821 {
21822 tree stmt;
21823
21824 switch (pragma_tok->pragma_kind)
21825 {
21826 case PRAGMA_OMP_ATOMIC:
21827 cp_parser_omp_atomic (parser, pragma_tok);
21828 return;
21829 case PRAGMA_OMP_CRITICAL:
21830 stmt = cp_parser_omp_critical (parser, pragma_tok);
21831 break;
21832 case PRAGMA_OMP_FOR:
21833 stmt = cp_parser_omp_for (parser, pragma_tok);
21834 break;
21835 case PRAGMA_OMP_MASTER:
21836 stmt = cp_parser_omp_master (parser, pragma_tok);
21837 break;
21838 case PRAGMA_OMP_ORDERED:
21839 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21840 break;
21841 case PRAGMA_OMP_PARALLEL:
21842 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21843 break;
21844 case PRAGMA_OMP_SECTIONS:
21845 stmt = cp_parser_omp_sections (parser, pragma_tok);
21846 break;
21847 case PRAGMA_OMP_SINGLE:
21848 stmt = cp_parser_omp_single (parser, pragma_tok);
21849 break;
21850 case PRAGMA_OMP_TASK:
21851 stmt = cp_parser_omp_task (parser, pragma_tok);
21852 break;
21853 default:
21854 gcc_unreachable ();
21855 }
21856
21857 if (stmt)
21858 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21859 }
21860 \f
21861 /* The parser. */
21862
21863 static GTY (()) cp_parser *the_parser;
21864
21865 \f
21866 /* Special handling for the first token or line in the file. The first
21867 thing in the file might be #pragma GCC pch_preprocess, which loads a
21868 PCH file, which is a GC collection point. So we need to handle this
21869 first pragma without benefit of an existing lexer structure.
21870
21871 Always returns one token to the caller in *FIRST_TOKEN. This is
21872 either the true first token of the file, or the first token after
21873 the initial pragma. */
21874
21875 static void
21876 cp_parser_initial_pragma (cp_token *first_token)
21877 {
21878 tree name = NULL;
21879
21880 cp_lexer_get_preprocessor_token (NULL, first_token);
21881 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21882 return;
21883
21884 cp_lexer_get_preprocessor_token (NULL, first_token);
21885 if (first_token->type == CPP_STRING)
21886 {
21887 name = first_token->u.value;
21888
21889 cp_lexer_get_preprocessor_token (NULL, first_token);
21890 if (first_token->type != CPP_PRAGMA_EOL)
21891 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21892 &first_token->location);
21893 }
21894 else
21895 error ("%Hexpected string literal", &first_token->location);
21896
21897 /* Skip to the end of the pragma. */
21898 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21899 cp_lexer_get_preprocessor_token (NULL, first_token);
21900
21901 /* Now actually load the PCH file. */
21902 if (name)
21903 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21904
21905 /* Read one more token to return to our caller. We have to do this
21906 after reading the PCH file in, since its pointers have to be
21907 live. */
21908 cp_lexer_get_preprocessor_token (NULL, first_token);
21909 }
21910
21911 /* Normal parsing of a pragma token. Here we can (and must) use the
21912 regular lexer. */
21913
21914 static bool
21915 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21916 {
21917 cp_token *pragma_tok;
21918 unsigned int id;
21919
21920 pragma_tok = cp_lexer_consume_token (parser->lexer);
21921 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21922 parser->lexer->in_pragma = true;
21923
21924 id = pragma_tok->pragma_kind;
21925 switch (id)
21926 {
21927 case PRAGMA_GCC_PCH_PREPROCESS:
21928 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21929 &pragma_tok->location);
21930 break;
21931
21932 case PRAGMA_OMP_BARRIER:
21933 switch (context)
21934 {
21935 case pragma_compound:
21936 cp_parser_omp_barrier (parser, pragma_tok);
21937 return false;
21938 case pragma_stmt:
21939 error ("%H%<#pragma omp barrier%> may only be "
21940 "used in compound statements", &pragma_tok->location);
21941 break;
21942 default:
21943 goto bad_stmt;
21944 }
21945 break;
21946
21947 case PRAGMA_OMP_FLUSH:
21948 switch (context)
21949 {
21950 case pragma_compound:
21951 cp_parser_omp_flush (parser, pragma_tok);
21952 return false;
21953 case pragma_stmt:
21954 error ("%H%<#pragma omp flush%> may only be "
21955 "used in compound statements", &pragma_tok->location);
21956 break;
21957 default:
21958 goto bad_stmt;
21959 }
21960 break;
21961
21962 case PRAGMA_OMP_TASKWAIT:
21963 switch (context)
21964 {
21965 case pragma_compound:
21966 cp_parser_omp_taskwait (parser, pragma_tok);
21967 return false;
21968 case pragma_stmt:
21969 error ("%H%<#pragma omp taskwait%> may only be "
21970 "used in compound statements",
21971 &pragma_tok->location);
21972 break;
21973 default:
21974 goto bad_stmt;
21975 }
21976 break;
21977
21978 case PRAGMA_OMP_THREADPRIVATE:
21979 cp_parser_omp_threadprivate (parser, pragma_tok);
21980 return false;
21981
21982 case PRAGMA_OMP_ATOMIC:
21983 case PRAGMA_OMP_CRITICAL:
21984 case PRAGMA_OMP_FOR:
21985 case PRAGMA_OMP_MASTER:
21986 case PRAGMA_OMP_ORDERED:
21987 case PRAGMA_OMP_PARALLEL:
21988 case PRAGMA_OMP_SECTIONS:
21989 case PRAGMA_OMP_SINGLE:
21990 case PRAGMA_OMP_TASK:
21991 if (context == pragma_external)
21992 goto bad_stmt;
21993 cp_parser_omp_construct (parser, pragma_tok);
21994 return true;
21995
21996 case PRAGMA_OMP_SECTION:
21997 error ("%H%<#pragma omp section%> may only be used in "
21998 "%<#pragma omp sections%> construct", &pragma_tok->location);
21999 break;
22000
22001 default:
22002 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22003 c_invoke_pragma_handler (id);
22004 break;
22005
22006 bad_stmt:
22007 cp_parser_error (parser, "expected declaration specifiers");
22008 break;
22009 }
22010
22011 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22012 return false;
22013 }
22014
22015 /* The interface the pragma parsers have to the lexer. */
22016
22017 enum cpp_ttype
22018 pragma_lex (tree *value)
22019 {
22020 cp_token *tok;
22021 enum cpp_ttype ret;
22022
22023 tok = cp_lexer_peek_token (the_parser->lexer);
22024
22025 ret = tok->type;
22026 *value = tok->u.value;
22027
22028 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22029 ret = CPP_EOF;
22030 else if (ret == CPP_STRING)
22031 *value = cp_parser_string_literal (the_parser, false, false);
22032 else
22033 {
22034 cp_lexer_consume_token (the_parser->lexer);
22035 if (ret == CPP_KEYWORD)
22036 ret = CPP_NAME;
22037 }
22038
22039 return ret;
22040 }
22041
22042 \f
22043 /* External interface. */
22044
22045 /* Parse one entire translation unit. */
22046
22047 void
22048 c_parse_file (void)
22049 {
22050 bool error_occurred;
22051 static bool already_called = false;
22052
22053 if (already_called)
22054 {
22055 sorry ("inter-module optimizations not implemented for C++");
22056 return;
22057 }
22058 already_called = true;
22059
22060 the_parser = cp_parser_new ();
22061 push_deferring_access_checks (flag_access_control
22062 ? dk_no_deferred : dk_no_check);
22063 error_occurred = cp_parser_translation_unit (the_parser);
22064 the_parser = NULL;
22065 }
22066
22067 #include "gt-cp-parser.h"