]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
parser.c (struct cp_parser): Update comment for greater_than_is_operator_p.
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
50
51 struct tree_check GTY(())
52 {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
60 };
61
62 /* A C++ token. */
63
64 typedef struct cp_token GTY (())
65 {
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
71 /* Token flags. */
72 unsigned char flags;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
93 location_t location;
94 } cp_token;
95
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
100
101 static const cp_token eof_token =
102 {
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
105 0
106 #else
107 {0, 0}
108 #endif
109 };
110
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
114 it is created. */
115
116 typedef struct cp_lexer GTY (())
117 {
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
122 buffer. */
123 size_t buffer_length;
124
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
128
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
132
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
136 tokens. */
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
141
142 /* True if we should output debugging information. */
143 bool debugging_p;
144
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
147 bool in_pragma;
148 } cp_lexer;
149
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
154 a lexer. */
155
156 typedef struct cp_token_cache GTY(())
157 {
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
160
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
163 } cp_token_cache;
164
165 /* Prototypes. */
166
167 static cp_lexer *cp_lexer_new_main
168 (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172 (cp_lexer *);
173 static int cp_lexer_saving_tokens
174 (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176 (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182 (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192 (cp_lexer *);
193 static void cp_lexer_purge_token
194 (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198 (cp_lexer *);
199 static void cp_lexer_commit_tokens
200 (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202 (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207 (cp_lexer *);
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
221
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
224
225 static void cp_parser_initial_pragma
226 (cp_token *);
227
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
231
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255
256 /* Variables. */
257
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
262
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264 preprocessor. */
265
266 static cp_lexer *
267 cp_lexer_new_main (void)
268 {
269 cp_token first_token;
270 cp_lexer *lexer;
271 cp_token *pos;
272 size_t alloc;
273 size_t space;
274 cp_token *buffer;
275
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
280
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
283
284 c_common_no_more_pch ();
285
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
288
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
295
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
299
300 /* Put the first token in the buffer. */
301 space = alloc;
302 pos = buffer;
303 *pos = first_token;
304
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
307 {
308 pos++;
309 if (!--space)
310 {
311 space = alloc;
312 alloc *= 2;
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
315 }
316 cp_lexer_get_preprocessor_token (lexer, pos);
317 }
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
330 }
331
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
334
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
337 {
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
341
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
347
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
350
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
354 #endif
355
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
357 return lexer;
358 }
359
360 /* Frees all resources associated with LEXER. */
361
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
364 {
365 if (lexer->buffer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
368 ggc_free (lexer);
369 }
370
371 /* Returns nonzero if debugging information should be output. */
372
373 #ifdef ENABLE_CHECKING
374
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
377 {
378 return lexer->debugging_p;
379 }
380
381 #endif /* ENABLE_CHECKING */
382
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385 {
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
387
388 return lexer->next_token - previous_p;
389 }
390
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393 {
394 return pos;
395 }
396
397 /* nonzero if we are presently saving tokens. */
398
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
401 {
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403 }
404
405 /* Store the next token from the preprocessor in *TOKEN. Return true
406 if we reach EOF. */
407
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410 cp_token *token)
411 {
412 static int is_extern_c = 0;
413
414 /* Get a new token from the preprocessor. */
415 token->type
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
421
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
428
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
431 {
432 if (C_IS_RESERVED_WORD (token->u.value))
433 {
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
443 }
444 else
445 {
446 if (warn_cxx0x_compat
447 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
448 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
449 {
450 /* Warn about the C++0x keyword (but still treat it as
451 an identifier). */
452 warning (OPT_Wc__0x_compat,
453 "identifier %<%s%> will become a keyword in C++0x",
454 IDENTIFIER_POINTER (token->u.value));
455
456 /* Clear out the C_RID_CODE so we don't warn about this
457 particular identifier-turned-keyword again. */
458 C_RID_CODE (token->u.value) = RID_MAX;
459 }
460
461 token->ambiguous_p = false;
462 token->keyword = RID_MAX;
463 }
464 }
465 /* Handle Objective-C++ keywords. */
466 else if (token->type == CPP_AT_NAME)
467 {
468 token->type = CPP_KEYWORD;
469 switch (C_RID_CODE (token->u.value))
470 {
471 /* Map 'class' to '@class', 'private' to '@private', etc. */
472 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
473 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
474 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
475 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
476 case RID_THROW: token->keyword = RID_AT_THROW; break;
477 case RID_TRY: token->keyword = RID_AT_TRY; break;
478 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
479 default: token->keyword = C_RID_CODE (token->u.value);
480 }
481 }
482 else if (token->type == CPP_PRAGMA)
483 {
484 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
485 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
486 token->u.value = NULL_TREE;
487 }
488 }
489
490 /* Update the globals input_location and in_system_header and the
491 input file stack from TOKEN. */
492 static inline void
493 cp_lexer_set_source_position_from_token (cp_token *token)
494 {
495 if (token->type != CPP_EOF)
496 {
497 input_location = token->location;
498 in_system_header = token->in_system_header;
499 restore_input_file_stack (token->input_file_stack_index);
500 }
501 }
502
503 /* Return a pointer to the next token in the token stream, but do not
504 consume it. */
505
506 static inline cp_token *
507 cp_lexer_peek_token (cp_lexer *lexer)
508 {
509 if (cp_lexer_debugging_p (lexer))
510 {
511 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
512 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
513 putc ('\n', cp_lexer_debug_stream);
514 }
515 return lexer->next_token;
516 }
517
518 /* Return true if the next token has the indicated TYPE. */
519
520 static inline bool
521 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
522 {
523 return cp_lexer_peek_token (lexer)->type == type;
524 }
525
526 /* Return true if the next token does not have the indicated TYPE. */
527
528 static inline bool
529 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
530 {
531 return !cp_lexer_next_token_is (lexer, type);
532 }
533
534 /* Return true if the next token is the indicated KEYWORD. */
535
536 static inline bool
537 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
538 {
539 return cp_lexer_peek_token (lexer)->keyword == keyword;
540 }
541
542 /* Return true if the next token is a keyword for a decl-specifier. */
543
544 static bool
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
546 {
547 cp_token *token;
548
549 token = cp_lexer_peek_token (lexer);
550 switch (token->keyword)
551 {
552 /* Storage classes. */
553 case RID_AUTO:
554 case RID_REGISTER:
555 case RID_STATIC:
556 case RID_EXTERN:
557 case RID_MUTABLE:
558 case RID_THREAD:
559 /* Elaborated type specifiers. */
560 case RID_ENUM:
561 case RID_CLASS:
562 case RID_STRUCT:
563 case RID_UNION:
564 case RID_TYPENAME:
565 /* Simple type specifiers. */
566 case RID_CHAR:
567 case RID_WCHAR:
568 case RID_BOOL:
569 case RID_SHORT:
570 case RID_INT:
571 case RID_LONG:
572 case RID_SIGNED:
573 case RID_UNSIGNED:
574 case RID_FLOAT:
575 case RID_DOUBLE:
576 case RID_VOID:
577 /* GNU extensions. */
578 case RID_ATTRIBUTE:
579 case RID_TYPEOF:
580 return true;
581
582 default:
583 return false;
584 }
585 }
586
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
592
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 {
596 cp_token *token;
597
598 /* N is 1-based, not zero-based. */
599 gcc_assert (n > 0);
600
601 if (cp_lexer_debugging_p (lexer))
602 fprintf (cp_lexer_debug_stream,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604
605 --n;
606 token = lexer->next_token;
607 gcc_assert (!n || token != &eof_token);
608 while (n != 0)
609 {
610 ++token;
611 if (token == lexer->last_token)
612 {
613 token = (cp_token *)&eof_token;
614 break;
615 }
616
617 if (token->type != CPP_PURGED)
618 --n;
619 }
620
621 if (cp_lexer_debugging_p (lexer))
622 {
623 cp_lexer_print_token (cp_lexer_debug_stream, token);
624 putc ('\n', cp_lexer_debug_stream);
625 }
626
627 return token;
628 }
629
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
632
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
635 {
636 cp_token *token = lexer->next_token;
637
638 gcc_assert (token != &eof_token);
639 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640
641 do
642 {
643 lexer->next_token++;
644 if (lexer->next_token == lexer->last_token)
645 {
646 lexer->next_token = (cp_token *)&eof_token;
647 break;
648 }
649
650 }
651 while (lexer->next_token->type == CPP_PURGED);
652
653 cp_lexer_set_source_position_from_token (token);
654
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
657 {
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659 cp_lexer_print_token (cp_lexer_debug_stream, token);
660 putc ('\n', cp_lexer_debug_stream);
661 }
662
663 return token;
664 }
665
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
668 token. */
669
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
672 {
673 cp_token *tok = lexer->next_token;
674
675 gcc_assert (tok != &eof_token);
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->u.value = NULL_TREE;
679 tok->keyword = RID_MAX;
680
681 do
682 {
683 tok++;
684 if (tok == lexer->last_token)
685 {
686 tok = (cp_token *)&eof_token;
687 break;
688 }
689 }
690 while (tok->type == CPP_PURGED);
691 lexer->next_token = tok;
692 }
693
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
697
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 {
701 cp_token *peek = lexer->next_token;
702
703 if (peek == &eof_token)
704 peek = lexer->last_token;
705
706 gcc_assert (tok < peek);
707
708 for ( tok += 1; tok != peek; tok += 1)
709 {
710 tok->type = CPP_PURGED;
711 tok->location = UNKNOWN_LOCATION;
712 tok->u.value = NULL_TREE;
713 tok->keyword = RID_MAX;
714 }
715 }
716
717 /* Begin saving tokens. All tokens consumed after this point will be
718 preserved. */
719
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
722 {
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726
727 VEC_safe_push (cp_token_position, heap,
728 lexer->saved_tokens, lexer->next_token);
729 }
730
731 /* Commit to the portion of the token stream most recently saved. */
732
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
735 {
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739
740 VEC_pop (cp_token_position, lexer->saved_tokens);
741 }
742
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
745
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 {
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752
753 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 }
755
756 /* Print a representation of the TOKEN on the STREAM. */
757
758 #ifdef ENABLE_CHECKING
759
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
762 {
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names[] = {
766 /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769 TTYPE_TABLE
770 #undef OP
771 #undef TK
772 /* C++ parser token types - see "Manifest constants", above. */
773 "KEYWORD",
774 "TEMPLATE_ID",
775 "NESTED_NAME_SPECIFIER",
776 "PURGED"
777 };
778
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token->type < ARRAY_SIZE(token_names));
782 fputs (token_names[token->type], stream);
783
784 /* For some tokens, print the associated data. */
785 switch (token->type)
786 {
787 case CPP_KEYWORD:
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 break;
792 /* else fall through */
793 case CPP_NAME:
794 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 break;
796
797 case CPP_STRING:
798 case CPP_WSTRING:
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800 break;
801
802 default:
803 break;
804 }
805 }
806
807 /* Start emitting debugging information. */
808
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
811 {
812 lexer->debugging_p = true;
813 }
814
815 /* Stop emitting debugging information. */
816
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
819 {
820 lexer->debugging_p = false;
821 }
822
823 #endif /* ENABLE_CHECKING */
824
825 /* Create a new cp_token_cache, representing a range of tokens. */
826
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
829 {
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
832 cache->last = last;
833 return cache;
834 }
835
836 \f
837 /* Decl-specifiers. */
838
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
840
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 {
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 }
846
847 /* Declarators. */
848
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
853
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
866
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
869
870 /* The obstack on which declarators and related data structures are
871 allocated. */
872 static struct obstack declarator_obstack;
873
874 /* Alloc BYTES from the declarator memory pool. */
875
876 static inline void *
877 alloc_declarator (size_t bytes)
878 {
879 return obstack_alloc (&declarator_obstack, bytes);
880 }
881
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
884
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
887 {
888 cp_declarator *declarator;
889
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
895
896 return declarator;
897 }
898
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 is, if any. */
904
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
908 {
909 cp_declarator *declarator;
910
911 /* It is valid to write:
912
913 class C { void f(); };
914 typedef C D;
915 void D::f();
916
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
920 well. */
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
932
933 return declarator;
934 }
935
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
939
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 {
943 cp_declarator *declarator;
944
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
949 if (target)
950 {
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
953 }
954 else
955 declarator->parameter_pack_p = false;
956
957 return declarator;
958 }
959
960 /* Like make_pointer_declarator -- but for references. */
961
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
964 {
965 cp_declarator *declarator;
966
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.pointer.qualifiers = cv_qualifiers;
970 declarator->u.pointer.class_type = NULL_TREE;
971 if (target)
972 {
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
975 }
976 else
977 declarator->parameter_pack_p = false;
978
979 return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
988 {
989 cp_declarator *declarator;
990
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
995
996 if (pointee)
997 {
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1000 }
1001 else
1002 declarator->parameter_pack_p = false;
1003
1004 return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014 cp_parameter_declarator *parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification)
1017 {
1018 cp_declarator *declarator;
1019
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 if (target)
1026 {
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1029 }
1030 else
1031 declarator->parameter_pack_p = false;
1032
1033 return declarator;
1034 }
1035
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1038
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1041 {
1042 cp_declarator *declarator;
1043
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1047 if (element)
1048 {
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1051 }
1052 else
1053 declarator->parameter_pack_p = false;
1054
1055 return declarator;
1056 }
1057
1058 cp_parameter_declarator *no_parameters;
1059
1060 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1061 DECLARATOR and DEFAULT_ARGUMENT. */
1062
1063 cp_parameter_declarator *
1064 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1065 cp_declarator *declarator,
1066 tree default_argument)
1067 {
1068 cp_parameter_declarator *parameter;
1069
1070 parameter = ((cp_parameter_declarator *)
1071 alloc_declarator (sizeof (cp_parameter_declarator)));
1072 parameter->next = NULL;
1073 if (decl_specifiers)
1074 parameter->decl_specifiers = *decl_specifiers;
1075 else
1076 clear_decl_specs (&parameter->decl_specifiers);
1077 parameter->declarator = declarator;
1078 parameter->default_argument = default_argument;
1079 parameter->ellipsis_p = false;
1080
1081 return parameter;
1082 }
1083
1084 /* Returns true iff DECLARATOR is a declaration for a function. */
1085
1086 static bool
1087 function_declarator_p (const cp_declarator *declarator)
1088 {
1089 while (declarator)
1090 {
1091 if (declarator->kind == cdk_function
1092 && declarator->declarator->kind == cdk_id)
1093 return true;
1094 if (declarator->kind == cdk_id
1095 || declarator->kind == cdk_error)
1096 return false;
1097 declarator = declarator->declarator;
1098 }
1099 return false;
1100 }
1101
1102 /* The parser. */
1103
1104 /* Overview
1105 --------
1106
1107 A cp_parser parses the token stream as specified by the C++
1108 grammar. Its job is purely parsing, not semantic analysis. For
1109 example, the parser breaks the token stream into declarators,
1110 expressions, statements, and other similar syntactic constructs.
1111 It does not check that the types of the expressions on either side
1112 of an assignment-statement are compatible, or that a function is
1113 not declared with a parameter of type `void'.
1114
1115 The parser invokes routines elsewhere in the compiler to perform
1116 semantic analysis and to build up the abstract syntax tree for the
1117 code processed.
1118
1119 The parser (and the template instantiation code, which is, in a
1120 way, a close relative of parsing) are the only parts of the
1121 compiler that should be calling push_scope and pop_scope, or
1122 related functions. The parser (and template instantiation code)
1123 keeps track of what scope is presently active; everything else
1124 should simply honor that. (The code that generates static
1125 initializers may also need to set the scope, in order to check
1126 access control correctly when emitting the initializers.)
1127
1128 Methodology
1129 -----------
1130
1131 The parser is of the standard recursive-descent variety. Upcoming
1132 tokens in the token stream are examined in order to determine which
1133 production to use when parsing a non-terminal. Some C++ constructs
1134 require arbitrary look ahead to disambiguate. For example, it is
1135 impossible, in the general case, to tell whether a statement is an
1136 expression or declaration without scanning the entire statement.
1137 Therefore, the parser is capable of "parsing tentatively." When the
1138 parser is not sure what construct comes next, it enters this mode.
1139 Then, while we attempt to parse the construct, the parser queues up
1140 error messages, rather than issuing them immediately, and saves the
1141 tokens it consumes. If the construct is parsed successfully, the
1142 parser "commits", i.e., it issues any queued error messages and
1143 the tokens that were being preserved are permanently discarded.
1144 If, however, the construct is not parsed successfully, the parser
1145 rolls back its state completely so that it can resume parsing using
1146 a different alternative.
1147
1148 Future Improvements
1149 -------------------
1150
1151 The performance of the parser could probably be improved substantially.
1152 We could often eliminate the need to parse tentatively by looking ahead
1153 a little bit. In some places, this approach might not entirely eliminate
1154 the need to parse tentatively, but it might still speed up the average
1155 case. */
1156
1157 /* Flags that are passed to some parsing functions. These values can
1158 be bitwise-ored together. */
1159
1160 typedef enum cp_parser_flags
1161 {
1162 /* No flags. */
1163 CP_PARSER_FLAGS_NONE = 0x0,
1164 /* The construct is optional. If it is not present, then no error
1165 should be issued. */
1166 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1167 /* When parsing a type-specifier, do not allow user-defined types. */
1168 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1169 } cp_parser_flags;
1170
1171 /* The different kinds of declarators we want to parse. */
1172
1173 typedef enum cp_parser_declarator_kind
1174 {
1175 /* We want an abstract declarator. */
1176 CP_PARSER_DECLARATOR_ABSTRACT,
1177 /* We want a named declarator. */
1178 CP_PARSER_DECLARATOR_NAMED,
1179 /* We don't mind, but the name must be an unqualified-id. */
1180 CP_PARSER_DECLARATOR_EITHER
1181 } cp_parser_declarator_kind;
1182
1183 /* The precedence values used to parse binary expressions. The minimum value
1184 of PREC must be 1, because zero is reserved to quickly discriminate
1185 binary operators from other tokens. */
1186
1187 enum cp_parser_prec
1188 {
1189 PREC_NOT_OPERATOR,
1190 PREC_LOGICAL_OR_EXPRESSION,
1191 PREC_LOGICAL_AND_EXPRESSION,
1192 PREC_INCLUSIVE_OR_EXPRESSION,
1193 PREC_EXCLUSIVE_OR_EXPRESSION,
1194 PREC_AND_EXPRESSION,
1195 PREC_EQUALITY_EXPRESSION,
1196 PREC_RELATIONAL_EXPRESSION,
1197 PREC_SHIFT_EXPRESSION,
1198 PREC_ADDITIVE_EXPRESSION,
1199 PREC_MULTIPLICATIVE_EXPRESSION,
1200 PREC_PM_EXPRESSION,
1201 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1202 };
1203
1204 /* A mapping from a token type to a corresponding tree node type, with a
1205 precedence value. */
1206
1207 typedef struct cp_parser_binary_operations_map_node
1208 {
1209 /* The token type. */
1210 enum cpp_ttype token_type;
1211 /* The corresponding tree code. */
1212 enum tree_code tree_type;
1213 /* The precedence of this operator. */
1214 enum cp_parser_prec prec;
1215 } cp_parser_binary_operations_map_node;
1216
1217 /* The status of a tentative parse. */
1218
1219 typedef enum cp_parser_status_kind
1220 {
1221 /* No errors have occurred. */
1222 CP_PARSER_STATUS_KIND_NO_ERROR,
1223 /* An error has occurred. */
1224 CP_PARSER_STATUS_KIND_ERROR,
1225 /* We are committed to this tentative parse, whether or not an error
1226 has occurred. */
1227 CP_PARSER_STATUS_KIND_COMMITTED
1228 } cp_parser_status_kind;
1229
1230 typedef struct cp_parser_expression_stack_entry
1231 {
1232 /* Left hand side of the binary operation we are currently
1233 parsing. */
1234 tree lhs;
1235 /* Original tree code for left hand side, if it was a binary
1236 expression itself (used for -Wparentheses). */
1237 enum tree_code lhs_type;
1238 /* Tree code for the binary operation we are parsing. */
1239 enum tree_code tree_type;
1240 /* Precedence of the binary operation we are parsing. */
1241 int prec;
1242 } cp_parser_expression_stack_entry;
1243
1244 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1245 entries because precedence levels on the stack are monotonically
1246 increasing. */
1247 typedef struct cp_parser_expression_stack_entry
1248 cp_parser_expression_stack[NUM_PREC_VALUES];
1249
1250 /* Context that is saved and restored when parsing tentatively. */
1251 typedef struct cp_parser_context GTY (())
1252 {
1253 /* If this is a tentative parsing context, the status of the
1254 tentative parse. */
1255 enum cp_parser_status_kind status;
1256 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1257 that are looked up in this context must be looked up both in the
1258 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1259 the context of the containing expression. */
1260 tree object_type;
1261
1262 /* The next parsing context in the stack. */
1263 struct cp_parser_context *next;
1264 } cp_parser_context;
1265
1266 /* Prototypes. */
1267
1268 /* Constructors and destructors. */
1269
1270 static cp_parser_context *cp_parser_context_new
1271 (cp_parser_context *);
1272
1273 /* Class variables. */
1274
1275 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1276
1277 /* The operator-precedence table used by cp_parser_binary_expression.
1278 Transformed into an associative array (binops_by_token) by
1279 cp_parser_new. */
1280
1281 static const cp_parser_binary_operations_map_node binops[] = {
1282 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1283 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1284
1285 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1286 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1287 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1288
1289 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1290 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1291
1292 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1293 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1294
1295 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1296 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1297 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1298 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1299
1300 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1301 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1302
1303 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1304
1305 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1306
1307 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1308
1309 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1310
1311 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1312 };
1313
1314 /* The same as binops, but initialized by cp_parser_new so that
1315 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1316 for speed. */
1317 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1318
1319 /* Constructors and destructors. */
1320
1321 /* Construct a new context. The context below this one on the stack
1322 is given by NEXT. */
1323
1324 static cp_parser_context *
1325 cp_parser_context_new (cp_parser_context* next)
1326 {
1327 cp_parser_context *context;
1328
1329 /* Allocate the storage. */
1330 if (cp_parser_context_free_list != NULL)
1331 {
1332 /* Pull the first entry from the free list. */
1333 context = cp_parser_context_free_list;
1334 cp_parser_context_free_list = context->next;
1335 memset (context, 0, sizeof (*context));
1336 }
1337 else
1338 context = GGC_CNEW (cp_parser_context);
1339
1340 /* No errors have occurred yet in this context. */
1341 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1342 /* If this is not the bottomost context, copy information that we
1343 need from the previous context. */
1344 if (next)
1345 {
1346 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1347 expression, then we are parsing one in this context, too. */
1348 context->object_type = next->object_type;
1349 /* Thread the stack. */
1350 context->next = next;
1351 }
1352
1353 return context;
1354 }
1355
1356 /* The cp_parser structure represents the C++ parser. */
1357
1358 typedef struct cp_parser GTY(())
1359 {
1360 /* The lexer from which we are obtaining tokens. */
1361 cp_lexer *lexer;
1362
1363 /* The scope in which names should be looked up. If NULL_TREE, then
1364 we look up names in the scope that is currently open in the
1365 source program. If non-NULL, this is either a TYPE or
1366 NAMESPACE_DECL for the scope in which we should look. It can
1367 also be ERROR_MARK, when we've parsed a bogus scope.
1368
1369 This value is not cleared automatically after a name is looked
1370 up, so we must be careful to clear it before starting a new look
1371 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1372 will look up `Z' in the scope of `X', rather than the current
1373 scope.) Unfortunately, it is difficult to tell when name lookup
1374 is complete, because we sometimes peek at a token, look it up,
1375 and then decide not to consume it. */
1376 tree scope;
1377
1378 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1379 last lookup took place. OBJECT_SCOPE is used if an expression
1380 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1381 respectively. QUALIFYING_SCOPE is used for an expression of the
1382 form "X::Y"; it refers to X. */
1383 tree object_scope;
1384 tree qualifying_scope;
1385
1386 /* A stack of parsing contexts. All but the bottom entry on the
1387 stack will be tentative contexts.
1388
1389 We parse tentatively in order to determine which construct is in
1390 use in some situations. For example, in order to determine
1391 whether a statement is an expression-statement or a
1392 declaration-statement we parse it tentatively as a
1393 declaration-statement. If that fails, we then reparse the same
1394 token stream as an expression-statement. */
1395 cp_parser_context *context;
1396
1397 /* True if we are parsing GNU C++. If this flag is not set, then
1398 GNU extensions are not recognized. */
1399 bool allow_gnu_extensions_p;
1400
1401 /* TRUE if the `>' token should be interpreted as the greater-than
1402 operator. FALSE if it is the end of a template-id or
1403 template-parameter-list. In C++0x mode, this flag also applies to
1404 `>>' tokens, which are viewed as two consecutive `>' tokens when
1405 this flag is FALSE. */
1406 bool greater_than_is_operator_p;
1407
1408 /* TRUE if default arguments are allowed within a parameter list
1409 that starts at this point. FALSE if only a gnu extension makes
1410 them permissible. */
1411 bool default_arg_ok_p;
1412
1413 /* TRUE if we are parsing an integral constant-expression. See
1414 [expr.const] for a precise definition. */
1415 bool integral_constant_expression_p;
1416
1417 /* TRUE if we are parsing an integral constant-expression -- but a
1418 non-constant expression should be permitted as well. This flag
1419 is used when parsing an array bound so that GNU variable-length
1420 arrays are tolerated. */
1421 bool allow_non_integral_constant_expression_p;
1422
1423 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1424 been seen that makes the expression non-constant. */
1425 bool non_integral_constant_expression_p;
1426
1427 /* TRUE if local variable names and `this' are forbidden in the
1428 current context. */
1429 bool local_variables_forbidden_p;
1430
1431 /* TRUE if the declaration we are parsing is part of a
1432 linkage-specification of the form `extern string-literal
1433 declaration'. */
1434 bool in_unbraced_linkage_specification_p;
1435
1436 /* TRUE if we are presently parsing a declarator, after the
1437 direct-declarator. */
1438 bool in_declarator_p;
1439
1440 /* TRUE if we are presently parsing a template-argument-list. */
1441 bool in_template_argument_list_p;
1442
1443 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1444 to IN_OMP_BLOCK if parsing OpenMP structured block and
1445 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1446 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1447 iteration-statement, OpenMP block or loop within that switch. */
1448 #define IN_SWITCH_STMT 1
1449 #define IN_ITERATION_STMT 2
1450 #define IN_OMP_BLOCK 4
1451 #define IN_OMP_FOR 8
1452 #define IN_IF_STMT 16
1453 unsigned char in_statement;
1454
1455 /* TRUE if we are presently parsing the body of a switch statement.
1456 Note that this doesn't quite overlap with in_statement above.
1457 The difference relates to giving the right sets of error messages:
1458 "case not in switch" vs "break statement used with OpenMP...". */
1459 bool in_switch_statement_p;
1460
1461 /* TRUE if we are parsing a type-id in an expression context. In
1462 such a situation, both "type (expr)" and "type (type)" are valid
1463 alternatives. */
1464 bool in_type_id_in_expr_p;
1465
1466 /* TRUE if we are currently in a header file where declarations are
1467 implicitly extern "C". */
1468 bool implicit_extern_c;
1469
1470 /* TRUE if strings in expressions should be translated to the execution
1471 character set. */
1472 bool translate_strings_p;
1473
1474 /* TRUE if we are presently parsing the body of a function, but not
1475 a local class. */
1476 bool in_function_body;
1477
1478 /* If non-NULL, then we are parsing a construct where new type
1479 definitions are not permitted. The string stored here will be
1480 issued as an error message if a type is defined. */
1481 const char *type_definition_forbidden_message;
1482
1483 /* A list of lists. The outer list is a stack, used for member
1484 functions of local classes. At each level there are two sub-list,
1485 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1486 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1487 TREE_VALUE's. The functions are chained in reverse declaration
1488 order.
1489
1490 The TREE_PURPOSE sublist contains those functions with default
1491 arguments that need post processing, and the TREE_VALUE sublist
1492 contains those functions with definitions that need post
1493 processing.
1494
1495 These lists can only be processed once the outermost class being
1496 defined is complete. */
1497 tree unparsed_functions_queues;
1498
1499 /* The number of classes whose definitions are currently in
1500 progress. */
1501 unsigned num_classes_being_defined;
1502
1503 /* The number of template parameter lists that apply directly to the
1504 current declaration. */
1505 unsigned num_template_parameter_lists;
1506 } cp_parser;
1507
1508 /* Prototypes. */
1509
1510 /* Constructors and destructors. */
1511
1512 static cp_parser *cp_parser_new
1513 (void);
1514
1515 /* Routines to parse various constructs.
1516
1517 Those that return `tree' will return the error_mark_node (rather
1518 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1519 Sometimes, they will return an ordinary node if error-recovery was
1520 attempted, even though a parse error occurred. So, to check
1521 whether or not a parse error occurred, you should always use
1522 cp_parser_error_occurred. If the construct is optional (indicated
1523 either by an `_opt' in the name of the function that does the
1524 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1525 the construct is not present. */
1526
1527 /* Lexical conventions [gram.lex] */
1528
1529 static tree cp_parser_identifier
1530 (cp_parser *);
1531 static tree cp_parser_string_literal
1532 (cp_parser *, bool, bool);
1533
1534 /* Basic concepts [gram.basic] */
1535
1536 static bool cp_parser_translation_unit
1537 (cp_parser *);
1538
1539 /* Expressions [gram.expr] */
1540
1541 static tree cp_parser_primary_expression
1542 (cp_parser *, bool, bool, bool, cp_id_kind *);
1543 static tree cp_parser_id_expression
1544 (cp_parser *, bool, bool, bool *, bool, bool);
1545 static tree cp_parser_unqualified_id
1546 (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier_opt
1548 (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_nested_name_specifier
1550 (cp_parser *, bool, bool, bool, bool);
1551 static tree cp_parser_class_or_namespace_name
1552 (cp_parser *, bool, bool, bool, bool, bool);
1553 static tree cp_parser_postfix_expression
1554 (cp_parser *, bool, bool);
1555 static tree cp_parser_postfix_open_square_expression
1556 (cp_parser *, tree, bool);
1557 static tree cp_parser_postfix_dot_deref_expression
1558 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1559 static tree cp_parser_parenthesized_expression_list
1560 (cp_parser *, bool, bool, bool, bool *);
1561 static void cp_parser_pseudo_destructor_name
1562 (cp_parser *, tree *, tree *);
1563 static tree cp_parser_unary_expression
1564 (cp_parser *, bool, bool);
1565 static enum tree_code cp_parser_unary_operator
1566 (cp_token *);
1567 static tree cp_parser_new_expression
1568 (cp_parser *);
1569 static tree cp_parser_new_placement
1570 (cp_parser *);
1571 static tree cp_parser_new_type_id
1572 (cp_parser *, tree *);
1573 static cp_declarator *cp_parser_new_declarator_opt
1574 (cp_parser *);
1575 static cp_declarator *cp_parser_direct_new_declarator
1576 (cp_parser *);
1577 static tree cp_parser_new_initializer
1578 (cp_parser *);
1579 static tree cp_parser_delete_expression
1580 (cp_parser *);
1581 static tree cp_parser_cast_expression
1582 (cp_parser *, bool, bool);
1583 static tree cp_parser_binary_expression
1584 (cp_parser *, bool);
1585 static tree cp_parser_question_colon_clause
1586 (cp_parser *, tree);
1587 static tree cp_parser_assignment_expression
1588 (cp_parser *, bool);
1589 static enum tree_code cp_parser_assignment_operator_opt
1590 (cp_parser *);
1591 static tree cp_parser_expression
1592 (cp_parser *, bool);
1593 static tree cp_parser_constant_expression
1594 (cp_parser *, bool, bool *);
1595 static tree cp_parser_builtin_offsetof
1596 (cp_parser *);
1597
1598 /* Statements [gram.stmt.stmt] */
1599
1600 static void cp_parser_statement
1601 (cp_parser *, tree, bool, bool *);
1602 static void cp_parser_label_for_labeled_statement
1603 (cp_parser *);
1604 static tree cp_parser_expression_statement
1605 (cp_parser *, tree);
1606 static tree cp_parser_compound_statement
1607 (cp_parser *, tree, bool);
1608 static void cp_parser_statement_seq_opt
1609 (cp_parser *, tree);
1610 static tree cp_parser_selection_statement
1611 (cp_parser *, bool *);
1612 static tree cp_parser_condition
1613 (cp_parser *);
1614 static tree cp_parser_iteration_statement
1615 (cp_parser *);
1616 static void cp_parser_for_init_statement
1617 (cp_parser *);
1618 static tree cp_parser_jump_statement
1619 (cp_parser *);
1620 static void cp_parser_declaration_statement
1621 (cp_parser *);
1622
1623 static tree cp_parser_implicitly_scoped_statement
1624 (cp_parser *, bool *);
1625 static void cp_parser_already_scoped_statement
1626 (cp_parser *);
1627
1628 /* Declarations [gram.dcl.dcl] */
1629
1630 static void cp_parser_declaration_seq_opt
1631 (cp_parser *);
1632 static void cp_parser_declaration
1633 (cp_parser *);
1634 static void cp_parser_block_declaration
1635 (cp_parser *, bool);
1636 static void cp_parser_simple_declaration
1637 (cp_parser *, bool);
1638 static void cp_parser_decl_specifier_seq
1639 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1641 (cp_parser *);
1642 static tree cp_parser_function_specifier_opt
1643 (cp_parser *, cp_decl_specifier_seq *);
1644 static tree cp_parser_type_specifier
1645 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1646 int *, bool *);
1647 static tree cp_parser_simple_type_specifier
1648 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1649 static tree cp_parser_type_name
1650 (cp_parser *);
1651 static tree cp_parser_elaborated_type_specifier
1652 (cp_parser *, bool, bool);
1653 static tree cp_parser_enum_specifier
1654 (cp_parser *);
1655 static void cp_parser_enumerator_list
1656 (cp_parser *, tree);
1657 static void cp_parser_enumerator_definition
1658 (cp_parser *, tree);
1659 static tree cp_parser_namespace_name
1660 (cp_parser *);
1661 static void cp_parser_namespace_definition
1662 (cp_parser *);
1663 static void cp_parser_namespace_body
1664 (cp_parser *);
1665 static tree cp_parser_qualified_namespace_specifier
1666 (cp_parser *);
1667 static void cp_parser_namespace_alias_definition
1668 (cp_parser *);
1669 static bool cp_parser_using_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_using_directive
1672 (cp_parser *);
1673 static void cp_parser_asm_definition
1674 (cp_parser *);
1675 static void cp_parser_linkage_specification
1676 (cp_parser *);
1677 static void cp_parser_static_assert
1678 (cp_parser *, bool);
1679
1680 /* Declarators [gram.dcl.decl] */
1681
1682 static tree cp_parser_init_declarator
1683 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1684 static cp_declarator *cp_parser_declarator
1685 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1686 static cp_declarator *cp_parser_direct_declarator
1687 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1688 static enum tree_code cp_parser_ptr_operator
1689 (cp_parser *, tree *, cp_cv_quals *);
1690 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1691 (cp_parser *);
1692 static tree cp_parser_declarator_id
1693 (cp_parser *, bool);
1694 static tree cp_parser_type_id
1695 (cp_parser *);
1696 static void cp_parser_type_specifier_seq
1697 (cp_parser *, bool, cp_decl_specifier_seq *);
1698 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1699 (cp_parser *);
1700 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1701 (cp_parser *, bool *);
1702 static cp_parameter_declarator *cp_parser_parameter_declaration
1703 (cp_parser *, bool, bool *);
1704 static void cp_parser_function_body
1705 (cp_parser *);
1706 static tree cp_parser_initializer
1707 (cp_parser *, bool *, bool *);
1708 static tree cp_parser_initializer_clause
1709 (cp_parser *, bool *);
1710 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1711 (cp_parser *, bool *);
1712
1713 static bool cp_parser_ctor_initializer_opt_and_function_body
1714 (cp_parser *);
1715
1716 /* Classes [gram.class] */
1717
1718 static tree cp_parser_class_name
1719 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1720 static tree cp_parser_class_specifier
1721 (cp_parser *);
1722 static tree cp_parser_class_head
1723 (cp_parser *, bool *, tree *, tree *);
1724 static enum tag_types cp_parser_class_key
1725 (cp_parser *);
1726 static void cp_parser_member_specification_opt
1727 (cp_parser *);
1728 static void cp_parser_member_declaration
1729 (cp_parser *);
1730 static tree cp_parser_pure_specifier
1731 (cp_parser *);
1732 static tree cp_parser_constant_initializer
1733 (cp_parser *);
1734
1735 /* Derived classes [gram.class.derived] */
1736
1737 static tree cp_parser_base_clause
1738 (cp_parser *);
1739 static tree cp_parser_base_specifier
1740 (cp_parser *);
1741
1742 /* Special member functions [gram.special] */
1743
1744 static tree cp_parser_conversion_function_id
1745 (cp_parser *);
1746 static tree cp_parser_conversion_type_id
1747 (cp_parser *);
1748 static cp_declarator *cp_parser_conversion_declarator_opt
1749 (cp_parser *);
1750 static bool cp_parser_ctor_initializer_opt
1751 (cp_parser *);
1752 static void cp_parser_mem_initializer_list
1753 (cp_parser *);
1754 static tree cp_parser_mem_initializer
1755 (cp_parser *);
1756 static tree cp_parser_mem_initializer_id
1757 (cp_parser *);
1758
1759 /* Overloading [gram.over] */
1760
1761 static tree cp_parser_operator_function_id
1762 (cp_parser *);
1763 static tree cp_parser_operator
1764 (cp_parser *);
1765
1766 /* Templates [gram.temp] */
1767
1768 static void cp_parser_template_declaration
1769 (cp_parser *, bool);
1770 static tree cp_parser_template_parameter_list
1771 (cp_parser *);
1772 static tree cp_parser_template_parameter
1773 (cp_parser *, bool *, bool *);
1774 static tree cp_parser_type_parameter
1775 (cp_parser *, bool *);
1776 static tree cp_parser_template_id
1777 (cp_parser *, bool, bool, bool);
1778 static tree cp_parser_template_name
1779 (cp_parser *, bool, bool, bool, bool *);
1780 static tree cp_parser_template_argument_list
1781 (cp_parser *);
1782 static tree cp_parser_template_argument
1783 (cp_parser *);
1784 static void cp_parser_explicit_instantiation
1785 (cp_parser *);
1786 static void cp_parser_explicit_specialization
1787 (cp_parser *);
1788
1789 /* Exception handling [gram.exception] */
1790
1791 static tree cp_parser_try_block
1792 (cp_parser *);
1793 static bool cp_parser_function_try_block
1794 (cp_parser *);
1795 static void cp_parser_handler_seq
1796 (cp_parser *);
1797 static void cp_parser_handler
1798 (cp_parser *);
1799 static tree cp_parser_exception_declaration
1800 (cp_parser *);
1801 static tree cp_parser_throw_expression
1802 (cp_parser *);
1803 static tree cp_parser_exception_specification_opt
1804 (cp_parser *);
1805 static tree cp_parser_type_id_list
1806 (cp_parser *);
1807
1808 /* GNU Extensions */
1809
1810 static tree cp_parser_asm_specification_opt
1811 (cp_parser *);
1812 static tree cp_parser_asm_operand_list
1813 (cp_parser *);
1814 static tree cp_parser_asm_clobber_list
1815 (cp_parser *);
1816 static tree cp_parser_attributes_opt
1817 (cp_parser *);
1818 static tree cp_parser_attribute_list
1819 (cp_parser *);
1820 static bool cp_parser_extension_opt
1821 (cp_parser *, int *);
1822 static void cp_parser_label_declaration
1823 (cp_parser *);
1824
1825 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1826 static bool cp_parser_pragma
1827 (cp_parser *, enum pragma_context);
1828
1829 /* Objective-C++ Productions */
1830
1831 static tree cp_parser_objc_message_receiver
1832 (cp_parser *);
1833 static tree cp_parser_objc_message_args
1834 (cp_parser *);
1835 static tree cp_parser_objc_message_expression
1836 (cp_parser *);
1837 static tree cp_parser_objc_encode_expression
1838 (cp_parser *);
1839 static tree cp_parser_objc_defs_expression
1840 (cp_parser *);
1841 static tree cp_parser_objc_protocol_expression
1842 (cp_parser *);
1843 static tree cp_parser_objc_selector_expression
1844 (cp_parser *);
1845 static tree cp_parser_objc_expression
1846 (cp_parser *);
1847 static bool cp_parser_objc_selector_p
1848 (enum cpp_ttype);
1849 static tree cp_parser_objc_selector
1850 (cp_parser *);
1851 static tree cp_parser_objc_protocol_refs_opt
1852 (cp_parser *);
1853 static void cp_parser_objc_declaration
1854 (cp_parser *);
1855 static tree cp_parser_objc_statement
1856 (cp_parser *);
1857
1858 /* Utility Routines */
1859
1860 static tree cp_parser_lookup_name
1861 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1862 static tree cp_parser_lookup_name_simple
1863 (cp_parser *, tree);
1864 static tree cp_parser_maybe_treat_template_as_class
1865 (tree, bool);
1866 static bool cp_parser_check_declarator_template_parameters
1867 (cp_parser *, cp_declarator *);
1868 static bool cp_parser_check_template_parameters
1869 (cp_parser *, unsigned);
1870 static tree cp_parser_simple_cast_expression
1871 (cp_parser *);
1872 static tree cp_parser_global_scope_opt
1873 (cp_parser *, bool);
1874 static bool cp_parser_constructor_declarator_p
1875 (cp_parser *, bool);
1876 static tree cp_parser_function_definition_from_specifiers_and_declarator
1877 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1878 static tree cp_parser_function_definition_after_declarator
1879 (cp_parser *, bool);
1880 static void cp_parser_template_declaration_after_export
1881 (cp_parser *, bool);
1882 static void cp_parser_perform_template_parameter_access_checks
1883 (VEC (deferred_access_check,gc)*);
1884 static tree cp_parser_single_declaration
1885 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1886 static tree cp_parser_functional_cast
1887 (cp_parser *, tree);
1888 static tree cp_parser_save_member_function_body
1889 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1890 static tree cp_parser_enclosed_template_argument_list
1891 (cp_parser *);
1892 static void cp_parser_save_default_args
1893 (cp_parser *, tree);
1894 static void cp_parser_late_parsing_for_member
1895 (cp_parser *, tree);
1896 static void cp_parser_late_parsing_default_args
1897 (cp_parser *, tree);
1898 static tree cp_parser_sizeof_operand
1899 (cp_parser *, enum rid);
1900 static bool cp_parser_declares_only_class_p
1901 (cp_parser *);
1902 static void cp_parser_set_storage_class
1903 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1904 static void cp_parser_set_decl_spec_type
1905 (cp_decl_specifier_seq *, tree, bool);
1906 static bool cp_parser_friend_p
1907 (const cp_decl_specifier_seq *);
1908 static cp_token *cp_parser_require
1909 (cp_parser *, enum cpp_ttype, const char *);
1910 static cp_token *cp_parser_require_keyword
1911 (cp_parser *, enum rid, const char *);
1912 static bool cp_parser_token_starts_function_definition_p
1913 (cp_token *);
1914 static bool cp_parser_next_token_starts_class_definition_p
1915 (cp_parser *);
1916 static bool cp_parser_next_token_ends_template_argument_p
1917 (cp_parser *);
1918 static bool cp_parser_nth_token_starts_template_argument_list_p
1919 (cp_parser *, size_t);
1920 static enum tag_types cp_parser_token_is_class_key
1921 (cp_token *);
1922 static void cp_parser_check_class_key
1923 (enum tag_types, tree type);
1924 static void cp_parser_check_access_in_redeclaration
1925 (tree type);
1926 static bool cp_parser_optional_template_keyword
1927 (cp_parser *);
1928 static void cp_parser_pre_parsed_nested_name_specifier
1929 (cp_parser *);
1930 static void cp_parser_cache_group
1931 (cp_parser *, enum cpp_ttype, unsigned);
1932 static void cp_parser_parse_tentatively
1933 (cp_parser *);
1934 static void cp_parser_commit_to_tentative_parse
1935 (cp_parser *);
1936 static void cp_parser_abort_tentative_parse
1937 (cp_parser *);
1938 static bool cp_parser_parse_definitely
1939 (cp_parser *);
1940 static inline bool cp_parser_parsing_tentatively
1941 (cp_parser *);
1942 static bool cp_parser_uncommitted_to_tentative_parse_p
1943 (cp_parser *);
1944 static void cp_parser_error
1945 (cp_parser *, const char *);
1946 static void cp_parser_name_lookup_error
1947 (cp_parser *, tree, tree, const char *);
1948 static bool cp_parser_simulate_error
1949 (cp_parser *);
1950 static bool cp_parser_check_type_definition
1951 (cp_parser *);
1952 static void cp_parser_check_for_definition_in_return_type
1953 (cp_declarator *, tree);
1954 static void cp_parser_check_for_invalid_template_id
1955 (cp_parser *, tree);
1956 static bool cp_parser_non_integral_constant_expression
1957 (cp_parser *, const char *);
1958 static void cp_parser_diagnose_invalid_type_name
1959 (cp_parser *, tree, tree);
1960 static bool cp_parser_parse_and_diagnose_invalid_type_name
1961 (cp_parser *);
1962 static int cp_parser_skip_to_closing_parenthesis
1963 (cp_parser *, bool, bool, bool);
1964 static void cp_parser_skip_to_end_of_statement
1965 (cp_parser *);
1966 static void cp_parser_consume_semicolon_at_end_of_statement
1967 (cp_parser *);
1968 static void cp_parser_skip_to_end_of_block_or_statement
1969 (cp_parser *);
1970 static void cp_parser_skip_to_closing_brace
1971 (cp_parser *);
1972 static void cp_parser_skip_to_end_of_template_parameter_list
1973 (cp_parser *);
1974 static void cp_parser_skip_to_pragma_eol
1975 (cp_parser*, cp_token *);
1976 static bool cp_parser_error_occurred
1977 (cp_parser *);
1978 static bool cp_parser_allow_gnu_extensions_p
1979 (cp_parser *);
1980 static bool cp_parser_is_string_literal
1981 (cp_token *);
1982 static bool cp_parser_is_keyword
1983 (cp_token *, enum rid);
1984 static tree cp_parser_make_typename_type
1985 (cp_parser *, tree, tree);
1986
1987 /* Returns nonzero if we are parsing tentatively. */
1988
1989 static inline bool
1990 cp_parser_parsing_tentatively (cp_parser* parser)
1991 {
1992 return parser->context->next != NULL;
1993 }
1994
1995 /* Returns nonzero if TOKEN is a string literal. */
1996
1997 static bool
1998 cp_parser_is_string_literal (cp_token* token)
1999 {
2000 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2001 }
2002
2003 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2004
2005 static bool
2006 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2007 {
2008 return token->keyword == keyword;
2009 }
2010
2011 /* If not parsing tentatively, issue a diagnostic of the form
2012 FILE:LINE: MESSAGE before TOKEN
2013 where TOKEN is the next token in the input stream. MESSAGE
2014 (specified by the caller) is usually of the form "expected
2015 OTHER-TOKEN". */
2016
2017 static void
2018 cp_parser_error (cp_parser* parser, const char* message)
2019 {
2020 if (!cp_parser_simulate_error (parser))
2021 {
2022 cp_token *token = cp_lexer_peek_token (parser->lexer);
2023 /* This diagnostic makes more sense if it is tagged to the line
2024 of the token we just peeked at. */
2025 cp_lexer_set_source_position_from_token (token);
2026
2027 if (token->type == CPP_PRAGMA)
2028 {
2029 error ("%<#pragma%> is not allowed here");
2030 cp_parser_skip_to_pragma_eol (parser, token);
2031 return;
2032 }
2033
2034 c_parse_error (message,
2035 /* Because c_parser_error does not understand
2036 CPP_KEYWORD, keywords are treated like
2037 identifiers. */
2038 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2039 token->u.value);
2040 }
2041 }
2042
2043 /* Issue an error about name-lookup failing. NAME is the
2044 IDENTIFIER_NODE DECL is the result of
2045 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2046 the thing that we hoped to find. */
2047
2048 static void
2049 cp_parser_name_lookup_error (cp_parser* parser,
2050 tree name,
2051 tree decl,
2052 const char* desired)
2053 {
2054 /* If name lookup completely failed, tell the user that NAME was not
2055 declared. */
2056 if (decl == error_mark_node)
2057 {
2058 if (parser->scope && parser->scope != global_namespace)
2059 error ("%<%E::%E%> has not been declared",
2060 parser->scope, name);
2061 else if (parser->scope == global_namespace)
2062 error ("%<::%E%> has not been declared", name);
2063 else if (parser->object_scope
2064 && !CLASS_TYPE_P (parser->object_scope))
2065 error ("request for member %qE in non-class type %qT",
2066 name, parser->object_scope);
2067 else if (parser->object_scope)
2068 error ("%<%T::%E%> has not been declared",
2069 parser->object_scope, name);
2070 else
2071 error ("%qE has not been declared", name);
2072 }
2073 else if (parser->scope && parser->scope != global_namespace)
2074 error ("%<%E::%E%> %s", parser->scope, name, desired);
2075 else if (parser->scope == global_namespace)
2076 error ("%<::%E%> %s", name, desired);
2077 else
2078 error ("%qE %s", name, desired);
2079 }
2080
2081 /* If we are parsing tentatively, remember that an error has occurred
2082 during this tentative parse. Returns true if the error was
2083 simulated; false if a message should be issued by the caller. */
2084
2085 static bool
2086 cp_parser_simulate_error (cp_parser* parser)
2087 {
2088 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2089 {
2090 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2091 return true;
2092 }
2093 return false;
2094 }
2095
2096 /* Check for repeated decl-specifiers. */
2097
2098 static void
2099 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2100 {
2101 cp_decl_spec ds;
2102
2103 for (ds = ds_first; ds != ds_last; ++ds)
2104 {
2105 unsigned count = decl_specs->specs[(int)ds];
2106 if (count < 2)
2107 continue;
2108 /* The "long" specifier is a special case because of "long long". */
2109 if (ds == ds_long)
2110 {
2111 if (count > 2)
2112 error ("%<long long long%> is too long for GCC");
2113 else if (pedantic && !in_system_header && warn_long_long)
2114 pedwarn ("ISO C++ does not support %<long long%>");
2115 }
2116 else if (count > 1)
2117 {
2118 static const char *const decl_spec_names[] = {
2119 "signed",
2120 "unsigned",
2121 "short",
2122 "long",
2123 "const",
2124 "volatile",
2125 "restrict",
2126 "inline",
2127 "virtual",
2128 "explicit",
2129 "friend",
2130 "typedef",
2131 "__complex",
2132 "__thread"
2133 };
2134 error ("duplicate %qs", decl_spec_names[(int)ds]);
2135 }
2136 }
2137 }
2138
2139 /* This function is called when a type is defined. If type
2140 definitions are forbidden at this point, an error message is
2141 issued. */
2142
2143 static bool
2144 cp_parser_check_type_definition (cp_parser* parser)
2145 {
2146 /* If types are forbidden here, issue a message. */
2147 if (parser->type_definition_forbidden_message)
2148 {
2149 /* Use `%s' to print the string in case there are any escape
2150 characters in the message. */
2151 error ("%s", parser->type_definition_forbidden_message);
2152 return false;
2153 }
2154 return true;
2155 }
2156
2157 /* This function is called when the DECLARATOR is processed. The TYPE
2158 was a type defined in the decl-specifiers. If it is invalid to
2159 define a type in the decl-specifiers for DECLARATOR, an error is
2160 issued. */
2161
2162 static void
2163 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2164 tree type)
2165 {
2166 /* [dcl.fct] forbids type definitions in return types.
2167 Unfortunately, it's not easy to know whether or not we are
2168 processing a return type until after the fact. */
2169 while (declarator
2170 && (declarator->kind == cdk_pointer
2171 || declarator->kind == cdk_reference
2172 || declarator->kind == cdk_ptrmem))
2173 declarator = declarator->declarator;
2174 if (declarator
2175 && declarator->kind == cdk_function)
2176 {
2177 error ("new types may not be defined in a return type");
2178 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2179 type);
2180 }
2181 }
2182
2183 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2184 "<" in any valid C++ program. If the next token is indeed "<",
2185 issue a message warning the user about what appears to be an
2186 invalid attempt to form a template-id. */
2187
2188 static void
2189 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2190 tree type)
2191 {
2192 cp_token_position start = 0;
2193
2194 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2195 {
2196 if (TYPE_P (type))
2197 error ("%qT is not a template", type);
2198 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2199 error ("%qE is not a template", type);
2200 else
2201 error ("invalid template-id");
2202 /* Remember the location of the invalid "<". */
2203 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2204 start = cp_lexer_token_position (parser->lexer, true);
2205 /* Consume the "<". */
2206 cp_lexer_consume_token (parser->lexer);
2207 /* Parse the template arguments. */
2208 cp_parser_enclosed_template_argument_list (parser);
2209 /* Permanently remove the invalid template arguments so that
2210 this error message is not issued again. */
2211 if (start)
2212 cp_lexer_purge_tokens_after (parser->lexer, start);
2213 }
2214 }
2215
2216 /* If parsing an integral constant-expression, issue an error message
2217 about the fact that THING appeared and return true. Otherwise,
2218 return false. In either case, set
2219 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2220
2221 static bool
2222 cp_parser_non_integral_constant_expression (cp_parser *parser,
2223 const char *thing)
2224 {
2225 parser->non_integral_constant_expression_p = true;
2226 if (parser->integral_constant_expression_p)
2227 {
2228 if (!parser->allow_non_integral_constant_expression_p)
2229 {
2230 error ("%s cannot appear in a constant-expression", thing);
2231 return true;
2232 }
2233 }
2234 return false;
2235 }
2236
2237 /* Emit a diagnostic for an invalid type name. SCOPE is the
2238 qualifying scope (or NULL, if none) for ID. This function commits
2239 to the current active tentative parse, if any. (Otherwise, the
2240 problematic construct might be encountered again later, resulting
2241 in duplicate error messages.) */
2242
2243 static void
2244 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2245 {
2246 tree decl, old_scope;
2247 /* Try to lookup the identifier. */
2248 old_scope = parser->scope;
2249 parser->scope = scope;
2250 decl = cp_parser_lookup_name_simple (parser, id);
2251 parser->scope = old_scope;
2252 /* If the lookup found a template-name, it means that the user forgot
2253 to specify an argument list. Emit a useful error message. */
2254 if (TREE_CODE (decl) == TEMPLATE_DECL)
2255 error ("invalid use of template-name %qE without an argument list", decl);
2256 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2257 error ("invalid use of destructor %qD as a type", id);
2258 else if (TREE_CODE (decl) == TYPE_DECL)
2259 /* Something like 'unsigned A a;' */
2260 error ("invalid combination of multiple type-specifiers");
2261 else if (!parser->scope)
2262 {
2263 /* Issue an error message. */
2264 error ("%qE does not name a type", id);
2265 /* If we're in a template class, it's possible that the user was
2266 referring to a type from a base class. For example:
2267
2268 template <typename T> struct A { typedef T X; };
2269 template <typename T> struct B : public A<T> { X x; };
2270
2271 The user should have said "typename A<T>::X". */
2272 if (processing_template_decl && current_class_type
2273 && TYPE_BINFO (current_class_type))
2274 {
2275 tree b;
2276
2277 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2278 b;
2279 b = TREE_CHAIN (b))
2280 {
2281 tree base_type = BINFO_TYPE (b);
2282 if (CLASS_TYPE_P (base_type)
2283 && dependent_type_p (base_type))
2284 {
2285 tree field;
2286 /* Go from a particular instantiation of the
2287 template (which will have an empty TYPE_FIELDs),
2288 to the main version. */
2289 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2290 for (field = TYPE_FIELDS (base_type);
2291 field;
2292 field = TREE_CHAIN (field))
2293 if (TREE_CODE (field) == TYPE_DECL
2294 && DECL_NAME (field) == id)
2295 {
2296 inform ("(perhaps %<typename %T::%E%> was intended)",
2297 BINFO_TYPE (b), id);
2298 break;
2299 }
2300 if (field)
2301 break;
2302 }
2303 }
2304 }
2305 }
2306 /* Here we diagnose qualified-ids where the scope is actually correct,
2307 but the identifier does not resolve to a valid type name. */
2308 else if (parser->scope != error_mark_node)
2309 {
2310 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2311 error ("%qE in namespace %qE does not name a type",
2312 id, parser->scope);
2313 else if (TYPE_P (parser->scope))
2314 error ("%qE in class %qT does not name a type", id, parser->scope);
2315 else
2316 gcc_unreachable ();
2317 }
2318 cp_parser_commit_to_tentative_parse (parser);
2319 }
2320
2321 /* Check for a common situation where a type-name should be present,
2322 but is not, and issue a sensible error message. Returns true if an
2323 invalid type-name was detected.
2324
2325 The situation handled by this function are variable declarations of the
2326 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2327 Usually, `ID' should name a type, but if we got here it means that it
2328 does not. We try to emit the best possible error message depending on
2329 how exactly the id-expression looks like. */
2330
2331 static bool
2332 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2333 {
2334 tree id;
2335
2336 cp_parser_parse_tentatively (parser);
2337 id = cp_parser_id_expression (parser,
2338 /*template_keyword_p=*/false,
2339 /*check_dependency_p=*/true,
2340 /*template_p=*/NULL,
2341 /*declarator_p=*/true,
2342 /*optional_p=*/false);
2343 /* After the id-expression, there should be a plain identifier,
2344 otherwise this is not a simple variable declaration. Also, if
2345 the scope is dependent, we cannot do much. */
2346 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2347 || (parser->scope && TYPE_P (parser->scope)
2348 && dependent_type_p (parser->scope))
2349 || TREE_CODE (id) == TYPE_DECL)
2350 {
2351 cp_parser_abort_tentative_parse (parser);
2352 return false;
2353 }
2354 if (!cp_parser_parse_definitely (parser))
2355 return false;
2356
2357 /* Emit a diagnostic for the invalid type. */
2358 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2359 /* Skip to the end of the declaration; there's no point in
2360 trying to process it. */
2361 cp_parser_skip_to_end_of_block_or_statement (parser);
2362 return true;
2363 }
2364
2365 /* Consume tokens up to, and including, the next non-nested closing `)'.
2366 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2367 are doing error recovery. Returns -1 if OR_COMMA is true and we
2368 found an unnested comma. */
2369
2370 static int
2371 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2372 bool recovering,
2373 bool or_comma,
2374 bool consume_paren)
2375 {
2376 unsigned paren_depth = 0;
2377 unsigned brace_depth = 0;
2378
2379 if (recovering && !or_comma
2380 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2381 return 0;
2382
2383 while (true)
2384 {
2385 cp_token * token = cp_lexer_peek_token (parser->lexer);
2386
2387 switch (token->type)
2388 {
2389 case CPP_EOF:
2390 case CPP_PRAGMA_EOL:
2391 /* If we've run out of tokens, then there is no closing `)'. */
2392 return 0;
2393
2394 case CPP_SEMICOLON:
2395 /* This matches the processing in skip_to_end_of_statement. */
2396 if (!brace_depth)
2397 return 0;
2398 break;
2399
2400 case CPP_OPEN_BRACE:
2401 ++brace_depth;
2402 break;
2403 case CPP_CLOSE_BRACE:
2404 if (!brace_depth--)
2405 return 0;
2406 break;
2407
2408 case CPP_COMMA:
2409 if (recovering && or_comma && !brace_depth && !paren_depth)
2410 return -1;
2411 break;
2412
2413 case CPP_OPEN_PAREN:
2414 if (!brace_depth)
2415 ++paren_depth;
2416 break;
2417
2418 case CPP_CLOSE_PAREN:
2419 if (!brace_depth && !paren_depth--)
2420 {
2421 if (consume_paren)
2422 cp_lexer_consume_token (parser->lexer);
2423 return 1;
2424 }
2425 break;
2426
2427 default:
2428 break;
2429 }
2430
2431 /* Consume the token. */
2432 cp_lexer_consume_token (parser->lexer);
2433 }
2434 }
2435
2436 /* Consume tokens until we reach the end of the current statement.
2437 Normally, that will be just before consuming a `;'. However, if a
2438 non-nested `}' comes first, then we stop before consuming that. */
2439
2440 static void
2441 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2442 {
2443 unsigned nesting_depth = 0;
2444
2445 while (true)
2446 {
2447 cp_token *token = cp_lexer_peek_token (parser->lexer);
2448
2449 switch (token->type)
2450 {
2451 case CPP_EOF:
2452 case CPP_PRAGMA_EOL:
2453 /* If we've run out of tokens, stop. */
2454 return;
2455
2456 case CPP_SEMICOLON:
2457 /* If the next token is a `;', we have reached the end of the
2458 statement. */
2459 if (!nesting_depth)
2460 return;
2461 break;
2462
2463 case CPP_CLOSE_BRACE:
2464 /* If this is a non-nested '}', stop before consuming it.
2465 That way, when confronted with something like:
2466
2467 { 3 + }
2468
2469 we stop before consuming the closing '}', even though we
2470 have not yet reached a `;'. */
2471 if (nesting_depth == 0)
2472 return;
2473
2474 /* If it is the closing '}' for a block that we have
2475 scanned, stop -- but only after consuming the token.
2476 That way given:
2477
2478 void f g () { ... }
2479 typedef int I;
2480
2481 we will stop after the body of the erroneously declared
2482 function, but before consuming the following `typedef'
2483 declaration. */
2484 if (--nesting_depth == 0)
2485 {
2486 cp_lexer_consume_token (parser->lexer);
2487 return;
2488 }
2489
2490 case CPP_OPEN_BRACE:
2491 ++nesting_depth;
2492 break;
2493
2494 default:
2495 break;
2496 }
2497
2498 /* Consume the token. */
2499 cp_lexer_consume_token (parser->lexer);
2500 }
2501 }
2502
2503 /* This function is called at the end of a statement or declaration.
2504 If the next token is a semicolon, it is consumed; otherwise, error
2505 recovery is attempted. */
2506
2507 static void
2508 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2509 {
2510 /* Look for the trailing `;'. */
2511 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2512 {
2513 /* If there is additional (erroneous) input, skip to the end of
2514 the statement. */
2515 cp_parser_skip_to_end_of_statement (parser);
2516 /* If the next token is now a `;', consume it. */
2517 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2518 cp_lexer_consume_token (parser->lexer);
2519 }
2520 }
2521
2522 /* Skip tokens until we have consumed an entire block, or until we
2523 have consumed a non-nested `;'. */
2524
2525 static void
2526 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2527 {
2528 int nesting_depth = 0;
2529
2530 while (nesting_depth >= 0)
2531 {
2532 cp_token *token = cp_lexer_peek_token (parser->lexer);
2533
2534 switch (token->type)
2535 {
2536 case CPP_EOF:
2537 case CPP_PRAGMA_EOL:
2538 /* If we've run out of tokens, stop. */
2539 return;
2540
2541 case CPP_SEMICOLON:
2542 /* Stop if this is an unnested ';'. */
2543 if (!nesting_depth)
2544 nesting_depth = -1;
2545 break;
2546
2547 case CPP_CLOSE_BRACE:
2548 /* Stop if this is an unnested '}', or closes the outermost
2549 nesting level. */
2550 nesting_depth--;
2551 if (!nesting_depth)
2552 nesting_depth = -1;
2553 break;
2554
2555 case CPP_OPEN_BRACE:
2556 /* Nest. */
2557 nesting_depth++;
2558 break;
2559
2560 default:
2561 break;
2562 }
2563
2564 /* Consume the token. */
2565 cp_lexer_consume_token (parser->lexer);
2566 }
2567 }
2568
2569 /* Skip tokens until a non-nested closing curly brace is the next
2570 token. */
2571
2572 static void
2573 cp_parser_skip_to_closing_brace (cp_parser *parser)
2574 {
2575 unsigned nesting_depth = 0;
2576
2577 while (true)
2578 {
2579 cp_token *token = cp_lexer_peek_token (parser->lexer);
2580
2581 switch (token->type)
2582 {
2583 case CPP_EOF:
2584 case CPP_PRAGMA_EOL:
2585 /* If we've run out of tokens, stop. */
2586 return;
2587
2588 case CPP_CLOSE_BRACE:
2589 /* If the next token is a non-nested `}', then we have reached
2590 the end of the current block. */
2591 if (nesting_depth-- == 0)
2592 return;
2593 break;
2594
2595 case CPP_OPEN_BRACE:
2596 /* If it the next token is a `{', then we are entering a new
2597 block. Consume the entire block. */
2598 ++nesting_depth;
2599 break;
2600
2601 default:
2602 break;
2603 }
2604
2605 /* Consume the token. */
2606 cp_lexer_consume_token (parser->lexer);
2607 }
2608 }
2609
2610 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2611 parameter is the PRAGMA token, allowing us to purge the entire pragma
2612 sequence. */
2613
2614 static void
2615 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2616 {
2617 cp_token *token;
2618
2619 parser->lexer->in_pragma = false;
2620
2621 do
2622 token = cp_lexer_consume_token (parser->lexer);
2623 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2624
2625 /* Ensure that the pragma is not parsed again. */
2626 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2627 }
2628
2629 /* Require pragma end of line, resyncing with it as necessary. The
2630 arguments are as for cp_parser_skip_to_pragma_eol. */
2631
2632 static void
2633 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2634 {
2635 parser->lexer->in_pragma = false;
2636 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2637 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2638 }
2639
2640 /* This is a simple wrapper around make_typename_type. When the id is
2641 an unresolved identifier node, we can provide a superior diagnostic
2642 using cp_parser_diagnose_invalid_type_name. */
2643
2644 static tree
2645 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2646 {
2647 tree result;
2648 if (TREE_CODE (id) == IDENTIFIER_NODE)
2649 {
2650 result = make_typename_type (scope, id, typename_type,
2651 /*complain=*/tf_none);
2652 if (result == error_mark_node)
2653 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2654 return result;
2655 }
2656 return make_typename_type (scope, id, typename_type, tf_error);
2657 }
2658
2659
2660 /* Create a new C++ parser. */
2661
2662 static cp_parser *
2663 cp_parser_new (void)
2664 {
2665 cp_parser *parser;
2666 cp_lexer *lexer;
2667 unsigned i;
2668
2669 /* cp_lexer_new_main is called before calling ggc_alloc because
2670 cp_lexer_new_main might load a PCH file. */
2671 lexer = cp_lexer_new_main ();
2672
2673 /* Initialize the binops_by_token so that we can get the tree
2674 directly from the token. */
2675 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2676 binops_by_token[binops[i].token_type] = binops[i];
2677
2678 parser = GGC_CNEW (cp_parser);
2679 parser->lexer = lexer;
2680 parser->context = cp_parser_context_new (NULL);
2681
2682 /* For now, we always accept GNU extensions. */
2683 parser->allow_gnu_extensions_p = 1;
2684
2685 /* The `>' token is a greater-than operator, not the end of a
2686 template-id. */
2687 parser->greater_than_is_operator_p = true;
2688
2689 parser->default_arg_ok_p = true;
2690
2691 /* We are not parsing a constant-expression. */
2692 parser->integral_constant_expression_p = false;
2693 parser->allow_non_integral_constant_expression_p = false;
2694 parser->non_integral_constant_expression_p = false;
2695
2696 /* Local variable names are not forbidden. */
2697 parser->local_variables_forbidden_p = false;
2698
2699 /* We are not processing an `extern "C"' declaration. */
2700 parser->in_unbraced_linkage_specification_p = false;
2701
2702 /* We are not processing a declarator. */
2703 parser->in_declarator_p = false;
2704
2705 /* We are not processing a template-argument-list. */
2706 parser->in_template_argument_list_p = false;
2707
2708 /* We are not in an iteration statement. */
2709 parser->in_statement = 0;
2710
2711 /* We are not in a switch statement. */
2712 parser->in_switch_statement_p = false;
2713
2714 /* We are not parsing a type-id inside an expression. */
2715 parser->in_type_id_in_expr_p = false;
2716
2717 /* Declarations aren't implicitly extern "C". */
2718 parser->implicit_extern_c = false;
2719
2720 /* String literals should be translated to the execution character set. */
2721 parser->translate_strings_p = true;
2722
2723 /* We are not parsing a function body. */
2724 parser->in_function_body = false;
2725
2726 /* The unparsed function queue is empty. */
2727 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2728
2729 /* There are no classes being defined. */
2730 parser->num_classes_being_defined = 0;
2731
2732 /* No template parameters apply. */
2733 parser->num_template_parameter_lists = 0;
2734
2735 return parser;
2736 }
2737
2738 /* Create a cp_lexer structure which will emit the tokens in CACHE
2739 and push it onto the parser's lexer stack. This is used for delayed
2740 parsing of in-class method bodies and default arguments, and should
2741 not be confused with tentative parsing. */
2742 static void
2743 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2744 {
2745 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2746 lexer->next = parser->lexer;
2747 parser->lexer = lexer;
2748
2749 /* Move the current source position to that of the first token in the
2750 new lexer. */
2751 cp_lexer_set_source_position_from_token (lexer->next_token);
2752 }
2753
2754 /* Pop the top lexer off the parser stack. This is never used for the
2755 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2756 static void
2757 cp_parser_pop_lexer (cp_parser *parser)
2758 {
2759 cp_lexer *lexer = parser->lexer;
2760 parser->lexer = lexer->next;
2761 cp_lexer_destroy (lexer);
2762
2763 /* Put the current source position back where it was before this
2764 lexer was pushed. */
2765 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2766 }
2767
2768 /* Lexical conventions [gram.lex] */
2769
2770 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2771 identifier. */
2772
2773 static tree
2774 cp_parser_identifier (cp_parser* parser)
2775 {
2776 cp_token *token;
2777
2778 /* Look for the identifier. */
2779 token = cp_parser_require (parser, CPP_NAME, "identifier");
2780 /* Return the value. */
2781 return token ? token->u.value : error_mark_node;
2782 }
2783
2784 /* Parse a sequence of adjacent string constants. Returns a
2785 TREE_STRING representing the combined, nul-terminated string
2786 constant. If TRANSLATE is true, translate the string to the
2787 execution character set. If WIDE_OK is true, a wide string is
2788 invalid here.
2789
2790 C++98 [lex.string] says that if a narrow string literal token is
2791 adjacent to a wide string literal token, the behavior is undefined.
2792 However, C99 6.4.5p4 says that this results in a wide string literal.
2793 We follow C99 here, for consistency with the C front end.
2794
2795 This code is largely lifted from lex_string() in c-lex.c.
2796
2797 FUTURE: ObjC++ will need to handle @-strings here. */
2798 static tree
2799 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2800 {
2801 tree value;
2802 bool wide = false;
2803 size_t count;
2804 struct obstack str_ob;
2805 cpp_string str, istr, *strs;
2806 cp_token *tok;
2807
2808 tok = cp_lexer_peek_token (parser->lexer);
2809 if (!cp_parser_is_string_literal (tok))
2810 {
2811 cp_parser_error (parser, "expected string-literal");
2812 return error_mark_node;
2813 }
2814
2815 /* Try to avoid the overhead of creating and destroying an obstack
2816 for the common case of just one string. */
2817 if (!cp_parser_is_string_literal
2818 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2819 {
2820 cp_lexer_consume_token (parser->lexer);
2821
2822 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2823 str.len = TREE_STRING_LENGTH (tok->u.value);
2824 count = 1;
2825 if (tok->type == CPP_WSTRING)
2826 wide = true;
2827
2828 strs = &str;
2829 }
2830 else
2831 {
2832 gcc_obstack_init (&str_ob);
2833 count = 0;
2834
2835 do
2836 {
2837 cp_lexer_consume_token (parser->lexer);
2838 count++;
2839 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2840 str.len = TREE_STRING_LENGTH (tok->u.value);
2841 if (tok->type == CPP_WSTRING)
2842 wide = true;
2843
2844 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2845
2846 tok = cp_lexer_peek_token (parser->lexer);
2847 }
2848 while (cp_parser_is_string_literal (tok));
2849
2850 strs = (cpp_string *) obstack_finish (&str_ob);
2851 }
2852
2853 if (wide && !wide_ok)
2854 {
2855 cp_parser_error (parser, "a wide string is invalid in this context");
2856 wide = false;
2857 }
2858
2859 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2860 (parse_in, strs, count, &istr, wide))
2861 {
2862 value = build_string (istr.len, (char *)istr.text);
2863 free ((void *)istr.text);
2864
2865 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2866 value = fix_string_type (value);
2867 }
2868 else
2869 /* cpp_interpret_string has issued an error. */
2870 value = error_mark_node;
2871
2872 if (count > 1)
2873 obstack_free (&str_ob, 0);
2874
2875 return value;
2876 }
2877
2878
2879 /* Basic concepts [gram.basic] */
2880
2881 /* Parse a translation-unit.
2882
2883 translation-unit:
2884 declaration-seq [opt]
2885
2886 Returns TRUE if all went well. */
2887
2888 static bool
2889 cp_parser_translation_unit (cp_parser* parser)
2890 {
2891 /* The address of the first non-permanent object on the declarator
2892 obstack. */
2893 static void *declarator_obstack_base;
2894
2895 bool success;
2896
2897 /* Create the declarator obstack, if necessary. */
2898 if (!cp_error_declarator)
2899 {
2900 gcc_obstack_init (&declarator_obstack);
2901 /* Create the error declarator. */
2902 cp_error_declarator = make_declarator (cdk_error);
2903 /* Create the empty parameter list. */
2904 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2905 /* Remember where the base of the declarator obstack lies. */
2906 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2907 }
2908
2909 cp_parser_declaration_seq_opt (parser);
2910
2911 /* If there are no tokens left then all went well. */
2912 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2913 {
2914 /* Get rid of the token array; we don't need it any more. */
2915 cp_lexer_destroy (parser->lexer);
2916 parser->lexer = NULL;
2917
2918 /* This file might have been a context that's implicitly extern
2919 "C". If so, pop the lang context. (Only relevant for PCH.) */
2920 if (parser->implicit_extern_c)
2921 {
2922 pop_lang_context ();
2923 parser->implicit_extern_c = false;
2924 }
2925
2926 /* Finish up. */
2927 finish_translation_unit ();
2928
2929 success = true;
2930 }
2931 else
2932 {
2933 cp_parser_error (parser, "expected declaration");
2934 success = false;
2935 }
2936
2937 /* Make sure the declarator obstack was fully cleaned up. */
2938 gcc_assert (obstack_next_free (&declarator_obstack)
2939 == declarator_obstack_base);
2940
2941 /* All went well. */
2942 return success;
2943 }
2944
2945 /* Expressions [gram.expr] */
2946
2947 /* Parse a primary-expression.
2948
2949 primary-expression:
2950 literal
2951 this
2952 ( expression )
2953 id-expression
2954
2955 GNU Extensions:
2956
2957 primary-expression:
2958 ( compound-statement )
2959 __builtin_va_arg ( assignment-expression , type-id )
2960 __builtin_offsetof ( type-id , offsetof-expression )
2961
2962 Objective-C++ Extension:
2963
2964 primary-expression:
2965 objc-expression
2966
2967 literal:
2968 __null
2969
2970 ADDRESS_P is true iff this expression was immediately preceded by
2971 "&" and therefore might denote a pointer-to-member. CAST_P is true
2972 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2973 true iff this expression is a template argument.
2974
2975 Returns a representation of the expression. Upon return, *IDK
2976 indicates what kind of id-expression (if any) was present. */
2977
2978 static tree
2979 cp_parser_primary_expression (cp_parser *parser,
2980 bool address_p,
2981 bool cast_p,
2982 bool template_arg_p,
2983 cp_id_kind *idk)
2984 {
2985 cp_token *token;
2986
2987 /* Assume the primary expression is not an id-expression. */
2988 *idk = CP_ID_KIND_NONE;
2989
2990 /* Peek at the next token. */
2991 token = cp_lexer_peek_token (parser->lexer);
2992 switch (token->type)
2993 {
2994 /* literal:
2995 integer-literal
2996 character-literal
2997 floating-literal
2998 string-literal
2999 boolean-literal */
3000 case CPP_CHAR:
3001 case CPP_WCHAR:
3002 case CPP_NUMBER:
3003 token = cp_lexer_consume_token (parser->lexer);
3004 /* Floating-point literals are only allowed in an integral
3005 constant expression if they are cast to an integral or
3006 enumeration type. */
3007 if (TREE_CODE (token->u.value) == REAL_CST
3008 && parser->integral_constant_expression_p
3009 && pedantic)
3010 {
3011 /* CAST_P will be set even in invalid code like "int(2.7 +
3012 ...)". Therefore, we have to check that the next token
3013 is sure to end the cast. */
3014 if (cast_p)
3015 {
3016 cp_token *next_token;
3017
3018 next_token = cp_lexer_peek_token (parser->lexer);
3019 if (/* The comma at the end of an
3020 enumerator-definition. */
3021 next_token->type != CPP_COMMA
3022 /* The curly brace at the end of an enum-specifier. */
3023 && next_token->type != CPP_CLOSE_BRACE
3024 /* The end of a statement. */
3025 && next_token->type != CPP_SEMICOLON
3026 /* The end of the cast-expression. */
3027 && next_token->type != CPP_CLOSE_PAREN
3028 /* The end of an array bound. */
3029 && next_token->type != CPP_CLOSE_SQUARE
3030 /* The closing ">" in a template-argument-list. */
3031 && (next_token->type != CPP_GREATER
3032 || parser->greater_than_is_operator_p)
3033 /* C++0x only: A ">>" treated like two ">" tokens,
3034 in a template-argument-list. */
3035 && (next_token->type != CPP_RSHIFT
3036 || !flag_cpp0x
3037 || parser->greater_than_is_operator_p))
3038 cast_p = false;
3039 }
3040
3041 /* If we are within a cast, then the constraint that the
3042 cast is to an integral or enumeration type will be
3043 checked at that point. If we are not within a cast, then
3044 this code is invalid. */
3045 if (!cast_p)
3046 cp_parser_non_integral_constant_expression
3047 (parser, "floating-point literal");
3048 }
3049 return token->u.value;
3050
3051 case CPP_STRING:
3052 case CPP_WSTRING:
3053 /* ??? Should wide strings be allowed when parser->translate_strings_p
3054 is false (i.e. in attributes)? If not, we can kill the third
3055 argument to cp_parser_string_literal. */
3056 return cp_parser_string_literal (parser,
3057 parser->translate_strings_p,
3058 true);
3059
3060 case CPP_OPEN_PAREN:
3061 {
3062 tree expr;
3063 bool saved_greater_than_is_operator_p;
3064
3065 /* Consume the `('. */
3066 cp_lexer_consume_token (parser->lexer);
3067 /* Within a parenthesized expression, a `>' token is always
3068 the greater-than operator. */
3069 saved_greater_than_is_operator_p
3070 = parser->greater_than_is_operator_p;
3071 parser->greater_than_is_operator_p = true;
3072 /* If we see `( { ' then we are looking at the beginning of
3073 a GNU statement-expression. */
3074 if (cp_parser_allow_gnu_extensions_p (parser)
3075 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3076 {
3077 /* Statement-expressions are not allowed by the standard. */
3078 if (pedantic)
3079 pedwarn ("ISO C++ forbids braced-groups within expressions");
3080
3081 /* And they're not allowed outside of a function-body; you
3082 cannot, for example, write:
3083
3084 int i = ({ int j = 3; j + 1; });
3085
3086 at class or namespace scope. */
3087 if (!parser->in_function_body)
3088 {
3089 error ("statement-expressions are allowed only inside functions");
3090 cp_parser_skip_to_end_of_block_or_statement (parser);
3091 expr = error_mark_node;
3092 }
3093 else
3094 {
3095 /* Start the statement-expression. */
3096 expr = begin_stmt_expr ();
3097 /* Parse the compound-statement. */
3098 cp_parser_compound_statement (parser, expr, false);
3099 /* Finish up. */
3100 expr = finish_stmt_expr (expr, false);
3101 }
3102 }
3103 else
3104 {
3105 /* Parse the parenthesized expression. */
3106 expr = cp_parser_expression (parser, cast_p);
3107 /* Let the front end know that this expression was
3108 enclosed in parentheses. This matters in case, for
3109 example, the expression is of the form `A::B', since
3110 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3111 not. */
3112 finish_parenthesized_expr (expr);
3113 }
3114 /* The `>' token might be the end of a template-id or
3115 template-parameter-list now. */
3116 parser->greater_than_is_operator_p
3117 = saved_greater_than_is_operator_p;
3118 /* Consume the `)'. */
3119 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3120 cp_parser_skip_to_end_of_statement (parser);
3121
3122 return expr;
3123 }
3124
3125 case CPP_KEYWORD:
3126 switch (token->keyword)
3127 {
3128 /* These two are the boolean literals. */
3129 case RID_TRUE:
3130 cp_lexer_consume_token (parser->lexer);
3131 return boolean_true_node;
3132 case RID_FALSE:
3133 cp_lexer_consume_token (parser->lexer);
3134 return boolean_false_node;
3135
3136 /* The `__null' literal. */
3137 case RID_NULL:
3138 cp_lexer_consume_token (parser->lexer);
3139 return null_node;
3140
3141 /* Recognize the `this' keyword. */
3142 case RID_THIS:
3143 cp_lexer_consume_token (parser->lexer);
3144 if (parser->local_variables_forbidden_p)
3145 {
3146 error ("%<this%> may not be used in this context");
3147 return error_mark_node;
3148 }
3149 /* Pointers cannot appear in constant-expressions. */
3150 if (cp_parser_non_integral_constant_expression (parser,
3151 "`this'"))
3152 return error_mark_node;
3153 return finish_this_expr ();
3154
3155 /* The `operator' keyword can be the beginning of an
3156 id-expression. */
3157 case RID_OPERATOR:
3158 goto id_expression;
3159
3160 case RID_FUNCTION_NAME:
3161 case RID_PRETTY_FUNCTION_NAME:
3162 case RID_C99_FUNCTION_NAME:
3163 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3164 __func__ are the names of variables -- but they are
3165 treated specially. Therefore, they are handled here,
3166 rather than relying on the generic id-expression logic
3167 below. Grammatically, these names are id-expressions.
3168
3169 Consume the token. */
3170 token = cp_lexer_consume_token (parser->lexer);
3171 /* Look up the name. */
3172 return finish_fname (token->u.value);
3173
3174 case RID_VA_ARG:
3175 {
3176 tree expression;
3177 tree type;
3178
3179 /* The `__builtin_va_arg' construct is used to handle
3180 `va_arg'. Consume the `__builtin_va_arg' token. */
3181 cp_lexer_consume_token (parser->lexer);
3182 /* Look for the opening `('. */
3183 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3184 /* Now, parse the assignment-expression. */
3185 expression = cp_parser_assignment_expression (parser,
3186 /*cast_p=*/false);
3187 /* Look for the `,'. */
3188 cp_parser_require (parser, CPP_COMMA, "`,'");
3189 /* Parse the type-id. */
3190 type = cp_parser_type_id (parser);
3191 /* Look for the closing `)'. */
3192 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3193 /* Using `va_arg' in a constant-expression is not
3194 allowed. */
3195 if (cp_parser_non_integral_constant_expression (parser,
3196 "`va_arg'"))
3197 return error_mark_node;
3198 return build_x_va_arg (expression, type);
3199 }
3200
3201 case RID_OFFSETOF:
3202 return cp_parser_builtin_offsetof (parser);
3203
3204 /* Objective-C++ expressions. */
3205 case RID_AT_ENCODE:
3206 case RID_AT_PROTOCOL:
3207 case RID_AT_SELECTOR:
3208 return cp_parser_objc_expression (parser);
3209
3210 default:
3211 cp_parser_error (parser, "expected primary-expression");
3212 return error_mark_node;
3213 }
3214
3215 /* An id-expression can start with either an identifier, a
3216 `::' as the beginning of a qualified-id, or the "operator"
3217 keyword. */
3218 case CPP_NAME:
3219 case CPP_SCOPE:
3220 case CPP_TEMPLATE_ID:
3221 case CPP_NESTED_NAME_SPECIFIER:
3222 {
3223 tree id_expression;
3224 tree decl;
3225 const char *error_msg;
3226 bool template_p;
3227 bool done;
3228
3229 id_expression:
3230 /* Parse the id-expression. */
3231 id_expression
3232 = cp_parser_id_expression (parser,
3233 /*template_keyword_p=*/false,
3234 /*check_dependency_p=*/true,
3235 &template_p,
3236 /*declarator_p=*/false,
3237 /*optional_p=*/false);
3238 if (id_expression == error_mark_node)
3239 return error_mark_node;
3240 token = cp_lexer_peek_token (parser->lexer);
3241 done = (token->type != CPP_OPEN_SQUARE
3242 && token->type != CPP_OPEN_PAREN
3243 && token->type != CPP_DOT
3244 && token->type != CPP_DEREF
3245 && token->type != CPP_PLUS_PLUS
3246 && token->type != CPP_MINUS_MINUS);
3247 /* If we have a template-id, then no further lookup is
3248 required. If the template-id was for a template-class, we
3249 will sometimes have a TYPE_DECL at this point. */
3250 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3251 || TREE_CODE (id_expression) == TYPE_DECL)
3252 decl = id_expression;
3253 /* Look up the name. */
3254 else
3255 {
3256 tree ambiguous_decls;
3257
3258 decl = cp_parser_lookup_name (parser, id_expression,
3259 none_type,
3260 template_p,
3261 /*is_namespace=*/false,
3262 /*check_dependency=*/true,
3263 &ambiguous_decls);
3264 /* If the lookup was ambiguous, an error will already have
3265 been issued. */
3266 if (ambiguous_decls)
3267 return error_mark_node;
3268
3269 /* In Objective-C++, an instance variable (ivar) may be preferred
3270 to whatever cp_parser_lookup_name() found. */
3271 decl = objc_lookup_ivar (decl, id_expression);
3272
3273 /* If name lookup gives us a SCOPE_REF, then the
3274 qualifying scope was dependent. */
3275 if (TREE_CODE (decl) == SCOPE_REF)
3276 return decl;
3277 /* Check to see if DECL is a local variable in a context
3278 where that is forbidden. */
3279 if (parser->local_variables_forbidden_p
3280 && local_variable_p (decl))
3281 {
3282 /* It might be that we only found DECL because we are
3283 trying to be generous with pre-ISO scoping rules.
3284 For example, consider:
3285
3286 int i;
3287 void g() {
3288 for (int i = 0; i < 10; ++i) {}
3289 extern void f(int j = i);
3290 }
3291
3292 Here, name look up will originally find the out
3293 of scope `i'. We need to issue a warning message,
3294 but then use the global `i'. */
3295 decl = check_for_out_of_scope_variable (decl);
3296 if (local_variable_p (decl))
3297 {
3298 error ("local variable %qD may not appear in this context",
3299 decl);
3300 return error_mark_node;
3301 }
3302 }
3303 }
3304
3305 decl = (finish_id_expression
3306 (id_expression, decl, parser->scope,
3307 idk,
3308 parser->integral_constant_expression_p,
3309 parser->allow_non_integral_constant_expression_p,
3310 &parser->non_integral_constant_expression_p,
3311 template_p, done, address_p,
3312 template_arg_p,
3313 &error_msg));
3314 if (error_msg)
3315 cp_parser_error (parser, error_msg);
3316 return decl;
3317 }
3318
3319 /* Anything else is an error. */
3320 default:
3321 /* ...unless we have an Objective-C++ message or string literal,
3322 that is. */
3323 if (c_dialect_objc ()
3324 && (token->type == CPP_OPEN_SQUARE
3325 || token->type == CPP_OBJC_STRING))
3326 return cp_parser_objc_expression (parser);
3327
3328 cp_parser_error (parser, "expected primary-expression");
3329 return error_mark_node;
3330 }
3331 }
3332
3333 /* Parse an id-expression.
3334
3335 id-expression:
3336 unqualified-id
3337 qualified-id
3338
3339 qualified-id:
3340 :: [opt] nested-name-specifier template [opt] unqualified-id
3341 :: identifier
3342 :: operator-function-id
3343 :: template-id
3344
3345 Return a representation of the unqualified portion of the
3346 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3347 a `::' or nested-name-specifier.
3348
3349 Often, if the id-expression was a qualified-id, the caller will
3350 want to make a SCOPE_REF to represent the qualified-id. This
3351 function does not do this in order to avoid wastefully creating
3352 SCOPE_REFs when they are not required.
3353
3354 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3355 `template' keyword.
3356
3357 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3358 uninstantiated templates.
3359
3360 If *TEMPLATE_P is non-NULL, it is set to true iff the
3361 `template' keyword is used to explicitly indicate that the entity
3362 named is a template.
3363
3364 If DECLARATOR_P is true, the id-expression is appearing as part of
3365 a declarator, rather than as part of an expression. */
3366
3367 static tree
3368 cp_parser_id_expression (cp_parser *parser,
3369 bool template_keyword_p,
3370 bool check_dependency_p,
3371 bool *template_p,
3372 bool declarator_p,
3373 bool optional_p)
3374 {
3375 bool global_scope_p;
3376 bool nested_name_specifier_p;
3377
3378 /* Assume the `template' keyword was not used. */
3379 if (template_p)
3380 *template_p = template_keyword_p;
3381
3382 /* Look for the optional `::' operator. */
3383 global_scope_p
3384 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3385 != NULL_TREE);
3386 /* Look for the optional nested-name-specifier. */
3387 nested_name_specifier_p
3388 = (cp_parser_nested_name_specifier_opt (parser,
3389 /*typename_keyword_p=*/false,
3390 check_dependency_p,
3391 /*type_p=*/false,
3392 declarator_p)
3393 != NULL_TREE);
3394 /* If there is a nested-name-specifier, then we are looking at
3395 the first qualified-id production. */
3396 if (nested_name_specifier_p)
3397 {
3398 tree saved_scope;
3399 tree saved_object_scope;
3400 tree saved_qualifying_scope;
3401 tree unqualified_id;
3402 bool is_template;
3403
3404 /* See if the next token is the `template' keyword. */
3405 if (!template_p)
3406 template_p = &is_template;
3407 *template_p = cp_parser_optional_template_keyword (parser);
3408 /* Name lookup we do during the processing of the
3409 unqualified-id might obliterate SCOPE. */
3410 saved_scope = parser->scope;
3411 saved_object_scope = parser->object_scope;
3412 saved_qualifying_scope = parser->qualifying_scope;
3413 /* Process the final unqualified-id. */
3414 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3415 check_dependency_p,
3416 declarator_p,
3417 /*optional_p=*/false);
3418 /* Restore the SAVED_SCOPE for our caller. */
3419 parser->scope = saved_scope;
3420 parser->object_scope = saved_object_scope;
3421 parser->qualifying_scope = saved_qualifying_scope;
3422
3423 return unqualified_id;
3424 }
3425 /* Otherwise, if we are in global scope, then we are looking at one
3426 of the other qualified-id productions. */
3427 else if (global_scope_p)
3428 {
3429 cp_token *token;
3430 tree id;
3431
3432 /* Peek at the next token. */
3433 token = cp_lexer_peek_token (parser->lexer);
3434
3435 /* If it's an identifier, and the next token is not a "<", then
3436 we can avoid the template-id case. This is an optimization
3437 for this common case. */
3438 if (token->type == CPP_NAME
3439 && !cp_parser_nth_token_starts_template_argument_list_p
3440 (parser, 2))
3441 return cp_parser_identifier (parser);
3442
3443 cp_parser_parse_tentatively (parser);
3444 /* Try a template-id. */
3445 id = cp_parser_template_id (parser,
3446 /*template_keyword_p=*/false,
3447 /*check_dependency_p=*/true,
3448 declarator_p);
3449 /* If that worked, we're done. */
3450 if (cp_parser_parse_definitely (parser))
3451 return id;
3452
3453 /* Peek at the next token. (Changes in the token buffer may
3454 have invalidated the pointer obtained above.) */
3455 token = cp_lexer_peek_token (parser->lexer);
3456
3457 switch (token->type)
3458 {
3459 case CPP_NAME:
3460 return cp_parser_identifier (parser);
3461
3462 case CPP_KEYWORD:
3463 if (token->keyword == RID_OPERATOR)
3464 return cp_parser_operator_function_id (parser);
3465 /* Fall through. */
3466
3467 default:
3468 cp_parser_error (parser, "expected id-expression");
3469 return error_mark_node;
3470 }
3471 }
3472 else
3473 return cp_parser_unqualified_id (parser, template_keyword_p,
3474 /*check_dependency_p=*/true,
3475 declarator_p,
3476 optional_p);
3477 }
3478
3479 /* Parse an unqualified-id.
3480
3481 unqualified-id:
3482 identifier
3483 operator-function-id
3484 conversion-function-id
3485 ~ class-name
3486 template-id
3487
3488 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3489 keyword, in a construct like `A::template ...'.
3490
3491 Returns a representation of unqualified-id. For the `identifier'
3492 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3493 production a BIT_NOT_EXPR is returned; the operand of the
3494 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3495 other productions, see the documentation accompanying the
3496 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3497 names are looked up in uninstantiated templates. If DECLARATOR_P
3498 is true, the unqualified-id is appearing as part of a declarator,
3499 rather than as part of an expression. */
3500
3501 static tree
3502 cp_parser_unqualified_id (cp_parser* parser,
3503 bool template_keyword_p,
3504 bool check_dependency_p,
3505 bool declarator_p,
3506 bool optional_p)
3507 {
3508 cp_token *token;
3509
3510 /* Peek at the next token. */
3511 token = cp_lexer_peek_token (parser->lexer);
3512
3513 switch (token->type)
3514 {
3515 case CPP_NAME:
3516 {
3517 tree id;
3518
3519 /* We don't know yet whether or not this will be a
3520 template-id. */
3521 cp_parser_parse_tentatively (parser);
3522 /* Try a template-id. */
3523 id = cp_parser_template_id (parser, template_keyword_p,
3524 check_dependency_p,
3525 declarator_p);
3526 /* If it worked, we're done. */
3527 if (cp_parser_parse_definitely (parser))
3528 return id;
3529 /* Otherwise, it's an ordinary identifier. */
3530 return cp_parser_identifier (parser);
3531 }
3532
3533 case CPP_TEMPLATE_ID:
3534 return cp_parser_template_id (parser, template_keyword_p,
3535 check_dependency_p,
3536 declarator_p);
3537
3538 case CPP_COMPL:
3539 {
3540 tree type_decl;
3541 tree qualifying_scope;
3542 tree object_scope;
3543 tree scope;
3544 bool done;
3545
3546 /* Consume the `~' token. */
3547 cp_lexer_consume_token (parser->lexer);
3548 /* Parse the class-name. The standard, as written, seems to
3549 say that:
3550
3551 template <typename T> struct S { ~S (); };
3552 template <typename T> S<T>::~S() {}
3553
3554 is invalid, since `~' must be followed by a class-name, but
3555 `S<T>' is dependent, and so not known to be a class.
3556 That's not right; we need to look in uninstantiated
3557 templates. A further complication arises from:
3558
3559 template <typename T> void f(T t) {
3560 t.T::~T();
3561 }
3562
3563 Here, it is not possible to look up `T' in the scope of `T'
3564 itself. We must look in both the current scope, and the
3565 scope of the containing complete expression.
3566
3567 Yet another issue is:
3568
3569 struct S {
3570 int S;
3571 ~S();
3572 };
3573
3574 S::~S() {}
3575
3576 The standard does not seem to say that the `S' in `~S'
3577 should refer to the type `S' and not the data member
3578 `S::S'. */
3579
3580 /* DR 244 says that we look up the name after the "~" in the
3581 same scope as we looked up the qualifying name. That idea
3582 isn't fully worked out; it's more complicated than that. */
3583 scope = parser->scope;
3584 object_scope = parser->object_scope;
3585 qualifying_scope = parser->qualifying_scope;
3586
3587 /* Check for invalid scopes. */
3588 if (scope == error_mark_node)
3589 {
3590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3591 cp_lexer_consume_token (parser->lexer);
3592 return error_mark_node;
3593 }
3594 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3595 {
3596 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3597 error ("scope %qT before %<~%> is not a class-name", scope);
3598 cp_parser_simulate_error (parser);
3599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3600 cp_lexer_consume_token (parser->lexer);
3601 return error_mark_node;
3602 }
3603 gcc_assert (!scope || TYPE_P (scope));
3604
3605 /* If the name is of the form "X::~X" it's OK. */
3606 token = cp_lexer_peek_token (parser->lexer);
3607 if (scope
3608 && token->type == CPP_NAME
3609 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3610 == CPP_OPEN_PAREN)
3611 && constructor_name_p (token->u.value, scope))
3612 {
3613 cp_lexer_consume_token (parser->lexer);
3614 return build_nt (BIT_NOT_EXPR, scope);
3615 }
3616
3617 /* If there was an explicit qualification (S::~T), first look
3618 in the scope given by the qualification (i.e., S). */
3619 done = false;
3620 type_decl = NULL_TREE;
3621 if (scope)
3622 {
3623 cp_parser_parse_tentatively (parser);
3624 type_decl = cp_parser_class_name (parser,
3625 /*typename_keyword_p=*/false,
3626 /*template_keyword_p=*/false,
3627 none_type,
3628 /*check_dependency=*/false,
3629 /*class_head_p=*/false,
3630 declarator_p);
3631 if (cp_parser_parse_definitely (parser))
3632 done = true;
3633 }
3634 /* In "N::S::~S", look in "N" as well. */
3635 if (!done && scope && qualifying_scope)
3636 {
3637 cp_parser_parse_tentatively (parser);
3638 parser->scope = qualifying_scope;
3639 parser->object_scope = NULL_TREE;
3640 parser->qualifying_scope = NULL_TREE;
3641 type_decl
3642 = cp_parser_class_name (parser,
3643 /*typename_keyword_p=*/false,
3644 /*template_keyword_p=*/false,
3645 none_type,
3646 /*check_dependency=*/false,
3647 /*class_head_p=*/false,
3648 declarator_p);
3649 if (cp_parser_parse_definitely (parser))
3650 done = true;
3651 }
3652 /* In "p->S::~T", look in the scope given by "*p" as well. */
3653 else if (!done && object_scope)
3654 {
3655 cp_parser_parse_tentatively (parser);
3656 parser->scope = object_scope;
3657 parser->object_scope = NULL_TREE;
3658 parser->qualifying_scope = NULL_TREE;
3659 type_decl
3660 = cp_parser_class_name (parser,
3661 /*typename_keyword_p=*/false,
3662 /*template_keyword_p=*/false,
3663 none_type,
3664 /*check_dependency=*/false,
3665 /*class_head_p=*/false,
3666 declarator_p);
3667 if (cp_parser_parse_definitely (parser))
3668 done = true;
3669 }
3670 /* Look in the surrounding context. */
3671 if (!done)
3672 {
3673 parser->scope = NULL_TREE;
3674 parser->object_scope = NULL_TREE;
3675 parser->qualifying_scope = NULL_TREE;
3676 type_decl
3677 = cp_parser_class_name (parser,
3678 /*typename_keyword_p=*/false,
3679 /*template_keyword_p=*/false,
3680 none_type,
3681 /*check_dependency=*/false,
3682 /*class_head_p=*/false,
3683 declarator_p);
3684 }
3685 /* If an error occurred, assume that the name of the
3686 destructor is the same as the name of the qualifying
3687 class. That allows us to keep parsing after running
3688 into ill-formed destructor names. */
3689 if (type_decl == error_mark_node && scope)
3690 return build_nt (BIT_NOT_EXPR, scope);
3691 else if (type_decl == error_mark_node)
3692 return error_mark_node;
3693
3694 /* Check that destructor name and scope match. */
3695 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3696 {
3697 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3698 error ("declaration of %<~%T%> as member of %qT",
3699 type_decl, scope);
3700 cp_parser_simulate_error (parser);
3701 return error_mark_node;
3702 }
3703
3704 /* [class.dtor]
3705
3706 A typedef-name that names a class shall not be used as the
3707 identifier in the declarator for a destructor declaration. */
3708 if (declarator_p
3709 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3710 && !DECL_SELF_REFERENCE_P (type_decl)
3711 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3712 error ("typedef-name %qD used as destructor declarator",
3713 type_decl);
3714
3715 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3716 }
3717
3718 case CPP_KEYWORD:
3719 if (token->keyword == RID_OPERATOR)
3720 {
3721 tree id;
3722
3723 /* This could be a template-id, so we try that first. */
3724 cp_parser_parse_tentatively (parser);
3725 /* Try a template-id. */
3726 id = cp_parser_template_id (parser, template_keyword_p,
3727 /*check_dependency_p=*/true,
3728 declarator_p);
3729 /* If that worked, we're done. */
3730 if (cp_parser_parse_definitely (parser))
3731 return id;
3732 /* We still don't know whether we're looking at an
3733 operator-function-id or a conversion-function-id. */
3734 cp_parser_parse_tentatively (parser);
3735 /* Try an operator-function-id. */
3736 id = cp_parser_operator_function_id (parser);
3737 /* If that didn't work, try a conversion-function-id. */
3738 if (!cp_parser_parse_definitely (parser))
3739 id = cp_parser_conversion_function_id (parser);
3740
3741 return id;
3742 }
3743 /* Fall through. */
3744
3745 default:
3746 if (optional_p)
3747 return NULL_TREE;
3748 cp_parser_error (parser, "expected unqualified-id");
3749 return error_mark_node;
3750 }
3751 }
3752
3753 /* Parse an (optional) nested-name-specifier.
3754
3755 nested-name-specifier:
3756 class-or-namespace-name :: nested-name-specifier [opt]
3757 class-or-namespace-name :: template nested-name-specifier [opt]
3758
3759 PARSER->SCOPE should be set appropriately before this function is
3760 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3761 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3762 in name lookups.
3763
3764 Sets PARSER->SCOPE to the class (TYPE) or namespace
3765 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3766 it unchanged if there is no nested-name-specifier. Returns the new
3767 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3768
3769 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3770 part of a declaration and/or decl-specifier. */
3771
3772 static tree
3773 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3774 bool typename_keyword_p,
3775 bool check_dependency_p,
3776 bool type_p,
3777 bool is_declaration)
3778 {
3779 bool success = false;
3780 cp_token_position start = 0;
3781 cp_token *token;
3782
3783 /* Remember where the nested-name-specifier starts. */
3784 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3785 {
3786 start = cp_lexer_token_position (parser->lexer, false);
3787 push_deferring_access_checks (dk_deferred);
3788 }
3789
3790 while (true)
3791 {
3792 tree new_scope;
3793 tree old_scope;
3794 tree saved_qualifying_scope;
3795 bool template_keyword_p;
3796
3797 /* Spot cases that cannot be the beginning of a
3798 nested-name-specifier. */
3799 token = cp_lexer_peek_token (parser->lexer);
3800
3801 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3802 the already parsed nested-name-specifier. */
3803 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3804 {
3805 /* Grab the nested-name-specifier and continue the loop. */
3806 cp_parser_pre_parsed_nested_name_specifier (parser);
3807 /* If we originally encountered this nested-name-specifier
3808 with IS_DECLARATION set to false, we will not have
3809 resolved TYPENAME_TYPEs, so we must do so here. */
3810 if (is_declaration
3811 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3812 {
3813 new_scope = resolve_typename_type (parser->scope,
3814 /*only_current_p=*/false);
3815 if (new_scope != error_mark_node)
3816 parser->scope = new_scope;
3817 }
3818 success = true;
3819 continue;
3820 }
3821
3822 /* Spot cases that cannot be the beginning of a
3823 nested-name-specifier. On the second and subsequent times
3824 through the loop, we look for the `template' keyword. */
3825 if (success && token->keyword == RID_TEMPLATE)
3826 ;
3827 /* A template-id can start a nested-name-specifier. */
3828 else if (token->type == CPP_TEMPLATE_ID)
3829 ;
3830 else
3831 {
3832 /* If the next token is not an identifier, then it is
3833 definitely not a class-or-namespace-name. */
3834 if (token->type != CPP_NAME)
3835 break;
3836 /* If the following token is neither a `<' (to begin a
3837 template-id), nor a `::', then we are not looking at a
3838 nested-name-specifier. */
3839 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3840 if (token->type != CPP_SCOPE
3841 && !cp_parser_nth_token_starts_template_argument_list_p
3842 (parser, 2))
3843 break;
3844 }
3845
3846 /* The nested-name-specifier is optional, so we parse
3847 tentatively. */
3848 cp_parser_parse_tentatively (parser);
3849
3850 /* Look for the optional `template' keyword, if this isn't the
3851 first time through the loop. */
3852 if (success)
3853 template_keyword_p = cp_parser_optional_template_keyword (parser);
3854 else
3855 template_keyword_p = false;
3856
3857 /* Save the old scope since the name lookup we are about to do
3858 might destroy it. */
3859 old_scope = parser->scope;
3860 saved_qualifying_scope = parser->qualifying_scope;
3861 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3862 look up names in "X<T>::I" in order to determine that "Y" is
3863 a template. So, if we have a typename at this point, we make
3864 an effort to look through it. */
3865 if (is_declaration
3866 && !typename_keyword_p
3867 && parser->scope
3868 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3869 parser->scope = resolve_typename_type (parser->scope,
3870 /*only_current_p=*/false);
3871 /* Parse the qualifying entity. */
3872 new_scope
3873 = cp_parser_class_or_namespace_name (parser,
3874 typename_keyword_p,
3875 template_keyword_p,
3876 check_dependency_p,
3877 type_p,
3878 is_declaration);
3879 /* Look for the `::' token. */
3880 cp_parser_require (parser, CPP_SCOPE, "`::'");
3881
3882 /* If we found what we wanted, we keep going; otherwise, we're
3883 done. */
3884 if (!cp_parser_parse_definitely (parser))
3885 {
3886 bool error_p = false;
3887
3888 /* Restore the OLD_SCOPE since it was valid before the
3889 failed attempt at finding the last
3890 class-or-namespace-name. */
3891 parser->scope = old_scope;
3892 parser->qualifying_scope = saved_qualifying_scope;
3893 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3894 break;
3895 /* If the next token is an identifier, and the one after
3896 that is a `::', then any valid interpretation would have
3897 found a class-or-namespace-name. */
3898 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3899 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3900 == CPP_SCOPE)
3901 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3902 != CPP_COMPL))
3903 {
3904 token = cp_lexer_consume_token (parser->lexer);
3905 if (!error_p)
3906 {
3907 if (!token->ambiguous_p)
3908 {
3909 tree decl;
3910 tree ambiguous_decls;
3911
3912 decl = cp_parser_lookup_name (parser, token->u.value,
3913 none_type,
3914 /*is_template=*/false,
3915 /*is_namespace=*/false,
3916 /*check_dependency=*/true,
3917 &ambiguous_decls);
3918 if (TREE_CODE (decl) == TEMPLATE_DECL)
3919 error ("%qD used without template parameters", decl);
3920 else if (ambiguous_decls)
3921 {
3922 error ("reference to %qD is ambiguous",
3923 token->u.value);
3924 print_candidates (ambiguous_decls);
3925 decl = error_mark_node;
3926 }
3927 else
3928 cp_parser_name_lookup_error
3929 (parser, token->u.value, decl,
3930 "is not a class or namespace");
3931 }
3932 parser->scope = error_mark_node;
3933 error_p = true;
3934 /* Treat this as a successful nested-name-specifier
3935 due to:
3936
3937 [basic.lookup.qual]
3938
3939 If the name found is not a class-name (clause
3940 _class_) or namespace-name (_namespace.def_), the
3941 program is ill-formed. */
3942 success = true;
3943 }
3944 cp_lexer_consume_token (parser->lexer);
3945 }
3946 break;
3947 }
3948 /* We've found one valid nested-name-specifier. */
3949 success = true;
3950 /* Name lookup always gives us a DECL. */
3951 if (TREE_CODE (new_scope) == TYPE_DECL)
3952 new_scope = TREE_TYPE (new_scope);
3953 /* Uses of "template" must be followed by actual templates. */
3954 if (template_keyword_p
3955 && !(CLASS_TYPE_P (new_scope)
3956 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3957 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3958 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3959 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3960 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3961 == TEMPLATE_ID_EXPR)))
3962 pedwarn (TYPE_P (new_scope)
3963 ? "%qT is not a template"
3964 : "%qD is not a template",
3965 new_scope);
3966 /* If it is a class scope, try to complete it; we are about to
3967 be looking up names inside the class. */
3968 if (TYPE_P (new_scope)
3969 /* Since checking types for dependency can be expensive,
3970 avoid doing it if the type is already complete. */
3971 && !COMPLETE_TYPE_P (new_scope)
3972 /* Do not try to complete dependent types. */
3973 && !dependent_type_p (new_scope))
3974 new_scope = complete_type (new_scope);
3975 /* Make sure we look in the right scope the next time through
3976 the loop. */
3977 parser->scope = new_scope;
3978 }
3979
3980 /* If parsing tentatively, replace the sequence of tokens that makes
3981 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3982 token. That way, should we re-parse the token stream, we will
3983 not have to repeat the effort required to do the parse, nor will
3984 we issue duplicate error messages. */
3985 if (success && start)
3986 {
3987 cp_token *token;
3988
3989 token = cp_lexer_token_at (parser->lexer, start);
3990 /* Reset the contents of the START token. */
3991 token->type = CPP_NESTED_NAME_SPECIFIER;
3992 /* Retrieve any deferred checks. Do not pop this access checks yet
3993 so the memory will not be reclaimed during token replacing below. */
3994 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3995 token->u.tree_check_value->value = parser->scope;
3996 token->u.tree_check_value->checks = get_deferred_access_checks ();
3997 token->u.tree_check_value->qualifying_scope =
3998 parser->qualifying_scope;
3999 token->keyword = RID_MAX;
4000
4001 /* Purge all subsequent tokens. */
4002 cp_lexer_purge_tokens_after (parser->lexer, start);
4003 }
4004
4005 if (start)
4006 pop_to_parent_deferring_access_checks ();
4007
4008 return success ? parser->scope : NULL_TREE;
4009 }
4010
4011 /* Parse a nested-name-specifier. See
4012 cp_parser_nested_name_specifier_opt for details. This function
4013 behaves identically, except that it will an issue an error if no
4014 nested-name-specifier is present. */
4015
4016 static tree
4017 cp_parser_nested_name_specifier (cp_parser *parser,
4018 bool typename_keyword_p,
4019 bool check_dependency_p,
4020 bool type_p,
4021 bool is_declaration)
4022 {
4023 tree scope;
4024
4025 /* Look for the nested-name-specifier. */
4026 scope = cp_parser_nested_name_specifier_opt (parser,
4027 typename_keyword_p,
4028 check_dependency_p,
4029 type_p,
4030 is_declaration);
4031 /* If it was not present, issue an error message. */
4032 if (!scope)
4033 {
4034 cp_parser_error (parser, "expected nested-name-specifier");
4035 parser->scope = NULL_TREE;
4036 }
4037
4038 return scope;
4039 }
4040
4041 /* Parse a class-or-namespace-name.
4042
4043 class-or-namespace-name:
4044 class-name
4045 namespace-name
4046
4047 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4048 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4049 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4050 TYPE_P is TRUE iff the next name should be taken as a class-name,
4051 even the same name is declared to be another entity in the same
4052 scope.
4053
4054 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4055 specified by the class-or-namespace-name. If neither is found the
4056 ERROR_MARK_NODE is returned. */
4057
4058 static tree
4059 cp_parser_class_or_namespace_name (cp_parser *parser,
4060 bool typename_keyword_p,
4061 bool template_keyword_p,
4062 bool check_dependency_p,
4063 bool type_p,
4064 bool is_declaration)
4065 {
4066 tree saved_scope;
4067 tree saved_qualifying_scope;
4068 tree saved_object_scope;
4069 tree scope;
4070 bool only_class_p;
4071
4072 /* Before we try to parse the class-name, we must save away the
4073 current PARSER->SCOPE since cp_parser_class_name will destroy
4074 it. */
4075 saved_scope = parser->scope;
4076 saved_qualifying_scope = parser->qualifying_scope;
4077 saved_object_scope = parser->object_scope;
4078 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4079 there is no need to look for a namespace-name. */
4080 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4081 if (!only_class_p)
4082 cp_parser_parse_tentatively (parser);
4083 scope = cp_parser_class_name (parser,
4084 typename_keyword_p,
4085 template_keyword_p,
4086 type_p ? class_type : none_type,
4087 check_dependency_p,
4088 /*class_head_p=*/false,
4089 is_declaration);
4090 /* If that didn't work, try for a namespace-name. */
4091 if (!only_class_p && !cp_parser_parse_definitely (parser))
4092 {
4093 /* Restore the saved scope. */
4094 parser->scope = saved_scope;
4095 parser->qualifying_scope = saved_qualifying_scope;
4096 parser->object_scope = saved_object_scope;
4097 /* If we are not looking at an identifier followed by the scope
4098 resolution operator, then this is not part of a
4099 nested-name-specifier. (Note that this function is only used
4100 to parse the components of a nested-name-specifier.) */
4101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4102 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4103 return error_mark_node;
4104 scope = cp_parser_namespace_name (parser);
4105 }
4106
4107 return scope;
4108 }
4109
4110 /* Parse a postfix-expression.
4111
4112 postfix-expression:
4113 primary-expression
4114 postfix-expression [ expression ]
4115 postfix-expression ( expression-list [opt] )
4116 simple-type-specifier ( expression-list [opt] )
4117 typename :: [opt] nested-name-specifier identifier
4118 ( expression-list [opt] )
4119 typename :: [opt] nested-name-specifier template [opt] template-id
4120 ( expression-list [opt] )
4121 postfix-expression . template [opt] id-expression
4122 postfix-expression -> template [opt] id-expression
4123 postfix-expression . pseudo-destructor-name
4124 postfix-expression -> pseudo-destructor-name
4125 postfix-expression ++
4126 postfix-expression --
4127 dynamic_cast < type-id > ( expression )
4128 static_cast < type-id > ( expression )
4129 reinterpret_cast < type-id > ( expression )
4130 const_cast < type-id > ( expression )
4131 typeid ( expression )
4132 typeid ( type-id )
4133
4134 GNU Extension:
4135
4136 postfix-expression:
4137 ( type-id ) { initializer-list , [opt] }
4138
4139 This extension is a GNU version of the C99 compound-literal
4140 construct. (The C99 grammar uses `type-name' instead of `type-id',
4141 but they are essentially the same concept.)
4142
4143 If ADDRESS_P is true, the postfix expression is the operand of the
4144 `&' operator. CAST_P is true if this expression is the target of a
4145 cast.
4146
4147 Returns a representation of the expression. */
4148
4149 static tree
4150 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4151 {
4152 cp_token *token;
4153 enum rid keyword;
4154 cp_id_kind idk = CP_ID_KIND_NONE;
4155 tree postfix_expression = NULL_TREE;
4156
4157 /* Peek at the next token. */
4158 token = cp_lexer_peek_token (parser->lexer);
4159 /* Some of the productions are determined by keywords. */
4160 keyword = token->keyword;
4161 switch (keyword)
4162 {
4163 case RID_DYNCAST:
4164 case RID_STATCAST:
4165 case RID_REINTCAST:
4166 case RID_CONSTCAST:
4167 {
4168 tree type;
4169 tree expression;
4170 const char *saved_message;
4171
4172 /* All of these can be handled in the same way from the point
4173 of view of parsing. Begin by consuming the token
4174 identifying the cast. */
4175 cp_lexer_consume_token (parser->lexer);
4176
4177 /* New types cannot be defined in the cast. */
4178 saved_message = parser->type_definition_forbidden_message;
4179 parser->type_definition_forbidden_message
4180 = "types may not be defined in casts";
4181
4182 /* Look for the opening `<'. */
4183 cp_parser_require (parser, CPP_LESS, "`<'");
4184 /* Parse the type to which we are casting. */
4185 type = cp_parser_type_id (parser);
4186 /* Look for the closing `>'. */
4187 cp_parser_require (parser, CPP_GREATER, "`>'");
4188 /* Restore the old message. */
4189 parser->type_definition_forbidden_message = saved_message;
4190
4191 /* And the expression which is being cast. */
4192 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4193 expression = cp_parser_expression (parser, /*cast_p=*/true);
4194 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4195
4196 /* Only type conversions to integral or enumeration types
4197 can be used in constant-expressions. */
4198 if (!cast_valid_in_integral_constant_expression_p (type)
4199 && (cp_parser_non_integral_constant_expression
4200 (parser,
4201 "a cast to a type other than an integral or "
4202 "enumeration type")))
4203 return error_mark_node;
4204
4205 switch (keyword)
4206 {
4207 case RID_DYNCAST:
4208 postfix_expression
4209 = build_dynamic_cast (type, expression);
4210 break;
4211 case RID_STATCAST:
4212 postfix_expression
4213 = build_static_cast (type, expression);
4214 break;
4215 case RID_REINTCAST:
4216 postfix_expression
4217 = build_reinterpret_cast (type, expression);
4218 break;
4219 case RID_CONSTCAST:
4220 postfix_expression
4221 = build_const_cast (type, expression);
4222 break;
4223 default:
4224 gcc_unreachable ();
4225 }
4226 }
4227 break;
4228
4229 case RID_TYPEID:
4230 {
4231 tree type;
4232 const char *saved_message;
4233 bool saved_in_type_id_in_expr_p;
4234
4235 /* Consume the `typeid' token. */
4236 cp_lexer_consume_token (parser->lexer);
4237 /* Look for the `(' token. */
4238 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4239 /* Types cannot be defined in a `typeid' expression. */
4240 saved_message = parser->type_definition_forbidden_message;
4241 parser->type_definition_forbidden_message
4242 = "types may not be defined in a `typeid\' expression";
4243 /* We can't be sure yet whether we're looking at a type-id or an
4244 expression. */
4245 cp_parser_parse_tentatively (parser);
4246 /* Try a type-id first. */
4247 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4248 parser->in_type_id_in_expr_p = true;
4249 type = cp_parser_type_id (parser);
4250 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4251 /* Look for the `)' token. Otherwise, we can't be sure that
4252 we're not looking at an expression: consider `typeid (int
4253 (3))', for example. */
4254 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4255 /* If all went well, simply lookup the type-id. */
4256 if (cp_parser_parse_definitely (parser))
4257 postfix_expression = get_typeid (type);
4258 /* Otherwise, fall back to the expression variant. */
4259 else
4260 {
4261 tree expression;
4262
4263 /* Look for an expression. */
4264 expression = cp_parser_expression (parser, /*cast_p=*/false);
4265 /* Compute its typeid. */
4266 postfix_expression = build_typeid (expression);
4267 /* Look for the `)' token. */
4268 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4269 }
4270 /* Restore the saved message. */
4271 parser->type_definition_forbidden_message = saved_message;
4272 /* `typeid' may not appear in an integral constant expression. */
4273 if (cp_parser_non_integral_constant_expression(parser,
4274 "`typeid' operator"))
4275 return error_mark_node;
4276 }
4277 break;
4278
4279 case RID_TYPENAME:
4280 {
4281 tree type;
4282 /* The syntax permitted here is the same permitted for an
4283 elaborated-type-specifier. */
4284 type = cp_parser_elaborated_type_specifier (parser,
4285 /*is_friend=*/false,
4286 /*is_declaration=*/false);
4287 postfix_expression = cp_parser_functional_cast (parser, type);
4288 }
4289 break;
4290
4291 default:
4292 {
4293 tree type;
4294
4295 /* If the next thing is a simple-type-specifier, we may be
4296 looking at a functional cast. We could also be looking at
4297 an id-expression. So, we try the functional cast, and if
4298 that doesn't work we fall back to the primary-expression. */
4299 cp_parser_parse_tentatively (parser);
4300 /* Look for the simple-type-specifier. */
4301 type = cp_parser_simple_type_specifier (parser,
4302 /*decl_specs=*/NULL,
4303 CP_PARSER_FLAGS_NONE);
4304 /* Parse the cast itself. */
4305 if (!cp_parser_error_occurred (parser))
4306 postfix_expression
4307 = cp_parser_functional_cast (parser, type);
4308 /* If that worked, we're done. */
4309 if (cp_parser_parse_definitely (parser))
4310 break;
4311
4312 /* If the functional-cast didn't work out, try a
4313 compound-literal. */
4314 if (cp_parser_allow_gnu_extensions_p (parser)
4315 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4316 {
4317 VEC(constructor_elt,gc) *initializer_list = NULL;
4318 bool saved_in_type_id_in_expr_p;
4319
4320 cp_parser_parse_tentatively (parser);
4321 /* Consume the `('. */
4322 cp_lexer_consume_token (parser->lexer);
4323 /* Parse the type. */
4324 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4325 parser->in_type_id_in_expr_p = true;
4326 type = cp_parser_type_id (parser);
4327 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4328 /* Look for the `)'. */
4329 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4330 /* Look for the `{'. */
4331 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4332 /* If things aren't going well, there's no need to
4333 keep going. */
4334 if (!cp_parser_error_occurred (parser))
4335 {
4336 bool non_constant_p;
4337 /* Parse the initializer-list. */
4338 initializer_list
4339 = cp_parser_initializer_list (parser, &non_constant_p);
4340 /* Allow a trailing `,'. */
4341 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4342 cp_lexer_consume_token (parser->lexer);
4343 /* Look for the final `}'. */
4344 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4345 }
4346 /* If that worked, we're definitely looking at a
4347 compound-literal expression. */
4348 if (cp_parser_parse_definitely (parser))
4349 {
4350 /* Warn the user that a compound literal is not
4351 allowed in standard C++. */
4352 if (pedantic)
4353 pedwarn ("ISO C++ forbids compound-literals");
4354 /* For simplicity, we disallow compound literals in
4355 constant-expressions. We could
4356 allow compound literals of integer type, whose
4357 initializer was a constant, in constant
4358 expressions. Permitting that usage, as a further
4359 extension, would not change the meaning of any
4360 currently accepted programs. (Of course, as
4361 compound literals are not part of ISO C++, the
4362 standard has nothing to say.) */
4363 if (cp_parser_non_integral_constant_expression
4364 (parser, "non-constant compound literals"))
4365 {
4366 postfix_expression = error_mark_node;
4367 break;
4368 }
4369 /* Form the representation of the compound-literal. */
4370 postfix_expression
4371 = finish_compound_literal (type, initializer_list);
4372 break;
4373 }
4374 }
4375
4376 /* It must be a primary-expression. */
4377 postfix_expression
4378 = cp_parser_primary_expression (parser, address_p, cast_p,
4379 /*template_arg_p=*/false,
4380 &idk);
4381 }
4382 break;
4383 }
4384
4385 /* Keep looping until the postfix-expression is complete. */
4386 while (true)
4387 {
4388 if (idk == CP_ID_KIND_UNQUALIFIED
4389 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4390 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4391 /* It is not a Koenig lookup function call. */
4392 postfix_expression
4393 = unqualified_name_lookup_error (postfix_expression);
4394
4395 /* Peek at the next token. */
4396 token = cp_lexer_peek_token (parser->lexer);
4397
4398 switch (token->type)
4399 {
4400 case CPP_OPEN_SQUARE:
4401 postfix_expression
4402 = cp_parser_postfix_open_square_expression (parser,
4403 postfix_expression,
4404 false);
4405 idk = CP_ID_KIND_NONE;
4406 break;
4407
4408 case CPP_OPEN_PAREN:
4409 /* postfix-expression ( expression-list [opt] ) */
4410 {
4411 bool koenig_p;
4412 bool is_builtin_constant_p;
4413 bool saved_integral_constant_expression_p = false;
4414 bool saved_non_integral_constant_expression_p = false;
4415 tree args;
4416
4417 is_builtin_constant_p
4418 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4419 if (is_builtin_constant_p)
4420 {
4421 /* The whole point of __builtin_constant_p is to allow
4422 non-constant expressions to appear as arguments. */
4423 saved_integral_constant_expression_p
4424 = parser->integral_constant_expression_p;
4425 saved_non_integral_constant_expression_p
4426 = parser->non_integral_constant_expression_p;
4427 parser->integral_constant_expression_p = false;
4428 }
4429 args = (cp_parser_parenthesized_expression_list
4430 (parser, /*is_attribute_list=*/false,
4431 /*cast_p=*/false, /*allow_expansion_p=*/true,
4432 /*non_constant_p=*/NULL));
4433 if (is_builtin_constant_p)
4434 {
4435 parser->integral_constant_expression_p
4436 = saved_integral_constant_expression_p;
4437 parser->non_integral_constant_expression_p
4438 = saved_non_integral_constant_expression_p;
4439 }
4440
4441 if (args == error_mark_node)
4442 {
4443 postfix_expression = error_mark_node;
4444 break;
4445 }
4446
4447 /* Function calls are not permitted in
4448 constant-expressions. */
4449 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4450 && cp_parser_non_integral_constant_expression (parser,
4451 "a function call"))
4452 {
4453 postfix_expression = error_mark_node;
4454 break;
4455 }
4456
4457 koenig_p = false;
4458 if (idk == CP_ID_KIND_UNQUALIFIED)
4459 {
4460 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4461 {
4462 if (args)
4463 {
4464 koenig_p = true;
4465 postfix_expression
4466 = perform_koenig_lookup (postfix_expression, args);
4467 }
4468 else
4469 postfix_expression
4470 = unqualified_fn_lookup_error (postfix_expression);
4471 }
4472 /* We do not perform argument-dependent lookup if
4473 normal lookup finds a non-function, in accordance
4474 with the expected resolution of DR 218. */
4475 else if (args && is_overloaded_fn (postfix_expression))
4476 {
4477 tree fn = get_first_fn (postfix_expression);
4478
4479 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4480 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4481
4482 /* Only do argument dependent lookup if regular
4483 lookup does not find a set of member functions.
4484 [basic.lookup.koenig]/2a */
4485 if (!DECL_FUNCTION_MEMBER_P (fn))
4486 {
4487 koenig_p = true;
4488 postfix_expression
4489 = perform_koenig_lookup (postfix_expression, args);
4490 }
4491 }
4492 }
4493
4494 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4495 {
4496 tree instance = TREE_OPERAND (postfix_expression, 0);
4497 tree fn = TREE_OPERAND (postfix_expression, 1);
4498
4499 if (processing_template_decl
4500 && (type_dependent_expression_p (instance)
4501 || (!BASELINK_P (fn)
4502 && TREE_CODE (fn) != FIELD_DECL)
4503 || type_dependent_expression_p (fn)
4504 || any_type_dependent_arguments_p (args)))
4505 {
4506 postfix_expression
4507 = build_nt_call_list (postfix_expression, args);
4508 break;
4509 }
4510
4511 if (BASELINK_P (fn))
4512 postfix_expression
4513 = (build_new_method_call
4514 (instance, fn, args, NULL_TREE,
4515 (idk == CP_ID_KIND_QUALIFIED
4516 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4517 /*fn_p=*/NULL));
4518 else
4519 postfix_expression
4520 = finish_call_expr (postfix_expression, args,
4521 /*disallow_virtual=*/false,
4522 /*koenig_p=*/false);
4523 }
4524 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4525 || TREE_CODE (postfix_expression) == MEMBER_REF
4526 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4527 postfix_expression = (build_offset_ref_call_from_tree
4528 (postfix_expression, args));
4529 else if (idk == CP_ID_KIND_QUALIFIED)
4530 /* A call to a static class member, or a namespace-scope
4531 function. */
4532 postfix_expression
4533 = finish_call_expr (postfix_expression, args,
4534 /*disallow_virtual=*/true,
4535 koenig_p);
4536 else
4537 /* All other function calls. */
4538 postfix_expression
4539 = finish_call_expr (postfix_expression, args,
4540 /*disallow_virtual=*/false,
4541 koenig_p);
4542
4543 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4544 idk = CP_ID_KIND_NONE;
4545 }
4546 break;
4547
4548 case CPP_DOT:
4549 case CPP_DEREF:
4550 /* postfix-expression . template [opt] id-expression
4551 postfix-expression . pseudo-destructor-name
4552 postfix-expression -> template [opt] id-expression
4553 postfix-expression -> pseudo-destructor-name */
4554
4555 /* Consume the `.' or `->' operator. */
4556 cp_lexer_consume_token (parser->lexer);
4557
4558 postfix_expression
4559 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4560 postfix_expression,
4561 false, &idk);
4562 break;
4563
4564 case CPP_PLUS_PLUS:
4565 /* postfix-expression ++ */
4566 /* Consume the `++' token. */
4567 cp_lexer_consume_token (parser->lexer);
4568 /* Generate a representation for the complete expression. */
4569 postfix_expression
4570 = finish_increment_expr (postfix_expression,
4571 POSTINCREMENT_EXPR);
4572 /* Increments may not appear in constant-expressions. */
4573 if (cp_parser_non_integral_constant_expression (parser,
4574 "an increment"))
4575 postfix_expression = error_mark_node;
4576 idk = CP_ID_KIND_NONE;
4577 break;
4578
4579 case CPP_MINUS_MINUS:
4580 /* postfix-expression -- */
4581 /* Consume the `--' token. */
4582 cp_lexer_consume_token (parser->lexer);
4583 /* Generate a representation for the complete expression. */
4584 postfix_expression
4585 = finish_increment_expr (postfix_expression,
4586 POSTDECREMENT_EXPR);
4587 /* Decrements may not appear in constant-expressions. */
4588 if (cp_parser_non_integral_constant_expression (parser,
4589 "a decrement"))
4590 postfix_expression = error_mark_node;
4591 idk = CP_ID_KIND_NONE;
4592 break;
4593
4594 default:
4595 return postfix_expression;
4596 }
4597 }
4598
4599 /* We should never get here. */
4600 gcc_unreachable ();
4601 return error_mark_node;
4602 }
4603
4604 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4605 by cp_parser_builtin_offsetof. We're looking for
4606
4607 postfix-expression [ expression ]
4608
4609 FOR_OFFSETOF is set if we're being called in that context, which
4610 changes how we deal with integer constant expressions. */
4611
4612 static tree
4613 cp_parser_postfix_open_square_expression (cp_parser *parser,
4614 tree postfix_expression,
4615 bool for_offsetof)
4616 {
4617 tree index;
4618
4619 /* Consume the `[' token. */
4620 cp_lexer_consume_token (parser->lexer);
4621
4622 /* Parse the index expression. */
4623 /* ??? For offsetof, there is a question of what to allow here. If
4624 offsetof is not being used in an integral constant expression context,
4625 then we *could* get the right answer by computing the value at runtime.
4626 If we are in an integral constant expression context, then we might
4627 could accept any constant expression; hard to say without analysis.
4628 Rather than open the barn door too wide right away, allow only integer
4629 constant expressions here. */
4630 if (for_offsetof)
4631 index = cp_parser_constant_expression (parser, false, NULL);
4632 else
4633 index = cp_parser_expression (parser, /*cast_p=*/false);
4634
4635 /* Look for the closing `]'. */
4636 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4637
4638 /* Build the ARRAY_REF. */
4639 postfix_expression = grok_array_decl (postfix_expression, index);
4640
4641 /* When not doing offsetof, array references are not permitted in
4642 constant-expressions. */
4643 if (!for_offsetof
4644 && (cp_parser_non_integral_constant_expression
4645 (parser, "an array reference")))
4646 postfix_expression = error_mark_node;
4647
4648 return postfix_expression;
4649 }
4650
4651 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4652 by cp_parser_builtin_offsetof. We're looking for
4653
4654 postfix-expression . template [opt] id-expression
4655 postfix-expression . pseudo-destructor-name
4656 postfix-expression -> template [opt] id-expression
4657 postfix-expression -> pseudo-destructor-name
4658
4659 FOR_OFFSETOF is set if we're being called in that context. That sorta
4660 limits what of the above we'll actually accept, but nevermind.
4661 TOKEN_TYPE is the "." or "->" token, which will already have been
4662 removed from the stream. */
4663
4664 static tree
4665 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4666 enum cpp_ttype token_type,
4667 tree postfix_expression,
4668 bool for_offsetof, cp_id_kind *idk)
4669 {
4670 tree name;
4671 bool dependent_p;
4672 bool pseudo_destructor_p;
4673 tree scope = NULL_TREE;
4674
4675 /* If this is a `->' operator, dereference the pointer. */
4676 if (token_type == CPP_DEREF)
4677 postfix_expression = build_x_arrow (postfix_expression);
4678 /* Check to see whether or not the expression is type-dependent. */
4679 dependent_p = type_dependent_expression_p (postfix_expression);
4680 /* The identifier following the `->' or `.' is not qualified. */
4681 parser->scope = NULL_TREE;
4682 parser->qualifying_scope = NULL_TREE;
4683 parser->object_scope = NULL_TREE;
4684 *idk = CP_ID_KIND_NONE;
4685 /* Enter the scope corresponding to the type of the object
4686 given by the POSTFIX_EXPRESSION. */
4687 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4688 {
4689 scope = TREE_TYPE (postfix_expression);
4690 /* According to the standard, no expression should ever have
4691 reference type. Unfortunately, we do not currently match
4692 the standard in this respect in that our internal representation
4693 of an expression may have reference type even when the standard
4694 says it does not. Therefore, we have to manually obtain the
4695 underlying type here. */
4696 scope = non_reference (scope);
4697 /* The type of the POSTFIX_EXPRESSION must be complete. */
4698 if (scope == unknown_type_node)
4699 {
4700 error ("%qE does not have class type", postfix_expression);
4701 scope = NULL_TREE;
4702 }
4703 else
4704 scope = complete_type_or_else (scope, NULL_TREE);
4705 /* Let the name lookup machinery know that we are processing a
4706 class member access expression. */
4707 parser->context->object_type = scope;
4708 /* If something went wrong, we want to be able to discern that case,
4709 as opposed to the case where there was no SCOPE due to the type
4710 of expression being dependent. */
4711 if (!scope)
4712 scope = error_mark_node;
4713 /* If the SCOPE was erroneous, make the various semantic analysis
4714 functions exit quickly -- and without issuing additional error
4715 messages. */
4716 if (scope == error_mark_node)
4717 postfix_expression = error_mark_node;
4718 }
4719
4720 /* Assume this expression is not a pseudo-destructor access. */
4721 pseudo_destructor_p = false;
4722
4723 /* If the SCOPE is a scalar type, then, if this is a valid program,
4724 we must be looking at a pseudo-destructor-name. */
4725 if (scope && SCALAR_TYPE_P (scope))
4726 {
4727 tree s;
4728 tree type;
4729
4730 cp_parser_parse_tentatively (parser);
4731 /* Parse the pseudo-destructor-name. */
4732 s = NULL_TREE;
4733 cp_parser_pseudo_destructor_name (parser, &s, &type);
4734 if (cp_parser_parse_definitely (parser))
4735 {
4736 pseudo_destructor_p = true;
4737 postfix_expression
4738 = finish_pseudo_destructor_expr (postfix_expression,
4739 s, TREE_TYPE (type));
4740 }
4741 }
4742
4743 if (!pseudo_destructor_p)
4744 {
4745 /* If the SCOPE is not a scalar type, we are looking at an
4746 ordinary class member access expression, rather than a
4747 pseudo-destructor-name. */
4748 bool template_p;
4749 /* Parse the id-expression. */
4750 name = (cp_parser_id_expression
4751 (parser,
4752 cp_parser_optional_template_keyword (parser),
4753 /*check_dependency_p=*/true,
4754 &template_p,
4755 /*declarator_p=*/false,
4756 /*optional_p=*/false));
4757 /* In general, build a SCOPE_REF if the member name is qualified.
4758 However, if the name was not dependent and has already been
4759 resolved; there is no need to build the SCOPE_REF. For example;
4760
4761 struct X { void f(); };
4762 template <typename T> void f(T* t) { t->X::f(); }
4763
4764 Even though "t" is dependent, "X::f" is not and has been resolved
4765 to a BASELINK; there is no need to include scope information. */
4766
4767 /* But we do need to remember that there was an explicit scope for
4768 virtual function calls. */
4769 if (parser->scope)
4770 *idk = CP_ID_KIND_QUALIFIED;
4771
4772 /* If the name is a template-id that names a type, we will get a
4773 TYPE_DECL here. That is invalid code. */
4774 if (TREE_CODE (name) == TYPE_DECL)
4775 {
4776 error ("invalid use of %qD", name);
4777 postfix_expression = error_mark_node;
4778 }
4779 else
4780 {
4781 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4782 {
4783 name = build_qualified_name (/*type=*/NULL_TREE,
4784 parser->scope,
4785 name,
4786 template_p);
4787 parser->scope = NULL_TREE;
4788 parser->qualifying_scope = NULL_TREE;
4789 parser->object_scope = NULL_TREE;
4790 }
4791 if (scope && name && BASELINK_P (name))
4792 adjust_result_of_qualified_name_lookup
4793 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4794 postfix_expression
4795 = finish_class_member_access_expr (postfix_expression, name,
4796 template_p);
4797 }
4798 }
4799
4800 /* We no longer need to look up names in the scope of the object on
4801 the left-hand side of the `.' or `->' operator. */
4802 parser->context->object_type = NULL_TREE;
4803
4804 /* Outside of offsetof, these operators may not appear in
4805 constant-expressions. */
4806 if (!for_offsetof
4807 && (cp_parser_non_integral_constant_expression
4808 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4809 postfix_expression = error_mark_node;
4810
4811 return postfix_expression;
4812 }
4813
4814 /* Parse a parenthesized expression-list.
4815
4816 expression-list:
4817 assignment-expression
4818 expression-list, assignment-expression
4819
4820 attribute-list:
4821 expression-list
4822 identifier
4823 identifier, expression-list
4824
4825 CAST_P is true if this expression is the target of a cast.
4826
4827 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4828 argument pack.
4829
4830 Returns a TREE_LIST. The TREE_VALUE of each node is a
4831 representation of an assignment-expression. Note that a TREE_LIST
4832 is returned even if there is only a single expression in the list.
4833 error_mark_node is returned if the ( and or ) are
4834 missing. NULL_TREE is returned on no expressions. The parentheses
4835 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4836 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4837 indicates whether or not all of the expressions in the list were
4838 constant. */
4839
4840 static tree
4841 cp_parser_parenthesized_expression_list (cp_parser* parser,
4842 bool is_attribute_list,
4843 bool cast_p,
4844 bool allow_expansion_p,
4845 bool *non_constant_p)
4846 {
4847 tree expression_list = NULL_TREE;
4848 bool fold_expr_p = is_attribute_list;
4849 tree identifier = NULL_TREE;
4850
4851 /* Assume all the expressions will be constant. */
4852 if (non_constant_p)
4853 *non_constant_p = false;
4854
4855 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4856 return error_mark_node;
4857
4858 /* Consume expressions until there are no more. */
4859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4860 while (true)
4861 {
4862 tree expr;
4863
4864 /* At the beginning of attribute lists, check to see if the
4865 next token is an identifier. */
4866 if (is_attribute_list
4867 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4868 {
4869 cp_token *token;
4870
4871 /* Consume the identifier. */
4872 token = cp_lexer_consume_token (parser->lexer);
4873 /* Save the identifier. */
4874 identifier = token->u.value;
4875 }
4876 else
4877 {
4878 /* Parse the next assignment-expression. */
4879 if (non_constant_p)
4880 {
4881 bool expr_non_constant_p;
4882 expr = (cp_parser_constant_expression
4883 (parser, /*allow_non_constant_p=*/true,
4884 &expr_non_constant_p));
4885 if (expr_non_constant_p)
4886 *non_constant_p = true;
4887 }
4888 else
4889 expr = cp_parser_assignment_expression (parser, cast_p);
4890
4891 if (fold_expr_p)
4892 expr = fold_non_dependent_expr (expr);
4893
4894 /* If we have an ellipsis, then this is an expression
4895 expansion. */
4896 if (allow_expansion_p
4897 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4898 {
4899 /* Consume the `...'. */
4900 cp_lexer_consume_token (parser->lexer);
4901
4902 /* Build the argument pack. */
4903 expr = make_pack_expansion (expr);
4904 }
4905
4906 /* Add it to the list. We add error_mark_node
4907 expressions to the list, so that we can still tell if
4908 the correct form for a parenthesized expression-list
4909 is found. That gives better errors. */
4910 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4911
4912 if (expr == error_mark_node)
4913 goto skip_comma;
4914 }
4915
4916 /* After the first item, attribute lists look the same as
4917 expression lists. */
4918 is_attribute_list = false;
4919
4920 get_comma:;
4921 /* If the next token isn't a `,', then we are done. */
4922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4923 break;
4924
4925 /* Otherwise, consume the `,' and keep going. */
4926 cp_lexer_consume_token (parser->lexer);
4927 }
4928
4929 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4930 {
4931 int ending;
4932
4933 skip_comma:;
4934 /* We try and resync to an unnested comma, as that will give the
4935 user better diagnostics. */
4936 ending = cp_parser_skip_to_closing_parenthesis (parser,
4937 /*recovering=*/true,
4938 /*or_comma=*/true,
4939 /*consume_paren=*/true);
4940 if (ending < 0)
4941 goto get_comma;
4942 if (!ending)
4943 return error_mark_node;
4944 }
4945
4946 /* We built up the list in reverse order so we must reverse it now. */
4947 expression_list = nreverse (expression_list);
4948 if (identifier)
4949 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4950
4951 return expression_list;
4952 }
4953
4954 /* Parse a pseudo-destructor-name.
4955
4956 pseudo-destructor-name:
4957 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4958 :: [opt] nested-name-specifier template template-id :: ~ type-name
4959 :: [opt] nested-name-specifier [opt] ~ type-name
4960
4961 If either of the first two productions is used, sets *SCOPE to the
4962 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4963 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4964 or ERROR_MARK_NODE if the parse fails. */
4965
4966 static void
4967 cp_parser_pseudo_destructor_name (cp_parser* parser,
4968 tree* scope,
4969 tree* type)
4970 {
4971 bool nested_name_specifier_p;
4972
4973 /* Assume that things will not work out. */
4974 *type = error_mark_node;
4975
4976 /* Look for the optional `::' operator. */
4977 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4978 /* Look for the optional nested-name-specifier. */
4979 nested_name_specifier_p
4980 = (cp_parser_nested_name_specifier_opt (parser,
4981 /*typename_keyword_p=*/false,
4982 /*check_dependency_p=*/true,
4983 /*type_p=*/false,
4984 /*is_declaration=*/true)
4985 != NULL_TREE);
4986 /* Now, if we saw a nested-name-specifier, we might be doing the
4987 second production. */
4988 if (nested_name_specifier_p
4989 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4990 {
4991 /* Consume the `template' keyword. */
4992 cp_lexer_consume_token (parser->lexer);
4993 /* Parse the template-id. */
4994 cp_parser_template_id (parser,
4995 /*template_keyword_p=*/true,
4996 /*check_dependency_p=*/false,
4997 /*is_declaration=*/true);
4998 /* Look for the `::' token. */
4999 cp_parser_require (parser, CPP_SCOPE, "`::'");
5000 }
5001 /* If the next token is not a `~', then there might be some
5002 additional qualification. */
5003 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5004 {
5005 /* Look for the type-name. */
5006 *scope = TREE_TYPE (cp_parser_type_name (parser));
5007
5008 if (*scope == error_mark_node)
5009 return;
5010
5011 /* If we don't have ::~, then something has gone wrong. Since
5012 the only caller of this function is looking for something
5013 after `.' or `->' after a scalar type, most likely the
5014 program is trying to get a member of a non-aggregate
5015 type. */
5016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5017 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5018 {
5019 cp_parser_error (parser, "request for member of non-aggregate type");
5020 return;
5021 }
5022
5023 /* Look for the `::' token. */
5024 cp_parser_require (parser, CPP_SCOPE, "`::'");
5025 }
5026 else
5027 *scope = NULL_TREE;
5028
5029 /* Look for the `~'. */
5030 cp_parser_require (parser, CPP_COMPL, "`~'");
5031 /* Look for the type-name again. We are not responsible for
5032 checking that it matches the first type-name. */
5033 *type = cp_parser_type_name (parser);
5034 }
5035
5036 /* Parse a unary-expression.
5037
5038 unary-expression:
5039 postfix-expression
5040 ++ cast-expression
5041 -- cast-expression
5042 unary-operator cast-expression
5043 sizeof unary-expression
5044 sizeof ( type-id )
5045 new-expression
5046 delete-expression
5047
5048 GNU Extensions:
5049
5050 unary-expression:
5051 __extension__ cast-expression
5052 __alignof__ unary-expression
5053 __alignof__ ( type-id )
5054 __real__ cast-expression
5055 __imag__ cast-expression
5056 && identifier
5057
5058 ADDRESS_P is true iff the unary-expression is appearing as the
5059 operand of the `&' operator. CAST_P is true if this expression is
5060 the target of a cast.
5061
5062 Returns a representation of the expression. */
5063
5064 static tree
5065 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5066 {
5067 cp_token *token;
5068 enum tree_code unary_operator;
5069
5070 /* Peek at the next token. */
5071 token = cp_lexer_peek_token (parser->lexer);
5072 /* Some keywords give away the kind of expression. */
5073 if (token->type == CPP_KEYWORD)
5074 {
5075 enum rid keyword = token->keyword;
5076
5077 switch (keyword)
5078 {
5079 case RID_ALIGNOF:
5080 case RID_SIZEOF:
5081 {
5082 tree operand;
5083 enum tree_code op;
5084
5085 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5086 /* Consume the token. */
5087 cp_lexer_consume_token (parser->lexer);
5088 /* Parse the operand. */
5089 operand = cp_parser_sizeof_operand (parser, keyword);
5090
5091 if (TYPE_P (operand))
5092 return cxx_sizeof_or_alignof_type (operand, op, true);
5093 else
5094 return cxx_sizeof_or_alignof_expr (operand, op);
5095 }
5096
5097 case RID_NEW:
5098 return cp_parser_new_expression (parser);
5099
5100 case RID_DELETE:
5101 return cp_parser_delete_expression (parser);
5102
5103 case RID_EXTENSION:
5104 {
5105 /* The saved value of the PEDANTIC flag. */
5106 int saved_pedantic;
5107 tree expr;
5108
5109 /* Save away the PEDANTIC flag. */
5110 cp_parser_extension_opt (parser, &saved_pedantic);
5111 /* Parse the cast-expression. */
5112 expr = cp_parser_simple_cast_expression (parser);
5113 /* Restore the PEDANTIC flag. */
5114 pedantic = saved_pedantic;
5115
5116 return expr;
5117 }
5118
5119 case RID_REALPART:
5120 case RID_IMAGPART:
5121 {
5122 tree expression;
5123
5124 /* Consume the `__real__' or `__imag__' token. */
5125 cp_lexer_consume_token (parser->lexer);
5126 /* Parse the cast-expression. */
5127 expression = cp_parser_simple_cast_expression (parser);
5128 /* Create the complete representation. */
5129 return build_x_unary_op ((keyword == RID_REALPART
5130 ? REALPART_EXPR : IMAGPART_EXPR),
5131 expression);
5132 }
5133 break;
5134
5135 default:
5136 break;
5137 }
5138 }
5139
5140 /* Look for the `:: new' and `:: delete', which also signal the
5141 beginning of a new-expression, or delete-expression,
5142 respectively. If the next token is `::', then it might be one of
5143 these. */
5144 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5145 {
5146 enum rid keyword;
5147
5148 /* See if the token after the `::' is one of the keywords in
5149 which we're interested. */
5150 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5151 /* If it's `new', we have a new-expression. */
5152 if (keyword == RID_NEW)
5153 return cp_parser_new_expression (parser);
5154 /* Similarly, for `delete'. */
5155 else if (keyword == RID_DELETE)
5156 return cp_parser_delete_expression (parser);
5157 }
5158
5159 /* Look for a unary operator. */
5160 unary_operator = cp_parser_unary_operator (token);
5161 /* The `++' and `--' operators can be handled similarly, even though
5162 they are not technically unary-operators in the grammar. */
5163 if (unary_operator == ERROR_MARK)
5164 {
5165 if (token->type == CPP_PLUS_PLUS)
5166 unary_operator = PREINCREMENT_EXPR;
5167 else if (token->type == CPP_MINUS_MINUS)
5168 unary_operator = PREDECREMENT_EXPR;
5169 /* Handle the GNU address-of-label extension. */
5170 else if (cp_parser_allow_gnu_extensions_p (parser)
5171 && token->type == CPP_AND_AND)
5172 {
5173 tree identifier;
5174
5175 /* Consume the '&&' token. */
5176 cp_lexer_consume_token (parser->lexer);
5177 /* Look for the identifier. */
5178 identifier = cp_parser_identifier (parser);
5179 /* Create an expression representing the address. */
5180 return finish_label_address_expr (identifier);
5181 }
5182 }
5183 if (unary_operator != ERROR_MARK)
5184 {
5185 tree cast_expression;
5186 tree expression = error_mark_node;
5187 const char *non_constant_p = NULL;
5188
5189 /* Consume the operator token. */
5190 token = cp_lexer_consume_token (parser->lexer);
5191 /* Parse the cast-expression. */
5192 cast_expression
5193 = cp_parser_cast_expression (parser,
5194 unary_operator == ADDR_EXPR,
5195 /*cast_p=*/false);
5196 /* Now, build an appropriate representation. */
5197 switch (unary_operator)
5198 {
5199 case INDIRECT_REF:
5200 non_constant_p = "`*'";
5201 expression = build_x_indirect_ref (cast_expression, "unary *");
5202 break;
5203
5204 case ADDR_EXPR:
5205 non_constant_p = "`&'";
5206 /* Fall through. */
5207 case BIT_NOT_EXPR:
5208 expression = build_x_unary_op (unary_operator, cast_expression);
5209 break;
5210
5211 case PREINCREMENT_EXPR:
5212 case PREDECREMENT_EXPR:
5213 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5214 ? "`++'" : "`--'");
5215 /* Fall through. */
5216 case UNARY_PLUS_EXPR:
5217 case NEGATE_EXPR:
5218 case TRUTH_NOT_EXPR:
5219 expression = finish_unary_op_expr (unary_operator, cast_expression);
5220 break;
5221
5222 default:
5223 gcc_unreachable ();
5224 }
5225
5226 if (non_constant_p
5227 && cp_parser_non_integral_constant_expression (parser,
5228 non_constant_p))
5229 expression = error_mark_node;
5230
5231 return expression;
5232 }
5233
5234 return cp_parser_postfix_expression (parser, address_p, cast_p);
5235 }
5236
5237 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5238 unary-operator, the corresponding tree code is returned. */
5239
5240 static enum tree_code
5241 cp_parser_unary_operator (cp_token* token)
5242 {
5243 switch (token->type)
5244 {
5245 case CPP_MULT:
5246 return INDIRECT_REF;
5247
5248 case CPP_AND:
5249 return ADDR_EXPR;
5250
5251 case CPP_PLUS:
5252 return UNARY_PLUS_EXPR;
5253
5254 case CPP_MINUS:
5255 return NEGATE_EXPR;
5256
5257 case CPP_NOT:
5258 return TRUTH_NOT_EXPR;
5259
5260 case CPP_COMPL:
5261 return BIT_NOT_EXPR;
5262
5263 default:
5264 return ERROR_MARK;
5265 }
5266 }
5267
5268 /* Parse a new-expression.
5269
5270 new-expression:
5271 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5272 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5273
5274 Returns a representation of the expression. */
5275
5276 static tree
5277 cp_parser_new_expression (cp_parser* parser)
5278 {
5279 bool global_scope_p;
5280 tree placement;
5281 tree type;
5282 tree initializer;
5283 tree nelts;
5284
5285 /* Look for the optional `::' operator. */
5286 global_scope_p
5287 = (cp_parser_global_scope_opt (parser,
5288 /*current_scope_valid_p=*/false)
5289 != NULL_TREE);
5290 /* Look for the `new' operator. */
5291 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5292 /* There's no easy way to tell a new-placement from the
5293 `( type-id )' construct. */
5294 cp_parser_parse_tentatively (parser);
5295 /* Look for a new-placement. */
5296 placement = cp_parser_new_placement (parser);
5297 /* If that didn't work out, there's no new-placement. */
5298 if (!cp_parser_parse_definitely (parser))
5299 placement = NULL_TREE;
5300
5301 /* If the next token is a `(', then we have a parenthesized
5302 type-id. */
5303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5304 {
5305 /* Consume the `('. */
5306 cp_lexer_consume_token (parser->lexer);
5307 /* Parse the type-id. */
5308 type = cp_parser_type_id (parser);
5309 /* Look for the closing `)'. */
5310 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5311 /* There should not be a direct-new-declarator in this production,
5312 but GCC used to allowed this, so we check and emit a sensible error
5313 message for this case. */
5314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5315 {
5316 error ("array bound forbidden after parenthesized type-id");
5317 inform ("try removing the parentheses around the type-id");
5318 cp_parser_direct_new_declarator (parser);
5319 }
5320 nelts = NULL_TREE;
5321 }
5322 /* Otherwise, there must be a new-type-id. */
5323 else
5324 type = cp_parser_new_type_id (parser, &nelts);
5325
5326 /* If the next token is a `(', then we have a new-initializer. */
5327 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5328 initializer = cp_parser_new_initializer (parser);
5329 else
5330 initializer = NULL_TREE;
5331
5332 /* A new-expression may not appear in an integral constant
5333 expression. */
5334 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5335 return error_mark_node;
5336
5337 /* Create a representation of the new-expression. */
5338 return build_new (placement, type, nelts, initializer, global_scope_p);
5339 }
5340
5341 /* Parse a new-placement.
5342
5343 new-placement:
5344 ( expression-list )
5345
5346 Returns the same representation as for an expression-list. */
5347
5348 static tree
5349 cp_parser_new_placement (cp_parser* parser)
5350 {
5351 tree expression_list;
5352
5353 /* Parse the expression-list. */
5354 expression_list = (cp_parser_parenthesized_expression_list
5355 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5356 /*non_constant_p=*/NULL));
5357
5358 return expression_list;
5359 }
5360
5361 /* Parse a new-type-id.
5362
5363 new-type-id:
5364 type-specifier-seq new-declarator [opt]
5365
5366 Returns the TYPE allocated. If the new-type-id indicates an array
5367 type, *NELTS is set to the number of elements in the last array
5368 bound; the TYPE will not include the last array bound. */
5369
5370 static tree
5371 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5372 {
5373 cp_decl_specifier_seq type_specifier_seq;
5374 cp_declarator *new_declarator;
5375 cp_declarator *declarator;
5376 cp_declarator *outer_declarator;
5377 const char *saved_message;
5378 tree type;
5379
5380 /* The type-specifier sequence must not contain type definitions.
5381 (It cannot contain declarations of new types either, but if they
5382 are not definitions we will catch that because they are not
5383 complete.) */
5384 saved_message = parser->type_definition_forbidden_message;
5385 parser->type_definition_forbidden_message
5386 = "types may not be defined in a new-type-id";
5387 /* Parse the type-specifier-seq. */
5388 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5389 &type_specifier_seq);
5390 /* Restore the old message. */
5391 parser->type_definition_forbidden_message = saved_message;
5392 /* Parse the new-declarator. */
5393 new_declarator = cp_parser_new_declarator_opt (parser);
5394
5395 /* Determine the number of elements in the last array dimension, if
5396 any. */
5397 *nelts = NULL_TREE;
5398 /* Skip down to the last array dimension. */
5399 declarator = new_declarator;
5400 outer_declarator = NULL;
5401 while (declarator && (declarator->kind == cdk_pointer
5402 || declarator->kind == cdk_ptrmem))
5403 {
5404 outer_declarator = declarator;
5405 declarator = declarator->declarator;
5406 }
5407 while (declarator
5408 && declarator->kind == cdk_array
5409 && declarator->declarator
5410 && declarator->declarator->kind == cdk_array)
5411 {
5412 outer_declarator = declarator;
5413 declarator = declarator->declarator;
5414 }
5415
5416 if (declarator && declarator->kind == cdk_array)
5417 {
5418 *nelts = declarator->u.array.bounds;
5419 if (*nelts == error_mark_node)
5420 *nelts = integer_one_node;
5421
5422 if (outer_declarator)
5423 outer_declarator->declarator = declarator->declarator;
5424 else
5425 new_declarator = NULL;
5426 }
5427
5428 type = groktypename (&type_specifier_seq, new_declarator);
5429 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5430 {
5431 *nelts = array_type_nelts_top (type);
5432 type = TREE_TYPE (type);
5433 }
5434 return type;
5435 }
5436
5437 /* Parse an (optional) new-declarator.
5438
5439 new-declarator:
5440 ptr-operator new-declarator [opt]
5441 direct-new-declarator
5442
5443 Returns the declarator. */
5444
5445 static cp_declarator *
5446 cp_parser_new_declarator_opt (cp_parser* parser)
5447 {
5448 enum tree_code code;
5449 tree type;
5450 cp_cv_quals cv_quals;
5451
5452 /* We don't know if there's a ptr-operator next, or not. */
5453 cp_parser_parse_tentatively (parser);
5454 /* Look for a ptr-operator. */
5455 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5456 /* If that worked, look for more new-declarators. */
5457 if (cp_parser_parse_definitely (parser))
5458 {
5459 cp_declarator *declarator;
5460
5461 /* Parse another optional declarator. */
5462 declarator = cp_parser_new_declarator_opt (parser);
5463
5464 /* Create the representation of the declarator. */
5465 if (type)
5466 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5467 else if (code == INDIRECT_REF)
5468 declarator = make_pointer_declarator (cv_quals, declarator);
5469 else
5470 declarator = make_reference_declarator (cv_quals, declarator);
5471
5472 return declarator;
5473 }
5474
5475 /* If the next token is a `[', there is a direct-new-declarator. */
5476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5477 return cp_parser_direct_new_declarator (parser);
5478
5479 return NULL;
5480 }
5481
5482 /* Parse a direct-new-declarator.
5483
5484 direct-new-declarator:
5485 [ expression ]
5486 direct-new-declarator [constant-expression]
5487
5488 */
5489
5490 static cp_declarator *
5491 cp_parser_direct_new_declarator (cp_parser* parser)
5492 {
5493 cp_declarator *declarator = NULL;
5494
5495 while (true)
5496 {
5497 tree expression;
5498
5499 /* Look for the opening `['. */
5500 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5501 /* The first expression is not required to be constant. */
5502 if (!declarator)
5503 {
5504 expression = cp_parser_expression (parser, /*cast_p=*/false);
5505 /* The standard requires that the expression have integral
5506 type. DR 74 adds enumeration types. We believe that the
5507 real intent is that these expressions be handled like the
5508 expression in a `switch' condition, which also allows
5509 classes with a single conversion to integral or
5510 enumeration type. */
5511 if (!processing_template_decl)
5512 {
5513 expression
5514 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5515 expression,
5516 /*complain=*/true);
5517 if (!expression)
5518 {
5519 error ("expression in new-declarator must have integral "
5520 "or enumeration type");
5521 expression = error_mark_node;
5522 }
5523 }
5524 }
5525 /* But all the other expressions must be. */
5526 else
5527 expression
5528 = cp_parser_constant_expression (parser,
5529 /*allow_non_constant=*/false,
5530 NULL);
5531 /* Look for the closing `]'. */
5532 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5533
5534 /* Add this bound to the declarator. */
5535 declarator = make_array_declarator (declarator, expression);
5536
5537 /* If the next token is not a `[', then there are no more
5538 bounds. */
5539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5540 break;
5541 }
5542
5543 return declarator;
5544 }
5545
5546 /* Parse a new-initializer.
5547
5548 new-initializer:
5549 ( expression-list [opt] )
5550
5551 Returns a representation of the expression-list. If there is no
5552 expression-list, VOID_ZERO_NODE is returned. */
5553
5554 static tree
5555 cp_parser_new_initializer (cp_parser* parser)
5556 {
5557 tree expression_list;
5558
5559 expression_list = (cp_parser_parenthesized_expression_list
5560 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5561 /*non_constant_p=*/NULL));
5562 if (!expression_list)
5563 expression_list = void_zero_node;
5564
5565 return expression_list;
5566 }
5567
5568 /* Parse a delete-expression.
5569
5570 delete-expression:
5571 :: [opt] delete cast-expression
5572 :: [opt] delete [ ] cast-expression
5573
5574 Returns a representation of the expression. */
5575
5576 static tree
5577 cp_parser_delete_expression (cp_parser* parser)
5578 {
5579 bool global_scope_p;
5580 bool array_p;
5581 tree expression;
5582
5583 /* Look for the optional `::' operator. */
5584 global_scope_p
5585 = (cp_parser_global_scope_opt (parser,
5586 /*current_scope_valid_p=*/false)
5587 != NULL_TREE);
5588 /* Look for the `delete' keyword. */
5589 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5590 /* See if the array syntax is in use. */
5591 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5592 {
5593 /* Consume the `[' token. */
5594 cp_lexer_consume_token (parser->lexer);
5595 /* Look for the `]' token. */
5596 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5597 /* Remember that this is the `[]' construct. */
5598 array_p = true;
5599 }
5600 else
5601 array_p = false;
5602
5603 /* Parse the cast-expression. */
5604 expression = cp_parser_simple_cast_expression (parser);
5605
5606 /* A delete-expression may not appear in an integral constant
5607 expression. */
5608 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5609 return error_mark_node;
5610
5611 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5612 }
5613
5614 /* Parse a cast-expression.
5615
5616 cast-expression:
5617 unary-expression
5618 ( type-id ) cast-expression
5619
5620 ADDRESS_P is true iff the unary-expression is appearing as the
5621 operand of the `&' operator. CAST_P is true if this expression is
5622 the target of a cast.
5623
5624 Returns a representation of the expression. */
5625
5626 static tree
5627 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5628 {
5629 /* If it's a `(', then we might be looking at a cast. */
5630 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5631 {
5632 tree type = NULL_TREE;
5633 tree expr = NULL_TREE;
5634 bool compound_literal_p;
5635 const char *saved_message;
5636
5637 /* There's no way to know yet whether or not this is a cast.
5638 For example, `(int (3))' is a unary-expression, while `(int)
5639 3' is a cast. So, we resort to parsing tentatively. */
5640 cp_parser_parse_tentatively (parser);
5641 /* Types may not be defined in a cast. */
5642 saved_message = parser->type_definition_forbidden_message;
5643 parser->type_definition_forbidden_message
5644 = "types may not be defined in casts";
5645 /* Consume the `('. */
5646 cp_lexer_consume_token (parser->lexer);
5647 /* A very tricky bit is that `(struct S) { 3 }' is a
5648 compound-literal (which we permit in C++ as an extension).
5649 But, that construct is not a cast-expression -- it is a
5650 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5651 is legal; if the compound-literal were a cast-expression,
5652 you'd need an extra set of parentheses.) But, if we parse
5653 the type-id, and it happens to be a class-specifier, then we
5654 will commit to the parse at that point, because we cannot
5655 undo the action that is done when creating a new class. So,
5656 then we cannot back up and do a postfix-expression.
5657
5658 Therefore, we scan ahead to the closing `)', and check to see
5659 if the token after the `)' is a `{'. If so, we are not
5660 looking at a cast-expression.
5661
5662 Save tokens so that we can put them back. */
5663 cp_lexer_save_tokens (parser->lexer);
5664 /* Skip tokens until the next token is a closing parenthesis.
5665 If we find the closing `)', and the next token is a `{', then
5666 we are looking at a compound-literal. */
5667 compound_literal_p
5668 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5669 /*consume_paren=*/true)
5670 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5671 /* Roll back the tokens we skipped. */
5672 cp_lexer_rollback_tokens (parser->lexer);
5673 /* If we were looking at a compound-literal, simulate an error
5674 so that the call to cp_parser_parse_definitely below will
5675 fail. */
5676 if (compound_literal_p)
5677 cp_parser_simulate_error (parser);
5678 else
5679 {
5680 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5681 parser->in_type_id_in_expr_p = true;
5682 /* Look for the type-id. */
5683 type = cp_parser_type_id (parser);
5684 /* Look for the closing `)'. */
5685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5686 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5687 }
5688
5689 /* Restore the saved message. */
5690 parser->type_definition_forbidden_message = saved_message;
5691
5692 /* If ok so far, parse the dependent expression. We cannot be
5693 sure it is a cast. Consider `(T ())'. It is a parenthesized
5694 ctor of T, but looks like a cast to function returning T
5695 without a dependent expression. */
5696 if (!cp_parser_error_occurred (parser))
5697 expr = cp_parser_cast_expression (parser,
5698 /*address_p=*/false,
5699 /*cast_p=*/true);
5700
5701 if (cp_parser_parse_definitely (parser))
5702 {
5703 /* Warn about old-style casts, if so requested. */
5704 if (warn_old_style_cast
5705 && !in_system_header
5706 && !VOID_TYPE_P (type)
5707 && current_lang_name != lang_name_c)
5708 warning (OPT_Wold_style_cast, "use of old-style cast");
5709
5710 /* Only type conversions to integral or enumeration types
5711 can be used in constant-expressions. */
5712 if (!cast_valid_in_integral_constant_expression_p (type)
5713 && (cp_parser_non_integral_constant_expression
5714 (parser,
5715 "a cast to a type other than an integral or "
5716 "enumeration type")))
5717 return error_mark_node;
5718
5719 /* Perform the cast. */
5720 expr = build_c_cast (type, expr);
5721 return expr;
5722 }
5723 }
5724
5725 /* If we get here, then it's not a cast, so it must be a
5726 unary-expression. */
5727 return cp_parser_unary_expression (parser, address_p, cast_p);
5728 }
5729
5730 /* Parse a binary expression of the general form:
5731
5732 pm-expression:
5733 cast-expression
5734 pm-expression .* cast-expression
5735 pm-expression ->* cast-expression
5736
5737 multiplicative-expression:
5738 pm-expression
5739 multiplicative-expression * pm-expression
5740 multiplicative-expression / pm-expression
5741 multiplicative-expression % pm-expression
5742
5743 additive-expression:
5744 multiplicative-expression
5745 additive-expression + multiplicative-expression
5746 additive-expression - multiplicative-expression
5747
5748 shift-expression:
5749 additive-expression
5750 shift-expression << additive-expression
5751 shift-expression >> additive-expression
5752
5753 relational-expression:
5754 shift-expression
5755 relational-expression < shift-expression
5756 relational-expression > shift-expression
5757 relational-expression <= shift-expression
5758 relational-expression >= shift-expression
5759
5760 GNU Extension:
5761
5762 relational-expression:
5763 relational-expression <? shift-expression
5764 relational-expression >? shift-expression
5765
5766 equality-expression:
5767 relational-expression
5768 equality-expression == relational-expression
5769 equality-expression != relational-expression
5770
5771 and-expression:
5772 equality-expression
5773 and-expression & equality-expression
5774
5775 exclusive-or-expression:
5776 and-expression
5777 exclusive-or-expression ^ and-expression
5778
5779 inclusive-or-expression:
5780 exclusive-or-expression
5781 inclusive-or-expression | exclusive-or-expression
5782
5783 logical-and-expression:
5784 inclusive-or-expression
5785 logical-and-expression && inclusive-or-expression
5786
5787 logical-or-expression:
5788 logical-and-expression
5789 logical-or-expression || logical-and-expression
5790
5791 All these are implemented with a single function like:
5792
5793 binary-expression:
5794 simple-cast-expression
5795 binary-expression <token> binary-expression
5796
5797 CAST_P is true if this expression is the target of a cast.
5798
5799 The binops_by_token map is used to get the tree codes for each <token> type.
5800 binary-expressions are associated according to a precedence table. */
5801
5802 #define TOKEN_PRECEDENCE(token) \
5803 (((token->type == CPP_GREATER \
5804 || (flag_cpp0x && token->type == CPP_RSHIFT)) \
5805 && !parser->greater_than_is_operator_p) \
5806 ? PREC_NOT_OPERATOR \
5807 : binops_by_token[token->type].prec)
5808
5809 static tree
5810 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5811 {
5812 cp_parser_expression_stack stack;
5813 cp_parser_expression_stack_entry *sp = &stack[0];
5814 tree lhs, rhs;
5815 cp_token *token;
5816 enum tree_code tree_type, lhs_type, rhs_type;
5817 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5818 bool overloaded_p;
5819
5820 /* Parse the first expression. */
5821 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5822 lhs_type = ERROR_MARK;
5823
5824 for (;;)
5825 {
5826 /* Get an operator token. */
5827 token = cp_lexer_peek_token (parser->lexer);
5828
5829 if (warn_cxx0x_compat
5830 && token->type == CPP_RSHIFT
5831 && !parser->greater_than_is_operator_p)
5832 {
5833 warning (OPT_Wc__0x_compat,
5834 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5835 &token->location);
5836 warning (OPT_Wc__0x_compat,
5837 "suggest parentheses around %<>>%> expression");
5838 }
5839
5840 new_prec = TOKEN_PRECEDENCE (token);
5841
5842 /* Popping an entry off the stack means we completed a subexpression:
5843 - either we found a token which is not an operator (`>' where it is not
5844 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5845 will happen repeatedly;
5846 - or, we found an operator which has lower priority. This is the case
5847 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5848 parsing `3 * 4'. */
5849 if (new_prec <= prec)
5850 {
5851 if (sp == stack)
5852 break;
5853 else
5854 goto pop;
5855 }
5856
5857 get_rhs:
5858 tree_type = binops_by_token[token->type].tree_type;
5859
5860 /* We used the operator token. */
5861 cp_lexer_consume_token (parser->lexer);
5862
5863 /* Extract another operand. It may be the RHS of this expression
5864 or the LHS of a new, higher priority expression. */
5865 rhs = cp_parser_simple_cast_expression (parser);
5866 rhs_type = ERROR_MARK;
5867
5868 /* Get another operator token. Look up its precedence to avoid
5869 building a useless (immediately popped) stack entry for common
5870 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5871 token = cp_lexer_peek_token (parser->lexer);
5872 lookahead_prec = TOKEN_PRECEDENCE (token);
5873 if (lookahead_prec > new_prec)
5874 {
5875 /* ... and prepare to parse the RHS of the new, higher priority
5876 expression. Since precedence levels on the stack are
5877 monotonically increasing, we do not have to care about
5878 stack overflows. */
5879 sp->prec = prec;
5880 sp->tree_type = tree_type;
5881 sp->lhs = lhs;
5882 sp->lhs_type = lhs_type;
5883 sp++;
5884 lhs = rhs;
5885 lhs_type = rhs_type;
5886 prec = new_prec;
5887 new_prec = lookahead_prec;
5888 goto get_rhs;
5889
5890 pop:
5891 /* If the stack is not empty, we have parsed into LHS the right side
5892 (`4' in the example above) of an expression we had suspended.
5893 We can use the information on the stack to recover the LHS (`3')
5894 from the stack together with the tree code (`MULT_EXPR'), and
5895 the precedence of the higher level subexpression
5896 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5897 which will be used to actually build the additive expression. */
5898 --sp;
5899 prec = sp->prec;
5900 tree_type = sp->tree_type;
5901 rhs = lhs;
5902 rhs_type = lhs_type;
5903 lhs = sp->lhs;
5904 lhs_type = sp->lhs_type;
5905 }
5906
5907 overloaded_p = false;
5908 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5909 &overloaded_p);
5910 lhs_type = tree_type;
5911
5912 /* If the binary operator required the use of an overloaded operator,
5913 then this expression cannot be an integral constant-expression.
5914 An overloaded operator can be used even if both operands are
5915 otherwise permissible in an integral constant-expression if at
5916 least one of the operands is of enumeration type. */
5917
5918 if (overloaded_p
5919 && (cp_parser_non_integral_constant_expression
5920 (parser, "calls to overloaded operators")))
5921 return error_mark_node;
5922 }
5923
5924 return lhs;
5925 }
5926
5927
5928 /* Parse the `? expression : assignment-expression' part of a
5929 conditional-expression. The LOGICAL_OR_EXPR is the
5930 logical-or-expression that started the conditional-expression.
5931 Returns a representation of the entire conditional-expression.
5932
5933 This routine is used by cp_parser_assignment_expression.
5934
5935 ? expression : assignment-expression
5936
5937 GNU Extensions:
5938
5939 ? : assignment-expression */
5940
5941 static tree
5942 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5943 {
5944 tree expr;
5945 tree assignment_expr;
5946
5947 /* Consume the `?' token. */
5948 cp_lexer_consume_token (parser->lexer);
5949 if (cp_parser_allow_gnu_extensions_p (parser)
5950 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5951 /* Implicit true clause. */
5952 expr = NULL_TREE;
5953 else
5954 /* Parse the expression. */
5955 expr = cp_parser_expression (parser, /*cast_p=*/false);
5956
5957 /* The next token should be a `:'. */
5958 cp_parser_require (parser, CPP_COLON, "`:'");
5959 /* Parse the assignment-expression. */
5960 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5961
5962 /* Build the conditional-expression. */
5963 return build_x_conditional_expr (logical_or_expr,
5964 expr,
5965 assignment_expr);
5966 }
5967
5968 /* Parse an assignment-expression.
5969
5970 assignment-expression:
5971 conditional-expression
5972 logical-or-expression assignment-operator assignment_expression
5973 throw-expression
5974
5975 CAST_P is true if this expression is the target of a cast.
5976
5977 Returns a representation for the expression. */
5978
5979 static tree
5980 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5981 {
5982 tree expr;
5983
5984 /* If the next token is the `throw' keyword, then we're looking at
5985 a throw-expression. */
5986 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5987 expr = cp_parser_throw_expression (parser);
5988 /* Otherwise, it must be that we are looking at a
5989 logical-or-expression. */
5990 else
5991 {
5992 /* Parse the binary expressions (logical-or-expression). */
5993 expr = cp_parser_binary_expression (parser, cast_p);
5994 /* If the next token is a `?' then we're actually looking at a
5995 conditional-expression. */
5996 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5997 return cp_parser_question_colon_clause (parser, expr);
5998 else
5999 {
6000 enum tree_code assignment_operator;
6001
6002 /* If it's an assignment-operator, we're using the second
6003 production. */
6004 assignment_operator
6005 = cp_parser_assignment_operator_opt (parser);
6006 if (assignment_operator != ERROR_MARK)
6007 {
6008 tree rhs;
6009
6010 /* Parse the right-hand side of the assignment. */
6011 rhs = cp_parser_assignment_expression (parser, cast_p);
6012 /* An assignment may not appear in a
6013 constant-expression. */
6014 if (cp_parser_non_integral_constant_expression (parser,
6015 "an assignment"))
6016 return error_mark_node;
6017 /* Build the assignment expression. */
6018 expr = build_x_modify_expr (expr,
6019 assignment_operator,
6020 rhs);
6021 }
6022 }
6023 }
6024
6025 return expr;
6026 }
6027
6028 /* Parse an (optional) assignment-operator.
6029
6030 assignment-operator: one of
6031 = *= /= %= += -= >>= <<= &= ^= |=
6032
6033 GNU Extension:
6034
6035 assignment-operator: one of
6036 <?= >?=
6037
6038 If the next token is an assignment operator, the corresponding tree
6039 code is returned, and the token is consumed. For example, for
6040 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6041 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6042 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6043 operator, ERROR_MARK is returned. */
6044
6045 static enum tree_code
6046 cp_parser_assignment_operator_opt (cp_parser* parser)
6047 {
6048 enum tree_code op;
6049 cp_token *token;
6050
6051 /* Peek at the next toen. */
6052 token = cp_lexer_peek_token (parser->lexer);
6053
6054 switch (token->type)
6055 {
6056 case CPP_EQ:
6057 op = NOP_EXPR;
6058 break;
6059
6060 case CPP_MULT_EQ:
6061 op = MULT_EXPR;
6062 break;
6063
6064 case CPP_DIV_EQ:
6065 op = TRUNC_DIV_EXPR;
6066 break;
6067
6068 case CPP_MOD_EQ:
6069 op = TRUNC_MOD_EXPR;
6070 break;
6071
6072 case CPP_PLUS_EQ:
6073 op = PLUS_EXPR;
6074 break;
6075
6076 case CPP_MINUS_EQ:
6077 op = MINUS_EXPR;
6078 break;
6079
6080 case CPP_RSHIFT_EQ:
6081 op = RSHIFT_EXPR;
6082 break;
6083
6084 case CPP_LSHIFT_EQ:
6085 op = LSHIFT_EXPR;
6086 break;
6087
6088 case CPP_AND_EQ:
6089 op = BIT_AND_EXPR;
6090 break;
6091
6092 case CPP_XOR_EQ:
6093 op = BIT_XOR_EXPR;
6094 break;
6095
6096 case CPP_OR_EQ:
6097 op = BIT_IOR_EXPR;
6098 break;
6099
6100 default:
6101 /* Nothing else is an assignment operator. */
6102 op = ERROR_MARK;
6103 }
6104
6105 /* If it was an assignment operator, consume it. */
6106 if (op != ERROR_MARK)
6107 cp_lexer_consume_token (parser->lexer);
6108
6109 return op;
6110 }
6111
6112 /* Parse an expression.
6113
6114 expression:
6115 assignment-expression
6116 expression , assignment-expression
6117
6118 CAST_P is true if this expression is the target of a cast.
6119
6120 Returns a representation of the expression. */
6121
6122 static tree
6123 cp_parser_expression (cp_parser* parser, bool cast_p)
6124 {
6125 tree expression = NULL_TREE;
6126
6127 while (true)
6128 {
6129 tree assignment_expression;
6130
6131 /* Parse the next assignment-expression. */
6132 assignment_expression
6133 = cp_parser_assignment_expression (parser, cast_p);
6134 /* If this is the first assignment-expression, we can just
6135 save it away. */
6136 if (!expression)
6137 expression = assignment_expression;
6138 else
6139 expression = build_x_compound_expr (expression,
6140 assignment_expression);
6141 /* If the next token is not a comma, then we are done with the
6142 expression. */
6143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6144 break;
6145 /* Consume the `,'. */
6146 cp_lexer_consume_token (parser->lexer);
6147 /* A comma operator cannot appear in a constant-expression. */
6148 if (cp_parser_non_integral_constant_expression (parser,
6149 "a comma operator"))
6150 expression = error_mark_node;
6151 }
6152
6153 return expression;
6154 }
6155
6156 /* Parse a constant-expression.
6157
6158 constant-expression:
6159 conditional-expression
6160
6161 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6162 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6163 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6164 is false, NON_CONSTANT_P should be NULL. */
6165
6166 static tree
6167 cp_parser_constant_expression (cp_parser* parser,
6168 bool allow_non_constant_p,
6169 bool *non_constant_p)
6170 {
6171 bool saved_integral_constant_expression_p;
6172 bool saved_allow_non_integral_constant_expression_p;
6173 bool saved_non_integral_constant_expression_p;
6174 tree expression;
6175
6176 /* It might seem that we could simply parse the
6177 conditional-expression, and then check to see if it were
6178 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6179 one that the compiler can figure out is constant, possibly after
6180 doing some simplifications or optimizations. The standard has a
6181 precise definition of constant-expression, and we must honor
6182 that, even though it is somewhat more restrictive.
6183
6184 For example:
6185
6186 int i[(2, 3)];
6187
6188 is not a legal declaration, because `(2, 3)' is not a
6189 constant-expression. The `,' operator is forbidden in a
6190 constant-expression. However, GCC's constant-folding machinery
6191 will fold this operation to an INTEGER_CST for `3'. */
6192
6193 /* Save the old settings. */
6194 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6195 saved_allow_non_integral_constant_expression_p
6196 = parser->allow_non_integral_constant_expression_p;
6197 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6198 /* We are now parsing a constant-expression. */
6199 parser->integral_constant_expression_p = true;
6200 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6201 parser->non_integral_constant_expression_p = false;
6202 /* Although the grammar says "conditional-expression", we parse an
6203 "assignment-expression", which also permits "throw-expression"
6204 and the use of assignment operators. In the case that
6205 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6206 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6207 actually essential that we look for an assignment-expression.
6208 For example, cp_parser_initializer_clauses uses this function to
6209 determine whether a particular assignment-expression is in fact
6210 constant. */
6211 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6212 /* Restore the old settings. */
6213 parser->integral_constant_expression_p
6214 = saved_integral_constant_expression_p;
6215 parser->allow_non_integral_constant_expression_p
6216 = saved_allow_non_integral_constant_expression_p;
6217 if (allow_non_constant_p)
6218 *non_constant_p = parser->non_integral_constant_expression_p;
6219 else if (parser->non_integral_constant_expression_p)
6220 expression = error_mark_node;
6221 parser->non_integral_constant_expression_p
6222 = saved_non_integral_constant_expression_p;
6223
6224 return expression;
6225 }
6226
6227 /* Parse __builtin_offsetof.
6228
6229 offsetof-expression:
6230 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6231
6232 offsetof-member-designator:
6233 id-expression
6234 | offsetof-member-designator "." id-expression
6235 | offsetof-member-designator "[" expression "]" */
6236
6237 static tree
6238 cp_parser_builtin_offsetof (cp_parser *parser)
6239 {
6240 int save_ice_p, save_non_ice_p;
6241 tree type, expr;
6242 cp_id_kind dummy;
6243
6244 /* We're about to accept non-integral-constant things, but will
6245 definitely yield an integral constant expression. Save and
6246 restore these values around our local parsing. */
6247 save_ice_p = parser->integral_constant_expression_p;
6248 save_non_ice_p = parser->non_integral_constant_expression_p;
6249
6250 /* Consume the "__builtin_offsetof" token. */
6251 cp_lexer_consume_token (parser->lexer);
6252 /* Consume the opening `('. */
6253 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6254 /* Parse the type-id. */
6255 type = cp_parser_type_id (parser);
6256 /* Look for the `,'. */
6257 cp_parser_require (parser, CPP_COMMA, "`,'");
6258
6259 /* Build the (type *)null that begins the traditional offsetof macro. */
6260 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6261
6262 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6263 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6264 true, &dummy);
6265 while (true)
6266 {
6267 cp_token *token = cp_lexer_peek_token (parser->lexer);
6268 switch (token->type)
6269 {
6270 case CPP_OPEN_SQUARE:
6271 /* offsetof-member-designator "[" expression "]" */
6272 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6273 break;
6274
6275 case CPP_DOT:
6276 /* offsetof-member-designator "." identifier */
6277 cp_lexer_consume_token (parser->lexer);
6278 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6279 true, &dummy);
6280 break;
6281
6282 case CPP_CLOSE_PAREN:
6283 /* Consume the ")" token. */
6284 cp_lexer_consume_token (parser->lexer);
6285 goto success;
6286
6287 default:
6288 /* Error. We know the following require will fail, but
6289 that gives the proper error message. */
6290 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6291 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6292 expr = error_mark_node;
6293 goto failure;
6294 }
6295 }
6296
6297 success:
6298 /* If we're processing a template, we can't finish the semantics yet.
6299 Otherwise we can fold the entire expression now. */
6300 if (processing_template_decl)
6301 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6302 else
6303 expr = finish_offsetof (expr);
6304
6305 failure:
6306 parser->integral_constant_expression_p = save_ice_p;
6307 parser->non_integral_constant_expression_p = save_non_ice_p;
6308
6309 return expr;
6310 }
6311
6312 /* Statements [gram.stmt.stmt] */
6313
6314 /* Parse a statement.
6315
6316 statement:
6317 labeled-statement
6318 expression-statement
6319 compound-statement
6320 selection-statement
6321 iteration-statement
6322 jump-statement
6323 declaration-statement
6324 try-block
6325
6326 IN_COMPOUND is true when the statement is nested inside a
6327 cp_parser_compound_statement; this matters for certain pragmas.
6328
6329 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6330 is a (possibly labeled) if statement which is not enclosed in braces
6331 and has an else clause. This is used to implement -Wparentheses. */
6332
6333 static void
6334 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6335 bool in_compound, bool *if_p)
6336 {
6337 tree statement;
6338 cp_token *token;
6339 location_t statement_location;
6340
6341 restart:
6342 if (if_p != NULL)
6343 *if_p = false;
6344 /* There is no statement yet. */
6345 statement = NULL_TREE;
6346 /* Peek at the next token. */
6347 token = cp_lexer_peek_token (parser->lexer);
6348 /* Remember the location of the first token in the statement. */
6349 statement_location = token->location;
6350 /* If this is a keyword, then that will often determine what kind of
6351 statement we have. */
6352 if (token->type == CPP_KEYWORD)
6353 {
6354 enum rid keyword = token->keyword;
6355
6356 switch (keyword)
6357 {
6358 case RID_CASE:
6359 case RID_DEFAULT:
6360 /* Looks like a labeled-statement with a case label.
6361 Parse the label, and then use tail recursion to parse
6362 the statement. */
6363 cp_parser_label_for_labeled_statement (parser);
6364 goto restart;
6365
6366 case RID_IF:
6367 case RID_SWITCH:
6368 statement = cp_parser_selection_statement (parser, if_p);
6369 break;
6370
6371 case RID_WHILE:
6372 case RID_DO:
6373 case RID_FOR:
6374 statement = cp_parser_iteration_statement (parser);
6375 break;
6376
6377 case RID_BREAK:
6378 case RID_CONTINUE:
6379 case RID_RETURN:
6380 case RID_GOTO:
6381 statement = cp_parser_jump_statement (parser);
6382 break;
6383
6384 /* Objective-C++ exception-handling constructs. */
6385 case RID_AT_TRY:
6386 case RID_AT_CATCH:
6387 case RID_AT_FINALLY:
6388 case RID_AT_SYNCHRONIZED:
6389 case RID_AT_THROW:
6390 statement = cp_parser_objc_statement (parser);
6391 break;
6392
6393 case RID_TRY:
6394 statement = cp_parser_try_block (parser);
6395 break;
6396
6397 case RID_NAMESPACE:
6398 /* This must be a namespace alias definition. */
6399 cp_parser_declaration_statement (parser);
6400 return;
6401
6402 default:
6403 /* It might be a keyword like `int' that can start a
6404 declaration-statement. */
6405 break;
6406 }
6407 }
6408 else if (token->type == CPP_NAME)
6409 {
6410 /* If the next token is a `:', then we are looking at a
6411 labeled-statement. */
6412 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6413 if (token->type == CPP_COLON)
6414 {
6415 /* Looks like a labeled-statement with an ordinary label.
6416 Parse the label, and then use tail recursion to parse
6417 the statement. */
6418 cp_parser_label_for_labeled_statement (parser);
6419 goto restart;
6420 }
6421 }
6422 /* Anything that starts with a `{' must be a compound-statement. */
6423 else if (token->type == CPP_OPEN_BRACE)
6424 statement = cp_parser_compound_statement (parser, NULL, false);
6425 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6426 a statement all its own. */
6427 else if (token->type == CPP_PRAGMA)
6428 {
6429 /* Only certain OpenMP pragmas are attached to statements, and thus
6430 are considered statements themselves. All others are not. In
6431 the context of a compound, accept the pragma as a "statement" and
6432 return so that we can check for a close brace. Otherwise we
6433 require a real statement and must go back and read one. */
6434 if (in_compound)
6435 cp_parser_pragma (parser, pragma_compound);
6436 else if (!cp_parser_pragma (parser, pragma_stmt))
6437 goto restart;
6438 return;
6439 }
6440 else if (token->type == CPP_EOF)
6441 {
6442 cp_parser_error (parser, "expected statement");
6443 return;
6444 }
6445
6446 /* Everything else must be a declaration-statement or an
6447 expression-statement. Try for the declaration-statement
6448 first, unless we are looking at a `;', in which case we know that
6449 we have an expression-statement. */
6450 if (!statement)
6451 {
6452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6453 {
6454 cp_parser_parse_tentatively (parser);
6455 /* Try to parse the declaration-statement. */
6456 cp_parser_declaration_statement (parser);
6457 /* If that worked, we're done. */
6458 if (cp_parser_parse_definitely (parser))
6459 return;
6460 }
6461 /* Look for an expression-statement instead. */
6462 statement = cp_parser_expression_statement (parser, in_statement_expr);
6463 }
6464
6465 /* Set the line number for the statement. */
6466 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6467 SET_EXPR_LOCATION (statement, statement_location);
6468 }
6469
6470 /* Parse the label for a labeled-statement, i.e.
6471
6472 identifier :
6473 case constant-expression :
6474 default :
6475
6476 GNU Extension:
6477 case constant-expression ... constant-expression : statement
6478
6479 When a label is parsed without errors, the label is added to the
6480 parse tree by the finish_* functions, so this function doesn't
6481 have to return the label. */
6482
6483 static void
6484 cp_parser_label_for_labeled_statement (cp_parser* parser)
6485 {
6486 cp_token *token;
6487
6488 /* The next token should be an identifier. */
6489 token = cp_lexer_peek_token (parser->lexer);
6490 if (token->type != CPP_NAME
6491 && token->type != CPP_KEYWORD)
6492 {
6493 cp_parser_error (parser, "expected labeled-statement");
6494 return;
6495 }
6496
6497 switch (token->keyword)
6498 {
6499 case RID_CASE:
6500 {
6501 tree expr, expr_hi;
6502 cp_token *ellipsis;
6503
6504 /* Consume the `case' token. */
6505 cp_lexer_consume_token (parser->lexer);
6506 /* Parse the constant-expression. */
6507 expr = cp_parser_constant_expression (parser,
6508 /*allow_non_constant_p=*/false,
6509 NULL);
6510
6511 ellipsis = cp_lexer_peek_token (parser->lexer);
6512 if (ellipsis->type == CPP_ELLIPSIS)
6513 {
6514 /* Consume the `...' token. */
6515 cp_lexer_consume_token (parser->lexer);
6516 expr_hi =
6517 cp_parser_constant_expression (parser,
6518 /*allow_non_constant_p=*/false,
6519 NULL);
6520 /* We don't need to emit warnings here, as the common code
6521 will do this for us. */
6522 }
6523 else
6524 expr_hi = NULL_TREE;
6525
6526 if (parser->in_switch_statement_p)
6527 finish_case_label (expr, expr_hi);
6528 else
6529 error ("case label %qE not within a switch statement", expr);
6530 }
6531 break;
6532
6533 case RID_DEFAULT:
6534 /* Consume the `default' token. */
6535 cp_lexer_consume_token (parser->lexer);
6536
6537 if (parser->in_switch_statement_p)
6538 finish_case_label (NULL_TREE, NULL_TREE);
6539 else
6540 error ("case label not within a switch statement");
6541 break;
6542
6543 default:
6544 /* Anything else must be an ordinary label. */
6545 finish_label_stmt (cp_parser_identifier (parser));
6546 break;
6547 }
6548
6549 /* Require the `:' token. */
6550 cp_parser_require (parser, CPP_COLON, "`:'");
6551 }
6552
6553 /* Parse an expression-statement.
6554
6555 expression-statement:
6556 expression [opt] ;
6557
6558 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6559 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6560 indicates whether this expression-statement is part of an
6561 expression statement. */
6562
6563 static tree
6564 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6565 {
6566 tree statement = NULL_TREE;
6567
6568 /* If the next token is a ';', then there is no expression
6569 statement. */
6570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6571 statement = cp_parser_expression (parser, /*cast_p=*/false);
6572
6573 /* Consume the final `;'. */
6574 cp_parser_consume_semicolon_at_end_of_statement (parser);
6575
6576 if (in_statement_expr
6577 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6578 /* This is the final expression statement of a statement
6579 expression. */
6580 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6581 else if (statement)
6582 statement = finish_expr_stmt (statement);
6583 else
6584 finish_stmt ();
6585
6586 return statement;
6587 }
6588
6589 /* Parse a compound-statement.
6590
6591 compound-statement:
6592 { statement-seq [opt] }
6593
6594 Returns a tree representing the statement. */
6595
6596 static tree
6597 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6598 bool in_try)
6599 {
6600 tree compound_stmt;
6601
6602 /* Consume the `{'. */
6603 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6604 return error_mark_node;
6605 /* Begin the compound-statement. */
6606 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6607 /* Parse an (optional) statement-seq. */
6608 cp_parser_statement_seq_opt (parser, in_statement_expr);
6609 /* Finish the compound-statement. */
6610 finish_compound_stmt (compound_stmt);
6611 /* Consume the `}'. */
6612 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6613
6614 return compound_stmt;
6615 }
6616
6617 /* Parse an (optional) statement-seq.
6618
6619 statement-seq:
6620 statement
6621 statement-seq [opt] statement */
6622
6623 static void
6624 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6625 {
6626 /* Scan statements until there aren't any more. */
6627 while (true)
6628 {
6629 cp_token *token = cp_lexer_peek_token (parser->lexer);
6630
6631 /* If we're looking at a `}', then we've run out of statements. */
6632 if (token->type == CPP_CLOSE_BRACE
6633 || token->type == CPP_EOF
6634 || token->type == CPP_PRAGMA_EOL)
6635 break;
6636
6637 /* If we are in a compound statement and find 'else' then
6638 something went wrong. */
6639 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6640 {
6641 if (parser->in_statement & IN_IF_STMT)
6642 break;
6643 else
6644 {
6645 token = cp_lexer_consume_token (parser->lexer);
6646 error ("%<else%> without a previous %<if%>");
6647 }
6648 }
6649
6650 /* Parse the statement. */
6651 cp_parser_statement (parser, in_statement_expr, true, NULL);
6652 }
6653 }
6654
6655 /* Parse a selection-statement.
6656
6657 selection-statement:
6658 if ( condition ) statement
6659 if ( condition ) statement else statement
6660 switch ( condition ) statement
6661
6662 Returns the new IF_STMT or SWITCH_STMT.
6663
6664 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6665 is a (possibly labeled) if statement which is not enclosed in
6666 braces and has an else clause. This is used to implement
6667 -Wparentheses. */
6668
6669 static tree
6670 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6671 {
6672 cp_token *token;
6673 enum rid keyword;
6674
6675 if (if_p != NULL)
6676 *if_p = false;
6677
6678 /* Peek at the next token. */
6679 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6680
6681 /* See what kind of keyword it is. */
6682 keyword = token->keyword;
6683 switch (keyword)
6684 {
6685 case RID_IF:
6686 case RID_SWITCH:
6687 {
6688 tree statement;
6689 tree condition;
6690
6691 /* Look for the `('. */
6692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6693 {
6694 cp_parser_skip_to_end_of_statement (parser);
6695 return error_mark_node;
6696 }
6697
6698 /* Begin the selection-statement. */
6699 if (keyword == RID_IF)
6700 statement = begin_if_stmt ();
6701 else
6702 statement = begin_switch_stmt ();
6703
6704 /* Parse the condition. */
6705 condition = cp_parser_condition (parser);
6706 /* Look for the `)'. */
6707 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6708 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6709 /*consume_paren=*/true);
6710
6711 if (keyword == RID_IF)
6712 {
6713 bool nested_if;
6714 unsigned char in_statement;
6715
6716 /* Add the condition. */
6717 finish_if_stmt_cond (condition, statement);
6718
6719 /* Parse the then-clause. */
6720 in_statement = parser->in_statement;
6721 parser->in_statement |= IN_IF_STMT;
6722 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6723 parser->in_statement = in_statement;
6724
6725 finish_then_clause (statement);
6726
6727 /* If the next token is `else', parse the else-clause. */
6728 if (cp_lexer_next_token_is_keyword (parser->lexer,
6729 RID_ELSE))
6730 {
6731 /* Consume the `else' keyword. */
6732 cp_lexer_consume_token (parser->lexer);
6733 begin_else_clause (statement);
6734 /* Parse the else-clause. */
6735 cp_parser_implicitly_scoped_statement (parser, NULL);
6736 finish_else_clause (statement);
6737
6738 /* If we are currently parsing a then-clause, then
6739 IF_P will not be NULL. We set it to true to
6740 indicate that this if statement has an else clause.
6741 This may trigger the Wparentheses warning below
6742 when we get back up to the parent if statement. */
6743 if (if_p != NULL)
6744 *if_p = true;
6745 }
6746 else
6747 {
6748 /* This if statement does not have an else clause. If
6749 NESTED_IF is true, then the then-clause is an if
6750 statement which does have an else clause. We warn
6751 about the potential ambiguity. */
6752 if (nested_if)
6753 warning (OPT_Wparentheses,
6754 ("%Hsuggest explicit braces "
6755 "to avoid ambiguous %<else%>"),
6756 EXPR_LOCUS (statement));
6757 }
6758
6759 /* Now we're all done with the if-statement. */
6760 finish_if_stmt (statement);
6761 }
6762 else
6763 {
6764 bool in_switch_statement_p;
6765 unsigned char in_statement;
6766
6767 /* Add the condition. */
6768 finish_switch_cond (condition, statement);
6769
6770 /* Parse the body of the switch-statement. */
6771 in_switch_statement_p = parser->in_switch_statement_p;
6772 in_statement = parser->in_statement;
6773 parser->in_switch_statement_p = true;
6774 parser->in_statement |= IN_SWITCH_STMT;
6775 cp_parser_implicitly_scoped_statement (parser, NULL);
6776 parser->in_switch_statement_p = in_switch_statement_p;
6777 parser->in_statement = in_statement;
6778
6779 /* Now we're all done with the switch-statement. */
6780 finish_switch_stmt (statement);
6781 }
6782
6783 return statement;
6784 }
6785 break;
6786
6787 default:
6788 cp_parser_error (parser, "expected selection-statement");
6789 return error_mark_node;
6790 }
6791 }
6792
6793 /* Parse a condition.
6794
6795 condition:
6796 expression
6797 type-specifier-seq declarator = assignment-expression
6798
6799 GNU Extension:
6800
6801 condition:
6802 type-specifier-seq declarator asm-specification [opt]
6803 attributes [opt] = assignment-expression
6804
6805 Returns the expression that should be tested. */
6806
6807 static tree
6808 cp_parser_condition (cp_parser* parser)
6809 {
6810 cp_decl_specifier_seq type_specifiers;
6811 const char *saved_message;
6812
6813 /* Try the declaration first. */
6814 cp_parser_parse_tentatively (parser);
6815 /* New types are not allowed in the type-specifier-seq for a
6816 condition. */
6817 saved_message = parser->type_definition_forbidden_message;
6818 parser->type_definition_forbidden_message
6819 = "types may not be defined in conditions";
6820 /* Parse the type-specifier-seq. */
6821 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6822 &type_specifiers);
6823 /* Restore the saved message. */
6824 parser->type_definition_forbidden_message = saved_message;
6825 /* If all is well, we might be looking at a declaration. */
6826 if (!cp_parser_error_occurred (parser))
6827 {
6828 tree decl;
6829 tree asm_specification;
6830 tree attributes;
6831 cp_declarator *declarator;
6832 tree initializer = NULL_TREE;
6833
6834 /* Parse the declarator. */
6835 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6836 /*ctor_dtor_or_conv_p=*/NULL,
6837 /*parenthesized_p=*/NULL,
6838 /*member_p=*/false);
6839 /* Parse the attributes. */
6840 attributes = cp_parser_attributes_opt (parser);
6841 /* Parse the asm-specification. */
6842 asm_specification = cp_parser_asm_specification_opt (parser);
6843 /* If the next token is not an `=', then we might still be
6844 looking at an expression. For example:
6845
6846 if (A(a).x)
6847
6848 looks like a decl-specifier-seq and a declarator -- but then
6849 there is no `=', so this is an expression. */
6850 cp_parser_require (parser, CPP_EQ, "`='");
6851 /* If we did see an `=', then we are looking at a declaration
6852 for sure. */
6853 if (cp_parser_parse_definitely (parser))
6854 {
6855 tree pushed_scope;
6856 bool non_constant_p;
6857
6858 /* Create the declaration. */
6859 decl = start_decl (declarator, &type_specifiers,
6860 /*initialized_p=*/true,
6861 attributes, /*prefix_attributes=*/NULL_TREE,
6862 &pushed_scope);
6863 /* Parse the assignment-expression. */
6864 initializer
6865 = cp_parser_constant_expression (parser,
6866 /*allow_non_constant_p=*/true,
6867 &non_constant_p);
6868 if (!non_constant_p)
6869 initializer = fold_non_dependent_expr (initializer);
6870
6871 /* Process the initializer. */
6872 cp_finish_decl (decl,
6873 initializer, !non_constant_p,
6874 asm_specification,
6875 LOOKUP_ONLYCONVERTING);
6876
6877 if (pushed_scope)
6878 pop_scope (pushed_scope);
6879
6880 return convert_from_reference (decl);
6881 }
6882 }
6883 /* If we didn't even get past the declarator successfully, we are
6884 definitely not looking at a declaration. */
6885 else
6886 cp_parser_abort_tentative_parse (parser);
6887
6888 /* Otherwise, we are looking at an expression. */
6889 return cp_parser_expression (parser, /*cast_p=*/false);
6890 }
6891
6892 /* Parse an iteration-statement.
6893
6894 iteration-statement:
6895 while ( condition ) statement
6896 do statement while ( expression ) ;
6897 for ( for-init-statement condition [opt] ; expression [opt] )
6898 statement
6899
6900 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6901
6902 static tree
6903 cp_parser_iteration_statement (cp_parser* parser)
6904 {
6905 cp_token *token;
6906 enum rid keyword;
6907 tree statement;
6908 unsigned char in_statement;
6909
6910 /* Peek at the next token. */
6911 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6912 if (!token)
6913 return error_mark_node;
6914
6915 /* Remember whether or not we are already within an iteration
6916 statement. */
6917 in_statement = parser->in_statement;
6918
6919 /* See what kind of keyword it is. */
6920 keyword = token->keyword;
6921 switch (keyword)
6922 {
6923 case RID_WHILE:
6924 {
6925 tree condition;
6926
6927 /* Begin the while-statement. */
6928 statement = begin_while_stmt ();
6929 /* Look for the `('. */
6930 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6931 /* Parse the condition. */
6932 condition = cp_parser_condition (parser);
6933 finish_while_stmt_cond (condition, statement);
6934 /* Look for the `)'. */
6935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6936 /* Parse the dependent statement. */
6937 parser->in_statement = IN_ITERATION_STMT;
6938 cp_parser_already_scoped_statement (parser);
6939 parser->in_statement = in_statement;
6940 /* We're done with the while-statement. */
6941 finish_while_stmt (statement);
6942 }
6943 break;
6944
6945 case RID_DO:
6946 {
6947 tree expression;
6948
6949 /* Begin the do-statement. */
6950 statement = begin_do_stmt ();
6951 /* Parse the body of the do-statement. */
6952 parser->in_statement = IN_ITERATION_STMT;
6953 cp_parser_implicitly_scoped_statement (parser, NULL);
6954 parser->in_statement = in_statement;
6955 finish_do_body (statement);
6956 /* Look for the `while' keyword. */
6957 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6958 /* Look for the `('. */
6959 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6960 /* Parse the expression. */
6961 expression = cp_parser_expression (parser, /*cast_p=*/false);
6962 /* We're done with the do-statement. */
6963 finish_do_stmt (expression, statement);
6964 /* Look for the `)'. */
6965 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6966 /* Look for the `;'. */
6967 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6968 }
6969 break;
6970
6971 case RID_FOR:
6972 {
6973 tree condition = NULL_TREE;
6974 tree expression = NULL_TREE;
6975
6976 /* Begin the for-statement. */
6977 statement = begin_for_stmt ();
6978 /* Look for the `('. */
6979 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6980 /* Parse the initialization. */
6981 cp_parser_for_init_statement (parser);
6982 finish_for_init_stmt (statement);
6983
6984 /* If there's a condition, process it. */
6985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6986 condition = cp_parser_condition (parser);
6987 finish_for_cond (condition, statement);
6988 /* Look for the `;'. */
6989 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6990
6991 /* If there's an expression, process it. */
6992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6993 expression = cp_parser_expression (parser, /*cast_p=*/false);
6994 finish_for_expr (expression, statement);
6995 /* Look for the `)'. */
6996 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6997
6998 /* Parse the body of the for-statement. */
6999 parser->in_statement = IN_ITERATION_STMT;
7000 cp_parser_already_scoped_statement (parser);
7001 parser->in_statement = in_statement;
7002
7003 /* We're done with the for-statement. */
7004 finish_for_stmt (statement);
7005 }
7006 break;
7007
7008 default:
7009 cp_parser_error (parser, "expected iteration-statement");
7010 statement = error_mark_node;
7011 break;
7012 }
7013
7014 return statement;
7015 }
7016
7017 /* Parse a for-init-statement.
7018
7019 for-init-statement:
7020 expression-statement
7021 simple-declaration */
7022
7023 static void
7024 cp_parser_for_init_statement (cp_parser* parser)
7025 {
7026 /* If the next token is a `;', then we have an empty
7027 expression-statement. Grammatically, this is also a
7028 simple-declaration, but an invalid one, because it does not
7029 declare anything. Therefore, if we did not handle this case
7030 specially, we would issue an error message about an invalid
7031 declaration. */
7032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7033 {
7034 /* We're going to speculatively look for a declaration, falling back
7035 to an expression, if necessary. */
7036 cp_parser_parse_tentatively (parser);
7037 /* Parse the declaration. */
7038 cp_parser_simple_declaration (parser,
7039 /*function_definition_allowed_p=*/false);
7040 /* If the tentative parse failed, then we shall need to look for an
7041 expression-statement. */
7042 if (cp_parser_parse_definitely (parser))
7043 return;
7044 }
7045
7046 cp_parser_expression_statement (parser, false);
7047 }
7048
7049 /* Parse a jump-statement.
7050
7051 jump-statement:
7052 break ;
7053 continue ;
7054 return expression [opt] ;
7055 goto identifier ;
7056
7057 GNU extension:
7058
7059 jump-statement:
7060 goto * expression ;
7061
7062 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7063
7064 static tree
7065 cp_parser_jump_statement (cp_parser* parser)
7066 {
7067 tree statement = error_mark_node;
7068 cp_token *token;
7069 enum rid keyword;
7070 unsigned char in_statement;
7071
7072 /* Peek at the next token. */
7073 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7074 if (!token)
7075 return error_mark_node;
7076
7077 /* See what kind of keyword it is. */
7078 keyword = token->keyword;
7079 switch (keyword)
7080 {
7081 case RID_BREAK:
7082 in_statement = parser->in_statement & ~IN_IF_STMT;
7083 switch (in_statement)
7084 {
7085 case 0:
7086 error ("break statement not within loop or switch");
7087 break;
7088 default:
7089 gcc_assert ((in_statement & IN_SWITCH_STMT)
7090 || in_statement == IN_ITERATION_STMT);
7091 statement = finish_break_stmt ();
7092 break;
7093 case IN_OMP_BLOCK:
7094 error ("invalid exit from OpenMP structured block");
7095 break;
7096 case IN_OMP_FOR:
7097 error ("break statement used with OpenMP for loop");
7098 break;
7099 }
7100 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7101 break;
7102
7103 case RID_CONTINUE:
7104 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7105 {
7106 case 0:
7107 error ("continue statement not within a loop");
7108 break;
7109 case IN_ITERATION_STMT:
7110 case IN_OMP_FOR:
7111 statement = finish_continue_stmt ();
7112 break;
7113 case IN_OMP_BLOCK:
7114 error ("invalid exit from OpenMP structured block");
7115 break;
7116 default:
7117 gcc_unreachable ();
7118 }
7119 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7120 break;
7121
7122 case RID_RETURN:
7123 {
7124 tree expr;
7125
7126 /* If the next token is a `;', then there is no
7127 expression. */
7128 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7129 expr = cp_parser_expression (parser, /*cast_p=*/false);
7130 else
7131 expr = NULL_TREE;
7132 /* Build the return-statement. */
7133 statement = finish_return_stmt (expr);
7134 /* Look for the final `;'. */
7135 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7136 }
7137 break;
7138
7139 case RID_GOTO:
7140 /* Create the goto-statement. */
7141 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7142 {
7143 /* Issue a warning about this use of a GNU extension. */
7144 if (pedantic)
7145 pedwarn ("ISO C++ forbids computed gotos");
7146 /* Consume the '*' token. */
7147 cp_lexer_consume_token (parser->lexer);
7148 /* Parse the dependent expression. */
7149 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7150 }
7151 else
7152 finish_goto_stmt (cp_parser_identifier (parser));
7153 /* Look for the final `;'. */
7154 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7155 break;
7156
7157 default:
7158 cp_parser_error (parser, "expected jump-statement");
7159 break;
7160 }
7161
7162 return statement;
7163 }
7164
7165 /* Parse a declaration-statement.
7166
7167 declaration-statement:
7168 block-declaration */
7169
7170 static void
7171 cp_parser_declaration_statement (cp_parser* parser)
7172 {
7173 void *p;
7174
7175 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7176 p = obstack_alloc (&declarator_obstack, 0);
7177
7178 /* Parse the block-declaration. */
7179 cp_parser_block_declaration (parser, /*statement_p=*/true);
7180
7181 /* Free any declarators allocated. */
7182 obstack_free (&declarator_obstack, p);
7183
7184 /* Finish off the statement. */
7185 finish_stmt ();
7186 }
7187
7188 /* Some dependent statements (like `if (cond) statement'), are
7189 implicitly in their own scope. In other words, if the statement is
7190 a single statement (as opposed to a compound-statement), it is
7191 none-the-less treated as if it were enclosed in braces. Any
7192 declarations appearing in the dependent statement are out of scope
7193 after control passes that point. This function parses a statement,
7194 but ensures that is in its own scope, even if it is not a
7195 compound-statement.
7196
7197 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7198 is a (possibly labeled) if statement which is not enclosed in
7199 braces and has an else clause. This is used to implement
7200 -Wparentheses.
7201
7202 Returns the new statement. */
7203
7204 static tree
7205 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7206 {
7207 tree statement;
7208
7209 if (if_p != NULL)
7210 *if_p = false;
7211
7212 /* Mark if () ; with a special NOP_EXPR. */
7213 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7214 {
7215 cp_lexer_consume_token (parser->lexer);
7216 statement = add_stmt (build_empty_stmt ());
7217 }
7218 /* if a compound is opened, we simply parse the statement directly. */
7219 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7220 statement = cp_parser_compound_statement (parser, NULL, false);
7221 /* If the token is not a `{', then we must take special action. */
7222 else
7223 {
7224 /* Create a compound-statement. */
7225 statement = begin_compound_stmt (0);
7226 /* Parse the dependent-statement. */
7227 cp_parser_statement (parser, NULL_TREE, false, if_p);
7228 /* Finish the dummy compound-statement. */
7229 finish_compound_stmt (statement);
7230 }
7231
7232 /* Return the statement. */
7233 return statement;
7234 }
7235
7236 /* For some dependent statements (like `while (cond) statement'), we
7237 have already created a scope. Therefore, even if the dependent
7238 statement is a compound-statement, we do not want to create another
7239 scope. */
7240
7241 static void
7242 cp_parser_already_scoped_statement (cp_parser* parser)
7243 {
7244 /* If the token is a `{', then we must take special action. */
7245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7246 cp_parser_statement (parser, NULL_TREE, false, NULL);
7247 else
7248 {
7249 /* Avoid calling cp_parser_compound_statement, so that we
7250 don't create a new scope. Do everything else by hand. */
7251 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7252 cp_parser_statement_seq_opt (parser, NULL_TREE);
7253 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7254 }
7255 }
7256
7257 /* Declarations [gram.dcl.dcl] */
7258
7259 /* Parse an optional declaration-sequence.
7260
7261 declaration-seq:
7262 declaration
7263 declaration-seq declaration */
7264
7265 static void
7266 cp_parser_declaration_seq_opt (cp_parser* parser)
7267 {
7268 while (true)
7269 {
7270 cp_token *token;
7271
7272 token = cp_lexer_peek_token (parser->lexer);
7273
7274 if (token->type == CPP_CLOSE_BRACE
7275 || token->type == CPP_EOF
7276 || token->type == CPP_PRAGMA_EOL)
7277 break;
7278
7279 if (token->type == CPP_SEMICOLON)
7280 {
7281 /* A declaration consisting of a single semicolon is
7282 invalid. Allow it unless we're being pedantic. */
7283 cp_lexer_consume_token (parser->lexer);
7284 if (pedantic && !in_system_header)
7285 pedwarn ("extra %<;%>");
7286 continue;
7287 }
7288
7289 /* If we're entering or exiting a region that's implicitly
7290 extern "C", modify the lang context appropriately. */
7291 if (!parser->implicit_extern_c && token->implicit_extern_c)
7292 {
7293 push_lang_context (lang_name_c);
7294 parser->implicit_extern_c = true;
7295 }
7296 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7297 {
7298 pop_lang_context ();
7299 parser->implicit_extern_c = false;
7300 }
7301
7302 if (token->type == CPP_PRAGMA)
7303 {
7304 /* A top-level declaration can consist solely of a #pragma.
7305 A nested declaration cannot, so this is done here and not
7306 in cp_parser_declaration. (A #pragma at block scope is
7307 handled in cp_parser_statement.) */
7308 cp_parser_pragma (parser, pragma_external);
7309 continue;
7310 }
7311
7312 /* Parse the declaration itself. */
7313 cp_parser_declaration (parser);
7314 }
7315 }
7316
7317 /* Parse a declaration.
7318
7319 declaration:
7320 block-declaration
7321 function-definition
7322 template-declaration
7323 explicit-instantiation
7324 explicit-specialization
7325 linkage-specification
7326 namespace-definition
7327
7328 GNU extension:
7329
7330 declaration:
7331 __extension__ declaration */
7332
7333 static void
7334 cp_parser_declaration (cp_parser* parser)
7335 {
7336 cp_token token1;
7337 cp_token token2;
7338 int saved_pedantic;
7339 void *p;
7340
7341 /* Check for the `__extension__' keyword. */
7342 if (cp_parser_extension_opt (parser, &saved_pedantic))
7343 {
7344 /* Parse the qualified declaration. */
7345 cp_parser_declaration (parser);
7346 /* Restore the PEDANTIC flag. */
7347 pedantic = saved_pedantic;
7348
7349 return;
7350 }
7351
7352 /* Try to figure out what kind of declaration is present. */
7353 token1 = *cp_lexer_peek_token (parser->lexer);
7354
7355 if (token1.type != CPP_EOF)
7356 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7357 else
7358 {
7359 token2.type = CPP_EOF;
7360 token2.keyword = RID_MAX;
7361 }
7362
7363 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7364 p = obstack_alloc (&declarator_obstack, 0);
7365
7366 /* If the next token is `extern' and the following token is a string
7367 literal, then we have a linkage specification. */
7368 if (token1.keyword == RID_EXTERN
7369 && cp_parser_is_string_literal (&token2))
7370 cp_parser_linkage_specification (parser);
7371 /* If the next token is `template', then we have either a template
7372 declaration, an explicit instantiation, or an explicit
7373 specialization. */
7374 else if (token1.keyword == RID_TEMPLATE)
7375 {
7376 /* `template <>' indicates a template specialization. */
7377 if (token2.type == CPP_LESS
7378 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7379 cp_parser_explicit_specialization (parser);
7380 /* `template <' indicates a template declaration. */
7381 else if (token2.type == CPP_LESS)
7382 cp_parser_template_declaration (parser, /*member_p=*/false);
7383 /* Anything else must be an explicit instantiation. */
7384 else
7385 cp_parser_explicit_instantiation (parser);
7386 }
7387 /* If the next token is `export', then we have a template
7388 declaration. */
7389 else if (token1.keyword == RID_EXPORT)
7390 cp_parser_template_declaration (parser, /*member_p=*/false);
7391 /* If the next token is `extern', 'static' or 'inline' and the one
7392 after that is `template', we have a GNU extended explicit
7393 instantiation directive. */
7394 else if (cp_parser_allow_gnu_extensions_p (parser)
7395 && (token1.keyword == RID_EXTERN
7396 || token1.keyword == RID_STATIC
7397 || token1.keyword == RID_INLINE)
7398 && token2.keyword == RID_TEMPLATE)
7399 cp_parser_explicit_instantiation (parser);
7400 /* If the next token is `namespace', check for a named or unnamed
7401 namespace definition. */
7402 else if (token1.keyword == RID_NAMESPACE
7403 && (/* A named namespace definition. */
7404 (token2.type == CPP_NAME
7405 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7406 != CPP_EQ))
7407 /* An unnamed namespace definition. */
7408 || token2.type == CPP_OPEN_BRACE
7409 || token2.keyword == RID_ATTRIBUTE))
7410 cp_parser_namespace_definition (parser);
7411 /* Objective-C++ declaration/definition. */
7412 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7413 cp_parser_objc_declaration (parser);
7414 /* We must have either a block declaration or a function
7415 definition. */
7416 else
7417 /* Try to parse a block-declaration, or a function-definition. */
7418 cp_parser_block_declaration (parser, /*statement_p=*/false);
7419
7420 /* Free any declarators allocated. */
7421 obstack_free (&declarator_obstack, p);
7422 }
7423
7424 /* Parse a block-declaration.
7425
7426 block-declaration:
7427 simple-declaration
7428 asm-definition
7429 namespace-alias-definition
7430 using-declaration
7431 using-directive
7432
7433 GNU Extension:
7434
7435 block-declaration:
7436 __extension__ block-declaration
7437 label-declaration
7438
7439 C++0x Extension:
7440
7441 block-declaration:
7442 static_assert-declaration
7443
7444 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7445 part of a declaration-statement. */
7446
7447 static void
7448 cp_parser_block_declaration (cp_parser *parser,
7449 bool statement_p)
7450 {
7451 cp_token *token1;
7452 int saved_pedantic;
7453
7454 /* Check for the `__extension__' keyword. */
7455 if (cp_parser_extension_opt (parser, &saved_pedantic))
7456 {
7457 /* Parse the qualified declaration. */
7458 cp_parser_block_declaration (parser, statement_p);
7459 /* Restore the PEDANTIC flag. */
7460 pedantic = saved_pedantic;
7461
7462 return;
7463 }
7464
7465 /* Peek at the next token to figure out which kind of declaration is
7466 present. */
7467 token1 = cp_lexer_peek_token (parser->lexer);
7468
7469 /* If the next keyword is `asm', we have an asm-definition. */
7470 if (token1->keyword == RID_ASM)
7471 {
7472 if (statement_p)
7473 cp_parser_commit_to_tentative_parse (parser);
7474 cp_parser_asm_definition (parser);
7475 }
7476 /* If the next keyword is `namespace', we have a
7477 namespace-alias-definition. */
7478 else if (token1->keyword == RID_NAMESPACE)
7479 cp_parser_namespace_alias_definition (parser);
7480 /* If the next keyword is `using', we have either a
7481 using-declaration or a using-directive. */
7482 else if (token1->keyword == RID_USING)
7483 {
7484 cp_token *token2;
7485
7486 if (statement_p)
7487 cp_parser_commit_to_tentative_parse (parser);
7488 /* If the token after `using' is `namespace', then we have a
7489 using-directive. */
7490 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7491 if (token2->keyword == RID_NAMESPACE)
7492 cp_parser_using_directive (parser);
7493 /* Otherwise, it's a using-declaration. */
7494 else
7495 cp_parser_using_declaration (parser,
7496 /*access_declaration_p=*/false);
7497 }
7498 /* If the next keyword is `__label__' we have a label declaration. */
7499 else if (token1->keyword == RID_LABEL)
7500 {
7501 if (statement_p)
7502 cp_parser_commit_to_tentative_parse (parser);
7503 cp_parser_label_declaration (parser);
7504 }
7505 /* If the next token is `static_assert' we have a static assertion. */
7506 else if (token1->keyword == RID_STATIC_ASSERT)
7507 cp_parser_static_assert (parser, /*member_p=*/false);
7508 /* Anything else must be a simple-declaration. */
7509 else
7510 cp_parser_simple_declaration (parser, !statement_p);
7511 }
7512
7513 /* Parse a simple-declaration.
7514
7515 simple-declaration:
7516 decl-specifier-seq [opt] init-declarator-list [opt] ;
7517
7518 init-declarator-list:
7519 init-declarator
7520 init-declarator-list , init-declarator
7521
7522 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7523 function-definition as a simple-declaration. */
7524
7525 static void
7526 cp_parser_simple_declaration (cp_parser* parser,
7527 bool function_definition_allowed_p)
7528 {
7529 cp_decl_specifier_seq decl_specifiers;
7530 int declares_class_or_enum;
7531 bool saw_declarator;
7532
7533 /* Defer access checks until we know what is being declared; the
7534 checks for names appearing in the decl-specifier-seq should be
7535 done as if we were in the scope of the thing being declared. */
7536 push_deferring_access_checks (dk_deferred);
7537
7538 /* Parse the decl-specifier-seq. We have to keep track of whether
7539 or not the decl-specifier-seq declares a named class or
7540 enumeration type, since that is the only case in which the
7541 init-declarator-list is allowed to be empty.
7542
7543 [dcl.dcl]
7544
7545 In a simple-declaration, the optional init-declarator-list can be
7546 omitted only when declaring a class or enumeration, that is when
7547 the decl-specifier-seq contains either a class-specifier, an
7548 elaborated-type-specifier, or an enum-specifier. */
7549 cp_parser_decl_specifier_seq (parser,
7550 CP_PARSER_FLAGS_OPTIONAL,
7551 &decl_specifiers,
7552 &declares_class_or_enum);
7553 /* We no longer need to defer access checks. */
7554 stop_deferring_access_checks ();
7555
7556 /* In a block scope, a valid declaration must always have a
7557 decl-specifier-seq. By not trying to parse declarators, we can
7558 resolve the declaration/expression ambiguity more quickly. */
7559 if (!function_definition_allowed_p
7560 && !decl_specifiers.any_specifiers_p)
7561 {
7562 cp_parser_error (parser, "expected declaration");
7563 goto done;
7564 }
7565
7566 /* If the next two tokens are both identifiers, the code is
7567 erroneous. The usual cause of this situation is code like:
7568
7569 T t;
7570
7571 where "T" should name a type -- but does not. */
7572 if (!decl_specifiers.type
7573 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7574 {
7575 /* If parsing tentatively, we should commit; we really are
7576 looking at a declaration. */
7577 cp_parser_commit_to_tentative_parse (parser);
7578 /* Give up. */
7579 goto done;
7580 }
7581
7582 /* If we have seen at least one decl-specifier, and the next token
7583 is not a parenthesis, then we must be looking at a declaration.
7584 (After "int (" we might be looking at a functional cast.) */
7585 if (decl_specifiers.any_specifiers_p
7586 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7587 cp_parser_commit_to_tentative_parse (parser);
7588
7589 /* Keep going until we hit the `;' at the end of the simple
7590 declaration. */
7591 saw_declarator = false;
7592 while (cp_lexer_next_token_is_not (parser->lexer,
7593 CPP_SEMICOLON))
7594 {
7595 cp_token *token;
7596 bool function_definition_p;
7597 tree decl;
7598
7599 if (saw_declarator)
7600 {
7601 /* If we are processing next declarator, coma is expected */
7602 token = cp_lexer_peek_token (parser->lexer);
7603 gcc_assert (token->type == CPP_COMMA);
7604 cp_lexer_consume_token (parser->lexer);
7605 }
7606 else
7607 saw_declarator = true;
7608
7609 /* Parse the init-declarator. */
7610 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7611 /*checks=*/NULL,
7612 function_definition_allowed_p,
7613 /*member_p=*/false,
7614 declares_class_or_enum,
7615 &function_definition_p);
7616 /* If an error occurred while parsing tentatively, exit quickly.
7617 (That usually happens when in the body of a function; each
7618 statement is treated as a declaration-statement until proven
7619 otherwise.) */
7620 if (cp_parser_error_occurred (parser))
7621 goto done;
7622 /* Handle function definitions specially. */
7623 if (function_definition_p)
7624 {
7625 /* If the next token is a `,', then we are probably
7626 processing something like:
7627
7628 void f() {}, *p;
7629
7630 which is erroneous. */
7631 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7632 error ("mixing declarations and function-definitions is forbidden");
7633 /* Otherwise, we're done with the list of declarators. */
7634 else
7635 {
7636 pop_deferring_access_checks ();
7637 return;
7638 }
7639 }
7640 /* The next token should be either a `,' or a `;'. */
7641 token = cp_lexer_peek_token (parser->lexer);
7642 /* If it's a `,', there are more declarators to come. */
7643 if (token->type == CPP_COMMA)
7644 /* will be consumed next time around */;
7645 /* If it's a `;', we are done. */
7646 else if (token->type == CPP_SEMICOLON)
7647 break;
7648 /* Anything else is an error. */
7649 else
7650 {
7651 /* If we have already issued an error message we don't need
7652 to issue another one. */
7653 if (decl != error_mark_node
7654 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7655 cp_parser_error (parser, "expected %<,%> or %<;%>");
7656 /* Skip tokens until we reach the end of the statement. */
7657 cp_parser_skip_to_end_of_statement (parser);
7658 /* If the next token is now a `;', consume it. */
7659 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7660 cp_lexer_consume_token (parser->lexer);
7661 goto done;
7662 }
7663 /* After the first time around, a function-definition is not
7664 allowed -- even if it was OK at first. For example:
7665
7666 int i, f() {}
7667
7668 is not valid. */
7669 function_definition_allowed_p = false;
7670 }
7671
7672 /* Issue an error message if no declarators are present, and the
7673 decl-specifier-seq does not itself declare a class or
7674 enumeration. */
7675 if (!saw_declarator)
7676 {
7677 if (cp_parser_declares_only_class_p (parser))
7678 shadow_tag (&decl_specifiers);
7679 /* Perform any deferred access checks. */
7680 perform_deferred_access_checks ();
7681 }
7682
7683 /* Consume the `;'. */
7684 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7685
7686 done:
7687 pop_deferring_access_checks ();
7688 }
7689
7690 /* Parse a decl-specifier-seq.
7691
7692 decl-specifier-seq:
7693 decl-specifier-seq [opt] decl-specifier
7694
7695 decl-specifier:
7696 storage-class-specifier
7697 type-specifier
7698 function-specifier
7699 friend
7700 typedef
7701
7702 GNU Extension:
7703
7704 decl-specifier:
7705 attributes
7706
7707 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7708
7709 The parser flags FLAGS is used to control type-specifier parsing.
7710
7711 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7712 flags:
7713
7714 1: one of the decl-specifiers is an elaborated-type-specifier
7715 (i.e., a type declaration)
7716 2: one of the decl-specifiers is an enum-specifier or a
7717 class-specifier (i.e., a type definition)
7718
7719 */
7720
7721 static void
7722 cp_parser_decl_specifier_seq (cp_parser* parser,
7723 cp_parser_flags flags,
7724 cp_decl_specifier_seq *decl_specs,
7725 int* declares_class_or_enum)
7726 {
7727 bool constructor_possible_p = !parser->in_declarator_p;
7728
7729 /* Clear DECL_SPECS. */
7730 clear_decl_specs (decl_specs);
7731
7732 /* Assume no class or enumeration type is declared. */
7733 *declares_class_or_enum = 0;
7734
7735 /* Keep reading specifiers until there are no more to read. */
7736 while (true)
7737 {
7738 bool constructor_p;
7739 bool found_decl_spec;
7740 cp_token *token;
7741
7742 /* Peek at the next token. */
7743 token = cp_lexer_peek_token (parser->lexer);
7744 /* Handle attributes. */
7745 if (token->keyword == RID_ATTRIBUTE)
7746 {
7747 /* Parse the attributes. */
7748 decl_specs->attributes
7749 = chainon (decl_specs->attributes,
7750 cp_parser_attributes_opt (parser));
7751 continue;
7752 }
7753 /* Assume we will find a decl-specifier keyword. */
7754 found_decl_spec = true;
7755 /* If the next token is an appropriate keyword, we can simply
7756 add it to the list. */
7757 switch (token->keyword)
7758 {
7759 /* decl-specifier:
7760 friend */
7761 case RID_FRIEND:
7762 if (!at_class_scope_p ())
7763 {
7764 error ("%<friend%> used outside of class");
7765 cp_lexer_purge_token (parser->lexer);
7766 }
7767 else
7768 {
7769 ++decl_specs->specs[(int) ds_friend];
7770 /* Consume the token. */
7771 cp_lexer_consume_token (parser->lexer);
7772 }
7773 break;
7774
7775 /* function-specifier:
7776 inline
7777 virtual
7778 explicit */
7779 case RID_INLINE:
7780 case RID_VIRTUAL:
7781 case RID_EXPLICIT:
7782 cp_parser_function_specifier_opt (parser, decl_specs);
7783 break;
7784
7785 /* decl-specifier:
7786 typedef */
7787 case RID_TYPEDEF:
7788 ++decl_specs->specs[(int) ds_typedef];
7789 /* Consume the token. */
7790 cp_lexer_consume_token (parser->lexer);
7791 /* A constructor declarator cannot appear in a typedef. */
7792 constructor_possible_p = false;
7793 /* The "typedef" keyword can only occur in a declaration; we
7794 may as well commit at this point. */
7795 cp_parser_commit_to_tentative_parse (parser);
7796
7797 if (decl_specs->storage_class != sc_none)
7798 decl_specs->conflicting_specifiers_p = true;
7799 break;
7800
7801 /* storage-class-specifier:
7802 auto
7803 register
7804 static
7805 extern
7806 mutable
7807
7808 GNU Extension:
7809 thread */
7810 case RID_AUTO:
7811 case RID_REGISTER:
7812 case RID_STATIC:
7813 case RID_EXTERN:
7814 case RID_MUTABLE:
7815 /* Consume the token. */
7816 cp_lexer_consume_token (parser->lexer);
7817 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7818 break;
7819 case RID_THREAD:
7820 /* Consume the token. */
7821 cp_lexer_consume_token (parser->lexer);
7822 ++decl_specs->specs[(int) ds_thread];
7823 break;
7824
7825 default:
7826 /* We did not yet find a decl-specifier yet. */
7827 found_decl_spec = false;
7828 break;
7829 }
7830
7831 /* Constructors are a special case. The `S' in `S()' is not a
7832 decl-specifier; it is the beginning of the declarator. */
7833 constructor_p
7834 = (!found_decl_spec
7835 && constructor_possible_p
7836 && (cp_parser_constructor_declarator_p
7837 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7838
7839 /* If we don't have a DECL_SPEC yet, then we must be looking at
7840 a type-specifier. */
7841 if (!found_decl_spec && !constructor_p)
7842 {
7843 int decl_spec_declares_class_or_enum;
7844 bool is_cv_qualifier;
7845 tree type_spec;
7846
7847 type_spec
7848 = cp_parser_type_specifier (parser, flags,
7849 decl_specs,
7850 /*is_declaration=*/true,
7851 &decl_spec_declares_class_or_enum,
7852 &is_cv_qualifier);
7853
7854 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7855
7856 /* If this type-specifier referenced a user-defined type
7857 (a typedef, class-name, etc.), then we can't allow any
7858 more such type-specifiers henceforth.
7859
7860 [dcl.spec]
7861
7862 The longest sequence of decl-specifiers that could
7863 possibly be a type name is taken as the
7864 decl-specifier-seq of a declaration. The sequence shall
7865 be self-consistent as described below.
7866
7867 [dcl.type]
7868
7869 As a general rule, at most one type-specifier is allowed
7870 in the complete decl-specifier-seq of a declaration. The
7871 only exceptions are the following:
7872
7873 -- const or volatile can be combined with any other
7874 type-specifier.
7875
7876 -- signed or unsigned can be combined with char, long,
7877 short, or int.
7878
7879 -- ..
7880
7881 Example:
7882
7883 typedef char* Pc;
7884 void g (const int Pc);
7885
7886 Here, Pc is *not* part of the decl-specifier seq; it's
7887 the declarator. Therefore, once we see a type-specifier
7888 (other than a cv-qualifier), we forbid any additional
7889 user-defined types. We *do* still allow things like `int
7890 int' to be considered a decl-specifier-seq, and issue the
7891 error message later. */
7892 if (type_spec && !is_cv_qualifier)
7893 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7894 /* A constructor declarator cannot follow a type-specifier. */
7895 if (type_spec)
7896 {
7897 constructor_possible_p = false;
7898 found_decl_spec = true;
7899 }
7900 }
7901
7902 /* If we still do not have a DECL_SPEC, then there are no more
7903 decl-specifiers. */
7904 if (!found_decl_spec)
7905 break;
7906
7907 decl_specs->any_specifiers_p = true;
7908 /* After we see one decl-specifier, further decl-specifiers are
7909 always optional. */
7910 flags |= CP_PARSER_FLAGS_OPTIONAL;
7911 }
7912
7913 cp_parser_check_decl_spec (decl_specs);
7914
7915 /* Don't allow a friend specifier with a class definition. */
7916 if (decl_specs->specs[(int) ds_friend] != 0
7917 && (*declares_class_or_enum & 2))
7918 error ("class definition may not be declared a friend");
7919 }
7920
7921 /* Parse an (optional) storage-class-specifier.
7922
7923 storage-class-specifier:
7924 auto
7925 register
7926 static
7927 extern
7928 mutable
7929
7930 GNU Extension:
7931
7932 storage-class-specifier:
7933 thread
7934
7935 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7936
7937 static tree
7938 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7939 {
7940 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7941 {
7942 case RID_AUTO:
7943 case RID_REGISTER:
7944 case RID_STATIC:
7945 case RID_EXTERN:
7946 case RID_MUTABLE:
7947 case RID_THREAD:
7948 /* Consume the token. */
7949 return cp_lexer_consume_token (parser->lexer)->u.value;
7950
7951 default:
7952 return NULL_TREE;
7953 }
7954 }
7955
7956 /* Parse an (optional) function-specifier.
7957
7958 function-specifier:
7959 inline
7960 virtual
7961 explicit
7962
7963 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7964 Updates DECL_SPECS, if it is non-NULL. */
7965
7966 static tree
7967 cp_parser_function_specifier_opt (cp_parser* parser,
7968 cp_decl_specifier_seq *decl_specs)
7969 {
7970 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7971 {
7972 case RID_INLINE:
7973 if (decl_specs)
7974 ++decl_specs->specs[(int) ds_inline];
7975 break;
7976
7977 case RID_VIRTUAL:
7978 /* 14.5.2.3 [temp.mem]
7979
7980 A member function template shall not be virtual. */
7981 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7982 error ("templates may not be %<virtual%>");
7983 else if (decl_specs)
7984 ++decl_specs->specs[(int) ds_virtual];
7985 break;
7986
7987 case RID_EXPLICIT:
7988 if (decl_specs)
7989 ++decl_specs->specs[(int) ds_explicit];
7990 break;
7991
7992 default:
7993 return NULL_TREE;
7994 }
7995
7996 /* Consume the token. */
7997 return cp_lexer_consume_token (parser->lexer)->u.value;
7998 }
7999
8000 /* Parse a linkage-specification.
8001
8002 linkage-specification:
8003 extern string-literal { declaration-seq [opt] }
8004 extern string-literal declaration */
8005
8006 static void
8007 cp_parser_linkage_specification (cp_parser* parser)
8008 {
8009 tree linkage;
8010
8011 /* Look for the `extern' keyword. */
8012 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8013
8014 /* Look for the string-literal. */
8015 linkage = cp_parser_string_literal (parser, false, false);
8016
8017 /* Transform the literal into an identifier. If the literal is a
8018 wide-character string, or contains embedded NULs, then we can't
8019 handle it as the user wants. */
8020 if (strlen (TREE_STRING_POINTER (linkage))
8021 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8022 {
8023 cp_parser_error (parser, "invalid linkage-specification");
8024 /* Assume C++ linkage. */
8025 linkage = lang_name_cplusplus;
8026 }
8027 else
8028 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8029
8030 /* We're now using the new linkage. */
8031 push_lang_context (linkage);
8032
8033 /* If the next token is a `{', then we're using the first
8034 production. */
8035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8036 {
8037 /* Consume the `{' token. */
8038 cp_lexer_consume_token (parser->lexer);
8039 /* Parse the declarations. */
8040 cp_parser_declaration_seq_opt (parser);
8041 /* Look for the closing `}'. */
8042 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8043 }
8044 /* Otherwise, there's just one declaration. */
8045 else
8046 {
8047 bool saved_in_unbraced_linkage_specification_p;
8048
8049 saved_in_unbraced_linkage_specification_p
8050 = parser->in_unbraced_linkage_specification_p;
8051 parser->in_unbraced_linkage_specification_p = true;
8052 cp_parser_declaration (parser);
8053 parser->in_unbraced_linkage_specification_p
8054 = saved_in_unbraced_linkage_specification_p;
8055 }
8056
8057 /* We're done with the linkage-specification. */
8058 pop_lang_context ();
8059 }
8060
8061 /* Parse a static_assert-declaration.
8062
8063 static_assert-declaration:
8064 static_assert ( constant-expression , string-literal ) ;
8065
8066 If MEMBER_P, this static_assert is a class member. */
8067
8068 static void
8069 cp_parser_static_assert(cp_parser *parser, bool member_p)
8070 {
8071 tree condition;
8072 tree message;
8073 cp_token *token;
8074 location_t saved_loc;
8075
8076 /* Peek at the `static_assert' token so we can keep track of exactly
8077 where the static assertion started. */
8078 token = cp_lexer_peek_token (parser->lexer);
8079 saved_loc = token->location;
8080
8081 /* Look for the `static_assert' keyword. */
8082 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8083 "`static_assert'"))
8084 return;
8085
8086 /* We know we are in a static assertion; commit to any tentative
8087 parse. */
8088 if (cp_parser_parsing_tentatively (parser))
8089 cp_parser_commit_to_tentative_parse (parser);
8090
8091 /* Parse the `(' starting the static assertion condition. */
8092 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8093
8094 /* Parse the constant-expression. */
8095 condition =
8096 cp_parser_constant_expression (parser,
8097 /*allow_non_constant_p=*/false,
8098 /*non_constant_p=*/NULL);
8099
8100 /* Parse the separating `,'. */
8101 cp_parser_require (parser, CPP_COMMA, "`,'");
8102
8103 /* Parse the string-literal message. */
8104 message = cp_parser_string_literal (parser,
8105 /*translate=*/false,
8106 /*wide_ok=*/true);
8107
8108 /* A `)' completes the static assertion. */
8109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8110 cp_parser_skip_to_closing_parenthesis (parser,
8111 /*recovering=*/true,
8112 /*or_comma=*/false,
8113 /*consume_paren=*/true);
8114
8115 /* A semicolon terminates the declaration. */
8116 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8117
8118 /* Complete the static assertion, which may mean either processing
8119 the static assert now or saving it for template instantiation. */
8120 finish_static_assert (condition, message, saved_loc, member_p);
8121 }
8122
8123 /* Special member functions [gram.special] */
8124
8125 /* Parse a conversion-function-id.
8126
8127 conversion-function-id:
8128 operator conversion-type-id
8129
8130 Returns an IDENTIFIER_NODE representing the operator. */
8131
8132 static tree
8133 cp_parser_conversion_function_id (cp_parser* parser)
8134 {
8135 tree type;
8136 tree saved_scope;
8137 tree saved_qualifying_scope;
8138 tree saved_object_scope;
8139 tree pushed_scope = NULL_TREE;
8140
8141 /* Look for the `operator' token. */
8142 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8143 return error_mark_node;
8144 /* When we parse the conversion-type-id, the current scope will be
8145 reset. However, we need that information in able to look up the
8146 conversion function later, so we save it here. */
8147 saved_scope = parser->scope;
8148 saved_qualifying_scope = parser->qualifying_scope;
8149 saved_object_scope = parser->object_scope;
8150 /* We must enter the scope of the class so that the names of
8151 entities declared within the class are available in the
8152 conversion-type-id. For example, consider:
8153
8154 struct S {
8155 typedef int I;
8156 operator I();
8157 };
8158
8159 S::operator I() { ... }
8160
8161 In order to see that `I' is a type-name in the definition, we
8162 must be in the scope of `S'. */
8163 if (saved_scope)
8164 pushed_scope = push_scope (saved_scope);
8165 /* Parse the conversion-type-id. */
8166 type = cp_parser_conversion_type_id (parser);
8167 /* Leave the scope of the class, if any. */
8168 if (pushed_scope)
8169 pop_scope (pushed_scope);
8170 /* Restore the saved scope. */
8171 parser->scope = saved_scope;
8172 parser->qualifying_scope = saved_qualifying_scope;
8173 parser->object_scope = saved_object_scope;
8174 /* If the TYPE is invalid, indicate failure. */
8175 if (type == error_mark_node)
8176 return error_mark_node;
8177 return mangle_conv_op_name_for_type (type);
8178 }
8179
8180 /* Parse a conversion-type-id:
8181
8182 conversion-type-id:
8183 type-specifier-seq conversion-declarator [opt]
8184
8185 Returns the TYPE specified. */
8186
8187 static tree
8188 cp_parser_conversion_type_id (cp_parser* parser)
8189 {
8190 tree attributes;
8191 cp_decl_specifier_seq type_specifiers;
8192 cp_declarator *declarator;
8193 tree type_specified;
8194
8195 /* Parse the attributes. */
8196 attributes = cp_parser_attributes_opt (parser);
8197 /* Parse the type-specifiers. */
8198 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8199 &type_specifiers);
8200 /* If that didn't work, stop. */
8201 if (type_specifiers.type == error_mark_node)
8202 return error_mark_node;
8203 /* Parse the conversion-declarator. */
8204 declarator = cp_parser_conversion_declarator_opt (parser);
8205
8206 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8207 /*initialized=*/0, &attributes);
8208 if (attributes)
8209 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8210 return type_specified;
8211 }
8212
8213 /* Parse an (optional) conversion-declarator.
8214
8215 conversion-declarator:
8216 ptr-operator conversion-declarator [opt]
8217
8218 */
8219
8220 static cp_declarator *
8221 cp_parser_conversion_declarator_opt (cp_parser* parser)
8222 {
8223 enum tree_code code;
8224 tree class_type;
8225 cp_cv_quals cv_quals;
8226
8227 /* We don't know if there's a ptr-operator next, or not. */
8228 cp_parser_parse_tentatively (parser);
8229 /* Try the ptr-operator. */
8230 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8231 /* If it worked, look for more conversion-declarators. */
8232 if (cp_parser_parse_definitely (parser))
8233 {
8234 cp_declarator *declarator;
8235
8236 /* Parse another optional declarator. */
8237 declarator = cp_parser_conversion_declarator_opt (parser);
8238
8239 /* Create the representation of the declarator. */
8240 if (class_type)
8241 declarator = make_ptrmem_declarator (cv_quals, class_type,
8242 declarator);
8243 else if (code == INDIRECT_REF)
8244 declarator = make_pointer_declarator (cv_quals, declarator);
8245 else
8246 declarator = make_reference_declarator (cv_quals, declarator);
8247
8248 return declarator;
8249 }
8250
8251 return NULL;
8252 }
8253
8254 /* Parse an (optional) ctor-initializer.
8255
8256 ctor-initializer:
8257 : mem-initializer-list
8258
8259 Returns TRUE iff the ctor-initializer was actually present. */
8260
8261 static bool
8262 cp_parser_ctor_initializer_opt (cp_parser* parser)
8263 {
8264 /* If the next token is not a `:', then there is no
8265 ctor-initializer. */
8266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8267 {
8268 /* Do default initialization of any bases and members. */
8269 if (DECL_CONSTRUCTOR_P (current_function_decl))
8270 finish_mem_initializers (NULL_TREE);
8271
8272 return false;
8273 }
8274
8275 /* Consume the `:' token. */
8276 cp_lexer_consume_token (parser->lexer);
8277 /* And the mem-initializer-list. */
8278 cp_parser_mem_initializer_list (parser);
8279
8280 return true;
8281 }
8282
8283 /* Parse a mem-initializer-list.
8284
8285 mem-initializer-list:
8286 mem-initializer ... [opt]
8287 mem-initializer ... [opt] , mem-initializer-list */
8288
8289 static void
8290 cp_parser_mem_initializer_list (cp_parser* parser)
8291 {
8292 tree mem_initializer_list = NULL_TREE;
8293
8294 /* Let the semantic analysis code know that we are starting the
8295 mem-initializer-list. */
8296 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8297 error ("only constructors take base initializers");
8298
8299 /* Loop through the list. */
8300 while (true)
8301 {
8302 tree mem_initializer;
8303
8304 /* Parse the mem-initializer. */
8305 mem_initializer = cp_parser_mem_initializer (parser);
8306 /* If the next token is a `...', we're expanding member initializers. */
8307 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8308 {
8309 /* Consume the `...'. */
8310 cp_lexer_consume_token (parser->lexer);
8311
8312 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8313 can be expanded but members cannot. */
8314 if (mem_initializer != error_mark_node
8315 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8316 {
8317 error ("cannot expand initializer for member %<%D%>",
8318 TREE_PURPOSE (mem_initializer));
8319 mem_initializer = error_mark_node;
8320 }
8321
8322 /* Construct the pack expansion type. */
8323 if (mem_initializer != error_mark_node)
8324 mem_initializer = make_pack_expansion (mem_initializer);
8325 }
8326 /* Add it to the list, unless it was erroneous. */
8327 if (mem_initializer != error_mark_node)
8328 {
8329 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8330 mem_initializer_list = mem_initializer;
8331 }
8332 /* If the next token is not a `,', we're done. */
8333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8334 break;
8335 /* Consume the `,' token. */
8336 cp_lexer_consume_token (parser->lexer);
8337 }
8338
8339 /* Perform semantic analysis. */
8340 if (DECL_CONSTRUCTOR_P (current_function_decl))
8341 finish_mem_initializers (mem_initializer_list);
8342 }
8343
8344 /* Parse a mem-initializer.
8345
8346 mem-initializer:
8347 mem-initializer-id ( expression-list [opt] )
8348
8349 GNU extension:
8350
8351 mem-initializer:
8352 ( expression-list [opt] )
8353
8354 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8355 class) or FIELD_DECL (for a non-static data member) to initialize;
8356 the TREE_VALUE is the expression-list. An empty initialization
8357 list is represented by void_list_node. */
8358
8359 static tree
8360 cp_parser_mem_initializer (cp_parser* parser)
8361 {
8362 tree mem_initializer_id;
8363 tree expression_list;
8364 tree member;
8365
8366 /* Find out what is being initialized. */
8367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8368 {
8369 pedwarn ("anachronistic old-style base class initializer");
8370 mem_initializer_id = NULL_TREE;
8371 }
8372 else
8373 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8374 member = expand_member_init (mem_initializer_id);
8375 if (member && !DECL_P (member))
8376 in_base_initializer = 1;
8377
8378 expression_list
8379 = cp_parser_parenthesized_expression_list (parser, false,
8380 /*cast_p=*/false,
8381 /*allow_expansion_p=*/true,
8382 /*non_constant_p=*/NULL);
8383 if (expression_list == error_mark_node)
8384 return error_mark_node;
8385 if (!expression_list)
8386 expression_list = void_type_node;
8387
8388 in_base_initializer = 0;
8389
8390 return member ? build_tree_list (member, expression_list) : error_mark_node;
8391 }
8392
8393 /* Parse a mem-initializer-id.
8394
8395 mem-initializer-id:
8396 :: [opt] nested-name-specifier [opt] class-name
8397 identifier
8398
8399 Returns a TYPE indicating the class to be initializer for the first
8400 production. Returns an IDENTIFIER_NODE indicating the data member
8401 to be initialized for the second production. */
8402
8403 static tree
8404 cp_parser_mem_initializer_id (cp_parser* parser)
8405 {
8406 bool global_scope_p;
8407 bool nested_name_specifier_p;
8408 bool template_p = false;
8409 tree id;
8410
8411 /* `typename' is not allowed in this context ([temp.res]). */
8412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8413 {
8414 error ("keyword %<typename%> not allowed in this context (a qualified "
8415 "member initializer is implicitly a type)");
8416 cp_lexer_consume_token (parser->lexer);
8417 }
8418 /* Look for the optional `::' operator. */
8419 global_scope_p
8420 = (cp_parser_global_scope_opt (parser,
8421 /*current_scope_valid_p=*/false)
8422 != NULL_TREE);
8423 /* Look for the optional nested-name-specifier. The simplest way to
8424 implement:
8425
8426 [temp.res]
8427
8428 The keyword `typename' is not permitted in a base-specifier or
8429 mem-initializer; in these contexts a qualified name that
8430 depends on a template-parameter is implicitly assumed to be a
8431 type name.
8432
8433 is to assume that we have seen the `typename' keyword at this
8434 point. */
8435 nested_name_specifier_p
8436 = (cp_parser_nested_name_specifier_opt (parser,
8437 /*typename_keyword_p=*/true,
8438 /*check_dependency_p=*/true,
8439 /*type_p=*/true,
8440 /*is_declaration=*/true)
8441 != NULL_TREE);
8442 if (nested_name_specifier_p)
8443 template_p = cp_parser_optional_template_keyword (parser);
8444 /* If there is a `::' operator or a nested-name-specifier, then we
8445 are definitely looking for a class-name. */
8446 if (global_scope_p || nested_name_specifier_p)
8447 return cp_parser_class_name (parser,
8448 /*typename_keyword_p=*/true,
8449 /*template_keyword_p=*/template_p,
8450 none_type,
8451 /*check_dependency_p=*/true,
8452 /*class_head_p=*/false,
8453 /*is_declaration=*/true);
8454 /* Otherwise, we could also be looking for an ordinary identifier. */
8455 cp_parser_parse_tentatively (parser);
8456 /* Try a class-name. */
8457 id = cp_parser_class_name (parser,
8458 /*typename_keyword_p=*/true,
8459 /*template_keyword_p=*/false,
8460 none_type,
8461 /*check_dependency_p=*/true,
8462 /*class_head_p=*/false,
8463 /*is_declaration=*/true);
8464 /* If we found one, we're done. */
8465 if (cp_parser_parse_definitely (parser))
8466 return id;
8467 /* Otherwise, look for an ordinary identifier. */
8468 return cp_parser_identifier (parser);
8469 }
8470
8471 /* Overloading [gram.over] */
8472
8473 /* Parse an operator-function-id.
8474
8475 operator-function-id:
8476 operator operator
8477
8478 Returns an IDENTIFIER_NODE for the operator which is a
8479 human-readable spelling of the identifier, e.g., `operator +'. */
8480
8481 static tree
8482 cp_parser_operator_function_id (cp_parser* parser)
8483 {
8484 /* Look for the `operator' keyword. */
8485 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8486 return error_mark_node;
8487 /* And then the name of the operator itself. */
8488 return cp_parser_operator (parser);
8489 }
8490
8491 /* Parse an operator.
8492
8493 operator:
8494 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8495 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8496 || ++ -- , ->* -> () []
8497
8498 GNU Extensions:
8499
8500 operator:
8501 <? >? <?= >?=
8502
8503 Returns an IDENTIFIER_NODE for the operator which is a
8504 human-readable spelling of the identifier, e.g., `operator +'. */
8505
8506 static tree
8507 cp_parser_operator (cp_parser* parser)
8508 {
8509 tree id = NULL_TREE;
8510 cp_token *token;
8511
8512 /* Peek at the next token. */
8513 token = cp_lexer_peek_token (parser->lexer);
8514 /* Figure out which operator we have. */
8515 switch (token->type)
8516 {
8517 case CPP_KEYWORD:
8518 {
8519 enum tree_code op;
8520
8521 /* The keyword should be either `new' or `delete'. */
8522 if (token->keyword == RID_NEW)
8523 op = NEW_EXPR;
8524 else if (token->keyword == RID_DELETE)
8525 op = DELETE_EXPR;
8526 else
8527 break;
8528
8529 /* Consume the `new' or `delete' token. */
8530 cp_lexer_consume_token (parser->lexer);
8531
8532 /* Peek at the next token. */
8533 token = cp_lexer_peek_token (parser->lexer);
8534 /* If it's a `[' token then this is the array variant of the
8535 operator. */
8536 if (token->type == CPP_OPEN_SQUARE)
8537 {
8538 /* Consume the `[' token. */
8539 cp_lexer_consume_token (parser->lexer);
8540 /* Look for the `]' token. */
8541 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8542 id = ansi_opname (op == NEW_EXPR
8543 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8544 }
8545 /* Otherwise, we have the non-array variant. */
8546 else
8547 id = ansi_opname (op);
8548
8549 return id;
8550 }
8551
8552 case CPP_PLUS:
8553 id = ansi_opname (PLUS_EXPR);
8554 break;
8555
8556 case CPP_MINUS:
8557 id = ansi_opname (MINUS_EXPR);
8558 break;
8559
8560 case CPP_MULT:
8561 id = ansi_opname (MULT_EXPR);
8562 break;
8563
8564 case CPP_DIV:
8565 id = ansi_opname (TRUNC_DIV_EXPR);
8566 break;
8567
8568 case CPP_MOD:
8569 id = ansi_opname (TRUNC_MOD_EXPR);
8570 break;
8571
8572 case CPP_XOR:
8573 id = ansi_opname (BIT_XOR_EXPR);
8574 break;
8575
8576 case CPP_AND:
8577 id = ansi_opname (BIT_AND_EXPR);
8578 break;
8579
8580 case CPP_OR:
8581 id = ansi_opname (BIT_IOR_EXPR);
8582 break;
8583
8584 case CPP_COMPL:
8585 id = ansi_opname (BIT_NOT_EXPR);
8586 break;
8587
8588 case CPP_NOT:
8589 id = ansi_opname (TRUTH_NOT_EXPR);
8590 break;
8591
8592 case CPP_EQ:
8593 id = ansi_assopname (NOP_EXPR);
8594 break;
8595
8596 case CPP_LESS:
8597 id = ansi_opname (LT_EXPR);
8598 break;
8599
8600 case CPP_GREATER:
8601 id = ansi_opname (GT_EXPR);
8602 break;
8603
8604 case CPP_PLUS_EQ:
8605 id = ansi_assopname (PLUS_EXPR);
8606 break;
8607
8608 case CPP_MINUS_EQ:
8609 id = ansi_assopname (MINUS_EXPR);
8610 break;
8611
8612 case CPP_MULT_EQ:
8613 id = ansi_assopname (MULT_EXPR);
8614 break;
8615
8616 case CPP_DIV_EQ:
8617 id = ansi_assopname (TRUNC_DIV_EXPR);
8618 break;
8619
8620 case CPP_MOD_EQ:
8621 id = ansi_assopname (TRUNC_MOD_EXPR);
8622 break;
8623
8624 case CPP_XOR_EQ:
8625 id = ansi_assopname (BIT_XOR_EXPR);
8626 break;
8627
8628 case CPP_AND_EQ:
8629 id = ansi_assopname (BIT_AND_EXPR);
8630 break;
8631
8632 case CPP_OR_EQ:
8633 id = ansi_assopname (BIT_IOR_EXPR);
8634 break;
8635
8636 case CPP_LSHIFT:
8637 id = ansi_opname (LSHIFT_EXPR);
8638 break;
8639
8640 case CPP_RSHIFT:
8641 id = ansi_opname (RSHIFT_EXPR);
8642 break;
8643
8644 case CPP_LSHIFT_EQ:
8645 id = ansi_assopname (LSHIFT_EXPR);
8646 break;
8647
8648 case CPP_RSHIFT_EQ:
8649 id = ansi_assopname (RSHIFT_EXPR);
8650 break;
8651
8652 case CPP_EQ_EQ:
8653 id = ansi_opname (EQ_EXPR);
8654 break;
8655
8656 case CPP_NOT_EQ:
8657 id = ansi_opname (NE_EXPR);
8658 break;
8659
8660 case CPP_LESS_EQ:
8661 id = ansi_opname (LE_EXPR);
8662 break;
8663
8664 case CPP_GREATER_EQ:
8665 id = ansi_opname (GE_EXPR);
8666 break;
8667
8668 case CPP_AND_AND:
8669 id = ansi_opname (TRUTH_ANDIF_EXPR);
8670 break;
8671
8672 case CPP_OR_OR:
8673 id = ansi_opname (TRUTH_ORIF_EXPR);
8674 break;
8675
8676 case CPP_PLUS_PLUS:
8677 id = ansi_opname (POSTINCREMENT_EXPR);
8678 break;
8679
8680 case CPP_MINUS_MINUS:
8681 id = ansi_opname (PREDECREMENT_EXPR);
8682 break;
8683
8684 case CPP_COMMA:
8685 id = ansi_opname (COMPOUND_EXPR);
8686 break;
8687
8688 case CPP_DEREF_STAR:
8689 id = ansi_opname (MEMBER_REF);
8690 break;
8691
8692 case CPP_DEREF:
8693 id = ansi_opname (COMPONENT_REF);
8694 break;
8695
8696 case CPP_OPEN_PAREN:
8697 /* Consume the `('. */
8698 cp_lexer_consume_token (parser->lexer);
8699 /* Look for the matching `)'. */
8700 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8701 return ansi_opname (CALL_EXPR);
8702
8703 case CPP_OPEN_SQUARE:
8704 /* Consume the `['. */
8705 cp_lexer_consume_token (parser->lexer);
8706 /* Look for the matching `]'. */
8707 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8708 return ansi_opname (ARRAY_REF);
8709
8710 default:
8711 /* Anything else is an error. */
8712 break;
8713 }
8714
8715 /* If we have selected an identifier, we need to consume the
8716 operator token. */
8717 if (id)
8718 cp_lexer_consume_token (parser->lexer);
8719 /* Otherwise, no valid operator name was present. */
8720 else
8721 {
8722 cp_parser_error (parser, "expected operator");
8723 id = error_mark_node;
8724 }
8725
8726 return id;
8727 }
8728
8729 /* Parse a template-declaration.
8730
8731 template-declaration:
8732 export [opt] template < template-parameter-list > declaration
8733
8734 If MEMBER_P is TRUE, this template-declaration occurs within a
8735 class-specifier.
8736
8737 The grammar rule given by the standard isn't correct. What
8738 is really meant is:
8739
8740 template-declaration:
8741 export [opt] template-parameter-list-seq
8742 decl-specifier-seq [opt] init-declarator [opt] ;
8743 export [opt] template-parameter-list-seq
8744 function-definition
8745
8746 template-parameter-list-seq:
8747 template-parameter-list-seq [opt]
8748 template < template-parameter-list > */
8749
8750 static void
8751 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8752 {
8753 /* Check for `export'. */
8754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8755 {
8756 /* Consume the `export' token. */
8757 cp_lexer_consume_token (parser->lexer);
8758 /* Warn that we do not support `export'. */
8759 warning (0, "keyword %<export%> not implemented, and will be ignored");
8760 }
8761
8762 cp_parser_template_declaration_after_export (parser, member_p);
8763 }
8764
8765 /* Parse a template-parameter-list.
8766
8767 template-parameter-list:
8768 template-parameter
8769 template-parameter-list , template-parameter
8770
8771 Returns a TREE_LIST. Each node represents a template parameter.
8772 The nodes are connected via their TREE_CHAINs. */
8773
8774 static tree
8775 cp_parser_template_parameter_list (cp_parser* parser)
8776 {
8777 tree parameter_list = NULL_TREE;
8778
8779 begin_template_parm_list ();
8780 while (true)
8781 {
8782 tree parameter;
8783 cp_token *token;
8784 bool is_non_type;
8785 bool is_parameter_pack;
8786
8787 /* Parse the template-parameter. */
8788 parameter = cp_parser_template_parameter (parser,
8789 &is_non_type,
8790 &is_parameter_pack);
8791 /* Add it to the list. */
8792 if (parameter != error_mark_node)
8793 parameter_list = process_template_parm (parameter_list,
8794 parameter,
8795 is_non_type,
8796 is_parameter_pack);
8797 else
8798 {
8799 tree err_parm = build_tree_list (parameter, parameter);
8800 TREE_VALUE (err_parm) = error_mark_node;
8801 parameter_list = chainon (parameter_list, err_parm);
8802 }
8803
8804 /* Peek at the next token. */
8805 token = cp_lexer_peek_token (parser->lexer);
8806 /* If it's not a `,', we're done. */
8807 if (token->type != CPP_COMMA)
8808 break;
8809 /* Otherwise, consume the `,' token. */
8810 cp_lexer_consume_token (parser->lexer);
8811 }
8812
8813 return end_template_parm_list (parameter_list);
8814 }
8815
8816 /* Parse a template-parameter.
8817
8818 template-parameter:
8819 type-parameter
8820 parameter-declaration
8821
8822 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8823 the parameter. The TREE_PURPOSE is the default value, if any.
8824 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8825 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
8826 set to true iff this parameter is a parameter pack. */
8827
8828 static tree
8829 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
8830 bool *is_parameter_pack)
8831 {
8832 cp_token *token;
8833 cp_parameter_declarator *parameter_declarator;
8834 tree parm;
8835
8836 /* Assume it is a type parameter or a template parameter. */
8837 *is_non_type = false;
8838 /* Assume it not a parameter pack. */
8839 *is_parameter_pack = false;
8840 /* Peek at the next token. */
8841 token = cp_lexer_peek_token (parser->lexer);
8842 /* If it is `class' or `template', we have a type-parameter. */
8843 if (token->keyword == RID_TEMPLATE)
8844 return cp_parser_type_parameter (parser, is_parameter_pack);
8845 /* If it is `class' or `typename' we do not know yet whether it is a
8846 type parameter or a non-type parameter. Consider:
8847
8848 template <typename T, typename T::X X> ...
8849
8850 or:
8851
8852 template <class C, class D*> ...
8853
8854 Here, the first parameter is a type parameter, and the second is
8855 a non-type parameter. We can tell by looking at the token after
8856 the identifier -- if it is a `,', `=', or `>' then we have a type
8857 parameter. */
8858 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8859 {
8860 /* Peek at the token after `class' or `typename'. */
8861 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8862 /* If it's an ellipsis, we have a template type parameter
8863 pack. */
8864 if (token->type == CPP_ELLIPSIS)
8865 return cp_parser_type_parameter (parser, is_parameter_pack);
8866 /* If it's an identifier, skip it. */
8867 if (token->type == CPP_NAME)
8868 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8869 /* Now, see if the token looks like the end of a template
8870 parameter. */
8871 if (token->type == CPP_COMMA
8872 || token->type == CPP_EQ
8873 || token->type == CPP_GREATER)
8874 return cp_parser_type_parameter (parser, is_parameter_pack);
8875 }
8876
8877 /* Otherwise, it is a non-type parameter.
8878
8879 [temp.param]
8880
8881 When parsing a default template-argument for a non-type
8882 template-parameter, the first non-nested `>' is taken as the end
8883 of the template parameter-list rather than a greater-than
8884 operator. */
8885 *is_non_type = true;
8886 parameter_declarator
8887 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8888 /*parenthesized_p=*/NULL);
8889
8890 /* If the parameter declaration is marked as a parameter pack, set
8891 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8892 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8893 grokdeclarator. */
8894 if (parameter_declarator
8895 && parameter_declarator->declarator
8896 && parameter_declarator->declarator->parameter_pack_p)
8897 {
8898 *is_parameter_pack = true;
8899 parameter_declarator->declarator->parameter_pack_p = false;
8900 }
8901
8902 /* If the next token is an ellipsis, and we don't already have it
8903 marked as a parameter pack, then we have a parameter pack (that
8904 has no declarator); */
8905 if (!*is_parameter_pack
8906 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8907 {
8908
8909 /* Consume the `...'. */
8910 cp_lexer_consume_token (parser->lexer);
8911 maybe_warn_variadic_templates ();
8912
8913 *is_parameter_pack = true;
8914 }
8915
8916 parm = grokdeclarator (parameter_declarator->declarator,
8917 &parameter_declarator->decl_specifiers,
8918 PARM, /*initialized=*/0,
8919 /*attrlist=*/NULL);
8920 if (parm == error_mark_node)
8921 return error_mark_node;
8922
8923 return build_tree_list (parameter_declarator->default_argument, parm);
8924 }
8925
8926 /* Parse a type-parameter.
8927
8928 type-parameter:
8929 class identifier [opt]
8930 class identifier [opt] = type-id
8931 typename identifier [opt]
8932 typename identifier [opt] = type-id
8933 template < template-parameter-list > class identifier [opt]
8934 template < template-parameter-list > class identifier [opt]
8935 = id-expression
8936
8937 GNU Extension (variadic templates):
8938
8939 type-parameter:
8940 class ... identifier [opt]
8941 typename ... identifier [opt]
8942
8943 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8944 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8945 the declaration of the parameter.
8946
8947 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8948
8949 static tree
8950 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
8951 {
8952 cp_token *token;
8953 tree parameter;
8954
8955 /* Look for a keyword to tell us what kind of parameter this is. */
8956 token = cp_parser_require (parser, CPP_KEYWORD,
8957 "`class', `typename', or `template'");
8958 if (!token)
8959 return error_mark_node;
8960
8961 switch (token->keyword)
8962 {
8963 case RID_CLASS:
8964 case RID_TYPENAME:
8965 {
8966 tree identifier;
8967 tree default_argument;
8968
8969 /* If the next token is an ellipsis, we have a template
8970 argument pack. */
8971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8972 {
8973 /* Consume the `...' token. */
8974 cp_lexer_consume_token (parser->lexer);
8975 maybe_warn_variadic_templates ();
8976
8977 *is_parameter_pack = true;
8978 }
8979
8980 /* If the next token is an identifier, then it names the
8981 parameter. */
8982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8983 identifier = cp_parser_identifier (parser);
8984 else
8985 identifier = NULL_TREE;
8986
8987 /* Create the parameter. */
8988 parameter = finish_template_type_parm (class_type_node, identifier);
8989
8990 /* If the next token is an `=', we have a default argument. */
8991 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8992 {
8993 /* Consume the `=' token. */
8994 cp_lexer_consume_token (parser->lexer);
8995 /* Parse the default-argument. */
8996 push_deferring_access_checks (dk_no_deferred);
8997 default_argument = cp_parser_type_id (parser);
8998
8999 /* Template parameter packs cannot have default
9000 arguments. */
9001 if (*is_parameter_pack)
9002 {
9003 if (identifier)
9004 error ("template parameter pack %qD cannot have a default argument",
9005 identifier);
9006 else
9007 error ("template parameter packs cannot have default arguments");
9008 default_argument = NULL_TREE;
9009 }
9010 pop_deferring_access_checks ();
9011 }
9012 else
9013 default_argument = NULL_TREE;
9014
9015 /* Create the combined representation of the parameter and the
9016 default argument. */
9017 parameter = build_tree_list (default_argument, parameter);
9018 }
9019 break;
9020
9021 case RID_TEMPLATE:
9022 {
9023 tree parameter_list;
9024 tree identifier;
9025 tree default_argument;
9026
9027 /* Look for the `<'. */
9028 cp_parser_require (parser, CPP_LESS, "`<'");
9029 /* Parse the template-parameter-list. */
9030 parameter_list = cp_parser_template_parameter_list (parser);
9031 /* Look for the `>'. */
9032 cp_parser_require (parser, CPP_GREATER, "`>'");
9033 /* Look for the `class' keyword. */
9034 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9035 /* If the next token is an ellipsis, we have a template
9036 argument pack. */
9037 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9038 {
9039 /* Consume the `...' token. */
9040 cp_lexer_consume_token (parser->lexer);
9041 maybe_warn_variadic_templates ();
9042
9043 *is_parameter_pack = true;
9044 }
9045 /* If the next token is an `=', then there is a
9046 default-argument. If the next token is a `>', we are at
9047 the end of the parameter-list. If the next token is a `,',
9048 then we are at the end of this parameter. */
9049 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9050 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9051 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9052 {
9053 identifier = cp_parser_identifier (parser);
9054 /* Treat invalid names as if the parameter were nameless. */
9055 if (identifier == error_mark_node)
9056 identifier = NULL_TREE;
9057 }
9058 else
9059 identifier = NULL_TREE;
9060
9061 /* Create the template parameter. */
9062 parameter = finish_template_template_parm (class_type_node,
9063 identifier);
9064
9065 /* If the next token is an `=', then there is a
9066 default-argument. */
9067 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9068 {
9069 bool is_template;
9070
9071 /* Consume the `='. */
9072 cp_lexer_consume_token (parser->lexer);
9073 /* Parse the id-expression. */
9074 push_deferring_access_checks (dk_no_deferred);
9075 default_argument
9076 = cp_parser_id_expression (parser,
9077 /*template_keyword_p=*/false,
9078 /*check_dependency_p=*/true,
9079 /*template_p=*/&is_template,
9080 /*declarator_p=*/false,
9081 /*optional_p=*/false);
9082 if (TREE_CODE (default_argument) == TYPE_DECL)
9083 /* If the id-expression was a template-id that refers to
9084 a template-class, we already have the declaration here,
9085 so no further lookup is needed. */
9086 ;
9087 else
9088 /* Look up the name. */
9089 default_argument
9090 = cp_parser_lookup_name (parser, default_argument,
9091 none_type,
9092 /*is_template=*/is_template,
9093 /*is_namespace=*/false,
9094 /*check_dependency=*/true,
9095 /*ambiguous_decls=*/NULL);
9096 /* See if the default argument is valid. */
9097 default_argument
9098 = check_template_template_default_arg (default_argument);
9099
9100 /* Template parameter packs cannot have default
9101 arguments. */
9102 if (*is_parameter_pack)
9103 {
9104 if (identifier)
9105 error ("template parameter pack %qD cannot have a default argument",
9106 identifier);
9107 else
9108 error ("template parameter packs cannot have default arguments");
9109 default_argument = NULL_TREE;
9110 }
9111 pop_deferring_access_checks ();
9112 }
9113 else
9114 default_argument = NULL_TREE;
9115
9116 /* Create the combined representation of the parameter and the
9117 default argument. */
9118 parameter = build_tree_list (default_argument, parameter);
9119 }
9120 break;
9121
9122 default:
9123 gcc_unreachable ();
9124 break;
9125 }
9126
9127 return parameter;
9128 }
9129
9130 /* Parse a template-id.
9131
9132 template-id:
9133 template-name < template-argument-list [opt] >
9134
9135 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9136 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9137 returned. Otherwise, if the template-name names a function, or set
9138 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9139 names a class, returns a TYPE_DECL for the specialization.
9140
9141 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9142 uninstantiated templates. */
9143
9144 static tree
9145 cp_parser_template_id (cp_parser *parser,
9146 bool template_keyword_p,
9147 bool check_dependency_p,
9148 bool is_declaration)
9149 {
9150 int i;
9151 tree template;
9152 tree arguments;
9153 tree template_id;
9154 cp_token_position start_of_id = 0;
9155 deferred_access_check *chk;
9156 VEC (deferred_access_check,gc) *access_check;
9157 cp_token *next_token, *next_token_2;
9158 bool is_identifier;
9159
9160 /* If the next token corresponds to a template-id, there is no need
9161 to reparse it. */
9162 next_token = cp_lexer_peek_token (parser->lexer);
9163 if (next_token->type == CPP_TEMPLATE_ID)
9164 {
9165 struct tree_check *check_value;
9166
9167 /* Get the stored value. */
9168 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9169 /* Perform any access checks that were deferred. */
9170 access_check = check_value->checks;
9171 if (access_check)
9172 {
9173 for (i = 0 ;
9174 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9175 ++i)
9176 {
9177 perform_or_defer_access_check (chk->binfo,
9178 chk->decl,
9179 chk->diag_decl);
9180 }
9181 }
9182 /* Return the stored value. */
9183 return check_value->value;
9184 }
9185
9186 /* Avoid performing name lookup if there is no possibility of
9187 finding a template-id. */
9188 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9189 || (next_token->type == CPP_NAME
9190 && !cp_parser_nth_token_starts_template_argument_list_p
9191 (parser, 2)))
9192 {
9193 cp_parser_error (parser, "expected template-id");
9194 return error_mark_node;
9195 }
9196
9197 /* Remember where the template-id starts. */
9198 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9199 start_of_id = cp_lexer_token_position (parser->lexer, false);
9200
9201 push_deferring_access_checks (dk_deferred);
9202
9203 /* Parse the template-name. */
9204 is_identifier = false;
9205 template = cp_parser_template_name (parser, template_keyword_p,
9206 check_dependency_p,
9207 is_declaration,
9208 &is_identifier);
9209 if (template == error_mark_node || is_identifier)
9210 {
9211 pop_deferring_access_checks ();
9212 return template;
9213 }
9214
9215 /* If we find the sequence `[:' after a template-name, it's probably
9216 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9217 parse correctly the argument list. */
9218 next_token = cp_lexer_peek_token (parser->lexer);
9219 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9220 if (next_token->type == CPP_OPEN_SQUARE
9221 && next_token->flags & DIGRAPH
9222 && next_token_2->type == CPP_COLON
9223 && !(next_token_2->flags & PREV_WHITE))
9224 {
9225 cp_parser_parse_tentatively (parser);
9226 /* Change `:' into `::'. */
9227 next_token_2->type = CPP_SCOPE;
9228 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9229 CPP_LESS. */
9230 cp_lexer_consume_token (parser->lexer);
9231 /* Parse the arguments. */
9232 arguments = cp_parser_enclosed_template_argument_list (parser);
9233 if (!cp_parser_parse_definitely (parser))
9234 {
9235 /* If we couldn't parse an argument list, then we revert our changes
9236 and return simply an error. Maybe this is not a template-id
9237 after all. */
9238 next_token_2->type = CPP_COLON;
9239 cp_parser_error (parser, "expected %<<%>");
9240 pop_deferring_access_checks ();
9241 return error_mark_node;
9242 }
9243 /* Otherwise, emit an error about the invalid digraph, but continue
9244 parsing because we got our argument list. */
9245 pedwarn ("%<<::%> cannot begin a template-argument list");
9246 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9247 "between %<<%> and %<::%>");
9248 if (!flag_permissive)
9249 {
9250 static bool hint;
9251 if (!hint)
9252 {
9253 inform ("(if you use -fpermissive G++ will accept your code)");
9254 hint = true;
9255 }
9256 }
9257 }
9258 else
9259 {
9260 /* Look for the `<' that starts the template-argument-list. */
9261 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9262 {
9263 pop_deferring_access_checks ();
9264 return error_mark_node;
9265 }
9266 /* Parse the arguments. */
9267 arguments = cp_parser_enclosed_template_argument_list (parser);
9268 }
9269
9270 /* Build a representation of the specialization. */
9271 if (TREE_CODE (template) == IDENTIFIER_NODE)
9272 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9273 else if (DECL_CLASS_TEMPLATE_P (template)
9274 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9275 {
9276 bool entering_scope;
9277 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9278 template (rather than some instantiation thereof) only if
9279 is not nested within some other construct. For example, in
9280 "template <typename T> void f(T) { A<T>::", A<T> is just an
9281 instantiation of A. */
9282 entering_scope = (template_parm_scope_p ()
9283 && cp_lexer_next_token_is (parser->lexer,
9284 CPP_SCOPE));
9285 template_id
9286 = finish_template_type (template, arguments, entering_scope);
9287 }
9288 else
9289 {
9290 /* If it's not a class-template or a template-template, it should be
9291 a function-template. */
9292 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9293 || TREE_CODE (template) == OVERLOAD
9294 || BASELINK_P (template)));
9295
9296 template_id = lookup_template_function (template, arguments);
9297 }
9298
9299 /* If parsing tentatively, replace the sequence of tokens that makes
9300 up the template-id with a CPP_TEMPLATE_ID token. That way,
9301 should we re-parse the token stream, we will not have to repeat
9302 the effort required to do the parse, nor will we issue duplicate
9303 error messages about problems during instantiation of the
9304 template. */
9305 if (start_of_id)
9306 {
9307 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9308
9309 /* Reset the contents of the START_OF_ID token. */
9310 token->type = CPP_TEMPLATE_ID;
9311 /* Retrieve any deferred checks. Do not pop this access checks yet
9312 so the memory will not be reclaimed during token replacing below. */
9313 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9314 token->u.tree_check_value->value = template_id;
9315 token->u.tree_check_value->checks = get_deferred_access_checks ();
9316 token->keyword = RID_MAX;
9317
9318 /* Purge all subsequent tokens. */
9319 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9320
9321 /* ??? Can we actually assume that, if template_id ==
9322 error_mark_node, we will have issued a diagnostic to the
9323 user, as opposed to simply marking the tentative parse as
9324 failed? */
9325 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9326 error ("parse error in template argument list");
9327 }
9328
9329 pop_deferring_access_checks ();
9330 return template_id;
9331 }
9332
9333 /* Parse a template-name.
9334
9335 template-name:
9336 identifier
9337
9338 The standard should actually say:
9339
9340 template-name:
9341 identifier
9342 operator-function-id
9343
9344 A defect report has been filed about this issue.
9345
9346 A conversion-function-id cannot be a template name because they cannot
9347 be part of a template-id. In fact, looking at this code:
9348
9349 a.operator K<int>()
9350
9351 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9352 It is impossible to call a templated conversion-function-id with an
9353 explicit argument list, since the only allowed template parameter is
9354 the type to which it is converting.
9355
9356 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9357 `template' keyword, in a construction like:
9358
9359 T::template f<3>()
9360
9361 In that case `f' is taken to be a template-name, even though there
9362 is no way of knowing for sure.
9363
9364 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9365 name refers to a set of overloaded functions, at least one of which
9366 is a template, or an IDENTIFIER_NODE with the name of the template,
9367 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9368 names are looked up inside uninstantiated templates. */
9369
9370 static tree
9371 cp_parser_template_name (cp_parser* parser,
9372 bool template_keyword_p,
9373 bool check_dependency_p,
9374 bool is_declaration,
9375 bool *is_identifier)
9376 {
9377 tree identifier;
9378 tree decl;
9379 tree fns;
9380
9381 /* If the next token is `operator', then we have either an
9382 operator-function-id or a conversion-function-id. */
9383 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9384 {
9385 /* We don't know whether we're looking at an
9386 operator-function-id or a conversion-function-id. */
9387 cp_parser_parse_tentatively (parser);
9388 /* Try an operator-function-id. */
9389 identifier = cp_parser_operator_function_id (parser);
9390 /* If that didn't work, try a conversion-function-id. */
9391 if (!cp_parser_parse_definitely (parser))
9392 {
9393 cp_parser_error (parser, "expected template-name");
9394 return error_mark_node;
9395 }
9396 }
9397 /* Look for the identifier. */
9398 else
9399 identifier = cp_parser_identifier (parser);
9400
9401 /* If we didn't find an identifier, we don't have a template-id. */
9402 if (identifier == error_mark_node)
9403 return error_mark_node;
9404
9405 /* If the name immediately followed the `template' keyword, then it
9406 is a template-name. However, if the next token is not `<', then
9407 we do not treat it as a template-name, since it is not being used
9408 as part of a template-id. This enables us to handle constructs
9409 like:
9410
9411 template <typename T> struct S { S(); };
9412 template <typename T> S<T>::S();
9413
9414 correctly. We would treat `S' as a template -- if it were `S<T>'
9415 -- but we do not if there is no `<'. */
9416
9417 if (processing_template_decl
9418 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9419 {
9420 /* In a declaration, in a dependent context, we pretend that the
9421 "template" keyword was present in order to improve error
9422 recovery. For example, given:
9423
9424 template <typename T> void f(T::X<int>);
9425
9426 we want to treat "X<int>" as a template-id. */
9427 if (is_declaration
9428 && !template_keyword_p
9429 && parser->scope && TYPE_P (parser->scope)
9430 && check_dependency_p
9431 && dependent_type_p (parser->scope)
9432 /* Do not do this for dtors (or ctors), since they never
9433 need the template keyword before their name. */
9434 && !constructor_name_p (identifier, parser->scope))
9435 {
9436 cp_token_position start = 0;
9437
9438 /* Explain what went wrong. */
9439 error ("non-template %qD used as template", identifier);
9440 inform ("use %<%T::template %D%> to indicate that it is a template",
9441 parser->scope, identifier);
9442 /* If parsing tentatively, find the location of the "<" token. */
9443 if (cp_parser_simulate_error (parser))
9444 start = cp_lexer_token_position (parser->lexer, true);
9445 /* Parse the template arguments so that we can issue error
9446 messages about them. */
9447 cp_lexer_consume_token (parser->lexer);
9448 cp_parser_enclosed_template_argument_list (parser);
9449 /* Skip tokens until we find a good place from which to
9450 continue parsing. */
9451 cp_parser_skip_to_closing_parenthesis (parser,
9452 /*recovering=*/true,
9453 /*or_comma=*/true,
9454 /*consume_paren=*/false);
9455 /* If parsing tentatively, permanently remove the
9456 template argument list. That will prevent duplicate
9457 error messages from being issued about the missing
9458 "template" keyword. */
9459 if (start)
9460 cp_lexer_purge_tokens_after (parser->lexer, start);
9461 if (is_identifier)
9462 *is_identifier = true;
9463 return identifier;
9464 }
9465
9466 /* If the "template" keyword is present, then there is generally
9467 no point in doing name-lookup, so we just return IDENTIFIER.
9468 But, if the qualifying scope is non-dependent then we can
9469 (and must) do name-lookup normally. */
9470 if (template_keyword_p
9471 && (!parser->scope
9472 || (TYPE_P (parser->scope)
9473 && dependent_type_p (parser->scope))))
9474 return identifier;
9475 }
9476
9477 /* Look up the name. */
9478 decl = cp_parser_lookup_name (parser, identifier,
9479 none_type,
9480 /*is_template=*/false,
9481 /*is_namespace=*/false,
9482 check_dependency_p,
9483 /*ambiguous_decls=*/NULL);
9484 decl = maybe_get_template_decl_from_type_decl (decl);
9485
9486 /* If DECL is a template, then the name was a template-name. */
9487 if (TREE_CODE (decl) == TEMPLATE_DECL)
9488 ;
9489 else
9490 {
9491 tree fn = NULL_TREE;
9492
9493 /* The standard does not explicitly indicate whether a name that
9494 names a set of overloaded declarations, some of which are
9495 templates, is a template-name. However, such a name should
9496 be a template-name; otherwise, there is no way to form a
9497 template-id for the overloaded templates. */
9498 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9499 if (TREE_CODE (fns) == OVERLOAD)
9500 for (fn = fns; fn; fn = OVL_NEXT (fn))
9501 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9502 break;
9503
9504 if (!fn)
9505 {
9506 /* The name does not name a template. */
9507 cp_parser_error (parser, "expected template-name");
9508 return error_mark_node;
9509 }
9510 }
9511
9512 /* If DECL is dependent, and refers to a function, then just return
9513 its name; we will look it up again during template instantiation. */
9514 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9515 {
9516 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9517 if (TYPE_P (scope) && dependent_type_p (scope))
9518 return identifier;
9519 }
9520
9521 return decl;
9522 }
9523
9524 /* Parse a template-argument-list.
9525
9526 template-argument-list:
9527 template-argument ... [opt]
9528 template-argument-list , template-argument ... [opt]
9529
9530 Returns a TREE_VEC containing the arguments. */
9531
9532 static tree
9533 cp_parser_template_argument_list (cp_parser* parser)
9534 {
9535 tree fixed_args[10];
9536 unsigned n_args = 0;
9537 unsigned alloced = 10;
9538 tree *arg_ary = fixed_args;
9539 tree vec;
9540 bool saved_in_template_argument_list_p;
9541 bool saved_ice_p;
9542 bool saved_non_ice_p;
9543
9544 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9545 parser->in_template_argument_list_p = true;
9546 /* Even if the template-id appears in an integral
9547 constant-expression, the contents of the argument list do
9548 not. */
9549 saved_ice_p = parser->integral_constant_expression_p;
9550 parser->integral_constant_expression_p = false;
9551 saved_non_ice_p = parser->non_integral_constant_expression_p;
9552 parser->non_integral_constant_expression_p = false;
9553 /* Parse the arguments. */
9554 do
9555 {
9556 tree argument;
9557
9558 if (n_args)
9559 /* Consume the comma. */
9560 cp_lexer_consume_token (parser->lexer);
9561
9562 /* Parse the template-argument. */
9563 argument = cp_parser_template_argument (parser);
9564
9565 /* If the next token is an ellipsis, we're expanding a template
9566 argument pack. */
9567 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9568 {
9569 /* Consume the `...' token. */
9570 cp_lexer_consume_token (parser->lexer);
9571
9572 /* Make the argument into a TYPE_PACK_EXPANSION or
9573 EXPR_PACK_EXPANSION. */
9574 argument = make_pack_expansion (argument);
9575 }
9576
9577 if (n_args == alloced)
9578 {
9579 alloced *= 2;
9580
9581 if (arg_ary == fixed_args)
9582 {
9583 arg_ary = XNEWVEC (tree, alloced);
9584 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9585 }
9586 else
9587 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9588 }
9589 arg_ary[n_args++] = argument;
9590 }
9591 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9592
9593 vec = make_tree_vec (n_args);
9594
9595 while (n_args--)
9596 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9597
9598 if (arg_ary != fixed_args)
9599 free (arg_ary);
9600 parser->non_integral_constant_expression_p = saved_non_ice_p;
9601 parser->integral_constant_expression_p = saved_ice_p;
9602 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9603 return vec;
9604 }
9605
9606 /* Parse a template-argument.
9607
9608 template-argument:
9609 assignment-expression
9610 type-id
9611 id-expression
9612
9613 The representation is that of an assignment-expression, type-id, or
9614 id-expression -- except that the qualified id-expression is
9615 evaluated, so that the value returned is either a DECL or an
9616 OVERLOAD.
9617
9618 Although the standard says "assignment-expression", it forbids
9619 throw-expressions or assignments in the template argument.
9620 Therefore, we use "conditional-expression" instead. */
9621
9622 static tree
9623 cp_parser_template_argument (cp_parser* parser)
9624 {
9625 tree argument;
9626 bool template_p;
9627 bool address_p;
9628 bool maybe_type_id = false;
9629 cp_token *token;
9630 cp_id_kind idk;
9631
9632 /* There's really no way to know what we're looking at, so we just
9633 try each alternative in order.
9634
9635 [temp.arg]
9636
9637 In a template-argument, an ambiguity between a type-id and an
9638 expression is resolved to a type-id, regardless of the form of
9639 the corresponding template-parameter.
9640
9641 Therefore, we try a type-id first. */
9642 cp_parser_parse_tentatively (parser);
9643 argument = cp_parser_type_id (parser);
9644 /* If there was no error parsing the type-id but the next token is a '>>',
9645 we probably found a typo for '> >'. But there are type-id which are
9646 also valid expressions. For instance:
9647
9648 struct X { int operator >> (int); };
9649 template <int V> struct Foo {};
9650 Foo<X () >> 5> r;
9651
9652 Here 'X()' is a valid type-id of a function type, but the user just
9653 wanted to write the expression "X() >> 5". Thus, we remember that we
9654 found a valid type-id, but we still try to parse the argument as an
9655 expression to see what happens. */
9656 if (!cp_parser_error_occurred (parser)
9657 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9658 {
9659 maybe_type_id = true;
9660 cp_parser_abort_tentative_parse (parser);
9661 }
9662 else
9663 {
9664 /* If the next token isn't a `,' or a `>', then this argument wasn't
9665 really finished. This means that the argument is not a valid
9666 type-id. */
9667 if (!cp_parser_next_token_ends_template_argument_p (parser))
9668 cp_parser_error (parser, "expected template-argument");
9669 /* If that worked, we're done. */
9670 if (cp_parser_parse_definitely (parser))
9671 return argument;
9672 }
9673 /* We're still not sure what the argument will be. */
9674 cp_parser_parse_tentatively (parser);
9675 /* Try a template. */
9676 argument = cp_parser_id_expression (parser,
9677 /*template_keyword_p=*/false,
9678 /*check_dependency_p=*/true,
9679 &template_p,
9680 /*declarator_p=*/false,
9681 /*optional_p=*/false);
9682 /* If the next token isn't a `,' or a `>', then this argument wasn't
9683 really finished. */
9684 if (!cp_parser_next_token_ends_template_argument_p (parser))
9685 cp_parser_error (parser, "expected template-argument");
9686 if (!cp_parser_error_occurred (parser))
9687 {
9688 /* Figure out what is being referred to. If the id-expression
9689 was for a class template specialization, then we will have a
9690 TYPE_DECL at this point. There is no need to do name lookup
9691 at this point in that case. */
9692 if (TREE_CODE (argument) != TYPE_DECL)
9693 argument = cp_parser_lookup_name (parser, argument,
9694 none_type,
9695 /*is_template=*/template_p,
9696 /*is_namespace=*/false,
9697 /*check_dependency=*/true,
9698 /*ambiguous_decls=*/NULL);
9699 if (TREE_CODE (argument) != TEMPLATE_DECL
9700 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9701 cp_parser_error (parser, "expected template-name");
9702 }
9703 if (cp_parser_parse_definitely (parser))
9704 return argument;
9705 /* It must be a non-type argument. There permitted cases are given
9706 in [temp.arg.nontype]:
9707
9708 -- an integral constant-expression of integral or enumeration
9709 type; or
9710
9711 -- the name of a non-type template-parameter; or
9712
9713 -- the name of an object or function with external linkage...
9714
9715 -- the address of an object or function with external linkage...
9716
9717 -- a pointer to member... */
9718 /* Look for a non-type template parameter. */
9719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9720 {
9721 cp_parser_parse_tentatively (parser);
9722 argument = cp_parser_primary_expression (parser,
9723 /*adress_p=*/false,
9724 /*cast_p=*/false,
9725 /*template_arg_p=*/true,
9726 &idk);
9727 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9728 || !cp_parser_next_token_ends_template_argument_p (parser))
9729 cp_parser_simulate_error (parser);
9730 if (cp_parser_parse_definitely (parser))
9731 return argument;
9732 }
9733
9734 /* If the next token is "&", the argument must be the address of an
9735 object or function with external linkage. */
9736 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9737 if (address_p)
9738 cp_lexer_consume_token (parser->lexer);
9739 /* See if we might have an id-expression. */
9740 token = cp_lexer_peek_token (parser->lexer);
9741 if (token->type == CPP_NAME
9742 || token->keyword == RID_OPERATOR
9743 || token->type == CPP_SCOPE
9744 || token->type == CPP_TEMPLATE_ID
9745 || token->type == CPP_NESTED_NAME_SPECIFIER)
9746 {
9747 cp_parser_parse_tentatively (parser);
9748 argument = cp_parser_primary_expression (parser,
9749 address_p,
9750 /*cast_p=*/false,
9751 /*template_arg_p=*/true,
9752 &idk);
9753 if (cp_parser_error_occurred (parser)
9754 || !cp_parser_next_token_ends_template_argument_p (parser))
9755 cp_parser_abort_tentative_parse (parser);
9756 else
9757 {
9758 if (TREE_CODE (argument) == INDIRECT_REF)
9759 {
9760 gcc_assert (REFERENCE_REF_P (argument));
9761 argument = TREE_OPERAND (argument, 0);
9762 }
9763
9764 if (TREE_CODE (argument) == VAR_DECL)
9765 {
9766 /* A variable without external linkage might still be a
9767 valid constant-expression, so no error is issued here
9768 if the external-linkage check fails. */
9769 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9770 cp_parser_simulate_error (parser);
9771 }
9772 else if (is_overloaded_fn (argument))
9773 /* All overloaded functions are allowed; if the external
9774 linkage test does not pass, an error will be issued
9775 later. */
9776 ;
9777 else if (address_p
9778 && (TREE_CODE (argument) == OFFSET_REF
9779 || TREE_CODE (argument) == SCOPE_REF))
9780 /* A pointer-to-member. */
9781 ;
9782 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9783 ;
9784 else
9785 cp_parser_simulate_error (parser);
9786
9787 if (cp_parser_parse_definitely (parser))
9788 {
9789 if (address_p)
9790 argument = build_x_unary_op (ADDR_EXPR, argument);
9791 return argument;
9792 }
9793 }
9794 }
9795 /* If the argument started with "&", there are no other valid
9796 alternatives at this point. */
9797 if (address_p)
9798 {
9799 cp_parser_error (parser, "invalid non-type template argument");
9800 return error_mark_node;
9801 }
9802
9803 /* If the argument wasn't successfully parsed as a type-id followed
9804 by '>>', the argument can only be a constant expression now.
9805 Otherwise, we try parsing the constant-expression tentatively,
9806 because the argument could really be a type-id. */
9807 if (maybe_type_id)
9808 cp_parser_parse_tentatively (parser);
9809 argument = cp_parser_constant_expression (parser,
9810 /*allow_non_constant_p=*/false,
9811 /*non_constant_p=*/NULL);
9812 argument = fold_non_dependent_expr (argument);
9813 if (!maybe_type_id)
9814 return argument;
9815 if (!cp_parser_next_token_ends_template_argument_p (parser))
9816 cp_parser_error (parser, "expected template-argument");
9817 if (cp_parser_parse_definitely (parser))
9818 return argument;
9819 /* We did our best to parse the argument as a non type-id, but that
9820 was the only alternative that matched (albeit with a '>' after
9821 it). We can assume it's just a typo from the user, and a
9822 diagnostic will then be issued. */
9823 return cp_parser_type_id (parser);
9824 }
9825
9826 /* Parse an explicit-instantiation.
9827
9828 explicit-instantiation:
9829 template declaration
9830
9831 Although the standard says `declaration', what it really means is:
9832
9833 explicit-instantiation:
9834 template decl-specifier-seq [opt] declarator [opt] ;
9835
9836 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9837 supposed to be allowed. A defect report has been filed about this
9838 issue.
9839
9840 GNU Extension:
9841
9842 explicit-instantiation:
9843 storage-class-specifier template
9844 decl-specifier-seq [opt] declarator [opt] ;
9845 function-specifier template
9846 decl-specifier-seq [opt] declarator [opt] ; */
9847
9848 static void
9849 cp_parser_explicit_instantiation (cp_parser* parser)
9850 {
9851 int declares_class_or_enum;
9852 cp_decl_specifier_seq decl_specifiers;
9853 tree extension_specifier = NULL_TREE;
9854
9855 /* Look for an (optional) storage-class-specifier or
9856 function-specifier. */
9857 if (cp_parser_allow_gnu_extensions_p (parser))
9858 {
9859 extension_specifier
9860 = cp_parser_storage_class_specifier_opt (parser);
9861 if (!extension_specifier)
9862 extension_specifier
9863 = cp_parser_function_specifier_opt (parser,
9864 /*decl_specs=*/NULL);
9865 }
9866
9867 /* Look for the `template' keyword. */
9868 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9869 /* Let the front end know that we are processing an explicit
9870 instantiation. */
9871 begin_explicit_instantiation ();
9872 /* [temp.explicit] says that we are supposed to ignore access
9873 control while processing explicit instantiation directives. */
9874 push_deferring_access_checks (dk_no_check);
9875 /* Parse a decl-specifier-seq. */
9876 cp_parser_decl_specifier_seq (parser,
9877 CP_PARSER_FLAGS_OPTIONAL,
9878 &decl_specifiers,
9879 &declares_class_or_enum);
9880 /* If there was exactly one decl-specifier, and it declared a class,
9881 and there's no declarator, then we have an explicit type
9882 instantiation. */
9883 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9884 {
9885 tree type;
9886
9887 type = check_tag_decl (&decl_specifiers);
9888 /* Turn access control back on for names used during
9889 template instantiation. */
9890 pop_deferring_access_checks ();
9891 if (type)
9892 do_type_instantiation (type, extension_specifier,
9893 /*complain=*/tf_error);
9894 }
9895 else
9896 {
9897 cp_declarator *declarator;
9898 tree decl;
9899
9900 /* Parse the declarator. */
9901 declarator
9902 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9903 /*ctor_dtor_or_conv_p=*/NULL,
9904 /*parenthesized_p=*/NULL,
9905 /*member_p=*/false);
9906 if (declares_class_or_enum & 2)
9907 cp_parser_check_for_definition_in_return_type (declarator,
9908 decl_specifiers.type);
9909 if (declarator != cp_error_declarator)
9910 {
9911 decl = grokdeclarator (declarator, &decl_specifiers,
9912 NORMAL, 0, &decl_specifiers.attributes);
9913 /* Turn access control back on for names used during
9914 template instantiation. */
9915 pop_deferring_access_checks ();
9916 /* Do the explicit instantiation. */
9917 do_decl_instantiation (decl, extension_specifier);
9918 }
9919 else
9920 {
9921 pop_deferring_access_checks ();
9922 /* Skip the body of the explicit instantiation. */
9923 cp_parser_skip_to_end_of_statement (parser);
9924 }
9925 }
9926 /* We're done with the instantiation. */
9927 end_explicit_instantiation ();
9928
9929 cp_parser_consume_semicolon_at_end_of_statement (parser);
9930 }
9931
9932 /* Parse an explicit-specialization.
9933
9934 explicit-specialization:
9935 template < > declaration
9936
9937 Although the standard says `declaration', what it really means is:
9938
9939 explicit-specialization:
9940 template <> decl-specifier [opt] init-declarator [opt] ;
9941 template <> function-definition
9942 template <> explicit-specialization
9943 template <> template-declaration */
9944
9945 static void
9946 cp_parser_explicit_specialization (cp_parser* parser)
9947 {
9948 bool need_lang_pop;
9949 /* Look for the `template' keyword. */
9950 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9951 /* Look for the `<'. */
9952 cp_parser_require (parser, CPP_LESS, "`<'");
9953 /* Look for the `>'. */
9954 cp_parser_require (parser, CPP_GREATER, "`>'");
9955 /* We have processed another parameter list. */
9956 ++parser->num_template_parameter_lists;
9957 /* [temp]
9958
9959 A template ... explicit specialization ... shall not have C
9960 linkage. */
9961 if (current_lang_name == lang_name_c)
9962 {
9963 error ("template specialization with C linkage");
9964 /* Give it C++ linkage to avoid confusing other parts of the
9965 front end. */
9966 push_lang_context (lang_name_cplusplus);
9967 need_lang_pop = true;
9968 }
9969 else
9970 need_lang_pop = false;
9971 /* Let the front end know that we are beginning a specialization. */
9972 if (!begin_specialization ())
9973 {
9974 end_specialization ();
9975 cp_parser_skip_to_end_of_block_or_statement (parser);
9976 return;
9977 }
9978
9979 /* If the next keyword is `template', we need to figure out whether
9980 or not we're looking a template-declaration. */
9981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9982 {
9983 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9984 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9985 cp_parser_template_declaration_after_export (parser,
9986 /*member_p=*/false);
9987 else
9988 cp_parser_explicit_specialization (parser);
9989 }
9990 else
9991 /* Parse the dependent declaration. */
9992 cp_parser_single_declaration (parser,
9993 /*checks=*/NULL,
9994 /*member_p=*/false,
9995 /*friend_p=*/NULL);
9996 /* We're done with the specialization. */
9997 end_specialization ();
9998 /* For the erroneous case of a template with C linkage, we pushed an
9999 implicit C++ linkage scope; exit that scope now. */
10000 if (need_lang_pop)
10001 pop_lang_context ();
10002 /* We're done with this parameter list. */
10003 --parser->num_template_parameter_lists;
10004 }
10005
10006 /* Parse a type-specifier.
10007
10008 type-specifier:
10009 simple-type-specifier
10010 class-specifier
10011 enum-specifier
10012 elaborated-type-specifier
10013 cv-qualifier
10014
10015 GNU Extension:
10016
10017 type-specifier:
10018 __complex__
10019
10020 Returns a representation of the type-specifier. For a
10021 class-specifier, enum-specifier, or elaborated-type-specifier, a
10022 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10023
10024 The parser flags FLAGS is used to control type-specifier parsing.
10025
10026 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10027 in a decl-specifier-seq.
10028
10029 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10030 class-specifier, enum-specifier, or elaborated-type-specifier, then
10031 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10032 if a type is declared; 2 if it is defined. Otherwise, it is set to
10033 zero.
10034
10035 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10036 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10037 is set to FALSE. */
10038
10039 static tree
10040 cp_parser_type_specifier (cp_parser* parser,
10041 cp_parser_flags flags,
10042 cp_decl_specifier_seq *decl_specs,
10043 bool is_declaration,
10044 int* declares_class_or_enum,
10045 bool* is_cv_qualifier)
10046 {
10047 tree type_spec = NULL_TREE;
10048 cp_token *token;
10049 enum rid keyword;
10050 cp_decl_spec ds = ds_last;
10051
10052 /* Assume this type-specifier does not declare a new type. */
10053 if (declares_class_or_enum)
10054 *declares_class_or_enum = 0;
10055 /* And that it does not specify a cv-qualifier. */
10056 if (is_cv_qualifier)
10057 *is_cv_qualifier = false;
10058 /* Peek at the next token. */
10059 token = cp_lexer_peek_token (parser->lexer);
10060
10061 /* If we're looking at a keyword, we can use that to guide the
10062 production we choose. */
10063 keyword = token->keyword;
10064 switch (keyword)
10065 {
10066 case RID_ENUM:
10067 /* Look for the enum-specifier. */
10068 type_spec = cp_parser_enum_specifier (parser);
10069 /* If that worked, we're done. */
10070 if (type_spec)
10071 {
10072 if (declares_class_or_enum)
10073 *declares_class_or_enum = 2;
10074 if (decl_specs)
10075 cp_parser_set_decl_spec_type (decl_specs,
10076 type_spec,
10077 /*user_defined_p=*/true);
10078 return type_spec;
10079 }
10080 else
10081 goto elaborated_type_specifier;
10082
10083 /* Any of these indicate either a class-specifier, or an
10084 elaborated-type-specifier. */
10085 case RID_CLASS:
10086 case RID_STRUCT:
10087 case RID_UNION:
10088 /* Parse tentatively so that we can back up if we don't find a
10089 class-specifier. */
10090 cp_parser_parse_tentatively (parser);
10091 /* Look for the class-specifier. */
10092 type_spec = cp_parser_class_specifier (parser);
10093 /* If that worked, we're done. */
10094 if (cp_parser_parse_definitely (parser))
10095 {
10096 if (declares_class_or_enum)
10097 *declares_class_or_enum = 2;
10098 if (decl_specs)
10099 cp_parser_set_decl_spec_type (decl_specs,
10100 type_spec,
10101 /*user_defined_p=*/true);
10102 return type_spec;
10103 }
10104
10105 /* Fall through. */
10106 elaborated_type_specifier:
10107 /* We're declaring (not defining) a class or enum. */
10108 if (declares_class_or_enum)
10109 *declares_class_or_enum = 1;
10110
10111 /* Fall through. */
10112 case RID_TYPENAME:
10113 /* Look for an elaborated-type-specifier. */
10114 type_spec
10115 = (cp_parser_elaborated_type_specifier
10116 (parser,
10117 decl_specs && decl_specs->specs[(int) ds_friend],
10118 is_declaration));
10119 if (decl_specs)
10120 cp_parser_set_decl_spec_type (decl_specs,
10121 type_spec,
10122 /*user_defined_p=*/true);
10123 return type_spec;
10124
10125 case RID_CONST:
10126 ds = ds_const;
10127 if (is_cv_qualifier)
10128 *is_cv_qualifier = true;
10129 break;
10130
10131 case RID_VOLATILE:
10132 ds = ds_volatile;
10133 if (is_cv_qualifier)
10134 *is_cv_qualifier = true;
10135 break;
10136
10137 case RID_RESTRICT:
10138 ds = ds_restrict;
10139 if (is_cv_qualifier)
10140 *is_cv_qualifier = true;
10141 break;
10142
10143 case RID_COMPLEX:
10144 /* The `__complex__' keyword is a GNU extension. */
10145 ds = ds_complex;
10146 break;
10147
10148 default:
10149 break;
10150 }
10151
10152 /* Handle simple keywords. */
10153 if (ds != ds_last)
10154 {
10155 if (decl_specs)
10156 {
10157 ++decl_specs->specs[(int)ds];
10158 decl_specs->any_specifiers_p = true;
10159 }
10160 return cp_lexer_consume_token (parser->lexer)->u.value;
10161 }
10162
10163 /* If we do not already have a type-specifier, assume we are looking
10164 at a simple-type-specifier. */
10165 type_spec = cp_parser_simple_type_specifier (parser,
10166 decl_specs,
10167 flags);
10168
10169 /* If we didn't find a type-specifier, and a type-specifier was not
10170 optional in this context, issue an error message. */
10171 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10172 {
10173 cp_parser_error (parser, "expected type specifier");
10174 return error_mark_node;
10175 }
10176
10177 return type_spec;
10178 }
10179
10180 /* Parse a simple-type-specifier.
10181
10182 simple-type-specifier:
10183 :: [opt] nested-name-specifier [opt] type-name
10184 :: [opt] nested-name-specifier template template-id
10185 char
10186 wchar_t
10187 bool
10188 short
10189 int
10190 long
10191 signed
10192 unsigned
10193 float
10194 double
10195 void
10196
10197 GNU Extension:
10198
10199 simple-type-specifier:
10200 __typeof__ unary-expression
10201 __typeof__ ( type-id )
10202
10203 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10204 appropriately updated. */
10205
10206 static tree
10207 cp_parser_simple_type_specifier (cp_parser* parser,
10208 cp_decl_specifier_seq *decl_specs,
10209 cp_parser_flags flags)
10210 {
10211 tree type = NULL_TREE;
10212 cp_token *token;
10213
10214 /* Peek at the next token. */
10215 token = cp_lexer_peek_token (parser->lexer);
10216
10217 /* If we're looking at a keyword, things are easy. */
10218 switch (token->keyword)
10219 {
10220 case RID_CHAR:
10221 if (decl_specs)
10222 decl_specs->explicit_char_p = true;
10223 type = char_type_node;
10224 break;
10225 case RID_WCHAR:
10226 type = wchar_type_node;
10227 break;
10228 case RID_BOOL:
10229 type = boolean_type_node;
10230 break;
10231 case RID_SHORT:
10232 if (decl_specs)
10233 ++decl_specs->specs[(int) ds_short];
10234 type = short_integer_type_node;
10235 break;
10236 case RID_INT:
10237 if (decl_specs)
10238 decl_specs->explicit_int_p = true;
10239 type = integer_type_node;
10240 break;
10241 case RID_LONG:
10242 if (decl_specs)
10243 ++decl_specs->specs[(int) ds_long];
10244 type = long_integer_type_node;
10245 break;
10246 case RID_SIGNED:
10247 if (decl_specs)
10248 ++decl_specs->specs[(int) ds_signed];
10249 type = integer_type_node;
10250 break;
10251 case RID_UNSIGNED:
10252 if (decl_specs)
10253 ++decl_specs->specs[(int) ds_unsigned];
10254 type = unsigned_type_node;
10255 break;
10256 case RID_FLOAT:
10257 type = float_type_node;
10258 break;
10259 case RID_DOUBLE:
10260 type = double_type_node;
10261 break;
10262 case RID_VOID:
10263 type = void_type_node;
10264 break;
10265
10266 case RID_TYPEOF:
10267 /* Consume the `typeof' token. */
10268 cp_lexer_consume_token (parser->lexer);
10269 /* Parse the operand to `typeof'. */
10270 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10271 /* If it is not already a TYPE, take its type. */
10272 if (!TYPE_P (type))
10273 type = finish_typeof (type);
10274
10275 if (decl_specs)
10276 cp_parser_set_decl_spec_type (decl_specs, type,
10277 /*user_defined_p=*/true);
10278
10279 return type;
10280
10281 default:
10282 break;
10283 }
10284
10285 /* If the type-specifier was for a built-in type, we're done. */
10286 if (type)
10287 {
10288 tree id;
10289
10290 /* Record the type. */
10291 if (decl_specs
10292 && (token->keyword != RID_SIGNED
10293 && token->keyword != RID_UNSIGNED
10294 && token->keyword != RID_SHORT
10295 && token->keyword != RID_LONG))
10296 cp_parser_set_decl_spec_type (decl_specs,
10297 type,
10298 /*user_defined=*/false);
10299 if (decl_specs)
10300 decl_specs->any_specifiers_p = true;
10301
10302 /* Consume the token. */
10303 id = cp_lexer_consume_token (parser->lexer)->u.value;
10304
10305 /* There is no valid C++ program where a non-template type is
10306 followed by a "<". That usually indicates that the user thought
10307 that the type was a template. */
10308 cp_parser_check_for_invalid_template_id (parser, type);
10309
10310 return TYPE_NAME (type);
10311 }
10312
10313 /* The type-specifier must be a user-defined type. */
10314 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10315 {
10316 bool qualified_p;
10317 bool global_p;
10318
10319 /* Don't gobble tokens or issue error messages if this is an
10320 optional type-specifier. */
10321 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10322 cp_parser_parse_tentatively (parser);
10323
10324 /* Look for the optional `::' operator. */
10325 global_p
10326 = (cp_parser_global_scope_opt (parser,
10327 /*current_scope_valid_p=*/false)
10328 != NULL_TREE);
10329 /* Look for the nested-name specifier. */
10330 qualified_p
10331 = (cp_parser_nested_name_specifier_opt (parser,
10332 /*typename_keyword_p=*/false,
10333 /*check_dependency_p=*/true,
10334 /*type_p=*/false,
10335 /*is_declaration=*/false)
10336 != NULL_TREE);
10337 /* If we have seen a nested-name-specifier, and the next token
10338 is `template', then we are using the template-id production. */
10339 if (parser->scope
10340 && cp_parser_optional_template_keyword (parser))
10341 {
10342 /* Look for the template-id. */
10343 type = cp_parser_template_id (parser,
10344 /*template_keyword_p=*/true,
10345 /*check_dependency_p=*/true,
10346 /*is_declaration=*/false);
10347 /* If the template-id did not name a type, we are out of
10348 luck. */
10349 if (TREE_CODE (type) != TYPE_DECL)
10350 {
10351 cp_parser_error (parser, "expected template-id for type");
10352 type = NULL_TREE;
10353 }
10354 }
10355 /* Otherwise, look for a type-name. */
10356 else
10357 type = cp_parser_type_name (parser);
10358 /* Keep track of all name-lookups performed in class scopes. */
10359 if (type
10360 && !global_p
10361 && !qualified_p
10362 && TREE_CODE (type) == TYPE_DECL
10363 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10364 maybe_note_name_used_in_class (DECL_NAME (type), type);
10365 /* If it didn't work out, we don't have a TYPE. */
10366 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10367 && !cp_parser_parse_definitely (parser))
10368 type = NULL_TREE;
10369 if (type && decl_specs)
10370 cp_parser_set_decl_spec_type (decl_specs, type,
10371 /*user_defined=*/true);
10372 }
10373
10374 /* If we didn't get a type-name, issue an error message. */
10375 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10376 {
10377 cp_parser_error (parser, "expected type-name");
10378 return error_mark_node;
10379 }
10380
10381 /* There is no valid C++ program where a non-template type is
10382 followed by a "<". That usually indicates that the user thought
10383 that the type was a template. */
10384 if (type && type != error_mark_node)
10385 {
10386 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10387 If it is, then the '<'...'>' enclose protocol names rather than
10388 template arguments, and so everything is fine. */
10389 if (c_dialect_objc ()
10390 && (objc_is_id (type) || objc_is_class_name (type)))
10391 {
10392 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10393 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10394
10395 /* Clobber the "unqualified" type previously entered into
10396 DECL_SPECS with the new, improved protocol-qualified version. */
10397 if (decl_specs)
10398 decl_specs->type = qual_type;
10399
10400 return qual_type;
10401 }
10402
10403 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10404 }
10405
10406 return type;
10407 }
10408
10409 /* Parse a type-name.
10410
10411 type-name:
10412 class-name
10413 enum-name
10414 typedef-name
10415
10416 enum-name:
10417 identifier
10418
10419 typedef-name:
10420 identifier
10421
10422 Returns a TYPE_DECL for the type. */
10423
10424 static tree
10425 cp_parser_type_name (cp_parser* parser)
10426 {
10427 tree type_decl;
10428 tree identifier;
10429
10430 /* We can't know yet whether it is a class-name or not. */
10431 cp_parser_parse_tentatively (parser);
10432 /* Try a class-name. */
10433 type_decl = cp_parser_class_name (parser,
10434 /*typename_keyword_p=*/false,
10435 /*template_keyword_p=*/false,
10436 none_type,
10437 /*check_dependency_p=*/true,
10438 /*class_head_p=*/false,
10439 /*is_declaration=*/false);
10440 /* If it's not a class-name, keep looking. */
10441 if (!cp_parser_parse_definitely (parser))
10442 {
10443 /* It must be a typedef-name or an enum-name. */
10444 identifier = cp_parser_identifier (parser);
10445 if (identifier == error_mark_node)
10446 return error_mark_node;
10447
10448 /* Look up the type-name. */
10449 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10450
10451 if (TREE_CODE (type_decl) != TYPE_DECL
10452 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10453 {
10454 /* See if this is an Objective-C type. */
10455 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10456 tree type = objc_get_protocol_qualified_type (identifier, protos);
10457 if (type)
10458 type_decl = TYPE_NAME (type);
10459 }
10460
10461 /* Issue an error if we did not find a type-name. */
10462 if (TREE_CODE (type_decl) != TYPE_DECL)
10463 {
10464 if (!cp_parser_simulate_error (parser))
10465 cp_parser_name_lookup_error (parser, identifier, type_decl,
10466 "is not a type");
10467 type_decl = error_mark_node;
10468 }
10469 /* Remember that the name was used in the definition of the
10470 current class so that we can check later to see if the
10471 meaning would have been different after the class was
10472 entirely defined. */
10473 else if (type_decl != error_mark_node
10474 && !parser->scope)
10475 maybe_note_name_used_in_class (identifier, type_decl);
10476 }
10477
10478 return type_decl;
10479 }
10480
10481
10482 /* Parse an elaborated-type-specifier. Note that the grammar given
10483 here incorporates the resolution to DR68.
10484
10485 elaborated-type-specifier:
10486 class-key :: [opt] nested-name-specifier [opt] identifier
10487 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10488 enum :: [opt] nested-name-specifier [opt] identifier
10489 typename :: [opt] nested-name-specifier identifier
10490 typename :: [opt] nested-name-specifier template [opt]
10491 template-id
10492
10493 GNU extension:
10494
10495 elaborated-type-specifier:
10496 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10497 class-key attributes :: [opt] nested-name-specifier [opt]
10498 template [opt] template-id
10499 enum attributes :: [opt] nested-name-specifier [opt] identifier
10500
10501 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10502 declared `friend'. If IS_DECLARATION is TRUE, then this
10503 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10504 something is being declared.
10505
10506 Returns the TYPE specified. */
10507
10508 static tree
10509 cp_parser_elaborated_type_specifier (cp_parser* parser,
10510 bool is_friend,
10511 bool is_declaration)
10512 {
10513 enum tag_types tag_type;
10514 tree identifier;
10515 tree type = NULL_TREE;
10516 tree attributes = NULL_TREE;
10517
10518 /* See if we're looking at the `enum' keyword. */
10519 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10520 {
10521 /* Consume the `enum' token. */
10522 cp_lexer_consume_token (parser->lexer);
10523 /* Remember that it's an enumeration type. */
10524 tag_type = enum_type;
10525 /* Parse the attributes. */
10526 attributes = cp_parser_attributes_opt (parser);
10527 }
10528 /* Or, it might be `typename'. */
10529 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10530 RID_TYPENAME))
10531 {
10532 /* Consume the `typename' token. */
10533 cp_lexer_consume_token (parser->lexer);
10534 /* Remember that it's a `typename' type. */
10535 tag_type = typename_type;
10536 /* The `typename' keyword is only allowed in templates. */
10537 if (!processing_template_decl)
10538 pedwarn ("using %<typename%> outside of template");
10539 }
10540 /* Otherwise it must be a class-key. */
10541 else
10542 {
10543 tag_type = cp_parser_class_key (parser);
10544 if (tag_type == none_type)
10545 return error_mark_node;
10546 /* Parse the attributes. */
10547 attributes = cp_parser_attributes_opt (parser);
10548 }
10549
10550 /* Look for the `::' operator. */
10551 cp_parser_global_scope_opt (parser,
10552 /*current_scope_valid_p=*/false);
10553 /* Look for the nested-name-specifier. */
10554 if (tag_type == typename_type)
10555 {
10556 if (!cp_parser_nested_name_specifier (parser,
10557 /*typename_keyword_p=*/true,
10558 /*check_dependency_p=*/true,
10559 /*type_p=*/true,
10560 is_declaration))
10561 return error_mark_node;
10562 }
10563 else
10564 /* Even though `typename' is not present, the proposed resolution
10565 to Core Issue 180 says that in `class A<T>::B', `B' should be
10566 considered a type-name, even if `A<T>' is dependent. */
10567 cp_parser_nested_name_specifier_opt (parser,
10568 /*typename_keyword_p=*/true,
10569 /*check_dependency_p=*/true,
10570 /*type_p=*/true,
10571 is_declaration);
10572 /* For everything but enumeration types, consider a template-id.
10573 For an enumeration type, consider only a plain identifier. */
10574 if (tag_type != enum_type)
10575 {
10576 bool template_p = false;
10577 tree decl;
10578
10579 /* Allow the `template' keyword. */
10580 template_p = cp_parser_optional_template_keyword (parser);
10581 /* If we didn't see `template', we don't know if there's a
10582 template-id or not. */
10583 if (!template_p)
10584 cp_parser_parse_tentatively (parser);
10585 /* Parse the template-id. */
10586 decl = cp_parser_template_id (parser, template_p,
10587 /*check_dependency_p=*/true,
10588 is_declaration);
10589 /* If we didn't find a template-id, look for an ordinary
10590 identifier. */
10591 if (!template_p && !cp_parser_parse_definitely (parser))
10592 ;
10593 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10594 in effect, then we must assume that, upon instantiation, the
10595 template will correspond to a class. */
10596 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10597 && tag_type == typename_type)
10598 type = make_typename_type (parser->scope, decl,
10599 typename_type,
10600 /*complain=*/tf_error);
10601 else
10602 type = TREE_TYPE (decl);
10603 }
10604
10605 if (!type)
10606 {
10607 identifier = cp_parser_identifier (parser);
10608
10609 if (identifier == error_mark_node)
10610 {
10611 parser->scope = NULL_TREE;
10612 return error_mark_node;
10613 }
10614
10615 /* For a `typename', we needn't call xref_tag. */
10616 if (tag_type == typename_type
10617 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10618 return cp_parser_make_typename_type (parser, parser->scope,
10619 identifier);
10620 /* Look up a qualified name in the usual way. */
10621 if (parser->scope)
10622 {
10623 tree decl;
10624
10625 decl = cp_parser_lookup_name (parser, identifier,
10626 tag_type,
10627 /*is_template=*/false,
10628 /*is_namespace=*/false,
10629 /*check_dependency=*/true,
10630 /*ambiguous_decls=*/NULL);
10631
10632 /* If we are parsing friend declaration, DECL may be a
10633 TEMPLATE_DECL tree node here. However, we need to check
10634 whether this TEMPLATE_DECL results in valid code. Consider
10635 the following example:
10636
10637 namespace N {
10638 template <class T> class C {};
10639 }
10640 class X {
10641 template <class T> friend class N::C; // #1, valid code
10642 };
10643 template <class T> class Y {
10644 friend class N::C; // #2, invalid code
10645 };
10646
10647 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10648 name lookup of `N::C'. We see that friend declaration must
10649 be template for the code to be valid. Note that
10650 processing_template_decl does not work here since it is
10651 always 1 for the above two cases. */
10652
10653 decl = (cp_parser_maybe_treat_template_as_class
10654 (decl, /*tag_name_p=*/is_friend
10655 && parser->num_template_parameter_lists));
10656
10657 if (TREE_CODE (decl) != TYPE_DECL)
10658 {
10659 cp_parser_diagnose_invalid_type_name (parser,
10660 parser->scope,
10661 identifier);
10662 return error_mark_node;
10663 }
10664
10665 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10666 {
10667 bool allow_template = (parser->num_template_parameter_lists
10668 || DECL_SELF_REFERENCE_P (decl));
10669 type = check_elaborated_type_specifier (tag_type, decl,
10670 allow_template);
10671
10672 if (type == error_mark_node)
10673 return error_mark_node;
10674 }
10675
10676 type = TREE_TYPE (decl);
10677 }
10678 else
10679 {
10680 /* An elaborated-type-specifier sometimes introduces a new type and
10681 sometimes names an existing type. Normally, the rule is that it
10682 introduces a new type only if there is not an existing type of
10683 the same name already in scope. For example, given:
10684
10685 struct S {};
10686 void f() { struct S s; }
10687
10688 the `struct S' in the body of `f' is the same `struct S' as in
10689 the global scope; the existing definition is used. However, if
10690 there were no global declaration, this would introduce a new
10691 local class named `S'.
10692
10693 An exception to this rule applies to the following code:
10694
10695 namespace N { struct S; }
10696
10697 Here, the elaborated-type-specifier names a new type
10698 unconditionally; even if there is already an `S' in the
10699 containing scope this declaration names a new type.
10700 This exception only applies if the elaborated-type-specifier
10701 forms the complete declaration:
10702
10703 [class.name]
10704
10705 A declaration consisting solely of `class-key identifier ;' is
10706 either a redeclaration of the name in the current scope or a
10707 forward declaration of the identifier as a class name. It
10708 introduces the name into the current scope.
10709
10710 We are in this situation precisely when the next token is a `;'.
10711
10712 An exception to the exception is that a `friend' declaration does
10713 *not* name a new type; i.e., given:
10714
10715 struct S { friend struct T; };
10716
10717 `T' is not a new type in the scope of `S'.
10718
10719 Also, `new struct S' or `sizeof (struct S)' never results in the
10720 definition of a new type; a new type can only be declared in a
10721 declaration context. */
10722
10723 tag_scope ts;
10724 bool template_p;
10725
10726 if (is_friend)
10727 /* Friends have special name lookup rules. */
10728 ts = ts_within_enclosing_non_class;
10729 else if (is_declaration
10730 && cp_lexer_next_token_is (parser->lexer,
10731 CPP_SEMICOLON))
10732 /* This is a `class-key identifier ;' */
10733 ts = ts_current;
10734 else
10735 ts = ts_global;
10736
10737 template_p =
10738 (parser->num_template_parameter_lists
10739 && (cp_parser_next_token_starts_class_definition_p (parser)
10740 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10741 /* An unqualified name was used to reference this type, so
10742 there were no qualifying templates. */
10743 if (!cp_parser_check_template_parameters (parser,
10744 /*num_templates=*/0))
10745 return error_mark_node;
10746 type = xref_tag (tag_type, identifier, ts, template_p);
10747 }
10748 }
10749
10750 if (type == error_mark_node)
10751 return error_mark_node;
10752
10753 /* Allow attributes on forward declarations of classes. */
10754 if (attributes)
10755 {
10756 if (TREE_CODE (type) == TYPENAME_TYPE)
10757 warning (OPT_Wattributes,
10758 "attributes ignored on uninstantiated type");
10759 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10760 && ! processing_explicit_instantiation)
10761 warning (OPT_Wattributes,
10762 "attributes ignored on template instantiation");
10763 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10764 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10765 else
10766 warning (OPT_Wattributes,
10767 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10768 }
10769
10770 if (tag_type != enum_type)
10771 cp_parser_check_class_key (tag_type, type);
10772
10773 /* A "<" cannot follow an elaborated type specifier. If that
10774 happens, the user was probably trying to form a template-id. */
10775 cp_parser_check_for_invalid_template_id (parser, type);
10776
10777 return type;
10778 }
10779
10780 /* Parse an enum-specifier.
10781
10782 enum-specifier:
10783 enum identifier [opt] { enumerator-list [opt] }
10784
10785 GNU Extensions:
10786 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10787 attributes[opt]
10788
10789 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10790 if the token stream isn't an enum-specifier after all. */
10791
10792 static tree
10793 cp_parser_enum_specifier (cp_parser* parser)
10794 {
10795 tree identifier;
10796 tree type;
10797 tree attributes;
10798
10799 /* Parse tentatively so that we can back up if we don't find a
10800 enum-specifier. */
10801 cp_parser_parse_tentatively (parser);
10802
10803 /* Caller guarantees that the current token is 'enum', an identifier
10804 possibly follows, and the token after that is an opening brace.
10805 If we don't have an identifier, fabricate an anonymous name for
10806 the enumeration being defined. */
10807 cp_lexer_consume_token (parser->lexer);
10808
10809 attributes = cp_parser_attributes_opt (parser);
10810
10811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10812 identifier = cp_parser_identifier (parser);
10813 else
10814 identifier = make_anon_name ();
10815
10816 /* Look for the `{' but don't consume it yet. */
10817 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10818 cp_parser_simulate_error (parser);
10819
10820 if (!cp_parser_parse_definitely (parser))
10821 return NULL_TREE;
10822
10823 /* Issue an error message if type-definitions are forbidden here. */
10824 if (!cp_parser_check_type_definition (parser))
10825 type = error_mark_node;
10826 else
10827 /* Create the new type. We do this before consuming the opening
10828 brace so the enum will be recorded as being on the line of its
10829 tag (or the 'enum' keyword, if there is no tag). */
10830 type = start_enum (identifier);
10831
10832 /* Consume the opening brace. */
10833 cp_lexer_consume_token (parser->lexer);
10834
10835 if (type == error_mark_node)
10836 {
10837 cp_parser_skip_to_end_of_block_or_statement (parser);
10838 return error_mark_node;
10839 }
10840
10841 /* If the next token is not '}', then there are some enumerators. */
10842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10843 cp_parser_enumerator_list (parser, type);
10844
10845 /* Consume the final '}'. */
10846 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10847
10848 /* Look for trailing attributes to apply to this enumeration, and
10849 apply them if appropriate. */
10850 if (cp_parser_allow_gnu_extensions_p (parser))
10851 {
10852 tree trailing_attr = cp_parser_attributes_opt (parser);
10853 cplus_decl_attributes (&type,
10854 trailing_attr,
10855 (int) ATTR_FLAG_TYPE_IN_PLACE);
10856 }
10857
10858 /* Finish up the enumeration. */
10859 finish_enum (type);
10860
10861 return type;
10862 }
10863
10864 /* Parse an enumerator-list. The enumerators all have the indicated
10865 TYPE.
10866
10867 enumerator-list:
10868 enumerator-definition
10869 enumerator-list , enumerator-definition */
10870
10871 static void
10872 cp_parser_enumerator_list (cp_parser* parser, tree type)
10873 {
10874 while (true)
10875 {
10876 /* Parse an enumerator-definition. */
10877 cp_parser_enumerator_definition (parser, type);
10878
10879 /* If the next token is not a ',', we've reached the end of
10880 the list. */
10881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10882 break;
10883 /* Otherwise, consume the `,' and keep going. */
10884 cp_lexer_consume_token (parser->lexer);
10885 /* If the next token is a `}', there is a trailing comma. */
10886 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10887 {
10888 if (pedantic && !in_system_header)
10889 pedwarn ("comma at end of enumerator list");
10890 break;
10891 }
10892 }
10893 }
10894
10895 /* Parse an enumerator-definition. The enumerator has the indicated
10896 TYPE.
10897
10898 enumerator-definition:
10899 enumerator
10900 enumerator = constant-expression
10901
10902 enumerator:
10903 identifier */
10904
10905 static void
10906 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10907 {
10908 tree identifier;
10909 tree value;
10910
10911 /* Look for the identifier. */
10912 identifier = cp_parser_identifier (parser);
10913 if (identifier == error_mark_node)
10914 return;
10915
10916 /* If the next token is an '=', then there is an explicit value. */
10917 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10918 {
10919 /* Consume the `=' token. */
10920 cp_lexer_consume_token (parser->lexer);
10921 /* Parse the value. */
10922 value = cp_parser_constant_expression (parser,
10923 /*allow_non_constant_p=*/false,
10924 NULL);
10925 }
10926 else
10927 value = NULL_TREE;
10928
10929 /* Create the enumerator. */
10930 build_enumerator (identifier, value, type);
10931 }
10932
10933 /* Parse a namespace-name.
10934
10935 namespace-name:
10936 original-namespace-name
10937 namespace-alias
10938
10939 Returns the NAMESPACE_DECL for the namespace. */
10940
10941 static tree
10942 cp_parser_namespace_name (cp_parser* parser)
10943 {
10944 tree identifier;
10945 tree namespace_decl;
10946
10947 /* Get the name of the namespace. */
10948 identifier = cp_parser_identifier (parser);
10949 if (identifier == error_mark_node)
10950 return error_mark_node;
10951
10952 /* Look up the identifier in the currently active scope. Look only
10953 for namespaces, due to:
10954
10955 [basic.lookup.udir]
10956
10957 When looking up a namespace-name in a using-directive or alias
10958 definition, only namespace names are considered.
10959
10960 And:
10961
10962 [basic.lookup.qual]
10963
10964 During the lookup of a name preceding the :: scope resolution
10965 operator, object, function, and enumerator names are ignored.
10966
10967 (Note that cp_parser_class_or_namespace_name only calls this
10968 function if the token after the name is the scope resolution
10969 operator.) */
10970 namespace_decl = cp_parser_lookup_name (parser, identifier,
10971 none_type,
10972 /*is_template=*/false,
10973 /*is_namespace=*/true,
10974 /*check_dependency=*/true,
10975 /*ambiguous_decls=*/NULL);
10976 /* If it's not a namespace, issue an error. */
10977 if (namespace_decl == error_mark_node
10978 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10979 {
10980 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10981 error ("%qD is not a namespace-name", identifier);
10982 cp_parser_error (parser, "expected namespace-name");
10983 namespace_decl = error_mark_node;
10984 }
10985
10986 return namespace_decl;
10987 }
10988
10989 /* Parse a namespace-definition.
10990
10991 namespace-definition:
10992 named-namespace-definition
10993 unnamed-namespace-definition
10994
10995 named-namespace-definition:
10996 original-namespace-definition
10997 extension-namespace-definition
10998
10999 original-namespace-definition:
11000 namespace identifier { namespace-body }
11001
11002 extension-namespace-definition:
11003 namespace original-namespace-name { namespace-body }
11004
11005 unnamed-namespace-definition:
11006 namespace { namespace-body } */
11007
11008 static void
11009 cp_parser_namespace_definition (cp_parser* parser)
11010 {
11011 tree identifier, attribs;
11012
11013 /* Look for the `namespace' keyword. */
11014 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11015
11016 /* Get the name of the namespace. We do not attempt to distinguish
11017 between an original-namespace-definition and an
11018 extension-namespace-definition at this point. The semantic
11019 analysis routines are responsible for that. */
11020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11021 identifier = cp_parser_identifier (parser);
11022 else
11023 identifier = NULL_TREE;
11024
11025 /* Parse any specified attributes. */
11026 attribs = cp_parser_attributes_opt (parser);
11027
11028 /* Look for the `{' to start the namespace. */
11029 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11030 /* Start the namespace. */
11031 push_namespace_with_attribs (identifier, attribs);
11032 /* Parse the body of the namespace. */
11033 cp_parser_namespace_body (parser);
11034 /* Finish the namespace. */
11035 pop_namespace ();
11036 /* Look for the final `}'. */
11037 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11038 }
11039
11040 /* Parse a namespace-body.
11041
11042 namespace-body:
11043 declaration-seq [opt] */
11044
11045 static void
11046 cp_parser_namespace_body (cp_parser* parser)
11047 {
11048 cp_parser_declaration_seq_opt (parser);
11049 }
11050
11051 /* Parse a namespace-alias-definition.
11052
11053 namespace-alias-definition:
11054 namespace identifier = qualified-namespace-specifier ; */
11055
11056 static void
11057 cp_parser_namespace_alias_definition (cp_parser* parser)
11058 {
11059 tree identifier;
11060 tree namespace_specifier;
11061
11062 /* Look for the `namespace' keyword. */
11063 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11064 /* Look for the identifier. */
11065 identifier = cp_parser_identifier (parser);
11066 if (identifier == error_mark_node)
11067 return;
11068 /* Look for the `=' token. */
11069 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11070 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11071 {
11072 error ("%<namespace%> definition is not allowed here");
11073 /* Skip the definition. */
11074 cp_lexer_consume_token (parser->lexer);
11075 cp_parser_skip_to_closing_brace (parser);
11076 cp_lexer_consume_token (parser->lexer);
11077 return;
11078 }
11079 cp_parser_require (parser, CPP_EQ, "`='");
11080 /* Look for the qualified-namespace-specifier. */
11081 namespace_specifier
11082 = cp_parser_qualified_namespace_specifier (parser);
11083 /* Look for the `;' token. */
11084 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11085
11086 /* Register the alias in the symbol table. */
11087 do_namespace_alias (identifier, namespace_specifier);
11088 }
11089
11090 /* Parse a qualified-namespace-specifier.
11091
11092 qualified-namespace-specifier:
11093 :: [opt] nested-name-specifier [opt] namespace-name
11094
11095 Returns a NAMESPACE_DECL corresponding to the specified
11096 namespace. */
11097
11098 static tree
11099 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11100 {
11101 /* Look for the optional `::'. */
11102 cp_parser_global_scope_opt (parser,
11103 /*current_scope_valid_p=*/false);
11104
11105 /* Look for the optional nested-name-specifier. */
11106 cp_parser_nested_name_specifier_opt (parser,
11107 /*typename_keyword_p=*/false,
11108 /*check_dependency_p=*/true,
11109 /*type_p=*/false,
11110 /*is_declaration=*/true);
11111
11112 return cp_parser_namespace_name (parser);
11113 }
11114
11115 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11116 access declaration.
11117
11118 using-declaration:
11119 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11120 using :: unqualified-id ;
11121
11122 access-declaration:
11123 qualified-id ;
11124
11125 */
11126
11127 static bool
11128 cp_parser_using_declaration (cp_parser* parser,
11129 bool access_declaration_p)
11130 {
11131 cp_token *token;
11132 bool typename_p = false;
11133 bool global_scope_p;
11134 tree decl;
11135 tree identifier;
11136 tree qscope;
11137
11138 if (access_declaration_p)
11139 cp_parser_parse_tentatively (parser);
11140 else
11141 {
11142 /* Look for the `using' keyword. */
11143 cp_parser_require_keyword (parser, RID_USING, "`using'");
11144
11145 /* Peek at the next token. */
11146 token = cp_lexer_peek_token (parser->lexer);
11147 /* See if it's `typename'. */
11148 if (token->keyword == RID_TYPENAME)
11149 {
11150 /* Remember that we've seen it. */
11151 typename_p = true;
11152 /* Consume the `typename' token. */
11153 cp_lexer_consume_token (parser->lexer);
11154 }
11155 }
11156
11157 /* Look for the optional global scope qualification. */
11158 global_scope_p
11159 = (cp_parser_global_scope_opt (parser,
11160 /*current_scope_valid_p=*/false)
11161 != NULL_TREE);
11162
11163 /* If we saw `typename', or didn't see `::', then there must be a
11164 nested-name-specifier present. */
11165 if (typename_p || !global_scope_p)
11166 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11167 /*check_dependency_p=*/true,
11168 /*type_p=*/false,
11169 /*is_declaration=*/true);
11170 /* Otherwise, we could be in either of the two productions. In that
11171 case, treat the nested-name-specifier as optional. */
11172 else
11173 qscope = cp_parser_nested_name_specifier_opt (parser,
11174 /*typename_keyword_p=*/false,
11175 /*check_dependency_p=*/true,
11176 /*type_p=*/false,
11177 /*is_declaration=*/true);
11178 if (!qscope)
11179 qscope = global_namespace;
11180
11181 if (access_declaration_p && cp_parser_error_occurred (parser))
11182 /* Something has already gone wrong; there's no need to parse
11183 further. Since an error has occurred, the return value of
11184 cp_parser_parse_definitely will be false, as required. */
11185 return cp_parser_parse_definitely (parser);
11186
11187 /* Parse the unqualified-id. */
11188 identifier = cp_parser_unqualified_id (parser,
11189 /*template_keyword_p=*/false,
11190 /*check_dependency_p=*/true,
11191 /*declarator_p=*/true,
11192 /*optional_p=*/false);
11193
11194 if (access_declaration_p)
11195 {
11196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11197 cp_parser_simulate_error (parser);
11198 if (!cp_parser_parse_definitely (parser))
11199 return false;
11200 }
11201
11202 /* The function we call to handle a using-declaration is different
11203 depending on what scope we are in. */
11204 if (qscope == error_mark_node || identifier == error_mark_node)
11205 ;
11206 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11207 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11208 /* [namespace.udecl]
11209
11210 A using declaration shall not name a template-id. */
11211 error ("a template-id may not appear in a using-declaration");
11212 else
11213 {
11214 if (at_class_scope_p ())
11215 {
11216 /* Create the USING_DECL. */
11217 decl = do_class_using_decl (parser->scope, identifier);
11218 /* Add it to the list of members in this class. */
11219 finish_member_declaration (decl);
11220 }
11221 else
11222 {
11223 decl = cp_parser_lookup_name_simple (parser, identifier);
11224 if (decl == error_mark_node)
11225 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11226 else if (!at_namespace_scope_p ())
11227 do_local_using_decl (decl, qscope, identifier);
11228 else
11229 do_toplevel_using_decl (decl, qscope, identifier);
11230 }
11231 }
11232
11233 /* Look for the final `;'. */
11234 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11235
11236 return true;
11237 }
11238
11239 /* Parse a using-directive.
11240
11241 using-directive:
11242 using namespace :: [opt] nested-name-specifier [opt]
11243 namespace-name ; */
11244
11245 static void
11246 cp_parser_using_directive (cp_parser* parser)
11247 {
11248 tree namespace_decl;
11249 tree attribs;
11250
11251 /* Look for the `using' keyword. */
11252 cp_parser_require_keyword (parser, RID_USING, "`using'");
11253 /* And the `namespace' keyword. */
11254 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11255 /* Look for the optional `::' operator. */
11256 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11257 /* And the optional nested-name-specifier. */
11258 cp_parser_nested_name_specifier_opt (parser,
11259 /*typename_keyword_p=*/false,
11260 /*check_dependency_p=*/true,
11261 /*type_p=*/false,
11262 /*is_declaration=*/true);
11263 /* Get the namespace being used. */
11264 namespace_decl = cp_parser_namespace_name (parser);
11265 /* And any specified attributes. */
11266 attribs = cp_parser_attributes_opt (parser);
11267 /* Update the symbol table. */
11268 parse_using_directive (namespace_decl, attribs);
11269 /* Look for the final `;'. */
11270 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11271 }
11272
11273 /* Parse an asm-definition.
11274
11275 asm-definition:
11276 asm ( string-literal ) ;
11277
11278 GNU Extension:
11279
11280 asm-definition:
11281 asm volatile [opt] ( string-literal ) ;
11282 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11283 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11284 : asm-operand-list [opt] ) ;
11285 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11286 : asm-operand-list [opt]
11287 : asm-operand-list [opt] ) ; */
11288
11289 static void
11290 cp_parser_asm_definition (cp_parser* parser)
11291 {
11292 tree string;
11293 tree outputs = NULL_TREE;
11294 tree inputs = NULL_TREE;
11295 tree clobbers = NULL_TREE;
11296 tree asm_stmt;
11297 bool volatile_p = false;
11298 bool extended_p = false;
11299
11300 /* Look for the `asm' keyword. */
11301 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11302 /* See if the next token is `volatile'. */
11303 if (cp_parser_allow_gnu_extensions_p (parser)
11304 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11305 {
11306 /* Remember that we saw the `volatile' keyword. */
11307 volatile_p = true;
11308 /* Consume the token. */
11309 cp_lexer_consume_token (parser->lexer);
11310 }
11311 /* Look for the opening `('. */
11312 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11313 return;
11314 /* Look for the string. */
11315 string = cp_parser_string_literal (parser, false, false);
11316 if (string == error_mark_node)
11317 {
11318 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11319 /*consume_paren=*/true);
11320 return;
11321 }
11322
11323 /* If we're allowing GNU extensions, check for the extended assembly
11324 syntax. Unfortunately, the `:' tokens need not be separated by
11325 a space in C, and so, for compatibility, we tolerate that here
11326 too. Doing that means that we have to treat the `::' operator as
11327 two `:' tokens. */
11328 if (cp_parser_allow_gnu_extensions_p (parser)
11329 && parser->in_function_body
11330 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11331 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11332 {
11333 bool inputs_p = false;
11334 bool clobbers_p = false;
11335
11336 /* The extended syntax was used. */
11337 extended_p = true;
11338
11339 /* Look for outputs. */
11340 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11341 {
11342 /* Consume the `:'. */
11343 cp_lexer_consume_token (parser->lexer);
11344 /* Parse the output-operands. */
11345 if (cp_lexer_next_token_is_not (parser->lexer,
11346 CPP_COLON)
11347 && cp_lexer_next_token_is_not (parser->lexer,
11348 CPP_SCOPE)
11349 && cp_lexer_next_token_is_not (parser->lexer,
11350 CPP_CLOSE_PAREN))
11351 outputs = cp_parser_asm_operand_list (parser);
11352 }
11353 /* If the next token is `::', there are no outputs, and the
11354 next token is the beginning of the inputs. */
11355 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11356 /* The inputs are coming next. */
11357 inputs_p = true;
11358
11359 /* Look for inputs. */
11360 if (inputs_p
11361 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11362 {
11363 /* Consume the `:' or `::'. */
11364 cp_lexer_consume_token (parser->lexer);
11365 /* Parse the output-operands. */
11366 if (cp_lexer_next_token_is_not (parser->lexer,
11367 CPP_COLON)
11368 && cp_lexer_next_token_is_not (parser->lexer,
11369 CPP_CLOSE_PAREN))
11370 inputs = cp_parser_asm_operand_list (parser);
11371 }
11372 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11373 /* The clobbers are coming next. */
11374 clobbers_p = true;
11375
11376 /* Look for clobbers. */
11377 if (clobbers_p
11378 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11379 {
11380 /* Consume the `:' or `::'. */
11381 cp_lexer_consume_token (parser->lexer);
11382 /* Parse the clobbers. */
11383 if (cp_lexer_next_token_is_not (parser->lexer,
11384 CPP_CLOSE_PAREN))
11385 clobbers = cp_parser_asm_clobber_list (parser);
11386 }
11387 }
11388 /* Look for the closing `)'. */
11389 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11390 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11391 /*consume_paren=*/true);
11392 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11393
11394 /* Create the ASM_EXPR. */
11395 if (parser->in_function_body)
11396 {
11397 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11398 inputs, clobbers);
11399 /* If the extended syntax was not used, mark the ASM_EXPR. */
11400 if (!extended_p)
11401 {
11402 tree temp = asm_stmt;
11403 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11404 temp = TREE_OPERAND (temp, 0);
11405
11406 ASM_INPUT_P (temp) = 1;
11407 }
11408 }
11409 else
11410 cgraph_add_asm_node (string);
11411 }
11412
11413 /* Declarators [gram.dcl.decl] */
11414
11415 /* Parse an init-declarator.
11416
11417 init-declarator:
11418 declarator initializer [opt]
11419
11420 GNU Extension:
11421
11422 init-declarator:
11423 declarator asm-specification [opt] attributes [opt] initializer [opt]
11424
11425 function-definition:
11426 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11427 function-body
11428 decl-specifier-seq [opt] declarator function-try-block
11429
11430 GNU Extension:
11431
11432 function-definition:
11433 __extension__ function-definition
11434
11435 The DECL_SPECIFIERS apply to this declarator. Returns a
11436 representation of the entity declared. If MEMBER_P is TRUE, then
11437 this declarator appears in a class scope. The new DECL created by
11438 this declarator is returned.
11439
11440 The CHECKS are access checks that should be performed once we know
11441 what entity is being declared (and, therefore, what classes have
11442 befriended it).
11443
11444 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11445 for a function-definition here as well. If the declarator is a
11446 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11447 be TRUE upon return. By that point, the function-definition will
11448 have been completely parsed.
11449
11450 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11451 is FALSE. */
11452
11453 static tree
11454 cp_parser_init_declarator (cp_parser* parser,
11455 cp_decl_specifier_seq *decl_specifiers,
11456 VEC (deferred_access_check,gc)* checks,
11457 bool function_definition_allowed_p,
11458 bool member_p,
11459 int declares_class_or_enum,
11460 bool* function_definition_p)
11461 {
11462 cp_token *token;
11463 cp_declarator *declarator;
11464 tree prefix_attributes;
11465 tree attributes;
11466 tree asm_specification;
11467 tree initializer;
11468 tree decl = NULL_TREE;
11469 tree scope;
11470 bool is_initialized;
11471 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11472 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11473 "(...)". */
11474 enum cpp_ttype initialization_kind;
11475 bool is_parenthesized_init = false;
11476 bool is_non_constant_init;
11477 int ctor_dtor_or_conv_p;
11478 bool friend_p;
11479 tree pushed_scope = NULL;
11480
11481 /* Gather the attributes that were provided with the
11482 decl-specifiers. */
11483 prefix_attributes = decl_specifiers->attributes;
11484
11485 /* Assume that this is not the declarator for a function
11486 definition. */
11487 if (function_definition_p)
11488 *function_definition_p = false;
11489
11490 /* Defer access checks while parsing the declarator; we cannot know
11491 what names are accessible until we know what is being
11492 declared. */
11493 resume_deferring_access_checks ();
11494
11495 /* Parse the declarator. */
11496 declarator
11497 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11498 &ctor_dtor_or_conv_p,
11499 /*parenthesized_p=*/NULL,
11500 /*member_p=*/false);
11501 /* Gather up the deferred checks. */
11502 stop_deferring_access_checks ();
11503
11504 /* If the DECLARATOR was erroneous, there's no need to go
11505 further. */
11506 if (declarator == cp_error_declarator)
11507 return error_mark_node;
11508
11509 /* Check that the number of template-parameter-lists is OK. */
11510 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11511 return error_mark_node;
11512
11513 if (declares_class_or_enum & 2)
11514 cp_parser_check_for_definition_in_return_type (declarator,
11515 decl_specifiers->type);
11516
11517 /* Figure out what scope the entity declared by the DECLARATOR is
11518 located in. `grokdeclarator' sometimes changes the scope, so
11519 we compute it now. */
11520 scope = get_scope_of_declarator (declarator);
11521
11522 /* If we're allowing GNU extensions, look for an asm-specification
11523 and attributes. */
11524 if (cp_parser_allow_gnu_extensions_p (parser))
11525 {
11526 /* Look for an asm-specification. */
11527 asm_specification = cp_parser_asm_specification_opt (parser);
11528 /* And attributes. */
11529 attributes = cp_parser_attributes_opt (parser);
11530 }
11531 else
11532 {
11533 asm_specification = NULL_TREE;
11534 attributes = NULL_TREE;
11535 }
11536
11537 /* Peek at the next token. */
11538 token = cp_lexer_peek_token (parser->lexer);
11539 /* Check to see if the token indicates the start of a
11540 function-definition. */
11541 if (cp_parser_token_starts_function_definition_p (token))
11542 {
11543 if (!function_definition_allowed_p)
11544 {
11545 /* If a function-definition should not appear here, issue an
11546 error message. */
11547 cp_parser_error (parser,
11548 "a function-definition is not allowed here");
11549 return error_mark_node;
11550 }
11551 else
11552 {
11553 /* Neither attributes nor an asm-specification are allowed
11554 on a function-definition. */
11555 if (asm_specification)
11556 error ("an asm-specification is not allowed on a function-definition");
11557 if (attributes)
11558 error ("attributes are not allowed on a function-definition");
11559 /* This is a function-definition. */
11560 *function_definition_p = true;
11561
11562 /* Parse the function definition. */
11563 if (member_p)
11564 decl = cp_parser_save_member_function_body (parser,
11565 decl_specifiers,
11566 declarator,
11567 prefix_attributes);
11568 else
11569 decl
11570 = (cp_parser_function_definition_from_specifiers_and_declarator
11571 (parser, decl_specifiers, prefix_attributes, declarator));
11572
11573 return decl;
11574 }
11575 }
11576
11577 /* [dcl.dcl]
11578
11579 Only in function declarations for constructors, destructors, and
11580 type conversions can the decl-specifier-seq be omitted.
11581
11582 We explicitly postpone this check past the point where we handle
11583 function-definitions because we tolerate function-definitions
11584 that are missing their return types in some modes. */
11585 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11586 {
11587 cp_parser_error (parser,
11588 "expected constructor, destructor, or type conversion");
11589 return error_mark_node;
11590 }
11591
11592 /* An `=' or an `(' indicates an initializer. */
11593 if (token->type == CPP_EQ
11594 || token->type == CPP_OPEN_PAREN)
11595 {
11596 is_initialized = true;
11597 initialization_kind = token->type;
11598 }
11599 else
11600 {
11601 /* If the init-declarator isn't initialized and isn't followed by a
11602 `,' or `;', it's not a valid init-declarator. */
11603 if (token->type != CPP_COMMA
11604 && token->type != CPP_SEMICOLON)
11605 {
11606 cp_parser_error (parser, "expected initializer");
11607 return error_mark_node;
11608 }
11609 is_initialized = false;
11610 initialization_kind = CPP_EOF;
11611 }
11612
11613 /* Because start_decl has side-effects, we should only call it if we
11614 know we're going ahead. By this point, we know that we cannot
11615 possibly be looking at any other construct. */
11616 cp_parser_commit_to_tentative_parse (parser);
11617
11618 /* If the decl specifiers were bad, issue an error now that we're
11619 sure this was intended to be a declarator. Then continue
11620 declaring the variable(s), as int, to try to cut down on further
11621 errors. */
11622 if (decl_specifiers->any_specifiers_p
11623 && decl_specifiers->type == error_mark_node)
11624 {
11625 cp_parser_error (parser, "invalid type in declaration");
11626 decl_specifiers->type = integer_type_node;
11627 }
11628
11629 /* Check to see whether or not this declaration is a friend. */
11630 friend_p = cp_parser_friend_p (decl_specifiers);
11631
11632 /* Enter the newly declared entry in the symbol table. If we're
11633 processing a declaration in a class-specifier, we wait until
11634 after processing the initializer. */
11635 if (!member_p)
11636 {
11637 if (parser->in_unbraced_linkage_specification_p)
11638 decl_specifiers->storage_class = sc_extern;
11639 decl = start_decl (declarator, decl_specifiers,
11640 is_initialized, attributes, prefix_attributes,
11641 &pushed_scope);
11642 }
11643 else if (scope)
11644 /* Enter the SCOPE. That way unqualified names appearing in the
11645 initializer will be looked up in SCOPE. */
11646 pushed_scope = push_scope (scope);
11647
11648 /* Perform deferred access control checks, now that we know in which
11649 SCOPE the declared entity resides. */
11650 if (!member_p && decl)
11651 {
11652 tree saved_current_function_decl = NULL_TREE;
11653
11654 /* If the entity being declared is a function, pretend that we
11655 are in its scope. If it is a `friend', it may have access to
11656 things that would not otherwise be accessible. */
11657 if (TREE_CODE (decl) == FUNCTION_DECL)
11658 {
11659 saved_current_function_decl = current_function_decl;
11660 current_function_decl = decl;
11661 }
11662
11663 /* Perform access checks for template parameters. */
11664 cp_parser_perform_template_parameter_access_checks (checks);
11665
11666 /* Perform the access control checks for the declarator and the
11667 the decl-specifiers. */
11668 perform_deferred_access_checks ();
11669
11670 /* Restore the saved value. */
11671 if (TREE_CODE (decl) == FUNCTION_DECL)
11672 current_function_decl = saved_current_function_decl;
11673 }
11674
11675 /* Parse the initializer. */
11676 initializer = NULL_TREE;
11677 is_parenthesized_init = false;
11678 is_non_constant_init = true;
11679 if (is_initialized)
11680 {
11681 if (function_declarator_p (declarator))
11682 {
11683 if (initialization_kind == CPP_EQ)
11684 initializer = cp_parser_pure_specifier (parser);
11685 else
11686 {
11687 /* If the declaration was erroneous, we don't really
11688 know what the user intended, so just silently
11689 consume the initializer. */
11690 if (decl != error_mark_node)
11691 error ("initializer provided for function");
11692 cp_parser_skip_to_closing_parenthesis (parser,
11693 /*recovering=*/true,
11694 /*or_comma=*/false,
11695 /*consume_paren=*/true);
11696 }
11697 }
11698 else
11699 initializer = cp_parser_initializer (parser,
11700 &is_parenthesized_init,
11701 &is_non_constant_init);
11702 }
11703
11704 /* The old parser allows attributes to appear after a parenthesized
11705 initializer. Mark Mitchell proposed removing this functionality
11706 on the GCC mailing lists on 2002-08-13. This parser accepts the
11707 attributes -- but ignores them. */
11708 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11709 if (cp_parser_attributes_opt (parser))
11710 warning (OPT_Wattributes,
11711 "attributes after parenthesized initializer ignored");
11712
11713 /* For an in-class declaration, use `grokfield' to create the
11714 declaration. */
11715 if (member_p)
11716 {
11717 if (pushed_scope)
11718 {
11719 pop_scope (pushed_scope);
11720 pushed_scope = false;
11721 }
11722 decl = grokfield (declarator, decl_specifiers,
11723 initializer, !is_non_constant_init,
11724 /*asmspec=*/NULL_TREE,
11725 prefix_attributes);
11726 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11727 cp_parser_save_default_args (parser, decl);
11728 }
11729
11730 /* Finish processing the declaration. But, skip friend
11731 declarations. */
11732 if (!friend_p && decl && decl != error_mark_node)
11733 {
11734 cp_finish_decl (decl,
11735 initializer, !is_non_constant_init,
11736 asm_specification,
11737 /* If the initializer is in parentheses, then this is
11738 a direct-initialization, which means that an
11739 `explicit' constructor is OK. Otherwise, an
11740 `explicit' constructor cannot be used. */
11741 ((is_parenthesized_init || !is_initialized)
11742 ? 0 : LOOKUP_ONLYCONVERTING));
11743 }
11744 else if (flag_cpp0x && friend_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
11745 /* Core issue #226 (C++0x only): A default template-argument
11746 shall not be specified in a friend class template
11747 declaration. */
11748 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
11749 /*is_partial=*/0, /*is_friend_decl=*/1);
11750
11751 if (!friend_p && pushed_scope)
11752 pop_scope (pushed_scope);
11753
11754 return decl;
11755 }
11756
11757 /* Parse a declarator.
11758
11759 declarator:
11760 direct-declarator
11761 ptr-operator declarator
11762
11763 abstract-declarator:
11764 ptr-operator abstract-declarator [opt]
11765 direct-abstract-declarator
11766
11767 GNU Extensions:
11768
11769 declarator:
11770 attributes [opt] direct-declarator
11771 attributes [opt] ptr-operator declarator
11772
11773 abstract-declarator:
11774 attributes [opt] ptr-operator abstract-declarator [opt]
11775 attributes [opt] direct-abstract-declarator
11776
11777 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11778 detect constructor, destructor or conversion operators. It is set
11779 to -1 if the declarator is a name, and +1 if it is a
11780 function. Otherwise it is set to zero. Usually you just want to
11781 test for >0, but internally the negative value is used.
11782
11783 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11784 a decl-specifier-seq unless it declares a constructor, destructor,
11785 or conversion. It might seem that we could check this condition in
11786 semantic analysis, rather than parsing, but that makes it difficult
11787 to handle something like `f()'. We want to notice that there are
11788 no decl-specifiers, and therefore realize that this is an
11789 expression, not a declaration.)
11790
11791 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11792 the declarator is a direct-declarator of the form "(...)".
11793
11794 MEMBER_P is true iff this declarator is a member-declarator. */
11795
11796 static cp_declarator *
11797 cp_parser_declarator (cp_parser* parser,
11798 cp_parser_declarator_kind dcl_kind,
11799 int* ctor_dtor_or_conv_p,
11800 bool* parenthesized_p,
11801 bool member_p)
11802 {
11803 cp_token *token;
11804 cp_declarator *declarator;
11805 enum tree_code code;
11806 cp_cv_quals cv_quals;
11807 tree class_type;
11808 tree attributes = NULL_TREE;
11809
11810 /* Assume this is not a constructor, destructor, or type-conversion
11811 operator. */
11812 if (ctor_dtor_or_conv_p)
11813 *ctor_dtor_or_conv_p = 0;
11814
11815 if (cp_parser_allow_gnu_extensions_p (parser))
11816 attributes = cp_parser_attributes_opt (parser);
11817
11818 /* Peek at the next token. */
11819 token = cp_lexer_peek_token (parser->lexer);
11820
11821 /* Check for the ptr-operator production. */
11822 cp_parser_parse_tentatively (parser);
11823 /* Parse the ptr-operator. */
11824 code = cp_parser_ptr_operator (parser,
11825 &class_type,
11826 &cv_quals);
11827 /* If that worked, then we have a ptr-operator. */
11828 if (cp_parser_parse_definitely (parser))
11829 {
11830 /* If a ptr-operator was found, then this declarator was not
11831 parenthesized. */
11832 if (parenthesized_p)
11833 *parenthesized_p = true;
11834 /* The dependent declarator is optional if we are parsing an
11835 abstract-declarator. */
11836 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11837 cp_parser_parse_tentatively (parser);
11838
11839 /* Parse the dependent declarator. */
11840 declarator = cp_parser_declarator (parser, dcl_kind,
11841 /*ctor_dtor_or_conv_p=*/NULL,
11842 /*parenthesized_p=*/NULL,
11843 /*member_p=*/false);
11844
11845 /* If we are parsing an abstract-declarator, we must handle the
11846 case where the dependent declarator is absent. */
11847 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11848 && !cp_parser_parse_definitely (parser))
11849 declarator = NULL;
11850
11851 /* Build the representation of the ptr-operator. */
11852 if (class_type)
11853 declarator = make_ptrmem_declarator (cv_quals,
11854 class_type,
11855 declarator);
11856 else if (code == INDIRECT_REF)
11857 declarator = make_pointer_declarator (cv_quals, declarator);
11858 else
11859 declarator = make_reference_declarator (cv_quals, declarator);
11860 }
11861 /* Everything else is a direct-declarator. */
11862 else
11863 {
11864 if (parenthesized_p)
11865 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11866 CPP_OPEN_PAREN);
11867 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11868 ctor_dtor_or_conv_p,
11869 member_p);
11870 }
11871
11872 if (attributes && declarator && declarator != cp_error_declarator)
11873 declarator->attributes = attributes;
11874
11875 return declarator;
11876 }
11877
11878 /* Parse a direct-declarator or direct-abstract-declarator.
11879
11880 direct-declarator:
11881 declarator-id
11882 direct-declarator ( parameter-declaration-clause )
11883 cv-qualifier-seq [opt]
11884 exception-specification [opt]
11885 direct-declarator [ constant-expression [opt] ]
11886 ( declarator )
11887
11888 direct-abstract-declarator:
11889 direct-abstract-declarator [opt]
11890 ( parameter-declaration-clause )
11891 cv-qualifier-seq [opt]
11892 exception-specification [opt]
11893 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11894 ( abstract-declarator )
11895
11896 Returns a representation of the declarator. DCL_KIND is
11897 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11898 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11899 we are parsing a direct-declarator. It is
11900 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11901 of ambiguity we prefer an abstract declarator, as per
11902 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11903 cp_parser_declarator. */
11904
11905 static cp_declarator *
11906 cp_parser_direct_declarator (cp_parser* parser,
11907 cp_parser_declarator_kind dcl_kind,
11908 int* ctor_dtor_or_conv_p,
11909 bool member_p)
11910 {
11911 cp_token *token;
11912 cp_declarator *declarator = NULL;
11913 tree scope = NULL_TREE;
11914 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11915 bool saved_in_declarator_p = parser->in_declarator_p;
11916 bool first = true;
11917 tree pushed_scope = NULL_TREE;
11918
11919 while (true)
11920 {
11921 /* Peek at the next token. */
11922 token = cp_lexer_peek_token (parser->lexer);
11923 if (token->type == CPP_OPEN_PAREN)
11924 {
11925 /* This is either a parameter-declaration-clause, or a
11926 parenthesized declarator. When we know we are parsing a
11927 named declarator, it must be a parenthesized declarator
11928 if FIRST is true. For instance, `(int)' is a
11929 parameter-declaration-clause, with an omitted
11930 direct-abstract-declarator. But `((*))', is a
11931 parenthesized abstract declarator. Finally, when T is a
11932 template parameter `(T)' is a
11933 parameter-declaration-clause, and not a parenthesized
11934 named declarator.
11935
11936 We first try and parse a parameter-declaration-clause,
11937 and then try a nested declarator (if FIRST is true).
11938
11939 It is not an error for it not to be a
11940 parameter-declaration-clause, even when FIRST is
11941 false. Consider,
11942
11943 int i (int);
11944 int i (3);
11945
11946 The first is the declaration of a function while the
11947 second is a the definition of a variable, including its
11948 initializer.
11949
11950 Having seen only the parenthesis, we cannot know which of
11951 these two alternatives should be selected. Even more
11952 complex are examples like:
11953
11954 int i (int (a));
11955 int i (int (3));
11956
11957 The former is a function-declaration; the latter is a
11958 variable initialization.
11959
11960 Thus again, we try a parameter-declaration-clause, and if
11961 that fails, we back out and return. */
11962
11963 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11964 {
11965 cp_parameter_declarator *params;
11966 unsigned saved_num_template_parameter_lists;
11967
11968 /* In a member-declarator, the only valid interpretation
11969 of a parenthesis is the start of a
11970 parameter-declaration-clause. (It is invalid to
11971 initialize a static data member with a parenthesized
11972 initializer; only the "=" form of initialization is
11973 permitted.) */
11974 if (!member_p)
11975 cp_parser_parse_tentatively (parser);
11976
11977 /* Consume the `('. */
11978 cp_lexer_consume_token (parser->lexer);
11979 if (first)
11980 {
11981 /* If this is going to be an abstract declarator, we're
11982 in a declarator and we can't have default args. */
11983 parser->default_arg_ok_p = false;
11984 parser->in_declarator_p = true;
11985 }
11986
11987 /* Inside the function parameter list, surrounding
11988 template-parameter-lists do not apply. */
11989 saved_num_template_parameter_lists
11990 = parser->num_template_parameter_lists;
11991 parser->num_template_parameter_lists = 0;
11992
11993 /* Parse the parameter-declaration-clause. */
11994 params = cp_parser_parameter_declaration_clause (parser);
11995
11996 parser->num_template_parameter_lists
11997 = saved_num_template_parameter_lists;
11998
11999 /* If all went well, parse the cv-qualifier-seq and the
12000 exception-specification. */
12001 if (member_p || cp_parser_parse_definitely (parser))
12002 {
12003 cp_cv_quals cv_quals;
12004 tree exception_specification;
12005
12006 if (ctor_dtor_or_conv_p)
12007 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12008 first = false;
12009 /* Consume the `)'. */
12010 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12011
12012 /* Parse the cv-qualifier-seq. */
12013 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12014 /* And the exception-specification. */
12015 exception_specification
12016 = cp_parser_exception_specification_opt (parser);
12017
12018 /* Create the function-declarator. */
12019 declarator = make_call_declarator (declarator,
12020 params,
12021 cv_quals,
12022 exception_specification);
12023 /* Any subsequent parameter lists are to do with
12024 return type, so are not those of the declared
12025 function. */
12026 parser->default_arg_ok_p = false;
12027
12028 /* Repeat the main loop. */
12029 continue;
12030 }
12031 }
12032
12033 /* If this is the first, we can try a parenthesized
12034 declarator. */
12035 if (first)
12036 {
12037 bool saved_in_type_id_in_expr_p;
12038
12039 parser->default_arg_ok_p = saved_default_arg_ok_p;
12040 parser->in_declarator_p = saved_in_declarator_p;
12041
12042 /* Consume the `('. */
12043 cp_lexer_consume_token (parser->lexer);
12044 /* Parse the nested declarator. */
12045 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12046 parser->in_type_id_in_expr_p = true;
12047 declarator
12048 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12049 /*parenthesized_p=*/NULL,
12050 member_p);
12051 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12052 first = false;
12053 /* Expect a `)'. */
12054 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12055 declarator = cp_error_declarator;
12056 if (declarator == cp_error_declarator)
12057 break;
12058
12059 goto handle_declarator;
12060 }
12061 /* Otherwise, we must be done. */
12062 else
12063 break;
12064 }
12065 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12066 && token->type == CPP_OPEN_SQUARE)
12067 {
12068 /* Parse an array-declarator. */
12069 tree bounds;
12070
12071 if (ctor_dtor_or_conv_p)
12072 *ctor_dtor_or_conv_p = 0;
12073
12074 first = false;
12075 parser->default_arg_ok_p = false;
12076 parser->in_declarator_p = true;
12077 /* Consume the `['. */
12078 cp_lexer_consume_token (parser->lexer);
12079 /* Peek at the next token. */
12080 token = cp_lexer_peek_token (parser->lexer);
12081 /* If the next token is `]', then there is no
12082 constant-expression. */
12083 if (token->type != CPP_CLOSE_SQUARE)
12084 {
12085 bool non_constant_p;
12086
12087 bounds
12088 = cp_parser_constant_expression (parser,
12089 /*allow_non_constant=*/true,
12090 &non_constant_p);
12091 if (!non_constant_p)
12092 bounds = fold_non_dependent_expr (bounds);
12093 /* Normally, the array bound must be an integral constant
12094 expression. However, as an extension, we allow VLAs
12095 in function scopes. */
12096 else if (!parser->in_function_body)
12097 {
12098 error ("array bound is not an integer constant");
12099 bounds = error_mark_node;
12100 }
12101 }
12102 else
12103 bounds = NULL_TREE;
12104 /* Look for the closing `]'. */
12105 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12106 {
12107 declarator = cp_error_declarator;
12108 break;
12109 }
12110
12111 declarator = make_array_declarator (declarator, bounds);
12112 }
12113 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12114 {
12115 tree qualifying_scope;
12116 tree unqualified_name;
12117 special_function_kind sfk;
12118 bool abstract_ok;
12119 bool pack_expansion_p = false;
12120
12121 /* Parse a declarator-id */
12122 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12123 if (abstract_ok)
12124 {
12125 cp_parser_parse_tentatively (parser);
12126
12127 /* If we see an ellipsis, we should be looking at a
12128 parameter pack. */
12129 if (token->type == CPP_ELLIPSIS)
12130 {
12131 /* Consume the `...' */
12132 cp_lexer_consume_token (parser->lexer);
12133
12134 pack_expansion_p = true;
12135 }
12136 }
12137
12138 unqualified_name
12139 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12140 qualifying_scope = parser->scope;
12141 if (abstract_ok)
12142 {
12143 bool okay = false;
12144
12145 if (!unqualified_name && pack_expansion_p)
12146 {
12147 /* Check whether an error occurred. */
12148 okay = !cp_parser_error_occurred (parser);
12149
12150 /* We already consumed the ellipsis to mark a
12151 parameter pack, but we have no way to report it,
12152 so abort the tentative parse. We will be exiting
12153 immediately anyway. */
12154 cp_parser_abort_tentative_parse (parser);
12155 }
12156 else
12157 okay = cp_parser_parse_definitely (parser);
12158
12159 if (!okay)
12160 unqualified_name = error_mark_node;
12161 else if (unqualified_name
12162 && (qualifying_scope
12163 || (TREE_CODE (unqualified_name)
12164 != IDENTIFIER_NODE)))
12165 {
12166 cp_parser_error (parser, "expected unqualified-id");
12167 unqualified_name = error_mark_node;
12168 }
12169 }
12170
12171 if (!unqualified_name)
12172 return NULL;
12173 if (unqualified_name == error_mark_node)
12174 {
12175 declarator = cp_error_declarator;
12176 pack_expansion_p = false;
12177 declarator->parameter_pack_p = false;
12178 break;
12179 }
12180
12181 if (qualifying_scope && at_namespace_scope_p ()
12182 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12183 {
12184 /* In the declaration of a member of a template class
12185 outside of the class itself, the SCOPE will sometimes
12186 be a TYPENAME_TYPE. For example, given:
12187
12188 template <typename T>
12189 int S<T>::R::i = 3;
12190
12191 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12192 this context, we must resolve S<T>::R to an ordinary
12193 type, rather than a typename type.
12194
12195 The reason we normally avoid resolving TYPENAME_TYPEs
12196 is that a specialization of `S' might render
12197 `S<T>::R' not a type. However, if `S' is
12198 specialized, then this `i' will not be used, so there
12199 is no harm in resolving the types here. */
12200 tree type;
12201
12202 /* Resolve the TYPENAME_TYPE. */
12203 type = resolve_typename_type (qualifying_scope,
12204 /*only_current_p=*/false);
12205 /* If that failed, the declarator is invalid. */
12206 if (type == error_mark_node)
12207 error ("%<%T::%E%> is not a type",
12208 TYPE_CONTEXT (qualifying_scope),
12209 TYPE_IDENTIFIER (qualifying_scope));
12210 qualifying_scope = type;
12211 }
12212
12213 sfk = sfk_none;
12214
12215 if (unqualified_name)
12216 {
12217 tree class_type;
12218
12219 if (qualifying_scope
12220 && CLASS_TYPE_P (qualifying_scope))
12221 class_type = qualifying_scope;
12222 else
12223 class_type = current_class_type;
12224
12225 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12226 {
12227 tree name_type = TREE_TYPE (unqualified_name);
12228 if (class_type && same_type_p (name_type, class_type))
12229 {
12230 if (qualifying_scope
12231 && CLASSTYPE_USE_TEMPLATE (name_type))
12232 {
12233 error ("invalid use of constructor as a template");
12234 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12235 "name the constructor in a qualified name",
12236 class_type,
12237 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12238 class_type, name_type);
12239 declarator = cp_error_declarator;
12240 break;
12241 }
12242 else
12243 unqualified_name = constructor_name (class_type);
12244 }
12245 else
12246 {
12247 /* We do not attempt to print the declarator
12248 here because we do not have enough
12249 information about its original syntactic
12250 form. */
12251 cp_parser_error (parser, "invalid declarator");
12252 declarator = cp_error_declarator;
12253 break;
12254 }
12255 }
12256
12257 if (class_type)
12258 {
12259 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12260 sfk = sfk_destructor;
12261 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12262 sfk = sfk_conversion;
12263 else if (/* There's no way to declare a constructor
12264 for an anonymous type, even if the type
12265 got a name for linkage purposes. */
12266 !TYPE_WAS_ANONYMOUS (class_type)
12267 && constructor_name_p (unqualified_name,
12268 class_type))
12269 {
12270 unqualified_name = constructor_name (class_type);
12271 sfk = sfk_constructor;
12272 }
12273
12274 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12275 *ctor_dtor_or_conv_p = -1;
12276 }
12277 }
12278 declarator = make_id_declarator (qualifying_scope,
12279 unqualified_name,
12280 sfk);
12281 declarator->id_loc = token->location;
12282 declarator->parameter_pack_p = pack_expansion_p;
12283
12284 if (pack_expansion_p)
12285 maybe_warn_variadic_templates ();
12286
12287 handle_declarator:;
12288 scope = get_scope_of_declarator (declarator);
12289 if (scope)
12290 /* Any names that appear after the declarator-id for a
12291 member are looked up in the containing scope. */
12292 pushed_scope = push_scope (scope);
12293 parser->in_declarator_p = true;
12294 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12295 || (declarator && declarator->kind == cdk_id))
12296 /* Default args are only allowed on function
12297 declarations. */
12298 parser->default_arg_ok_p = saved_default_arg_ok_p;
12299 else
12300 parser->default_arg_ok_p = false;
12301
12302 first = false;
12303 }
12304 /* We're done. */
12305 else
12306 break;
12307 }
12308
12309 /* For an abstract declarator, we might wind up with nothing at this
12310 point. That's an error; the declarator is not optional. */
12311 if (!declarator)
12312 cp_parser_error (parser, "expected declarator");
12313
12314 /* If we entered a scope, we must exit it now. */
12315 if (pushed_scope)
12316 pop_scope (pushed_scope);
12317
12318 parser->default_arg_ok_p = saved_default_arg_ok_p;
12319 parser->in_declarator_p = saved_in_declarator_p;
12320
12321 return declarator;
12322 }
12323
12324 /* Parse a ptr-operator.
12325
12326 ptr-operator:
12327 * cv-qualifier-seq [opt]
12328 &
12329 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12330
12331 GNU Extension:
12332
12333 ptr-operator:
12334 & cv-qualifier-seq [opt]
12335
12336 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12337 Returns ADDR_EXPR if a reference was used. In the case of a
12338 pointer-to-member, *TYPE is filled in with the TYPE containing the
12339 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12340 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12341 ERROR_MARK if an error occurred. */
12342
12343 static enum tree_code
12344 cp_parser_ptr_operator (cp_parser* parser,
12345 tree* type,
12346 cp_cv_quals *cv_quals)
12347 {
12348 enum tree_code code = ERROR_MARK;
12349 cp_token *token;
12350
12351 /* Assume that it's not a pointer-to-member. */
12352 *type = NULL_TREE;
12353 /* And that there are no cv-qualifiers. */
12354 *cv_quals = TYPE_UNQUALIFIED;
12355
12356 /* Peek at the next token. */
12357 token = cp_lexer_peek_token (parser->lexer);
12358 /* If it's a `*' or `&' we have a pointer or reference. */
12359 if (token->type == CPP_MULT || token->type == CPP_AND)
12360 {
12361 /* Remember which ptr-operator we were processing. */
12362 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12363
12364 /* Consume the `*' or `&'. */
12365 cp_lexer_consume_token (parser->lexer);
12366
12367 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12368 `&', if we are allowing GNU extensions. (The only qualifier
12369 that can legally appear after `&' is `restrict', but that is
12370 enforced during semantic analysis. */
12371 if (code == INDIRECT_REF
12372 || cp_parser_allow_gnu_extensions_p (parser))
12373 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12374 }
12375 else
12376 {
12377 /* Try the pointer-to-member case. */
12378 cp_parser_parse_tentatively (parser);
12379 /* Look for the optional `::' operator. */
12380 cp_parser_global_scope_opt (parser,
12381 /*current_scope_valid_p=*/false);
12382 /* Look for the nested-name specifier. */
12383 cp_parser_nested_name_specifier (parser,
12384 /*typename_keyword_p=*/false,
12385 /*check_dependency_p=*/true,
12386 /*type_p=*/false,
12387 /*is_declaration=*/false);
12388 /* If we found it, and the next token is a `*', then we are
12389 indeed looking at a pointer-to-member operator. */
12390 if (!cp_parser_error_occurred (parser)
12391 && cp_parser_require (parser, CPP_MULT, "`*'"))
12392 {
12393 /* Indicate that the `*' operator was used. */
12394 code = INDIRECT_REF;
12395
12396 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12397 error ("%qD is a namespace", parser->scope);
12398 else
12399 {
12400 /* The type of which the member is a member is given by the
12401 current SCOPE. */
12402 *type = parser->scope;
12403 /* The next name will not be qualified. */
12404 parser->scope = NULL_TREE;
12405 parser->qualifying_scope = NULL_TREE;
12406 parser->object_scope = NULL_TREE;
12407 /* Look for the optional cv-qualifier-seq. */
12408 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12409 }
12410 }
12411 /* If that didn't work we don't have a ptr-operator. */
12412 if (!cp_parser_parse_definitely (parser))
12413 cp_parser_error (parser, "expected ptr-operator");
12414 }
12415
12416 return code;
12417 }
12418
12419 /* Parse an (optional) cv-qualifier-seq.
12420
12421 cv-qualifier-seq:
12422 cv-qualifier cv-qualifier-seq [opt]
12423
12424 cv-qualifier:
12425 const
12426 volatile
12427
12428 GNU Extension:
12429
12430 cv-qualifier:
12431 __restrict__
12432
12433 Returns a bitmask representing the cv-qualifiers. */
12434
12435 static cp_cv_quals
12436 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12437 {
12438 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12439
12440 while (true)
12441 {
12442 cp_token *token;
12443 cp_cv_quals cv_qualifier;
12444
12445 /* Peek at the next token. */
12446 token = cp_lexer_peek_token (parser->lexer);
12447 /* See if it's a cv-qualifier. */
12448 switch (token->keyword)
12449 {
12450 case RID_CONST:
12451 cv_qualifier = TYPE_QUAL_CONST;
12452 break;
12453
12454 case RID_VOLATILE:
12455 cv_qualifier = TYPE_QUAL_VOLATILE;
12456 break;
12457
12458 case RID_RESTRICT:
12459 cv_qualifier = TYPE_QUAL_RESTRICT;
12460 break;
12461
12462 default:
12463 cv_qualifier = TYPE_UNQUALIFIED;
12464 break;
12465 }
12466
12467 if (!cv_qualifier)
12468 break;
12469
12470 if (cv_quals & cv_qualifier)
12471 {
12472 error ("duplicate cv-qualifier");
12473 cp_lexer_purge_token (parser->lexer);
12474 }
12475 else
12476 {
12477 cp_lexer_consume_token (parser->lexer);
12478 cv_quals |= cv_qualifier;
12479 }
12480 }
12481
12482 return cv_quals;
12483 }
12484
12485 /* Parse a declarator-id.
12486
12487 declarator-id:
12488 id-expression
12489 :: [opt] nested-name-specifier [opt] type-name
12490
12491 In the `id-expression' case, the value returned is as for
12492 cp_parser_id_expression if the id-expression was an unqualified-id.
12493 If the id-expression was a qualified-id, then a SCOPE_REF is
12494 returned. The first operand is the scope (either a NAMESPACE_DECL
12495 or TREE_TYPE), but the second is still just a representation of an
12496 unqualified-id. */
12497
12498 static tree
12499 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12500 {
12501 tree id;
12502 /* The expression must be an id-expression. Assume that qualified
12503 names are the names of types so that:
12504
12505 template <class T>
12506 int S<T>::R::i = 3;
12507
12508 will work; we must treat `S<T>::R' as the name of a type.
12509 Similarly, assume that qualified names are templates, where
12510 required, so that:
12511
12512 template <class T>
12513 int S<T>::R<T>::i = 3;
12514
12515 will work, too. */
12516 id = cp_parser_id_expression (parser,
12517 /*template_keyword_p=*/false,
12518 /*check_dependency_p=*/false,
12519 /*template_p=*/NULL,
12520 /*declarator_p=*/true,
12521 optional_p);
12522 if (id && BASELINK_P (id))
12523 id = BASELINK_FUNCTIONS (id);
12524 return id;
12525 }
12526
12527 /* Parse a type-id.
12528
12529 type-id:
12530 type-specifier-seq abstract-declarator [opt]
12531
12532 Returns the TYPE specified. */
12533
12534 static tree
12535 cp_parser_type_id (cp_parser* parser)
12536 {
12537 cp_decl_specifier_seq type_specifier_seq;
12538 cp_declarator *abstract_declarator;
12539
12540 /* Parse the type-specifier-seq. */
12541 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12542 &type_specifier_seq);
12543 if (type_specifier_seq.type == error_mark_node)
12544 return error_mark_node;
12545
12546 /* There might or might not be an abstract declarator. */
12547 cp_parser_parse_tentatively (parser);
12548 /* Look for the declarator. */
12549 abstract_declarator
12550 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12551 /*parenthesized_p=*/NULL,
12552 /*member_p=*/false);
12553 /* Check to see if there really was a declarator. */
12554 if (!cp_parser_parse_definitely (parser))
12555 abstract_declarator = NULL;
12556
12557 return groktypename (&type_specifier_seq, abstract_declarator);
12558 }
12559
12560 /* Parse a type-specifier-seq.
12561
12562 type-specifier-seq:
12563 type-specifier type-specifier-seq [opt]
12564
12565 GNU extension:
12566
12567 type-specifier-seq:
12568 attributes type-specifier-seq [opt]
12569
12570 If IS_CONDITION is true, we are at the start of a "condition",
12571 e.g., we've just seen "if (".
12572
12573 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12574
12575 static void
12576 cp_parser_type_specifier_seq (cp_parser* parser,
12577 bool is_condition,
12578 cp_decl_specifier_seq *type_specifier_seq)
12579 {
12580 bool seen_type_specifier = false;
12581 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12582
12583 /* Clear the TYPE_SPECIFIER_SEQ. */
12584 clear_decl_specs (type_specifier_seq);
12585
12586 /* Parse the type-specifiers and attributes. */
12587 while (true)
12588 {
12589 tree type_specifier;
12590 bool is_cv_qualifier;
12591
12592 /* Check for attributes first. */
12593 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12594 {
12595 type_specifier_seq->attributes =
12596 chainon (type_specifier_seq->attributes,
12597 cp_parser_attributes_opt (parser));
12598 continue;
12599 }
12600
12601 /* Look for the type-specifier. */
12602 type_specifier = cp_parser_type_specifier (parser,
12603 flags,
12604 type_specifier_seq,
12605 /*is_declaration=*/false,
12606 NULL,
12607 &is_cv_qualifier);
12608 if (!type_specifier)
12609 {
12610 /* If the first type-specifier could not be found, this is not a
12611 type-specifier-seq at all. */
12612 if (!seen_type_specifier)
12613 {
12614 cp_parser_error (parser, "expected type-specifier");
12615 type_specifier_seq->type = error_mark_node;
12616 return;
12617 }
12618 /* If subsequent type-specifiers could not be found, the
12619 type-specifier-seq is complete. */
12620 break;
12621 }
12622
12623 seen_type_specifier = true;
12624 /* The standard says that a condition can be:
12625
12626 type-specifier-seq declarator = assignment-expression
12627
12628 However, given:
12629
12630 struct S {};
12631 if (int S = ...)
12632
12633 we should treat the "S" as a declarator, not as a
12634 type-specifier. The standard doesn't say that explicitly for
12635 type-specifier-seq, but it does say that for
12636 decl-specifier-seq in an ordinary declaration. Perhaps it
12637 would be clearer just to allow a decl-specifier-seq here, and
12638 then add a semantic restriction that if any decl-specifiers
12639 that are not type-specifiers appear, the program is invalid. */
12640 if (is_condition && !is_cv_qualifier)
12641 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12642 }
12643
12644 cp_parser_check_decl_spec (type_specifier_seq);
12645 }
12646
12647 /* Parse a parameter-declaration-clause.
12648
12649 parameter-declaration-clause:
12650 parameter-declaration-list [opt] ... [opt]
12651 parameter-declaration-list , ...
12652
12653 Returns a representation for the parameter declarations. A return
12654 value of NULL indicates a parameter-declaration-clause consisting
12655 only of an ellipsis. */
12656
12657 static cp_parameter_declarator *
12658 cp_parser_parameter_declaration_clause (cp_parser* parser)
12659 {
12660 cp_parameter_declarator *parameters;
12661 cp_token *token;
12662 bool ellipsis_p;
12663 bool is_error;
12664
12665 /* Peek at the next token. */
12666 token = cp_lexer_peek_token (parser->lexer);
12667 /* Check for trivial parameter-declaration-clauses. */
12668 if (token->type == CPP_ELLIPSIS)
12669 {
12670 /* Consume the `...' token. */
12671 cp_lexer_consume_token (parser->lexer);
12672 return NULL;
12673 }
12674 else if (token->type == CPP_CLOSE_PAREN)
12675 /* There are no parameters. */
12676 {
12677 #ifndef NO_IMPLICIT_EXTERN_C
12678 if (in_system_header && current_class_type == NULL
12679 && current_lang_name == lang_name_c)
12680 return NULL;
12681 else
12682 #endif
12683 return no_parameters;
12684 }
12685 /* Check for `(void)', too, which is a special case. */
12686 else if (token->keyword == RID_VOID
12687 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12688 == CPP_CLOSE_PAREN))
12689 {
12690 /* Consume the `void' token. */
12691 cp_lexer_consume_token (parser->lexer);
12692 /* There are no parameters. */
12693 return no_parameters;
12694 }
12695
12696 /* Parse the parameter-declaration-list. */
12697 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12698 /* If a parse error occurred while parsing the
12699 parameter-declaration-list, then the entire
12700 parameter-declaration-clause is erroneous. */
12701 if (is_error)
12702 return NULL;
12703
12704 /* Peek at the next token. */
12705 token = cp_lexer_peek_token (parser->lexer);
12706 /* If it's a `,', the clause should terminate with an ellipsis. */
12707 if (token->type == CPP_COMMA)
12708 {
12709 /* Consume the `,'. */
12710 cp_lexer_consume_token (parser->lexer);
12711 /* Expect an ellipsis. */
12712 ellipsis_p
12713 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12714 }
12715 /* It might also be `...' if the optional trailing `,' was
12716 omitted. */
12717 else if (token->type == CPP_ELLIPSIS)
12718 {
12719 /* Consume the `...' token. */
12720 cp_lexer_consume_token (parser->lexer);
12721 /* And remember that we saw it. */
12722 ellipsis_p = true;
12723 }
12724 else
12725 ellipsis_p = false;
12726
12727 /* Finish the parameter list. */
12728 if (parameters && ellipsis_p)
12729 parameters->ellipsis_p = true;
12730
12731 return parameters;
12732 }
12733
12734 /* Parse a parameter-declaration-list.
12735
12736 parameter-declaration-list:
12737 parameter-declaration
12738 parameter-declaration-list , parameter-declaration
12739
12740 Returns a representation of the parameter-declaration-list, as for
12741 cp_parser_parameter_declaration_clause. However, the
12742 `void_list_node' is never appended to the list. Upon return,
12743 *IS_ERROR will be true iff an error occurred. */
12744
12745 static cp_parameter_declarator *
12746 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12747 {
12748 cp_parameter_declarator *parameters = NULL;
12749 cp_parameter_declarator **tail = &parameters;
12750 bool saved_in_unbraced_linkage_specification_p;
12751
12752 /* Assume all will go well. */
12753 *is_error = false;
12754 /* The special considerations that apply to a function within an
12755 unbraced linkage specifications do not apply to the parameters
12756 to the function. */
12757 saved_in_unbraced_linkage_specification_p
12758 = parser->in_unbraced_linkage_specification_p;
12759 parser->in_unbraced_linkage_specification_p = false;
12760
12761 /* Look for more parameters. */
12762 while (true)
12763 {
12764 cp_parameter_declarator *parameter;
12765 bool parenthesized_p;
12766 /* Parse the parameter. */
12767 parameter
12768 = cp_parser_parameter_declaration (parser,
12769 /*template_parm_p=*/false,
12770 &parenthesized_p);
12771
12772 /* If a parse error occurred parsing the parameter declaration,
12773 then the entire parameter-declaration-list is erroneous. */
12774 if (!parameter)
12775 {
12776 *is_error = true;
12777 parameters = NULL;
12778 break;
12779 }
12780 /* Add the new parameter to the list. */
12781 *tail = parameter;
12782 tail = &parameter->next;
12783
12784 /* Peek at the next token. */
12785 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12786 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12787 /* These are for Objective-C++ */
12788 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12789 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12790 /* The parameter-declaration-list is complete. */
12791 break;
12792 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12793 {
12794 cp_token *token;
12795
12796 /* Peek at the next token. */
12797 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12798 /* If it's an ellipsis, then the list is complete. */
12799 if (token->type == CPP_ELLIPSIS)
12800 break;
12801 /* Otherwise, there must be more parameters. Consume the
12802 `,'. */
12803 cp_lexer_consume_token (parser->lexer);
12804 /* When parsing something like:
12805
12806 int i(float f, double d)
12807
12808 we can tell after seeing the declaration for "f" that we
12809 are not looking at an initialization of a variable "i",
12810 but rather at the declaration of a function "i".
12811
12812 Due to the fact that the parsing of template arguments
12813 (as specified to a template-id) requires backtracking we
12814 cannot use this technique when inside a template argument
12815 list. */
12816 if (!parser->in_template_argument_list_p
12817 && !parser->in_type_id_in_expr_p
12818 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12819 /* However, a parameter-declaration of the form
12820 "foat(f)" (which is a valid declaration of a
12821 parameter "f") can also be interpreted as an
12822 expression (the conversion of "f" to "float"). */
12823 && !parenthesized_p)
12824 cp_parser_commit_to_tentative_parse (parser);
12825 }
12826 else
12827 {
12828 cp_parser_error (parser, "expected %<,%> or %<...%>");
12829 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12830 cp_parser_skip_to_closing_parenthesis (parser,
12831 /*recovering=*/true,
12832 /*or_comma=*/false,
12833 /*consume_paren=*/false);
12834 break;
12835 }
12836 }
12837
12838 parser->in_unbraced_linkage_specification_p
12839 = saved_in_unbraced_linkage_specification_p;
12840
12841 return parameters;
12842 }
12843
12844 /* Parse a parameter declaration.
12845
12846 parameter-declaration:
12847 decl-specifier-seq ... [opt] declarator
12848 decl-specifier-seq declarator = assignment-expression
12849 decl-specifier-seq ... [opt] abstract-declarator [opt]
12850 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12851
12852 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12853 declares a template parameter. (In that case, a non-nested `>'
12854 token encountered during the parsing of the assignment-expression
12855 is not interpreted as a greater-than operator.)
12856
12857 Returns a representation of the parameter, or NULL if an error
12858 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12859 true iff the declarator is of the form "(p)". */
12860
12861 static cp_parameter_declarator *
12862 cp_parser_parameter_declaration (cp_parser *parser,
12863 bool template_parm_p,
12864 bool *parenthesized_p)
12865 {
12866 int declares_class_or_enum;
12867 bool greater_than_is_operator_p;
12868 cp_decl_specifier_seq decl_specifiers;
12869 cp_declarator *declarator;
12870 tree default_argument;
12871 cp_token *token;
12872 const char *saved_message;
12873
12874 /* In a template parameter, `>' is not an operator.
12875
12876 [temp.param]
12877
12878 When parsing a default template-argument for a non-type
12879 template-parameter, the first non-nested `>' is taken as the end
12880 of the template parameter-list rather than a greater-than
12881 operator. */
12882 greater_than_is_operator_p = !template_parm_p;
12883
12884 /* Type definitions may not appear in parameter types. */
12885 saved_message = parser->type_definition_forbidden_message;
12886 parser->type_definition_forbidden_message
12887 = "types may not be defined in parameter types";
12888
12889 /* Parse the declaration-specifiers. */
12890 cp_parser_decl_specifier_seq (parser,
12891 CP_PARSER_FLAGS_NONE,
12892 &decl_specifiers,
12893 &declares_class_or_enum);
12894 /* If an error occurred, there's no reason to attempt to parse the
12895 rest of the declaration. */
12896 if (cp_parser_error_occurred (parser))
12897 {
12898 parser->type_definition_forbidden_message = saved_message;
12899 return NULL;
12900 }
12901
12902 /* Peek at the next token. */
12903 token = cp_lexer_peek_token (parser->lexer);
12904
12905 /* If the next token is a `)', `,', `=', `>', or `...', then there
12906 is no declarator. However, when variadic templates are enabled,
12907 there may be a declarator following `...'. */
12908 if (token->type == CPP_CLOSE_PAREN
12909 || token->type == CPP_COMMA
12910 || token->type == CPP_EQ
12911 || token->type == CPP_GREATER)
12912 {
12913 declarator = NULL;
12914 if (parenthesized_p)
12915 *parenthesized_p = false;
12916 }
12917 /* Otherwise, there should be a declarator. */
12918 else
12919 {
12920 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12921 parser->default_arg_ok_p = false;
12922
12923 /* After seeing a decl-specifier-seq, if the next token is not a
12924 "(", there is no possibility that the code is a valid
12925 expression. Therefore, if parsing tentatively, we commit at
12926 this point. */
12927 if (!parser->in_template_argument_list_p
12928 /* In an expression context, having seen:
12929
12930 (int((char ...
12931
12932 we cannot be sure whether we are looking at a
12933 function-type (taking a "char" as a parameter) or a cast
12934 of some object of type "char" to "int". */
12935 && !parser->in_type_id_in_expr_p
12936 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12937 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12938 cp_parser_commit_to_tentative_parse (parser);
12939 /* Parse the declarator. */
12940 declarator = cp_parser_declarator (parser,
12941 CP_PARSER_DECLARATOR_EITHER,
12942 /*ctor_dtor_or_conv_p=*/NULL,
12943 parenthesized_p,
12944 /*member_p=*/false);
12945 parser->default_arg_ok_p = saved_default_arg_ok_p;
12946 /* After the declarator, allow more attributes. */
12947 decl_specifiers.attributes
12948 = chainon (decl_specifiers.attributes,
12949 cp_parser_attributes_opt (parser));
12950 }
12951
12952 /* If the next token is an ellipsis, and the type of the declarator
12953 contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12954 we actually have a parameter pack expansion expression. Otherwise,
12955 leave the ellipsis for a C-style variadic function. */
12956 token = cp_lexer_peek_token (parser->lexer);
12957 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12958 {
12959 tree type = decl_specifiers.type;
12960
12961 if (DECL_P (type))
12962 type = TREE_TYPE (type);
12963
12964 if (TREE_CODE (type) != TYPE_PACK_EXPANSION
12965 && (!declarator || !declarator->parameter_pack_p)
12966 && uses_parameter_packs (type))
12967 {
12968 /* Consume the `...'. */
12969 cp_lexer_consume_token (parser->lexer);
12970 maybe_warn_variadic_templates ();
12971
12972 /* Build a pack expansion type */
12973 if (declarator)
12974 declarator->parameter_pack_p = true;
12975 else
12976 decl_specifiers.type = make_pack_expansion (type);
12977 }
12978 }
12979
12980 /* The restriction on defining new types applies only to the type
12981 of the parameter, not to the default argument. */
12982 parser->type_definition_forbidden_message = saved_message;
12983
12984 /* If the next token is `=', then process a default argument. */
12985 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12986 {
12987 bool saved_greater_than_is_operator_p;
12988 /* Consume the `='. */
12989 cp_lexer_consume_token (parser->lexer);
12990
12991 /* If we are defining a class, then the tokens that make up the
12992 default argument must be saved and processed later. */
12993 if (!template_parm_p && at_class_scope_p ()
12994 && TYPE_BEING_DEFINED (current_class_type))
12995 {
12996 unsigned depth = 0;
12997 cp_token *first_token;
12998 cp_token *token;
12999
13000 /* Add tokens until we have processed the entire default
13001 argument. We add the range [first_token, token). */
13002 first_token = cp_lexer_peek_token (parser->lexer);
13003 while (true)
13004 {
13005 bool done = false;
13006
13007 /* Peek at the next token. */
13008 token = cp_lexer_peek_token (parser->lexer);
13009 /* What we do depends on what token we have. */
13010 switch (token->type)
13011 {
13012 /* In valid code, a default argument must be
13013 immediately followed by a `,' `)', or `...'. */
13014 case CPP_COMMA:
13015 case CPP_CLOSE_PAREN:
13016 case CPP_ELLIPSIS:
13017 /* If we run into a non-nested `;', `}', or `]',
13018 then the code is invalid -- but the default
13019 argument is certainly over. */
13020 case CPP_SEMICOLON:
13021 case CPP_CLOSE_BRACE:
13022 case CPP_CLOSE_SQUARE:
13023 if (depth == 0)
13024 done = true;
13025 /* Update DEPTH, if necessary. */
13026 else if (token->type == CPP_CLOSE_PAREN
13027 || token->type == CPP_CLOSE_BRACE
13028 || token->type == CPP_CLOSE_SQUARE)
13029 --depth;
13030 break;
13031
13032 case CPP_OPEN_PAREN:
13033 case CPP_OPEN_SQUARE:
13034 case CPP_OPEN_BRACE:
13035 ++depth;
13036 break;
13037
13038 case CPP_RSHIFT:
13039 if (!flag_cpp0x)
13040 break;
13041 /* Fall through for C++0x, which treats the `>>'
13042 operator like two `>' tokens in certain
13043 cases. */
13044
13045 case CPP_GREATER:
13046 /* If we see a non-nested `>', and `>' is not an
13047 operator, then it marks the end of the default
13048 argument. */
13049 if (!depth && !greater_than_is_operator_p)
13050 done = true;
13051 break;
13052
13053 /* If we run out of tokens, issue an error message. */
13054 case CPP_EOF:
13055 case CPP_PRAGMA_EOL:
13056 error ("file ends in default argument");
13057 done = true;
13058 break;
13059
13060 case CPP_NAME:
13061 case CPP_SCOPE:
13062 /* In these cases, we should look for template-ids.
13063 For example, if the default argument is
13064 `X<int, double>()', we need to do name lookup to
13065 figure out whether or not `X' is a template; if
13066 so, the `,' does not end the default argument.
13067
13068 That is not yet done. */
13069 break;
13070
13071 default:
13072 break;
13073 }
13074
13075 /* If we've reached the end, stop. */
13076 if (done)
13077 break;
13078
13079 /* Add the token to the token block. */
13080 token = cp_lexer_consume_token (parser->lexer);
13081 }
13082
13083 /* Create a DEFAULT_ARG to represented the unparsed default
13084 argument. */
13085 default_argument = make_node (DEFAULT_ARG);
13086 DEFARG_TOKENS (default_argument)
13087 = cp_token_cache_new (first_token, token);
13088 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13089 }
13090 /* Outside of a class definition, we can just parse the
13091 assignment-expression. */
13092 else
13093 {
13094 bool saved_local_variables_forbidden_p;
13095
13096 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13097 set correctly. */
13098 saved_greater_than_is_operator_p
13099 = parser->greater_than_is_operator_p;
13100 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13101 /* Local variable names (and the `this' keyword) may not
13102 appear in a default argument. */
13103 saved_local_variables_forbidden_p
13104 = parser->local_variables_forbidden_p;
13105 parser->local_variables_forbidden_p = true;
13106 /* The default argument expression may cause implicitly
13107 defined member functions to be synthesized, which will
13108 result in garbage collection. We must treat this
13109 situation as if we were within the body of function so as
13110 to avoid collecting live data on the stack. */
13111 ++function_depth;
13112 /* Parse the assignment-expression. */
13113 if (template_parm_p)
13114 push_deferring_access_checks (dk_no_deferred);
13115 default_argument
13116 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13117 if (template_parm_p)
13118 pop_deferring_access_checks ();
13119 /* Restore saved state. */
13120 --function_depth;
13121 parser->greater_than_is_operator_p
13122 = saved_greater_than_is_operator_p;
13123 parser->local_variables_forbidden_p
13124 = saved_local_variables_forbidden_p;
13125 }
13126 if (!parser->default_arg_ok_p)
13127 {
13128 if (!flag_pedantic_errors)
13129 warning (0, "deprecated use of default argument for parameter of non-function");
13130 else
13131 {
13132 error ("default arguments are only permitted for function parameters");
13133 default_argument = NULL_TREE;
13134 }
13135 }
13136 }
13137 else
13138 default_argument = NULL_TREE;
13139
13140 return make_parameter_declarator (&decl_specifiers,
13141 declarator,
13142 default_argument);
13143 }
13144
13145 /* Parse a function-body.
13146
13147 function-body:
13148 compound_statement */
13149
13150 static void
13151 cp_parser_function_body (cp_parser *parser)
13152 {
13153 cp_parser_compound_statement (parser, NULL, false);
13154 }
13155
13156 /* Parse a ctor-initializer-opt followed by a function-body. Return
13157 true if a ctor-initializer was present. */
13158
13159 static bool
13160 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13161 {
13162 tree body;
13163 bool ctor_initializer_p;
13164
13165 /* Begin the function body. */
13166 body = begin_function_body ();
13167 /* Parse the optional ctor-initializer. */
13168 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13169 /* Parse the function-body. */
13170 cp_parser_function_body (parser);
13171 /* Finish the function body. */
13172 finish_function_body (body);
13173
13174 return ctor_initializer_p;
13175 }
13176
13177 /* Parse an initializer.
13178
13179 initializer:
13180 = initializer-clause
13181 ( expression-list )
13182
13183 Returns an expression representing the initializer. If no
13184 initializer is present, NULL_TREE is returned.
13185
13186 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13187 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13188 set to FALSE if there is no initializer present. If there is an
13189 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13190 is set to true; otherwise it is set to false. */
13191
13192 static tree
13193 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13194 bool* non_constant_p)
13195 {
13196 cp_token *token;
13197 tree init;
13198
13199 /* Peek at the next token. */
13200 token = cp_lexer_peek_token (parser->lexer);
13201
13202 /* Let our caller know whether or not this initializer was
13203 parenthesized. */
13204 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13205 /* Assume that the initializer is constant. */
13206 *non_constant_p = false;
13207
13208 if (token->type == CPP_EQ)
13209 {
13210 /* Consume the `='. */
13211 cp_lexer_consume_token (parser->lexer);
13212 /* Parse the initializer-clause. */
13213 init = cp_parser_initializer_clause (parser, non_constant_p);
13214 }
13215 else if (token->type == CPP_OPEN_PAREN)
13216 init = cp_parser_parenthesized_expression_list (parser, false,
13217 /*cast_p=*/false,
13218 /*allow_expansion_p=*/true,
13219 non_constant_p);
13220 else
13221 {
13222 /* Anything else is an error. */
13223 cp_parser_error (parser, "expected initializer");
13224 init = error_mark_node;
13225 }
13226
13227 return init;
13228 }
13229
13230 /* Parse an initializer-clause.
13231
13232 initializer-clause:
13233 assignment-expression
13234 { initializer-list , [opt] }
13235 { }
13236
13237 Returns an expression representing the initializer.
13238
13239 If the `assignment-expression' production is used the value
13240 returned is simply a representation for the expression.
13241
13242 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13243 the elements of the initializer-list (or NULL, if the last
13244 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13245 NULL_TREE. There is no way to detect whether or not the optional
13246 trailing `,' was provided. NON_CONSTANT_P is as for
13247 cp_parser_initializer. */
13248
13249 static tree
13250 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13251 {
13252 tree initializer;
13253
13254 /* Assume the expression is constant. */
13255 *non_constant_p = false;
13256
13257 /* If it is not a `{', then we are looking at an
13258 assignment-expression. */
13259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13260 {
13261 initializer
13262 = cp_parser_constant_expression (parser,
13263 /*allow_non_constant_p=*/true,
13264 non_constant_p);
13265 if (!*non_constant_p)
13266 initializer = fold_non_dependent_expr (initializer);
13267 }
13268 else
13269 {
13270 /* Consume the `{' token. */
13271 cp_lexer_consume_token (parser->lexer);
13272 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13273 initializer = make_node (CONSTRUCTOR);
13274 /* If it's not a `}', then there is a non-trivial initializer. */
13275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13276 {
13277 /* Parse the initializer list. */
13278 CONSTRUCTOR_ELTS (initializer)
13279 = cp_parser_initializer_list (parser, non_constant_p);
13280 /* A trailing `,' token is allowed. */
13281 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13282 cp_lexer_consume_token (parser->lexer);
13283 }
13284 /* Now, there should be a trailing `}'. */
13285 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13286 }
13287
13288 return initializer;
13289 }
13290
13291 /* Parse an initializer-list.
13292
13293 initializer-list:
13294 initializer-clause ... [opt]
13295 initializer-list , initializer-clause ... [opt]
13296
13297 GNU Extension:
13298
13299 initializer-list:
13300 identifier : initializer-clause
13301 initializer-list, identifier : initializer-clause
13302
13303 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13304 for the initializer. If the INDEX of the elt is non-NULL, it is the
13305 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13306 as for cp_parser_initializer. */
13307
13308 static VEC(constructor_elt,gc) *
13309 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13310 {
13311 VEC(constructor_elt,gc) *v = NULL;
13312
13313 /* Assume all of the expressions are constant. */
13314 *non_constant_p = false;
13315
13316 /* Parse the rest of the list. */
13317 while (true)
13318 {
13319 cp_token *token;
13320 tree identifier;
13321 tree initializer;
13322 bool clause_non_constant_p;
13323
13324 /* If the next token is an identifier and the following one is a
13325 colon, we are looking at the GNU designated-initializer
13326 syntax. */
13327 if (cp_parser_allow_gnu_extensions_p (parser)
13328 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13329 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13330 {
13331 /* Warn the user that they are using an extension. */
13332 if (pedantic)
13333 pedwarn ("ISO C++ does not allow designated initializers");
13334 /* Consume the identifier. */
13335 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13336 /* Consume the `:'. */
13337 cp_lexer_consume_token (parser->lexer);
13338 }
13339 else
13340 identifier = NULL_TREE;
13341
13342 /* Parse the initializer. */
13343 initializer = cp_parser_initializer_clause (parser,
13344 &clause_non_constant_p);
13345 /* If any clause is non-constant, so is the entire initializer. */
13346 if (clause_non_constant_p)
13347 *non_constant_p = true;
13348
13349 /* If we have an ellipsis, this is an initializer pack
13350 expansion. */
13351 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13352 {
13353 /* Consume the `...'. */
13354 cp_lexer_consume_token (parser->lexer);
13355
13356 /* Turn the initializer into an initializer expansion. */
13357 initializer = make_pack_expansion (initializer);
13358 }
13359
13360 /* Add it to the vector. */
13361 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13362
13363 /* If the next token is not a comma, we have reached the end of
13364 the list. */
13365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13366 break;
13367
13368 /* Peek at the next token. */
13369 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13370 /* If the next token is a `}', then we're still done. An
13371 initializer-clause can have a trailing `,' after the
13372 initializer-list and before the closing `}'. */
13373 if (token->type == CPP_CLOSE_BRACE)
13374 break;
13375
13376 /* Consume the `,' token. */
13377 cp_lexer_consume_token (parser->lexer);
13378 }
13379
13380 return v;
13381 }
13382
13383 /* Classes [gram.class] */
13384
13385 /* Parse a class-name.
13386
13387 class-name:
13388 identifier
13389 template-id
13390
13391 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13392 to indicate that names looked up in dependent types should be
13393 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13394 keyword has been used to indicate that the name that appears next
13395 is a template. TAG_TYPE indicates the explicit tag given before
13396 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13397 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13398 is the class being defined in a class-head.
13399
13400 Returns the TYPE_DECL representing the class. */
13401
13402 static tree
13403 cp_parser_class_name (cp_parser *parser,
13404 bool typename_keyword_p,
13405 bool template_keyword_p,
13406 enum tag_types tag_type,
13407 bool check_dependency_p,
13408 bool class_head_p,
13409 bool is_declaration)
13410 {
13411 tree decl;
13412 tree scope;
13413 bool typename_p;
13414 cp_token *token;
13415
13416 /* All class-names start with an identifier. */
13417 token = cp_lexer_peek_token (parser->lexer);
13418 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13419 {
13420 cp_parser_error (parser, "expected class-name");
13421 return error_mark_node;
13422 }
13423
13424 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13425 to a template-id, so we save it here. */
13426 scope = parser->scope;
13427 if (scope == error_mark_node)
13428 return error_mark_node;
13429
13430 /* Any name names a type if we're following the `typename' keyword
13431 in a qualified name where the enclosing scope is type-dependent. */
13432 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13433 && dependent_type_p (scope));
13434 /* Handle the common case (an identifier, but not a template-id)
13435 efficiently. */
13436 if (token->type == CPP_NAME
13437 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13438 {
13439 cp_token *identifier_token;
13440 tree identifier;
13441 bool ambiguous_p;
13442
13443 /* Look for the identifier. */
13444 identifier_token = cp_lexer_peek_token (parser->lexer);
13445 ambiguous_p = identifier_token->ambiguous_p;
13446 identifier = cp_parser_identifier (parser);
13447 /* If the next token isn't an identifier, we are certainly not
13448 looking at a class-name. */
13449 if (identifier == error_mark_node)
13450 decl = error_mark_node;
13451 /* If we know this is a type-name, there's no need to look it
13452 up. */
13453 else if (typename_p)
13454 decl = identifier;
13455 else
13456 {
13457 tree ambiguous_decls;
13458 /* If we already know that this lookup is ambiguous, then
13459 we've already issued an error message; there's no reason
13460 to check again. */
13461 if (ambiguous_p)
13462 {
13463 cp_parser_simulate_error (parser);
13464 return error_mark_node;
13465 }
13466 /* If the next token is a `::', then the name must be a type
13467 name.
13468
13469 [basic.lookup.qual]
13470
13471 During the lookup for a name preceding the :: scope
13472 resolution operator, object, function, and enumerator
13473 names are ignored. */
13474 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13475 tag_type = typename_type;
13476 /* Look up the name. */
13477 decl = cp_parser_lookup_name (parser, identifier,
13478 tag_type,
13479 /*is_template=*/false,
13480 /*is_namespace=*/false,
13481 check_dependency_p,
13482 &ambiguous_decls);
13483 if (ambiguous_decls)
13484 {
13485 error ("reference to %qD is ambiguous", identifier);
13486 print_candidates (ambiguous_decls);
13487 if (cp_parser_parsing_tentatively (parser))
13488 {
13489 identifier_token->ambiguous_p = true;
13490 cp_parser_simulate_error (parser);
13491 }
13492 return error_mark_node;
13493 }
13494 }
13495 }
13496 else
13497 {
13498 /* Try a template-id. */
13499 decl = cp_parser_template_id (parser, template_keyword_p,
13500 check_dependency_p,
13501 is_declaration);
13502 if (decl == error_mark_node)
13503 return error_mark_node;
13504 }
13505
13506 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13507
13508 /* If this is a typename, create a TYPENAME_TYPE. */
13509 if (typename_p && decl != error_mark_node)
13510 {
13511 decl = make_typename_type (scope, decl, typename_type,
13512 /*complain=*/tf_error);
13513 if (decl != error_mark_node)
13514 decl = TYPE_NAME (decl);
13515 }
13516
13517 /* Check to see that it is really the name of a class. */
13518 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13519 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13520 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13521 /* Situations like this:
13522
13523 template <typename T> struct A {
13524 typename T::template X<int>::I i;
13525 };
13526
13527 are problematic. Is `T::template X<int>' a class-name? The
13528 standard does not seem to be definitive, but there is no other
13529 valid interpretation of the following `::'. Therefore, those
13530 names are considered class-names. */
13531 {
13532 decl = make_typename_type (scope, decl, tag_type, tf_error);
13533 if (decl != error_mark_node)
13534 decl = TYPE_NAME (decl);
13535 }
13536 else if (TREE_CODE (decl) != TYPE_DECL
13537 || TREE_TYPE (decl) == error_mark_node
13538 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13539 decl = error_mark_node;
13540
13541 if (decl == error_mark_node)
13542 cp_parser_error (parser, "expected class-name");
13543
13544 return decl;
13545 }
13546
13547 /* Parse a class-specifier.
13548
13549 class-specifier:
13550 class-head { member-specification [opt] }
13551
13552 Returns the TREE_TYPE representing the class. */
13553
13554 static tree
13555 cp_parser_class_specifier (cp_parser* parser)
13556 {
13557 cp_token *token;
13558 tree type;
13559 tree attributes = NULL_TREE;
13560 int has_trailing_semicolon;
13561 bool nested_name_specifier_p;
13562 unsigned saved_num_template_parameter_lists;
13563 bool saved_in_function_body;
13564 tree old_scope = NULL_TREE;
13565 tree scope = NULL_TREE;
13566 tree bases;
13567
13568 push_deferring_access_checks (dk_no_deferred);
13569
13570 /* Parse the class-head. */
13571 type = cp_parser_class_head (parser,
13572 &nested_name_specifier_p,
13573 &attributes,
13574 &bases);
13575 /* If the class-head was a semantic disaster, skip the entire body
13576 of the class. */
13577 if (!type)
13578 {
13579 cp_parser_skip_to_end_of_block_or_statement (parser);
13580 pop_deferring_access_checks ();
13581 return error_mark_node;
13582 }
13583
13584 /* Look for the `{'. */
13585 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13586 {
13587 pop_deferring_access_checks ();
13588 return error_mark_node;
13589 }
13590
13591 /* Process the base classes. If they're invalid, skip the
13592 entire class body. */
13593 if (!xref_basetypes (type, bases))
13594 {
13595 cp_parser_skip_to_closing_brace (parser);
13596
13597 /* Consuming the closing brace yields better error messages
13598 later on. */
13599 cp_lexer_consume_token (parser->lexer);
13600 pop_deferring_access_checks ();
13601 return error_mark_node;
13602 }
13603
13604 /* Issue an error message if type-definitions are forbidden here. */
13605 cp_parser_check_type_definition (parser);
13606 /* Remember that we are defining one more class. */
13607 ++parser->num_classes_being_defined;
13608 /* Inside the class, surrounding template-parameter-lists do not
13609 apply. */
13610 saved_num_template_parameter_lists
13611 = parser->num_template_parameter_lists;
13612 parser->num_template_parameter_lists = 0;
13613 /* We are not in a function body. */
13614 saved_in_function_body = parser->in_function_body;
13615 parser->in_function_body = false;
13616
13617 /* Start the class. */
13618 if (nested_name_specifier_p)
13619 {
13620 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13621 old_scope = push_inner_scope (scope);
13622 }
13623 type = begin_class_definition (type, attributes);
13624
13625 if (type == error_mark_node)
13626 /* If the type is erroneous, skip the entire body of the class. */
13627 cp_parser_skip_to_closing_brace (parser);
13628 else
13629 /* Parse the member-specification. */
13630 cp_parser_member_specification_opt (parser);
13631
13632 /* Look for the trailing `}'. */
13633 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13634 /* We get better error messages by noticing a common problem: a
13635 missing trailing `;'. */
13636 token = cp_lexer_peek_token (parser->lexer);
13637 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13638 /* Look for trailing attributes to apply to this class. */
13639 if (cp_parser_allow_gnu_extensions_p (parser))
13640 attributes = cp_parser_attributes_opt (parser);
13641 if (type != error_mark_node)
13642 type = finish_struct (type, attributes);
13643 if (nested_name_specifier_p)
13644 pop_inner_scope (old_scope, scope);
13645 /* If this class is not itself within the scope of another class,
13646 then we need to parse the bodies of all of the queued function
13647 definitions. Note that the queued functions defined in a class
13648 are not always processed immediately following the
13649 class-specifier for that class. Consider:
13650
13651 struct A {
13652 struct B { void f() { sizeof (A); } };
13653 };
13654
13655 If `f' were processed before the processing of `A' were
13656 completed, there would be no way to compute the size of `A'.
13657 Note that the nesting we are interested in here is lexical --
13658 not the semantic nesting given by TYPE_CONTEXT. In particular,
13659 for:
13660
13661 struct A { struct B; };
13662 struct A::B { void f() { } };
13663
13664 there is no need to delay the parsing of `A::B::f'. */
13665 if (--parser->num_classes_being_defined == 0)
13666 {
13667 tree queue_entry;
13668 tree fn;
13669 tree class_type = NULL_TREE;
13670 tree pushed_scope = NULL_TREE;
13671
13672 /* In a first pass, parse default arguments to the functions.
13673 Then, in a second pass, parse the bodies of the functions.
13674 This two-phased approach handles cases like:
13675
13676 struct S {
13677 void f() { g(); }
13678 void g(int i = 3);
13679 };
13680
13681 */
13682 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13683 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13684 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13685 TREE_PURPOSE (parser->unparsed_functions_queues)
13686 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13687 {
13688 fn = TREE_VALUE (queue_entry);
13689 /* If there are default arguments that have not yet been processed,
13690 take care of them now. */
13691 if (class_type != TREE_PURPOSE (queue_entry))
13692 {
13693 if (pushed_scope)
13694 pop_scope (pushed_scope);
13695 class_type = TREE_PURPOSE (queue_entry);
13696 pushed_scope = push_scope (class_type);
13697 }
13698 /* Make sure that any template parameters are in scope. */
13699 maybe_begin_member_template_processing (fn);
13700 /* Parse the default argument expressions. */
13701 cp_parser_late_parsing_default_args (parser, fn);
13702 /* Remove any template parameters from the symbol table. */
13703 maybe_end_member_template_processing ();
13704 }
13705 if (pushed_scope)
13706 pop_scope (pushed_scope);
13707 /* Now parse the body of the functions. */
13708 for (TREE_VALUE (parser->unparsed_functions_queues)
13709 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13710 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13711 TREE_VALUE (parser->unparsed_functions_queues)
13712 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13713 {
13714 /* Figure out which function we need to process. */
13715 fn = TREE_VALUE (queue_entry);
13716 /* Parse the function. */
13717 cp_parser_late_parsing_for_member (parser, fn);
13718 }
13719 }
13720
13721 /* Put back any saved access checks. */
13722 pop_deferring_access_checks ();
13723
13724 /* Restore saved state. */
13725 parser->in_function_body = saved_in_function_body;
13726 parser->num_template_parameter_lists
13727 = saved_num_template_parameter_lists;
13728
13729 return type;
13730 }
13731
13732 /* Parse a class-head.
13733
13734 class-head:
13735 class-key identifier [opt] base-clause [opt]
13736 class-key nested-name-specifier identifier base-clause [opt]
13737 class-key nested-name-specifier [opt] template-id
13738 base-clause [opt]
13739
13740 GNU Extensions:
13741 class-key attributes identifier [opt] base-clause [opt]
13742 class-key attributes nested-name-specifier identifier base-clause [opt]
13743 class-key attributes nested-name-specifier [opt] template-id
13744 base-clause [opt]
13745
13746 Upon return BASES is initialized to the list of base classes (or
13747 NULL, if there are none) in the same form returned by
13748 cp_parser_base_clause.
13749
13750 Returns the TYPE of the indicated class. Sets
13751 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13752 involving a nested-name-specifier was used, and FALSE otherwise.
13753
13754 Returns error_mark_node if this is not a class-head.
13755
13756 Returns NULL_TREE if the class-head is syntactically valid, but
13757 semantically invalid in a way that means we should skip the entire
13758 body of the class. */
13759
13760 static tree
13761 cp_parser_class_head (cp_parser* parser,
13762 bool* nested_name_specifier_p,
13763 tree *attributes_p,
13764 tree *bases)
13765 {
13766 tree nested_name_specifier;
13767 enum tag_types class_key;
13768 tree id = NULL_TREE;
13769 tree type = NULL_TREE;
13770 tree attributes;
13771 bool template_id_p = false;
13772 bool qualified_p = false;
13773 bool invalid_nested_name_p = false;
13774 bool invalid_explicit_specialization_p = false;
13775 tree pushed_scope = NULL_TREE;
13776 unsigned num_templates;
13777
13778 /* Assume no nested-name-specifier will be present. */
13779 *nested_name_specifier_p = false;
13780 /* Assume no template parameter lists will be used in defining the
13781 type. */
13782 num_templates = 0;
13783
13784 *bases = NULL_TREE;
13785
13786 /* Look for the class-key. */
13787 class_key = cp_parser_class_key (parser);
13788 if (class_key == none_type)
13789 return error_mark_node;
13790
13791 /* Parse the attributes. */
13792 attributes = cp_parser_attributes_opt (parser);
13793
13794 /* If the next token is `::', that is invalid -- but sometimes
13795 people do try to write:
13796
13797 struct ::S {};
13798
13799 Handle this gracefully by accepting the extra qualifier, and then
13800 issuing an error about it later if this really is a
13801 class-head. If it turns out just to be an elaborated type
13802 specifier, remain silent. */
13803 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13804 qualified_p = true;
13805
13806 push_deferring_access_checks (dk_no_check);
13807
13808 /* Determine the name of the class. Begin by looking for an
13809 optional nested-name-specifier. */
13810 nested_name_specifier
13811 = cp_parser_nested_name_specifier_opt (parser,
13812 /*typename_keyword_p=*/false,
13813 /*check_dependency_p=*/false,
13814 /*type_p=*/false,
13815 /*is_declaration=*/false);
13816 /* If there was a nested-name-specifier, then there *must* be an
13817 identifier. */
13818 if (nested_name_specifier)
13819 {
13820 /* Although the grammar says `identifier', it really means
13821 `class-name' or `template-name'. You are only allowed to
13822 define a class that has already been declared with this
13823 syntax.
13824
13825 The proposed resolution for Core Issue 180 says that wherever
13826 you see `class T::X' you should treat `X' as a type-name.
13827
13828 It is OK to define an inaccessible class; for example:
13829
13830 class A { class B; };
13831 class A::B {};
13832
13833 We do not know if we will see a class-name, or a
13834 template-name. We look for a class-name first, in case the
13835 class-name is a template-id; if we looked for the
13836 template-name first we would stop after the template-name. */
13837 cp_parser_parse_tentatively (parser);
13838 type = cp_parser_class_name (parser,
13839 /*typename_keyword_p=*/false,
13840 /*template_keyword_p=*/false,
13841 class_type,
13842 /*check_dependency_p=*/false,
13843 /*class_head_p=*/true,
13844 /*is_declaration=*/false);
13845 /* If that didn't work, ignore the nested-name-specifier. */
13846 if (!cp_parser_parse_definitely (parser))
13847 {
13848 invalid_nested_name_p = true;
13849 id = cp_parser_identifier (parser);
13850 if (id == error_mark_node)
13851 id = NULL_TREE;
13852 }
13853 /* If we could not find a corresponding TYPE, treat this
13854 declaration like an unqualified declaration. */
13855 if (type == error_mark_node)
13856 nested_name_specifier = NULL_TREE;
13857 /* Otherwise, count the number of templates used in TYPE and its
13858 containing scopes. */
13859 else
13860 {
13861 tree scope;
13862
13863 for (scope = TREE_TYPE (type);
13864 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13865 scope = (TYPE_P (scope)
13866 ? TYPE_CONTEXT (scope)
13867 : DECL_CONTEXT (scope)))
13868 if (TYPE_P (scope)
13869 && CLASS_TYPE_P (scope)
13870 && CLASSTYPE_TEMPLATE_INFO (scope)
13871 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13872 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13873 ++num_templates;
13874 }
13875 }
13876 /* Otherwise, the identifier is optional. */
13877 else
13878 {
13879 /* We don't know whether what comes next is a template-id,
13880 an identifier, or nothing at all. */
13881 cp_parser_parse_tentatively (parser);
13882 /* Check for a template-id. */
13883 id = cp_parser_template_id (parser,
13884 /*template_keyword_p=*/false,
13885 /*check_dependency_p=*/true,
13886 /*is_declaration=*/true);
13887 /* If that didn't work, it could still be an identifier. */
13888 if (!cp_parser_parse_definitely (parser))
13889 {
13890 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13891 id = cp_parser_identifier (parser);
13892 else
13893 id = NULL_TREE;
13894 }
13895 else
13896 {
13897 template_id_p = true;
13898 ++num_templates;
13899 }
13900 }
13901
13902 pop_deferring_access_checks ();
13903
13904 if (id)
13905 cp_parser_check_for_invalid_template_id (parser, id);
13906
13907 /* If it's not a `:' or a `{' then we can't really be looking at a
13908 class-head, since a class-head only appears as part of a
13909 class-specifier. We have to detect this situation before calling
13910 xref_tag, since that has irreversible side-effects. */
13911 if (!cp_parser_next_token_starts_class_definition_p (parser))
13912 {
13913 cp_parser_error (parser, "expected %<{%> or %<:%>");
13914 return error_mark_node;
13915 }
13916
13917 /* At this point, we're going ahead with the class-specifier, even
13918 if some other problem occurs. */
13919 cp_parser_commit_to_tentative_parse (parser);
13920 /* Issue the error about the overly-qualified name now. */
13921 if (qualified_p)
13922 cp_parser_error (parser,
13923 "global qualification of class name is invalid");
13924 else if (invalid_nested_name_p)
13925 cp_parser_error (parser,
13926 "qualified name does not name a class");
13927 else if (nested_name_specifier)
13928 {
13929 tree scope;
13930
13931 /* Reject typedef-names in class heads. */
13932 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13933 {
13934 error ("invalid class name in declaration of %qD", type);
13935 type = NULL_TREE;
13936 goto done;
13937 }
13938
13939 /* Figure out in what scope the declaration is being placed. */
13940 scope = current_scope ();
13941 /* If that scope does not contain the scope in which the
13942 class was originally declared, the program is invalid. */
13943 if (scope && !is_ancestor (scope, nested_name_specifier))
13944 {
13945 error ("declaration of %qD in %qD which does not enclose %qD",
13946 type, scope, nested_name_specifier);
13947 type = NULL_TREE;
13948 goto done;
13949 }
13950 /* [dcl.meaning]
13951
13952 A declarator-id shall not be qualified exception of the
13953 definition of a ... nested class outside of its class
13954 ... [or] a the definition or explicit instantiation of a
13955 class member of a namespace outside of its namespace. */
13956 if (scope == nested_name_specifier)
13957 {
13958 pedwarn ("extra qualification ignored");
13959 nested_name_specifier = NULL_TREE;
13960 num_templates = 0;
13961 }
13962 }
13963 /* An explicit-specialization must be preceded by "template <>". If
13964 it is not, try to recover gracefully. */
13965 if (at_namespace_scope_p ()
13966 && parser->num_template_parameter_lists == 0
13967 && template_id_p)
13968 {
13969 error ("an explicit specialization must be preceded by %<template <>%>");
13970 invalid_explicit_specialization_p = true;
13971 /* Take the same action that would have been taken by
13972 cp_parser_explicit_specialization. */
13973 ++parser->num_template_parameter_lists;
13974 begin_specialization ();
13975 }
13976 /* There must be no "return" statements between this point and the
13977 end of this function; set "type "to the correct return value and
13978 use "goto done;" to return. */
13979 /* Make sure that the right number of template parameters were
13980 present. */
13981 if (!cp_parser_check_template_parameters (parser, num_templates))
13982 {
13983 /* If something went wrong, there is no point in even trying to
13984 process the class-definition. */
13985 type = NULL_TREE;
13986 goto done;
13987 }
13988
13989 /* Look up the type. */
13990 if (template_id_p)
13991 {
13992 type = TREE_TYPE (id);
13993 type = maybe_process_partial_specialization (type);
13994 if (nested_name_specifier)
13995 pushed_scope = push_scope (nested_name_specifier);
13996 }
13997 else if (nested_name_specifier)
13998 {
13999 tree class_type;
14000
14001 /* Given:
14002
14003 template <typename T> struct S { struct T };
14004 template <typename T> struct S<T>::T { };
14005
14006 we will get a TYPENAME_TYPE when processing the definition of
14007 `S::T'. We need to resolve it to the actual type before we
14008 try to define it. */
14009 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14010 {
14011 class_type = resolve_typename_type (TREE_TYPE (type),
14012 /*only_current_p=*/false);
14013 if (class_type != error_mark_node)
14014 type = TYPE_NAME (class_type);
14015 else
14016 {
14017 cp_parser_error (parser, "could not resolve typename type");
14018 type = error_mark_node;
14019 }
14020 }
14021
14022 maybe_process_partial_specialization (TREE_TYPE (type));
14023 class_type = current_class_type;
14024 /* Enter the scope indicated by the nested-name-specifier. */
14025 pushed_scope = push_scope (nested_name_specifier);
14026 /* Get the canonical version of this type. */
14027 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14028 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14029 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14030 {
14031 type = push_template_decl (type);
14032 if (type == error_mark_node)
14033 {
14034 type = NULL_TREE;
14035 goto done;
14036 }
14037 }
14038
14039 type = TREE_TYPE (type);
14040 *nested_name_specifier_p = true;
14041 }
14042 else /* The name is not a nested name. */
14043 {
14044 /* If the class was unnamed, create a dummy name. */
14045 if (!id)
14046 id = make_anon_name ();
14047 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14048 parser->num_template_parameter_lists);
14049 }
14050
14051 /* Indicate whether this class was declared as a `class' or as a
14052 `struct'. */
14053 if (TREE_CODE (type) == RECORD_TYPE)
14054 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14055 cp_parser_check_class_key (class_key, type);
14056
14057 /* If this type was already complete, and we see another definition,
14058 that's an error. */
14059 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14060 {
14061 error ("redefinition of %q#T", type);
14062 error ("previous definition of %q+#T", type);
14063 type = NULL_TREE;
14064 goto done;
14065 }
14066 else if (type == error_mark_node)
14067 type = NULL_TREE;
14068
14069 /* We will have entered the scope containing the class; the names of
14070 base classes should be looked up in that context. For example:
14071
14072 struct A { struct B {}; struct C; };
14073 struct A::C : B {};
14074
14075 is valid. */
14076
14077 /* Get the list of base-classes, if there is one. */
14078 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14079 *bases = cp_parser_base_clause (parser);
14080
14081 done:
14082 /* Leave the scope given by the nested-name-specifier. We will
14083 enter the class scope itself while processing the members. */
14084 if (pushed_scope)
14085 pop_scope (pushed_scope);
14086
14087 if (invalid_explicit_specialization_p)
14088 {
14089 end_specialization ();
14090 --parser->num_template_parameter_lists;
14091 }
14092 *attributes_p = attributes;
14093 return type;
14094 }
14095
14096 /* Parse a class-key.
14097
14098 class-key:
14099 class
14100 struct
14101 union
14102
14103 Returns the kind of class-key specified, or none_type to indicate
14104 error. */
14105
14106 static enum tag_types
14107 cp_parser_class_key (cp_parser* parser)
14108 {
14109 cp_token *token;
14110 enum tag_types tag_type;
14111
14112 /* Look for the class-key. */
14113 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14114 if (!token)
14115 return none_type;
14116
14117 /* Check to see if the TOKEN is a class-key. */
14118 tag_type = cp_parser_token_is_class_key (token);
14119 if (!tag_type)
14120 cp_parser_error (parser, "expected class-key");
14121 return tag_type;
14122 }
14123
14124 /* Parse an (optional) member-specification.
14125
14126 member-specification:
14127 member-declaration member-specification [opt]
14128 access-specifier : member-specification [opt] */
14129
14130 static void
14131 cp_parser_member_specification_opt (cp_parser* parser)
14132 {
14133 while (true)
14134 {
14135 cp_token *token;
14136 enum rid keyword;
14137
14138 /* Peek at the next token. */
14139 token = cp_lexer_peek_token (parser->lexer);
14140 /* If it's a `}', or EOF then we've seen all the members. */
14141 if (token->type == CPP_CLOSE_BRACE
14142 || token->type == CPP_EOF
14143 || token->type == CPP_PRAGMA_EOL)
14144 break;
14145
14146 /* See if this token is a keyword. */
14147 keyword = token->keyword;
14148 switch (keyword)
14149 {
14150 case RID_PUBLIC:
14151 case RID_PROTECTED:
14152 case RID_PRIVATE:
14153 /* Consume the access-specifier. */
14154 cp_lexer_consume_token (parser->lexer);
14155 /* Remember which access-specifier is active. */
14156 current_access_specifier = token->u.value;
14157 /* Look for the `:'. */
14158 cp_parser_require (parser, CPP_COLON, "`:'");
14159 break;
14160
14161 default:
14162 /* Accept #pragmas at class scope. */
14163 if (token->type == CPP_PRAGMA)
14164 {
14165 cp_parser_pragma (parser, pragma_external);
14166 break;
14167 }
14168
14169 /* Otherwise, the next construction must be a
14170 member-declaration. */
14171 cp_parser_member_declaration (parser);
14172 }
14173 }
14174 }
14175
14176 /* Parse a member-declaration.
14177
14178 member-declaration:
14179 decl-specifier-seq [opt] member-declarator-list [opt] ;
14180 function-definition ; [opt]
14181 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14182 using-declaration
14183 template-declaration
14184
14185 member-declarator-list:
14186 member-declarator
14187 member-declarator-list , member-declarator
14188
14189 member-declarator:
14190 declarator pure-specifier [opt]
14191 declarator constant-initializer [opt]
14192 identifier [opt] : constant-expression
14193
14194 GNU Extensions:
14195
14196 member-declaration:
14197 __extension__ member-declaration
14198
14199 member-declarator:
14200 declarator attributes [opt] pure-specifier [opt]
14201 declarator attributes [opt] constant-initializer [opt]
14202 identifier [opt] attributes [opt] : constant-expression
14203
14204 C++0x Extensions:
14205
14206 member-declaration:
14207 static_assert-declaration */
14208
14209 static void
14210 cp_parser_member_declaration (cp_parser* parser)
14211 {
14212 cp_decl_specifier_seq decl_specifiers;
14213 tree prefix_attributes;
14214 tree decl;
14215 int declares_class_or_enum;
14216 bool friend_p;
14217 cp_token *token;
14218 int saved_pedantic;
14219
14220 /* Check for the `__extension__' keyword. */
14221 if (cp_parser_extension_opt (parser, &saved_pedantic))
14222 {
14223 /* Recurse. */
14224 cp_parser_member_declaration (parser);
14225 /* Restore the old value of the PEDANTIC flag. */
14226 pedantic = saved_pedantic;
14227
14228 return;
14229 }
14230
14231 /* Check for a template-declaration. */
14232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14233 {
14234 /* An explicit specialization here is an error condition, and we
14235 expect the specialization handler to detect and report this. */
14236 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14237 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14238 cp_parser_explicit_specialization (parser);
14239 else
14240 cp_parser_template_declaration (parser, /*member_p=*/true);
14241
14242 return;
14243 }
14244
14245 /* Check for a using-declaration. */
14246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14247 {
14248 /* Parse the using-declaration. */
14249 cp_parser_using_declaration (parser,
14250 /*access_declaration_p=*/false);
14251 return;
14252 }
14253
14254 /* Check for @defs. */
14255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14256 {
14257 tree ivar, member;
14258 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14259 ivar = ivar_chains;
14260 while (ivar)
14261 {
14262 member = ivar;
14263 ivar = TREE_CHAIN (member);
14264 TREE_CHAIN (member) = NULL_TREE;
14265 finish_member_declaration (member);
14266 }
14267 return;
14268 }
14269
14270 /* If the next token is `static_assert' we have a static assertion. */
14271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14272 {
14273 cp_parser_static_assert (parser, /*member_p=*/true);
14274 return;
14275 }
14276
14277 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14278 return;
14279
14280 /* Parse the decl-specifier-seq. */
14281 cp_parser_decl_specifier_seq (parser,
14282 CP_PARSER_FLAGS_OPTIONAL,
14283 &decl_specifiers,
14284 &declares_class_or_enum);
14285 prefix_attributes = decl_specifiers.attributes;
14286 decl_specifiers.attributes = NULL_TREE;
14287 /* Check for an invalid type-name. */
14288 if (!decl_specifiers.type
14289 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14290 return;
14291 /* If there is no declarator, then the decl-specifier-seq should
14292 specify a type. */
14293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14294 {
14295 /* If there was no decl-specifier-seq, and the next token is a
14296 `;', then we have something like:
14297
14298 struct S { ; };
14299
14300 [class.mem]
14301
14302 Each member-declaration shall declare at least one member
14303 name of the class. */
14304 if (!decl_specifiers.any_specifiers_p)
14305 {
14306 cp_token *token = cp_lexer_peek_token (parser->lexer);
14307 if (pedantic && !token->in_system_header)
14308 pedwarn ("%Hextra %<;%>", &token->location);
14309 }
14310 else
14311 {
14312 tree type;
14313
14314 /* See if this declaration is a friend. */
14315 friend_p = cp_parser_friend_p (&decl_specifiers);
14316 /* If there were decl-specifiers, check to see if there was
14317 a class-declaration. */
14318 type = check_tag_decl (&decl_specifiers);
14319 /* Nested classes have already been added to the class, but
14320 a `friend' needs to be explicitly registered. */
14321 if (friend_p)
14322 {
14323 /* If the `friend' keyword was present, the friend must
14324 be introduced with a class-key. */
14325 if (!declares_class_or_enum)
14326 error ("a class-key must be used when declaring a friend");
14327 /* In this case:
14328
14329 template <typename T> struct A {
14330 friend struct A<T>::B;
14331 };
14332
14333 A<T>::B will be represented by a TYPENAME_TYPE, and
14334 therefore not recognized by check_tag_decl. */
14335 if (!type
14336 && decl_specifiers.type
14337 && TYPE_P (decl_specifiers.type))
14338 type = decl_specifiers.type;
14339 if (!type || !TYPE_P (type))
14340 error ("friend declaration does not name a class or "
14341 "function");
14342 else
14343 make_friend_class (current_class_type, type,
14344 /*complain=*/true);
14345 }
14346 /* If there is no TYPE, an error message will already have
14347 been issued. */
14348 else if (!type || type == error_mark_node)
14349 ;
14350 /* An anonymous aggregate has to be handled specially; such
14351 a declaration really declares a data member (with a
14352 particular type), as opposed to a nested class. */
14353 else if (ANON_AGGR_TYPE_P (type))
14354 {
14355 /* Remove constructors and such from TYPE, now that we
14356 know it is an anonymous aggregate. */
14357 fixup_anonymous_aggr (type);
14358 /* And make the corresponding data member. */
14359 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14360 /* Add it to the class. */
14361 finish_member_declaration (decl);
14362 }
14363 else
14364 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14365 }
14366 }
14367 else
14368 {
14369 /* See if these declarations will be friends. */
14370 friend_p = cp_parser_friend_p (&decl_specifiers);
14371
14372 /* Keep going until we hit the `;' at the end of the
14373 declaration. */
14374 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14375 {
14376 tree attributes = NULL_TREE;
14377 tree first_attribute;
14378
14379 /* Peek at the next token. */
14380 token = cp_lexer_peek_token (parser->lexer);
14381
14382 /* Check for a bitfield declaration. */
14383 if (token->type == CPP_COLON
14384 || (token->type == CPP_NAME
14385 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14386 == CPP_COLON))
14387 {
14388 tree identifier;
14389 tree width;
14390
14391 /* Get the name of the bitfield. Note that we cannot just
14392 check TOKEN here because it may have been invalidated by
14393 the call to cp_lexer_peek_nth_token above. */
14394 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14395 identifier = cp_parser_identifier (parser);
14396 else
14397 identifier = NULL_TREE;
14398
14399 /* Consume the `:' token. */
14400 cp_lexer_consume_token (parser->lexer);
14401 /* Get the width of the bitfield. */
14402 width
14403 = cp_parser_constant_expression (parser,
14404 /*allow_non_constant=*/false,
14405 NULL);
14406
14407 /* Look for attributes that apply to the bitfield. */
14408 attributes = cp_parser_attributes_opt (parser);
14409 /* Remember which attributes are prefix attributes and
14410 which are not. */
14411 first_attribute = attributes;
14412 /* Combine the attributes. */
14413 attributes = chainon (prefix_attributes, attributes);
14414
14415 /* Create the bitfield declaration. */
14416 decl = grokbitfield (identifier
14417 ? make_id_declarator (NULL_TREE,
14418 identifier,
14419 sfk_none)
14420 : NULL,
14421 &decl_specifiers,
14422 width);
14423 /* Apply the attributes. */
14424 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14425 }
14426 else
14427 {
14428 cp_declarator *declarator;
14429 tree initializer;
14430 tree asm_specification;
14431 int ctor_dtor_or_conv_p;
14432
14433 /* Parse the declarator. */
14434 declarator
14435 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14436 &ctor_dtor_or_conv_p,
14437 /*parenthesized_p=*/NULL,
14438 /*member_p=*/true);
14439
14440 /* If something went wrong parsing the declarator, make sure
14441 that we at least consume some tokens. */
14442 if (declarator == cp_error_declarator)
14443 {
14444 /* Skip to the end of the statement. */
14445 cp_parser_skip_to_end_of_statement (parser);
14446 /* If the next token is not a semicolon, that is
14447 probably because we just skipped over the body of
14448 a function. So, we consume a semicolon if
14449 present, but do not issue an error message if it
14450 is not present. */
14451 if (cp_lexer_next_token_is (parser->lexer,
14452 CPP_SEMICOLON))
14453 cp_lexer_consume_token (parser->lexer);
14454 return;
14455 }
14456
14457 if (declares_class_or_enum & 2)
14458 cp_parser_check_for_definition_in_return_type
14459 (declarator, decl_specifiers.type);
14460
14461 /* Look for an asm-specification. */
14462 asm_specification = cp_parser_asm_specification_opt (parser);
14463 /* Look for attributes that apply to the declaration. */
14464 attributes = cp_parser_attributes_opt (parser);
14465 /* Remember which attributes are prefix attributes and
14466 which are not. */
14467 first_attribute = attributes;
14468 /* Combine the attributes. */
14469 attributes = chainon (prefix_attributes, attributes);
14470
14471 /* If it's an `=', then we have a constant-initializer or a
14472 pure-specifier. It is not correct to parse the
14473 initializer before registering the member declaration
14474 since the member declaration should be in scope while
14475 its initializer is processed. However, the rest of the
14476 front end does not yet provide an interface that allows
14477 us to handle this correctly. */
14478 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14479 {
14480 /* In [class.mem]:
14481
14482 A pure-specifier shall be used only in the declaration of
14483 a virtual function.
14484
14485 A member-declarator can contain a constant-initializer
14486 only if it declares a static member of integral or
14487 enumeration type.
14488
14489 Therefore, if the DECLARATOR is for a function, we look
14490 for a pure-specifier; otherwise, we look for a
14491 constant-initializer. When we call `grokfield', it will
14492 perform more stringent semantics checks. */
14493 if (function_declarator_p (declarator))
14494 initializer = cp_parser_pure_specifier (parser);
14495 else
14496 /* Parse the initializer. */
14497 initializer = cp_parser_constant_initializer (parser);
14498 }
14499 /* Otherwise, there is no initializer. */
14500 else
14501 initializer = NULL_TREE;
14502
14503 /* See if we are probably looking at a function
14504 definition. We are certainly not looking at a
14505 member-declarator. Calling `grokfield' has
14506 side-effects, so we must not do it unless we are sure
14507 that we are looking at a member-declarator. */
14508 if (cp_parser_token_starts_function_definition_p
14509 (cp_lexer_peek_token (parser->lexer)))
14510 {
14511 /* The grammar does not allow a pure-specifier to be
14512 used when a member function is defined. (It is
14513 possible that this fact is an oversight in the
14514 standard, since a pure function may be defined
14515 outside of the class-specifier. */
14516 if (initializer)
14517 error ("pure-specifier on function-definition");
14518 decl = cp_parser_save_member_function_body (parser,
14519 &decl_specifiers,
14520 declarator,
14521 attributes);
14522 /* If the member was not a friend, declare it here. */
14523 if (!friend_p)
14524 finish_member_declaration (decl);
14525 /* Peek at the next token. */
14526 token = cp_lexer_peek_token (parser->lexer);
14527 /* If the next token is a semicolon, consume it. */
14528 if (token->type == CPP_SEMICOLON)
14529 {
14530 if (pedantic && !in_system_header)
14531 pedwarn ("extra %<;%>");
14532 cp_lexer_consume_token (parser->lexer);
14533 }
14534 return;
14535 }
14536 else
14537 /* Create the declaration. */
14538 decl = grokfield (declarator, &decl_specifiers,
14539 initializer, /*init_const_expr_p=*/true,
14540 asm_specification,
14541 attributes);
14542 }
14543
14544 /* Reset PREFIX_ATTRIBUTES. */
14545 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14546 attributes = TREE_CHAIN (attributes);
14547 if (attributes)
14548 TREE_CHAIN (attributes) = NULL_TREE;
14549
14550 /* If there is any qualification still in effect, clear it
14551 now; we will be starting fresh with the next declarator. */
14552 parser->scope = NULL_TREE;
14553 parser->qualifying_scope = NULL_TREE;
14554 parser->object_scope = NULL_TREE;
14555 /* If it's a `,', then there are more declarators. */
14556 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14557 cp_lexer_consume_token (parser->lexer);
14558 /* If the next token isn't a `;', then we have a parse error. */
14559 else if (cp_lexer_next_token_is_not (parser->lexer,
14560 CPP_SEMICOLON))
14561 {
14562 cp_parser_error (parser, "expected %<;%>");
14563 /* Skip tokens until we find a `;'. */
14564 cp_parser_skip_to_end_of_statement (parser);
14565
14566 break;
14567 }
14568
14569 if (decl)
14570 {
14571 /* Add DECL to the list of members. */
14572 if (!friend_p)
14573 finish_member_declaration (decl);
14574
14575 if (TREE_CODE (decl) == FUNCTION_DECL)
14576 cp_parser_save_default_args (parser, decl);
14577 }
14578 }
14579 }
14580
14581 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14582 }
14583
14584 /* Parse a pure-specifier.
14585
14586 pure-specifier:
14587 = 0
14588
14589 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14590 Otherwise, ERROR_MARK_NODE is returned. */
14591
14592 static tree
14593 cp_parser_pure_specifier (cp_parser* parser)
14594 {
14595 cp_token *token;
14596
14597 /* Look for the `=' token. */
14598 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14599 return error_mark_node;
14600 /* Look for the `0' token. */
14601 token = cp_lexer_consume_token (parser->lexer);
14602 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14603 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14604 {
14605 cp_parser_error (parser,
14606 "invalid pure specifier (only `= 0' is allowed)");
14607 cp_parser_skip_to_end_of_statement (parser);
14608 return error_mark_node;
14609 }
14610 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14611 {
14612 error ("templates may not be %<virtual%>");
14613 return error_mark_node;
14614 }
14615
14616 return integer_zero_node;
14617 }
14618
14619 /* Parse a constant-initializer.
14620
14621 constant-initializer:
14622 = constant-expression
14623
14624 Returns a representation of the constant-expression. */
14625
14626 static tree
14627 cp_parser_constant_initializer (cp_parser* parser)
14628 {
14629 /* Look for the `=' token. */
14630 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14631 return error_mark_node;
14632
14633 /* It is invalid to write:
14634
14635 struct S { static const int i = { 7 }; };
14636
14637 */
14638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14639 {
14640 cp_parser_error (parser,
14641 "a brace-enclosed initializer is not allowed here");
14642 /* Consume the opening brace. */
14643 cp_lexer_consume_token (parser->lexer);
14644 /* Skip the initializer. */
14645 cp_parser_skip_to_closing_brace (parser);
14646 /* Look for the trailing `}'. */
14647 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14648
14649 return error_mark_node;
14650 }
14651
14652 return cp_parser_constant_expression (parser,
14653 /*allow_non_constant=*/false,
14654 NULL);
14655 }
14656
14657 /* Derived classes [gram.class.derived] */
14658
14659 /* Parse a base-clause.
14660
14661 base-clause:
14662 : base-specifier-list
14663
14664 base-specifier-list:
14665 base-specifier ... [opt]
14666 base-specifier-list , base-specifier ... [opt]
14667
14668 Returns a TREE_LIST representing the base-classes, in the order in
14669 which they were declared. The representation of each node is as
14670 described by cp_parser_base_specifier.
14671
14672 In the case that no bases are specified, this function will return
14673 NULL_TREE, not ERROR_MARK_NODE. */
14674
14675 static tree
14676 cp_parser_base_clause (cp_parser* parser)
14677 {
14678 tree bases = NULL_TREE;
14679
14680 /* Look for the `:' that begins the list. */
14681 cp_parser_require (parser, CPP_COLON, "`:'");
14682
14683 /* Scan the base-specifier-list. */
14684 while (true)
14685 {
14686 cp_token *token;
14687 tree base;
14688 bool pack_expansion_p = false;
14689
14690 /* Look for the base-specifier. */
14691 base = cp_parser_base_specifier (parser);
14692 /* Look for the (optional) ellipsis. */
14693 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14694 {
14695 /* Consume the `...'. */
14696 cp_lexer_consume_token (parser->lexer);
14697
14698 pack_expansion_p = true;
14699 }
14700
14701 /* Add BASE to the front of the list. */
14702 if (base != error_mark_node)
14703 {
14704 if (pack_expansion_p)
14705 /* Make this a pack expansion type. */
14706 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14707 else
14708 check_for_bare_parameter_packs (TREE_VALUE (base));
14709
14710 TREE_CHAIN (base) = bases;
14711 bases = base;
14712 }
14713 /* Peek at the next token. */
14714 token = cp_lexer_peek_token (parser->lexer);
14715 /* If it's not a comma, then the list is complete. */
14716 if (token->type != CPP_COMMA)
14717 break;
14718 /* Consume the `,'. */
14719 cp_lexer_consume_token (parser->lexer);
14720 }
14721
14722 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14723 base class had a qualified name. However, the next name that
14724 appears is certainly not qualified. */
14725 parser->scope = NULL_TREE;
14726 parser->qualifying_scope = NULL_TREE;
14727 parser->object_scope = NULL_TREE;
14728
14729 return nreverse (bases);
14730 }
14731
14732 /* Parse a base-specifier.
14733
14734 base-specifier:
14735 :: [opt] nested-name-specifier [opt] class-name
14736 virtual access-specifier [opt] :: [opt] nested-name-specifier
14737 [opt] class-name
14738 access-specifier virtual [opt] :: [opt] nested-name-specifier
14739 [opt] class-name
14740
14741 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14742 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14743 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14744 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14745
14746 static tree
14747 cp_parser_base_specifier (cp_parser* parser)
14748 {
14749 cp_token *token;
14750 bool done = false;
14751 bool virtual_p = false;
14752 bool duplicate_virtual_error_issued_p = false;
14753 bool duplicate_access_error_issued_p = false;
14754 bool class_scope_p, template_p;
14755 tree access = access_default_node;
14756 tree type;
14757
14758 /* Process the optional `virtual' and `access-specifier'. */
14759 while (!done)
14760 {
14761 /* Peek at the next token. */
14762 token = cp_lexer_peek_token (parser->lexer);
14763 /* Process `virtual'. */
14764 switch (token->keyword)
14765 {
14766 case RID_VIRTUAL:
14767 /* If `virtual' appears more than once, issue an error. */
14768 if (virtual_p && !duplicate_virtual_error_issued_p)
14769 {
14770 cp_parser_error (parser,
14771 "%<virtual%> specified more than once in base-specified");
14772 duplicate_virtual_error_issued_p = true;
14773 }
14774
14775 virtual_p = true;
14776
14777 /* Consume the `virtual' token. */
14778 cp_lexer_consume_token (parser->lexer);
14779
14780 break;
14781
14782 case RID_PUBLIC:
14783 case RID_PROTECTED:
14784 case RID_PRIVATE:
14785 /* If more than one access specifier appears, issue an
14786 error. */
14787 if (access != access_default_node
14788 && !duplicate_access_error_issued_p)
14789 {
14790 cp_parser_error (parser,
14791 "more than one access specifier in base-specified");
14792 duplicate_access_error_issued_p = true;
14793 }
14794
14795 access = ridpointers[(int) token->keyword];
14796
14797 /* Consume the access-specifier. */
14798 cp_lexer_consume_token (parser->lexer);
14799
14800 break;
14801
14802 default:
14803 done = true;
14804 break;
14805 }
14806 }
14807 /* It is not uncommon to see programs mechanically, erroneously, use
14808 the 'typename' keyword to denote (dependent) qualified types
14809 as base classes. */
14810 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14811 {
14812 if (!processing_template_decl)
14813 error ("keyword %<typename%> not allowed outside of templates");
14814 else
14815 error ("keyword %<typename%> not allowed in this context "
14816 "(the base class is implicitly a type)");
14817 cp_lexer_consume_token (parser->lexer);
14818 }
14819
14820 /* Look for the optional `::' operator. */
14821 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14822 /* Look for the nested-name-specifier. The simplest way to
14823 implement:
14824
14825 [temp.res]
14826
14827 The keyword `typename' is not permitted in a base-specifier or
14828 mem-initializer; in these contexts a qualified name that
14829 depends on a template-parameter is implicitly assumed to be a
14830 type name.
14831
14832 is to pretend that we have seen the `typename' keyword at this
14833 point. */
14834 cp_parser_nested_name_specifier_opt (parser,
14835 /*typename_keyword_p=*/true,
14836 /*check_dependency_p=*/true,
14837 typename_type,
14838 /*is_declaration=*/true);
14839 /* If the base class is given by a qualified name, assume that names
14840 we see are type names or templates, as appropriate. */
14841 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14842 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14843
14844 /* Finally, look for the class-name. */
14845 type = cp_parser_class_name (parser,
14846 class_scope_p,
14847 template_p,
14848 typename_type,
14849 /*check_dependency_p=*/true,
14850 /*class_head_p=*/false,
14851 /*is_declaration=*/true);
14852
14853 if (type == error_mark_node)
14854 return error_mark_node;
14855
14856 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14857 }
14858
14859 /* Exception handling [gram.exception] */
14860
14861 /* Parse an (optional) exception-specification.
14862
14863 exception-specification:
14864 throw ( type-id-list [opt] )
14865
14866 Returns a TREE_LIST representing the exception-specification. The
14867 TREE_VALUE of each node is a type. */
14868
14869 static tree
14870 cp_parser_exception_specification_opt (cp_parser* parser)
14871 {
14872 cp_token *token;
14873 tree type_id_list;
14874
14875 /* Peek at the next token. */
14876 token = cp_lexer_peek_token (parser->lexer);
14877 /* If it's not `throw', then there's no exception-specification. */
14878 if (!cp_parser_is_keyword (token, RID_THROW))
14879 return NULL_TREE;
14880
14881 /* Consume the `throw'. */
14882 cp_lexer_consume_token (parser->lexer);
14883
14884 /* Look for the `('. */
14885 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14886
14887 /* Peek at the next token. */
14888 token = cp_lexer_peek_token (parser->lexer);
14889 /* If it's not a `)', then there is a type-id-list. */
14890 if (token->type != CPP_CLOSE_PAREN)
14891 {
14892 const char *saved_message;
14893
14894 /* Types may not be defined in an exception-specification. */
14895 saved_message = parser->type_definition_forbidden_message;
14896 parser->type_definition_forbidden_message
14897 = "types may not be defined in an exception-specification";
14898 /* Parse the type-id-list. */
14899 type_id_list = cp_parser_type_id_list (parser);
14900 /* Restore the saved message. */
14901 parser->type_definition_forbidden_message = saved_message;
14902 }
14903 else
14904 type_id_list = empty_except_spec;
14905
14906 /* Look for the `)'. */
14907 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14908
14909 return type_id_list;
14910 }
14911
14912 /* Parse an (optional) type-id-list.
14913
14914 type-id-list:
14915 type-id ... [opt]
14916 type-id-list , type-id ... [opt]
14917
14918 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14919 in the order that the types were presented. */
14920
14921 static tree
14922 cp_parser_type_id_list (cp_parser* parser)
14923 {
14924 tree types = NULL_TREE;
14925
14926 while (true)
14927 {
14928 cp_token *token;
14929 tree type;
14930
14931 /* Get the next type-id. */
14932 type = cp_parser_type_id (parser);
14933 /* Parse the optional ellipsis. */
14934 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14935 {
14936 /* Consume the `...'. */
14937 cp_lexer_consume_token (parser->lexer);
14938
14939 /* Turn the type into a pack expansion expression. */
14940 type = make_pack_expansion (type);
14941 }
14942 /* Add it to the list. */
14943 types = add_exception_specifier (types, type, /*complain=*/1);
14944 /* Peek at the next token. */
14945 token = cp_lexer_peek_token (parser->lexer);
14946 /* If it is not a `,', we are done. */
14947 if (token->type != CPP_COMMA)
14948 break;
14949 /* Consume the `,'. */
14950 cp_lexer_consume_token (parser->lexer);
14951 }
14952
14953 return nreverse (types);
14954 }
14955
14956 /* Parse a try-block.
14957
14958 try-block:
14959 try compound-statement handler-seq */
14960
14961 static tree
14962 cp_parser_try_block (cp_parser* parser)
14963 {
14964 tree try_block;
14965
14966 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14967 try_block = begin_try_block ();
14968 cp_parser_compound_statement (parser, NULL, true);
14969 finish_try_block (try_block);
14970 cp_parser_handler_seq (parser);
14971 finish_handler_sequence (try_block);
14972
14973 return try_block;
14974 }
14975
14976 /* Parse a function-try-block.
14977
14978 function-try-block:
14979 try ctor-initializer [opt] function-body handler-seq */
14980
14981 static bool
14982 cp_parser_function_try_block (cp_parser* parser)
14983 {
14984 tree compound_stmt;
14985 tree try_block;
14986 bool ctor_initializer_p;
14987
14988 /* Look for the `try' keyword. */
14989 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14990 return false;
14991 /* Let the rest of the front end know where we are. */
14992 try_block = begin_function_try_block (&compound_stmt);
14993 /* Parse the function-body. */
14994 ctor_initializer_p
14995 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14996 /* We're done with the `try' part. */
14997 finish_function_try_block (try_block);
14998 /* Parse the handlers. */
14999 cp_parser_handler_seq (parser);
15000 /* We're done with the handlers. */
15001 finish_function_handler_sequence (try_block, compound_stmt);
15002
15003 return ctor_initializer_p;
15004 }
15005
15006 /* Parse a handler-seq.
15007
15008 handler-seq:
15009 handler handler-seq [opt] */
15010
15011 static void
15012 cp_parser_handler_seq (cp_parser* parser)
15013 {
15014 while (true)
15015 {
15016 cp_token *token;
15017
15018 /* Parse the handler. */
15019 cp_parser_handler (parser);
15020 /* Peek at the next token. */
15021 token = cp_lexer_peek_token (parser->lexer);
15022 /* If it's not `catch' then there are no more handlers. */
15023 if (!cp_parser_is_keyword (token, RID_CATCH))
15024 break;
15025 }
15026 }
15027
15028 /* Parse a handler.
15029
15030 handler:
15031 catch ( exception-declaration ) compound-statement */
15032
15033 static void
15034 cp_parser_handler (cp_parser* parser)
15035 {
15036 tree handler;
15037 tree declaration;
15038
15039 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15040 handler = begin_handler ();
15041 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15042 declaration = cp_parser_exception_declaration (parser);
15043 finish_handler_parms (declaration, handler);
15044 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15045 cp_parser_compound_statement (parser, NULL, false);
15046 finish_handler (handler);
15047 }
15048
15049 /* Parse an exception-declaration.
15050
15051 exception-declaration:
15052 type-specifier-seq declarator
15053 type-specifier-seq abstract-declarator
15054 type-specifier-seq
15055 ...
15056
15057 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15058 ellipsis variant is used. */
15059
15060 static tree
15061 cp_parser_exception_declaration (cp_parser* parser)
15062 {
15063 cp_decl_specifier_seq type_specifiers;
15064 cp_declarator *declarator;
15065 const char *saved_message;
15066
15067 /* If it's an ellipsis, it's easy to handle. */
15068 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15069 {
15070 /* Consume the `...' token. */
15071 cp_lexer_consume_token (parser->lexer);
15072 return NULL_TREE;
15073 }
15074
15075 /* Types may not be defined in exception-declarations. */
15076 saved_message = parser->type_definition_forbidden_message;
15077 parser->type_definition_forbidden_message
15078 = "types may not be defined in exception-declarations";
15079
15080 /* Parse the type-specifier-seq. */
15081 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15082 &type_specifiers);
15083 /* If it's a `)', then there is no declarator. */
15084 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15085 declarator = NULL;
15086 else
15087 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15088 /*ctor_dtor_or_conv_p=*/NULL,
15089 /*parenthesized_p=*/NULL,
15090 /*member_p=*/false);
15091
15092 /* Restore the saved message. */
15093 parser->type_definition_forbidden_message = saved_message;
15094
15095 if (!type_specifiers.any_specifiers_p)
15096 return error_mark_node;
15097
15098 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15099 }
15100
15101 /* Parse a throw-expression.
15102
15103 throw-expression:
15104 throw assignment-expression [opt]
15105
15106 Returns a THROW_EXPR representing the throw-expression. */
15107
15108 static tree
15109 cp_parser_throw_expression (cp_parser* parser)
15110 {
15111 tree expression;
15112 cp_token* token;
15113
15114 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15115 token = cp_lexer_peek_token (parser->lexer);
15116 /* Figure out whether or not there is an assignment-expression
15117 following the "throw" keyword. */
15118 if (token->type == CPP_COMMA
15119 || token->type == CPP_SEMICOLON
15120 || token->type == CPP_CLOSE_PAREN
15121 || token->type == CPP_CLOSE_SQUARE
15122 || token->type == CPP_CLOSE_BRACE
15123 || token->type == CPP_COLON)
15124 expression = NULL_TREE;
15125 else
15126 expression = cp_parser_assignment_expression (parser,
15127 /*cast_p=*/false);
15128
15129 return build_throw (expression);
15130 }
15131
15132 /* GNU Extensions */
15133
15134 /* Parse an (optional) asm-specification.
15135
15136 asm-specification:
15137 asm ( string-literal )
15138
15139 If the asm-specification is present, returns a STRING_CST
15140 corresponding to the string-literal. Otherwise, returns
15141 NULL_TREE. */
15142
15143 static tree
15144 cp_parser_asm_specification_opt (cp_parser* parser)
15145 {
15146 cp_token *token;
15147 tree asm_specification;
15148
15149 /* Peek at the next token. */
15150 token = cp_lexer_peek_token (parser->lexer);
15151 /* If the next token isn't the `asm' keyword, then there's no
15152 asm-specification. */
15153 if (!cp_parser_is_keyword (token, RID_ASM))
15154 return NULL_TREE;
15155
15156 /* Consume the `asm' token. */
15157 cp_lexer_consume_token (parser->lexer);
15158 /* Look for the `('. */
15159 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15160
15161 /* Look for the string-literal. */
15162 asm_specification = cp_parser_string_literal (parser, false, false);
15163
15164 /* Look for the `)'. */
15165 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15166
15167 return asm_specification;
15168 }
15169
15170 /* Parse an asm-operand-list.
15171
15172 asm-operand-list:
15173 asm-operand
15174 asm-operand-list , asm-operand
15175
15176 asm-operand:
15177 string-literal ( expression )
15178 [ string-literal ] string-literal ( expression )
15179
15180 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15181 each node is the expression. The TREE_PURPOSE is itself a
15182 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15183 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15184 is a STRING_CST for the string literal before the parenthesis. */
15185
15186 static tree
15187 cp_parser_asm_operand_list (cp_parser* parser)
15188 {
15189 tree asm_operands = NULL_TREE;
15190
15191 while (true)
15192 {
15193 tree string_literal;
15194 tree expression;
15195 tree name;
15196
15197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15198 {
15199 /* Consume the `[' token. */
15200 cp_lexer_consume_token (parser->lexer);
15201 /* Read the operand name. */
15202 name = cp_parser_identifier (parser);
15203 if (name != error_mark_node)
15204 name = build_string (IDENTIFIER_LENGTH (name),
15205 IDENTIFIER_POINTER (name));
15206 /* Look for the closing `]'. */
15207 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15208 }
15209 else
15210 name = NULL_TREE;
15211 /* Look for the string-literal. */
15212 string_literal = cp_parser_string_literal (parser, false, false);
15213
15214 /* Look for the `('. */
15215 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15216 /* Parse the expression. */
15217 expression = cp_parser_expression (parser, /*cast_p=*/false);
15218 /* Look for the `)'. */
15219 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15220
15221 /* Add this operand to the list. */
15222 asm_operands = tree_cons (build_tree_list (name, string_literal),
15223 expression,
15224 asm_operands);
15225 /* If the next token is not a `,', there are no more
15226 operands. */
15227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15228 break;
15229 /* Consume the `,'. */
15230 cp_lexer_consume_token (parser->lexer);
15231 }
15232
15233 return nreverse (asm_operands);
15234 }
15235
15236 /* Parse an asm-clobber-list.
15237
15238 asm-clobber-list:
15239 string-literal
15240 asm-clobber-list , string-literal
15241
15242 Returns a TREE_LIST, indicating the clobbers in the order that they
15243 appeared. The TREE_VALUE of each node is a STRING_CST. */
15244
15245 static tree
15246 cp_parser_asm_clobber_list (cp_parser* parser)
15247 {
15248 tree clobbers = NULL_TREE;
15249
15250 while (true)
15251 {
15252 tree string_literal;
15253
15254 /* Look for the string literal. */
15255 string_literal = cp_parser_string_literal (parser, false, false);
15256 /* Add it to the list. */
15257 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15258 /* If the next token is not a `,', then the list is
15259 complete. */
15260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15261 break;
15262 /* Consume the `,' token. */
15263 cp_lexer_consume_token (parser->lexer);
15264 }
15265
15266 return clobbers;
15267 }
15268
15269 /* Parse an (optional) series of attributes.
15270
15271 attributes:
15272 attributes attribute
15273
15274 attribute:
15275 __attribute__ (( attribute-list [opt] ))
15276
15277 The return value is as for cp_parser_attribute_list. */
15278
15279 static tree
15280 cp_parser_attributes_opt (cp_parser* parser)
15281 {
15282 tree attributes = NULL_TREE;
15283
15284 while (true)
15285 {
15286 cp_token *token;
15287 tree attribute_list;
15288
15289 /* Peek at the next token. */
15290 token = cp_lexer_peek_token (parser->lexer);
15291 /* If it's not `__attribute__', then we're done. */
15292 if (token->keyword != RID_ATTRIBUTE)
15293 break;
15294
15295 /* Consume the `__attribute__' keyword. */
15296 cp_lexer_consume_token (parser->lexer);
15297 /* Look for the two `(' tokens. */
15298 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15299 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15300
15301 /* Peek at the next token. */
15302 token = cp_lexer_peek_token (parser->lexer);
15303 if (token->type != CPP_CLOSE_PAREN)
15304 /* Parse the attribute-list. */
15305 attribute_list = cp_parser_attribute_list (parser);
15306 else
15307 /* If the next token is a `)', then there is no attribute
15308 list. */
15309 attribute_list = NULL;
15310
15311 /* Look for the two `)' tokens. */
15312 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15313 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15314
15315 /* Add these new attributes to the list. */
15316 attributes = chainon (attributes, attribute_list);
15317 }
15318
15319 return attributes;
15320 }
15321
15322 /* Parse an attribute-list.
15323
15324 attribute-list:
15325 attribute
15326 attribute-list , attribute
15327
15328 attribute:
15329 identifier
15330 identifier ( identifier )
15331 identifier ( identifier , expression-list )
15332 identifier ( expression-list )
15333
15334 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15335 to an attribute. The TREE_PURPOSE of each node is the identifier
15336 indicating which attribute is in use. The TREE_VALUE represents
15337 the arguments, if any. */
15338
15339 static tree
15340 cp_parser_attribute_list (cp_parser* parser)
15341 {
15342 tree attribute_list = NULL_TREE;
15343 bool save_translate_strings_p = parser->translate_strings_p;
15344
15345 parser->translate_strings_p = false;
15346 while (true)
15347 {
15348 cp_token *token;
15349 tree identifier;
15350 tree attribute;
15351
15352 /* Look for the identifier. We also allow keywords here; for
15353 example `__attribute__ ((const))' is legal. */
15354 token = cp_lexer_peek_token (parser->lexer);
15355 if (token->type == CPP_NAME
15356 || token->type == CPP_KEYWORD)
15357 {
15358 tree arguments = NULL_TREE;
15359
15360 /* Consume the token. */
15361 token = cp_lexer_consume_token (parser->lexer);
15362
15363 /* Save away the identifier that indicates which attribute
15364 this is. */
15365 identifier = token->u.value;
15366 attribute = build_tree_list (identifier, NULL_TREE);
15367
15368 /* Peek at the next token. */
15369 token = cp_lexer_peek_token (parser->lexer);
15370 /* If it's an `(', then parse the attribute arguments. */
15371 if (token->type == CPP_OPEN_PAREN)
15372 {
15373 arguments = cp_parser_parenthesized_expression_list
15374 (parser, true, /*cast_p=*/false,
15375 /*allow_expansion_p=*/false,
15376 /*non_constant_p=*/NULL);
15377 /* Save the arguments away. */
15378 TREE_VALUE (attribute) = arguments;
15379 }
15380
15381 if (arguments != error_mark_node)
15382 {
15383 /* Add this attribute to the list. */
15384 TREE_CHAIN (attribute) = attribute_list;
15385 attribute_list = attribute;
15386 }
15387
15388 token = cp_lexer_peek_token (parser->lexer);
15389 }
15390 /* Now, look for more attributes. If the next token isn't a
15391 `,', we're done. */
15392 if (token->type != CPP_COMMA)
15393 break;
15394
15395 /* Consume the comma and keep going. */
15396 cp_lexer_consume_token (parser->lexer);
15397 }
15398 parser->translate_strings_p = save_translate_strings_p;
15399
15400 /* We built up the list in reverse order. */
15401 return nreverse (attribute_list);
15402 }
15403
15404 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15405 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15406 current value of the PEDANTIC flag, regardless of whether or not
15407 the `__extension__' keyword is present. The caller is responsible
15408 for restoring the value of the PEDANTIC flag. */
15409
15410 static bool
15411 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15412 {
15413 /* Save the old value of the PEDANTIC flag. */
15414 *saved_pedantic = pedantic;
15415
15416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15417 {
15418 /* Consume the `__extension__' token. */
15419 cp_lexer_consume_token (parser->lexer);
15420 /* We're not being pedantic while the `__extension__' keyword is
15421 in effect. */
15422 pedantic = 0;
15423
15424 return true;
15425 }
15426
15427 return false;
15428 }
15429
15430 /* Parse a label declaration.
15431
15432 label-declaration:
15433 __label__ label-declarator-seq ;
15434
15435 label-declarator-seq:
15436 identifier , label-declarator-seq
15437 identifier */
15438
15439 static void
15440 cp_parser_label_declaration (cp_parser* parser)
15441 {
15442 /* Look for the `__label__' keyword. */
15443 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15444
15445 while (true)
15446 {
15447 tree identifier;
15448
15449 /* Look for an identifier. */
15450 identifier = cp_parser_identifier (parser);
15451 /* If we failed, stop. */
15452 if (identifier == error_mark_node)
15453 break;
15454 /* Declare it as a label. */
15455 finish_label_decl (identifier);
15456 /* If the next token is a `;', stop. */
15457 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15458 break;
15459 /* Look for the `,' separating the label declarations. */
15460 cp_parser_require (parser, CPP_COMMA, "`,'");
15461 }
15462
15463 /* Look for the final `;'. */
15464 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15465 }
15466
15467 /* Support Functions */
15468
15469 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15470 NAME should have one of the representations used for an
15471 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15472 is returned. If PARSER->SCOPE is a dependent type, then a
15473 SCOPE_REF is returned.
15474
15475 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15476 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15477 was formed. Abstractly, such entities should not be passed to this
15478 function, because they do not need to be looked up, but it is
15479 simpler to check for this special case here, rather than at the
15480 call-sites.
15481
15482 In cases not explicitly covered above, this function returns a
15483 DECL, OVERLOAD, or baselink representing the result of the lookup.
15484 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15485 is returned.
15486
15487 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15488 (e.g., "struct") that was used. In that case bindings that do not
15489 refer to types are ignored.
15490
15491 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15492 ignored.
15493
15494 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15495 are ignored.
15496
15497 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15498 types.
15499
15500 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15501 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15502 NULL_TREE otherwise. */
15503
15504 static tree
15505 cp_parser_lookup_name (cp_parser *parser, tree name,
15506 enum tag_types tag_type,
15507 bool is_template,
15508 bool is_namespace,
15509 bool check_dependency,
15510 tree *ambiguous_decls)
15511 {
15512 int flags = 0;
15513 tree decl;
15514 tree object_type = parser->context->object_type;
15515
15516 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15517 flags |= LOOKUP_COMPLAIN;
15518
15519 /* Assume that the lookup will be unambiguous. */
15520 if (ambiguous_decls)
15521 *ambiguous_decls = NULL_TREE;
15522
15523 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15524 no longer valid. Note that if we are parsing tentatively, and
15525 the parse fails, OBJECT_TYPE will be automatically restored. */
15526 parser->context->object_type = NULL_TREE;
15527
15528 if (name == error_mark_node)
15529 return error_mark_node;
15530
15531 /* A template-id has already been resolved; there is no lookup to
15532 do. */
15533 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15534 return name;
15535 if (BASELINK_P (name))
15536 {
15537 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15538 == TEMPLATE_ID_EXPR);
15539 return name;
15540 }
15541
15542 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15543 it should already have been checked to make sure that the name
15544 used matches the type being destroyed. */
15545 if (TREE_CODE (name) == BIT_NOT_EXPR)
15546 {
15547 tree type;
15548
15549 /* Figure out to which type this destructor applies. */
15550 if (parser->scope)
15551 type = parser->scope;
15552 else if (object_type)
15553 type = object_type;
15554 else
15555 type = current_class_type;
15556 /* If that's not a class type, there is no destructor. */
15557 if (!type || !CLASS_TYPE_P (type))
15558 return error_mark_node;
15559 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15560 lazily_declare_fn (sfk_destructor, type);
15561 if (!CLASSTYPE_DESTRUCTORS (type))
15562 return error_mark_node;
15563 /* If it was a class type, return the destructor. */
15564 return CLASSTYPE_DESTRUCTORS (type);
15565 }
15566
15567 /* By this point, the NAME should be an ordinary identifier. If
15568 the id-expression was a qualified name, the qualifying scope is
15569 stored in PARSER->SCOPE at this point. */
15570 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15571
15572 /* Perform the lookup. */
15573 if (parser->scope)
15574 {
15575 bool dependent_p;
15576
15577 if (parser->scope == error_mark_node)
15578 return error_mark_node;
15579
15580 /* If the SCOPE is dependent, the lookup must be deferred until
15581 the template is instantiated -- unless we are explicitly
15582 looking up names in uninstantiated templates. Even then, we
15583 cannot look up the name if the scope is not a class type; it
15584 might, for example, be a template type parameter. */
15585 dependent_p = (TYPE_P (parser->scope)
15586 && !(parser->in_declarator_p
15587 && currently_open_class (parser->scope))
15588 && dependent_type_p (parser->scope));
15589 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15590 && dependent_p)
15591 {
15592 if (tag_type)
15593 {
15594 tree type;
15595
15596 /* The resolution to Core Issue 180 says that `struct
15597 A::B' should be considered a type-name, even if `A'
15598 is dependent. */
15599 type = make_typename_type (parser->scope, name, tag_type,
15600 /*complain=*/tf_error);
15601 decl = TYPE_NAME (type);
15602 }
15603 else if (is_template
15604 && (cp_parser_next_token_ends_template_argument_p (parser)
15605 || cp_lexer_next_token_is (parser->lexer,
15606 CPP_CLOSE_PAREN)))
15607 decl = make_unbound_class_template (parser->scope,
15608 name, NULL_TREE,
15609 /*complain=*/tf_error);
15610 else
15611 decl = build_qualified_name (/*type=*/NULL_TREE,
15612 parser->scope, name,
15613 is_template);
15614 }
15615 else
15616 {
15617 tree pushed_scope = NULL_TREE;
15618
15619 /* If PARSER->SCOPE is a dependent type, then it must be a
15620 class type, and we must not be checking dependencies;
15621 otherwise, we would have processed this lookup above. So
15622 that PARSER->SCOPE is not considered a dependent base by
15623 lookup_member, we must enter the scope here. */
15624 if (dependent_p)
15625 pushed_scope = push_scope (parser->scope);
15626 /* If the PARSER->SCOPE is a template specialization, it
15627 may be instantiated during name lookup. In that case,
15628 errors may be issued. Even if we rollback the current
15629 tentative parse, those errors are valid. */
15630 decl = lookup_qualified_name (parser->scope, name,
15631 tag_type != none_type,
15632 /*complain=*/true);
15633 if (pushed_scope)
15634 pop_scope (pushed_scope);
15635 }
15636 parser->qualifying_scope = parser->scope;
15637 parser->object_scope = NULL_TREE;
15638 }
15639 else if (object_type)
15640 {
15641 tree object_decl = NULL_TREE;
15642 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15643 OBJECT_TYPE is not a class. */
15644 if (CLASS_TYPE_P (object_type))
15645 /* If the OBJECT_TYPE is a template specialization, it may
15646 be instantiated during name lookup. In that case, errors
15647 may be issued. Even if we rollback the current tentative
15648 parse, those errors are valid. */
15649 object_decl = lookup_member (object_type,
15650 name,
15651 /*protect=*/0,
15652 tag_type != none_type);
15653 /* Look it up in the enclosing context, too. */
15654 decl = lookup_name_real (name, tag_type != none_type,
15655 /*nonclass=*/0,
15656 /*block_p=*/true, is_namespace, flags);
15657 parser->object_scope = object_type;
15658 parser->qualifying_scope = NULL_TREE;
15659 if (object_decl)
15660 decl = object_decl;
15661 }
15662 else
15663 {
15664 decl = lookup_name_real (name, tag_type != none_type,
15665 /*nonclass=*/0,
15666 /*block_p=*/true, is_namespace, flags);
15667 parser->qualifying_scope = NULL_TREE;
15668 parser->object_scope = NULL_TREE;
15669 }
15670
15671 /* If the lookup failed, let our caller know. */
15672 if (!decl || decl == error_mark_node)
15673 return error_mark_node;
15674
15675 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15676 if (TREE_CODE (decl) == TREE_LIST)
15677 {
15678 if (ambiguous_decls)
15679 *ambiguous_decls = decl;
15680 /* The error message we have to print is too complicated for
15681 cp_parser_error, so we incorporate its actions directly. */
15682 if (!cp_parser_simulate_error (parser))
15683 {
15684 error ("reference to %qD is ambiguous", name);
15685 print_candidates (decl);
15686 }
15687 return error_mark_node;
15688 }
15689
15690 gcc_assert (DECL_P (decl)
15691 || TREE_CODE (decl) == OVERLOAD
15692 || TREE_CODE (decl) == SCOPE_REF
15693 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15694 || BASELINK_P (decl));
15695
15696 /* If we have resolved the name of a member declaration, check to
15697 see if the declaration is accessible. When the name resolves to
15698 set of overloaded functions, accessibility is checked when
15699 overload resolution is done.
15700
15701 During an explicit instantiation, access is not checked at all,
15702 as per [temp.explicit]. */
15703 if (DECL_P (decl))
15704 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15705
15706 return decl;
15707 }
15708
15709 /* Like cp_parser_lookup_name, but for use in the typical case where
15710 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15711 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15712
15713 static tree
15714 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15715 {
15716 return cp_parser_lookup_name (parser, name,
15717 none_type,
15718 /*is_template=*/false,
15719 /*is_namespace=*/false,
15720 /*check_dependency=*/true,
15721 /*ambiguous_decls=*/NULL);
15722 }
15723
15724 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15725 the current context, return the TYPE_DECL. If TAG_NAME_P is
15726 true, the DECL indicates the class being defined in a class-head,
15727 or declared in an elaborated-type-specifier.
15728
15729 Otherwise, return DECL. */
15730
15731 static tree
15732 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15733 {
15734 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15735 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15736
15737 struct A {
15738 template <typename T> struct B;
15739 };
15740
15741 template <typename T> struct A::B {};
15742
15743 Similarly, in an elaborated-type-specifier:
15744
15745 namespace N { struct X{}; }
15746
15747 struct A {
15748 template <typename T> friend struct N::X;
15749 };
15750
15751 However, if the DECL refers to a class type, and we are in
15752 the scope of the class, then the name lookup automatically
15753 finds the TYPE_DECL created by build_self_reference rather
15754 than a TEMPLATE_DECL. For example, in:
15755
15756 template <class T> struct S {
15757 S s;
15758 };
15759
15760 there is no need to handle such case. */
15761
15762 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15763 return DECL_TEMPLATE_RESULT (decl);
15764
15765 return decl;
15766 }
15767
15768 /* If too many, or too few, template-parameter lists apply to the
15769 declarator, issue an error message. Returns TRUE if all went well,
15770 and FALSE otherwise. */
15771
15772 static bool
15773 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15774 cp_declarator *declarator)
15775 {
15776 unsigned num_templates;
15777
15778 /* We haven't seen any classes that involve template parameters yet. */
15779 num_templates = 0;
15780
15781 switch (declarator->kind)
15782 {
15783 case cdk_id:
15784 if (declarator->u.id.qualifying_scope)
15785 {
15786 tree scope;
15787 tree member;
15788
15789 scope = declarator->u.id.qualifying_scope;
15790 member = declarator->u.id.unqualified_name;
15791
15792 while (scope && CLASS_TYPE_P (scope))
15793 {
15794 /* You're supposed to have one `template <...>'
15795 for every template class, but you don't need one
15796 for a full specialization. For example:
15797
15798 template <class T> struct S{};
15799 template <> struct S<int> { void f(); };
15800 void S<int>::f () {}
15801
15802 is correct; there shouldn't be a `template <>' for
15803 the definition of `S<int>::f'. */
15804 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15805 /* If SCOPE does not have template information of any
15806 kind, then it is not a template, nor is it nested
15807 within a template. */
15808 break;
15809 if (explicit_class_specialization_p (scope))
15810 break;
15811 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15812 ++num_templates;
15813
15814 scope = TYPE_CONTEXT (scope);
15815 }
15816 }
15817 else if (TREE_CODE (declarator->u.id.unqualified_name)
15818 == TEMPLATE_ID_EXPR)
15819 /* If the DECLARATOR has the form `X<y>' then it uses one
15820 additional level of template parameters. */
15821 ++num_templates;
15822
15823 return cp_parser_check_template_parameters (parser,
15824 num_templates);
15825
15826 case cdk_function:
15827 case cdk_array:
15828 case cdk_pointer:
15829 case cdk_reference:
15830 case cdk_ptrmem:
15831 return (cp_parser_check_declarator_template_parameters
15832 (parser, declarator->declarator));
15833
15834 case cdk_error:
15835 return true;
15836
15837 default:
15838 gcc_unreachable ();
15839 }
15840 return false;
15841 }
15842
15843 /* NUM_TEMPLATES were used in the current declaration. If that is
15844 invalid, return FALSE and issue an error messages. Otherwise,
15845 return TRUE. */
15846
15847 static bool
15848 cp_parser_check_template_parameters (cp_parser* parser,
15849 unsigned num_templates)
15850 {
15851 /* If there are more template classes than parameter lists, we have
15852 something like:
15853
15854 template <class T> void S<T>::R<T>::f (); */
15855 if (parser->num_template_parameter_lists < num_templates)
15856 {
15857 error ("too few template-parameter-lists");
15858 return false;
15859 }
15860 /* If there are the same number of template classes and parameter
15861 lists, that's OK. */
15862 if (parser->num_template_parameter_lists == num_templates)
15863 return true;
15864 /* If there are more, but only one more, then we are referring to a
15865 member template. That's OK too. */
15866 if (parser->num_template_parameter_lists == num_templates + 1)
15867 return true;
15868 /* Otherwise, there are too many template parameter lists. We have
15869 something like:
15870
15871 template <class T> template <class U> void S::f(); */
15872 error ("too many template-parameter-lists");
15873 return false;
15874 }
15875
15876 /* Parse an optional `::' token indicating that the following name is
15877 from the global namespace. If so, PARSER->SCOPE is set to the
15878 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15879 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15880 Returns the new value of PARSER->SCOPE, if the `::' token is
15881 present, and NULL_TREE otherwise. */
15882
15883 static tree
15884 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15885 {
15886 cp_token *token;
15887
15888 /* Peek at the next token. */
15889 token = cp_lexer_peek_token (parser->lexer);
15890 /* If we're looking at a `::' token then we're starting from the
15891 global namespace, not our current location. */
15892 if (token->type == CPP_SCOPE)
15893 {
15894 /* Consume the `::' token. */
15895 cp_lexer_consume_token (parser->lexer);
15896 /* Set the SCOPE so that we know where to start the lookup. */
15897 parser->scope = global_namespace;
15898 parser->qualifying_scope = global_namespace;
15899 parser->object_scope = NULL_TREE;
15900
15901 return parser->scope;
15902 }
15903 else if (!current_scope_valid_p)
15904 {
15905 parser->scope = NULL_TREE;
15906 parser->qualifying_scope = NULL_TREE;
15907 parser->object_scope = NULL_TREE;
15908 }
15909
15910 return NULL_TREE;
15911 }
15912
15913 /* Returns TRUE if the upcoming token sequence is the start of a
15914 constructor declarator. If FRIEND_P is true, the declarator is
15915 preceded by the `friend' specifier. */
15916
15917 static bool
15918 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15919 {
15920 bool constructor_p;
15921 tree type_decl = NULL_TREE;
15922 bool nested_name_p;
15923 cp_token *next_token;
15924
15925 /* The common case is that this is not a constructor declarator, so
15926 try to avoid doing lots of work if at all possible. It's not
15927 valid declare a constructor at function scope. */
15928 if (parser->in_function_body)
15929 return false;
15930 /* And only certain tokens can begin a constructor declarator. */
15931 next_token = cp_lexer_peek_token (parser->lexer);
15932 if (next_token->type != CPP_NAME
15933 && next_token->type != CPP_SCOPE
15934 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15935 && next_token->type != CPP_TEMPLATE_ID)
15936 return false;
15937
15938 /* Parse tentatively; we are going to roll back all of the tokens
15939 consumed here. */
15940 cp_parser_parse_tentatively (parser);
15941 /* Assume that we are looking at a constructor declarator. */
15942 constructor_p = true;
15943
15944 /* Look for the optional `::' operator. */
15945 cp_parser_global_scope_opt (parser,
15946 /*current_scope_valid_p=*/false);
15947 /* Look for the nested-name-specifier. */
15948 nested_name_p
15949 = (cp_parser_nested_name_specifier_opt (parser,
15950 /*typename_keyword_p=*/false,
15951 /*check_dependency_p=*/false,
15952 /*type_p=*/false,
15953 /*is_declaration=*/false)
15954 != NULL_TREE);
15955 /* Outside of a class-specifier, there must be a
15956 nested-name-specifier. */
15957 if (!nested_name_p &&
15958 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15959 || friend_p))
15960 constructor_p = false;
15961 /* If we still think that this might be a constructor-declarator,
15962 look for a class-name. */
15963 if (constructor_p)
15964 {
15965 /* If we have:
15966
15967 template <typename T> struct S { S(); };
15968 template <typename T> S<T>::S ();
15969
15970 we must recognize that the nested `S' names a class.
15971 Similarly, for:
15972
15973 template <typename T> S<T>::S<T> ();
15974
15975 we must recognize that the nested `S' names a template. */
15976 type_decl = cp_parser_class_name (parser,
15977 /*typename_keyword_p=*/false,
15978 /*template_keyword_p=*/false,
15979 none_type,
15980 /*check_dependency_p=*/false,
15981 /*class_head_p=*/false,
15982 /*is_declaration=*/false);
15983 /* If there was no class-name, then this is not a constructor. */
15984 constructor_p = !cp_parser_error_occurred (parser);
15985 }
15986
15987 /* If we're still considering a constructor, we have to see a `(',
15988 to begin the parameter-declaration-clause, followed by either a
15989 `)', an `...', or a decl-specifier. We need to check for a
15990 type-specifier to avoid being fooled into thinking that:
15991
15992 S::S (f) (int);
15993
15994 is a constructor. (It is actually a function named `f' that
15995 takes one parameter (of type `int') and returns a value of type
15996 `S::S'. */
15997 if (constructor_p
15998 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15999 {
16000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16001 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16002 /* A parameter declaration begins with a decl-specifier,
16003 which is either the "attribute" keyword, a storage class
16004 specifier, or (usually) a type-specifier. */
16005 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16006 {
16007 tree type;
16008 tree pushed_scope = NULL_TREE;
16009 unsigned saved_num_template_parameter_lists;
16010
16011 /* Names appearing in the type-specifier should be looked up
16012 in the scope of the class. */
16013 if (current_class_type)
16014 type = NULL_TREE;
16015 else
16016 {
16017 type = TREE_TYPE (type_decl);
16018 if (TREE_CODE (type) == TYPENAME_TYPE)
16019 {
16020 type = resolve_typename_type (type,
16021 /*only_current_p=*/false);
16022 if (type == error_mark_node)
16023 {
16024 cp_parser_abort_tentative_parse (parser);
16025 return false;
16026 }
16027 }
16028 pushed_scope = push_scope (type);
16029 }
16030
16031 /* Inside the constructor parameter list, surrounding
16032 template-parameter-lists do not apply. */
16033 saved_num_template_parameter_lists
16034 = parser->num_template_parameter_lists;
16035 parser->num_template_parameter_lists = 0;
16036
16037 /* Look for the type-specifier. */
16038 cp_parser_type_specifier (parser,
16039 CP_PARSER_FLAGS_NONE,
16040 /*decl_specs=*/NULL,
16041 /*is_declarator=*/true,
16042 /*declares_class_or_enum=*/NULL,
16043 /*is_cv_qualifier=*/NULL);
16044
16045 parser->num_template_parameter_lists
16046 = saved_num_template_parameter_lists;
16047
16048 /* Leave the scope of the class. */
16049 if (pushed_scope)
16050 pop_scope (pushed_scope);
16051
16052 constructor_p = !cp_parser_error_occurred (parser);
16053 }
16054 }
16055 else
16056 constructor_p = false;
16057 /* We did not really want to consume any tokens. */
16058 cp_parser_abort_tentative_parse (parser);
16059
16060 return constructor_p;
16061 }
16062
16063 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16064 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16065 they must be performed once we are in the scope of the function.
16066
16067 Returns the function defined. */
16068
16069 static tree
16070 cp_parser_function_definition_from_specifiers_and_declarator
16071 (cp_parser* parser,
16072 cp_decl_specifier_seq *decl_specifiers,
16073 tree attributes,
16074 const cp_declarator *declarator)
16075 {
16076 tree fn;
16077 bool success_p;
16078
16079 /* Begin the function-definition. */
16080 success_p = start_function (decl_specifiers, declarator, attributes);
16081
16082 /* The things we're about to see are not directly qualified by any
16083 template headers we've seen thus far. */
16084 reset_specialization ();
16085
16086 /* If there were names looked up in the decl-specifier-seq that we
16087 did not check, check them now. We must wait until we are in the
16088 scope of the function to perform the checks, since the function
16089 might be a friend. */
16090 perform_deferred_access_checks ();
16091
16092 if (!success_p)
16093 {
16094 /* Skip the entire function. */
16095 cp_parser_skip_to_end_of_block_or_statement (parser);
16096 fn = error_mark_node;
16097 }
16098 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16099 {
16100 /* Seen already, skip it. An error message has already been output. */
16101 cp_parser_skip_to_end_of_block_or_statement (parser);
16102 fn = current_function_decl;
16103 current_function_decl = NULL_TREE;
16104 /* If this is a function from a class, pop the nested class. */
16105 if (current_class_name)
16106 pop_nested_class ();
16107 }
16108 else
16109 fn = cp_parser_function_definition_after_declarator (parser,
16110 /*inline_p=*/false);
16111
16112 return fn;
16113 }
16114
16115 /* Parse the part of a function-definition that follows the
16116 declarator. INLINE_P is TRUE iff this function is an inline
16117 function defined with a class-specifier.
16118
16119 Returns the function defined. */
16120
16121 static tree
16122 cp_parser_function_definition_after_declarator (cp_parser* parser,
16123 bool inline_p)
16124 {
16125 tree fn;
16126 bool ctor_initializer_p = false;
16127 bool saved_in_unbraced_linkage_specification_p;
16128 bool saved_in_function_body;
16129 unsigned saved_num_template_parameter_lists;
16130
16131 saved_in_function_body = parser->in_function_body;
16132 parser->in_function_body = true;
16133 /* If the next token is `return', then the code may be trying to
16134 make use of the "named return value" extension that G++ used to
16135 support. */
16136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16137 {
16138 /* Consume the `return' keyword. */
16139 cp_lexer_consume_token (parser->lexer);
16140 /* Look for the identifier that indicates what value is to be
16141 returned. */
16142 cp_parser_identifier (parser);
16143 /* Issue an error message. */
16144 error ("named return values are no longer supported");
16145 /* Skip tokens until we reach the start of the function body. */
16146 while (true)
16147 {
16148 cp_token *token = cp_lexer_peek_token (parser->lexer);
16149 if (token->type == CPP_OPEN_BRACE
16150 || token->type == CPP_EOF
16151 || token->type == CPP_PRAGMA_EOL)
16152 break;
16153 cp_lexer_consume_token (parser->lexer);
16154 }
16155 }
16156 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16157 anything declared inside `f'. */
16158 saved_in_unbraced_linkage_specification_p
16159 = parser->in_unbraced_linkage_specification_p;
16160 parser->in_unbraced_linkage_specification_p = false;
16161 /* Inside the function, surrounding template-parameter-lists do not
16162 apply. */
16163 saved_num_template_parameter_lists
16164 = parser->num_template_parameter_lists;
16165 parser->num_template_parameter_lists = 0;
16166 /* If the next token is `try', then we are looking at a
16167 function-try-block. */
16168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16169 ctor_initializer_p = cp_parser_function_try_block (parser);
16170 /* A function-try-block includes the function-body, so we only do
16171 this next part if we're not processing a function-try-block. */
16172 else
16173 ctor_initializer_p
16174 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16175
16176 /* Finish the function. */
16177 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16178 (inline_p ? 2 : 0));
16179 /* Generate code for it, if necessary. */
16180 expand_or_defer_fn (fn);
16181 /* Restore the saved values. */
16182 parser->in_unbraced_linkage_specification_p
16183 = saved_in_unbraced_linkage_specification_p;
16184 parser->num_template_parameter_lists
16185 = saved_num_template_parameter_lists;
16186 parser->in_function_body = saved_in_function_body;
16187
16188 return fn;
16189 }
16190
16191 /* Parse a template-declaration, assuming that the `export' (and
16192 `extern') keywords, if present, has already been scanned. MEMBER_P
16193 is as for cp_parser_template_declaration. */
16194
16195 static void
16196 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16197 {
16198 tree decl = NULL_TREE;
16199 VEC (deferred_access_check,gc) *checks;
16200 tree parameter_list;
16201 bool friend_p = false;
16202 bool need_lang_pop;
16203
16204 /* Look for the `template' keyword. */
16205 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16206 return;
16207
16208 /* And the `<'. */
16209 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16210 return;
16211 if (at_class_scope_p () && current_function_decl)
16212 {
16213 /* 14.5.2.2 [temp.mem]
16214
16215 A local class shall not have member templates. */
16216 error ("invalid declaration of member template in local class");
16217 cp_parser_skip_to_end_of_block_or_statement (parser);
16218 return;
16219 }
16220 /* [temp]
16221
16222 A template ... shall not have C linkage. */
16223 if (current_lang_name == lang_name_c)
16224 {
16225 error ("template with C linkage");
16226 /* Give it C++ linkage to avoid confusing other parts of the
16227 front end. */
16228 push_lang_context (lang_name_cplusplus);
16229 need_lang_pop = true;
16230 }
16231 else
16232 need_lang_pop = false;
16233
16234 /* We cannot perform access checks on the template parameter
16235 declarations until we know what is being declared, just as we
16236 cannot check the decl-specifier list. */
16237 push_deferring_access_checks (dk_deferred);
16238
16239 /* If the next token is `>', then we have an invalid
16240 specialization. Rather than complain about an invalid template
16241 parameter, issue an error message here. */
16242 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16243 {
16244 cp_parser_error (parser, "invalid explicit specialization");
16245 begin_specialization ();
16246 parameter_list = NULL_TREE;
16247 }
16248 else
16249 /* Parse the template parameters. */
16250 parameter_list = cp_parser_template_parameter_list (parser);
16251
16252 /* Get the deferred access checks from the parameter list. These
16253 will be checked once we know what is being declared, as for a
16254 member template the checks must be performed in the scope of the
16255 class containing the member. */
16256 checks = get_deferred_access_checks ();
16257
16258 /* Look for the `>'. */
16259 cp_parser_skip_to_end_of_template_parameter_list (parser);
16260 /* We just processed one more parameter list. */
16261 ++parser->num_template_parameter_lists;
16262 /* If the next token is `template', there are more template
16263 parameters. */
16264 if (cp_lexer_next_token_is_keyword (parser->lexer,
16265 RID_TEMPLATE))
16266 cp_parser_template_declaration_after_export (parser, member_p);
16267 else
16268 {
16269 /* There are no access checks when parsing a template, as we do not
16270 know if a specialization will be a friend. */
16271 push_deferring_access_checks (dk_no_check);
16272 decl = cp_parser_single_declaration (parser,
16273 checks,
16274 member_p,
16275 &friend_p);
16276 pop_deferring_access_checks ();
16277
16278 /* If this is a member template declaration, let the front
16279 end know. */
16280 if (member_p && !friend_p && decl)
16281 {
16282 if (TREE_CODE (decl) == TYPE_DECL)
16283 cp_parser_check_access_in_redeclaration (decl);
16284
16285 decl = finish_member_template_decl (decl);
16286 }
16287 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16288 make_friend_class (current_class_type, TREE_TYPE (decl),
16289 /*complain=*/true);
16290 }
16291 /* We are done with the current parameter list. */
16292 --parser->num_template_parameter_lists;
16293
16294 pop_deferring_access_checks ();
16295
16296 /* Finish up. */
16297 finish_template_decl (parameter_list);
16298
16299 /* Register member declarations. */
16300 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16301 finish_member_declaration (decl);
16302 /* For the erroneous case of a template with C linkage, we pushed an
16303 implicit C++ linkage scope; exit that scope now. */
16304 if (need_lang_pop)
16305 pop_lang_context ();
16306 /* If DECL is a function template, we must return to parse it later.
16307 (Even though there is no definition, there might be default
16308 arguments that need handling.) */
16309 if (member_p && decl
16310 && (TREE_CODE (decl) == FUNCTION_DECL
16311 || DECL_FUNCTION_TEMPLATE_P (decl)))
16312 TREE_VALUE (parser->unparsed_functions_queues)
16313 = tree_cons (NULL_TREE, decl,
16314 TREE_VALUE (parser->unparsed_functions_queues));
16315 }
16316
16317 /* Perform the deferred access checks from a template-parameter-list.
16318 CHECKS is a TREE_LIST of access checks, as returned by
16319 get_deferred_access_checks. */
16320
16321 static void
16322 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16323 {
16324 ++processing_template_parmlist;
16325 perform_access_checks (checks);
16326 --processing_template_parmlist;
16327 }
16328
16329 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16330 `function-definition' sequence. MEMBER_P is true, this declaration
16331 appears in a class scope.
16332
16333 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16334 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16335
16336 static tree
16337 cp_parser_single_declaration (cp_parser* parser,
16338 VEC (deferred_access_check,gc)* checks,
16339 bool member_p,
16340 bool* friend_p)
16341 {
16342 int declares_class_or_enum;
16343 tree decl = NULL_TREE;
16344 cp_decl_specifier_seq decl_specifiers;
16345 bool function_definition_p = false;
16346
16347 /* This function is only used when processing a template
16348 declaration. */
16349 gcc_assert (innermost_scope_kind () == sk_template_parms
16350 || innermost_scope_kind () == sk_template_spec);
16351
16352 /* Defer access checks until we know what is being declared. */
16353 push_deferring_access_checks (dk_deferred);
16354
16355 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16356 alternative. */
16357 cp_parser_decl_specifier_seq (parser,
16358 CP_PARSER_FLAGS_OPTIONAL,
16359 &decl_specifiers,
16360 &declares_class_or_enum);
16361 if (friend_p)
16362 *friend_p = cp_parser_friend_p (&decl_specifiers);
16363
16364 /* There are no template typedefs. */
16365 if (decl_specifiers.specs[(int) ds_typedef])
16366 {
16367 error ("template declaration of %qs", "typedef");
16368 decl = error_mark_node;
16369 }
16370
16371 /* Gather up the access checks that occurred the
16372 decl-specifier-seq. */
16373 stop_deferring_access_checks ();
16374
16375 /* Check for the declaration of a template class. */
16376 if (declares_class_or_enum)
16377 {
16378 if (cp_parser_declares_only_class_p (parser))
16379 {
16380 decl = shadow_tag (&decl_specifiers);
16381
16382 /* In this case:
16383
16384 struct C {
16385 friend template <typename T> struct A<T>::B;
16386 };
16387
16388 A<T>::B will be represented by a TYPENAME_TYPE, and
16389 therefore not recognized by shadow_tag. */
16390 if (friend_p && *friend_p
16391 && !decl
16392 && decl_specifiers.type
16393 && TYPE_P (decl_specifiers.type))
16394 decl = decl_specifiers.type;
16395
16396 if (decl && decl != error_mark_node)
16397 decl = TYPE_NAME (decl);
16398 else
16399 decl = error_mark_node;
16400
16401 /* Perform access checks for template parameters. */
16402 cp_parser_perform_template_parameter_access_checks (checks);
16403 }
16404 }
16405 /* If it's not a template class, try for a template function. If
16406 the next token is a `;', then this declaration does not declare
16407 anything. But, if there were errors in the decl-specifiers, then
16408 the error might well have come from an attempted class-specifier.
16409 In that case, there's no need to warn about a missing declarator. */
16410 if (!decl
16411 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16412 || decl_specifiers.type != error_mark_node))
16413 decl = cp_parser_init_declarator (parser,
16414 &decl_specifiers,
16415 checks,
16416 /*function_definition_allowed_p=*/true,
16417 member_p,
16418 declares_class_or_enum,
16419 &function_definition_p);
16420
16421 pop_deferring_access_checks ();
16422
16423 /* Clear any current qualification; whatever comes next is the start
16424 of something new. */
16425 parser->scope = NULL_TREE;
16426 parser->qualifying_scope = NULL_TREE;
16427 parser->object_scope = NULL_TREE;
16428 /* Look for a trailing `;' after the declaration. */
16429 if (!function_definition_p
16430 && (decl == error_mark_node
16431 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16432 cp_parser_skip_to_end_of_block_or_statement (parser);
16433
16434 return decl;
16435 }
16436
16437 /* Parse a cast-expression that is not the operand of a unary "&". */
16438
16439 static tree
16440 cp_parser_simple_cast_expression (cp_parser *parser)
16441 {
16442 return cp_parser_cast_expression (parser, /*address_p=*/false,
16443 /*cast_p=*/false);
16444 }
16445
16446 /* Parse a functional cast to TYPE. Returns an expression
16447 representing the cast. */
16448
16449 static tree
16450 cp_parser_functional_cast (cp_parser* parser, tree type)
16451 {
16452 tree expression_list;
16453 tree cast;
16454
16455 expression_list
16456 = cp_parser_parenthesized_expression_list (parser, false,
16457 /*cast_p=*/true,
16458 /*allow_expansion_p=*/true,
16459 /*non_constant_p=*/NULL);
16460
16461 cast = build_functional_cast (type, expression_list);
16462 /* [expr.const]/1: In an integral constant expression "only type
16463 conversions to integral or enumeration type can be used". */
16464 if (TREE_CODE (type) == TYPE_DECL)
16465 type = TREE_TYPE (type);
16466 if (cast != error_mark_node
16467 && !cast_valid_in_integral_constant_expression_p (type)
16468 && (cp_parser_non_integral_constant_expression
16469 (parser, "a call to a constructor")))
16470 return error_mark_node;
16471 return cast;
16472 }
16473
16474 /* Save the tokens that make up the body of a member function defined
16475 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16476 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16477 specifiers applied to the declaration. Returns the FUNCTION_DECL
16478 for the member function. */
16479
16480 static tree
16481 cp_parser_save_member_function_body (cp_parser* parser,
16482 cp_decl_specifier_seq *decl_specifiers,
16483 cp_declarator *declarator,
16484 tree attributes)
16485 {
16486 cp_token *first;
16487 cp_token *last;
16488 tree fn;
16489
16490 /* Create the function-declaration. */
16491 fn = start_method (decl_specifiers, declarator, attributes);
16492 /* If something went badly wrong, bail out now. */
16493 if (fn == error_mark_node)
16494 {
16495 /* If there's a function-body, skip it. */
16496 if (cp_parser_token_starts_function_definition_p
16497 (cp_lexer_peek_token (parser->lexer)))
16498 cp_parser_skip_to_end_of_block_or_statement (parser);
16499 return error_mark_node;
16500 }
16501
16502 /* Remember it, if there default args to post process. */
16503 cp_parser_save_default_args (parser, fn);
16504
16505 /* Save away the tokens that make up the body of the
16506 function. */
16507 first = parser->lexer->next_token;
16508 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16509 /* Handle function try blocks. */
16510 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16511 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16512 last = parser->lexer->next_token;
16513
16514 /* Save away the inline definition; we will process it when the
16515 class is complete. */
16516 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16517 DECL_PENDING_INLINE_P (fn) = 1;
16518
16519 /* We need to know that this was defined in the class, so that
16520 friend templates are handled correctly. */
16521 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16522
16523 /* We're done with the inline definition. */
16524 finish_method (fn);
16525
16526 /* Add FN to the queue of functions to be parsed later. */
16527 TREE_VALUE (parser->unparsed_functions_queues)
16528 = tree_cons (NULL_TREE, fn,
16529 TREE_VALUE (parser->unparsed_functions_queues));
16530
16531 return fn;
16532 }
16533
16534 /* Parse a template-argument-list, as well as the trailing ">" (but
16535 not the opening ">"). See cp_parser_template_argument_list for the
16536 return value. */
16537
16538 static tree
16539 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16540 {
16541 tree arguments;
16542 tree saved_scope;
16543 tree saved_qualifying_scope;
16544 tree saved_object_scope;
16545 bool saved_greater_than_is_operator_p;
16546 bool saved_skip_evaluation;
16547
16548 /* [temp.names]
16549
16550 When parsing a template-id, the first non-nested `>' is taken as
16551 the end of the template-argument-list rather than a greater-than
16552 operator. */
16553 saved_greater_than_is_operator_p
16554 = parser->greater_than_is_operator_p;
16555 parser->greater_than_is_operator_p = false;
16556 /* Parsing the argument list may modify SCOPE, so we save it
16557 here. */
16558 saved_scope = parser->scope;
16559 saved_qualifying_scope = parser->qualifying_scope;
16560 saved_object_scope = parser->object_scope;
16561 /* We need to evaluate the template arguments, even though this
16562 template-id may be nested within a "sizeof". */
16563 saved_skip_evaluation = skip_evaluation;
16564 skip_evaluation = false;
16565 /* Parse the template-argument-list itself. */
16566 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16567 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16568 arguments = NULL_TREE;
16569 else
16570 arguments = cp_parser_template_argument_list (parser);
16571 /* Look for the `>' that ends the template-argument-list. If we find
16572 a '>>' instead, it's probably just a typo. */
16573 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16574 {
16575 if (flag_cpp0x)
16576 {
16577 /* In C++0x, a `>>' in a template argument list or cast
16578 expression is considered to be two separate `>'
16579 tokens. So, change the current token to a `>', but don't
16580 consume it: it will be consumed later when the outer
16581 template argument list (or cast expression) is parsed.
16582 Note that this replacement of `>' for `>>' is necessary
16583 even if we are parsing tentatively: in the tentative
16584 case, after calling
16585 cp_parser_enclosed_template_argument_list we will always
16586 throw away all of the template arguments and the first
16587 closing `>', either because the template argument list
16588 was erroneous or because we are replacing those tokens
16589 with a CPP_TEMPLATE_ID token. The second `>' (which will
16590 not have been thrown away) is needed either to close an
16591 outer template argument list or to complete a new-style
16592 cast. */
16593 cp_token *token = cp_lexer_peek_token (parser->lexer);
16594 token->type = CPP_GREATER;
16595 }
16596 else if (!saved_greater_than_is_operator_p)
16597 {
16598 /* If we're in a nested template argument list, the '>>' has
16599 to be a typo for '> >'. We emit the error message, but we
16600 continue parsing and we push a '>' as next token, so that
16601 the argument list will be parsed correctly. Note that the
16602 global source location is still on the token before the
16603 '>>', so we need to say explicitly where we want it. */
16604 cp_token *token = cp_lexer_peek_token (parser->lexer);
16605 error ("%H%<>>%> should be %<> >%> "
16606 "within a nested template argument list",
16607 &token->location);
16608
16609 token->type = CPP_GREATER;
16610 }
16611 else
16612 {
16613 /* If this is not a nested template argument list, the '>>'
16614 is a typo for '>'. Emit an error message and continue.
16615 Same deal about the token location, but here we can get it
16616 right by consuming the '>>' before issuing the diagnostic. */
16617 cp_lexer_consume_token (parser->lexer);
16618 error ("spurious %<>>%>, use %<>%> to terminate "
16619 "a template argument list");
16620 }
16621 }
16622 else
16623 cp_parser_skip_to_end_of_template_parameter_list (parser);
16624 /* The `>' token might be a greater-than operator again now. */
16625 parser->greater_than_is_operator_p
16626 = saved_greater_than_is_operator_p;
16627 /* Restore the SAVED_SCOPE. */
16628 parser->scope = saved_scope;
16629 parser->qualifying_scope = saved_qualifying_scope;
16630 parser->object_scope = saved_object_scope;
16631 skip_evaluation = saved_skip_evaluation;
16632
16633 return arguments;
16634 }
16635
16636 /* MEMBER_FUNCTION is a member function, or a friend. If default
16637 arguments, or the body of the function have not yet been parsed,
16638 parse them now. */
16639
16640 static void
16641 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16642 {
16643 /* If this member is a template, get the underlying
16644 FUNCTION_DECL. */
16645 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16646 member_function = DECL_TEMPLATE_RESULT (member_function);
16647
16648 /* There should not be any class definitions in progress at this
16649 point; the bodies of members are only parsed outside of all class
16650 definitions. */
16651 gcc_assert (parser->num_classes_being_defined == 0);
16652 /* While we're parsing the member functions we might encounter more
16653 classes. We want to handle them right away, but we don't want
16654 them getting mixed up with functions that are currently in the
16655 queue. */
16656 parser->unparsed_functions_queues
16657 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16658
16659 /* Make sure that any template parameters are in scope. */
16660 maybe_begin_member_template_processing (member_function);
16661
16662 /* If the body of the function has not yet been parsed, parse it
16663 now. */
16664 if (DECL_PENDING_INLINE_P (member_function))
16665 {
16666 tree function_scope;
16667 cp_token_cache *tokens;
16668
16669 /* The function is no longer pending; we are processing it. */
16670 tokens = DECL_PENDING_INLINE_INFO (member_function);
16671 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16672 DECL_PENDING_INLINE_P (member_function) = 0;
16673
16674 /* If this is a local class, enter the scope of the containing
16675 function. */
16676 function_scope = current_function_decl;
16677 if (function_scope)
16678 push_function_context_to (function_scope);
16679
16680
16681 /* Push the body of the function onto the lexer stack. */
16682 cp_parser_push_lexer_for_tokens (parser, tokens);
16683
16684 /* Let the front end know that we going to be defining this
16685 function. */
16686 start_preparsed_function (member_function, NULL_TREE,
16687 SF_PRE_PARSED | SF_INCLASS_INLINE);
16688
16689 /* Don't do access checking if it is a templated function. */
16690 if (processing_template_decl)
16691 push_deferring_access_checks (dk_no_check);
16692
16693 /* Now, parse the body of the function. */
16694 cp_parser_function_definition_after_declarator (parser,
16695 /*inline_p=*/true);
16696
16697 if (processing_template_decl)
16698 pop_deferring_access_checks ();
16699
16700 /* Leave the scope of the containing function. */
16701 if (function_scope)
16702 pop_function_context_from (function_scope);
16703 cp_parser_pop_lexer (parser);
16704 }
16705
16706 /* Remove any template parameters from the symbol table. */
16707 maybe_end_member_template_processing ();
16708
16709 /* Restore the queue. */
16710 parser->unparsed_functions_queues
16711 = TREE_CHAIN (parser->unparsed_functions_queues);
16712 }
16713
16714 /* If DECL contains any default args, remember it on the unparsed
16715 functions queue. */
16716
16717 static void
16718 cp_parser_save_default_args (cp_parser* parser, tree decl)
16719 {
16720 tree probe;
16721
16722 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16723 probe;
16724 probe = TREE_CHAIN (probe))
16725 if (TREE_PURPOSE (probe))
16726 {
16727 TREE_PURPOSE (parser->unparsed_functions_queues)
16728 = tree_cons (current_class_type, decl,
16729 TREE_PURPOSE (parser->unparsed_functions_queues));
16730 break;
16731 }
16732 }
16733
16734 /* FN is a FUNCTION_DECL which may contains a parameter with an
16735 unparsed DEFAULT_ARG. Parse the default args now. This function
16736 assumes that the current scope is the scope in which the default
16737 argument should be processed. */
16738
16739 static void
16740 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16741 {
16742 bool saved_local_variables_forbidden_p;
16743 tree parm;
16744
16745 /* While we're parsing the default args, we might (due to the
16746 statement expression extension) encounter more classes. We want
16747 to handle them right away, but we don't want them getting mixed
16748 up with default args that are currently in the queue. */
16749 parser->unparsed_functions_queues
16750 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16751
16752 /* Local variable names (and the `this' keyword) may not appear
16753 in a default argument. */
16754 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16755 parser->local_variables_forbidden_p = true;
16756
16757 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16758 parm;
16759 parm = TREE_CHAIN (parm))
16760 {
16761 cp_token_cache *tokens;
16762 tree default_arg = TREE_PURPOSE (parm);
16763 tree parsed_arg;
16764 VEC(tree,gc) *insts;
16765 tree copy;
16766 unsigned ix;
16767
16768 if (!default_arg)
16769 continue;
16770
16771 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16772 /* This can happen for a friend declaration for a function
16773 already declared with default arguments. */
16774 continue;
16775
16776 /* Push the saved tokens for the default argument onto the parser's
16777 lexer stack. */
16778 tokens = DEFARG_TOKENS (default_arg);
16779 cp_parser_push_lexer_for_tokens (parser, tokens);
16780
16781 /* Parse the assignment-expression. */
16782 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16783
16784 if (!processing_template_decl)
16785 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16786
16787 TREE_PURPOSE (parm) = parsed_arg;
16788
16789 /* Update any instantiations we've already created. */
16790 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16791 VEC_iterate (tree, insts, ix, copy); ix++)
16792 TREE_PURPOSE (copy) = parsed_arg;
16793
16794 /* If the token stream has not been completely used up, then
16795 there was extra junk after the end of the default
16796 argument. */
16797 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16798 cp_parser_error (parser, "expected %<,%>");
16799
16800 /* Revert to the main lexer. */
16801 cp_parser_pop_lexer (parser);
16802 }
16803
16804 /* Make sure no default arg is missing. */
16805 check_default_args (fn);
16806
16807 /* Restore the state of local_variables_forbidden_p. */
16808 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16809
16810 /* Restore the queue. */
16811 parser->unparsed_functions_queues
16812 = TREE_CHAIN (parser->unparsed_functions_queues);
16813 }
16814
16815 /* Parse the operand of `sizeof' (or a similar operator). Returns
16816 either a TYPE or an expression, depending on the form of the
16817 input. The KEYWORD indicates which kind of expression we have
16818 encountered. */
16819
16820 static tree
16821 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16822 {
16823 static const char *format;
16824 tree expr = NULL_TREE;
16825 const char *saved_message;
16826 bool saved_integral_constant_expression_p;
16827 bool saved_non_integral_constant_expression_p;
16828 bool pack_expansion_p = false;
16829
16830 /* Initialize FORMAT the first time we get here. */
16831 if (!format)
16832 format = "types may not be defined in '%s' expressions";
16833
16834 /* Types cannot be defined in a `sizeof' expression. Save away the
16835 old message. */
16836 saved_message = parser->type_definition_forbidden_message;
16837 /* And create the new one. */
16838 parser->type_definition_forbidden_message
16839 = XNEWVEC (const char, strlen (format)
16840 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16841 + 1 /* `\0' */);
16842 sprintf ((char *) parser->type_definition_forbidden_message,
16843 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16844
16845 /* The restrictions on constant-expressions do not apply inside
16846 sizeof expressions. */
16847 saved_integral_constant_expression_p
16848 = parser->integral_constant_expression_p;
16849 saved_non_integral_constant_expression_p
16850 = parser->non_integral_constant_expression_p;
16851 parser->integral_constant_expression_p = false;
16852
16853 /* If it's a `...', then we are computing the length of a parameter
16854 pack. */
16855 if (keyword == RID_SIZEOF
16856 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16857 {
16858 /* Consume the `...'. */
16859 cp_lexer_consume_token (parser->lexer);
16860 maybe_warn_variadic_templates ();
16861
16862 /* Note that this is an expansion. */
16863 pack_expansion_p = true;
16864 }
16865
16866 /* Do not actually evaluate the expression. */
16867 ++skip_evaluation;
16868 /* If it's a `(', then we might be looking at the type-id
16869 construction. */
16870 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16871 {
16872 tree type;
16873 bool saved_in_type_id_in_expr_p;
16874
16875 /* We can't be sure yet whether we're looking at a type-id or an
16876 expression. */
16877 cp_parser_parse_tentatively (parser);
16878 /* Consume the `('. */
16879 cp_lexer_consume_token (parser->lexer);
16880 /* Parse the type-id. */
16881 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16882 parser->in_type_id_in_expr_p = true;
16883 type = cp_parser_type_id (parser);
16884 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16885 /* Now, look for the trailing `)'. */
16886 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16887 /* If all went well, then we're done. */
16888 if (cp_parser_parse_definitely (parser))
16889 {
16890 cp_decl_specifier_seq decl_specs;
16891
16892 /* Build a trivial decl-specifier-seq. */
16893 clear_decl_specs (&decl_specs);
16894 decl_specs.type = type;
16895
16896 /* Call grokdeclarator to figure out what type this is. */
16897 expr = grokdeclarator (NULL,
16898 &decl_specs,
16899 TYPENAME,
16900 /*initialized=*/0,
16901 /*attrlist=*/NULL);
16902 }
16903 }
16904
16905 /* If the type-id production did not work out, then we must be
16906 looking at the unary-expression production. */
16907 if (!expr)
16908 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16909 /*cast_p=*/false);
16910
16911 if (pack_expansion_p)
16912 /* Build a pack expansion. */
16913 expr = make_pack_expansion (expr);
16914
16915 /* Go back to evaluating expressions. */
16916 --skip_evaluation;
16917
16918 /* Free the message we created. */
16919 free ((char *) parser->type_definition_forbidden_message);
16920 /* And restore the old one. */
16921 parser->type_definition_forbidden_message = saved_message;
16922 parser->integral_constant_expression_p
16923 = saved_integral_constant_expression_p;
16924 parser->non_integral_constant_expression_p
16925 = saved_non_integral_constant_expression_p;
16926
16927 return expr;
16928 }
16929
16930 /* If the current declaration has no declarator, return true. */
16931
16932 static bool
16933 cp_parser_declares_only_class_p (cp_parser *parser)
16934 {
16935 /* If the next token is a `;' or a `,' then there is no
16936 declarator. */
16937 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16938 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16939 }
16940
16941 /* Update the DECL_SPECS to reflect the storage class indicated by
16942 KEYWORD. */
16943
16944 static void
16945 cp_parser_set_storage_class (cp_parser *parser,
16946 cp_decl_specifier_seq *decl_specs,
16947 enum rid keyword)
16948 {
16949 cp_storage_class storage_class;
16950
16951 if (parser->in_unbraced_linkage_specification_p)
16952 {
16953 error ("invalid use of %qD in linkage specification",
16954 ridpointers[keyword]);
16955 return;
16956 }
16957 else if (decl_specs->storage_class != sc_none)
16958 {
16959 decl_specs->conflicting_specifiers_p = true;
16960 return;
16961 }
16962
16963 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16964 && decl_specs->specs[(int) ds_thread])
16965 {
16966 error ("%<__thread%> before %qD", ridpointers[keyword]);
16967 decl_specs->specs[(int) ds_thread] = 0;
16968 }
16969
16970 switch (keyword)
16971 {
16972 case RID_AUTO:
16973 storage_class = sc_auto;
16974 break;
16975 case RID_REGISTER:
16976 storage_class = sc_register;
16977 break;
16978 case RID_STATIC:
16979 storage_class = sc_static;
16980 break;
16981 case RID_EXTERN:
16982 storage_class = sc_extern;
16983 break;
16984 case RID_MUTABLE:
16985 storage_class = sc_mutable;
16986 break;
16987 default:
16988 gcc_unreachable ();
16989 }
16990 decl_specs->storage_class = storage_class;
16991
16992 /* A storage class specifier cannot be applied alongside a typedef
16993 specifier. If there is a typedef specifier present then set
16994 conflicting_specifiers_p which will trigger an error later
16995 on in grokdeclarator. */
16996 if (decl_specs->specs[(int)ds_typedef])
16997 decl_specs->conflicting_specifiers_p = true;
16998 }
16999
17000 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17001 is true, the type is a user-defined type; otherwise it is a
17002 built-in type specified by a keyword. */
17003
17004 static void
17005 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17006 tree type_spec,
17007 bool user_defined_p)
17008 {
17009 decl_specs->any_specifiers_p = true;
17010
17011 /* If the user tries to redeclare bool or wchar_t (with, for
17012 example, in "typedef int wchar_t;") we remember that this is what
17013 happened. In system headers, we ignore these declarations so
17014 that G++ can work with system headers that are not C++-safe. */
17015 if (decl_specs->specs[(int) ds_typedef]
17016 && !user_defined_p
17017 && (type_spec == boolean_type_node
17018 || type_spec == wchar_type_node)
17019 && (decl_specs->type
17020 || decl_specs->specs[(int) ds_long]
17021 || decl_specs->specs[(int) ds_short]
17022 || decl_specs->specs[(int) ds_unsigned]
17023 || decl_specs->specs[(int) ds_signed]))
17024 {
17025 decl_specs->redefined_builtin_type = type_spec;
17026 if (!decl_specs->type)
17027 {
17028 decl_specs->type = type_spec;
17029 decl_specs->user_defined_type_p = false;
17030 }
17031 }
17032 else if (decl_specs->type)
17033 decl_specs->multiple_types_p = true;
17034 else
17035 {
17036 decl_specs->type = type_spec;
17037 decl_specs->user_defined_type_p = user_defined_p;
17038 decl_specs->redefined_builtin_type = NULL_TREE;
17039 }
17040 }
17041
17042 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17043 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17044
17045 static bool
17046 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17047 {
17048 return decl_specifiers->specs[(int) ds_friend] != 0;
17049 }
17050
17051 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17052 issue an error message indicating that TOKEN_DESC was expected.
17053
17054 Returns the token consumed, if the token had the appropriate type.
17055 Otherwise, returns NULL. */
17056
17057 static cp_token *
17058 cp_parser_require (cp_parser* parser,
17059 enum cpp_ttype type,
17060 const char* token_desc)
17061 {
17062 if (cp_lexer_next_token_is (parser->lexer, type))
17063 return cp_lexer_consume_token (parser->lexer);
17064 else
17065 {
17066 /* Output the MESSAGE -- unless we're parsing tentatively. */
17067 if (!cp_parser_simulate_error (parser))
17068 {
17069 char *message = concat ("expected ", token_desc, NULL);
17070 cp_parser_error (parser, message);
17071 free (message);
17072 }
17073 return NULL;
17074 }
17075 }
17076
17077 /* An error message is produced if the next token is not '>'.
17078 All further tokens are skipped until the desired token is
17079 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17080
17081 static void
17082 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17083 {
17084 /* Current level of '< ... >'. */
17085 unsigned level = 0;
17086 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17087 unsigned nesting_depth = 0;
17088
17089 /* Are we ready, yet? If not, issue error message. */
17090 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17091 return;
17092
17093 /* Skip tokens until the desired token is found. */
17094 while (true)
17095 {
17096 /* Peek at the next token. */
17097 switch (cp_lexer_peek_token (parser->lexer)->type)
17098 {
17099 case CPP_LESS:
17100 if (!nesting_depth)
17101 ++level;
17102 break;
17103
17104 case CPP_RSHIFT:
17105 if (!flag_cpp0x)
17106 /* C++0x views the `>>' operator as two `>' tokens, but
17107 C++98 does not. */
17108 break;
17109 else if (!nesting_depth && level-- == 0)
17110 {
17111 /* We've hit a `>>' where the first `>' closes the
17112 template argument list, and the second `>' is
17113 spurious. Just consume the `>>' and stop; we've
17114 already produced at least one error. */
17115 cp_lexer_consume_token (parser->lexer);
17116 return;
17117 }
17118 /* Fall through for C++0x, so we handle the second `>' in
17119 the `>>'. */
17120
17121 case CPP_GREATER:
17122 if (!nesting_depth && level-- == 0)
17123 {
17124 /* We've reached the token we want, consume it and stop. */
17125 cp_lexer_consume_token (parser->lexer);
17126 return;
17127 }
17128 break;
17129
17130 case CPP_OPEN_PAREN:
17131 case CPP_OPEN_SQUARE:
17132 ++nesting_depth;
17133 break;
17134
17135 case CPP_CLOSE_PAREN:
17136 case CPP_CLOSE_SQUARE:
17137 if (nesting_depth-- == 0)
17138 return;
17139 break;
17140
17141 case CPP_EOF:
17142 case CPP_PRAGMA_EOL:
17143 case CPP_SEMICOLON:
17144 case CPP_OPEN_BRACE:
17145 case CPP_CLOSE_BRACE:
17146 /* The '>' was probably forgotten, don't look further. */
17147 return;
17148
17149 default:
17150 break;
17151 }
17152
17153 /* Consume this token. */
17154 cp_lexer_consume_token (parser->lexer);
17155 }
17156 }
17157
17158 /* If the next token is the indicated keyword, consume it. Otherwise,
17159 issue an error message indicating that TOKEN_DESC was expected.
17160
17161 Returns the token consumed, if the token had the appropriate type.
17162 Otherwise, returns NULL. */
17163
17164 static cp_token *
17165 cp_parser_require_keyword (cp_parser* parser,
17166 enum rid keyword,
17167 const char* token_desc)
17168 {
17169 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17170
17171 if (token && token->keyword != keyword)
17172 {
17173 dyn_string_t error_msg;
17174
17175 /* Format the error message. */
17176 error_msg = dyn_string_new (0);
17177 dyn_string_append_cstr (error_msg, "expected ");
17178 dyn_string_append_cstr (error_msg, token_desc);
17179 cp_parser_error (parser, error_msg->s);
17180 dyn_string_delete (error_msg);
17181 return NULL;
17182 }
17183
17184 return token;
17185 }
17186
17187 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17188 function-definition. */
17189
17190 static bool
17191 cp_parser_token_starts_function_definition_p (cp_token* token)
17192 {
17193 return (/* An ordinary function-body begins with an `{'. */
17194 token->type == CPP_OPEN_BRACE
17195 /* A ctor-initializer begins with a `:'. */
17196 || token->type == CPP_COLON
17197 /* A function-try-block begins with `try'. */
17198 || token->keyword == RID_TRY
17199 /* The named return value extension begins with `return'. */
17200 || token->keyword == RID_RETURN);
17201 }
17202
17203 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17204 definition. */
17205
17206 static bool
17207 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17208 {
17209 cp_token *token;
17210
17211 token = cp_lexer_peek_token (parser->lexer);
17212 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17213 }
17214
17215 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17216 C++0x) ending a template-argument. */
17217
17218 static bool
17219 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17220 {
17221 cp_token *token;
17222
17223 token = cp_lexer_peek_token (parser->lexer);
17224 return (token->type == CPP_COMMA
17225 || token->type == CPP_GREATER
17226 || token->type == CPP_ELLIPSIS
17227 || (flag_cpp0x && token->type == CPP_RSHIFT));
17228 }
17229
17230 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17231 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17232
17233 static bool
17234 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17235 size_t n)
17236 {
17237 cp_token *token;
17238
17239 token = cp_lexer_peek_nth_token (parser->lexer, n);
17240 if (token->type == CPP_LESS)
17241 return true;
17242 /* Check for the sequence `<::' in the original code. It would be lexed as
17243 `[:', where `[' is a digraph, and there is no whitespace before
17244 `:'. */
17245 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17246 {
17247 cp_token *token2;
17248 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17249 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17250 return true;
17251 }
17252 return false;
17253 }
17254
17255 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17256 or none_type otherwise. */
17257
17258 static enum tag_types
17259 cp_parser_token_is_class_key (cp_token* token)
17260 {
17261 switch (token->keyword)
17262 {
17263 case RID_CLASS:
17264 return class_type;
17265 case RID_STRUCT:
17266 return record_type;
17267 case RID_UNION:
17268 return union_type;
17269
17270 default:
17271 return none_type;
17272 }
17273 }
17274
17275 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17276
17277 static void
17278 cp_parser_check_class_key (enum tag_types class_key, tree type)
17279 {
17280 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17281 pedwarn ("%qs tag used in naming %q#T",
17282 class_key == union_type ? "union"
17283 : class_key == record_type ? "struct" : "class",
17284 type);
17285 }
17286
17287 /* Issue an error message if DECL is redeclared with different
17288 access than its original declaration [class.access.spec/3].
17289 This applies to nested classes and nested class templates.
17290 [class.mem/1]. */
17291
17292 static void
17293 cp_parser_check_access_in_redeclaration (tree decl)
17294 {
17295 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17296 return;
17297
17298 if ((TREE_PRIVATE (decl)
17299 != (current_access_specifier == access_private_node))
17300 || (TREE_PROTECTED (decl)
17301 != (current_access_specifier == access_protected_node)))
17302 error ("%qD redeclared with different access", decl);
17303 }
17304
17305 /* Look for the `template' keyword, as a syntactic disambiguator.
17306 Return TRUE iff it is present, in which case it will be
17307 consumed. */
17308
17309 static bool
17310 cp_parser_optional_template_keyword (cp_parser *parser)
17311 {
17312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17313 {
17314 /* The `template' keyword can only be used within templates;
17315 outside templates the parser can always figure out what is a
17316 template and what is not. */
17317 if (!processing_template_decl)
17318 {
17319 error ("%<template%> (as a disambiguator) is only allowed "
17320 "within templates");
17321 /* If this part of the token stream is rescanned, the same
17322 error message would be generated. So, we purge the token
17323 from the stream. */
17324 cp_lexer_purge_token (parser->lexer);
17325 return false;
17326 }
17327 else
17328 {
17329 /* Consume the `template' keyword. */
17330 cp_lexer_consume_token (parser->lexer);
17331 return true;
17332 }
17333 }
17334
17335 return false;
17336 }
17337
17338 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17339 set PARSER->SCOPE, and perform other related actions. */
17340
17341 static void
17342 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17343 {
17344 int i;
17345 struct tree_check *check_value;
17346 deferred_access_check *chk;
17347 VEC (deferred_access_check,gc) *checks;
17348
17349 /* Get the stored value. */
17350 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17351 /* Perform any access checks that were deferred. */
17352 checks = check_value->checks;
17353 if (checks)
17354 {
17355 for (i = 0 ;
17356 VEC_iterate (deferred_access_check, checks, i, chk) ;
17357 ++i)
17358 {
17359 perform_or_defer_access_check (chk->binfo,
17360 chk->decl,
17361 chk->diag_decl);
17362 }
17363 }
17364 /* Set the scope from the stored value. */
17365 parser->scope = check_value->value;
17366 parser->qualifying_scope = check_value->qualifying_scope;
17367 parser->object_scope = NULL_TREE;
17368 }
17369
17370 /* Consume tokens up through a non-nested END token. */
17371
17372 static void
17373 cp_parser_cache_group (cp_parser *parser,
17374 enum cpp_ttype end,
17375 unsigned depth)
17376 {
17377 while (true)
17378 {
17379 cp_token *token;
17380
17381 /* Abort a parenthesized expression if we encounter a brace. */
17382 if ((end == CPP_CLOSE_PAREN || depth == 0)
17383 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17384 return;
17385 /* If we've reached the end of the file, stop. */
17386 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17387 || (end != CPP_PRAGMA_EOL
17388 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17389 return;
17390 /* Consume the next token. */
17391 token = cp_lexer_consume_token (parser->lexer);
17392 /* See if it starts a new group. */
17393 if (token->type == CPP_OPEN_BRACE)
17394 {
17395 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17396 if (depth == 0)
17397 return;
17398 }
17399 else if (token->type == CPP_OPEN_PAREN)
17400 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17401 else if (token->type == CPP_PRAGMA)
17402 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17403 else if (token->type == end)
17404 return;
17405 }
17406 }
17407
17408 /* Begin parsing tentatively. We always save tokens while parsing
17409 tentatively so that if the tentative parsing fails we can restore the
17410 tokens. */
17411
17412 static void
17413 cp_parser_parse_tentatively (cp_parser* parser)
17414 {
17415 /* Enter a new parsing context. */
17416 parser->context = cp_parser_context_new (parser->context);
17417 /* Begin saving tokens. */
17418 cp_lexer_save_tokens (parser->lexer);
17419 /* In order to avoid repetitive access control error messages,
17420 access checks are queued up until we are no longer parsing
17421 tentatively. */
17422 push_deferring_access_checks (dk_deferred);
17423 }
17424
17425 /* Commit to the currently active tentative parse. */
17426
17427 static void
17428 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17429 {
17430 cp_parser_context *context;
17431 cp_lexer *lexer;
17432
17433 /* Mark all of the levels as committed. */
17434 lexer = parser->lexer;
17435 for (context = parser->context; context->next; context = context->next)
17436 {
17437 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17438 break;
17439 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17440 while (!cp_lexer_saving_tokens (lexer))
17441 lexer = lexer->next;
17442 cp_lexer_commit_tokens (lexer);
17443 }
17444 }
17445
17446 /* Abort the currently active tentative parse. All consumed tokens
17447 will be rolled back, and no diagnostics will be issued. */
17448
17449 static void
17450 cp_parser_abort_tentative_parse (cp_parser* parser)
17451 {
17452 cp_parser_simulate_error (parser);
17453 /* Now, pretend that we want to see if the construct was
17454 successfully parsed. */
17455 cp_parser_parse_definitely (parser);
17456 }
17457
17458 /* Stop parsing tentatively. If a parse error has occurred, restore the
17459 token stream. Otherwise, commit to the tokens we have consumed.
17460 Returns true if no error occurred; false otherwise. */
17461
17462 static bool
17463 cp_parser_parse_definitely (cp_parser* parser)
17464 {
17465 bool error_occurred;
17466 cp_parser_context *context;
17467
17468 /* Remember whether or not an error occurred, since we are about to
17469 destroy that information. */
17470 error_occurred = cp_parser_error_occurred (parser);
17471 /* Remove the topmost context from the stack. */
17472 context = parser->context;
17473 parser->context = context->next;
17474 /* If no parse errors occurred, commit to the tentative parse. */
17475 if (!error_occurred)
17476 {
17477 /* Commit to the tokens read tentatively, unless that was
17478 already done. */
17479 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17480 cp_lexer_commit_tokens (parser->lexer);
17481
17482 pop_to_parent_deferring_access_checks ();
17483 }
17484 /* Otherwise, if errors occurred, roll back our state so that things
17485 are just as they were before we began the tentative parse. */
17486 else
17487 {
17488 cp_lexer_rollback_tokens (parser->lexer);
17489 pop_deferring_access_checks ();
17490 }
17491 /* Add the context to the front of the free list. */
17492 context->next = cp_parser_context_free_list;
17493 cp_parser_context_free_list = context;
17494
17495 return !error_occurred;
17496 }
17497
17498 /* Returns true if we are parsing tentatively and are not committed to
17499 this tentative parse. */
17500
17501 static bool
17502 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17503 {
17504 return (cp_parser_parsing_tentatively (parser)
17505 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17506 }
17507
17508 /* Returns nonzero iff an error has occurred during the most recent
17509 tentative parse. */
17510
17511 static bool
17512 cp_parser_error_occurred (cp_parser* parser)
17513 {
17514 return (cp_parser_parsing_tentatively (parser)
17515 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17516 }
17517
17518 /* Returns nonzero if GNU extensions are allowed. */
17519
17520 static bool
17521 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17522 {
17523 return parser->allow_gnu_extensions_p;
17524 }
17525 \f
17526 /* Objective-C++ Productions */
17527
17528
17529 /* Parse an Objective-C expression, which feeds into a primary-expression
17530 above.
17531
17532 objc-expression:
17533 objc-message-expression
17534 objc-string-literal
17535 objc-encode-expression
17536 objc-protocol-expression
17537 objc-selector-expression
17538
17539 Returns a tree representation of the expression. */
17540
17541 static tree
17542 cp_parser_objc_expression (cp_parser* parser)
17543 {
17544 /* Try to figure out what kind of declaration is present. */
17545 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17546
17547 switch (kwd->type)
17548 {
17549 case CPP_OPEN_SQUARE:
17550 return cp_parser_objc_message_expression (parser);
17551
17552 case CPP_OBJC_STRING:
17553 kwd = cp_lexer_consume_token (parser->lexer);
17554 return objc_build_string_object (kwd->u.value);
17555
17556 case CPP_KEYWORD:
17557 switch (kwd->keyword)
17558 {
17559 case RID_AT_ENCODE:
17560 return cp_parser_objc_encode_expression (parser);
17561
17562 case RID_AT_PROTOCOL:
17563 return cp_parser_objc_protocol_expression (parser);
17564
17565 case RID_AT_SELECTOR:
17566 return cp_parser_objc_selector_expression (parser);
17567
17568 default:
17569 break;
17570 }
17571 default:
17572 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17573 cp_parser_skip_to_end_of_block_or_statement (parser);
17574 }
17575
17576 return error_mark_node;
17577 }
17578
17579 /* Parse an Objective-C message expression.
17580
17581 objc-message-expression:
17582 [ objc-message-receiver objc-message-args ]
17583
17584 Returns a representation of an Objective-C message. */
17585
17586 static tree
17587 cp_parser_objc_message_expression (cp_parser* parser)
17588 {
17589 tree receiver, messageargs;
17590
17591 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17592 receiver = cp_parser_objc_message_receiver (parser);
17593 messageargs = cp_parser_objc_message_args (parser);
17594 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17595
17596 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17597 }
17598
17599 /* Parse an objc-message-receiver.
17600
17601 objc-message-receiver:
17602 expression
17603 simple-type-specifier
17604
17605 Returns a representation of the type or expression. */
17606
17607 static tree
17608 cp_parser_objc_message_receiver (cp_parser* parser)
17609 {
17610 tree rcv;
17611
17612 /* An Objective-C message receiver may be either (1) a type
17613 or (2) an expression. */
17614 cp_parser_parse_tentatively (parser);
17615 rcv = cp_parser_expression (parser, false);
17616
17617 if (cp_parser_parse_definitely (parser))
17618 return rcv;
17619
17620 rcv = cp_parser_simple_type_specifier (parser,
17621 /*decl_specs=*/NULL,
17622 CP_PARSER_FLAGS_NONE);
17623
17624 return objc_get_class_reference (rcv);
17625 }
17626
17627 /* Parse the arguments and selectors comprising an Objective-C message.
17628
17629 objc-message-args:
17630 objc-selector
17631 objc-selector-args
17632 objc-selector-args , objc-comma-args
17633
17634 objc-selector-args:
17635 objc-selector [opt] : assignment-expression
17636 objc-selector-args objc-selector [opt] : assignment-expression
17637
17638 objc-comma-args:
17639 assignment-expression
17640 objc-comma-args , assignment-expression
17641
17642 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17643 selector arguments and TREE_VALUE containing a list of comma
17644 arguments. */
17645
17646 static tree
17647 cp_parser_objc_message_args (cp_parser* parser)
17648 {
17649 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17650 bool maybe_unary_selector_p = true;
17651 cp_token *token = cp_lexer_peek_token (parser->lexer);
17652
17653 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17654 {
17655 tree selector = NULL_TREE, arg;
17656
17657 if (token->type != CPP_COLON)
17658 selector = cp_parser_objc_selector (parser);
17659
17660 /* Detect if we have a unary selector. */
17661 if (maybe_unary_selector_p
17662 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17663 return build_tree_list (selector, NULL_TREE);
17664
17665 maybe_unary_selector_p = false;
17666 cp_parser_require (parser, CPP_COLON, "`:'");
17667 arg = cp_parser_assignment_expression (parser, false);
17668
17669 sel_args
17670 = chainon (sel_args,
17671 build_tree_list (selector, arg));
17672
17673 token = cp_lexer_peek_token (parser->lexer);
17674 }
17675
17676 /* Handle non-selector arguments, if any. */
17677 while (token->type == CPP_COMMA)
17678 {
17679 tree arg;
17680
17681 cp_lexer_consume_token (parser->lexer);
17682 arg = cp_parser_assignment_expression (parser, false);
17683
17684 addl_args
17685 = chainon (addl_args,
17686 build_tree_list (NULL_TREE, arg));
17687
17688 token = cp_lexer_peek_token (parser->lexer);
17689 }
17690
17691 return build_tree_list (sel_args, addl_args);
17692 }
17693
17694 /* Parse an Objective-C encode expression.
17695
17696 objc-encode-expression:
17697 @encode objc-typename
17698
17699 Returns an encoded representation of the type argument. */
17700
17701 static tree
17702 cp_parser_objc_encode_expression (cp_parser* parser)
17703 {
17704 tree type;
17705
17706 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17707 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17708 type = complete_type (cp_parser_type_id (parser));
17709 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17710
17711 if (!type)
17712 {
17713 error ("%<@encode%> must specify a type as an argument");
17714 return error_mark_node;
17715 }
17716
17717 return objc_build_encode_expr (type);
17718 }
17719
17720 /* Parse an Objective-C @defs expression. */
17721
17722 static tree
17723 cp_parser_objc_defs_expression (cp_parser *parser)
17724 {
17725 tree name;
17726
17727 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17728 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17729 name = cp_parser_identifier (parser);
17730 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17731
17732 return objc_get_class_ivars (name);
17733 }
17734
17735 /* Parse an Objective-C protocol expression.
17736
17737 objc-protocol-expression:
17738 @protocol ( identifier )
17739
17740 Returns a representation of the protocol expression. */
17741
17742 static tree
17743 cp_parser_objc_protocol_expression (cp_parser* parser)
17744 {
17745 tree proto;
17746
17747 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17748 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17749 proto = cp_parser_identifier (parser);
17750 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17751
17752 return objc_build_protocol_expr (proto);
17753 }
17754
17755 /* Parse an Objective-C selector expression.
17756
17757 objc-selector-expression:
17758 @selector ( objc-method-signature )
17759
17760 objc-method-signature:
17761 objc-selector
17762 objc-selector-seq
17763
17764 objc-selector-seq:
17765 objc-selector :
17766 objc-selector-seq objc-selector :
17767
17768 Returns a representation of the method selector. */
17769
17770 static tree
17771 cp_parser_objc_selector_expression (cp_parser* parser)
17772 {
17773 tree sel_seq = NULL_TREE;
17774 bool maybe_unary_selector_p = true;
17775 cp_token *token;
17776
17777 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17778 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17779 token = cp_lexer_peek_token (parser->lexer);
17780
17781 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17782 || token->type == CPP_SCOPE)
17783 {
17784 tree selector = NULL_TREE;
17785
17786 if (token->type != CPP_COLON
17787 || token->type == CPP_SCOPE)
17788 selector = cp_parser_objc_selector (parser);
17789
17790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17791 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17792 {
17793 /* Detect if we have a unary selector. */
17794 if (maybe_unary_selector_p)
17795 {
17796 sel_seq = selector;
17797 goto finish_selector;
17798 }
17799 else
17800 {
17801 cp_parser_error (parser, "expected %<:%>");
17802 }
17803 }
17804 maybe_unary_selector_p = false;
17805 token = cp_lexer_consume_token (parser->lexer);
17806
17807 if (token->type == CPP_SCOPE)
17808 {
17809 sel_seq
17810 = chainon (sel_seq,
17811 build_tree_list (selector, NULL_TREE));
17812 sel_seq
17813 = chainon (sel_seq,
17814 build_tree_list (NULL_TREE, NULL_TREE));
17815 }
17816 else
17817 sel_seq
17818 = chainon (sel_seq,
17819 build_tree_list (selector, NULL_TREE));
17820
17821 token = cp_lexer_peek_token (parser->lexer);
17822 }
17823
17824 finish_selector:
17825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17826
17827 return objc_build_selector_expr (sel_seq);
17828 }
17829
17830 /* Parse a list of identifiers.
17831
17832 objc-identifier-list:
17833 identifier
17834 objc-identifier-list , identifier
17835
17836 Returns a TREE_LIST of identifier nodes. */
17837
17838 static tree
17839 cp_parser_objc_identifier_list (cp_parser* parser)
17840 {
17841 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17842 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17843
17844 while (sep->type == CPP_COMMA)
17845 {
17846 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17847 list = chainon (list,
17848 build_tree_list (NULL_TREE,
17849 cp_parser_identifier (parser)));
17850 sep = cp_lexer_peek_token (parser->lexer);
17851 }
17852
17853 return list;
17854 }
17855
17856 /* Parse an Objective-C alias declaration.
17857
17858 objc-alias-declaration:
17859 @compatibility_alias identifier identifier ;
17860
17861 This function registers the alias mapping with the Objective-C front end.
17862 It returns nothing. */
17863
17864 static void
17865 cp_parser_objc_alias_declaration (cp_parser* parser)
17866 {
17867 tree alias, orig;
17868
17869 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17870 alias = cp_parser_identifier (parser);
17871 orig = cp_parser_identifier (parser);
17872 objc_declare_alias (alias, orig);
17873 cp_parser_consume_semicolon_at_end_of_statement (parser);
17874 }
17875
17876 /* Parse an Objective-C class forward-declaration.
17877
17878 objc-class-declaration:
17879 @class objc-identifier-list ;
17880
17881 The function registers the forward declarations with the Objective-C
17882 front end. It returns nothing. */
17883
17884 static void
17885 cp_parser_objc_class_declaration (cp_parser* parser)
17886 {
17887 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17888 objc_declare_class (cp_parser_objc_identifier_list (parser));
17889 cp_parser_consume_semicolon_at_end_of_statement (parser);
17890 }
17891
17892 /* Parse a list of Objective-C protocol references.
17893
17894 objc-protocol-refs-opt:
17895 objc-protocol-refs [opt]
17896
17897 objc-protocol-refs:
17898 < objc-identifier-list >
17899
17900 Returns a TREE_LIST of identifiers, if any. */
17901
17902 static tree
17903 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17904 {
17905 tree protorefs = NULL_TREE;
17906
17907 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17908 {
17909 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17910 protorefs = cp_parser_objc_identifier_list (parser);
17911 cp_parser_require (parser, CPP_GREATER, "`>'");
17912 }
17913
17914 return protorefs;
17915 }
17916
17917 /* Parse a Objective-C visibility specification. */
17918
17919 static void
17920 cp_parser_objc_visibility_spec (cp_parser* parser)
17921 {
17922 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17923
17924 switch (vis->keyword)
17925 {
17926 case RID_AT_PRIVATE:
17927 objc_set_visibility (2);
17928 break;
17929 case RID_AT_PROTECTED:
17930 objc_set_visibility (0);
17931 break;
17932 case RID_AT_PUBLIC:
17933 objc_set_visibility (1);
17934 break;
17935 default:
17936 return;
17937 }
17938
17939 /* Eat '@private'/'@protected'/'@public'. */
17940 cp_lexer_consume_token (parser->lexer);
17941 }
17942
17943 /* Parse an Objective-C method type. */
17944
17945 static void
17946 cp_parser_objc_method_type (cp_parser* parser)
17947 {
17948 objc_set_method_type
17949 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17950 ? PLUS_EXPR
17951 : MINUS_EXPR);
17952 }
17953
17954 /* Parse an Objective-C protocol qualifier. */
17955
17956 static tree
17957 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17958 {
17959 tree quals = NULL_TREE, node;
17960 cp_token *token = cp_lexer_peek_token (parser->lexer);
17961
17962 node = token->u.value;
17963
17964 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17965 && (node == ridpointers [(int) RID_IN]
17966 || node == ridpointers [(int) RID_OUT]
17967 || node == ridpointers [(int) RID_INOUT]
17968 || node == ridpointers [(int) RID_BYCOPY]
17969 || node == ridpointers [(int) RID_BYREF]
17970 || node == ridpointers [(int) RID_ONEWAY]))
17971 {
17972 quals = tree_cons (NULL_TREE, node, quals);
17973 cp_lexer_consume_token (parser->lexer);
17974 token = cp_lexer_peek_token (parser->lexer);
17975 node = token->u.value;
17976 }
17977
17978 return quals;
17979 }
17980
17981 /* Parse an Objective-C typename. */
17982
17983 static tree
17984 cp_parser_objc_typename (cp_parser* parser)
17985 {
17986 tree typename = NULL_TREE;
17987
17988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17989 {
17990 tree proto_quals, cp_type = NULL_TREE;
17991
17992 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17993 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17994
17995 /* An ObjC type name may consist of just protocol qualifiers, in which
17996 case the type shall default to 'id'. */
17997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17998 cp_type = cp_parser_type_id (parser);
17999
18000 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18001 typename = build_tree_list (proto_quals, cp_type);
18002 }
18003
18004 return typename;
18005 }
18006
18007 /* Check to see if TYPE refers to an Objective-C selector name. */
18008
18009 static bool
18010 cp_parser_objc_selector_p (enum cpp_ttype type)
18011 {
18012 return (type == CPP_NAME || type == CPP_KEYWORD
18013 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18014 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18015 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18016 || type == CPP_XOR || type == CPP_XOR_EQ);
18017 }
18018
18019 /* Parse an Objective-C selector. */
18020
18021 static tree
18022 cp_parser_objc_selector (cp_parser* parser)
18023 {
18024 cp_token *token = cp_lexer_consume_token (parser->lexer);
18025
18026 if (!cp_parser_objc_selector_p (token->type))
18027 {
18028 error ("invalid Objective-C++ selector name");
18029 return error_mark_node;
18030 }
18031
18032 /* C++ operator names are allowed to appear in ObjC selectors. */
18033 switch (token->type)
18034 {
18035 case CPP_AND_AND: return get_identifier ("and");
18036 case CPP_AND_EQ: return get_identifier ("and_eq");
18037 case CPP_AND: return get_identifier ("bitand");
18038 case CPP_OR: return get_identifier ("bitor");
18039 case CPP_COMPL: return get_identifier ("compl");
18040 case CPP_NOT: return get_identifier ("not");
18041 case CPP_NOT_EQ: return get_identifier ("not_eq");
18042 case CPP_OR_OR: return get_identifier ("or");
18043 case CPP_OR_EQ: return get_identifier ("or_eq");
18044 case CPP_XOR: return get_identifier ("xor");
18045 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18046 default: return token->u.value;
18047 }
18048 }
18049
18050 /* Parse an Objective-C params list. */
18051
18052 static tree
18053 cp_parser_objc_method_keyword_params (cp_parser* parser)
18054 {
18055 tree params = NULL_TREE;
18056 bool maybe_unary_selector_p = true;
18057 cp_token *token = cp_lexer_peek_token (parser->lexer);
18058
18059 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18060 {
18061 tree selector = NULL_TREE, typename, identifier;
18062
18063 if (token->type != CPP_COLON)
18064 selector = cp_parser_objc_selector (parser);
18065
18066 /* Detect if we have a unary selector. */
18067 if (maybe_unary_selector_p
18068 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18069 return selector;
18070
18071 maybe_unary_selector_p = false;
18072 cp_parser_require (parser, CPP_COLON, "`:'");
18073 typename = cp_parser_objc_typename (parser);
18074 identifier = cp_parser_identifier (parser);
18075
18076 params
18077 = chainon (params,
18078 objc_build_keyword_decl (selector,
18079 typename,
18080 identifier));
18081
18082 token = cp_lexer_peek_token (parser->lexer);
18083 }
18084
18085 return params;
18086 }
18087
18088 /* Parse the non-keyword Objective-C params. */
18089
18090 static tree
18091 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18092 {
18093 tree params = make_node (TREE_LIST);
18094 cp_token *token = cp_lexer_peek_token (parser->lexer);
18095 *ellipsisp = false; /* Initially, assume no ellipsis. */
18096
18097 while (token->type == CPP_COMMA)
18098 {
18099 cp_parameter_declarator *parmdecl;
18100 tree parm;
18101
18102 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18103 token = cp_lexer_peek_token (parser->lexer);
18104
18105 if (token->type == CPP_ELLIPSIS)
18106 {
18107 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18108 *ellipsisp = true;
18109 break;
18110 }
18111
18112 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18113 parm = grokdeclarator (parmdecl->declarator,
18114 &parmdecl->decl_specifiers,
18115 PARM, /*initialized=*/0,
18116 /*attrlist=*/NULL);
18117
18118 chainon (params, build_tree_list (NULL_TREE, parm));
18119 token = cp_lexer_peek_token (parser->lexer);
18120 }
18121
18122 return params;
18123 }
18124
18125 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18126
18127 static void
18128 cp_parser_objc_interstitial_code (cp_parser* parser)
18129 {
18130 cp_token *token = cp_lexer_peek_token (parser->lexer);
18131
18132 /* If the next token is `extern' and the following token is a string
18133 literal, then we have a linkage specification. */
18134 if (token->keyword == RID_EXTERN
18135 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18136 cp_parser_linkage_specification (parser);
18137 /* Handle #pragma, if any. */
18138 else if (token->type == CPP_PRAGMA)
18139 cp_parser_pragma (parser, pragma_external);
18140 /* Allow stray semicolons. */
18141 else if (token->type == CPP_SEMICOLON)
18142 cp_lexer_consume_token (parser->lexer);
18143 /* Finally, try to parse a block-declaration, or a function-definition. */
18144 else
18145 cp_parser_block_declaration (parser, /*statement_p=*/false);
18146 }
18147
18148 /* Parse a method signature. */
18149
18150 static tree
18151 cp_parser_objc_method_signature (cp_parser* parser)
18152 {
18153 tree rettype, kwdparms, optparms;
18154 bool ellipsis = false;
18155
18156 cp_parser_objc_method_type (parser);
18157 rettype = cp_parser_objc_typename (parser);
18158 kwdparms = cp_parser_objc_method_keyword_params (parser);
18159 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18160
18161 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18162 }
18163
18164 /* Pars an Objective-C method prototype list. */
18165
18166 static void
18167 cp_parser_objc_method_prototype_list (cp_parser* parser)
18168 {
18169 cp_token *token = cp_lexer_peek_token (parser->lexer);
18170
18171 while (token->keyword != RID_AT_END)
18172 {
18173 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18174 {
18175 objc_add_method_declaration
18176 (cp_parser_objc_method_signature (parser));
18177 cp_parser_consume_semicolon_at_end_of_statement (parser);
18178 }
18179 else
18180 /* Allow for interspersed non-ObjC++ code. */
18181 cp_parser_objc_interstitial_code (parser);
18182
18183 token = cp_lexer_peek_token (parser->lexer);
18184 }
18185
18186 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18187 objc_finish_interface ();
18188 }
18189
18190 /* Parse an Objective-C method definition list. */
18191
18192 static void
18193 cp_parser_objc_method_definition_list (cp_parser* parser)
18194 {
18195 cp_token *token = cp_lexer_peek_token (parser->lexer);
18196
18197 while (token->keyword != RID_AT_END)
18198 {
18199 tree meth;
18200
18201 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18202 {
18203 push_deferring_access_checks (dk_deferred);
18204 objc_start_method_definition
18205 (cp_parser_objc_method_signature (parser));
18206
18207 /* For historical reasons, we accept an optional semicolon. */
18208 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18209 cp_lexer_consume_token (parser->lexer);
18210
18211 perform_deferred_access_checks ();
18212 stop_deferring_access_checks ();
18213 meth = cp_parser_function_definition_after_declarator (parser,
18214 false);
18215 pop_deferring_access_checks ();
18216 objc_finish_method_definition (meth);
18217 }
18218 else
18219 /* Allow for interspersed non-ObjC++ code. */
18220 cp_parser_objc_interstitial_code (parser);
18221
18222 token = cp_lexer_peek_token (parser->lexer);
18223 }
18224
18225 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18226 objc_finish_implementation ();
18227 }
18228
18229 /* Parse Objective-C ivars. */
18230
18231 static void
18232 cp_parser_objc_class_ivars (cp_parser* parser)
18233 {
18234 cp_token *token = cp_lexer_peek_token (parser->lexer);
18235
18236 if (token->type != CPP_OPEN_BRACE)
18237 return; /* No ivars specified. */
18238
18239 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18240 token = cp_lexer_peek_token (parser->lexer);
18241
18242 while (token->type != CPP_CLOSE_BRACE)
18243 {
18244 cp_decl_specifier_seq declspecs;
18245 int decl_class_or_enum_p;
18246 tree prefix_attributes;
18247
18248 cp_parser_objc_visibility_spec (parser);
18249
18250 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18251 break;
18252
18253 cp_parser_decl_specifier_seq (parser,
18254 CP_PARSER_FLAGS_OPTIONAL,
18255 &declspecs,
18256 &decl_class_or_enum_p);
18257 prefix_attributes = declspecs.attributes;
18258 declspecs.attributes = NULL_TREE;
18259
18260 /* Keep going until we hit the `;' at the end of the
18261 declaration. */
18262 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18263 {
18264 tree width = NULL_TREE, attributes, first_attribute, decl;
18265 cp_declarator *declarator = NULL;
18266 int ctor_dtor_or_conv_p;
18267
18268 /* Check for a (possibly unnamed) bitfield declaration. */
18269 token = cp_lexer_peek_token (parser->lexer);
18270 if (token->type == CPP_COLON)
18271 goto eat_colon;
18272
18273 if (token->type == CPP_NAME
18274 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18275 == CPP_COLON))
18276 {
18277 /* Get the name of the bitfield. */
18278 declarator = make_id_declarator (NULL_TREE,
18279 cp_parser_identifier (parser),
18280 sfk_none);
18281
18282 eat_colon:
18283 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18284 /* Get the width of the bitfield. */
18285 width
18286 = cp_parser_constant_expression (parser,
18287 /*allow_non_constant=*/false,
18288 NULL);
18289 }
18290 else
18291 {
18292 /* Parse the declarator. */
18293 declarator
18294 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18295 &ctor_dtor_or_conv_p,
18296 /*parenthesized_p=*/NULL,
18297 /*member_p=*/false);
18298 }
18299
18300 /* Look for attributes that apply to the ivar. */
18301 attributes = cp_parser_attributes_opt (parser);
18302 /* Remember which attributes are prefix attributes and
18303 which are not. */
18304 first_attribute = attributes;
18305 /* Combine the attributes. */
18306 attributes = chainon (prefix_attributes, attributes);
18307
18308 if (width)
18309 {
18310 /* Create the bitfield declaration. */
18311 decl = grokbitfield (declarator, &declspecs, width);
18312 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18313 }
18314 else
18315 decl = grokfield (declarator, &declspecs,
18316 NULL_TREE, /*init_const_expr_p=*/false,
18317 NULL_TREE, attributes);
18318
18319 /* Add the instance variable. */
18320 objc_add_instance_variable (decl);
18321
18322 /* Reset PREFIX_ATTRIBUTES. */
18323 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18324 attributes = TREE_CHAIN (attributes);
18325 if (attributes)
18326 TREE_CHAIN (attributes) = NULL_TREE;
18327
18328 token = cp_lexer_peek_token (parser->lexer);
18329
18330 if (token->type == CPP_COMMA)
18331 {
18332 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18333 continue;
18334 }
18335 break;
18336 }
18337
18338 cp_parser_consume_semicolon_at_end_of_statement (parser);
18339 token = cp_lexer_peek_token (parser->lexer);
18340 }
18341
18342 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18343 /* For historical reasons, we accept an optional semicolon. */
18344 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18345 cp_lexer_consume_token (parser->lexer);
18346 }
18347
18348 /* Parse an Objective-C protocol declaration. */
18349
18350 static void
18351 cp_parser_objc_protocol_declaration (cp_parser* parser)
18352 {
18353 tree proto, protorefs;
18354 cp_token *tok;
18355
18356 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18358 {
18359 error ("identifier expected after %<@protocol%>");
18360 goto finish;
18361 }
18362
18363 /* See if we have a forward declaration or a definition. */
18364 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18365
18366 /* Try a forward declaration first. */
18367 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18368 {
18369 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18370 finish:
18371 cp_parser_consume_semicolon_at_end_of_statement (parser);
18372 }
18373
18374 /* Ok, we got a full-fledged definition (or at least should). */
18375 else
18376 {
18377 proto = cp_parser_identifier (parser);
18378 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18379 objc_start_protocol (proto, protorefs);
18380 cp_parser_objc_method_prototype_list (parser);
18381 }
18382 }
18383
18384 /* Parse an Objective-C superclass or category. */
18385
18386 static void
18387 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18388 tree *categ)
18389 {
18390 cp_token *next = cp_lexer_peek_token (parser->lexer);
18391
18392 *super = *categ = NULL_TREE;
18393 if (next->type == CPP_COLON)
18394 {
18395 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18396 *super = cp_parser_identifier (parser);
18397 }
18398 else if (next->type == CPP_OPEN_PAREN)
18399 {
18400 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18401 *categ = cp_parser_identifier (parser);
18402 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18403 }
18404 }
18405
18406 /* Parse an Objective-C class interface. */
18407
18408 static void
18409 cp_parser_objc_class_interface (cp_parser* parser)
18410 {
18411 tree name, super, categ, protos;
18412
18413 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18414 name = cp_parser_identifier (parser);
18415 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18416 protos = cp_parser_objc_protocol_refs_opt (parser);
18417
18418 /* We have either a class or a category on our hands. */
18419 if (categ)
18420 objc_start_category_interface (name, categ, protos);
18421 else
18422 {
18423 objc_start_class_interface (name, super, protos);
18424 /* Handle instance variable declarations, if any. */
18425 cp_parser_objc_class_ivars (parser);
18426 objc_continue_interface ();
18427 }
18428
18429 cp_parser_objc_method_prototype_list (parser);
18430 }
18431
18432 /* Parse an Objective-C class implementation. */
18433
18434 static void
18435 cp_parser_objc_class_implementation (cp_parser* parser)
18436 {
18437 tree name, super, categ;
18438
18439 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18440 name = cp_parser_identifier (parser);
18441 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18442
18443 /* We have either a class or a category on our hands. */
18444 if (categ)
18445 objc_start_category_implementation (name, categ);
18446 else
18447 {
18448 objc_start_class_implementation (name, super);
18449 /* Handle instance variable declarations, if any. */
18450 cp_parser_objc_class_ivars (parser);
18451 objc_continue_implementation ();
18452 }
18453
18454 cp_parser_objc_method_definition_list (parser);
18455 }
18456
18457 /* Consume the @end token and finish off the implementation. */
18458
18459 static void
18460 cp_parser_objc_end_implementation (cp_parser* parser)
18461 {
18462 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18463 objc_finish_implementation ();
18464 }
18465
18466 /* Parse an Objective-C declaration. */
18467
18468 static void
18469 cp_parser_objc_declaration (cp_parser* parser)
18470 {
18471 /* Try to figure out what kind of declaration is present. */
18472 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18473
18474 switch (kwd->keyword)
18475 {
18476 case RID_AT_ALIAS:
18477 cp_parser_objc_alias_declaration (parser);
18478 break;
18479 case RID_AT_CLASS:
18480 cp_parser_objc_class_declaration (parser);
18481 break;
18482 case RID_AT_PROTOCOL:
18483 cp_parser_objc_protocol_declaration (parser);
18484 break;
18485 case RID_AT_INTERFACE:
18486 cp_parser_objc_class_interface (parser);
18487 break;
18488 case RID_AT_IMPLEMENTATION:
18489 cp_parser_objc_class_implementation (parser);
18490 break;
18491 case RID_AT_END:
18492 cp_parser_objc_end_implementation (parser);
18493 break;
18494 default:
18495 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18496 cp_parser_skip_to_end_of_block_or_statement (parser);
18497 }
18498 }
18499
18500 /* Parse an Objective-C try-catch-finally statement.
18501
18502 objc-try-catch-finally-stmt:
18503 @try compound-statement objc-catch-clause-seq [opt]
18504 objc-finally-clause [opt]
18505
18506 objc-catch-clause-seq:
18507 objc-catch-clause objc-catch-clause-seq [opt]
18508
18509 objc-catch-clause:
18510 @catch ( exception-declaration ) compound-statement
18511
18512 objc-finally-clause
18513 @finally compound-statement
18514
18515 Returns NULL_TREE. */
18516
18517 static tree
18518 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18519 location_t location;
18520 tree stmt;
18521
18522 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18523 location = cp_lexer_peek_token (parser->lexer)->location;
18524 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18525 node, lest it get absorbed into the surrounding block. */
18526 stmt = push_stmt_list ();
18527 cp_parser_compound_statement (parser, NULL, false);
18528 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18529
18530 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18531 {
18532 cp_parameter_declarator *parmdecl;
18533 tree parm;
18534
18535 cp_lexer_consume_token (parser->lexer);
18536 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18537 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18538 parm = grokdeclarator (parmdecl->declarator,
18539 &parmdecl->decl_specifiers,
18540 PARM, /*initialized=*/0,
18541 /*attrlist=*/NULL);
18542 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18543 objc_begin_catch_clause (parm);
18544 cp_parser_compound_statement (parser, NULL, false);
18545 objc_finish_catch_clause ();
18546 }
18547
18548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18549 {
18550 cp_lexer_consume_token (parser->lexer);
18551 location = cp_lexer_peek_token (parser->lexer)->location;
18552 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18553 node, lest it get absorbed into the surrounding block. */
18554 stmt = push_stmt_list ();
18555 cp_parser_compound_statement (parser, NULL, false);
18556 objc_build_finally_clause (location, pop_stmt_list (stmt));
18557 }
18558
18559 return objc_finish_try_stmt ();
18560 }
18561
18562 /* Parse an Objective-C synchronized statement.
18563
18564 objc-synchronized-stmt:
18565 @synchronized ( expression ) compound-statement
18566
18567 Returns NULL_TREE. */
18568
18569 static tree
18570 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18571 location_t location;
18572 tree lock, stmt;
18573
18574 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18575
18576 location = cp_lexer_peek_token (parser->lexer)->location;
18577 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18578 lock = cp_parser_expression (parser, false);
18579 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18580
18581 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18582 node, lest it get absorbed into the surrounding block. */
18583 stmt = push_stmt_list ();
18584 cp_parser_compound_statement (parser, NULL, false);
18585
18586 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18587 }
18588
18589 /* Parse an Objective-C throw statement.
18590
18591 objc-throw-stmt:
18592 @throw assignment-expression [opt] ;
18593
18594 Returns a constructed '@throw' statement. */
18595
18596 static tree
18597 cp_parser_objc_throw_statement (cp_parser *parser) {
18598 tree expr = NULL_TREE;
18599
18600 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18601
18602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18603 expr = cp_parser_assignment_expression (parser, false);
18604
18605 cp_parser_consume_semicolon_at_end_of_statement (parser);
18606
18607 return objc_build_throw_stmt (expr);
18608 }
18609
18610 /* Parse an Objective-C statement. */
18611
18612 static tree
18613 cp_parser_objc_statement (cp_parser * parser) {
18614 /* Try to figure out what kind of declaration is present. */
18615 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18616
18617 switch (kwd->keyword)
18618 {
18619 case RID_AT_TRY:
18620 return cp_parser_objc_try_catch_finally_statement (parser);
18621 case RID_AT_SYNCHRONIZED:
18622 return cp_parser_objc_synchronized_statement (parser);
18623 case RID_AT_THROW:
18624 return cp_parser_objc_throw_statement (parser);
18625 default:
18626 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18627 cp_parser_skip_to_end_of_block_or_statement (parser);
18628 }
18629
18630 return error_mark_node;
18631 }
18632 \f
18633 /* OpenMP 2.5 parsing routines. */
18634
18635 /* Returns name of the next clause.
18636 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18637 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18638 returned and the token is consumed. */
18639
18640 static pragma_omp_clause
18641 cp_parser_omp_clause_name (cp_parser *parser)
18642 {
18643 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18644
18645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18646 result = PRAGMA_OMP_CLAUSE_IF;
18647 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18648 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18649 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18650 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18651 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18652 {
18653 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18654 const char *p = IDENTIFIER_POINTER (id);
18655
18656 switch (p[0])
18657 {
18658 case 'c':
18659 if (!strcmp ("copyin", p))
18660 result = PRAGMA_OMP_CLAUSE_COPYIN;
18661 else if (!strcmp ("copyprivate", p))
18662 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18663 break;
18664 case 'f':
18665 if (!strcmp ("firstprivate", p))
18666 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18667 break;
18668 case 'l':
18669 if (!strcmp ("lastprivate", p))
18670 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18671 break;
18672 case 'n':
18673 if (!strcmp ("nowait", p))
18674 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18675 else if (!strcmp ("num_threads", p))
18676 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18677 break;
18678 case 'o':
18679 if (!strcmp ("ordered", p))
18680 result = PRAGMA_OMP_CLAUSE_ORDERED;
18681 break;
18682 case 'r':
18683 if (!strcmp ("reduction", p))
18684 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18685 break;
18686 case 's':
18687 if (!strcmp ("schedule", p))
18688 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18689 else if (!strcmp ("shared", p))
18690 result = PRAGMA_OMP_CLAUSE_SHARED;
18691 break;
18692 }
18693 }
18694
18695 if (result != PRAGMA_OMP_CLAUSE_NONE)
18696 cp_lexer_consume_token (parser->lexer);
18697
18698 return result;
18699 }
18700
18701 /* Validate that a clause of the given type does not already exist. */
18702
18703 static void
18704 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18705 {
18706 tree c;
18707
18708 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18709 if (OMP_CLAUSE_CODE (c) == code)
18710 {
18711 error ("too many %qs clauses", name);
18712 break;
18713 }
18714 }
18715
18716 /* OpenMP 2.5:
18717 variable-list:
18718 identifier
18719 variable-list , identifier
18720
18721 In addition, we match a closing parenthesis. An opening parenthesis
18722 will have been consumed by the caller.
18723
18724 If KIND is nonzero, create the appropriate node and install the decl
18725 in OMP_CLAUSE_DECL and add the node to the head of the list.
18726
18727 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18728 return the list created. */
18729
18730 static tree
18731 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18732 tree list)
18733 {
18734 while (1)
18735 {
18736 tree name, decl;
18737
18738 name = cp_parser_id_expression (parser, /*template_p=*/false,
18739 /*check_dependency_p=*/true,
18740 /*template_p=*/NULL,
18741 /*declarator_p=*/false,
18742 /*optional_p=*/false);
18743 if (name == error_mark_node)
18744 goto skip_comma;
18745
18746 decl = cp_parser_lookup_name_simple (parser, name);
18747 if (decl == error_mark_node)
18748 cp_parser_name_lookup_error (parser, name, decl, NULL);
18749 else if (kind != 0)
18750 {
18751 tree u = build_omp_clause (kind);
18752 OMP_CLAUSE_DECL (u) = decl;
18753 OMP_CLAUSE_CHAIN (u) = list;
18754 list = u;
18755 }
18756 else
18757 list = tree_cons (decl, NULL_TREE, list);
18758
18759 get_comma:
18760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18761 break;
18762 cp_lexer_consume_token (parser->lexer);
18763 }
18764
18765 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18766 {
18767 int ending;
18768
18769 /* Try to resync to an unnested comma. Copied from
18770 cp_parser_parenthesized_expression_list. */
18771 skip_comma:
18772 ending = cp_parser_skip_to_closing_parenthesis (parser,
18773 /*recovering=*/true,
18774 /*or_comma=*/true,
18775 /*consume_paren=*/true);
18776 if (ending < 0)
18777 goto get_comma;
18778 }
18779
18780 return list;
18781 }
18782
18783 /* Similarly, but expect leading and trailing parenthesis. This is a very
18784 common case for omp clauses. */
18785
18786 static tree
18787 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18788 {
18789 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18790 return cp_parser_omp_var_list_no_open (parser, kind, list);
18791 return list;
18792 }
18793
18794 /* OpenMP 2.5:
18795 default ( shared | none ) */
18796
18797 static tree
18798 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18799 {
18800 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18801 tree c;
18802
18803 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18804 return list;
18805 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18806 {
18807 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18808 const char *p = IDENTIFIER_POINTER (id);
18809
18810 switch (p[0])
18811 {
18812 case 'n':
18813 if (strcmp ("none", p) != 0)
18814 goto invalid_kind;
18815 kind = OMP_CLAUSE_DEFAULT_NONE;
18816 break;
18817
18818 case 's':
18819 if (strcmp ("shared", p) != 0)
18820 goto invalid_kind;
18821 kind = OMP_CLAUSE_DEFAULT_SHARED;
18822 break;
18823
18824 default:
18825 goto invalid_kind;
18826 }
18827
18828 cp_lexer_consume_token (parser->lexer);
18829 }
18830 else
18831 {
18832 invalid_kind:
18833 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18834 }
18835
18836 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18837 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18838 /*or_comma=*/false,
18839 /*consume_paren=*/true);
18840
18841 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18842 return list;
18843
18844 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18845 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18846 OMP_CLAUSE_CHAIN (c) = list;
18847 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18848
18849 return c;
18850 }
18851
18852 /* OpenMP 2.5:
18853 if ( expression ) */
18854
18855 static tree
18856 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18857 {
18858 tree t, c;
18859
18860 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18861 return list;
18862
18863 t = cp_parser_condition (parser);
18864
18865 if (t == error_mark_node
18866 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18867 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18868 /*or_comma=*/false,
18869 /*consume_paren=*/true);
18870
18871 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18872
18873 c = build_omp_clause (OMP_CLAUSE_IF);
18874 OMP_CLAUSE_IF_EXPR (c) = t;
18875 OMP_CLAUSE_CHAIN (c) = list;
18876
18877 return c;
18878 }
18879
18880 /* OpenMP 2.5:
18881 nowait */
18882
18883 static tree
18884 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18885 {
18886 tree c;
18887
18888 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18889
18890 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18891 OMP_CLAUSE_CHAIN (c) = list;
18892 return c;
18893 }
18894
18895 /* OpenMP 2.5:
18896 num_threads ( expression ) */
18897
18898 static tree
18899 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18900 {
18901 tree t, c;
18902
18903 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18904 return list;
18905
18906 t = cp_parser_expression (parser, false);
18907
18908 if (t == error_mark_node
18909 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18910 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18911 /*or_comma=*/false,
18912 /*consume_paren=*/true);
18913
18914 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18915
18916 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18917 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18918 OMP_CLAUSE_CHAIN (c) = list;
18919
18920 return c;
18921 }
18922
18923 /* OpenMP 2.5:
18924 ordered */
18925
18926 static tree
18927 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18928 {
18929 tree c;
18930
18931 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18932
18933 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18934 OMP_CLAUSE_CHAIN (c) = list;
18935 return c;
18936 }
18937
18938 /* OpenMP 2.5:
18939 reduction ( reduction-operator : variable-list )
18940
18941 reduction-operator:
18942 One of: + * - & ^ | && || */
18943
18944 static tree
18945 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18946 {
18947 enum tree_code code;
18948 tree nlist, c;
18949
18950 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18951 return list;
18952
18953 switch (cp_lexer_peek_token (parser->lexer)->type)
18954 {
18955 case CPP_PLUS:
18956 code = PLUS_EXPR;
18957 break;
18958 case CPP_MULT:
18959 code = MULT_EXPR;
18960 break;
18961 case CPP_MINUS:
18962 code = MINUS_EXPR;
18963 break;
18964 case CPP_AND:
18965 code = BIT_AND_EXPR;
18966 break;
18967 case CPP_XOR:
18968 code = BIT_XOR_EXPR;
18969 break;
18970 case CPP_OR:
18971 code = BIT_IOR_EXPR;
18972 break;
18973 case CPP_AND_AND:
18974 code = TRUTH_ANDIF_EXPR;
18975 break;
18976 case CPP_OR_OR:
18977 code = TRUTH_ORIF_EXPR;
18978 break;
18979 default:
18980 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18981 resync_fail:
18982 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18983 /*or_comma=*/false,
18984 /*consume_paren=*/true);
18985 return list;
18986 }
18987 cp_lexer_consume_token (parser->lexer);
18988
18989 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18990 goto resync_fail;
18991
18992 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18993 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18994 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18995
18996 return nlist;
18997 }
18998
18999 /* OpenMP 2.5:
19000 schedule ( schedule-kind )
19001 schedule ( schedule-kind , expression )
19002
19003 schedule-kind:
19004 static | dynamic | guided | runtime */
19005
19006 static tree
19007 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19008 {
19009 tree c, t;
19010
19011 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19012 return list;
19013
19014 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19015
19016 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19017 {
19018 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19019 const char *p = IDENTIFIER_POINTER (id);
19020
19021 switch (p[0])
19022 {
19023 case 'd':
19024 if (strcmp ("dynamic", p) != 0)
19025 goto invalid_kind;
19026 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19027 break;
19028
19029 case 'g':
19030 if (strcmp ("guided", p) != 0)
19031 goto invalid_kind;
19032 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19033 break;
19034
19035 case 'r':
19036 if (strcmp ("runtime", p) != 0)
19037 goto invalid_kind;
19038 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19039 break;
19040
19041 default:
19042 goto invalid_kind;
19043 }
19044 }
19045 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19046 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19047 else
19048 goto invalid_kind;
19049 cp_lexer_consume_token (parser->lexer);
19050
19051 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19052 {
19053 cp_lexer_consume_token (parser->lexer);
19054
19055 t = cp_parser_assignment_expression (parser, false);
19056
19057 if (t == error_mark_node)
19058 goto resync_fail;
19059 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19060 error ("schedule %<runtime%> does not take "
19061 "a %<chunk_size%> parameter");
19062 else
19063 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19064
19065 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19066 goto resync_fail;
19067 }
19068 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19069 goto resync_fail;
19070
19071 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19072 OMP_CLAUSE_CHAIN (c) = list;
19073 return c;
19074
19075 invalid_kind:
19076 cp_parser_error (parser, "invalid schedule kind");
19077 resync_fail:
19078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19079 /*or_comma=*/false,
19080 /*consume_paren=*/true);
19081 return list;
19082 }
19083
19084 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19085 is a bitmask in MASK. Return the list of clauses found; the result
19086 of clause default goes in *pdefault. */
19087
19088 static tree
19089 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19090 const char *where, cp_token *pragma_tok)
19091 {
19092 tree clauses = NULL;
19093
19094 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19095 {
19096 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19097 const char *c_name;
19098 tree prev = clauses;
19099
19100 switch (c_kind)
19101 {
19102 case PRAGMA_OMP_CLAUSE_COPYIN:
19103 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19104 c_name = "copyin";
19105 break;
19106 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19107 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19108 clauses);
19109 c_name = "copyprivate";
19110 break;
19111 case PRAGMA_OMP_CLAUSE_DEFAULT:
19112 clauses = cp_parser_omp_clause_default (parser, clauses);
19113 c_name = "default";
19114 break;
19115 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19116 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19117 clauses);
19118 c_name = "firstprivate";
19119 break;
19120 case PRAGMA_OMP_CLAUSE_IF:
19121 clauses = cp_parser_omp_clause_if (parser, clauses);
19122 c_name = "if";
19123 break;
19124 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19125 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19126 clauses);
19127 c_name = "lastprivate";
19128 break;
19129 case PRAGMA_OMP_CLAUSE_NOWAIT:
19130 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19131 c_name = "nowait";
19132 break;
19133 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19134 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19135 c_name = "num_threads";
19136 break;
19137 case PRAGMA_OMP_CLAUSE_ORDERED:
19138 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19139 c_name = "ordered";
19140 break;
19141 case PRAGMA_OMP_CLAUSE_PRIVATE:
19142 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19143 clauses);
19144 c_name = "private";
19145 break;
19146 case PRAGMA_OMP_CLAUSE_REDUCTION:
19147 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19148 c_name = "reduction";
19149 break;
19150 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19151 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19152 c_name = "schedule";
19153 break;
19154 case PRAGMA_OMP_CLAUSE_SHARED:
19155 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19156 clauses);
19157 c_name = "shared";
19158 break;
19159 default:
19160 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19161 goto saw_error;
19162 }
19163
19164 if (((mask >> c_kind) & 1) == 0)
19165 {
19166 /* Remove the invalid clause(s) from the list to avoid
19167 confusing the rest of the compiler. */
19168 clauses = prev;
19169 error ("%qs is not valid for %qs", c_name, where);
19170 }
19171 }
19172 saw_error:
19173 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19174 return finish_omp_clauses (clauses);
19175 }
19176
19177 /* OpenMP 2.5:
19178 structured-block:
19179 statement
19180
19181 In practice, we're also interested in adding the statement to an
19182 outer node. So it is convenient if we work around the fact that
19183 cp_parser_statement calls add_stmt. */
19184
19185 static unsigned
19186 cp_parser_begin_omp_structured_block (cp_parser *parser)
19187 {
19188 unsigned save = parser->in_statement;
19189
19190 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19191 This preserves the "not within loop or switch" style error messages
19192 for nonsense cases like
19193 void foo() {
19194 #pragma omp single
19195 break;
19196 }
19197 */
19198 if (parser->in_statement)
19199 parser->in_statement = IN_OMP_BLOCK;
19200
19201 return save;
19202 }
19203
19204 static void
19205 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19206 {
19207 parser->in_statement = save;
19208 }
19209
19210 static tree
19211 cp_parser_omp_structured_block (cp_parser *parser)
19212 {
19213 tree stmt = begin_omp_structured_block ();
19214 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19215
19216 cp_parser_statement (parser, NULL_TREE, false, NULL);
19217
19218 cp_parser_end_omp_structured_block (parser, save);
19219 return finish_omp_structured_block (stmt);
19220 }
19221
19222 /* OpenMP 2.5:
19223 # pragma omp atomic new-line
19224 expression-stmt
19225
19226 expression-stmt:
19227 x binop= expr | x++ | ++x | x-- | --x
19228 binop:
19229 +, *, -, /, &, ^, |, <<, >>
19230
19231 where x is an lvalue expression with scalar type. */
19232
19233 static void
19234 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19235 {
19236 tree lhs, rhs;
19237 enum tree_code code;
19238
19239 cp_parser_require_pragma_eol (parser, pragma_tok);
19240
19241 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19242 /*cast_p=*/false);
19243 switch (TREE_CODE (lhs))
19244 {
19245 case ERROR_MARK:
19246 goto saw_error;
19247
19248 case PREINCREMENT_EXPR:
19249 case POSTINCREMENT_EXPR:
19250 lhs = TREE_OPERAND (lhs, 0);
19251 code = PLUS_EXPR;
19252 rhs = integer_one_node;
19253 break;
19254
19255 case PREDECREMENT_EXPR:
19256 case POSTDECREMENT_EXPR:
19257 lhs = TREE_OPERAND (lhs, 0);
19258 code = MINUS_EXPR;
19259 rhs = integer_one_node;
19260 break;
19261
19262 default:
19263 switch (cp_lexer_peek_token (parser->lexer)->type)
19264 {
19265 case CPP_MULT_EQ:
19266 code = MULT_EXPR;
19267 break;
19268 case CPP_DIV_EQ:
19269 code = TRUNC_DIV_EXPR;
19270 break;
19271 case CPP_PLUS_EQ:
19272 code = PLUS_EXPR;
19273 break;
19274 case CPP_MINUS_EQ:
19275 code = MINUS_EXPR;
19276 break;
19277 case CPP_LSHIFT_EQ:
19278 code = LSHIFT_EXPR;
19279 break;
19280 case CPP_RSHIFT_EQ:
19281 code = RSHIFT_EXPR;
19282 break;
19283 case CPP_AND_EQ:
19284 code = BIT_AND_EXPR;
19285 break;
19286 case CPP_OR_EQ:
19287 code = BIT_IOR_EXPR;
19288 break;
19289 case CPP_XOR_EQ:
19290 code = BIT_XOR_EXPR;
19291 break;
19292 default:
19293 cp_parser_error (parser,
19294 "invalid operator for %<#pragma omp atomic%>");
19295 goto saw_error;
19296 }
19297 cp_lexer_consume_token (parser->lexer);
19298
19299 rhs = cp_parser_expression (parser, false);
19300 if (rhs == error_mark_node)
19301 goto saw_error;
19302 break;
19303 }
19304 finish_omp_atomic (code, lhs, rhs);
19305 cp_parser_consume_semicolon_at_end_of_statement (parser);
19306 return;
19307
19308 saw_error:
19309 cp_parser_skip_to_end_of_block_or_statement (parser);
19310 }
19311
19312
19313 /* OpenMP 2.5:
19314 # pragma omp barrier new-line */
19315
19316 static void
19317 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19318 {
19319 cp_parser_require_pragma_eol (parser, pragma_tok);
19320 finish_omp_barrier ();
19321 }
19322
19323 /* OpenMP 2.5:
19324 # pragma omp critical [(name)] new-line
19325 structured-block */
19326
19327 static tree
19328 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19329 {
19330 tree stmt, name = NULL;
19331
19332 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19333 {
19334 cp_lexer_consume_token (parser->lexer);
19335
19336 name = cp_parser_identifier (parser);
19337
19338 if (name == error_mark_node
19339 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19340 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19341 /*or_comma=*/false,
19342 /*consume_paren=*/true);
19343 if (name == error_mark_node)
19344 name = NULL;
19345 }
19346 cp_parser_require_pragma_eol (parser, pragma_tok);
19347
19348 stmt = cp_parser_omp_structured_block (parser);
19349 return c_finish_omp_critical (stmt, name);
19350 }
19351
19352 /* OpenMP 2.5:
19353 # pragma omp flush flush-vars[opt] new-line
19354
19355 flush-vars:
19356 ( variable-list ) */
19357
19358 static void
19359 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19360 {
19361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19362 (void) cp_parser_omp_var_list (parser, 0, NULL);
19363 cp_parser_require_pragma_eol (parser, pragma_tok);
19364
19365 finish_omp_flush ();
19366 }
19367
19368 /* Parse the restricted form of the for statment allowed by OpenMP. */
19369
19370 static tree
19371 cp_parser_omp_for_loop (cp_parser *parser)
19372 {
19373 tree init, cond, incr, body, decl, pre_body;
19374 location_t loc;
19375
19376 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19377 {
19378 cp_parser_error (parser, "for statement expected");
19379 return NULL;
19380 }
19381 loc = cp_lexer_consume_token (parser->lexer)->location;
19382 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19383 return NULL;
19384
19385 init = decl = NULL;
19386 pre_body = push_stmt_list ();
19387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19388 {
19389 cp_decl_specifier_seq type_specifiers;
19390
19391 /* First, try to parse as an initialized declaration. See
19392 cp_parser_condition, from whence the bulk of this is copied. */
19393
19394 cp_parser_parse_tentatively (parser);
19395 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19396 &type_specifiers);
19397 if (!cp_parser_error_occurred (parser))
19398 {
19399 tree asm_specification, attributes;
19400 cp_declarator *declarator;
19401
19402 declarator = cp_parser_declarator (parser,
19403 CP_PARSER_DECLARATOR_NAMED,
19404 /*ctor_dtor_or_conv_p=*/NULL,
19405 /*parenthesized_p=*/NULL,
19406 /*member_p=*/false);
19407 attributes = cp_parser_attributes_opt (parser);
19408 asm_specification = cp_parser_asm_specification_opt (parser);
19409
19410 cp_parser_require (parser, CPP_EQ, "`='");
19411 if (cp_parser_parse_definitely (parser))
19412 {
19413 tree pushed_scope;
19414
19415 decl = start_decl (declarator, &type_specifiers,
19416 /*initialized_p=*/false, attributes,
19417 /*prefix_attributes=*/NULL_TREE,
19418 &pushed_scope);
19419
19420 init = cp_parser_assignment_expression (parser, false);
19421
19422 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19423 asm_specification, LOOKUP_ONLYCONVERTING);
19424
19425 if (pushed_scope)
19426 pop_scope (pushed_scope);
19427 }
19428 }
19429 else
19430 cp_parser_abort_tentative_parse (parser);
19431
19432 /* If parsing as an initialized declaration failed, try again as
19433 a simple expression. */
19434 if (decl == NULL)
19435 init = cp_parser_expression (parser, false);
19436 }
19437 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19438 pre_body = pop_stmt_list (pre_body);
19439
19440 cond = NULL;
19441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19442 cond = cp_parser_condition (parser);
19443 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19444
19445 incr = NULL;
19446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19447 incr = cp_parser_expression (parser, false);
19448
19449 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19451 /*or_comma=*/false,
19452 /*consume_paren=*/true);
19453
19454 /* Note that we saved the original contents of this flag when we entered
19455 the structured block, and so we don't need to re-save it here. */
19456 parser->in_statement = IN_OMP_FOR;
19457
19458 /* Note that the grammar doesn't call for a structured block here,
19459 though the loop as a whole is a structured block. */
19460 body = push_stmt_list ();
19461 cp_parser_statement (parser, NULL_TREE, false, NULL);
19462 body = pop_stmt_list (body);
19463
19464 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19465 }
19466
19467 /* OpenMP 2.5:
19468 #pragma omp for for-clause[optseq] new-line
19469 for-loop */
19470
19471 #define OMP_FOR_CLAUSE_MASK \
19472 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19473 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19474 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19475 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19476 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19477 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19478 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19479
19480 static tree
19481 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19482 {
19483 tree clauses, sb, ret;
19484 unsigned int save;
19485
19486 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19487 "#pragma omp for", pragma_tok);
19488
19489 sb = begin_omp_structured_block ();
19490 save = cp_parser_begin_omp_structured_block (parser);
19491
19492 ret = cp_parser_omp_for_loop (parser);
19493 if (ret)
19494 OMP_FOR_CLAUSES (ret) = clauses;
19495
19496 cp_parser_end_omp_structured_block (parser, save);
19497 add_stmt (finish_omp_structured_block (sb));
19498
19499 return ret;
19500 }
19501
19502 /* OpenMP 2.5:
19503 # pragma omp master new-line
19504 structured-block */
19505
19506 static tree
19507 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19508 {
19509 cp_parser_require_pragma_eol (parser, pragma_tok);
19510 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19511 }
19512
19513 /* OpenMP 2.5:
19514 # pragma omp ordered new-line
19515 structured-block */
19516
19517 static tree
19518 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19519 {
19520 cp_parser_require_pragma_eol (parser, pragma_tok);
19521 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19522 }
19523
19524 /* OpenMP 2.5:
19525
19526 section-scope:
19527 { section-sequence }
19528
19529 section-sequence:
19530 section-directive[opt] structured-block
19531 section-sequence section-directive structured-block */
19532
19533 static tree
19534 cp_parser_omp_sections_scope (cp_parser *parser)
19535 {
19536 tree stmt, substmt;
19537 bool error_suppress = false;
19538 cp_token *tok;
19539
19540 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19541 return NULL_TREE;
19542
19543 stmt = push_stmt_list ();
19544
19545 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19546 {
19547 unsigned save;
19548
19549 substmt = begin_omp_structured_block ();
19550 save = cp_parser_begin_omp_structured_block (parser);
19551
19552 while (1)
19553 {
19554 cp_parser_statement (parser, NULL_TREE, false, NULL);
19555
19556 tok = cp_lexer_peek_token (parser->lexer);
19557 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19558 break;
19559 if (tok->type == CPP_CLOSE_BRACE)
19560 break;
19561 if (tok->type == CPP_EOF)
19562 break;
19563 }
19564
19565 cp_parser_end_omp_structured_block (parser, save);
19566 substmt = finish_omp_structured_block (substmt);
19567 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19568 add_stmt (substmt);
19569 }
19570
19571 while (1)
19572 {
19573 tok = cp_lexer_peek_token (parser->lexer);
19574 if (tok->type == CPP_CLOSE_BRACE)
19575 break;
19576 if (tok->type == CPP_EOF)
19577 break;
19578
19579 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19580 {
19581 cp_lexer_consume_token (parser->lexer);
19582 cp_parser_require_pragma_eol (parser, tok);
19583 error_suppress = false;
19584 }
19585 else if (!error_suppress)
19586 {
19587 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19588 error_suppress = true;
19589 }
19590
19591 substmt = cp_parser_omp_structured_block (parser);
19592 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19593 add_stmt (substmt);
19594 }
19595 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19596
19597 substmt = pop_stmt_list (stmt);
19598
19599 stmt = make_node (OMP_SECTIONS);
19600 TREE_TYPE (stmt) = void_type_node;
19601 OMP_SECTIONS_BODY (stmt) = substmt;
19602
19603 add_stmt (stmt);
19604 return stmt;
19605 }
19606
19607 /* OpenMP 2.5:
19608 # pragma omp sections sections-clause[optseq] newline
19609 sections-scope */
19610
19611 #define OMP_SECTIONS_CLAUSE_MASK \
19612 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19613 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19614 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19615 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19616 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19617
19618 static tree
19619 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19620 {
19621 tree clauses, ret;
19622
19623 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19624 "#pragma omp sections", pragma_tok);
19625
19626 ret = cp_parser_omp_sections_scope (parser);
19627 if (ret)
19628 OMP_SECTIONS_CLAUSES (ret) = clauses;
19629
19630 return ret;
19631 }
19632
19633 /* OpenMP 2.5:
19634 # pragma parallel parallel-clause new-line
19635 # pragma parallel for parallel-for-clause new-line
19636 # pragma parallel sections parallel-sections-clause new-line */
19637
19638 #define OMP_PARALLEL_CLAUSE_MASK \
19639 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19640 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19641 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19642 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19643 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19644 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19645 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19646 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19647
19648 static tree
19649 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19650 {
19651 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19652 const char *p_name = "#pragma omp parallel";
19653 tree stmt, clauses, par_clause, ws_clause, block;
19654 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19655 unsigned int save;
19656
19657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19658 {
19659 cp_lexer_consume_token (parser->lexer);
19660 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19661 p_name = "#pragma omp parallel for";
19662 mask |= OMP_FOR_CLAUSE_MASK;
19663 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19664 }
19665 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19666 {
19667 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19668 const char *p = IDENTIFIER_POINTER (id);
19669 if (strcmp (p, "sections") == 0)
19670 {
19671 cp_lexer_consume_token (parser->lexer);
19672 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19673 p_name = "#pragma omp parallel sections";
19674 mask |= OMP_SECTIONS_CLAUSE_MASK;
19675 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19676 }
19677 }
19678
19679 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19680 block = begin_omp_parallel ();
19681 save = cp_parser_begin_omp_structured_block (parser);
19682
19683 switch (p_kind)
19684 {
19685 case PRAGMA_OMP_PARALLEL:
19686 cp_parser_already_scoped_statement (parser);
19687 par_clause = clauses;
19688 break;
19689
19690 case PRAGMA_OMP_PARALLEL_FOR:
19691 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19692 stmt = cp_parser_omp_for_loop (parser);
19693 if (stmt)
19694 OMP_FOR_CLAUSES (stmt) = ws_clause;
19695 break;
19696
19697 case PRAGMA_OMP_PARALLEL_SECTIONS:
19698 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19699 stmt = cp_parser_omp_sections_scope (parser);
19700 if (stmt)
19701 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19702 break;
19703
19704 default:
19705 gcc_unreachable ();
19706 }
19707
19708 cp_parser_end_omp_structured_block (parser, save);
19709 stmt = finish_omp_parallel (par_clause, block);
19710 if (p_kind != PRAGMA_OMP_PARALLEL)
19711 OMP_PARALLEL_COMBINED (stmt) = 1;
19712 return stmt;
19713 }
19714
19715 /* OpenMP 2.5:
19716 # pragma omp single single-clause[optseq] new-line
19717 structured-block */
19718
19719 #define OMP_SINGLE_CLAUSE_MASK \
19720 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19721 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19722 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19723 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19724
19725 static tree
19726 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19727 {
19728 tree stmt = make_node (OMP_SINGLE);
19729 TREE_TYPE (stmt) = void_type_node;
19730
19731 OMP_SINGLE_CLAUSES (stmt)
19732 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19733 "#pragma omp single", pragma_tok);
19734 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19735
19736 return add_stmt (stmt);
19737 }
19738
19739 /* OpenMP 2.5:
19740 # pragma omp threadprivate (variable-list) */
19741
19742 static void
19743 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19744 {
19745 tree vars;
19746
19747 vars = cp_parser_omp_var_list (parser, 0, NULL);
19748 cp_parser_require_pragma_eol (parser, pragma_tok);
19749
19750 finish_omp_threadprivate (vars);
19751 }
19752
19753 /* Main entry point to OpenMP statement pragmas. */
19754
19755 static void
19756 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19757 {
19758 tree stmt;
19759
19760 switch (pragma_tok->pragma_kind)
19761 {
19762 case PRAGMA_OMP_ATOMIC:
19763 cp_parser_omp_atomic (parser, pragma_tok);
19764 return;
19765 case PRAGMA_OMP_CRITICAL:
19766 stmt = cp_parser_omp_critical (parser, pragma_tok);
19767 break;
19768 case PRAGMA_OMP_FOR:
19769 stmt = cp_parser_omp_for (parser, pragma_tok);
19770 break;
19771 case PRAGMA_OMP_MASTER:
19772 stmt = cp_parser_omp_master (parser, pragma_tok);
19773 break;
19774 case PRAGMA_OMP_ORDERED:
19775 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19776 break;
19777 case PRAGMA_OMP_PARALLEL:
19778 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19779 break;
19780 case PRAGMA_OMP_SECTIONS:
19781 stmt = cp_parser_omp_sections (parser, pragma_tok);
19782 break;
19783 case PRAGMA_OMP_SINGLE:
19784 stmt = cp_parser_omp_single (parser, pragma_tok);
19785 break;
19786 default:
19787 gcc_unreachable ();
19788 }
19789
19790 if (stmt)
19791 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19792 }
19793 \f
19794 /* The parser. */
19795
19796 static GTY (()) cp_parser *the_parser;
19797
19798 \f
19799 /* Special handling for the first token or line in the file. The first
19800 thing in the file might be #pragma GCC pch_preprocess, which loads a
19801 PCH file, which is a GC collection point. So we need to handle this
19802 first pragma without benefit of an existing lexer structure.
19803
19804 Always returns one token to the caller in *FIRST_TOKEN. This is
19805 either the true first token of the file, or the first token after
19806 the initial pragma. */
19807
19808 static void
19809 cp_parser_initial_pragma (cp_token *first_token)
19810 {
19811 tree name = NULL;
19812
19813 cp_lexer_get_preprocessor_token (NULL, first_token);
19814 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19815 return;
19816
19817 cp_lexer_get_preprocessor_token (NULL, first_token);
19818 if (first_token->type == CPP_STRING)
19819 {
19820 name = first_token->u.value;
19821
19822 cp_lexer_get_preprocessor_token (NULL, first_token);
19823 if (first_token->type != CPP_PRAGMA_EOL)
19824 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19825 }
19826 else
19827 error ("expected string literal");
19828
19829 /* Skip to the end of the pragma. */
19830 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19831 cp_lexer_get_preprocessor_token (NULL, first_token);
19832
19833 /* Now actually load the PCH file. */
19834 if (name)
19835 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19836
19837 /* Read one more token to return to our caller. We have to do this
19838 after reading the PCH file in, since its pointers have to be
19839 live. */
19840 cp_lexer_get_preprocessor_token (NULL, first_token);
19841 }
19842
19843 /* Normal parsing of a pragma token. Here we can (and must) use the
19844 regular lexer. */
19845
19846 static bool
19847 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19848 {
19849 cp_token *pragma_tok;
19850 unsigned int id;
19851
19852 pragma_tok = cp_lexer_consume_token (parser->lexer);
19853 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19854 parser->lexer->in_pragma = true;
19855
19856 id = pragma_tok->pragma_kind;
19857 switch (id)
19858 {
19859 case PRAGMA_GCC_PCH_PREPROCESS:
19860 error ("%<#pragma GCC pch_preprocess%> must be first");
19861 break;
19862
19863 case PRAGMA_OMP_BARRIER:
19864 switch (context)
19865 {
19866 case pragma_compound:
19867 cp_parser_omp_barrier (parser, pragma_tok);
19868 return false;
19869 case pragma_stmt:
19870 error ("%<#pragma omp barrier%> may only be "
19871 "used in compound statements");
19872 break;
19873 default:
19874 goto bad_stmt;
19875 }
19876 break;
19877
19878 case PRAGMA_OMP_FLUSH:
19879 switch (context)
19880 {
19881 case pragma_compound:
19882 cp_parser_omp_flush (parser, pragma_tok);
19883 return false;
19884 case pragma_stmt:
19885 error ("%<#pragma omp flush%> may only be "
19886 "used in compound statements");
19887 break;
19888 default:
19889 goto bad_stmt;
19890 }
19891 break;
19892
19893 case PRAGMA_OMP_THREADPRIVATE:
19894 cp_parser_omp_threadprivate (parser, pragma_tok);
19895 return false;
19896
19897 case PRAGMA_OMP_ATOMIC:
19898 case PRAGMA_OMP_CRITICAL:
19899 case PRAGMA_OMP_FOR:
19900 case PRAGMA_OMP_MASTER:
19901 case PRAGMA_OMP_ORDERED:
19902 case PRAGMA_OMP_PARALLEL:
19903 case PRAGMA_OMP_SECTIONS:
19904 case PRAGMA_OMP_SINGLE:
19905 if (context == pragma_external)
19906 goto bad_stmt;
19907 cp_parser_omp_construct (parser, pragma_tok);
19908 return true;
19909
19910 case PRAGMA_OMP_SECTION:
19911 error ("%<#pragma omp section%> may only be used in "
19912 "%<#pragma omp sections%> construct");
19913 break;
19914
19915 default:
19916 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19917 c_invoke_pragma_handler (id);
19918 break;
19919
19920 bad_stmt:
19921 cp_parser_error (parser, "expected declaration specifiers");
19922 break;
19923 }
19924
19925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19926 return false;
19927 }
19928
19929 /* The interface the pragma parsers have to the lexer. */
19930
19931 enum cpp_ttype
19932 pragma_lex (tree *value)
19933 {
19934 cp_token *tok;
19935 enum cpp_ttype ret;
19936
19937 tok = cp_lexer_peek_token (the_parser->lexer);
19938
19939 ret = tok->type;
19940 *value = tok->u.value;
19941
19942 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19943 ret = CPP_EOF;
19944 else if (ret == CPP_STRING)
19945 *value = cp_parser_string_literal (the_parser, false, false);
19946 else
19947 {
19948 cp_lexer_consume_token (the_parser->lexer);
19949 if (ret == CPP_KEYWORD)
19950 ret = CPP_NAME;
19951 }
19952
19953 return ret;
19954 }
19955
19956 \f
19957 /* External interface. */
19958
19959 /* Parse one entire translation unit. */
19960
19961 void
19962 c_parse_file (void)
19963 {
19964 bool error_occurred;
19965 static bool already_called = false;
19966
19967 if (already_called)
19968 {
19969 sorry ("inter-module optimizations not implemented for C++");
19970 return;
19971 }
19972 already_called = true;
19973
19974 the_parser = cp_parser_new ();
19975 push_deferring_access_checks (flag_access_control
19976 ? dk_no_deferred : dk_no_check);
19977 error_occurred = cp_parser_translation_unit (the_parser);
19978 the_parser = NULL;
19979 }
19980
19981 #include "gt-cp-parser.h"
This page took 0.929405 seconds and 6 git commands to generate.