]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
error.c (dump_expr): Handle dependent names that designate types.
[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. */
1404 bool greater_than_is_operator_p;
1405
1406 /* TRUE if default arguments are allowed within a parameter list
1407 that starts at this point. FALSE if only a gnu extension makes
1408 them permissible. */
1409 bool default_arg_ok_p;
1410
1411 /* TRUE if we are parsing an integral constant-expression. See
1412 [expr.const] for a precise definition. */
1413 bool integral_constant_expression_p;
1414
1415 /* TRUE if we are parsing an integral constant-expression -- but a
1416 non-constant expression should be permitted as well. This flag
1417 is used when parsing an array bound so that GNU variable-length
1418 arrays are tolerated. */
1419 bool allow_non_integral_constant_expression_p;
1420
1421 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1422 been seen that makes the expression non-constant. */
1423 bool non_integral_constant_expression_p;
1424
1425 /* TRUE if local variable names and `this' are forbidden in the
1426 current context. */
1427 bool local_variables_forbidden_p;
1428
1429 /* TRUE if the declaration we are parsing is part of a
1430 linkage-specification of the form `extern string-literal
1431 declaration'. */
1432 bool in_unbraced_linkage_specification_p;
1433
1434 /* TRUE if we are presently parsing a declarator, after the
1435 direct-declarator. */
1436 bool in_declarator_p;
1437
1438 /* TRUE if we are presently parsing a template-argument-list. */
1439 bool in_template_argument_list_p;
1440
1441 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1442 to IN_OMP_BLOCK if parsing OpenMP structured block and
1443 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1444 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1445 iteration-statement, OpenMP block or loop within that switch. */
1446 #define IN_SWITCH_STMT 1
1447 #define IN_ITERATION_STMT 2
1448 #define IN_OMP_BLOCK 4
1449 #define IN_OMP_FOR 8
1450 #define IN_IF_STMT 16
1451 unsigned char in_statement;
1452
1453 /* TRUE if we are presently parsing the body of a switch statement.
1454 Note that this doesn't quite overlap with in_statement above.
1455 The difference relates to giving the right sets of error messages:
1456 "case not in switch" vs "break statement used with OpenMP...". */
1457 bool in_switch_statement_p;
1458
1459 /* TRUE if we are parsing a type-id in an expression context. In
1460 such a situation, both "type (expr)" and "type (type)" are valid
1461 alternatives. */
1462 bool in_type_id_in_expr_p;
1463
1464 /* TRUE if we are currently in a header file where declarations are
1465 implicitly extern "C". */
1466 bool implicit_extern_c;
1467
1468 /* TRUE if strings in expressions should be translated to the execution
1469 character set. */
1470 bool translate_strings_p;
1471
1472 /* TRUE if we are presently parsing the body of a function, but not
1473 a local class. */
1474 bool in_function_body;
1475
1476 /* If non-NULL, then we are parsing a construct where new type
1477 definitions are not permitted. The string stored here will be
1478 issued as an error message if a type is defined. */
1479 const char *type_definition_forbidden_message;
1480
1481 /* A list of lists. The outer list is a stack, used for member
1482 functions of local classes. At each level there are two sub-list,
1483 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1484 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1485 TREE_VALUE's. The functions are chained in reverse declaration
1486 order.
1487
1488 The TREE_PURPOSE sublist contains those functions with default
1489 arguments that need post processing, and the TREE_VALUE sublist
1490 contains those functions with definitions that need post
1491 processing.
1492
1493 These lists can only be processed once the outermost class being
1494 defined is complete. */
1495 tree unparsed_functions_queues;
1496
1497 /* The number of classes whose definitions are currently in
1498 progress. */
1499 unsigned num_classes_being_defined;
1500
1501 /* The number of template parameter lists that apply directly to the
1502 current declaration. */
1503 unsigned num_template_parameter_lists;
1504 } cp_parser;
1505
1506 /* Prototypes. */
1507
1508 /* Constructors and destructors. */
1509
1510 static cp_parser *cp_parser_new
1511 (void);
1512
1513 /* Routines to parse various constructs.
1514
1515 Those that return `tree' will return the error_mark_node (rather
1516 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517 Sometimes, they will return an ordinary node if error-recovery was
1518 attempted, even though a parse error occurred. So, to check
1519 whether or not a parse error occurred, you should always use
1520 cp_parser_error_occurred. If the construct is optional (indicated
1521 either by an `_opt' in the name of the function that does the
1522 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523 the construct is not present. */
1524
1525 /* Lexical conventions [gram.lex] */
1526
1527 static tree cp_parser_identifier
1528 (cp_parser *);
1529 static tree cp_parser_string_literal
1530 (cp_parser *, bool, bool);
1531
1532 /* Basic concepts [gram.basic] */
1533
1534 static bool cp_parser_translation_unit
1535 (cp_parser *);
1536
1537 /* Expressions [gram.expr] */
1538
1539 static tree cp_parser_primary_expression
1540 (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542 (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544 (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546 (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548 (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_class_or_namespace_name
1550 (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552 (cp_parser *, bool, bool);
1553 static tree cp_parser_postfix_open_square_expression
1554 (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1557 static tree cp_parser_parenthesized_expression_list
1558 (cp_parser *, bool, bool, bool, bool *);
1559 static void cp_parser_pseudo_destructor_name
1560 (cp_parser *, tree *, tree *);
1561 static tree cp_parser_unary_expression
1562 (cp_parser *, bool, bool);
1563 static enum tree_code cp_parser_unary_operator
1564 (cp_token *);
1565 static tree cp_parser_new_expression
1566 (cp_parser *);
1567 static tree cp_parser_new_placement
1568 (cp_parser *);
1569 static tree cp_parser_new_type_id
1570 (cp_parser *, tree *);
1571 static cp_declarator *cp_parser_new_declarator_opt
1572 (cp_parser *);
1573 static cp_declarator *cp_parser_direct_new_declarator
1574 (cp_parser *);
1575 static tree cp_parser_new_initializer
1576 (cp_parser *);
1577 static tree cp_parser_delete_expression
1578 (cp_parser *);
1579 static tree cp_parser_cast_expression
1580 (cp_parser *, bool, bool);
1581 static tree cp_parser_binary_expression
1582 (cp_parser *, bool);
1583 static tree cp_parser_question_colon_clause
1584 (cp_parser *, tree);
1585 static tree cp_parser_assignment_expression
1586 (cp_parser *, bool);
1587 static enum tree_code cp_parser_assignment_operator_opt
1588 (cp_parser *);
1589 static tree cp_parser_expression
1590 (cp_parser *, bool);
1591 static tree cp_parser_constant_expression
1592 (cp_parser *, bool, bool *);
1593 static tree cp_parser_builtin_offsetof
1594 (cp_parser *);
1595
1596 /* Statements [gram.stmt.stmt] */
1597
1598 static void cp_parser_statement
1599 (cp_parser *, tree, bool, bool *);
1600 static void cp_parser_label_for_labeled_statement
1601 (cp_parser *);
1602 static tree cp_parser_expression_statement
1603 (cp_parser *, tree);
1604 static tree cp_parser_compound_statement
1605 (cp_parser *, tree, bool);
1606 static void cp_parser_statement_seq_opt
1607 (cp_parser *, tree);
1608 static tree cp_parser_selection_statement
1609 (cp_parser *, bool *);
1610 static tree cp_parser_condition
1611 (cp_parser *);
1612 static tree cp_parser_iteration_statement
1613 (cp_parser *);
1614 static void cp_parser_for_init_statement
1615 (cp_parser *);
1616 static tree cp_parser_jump_statement
1617 (cp_parser *);
1618 static void cp_parser_declaration_statement
1619 (cp_parser *);
1620
1621 static tree cp_parser_implicitly_scoped_statement
1622 (cp_parser *, bool *);
1623 static void cp_parser_already_scoped_statement
1624 (cp_parser *);
1625
1626 /* Declarations [gram.dcl.dcl] */
1627
1628 static void cp_parser_declaration_seq_opt
1629 (cp_parser *);
1630 static void cp_parser_declaration
1631 (cp_parser *);
1632 static void cp_parser_block_declaration
1633 (cp_parser *, bool);
1634 static void cp_parser_simple_declaration
1635 (cp_parser *, bool);
1636 static void cp_parser_decl_specifier_seq
1637 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1638 static tree cp_parser_storage_class_specifier_opt
1639 (cp_parser *);
1640 static tree cp_parser_function_specifier_opt
1641 (cp_parser *, cp_decl_specifier_seq *);
1642 static tree cp_parser_type_specifier
1643 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1644 int *, bool *);
1645 static tree cp_parser_simple_type_specifier
1646 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1647 static tree cp_parser_type_name
1648 (cp_parser *);
1649 static tree cp_parser_elaborated_type_specifier
1650 (cp_parser *, bool, bool);
1651 static tree cp_parser_enum_specifier
1652 (cp_parser *);
1653 static void cp_parser_enumerator_list
1654 (cp_parser *, tree);
1655 static void cp_parser_enumerator_definition
1656 (cp_parser *, tree);
1657 static tree cp_parser_namespace_name
1658 (cp_parser *);
1659 static void cp_parser_namespace_definition
1660 (cp_parser *);
1661 static void cp_parser_namespace_body
1662 (cp_parser *);
1663 static tree cp_parser_qualified_namespace_specifier
1664 (cp_parser *);
1665 static void cp_parser_namespace_alias_definition
1666 (cp_parser *);
1667 static bool cp_parser_using_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_using_directive
1670 (cp_parser *);
1671 static void cp_parser_asm_definition
1672 (cp_parser *);
1673 static void cp_parser_linkage_specification
1674 (cp_parser *);
1675 static void cp_parser_static_assert
1676 (cp_parser *, bool);
1677
1678 /* Declarators [gram.dcl.decl] */
1679
1680 static tree cp_parser_init_declarator
1681 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1682 static cp_declarator *cp_parser_declarator
1683 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1684 static cp_declarator *cp_parser_direct_declarator
1685 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1686 static enum tree_code cp_parser_ptr_operator
1687 (cp_parser *, tree *, cp_cv_quals *);
1688 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1689 (cp_parser *);
1690 static tree cp_parser_declarator_id
1691 (cp_parser *, bool);
1692 static tree cp_parser_type_id
1693 (cp_parser *);
1694 static void cp_parser_type_specifier_seq
1695 (cp_parser *, bool, cp_decl_specifier_seq *);
1696 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1697 (cp_parser *);
1698 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1699 (cp_parser *, bool *);
1700 static cp_parameter_declarator *cp_parser_parameter_declaration
1701 (cp_parser *, bool, bool *);
1702 static void cp_parser_function_body
1703 (cp_parser *);
1704 static tree cp_parser_initializer
1705 (cp_parser *, bool *, bool *);
1706 static tree cp_parser_initializer_clause
1707 (cp_parser *, bool *);
1708 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1709 (cp_parser *, bool *);
1710
1711 static bool cp_parser_ctor_initializer_opt_and_function_body
1712 (cp_parser *);
1713
1714 /* Classes [gram.class] */
1715
1716 static tree cp_parser_class_name
1717 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1718 static tree cp_parser_class_specifier
1719 (cp_parser *);
1720 static tree cp_parser_class_head
1721 (cp_parser *, bool *, tree *, tree *);
1722 static enum tag_types cp_parser_class_key
1723 (cp_parser *);
1724 static void cp_parser_member_specification_opt
1725 (cp_parser *);
1726 static void cp_parser_member_declaration
1727 (cp_parser *);
1728 static tree cp_parser_pure_specifier
1729 (cp_parser *);
1730 static tree cp_parser_constant_initializer
1731 (cp_parser *);
1732
1733 /* Derived classes [gram.class.derived] */
1734
1735 static tree cp_parser_base_clause
1736 (cp_parser *);
1737 static tree cp_parser_base_specifier
1738 (cp_parser *);
1739
1740 /* Special member functions [gram.special] */
1741
1742 static tree cp_parser_conversion_function_id
1743 (cp_parser *);
1744 static tree cp_parser_conversion_type_id
1745 (cp_parser *);
1746 static cp_declarator *cp_parser_conversion_declarator_opt
1747 (cp_parser *);
1748 static bool cp_parser_ctor_initializer_opt
1749 (cp_parser *);
1750 static void cp_parser_mem_initializer_list
1751 (cp_parser *);
1752 static tree cp_parser_mem_initializer
1753 (cp_parser *);
1754 static tree cp_parser_mem_initializer_id
1755 (cp_parser *);
1756
1757 /* Overloading [gram.over] */
1758
1759 static tree cp_parser_operator_function_id
1760 (cp_parser *);
1761 static tree cp_parser_operator
1762 (cp_parser *);
1763
1764 /* Templates [gram.temp] */
1765
1766 static void cp_parser_template_declaration
1767 (cp_parser *, bool);
1768 static tree cp_parser_template_parameter_list
1769 (cp_parser *);
1770 static tree cp_parser_template_parameter
1771 (cp_parser *, bool *, bool *);
1772 static tree cp_parser_type_parameter
1773 (cp_parser *, bool *);
1774 static tree cp_parser_template_id
1775 (cp_parser *, bool, bool, bool);
1776 static tree cp_parser_template_name
1777 (cp_parser *, bool, bool, bool, bool *);
1778 static tree cp_parser_template_argument_list
1779 (cp_parser *);
1780 static tree cp_parser_template_argument
1781 (cp_parser *);
1782 static void cp_parser_explicit_instantiation
1783 (cp_parser *);
1784 static void cp_parser_explicit_specialization
1785 (cp_parser *);
1786
1787 /* Exception handling [gram.exception] */
1788
1789 static tree cp_parser_try_block
1790 (cp_parser *);
1791 static bool cp_parser_function_try_block
1792 (cp_parser *);
1793 static void cp_parser_handler_seq
1794 (cp_parser *);
1795 static void cp_parser_handler
1796 (cp_parser *);
1797 static tree cp_parser_exception_declaration
1798 (cp_parser *);
1799 static tree cp_parser_throw_expression
1800 (cp_parser *);
1801 static tree cp_parser_exception_specification_opt
1802 (cp_parser *);
1803 static tree cp_parser_type_id_list
1804 (cp_parser *);
1805
1806 /* GNU Extensions */
1807
1808 static tree cp_parser_asm_specification_opt
1809 (cp_parser *);
1810 static tree cp_parser_asm_operand_list
1811 (cp_parser *);
1812 static tree cp_parser_asm_clobber_list
1813 (cp_parser *);
1814 static tree cp_parser_attributes_opt
1815 (cp_parser *);
1816 static tree cp_parser_attribute_list
1817 (cp_parser *);
1818 static bool cp_parser_extension_opt
1819 (cp_parser *, int *);
1820 static void cp_parser_label_declaration
1821 (cp_parser *);
1822
1823 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1824 static bool cp_parser_pragma
1825 (cp_parser *, enum pragma_context);
1826
1827 /* Objective-C++ Productions */
1828
1829 static tree cp_parser_objc_message_receiver
1830 (cp_parser *);
1831 static tree cp_parser_objc_message_args
1832 (cp_parser *);
1833 static tree cp_parser_objc_message_expression
1834 (cp_parser *);
1835 static tree cp_parser_objc_encode_expression
1836 (cp_parser *);
1837 static tree cp_parser_objc_defs_expression
1838 (cp_parser *);
1839 static tree cp_parser_objc_protocol_expression
1840 (cp_parser *);
1841 static tree cp_parser_objc_selector_expression
1842 (cp_parser *);
1843 static tree cp_parser_objc_expression
1844 (cp_parser *);
1845 static bool cp_parser_objc_selector_p
1846 (enum cpp_ttype);
1847 static tree cp_parser_objc_selector
1848 (cp_parser *);
1849 static tree cp_parser_objc_protocol_refs_opt
1850 (cp_parser *);
1851 static void cp_parser_objc_declaration
1852 (cp_parser *);
1853 static tree cp_parser_objc_statement
1854 (cp_parser *);
1855
1856 /* Utility Routines */
1857
1858 static tree cp_parser_lookup_name
1859 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1860 static tree cp_parser_lookup_name_simple
1861 (cp_parser *, tree);
1862 static tree cp_parser_maybe_treat_template_as_class
1863 (tree, bool);
1864 static bool cp_parser_check_declarator_template_parameters
1865 (cp_parser *, cp_declarator *);
1866 static bool cp_parser_check_template_parameters
1867 (cp_parser *, unsigned);
1868 static tree cp_parser_simple_cast_expression
1869 (cp_parser *);
1870 static tree cp_parser_global_scope_opt
1871 (cp_parser *, bool);
1872 static bool cp_parser_constructor_declarator_p
1873 (cp_parser *, bool);
1874 static tree cp_parser_function_definition_from_specifiers_and_declarator
1875 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1876 static tree cp_parser_function_definition_after_declarator
1877 (cp_parser *, bool);
1878 static void cp_parser_template_declaration_after_export
1879 (cp_parser *, bool);
1880 static void cp_parser_perform_template_parameter_access_checks
1881 (VEC (deferred_access_check,gc)*);
1882 static tree cp_parser_single_declaration
1883 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1884 static tree cp_parser_functional_cast
1885 (cp_parser *, tree);
1886 static tree cp_parser_save_member_function_body
1887 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1888 static tree cp_parser_enclosed_template_argument_list
1889 (cp_parser *);
1890 static void cp_parser_save_default_args
1891 (cp_parser *, tree);
1892 static void cp_parser_late_parsing_for_member
1893 (cp_parser *, tree);
1894 static void cp_parser_late_parsing_default_args
1895 (cp_parser *, tree);
1896 static tree cp_parser_sizeof_operand
1897 (cp_parser *, enum rid);
1898 static bool cp_parser_declares_only_class_p
1899 (cp_parser *);
1900 static void cp_parser_set_storage_class
1901 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1902 static void cp_parser_set_decl_spec_type
1903 (cp_decl_specifier_seq *, tree, bool);
1904 static bool cp_parser_friend_p
1905 (const cp_decl_specifier_seq *);
1906 static cp_token *cp_parser_require
1907 (cp_parser *, enum cpp_ttype, const char *);
1908 static cp_token *cp_parser_require_keyword
1909 (cp_parser *, enum rid, const char *);
1910 static bool cp_parser_token_starts_function_definition_p
1911 (cp_token *);
1912 static bool cp_parser_next_token_starts_class_definition_p
1913 (cp_parser *);
1914 static bool cp_parser_next_token_ends_template_argument_p
1915 (cp_parser *);
1916 static bool cp_parser_nth_token_starts_template_argument_list_p
1917 (cp_parser *, size_t);
1918 static enum tag_types cp_parser_token_is_class_key
1919 (cp_token *);
1920 static void cp_parser_check_class_key
1921 (enum tag_types, tree type);
1922 static void cp_parser_check_access_in_redeclaration
1923 (tree type);
1924 static bool cp_parser_optional_template_keyword
1925 (cp_parser *);
1926 static void cp_parser_pre_parsed_nested_name_specifier
1927 (cp_parser *);
1928 static void cp_parser_cache_group
1929 (cp_parser *, enum cpp_ttype, unsigned);
1930 static void cp_parser_parse_tentatively
1931 (cp_parser *);
1932 static void cp_parser_commit_to_tentative_parse
1933 (cp_parser *);
1934 static void cp_parser_abort_tentative_parse
1935 (cp_parser *);
1936 static bool cp_parser_parse_definitely
1937 (cp_parser *);
1938 static inline bool cp_parser_parsing_tentatively
1939 (cp_parser *);
1940 static bool cp_parser_uncommitted_to_tentative_parse_p
1941 (cp_parser *);
1942 static void cp_parser_error
1943 (cp_parser *, const char *);
1944 static void cp_parser_name_lookup_error
1945 (cp_parser *, tree, tree, const char *);
1946 static bool cp_parser_simulate_error
1947 (cp_parser *);
1948 static bool cp_parser_check_type_definition
1949 (cp_parser *);
1950 static void cp_parser_check_for_definition_in_return_type
1951 (cp_declarator *, tree);
1952 static void cp_parser_check_for_invalid_template_id
1953 (cp_parser *, tree);
1954 static bool cp_parser_non_integral_constant_expression
1955 (cp_parser *, const char *);
1956 static void cp_parser_diagnose_invalid_type_name
1957 (cp_parser *, tree, tree);
1958 static bool cp_parser_parse_and_diagnose_invalid_type_name
1959 (cp_parser *);
1960 static int cp_parser_skip_to_closing_parenthesis
1961 (cp_parser *, bool, bool, bool);
1962 static void cp_parser_skip_to_end_of_statement
1963 (cp_parser *);
1964 static void cp_parser_consume_semicolon_at_end_of_statement
1965 (cp_parser *);
1966 static void cp_parser_skip_to_end_of_block_or_statement
1967 (cp_parser *);
1968 static void cp_parser_skip_to_closing_brace
1969 (cp_parser *);
1970 static void cp_parser_skip_to_end_of_template_parameter_list
1971 (cp_parser *);
1972 static void cp_parser_skip_to_pragma_eol
1973 (cp_parser*, cp_token *);
1974 static bool cp_parser_error_occurred
1975 (cp_parser *);
1976 static bool cp_parser_allow_gnu_extensions_p
1977 (cp_parser *);
1978 static bool cp_parser_is_string_literal
1979 (cp_token *);
1980 static bool cp_parser_is_keyword
1981 (cp_token *, enum rid);
1982 static tree cp_parser_make_typename_type
1983 (cp_parser *, tree, tree);
1984
1985 /* Returns nonzero if we are parsing tentatively. */
1986
1987 static inline bool
1988 cp_parser_parsing_tentatively (cp_parser* parser)
1989 {
1990 return parser->context->next != NULL;
1991 }
1992
1993 /* Returns nonzero if TOKEN is a string literal. */
1994
1995 static bool
1996 cp_parser_is_string_literal (cp_token* token)
1997 {
1998 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1999 }
2000
2001 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2002
2003 static bool
2004 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2005 {
2006 return token->keyword == keyword;
2007 }
2008
2009 /* If not parsing tentatively, issue a diagnostic of the form
2010 FILE:LINE: MESSAGE before TOKEN
2011 where TOKEN is the next token in the input stream. MESSAGE
2012 (specified by the caller) is usually of the form "expected
2013 OTHER-TOKEN". */
2014
2015 static void
2016 cp_parser_error (cp_parser* parser, const char* message)
2017 {
2018 if (!cp_parser_simulate_error (parser))
2019 {
2020 cp_token *token = cp_lexer_peek_token (parser->lexer);
2021 /* This diagnostic makes more sense if it is tagged to the line
2022 of the token we just peeked at. */
2023 cp_lexer_set_source_position_from_token (token);
2024
2025 if (token->type == CPP_PRAGMA)
2026 {
2027 error ("%<#pragma%> is not allowed here");
2028 cp_parser_skip_to_pragma_eol (parser, token);
2029 return;
2030 }
2031
2032 c_parse_error (message,
2033 /* Because c_parser_error does not understand
2034 CPP_KEYWORD, keywords are treated like
2035 identifiers. */
2036 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2037 token->u.value);
2038 }
2039 }
2040
2041 /* Issue an error about name-lookup failing. NAME is the
2042 IDENTIFIER_NODE DECL is the result of
2043 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2044 the thing that we hoped to find. */
2045
2046 static void
2047 cp_parser_name_lookup_error (cp_parser* parser,
2048 tree name,
2049 tree decl,
2050 const char* desired)
2051 {
2052 /* If name lookup completely failed, tell the user that NAME was not
2053 declared. */
2054 if (decl == error_mark_node)
2055 {
2056 if (parser->scope && parser->scope != global_namespace)
2057 error ("%<%E::%E%> has not been declared",
2058 parser->scope, name);
2059 else if (parser->scope == global_namespace)
2060 error ("%<::%E%> has not been declared", name);
2061 else if (parser->object_scope
2062 && !CLASS_TYPE_P (parser->object_scope))
2063 error ("request for member %qE in non-class type %qT",
2064 name, parser->object_scope);
2065 else if (parser->object_scope)
2066 error ("%<%T::%E%> has not been declared",
2067 parser->object_scope, name);
2068 else
2069 error ("%qE has not been declared", name);
2070 }
2071 else if (parser->scope && parser->scope != global_namespace)
2072 error ("%<%E::%E%> %s", parser->scope, name, desired);
2073 else if (parser->scope == global_namespace)
2074 error ("%<::%E%> %s", name, desired);
2075 else
2076 error ("%qE %s", name, desired);
2077 }
2078
2079 /* If we are parsing tentatively, remember that an error has occurred
2080 during this tentative parse. Returns true if the error was
2081 simulated; false if a message should be issued by the caller. */
2082
2083 static bool
2084 cp_parser_simulate_error (cp_parser* parser)
2085 {
2086 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2087 {
2088 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2089 return true;
2090 }
2091 return false;
2092 }
2093
2094 /* Check for repeated decl-specifiers. */
2095
2096 static void
2097 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2098 {
2099 cp_decl_spec ds;
2100
2101 for (ds = ds_first; ds != ds_last; ++ds)
2102 {
2103 unsigned count = decl_specs->specs[(int)ds];
2104 if (count < 2)
2105 continue;
2106 /* The "long" specifier is a special case because of "long long". */
2107 if (ds == ds_long)
2108 {
2109 if (count > 2)
2110 error ("%<long long long%> is too long for GCC");
2111 else if (pedantic && !in_system_header && warn_long_long)
2112 pedwarn ("ISO C++ does not support %<long long%>");
2113 }
2114 else if (count > 1)
2115 {
2116 static const char *const decl_spec_names[] = {
2117 "signed",
2118 "unsigned",
2119 "short",
2120 "long",
2121 "const",
2122 "volatile",
2123 "restrict",
2124 "inline",
2125 "virtual",
2126 "explicit",
2127 "friend",
2128 "typedef",
2129 "__complex",
2130 "__thread"
2131 };
2132 error ("duplicate %qs", decl_spec_names[(int)ds]);
2133 }
2134 }
2135 }
2136
2137 /* This function is called when a type is defined. If type
2138 definitions are forbidden at this point, an error message is
2139 issued. */
2140
2141 static bool
2142 cp_parser_check_type_definition (cp_parser* parser)
2143 {
2144 /* If types are forbidden here, issue a message. */
2145 if (parser->type_definition_forbidden_message)
2146 {
2147 /* Use `%s' to print the string in case there are any escape
2148 characters in the message. */
2149 error ("%s", parser->type_definition_forbidden_message);
2150 return false;
2151 }
2152 return true;
2153 }
2154
2155 /* This function is called when the DECLARATOR is processed. The TYPE
2156 was a type defined in the decl-specifiers. If it is invalid to
2157 define a type in the decl-specifiers for DECLARATOR, an error is
2158 issued. */
2159
2160 static void
2161 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2162 tree type)
2163 {
2164 /* [dcl.fct] forbids type definitions in return types.
2165 Unfortunately, it's not easy to know whether or not we are
2166 processing a return type until after the fact. */
2167 while (declarator
2168 && (declarator->kind == cdk_pointer
2169 || declarator->kind == cdk_reference
2170 || declarator->kind == cdk_ptrmem))
2171 declarator = declarator->declarator;
2172 if (declarator
2173 && declarator->kind == cdk_function)
2174 {
2175 error ("new types may not be defined in a return type");
2176 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2177 type);
2178 }
2179 }
2180
2181 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2182 "<" in any valid C++ program. If the next token is indeed "<",
2183 issue a message warning the user about what appears to be an
2184 invalid attempt to form a template-id. */
2185
2186 static void
2187 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2188 tree type)
2189 {
2190 cp_token_position start = 0;
2191
2192 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2193 {
2194 if (TYPE_P (type))
2195 error ("%qT is not a template", type);
2196 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2197 error ("%qE is not a template", type);
2198 else
2199 error ("invalid template-id");
2200 /* Remember the location of the invalid "<". */
2201 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2202 start = cp_lexer_token_position (parser->lexer, true);
2203 /* Consume the "<". */
2204 cp_lexer_consume_token (parser->lexer);
2205 /* Parse the template arguments. */
2206 cp_parser_enclosed_template_argument_list (parser);
2207 /* Permanently remove the invalid template arguments so that
2208 this error message is not issued again. */
2209 if (start)
2210 cp_lexer_purge_tokens_after (parser->lexer, start);
2211 }
2212 }
2213
2214 /* If parsing an integral constant-expression, issue an error message
2215 about the fact that THING appeared and return true. Otherwise,
2216 return false. In either case, set
2217 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2218
2219 static bool
2220 cp_parser_non_integral_constant_expression (cp_parser *parser,
2221 const char *thing)
2222 {
2223 parser->non_integral_constant_expression_p = true;
2224 if (parser->integral_constant_expression_p)
2225 {
2226 if (!parser->allow_non_integral_constant_expression_p)
2227 {
2228 error ("%s cannot appear in a constant-expression", thing);
2229 return true;
2230 }
2231 }
2232 return false;
2233 }
2234
2235 /* Emit a diagnostic for an invalid type name. SCOPE is the
2236 qualifying scope (or NULL, if none) for ID. This function commits
2237 to the current active tentative parse, if any. (Otherwise, the
2238 problematic construct might be encountered again later, resulting
2239 in duplicate error messages.) */
2240
2241 static void
2242 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2243 {
2244 tree decl, old_scope;
2245 /* Try to lookup the identifier. */
2246 old_scope = parser->scope;
2247 parser->scope = scope;
2248 decl = cp_parser_lookup_name_simple (parser, id);
2249 parser->scope = old_scope;
2250 /* If the lookup found a template-name, it means that the user forgot
2251 to specify an argument list. Emit a useful error message. */
2252 if (TREE_CODE (decl) == TEMPLATE_DECL)
2253 error ("invalid use of template-name %qE without an argument list", decl);
2254 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2255 error ("invalid use of destructor %qD as a type", id);
2256 else if (TREE_CODE (decl) == TYPE_DECL)
2257 /* Something like 'unsigned A a;' */
2258 error ("invalid combination of multiple type-specifiers");
2259 else if (!parser->scope)
2260 {
2261 /* Issue an error message. */
2262 error ("%qE does not name a type", id);
2263 /* If we're in a template class, it's possible that the user was
2264 referring to a type from a base class. For example:
2265
2266 template <typename T> struct A { typedef T X; };
2267 template <typename T> struct B : public A<T> { X x; };
2268
2269 The user should have said "typename A<T>::X". */
2270 if (processing_template_decl && current_class_type
2271 && TYPE_BINFO (current_class_type))
2272 {
2273 tree b;
2274
2275 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2276 b;
2277 b = TREE_CHAIN (b))
2278 {
2279 tree base_type = BINFO_TYPE (b);
2280 if (CLASS_TYPE_P (base_type)
2281 && dependent_type_p (base_type))
2282 {
2283 tree field;
2284 /* Go from a particular instantiation of the
2285 template (which will have an empty TYPE_FIELDs),
2286 to the main version. */
2287 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2288 for (field = TYPE_FIELDS (base_type);
2289 field;
2290 field = TREE_CHAIN (field))
2291 if (TREE_CODE (field) == TYPE_DECL
2292 && DECL_NAME (field) == id)
2293 {
2294 inform ("(perhaps %<typename %T::%E%> was intended)",
2295 BINFO_TYPE (b), id);
2296 break;
2297 }
2298 if (field)
2299 break;
2300 }
2301 }
2302 }
2303 }
2304 /* Here we diagnose qualified-ids where the scope is actually correct,
2305 but the identifier does not resolve to a valid type name. */
2306 else if (parser->scope != error_mark_node)
2307 {
2308 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2309 error ("%qE in namespace %qE does not name a type",
2310 id, parser->scope);
2311 else if (TYPE_P (parser->scope))
2312 error ("%qE in class %qT does not name a type", id, parser->scope);
2313 else
2314 gcc_unreachable ();
2315 }
2316 cp_parser_commit_to_tentative_parse (parser);
2317 }
2318
2319 /* Check for a common situation where a type-name should be present,
2320 but is not, and issue a sensible error message. Returns true if an
2321 invalid type-name was detected.
2322
2323 The situation handled by this function are variable declarations of the
2324 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2325 Usually, `ID' should name a type, but if we got here it means that it
2326 does not. We try to emit the best possible error message depending on
2327 how exactly the id-expression looks like. */
2328
2329 static bool
2330 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2331 {
2332 tree id;
2333
2334 cp_parser_parse_tentatively (parser);
2335 id = cp_parser_id_expression (parser,
2336 /*template_keyword_p=*/false,
2337 /*check_dependency_p=*/true,
2338 /*template_p=*/NULL,
2339 /*declarator_p=*/true,
2340 /*optional_p=*/false);
2341 /* After the id-expression, there should be a plain identifier,
2342 otherwise this is not a simple variable declaration. Also, if
2343 the scope is dependent, we cannot do much. */
2344 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2345 || (parser->scope && TYPE_P (parser->scope)
2346 && dependent_type_p (parser->scope)))
2347 {
2348 cp_parser_abort_tentative_parse (parser);
2349 return false;
2350 }
2351 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2352 return false;
2353
2354 /* Emit a diagnostic for the invalid type. */
2355 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2356 /* Skip to the end of the declaration; there's no point in
2357 trying to process it. */
2358 cp_parser_skip_to_end_of_block_or_statement (parser);
2359 return true;
2360 }
2361
2362 /* Consume tokens up to, and including, the next non-nested closing `)'.
2363 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2364 are doing error recovery. Returns -1 if OR_COMMA is true and we
2365 found an unnested comma. */
2366
2367 static int
2368 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2369 bool recovering,
2370 bool or_comma,
2371 bool consume_paren)
2372 {
2373 unsigned paren_depth = 0;
2374 unsigned brace_depth = 0;
2375
2376 if (recovering && !or_comma
2377 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2378 return 0;
2379
2380 while (true)
2381 {
2382 cp_token * token = cp_lexer_peek_token (parser->lexer);
2383
2384 switch (token->type)
2385 {
2386 case CPP_EOF:
2387 case CPP_PRAGMA_EOL:
2388 /* If we've run out of tokens, then there is no closing `)'. */
2389 return 0;
2390
2391 case CPP_SEMICOLON:
2392 /* This matches the processing in skip_to_end_of_statement. */
2393 if (!brace_depth)
2394 return 0;
2395 break;
2396
2397 case CPP_OPEN_BRACE:
2398 ++brace_depth;
2399 break;
2400 case CPP_CLOSE_BRACE:
2401 if (!brace_depth--)
2402 return 0;
2403 break;
2404
2405 case CPP_COMMA:
2406 if (recovering && or_comma && !brace_depth && !paren_depth)
2407 return -1;
2408 break;
2409
2410 case CPP_OPEN_PAREN:
2411 if (!brace_depth)
2412 ++paren_depth;
2413 break;
2414
2415 case CPP_CLOSE_PAREN:
2416 if (!brace_depth && !paren_depth--)
2417 {
2418 if (consume_paren)
2419 cp_lexer_consume_token (parser->lexer);
2420 return 1;
2421 }
2422 break;
2423
2424 default:
2425 break;
2426 }
2427
2428 /* Consume the token. */
2429 cp_lexer_consume_token (parser->lexer);
2430 }
2431 }
2432
2433 /* Consume tokens until we reach the end of the current statement.
2434 Normally, that will be just before consuming a `;'. However, if a
2435 non-nested `}' comes first, then we stop before consuming that. */
2436
2437 static void
2438 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2439 {
2440 unsigned nesting_depth = 0;
2441
2442 while (true)
2443 {
2444 cp_token *token = cp_lexer_peek_token (parser->lexer);
2445
2446 switch (token->type)
2447 {
2448 case CPP_EOF:
2449 case CPP_PRAGMA_EOL:
2450 /* If we've run out of tokens, stop. */
2451 return;
2452
2453 case CPP_SEMICOLON:
2454 /* If the next token is a `;', we have reached the end of the
2455 statement. */
2456 if (!nesting_depth)
2457 return;
2458 break;
2459
2460 case CPP_CLOSE_BRACE:
2461 /* If this is a non-nested '}', stop before consuming it.
2462 That way, when confronted with something like:
2463
2464 { 3 + }
2465
2466 we stop before consuming the closing '}', even though we
2467 have not yet reached a `;'. */
2468 if (nesting_depth == 0)
2469 return;
2470
2471 /* If it is the closing '}' for a block that we have
2472 scanned, stop -- but only after consuming the token.
2473 That way given:
2474
2475 void f g () { ... }
2476 typedef int I;
2477
2478 we will stop after the body of the erroneously declared
2479 function, but before consuming the following `typedef'
2480 declaration. */
2481 if (--nesting_depth == 0)
2482 {
2483 cp_lexer_consume_token (parser->lexer);
2484 return;
2485 }
2486
2487 case CPP_OPEN_BRACE:
2488 ++nesting_depth;
2489 break;
2490
2491 default:
2492 break;
2493 }
2494
2495 /* Consume the token. */
2496 cp_lexer_consume_token (parser->lexer);
2497 }
2498 }
2499
2500 /* This function is called at the end of a statement or declaration.
2501 If the next token is a semicolon, it is consumed; otherwise, error
2502 recovery is attempted. */
2503
2504 static void
2505 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2506 {
2507 /* Look for the trailing `;'. */
2508 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2509 {
2510 /* If there is additional (erroneous) input, skip to the end of
2511 the statement. */
2512 cp_parser_skip_to_end_of_statement (parser);
2513 /* If the next token is now a `;', consume it. */
2514 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2515 cp_lexer_consume_token (parser->lexer);
2516 }
2517 }
2518
2519 /* Skip tokens until we have consumed an entire block, or until we
2520 have consumed a non-nested `;'. */
2521
2522 static void
2523 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2524 {
2525 int nesting_depth = 0;
2526
2527 while (nesting_depth >= 0)
2528 {
2529 cp_token *token = cp_lexer_peek_token (parser->lexer);
2530
2531 switch (token->type)
2532 {
2533 case CPP_EOF:
2534 case CPP_PRAGMA_EOL:
2535 /* If we've run out of tokens, stop. */
2536 return;
2537
2538 case CPP_SEMICOLON:
2539 /* Stop if this is an unnested ';'. */
2540 if (!nesting_depth)
2541 nesting_depth = -1;
2542 break;
2543
2544 case CPP_CLOSE_BRACE:
2545 /* Stop if this is an unnested '}', or closes the outermost
2546 nesting level. */
2547 nesting_depth--;
2548 if (!nesting_depth)
2549 nesting_depth = -1;
2550 break;
2551
2552 case CPP_OPEN_BRACE:
2553 /* Nest. */
2554 nesting_depth++;
2555 break;
2556
2557 default:
2558 break;
2559 }
2560
2561 /* Consume the token. */
2562 cp_lexer_consume_token (parser->lexer);
2563 }
2564 }
2565
2566 /* Skip tokens until a non-nested closing curly brace is the next
2567 token. */
2568
2569 static void
2570 cp_parser_skip_to_closing_brace (cp_parser *parser)
2571 {
2572 unsigned nesting_depth = 0;
2573
2574 while (true)
2575 {
2576 cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
2578 switch (token->type)
2579 {
2580 case CPP_EOF:
2581 case CPP_PRAGMA_EOL:
2582 /* If we've run out of tokens, stop. */
2583 return;
2584
2585 case CPP_CLOSE_BRACE:
2586 /* If the next token is a non-nested `}', then we have reached
2587 the end of the current block. */
2588 if (nesting_depth-- == 0)
2589 return;
2590 break;
2591
2592 case CPP_OPEN_BRACE:
2593 /* If it the next token is a `{', then we are entering a new
2594 block. Consume the entire block. */
2595 ++nesting_depth;
2596 break;
2597
2598 default:
2599 break;
2600 }
2601
2602 /* Consume the token. */
2603 cp_lexer_consume_token (parser->lexer);
2604 }
2605 }
2606
2607 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2608 parameter is the PRAGMA token, allowing us to purge the entire pragma
2609 sequence. */
2610
2611 static void
2612 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2613 {
2614 cp_token *token;
2615
2616 parser->lexer->in_pragma = false;
2617
2618 do
2619 token = cp_lexer_consume_token (parser->lexer);
2620 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2621
2622 /* Ensure that the pragma is not parsed again. */
2623 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2624 }
2625
2626 /* Require pragma end of line, resyncing with it as necessary. The
2627 arguments are as for cp_parser_skip_to_pragma_eol. */
2628
2629 static void
2630 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2631 {
2632 parser->lexer->in_pragma = false;
2633 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2635 }
2636
2637 /* This is a simple wrapper around make_typename_type. When the id is
2638 an unresolved identifier node, we can provide a superior diagnostic
2639 using cp_parser_diagnose_invalid_type_name. */
2640
2641 static tree
2642 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2643 {
2644 tree result;
2645 if (TREE_CODE (id) == IDENTIFIER_NODE)
2646 {
2647 result = make_typename_type (scope, id, typename_type,
2648 /*complain=*/tf_none);
2649 if (result == error_mark_node)
2650 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2651 return result;
2652 }
2653 return make_typename_type (scope, id, typename_type, tf_error);
2654 }
2655
2656
2657 /* Create a new C++ parser. */
2658
2659 static cp_parser *
2660 cp_parser_new (void)
2661 {
2662 cp_parser *parser;
2663 cp_lexer *lexer;
2664 unsigned i;
2665
2666 /* cp_lexer_new_main is called before calling ggc_alloc because
2667 cp_lexer_new_main might load a PCH file. */
2668 lexer = cp_lexer_new_main ();
2669
2670 /* Initialize the binops_by_token so that we can get the tree
2671 directly from the token. */
2672 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2673 binops_by_token[binops[i].token_type] = binops[i];
2674
2675 parser = GGC_CNEW (cp_parser);
2676 parser->lexer = lexer;
2677 parser->context = cp_parser_context_new (NULL);
2678
2679 /* For now, we always accept GNU extensions. */
2680 parser->allow_gnu_extensions_p = 1;
2681
2682 /* The `>' token is a greater-than operator, not the end of a
2683 template-id. */
2684 parser->greater_than_is_operator_p = true;
2685
2686 parser->default_arg_ok_p = true;
2687
2688 /* We are not parsing a constant-expression. */
2689 parser->integral_constant_expression_p = false;
2690 parser->allow_non_integral_constant_expression_p = false;
2691 parser->non_integral_constant_expression_p = false;
2692
2693 /* Local variable names are not forbidden. */
2694 parser->local_variables_forbidden_p = false;
2695
2696 /* We are not processing an `extern "C"' declaration. */
2697 parser->in_unbraced_linkage_specification_p = false;
2698
2699 /* We are not processing a declarator. */
2700 parser->in_declarator_p = false;
2701
2702 /* We are not processing a template-argument-list. */
2703 parser->in_template_argument_list_p = false;
2704
2705 /* We are not in an iteration statement. */
2706 parser->in_statement = 0;
2707
2708 /* We are not in a switch statement. */
2709 parser->in_switch_statement_p = false;
2710
2711 /* We are not parsing a type-id inside an expression. */
2712 parser->in_type_id_in_expr_p = false;
2713
2714 /* Declarations aren't implicitly extern "C". */
2715 parser->implicit_extern_c = false;
2716
2717 /* String literals should be translated to the execution character set. */
2718 parser->translate_strings_p = true;
2719
2720 /* We are not parsing a function body. */
2721 parser->in_function_body = false;
2722
2723 /* The unparsed function queue is empty. */
2724 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2725
2726 /* There are no classes being defined. */
2727 parser->num_classes_being_defined = 0;
2728
2729 /* No template parameters apply. */
2730 parser->num_template_parameter_lists = 0;
2731
2732 return parser;
2733 }
2734
2735 /* Create a cp_lexer structure which will emit the tokens in CACHE
2736 and push it onto the parser's lexer stack. This is used for delayed
2737 parsing of in-class method bodies and default arguments, and should
2738 not be confused with tentative parsing. */
2739 static void
2740 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2741 {
2742 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2743 lexer->next = parser->lexer;
2744 parser->lexer = lexer;
2745
2746 /* Move the current source position to that of the first token in the
2747 new lexer. */
2748 cp_lexer_set_source_position_from_token (lexer->next_token);
2749 }
2750
2751 /* Pop the top lexer off the parser stack. This is never used for the
2752 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2753 static void
2754 cp_parser_pop_lexer (cp_parser *parser)
2755 {
2756 cp_lexer *lexer = parser->lexer;
2757 parser->lexer = lexer->next;
2758 cp_lexer_destroy (lexer);
2759
2760 /* Put the current source position back where it was before this
2761 lexer was pushed. */
2762 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2763 }
2764
2765 /* Lexical conventions [gram.lex] */
2766
2767 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2768 identifier. */
2769
2770 static tree
2771 cp_parser_identifier (cp_parser* parser)
2772 {
2773 cp_token *token;
2774
2775 /* Look for the identifier. */
2776 token = cp_parser_require (parser, CPP_NAME, "identifier");
2777 /* Return the value. */
2778 return token ? token->u.value : error_mark_node;
2779 }
2780
2781 /* Parse a sequence of adjacent string constants. Returns a
2782 TREE_STRING representing the combined, nul-terminated string
2783 constant. If TRANSLATE is true, translate the string to the
2784 execution character set. If WIDE_OK is true, a wide string is
2785 invalid here.
2786
2787 C++98 [lex.string] says that if a narrow string literal token is
2788 adjacent to a wide string literal token, the behavior is undefined.
2789 However, C99 6.4.5p4 says that this results in a wide string literal.
2790 We follow C99 here, for consistency with the C front end.
2791
2792 This code is largely lifted from lex_string() in c-lex.c.
2793
2794 FUTURE: ObjC++ will need to handle @-strings here. */
2795 static tree
2796 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2797 {
2798 tree value;
2799 bool wide = false;
2800 size_t count;
2801 struct obstack str_ob;
2802 cpp_string str, istr, *strs;
2803 cp_token *tok;
2804
2805 tok = cp_lexer_peek_token (parser->lexer);
2806 if (!cp_parser_is_string_literal (tok))
2807 {
2808 cp_parser_error (parser, "expected string-literal");
2809 return error_mark_node;
2810 }
2811
2812 /* Try to avoid the overhead of creating and destroying an obstack
2813 for the common case of just one string. */
2814 if (!cp_parser_is_string_literal
2815 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2816 {
2817 cp_lexer_consume_token (parser->lexer);
2818
2819 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2820 str.len = TREE_STRING_LENGTH (tok->u.value);
2821 count = 1;
2822 if (tok->type == CPP_WSTRING)
2823 wide = true;
2824
2825 strs = &str;
2826 }
2827 else
2828 {
2829 gcc_obstack_init (&str_ob);
2830 count = 0;
2831
2832 do
2833 {
2834 cp_lexer_consume_token (parser->lexer);
2835 count++;
2836 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2837 str.len = TREE_STRING_LENGTH (tok->u.value);
2838 if (tok->type == CPP_WSTRING)
2839 wide = true;
2840
2841 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2842
2843 tok = cp_lexer_peek_token (parser->lexer);
2844 }
2845 while (cp_parser_is_string_literal (tok));
2846
2847 strs = (cpp_string *) obstack_finish (&str_ob);
2848 }
2849
2850 if (wide && !wide_ok)
2851 {
2852 cp_parser_error (parser, "a wide string is invalid in this context");
2853 wide = false;
2854 }
2855
2856 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2857 (parse_in, strs, count, &istr, wide))
2858 {
2859 value = build_string (istr.len, (char *)istr.text);
2860 free ((void *)istr.text);
2861
2862 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2863 value = fix_string_type (value);
2864 }
2865 else
2866 /* cpp_interpret_string has issued an error. */
2867 value = error_mark_node;
2868
2869 if (count > 1)
2870 obstack_free (&str_ob, 0);
2871
2872 return value;
2873 }
2874
2875
2876 /* Basic concepts [gram.basic] */
2877
2878 /* Parse a translation-unit.
2879
2880 translation-unit:
2881 declaration-seq [opt]
2882
2883 Returns TRUE if all went well. */
2884
2885 static bool
2886 cp_parser_translation_unit (cp_parser* parser)
2887 {
2888 /* The address of the first non-permanent object on the declarator
2889 obstack. */
2890 static void *declarator_obstack_base;
2891
2892 bool success;
2893
2894 /* Create the declarator obstack, if necessary. */
2895 if (!cp_error_declarator)
2896 {
2897 gcc_obstack_init (&declarator_obstack);
2898 /* Create the error declarator. */
2899 cp_error_declarator = make_declarator (cdk_error);
2900 /* Create the empty parameter list. */
2901 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2902 /* Remember where the base of the declarator obstack lies. */
2903 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2904 }
2905
2906 cp_parser_declaration_seq_opt (parser);
2907
2908 /* If there are no tokens left then all went well. */
2909 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2910 {
2911 /* Get rid of the token array; we don't need it any more. */
2912 cp_lexer_destroy (parser->lexer);
2913 parser->lexer = NULL;
2914
2915 /* This file might have been a context that's implicitly extern
2916 "C". If so, pop the lang context. (Only relevant for PCH.) */
2917 if (parser->implicit_extern_c)
2918 {
2919 pop_lang_context ();
2920 parser->implicit_extern_c = false;
2921 }
2922
2923 /* Finish up. */
2924 finish_translation_unit ();
2925
2926 success = true;
2927 }
2928 else
2929 {
2930 cp_parser_error (parser, "expected declaration");
2931 success = false;
2932 }
2933
2934 /* Make sure the declarator obstack was fully cleaned up. */
2935 gcc_assert (obstack_next_free (&declarator_obstack)
2936 == declarator_obstack_base);
2937
2938 /* All went well. */
2939 return success;
2940 }
2941
2942 /* Expressions [gram.expr] */
2943
2944 /* Parse a primary-expression.
2945
2946 primary-expression:
2947 literal
2948 this
2949 ( expression )
2950 id-expression
2951
2952 GNU Extensions:
2953
2954 primary-expression:
2955 ( compound-statement )
2956 __builtin_va_arg ( assignment-expression , type-id )
2957 __builtin_offsetof ( type-id , offsetof-expression )
2958
2959 Objective-C++ Extension:
2960
2961 primary-expression:
2962 objc-expression
2963
2964 literal:
2965 __null
2966
2967 ADDRESS_P is true iff this expression was immediately preceded by
2968 "&" and therefore might denote a pointer-to-member. CAST_P is true
2969 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2970 true iff this expression is a template argument.
2971
2972 Returns a representation of the expression. Upon return, *IDK
2973 indicates what kind of id-expression (if any) was present. */
2974
2975 static tree
2976 cp_parser_primary_expression (cp_parser *parser,
2977 bool address_p,
2978 bool cast_p,
2979 bool template_arg_p,
2980 cp_id_kind *idk)
2981 {
2982 cp_token *token;
2983
2984 /* Assume the primary expression is not an id-expression. */
2985 *idk = CP_ID_KIND_NONE;
2986
2987 /* Peek at the next token. */
2988 token = cp_lexer_peek_token (parser->lexer);
2989 switch (token->type)
2990 {
2991 /* literal:
2992 integer-literal
2993 character-literal
2994 floating-literal
2995 string-literal
2996 boolean-literal */
2997 case CPP_CHAR:
2998 case CPP_WCHAR:
2999 case CPP_NUMBER:
3000 token = cp_lexer_consume_token (parser->lexer);
3001 /* Floating-point literals are only allowed in an integral
3002 constant expression if they are cast to an integral or
3003 enumeration type. */
3004 if (TREE_CODE (token->u.value) == REAL_CST
3005 && parser->integral_constant_expression_p
3006 && pedantic)
3007 {
3008 /* CAST_P will be set even in invalid code like "int(2.7 +
3009 ...)". Therefore, we have to check that the next token
3010 is sure to end the cast. */
3011 if (cast_p)
3012 {
3013 cp_token *next_token;
3014
3015 next_token = cp_lexer_peek_token (parser->lexer);
3016 if (/* The comma at the end of an
3017 enumerator-definition. */
3018 next_token->type != CPP_COMMA
3019 /* The curly brace at the end of an enum-specifier. */
3020 && next_token->type != CPP_CLOSE_BRACE
3021 /* The end of a statement. */
3022 && next_token->type != CPP_SEMICOLON
3023 /* The end of the cast-expression. */
3024 && next_token->type != CPP_CLOSE_PAREN
3025 /* The end of an array bound. */
3026 && next_token->type != CPP_CLOSE_SQUARE
3027 /* The closing ">" in a template-argument-list. */
3028 && (next_token->type != CPP_GREATER
3029 || parser->greater_than_is_operator_p))
3030 cast_p = false;
3031 }
3032
3033 /* If we are within a cast, then the constraint that the
3034 cast is to an integral or enumeration type will be
3035 checked at that point. If we are not within a cast, then
3036 this code is invalid. */
3037 if (!cast_p)
3038 cp_parser_non_integral_constant_expression
3039 (parser, "floating-point literal");
3040 }
3041 return token->u.value;
3042
3043 case CPP_STRING:
3044 case CPP_WSTRING:
3045 /* ??? Should wide strings be allowed when parser->translate_strings_p
3046 is false (i.e. in attributes)? If not, we can kill the third
3047 argument to cp_parser_string_literal. */
3048 return cp_parser_string_literal (parser,
3049 parser->translate_strings_p,
3050 true);
3051
3052 case CPP_OPEN_PAREN:
3053 {
3054 tree expr;
3055 bool saved_greater_than_is_operator_p;
3056
3057 /* Consume the `('. */
3058 cp_lexer_consume_token (parser->lexer);
3059 /* Within a parenthesized expression, a `>' token is always
3060 the greater-than operator. */
3061 saved_greater_than_is_operator_p
3062 = parser->greater_than_is_operator_p;
3063 parser->greater_than_is_operator_p = true;
3064 /* If we see `( { ' then we are looking at the beginning of
3065 a GNU statement-expression. */
3066 if (cp_parser_allow_gnu_extensions_p (parser)
3067 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3068 {
3069 /* Statement-expressions are not allowed by the standard. */
3070 if (pedantic)
3071 pedwarn ("ISO C++ forbids braced-groups within expressions");
3072
3073 /* And they're not allowed outside of a function-body; you
3074 cannot, for example, write:
3075
3076 int i = ({ int j = 3; j + 1; });
3077
3078 at class or namespace scope. */
3079 if (!parser->in_function_body)
3080 {
3081 error ("statement-expressions are allowed only inside functions");
3082 cp_parser_skip_to_end_of_block_or_statement (parser);
3083 expr = error_mark_node;
3084 }
3085 else
3086 {
3087 /* Start the statement-expression. */
3088 expr = begin_stmt_expr ();
3089 /* Parse the compound-statement. */
3090 cp_parser_compound_statement (parser, expr, false);
3091 /* Finish up. */
3092 expr = finish_stmt_expr (expr, false);
3093 }
3094 }
3095 else
3096 {
3097 /* Parse the parenthesized expression. */
3098 expr = cp_parser_expression (parser, cast_p);
3099 /* Let the front end know that this expression was
3100 enclosed in parentheses. This matters in case, for
3101 example, the expression is of the form `A::B', since
3102 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3103 not. */
3104 finish_parenthesized_expr (expr);
3105 }
3106 /* The `>' token might be the end of a template-id or
3107 template-parameter-list now. */
3108 parser->greater_than_is_operator_p
3109 = saved_greater_than_is_operator_p;
3110 /* Consume the `)'. */
3111 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3112 cp_parser_skip_to_end_of_statement (parser);
3113
3114 return expr;
3115 }
3116
3117 case CPP_KEYWORD:
3118 switch (token->keyword)
3119 {
3120 /* These two are the boolean literals. */
3121 case RID_TRUE:
3122 cp_lexer_consume_token (parser->lexer);
3123 return boolean_true_node;
3124 case RID_FALSE:
3125 cp_lexer_consume_token (parser->lexer);
3126 return boolean_false_node;
3127
3128 /* The `__null' literal. */
3129 case RID_NULL:
3130 cp_lexer_consume_token (parser->lexer);
3131 return null_node;
3132
3133 /* Recognize the `this' keyword. */
3134 case RID_THIS:
3135 cp_lexer_consume_token (parser->lexer);
3136 if (parser->local_variables_forbidden_p)
3137 {
3138 error ("%<this%> may not be used in this context");
3139 return error_mark_node;
3140 }
3141 /* Pointers cannot appear in constant-expressions. */
3142 if (cp_parser_non_integral_constant_expression (parser,
3143 "`this'"))
3144 return error_mark_node;
3145 return finish_this_expr ();
3146
3147 /* The `operator' keyword can be the beginning of an
3148 id-expression. */
3149 case RID_OPERATOR:
3150 goto id_expression;
3151
3152 case RID_FUNCTION_NAME:
3153 case RID_PRETTY_FUNCTION_NAME:
3154 case RID_C99_FUNCTION_NAME:
3155 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3156 __func__ are the names of variables -- but they are
3157 treated specially. Therefore, they are handled here,
3158 rather than relying on the generic id-expression logic
3159 below. Grammatically, these names are id-expressions.
3160
3161 Consume the token. */
3162 token = cp_lexer_consume_token (parser->lexer);
3163 /* Look up the name. */
3164 return finish_fname (token->u.value);
3165
3166 case RID_VA_ARG:
3167 {
3168 tree expression;
3169 tree type;
3170
3171 /* The `__builtin_va_arg' construct is used to handle
3172 `va_arg'. Consume the `__builtin_va_arg' token. */
3173 cp_lexer_consume_token (parser->lexer);
3174 /* Look for the opening `('. */
3175 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3176 /* Now, parse the assignment-expression. */
3177 expression = cp_parser_assignment_expression (parser,
3178 /*cast_p=*/false);
3179 /* Look for the `,'. */
3180 cp_parser_require (parser, CPP_COMMA, "`,'");
3181 /* Parse the type-id. */
3182 type = cp_parser_type_id (parser);
3183 /* Look for the closing `)'. */
3184 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3185 /* Using `va_arg' in a constant-expression is not
3186 allowed. */
3187 if (cp_parser_non_integral_constant_expression (parser,
3188 "`va_arg'"))
3189 return error_mark_node;
3190 return build_x_va_arg (expression, type);
3191 }
3192
3193 case RID_OFFSETOF:
3194 return cp_parser_builtin_offsetof (parser);
3195
3196 /* Objective-C++ expressions. */
3197 case RID_AT_ENCODE:
3198 case RID_AT_PROTOCOL:
3199 case RID_AT_SELECTOR:
3200 return cp_parser_objc_expression (parser);
3201
3202 default:
3203 cp_parser_error (parser, "expected primary-expression");
3204 return error_mark_node;
3205 }
3206
3207 /* An id-expression can start with either an identifier, a
3208 `::' as the beginning of a qualified-id, or the "operator"
3209 keyword. */
3210 case CPP_NAME:
3211 case CPP_SCOPE:
3212 case CPP_TEMPLATE_ID:
3213 case CPP_NESTED_NAME_SPECIFIER:
3214 {
3215 tree id_expression;
3216 tree decl;
3217 const char *error_msg;
3218 bool template_p;
3219 bool done;
3220
3221 id_expression:
3222 /* Parse the id-expression. */
3223 id_expression
3224 = cp_parser_id_expression (parser,
3225 /*template_keyword_p=*/false,
3226 /*check_dependency_p=*/true,
3227 &template_p,
3228 /*declarator_p=*/false,
3229 /*optional_p=*/false);
3230 if (id_expression == error_mark_node)
3231 return error_mark_node;
3232 token = cp_lexer_peek_token (parser->lexer);
3233 done = (token->type != CPP_OPEN_SQUARE
3234 && token->type != CPP_OPEN_PAREN
3235 && token->type != CPP_DOT
3236 && token->type != CPP_DEREF
3237 && token->type != CPP_PLUS_PLUS
3238 && token->type != CPP_MINUS_MINUS);
3239 /* If we have a template-id, then no further lookup is
3240 required. If the template-id was for a template-class, we
3241 will sometimes have a TYPE_DECL at this point. */
3242 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3243 || TREE_CODE (id_expression) == TYPE_DECL)
3244 decl = id_expression;
3245 /* Look up the name. */
3246 else
3247 {
3248 tree ambiguous_decls;
3249
3250 decl = cp_parser_lookup_name (parser, id_expression,
3251 none_type,
3252 template_p,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls);
3256 /* If the lookup was ambiguous, an error will already have
3257 been issued. */
3258 if (ambiguous_decls)
3259 return error_mark_node;
3260
3261 /* In Objective-C++, an instance variable (ivar) may be preferred
3262 to whatever cp_parser_lookup_name() found. */
3263 decl = objc_lookup_ivar (decl, id_expression);
3264
3265 /* If name lookup gives us a SCOPE_REF, then the
3266 qualifying scope was dependent. */
3267 if (TREE_CODE (decl) == SCOPE_REF)
3268 return decl;
3269 /* Check to see if DECL is a local variable in a context
3270 where that is forbidden. */
3271 if (parser->local_variables_forbidden_p
3272 && local_variable_p (decl))
3273 {
3274 /* It might be that we only found DECL because we are
3275 trying to be generous with pre-ISO scoping rules.
3276 For example, consider:
3277
3278 int i;
3279 void g() {
3280 for (int i = 0; i < 10; ++i) {}
3281 extern void f(int j = i);
3282 }
3283
3284 Here, name look up will originally find the out
3285 of scope `i'. We need to issue a warning message,
3286 but then use the global `i'. */
3287 decl = check_for_out_of_scope_variable (decl);
3288 if (local_variable_p (decl))
3289 {
3290 error ("local variable %qD may not appear in this context",
3291 decl);
3292 return error_mark_node;
3293 }
3294 }
3295 }
3296
3297 decl = (finish_id_expression
3298 (id_expression, decl, parser->scope,
3299 idk,
3300 parser->integral_constant_expression_p,
3301 parser->allow_non_integral_constant_expression_p,
3302 &parser->non_integral_constant_expression_p,
3303 template_p, done, address_p,
3304 template_arg_p,
3305 &error_msg));
3306 if (error_msg)
3307 cp_parser_error (parser, error_msg);
3308 return decl;
3309 }
3310
3311 /* Anything else is an error. */
3312 default:
3313 /* ...unless we have an Objective-C++ message or string literal,
3314 that is. */
3315 if (c_dialect_objc ()
3316 && (token->type == CPP_OPEN_SQUARE
3317 || token->type == CPP_OBJC_STRING))
3318 return cp_parser_objc_expression (parser);
3319
3320 cp_parser_error (parser, "expected primary-expression");
3321 return error_mark_node;
3322 }
3323 }
3324
3325 /* Parse an id-expression.
3326
3327 id-expression:
3328 unqualified-id
3329 qualified-id
3330
3331 qualified-id:
3332 :: [opt] nested-name-specifier template [opt] unqualified-id
3333 :: identifier
3334 :: operator-function-id
3335 :: template-id
3336
3337 Return a representation of the unqualified portion of the
3338 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3339 a `::' or nested-name-specifier.
3340
3341 Often, if the id-expression was a qualified-id, the caller will
3342 want to make a SCOPE_REF to represent the qualified-id. This
3343 function does not do this in order to avoid wastefully creating
3344 SCOPE_REFs when they are not required.
3345
3346 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3347 `template' keyword.
3348
3349 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3350 uninstantiated templates.
3351
3352 If *TEMPLATE_P is non-NULL, it is set to true iff the
3353 `template' keyword is used to explicitly indicate that the entity
3354 named is a template.
3355
3356 If DECLARATOR_P is true, the id-expression is appearing as part of
3357 a declarator, rather than as part of an expression. */
3358
3359 static tree
3360 cp_parser_id_expression (cp_parser *parser,
3361 bool template_keyword_p,
3362 bool check_dependency_p,
3363 bool *template_p,
3364 bool declarator_p,
3365 bool optional_p)
3366 {
3367 bool global_scope_p;
3368 bool nested_name_specifier_p;
3369
3370 /* Assume the `template' keyword was not used. */
3371 if (template_p)
3372 *template_p = template_keyword_p;
3373
3374 /* Look for the optional `::' operator. */
3375 global_scope_p
3376 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3377 != NULL_TREE);
3378 /* Look for the optional nested-name-specifier. */
3379 nested_name_specifier_p
3380 = (cp_parser_nested_name_specifier_opt (parser,
3381 /*typename_keyword_p=*/false,
3382 check_dependency_p,
3383 /*type_p=*/false,
3384 declarator_p)
3385 != NULL_TREE);
3386 /* If there is a nested-name-specifier, then we are looking at
3387 the first qualified-id production. */
3388 if (nested_name_specifier_p)
3389 {
3390 tree saved_scope;
3391 tree saved_object_scope;
3392 tree saved_qualifying_scope;
3393 tree unqualified_id;
3394 bool is_template;
3395
3396 /* See if the next token is the `template' keyword. */
3397 if (!template_p)
3398 template_p = &is_template;
3399 *template_p = cp_parser_optional_template_keyword (parser);
3400 /* Name lookup we do during the processing of the
3401 unqualified-id might obliterate SCOPE. */
3402 saved_scope = parser->scope;
3403 saved_object_scope = parser->object_scope;
3404 saved_qualifying_scope = parser->qualifying_scope;
3405 /* Process the final unqualified-id. */
3406 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3407 check_dependency_p,
3408 declarator_p,
3409 /*optional_p=*/false);
3410 /* Restore the SAVED_SCOPE for our caller. */
3411 parser->scope = saved_scope;
3412 parser->object_scope = saved_object_scope;
3413 parser->qualifying_scope = saved_qualifying_scope;
3414
3415 return unqualified_id;
3416 }
3417 /* Otherwise, if we are in global scope, then we are looking at one
3418 of the other qualified-id productions. */
3419 else if (global_scope_p)
3420 {
3421 cp_token *token;
3422 tree id;
3423
3424 /* Peek at the next token. */
3425 token = cp_lexer_peek_token (parser->lexer);
3426
3427 /* If it's an identifier, and the next token is not a "<", then
3428 we can avoid the template-id case. This is an optimization
3429 for this common case. */
3430 if (token->type == CPP_NAME
3431 && !cp_parser_nth_token_starts_template_argument_list_p
3432 (parser, 2))
3433 return cp_parser_identifier (parser);
3434
3435 cp_parser_parse_tentatively (parser);
3436 /* Try a template-id. */
3437 id = cp_parser_template_id (parser,
3438 /*template_keyword_p=*/false,
3439 /*check_dependency_p=*/true,
3440 declarator_p);
3441 /* If that worked, we're done. */
3442 if (cp_parser_parse_definitely (parser))
3443 return id;
3444
3445 /* Peek at the next token. (Changes in the token buffer may
3446 have invalidated the pointer obtained above.) */
3447 token = cp_lexer_peek_token (parser->lexer);
3448
3449 switch (token->type)
3450 {
3451 case CPP_NAME:
3452 return cp_parser_identifier (parser);
3453
3454 case CPP_KEYWORD:
3455 if (token->keyword == RID_OPERATOR)
3456 return cp_parser_operator_function_id (parser);
3457 /* Fall through. */
3458
3459 default:
3460 cp_parser_error (parser, "expected id-expression");
3461 return error_mark_node;
3462 }
3463 }
3464 else
3465 return cp_parser_unqualified_id (parser, template_keyword_p,
3466 /*check_dependency_p=*/true,
3467 declarator_p,
3468 optional_p);
3469 }
3470
3471 /* Parse an unqualified-id.
3472
3473 unqualified-id:
3474 identifier
3475 operator-function-id
3476 conversion-function-id
3477 ~ class-name
3478 template-id
3479
3480 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3481 keyword, in a construct like `A::template ...'.
3482
3483 Returns a representation of unqualified-id. For the `identifier'
3484 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3485 production a BIT_NOT_EXPR is returned; the operand of the
3486 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3487 other productions, see the documentation accompanying the
3488 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3489 names are looked up in uninstantiated templates. If DECLARATOR_P
3490 is true, the unqualified-id is appearing as part of a declarator,
3491 rather than as part of an expression. */
3492
3493 static tree
3494 cp_parser_unqualified_id (cp_parser* parser,
3495 bool template_keyword_p,
3496 bool check_dependency_p,
3497 bool declarator_p,
3498 bool optional_p)
3499 {
3500 cp_token *token;
3501
3502 /* Peek at the next token. */
3503 token = cp_lexer_peek_token (parser->lexer);
3504
3505 switch (token->type)
3506 {
3507 case CPP_NAME:
3508 {
3509 tree id;
3510
3511 /* We don't know yet whether or not this will be a
3512 template-id. */
3513 cp_parser_parse_tentatively (parser);
3514 /* Try a template-id. */
3515 id = cp_parser_template_id (parser, template_keyword_p,
3516 check_dependency_p,
3517 declarator_p);
3518 /* If it worked, we're done. */
3519 if (cp_parser_parse_definitely (parser))
3520 return id;
3521 /* Otherwise, it's an ordinary identifier. */
3522 return cp_parser_identifier (parser);
3523 }
3524
3525 case CPP_TEMPLATE_ID:
3526 return cp_parser_template_id (parser, template_keyword_p,
3527 check_dependency_p,
3528 declarator_p);
3529
3530 case CPP_COMPL:
3531 {
3532 tree type_decl;
3533 tree qualifying_scope;
3534 tree object_scope;
3535 tree scope;
3536 bool done;
3537
3538 /* Consume the `~' token. */
3539 cp_lexer_consume_token (parser->lexer);
3540 /* Parse the class-name. The standard, as written, seems to
3541 say that:
3542
3543 template <typename T> struct S { ~S (); };
3544 template <typename T> S<T>::~S() {}
3545
3546 is invalid, since `~' must be followed by a class-name, but
3547 `S<T>' is dependent, and so not known to be a class.
3548 That's not right; we need to look in uninstantiated
3549 templates. A further complication arises from:
3550
3551 template <typename T> void f(T t) {
3552 t.T::~T();
3553 }
3554
3555 Here, it is not possible to look up `T' in the scope of `T'
3556 itself. We must look in both the current scope, and the
3557 scope of the containing complete expression.
3558
3559 Yet another issue is:
3560
3561 struct S {
3562 int S;
3563 ~S();
3564 };
3565
3566 S::~S() {}
3567
3568 The standard does not seem to say that the `S' in `~S'
3569 should refer to the type `S' and not the data member
3570 `S::S'. */
3571
3572 /* DR 244 says that we look up the name after the "~" in the
3573 same scope as we looked up the qualifying name. That idea
3574 isn't fully worked out; it's more complicated than that. */
3575 scope = parser->scope;
3576 object_scope = parser->object_scope;
3577 qualifying_scope = parser->qualifying_scope;
3578
3579 /* Check for invalid scopes. */
3580 if (scope == error_mark_node)
3581 {
3582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3583 cp_lexer_consume_token (parser->lexer);
3584 return error_mark_node;
3585 }
3586 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3587 {
3588 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3589 error ("scope %qT before %<~%> is not a class-name", scope);
3590 cp_parser_simulate_error (parser);
3591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3592 cp_lexer_consume_token (parser->lexer);
3593 return error_mark_node;
3594 }
3595 gcc_assert (!scope || TYPE_P (scope));
3596
3597 /* If the name is of the form "X::~X" it's OK. */
3598 token = cp_lexer_peek_token (parser->lexer);
3599 if (scope
3600 && token->type == CPP_NAME
3601 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3602 == CPP_OPEN_PAREN)
3603 && constructor_name_p (token->u.value, scope))
3604 {
3605 cp_lexer_consume_token (parser->lexer);
3606 return build_nt (BIT_NOT_EXPR, scope);
3607 }
3608
3609 /* If there was an explicit qualification (S::~T), first look
3610 in the scope given by the qualification (i.e., S). */
3611 done = false;
3612 type_decl = NULL_TREE;
3613 if (scope)
3614 {
3615 cp_parser_parse_tentatively (parser);
3616 type_decl = cp_parser_class_name (parser,
3617 /*typename_keyword_p=*/false,
3618 /*template_keyword_p=*/false,
3619 none_type,
3620 /*check_dependency=*/false,
3621 /*class_head_p=*/false,
3622 declarator_p);
3623 if (cp_parser_parse_definitely (parser))
3624 done = true;
3625 }
3626 /* In "N::S::~S", look in "N" as well. */
3627 if (!done && scope && qualifying_scope)
3628 {
3629 cp_parser_parse_tentatively (parser);
3630 parser->scope = qualifying_scope;
3631 parser->object_scope = NULL_TREE;
3632 parser->qualifying_scope = NULL_TREE;
3633 type_decl
3634 = cp_parser_class_name (parser,
3635 /*typename_keyword_p=*/false,
3636 /*template_keyword_p=*/false,
3637 none_type,
3638 /*check_dependency=*/false,
3639 /*class_head_p=*/false,
3640 declarator_p);
3641 if (cp_parser_parse_definitely (parser))
3642 done = true;
3643 }
3644 /* In "p->S::~T", look in the scope given by "*p" as well. */
3645 else if (!done && object_scope)
3646 {
3647 cp_parser_parse_tentatively (parser);
3648 parser->scope = object_scope;
3649 parser->object_scope = NULL_TREE;
3650 parser->qualifying_scope = NULL_TREE;
3651 type_decl
3652 = cp_parser_class_name (parser,
3653 /*typename_keyword_p=*/false,
3654 /*template_keyword_p=*/false,
3655 none_type,
3656 /*check_dependency=*/false,
3657 /*class_head_p=*/false,
3658 declarator_p);
3659 if (cp_parser_parse_definitely (parser))
3660 done = true;
3661 }
3662 /* Look in the surrounding context. */
3663 if (!done)
3664 {
3665 parser->scope = NULL_TREE;
3666 parser->object_scope = NULL_TREE;
3667 parser->qualifying_scope = NULL_TREE;
3668 type_decl
3669 = cp_parser_class_name (parser,
3670 /*typename_keyword_p=*/false,
3671 /*template_keyword_p=*/false,
3672 none_type,
3673 /*check_dependency=*/false,
3674 /*class_head_p=*/false,
3675 declarator_p);
3676 }
3677 /* If an error occurred, assume that the name of the
3678 destructor is the same as the name of the qualifying
3679 class. That allows us to keep parsing after running
3680 into ill-formed destructor names. */
3681 if (type_decl == error_mark_node && scope)
3682 return build_nt (BIT_NOT_EXPR, scope);
3683 else if (type_decl == error_mark_node)
3684 return error_mark_node;
3685
3686 /* Check that destructor name and scope match. */
3687 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3688 {
3689 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3690 error ("declaration of %<~%T%> as member of %qT",
3691 type_decl, scope);
3692 cp_parser_simulate_error (parser);
3693 return error_mark_node;
3694 }
3695
3696 /* [class.dtor]
3697
3698 A typedef-name that names a class shall not be used as the
3699 identifier in the declarator for a destructor declaration. */
3700 if (declarator_p
3701 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3702 && !DECL_SELF_REFERENCE_P (type_decl)
3703 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3704 error ("typedef-name %qD used as destructor declarator",
3705 type_decl);
3706
3707 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3708 }
3709
3710 case CPP_KEYWORD:
3711 if (token->keyword == RID_OPERATOR)
3712 {
3713 tree id;
3714
3715 /* This could be a template-id, so we try that first. */
3716 cp_parser_parse_tentatively (parser);
3717 /* Try a template-id. */
3718 id = cp_parser_template_id (parser, template_keyword_p,
3719 /*check_dependency_p=*/true,
3720 declarator_p);
3721 /* If that worked, we're done. */
3722 if (cp_parser_parse_definitely (parser))
3723 return id;
3724 /* We still don't know whether we're looking at an
3725 operator-function-id or a conversion-function-id. */
3726 cp_parser_parse_tentatively (parser);
3727 /* Try an operator-function-id. */
3728 id = cp_parser_operator_function_id (parser);
3729 /* If that didn't work, try a conversion-function-id. */
3730 if (!cp_parser_parse_definitely (parser))
3731 id = cp_parser_conversion_function_id (parser);
3732
3733 return id;
3734 }
3735 /* Fall through. */
3736
3737 default:
3738 if (optional_p)
3739 return NULL_TREE;
3740 cp_parser_error (parser, "expected unqualified-id");
3741 return error_mark_node;
3742 }
3743 }
3744
3745 /* Parse an (optional) nested-name-specifier.
3746
3747 nested-name-specifier:
3748 class-or-namespace-name :: nested-name-specifier [opt]
3749 class-or-namespace-name :: template nested-name-specifier [opt]
3750
3751 PARSER->SCOPE should be set appropriately before this function is
3752 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3753 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3754 in name lookups.
3755
3756 Sets PARSER->SCOPE to the class (TYPE) or namespace
3757 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3758 it unchanged if there is no nested-name-specifier. Returns the new
3759 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3760
3761 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3762 part of a declaration and/or decl-specifier. */
3763
3764 static tree
3765 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3766 bool typename_keyword_p,
3767 bool check_dependency_p,
3768 bool type_p,
3769 bool is_declaration)
3770 {
3771 bool success = false;
3772 cp_token_position start = 0;
3773 cp_token *token;
3774
3775 /* Remember where the nested-name-specifier starts. */
3776 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3777 {
3778 start = cp_lexer_token_position (parser->lexer, false);
3779 push_deferring_access_checks (dk_deferred);
3780 }
3781
3782 while (true)
3783 {
3784 tree new_scope;
3785 tree old_scope;
3786 tree saved_qualifying_scope;
3787 bool template_keyword_p;
3788
3789 /* Spot cases that cannot be the beginning of a
3790 nested-name-specifier. */
3791 token = cp_lexer_peek_token (parser->lexer);
3792
3793 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3794 the already parsed nested-name-specifier. */
3795 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3796 {
3797 /* Grab the nested-name-specifier and continue the loop. */
3798 cp_parser_pre_parsed_nested_name_specifier (parser);
3799 /* If we originally encountered this nested-name-specifier
3800 with IS_DECLARATION set to false, we will not have
3801 resolved TYPENAME_TYPEs, so we must do so here. */
3802 if (is_declaration
3803 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3804 {
3805 new_scope = resolve_typename_type (parser->scope,
3806 /*only_current_p=*/false);
3807 if (new_scope != error_mark_node)
3808 parser->scope = new_scope;
3809 }
3810 success = true;
3811 continue;
3812 }
3813
3814 /* Spot cases that cannot be the beginning of a
3815 nested-name-specifier. On the second and subsequent times
3816 through the loop, we look for the `template' keyword. */
3817 if (success && token->keyword == RID_TEMPLATE)
3818 ;
3819 /* A template-id can start a nested-name-specifier. */
3820 else if (token->type == CPP_TEMPLATE_ID)
3821 ;
3822 else
3823 {
3824 /* If the next token is not an identifier, then it is
3825 definitely not a class-or-namespace-name. */
3826 if (token->type != CPP_NAME)
3827 break;
3828 /* If the following token is neither a `<' (to begin a
3829 template-id), nor a `::', then we are not looking at a
3830 nested-name-specifier. */
3831 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3832 if (token->type != CPP_SCOPE
3833 && !cp_parser_nth_token_starts_template_argument_list_p
3834 (parser, 2))
3835 break;
3836 }
3837
3838 /* The nested-name-specifier is optional, so we parse
3839 tentatively. */
3840 cp_parser_parse_tentatively (parser);
3841
3842 /* Look for the optional `template' keyword, if this isn't the
3843 first time through the loop. */
3844 if (success)
3845 template_keyword_p = cp_parser_optional_template_keyword (parser);
3846 else
3847 template_keyword_p = false;
3848
3849 /* Save the old scope since the name lookup we are about to do
3850 might destroy it. */
3851 old_scope = parser->scope;
3852 saved_qualifying_scope = parser->qualifying_scope;
3853 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3854 look up names in "X<T>::I" in order to determine that "Y" is
3855 a template. So, if we have a typename at this point, we make
3856 an effort to look through it. */
3857 if (is_declaration
3858 && !typename_keyword_p
3859 && parser->scope
3860 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3861 parser->scope = resolve_typename_type (parser->scope,
3862 /*only_current_p=*/false);
3863 /* Parse the qualifying entity. */
3864 new_scope
3865 = cp_parser_class_or_namespace_name (parser,
3866 typename_keyword_p,
3867 template_keyword_p,
3868 check_dependency_p,
3869 type_p,
3870 is_declaration);
3871 /* Look for the `::' token. */
3872 cp_parser_require (parser, CPP_SCOPE, "`::'");
3873
3874 /* If we found what we wanted, we keep going; otherwise, we're
3875 done. */
3876 if (!cp_parser_parse_definitely (parser))
3877 {
3878 bool error_p = false;
3879
3880 /* Restore the OLD_SCOPE since it was valid before the
3881 failed attempt at finding the last
3882 class-or-namespace-name. */
3883 parser->scope = old_scope;
3884 parser->qualifying_scope = saved_qualifying_scope;
3885 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3886 break;
3887 /* If the next token is an identifier, and the one after
3888 that is a `::', then any valid interpretation would have
3889 found a class-or-namespace-name. */
3890 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3891 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3892 == CPP_SCOPE)
3893 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3894 != CPP_COMPL))
3895 {
3896 token = cp_lexer_consume_token (parser->lexer);
3897 if (!error_p)
3898 {
3899 if (!token->ambiguous_p)
3900 {
3901 tree decl;
3902 tree ambiguous_decls;
3903
3904 decl = cp_parser_lookup_name (parser, token->u.value,
3905 none_type,
3906 /*is_template=*/false,
3907 /*is_namespace=*/false,
3908 /*check_dependency=*/true,
3909 &ambiguous_decls);
3910 if (TREE_CODE (decl) == TEMPLATE_DECL)
3911 error ("%qD used without template parameters", decl);
3912 else if (ambiguous_decls)
3913 {
3914 error ("reference to %qD is ambiguous",
3915 token->u.value);
3916 print_candidates (ambiguous_decls);
3917 decl = error_mark_node;
3918 }
3919 else
3920 cp_parser_name_lookup_error
3921 (parser, token->u.value, decl,
3922 "is not a class or namespace");
3923 }
3924 parser->scope = error_mark_node;
3925 error_p = true;
3926 /* Treat this as a successful nested-name-specifier
3927 due to:
3928
3929 [basic.lookup.qual]
3930
3931 If the name found is not a class-name (clause
3932 _class_) or namespace-name (_namespace.def_), the
3933 program is ill-formed. */
3934 success = true;
3935 }
3936 cp_lexer_consume_token (parser->lexer);
3937 }
3938 break;
3939 }
3940 /* We've found one valid nested-name-specifier. */
3941 success = true;
3942 /* Name lookup always gives us a DECL. */
3943 if (TREE_CODE (new_scope) == TYPE_DECL)
3944 new_scope = TREE_TYPE (new_scope);
3945 /* Uses of "template" must be followed by actual templates. */
3946 if (template_keyword_p
3947 && !(CLASS_TYPE_P (new_scope)
3948 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3949 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3950 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3951 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3952 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3953 == TEMPLATE_ID_EXPR)))
3954 pedwarn (TYPE_P (new_scope)
3955 ? "%qT is not a template"
3956 : "%qD is not a template",
3957 new_scope);
3958 /* If it is a class scope, try to complete it; we are about to
3959 be looking up names inside the class. */
3960 if (TYPE_P (new_scope)
3961 /* Since checking types for dependency can be expensive,
3962 avoid doing it if the type is already complete. */
3963 && !COMPLETE_TYPE_P (new_scope)
3964 /* Do not try to complete dependent types. */
3965 && !dependent_type_p (new_scope))
3966 new_scope = complete_type (new_scope);
3967 /* Make sure we look in the right scope the next time through
3968 the loop. */
3969 parser->scope = new_scope;
3970 }
3971
3972 /* If parsing tentatively, replace the sequence of tokens that makes
3973 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3974 token. That way, should we re-parse the token stream, we will
3975 not have to repeat the effort required to do the parse, nor will
3976 we issue duplicate error messages. */
3977 if (success && start)
3978 {
3979 cp_token *token;
3980
3981 token = cp_lexer_token_at (parser->lexer, start);
3982 /* Reset the contents of the START token. */
3983 token->type = CPP_NESTED_NAME_SPECIFIER;
3984 /* Retrieve any deferred checks. Do not pop this access checks yet
3985 so the memory will not be reclaimed during token replacing below. */
3986 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3987 token->u.tree_check_value->value = parser->scope;
3988 token->u.tree_check_value->checks = get_deferred_access_checks ();
3989 token->u.tree_check_value->qualifying_scope =
3990 parser->qualifying_scope;
3991 token->keyword = RID_MAX;
3992
3993 /* Purge all subsequent tokens. */
3994 cp_lexer_purge_tokens_after (parser->lexer, start);
3995 }
3996
3997 if (start)
3998 pop_to_parent_deferring_access_checks ();
3999
4000 return success ? parser->scope : NULL_TREE;
4001 }
4002
4003 /* Parse a nested-name-specifier. See
4004 cp_parser_nested_name_specifier_opt for details. This function
4005 behaves identically, except that it will an issue an error if no
4006 nested-name-specifier is present. */
4007
4008 static tree
4009 cp_parser_nested_name_specifier (cp_parser *parser,
4010 bool typename_keyword_p,
4011 bool check_dependency_p,
4012 bool type_p,
4013 bool is_declaration)
4014 {
4015 tree scope;
4016
4017 /* Look for the nested-name-specifier. */
4018 scope = cp_parser_nested_name_specifier_opt (parser,
4019 typename_keyword_p,
4020 check_dependency_p,
4021 type_p,
4022 is_declaration);
4023 /* If it was not present, issue an error message. */
4024 if (!scope)
4025 {
4026 cp_parser_error (parser, "expected nested-name-specifier");
4027 parser->scope = NULL_TREE;
4028 }
4029
4030 return scope;
4031 }
4032
4033 /* Parse a class-or-namespace-name.
4034
4035 class-or-namespace-name:
4036 class-name
4037 namespace-name
4038
4039 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4040 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4041 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4042 TYPE_P is TRUE iff the next name should be taken as a class-name,
4043 even the same name is declared to be another entity in the same
4044 scope.
4045
4046 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4047 specified by the class-or-namespace-name. If neither is found the
4048 ERROR_MARK_NODE is returned. */
4049
4050 static tree
4051 cp_parser_class_or_namespace_name (cp_parser *parser,
4052 bool typename_keyword_p,
4053 bool template_keyword_p,
4054 bool check_dependency_p,
4055 bool type_p,
4056 bool is_declaration)
4057 {
4058 tree saved_scope;
4059 tree saved_qualifying_scope;
4060 tree saved_object_scope;
4061 tree scope;
4062 bool only_class_p;
4063
4064 /* Before we try to parse the class-name, we must save away the
4065 current PARSER->SCOPE since cp_parser_class_name will destroy
4066 it. */
4067 saved_scope = parser->scope;
4068 saved_qualifying_scope = parser->qualifying_scope;
4069 saved_object_scope = parser->object_scope;
4070 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4071 there is no need to look for a namespace-name. */
4072 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4073 if (!only_class_p)
4074 cp_parser_parse_tentatively (parser);
4075 scope = cp_parser_class_name (parser,
4076 typename_keyword_p,
4077 template_keyword_p,
4078 type_p ? class_type : none_type,
4079 check_dependency_p,
4080 /*class_head_p=*/false,
4081 is_declaration);
4082 /* If that didn't work, try for a namespace-name. */
4083 if (!only_class_p && !cp_parser_parse_definitely (parser))
4084 {
4085 /* Restore the saved scope. */
4086 parser->scope = saved_scope;
4087 parser->qualifying_scope = saved_qualifying_scope;
4088 parser->object_scope = saved_object_scope;
4089 /* If we are not looking at an identifier followed by the scope
4090 resolution operator, then this is not part of a
4091 nested-name-specifier. (Note that this function is only used
4092 to parse the components of a nested-name-specifier.) */
4093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4094 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4095 return error_mark_node;
4096 scope = cp_parser_namespace_name (parser);
4097 }
4098
4099 return scope;
4100 }
4101
4102 /* Parse a postfix-expression.
4103
4104 postfix-expression:
4105 primary-expression
4106 postfix-expression [ expression ]
4107 postfix-expression ( expression-list [opt] )
4108 simple-type-specifier ( expression-list [opt] )
4109 typename :: [opt] nested-name-specifier identifier
4110 ( expression-list [opt] )
4111 typename :: [opt] nested-name-specifier template [opt] template-id
4112 ( expression-list [opt] )
4113 postfix-expression . template [opt] id-expression
4114 postfix-expression -> template [opt] id-expression
4115 postfix-expression . pseudo-destructor-name
4116 postfix-expression -> pseudo-destructor-name
4117 postfix-expression ++
4118 postfix-expression --
4119 dynamic_cast < type-id > ( expression )
4120 static_cast < type-id > ( expression )
4121 reinterpret_cast < type-id > ( expression )
4122 const_cast < type-id > ( expression )
4123 typeid ( expression )
4124 typeid ( type-id )
4125
4126 GNU Extension:
4127
4128 postfix-expression:
4129 ( type-id ) { initializer-list , [opt] }
4130
4131 This extension is a GNU version of the C99 compound-literal
4132 construct. (The C99 grammar uses `type-name' instead of `type-id',
4133 but they are essentially the same concept.)
4134
4135 If ADDRESS_P is true, the postfix expression is the operand of the
4136 `&' operator. CAST_P is true if this expression is the target of a
4137 cast.
4138
4139 Returns a representation of the expression. */
4140
4141 static tree
4142 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4143 {
4144 cp_token *token;
4145 enum rid keyword;
4146 cp_id_kind idk = CP_ID_KIND_NONE;
4147 tree postfix_expression = NULL_TREE;
4148
4149 /* Peek at the next token. */
4150 token = cp_lexer_peek_token (parser->lexer);
4151 /* Some of the productions are determined by keywords. */
4152 keyword = token->keyword;
4153 switch (keyword)
4154 {
4155 case RID_DYNCAST:
4156 case RID_STATCAST:
4157 case RID_REINTCAST:
4158 case RID_CONSTCAST:
4159 {
4160 tree type;
4161 tree expression;
4162 const char *saved_message;
4163
4164 /* All of these can be handled in the same way from the point
4165 of view of parsing. Begin by consuming the token
4166 identifying the cast. */
4167 cp_lexer_consume_token (parser->lexer);
4168
4169 /* New types cannot be defined in the cast. */
4170 saved_message = parser->type_definition_forbidden_message;
4171 parser->type_definition_forbidden_message
4172 = "types may not be defined in casts";
4173
4174 /* Look for the opening `<'. */
4175 cp_parser_require (parser, CPP_LESS, "`<'");
4176 /* Parse the type to which we are casting. */
4177 type = cp_parser_type_id (parser);
4178 /* Look for the closing `>'. */
4179 cp_parser_require (parser, CPP_GREATER, "`>'");
4180 /* Restore the old message. */
4181 parser->type_definition_forbidden_message = saved_message;
4182
4183 /* And the expression which is being cast. */
4184 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4185 expression = cp_parser_expression (parser, /*cast_p=*/true);
4186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4187
4188 /* Only type conversions to integral or enumeration types
4189 can be used in constant-expressions. */
4190 if (!cast_valid_in_integral_constant_expression_p (type)
4191 && (cp_parser_non_integral_constant_expression
4192 (parser,
4193 "a cast to a type other than an integral or "
4194 "enumeration type")))
4195 return error_mark_node;
4196
4197 switch (keyword)
4198 {
4199 case RID_DYNCAST:
4200 postfix_expression
4201 = build_dynamic_cast (type, expression);
4202 break;
4203 case RID_STATCAST:
4204 postfix_expression
4205 = build_static_cast (type, expression);
4206 break;
4207 case RID_REINTCAST:
4208 postfix_expression
4209 = build_reinterpret_cast (type, expression);
4210 break;
4211 case RID_CONSTCAST:
4212 postfix_expression
4213 = build_const_cast (type, expression);
4214 break;
4215 default:
4216 gcc_unreachable ();
4217 }
4218 }
4219 break;
4220
4221 case RID_TYPEID:
4222 {
4223 tree type;
4224 const char *saved_message;
4225 bool saved_in_type_id_in_expr_p;
4226
4227 /* Consume the `typeid' token. */
4228 cp_lexer_consume_token (parser->lexer);
4229 /* Look for the `(' token. */
4230 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4231 /* Types cannot be defined in a `typeid' expression. */
4232 saved_message = parser->type_definition_forbidden_message;
4233 parser->type_definition_forbidden_message
4234 = "types may not be defined in a `typeid\' expression";
4235 /* We can't be sure yet whether we're looking at a type-id or an
4236 expression. */
4237 cp_parser_parse_tentatively (parser);
4238 /* Try a type-id first. */
4239 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4240 parser->in_type_id_in_expr_p = true;
4241 type = cp_parser_type_id (parser);
4242 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4243 /* Look for the `)' token. Otherwise, we can't be sure that
4244 we're not looking at an expression: consider `typeid (int
4245 (3))', for example. */
4246 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4247 /* If all went well, simply lookup the type-id. */
4248 if (cp_parser_parse_definitely (parser))
4249 postfix_expression = get_typeid (type);
4250 /* Otherwise, fall back to the expression variant. */
4251 else
4252 {
4253 tree expression;
4254
4255 /* Look for an expression. */
4256 expression = cp_parser_expression (parser, /*cast_p=*/false);
4257 /* Compute its typeid. */
4258 postfix_expression = build_typeid (expression);
4259 /* Look for the `)' token. */
4260 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4261 }
4262 /* Restore the saved message. */
4263 parser->type_definition_forbidden_message = saved_message;
4264 /* `typeid' may not appear in an integral constant expression. */
4265 if (cp_parser_non_integral_constant_expression(parser,
4266 "`typeid' operator"))
4267 return error_mark_node;
4268 }
4269 break;
4270
4271 case RID_TYPENAME:
4272 {
4273 tree type;
4274 /* The syntax permitted here is the same permitted for an
4275 elaborated-type-specifier. */
4276 type = cp_parser_elaborated_type_specifier (parser,
4277 /*is_friend=*/false,
4278 /*is_declaration=*/false);
4279 postfix_expression = cp_parser_functional_cast (parser, type);
4280 }
4281 break;
4282
4283 default:
4284 {
4285 tree type;
4286
4287 /* If the next thing is a simple-type-specifier, we may be
4288 looking at a functional cast. We could also be looking at
4289 an id-expression. So, we try the functional cast, and if
4290 that doesn't work we fall back to the primary-expression. */
4291 cp_parser_parse_tentatively (parser);
4292 /* Look for the simple-type-specifier. */
4293 type = cp_parser_simple_type_specifier (parser,
4294 /*decl_specs=*/NULL,
4295 CP_PARSER_FLAGS_NONE);
4296 /* Parse the cast itself. */
4297 if (!cp_parser_error_occurred (parser))
4298 postfix_expression
4299 = cp_parser_functional_cast (parser, type);
4300 /* If that worked, we're done. */
4301 if (cp_parser_parse_definitely (parser))
4302 break;
4303
4304 /* If the functional-cast didn't work out, try a
4305 compound-literal. */
4306 if (cp_parser_allow_gnu_extensions_p (parser)
4307 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4308 {
4309 VEC(constructor_elt,gc) *initializer_list = NULL;
4310 bool saved_in_type_id_in_expr_p;
4311
4312 cp_parser_parse_tentatively (parser);
4313 /* Consume the `('. */
4314 cp_lexer_consume_token (parser->lexer);
4315 /* Parse the type. */
4316 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4317 parser->in_type_id_in_expr_p = true;
4318 type = cp_parser_type_id (parser);
4319 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4320 /* Look for the `)'. */
4321 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4322 /* Look for the `{'. */
4323 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4324 /* If things aren't going well, there's no need to
4325 keep going. */
4326 if (!cp_parser_error_occurred (parser))
4327 {
4328 bool non_constant_p;
4329 /* Parse the initializer-list. */
4330 initializer_list
4331 = cp_parser_initializer_list (parser, &non_constant_p);
4332 /* Allow a trailing `,'. */
4333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4334 cp_lexer_consume_token (parser->lexer);
4335 /* Look for the final `}'. */
4336 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4337 }
4338 /* If that worked, we're definitely looking at a
4339 compound-literal expression. */
4340 if (cp_parser_parse_definitely (parser))
4341 {
4342 /* Warn the user that a compound literal is not
4343 allowed in standard C++. */
4344 if (pedantic)
4345 pedwarn ("ISO C++ forbids compound-literals");
4346 /* For simplicity, we disallow compound literals in
4347 constant-expressions. We could
4348 allow compound literals of integer type, whose
4349 initializer was a constant, in constant
4350 expressions. Permitting that usage, as a further
4351 extension, would not change the meaning of any
4352 currently accepted programs. (Of course, as
4353 compound literals are not part of ISO C++, the
4354 standard has nothing to say.) */
4355 if (cp_parser_non_integral_constant_expression
4356 (parser, "non-constant compound literals"))
4357 {
4358 postfix_expression = error_mark_node;
4359 break;
4360 }
4361 /* Form the representation of the compound-literal. */
4362 postfix_expression
4363 = finish_compound_literal (type, initializer_list);
4364 break;
4365 }
4366 }
4367
4368 /* It must be a primary-expression. */
4369 postfix_expression
4370 = cp_parser_primary_expression (parser, address_p, cast_p,
4371 /*template_arg_p=*/false,
4372 &idk);
4373 }
4374 break;
4375 }
4376
4377 /* Keep looping until the postfix-expression is complete. */
4378 while (true)
4379 {
4380 if (idk == CP_ID_KIND_UNQUALIFIED
4381 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4382 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4383 /* It is not a Koenig lookup function call. */
4384 postfix_expression
4385 = unqualified_name_lookup_error (postfix_expression);
4386
4387 /* Peek at the next token. */
4388 token = cp_lexer_peek_token (parser->lexer);
4389
4390 switch (token->type)
4391 {
4392 case CPP_OPEN_SQUARE:
4393 postfix_expression
4394 = cp_parser_postfix_open_square_expression (parser,
4395 postfix_expression,
4396 false);
4397 idk = CP_ID_KIND_NONE;
4398 break;
4399
4400 case CPP_OPEN_PAREN:
4401 /* postfix-expression ( expression-list [opt] ) */
4402 {
4403 bool koenig_p;
4404 bool is_builtin_constant_p;
4405 bool saved_integral_constant_expression_p = false;
4406 bool saved_non_integral_constant_expression_p = false;
4407 tree args;
4408
4409 is_builtin_constant_p
4410 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4411 if (is_builtin_constant_p)
4412 {
4413 /* The whole point of __builtin_constant_p is to allow
4414 non-constant expressions to appear as arguments. */
4415 saved_integral_constant_expression_p
4416 = parser->integral_constant_expression_p;
4417 saved_non_integral_constant_expression_p
4418 = parser->non_integral_constant_expression_p;
4419 parser->integral_constant_expression_p = false;
4420 }
4421 args = (cp_parser_parenthesized_expression_list
4422 (parser, /*is_attribute_list=*/false,
4423 /*cast_p=*/false, /*allow_expansion_p=*/true,
4424 /*non_constant_p=*/NULL));
4425 if (is_builtin_constant_p)
4426 {
4427 parser->integral_constant_expression_p
4428 = saved_integral_constant_expression_p;
4429 parser->non_integral_constant_expression_p
4430 = saved_non_integral_constant_expression_p;
4431 }
4432
4433 if (args == error_mark_node)
4434 {
4435 postfix_expression = error_mark_node;
4436 break;
4437 }
4438
4439 /* Function calls are not permitted in
4440 constant-expressions. */
4441 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4442 && cp_parser_non_integral_constant_expression (parser,
4443 "a function call"))
4444 {
4445 postfix_expression = error_mark_node;
4446 break;
4447 }
4448
4449 koenig_p = false;
4450 if (idk == CP_ID_KIND_UNQUALIFIED)
4451 {
4452 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4453 {
4454 if (args)
4455 {
4456 koenig_p = true;
4457 postfix_expression
4458 = perform_koenig_lookup (postfix_expression, args);
4459 }
4460 else
4461 postfix_expression
4462 = unqualified_fn_lookup_error (postfix_expression);
4463 }
4464 /* We do not perform argument-dependent lookup if
4465 normal lookup finds a non-function, in accordance
4466 with the expected resolution of DR 218. */
4467 else if (args && is_overloaded_fn (postfix_expression))
4468 {
4469 tree fn = get_first_fn (postfix_expression);
4470
4471 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4472 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4473
4474 /* Only do argument dependent lookup if regular
4475 lookup does not find a set of member functions.
4476 [basic.lookup.koenig]/2a */
4477 if (!DECL_FUNCTION_MEMBER_P (fn))
4478 {
4479 koenig_p = true;
4480 postfix_expression
4481 = perform_koenig_lookup (postfix_expression, args);
4482 }
4483 }
4484 }
4485
4486 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4487 {
4488 tree instance = TREE_OPERAND (postfix_expression, 0);
4489 tree fn = TREE_OPERAND (postfix_expression, 1);
4490
4491 if (processing_template_decl
4492 && (type_dependent_expression_p (instance)
4493 || (!BASELINK_P (fn)
4494 && TREE_CODE (fn) != FIELD_DECL)
4495 || type_dependent_expression_p (fn)
4496 || any_type_dependent_arguments_p (args)))
4497 {
4498 postfix_expression
4499 = build_nt_call_list (postfix_expression, args);
4500 break;
4501 }
4502
4503 if (BASELINK_P (fn))
4504 postfix_expression
4505 = (build_new_method_call
4506 (instance, fn, args, NULL_TREE,
4507 (idk == CP_ID_KIND_QUALIFIED
4508 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4509 /*fn_p=*/NULL));
4510 else
4511 postfix_expression
4512 = finish_call_expr (postfix_expression, args,
4513 /*disallow_virtual=*/false,
4514 /*koenig_p=*/false);
4515 }
4516 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4517 || TREE_CODE (postfix_expression) == MEMBER_REF
4518 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4519 postfix_expression = (build_offset_ref_call_from_tree
4520 (postfix_expression, args));
4521 else if (idk == CP_ID_KIND_QUALIFIED)
4522 /* A call to a static class member, or a namespace-scope
4523 function. */
4524 postfix_expression
4525 = finish_call_expr (postfix_expression, args,
4526 /*disallow_virtual=*/true,
4527 koenig_p);
4528 else
4529 /* All other function calls. */
4530 postfix_expression
4531 = finish_call_expr (postfix_expression, args,
4532 /*disallow_virtual=*/false,
4533 koenig_p);
4534
4535 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4536 idk = CP_ID_KIND_NONE;
4537 }
4538 break;
4539
4540 case CPP_DOT:
4541 case CPP_DEREF:
4542 /* postfix-expression . template [opt] id-expression
4543 postfix-expression . pseudo-destructor-name
4544 postfix-expression -> template [opt] id-expression
4545 postfix-expression -> pseudo-destructor-name */
4546
4547 /* Consume the `.' or `->' operator. */
4548 cp_lexer_consume_token (parser->lexer);
4549
4550 postfix_expression
4551 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4552 postfix_expression,
4553 false, &idk);
4554 break;
4555
4556 case CPP_PLUS_PLUS:
4557 /* postfix-expression ++ */
4558 /* Consume the `++' token. */
4559 cp_lexer_consume_token (parser->lexer);
4560 /* Generate a representation for the complete expression. */
4561 postfix_expression
4562 = finish_increment_expr (postfix_expression,
4563 POSTINCREMENT_EXPR);
4564 /* Increments may not appear in constant-expressions. */
4565 if (cp_parser_non_integral_constant_expression (parser,
4566 "an increment"))
4567 postfix_expression = error_mark_node;
4568 idk = CP_ID_KIND_NONE;
4569 break;
4570
4571 case CPP_MINUS_MINUS:
4572 /* postfix-expression -- */
4573 /* Consume the `--' token. */
4574 cp_lexer_consume_token (parser->lexer);
4575 /* Generate a representation for the complete expression. */
4576 postfix_expression
4577 = finish_increment_expr (postfix_expression,
4578 POSTDECREMENT_EXPR);
4579 /* Decrements may not appear in constant-expressions. */
4580 if (cp_parser_non_integral_constant_expression (parser,
4581 "a decrement"))
4582 postfix_expression = error_mark_node;
4583 idk = CP_ID_KIND_NONE;
4584 break;
4585
4586 default:
4587 return postfix_expression;
4588 }
4589 }
4590
4591 /* We should never get here. */
4592 gcc_unreachable ();
4593 return error_mark_node;
4594 }
4595
4596 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4597 by cp_parser_builtin_offsetof. We're looking for
4598
4599 postfix-expression [ expression ]
4600
4601 FOR_OFFSETOF is set if we're being called in that context, which
4602 changes how we deal with integer constant expressions. */
4603
4604 static tree
4605 cp_parser_postfix_open_square_expression (cp_parser *parser,
4606 tree postfix_expression,
4607 bool for_offsetof)
4608 {
4609 tree index;
4610
4611 /* Consume the `[' token. */
4612 cp_lexer_consume_token (parser->lexer);
4613
4614 /* Parse the index expression. */
4615 /* ??? For offsetof, there is a question of what to allow here. If
4616 offsetof is not being used in an integral constant expression context,
4617 then we *could* get the right answer by computing the value at runtime.
4618 If we are in an integral constant expression context, then we might
4619 could accept any constant expression; hard to say without analysis.
4620 Rather than open the barn door too wide right away, allow only integer
4621 constant expressions here. */
4622 if (for_offsetof)
4623 index = cp_parser_constant_expression (parser, false, NULL);
4624 else
4625 index = cp_parser_expression (parser, /*cast_p=*/false);
4626
4627 /* Look for the closing `]'. */
4628 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4629
4630 /* Build the ARRAY_REF. */
4631 postfix_expression = grok_array_decl (postfix_expression, index);
4632
4633 /* When not doing offsetof, array references are not permitted in
4634 constant-expressions. */
4635 if (!for_offsetof
4636 && (cp_parser_non_integral_constant_expression
4637 (parser, "an array reference")))
4638 postfix_expression = error_mark_node;
4639
4640 return postfix_expression;
4641 }
4642
4643 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4644 by cp_parser_builtin_offsetof. We're looking for
4645
4646 postfix-expression . template [opt] id-expression
4647 postfix-expression . pseudo-destructor-name
4648 postfix-expression -> template [opt] id-expression
4649 postfix-expression -> pseudo-destructor-name
4650
4651 FOR_OFFSETOF is set if we're being called in that context. That sorta
4652 limits what of the above we'll actually accept, but nevermind.
4653 TOKEN_TYPE is the "." or "->" token, which will already have been
4654 removed from the stream. */
4655
4656 static tree
4657 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4658 enum cpp_ttype token_type,
4659 tree postfix_expression,
4660 bool for_offsetof, cp_id_kind *idk)
4661 {
4662 tree name;
4663 bool dependent_p;
4664 bool pseudo_destructor_p;
4665 tree scope = NULL_TREE;
4666
4667 /* If this is a `->' operator, dereference the pointer. */
4668 if (token_type == CPP_DEREF)
4669 postfix_expression = build_x_arrow (postfix_expression);
4670 /* Check to see whether or not the expression is type-dependent. */
4671 dependent_p = type_dependent_expression_p (postfix_expression);
4672 /* The identifier following the `->' or `.' is not qualified. */
4673 parser->scope = NULL_TREE;
4674 parser->qualifying_scope = NULL_TREE;
4675 parser->object_scope = NULL_TREE;
4676 *idk = CP_ID_KIND_NONE;
4677 /* Enter the scope corresponding to the type of the object
4678 given by the POSTFIX_EXPRESSION. */
4679 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4680 {
4681 scope = TREE_TYPE (postfix_expression);
4682 /* According to the standard, no expression should ever have
4683 reference type. Unfortunately, we do not currently match
4684 the standard in this respect in that our internal representation
4685 of an expression may have reference type even when the standard
4686 says it does not. Therefore, we have to manually obtain the
4687 underlying type here. */
4688 scope = non_reference (scope);
4689 /* The type of the POSTFIX_EXPRESSION must be complete. */
4690 if (scope == unknown_type_node)
4691 {
4692 error ("%qE does not have class type", postfix_expression);
4693 scope = NULL_TREE;
4694 }
4695 else
4696 scope = complete_type_or_else (scope, NULL_TREE);
4697 /* Let the name lookup machinery know that we are processing a
4698 class member access expression. */
4699 parser->context->object_type = scope;
4700 /* If something went wrong, we want to be able to discern that case,
4701 as opposed to the case where there was no SCOPE due to the type
4702 of expression being dependent. */
4703 if (!scope)
4704 scope = error_mark_node;
4705 /* If the SCOPE was erroneous, make the various semantic analysis
4706 functions exit quickly -- and without issuing additional error
4707 messages. */
4708 if (scope == error_mark_node)
4709 postfix_expression = error_mark_node;
4710 }
4711
4712 /* Assume this expression is not a pseudo-destructor access. */
4713 pseudo_destructor_p = false;
4714
4715 /* If the SCOPE is a scalar type, then, if this is a valid program,
4716 we must be looking at a pseudo-destructor-name. */
4717 if (scope && SCALAR_TYPE_P (scope))
4718 {
4719 tree s;
4720 tree type;
4721
4722 cp_parser_parse_tentatively (parser);
4723 /* Parse the pseudo-destructor-name. */
4724 s = NULL_TREE;
4725 cp_parser_pseudo_destructor_name (parser, &s, &type);
4726 if (cp_parser_parse_definitely (parser))
4727 {
4728 pseudo_destructor_p = true;
4729 postfix_expression
4730 = finish_pseudo_destructor_expr (postfix_expression,
4731 s, TREE_TYPE (type));
4732 }
4733 }
4734
4735 if (!pseudo_destructor_p)
4736 {
4737 /* If the SCOPE is not a scalar type, we are looking at an
4738 ordinary class member access expression, rather than a
4739 pseudo-destructor-name. */
4740 bool template_p;
4741 /* Parse the id-expression. */
4742 name = (cp_parser_id_expression
4743 (parser,
4744 cp_parser_optional_template_keyword (parser),
4745 /*check_dependency_p=*/true,
4746 &template_p,
4747 /*declarator_p=*/false,
4748 /*optional_p=*/false));
4749 /* In general, build a SCOPE_REF if the member name is qualified.
4750 However, if the name was not dependent and has already been
4751 resolved; there is no need to build the SCOPE_REF. For example;
4752
4753 struct X { void f(); };
4754 template <typename T> void f(T* t) { t->X::f(); }
4755
4756 Even though "t" is dependent, "X::f" is not and has been resolved
4757 to a BASELINK; there is no need to include scope information. */
4758
4759 /* But we do need to remember that there was an explicit scope for
4760 virtual function calls. */
4761 if (parser->scope)
4762 *idk = CP_ID_KIND_QUALIFIED;
4763
4764 /* If the name is a template-id that names a type, we will get a
4765 TYPE_DECL here. That is invalid code. */
4766 if (TREE_CODE (name) == TYPE_DECL)
4767 {
4768 error ("invalid use of %qD", name);
4769 postfix_expression = error_mark_node;
4770 }
4771 else
4772 {
4773 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4774 {
4775 name = build_qualified_name (/*type=*/NULL_TREE,
4776 parser->scope,
4777 name,
4778 template_p);
4779 parser->scope = NULL_TREE;
4780 parser->qualifying_scope = NULL_TREE;
4781 parser->object_scope = NULL_TREE;
4782 }
4783 if (scope && name && BASELINK_P (name))
4784 adjust_result_of_qualified_name_lookup
4785 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4786 postfix_expression
4787 = finish_class_member_access_expr (postfix_expression, name,
4788 template_p);
4789 }
4790 }
4791
4792 /* We no longer need to look up names in the scope of the object on
4793 the left-hand side of the `.' or `->' operator. */
4794 parser->context->object_type = NULL_TREE;
4795
4796 /* Outside of offsetof, these operators may not appear in
4797 constant-expressions. */
4798 if (!for_offsetof
4799 && (cp_parser_non_integral_constant_expression
4800 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4801 postfix_expression = error_mark_node;
4802
4803 return postfix_expression;
4804 }
4805
4806 /* Parse a parenthesized expression-list.
4807
4808 expression-list:
4809 assignment-expression
4810 expression-list, assignment-expression
4811
4812 attribute-list:
4813 expression-list
4814 identifier
4815 identifier, expression-list
4816
4817 CAST_P is true if this expression is the target of a cast.
4818
4819 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4820 argument pack.
4821
4822 Returns a TREE_LIST. The TREE_VALUE of each node is a
4823 representation of an assignment-expression. Note that a TREE_LIST
4824 is returned even if there is only a single expression in the list.
4825 error_mark_node is returned if the ( and or ) are
4826 missing. NULL_TREE is returned on no expressions. The parentheses
4827 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4828 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4829 indicates whether or not all of the expressions in the list were
4830 constant. */
4831
4832 static tree
4833 cp_parser_parenthesized_expression_list (cp_parser* parser,
4834 bool is_attribute_list,
4835 bool cast_p,
4836 bool allow_expansion_p,
4837 bool *non_constant_p)
4838 {
4839 tree expression_list = NULL_TREE;
4840 bool fold_expr_p = is_attribute_list;
4841 tree identifier = NULL_TREE;
4842
4843 /* Assume all the expressions will be constant. */
4844 if (non_constant_p)
4845 *non_constant_p = false;
4846
4847 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4848 return error_mark_node;
4849
4850 /* Consume expressions until there are no more. */
4851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4852 while (true)
4853 {
4854 tree expr;
4855
4856 /* At the beginning of attribute lists, check to see if the
4857 next token is an identifier. */
4858 if (is_attribute_list
4859 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4860 {
4861 cp_token *token;
4862
4863 /* Consume the identifier. */
4864 token = cp_lexer_consume_token (parser->lexer);
4865 /* Save the identifier. */
4866 identifier = token->u.value;
4867 }
4868 else
4869 {
4870 /* Parse the next assignment-expression. */
4871 if (non_constant_p)
4872 {
4873 bool expr_non_constant_p;
4874 expr = (cp_parser_constant_expression
4875 (parser, /*allow_non_constant_p=*/true,
4876 &expr_non_constant_p));
4877 if (expr_non_constant_p)
4878 *non_constant_p = true;
4879 }
4880 else
4881 expr = cp_parser_assignment_expression (parser, cast_p);
4882
4883 if (fold_expr_p)
4884 expr = fold_non_dependent_expr (expr);
4885
4886 /* If we have an ellipsis, then this is an expression
4887 expansion. */
4888 if (allow_expansion_p
4889 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4890 {
4891 /* Consume the `...'. */
4892 cp_lexer_consume_token (parser->lexer);
4893
4894 /* Build the argument pack. */
4895 expr = make_pack_expansion (expr);
4896 }
4897
4898 /* Add it to the list. We add error_mark_node
4899 expressions to the list, so that we can still tell if
4900 the correct form for a parenthesized expression-list
4901 is found. That gives better errors. */
4902 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4903
4904 if (expr == error_mark_node)
4905 goto skip_comma;
4906 }
4907
4908 /* After the first item, attribute lists look the same as
4909 expression lists. */
4910 is_attribute_list = false;
4911
4912 get_comma:;
4913 /* If the next token isn't a `,', then we are done. */
4914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4915 break;
4916
4917 /* Otherwise, consume the `,' and keep going. */
4918 cp_lexer_consume_token (parser->lexer);
4919 }
4920
4921 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4922 {
4923 int ending;
4924
4925 skip_comma:;
4926 /* We try and resync to an unnested comma, as that will give the
4927 user better diagnostics. */
4928 ending = cp_parser_skip_to_closing_parenthesis (parser,
4929 /*recovering=*/true,
4930 /*or_comma=*/true,
4931 /*consume_paren=*/true);
4932 if (ending < 0)
4933 goto get_comma;
4934 if (!ending)
4935 return error_mark_node;
4936 }
4937
4938 /* We built up the list in reverse order so we must reverse it now. */
4939 expression_list = nreverse (expression_list);
4940 if (identifier)
4941 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4942
4943 return expression_list;
4944 }
4945
4946 /* Parse a pseudo-destructor-name.
4947
4948 pseudo-destructor-name:
4949 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4950 :: [opt] nested-name-specifier template template-id :: ~ type-name
4951 :: [opt] nested-name-specifier [opt] ~ type-name
4952
4953 If either of the first two productions is used, sets *SCOPE to the
4954 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4955 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4956 or ERROR_MARK_NODE if the parse fails. */
4957
4958 static void
4959 cp_parser_pseudo_destructor_name (cp_parser* parser,
4960 tree* scope,
4961 tree* type)
4962 {
4963 bool nested_name_specifier_p;
4964
4965 /* Assume that things will not work out. */
4966 *type = error_mark_node;
4967
4968 /* Look for the optional `::' operator. */
4969 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4970 /* Look for the optional nested-name-specifier. */
4971 nested_name_specifier_p
4972 = (cp_parser_nested_name_specifier_opt (parser,
4973 /*typename_keyword_p=*/false,
4974 /*check_dependency_p=*/true,
4975 /*type_p=*/false,
4976 /*is_declaration=*/true)
4977 != NULL_TREE);
4978 /* Now, if we saw a nested-name-specifier, we might be doing the
4979 second production. */
4980 if (nested_name_specifier_p
4981 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4982 {
4983 /* Consume the `template' keyword. */
4984 cp_lexer_consume_token (parser->lexer);
4985 /* Parse the template-id. */
4986 cp_parser_template_id (parser,
4987 /*template_keyword_p=*/true,
4988 /*check_dependency_p=*/false,
4989 /*is_declaration=*/true);
4990 /* Look for the `::' token. */
4991 cp_parser_require (parser, CPP_SCOPE, "`::'");
4992 }
4993 /* If the next token is not a `~', then there might be some
4994 additional qualification. */
4995 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4996 {
4997 /* Look for the type-name. */
4998 *scope = TREE_TYPE (cp_parser_type_name (parser));
4999
5000 if (*scope == error_mark_node)
5001 return;
5002
5003 /* If we don't have ::~, then something has gone wrong. Since
5004 the only caller of this function is looking for something
5005 after `.' or `->' after a scalar type, most likely the
5006 program is trying to get a member of a non-aggregate
5007 type. */
5008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5009 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5010 {
5011 cp_parser_error (parser, "request for member of non-aggregate type");
5012 return;
5013 }
5014
5015 /* Look for the `::' token. */
5016 cp_parser_require (parser, CPP_SCOPE, "`::'");
5017 }
5018 else
5019 *scope = NULL_TREE;
5020
5021 /* Look for the `~'. */
5022 cp_parser_require (parser, CPP_COMPL, "`~'");
5023 /* Look for the type-name again. We are not responsible for
5024 checking that it matches the first type-name. */
5025 *type = cp_parser_type_name (parser);
5026 }
5027
5028 /* Parse a unary-expression.
5029
5030 unary-expression:
5031 postfix-expression
5032 ++ cast-expression
5033 -- cast-expression
5034 unary-operator cast-expression
5035 sizeof unary-expression
5036 sizeof ( type-id )
5037 new-expression
5038 delete-expression
5039
5040 GNU Extensions:
5041
5042 unary-expression:
5043 __extension__ cast-expression
5044 __alignof__ unary-expression
5045 __alignof__ ( type-id )
5046 __real__ cast-expression
5047 __imag__ cast-expression
5048 && identifier
5049
5050 ADDRESS_P is true iff the unary-expression is appearing as the
5051 operand of the `&' operator. CAST_P is true if this expression is
5052 the target of a cast.
5053
5054 Returns a representation of the expression. */
5055
5056 static tree
5057 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5058 {
5059 cp_token *token;
5060 enum tree_code unary_operator;
5061
5062 /* Peek at the next token. */
5063 token = cp_lexer_peek_token (parser->lexer);
5064 /* Some keywords give away the kind of expression. */
5065 if (token->type == CPP_KEYWORD)
5066 {
5067 enum rid keyword = token->keyword;
5068
5069 switch (keyword)
5070 {
5071 case RID_ALIGNOF:
5072 case RID_SIZEOF:
5073 {
5074 tree operand;
5075 enum tree_code op;
5076
5077 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5078 /* Consume the token. */
5079 cp_lexer_consume_token (parser->lexer);
5080 /* Parse the operand. */
5081 operand = cp_parser_sizeof_operand (parser, keyword);
5082
5083 if (TYPE_P (operand))
5084 return cxx_sizeof_or_alignof_type (operand, op, true);
5085 else
5086 return cxx_sizeof_or_alignof_expr (operand, op);
5087 }
5088
5089 case RID_NEW:
5090 return cp_parser_new_expression (parser);
5091
5092 case RID_DELETE:
5093 return cp_parser_delete_expression (parser);
5094
5095 case RID_EXTENSION:
5096 {
5097 /* The saved value of the PEDANTIC flag. */
5098 int saved_pedantic;
5099 tree expr;
5100
5101 /* Save away the PEDANTIC flag. */
5102 cp_parser_extension_opt (parser, &saved_pedantic);
5103 /* Parse the cast-expression. */
5104 expr = cp_parser_simple_cast_expression (parser);
5105 /* Restore the PEDANTIC flag. */
5106 pedantic = saved_pedantic;
5107
5108 return expr;
5109 }
5110
5111 case RID_REALPART:
5112 case RID_IMAGPART:
5113 {
5114 tree expression;
5115
5116 /* Consume the `__real__' or `__imag__' token. */
5117 cp_lexer_consume_token (parser->lexer);
5118 /* Parse the cast-expression. */
5119 expression = cp_parser_simple_cast_expression (parser);
5120 /* Create the complete representation. */
5121 return build_x_unary_op ((keyword == RID_REALPART
5122 ? REALPART_EXPR : IMAGPART_EXPR),
5123 expression);
5124 }
5125 break;
5126
5127 default:
5128 break;
5129 }
5130 }
5131
5132 /* Look for the `:: new' and `:: delete', which also signal the
5133 beginning of a new-expression, or delete-expression,
5134 respectively. If the next token is `::', then it might be one of
5135 these. */
5136 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5137 {
5138 enum rid keyword;
5139
5140 /* See if the token after the `::' is one of the keywords in
5141 which we're interested. */
5142 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5143 /* If it's `new', we have a new-expression. */
5144 if (keyword == RID_NEW)
5145 return cp_parser_new_expression (parser);
5146 /* Similarly, for `delete'. */
5147 else if (keyword == RID_DELETE)
5148 return cp_parser_delete_expression (parser);
5149 }
5150
5151 /* Look for a unary operator. */
5152 unary_operator = cp_parser_unary_operator (token);
5153 /* The `++' and `--' operators can be handled similarly, even though
5154 they are not technically unary-operators in the grammar. */
5155 if (unary_operator == ERROR_MARK)
5156 {
5157 if (token->type == CPP_PLUS_PLUS)
5158 unary_operator = PREINCREMENT_EXPR;
5159 else if (token->type == CPP_MINUS_MINUS)
5160 unary_operator = PREDECREMENT_EXPR;
5161 /* Handle the GNU address-of-label extension. */
5162 else if (cp_parser_allow_gnu_extensions_p (parser)
5163 && token->type == CPP_AND_AND)
5164 {
5165 tree identifier;
5166
5167 /* Consume the '&&' token. */
5168 cp_lexer_consume_token (parser->lexer);
5169 /* Look for the identifier. */
5170 identifier = cp_parser_identifier (parser);
5171 /* Create an expression representing the address. */
5172 return finish_label_address_expr (identifier);
5173 }
5174 }
5175 if (unary_operator != ERROR_MARK)
5176 {
5177 tree cast_expression;
5178 tree expression = error_mark_node;
5179 const char *non_constant_p = NULL;
5180
5181 /* Consume the operator token. */
5182 token = cp_lexer_consume_token (parser->lexer);
5183 /* Parse the cast-expression. */
5184 cast_expression
5185 = cp_parser_cast_expression (parser,
5186 unary_operator == ADDR_EXPR,
5187 /*cast_p=*/false);
5188 /* Now, build an appropriate representation. */
5189 switch (unary_operator)
5190 {
5191 case INDIRECT_REF:
5192 non_constant_p = "`*'";
5193 expression = build_x_indirect_ref (cast_expression, "unary *");
5194 break;
5195
5196 case ADDR_EXPR:
5197 non_constant_p = "`&'";
5198 /* Fall through. */
5199 case BIT_NOT_EXPR:
5200 expression = build_x_unary_op (unary_operator, cast_expression);
5201 break;
5202
5203 case PREINCREMENT_EXPR:
5204 case PREDECREMENT_EXPR:
5205 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5206 ? "`++'" : "`--'");
5207 /* Fall through. */
5208 case UNARY_PLUS_EXPR:
5209 case NEGATE_EXPR:
5210 case TRUTH_NOT_EXPR:
5211 expression = finish_unary_op_expr (unary_operator, cast_expression);
5212 break;
5213
5214 default:
5215 gcc_unreachable ();
5216 }
5217
5218 if (non_constant_p
5219 && cp_parser_non_integral_constant_expression (parser,
5220 non_constant_p))
5221 expression = error_mark_node;
5222
5223 return expression;
5224 }
5225
5226 return cp_parser_postfix_expression (parser, address_p, cast_p);
5227 }
5228
5229 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5230 unary-operator, the corresponding tree code is returned. */
5231
5232 static enum tree_code
5233 cp_parser_unary_operator (cp_token* token)
5234 {
5235 switch (token->type)
5236 {
5237 case CPP_MULT:
5238 return INDIRECT_REF;
5239
5240 case CPP_AND:
5241 return ADDR_EXPR;
5242
5243 case CPP_PLUS:
5244 return UNARY_PLUS_EXPR;
5245
5246 case CPP_MINUS:
5247 return NEGATE_EXPR;
5248
5249 case CPP_NOT:
5250 return TRUTH_NOT_EXPR;
5251
5252 case CPP_COMPL:
5253 return BIT_NOT_EXPR;
5254
5255 default:
5256 return ERROR_MARK;
5257 }
5258 }
5259
5260 /* Parse a new-expression.
5261
5262 new-expression:
5263 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5264 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5265
5266 Returns a representation of the expression. */
5267
5268 static tree
5269 cp_parser_new_expression (cp_parser* parser)
5270 {
5271 bool global_scope_p;
5272 tree placement;
5273 tree type;
5274 tree initializer;
5275 tree nelts;
5276
5277 /* Look for the optional `::' operator. */
5278 global_scope_p
5279 = (cp_parser_global_scope_opt (parser,
5280 /*current_scope_valid_p=*/false)
5281 != NULL_TREE);
5282 /* Look for the `new' operator. */
5283 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5284 /* There's no easy way to tell a new-placement from the
5285 `( type-id )' construct. */
5286 cp_parser_parse_tentatively (parser);
5287 /* Look for a new-placement. */
5288 placement = cp_parser_new_placement (parser);
5289 /* If that didn't work out, there's no new-placement. */
5290 if (!cp_parser_parse_definitely (parser))
5291 placement = NULL_TREE;
5292
5293 /* If the next token is a `(', then we have a parenthesized
5294 type-id. */
5295 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5296 {
5297 /* Consume the `('. */
5298 cp_lexer_consume_token (parser->lexer);
5299 /* Parse the type-id. */
5300 type = cp_parser_type_id (parser);
5301 /* Look for the closing `)'. */
5302 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5303 /* There should not be a direct-new-declarator in this production,
5304 but GCC used to allowed this, so we check and emit a sensible error
5305 message for this case. */
5306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5307 {
5308 error ("array bound forbidden after parenthesized type-id");
5309 inform ("try removing the parentheses around the type-id");
5310 cp_parser_direct_new_declarator (parser);
5311 }
5312 nelts = NULL_TREE;
5313 }
5314 /* Otherwise, there must be a new-type-id. */
5315 else
5316 type = cp_parser_new_type_id (parser, &nelts);
5317
5318 /* If the next token is a `(', then we have a new-initializer. */
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5320 initializer = cp_parser_new_initializer (parser);
5321 else
5322 initializer = NULL_TREE;
5323
5324 /* A new-expression may not appear in an integral constant
5325 expression. */
5326 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5327 return error_mark_node;
5328
5329 /* Create a representation of the new-expression. */
5330 return build_new (placement, type, nelts, initializer, global_scope_p);
5331 }
5332
5333 /* Parse a new-placement.
5334
5335 new-placement:
5336 ( expression-list )
5337
5338 Returns the same representation as for an expression-list. */
5339
5340 static tree
5341 cp_parser_new_placement (cp_parser* parser)
5342 {
5343 tree expression_list;
5344
5345 /* Parse the expression-list. */
5346 expression_list = (cp_parser_parenthesized_expression_list
5347 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5348 /*non_constant_p=*/NULL));
5349
5350 return expression_list;
5351 }
5352
5353 /* Parse a new-type-id.
5354
5355 new-type-id:
5356 type-specifier-seq new-declarator [opt]
5357
5358 Returns the TYPE allocated. If the new-type-id indicates an array
5359 type, *NELTS is set to the number of elements in the last array
5360 bound; the TYPE will not include the last array bound. */
5361
5362 static tree
5363 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5364 {
5365 cp_decl_specifier_seq type_specifier_seq;
5366 cp_declarator *new_declarator;
5367 cp_declarator *declarator;
5368 cp_declarator *outer_declarator;
5369 const char *saved_message;
5370 tree type;
5371
5372 /* The type-specifier sequence must not contain type definitions.
5373 (It cannot contain declarations of new types either, but if they
5374 are not definitions we will catch that because they are not
5375 complete.) */
5376 saved_message = parser->type_definition_forbidden_message;
5377 parser->type_definition_forbidden_message
5378 = "types may not be defined in a new-type-id";
5379 /* Parse the type-specifier-seq. */
5380 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5381 &type_specifier_seq);
5382 /* Restore the old message. */
5383 parser->type_definition_forbidden_message = saved_message;
5384 /* Parse the new-declarator. */
5385 new_declarator = cp_parser_new_declarator_opt (parser);
5386
5387 /* Determine the number of elements in the last array dimension, if
5388 any. */
5389 *nelts = NULL_TREE;
5390 /* Skip down to the last array dimension. */
5391 declarator = new_declarator;
5392 outer_declarator = NULL;
5393 while (declarator && (declarator->kind == cdk_pointer
5394 || declarator->kind == cdk_ptrmem))
5395 {
5396 outer_declarator = declarator;
5397 declarator = declarator->declarator;
5398 }
5399 while (declarator
5400 && declarator->kind == cdk_array
5401 && declarator->declarator
5402 && declarator->declarator->kind == cdk_array)
5403 {
5404 outer_declarator = declarator;
5405 declarator = declarator->declarator;
5406 }
5407
5408 if (declarator && declarator->kind == cdk_array)
5409 {
5410 *nelts = declarator->u.array.bounds;
5411 if (*nelts == error_mark_node)
5412 *nelts = integer_one_node;
5413
5414 if (outer_declarator)
5415 outer_declarator->declarator = declarator->declarator;
5416 else
5417 new_declarator = NULL;
5418 }
5419
5420 type = groktypename (&type_specifier_seq, new_declarator);
5421 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5422 {
5423 *nelts = array_type_nelts_top (type);
5424 type = TREE_TYPE (type);
5425 }
5426 return type;
5427 }
5428
5429 /* Parse an (optional) new-declarator.
5430
5431 new-declarator:
5432 ptr-operator new-declarator [opt]
5433 direct-new-declarator
5434
5435 Returns the declarator. */
5436
5437 static cp_declarator *
5438 cp_parser_new_declarator_opt (cp_parser* parser)
5439 {
5440 enum tree_code code;
5441 tree type;
5442 cp_cv_quals cv_quals;
5443
5444 /* We don't know if there's a ptr-operator next, or not. */
5445 cp_parser_parse_tentatively (parser);
5446 /* Look for a ptr-operator. */
5447 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5448 /* If that worked, look for more new-declarators. */
5449 if (cp_parser_parse_definitely (parser))
5450 {
5451 cp_declarator *declarator;
5452
5453 /* Parse another optional declarator. */
5454 declarator = cp_parser_new_declarator_opt (parser);
5455
5456 /* Create the representation of the declarator. */
5457 if (type)
5458 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5459 else if (code == INDIRECT_REF)
5460 declarator = make_pointer_declarator (cv_quals, declarator);
5461 else
5462 declarator = make_reference_declarator (cv_quals, declarator);
5463
5464 return declarator;
5465 }
5466
5467 /* If the next token is a `[', there is a direct-new-declarator. */
5468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5469 return cp_parser_direct_new_declarator (parser);
5470
5471 return NULL;
5472 }
5473
5474 /* Parse a direct-new-declarator.
5475
5476 direct-new-declarator:
5477 [ expression ]
5478 direct-new-declarator [constant-expression]
5479
5480 */
5481
5482 static cp_declarator *
5483 cp_parser_direct_new_declarator (cp_parser* parser)
5484 {
5485 cp_declarator *declarator = NULL;
5486
5487 while (true)
5488 {
5489 tree expression;
5490
5491 /* Look for the opening `['. */
5492 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5493 /* The first expression is not required to be constant. */
5494 if (!declarator)
5495 {
5496 expression = cp_parser_expression (parser, /*cast_p=*/false);
5497 /* The standard requires that the expression have integral
5498 type. DR 74 adds enumeration types. We believe that the
5499 real intent is that these expressions be handled like the
5500 expression in a `switch' condition, which also allows
5501 classes with a single conversion to integral or
5502 enumeration type. */
5503 if (!processing_template_decl)
5504 {
5505 expression
5506 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5507 expression,
5508 /*complain=*/true);
5509 if (!expression)
5510 {
5511 error ("expression in new-declarator must have integral "
5512 "or enumeration type");
5513 expression = error_mark_node;
5514 }
5515 }
5516 }
5517 /* But all the other expressions must be. */
5518 else
5519 expression
5520 = cp_parser_constant_expression (parser,
5521 /*allow_non_constant=*/false,
5522 NULL);
5523 /* Look for the closing `]'. */
5524 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5525
5526 /* Add this bound to the declarator. */
5527 declarator = make_array_declarator (declarator, expression);
5528
5529 /* If the next token is not a `[', then there are no more
5530 bounds. */
5531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5532 break;
5533 }
5534
5535 return declarator;
5536 }
5537
5538 /* Parse a new-initializer.
5539
5540 new-initializer:
5541 ( expression-list [opt] )
5542
5543 Returns a representation of the expression-list. If there is no
5544 expression-list, VOID_ZERO_NODE is returned. */
5545
5546 static tree
5547 cp_parser_new_initializer (cp_parser* parser)
5548 {
5549 tree expression_list;
5550
5551 expression_list = (cp_parser_parenthesized_expression_list
5552 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5553 /*non_constant_p=*/NULL));
5554 if (!expression_list)
5555 expression_list = void_zero_node;
5556
5557 return expression_list;
5558 }
5559
5560 /* Parse a delete-expression.
5561
5562 delete-expression:
5563 :: [opt] delete cast-expression
5564 :: [opt] delete [ ] cast-expression
5565
5566 Returns a representation of the expression. */
5567
5568 static tree
5569 cp_parser_delete_expression (cp_parser* parser)
5570 {
5571 bool global_scope_p;
5572 bool array_p;
5573 tree expression;
5574
5575 /* Look for the optional `::' operator. */
5576 global_scope_p
5577 = (cp_parser_global_scope_opt (parser,
5578 /*current_scope_valid_p=*/false)
5579 != NULL_TREE);
5580 /* Look for the `delete' keyword. */
5581 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5582 /* See if the array syntax is in use. */
5583 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5584 {
5585 /* Consume the `[' token. */
5586 cp_lexer_consume_token (parser->lexer);
5587 /* Look for the `]' token. */
5588 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5589 /* Remember that this is the `[]' construct. */
5590 array_p = true;
5591 }
5592 else
5593 array_p = false;
5594
5595 /* Parse the cast-expression. */
5596 expression = cp_parser_simple_cast_expression (parser);
5597
5598 /* A delete-expression may not appear in an integral constant
5599 expression. */
5600 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5601 return error_mark_node;
5602
5603 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5604 }
5605
5606 /* Parse a cast-expression.
5607
5608 cast-expression:
5609 unary-expression
5610 ( type-id ) cast-expression
5611
5612 ADDRESS_P is true iff the unary-expression is appearing as the
5613 operand of the `&' operator. CAST_P is true if this expression is
5614 the target of a cast.
5615
5616 Returns a representation of the expression. */
5617
5618 static tree
5619 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5620 {
5621 /* If it's a `(', then we might be looking at a cast. */
5622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5623 {
5624 tree type = NULL_TREE;
5625 tree expr = NULL_TREE;
5626 bool compound_literal_p;
5627 const char *saved_message;
5628
5629 /* There's no way to know yet whether or not this is a cast.
5630 For example, `(int (3))' is a unary-expression, while `(int)
5631 3' is a cast. So, we resort to parsing tentatively. */
5632 cp_parser_parse_tentatively (parser);
5633 /* Types may not be defined in a cast. */
5634 saved_message = parser->type_definition_forbidden_message;
5635 parser->type_definition_forbidden_message
5636 = "types may not be defined in casts";
5637 /* Consume the `('. */
5638 cp_lexer_consume_token (parser->lexer);
5639 /* A very tricky bit is that `(struct S) { 3 }' is a
5640 compound-literal (which we permit in C++ as an extension).
5641 But, that construct is not a cast-expression -- it is a
5642 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5643 is legal; if the compound-literal were a cast-expression,
5644 you'd need an extra set of parentheses.) But, if we parse
5645 the type-id, and it happens to be a class-specifier, then we
5646 will commit to the parse at that point, because we cannot
5647 undo the action that is done when creating a new class. So,
5648 then we cannot back up and do a postfix-expression.
5649
5650 Therefore, we scan ahead to the closing `)', and check to see
5651 if the token after the `)' is a `{'. If so, we are not
5652 looking at a cast-expression.
5653
5654 Save tokens so that we can put them back. */
5655 cp_lexer_save_tokens (parser->lexer);
5656 /* Skip tokens until the next token is a closing parenthesis.
5657 If we find the closing `)', and the next token is a `{', then
5658 we are looking at a compound-literal. */
5659 compound_literal_p
5660 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5661 /*consume_paren=*/true)
5662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5663 /* Roll back the tokens we skipped. */
5664 cp_lexer_rollback_tokens (parser->lexer);
5665 /* If we were looking at a compound-literal, simulate an error
5666 so that the call to cp_parser_parse_definitely below will
5667 fail. */
5668 if (compound_literal_p)
5669 cp_parser_simulate_error (parser);
5670 else
5671 {
5672 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5673 parser->in_type_id_in_expr_p = true;
5674 /* Look for the type-id. */
5675 type = cp_parser_type_id (parser);
5676 /* Look for the closing `)'. */
5677 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5678 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5679 }
5680
5681 /* Restore the saved message. */
5682 parser->type_definition_forbidden_message = saved_message;
5683
5684 /* If ok so far, parse the dependent expression. We cannot be
5685 sure it is a cast. Consider `(T ())'. It is a parenthesized
5686 ctor of T, but looks like a cast to function returning T
5687 without a dependent expression. */
5688 if (!cp_parser_error_occurred (parser))
5689 expr = cp_parser_cast_expression (parser,
5690 /*address_p=*/false,
5691 /*cast_p=*/true);
5692
5693 if (cp_parser_parse_definitely (parser))
5694 {
5695 /* Warn about old-style casts, if so requested. */
5696 if (warn_old_style_cast
5697 && !in_system_header
5698 && !VOID_TYPE_P (type)
5699 && current_lang_name != lang_name_c)
5700 warning (OPT_Wold_style_cast, "use of old-style cast");
5701
5702 /* Only type conversions to integral or enumeration types
5703 can be used in constant-expressions. */
5704 if (!cast_valid_in_integral_constant_expression_p (type)
5705 && (cp_parser_non_integral_constant_expression
5706 (parser,
5707 "a cast to a type other than an integral or "
5708 "enumeration type")))
5709 return error_mark_node;
5710
5711 /* Perform the cast. */
5712 expr = build_c_cast (type, expr);
5713 return expr;
5714 }
5715 }
5716
5717 /* If we get here, then it's not a cast, so it must be a
5718 unary-expression. */
5719 return cp_parser_unary_expression (parser, address_p, cast_p);
5720 }
5721
5722 /* Parse a binary expression of the general form:
5723
5724 pm-expression:
5725 cast-expression
5726 pm-expression .* cast-expression
5727 pm-expression ->* cast-expression
5728
5729 multiplicative-expression:
5730 pm-expression
5731 multiplicative-expression * pm-expression
5732 multiplicative-expression / pm-expression
5733 multiplicative-expression % pm-expression
5734
5735 additive-expression:
5736 multiplicative-expression
5737 additive-expression + multiplicative-expression
5738 additive-expression - multiplicative-expression
5739
5740 shift-expression:
5741 additive-expression
5742 shift-expression << additive-expression
5743 shift-expression >> additive-expression
5744
5745 relational-expression:
5746 shift-expression
5747 relational-expression < shift-expression
5748 relational-expression > shift-expression
5749 relational-expression <= shift-expression
5750 relational-expression >= shift-expression
5751
5752 GNU Extension:
5753
5754 relational-expression:
5755 relational-expression <? shift-expression
5756 relational-expression >? shift-expression
5757
5758 equality-expression:
5759 relational-expression
5760 equality-expression == relational-expression
5761 equality-expression != relational-expression
5762
5763 and-expression:
5764 equality-expression
5765 and-expression & equality-expression
5766
5767 exclusive-or-expression:
5768 and-expression
5769 exclusive-or-expression ^ and-expression
5770
5771 inclusive-or-expression:
5772 exclusive-or-expression
5773 inclusive-or-expression | exclusive-or-expression
5774
5775 logical-and-expression:
5776 inclusive-or-expression
5777 logical-and-expression && inclusive-or-expression
5778
5779 logical-or-expression:
5780 logical-and-expression
5781 logical-or-expression || logical-and-expression
5782
5783 All these are implemented with a single function like:
5784
5785 binary-expression:
5786 simple-cast-expression
5787 binary-expression <token> binary-expression
5788
5789 CAST_P is true if this expression is the target of a cast.
5790
5791 The binops_by_token map is used to get the tree codes for each <token> type.
5792 binary-expressions are associated according to a precedence table. */
5793
5794 #define TOKEN_PRECEDENCE(token) \
5795 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5796 ? PREC_NOT_OPERATOR \
5797 : binops_by_token[token->type].prec)
5798
5799 static tree
5800 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5801 {
5802 cp_parser_expression_stack stack;
5803 cp_parser_expression_stack_entry *sp = &stack[0];
5804 tree lhs, rhs;
5805 cp_token *token;
5806 enum tree_code tree_type, lhs_type, rhs_type;
5807 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5808 bool overloaded_p;
5809
5810 /* Parse the first expression. */
5811 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5812 lhs_type = ERROR_MARK;
5813
5814 for (;;)
5815 {
5816 /* Get an operator token. */
5817 token = cp_lexer_peek_token (parser->lexer);
5818
5819 new_prec = TOKEN_PRECEDENCE (token);
5820
5821 /* Popping an entry off the stack means we completed a subexpression:
5822 - either we found a token which is not an operator (`>' where it is not
5823 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5824 will happen repeatedly;
5825 - or, we found an operator which has lower priority. This is the case
5826 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5827 parsing `3 * 4'. */
5828 if (new_prec <= prec)
5829 {
5830 if (sp == stack)
5831 break;
5832 else
5833 goto pop;
5834 }
5835
5836 get_rhs:
5837 tree_type = binops_by_token[token->type].tree_type;
5838
5839 /* We used the operator token. */
5840 cp_lexer_consume_token (parser->lexer);
5841
5842 /* Extract another operand. It may be the RHS of this expression
5843 or the LHS of a new, higher priority expression. */
5844 rhs = cp_parser_simple_cast_expression (parser);
5845 rhs_type = ERROR_MARK;
5846
5847 /* Get another operator token. Look up its precedence to avoid
5848 building a useless (immediately popped) stack entry for common
5849 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5850 token = cp_lexer_peek_token (parser->lexer);
5851 lookahead_prec = TOKEN_PRECEDENCE (token);
5852 if (lookahead_prec > new_prec)
5853 {
5854 /* ... and prepare to parse the RHS of the new, higher priority
5855 expression. Since precedence levels on the stack are
5856 monotonically increasing, we do not have to care about
5857 stack overflows. */
5858 sp->prec = prec;
5859 sp->tree_type = tree_type;
5860 sp->lhs = lhs;
5861 sp->lhs_type = lhs_type;
5862 sp++;
5863 lhs = rhs;
5864 lhs_type = rhs_type;
5865 prec = new_prec;
5866 new_prec = lookahead_prec;
5867 goto get_rhs;
5868
5869 pop:
5870 /* If the stack is not empty, we have parsed into LHS the right side
5871 (`4' in the example above) of an expression we had suspended.
5872 We can use the information on the stack to recover the LHS (`3')
5873 from the stack together with the tree code (`MULT_EXPR'), and
5874 the precedence of the higher level subexpression
5875 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5876 which will be used to actually build the additive expression. */
5877 --sp;
5878 prec = sp->prec;
5879 tree_type = sp->tree_type;
5880 rhs = lhs;
5881 rhs_type = lhs_type;
5882 lhs = sp->lhs;
5883 lhs_type = sp->lhs_type;
5884 }
5885
5886 overloaded_p = false;
5887 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5888 &overloaded_p);
5889 lhs_type = tree_type;
5890
5891 /* If the binary operator required the use of an overloaded operator,
5892 then this expression cannot be an integral constant-expression.
5893 An overloaded operator can be used even if both operands are
5894 otherwise permissible in an integral constant-expression if at
5895 least one of the operands is of enumeration type. */
5896
5897 if (overloaded_p
5898 && (cp_parser_non_integral_constant_expression
5899 (parser, "calls to overloaded operators")))
5900 return error_mark_node;
5901 }
5902
5903 return lhs;
5904 }
5905
5906
5907 /* Parse the `? expression : assignment-expression' part of a
5908 conditional-expression. The LOGICAL_OR_EXPR is the
5909 logical-or-expression that started the conditional-expression.
5910 Returns a representation of the entire conditional-expression.
5911
5912 This routine is used by cp_parser_assignment_expression.
5913
5914 ? expression : assignment-expression
5915
5916 GNU Extensions:
5917
5918 ? : assignment-expression */
5919
5920 static tree
5921 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5922 {
5923 tree expr;
5924 tree assignment_expr;
5925
5926 /* Consume the `?' token. */
5927 cp_lexer_consume_token (parser->lexer);
5928 if (cp_parser_allow_gnu_extensions_p (parser)
5929 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5930 /* Implicit true clause. */
5931 expr = NULL_TREE;
5932 else
5933 /* Parse the expression. */
5934 expr = cp_parser_expression (parser, /*cast_p=*/false);
5935
5936 /* The next token should be a `:'. */
5937 cp_parser_require (parser, CPP_COLON, "`:'");
5938 /* Parse the assignment-expression. */
5939 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5940
5941 /* Build the conditional-expression. */
5942 return build_x_conditional_expr (logical_or_expr,
5943 expr,
5944 assignment_expr);
5945 }
5946
5947 /* Parse an assignment-expression.
5948
5949 assignment-expression:
5950 conditional-expression
5951 logical-or-expression assignment-operator assignment_expression
5952 throw-expression
5953
5954 CAST_P is true if this expression is the target of a cast.
5955
5956 Returns a representation for the expression. */
5957
5958 static tree
5959 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5960 {
5961 tree expr;
5962
5963 /* If the next token is the `throw' keyword, then we're looking at
5964 a throw-expression. */
5965 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5966 expr = cp_parser_throw_expression (parser);
5967 /* Otherwise, it must be that we are looking at a
5968 logical-or-expression. */
5969 else
5970 {
5971 /* Parse the binary expressions (logical-or-expression). */
5972 expr = cp_parser_binary_expression (parser, cast_p);
5973 /* If the next token is a `?' then we're actually looking at a
5974 conditional-expression. */
5975 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5976 return cp_parser_question_colon_clause (parser, expr);
5977 else
5978 {
5979 enum tree_code assignment_operator;
5980
5981 /* If it's an assignment-operator, we're using the second
5982 production. */
5983 assignment_operator
5984 = cp_parser_assignment_operator_opt (parser);
5985 if (assignment_operator != ERROR_MARK)
5986 {
5987 tree rhs;
5988
5989 /* Parse the right-hand side of the assignment. */
5990 rhs = cp_parser_assignment_expression (parser, cast_p);
5991 /* An assignment may not appear in a
5992 constant-expression. */
5993 if (cp_parser_non_integral_constant_expression (parser,
5994 "an assignment"))
5995 return error_mark_node;
5996 /* Build the assignment expression. */
5997 expr = build_x_modify_expr (expr,
5998 assignment_operator,
5999 rhs);
6000 }
6001 }
6002 }
6003
6004 return expr;
6005 }
6006
6007 /* Parse an (optional) assignment-operator.
6008
6009 assignment-operator: one of
6010 = *= /= %= += -= >>= <<= &= ^= |=
6011
6012 GNU Extension:
6013
6014 assignment-operator: one of
6015 <?= >?=
6016
6017 If the next token is an assignment operator, the corresponding tree
6018 code is returned, and the token is consumed. For example, for
6019 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6020 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6021 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6022 operator, ERROR_MARK is returned. */
6023
6024 static enum tree_code
6025 cp_parser_assignment_operator_opt (cp_parser* parser)
6026 {
6027 enum tree_code op;
6028 cp_token *token;
6029
6030 /* Peek at the next toen. */
6031 token = cp_lexer_peek_token (parser->lexer);
6032
6033 switch (token->type)
6034 {
6035 case CPP_EQ:
6036 op = NOP_EXPR;
6037 break;
6038
6039 case CPP_MULT_EQ:
6040 op = MULT_EXPR;
6041 break;
6042
6043 case CPP_DIV_EQ:
6044 op = TRUNC_DIV_EXPR;
6045 break;
6046
6047 case CPP_MOD_EQ:
6048 op = TRUNC_MOD_EXPR;
6049 break;
6050
6051 case CPP_PLUS_EQ:
6052 op = PLUS_EXPR;
6053 break;
6054
6055 case CPP_MINUS_EQ:
6056 op = MINUS_EXPR;
6057 break;
6058
6059 case CPP_RSHIFT_EQ:
6060 op = RSHIFT_EXPR;
6061 break;
6062
6063 case CPP_LSHIFT_EQ:
6064 op = LSHIFT_EXPR;
6065 break;
6066
6067 case CPP_AND_EQ:
6068 op = BIT_AND_EXPR;
6069 break;
6070
6071 case CPP_XOR_EQ:
6072 op = BIT_XOR_EXPR;
6073 break;
6074
6075 case CPP_OR_EQ:
6076 op = BIT_IOR_EXPR;
6077 break;
6078
6079 default:
6080 /* Nothing else is an assignment operator. */
6081 op = ERROR_MARK;
6082 }
6083
6084 /* If it was an assignment operator, consume it. */
6085 if (op != ERROR_MARK)
6086 cp_lexer_consume_token (parser->lexer);
6087
6088 return op;
6089 }
6090
6091 /* Parse an expression.
6092
6093 expression:
6094 assignment-expression
6095 expression , assignment-expression
6096
6097 CAST_P is true if this expression is the target of a cast.
6098
6099 Returns a representation of the expression. */
6100
6101 static tree
6102 cp_parser_expression (cp_parser* parser, bool cast_p)
6103 {
6104 tree expression = NULL_TREE;
6105
6106 while (true)
6107 {
6108 tree assignment_expression;
6109
6110 /* Parse the next assignment-expression. */
6111 assignment_expression
6112 = cp_parser_assignment_expression (parser, cast_p);
6113 /* If this is the first assignment-expression, we can just
6114 save it away. */
6115 if (!expression)
6116 expression = assignment_expression;
6117 else
6118 expression = build_x_compound_expr (expression,
6119 assignment_expression);
6120 /* If the next token is not a comma, then we are done with the
6121 expression. */
6122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6123 break;
6124 /* Consume the `,'. */
6125 cp_lexer_consume_token (parser->lexer);
6126 /* A comma operator cannot appear in a constant-expression. */
6127 if (cp_parser_non_integral_constant_expression (parser,
6128 "a comma operator"))
6129 expression = error_mark_node;
6130 }
6131
6132 return expression;
6133 }
6134
6135 /* Parse a constant-expression.
6136
6137 constant-expression:
6138 conditional-expression
6139
6140 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6141 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6142 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6143 is false, NON_CONSTANT_P should be NULL. */
6144
6145 static tree
6146 cp_parser_constant_expression (cp_parser* parser,
6147 bool allow_non_constant_p,
6148 bool *non_constant_p)
6149 {
6150 bool saved_integral_constant_expression_p;
6151 bool saved_allow_non_integral_constant_expression_p;
6152 bool saved_non_integral_constant_expression_p;
6153 tree expression;
6154
6155 /* It might seem that we could simply parse the
6156 conditional-expression, and then check to see if it were
6157 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6158 one that the compiler can figure out is constant, possibly after
6159 doing some simplifications or optimizations. The standard has a
6160 precise definition of constant-expression, and we must honor
6161 that, even though it is somewhat more restrictive.
6162
6163 For example:
6164
6165 int i[(2, 3)];
6166
6167 is not a legal declaration, because `(2, 3)' is not a
6168 constant-expression. The `,' operator is forbidden in a
6169 constant-expression. However, GCC's constant-folding machinery
6170 will fold this operation to an INTEGER_CST for `3'. */
6171
6172 /* Save the old settings. */
6173 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6174 saved_allow_non_integral_constant_expression_p
6175 = parser->allow_non_integral_constant_expression_p;
6176 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6177 /* We are now parsing a constant-expression. */
6178 parser->integral_constant_expression_p = true;
6179 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6180 parser->non_integral_constant_expression_p = false;
6181 /* Although the grammar says "conditional-expression", we parse an
6182 "assignment-expression", which also permits "throw-expression"
6183 and the use of assignment operators. In the case that
6184 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6185 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6186 actually essential that we look for an assignment-expression.
6187 For example, cp_parser_initializer_clauses uses this function to
6188 determine whether a particular assignment-expression is in fact
6189 constant. */
6190 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6191 /* Restore the old settings. */
6192 parser->integral_constant_expression_p
6193 = saved_integral_constant_expression_p;
6194 parser->allow_non_integral_constant_expression_p
6195 = saved_allow_non_integral_constant_expression_p;
6196 if (allow_non_constant_p)
6197 *non_constant_p = parser->non_integral_constant_expression_p;
6198 else if (parser->non_integral_constant_expression_p)
6199 expression = error_mark_node;
6200 parser->non_integral_constant_expression_p
6201 = saved_non_integral_constant_expression_p;
6202
6203 return expression;
6204 }
6205
6206 /* Parse __builtin_offsetof.
6207
6208 offsetof-expression:
6209 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6210
6211 offsetof-member-designator:
6212 id-expression
6213 | offsetof-member-designator "." id-expression
6214 | offsetof-member-designator "[" expression "]" */
6215
6216 static tree
6217 cp_parser_builtin_offsetof (cp_parser *parser)
6218 {
6219 int save_ice_p, save_non_ice_p;
6220 tree type, expr;
6221 cp_id_kind dummy;
6222
6223 /* We're about to accept non-integral-constant things, but will
6224 definitely yield an integral constant expression. Save and
6225 restore these values around our local parsing. */
6226 save_ice_p = parser->integral_constant_expression_p;
6227 save_non_ice_p = parser->non_integral_constant_expression_p;
6228
6229 /* Consume the "__builtin_offsetof" token. */
6230 cp_lexer_consume_token (parser->lexer);
6231 /* Consume the opening `('. */
6232 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6233 /* Parse the type-id. */
6234 type = cp_parser_type_id (parser);
6235 /* Look for the `,'. */
6236 cp_parser_require (parser, CPP_COMMA, "`,'");
6237
6238 /* Build the (type *)null that begins the traditional offsetof macro. */
6239 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6240
6241 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6242 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6243 true, &dummy);
6244 while (true)
6245 {
6246 cp_token *token = cp_lexer_peek_token (parser->lexer);
6247 switch (token->type)
6248 {
6249 case CPP_OPEN_SQUARE:
6250 /* offsetof-member-designator "[" expression "]" */
6251 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6252 break;
6253
6254 case CPP_DOT:
6255 /* offsetof-member-designator "." identifier */
6256 cp_lexer_consume_token (parser->lexer);
6257 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6258 true, &dummy);
6259 break;
6260
6261 case CPP_CLOSE_PAREN:
6262 /* Consume the ")" token. */
6263 cp_lexer_consume_token (parser->lexer);
6264 goto success;
6265
6266 default:
6267 /* Error. We know the following require will fail, but
6268 that gives the proper error message. */
6269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6270 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6271 expr = error_mark_node;
6272 goto failure;
6273 }
6274 }
6275
6276 success:
6277 /* If we're processing a template, we can't finish the semantics yet.
6278 Otherwise we can fold the entire expression now. */
6279 if (processing_template_decl)
6280 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6281 else
6282 expr = finish_offsetof (expr);
6283
6284 failure:
6285 parser->integral_constant_expression_p = save_ice_p;
6286 parser->non_integral_constant_expression_p = save_non_ice_p;
6287
6288 return expr;
6289 }
6290
6291 /* Statements [gram.stmt.stmt] */
6292
6293 /* Parse a statement.
6294
6295 statement:
6296 labeled-statement
6297 expression-statement
6298 compound-statement
6299 selection-statement
6300 iteration-statement
6301 jump-statement
6302 declaration-statement
6303 try-block
6304
6305 IN_COMPOUND is true when the statement is nested inside a
6306 cp_parser_compound_statement; this matters for certain pragmas.
6307
6308 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6309 is a (possibly labeled) if statement which is not enclosed in braces
6310 and has an else clause. This is used to implement -Wparentheses. */
6311
6312 static void
6313 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6314 bool in_compound, bool *if_p)
6315 {
6316 tree statement;
6317 cp_token *token;
6318 location_t statement_location;
6319
6320 restart:
6321 if (if_p != NULL)
6322 *if_p = false;
6323 /* There is no statement yet. */
6324 statement = NULL_TREE;
6325 /* Peek at the next token. */
6326 token = cp_lexer_peek_token (parser->lexer);
6327 /* Remember the location of the first token in the statement. */
6328 statement_location = token->location;
6329 /* If this is a keyword, then that will often determine what kind of
6330 statement we have. */
6331 if (token->type == CPP_KEYWORD)
6332 {
6333 enum rid keyword = token->keyword;
6334
6335 switch (keyword)
6336 {
6337 case RID_CASE:
6338 case RID_DEFAULT:
6339 /* Looks like a labeled-statement with a case label.
6340 Parse the label, and then use tail recursion to parse
6341 the statement. */
6342 cp_parser_label_for_labeled_statement (parser);
6343 goto restart;
6344
6345 case RID_IF:
6346 case RID_SWITCH:
6347 statement = cp_parser_selection_statement (parser, if_p);
6348 break;
6349
6350 case RID_WHILE:
6351 case RID_DO:
6352 case RID_FOR:
6353 statement = cp_parser_iteration_statement (parser);
6354 break;
6355
6356 case RID_BREAK:
6357 case RID_CONTINUE:
6358 case RID_RETURN:
6359 case RID_GOTO:
6360 statement = cp_parser_jump_statement (parser);
6361 break;
6362
6363 /* Objective-C++ exception-handling constructs. */
6364 case RID_AT_TRY:
6365 case RID_AT_CATCH:
6366 case RID_AT_FINALLY:
6367 case RID_AT_SYNCHRONIZED:
6368 case RID_AT_THROW:
6369 statement = cp_parser_objc_statement (parser);
6370 break;
6371
6372 case RID_TRY:
6373 statement = cp_parser_try_block (parser);
6374 break;
6375
6376 case RID_NAMESPACE:
6377 /* This must be a namespace alias definition. */
6378 cp_parser_declaration_statement (parser);
6379 return;
6380
6381 default:
6382 /* It might be a keyword like `int' that can start a
6383 declaration-statement. */
6384 break;
6385 }
6386 }
6387 else if (token->type == CPP_NAME)
6388 {
6389 /* If the next token is a `:', then we are looking at a
6390 labeled-statement. */
6391 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6392 if (token->type == CPP_COLON)
6393 {
6394 /* Looks like a labeled-statement with an ordinary label.
6395 Parse the label, and then use tail recursion to parse
6396 the statement. */
6397 cp_parser_label_for_labeled_statement (parser);
6398 goto restart;
6399 }
6400 }
6401 /* Anything that starts with a `{' must be a compound-statement. */
6402 else if (token->type == CPP_OPEN_BRACE)
6403 statement = cp_parser_compound_statement (parser, NULL, false);
6404 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6405 a statement all its own. */
6406 else if (token->type == CPP_PRAGMA)
6407 {
6408 /* Only certain OpenMP pragmas are attached to statements, and thus
6409 are considered statements themselves. All others are not. In
6410 the context of a compound, accept the pragma as a "statement" and
6411 return so that we can check for a close brace. Otherwise we
6412 require a real statement and must go back and read one. */
6413 if (in_compound)
6414 cp_parser_pragma (parser, pragma_compound);
6415 else if (!cp_parser_pragma (parser, pragma_stmt))
6416 goto restart;
6417 return;
6418 }
6419 else if (token->type == CPP_EOF)
6420 {
6421 cp_parser_error (parser, "expected statement");
6422 return;
6423 }
6424
6425 /* Everything else must be a declaration-statement or an
6426 expression-statement. Try for the declaration-statement
6427 first, unless we are looking at a `;', in which case we know that
6428 we have an expression-statement. */
6429 if (!statement)
6430 {
6431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6432 {
6433 cp_parser_parse_tentatively (parser);
6434 /* Try to parse the declaration-statement. */
6435 cp_parser_declaration_statement (parser);
6436 /* If that worked, we're done. */
6437 if (cp_parser_parse_definitely (parser))
6438 return;
6439 }
6440 /* Look for an expression-statement instead. */
6441 statement = cp_parser_expression_statement (parser, in_statement_expr);
6442 }
6443
6444 /* Set the line number for the statement. */
6445 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6446 SET_EXPR_LOCATION (statement, statement_location);
6447 }
6448
6449 /* Parse the label for a labeled-statement, i.e.
6450
6451 identifier :
6452 case constant-expression :
6453 default :
6454
6455 GNU Extension:
6456 case constant-expression ... constant-expression : statement
6457
6458 When a label is parsed without errors, the label is added to the
6459 parse tree by the finish_* functions, so this function doesn't
6460 have to return the label. */
6461
6462 static void
6463 cp_parser_label_for_labeled_statement (cp_parser* parser)
6464 {
6465 cp_token *token;
6466
6467 /* The next token should be an identifier. */
6468 token = cp_lexer_peek_token (parser->lexer);
6469 if (token->type != CPP_NAME
6470 && token->type != CPP_KEYWORD)
6471 {
6472 cp_parser_error (parser, "expected labeled-statement");
6473 return;
6474 }
6475
6476 switch (token->keyword)
6477 {
6478 case RID_CASE:
6479 {
6480 tree expr, expr_hi;
6481 cp_token *ellipsis;
6482
6483 /* Consume the `case' token. */
6484 cp_lexer_consume_token (parser->lexer);
6485 /* Parse the constant-expression. */
6486 expr = cp_parser_constant_expression (parser,
6487 /*allow_non_constant_p=*/false,
6488 NULL);
6489
6490 ellipsis = cp_lexer_peek_token (parser->lexer);
6491 if (ellipsis->type == CPP_ELLIPSIS)
6492 {
6493 /* Consume the `...' token. */
6494 cp_lexer_consume_token (parser->lexer);
6495 expr_hi =
6496 cp_parser_constant_expression (parser,
6497 /*allow_non_constant_p=*/false,
6498 NULL);
6499 /* We don't need to emit warnings here, as the common code
6500 will do this for us. */
6501 }
6502 else
6503 expr_hi = NULL_TREE;
6504
6505 if (parser->in_switch_statement_p)
6506 finish_case_label (expr, expr_hi);
6507 else
6508 error ("case label %qE not within a switch statement", expr);
6509 }
6510 break;
6511
6512 case RID_DEFAULT:
6513 /* Consume the `default' token. */
6514 cp_lexer_consume_token (parser->lexer);
6515
6516 if (parser->in_switch_statement_p)
6517 finish_case_label (NULL_TREE, NULL_TREE);
6518 else
6519 error ("case label not within a switch statement");
6520 break;
6521
6522 default:
6523 /* Anything else must be an ordinary label. */
6524 finish_label_stmt (cp_parser_identifier (parser));
6525 break;
6526 }
6527
6528 /* Require the `:' token. */
6529 cp_parser_require (parser, CPP_COLON, "`:'");
6530 }
6531
6532 /* Parse an expression-statement.
6533
6534 expression-statement:
6535 expression [opt] ;
6536
6537 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6538 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6539 indicates whether this expression-statement is part of an
6540 expression statement. */
6541
6542 static tree
6543 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6544 {
6545 tree statement = NULL_TREE;
6546
6547 /* If the next token is a ';', then there is no expression
6548 statement. */
6549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6550 statement = cp_parser_expression (parser, /*cast_p=*/false);
6551
6552 /* Consume the final `;'. */
6553 cp_parser_consume_semicolon_at_end_of_statement (parser);
6554
6555 if (in_statement_expr
6556 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6557 /* This is the final expression statement of a statement
6558 expression. */
6559 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6560 else if (statement)
6561 statement = finish_expr_stmt (statement);
6562 else
6563 finish_stmt ();
6564
6565 return statement;
6566 }
6567
6568 /* Parse a compound-statement.
6569
6570 compound-statement:
6571 { statement-seq [opt] }
6572
6573 Returns a tree representing the statement. */
6574
6575 static tree
6576 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6577 bool in_try)
6578 {
6579 tree compound_stmt;
6580
6581 /* Consume the `{'. */
6582 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6583 return error_mark_node;
6584 /* Begin the compound-statement. */
6585 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6586 /* Parse an (optional) statement-seq. */
6587 cp_parser_statement_seq_opt (parser, in_statement_expr);
6588 /* Finish the compound-statement. */
6589 finish_compound_stmt (compound_stmt);
6590 /* Consume the `}'. */
6591 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6592
6593 return compound_stmt;
6594 }
6595
6596 /* Parse an (optional) statement-seq.
6597
6598 statement-seq:
6599 statement
6600 statement-seq [opt] statement */
6601
6602 static void
6603 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6604 {
6605 /* Scan statements until there aren't any more. */
6606 while (true)
6607 {
6608 cp_token *token = cp_lexer_peek_token (parser->lexer);
6609
6610 /* If we're looking at a `}', then we've run out of statements. */
6611 if (token->type == CPP_CLOSE_BRACE
6612 || token->type == CPP_EOF
6613 || token->type == CPP_PRAGMA_EOL)
6614 break;
6615
6616 /* If we are in a compound statement and find 'else' then
6617 something went wrong. */
6618 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6619 {
6620 if (parser->in_statement & IN_IF_STMT)
6621 break;
6622 else
6623 {
6624 token = cp_lexer_consume_token (parser->lexer);
6625 error ("%<else%> without a previous %<if%>");
6626 }
6627 }
6628
6629 /* Parse the statement. */
6630 cp_parser_statement (parser, in_statement_expr, true, NULL);
6631 }
6632 }
6633
6634 /* Parse a selection-statement.
6635
6636 selection-statement:
6637 if ( condition ) statement
6638 if ( condition ) statement else statement
6639 switch ( condition ) statement
6640
6641 Returns the new IF_STMT or SWITCH_STMT.
6642
6643 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6644 is a (possibly labeled) if statement which is not enclosed in
6645 braces and has an else clause. This is used to implement
6646 -Wparentheses. */
6647
6648 static tree
6649 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6650 {
6651 cp_token *token;
6652 enum rid keyword;
6653
6654 if (if_p != NULL)
6655 *if_p = false;
6656
6657 /* Peek at the next token. */
6658 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6659
6660 /* See what kind of keyword it is. */
6661 keyword = token->keyword;
6662 switch (keyword)
6663 {
6664 case RID_IF:
6665 case RID_SWITCH:
6666 {
6667 tree statement;
6668 tree condition;
6669
6670 /* Look for the `('. */
6671 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6672 {
6673 cp_parser_skip_to_end_of_statement (parser);
6674 return error_mark_node;
6675 }
6676
6677 /* Begin the selection-statement. */
6678 if (keyword == RID_IF)
6679 statement = begin_if_stmt ();
6680 else
6681 statement = begin_switch_stmt ();
6682
6683 /* Parse the condition. */
6684 condition = cp_parser_condition (parser);
6685 /* Look for the `)'. */
6686 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6687 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6688 /*consume_paren=*/true);
6689
6690 if (keyword == RID_IF)
6691 {
6692 bool nested_if;
6693 unsigned char in_statement;
6694
6695 /* Add the condition. */
6696 finish_if_stmt_cond (condition, statement);
6697
6698 /* Parse the then-clause. */
6699 in_statement = parser->in_statement;
6700 parser->in_statement |= IN_IF_STMT;
6701 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6702 parser->in_statement = in_statement;
6703
6704 finish_then_clause (statement);
6705
6706 /* If the next token is `else', parse the else-clause. */
6707 if (cp_lexer_next_token_is_keyword (parser->lexer,
6708 RID_ELSE))
6709 {
6710 /* Consume the `else' keyword. */
6711 cp_lexer_consume_token (parser->lexer);
6712 begin_else_clause (statement);
6713 /* Parse the else-clause. */
6714 cp_parser_implicitly_scoped_statement (parser, NULL);
6715 finish_else_clause (statement);
6716
6717 /* If we are currently parsing a then-clause, then
6718 IF_P will not be NULL. We set it to true to
6719 indicate that this if statement has an else clause.
6720 This may trigger the Wparentheses warning below
6721 when we get back up to the parent if statement. */
6722 if (if_p != NULL)
6723 *if_p = true;
6724 }
6725 else
6726 {
6727 /* This if statement does not have an else clause. If
6728 NESTED_IF is true, then the then-clause is an if
6729 statement which does have an else clause. We warn
6730 about the potential ambiguity. */
6731 if (nested_if)
6732 warning (OPT_Wparentheses,
6733 ("%Hsuggest explicit braces "
6734 "to avoid ambiguous %<else%>"),
6735 EXPR_LOCUS (statement));
6736 }
6737
6738 /* Now we're all done with the if-statement. */
6739 finish_if_stmt (statement);
6740 }
6741 else
6742 {
6743 bool in_switch_statement_p;
6744 unsigned char in_statement;
6745
6746 /* Add the condition. */
6747 finish_switch_cond (condition, statement);
6748
6749 /* Parse the body of the switch-statement. */
6750 in_switch_statement_p = parser->in_switch_statement_p;
6751 in_statement = parser->in_statement;
6752 parser->in_switch_statement_p = true;
6753 parser->in_statement |= IN_SWITCH_STMT;
6754 cp_parser_implicitly_scoped_statement (parser, NULL);
6755 parser->in_switch_statement_p = in_switch_statement_p;
6756 parser->in_statement = in_statement;
6757
6758 /* Now we're all done with the switch-statement. */
6759 finish_switch_stmt (statement);
6760 }
6761
6762 return statement;
6763 }
6764 break;
6765
6766 default:
6767 cp_parser_error (parser, "expected selection-statement");
6768 return error_mark_node;
6769 }
6770 }
6771
6772 /* Parse a condition.
6773
6774 condition:
6775 expression
6776 type-specifier-seq declarator = assignment-expression
6777
6778 GNU Extension:
6779
6780 condition:
6781 type-specifier-seq declarator asm-specification [opt]
6782 attributes [opt] = assignment-expression
6783
6784 Returns the expression that should be tested. */
6785
6786 static tree
6787 cp_parser_condition (cp_parser* parser)
6788 {
6789 cp_decl_specifier_seq type_specifiers;
6790 const char *saved_message;
6791
6792 /* Try the declaration first. */
6793 cp_parser_parse_tentatively (parser);
6794 /* New types are not allowed in the type-specifier-seq for a
6795 condition. */
6796 saved_message = parser->type_definition_forbidden_message;
6797 parser->type_definition_forbidden_message
6798 = "types may not be defined in conditions";
6799 /* Parse the type-specifier-seq. */
6800 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6801 &type_specifiers);
6802 /* Restore the saved message. */
6803 parser->type_definition_forbidden_message = saved_message;
6804 /* If all is well, we might be looking at a declaration. */
6805 if (!cp_parser_error_occurred (parser))
6806 {
6807 tree decl;
6808 tree asm_specification;
6809 tree attributes;
6810 cp_declarator *declarator;
6811 tree initializer = NULL_TREE;
6812
6813 /* Parse the declarator. */
6814 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6815 /*ctor_dtor_or_conv_p=*/NULL,
6816 /*parenthesized_p=*/NULL,
6817 /*member_p=*/false);
6818 /* Parse the attributes. */
6819 attributes = cp_parser_attributes_opt (parser);
6820 /* Parse the asm-specification. */
6821 asm_specification = cp_parser_asm_specification_opt (parser);
6822 /* If the next token is not an `=', then we might still be
6823 looking at an expression. For example:
6824
6825 if (A(a).x)
6826
6827 looks like a decl-specifier-seq and a declarator -- but then
6828 there is no `=', so this is an expression. */
6829 cp_parser_require (parser, CPP_EQ, "`='");
6830 /* If we did see an `=', then we are looking at a declaration
6831 for sure. */
6832 if (cp_parser_parse_definitely (parser))
6833 {
6834 tree pushed_scope;
6835 bool non_constant_p;
6836
6837 /* Create the declaration. */
6838 decl = start_decl (declarator, &type_specifiers,
6839 /*initialized_p=*/true,
6840 attributes, /*prefix_attributes=*/NULL_TREE,
6841 &pushed_scope);
6842 /* Parse the assignment-expression. */
6843 initializer
6844 = cp_parser_constant_expression (parser,
6845 /*allow_non_constant_p=*/true,
6846 &non_constant_p);
6847 if (!non_constant_p)
6848 initializer = fold_non_dependent_expr (initializer);
6849
6850 /* Process the initializer. */
6851 cp_finish_decl (decl,
6852 initializer, !non_constant_p,
6853 asm_specification,
6854 LOOKUP_ONLYCONVERTING);
6855
6856 if (pushed_scope)
6857 pop_scope (pushed_scope);
6858
6859 return convert_from_reference (decl);
6860 }
6861 }
6862 /* If we didn't even get past the declarator successfully, we are
6863 definitely not looking at a declaration. */
6864 else
6865 cp_parser_abort_tentative_parse (parser);
6866
6867 /* Otherwise, we are looking at an expression. */
6868 return cp_parser_expression (parser, /*cast_p=*/false);
6869 }
6870
6871 /* Parse an iteration-statement.
6872
6873 iteration-statement:
6874 while ( condition ) statement
6875 do statement while ( expression ) ;
6876 for ( for-init-statement condition [opt] ; expression [opt] )
6877 statement
6878
6879 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6880
6881 static tree
6882 cp_parser_iteration_statement (cp_parser* parser)
6883 {
6884 cp_token *token;
6885 enum rid keyword;
6886 tree statement;
6887 unsigned char in_statement;
6888
6889 /* Peek at the next token. */
6890 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6891 if (!token)
6892 return error_mark_node;
6893
6894 /* Remember whether or not we are already within an iteration
6895 statement. */
6896 in_statement = parser->in_statement;
6897
6898 /* See what kind of keyword it is. */
6899 keyword = token->keyword;
6900 switch (keyword)
6901 {
6902 case RID_WHILE:
6903 {
6904 tree condition;
6905
6906 /* Begin the while-statement. */
6907 statement = begin_while_stmt ();
6908 /* Look for the `('. */
6909 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6910 /* Parse the condition. */
6911 condition = cp_parser_condition (parser);
6912 finish_while_stmt_cond (condition, statement);
6913 /* Look for the `)'. */
6914 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6915 /* Parse the dependent statement. */
6916 parser->in_statement = IN_ITERATION_STMT;
6917 cp_parser_already_scoped_statement (parser);
6918 parser->in_statement = in_statement;
6919 /* We're done with the while-statement. */
6920 finish_while_stmt (statement);
6921 }
6922 break;
6923
6924 case RID_DO:
6925 {
6926 tree expression;
6927
6928 /* Begin the do-statement. */
6929 statement = begin_do_stmt ();
6930 /* Parse the body of the do-statement. */
6931 parser->in_statement = IN_ITERATION_STMT;
6932 cp_parser_implicitly_scoped_statement (parser, NULL);
6933 parser->in_statement = in_statement;
6934 finish_do_body (statement);
6935 /* Look for the `while' keyword. */
6936 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6937 /* Look for the `('. */
6938 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6939 /* Parse the expression. */
6940 expression = cp_parser_expression (parser, /*cast_p=*/false);
6941 /* We're done with the do-statement. */
6942 finish_do_stmt (expression, statement);
6943 /* Look for the `)'. */
6944 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6945 /* Look for the `;'. */
6946 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6947 }
6948 break;
6949
6950 case RID_FOR:
6951 {
6952 tree condition = NULL_TREE;
6953 tree expression = NULL_TREE;
6954
6955 /* Begin the for-statement. */
6956 statement = begin_for_stmt ();
6957 /* Look for the `('. */
6958 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6959 /* Parse the initialization. */
6960 cp_parser_for_init_statement (parser);
6961 finish_for_init_stmt (statement);
6962
6963 /* If there's a condition, process it. */
6964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6965 condition = cp_parser_condition (parser);
6966 finish_for_cond (condition, statement);
6967 /* Look for the `;'. */
6968 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6969
6970 /* If there's an expression, process it. */
6971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6972 expression = cp_parser_expression (parser, /*cast_p=*/false);
6973 finish_for_expr (expression, statement);
6974 /* Look for the `)'. */
6975 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6976
6977 /* Parse the body of the for-statement. */
6978 parser->in_statement = IN_ITERATION_STMT;
6979 cp_parser_already_scoped_statement (parser);
6980 parser->in_statement = in_statement;
6981
6982 /* We're done with the for-statement. */
6983 finish_for_stmt (statement);
6984 }
6985 break;
6986
6987 default:
6988 cp_parser_error (parser, "expected iteration-statement");
6989 statement = error_mark_node;
6990 break;
6991 }
6992
6993 return statement;
6994 }
6995
6996 /* Parse a for-init-statement.
6997
6998 for-init-statement:
6999 expression-statement
7000 simple-declaration */
7001
7002 static void
7003 cp_parser_for_init_statement (cp_parser* parser)
7004 {
7005 /* If the next token is a `;', then we have an empty
7006 expression-statement. Grammatically, this is also a
7007 simple-declaration, but an invalid one, because it does not
7008 declare anything. Therefore, if we did not handle this case
7009 specially, we would issue an error message about an invalid
7010 declaration. */
7011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7012 {
7013 /* We're going to speculatively look for a declaration, falling back
7014 to an expression, if necessary. */
7015 cp_parser_parse_tentatively (parser);
7016 /* Parse the declaration. */
7017 cp_parser_simple_declaration (parser,
7018 /*function_definition_allowed_p=*/false);
7019 /* If the tentative parse failed, then we shall need to look for an
7020 expression-statement. */
7021 if (cp_parser_parse_definitely (parser))
7022 return;
7023 }
7024
7025 cp_parser_expression_statement (parser, false);
7026 }
7027
7028 /* Parse a jump-statement.
7029
7030 jump-statement:
7031 break ;
7032 continue ;
7033 return expression [opt] ;
7034 goto identifier ;
7035
7036 GNU extension:
7037
7038 jump-statement:
7039 goto * expression ;
7040
7041 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7042
7043 static tree
7044 cp_parser_jump_statement (cp_parser* parser)
7045 {
7046 tree statement = error_mark_node;
7047 cp_token *token;
7048 enum rid keyword;
7049 unsigned char in_statement;
7050
7051 /* Peek at the next token. */
7052 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7053 if (!token)
7054 return error_mark_node;
7055
7056 /* See what kind of keyword it is. */
7057 keyword = token->keyword;
7058 switch (keyword)
7059 {
7060 case RID_BREAK:
7061 in_statement = parser->in_statement & ~IN_IF_STMT;
7062 switch (in_statement)
7063 {
7064 case 0:
7065 error ("break statement not within loop or switch");
7066 break;
7067 default:
7068 gcc_assert ((in_statement & IN_SWITCH_STMT)
7069 || in_statement == IN_ITERATION_STMT);
7070 statement = finish_break_stmt ();
7071 break;
7072 case IN_OMP_BLOCK:
7073 error ("invalid exit from OpenMP structured block");
7074 break;
7075 case IN_OMP_FOR:
7076 error ("break statement used with OpenMP for loop");
7077 break;
7078 }
7079 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7080 break;
7081
7082 case RID_CONTINUE:
7083 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7084 {
7085 case 0:
7086 error ("continue statement not within a loop");
7087 break;
7088 case IN_ITERATION_STMT:
7089 case IN_OMP_FOR:
7090 statement = finish_continue_stmt ();
7091 break;
7092 case IN_OMP_BLOCK:
7093 error ("invalid exit from OpenMP structured block");
7094 break;
7095 default:
7096 gcc_unreachable ();
7097 }
7098 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7099 break;
7100
7101 case RID_RETURN:
7102 {
7103 tree expr;
7104
7105 /* If the next token is a `;', then there is no
7106 expression. */
7107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7108 expr = cp_parser_expression (parser, /*cast_p=*/false);
7109 else
7110 expr = NULL_TREE;
7111 /* Build the return-statement. */
7112 statement = finish_return_stmt (expr);
7113 /* Look for the final `;'. */
7114 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7115 }
7116 break;
7117
7118 case RID_GOTO:
7119 /* Create the goto-statement. */
7120 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7121 {
7122 /* Issue a warning about this use of a GNU extension. */
7123 if (pedantic)
7124 pedwarn ("ISO C++ forbids computed gotos");
7125 /* Consume the '*' token. */
7126 cp_lexer_consume_token (parser->lexer);
7127 /* Parse the dependent expression. */
7128 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7129 }
7130 else
7131 finish_goto_stmt (cp_parser_identifier (parser));
7132 /* Look for the final `;'. */
7133 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7134 break;
7135
7136 default:
7137 cp_parser_error (parser, "expected jump-statement");
7138 break;
7139 }
7140
7141 return statement;
7142 }
7143
7144 /* Parse a declaration-statement.
7145
7146 declaration-statement:
7147 block-declaration */
7148
7149 static void
7150 cp_parser_declaration_statement (cp_parser* parser)
7151 {
7152 void *p;
7153
7154 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7155 p = obstack_alloc (&declarator_obstack, 0);
7156
7157 /* Parse the block-declaration. */
7158 cp_parser_block_declaration (parser, /*statement_p=*/true);
7159
7160 /* Free any declarators allocated. */
7161 obstack_free (&declarator_obstack, p);
7162
7163 /* Finish off the statement. */
7164 finish_stmt ();
7165 }
7166
7167 /* Some dependent statements (like `if (cond) statement'), are
7168 implicitly in their own scope. In other words, if the statement is
7169 a single statement (as opposed to a compound-statement), it is
7170 none-the-less treated as if it were enclosed in braces. Any
7171 declarations appearing in the dependent statement are out of scope
7172 after control passes that point. This function parses a statement,
7173 but ensures that is in its own scope, even if it is not a
7174 compound-statement.
7175
7176 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7177 is a (possibly labeled) if statement which is not enclosed in
7178 braces and has an else clause. This is used to implement
7179 -Wparentheses.
7180
7181 Returns the new statement. */
7182
7183 static tree
7184 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7185 {
7186 tree statement;
7187
7188 if (if_p != NULL)
7189 *if_p = false;
7190
7191 /* Mark if () ; with a special NOP_EXPR. */
7192 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7193 {
7194 cp_lexer_consume_token (parser->lexer);
7195 statement = add_stmt (build_empty_stmt ());
7196 }
7197 /* if a compound is opened, we simply parse the statement directly. */
7198 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7199 statement = cp_parser_compound_statement (parser, NULL, false);
7200 /* If the token is not a `{', then we must take special action. */
7201 else
7202 {
7203 /* Create a compound-statement. */
7204 statement = begin_compound_stmt (0);
7205 /* Parse the dependent-statement. */
7206 cp_parser_statement (parser, NULL_TREE, false, if_p);
7207 /* Finish the dummy compound-statement. */
7208 finish_compound_stmt (statement);
7209 }
7210
7211 /* Return the statement. */
7212 return statement;
7213 }
7214
7215 /* For some dependent statements (like `while (cond) statement'), we
7216 have already created a scope. Therefore, even if the dependent
7217 statement is a compound-statement, we do not want to create another
7218 scope. */
7219
7220 static void
7221 cp_parser_already_scoped_statement (cp_parser* parser)
7222 {
7223 /* If the token is a `{', then we must take special action. */
7224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7225 cp_parser_statement (parser, NULL_TREE, false, NULL);
7226 else
7227 {
7228 /* Avoid calling cp_parser_compound_statement, so that we
7229 don't create a new scope. Do everything else by hand. */
7230 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7231 cp_parser_statement_seq_opt (parser, NULL_TREE);
7232 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7233 }
7234 }
7235
7236 /* Declarations [gram.dcl.dcl] */
7237
7238 /* Parse an optional declaration-sequence.
7239
7240 declaration-seq:
7241 declaration
7242 declaration-seq declaration */
7243
7244 static void
7245 cp_parser_declaration_seq_opt (cp_parser* parser)
7246 {
7247 while (true)
7248 {
7249 cp_token *token;
7250
7251 token = cp_lexer_peek_token (parser->lexer);
7252
7253 if (token->type == CPP_CLOSE_BRACE
7254 || token->type == CPP_EOF
7255 || token->type == CPP_PRAGMA_EOL)
7256 break;
7257
7258 if (token->type == CPP_SEMICOLON)
7259 {
7260 /* A declaration consisting of a single semicolon is
7261 invalid. Allow it unless we're being pedantic. */
7262 cp_lexer_consume_token (parser->lexer);
7263 if (pedantic && !in_system_header)
7264 pedwarn ("extra %<;%>");
7265 continue;
7266 }
7267
7268 /* If we're entering or exiting a region that's implicitly
7269 extern "C", modify the lang context appropriately. */
7270 if (!parser->implicit_extern_c && token->implicit_extern_c)
7271 {
7272 push_lang_context (lang_name_c);
7273 parser->implicit_extern_c = true;
7274 }
7275 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7276 {
7277 pop_lang_context ();
7278 parser->implicit_extern_c = false;
7279 }
7280
7281 if (token->type == CPP_PRAGMA)
7282 {
7283 /* A top-level declaration can consist solely of a #pragma.
7284 A nested declaration cannot, so this is done here and not
7285 in cp_parser_declaration. (A #pragma at block scope is
7286 handled in cp_parser_statement.) */
7287 cp_parser_pragma (parser, pragma_external);
7288 continue;
7289 }
7290
7291 /* Parse the declaration itself. */
7292 cp_parser_declaration (parser);
7293 }
7294 }
7295
7296 /* Parse a declaration.
7297
7298 declaration:
7299 block-declaration
7300 function-definition
7301 template-declaration
7302 explicit-instantiation
7303 explicit-specialization
7304 linkage-specification
7305 namespace-definition
7306
7307 GNU extension:
7308
7309 declaration:
7310 __extension__ declaration */
7311
7312 static void
7313 cp_parser_declaration (cp_parser* parser)
7314 {
7315 cp_token token1;
7316 cp_token token2;
7317 int saved_pedantic;
7318 void *p;
7319
7320 /* Check for the `__extension__' keyword. */
7321 if (cp_parser_extension_opt (parser, &saved_pedantic))
7322 {
7323 /* Parse the qualified declaration. */
7324 cp_parser_declaration (parser);
7325 /* Restore the PEDANTIC flag. */
7326 pedantic = saved_pedantic;
7327
7328 return;
7329 }
7330
7331 /* Try to figure out what kind of declaration is present. */
7332 token1 = *cp_lexer_peek_token (parser->lexer);
7333
7334 if (token1.type != CPP_EOF)
7335 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7336 else
7337 {
7338 token2.type = CPP_EOF;
7339 token2.keyword = RID_MAX;
7340 }
7341
7342 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7343 p = obstack_alloc (&declarator_obstack, 0);
7344
7345 /* If the next token is `extern' and the following token is a string
7346 literal, then we have a linkage specification. */
7347 if (token1.keyword == RID_EXTERN
7348 && cp_parser_is_string_literal (&token2))
7349 cp_parser_linkage_specification (parser);
7350 /* If the next token is `template', then we have either a template
7351 declaration, an explicit instantiation, or an explicit
7352 specialization. */
7353 else if (token1.keyword == RID_TEMPLATE)
7354 {
7355 /* `template <>' indicates a template specialization. */
7356 if (token2.type == CPP_LESS
7357 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7358 cp_parser_explicit_specialization (parser);
7359 /* `template <' indicates a template declaration. */
7360 else if (token2.type == CPP_LESS)
7361 cp_parser_template_declaration (parser, /*member_p=*/false);
7362 /* Anything else must be an explicit instantiation. */
7363 else
7364 cp_parser_explicit_instantiation (parser);
7365 }
7366 /* If the next token is `export', then we have a template
7367 declaration. */
7368 else if (token1.keyword == RID_EXPORT)
7369 cp_parser_template_declaration (parser, /*member_p=*/false);
7370 /* If the next token is `extern', 'static' or 'inline' and the one
7371 after that is `template', we have a GNU extended explicit
7372 instantiation directive. */
7373 else if (cp_parser_allow_gnu_extensions_p (parser)
7374 && (token1.keyword == RID_EXTERN
7375 || token1.keyword == RID_STATIC
7376 || token1.keyword == RID_INLINE)
7377 && token2.keyword == RID_TEMPLATE)
7378 cp_parser_explicit_instantiation (parser);
7379 /* If the next token is `namespace', check for a named or unnamed
7380 namespace definition. */
7381 else if (token1.keyword == RID_NAMESPACE
7382 && (/* A named namespace definition. */
7383 (token2.type == CPP_NAME
7384 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7385 != CPP_EQ))
7386 /* An unnamed namespace definition. */
7387 || token2.type == CPP_OPEN_BRACE
7388 || token2.keyword == RID_ATTRIBUTE))
7389 cp_parser_namespace_definition (parser);
7390 /* Objective-C++ declaration/definition. */
7391 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7392 cp_parser_objc_declaration (parser);
7393 /* We must have either a block declaration or a function
7394 definition. */
7395 else
7396 /* Try to parse a block-declaration, or a function-definition. */
7397 cp_parser_block_declaration (parser, /*statement_p=*/false);
7398
7399 /* Free any declarators allocated. */
7400 obstack_free (&declarator_obstack, p);
7401 }
7402
7403 /* Parse a block-declaration.
7404
7405 block-declaration:
7406 simple-declaration
7407 asm-definition
7408 namespace-alias-definition
7409 using-declaration
7410 using-directive
7411
7412 GNU Extension:
7413
7414 block-declaration:
7415 __extension__ block-declaration
7416 label-declaration
7417
7418 C++0x Extension:
7419
7420 block-declaration:
7421 static_assert-declaration
7422
7423 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7424 part of a declaration-statement. */
7425
7426 static void
7427 cp_parser_block_declaration (cp_parser *parser,
7428 bool statement_p)
7429 {
7430 cp_token *token1;
7431 int saved_pedantic;
7432
7433 /* Check for the `__extension__' keyword. */
7434 if (cp_parser_extension_opt (parser, &saved_pedantic))
7435 {
7436 /* Parse the qualified declaration. */
7437 cp_parser_block_declaration (parser, statement_p);
7438 /* Restore the PEDANTIC flag. */
7439 pedantic = saved_pedantic;
7440
7441 return;
7442 }
7443
7444 /* Peek at the next token to figure out which kind of declaration is
7445 present. */
7446 token1 = cp_lexer_peek_token (parser->lexer);
7447
7448 /* If the next keyword is `asm', we have an asm-definition. */
7449 if (token1->keyword == RID_ASM)
7450 {
7451 if (statement_p)
7452 cp_parser_commit_to_tentative_parse (parser);
7453 cp_parser_asm_definition (parser);
7454 }
7455 /* If the next keyword is `namespace', we have a
7456 namespace-alias-definition. */
7457 else if (token1->keyword == RID_NAMESPACE)
7458 cp_parser_namespace_alias_definition (parser);
7459 /* If the next keyword is `using', we have either a
7460 using-declaration or a using-directive. */
7461 else if (token1->keyword == RID_USING)
7462 {
7463 cp_token *token2;
7464
7465 if (statement_p)
7466 cp_parser_commit_to_tentative_parse (parser);
7467 /* If the token after `using' is `namespace', then we have a
7468 using-directive. */
7469 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7470 if (token2->keyword == RID_NAMESPACE)
7471 cp_parser_using_directive (parser);
7472 /* Otherwise, it's a using-declaration. */
7473 else
7474 cp_parser_using_declaration (parser,
7475 /*access_declaration_p=*/false);
7476 }
7477 /* If the next keyword is `__label__' we have a label declaration. */
7478 else if (token1->keyword == RID_LABEL)
7479 {
7480 if (statement_p)
7481 cp_parser_commit_to_tentative_parse (parser);
7482 cp_parser_label_declaration (parser);
7483 }
7484 /* If the next token is `static_assert' we have a static assertion. */
7485 else if (token1->keyword == RID_STATIC_ASSERT)
7486 cp_parser_static_assert (parser, /*member_p=*/false);
7487 /* Anything else must be a simple-declaration. */
7488 else
7489 cp_parser_simple_declaration (parser, !statement_p);
7490 }
7491
7492 /* Parse a simple-declaration.
7493
7494 simple-declaration:
7495 decl-specifier-seq [opt] init-declarator-list [opt] ;
7496
7497 init-declarator-list:
7498 init-declarator
7499 init-declarator-list , init-declarator
7500
7501 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7502 function-definition as a simple-declaration. */
7503
7504 static void
7505 cp_parser_simple_declaration (cp_parser* parser,
7506 bool function_definition_allowed_p)
7507 {
7508 cp_decl_specifier_seq decl_specifiers;
7509 int declares_class_or_enum;
7510 bool saw_declarator;
7511
7512 /* Defer access checks until we know what is being declared; the
7513 checks for names appearing in the decl-specifier-seq should be
7514 done as if we were in the scope of the thing being declared. */
7515 push_deferring_access_checks (dk_deferred);
7516
7517 /* Parse the decl-specifier-seq. We have to keep track of whether
7518 or not the decl-specifier-seq declares a named class or
7519 enumeration type, since that is the only case in which the
7520 init-declarator-list is allowed to be empty.
7521
7522 [dcl.dcl]
7523
7524 In a simple-declaration, the optional init-declarator-list can be
7525 omitted only when declaring a class or enumeration, that is when
7526 the decl-specifier-seq contains either a class-specifier, an
7527 elaborated-type-specifier, or an enum-specifier. */
7528 cp_parser_decl_specifier_seq (parser,
7529 CP_PARSER_FLAGS_OPTIONAL,
7530 &decl_specifiers,
7531 &declares_class_or_enum);
7532 /* We no longer need to defer access checks. */
7533 stop_deferring_access_checks ();
7534
7535 /* In a block scope, a valid declaration must always have a
7536 decl-specifier-seq. By not trying to parse declarators, we can
7537 resolve the declaration/expression ambiguity more quickly. */
7538 if (!function_definition_allowed_p
7539 && !decl_specifiers.any_specifiers_p)
7540 {
7541 cp_parser_error (parser, "expected declaration");
7542 goto done;
7543 }
7544
7545 /* If the next two tokens are both identifiers, the code is
7546 erroneous. The usual cause of this situation is code like:
7547
7548 T t;
7549
7550 where "T" should name a type -- but does not. */
7551 if (!decl_specifiers.type
7552 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7553 {
7554 /* If parsing tentatively, we should commit; we really are
7555 looking at a declaration. */
7556 cp_parser_commit_to_tentative_parse (parser);
7557 /* Give up. */
7558 goto done;
7559 }
7560
7561 /* If we have seen at least one decl-specifier, and the next token
7562 is not a parenthesis, then we must be looking at a declaration.
7563 (After "int (" we might be looking at a functional cast.) */
7564 if (decl_specifiers.any_specifiers_p
7565 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7566 cp_parser_commit_to_tentative_parse (parser);
7567
7568 /* Keep going until we hit the `;' at the end of the simple
7569 declaration. */
7570 saw_declarator = false;
7571 while (cp_lexer_next_token_is_not (parser->lexer,
7572 CPP_SEMICOLON))
7573 {
7574 cp_token *token;
7575 bool function_definition_p;
7576 tree decl;
7577
7578 if (saw_declarator)
7579 {
7580 /* If we are processing next declarator, coma is expected */
7581 token = cp_lexer_peek_token (parser->lexer);
7582 gcc_assert (token->type == CPP_COMMA);
7583 cp_lexer_consume_token (parser->lexer);
7584 }
7585 else
7586 saw_declarator = true;
7587
7588 /* Parse the init-declarator. */
7589 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7590 /*checks=*/NULL,
7591 function_definition_allowed_p,
7592 /*member_p=*/false,
7593 declares_class_or_enum,
7594 &function_definition_p);
7595 /* If an error occurred while parsing tentatively, exit quickly.
7596 (That usually happens when in the body of a function; each
7597 statement is treated as a declaration-statement until proven
7598 otherwise.) */
7599 if (cp_parser_error_occurred (parser))
7600 goto done;
7601 /* Handle function definitions specially. */
7602 if (function_definition_p)
7603 {
7604 /* If the next token is a `,', then we are probably
7605 processing something like:
7606
7607 void f() {}, *p;
7608
7609 which is erroneous. */
7610 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7611 error ("mixing declarations and function-definitions is forbidden");
7612 /* Otherwise, we're done with the list of declarators. */
7613 else
7614 {
7615 pop_deferring_access_checks ();
7616 return;
7617 }
7618 }
7619 /* The next token should be either a `,' or a `;'. */
7620 token = cp_lexer_peek_token (parser->lexer);
7621 /* If it's a `,', there are more declarators to come. */
7622 if (token->type == CPP_COMMA)
7623 /* will be consumed next time around */;
7624 /* If it's a `;', we are done. */
7625 else if (token->type == CPP_SEMICOLON)
7626 break;
7627 /* Anything else is an error. */
7628 else
7629 {
7630 /* If we have already issued an error message we don't need
7631 to issue another one. */
7632 if (decl != error_mark_node
7633 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7634 cp_parser_error (parser, "expected %<,%> or %<;%>");
7635 /* Skip tokens until we reach the end of the statement. */
7636 cp_parser_skip_to_end_of_statement (parser);
7637 /* If the next token is now a `;', consume it. */
7638 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7639 cp_lexer_consume_token (parser->lexer);
7640 goto done;
7641 }
7642 /* After the first time around, a function-definition is not
7643 allowed -- even if it was OK at first. For example:
7644
7645 int i, f() {}
7646
7647 is not valid. */
7648 function_definition_allowed_p = false;
7649 }
7650
7651 /* Issue an error message if no declarators are present, and the
7652 decl-specifier-seq does not itself declare a class or
7653 enumeration. */
7654 if (!saw_declarator)
7655 {
7656 if (cp_parser_declares_only_class_p (parser))
7657 shadow_tag (&decl_specifiers);
7658 /* Perform any deferred access checks. */
7659 perform_deferred_access_checks ();
7660 }
7661
7662 /* Consume the `;'. */
7663 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7664
7665 done:
7666 pop_deferring_access_checks ();
7667 }
7668
7669 /* Parse a decl-specifier-seq.
7670
7671 decl-specifier-seq:
7672 decl-specifier-seq [opt] decl-specifier
7673
7674 decl-specifier:
7675 storage-class-specifier
7676 type-specifier
7677 function-specifier
7678 friend
7679 typedef
7680
7681 GNU Extension:
7682
7683 decl-specifier:
7684 attributes
7685
7686 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7687
7688 The parser flags FLAGS is used to control type-specifier parsing.
7689
7690 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7691 flags:
7692
7693 1: one of the decl-specifiers is an elaborated-type-specifier
7694 (i.e., a type declaration)
7695 2: one of the decl-specifiers is an enum-specifier or a
7696 class-specifier (i.e., a type definition)
7697
7698 */
7699
7700 static void
7701 cp_parser_decl_specifier_seq (cp_parser* parser,
7702 cp_parser_flags flags,
7703 cp_decl_specifier_seq *decl_specs,
7704 int* declares_class_or_enum)
7705 {
7706 bool constructor_possible_p = !parser->in_declarator_p;
7707
7708 /* Clear DECL_SPECS. */
7709 clear_decl_specs (decl_specs);
7710
7711 /* Assume no class or enumeration type is declared. */
7712 *declares_class_or_enum = 0;
7713
7714 /* Keep reading specifiers until there are no more to read. */
7715 while (true)
7716 {
7717 bool constructor_p;
7718 bool found_decl_spec;
7719 cp_token *token;
7720
7721 /* Peek at the next token. */
7722 token = cp_lexer_peek_token (parser->lexer);
7723 /* Handle attributes. */
7724 if (token->keyword == RID_ATTRIBUTE)
7725 {
7726 /* Parse the attributes. */
7727 decl_specs->attributes
7728 = chainon (decl_specs->attributes,
7729 cp_parser_attributes_opt (parser));
7730 continue;
7731 }
7732 /* Assume we will find a decl-specifier keyword. */
7733 found_decl_spec = true;
7734 /* If the next token is an appropriate keyword, we can simply
7735 add it to the list. */
7736 switch (token->keyword)
7737 {
7738 /* decl-specifier:
7739 friend */
7740 case RID_FRIEND:
7741 if (!at_class_scope_p ())
7742 {
7743 error ("%<friend%> used outside of class");
7744 cp_lexer_purge_token (parser->lexer);
7745 }
7746 else
7747 {
7748 ++decl_specs->specs[(int) ds_friend];
7749 /* Consume the token. */
7750 cp_lexer_consume_token (parser->lexer);
7751 }
7752 break;
7753
7754 /* function-specifier:
7755 inline
7756 virtual
7757 explicit */
7758 case RID_INLINE:
7759 case RID_VIRTUAL:
7760 case RID_EXPLICIT:
7761 cp_parser_function_specifier_opt (parser, decl_specs);
7762 break;
7763
7764 /* decl-specifier:
7765 typedef */
7766 case RID_TYPEDEF:
7767 ++decl_specs->specs[(int) ds_typedef];
7768 /* Consume the token. */
7769 cp_lexer_consume_token (parser->lexer);
7770 /* A constructor declarator cannot appear in a typedef. */
7771 constructor_possible_p = false;
7772 /* The "typedef" keyword can only occur in a declaration; we
7773 may as well commit at this point. */
7774 cp_parser_commit_to_tentative_parse (parser);
7775
7776 if (decl_specs->storage_class != sc_none)
7777 decl_specs->conflicting_specifiers_p = true;
7778 break;
7779
7780 /* storage-class-specifier:
7781 auto
7782 register
7783 static
7784 extern
7785 mutable
7786
7787 GNU Extension:
7788 thread */
7789 case RID_AUTO:
7790 case RID_REGISTER:
7791 case RID_STATIC:
7792 case RID_EXTERN:
7793 case RID_MUTABLE:
7794 /* Consume the token. */
7795 cp_lexer_consume_token (parser->lexer);
7796 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7797 break;
7798 case RID_THREAD:
7799 /* Consume the token. */
7800 cp_lexer_consume_token (parser->lexer);
7801 ++decl_specs->specs[(int) ds_thread];
7802 break;
7803
7804 default:
7805 /* We did not yet find a decl-specifier yet. */
7806 found_decl_spec = false;
7807 break;
7808 }
7809
7810 /* Constructors are a special case. The `S' in `S()' is not a
7811 decl-specifier; it is the beginning of the declarator. */
7812 constructor_p
7813 = (!found_decl_spec
7814 && constructor_possible_p
7815 && (cp_parser_constructor_declarator_p
7816 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7817
7818 /* If we don't have a DECL_SPEC yet, then we must be looking at
7819 a type-specifier. */
7820 if (!found_decl_spec && !constructor_p)
7821 {
7822 int decl_spec_declares_class_or_enum;
7823 bool is_cv_qualifier;
7824 tree type_spec;
7825
7826 type_spec
7827 = cp_parser_type_specifier (parser, flags,
7828 decl_specs,
7829 /*is_declaration=*/true,
7830 &decl_spec_declares_class_or_enum,
7831 &is_cv_qualifier);
7832
7833 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7834
7835 /* If this type-specifier referenced a user-defined type
7836 (a typedef, class-name, etc.), then we can't allow any
7837 more such type-specifiers henceforth.
7838
7839 [dcl.spec]
7840
7841 The longest sequence of decl-specifiers that could
7842 possibly be a type name is taken as the
7843 decl-specifier-seq of a declaration. The sequence shall
7844 be self-consistent as described below.
7845
7846 [dcl.type]
7847
7848 As a general rule, at most one type-specifier is allowed
7849 in the complete decl-specifier-seq of a declaration. The
7850 only exceptions are the following:
7851
7852 -- const or volatile can be combined with any other
7853 type-specifier.
7854
7855 -- signed or unsigned can be combined with char, long,
7856 short, or int.
7857
7858 -- ..
7859
7860 Example:
7861
7862 typedef char* Pc;
7863 void g (const int Pc);
7864
7865 Here, Pc is *not* part of the decl-specifier seq; it's
7866 the declarator. Therefore, once we see a type-specifier
7867 (other than a cv-qualifier), we forbid any additional
7868 user-defined types. We *do* still allow things like `int
7869 int' to be considered a decl-specifier-seq, and issue the
7870 error message later. */
7871 if (type_spec && !is_cv_qualifier)
7872 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7873 /* A constructor declarator cannot follow a type-specifier. */
7874 if (type_spec)
7875 {
7876 constructor_possible_p = false;
7877 found_decl_spec = true;
7878 }
7879 }
7880
7881 /* If we still do not have a DECL_SPEC, then there are no more
7882 decl-specifiers. */
7883 if (!found_decl_spec)
7884 break;
7885
7886 decl_specs->any_specifiers_p = true;
7887 /* After we see one decl-specifier, further decl-specifiers are
7888 always optional. */
7889 flags |= CP_PARSER_FLAGS_OPTIONAL;
7890 }
7891
7892 cp_parser_check_decl_spec (decl_specs);
7893
7894 /* Don't allow a friend specifier with a class definition. */
7895 if (decl_specs->specs[(int) ds_friend] != 0
7896 && (*declares_class_or_enum & 2))
7897 error ("class definition may not be declared a friend");
7898 }
7899
7900 /* Parse an (optional) storage-class-specifier.
7901
7902 storage-class-specifier:
7903 auto
7904 register
7905 static
7906 extern
7907 mutable
7908
7909 GNU Extension:
7910
7911 storage-class-specifier:
7912 thread
7913
7914 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7915
7916 static tree
7917 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7918 {
7919 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7920 {
7921 case RID_AUTO:
7922 case RID_REGISTER:
7923 case RID_STATIC:
7924 case RID_EXTERN:
7925 case RID_MUTABLE:
7926 case RID_THREAD:
7927 /* Consume the token. */
7928 return cp_lexer_consume_token (parser->lexer)->u.value;
7929
7930 default:
7931 return NULL_TREE;
7932 }
7933 }
7934
7935 /* Parse an (optional) function-specifier.
7936
7937 function-specifier:
7938 inline
7939 virtual
7940 explicit
7941
7942 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7943 Updates DECL_SPECS, if it is non-NULL. */
7944
7945 static tree
7946 cp_parser_function_specifier_opt (cp_parser* parser,
7947 cp_decl_specifier_seq *decl_specs)
7948 {
7949 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7950 {
7951 case RID_INLINE:
7952 if (decl_specs)
7953 ++decl_specs->specs[(int) ds_inline];
7954 break;
7955
7956 case RID_VIRTUAL:
7957 /* 14.5.2.3 [temp.mem]
7958
7959 A member function template shall not be virtual. */
7960 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7961 error ("templates may not be %<virtual%>");
7962 else if (decl_specs)
7963 ++decl_specs->specs[(int) ds_virtual];
7964 break;
7965
7966 case RID_EXPLICIT:
7967 if (decl_specs)
7968 ++decl_specs->specs[(int) ds_explicit];
7969 break;
7970
7971 default:
7972 return NULL_TREE;
7973 }
7974
7975 /* Consume the token. */
7976 return cp_lexer_consume_token (parser->lexer)->u.value;
7977 }
7978
7979 /* Parse a linkage-specification.
7980
7981 linkage-specification:
7982 extern string-literal { declaration-seq [opt] }
7983 extern string-literal declaration */
7984
7985 static void
7986 cp_parser_linkage_specification (cp_parser* parser)
7987 {
7988 tree linkage;
7989
7990 /* Look for the `extern' keyword. */
7991 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7992
7993 /* Look for the string-literal. */
7994 linkage = cp_parser_string_literal (parser, false, false);
7995
7996 /* Transform the literal into an identifier. If the literal is a
7997 wide-character string, or contains embedded NULs, then we can't
7998 handle it as the user wants. */
7999 if (strlen (TREE_STRING_POINTER (linkage))
8000 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8001 {
8002 cp_parser_error (parser, "invalid linkage-specification");
8003 /* Assume C++ linkage. */
8004 linkage = lang_name_cplusplus;
8005 }
8006 else
8007 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8008
8009 /* We're now using the new linkage. */
8010 push_lang_context (linkage);
8011
8012 /* If the next token is a `{', then we're using the first
8013 production. */
8014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8015 {
8016 /* Consume the `{' token. */
8017 cp_lexer_consume_token (parser->lexer);
8018 /* Parse the declarations. */
8019 cp_parser_declaration_seq_opt (parser);
8020 /* Look for the closing `}'. */
8021 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8022 }
8023 /* Otherwise, there's just one declaration. */
8024 else
8025 {
8026 bool saved_in_unbraced_linkage_specification_p;
8027
8028 saved_in_unbraced_linkage_specification_p
8029 = parser->in_unbraced_linkage_specification_p;
8030 parser->in_unbraced_linkage_specification_p = true;
8031 cp_parser_declaration (parser);
8032 parser->in_unbraced_linkage_specification_p
8033 = saved_in_unbraced_linkage_specification_p;
8034 }
8035
8036 /* We're done with the linkage-specification. */
8037 pop_lang_context ();
8038 }
8039
8040 /* Parse a static_assert-declaration.
8041
8042 static_assert-declaration:
8043 static_assert ( constant-expression , string-literal ) ;
8044
8045 If MEMBER_P, this static_assert is a class member. */
8046
8047 static void
8048 cp_parser_static_assert(cp_parser *parser, bool member_p)
8049 {
8050 tree condition;
8051 tree message;
8052 cp_token *token;
8053 location_t saved_loc;
8054
8055 /* Peek at the `static_assert' token so we can keep track of exactly
8056 where the static assertion started. */
8057 token = cp_lexer_peek_token (parser->lexer);
8058 saved_loc = token->location;
8059
8060 /* Look for the `static_assert' keyword. */
8061 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8062 "`static_assert'"))
8063 return;
8064
8065 /* We know we are in a static assertion; commit to any tentative
8066 parse. */
8067 if (cp_parser_parsing_tentatively (parser))
8068 cp_parser_commit_to_tentative_parse (parser);
8069
8070 /* Parse the `(' starting the static assertion condition. */
8071 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8072
8073 /* Parse the constant-expression. */
8074 condition =
8075 cp_parser_constant_expression (parser,
8076 /*allow_non_constant_p=*/false,
8077 /*non_constant_p=*/NULL);
8078
8079 /* Parse the separating `,'. */
8080 cp_parser_require (parser, CPP_COMMA, "`,'");
8081
8082 /* Parse the string-literal message. */
8083 message = cp_parser_string_literal (parser,
8084 /*translate=*/false,
8085 /*wide_ok=*/true);
8086
8087 /* A `)' completes the static assertion. */
8088 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8089 cp_parser_skip_to_closing_parenthesis (parser,
8090 /*recovering=*/true,
8091 /*or_comma=*/false,
8092 /*consume_paren=*/true);
8093
8094 /* A semicolon terminates the declaration. */
8095 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8096
8097 /* Complete the static assertion, which may mean either processing
8098 the static assert now or saving it for template instantiation. */
8099 finish_static_assert (condition, message, saved_loc, member_p);
8100 }
8101
8102 /* Special member functions [gram.special] */
8103
8104 /* Parse a conversion-function-id.
8105
8106 conversion-function-id:
8107 operator conversion-type-id
8108
8109 Returns an IDENTIFIER_NODE representing the operator. */
8110
8111 static tree
8112 cp_parser_conversion_function_id (cp_parser* parser)
8113 {
8114 tree type;
8115 tree saved_scope;
8116 tree saved_qualifying_scope;
8117 tree saved_object_scope;
8118 tree pushed_scope = NULL_TREE;
8119
8120 /* Look for the `operator' token. */
8121 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8122 return error_mark_node;
8123 /* When we parse the conversion-type-id, the current scope will be
8124 reset. However, we need that information in able to look up the
8125 conversion function later, so we save it here. */
8126 saved_scope = parser->scope;
8127 saved_qualifying_scope = parser->qualifying_scope;
8128 saved_object_scope = parser->object_scope;
8129 /* We must enter the scope of the class so that the names of
8130 entities declared within the class are available in the
8131 conversion-type-id. For example, consider:
8132
8133 struct S {
8134 typedef int I;
8135 operator I();
8136 };
8137
8138 S::operator I() { ... }
8139
8140 In order to see that `I' is a type-name in the definition, we
8141 must be in the scope of `S'. */
8142 if (saved_scope)
8143 pushed_scope = push_scope (saved_scope);
8144 /* Parse the conversion-type-id. */
8145 type = cp_parser_conversion_type_id (parser);
8146 /* Leave the scope of the class, if any. */
8147 if (pushed_scope)
8148 pop_scope (pushed_scope);
8149 /* Restore the saved scope. */
8150 parser->scope = saved_scope;
8151 parser->qualifying_scope = saved_qualifying_scope;
8152 parser->object_scope = saved_object_scope;
8153 /* If the TYPE is invalid, indicate failure. */
8154 if (type == error_mark_node)
8155 return error_mark_node;
8156 return mangle_conv_op_name_for_type (type);
8157 }
8158
8159 /* Parse a conversion-type-id:
8160
8161 conversion-type-id:
8162 type-specifier-seq conversion-declarator [opt]
8163
8164 Returns the TYPE specified. */
8165
8166 static tree
8167 cp_parser_conversion_type_id (cp_parser* parser)
8168 {
8169 tree attributes;
8170 cp_decl_specifier_seq type_specifiers;
8171 cp_declarator *declarator;
8172 tree type_specified;
8173
8174 /* Parse the attributes. */
8175 attributes = cp_parser_attributes_opt (parser);
8176 /* Parse the type-specifiers. */
8177 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8178 &type_specifiers);
8179 /* If that didn't work, stop. */
8180 if (type_specifiers.type == error_mark_node)
8181 return error_mark_node;
8182 /* Parse the conversion-declarator. */
8183 declarator = cp_parser_conversion_declarator_opt (parser);
8184
8185 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8186 /*initialized=*/0, &attributes);
8187 if (attributes)
8188 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8189 return type_specified;
8190 }
8191
8192 /* Parse an (optional) conversion-declarator.
8193
8194 conversion-declarator:
8195 ptr-operator conversion-declarator [opt]
8196
8197 */
8198
8199 static cp_declarator *
8200 cp_parser_conversion_declarator_opt (cp_parser* parser)
8201 {
8202 enum tree_code code;
8203 tree class_type;
8204 cp_cv_quals cv_quals;
8205
8206 /* We don't know if there's a ptr-operator next, or not. */
8207 cp_parser_parse_tentatively (parser);
8208 /* Try the ptr-operator. */
8209 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8210 /* If it worked, look for more conversion-declarators. */
8211 if (cp_parser_parse_definitely (parser))
8212 {
8213 cp_declarator *declarator;
8214
8215 /* Parse another optional declarator. */
8216 declarator = cp_parser_conversion_declarator_opt (parser);
8217
8218 /* Create the representation of the declarator. */
8219 if (class_type)
8220 declarator = make_ptrmem_declarator (cv_quals, class_type,
8221 declarator);
8222 else if (code == INDIRECT_REF)
8223 declarator = make_pointer_declarator (cv_quals, declarator);
8224 else
8225 declarator = make_reference_declarator (cv_quals, declarator);
8226
8227 return declarator;
8228 }
8229
8230 return NULL;
8231 }
8232
8233 /* Parse an (optional) ctor-initializer.
8234
8235 ctor-initializer:
8236 : mem-initializer-list
8237
8238 Returns TRUE iff the ctor-initializer was actually present. */
8239
8240 static bool
8241 cp_parser_ctor_initializer_opt (cp_parser* parser)
8242 {
8243 /* If the next token is not a `:', then there is no
8244 ctor-initializer. */
8245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8246 {
8247 /* Do default initialization of any bases and members. */
8248 if (DECL_CONSTRUCTOR_P (current_function_decl))
8249 finish_mem_initializers (NULL_TREE);
8250
8251 return false;
8252 }
8253
8254 /* Consume the `:' token. */
8255 cp_lexer_consume_token (parser->lexer);
8256 /* And the mem-initializer-list. */
8257 cp_parser_mem_initializer_list (parser);
8258
8259 return true;
8260 }
8261
8262 /* Parse a mem-initializer-list.
8263
8264 mem-initializer-list:
8265 mem-initializer ... [opt]
8266 mem-initializer ... [opt] , mem-initializer-list */
8267
8268 static void
8269 cp_parser_mem_initializer_list (cp_parser* parser)
8270 {
8271 tree mem_initializer_list = NULL_TREE;
8272
8273 /* Let the semantic analysis code know that we are starting the
8274 mem-initializer-list. */
8275 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8276 error ("only constructors take base initializers");
8277
8278 /* Loop through the list. */
8279 while (true)
8280 {
8281 tree mem_initializer;
8282
8283 /* Parse the mem-initializer. */
8284 mem_initializer = cp_parser_mem_initializer (parser);
8285 /* If the next token is a `...', we're expanding member initializers. */
8286 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8287 {
8288 /* Consume the `...'. */
8289 cp_lexer_consume_token (parser->lexer);
8290
8291 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8292 can be expanded but members cannot. */
8293 if (mem_initializer != error_mark_node
8294 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8295 {
8296 error ("cannot expand initializer for member %<%D%>",
8297 TREE_PURPOSE (mem_initializer));
8298 mem_initializer = error_mark_node;
8299 }
8300
8301 /* Construct the pack expansion type. */
8302 if (mem_initializer != error_mark_node)
8303 mem_initializer = make_pack_expansion (mem_initializer);
8304 }
8305 /* Add it to the list, unless it was erroneous. */
8306 if (mem_initializer != error_mark_node)
8307 {
8308 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8309 mem_initializer_list = mem_initializer;
8310 }
8311 /* If the next token is not a `,', we're done. */
8312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8313 break;
8314 /* Consume the `,' token. */
8315 cp_lexer_consume_token (parser->lexer);
8316 }
8317
8318 /* Perform semantic analysis. */
8319 if (DECL_CONSTRUCTOR_P (current_function_decl))
8320 finish_mem_initializers (mem_initializer_list);
8321 }
8322
8323 /* Parse a mem-initializer.
8324
8325 mem-initializer:
8326 mem-initializer-id ( expression-list [opt] )
8327
8328 GNU extension:
8329
8330 mem-initializer:
8331 ( expression-list [opt] )
8332
8333 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8334 class) or FIELD_DECL (for a non-static data member) to initialize;
8335 the TREE_VALUE is the expression-list. An empty initialization
8336 list is represented by void_list_node. */
8337
8338 static tree
8339 cp_parser_mem_initializer (cp_parser* parser)
8340 {
8341 tree mem_initializer_id;
8342 tree expression_list;
8343 tree member;
8344
8345 /* Find out what is being initialized. */
8346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8347 {
8348 pedwarn ("anachronistic old-style base class initializer");
8349 mem_initializer_id = NULL_TREE;
8350 }
8351 else
8352 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8353 member = expand_member_init (mem_initializer_id);
8354 if (member && !DECL_P (member))
8355 in_base_initializer = 1;
8356
8357 expression_list
8358 = cp_parser_parenthesized_expression_list (parser, false,
8359 /*cast_p=*/false,
8360 /*allow_expansion_p=*/true,
8361 /*non_constant_p=*/NULL);
8362 if (expression_list == error_mark_node)
8363 return error_mark_node;
8364 if (!expression_list)
8365 expression_list = void_type_node;
8366
8367 in_base_initializer = 0;
8368
8369 return member ? build_tree_list (member, expression_list) : error_mark_node;
8370 }
8371
8372 /* Parse a mem-initializer-id.
8373
8374 mem-initializer-id:
8375 :: [opt] nested-name-specifier [opt] class-name
8376 identifier
8377
8378 Returns a TYPE indicating the class to be initializer for the first
8379 production. Returns an IDENTIFIER_NODE indicating the data member
8380 to be initialized for the second production. */
8381
8382 static tree
8383 cp_parser_mem_initializer_id (cp_parser* parser)
8384 {
8385 bool global_scope_p;
8386 bool nested_name_specifier_p;
8387 bool template_p = false;
8388 tree id;
8389
8390 /* `typename' is not allowed in this context ([temp.res]). */
8391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8392 {
8393 error ("keyword %<typename%> not allowed in this context (a qualified "
8394 "member initializer is implicitly a type)");
8395 cp_lexer_consume_token (parser->lexer);
8396 }
8397 /* Look for the optional `::' operator. */
8398 global_scope_p
8399 = (cp_parser_global_scope_opt (parser,
8400 /*current_scope_valid_p=*/false)
8401 != NULL_TREE);
8402 /* Look for the optional nested-name-specifier. The simplest way to
8403 implement:
8404
8405 [temp.res]
8406
8407 The keyword `typename' is not permitted in a base-specifier or
8408 mem-initializer; in these contexts a qualified name that
8409 depends on a template-parameter is implicitly assumed to be a
8410 type name.
8411
8412 is to assume that we have seen the `typename' keyword at this
8413 point. */
8414 nested_name_specifier_p
8415 = (cp_parser_nested_name_specifier_opt (parser,
8416 /*typename_keyword_p=*/true,
8417 /*check_dependency_p=*/true,
8418 /*type_p=*/true,
8419 /*is_declaration=*/true)
8420 != NULL_TREE);
8421 if (nested_name_specifier_p)
8422 template_p = cp_parser_optional_template_keyword (parser);
8423 /* If there is a `::' operator or a nested-name-specifier, then we
8424 are definitely looking for a class-name. */
8425 if (global_scope_p || nested_name_specifier_p)
8426 return cp_parser_class_name (parser,
8427 /*typename_keyword_p=*/true,
8428 /*template_keyword_p=*/template_p,
8429 none_type,
8430 /*check_dependency_p=*/true,
8431 /*class_head_p=*/false,
8432 /*is_declaration=*/true);
8433 /* Otherwise, we could also be looking for an ordinary identifier. */
8434 cp_parser_parse_tentatively (parser);
8435 /* Try a class-name. */
8436 id = cp_parser_class_name (parser,
8437 /*typename_keyword_p=*/true,
8438 /*template_keyword_p=*/false,
8439 none_type,
8440 /*check_dependency_p=*/true,
8441 /*class_head_p=*/false,
8442 /*is_declaration=*/true);
8443 /* If we found one, we're done. */
8444 if (cp_parser_parse_definitely (parser))
8445 return id;
8446 /* Otherwise, look for an ordinary identifier. */
8447 return cp_parser_identifier (parser);
8448 }
8449
8450 /* Overloading [gram.over] */
8451
8452 /* Parse an operator-function-id.
8453
8454 operator-function-id:
8455 operator operator
8456
8457 Returns an IDENTIFIER_NODE for the operator which is a
8458 human-readable spelling of the identifier, e.g., `operator +'. */
8459
8460 static tree
8461 cp_parser_operator_function_id (cp_parser* parser)
8462 {
8463 /* Look for the `operator' keyword. */
8464 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8465 return error_mark_node;
8466 /* And then the name of the operator itself. */
8467 return cp_parser_operator (parser);
8468 }
8469
8470 /* Parse an operator.
8471
8472 operator:
8473 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8474 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8475 || ++ -- , ->* -> () []
8476
8477 GNU Extensions:
8478
8479 operator:
8480 <? >? <?= >?=
8481
8482 Returns an IDENTIFIER_NODE for the operator which is a
8483 human-readable spelling of the identifier, e.g., `operator +'. */
8484
8485 static tree
8486 cp_parser_operator (cp_parser* parser)
8487 {
8488 tree id = NULL_TREE;
8489 cp_token *token;
8490
8491 /* Peek at the next token. */
8492 token = cp_lexer_peek_token (parser->lexer);
8493 /* Figure out which operator we have. */
8494 switch (token->type)
8495 {
8496 case CPP_KEYWORD:
8497 {
8498 enum tree_code op;
8499
8500 /* The keyword should be either `new' or `delete'. */
8501 if (token->keyword == RID_NEW)
8502 op = NEW_EXPR;
8503 else if (token->keyword == RID_DELETE)
8504 op = DELETE_EXPR;
8505 else
8506 break;
8507
8508 /* Consume the `new' or `delete' token. */
8509 cp_lexer_consume_token (parser->lexer);
8510
8511 /* Peek at the next token. */
8512 token = cp_lexer_peek_token (parser->lexer);
8513 /* If it's a `[' token then this is the array variant of the
8514 operator. */
8515 if (token->type == CPP_OPEN_SQUARE)
8516 {
8517 /* Consume the `[' token. */
8518 cp_lexer_consume_token (parser->lexer);
8519 /* Look for the `]' token. */
8520 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8521 id = ansi_opname (op == NEW_EXPR
8522 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8523 }
8524 /* Otherwise, we have the non-array variant. */
8525 else
8526 id = ansi_opname (op);
8527
8528 return id;
8529 }
8530
8531 case CPP_PLUS:
8532 id = ansi_opname (PLUS_EXPR);
8533 break;
8534
8535 case CPP_MINUS:
8536 id = ansi_opname (MINUS_EXPR);
8537 break;
8538
8539 case CPP_MULT:
8540 id = ansi_opname (MULT_EXPR);
8541 break;
8542
8543 case CPP_DIV:
8544 id = ansi_opname (TRUNC_DIV_EXPR);
8545 break;
8546
8547 case CPP_MOD:
8548 id = ansi_opname (TRUNC_MOD_EXPR);
8549 break;
8550
8551 case CPP_XOR:
8552 id = ansi_opname (BIT_XOR_EXPR);
8553 break;
8554
8555 case CPP_AND:
8556 id = ansi_opname (BIT_AND_EXPR);
8557 break;
8558
8559 case CPP_OR:
8560 id = ansi_opname (BIT_IOR_EXPR);
8561 break;
8562
8563 case CPP_COMPL:
8564 id = ansi_opname (BIT_NOT_EXPR);
8565 break;
8566
8567 case CPP_NOT:
8568 id = ansi_opname (TRUTH_NOT_EXPR);
8569 break;
8570
8571 case CPP_EQ:
8572 id = ansi_assopname (NOP_EXPR);
8573 break;
8574
8575 case CPP_LESS:
8576 id = ansi_opname (LT_EXPR);
8577 break;
8578
8579 case CPP_GREATER:
8580 id = ansi_opname (GT_EXPR);
8581 break;
8582
8583 case CPP_PLUS_EQ:
8584 id = ansi_assopname (PLUS_EXPR);
8585 break;
8586
8587 case CPP_MINUS_EQ:
8588 id = ansi_assopname (MINUS_EXPR);
8589 break;
8590
8591 case CPP_MULT_EQ:
8592 id = ansi_assopname (MULT_EXPR);
8593 break;
8594
8595 case CPP_DIV_EQ:
8596 id = ansi_assopname (TRUNC_DIV_EXPR);
8597 break;
8598
8599 case CPP_MOD_EQ:
8600 id = ansi_assopname (TRUNC_MOD_EXPR);
8601 break;
8602
8603 case CPP_XOR_EQ:
8604 id = ansi_assopname (BIT_XOR_EXPR);
8605 break;
8606
8607 case CPP_AND_EQ:
8608 id = ansi_assopname (BIT_AND_EXPR);
8609 break;
8610
8611 case CPP_OR_EQ:
8612 id = ansi_assopname (BIT_IOR_EXPR);
8613 break;
8614
8615 case CPP_LSHIFT:
8616 id = ansi_opname (LSHIFT_EXPR);
8617 break;
8618
8619 case CPP_RSHIFT:
8620 id = ansi_opname (RSHIFT_EXPR);
8621 break;
8622
8623 case CPP_LSHIFT_EQ:
8624 id = ansi_assopname (LSHIFT_EXPR);
8625 break;
8626
8627 case CPP_RSHIFT_EQ:
8628 id = ansi_assopname (RSHIFT_EXPR);
8629 break;
8630
8631 case CPP_EQ_EQ:
8632 id = ansi_opname (EQ_EXPR);
8633 break;
8634
8635 case CPP_NOT_EQ:
8636 id = ansi_opname (NE_EXPR);
8637 break;
8638
8639 case CPP_LESS_EQ:
8640 id = ansi_opname (LE_EXPR);
8641 break;
8642
8643 case CPP_GREATER_EQ:
8644 id = ansi_opname (GE_EXPR);
8645 break;
8646
8647 case CPP_AND_AND:
8648 id = ansi_opname (TRUTH_ANDIF_EXPR);
8649 break;
8650
8651 case CPP_OR_OR:
8652 id = ansi_opname (TRUTH_ORIF_EXPR);
8653 break;
8654
8655 case CPP_PLUS_PLUS:
8656 id = ansi_opname (POSTINCREMENT_EXPR);
8657 break;
8658
8659 case CPP_MINUS_MINUS:
8660 id = ansi_opname (PREDECREMENT_EXPR);
8661 break;
8662
8663 case CPP_COMMA:
8664 id = ansi_opname (COMPOUND_EXPR);
8665 break;
8666
8667 case CPP_DEREF_STAR:
8668 id = ansi_opname (MEMBER_REF);
8669 break;
8670
8671 case CPP_DEREF:
8672 id = ansi_opname (COMPONENT_REF);
8673 break;
8674
8675 case CPP_OPEN_PAREN:
8676 /* Consume the `('. */
8677 cp_lexer_consume_token (parser->lexer);
8678 /* Look for the matching `)'. */
8679 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8680 return ansi_opname (CALL_EXPR);
8681
8682 case CPP_OPEN_SQUARE:
8683 /* Consume the `['. */
8684 cp_lexer_consume_token (parser->lexer);
8685 /* Look for the matching `]'. */
8686 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8687 return ansi_opname (ARRAY_REF);
8688
8689 default:
8690 /* Anything else is an error. */
8691 break;
8692 }
8693
8694 /* If we have selected an identifier, we need to consume the
8695 operator token. */
8696 if (id)
8697 cp_lexer_consume_token (parser->lexer);
8698 /* Otherwise, no valid operator name was present. */
8699 else
8700 {
8701 cp_parser_error (parser, "expected operator");
8702 id = error_mark_node;
8703 }
8704
8705 return id;
8706 }
8707
8708 /* Parse a template-declaration.
8709
8710 template-declaration:
8711 export [opt] template < template-parameter-list > declaration
8712
8713 If MEMBER_P is TRUE, this template-declaration occurs within a
8714 class-specifier.
8715
8716 The grammar rule given by the standard isn't correct. What
8717 is really meant is:
8718
8719 template-declaration:
8720 export [opt] template-parameter-list-seq
8721 decl-specifier-seq [opt] init-declarator [opt] ;
8722 export [opt] template-parameter-list-seq
8723 function-definition
8724
8725 template-parameter-list-seq:
8726 template-parameter-list-seq [opt]
8727 template < template-parameter-list > */
8728
8729 static void
8730 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8731 {
8732 /* Check for `export'. */
8733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8734 {
8735 /* Consume the `export' token. */
8736 cp_lexer_consume_token (parser->lexer);
8737 /* Warn that we do not support `export'. */
8738 warning (0, "keyword %<export%> not implemented, and will be ignored");
8739 }
8740
8741 cp_parser_template_declaration_after_export (parser, member_p);
8742 }
8743
8744 /* Parse a template-parameter-list.
8745
8746 template-parameter-list:
8747 template-parameter
8748 template-parameter-list , template-parameter
8749
8750 Returns a TREE_LIST. Each node represents a template parameter.
8751 The nodes are connected via their TREE_CHAINs. */
8752
8753 static tree
8754 cp_parser_template_parameter_list (cp_parser* parser)
8755 {
8756 tree parameter_list = NULL_TREE;
8757
8758 begin_template_parm_list ();
8759 while (true)
8760 {
8761 tree parameter;
8762 cp_token *token;
8763 bool is_non_type;
8764 bool is_parameter_pack;
8765
8766 /* Parse the template-parameter. */
8767 parameter = cp_parser_template_parameter (parser,
8768 &is_non_type,
8769 &is_parameter_pack);
8770 /* Add it to the list. */
8771 if (parameter != error_mark_node)
8772 parameter_list = process_template_parm (parameter_list,
8773 parameter,
8774 is_non_type,
8775 is_parameter_pack);
8776 else
8777 {
8778 tree err_parm = build_tree_list (parameter, parameter);
8779 TREE_VALUE (err_parm) = error_mark_node;
8780 parameter_list = chainon (parameter_list, err_parm);
8781 }
8782
8783 /* Peek at the next token. */
8784 token = cp_lexer_peek_token (parser->lexer);
8785 /* If it's not a `,', we're done. */
8786 if (token->type != CPP_COMMA)
8787 break;
8788 /* Otherwise, consume the `,' token. */
8789 cp_lexer_consume_token (parser->lexer);
8790 }
8791
8792 return end_template_parm_list (parameter_list);
8793 }
8794
8795 /* Parse a template-parameter.
8796
8797 template-parameter:
8798 type-parameter
8799 parameter-declaration
8800
8801 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8802 the parameter. The TREE_PURPOSE is the default value, if any.
8803 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8804 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
8805 set to true iff this parameter is a parameter pack. */
8806
8807 static tree
8808 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
8809 bool *is_parameter_pack)
8810 {
8811 cp_token *token;
8812 cp_parameter_declarator *parameter_declarator;
8813 tree parm;
8814
8815 /* Assume it is a type parameter or a template parameter. */
8816 *is_non_type = false;
8817 /* Assume it not a parameter pack. */
8818 *is_parameter_pack = false;
8819 /* Peek at the next token. */
8820 token = cp_lexer_peek_token (parser->lexer);
8821 /* If it is `class' or `template', we have a type-parameter. */
8822 if (token->keyword == RID_TEMPLATE)
8823 return cp_parser_type_parameter (parser, is_parameter_pack);
8824 /* If it is `class' or `typename' we do not know yet whether it is a
8825 type parameter or a non-type parameter. Consider:
8826
8827 template <typename T, typename T::X X> ...
8828
8829 or:
8830
8831 template <class C, class D*> ...
8832
8833 Here, the first parameter is a type parameter, and the second is
8834 a non-type parameter. We can tell by looking at the token after
8835 the identifier -- if it is a `,', `=', or `>' then we have a type
8836 parameter. */
8837 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8838 {
8839 /* Peek at the token after `class' or `typename'. */
8840 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8841 /* If it's an ellipsis, we have a template type parameter
8842 pack. */
8843 if (token->type == CPP_ELLIPSIS)
8844 return cp_parser_type_parameter (parser, is_parameter_pack);
8845 /* If it's an identifier, skip it. */
8846 if (token->type == CPP_NAME)
8847 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8848 /* Now, see if the token looks like the end of a template
8849 parameter. */
8850 if (token->type == CPP_COMMA
8851 || token->type == CPP_EQ
8852 || token->type == CPP_GREATER)
8853 return cp_parser_type_parameter (parser, is_parameter_pack);
8854 }
8855
8856 /* Otherwise, it is a non-type parameter.
8857
8858 [temp.param]
8859
8860 When parsing a default template-argument for a non-type
8861 template-parameter, the first non-nested `>' is taken as the end
8862 of the template parameter-list rather than a greater-than
8863 operator. */
8864 *is_non_type = true;
8865 parameter_declarator
8866 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8867 /*parenthesized_p=*/NULL);
8868
8869 /* If the parameter declaration is marked as a parameter pack, set
8870 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8871 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8872 grokdeclarator. */
8873 if (parameter_declarator
8874 && parameter_declarator->declarator
8875 && parameter_declarator->declarator->parameter_pack_p)
8876 {
8877 *is_parameter_pack = true;
8878 parameter_declarator->declarator->parameter_pack_p = false;
8879 }
8880
8881 /* If the next token is an ellipsis, and we don't already have it
8882 marked as a parameter pack, then we have a parameter pack (that
8883 has no declarator); */
8884 if (!*is_parameter_pack
8885 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8886 {
8887
8888 /* Consume the `...'. */
8889 cp_lexer_consume_token (parser->lexer);
8890 maybe_warn_variadic_templates ();
8891
8892 *is_parameter_pack = true;
8893 }
8894
8895 parm = grokdeclarator (parameter_declarator->declarator,
8896 &parameter_declarator->decl_specifiers,
8897 PARM, /*initialized=*/0,
8898 /*attrlist=*/NULL);
8899 if (parm == error_mark_node)
8900 return error_mark_node;
8901
8902 return build_tree_list (parameter_declarator->default_argument, parm);
8903 }
8904
8905 /* Parse a type-parameter.
8906
8907 type-parameter:
8908 class identifier [opt]
8909 class identifier [opt] = type-id
8910 typename identifier [opt]
8911 typename identifier [opt] = type-id
8912 template < template-parameter-list > class identifier [opt]
8913 template < template-parameter-list > class identifier [opt]
8914 = id-expression
8915
8916 GNU Extension (variadic templates):
8917
8918 type-parameter:
8919 class ... identifier [opt]
8920 typename ... identifier [opt]
8921
8922 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8923 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8924 the declaration of the parameter.
8925
8926 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8927
8928 static tree
8929 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
8930 {
8931 cp_token *token;
8932 tree parameter;
8933
8934 /* Look for a keyword to tell us what kind of parameter this is. */
8935 token = cp_parser_require (parser, CPP_KEYWORD,
8936 "`class', `typename', or `template'");
8937 if (!token)
8938 return error_mark_node;
8939
8940 switch (token->keyword)
8941 {
8942 case RID_CLASS:
8943 case RID_TYPENAME:
8944 {
8945 tree identifier;
8946 tree default_argument;
8947
8948 /* If the next token is an ellipsis, we have a template
8949 argument pack. */
8950 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8951 {
8952 /* Consume the `...' token. */
8953 cp_lexer_consume_token (parser->lexer);
8954 maybe_warn_variadic_templates ();
8955
8956 *is_parameter_pack = true;
8957 }
8958
8959 /* If the next token is an identifier, then it names the
8960 parameter. */
8961 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8962 identifier = cp_parser_identifier (parser);
8963 else
8964 identifier = NULL_TREE;
8965
8966 /* Create the parameter. */
8967 parameter = finish_template_type_parm (class_type_node, identifier);
8968
8969 /* If the next token is an `=', we have a default argument. */
8970 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8971 {
8972 /* Consume the `=' token. */
8973 cp_lexer_consume_token (parser->lexer);
8974 /* Parse the default-argument. */
8975 push_deferring_access_checks (dk_no_deferred);
8976 default_argument = cp_parser_type_id (parser);
8977
8978 /* Template parameter packs cannot have default
8979 arguments. */
8980 if (*is_parameter_pack)
8981 {
8982 if (identifier)
8983 error ("template parameter pack %qD cannot have a default argument",
8984 identifier);
8985 else
8986 error ("template parameter packs cannot have default arguments");
8987 default_argument = NULL_TREE;
8988 }
8989 pop_deferring_access_checks ();
8990 }
8991 else
8992 default_argument = NULL_TREE;
8993
8994 /* Create the combined representation of the parameter and the
8995 default argument. */
8996 parameter = build_tree_list (default_argument, parameter);
8997 }
8998 break;
8999
9000 case RID_TEMPLATE:
9001 {
9002 tree parameter_list;
9003 tree identifier;
9004 tree default_argument;
9005
9006 /* Look for the `<'. */
9007 cp_parser_require (parser, CPP_LESS, "`<'");
9008 /* Parse the template-parameter-list. */
9009 parameter_list = cp_parser_template_parameter_list (parser);
9010 /* Look for the `>'. */
9011 cp_parser_require (parser, CPP_GREATER, "`>'");
9012 /* Look for the `class' keyword. */
9013 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9014 /* If the next token is an ellipsis, we have a template
9015 argument pack. */
9016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9017 {
9018 /* Consume the `...' token. */
9019 cp_lexer_consume_token (parser->lexer);
9020 maybe_warn_variadic_templates ();
9021
9022 *is_parameter_pack = true;
9023 }
9024 /* If the next token is an `=', then there is a
9025 default-argument. If the next token is a `>', we are at
9026 the end of the parameter-list. If the next token is a `,',
9027 then we are at the end of this parameter. */
9028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9029 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9030 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9031 {
9032 identifier = cp_parser_identifier (parser);
9033 /* Treat invalid names as if the parameter were nameless. */
9034 if (identifier == error_mark_node)
9035 identifier = NULL_TREE;
9036 }
9037 else
9038 identifier = NULL_TREE;
9039
9040 /* Create the template parameter. */
9041 parameter = finish_template_template_parm (class_type_node,
9042 identifier);
9043
9044 /* If the next token is an `=', then there is a
9045 default-argument. */
9046 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9047 {
9048 bool is_template;
9049
9050 /* Consume the `='. */
9051 cp_lexer_consume_token (parser->lexer);
9052 /* Parse the id-expression. */
9053 push_deferring_access_checks (dk_no_deferred);
9054 default_argument
9055 = cp_parser_id_expression (parser,
9056 /*template_keyword_p=*/false,
9057 /*check_dependency_p=*/true,
9058 /*template_p=*/&is_template,
9059 /*declarator_p=*/false,
9060 /*optional_p=*/false);
9061 if (TREE_CODE (default_argument) == TYPE_DECL)
9062 /* If the id-expression was a template-id that refers to
9063 a template-class, we already have the declaration here,
9064 so no further lookup is needed. */
9065 ;
9066 else
9067 /* Look up the name. */
9068 default_argument
9069 = cp_parser_lookup_name (parser, default_argument,
9070 none_type,
9071 /*is_template=*/is_template,
9072 /*is_namespace=*/false,
9073 /*check_dependency=*/true,
9074 /*ambiguous_decls=*/NULL);
9075 /* See if the default argument is valid. */
9076 default_argument
9077 = check_template_template_default_arg (default_argument);
9078
9079 /* Template parameter packs cannot have default
9080 arguments. */
9081 if (*is_parameter_pack)
9082 {
9083 if (identifier)
9084 error ("template parameter pack %qD cannot have a default argument",
9085 identifier);
9086 else
9087 error ("template parameter packs cannot have default arguments");
9088 default_argument = NULL_TREE;
9089 }
9090 pop_deferring_access_checks ();
9091 }
9092 else
9093 default_argument = NULL_TREE;
9094
9095 /* Create the combined representation of the parameter and the
9096 default argument. */
9097 parameter = build_tree_list (default_argument, parameter);
9098 }
9099 break;
9100
9101 default:
9102 gcc_unreachable ();
9103 break;
9104 }
9105
9106 return parameter;
9107 }
9108
9109 /* Parse a template-id.
9110
9111 template-id:
9112 template-name < template-argument-list [opt] >
9113
9114 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9115 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9116 returned. Otherwise, if the template-name names a function, or set
9117 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9118 names a class, returns a TYPE_DECL for the specialization.
9119
9120 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9121 uninstantiated templates. */
9122
9123 static tree
9124 cp_parser_template_id (cp_parser *parser,
9125 bool template_keyword_p,
9126 bool check_dependency_p,
9127 bool is_declaration)
9128 {
9129 int i;
9130 tree template;
9131 tree arguments;
9132 tree template_id;
9133 cp_token_position start_of_id = 0;
9134 deferred_access_check *chk;
9135 VEC (deferred_access_check,gc) *access_check;
9136 cp_token *next_token, *next_token_2;
9137 bool is_identifier;
9138
9139 /* If the next token corresponds to a template-id, there is no need
9140 to reparse it. */
9141 next_token = cp_lexer_peek_token (parser->lexer);
9142 if (next_token->type == CPP_TEMPLATE_ID)
9143 {
9144 struct tree_check *check_value;
9145
9146 /* Get the stored value. */
9147 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9148 /* Perform any access checks that were deferred. */
9149 access_check = check_value->checks;
9150 if (access_check)
9151 {
9152 for (i = 0 ;
9153 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9154 ++i)
9155 {
9156 perform_or_defer_access_check (chk->binfo,
9157 chk->decl,
9158 chk->diag_decl);
9159 }
9160 }
9161 /* Return the stored value. */
9162 return check_value->value;
9163 }
9164
9165 /* Avoid performing name lookup if there is no possibility of
9166 finding a template-id. */
9167 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9168 || (next_token->type == CPP_NAME
9169 && !cp_parser_nth_token_starts_template_argument_list_p
9170 (parser, 2)))
9171 {
9172 cp_parser_error (parser, "expected template-id");
9173 return error_mark_node;
9174 }
9175
9176 /* Remember where the template-id starts. */
9177 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9178 start_of_id = cp_lexer_token_position (parser->lexer, false);
9179
9180 push_deferring_access_checks (dk_deferred);
9181
9182 /* Parse the template-name. */
9183 is_identifier = false;
9184 template = cp_parser_template_name (parser, template_keyword_p,
9185 check_dependency_p,
9186 is_declaration,
9187 &is_identifier);
9188 if (template == error_mark_node || is_identifier)
9189 {
9190 pop_deferring_access_checks ();
9191 return template;
9192 }
9193
9194 /* If we find the sequence `[:' after a template-name, it's probably
9195 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9196 parse correctly the argument list. */
9197 next_token = cp_lexer_peek_token (parser->lexer);
9198 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9199 if (next_token->type == CPP_OPEN_SQUARE
9200 && next_token->flags & DIGRAPH
9201 && next_token_2->type == CPP_COLON
9202 && !(next_token_2->flags & PREV_WHITE))
9203 {
9204 cp_parser_parse_tentatively (parser);
9205 /* Change `:' into `::'. */
9206 next_token_2->type = CPP_SCOPE;
9207 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9208 CPP_LESS. */
9209 cp_lexer_consume_token (parser->lexer);
9210 /* Parse the arguments. */
9211 arguments = cp_parser_enclosed_template_argument_list (parser);
9212 if (!cp_parser_parse_definitely (parser))
9213 {
9214 /* If we couldn't parse an argument list, then we revert our changes
9215 and return simply an error. Maybe this is not a template-id
9216 after all. */
9217 next_token_2->type = CPP_COLON;
9218 cp_parser_error (parser, "expected %<<%>");
9219 pop_deferring_access_checks ();
9220 return error_mark_node;
9221 }
9222 /* Otherwise, emit an error about the invalid digraph, but continue
9223 parsing because we got our argument list. */
9224 pedwarn ("%<<::%> cannot begin a template-argument list");
9225 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9226 "between %<<%> and %<::%>");
9227 if (!flag_permissive)
9228 {
9229 static bool hint;
9230 if (!hint)
9231 {
9232 inform ("(if you use -fpermissive G++ will accept your code)");
9233 hint = true;
9234 }
9235 }
9236 }
9237 else
9238 {
9239 /* Look for the `<' that starts the template-argument-list. */
9240 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9241 {
9242 pop_deferring_access_checks ();
9243 return error_mark_node;
9244 }
9245 /* Parse the arguments. */
9246 arguments = cp_parser_enclosed_template_argument_list (parser);
9247 }
9248
9249 /* Build a representation of the specialization. */
9250 if (TREE_CODE (template) == IDENTIFIER_NODE)
9251 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9252 else if (DECL_CLASS_TEMPLATE_P (template)
9253 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9254 {
9255 bool entering_scope;
9256 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9257 template (rather than some instantiation thereof) only if
9258 is not nested within some other construct. For example, in
9259 "template <typename T> void f(T) { A<T>::", A<T> is just an
9260 instantiation of A. */
9261 entering_scope = (template_parm_scope_p ()
9262 && cp_lexer_next_token_is (parser->lexer,
9263 CPP_SCOPE));
9264 template_id
9265 = finish_template_type (template, arguments, entering_scope);
9266 }
9267 else
9268 {
9269 /* If it's not a class-template or a template-template, it should be
9270 a function-template. */
9271 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9272 || TREE_CODE (template) == OVERLOAD
9273 || BASELINK_P (template)));
9274
9275 template_id = lookup_template_function (template, arguments);
9276 }
9277
9278 /* If parsing tentatively, replace the sequence of tokens that makes
9279 up the template-id with a CPP_TEMPLATE_ID token. That way,
9280 should we re-parse the token stream, we will not have to repeat
9281 the effort required to do the parse, nor will we issue duplicate
9282 error messages about problems during instantiation of the
9283 template. */
9284 if (start_of_id)
9285 {
9286 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9287
9288 /* Reset the contents of the START_OF_ID token. */
9289 token->type = CPP_TEMPLATE_ID;
9290 /* Retrieve any deferred checks. Do not pop this access checks yet
9291 so the memory will not be reclaimed during token replacing below. */
9292 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9293 token->u.tree_check_value->value = template_id;
9294 token->u.tree_check_value->checks = get_deferred_access_checks ();
9295 token->keyword = RID_MAX;
9296
9297 /* Purge all subsequent tokens. */
9298 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9299
9300 /* ??? Can we actually assume that, if template_id ==
9301 error_mark_node, we will have issued a diagnostic to the
9302 user, as opposed to simply marking the tentative parse as
9303 failed? */
9304 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9305 error ("parse error in template argument list");
9306 }
9307
9308 pop_deferring_access_checks ();
9309 return template_id;
9310 }
9311
9312 /* Parse a template-name.
9313
9314 template-name:
9315 identifier
9316
9317 The standard should actually say:
9318
9319 template-name:
9320 identifier
9321 operator-function-id
9322
9323 A defect report has been filed about this issue.
9324
9325 A conversion-function-id cannot be a template name because they cannot
9326 be part of a template-id. In fact, looking at this code:
9327
9328 a.operator K<int>()
9329
9330 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9331 It is impossible to call a templated conversion-function-id with an
9332 explicit argument list, since the only allowed template parameter is
9333 the type to which it is converting.
9334
9335 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9336 `template' keyword, in a construction like:
9337
9338 T::template f<3>()
9339
9340 In that case `f' is taken to be a template-name, even though there
9341 is no way of knowing for sure.
9342
9343 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9344 name refers to a set of overloaded functions, at least one of which
9345 is a template, or an IDENTIFIER_NODE with the name of the template,
9346 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9347 names are looked up inside uninstantiated templates. */
9348
9349 static tree
9350 cp_parser_template_name (cp_parser* parser,
9351 bool template_keyword_p,
9352 bool check_dependency_p,
9353 bool is_declaration,
9354 bool *is_identifier)
9355 {
9356 tree identifier;
9357 tree decl;
9358 tree fns;
9359
9360 /* If the next token is `operator', then we have either an
9361 operator-function-id or a conversion-function-id. */
9362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9363 {
9364 /* We don't know whether we're looking at an
9365 operator-function-id or a conversion-function-id. */
9366 cp_parser_parse_tentatively (parser);
9367 /* Try an operator-function-id. */
9368 identifier = cp_parser_operator_function_id (parser);
9369 /* If that didn't work, try a conversion-function-id. */
9370 if (!cp_parser_parse_definitely (parser))
9371 {
9372 cp_parser_error (parser, "expected template-name");
9373 return error_mark_node;
9374 }
9375 }
9376 /* Look for the identifier. */
9377 else
9378 identifier = cp_parser_identifier (parser);
9379
9380 /* If we didn't find an identifier, we don't have a template-id. */
9381 if (identifier == error_mark_node)
9382 return error_mark_node;
9383
9384 /* If the name immediately followed the `template' keyword, then it
9385 is a template-name. However, if the next token is not `<', then
9386 we do not treat it as a template-name, since it is not being used
9387 as part of a template-id. This enables us to handle constructs
9388 like:
9389
9390 template <typename T> struct S { S(); };
9391 template <typename T> S<T>::S();
9392
9393 correctly. We would treat `S' as a template -- if it were `S<T>'
9394 -- but we do not if there is no `<'. */
9395
9396 if (processing_template_decl
9397 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9398 {
9399 /* In a declaration, in a dependent context, we pretend that the
9400 "template" keyword was present in order to improve error
9401 recovery. For example, given:
9402
9403 template <typename T> void f(T::X<int>);
9404
9405 we want to treat "X<int>" as a template-id. */
9406 if (is_declaration
9407 && !template_keyword_p
9408 && parser->scope && TYPE_P (parser->scope)
9409 && check_dependency_p
9410 && dependent_type_p (parser->scope)
9411 /* Do not do this for dtors (or ctors), since they never
9412 need the template keyword before their name. */
9413 && !constructor_name_p (identifier, parser->scope))
9414 {
9415 cp_token_position start = 0;
9416
9417 /* Explain what went wrong. */
9418 error ("non-template %qD used as template", identifier);
9419 inform ("use %<%T::template %D%> to indicate that it is a template",
9420 parser->scope, identifier);
9421 /* If parsing tentatively, find the location of the "<" token. */
9422 if (cp_parser_simulate_error (parser))
9423 start = cp_lexer_token_position (parser->lexer, true);
9424 /* Parse the template arguments so that we can issue error
9425 messages about them. */
9426 cp_lexer_consume_token (parser->lexer);
9427 cp_parser_enclosed_template_argument_list (parser);
9428 /* Skip tokens until we find a good place from which to
9429 continue parsing. */
9430 cp_parser_skip_to_closing_parenthesis (parser,
9431 /*recovering=*/true,
9432 /*or_comma=*/true,
9433 /*consume_paren=*/false);
9434 /* If parsing tentatively, permanently remove the
9435 template argument list. That will prevent duplicate
9436 error messages from being issued about the missing
9437 "template" keyword. */
9438 if (start)
9439 cp_lexer_purge_tokens_after (parser->lexer, start);
9440 if (is_identifier)
9441 *is_identifier = true;
9442 return identifier;
9443 }
9444
9445 /* If the "template" keyword is present, then there is generally
9446 no point in doing name-lookup, so we just return IDENTIFIER.
9447 But, if the qualifying scope is non-dependent then we can
9448 (and must) do name-lookup normally. */
9449 if (template_keyword_p
9450 && (!parser->scope
9451 || (TYPE_P (parser->scope)
9452 && dependent_type_p (parser->scope))))
9453 return identifier;
9454 }
9455
9456 /* Look up the name. */
9457 decl = cp_parser_lookup_name (parser, identifier,
9458 none_type,
9459 /*is_template=*/false,
9460 /*is_namespace=*/false,
9461 check_dependency_p,
9462 /*ambiguous_decls=*/NULL);
9463 decl = maybe_get_template_decl_from_type_decl (decl);
9464
9465 /* If DECL is a template, then the name was a template-name. */
9466 if (TREE_CODE (decl) == TEMPLATE_DECL)
9467 ;
9468 else
9469 {
9470 tree fn = NULL_TREE;
9471
9472 /* The standard does not explicitly indicate whether a name that
9473 names a set of overloaded declarations, some of which are
9474 templates, is a template-name. However, such a name should
9475 be a template-name; otherwise, there is no way to form a
9476 template-id for the overloaded templates. */
9477 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9478 if (TREE_CODE (fns) == OVERLOAD)
9479 for (fn = fns; fn; fn = OVL_NEXT (fn))
9480 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9481 break;
9482
9483 if (!fn)
9484 {
9485 /* The name does not name a template. */
9486 cp_parser_error (parser, "expected template-name");
9487 return error_mark_node;
9488 }
9489 }
9490
9491 /* If DECL is dependent, and refers to a function, then just return
9492 its name; we will look it up again during template instantiation. */
9493 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9494 {
9495 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9496 if (TYPE_P (scope) && dependent_type_p (scope))
9497 return identifier;
9498 }
9499
9500 return decl;
9501 }
9502
9503 /* Parse a template-argument-list.
9504
9505 template-argument-list:
9506 template-argument ... [opt]
9507 template-argument-list , template-argument ... [opt]
9508
9509 Returns a TREE_VEC containing the arguments. */
9510
9511 static tree
9512 cp_parser_template_argument_list (cp_parser* parser)
9513 {
9514 tree fixed_args[10];
9515 unsigned n_args = 0;
9516 unsigned alloced = 10;
9517 tree *arg_ary = fixed_args;
9518 tree vec;
9519 bool saved_in_template_argument_list_p;
9520 bool saved_ice_p;
9521 bool saved_non_ice_p;
9522
9523 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9524 parser->in_template_argument_list_p = true;
9525 /* Even if the template-id appears in an integral
9526 constant-expression, the contents of the argument list do
9527 not. */
9528 saved_ice_p = parser->integral_constant_expression_p;
9529 parser->integral_constant_expression_p = false;
9530 saved_non_ice_p = parser->non_integral_constant_expression_p;
9531 parser->non_integral_constant_expression_p = false;
9532 /* Parse the arguments. */
9533 do
9534 {
9535 tree argument;
9536
9537 if (n_args)
9538 /* Consume the comma. */
9539 cp_lexer_consume_token (parser->lexer);
9540
9541 /* Parse the template-argument. */
9542 argument = cp_parser_template_argument (parser);
9543
9544 /* If the next token is an ellipsis, we're expanding a template
9545 argument pack. */
9546 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9547 {
9548 /* Consume the `...' token. */
9549 cp_lexer_consume_token (parser->lexer);
9550
9551 /* Make the argument into a TYPE_PACK_EXPANSION or
9552 EXPR_PACK_EXPANSION. */
9553 argument = make_pack_expansion (argument);
9554 }
9555
9556 if (n_args == alloced)
9557 {
9558 alloced *= 2;
9559
9560 if (arg_ary == fixed_args)
9561 {
9562 arg_ary = XNEWVEC (tree, alloced);
9563 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9564 }
9565 else
9566 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9567 }
9568 arg_ary[n_args++] = argument;
9569 }
9570 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9571
9572 vec = make_tree_vec (n_args);
9573
9574 while (n_args--)
9575 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9576
9577 if (arg_ary != fixed_args)
9578 free (arg_ary);
9579 parser->non_integral_constant_expression_p = saved_non_ice_p;
9580 parser->integral_constant_expression_p = saved_ice_p;
9581 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9582 return vec;
9583 }
9584
9585 /* Parse a template-argument.
9586
9587 template-argument:
9588 assignment-expression
9589 type-id
9590 id-expression
9591
9592 The representation is that of an assignment-expression, type-id, or
9593 id-expression -- except that the qualified id-expression is
9594 evaluated, so that the value returned is either a DECL or an
9595 OVERLOAD.
9596
9597 Although the standard says "assignment-expression", it forbids
9598 throw-expressions or assignments in the template argument.
9599 Therefore, we use "conditional-expression" instead. */
9600
9601 static tree
9602 cp_parser_template_argument (cp_parser* parser)
9603 {
9604 tree argument;
9605 bool template_p;
9606 bool address_p;
9607 bool maybe_type_id = false;
9608 cp_token *token;
9609 cp_id_kind idk;
9610
9611 /* There's really no way to know what we're looking at, so we just
9612 try each alternative in order.
9613
9614 [temp.arg]
9615
9616 In a template-argument, an ambiguity between a type-id and an
9617 expression is resolved to a type-id, regardless of the form of
9618 the corresponding template-parameter.
9619
9620 Therefore, we try a type-id first. */
9621 cp_parser_parse_tentatively (parser);
9622 argument = cp_parser_type_id (parser);
9623 /* If there was no error parsing the type-id but the next token is a '>>',
9624 we probably found a typo for '> >'. But there are type-id which are
9625 also valid expressions. For instance:
9626
9627 struct X { int operator >> (int); };
9628 template <int V> struct Foo {};
9629 Foo<X () >> 5> r;
9630
9631 Here 'X()' is a valid type-id of a function type, but the user just
9632 wanted to write the expression "X() >> 5". Thus, we remember that we
9633 found a valid type-id, but we still try to parse the argument as an
9634 expression to see what happens. */
9635 if (!cp_parser_error_occurred (parser)
9636 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9637 {
9638 maybe_type_id = true;
9639 cp_parser_abort_tentative_parse (parser);
9640 }
9641 else
9642 {
9643 /* If the next token isn't a `,' or a `>', then this argument wasn't
9644 really finished. This means that the argument is not a valid
9645 type-id. */
9646 if (!cp_parser_next_token_ends_template_argument_p (parser))
9647 cp_parser_error (parser, "expected template-argument");
9648 /* If that worked, we're done. */
9649 if (cp_parser_parse_definitely (parser))
9650 return argument;
9651 }
9652 /* We're still not sure what the argument will be. */
9653 cp_parser_parse_tentatively (parser);
9654 /* Try a template. */
9655 argument = cp_parser_id_expression (parser,
9656 /*template_keyword_p=*/false,
9657 /*check_dependency_p=*/true,
9658 &template_p,
9659 /*declarator_p=*/false,
9660 /*optional_p=*/false);
9661 /* If the next token isn't a `,' or a `>', then this argument wasn't
9662 really finished. */
9663 if (!cp_parser_next_token_ends_template_argument_p (parser))
9664 cp_parser_error (parser, "expected template-argument");
9665 if (!cp_parser_error_occurred (parser))
9666 {
9667 /* Figure out what is being referred to. If the id-expression
9668 was for a class template specialization, then we will have a
9669 TYPE_DECL at this point. There is no need to do name lookup
9670 at this point in that case. */
9671 if (TREE_CODE (argument) != TYPE_DECL)
9672 argument = cp_parser_lookup_name (parser, argument,
9673 none_type,
9674 /*is_template=*/template_p,
9675 /*is_namespace=*/false,
9676 /*check_dependency=*/true,
9677 /*ambiguous_decls=*/NULL);
9678 if (TREE_CODE (argument) != TEMPLATE_DECL
9679 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9680 cp_parser_error (parser, "expected template-name");
9681 }
9682 if (cp_parser_parse_definitely (parser))
9683 return argument;
9684 /* It must be a non-type argument. There permitted cases are given
9685 in [temp.arg.nontype]:
9686
9687 -- an integral constant-expression of integral or enumeration
9688 type; or
9689
9690 -- the name of a non-type template-parameter; or
9691
9692 -- the name of an object or function with external linkage...
9693
9694 -- the address of an object or function with external linkage...
9695
9696 -- a pointer to member... */
9697 /* Look for a non-type template parameter. */
9698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9699 {
9700 cp_parser_parse_tentatively (parser);
9701 argument = cp_parser_primary_expression (parser,
9702 /*adress_p=*/false,
9703 /*cast_p=*/false,
9704 /*template_arg_p=*/true,
9705 &idk);
9706 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9707 || !cp_parser_next_token_ends_template_argument_p (parser))
9708 cp_parser_simulate_error (parser);
9709 if (cp_parser_parse_definitely (parser))
9710 return argument;
9711 }
9712
9713 /* If the next token is "&", the argument must be the address of an
9714 object or function with external linkage. */
9715 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9716 if (address_p)
9717 cp_lexer_consume_token (parser->lexer);
9718 /* See if we might have an id-expression. */
9719 token = cp_lexer_peek_token (parser->lexer);
9720 if (token->type == CPP_NAME
9721 || token->keyword == RID_OPERATOR
9722 || token->type == CPP_SCOPE
9723 || token->type == CPP_TEMPLATE_ID
9724 || token->type == CPP_NESTED_NAME_SPECIFIER)
9725 {
9726 cp_parser_parse_tentatively (parser);
9727 argument = cp_parser_primary_expression (parser,
9728 address_p,
9729 /*cast_p=*/false,
9730 /*template_arg_p=*/true,
9731 &idk);
9732 if (cp_parser_error_occurred (parser)
9733 || !cp_parser_next_token_ends_template_argument_p (parser))
9734 cp_parser_abort_tentative_parse (parser);
9735 else
9736 {
9737 if (TREE_CODE (argument) == INDIRECT_REF)
9738 {
9739 gcc_assert (REFERENCE_REF_P (argument));
9740 argument = TREE_OPERAND (argument, 0);
9741 }
9742
9743 if (TREE_CODE (argument) == VAR_DECL)
9744 {
9745 /* A variable without external linkage might still be a
9746 valid constant-expression, so no error is issued here
9747 if the external-linkage check fails. */
9748 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9749 cp_parser_simulate_error (parser);
9750 }
9751 else if (is_overloaded_fn (argument))
9752 /* All overloaded functions are allowed; if the external
9753 linkage test does not pass, an error will be issued
9754 later. */
9755 ;
9756 else if (address_p
9757 && (TREE_CODE (argument) == OFFSET_REF
9758 || TREE_CODE (argument) == SCOPE_REF))
9759 /* A pointer-to-member. */
9760 ;
9761 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9762 ;
9763 else
9764 cp_parser_simulate_error (parser);
9765
9766 if (cp_parser_parse_definitely (parser))
9767 {
9768 if (address_p)
9769 argument = build_x_unary_op (ADDR_EXPR, argument);
9770 return argument;
9771 }
9772 }
9773 }
9774 /* If the argument started with "&", there are no other valid
9775 alternatives at this point. */
9776 if (address_p)
9777 {
9778 cp_parser_error (parser, "invalid non-type template argument");
9779 return error_mark_node;
9780 }
9781
9782 /* If the argument wasn't successfully parsed as a type-id followed
9783 by '>>', the argument can only be a constant expression now.
9784 Otherwise, we try parsing the constant-expression tentatively,
9785 because the argument could really be a type-id. */
9786 if (maybe_type_id)
9787 cp_parser_parse_tentatively (parser);
9788 argument = cp_parser_constant_expression (parser,
9789 /*allow_non_constant_p=*/false,
9790 /*non_constant_p=*/NULL);
9791 argument = fold_non_dependent_expr (argument);
9792 if (!maybe_type_id)
9793 return argument;
9794 if (!cp_parser_next_token_ends_template_argument_p (parser))
9795 cp_parser_error (parser, "expected template-argument");
9796 if (cp_parser_parse_definitely (parser))
9797 return argument;
9798 /* We did our best to parse the argument as a non type-id, but that
9799 was the only alternative that matched (albeit with a '>' after
9800 it). We can assume it's just a typo from the user, and a
9801 diagnostic will then be issued. */
9802 return cp_parser_type_id (parser);
9803 }
9804
9805 /* Parse an explicit-instantiation.
9806
9807 explicit-instantiation:
9808 template declaration
9809
9810 Although the standard says `declaration', what it really means is:
9811
9812 explicit-instantiation:
9813 template decl-specifier-seq [opt] declarator [opt] ;
9814
9815 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9816 supposed to be allowed. A defect report has been filed about this
9817 issue.
9818
9819 GNU Extension:
9820
9821 explicit-instantiation:
9822 storage-class-specifier template
9823 decl-specifier-seq [opt] declarator [opt] ;
9824 function-specifier template
9825 decl-specifier-seq [opt] declarator [opt] ; */
9826
9827 static void
9828 cp_parser_explicit_instantiation (cp_parser* parser)
9829 {
9830 int declares_class_or_enum;
9831 cp_decl_specifier_seq decl_specifiers;
9832 tree extension_specifier = NULL_TREE;
9833
9834 /* Look for an (optional) storage-class-specifier or
9835 function-specifier. */
9836 if (cp_parser_allow_gnu_extensions_p (parser))
9837 {
9838 extension_specifier
9839 = cp_parser_storage_class_specifier_opt (parser);
9840 if (!extension_specifier)
9841 extension_specifier
9842 = cp_parser_function_specifier_opt (parser,
9843 /*decl_specs=*/NULL);
9844 }
9845
9846 /* Look for the `template' keyword. */
9847 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9848 /* Let the front end know that we are processing an explicit
9849 instantiation. */
9850 begin_explicit_instantiation ();
9851 /* [temp.explicit] says that we are supposed to ignore access
9852 control while processing explicit instantiation directives. */
9853 push_deferring_access_checks (dk_no_check);
9854 /* Parse a decl-specifier-seq. */
9855 cp_parser_decl_specifier_seq (parser,
9856 CP_PARSER_FLAGS_OPTIONAL,
9857 &decl_specifiers,
9858 &declares_class_or_enum);
9859 /* If there was exactly one decl-specifier, and it declared a class,
9860 and there's no declarator, then we have an explicit type
9861 instantiation. */
9862 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9863 {
9864 tree type;
9865
9866 type = check_tag_decl (&decl_specifiers);
9867 /* Turn access control back on for names used during
9868 template instantiation. */
9869 pop_deferring_access_checks ();
9870 if (type)
9871 do_type_instantiation (type, extension_specifier,
9872 /*complain=*/tf_error);
9873 }
9874 else
9875 {
9876 cp_declarator *declarator;
9877 tree decl;
9878
9879 /* Parse the declarator. */
9880 declarator
9881 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9882 /*ctor_dtor_or_conv_p=*/NULL,
9883 /*parenthesized_p=*/NULL,
9884 /*member_p=*/false);
9885 if (declares_class_or_enum & 2)
9886 cp_parser_check_for_definition_in_return_type (declarator,
9887 decl_specifiers.type);
9888 if (declarator != cp_error_declarator)
9889 {
9890 decl = grokdeclarator (declarator, &decl_specifiers,
9891 NORMAL, 0, &decl_specifiers.attributes);
9892 /* Turn access control back on for names used during
9893 template instantiation. */
9894 pop_deferring_access_checks ();
9895 /* Do the explicit instantiation. */
9896 do_decl_instantiation (decl, extension_specifier);
9897 }
9898 else
9899 {
9900 pop_deferring_access_checks ();
9901 /* Skip the body of the explicit instantiation. */
9902 cp_parser_skip_to_end_of_statement (parser);
9903 }
9904 }
9905 /* We're done with the instantiation. */
9906 end_explicit_instantiation ();
9907
9908 cp_parser_consume_semicolon_at_end_of_statement (parser);
9909 }
9910
9911 /* Parse an explicit-specialization.
9912
9913 explicit-specialization:
9914 template < > declaration
9915
9916 Although the standard says `declaration', what it really means is:
9917
9918 explicit-specialization:
9919 template <> decl-specifier [opt] init-declarator [opt] ;
9920 template <> function-definition
9921 template <> explicit-specialization
9922 template <> template-declaration */
9923
9924 static void
9925 cp_parser_explicit_specialization (cp_parser* parser)
9926 {
9927 bool need_lang_pop;
9928 /* Look for the `template' keyword. */
9929 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9930 /* Look for the `<'. */
9931 cp_parser_require (parser, CPP_LESS, "`<'");
9932 /* Look for the `>'. */
9933 cp_parser_require (parser, CPP_GREATER, "`>'");
9934 /* We have processed another parameter list. */
9935 ++parser->num_template_parameter_lists;
9936 /* [temp]
9937
9938 A template ... explicit specialization ... shall not have C
9939 linkage. */
9940 if (current_lang_name == lang_name_c)
9941 {
9942 error ("template specialization with C linkage");
9943 /* Give it C++ linkage to avoid confusing other parts of the
9944 front end. */
9945 push_lang_context (lang_name_cplusplus);
9946 need_lang_pop = true;
9947 }
9948 else
9949 need_lang_pop = false;
9950 /* Let the front end know that we are beginning a specialization. */
9951 if (!begin_specialization ())
9952 {
9953 end_specialization ();
9954 cp_parser_skip_to_end_of_block_or_statement (parser);
9955 return;
9956 }
9957
9958 /* If the next keyword is `template', we need to figure out whether
9959 or not we're looking a template-declaration. */
9960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9961 {
9962 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9963 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9964 cp_parser_template_declaration_after_export (parser,
9965 /*member_p=*/false);
9966 else
9967 cp_parser_explicit_specialization (parser);
9968 }
9969 else
9970 /* Parse the dependent declaration. */
9971 cp_parser_single_declaration (parser,
9972 /*checks=*/NULL,
9973 /*member_p=*/false,
9974 /*friend_p=*/NULL);
9975 /* We're done with the specialization. */
9976 end_specialization ();
9977 /* For the erroneous case of a template with C linkage, we pushed an
9978 implicit C++ linkage scope; exit that scope now. */
9979 if (need_lang_pop)
9980 pop_lang_context ();
9981 /* We're done with this parameter list. */
9982 --parser->num_template_parameter_lists;
9983 }
9984
9985 /* Parse a type-specifier.
9986
9987 type-specifier:
9988 simple-type-specifier
9989 class-specifier
9990 enum-specifier
9991 elaborated-type-specifier
9992 cv-qualifier
9993
9994 GNU Extension:
9995
9996 type-specifier:
9997 __complex__
9998
9999 Returns a representation of the type-specifier. For a
10000 class-specifier, enum-specifier, or elaborated-type-specifier, a
10001 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10002
10003 The parser flags FLAGS is used to control type-specifier parsing.
10004
10005 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10006 in a decl-specifier-seq.
10007
10008 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10009 class-specifier, enum-specifier, or elaborated-type-specifier, then
10010 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10011 if a type is declared; 2 if it is defined. Otherwise, it is set to
10012 zero.
10013
10014 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10015 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10016 is set to FALSE. */
10017
10018 static tree
10019 cp_parser_type_specifier (cp_parser* parser,
10020 cp_parser_flags flags,
10021 cp_decl_specifier_seq *decl_specs,
10022 bool is_declaration,
10023 int* declares_class_or_enum,
10024 bool* is_cv_qualifier)
10025 {
10026 tree type_spec = NULL_TREE;
10027 cp_token *token;
10028 enum rid keyword;
10029 cp_decl_spec ds = ds_last;
10030
10031 /* Assume this type-specifier does not declare a new type. */
10032 if (declares_class_or_enum)
10033 *declares_class_or_enum = 0;
10034 /* And that it does not specify a cv-qualifier. */
10035 if (is_cv_qualifier)
10036 *is_cv_qualifier = false;
10037 /* Peek at the next token. */
10038 token = cp_lexer_peek_token (parser->lexer);
10039
10040 /* If we're looking at a keyword, we can use that to guide the
10041 production we choose. */
10042 keyword = token->keyword;
10043 switch (keyword)
10044 {
10045 case RID_ENUM:
10046 /* Look for the enum-specifier. */
10047 type_spec = cp_parser_enum_specifier (parser);
10048 /* If that worked, we're done. */
10049 if (type_spec)
10050 {
10051 if (declares_class_or_enum)
10052 *declares_class_or_enum = 2;
10053 if (decl_specs)
10054 cp_parser_set_decl_spec_type (decl_specs,
10055 type_spec,
10056 /*user_defined_p=*/true);
10057 return type_spec;
10058 }
10059 else
10060 goto elaborated_type_specifier;
10061
10062 /* Any of these indicate either a class-specifier, or an
10063 elaborated-type-specifier. */
10064 case RID_CLASS:
10065 case RID_STRUCT:
10066 case RID_UNION:
10067 /* Parse tentatively so that we can back up if we don't find a
10068 class-specifier. */
10069 cp_parser_parse_tentatively (parser);
10070 /* Look for the class-specifier. */
10071 type_spec = cp_parser_class_specifier (parser);
10072 /* If that worked, we're done. */
10073 if (cp_parser_parse_definitely (parser))
10074 {
10075 if (declares_class_or_enum)
10076 *declares_class_or_enum = 2;
10077 if (decl_specs)
10078 cp_parser_set_decl_spec_type (decl_specs,
10079 type_spec,
10080 /*user_defined_p=*/true);
10081 return type_spec;
10082 }
10083
10084 /* Fall through. */
10085 elaborated_type_specifier:
10086 /* We're declaring (not defining) a class or enum. */
10087 if (declares_class_or_enum)
10088 *declares_class_or_enum = 1;
10089
10090 /* Fall through. */
10091 case RID_TYPENAME:
10092 /* Look for an elaborated-type-specifier. */
10093 type_spec
10094 = (cp_parser_elaborated_type_specifier
10095 (parser,
10096 decl_specs && decl_specs->specs[(int) ds_friend],
10097 is_declaration));
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 case RID_CONST:
10105 ds = ds_const;
10106 if (is_cv_qualifier)
10107 *is_cv_qualifier = true;
10108 break;
10109
10110 case RID_VOLATILE:
10111 ds = ds_volatile;
10112 if (is_cv_qualifier)
10113 *is_cv_qualifier = true;
10114 break;
10115
10116 case RID_RESTRICT:
10117 ds = ds_restrict;
10118 if (is_cv_qualifier)
10119 *is_cv_qualifier = true;
10120 break;
10121
10122 case RID_COMPLEX:
10123 /* The `__complex__' keyword is a GNU extension. */
10124 ds = ds_complex;
10125 break;
10126
10127 default:
10128 break;
10129 }
10130
10131 /* Handle simple keywords. */
10132 if (ds != ds_last)
10133 {
10134 if (decl_specs)
10135 {
10136 ++decl_specs->specs[(int)ds];
10137 decl_specs->any_specifiers_p = true;
10138 }
10139 return cp_lexer_consume_token (parser->lexer)->u.value;
10140 }
10141
10142 /* If we do not already have a type-specifier, assume we are looking
10143 at a simple-type-specifier. */
10144 type_spec = cp_parser_simple_type_specifier (parser,
10145 decl_specs,
10146 flags);
10147
10148 /* If we didn't find a type-specifier, and a type-specifier was not
10149 optional in this context, issue an error message. */
10150 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10151 {
10152 cp_parser_error (parser, "expected type specifier");
10153 return error_mark_node;
10154 }
10155
10156 return type_spec;
10157 }
10158
10159 /* Parse a simple-type-specifier.
10160
10161 simple-type-specifier:
10162 :: [opt] nested-name-specifier [opt] type-name
10163 :: [opt] nested-name-specifier template template-id
10164 char
10165 wchar_t
10166 bool
10167 short
10168 int
10169 long
10170 signed
10171 unsigned
10172 float
10173 double
10174 void
10175
10176 GNU Extension:
10177
10178 simple-type-specifier:
10179 __typeof__ unary-expression
10180 __typeof__ ( type-id )
10181
10182 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10183 appropriately updated. */
10184
10185 static tree
10186 cp_parser_simple_type_specifier (cp_parser* parser,
10187 cp_decl_specifier_seq *decl_specs,
10188 cp_parser_flags flags)
10189 {
10190 tree type = NULL_TREE;
10191 cp_token *token;
10192
10193 /* Peek at the next token. */
10194 token = cp_lexer_peek_token (parser->lexer);
10195
10196 /* If we're looking at a keyword, things are easy. */
10197 switch (token->keyword)
10198 {
10199 case RID_CHAR:
10200 if (decl_specs)
10201 decl_specs->explicit_char_p = true;
10202 type = char_type_node;
10203 break;
10204 case RID_WCHAR:
10205 type = wchar_type_node;
10206 break;
10207 case RID_BOOL:
10208 type = boolean_type_node;
10209 break;
10210 case RID_SHORT:
10211 if (decl_specs)
10212 ++decl_specs->specs[(int) ds_short];
10213 type = short_integer_type_node;
10214 break;
10215 case RID_INT:
10216 if (decl_specs)
10217 decl_specs->explicit_int_p = true;
10218 type = integer_type_node;
10219 break;
10220 case RID_LONG:
10221 if (decl_specs)
10222 ++decl_specs->specs[(int) ds_long];
10223 type = long_integer_type_node;
10224 break;
10225 case RID_SIGNED:
10226 if (decl_specs)
10227 ++decl_specs->specs[(int) ds_signed];
10228 type = integer_type_node;
10229 break;
10230 case RID_UNSIGNED:
10231 if (decl_specs)
10232 ++decl_specs->specs[(int) ds_unsigned];
10233 type = unsigned_type_node;
10234 break;
10235 case RID_FLOAT:
10236 type = float_type_node;
10237 break;
10238 case RID_DOUBLE:
10239 type = double_type_node;
10240 break;
10241 case RID_VOID:
10242 type = void_type_node;
10243 break;
10244
10245 case RID_TYPEOF:
10246 /* Consume the `typeof' token. */
10247 cp_lexer_consume_token (parser->lexer);
10248 /* Parse the operand to `typeof'. */
10249 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10250 /* If it is not already a TYPE, take its type. */
10251 if (!TYPE_P (type))
10252 type = finish_typeof (type);
10253
10254 if (decl_specs)
10255 cp_parser_set_decl_spec_type (decl_specs, type,
10256 /*user_defined_p=*/true);
10257
10258 return type;
10259
10260 default:
10261 break;
10262 }
10263
10264 /* If the type-specifier was for a built-in type, we're done. */
10265 if (type)
10266 {
10267 tree id;
10268
10269 /* Record the type. */
10270 if (decl_specs
10271 && (token->keyword != RID_SIGNED
10272 && token->keyword != RID_UNSIGNED
10273 && token->keyword != RID_SHORT
10274 && token->keyword != RID_LONG))
10275 cp_parser_set_decl_spec_type (decl_specs,
10276 type,
10277 /*user_defined=*/false);
10278 if (decl_specs)
10279 decl_specs->any_specifiers_p = true;
10280
10281 /* Consume the token. */
10282 id = cp_lexer_consume_token (parser->lexer)->u.value;
10283
10284 /* There is no valid C++ program where a non-template type is
10285 followed by a "<". That usually indicates that the user thought
10286 that the type was a template. */
10287 cp_parser_check_for_invalid_template_id (parser, type);
10288
10289 return TYPE_NAME (type);
10290 }
10291
10292 /* The type-specifier must be a user-defined type. */
10293 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10294 {
10295 bool qualified_p;
10296 bool global_p;
10297
10298 /* Don't gobble tokens or issue error messages if this is an
10299 optional type-specifier. */
10300 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10301 cp_parser_parse_tentatively (parser);
10302
10303 /* Look for the optional `::' operator. */
10304 global_p
10305 = (cp_parser_global_scope_opt (parser,
10306 /*current_scope_valid_p=*/false)
10307 != NULL_TREE);
10308 /* Look for the nested-name specifier. */
10309 qualified_p
10310 = (cp_parser_nested_name_specifier_opt (parser,
10311 /*typename_keyword_p=*/false,
10312 /*check_dependency_p=*/true,
10313 /*type_p=*/false,
10314 /*is_declaration=*/false)
10315 != NULL_TREE);
10316 /* If we have seen a nested-name-specifier, and the next token
10317 is `template', then we are using the template-id production. */
10318 if (parser->scope
10319 && cp_parser_optional_template_keyword (parser))
10320 {
10321 /* Look for the template-id. */
10322 type = cp_parser_template_id (parser,
10323 /*template_keyword_p=*/true,
10324 /*check_dependency_p=*/true,
10325 /*is_declaration=*/false);
10326 /* If the template-id did not name a type, we are out of
10327 luck. */
10328 if (TREE_CODE (type) != TYPE_DECL)
10329 {
10330 cp_parser_error (parser, "expected template-id for type");
10331 type = NULL_TREE;
10332 }
10333 }
10334 /* Otherwise, look for a type-name. */
10335 else
10336 type = cp_parser_type_name (parser);
10337 /* Keep track of all name-lookups performed in class scopes. */
10338 if (type
10339 && !global_p
10340 && !qualified_p
10341 && TREE_CODE (type) == TYPE_DECL
10342 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10343 maybe_note_name_used_in_class (DECL_NAME (type), type);
10344 /* If it didn't work out, we don't have a TYPE. */
10345 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10346 && !cp_parser_parse_definitely (parser))
10347 type = NULL_TREE;
10348 if (type && decl_specs)
10349 cp_parser_set_decl_spec_type (decl_specs, type,
10350 /*user_defined=*/true);
10351 }
10352
10353 /* If we didn't get a type-name, issue an error message. */
10354 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10355 {
10356 cp_parser_error (parser, "expected type-name");
10357 return error_mark_node;
10358 }
10359
10360 /* There is no valid C++ program where a non-template type is
10361 followed by a "<". That usually indicates that the user thought
10362 that the type was a template. */
10363 if (type && type != error_mark_node)
10364 {
10365 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10366 If it is, then the '<'...'>' enclose protocol names rather than
10367 template arguments, and so everything is fine. */
10368 if (c_dialect_objc ()
10369 && (objc_is_id (type) || objc_is_class_name (type)))
10370 {
10371 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10372 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10373
10374 /* Clobber the "unqualified" type previously entered into
10375 DECL_SPECS with the new, improved protocol-qualified version. */
10376 if (decl_specs)
10377 decl_specs->type = qual_type;
10378
10379 return qual_type;
10380 }
10381
10382 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10383 }
10384
10385 return type;
10386 }
10387
10388 /* Parse a type-name.
10389
10390 type-name:
10391 class-name
10392 enum-name
10393 typedef-name
10394
10395 enum-name:
10396 identifier
10397
10398 typedef-name:
10399 identifier
10400
10401 Returns a TYPE_DECL for the type. */
10402
10403 static tree
10404 cp_parser_type_name (cp_parser* parser)
10405 {
10406 tree type_decl;
10407 tree identifier;
10408
10409 /* We can't know yet whether it is a class-name or not. */
10410 cp_parser_parse_tentatively (parser);
10411 /* Try a class-name. */
10412 type_decl = cp_parser_class_name (parser,
10413 /*typename_keyword_p=*/false,
10414 /*template_keyword_p=*/false,
10415 none_type,
10416 /*check_dependency_p=*/true,
10417 /*class_head_p=*/false,
10418 /*is_declaration=*/false);
10419 /* If it's not a class-name, keep looking. */
10420 if (!cp_parser_parse_definitely (parser))
10421 {
10422 /* It must be a typedef-name or an enum-name. */
10423 identifier = cp_parser_identifier (parser);
10424 if (identifier == error_mark_node)
10425 return error_mark_node;
10426
10427 /* Look up the type-name. */
10428 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10429
10430 if (TREE_CODE (type_decl) != TYPE_DECL
10431 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10432 {
10433 /* See if this is an Objective-C type. */
10434 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10435 tree type = objc_get_protocol_qualified_type (identifier, protos);
10436 if (type)
10437 type_decl = TYPE_NAME (type);
10438 }
10439
10440 /* Issue an error if we did not find a type-name. */
10441 if (TREE_CODE (type_decl) != TYPE_DECL)
10442 {
10443 if (!cp_parser_simulate_error (parser))
10444 cp_parser_name_lookup_error (parser, identifier, type_decl,
10445 "is not a type");
10446 type_decl = error_mark_node;
10447 }
10448 /* Remember that the name was used in the definition of the
10449 current class so that we can check later to see if the
10450 meaning would have been different after the class was
10451 entirely defined. */
10452 else if (type_decl != error_mark_node
10453 && !parser->scope)
10454 maybe_note_name_used_in_class (identifier, type_decl);
10455 }
10456
10457 return type_decl;
10458 }
10459
10460
10461 /* Parse an elaborated-type-specifier. Note that the grammar given
10462 here incorporates the resolution to DR68.
10463
10464 elaborated-type-specifier:
10465 class-key :: [opt] nested-name-specifier [opt] identifier
10466 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10467 enum :: [opt] nested-name-specifier [opt] identifier
10468 typename :: [opt] nested-name-specifier identifier
10469 typename :: [opt] nested-name-specifier template [opt]
10470 template-id
10471
10472 GNU extension:
10473
10474 elaborated-type-specifier:
10475 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10476 class-key attributes :: [opt] nested-name-specifier [opt]
10477 template [opt] template-id
10478 enum attributes :: [opt] nested-name-specifier [opt] identifier
10479
10480 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10481 declared `friend'. If IS_DECLARATION is TRUE, then this
10482 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10483 something is being declared.
10484
10485 Returns the TYPE specified. */
10486
10487 static tree
10488 cp_parser_elaborated_type_specifier (cp_parser* parser,
10489 bool is_friend,
10490 bool is_declaration)
10491 {
10492 enum tag_types tag_type;
10493 tree identifier;
10494 tree type = NULL_TREE;
10495 tree attributes = NULL_TREE;
10496
10497 /* See if we're looking at the `enum' keyword. */
10498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10499 {
10500 /* Consume the `enum' token. */
10501 cp_lexer_consume_token (parser->lexer);
10502 /* Remember that it's an enumeration type. */
10503 tag_type = enum_type;
10504 /* Parse the attributes. */
10505 attributes = cp_parser_attributes_opt (parser);
10506 }
10507 /* Or, it might be `typename'. */
10508 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10509 RID_TYPENAME))
10510 {
10511 /* Consume the `typename' token. */
10512 cp_lexer_consume_token (parser->lexer);
10513 /* Remember that it's a `typename' type. */
10514 tag_type = typename_type;
10515 /* The `typename' keyword is only allowed in templates. */
10516 if (!processing_template_decl)
10517 pedwarn ("using %<typename%> outside of template");
10518 }
10519 /* Otherwise it must be a class-key. */
10520 else
10521 {
10522 tag_type = cp_parser_class_key (parser);
10523 if (tag_type == none_type)
10524 return error_mark_node;
10525 /* Parse the attributes. */
10526 attributes = cp_parser_attributes_opt (parser);
10527 }
10528
10529 /* Look for the `::' operator. */
10530 cp_parser_global_scope_opt (parser,
10531 /*current_scope_valid_p=*/false);
10532 /* Look for the nested-name-specifier. */
10533 if (tag_type == typename_type)
10534 {
10535 if (!cp_parser_nested_name_specifier (parser,
10536 /*typename_keyword_p=*/true,
10537 /*check_dependency_p=*/true,
10538 /*type_p=*/true,
10539 is_declaration))
10540 return error_mark_node;
10541 }
10542 else
10543 /* Even though `typename' is not present, the proposed resolution
10544 to Core Issue 180 says that in `class A<T>::B', `B' should be
10545 considered a type-name, even if `A<T>' is dependent. */
10546 cp_parser_nested_name_specifier_opt (parser,
10547 /*typename_keyword_p=*/true,
10548 /*check_dependency_p=*/true,
10549 /*type_p=*/true,
10550 is_declaration);
10551 /* For everything but enumeration types, consider a template-id.
10552 For an enumeration type, consider only a plain identifier. */
10553 if (tag_type != enum_type)
10554 {
10555 bool template_p = false;
10556 tree decl;
10557
10558 /* Allow the `template' keyword. */
10559 template_p = cp_parser_optional_template_keyword (parser);
10560 /* If we didn't see `template', we don't know if there's a
10561 template-id or not. */
10562 if (!template_p)
10563 cp_parser_parse_tentatively (parser);
10564 /* Parse the template-id. */
10565 decl = cp_parser_template_id (parser, template_p,
10566 /*check_dependency_p=*/true,
10567 is_declaration);
10568 /* If we didn't find a template-id, look for an ordinary
10569 identifier. */
10570 if (!template_p && !cp_parser_parse_definitely (parser))
10571 ;
10572 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10573 in effect, then we must assume that, upon instantiation, the
10574 template will correspond to a class. */
10575 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10576 && tag_type == typename_type)
10577 type = make_typename_type (parser->scope, decl,
10578 typename_type,
10579 /*complain=*/tf_error);
10580 else
10581 type = TREE_TYPE (decl);
10582 }
10583
10584 if (!type)
10585 {
10586 identifier = cp_parser_identifier (parser);
10587
10588 if (identifier == error_mark_node)
10589 {
10590 parser->scope = NULL_TREE;
10591 return error_mark_node;
10592 }
10593
10594 /* For a `typename', we needn't call xref_tag. */
10595 if (tag_type == typename_type
10596 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10597 return cp_parser_make_typename_type (parser, parser->scope,
10598 identifier);
10599 /* Look up a qualified name in the usual way. */
10600 if (parser->scope)
10601 {
10602 tree decl;
10603
10604 decl = cp_parser_lookup_name (parser, identifier,
10605 tag_type,
10606 /*is_template=*/false,
10607 /*is_namespace=*/false,
10608 /*check_dependency=*/true,
10609 /*ambiguous_decls=*/NULL);
10610
10611 /* If we are parsing friend declaration, DECL may be a
10612 TEMPLATE_DECL tree node here. However, we need to check
10613 whether this TEMPLATE_DECL results in valid code. Consider
10614 the following example:
10615
10616 namespace N {
10617 template <class T> class C {};
10618 }
10619 class X {
10620 template <class T> friend class N::C; // #1, valid code
10621 };
10622 template <class T> class Y {
10623 friend class N::C; // #2, invalid code
10624 };
10625
10626 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10627 name lookup of `N::C'. We see that friend declaration must
10628 be template for the code to be valid. Note that
10629 processing_template_decl does not work here since it is
10630 always 1 for the above two cases. */
10631
10632 decl = (cp_parser_maybe_treat_template_as_class
10633 (decl, /*tag_name_p=*/is_friend
10634 && parser->num_template_parameter_lists));
10635
10636 if (TREE_CODE (decl) != TYPE_DECL)
10637 {
10638 cp_parser_diagnose_invalid_type_name (parser,
10639 parser->scope,
10640 identifier);
10641 return error_mark_node;
10642 }
10643
10644 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10645 {
10646 bool allow_template = (parser->num_template_parameter_lists
10647 || DECL_SELF_REFERENCE_P (decl));
10648 type = check_elaborated_type_specifier (tag_type, decl,
10649 allow_template);
10650
10651 if (type == error_mark_node)
10652 return error_mark_node;
10653 }
10654
10655 type = TREE_TYPE (decl);
10656 }
10657 else
10658 {
10659 /* An elaborated-type-specifier sometimes introduces a new type and
10660 sometimes names an existing type. Normally, the rule is that it
10661 introduces a new type only if there is not an existing type of
10662 the same name already in scope. For example, given:
10663
10664 struct S {};
10665 void f() { struct S s; }
10666
10667 the `struct S' in the body of `f' is the same `struct S' as in
10668 the global scope; the existing definition is used. However, if
10669 there were no global declaration, this would introduce a new
10670 local class named `S'.
10671
10672 An exception to this rule applies to the following code:
10673
10674 namespace N { struct S; }
10675
10676 Here, the elaborated-type-specifier names a new type
10677 unconditionally; even if there is already an `S' in the
10678 containing scope this declaration names a new type.
10679 This exception only applies if the elaborated-type-specifier
10680 forms the complete declaration:
10681
10682 [class.name]
10683
10684 A declaration consisting solely of `class-key identifier ;' is
10685 either a redeclaration of the name in the current scope or a
10686 forward declaration of the identifier as a class name. It
10687 introduces the name into the current scope.
10688
10689 We are in this situation precisely when the next token is a `;'.
10690
10691 An exception to the exception is that a `friend' declaration does
10692 *not* name a new type; i.e., given:
10693
10694 struct S { friend struct T; };
10695
10696 `T' is not a new type in the scope of `S'.
10697
10698 Also, `new struct S' or `sizeof (struct S)' never results in the
10699 definition of a new type; a new type can only be declared in a
10700 declaration context. */
10701
10702 tag_scope ts;
10703 bool template_p;
10704
10705 if (is_friend)
10706 /* Friends have special name lookup rules. */
10707 ts = ts_within_enclosing_non_class;
10708 else if (is_declaration
10709 && cp_lexer_next_token_is (parser->lexer,
10710 CPP_SEMICOLON))
10711 /* This is a `class-key identifier ;' */
10712 ts = ts_current;
10713 else
10714 ts = ts_global;
10715
10716 template_p =
10717 (parser->num_template_parameter_lists
10718 && (cp_parser_next_token_starts_class_definition_p (parser)
10719 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10720 /* An unqualified name was used to reference this type, so
10721 there were no qualifying templates. */
10722 if (!cp_parser_check_template_parameters (parser,
10723 /*num_templates=*/0))
10724 return error_mark_node;
10725 type = xref_tag (tag_type, identifier, ts, template_p);
10726 }
10727 }
10728
10729 if (type == error_mark_node)
10730 return error_mark_node;
10731
10732 /* Allow attributes on forward declarations of classes. */
10733 if (attributes)
10734 {
10735 if (TREE_CODE (type) == TYPENAME_TYPE)
10736 warning (OPT_Wattributes,
10737 "attributes ignored on uninstantiated type");
10738 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10739 && ! processing_explicit_instantiation)
10740 warning (OPT_Wattributes,
10741 "attributes ignored on template instantiation");
10742 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10743 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10744 else
10745 warning (OPT_Wattributes,
10746 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10747 }
10748
10749 if (tag_type != enum_type)
10750 cp_parser_check_class_key (tag_type, type);
10751
10752 /* A "<" cannot follow an elaborated type specifier. If that
10753 happens, the user was probably trying to form a template-id. */
10754 cp_parser_check_for_invalid_template_id (parser, type);
10755
10756 return type;
10757 }
10758
10759 /* Parse an enum-specifier.
10760
10761 enum-specifier:
10762 enum identifier [opt] { enumerator-list [opt] }
10763
10764 GNU Extensions:
10765 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10766 attributes[opt]
10767
10768 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10769 if the token stream isn't an enum-specifier after all. */
10770
10771 static tree
10772 cp_parser_enum_specifier (cp_parser* parser)
10773 {
10774 tree identifier;
10775 tree type;
10776 tree attributes;
10777
10778 /* Parse tentatively so that we can back up if we don't find a
10779 enum-specifier. */
10780 cp_parser_parse_tentatively (parser);
10781
10782 /* Caller guarantees that the current token is 'enum', an identifier
10783 possibly follows, and the token after that is an opening brace.
10784 If we don't have an identifier, fabricate an anonymous name for
10785 the enumeration being defined. */
10786 cp_lexer_consume_token (parser->lexer);
10787
10788 attributes = cp_parser_attributes_opt (parser);
10789
10790 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10791 identifier = cp_parser_identifier (parser);
10792 else
10793 identifier = make_anon_name ();
10794
10795 /* Look for the `{' but don't consume it yet. */
10796 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10797 cp_parser_simulate_error (parser);
10798
10799 if (!cp_parser_parse_definitely (parser))
10800 return NULL_TREE;
10801
10802 /* Issue an error message if type-definitions are forbidden here. */
10803 if (!cp_parser_check_type_definition (parser))
10804 type = error_mark_node;
10805 else
10806 /* Create the new type. We do this before consuming the opening
10807 brace so the enum will be recorded as being on the line of its
10808 tag (or the 'enum' keyword, if there is no tag). */
10809 type = start_enum (identifier);
10810
10811 /* Consume the opening brace. */
10812 cp_lexer_consume_token (parser->lexer);
10813
10814 if (type == error_mark_node)
10815 {
10816 cp_parser_skip_to_end_of_block_or_statement (parser);
10817 return error_mark_node;
10818 }
10819
10820 /* If the next token is not '}', then there are some enumerators. */
10821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10822 cp_parser_enumerator_list (parser, type);
10823
10824 /* Consume the final '}'. */
10825 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10826
10827 /* Look for trailing attributes to apply to this enumeration, and
10828 apply them if appropriate. */
10829 if (cp_parser_allow_gnu_extensions_p (parser))
10830 {
10831 tree trailing_attr = cp_parser_attributes_opt (parser);
10832 cplus_decl_attributes (&type,
10833 trailing_attr,
10834 (int) ATTR_FLAG_TYPE_IN_PLACE);
10835 }
10836
10837 /* Finish up the enumeration. */
10838 finish_enum (type);
10839
10840 return type;
10841 }
10842
10843 /* Parse an enumerator-list. The enumerators all have the indicated
10844 TYPE.
10845
10846 enumerator-list:
10847 enumerator-definition
10848 enumerator-list , enumerator-definition */
10849
10850 static void
10851 cp_parser_enumerator_list (cp_parser* parser, tree type)
10852 {
10853 while (true)
10854 {
10855 /* Parse an enumerator-definition. */
10856 cp_parser_enumerator_definition (parser, type);
10857
10858 /* If the next token is not a ',', we've reached the end of
10859 the list. */
10860 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10861 break;
10862 /* Otherwise, consume the `,' and keep going. */
10863 cp_lexer_consume_token (parser->lexer);
10864 /* If the next token is a `}', there is a trailing comma. */
10865 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10866 {
10867 if (pedantic && !in_system_header)
10868 pedwarn ("comma at end of enumerator list");
10869 break;
10870 }
10871 }
10872 }
10873
10874 /* Parse an enumerator-definition. The enumerator has the indicated
10875 TYPE.
10876
10877 enumerator-definition:
10878 enumerator
10879 enumerator = constant-expression
10880
10881 enumerator:
10882 identifier */
10883
10884 static void
10885 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10886 {
10887 tree identifier;
10888 tree value;
10889
10890 /* Look for the identifier. */
10891 identifier = cp_parser_identifier (parser);
10892 if (identifier == error_mark_node)
10893 return;
10894
10895 /* If the next token is an '=', then there is an explicit value. */
10896 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10897 {
10898 /* Consume the `=' token. */
10899 cp_lexer_consume_token (parser->lexer);
10900 /* Parse the value. */
10901 value = cp_parser_constant_expression (parser,
10902 /*allow_non_constant_p=*/false,
10903 NULL);
10904 }
10905 else
10906 value = NULL_TREE;
10907
10908 /* Create the enumerator. */
10909 build_enumerator (identifier, value, type);
10910 }
10911
10912 /* Parse a namespace-name.
10913
10914 namespace-name:
10915 original-namespace-name
10916 namespace-alias
10917
10918 Returns the NAMESPACE_DECL for the namespace. */
10919
10920 static tree
10921 cp_parser_namespace_name (cp_parser* parser)
10922 {
10923 tree identifier;
10924 tree namespace_decl;
10925
10926 /* Get the name of the namespace. */
10927 identifier = cp_parser_identifier (parser);
10928 if (identifier == error_mark_node)
10929 return error_mark_node;
10930
10931 /* Look up the identifier in the currently active scope. Look only
10932 for namespaces, due to:
10933
10934 [basic.lookup.udir]
10935
10936 When looking up a namespace-name in a using-directive or alias
10937 definition, only namespace names are considered.
10938
10939 And:
10940
10941 [basic.lookup.qual]
10942
10943 During the lookup of a name preceding the :: scope resolution
10944 operator, object, function, and enumerator names are ignored.
10945
10946 (Note that cp_parser_class_or_namespace_name only calls this
10947 function if the token after the name is the scope resolution
10948 operator.) */
10949 namespace_decl = cp_parser_lookup_name (parser, identifier,
10950 none_type,
10951 /*is_template=*/false,
10952 /*is_namespace=*/true,
10953 /*check_dependency=*/true,
10954 /*ambiguous_decls=*/NULL);
10955 /* If it's not a namespace, issue an error. */
10956 if (namespace_decl == error_mark_node
10957 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10958 {
10959 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10960 error ("%qD is not a namespace-name", identifier);
10961 cp_parser_error (parser, "expected namespace-name");
10962 namespace_decl = error_mark_node;
10963 }
10964
10965 return namespace_decl;
10966 }
10967
10968 /* Parse a namespace-definition.
10969
10970 namespace-definition:
10971 named-namespace-definition
10972 unnamed-namespace-definition
10973
10974 named-namespace-definition:
10975 original-namespace-definition
10976 extension-namespace-definition
10977
10978 original-namespace-definition:
10979 namespace identifier { namespace-body }
10980
10981 extension-namespace-definition:
10982 namespace original-namespace-name { namespace-body }
10983
10984 unnamed-namespace-definition:
10985 namespace { namespace-body } */
10986
10987 static void
10988 cp_parser_namespace_definition (cp_parser* parser)
10989 {
10990 tree identifier, attribs;
10991
10992 /* Look for the `namespace' keyword. */
10993 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10994
10995 /* Get the name of the namespace. We do not attempt to distinguish
10996 between an original-namespace-definition and an
10997 extension-namespace-definition at this point. The semantic
10998 analysis routines are responsible for that. */
10999 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11000 identifier = cp_parser_identifier (parser);
11001 else
11002 identifier = NULL_TREE;
11003
11004 /* Parse any specified attributes. */
11005 attribs = cp_parser_attributes_opt (parser);
11006
11007 /* Look for the `{' to start the namespace. */
11008 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11009 /* Start the namespace. */
11010 push_namespace_with_attribs (identifier, attribs);
11011 /* Parse the body of the namespace. */
11012 cp_parser_namespace_body (parser);
11013 /* Finish the namespace. */
11014 pop_namespace ();
11015 /* Look for the final `}'. */
11016 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11017 }
11018
11019 /* Parse a namespace-body.
11020
11021 namespace-body:
11022 declaration-seq [opt] */
11023
11024 static void
11025 cp_parser_namespace_body (cp_parser* parser)
11026 {
11027 cp_parser_declaration_seq_opt (parser);
11028 }
11029
11030 /* Parse a namespace-alias-definition.
11031
11032 namespace-alias-definition:
11033 namespace identifier = qualified-namespace-specifier ; */
11034
11035 static void
11036 cp_parser_namespace_alias_definition (cp_parser* parser)
11037 {
11038 tree identifier;
11039 tree namespace_specifier;
11040
11041 /* Look for the `namespace' keyword. */
11042 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11043 /* Look for the identifier. */
11044 identifier = cp_parser_identifier (parser);
11045 if (identifier == error_mark_node)
11046 return;
11047 /* Look for the `=' token. */
11048 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11049 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11050 {
11051 error ("%<namespace%> definition is not allowed here");
11052 /* Skip the definition. */
11053 cp_lexer_consume_token (parser->lexer);
11054 cp_parser_skip_to_closing_brace (parser);
11055 cp_lexer_consume_token (parser->lexer);
11056 return;
11057 }
11058 cp_parser_require (parser, CPP_EQ, "`='");
11059 /* Look for the qualified-namespace-specifier. */
11060 namespace_specifier
11061 = cp_parser_qualified_namespace_specifier (parser);
11062 /* Look for the `;' token. */
11063 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11064
11065 /* Register the alias in the symbol table. */
11066 do_namespace_alias (identifier, namespace_specifier);
11067 }
11068
11069 /* Parse a qualified-namespace-specifier.
11070
11071 qualified-namespace-specifier:
11072 :: [opt] nested-name-specifier [opt] namespace-name
11073
11074 Returns a NAMESPACE_DECL corresponding to the specified
11075 namespace. */
11076
11077 static tree
11078 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11079 {
11080 /* Look for the optional `::'. */
11081 cp_parser_global_scope_opt (parser,
11082 /*current_scope_valid_p=*/false);
11083
11084 /* Look for the optional nested-name-specifier. */
11085 cp_parser_nested_name_specifier_opt (parser,
11086 /*typename_keyword_p=*/false,
11087 /*check_dependency_p=*/true,
11088 /*type_p=*/false,
11089 /*is_declaration=*/true);
11090
11091 return cp_parser_namespace_name (parser);
11092 }
11093
11094 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11095 access declaration.
11096
11097 using-declaration:
11098 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11099 using :: unqualified-id ;
11100
11101 access-declaration:
11102 qualified-id ;
11103
11104 */
11105
11106 static bool
11107 cp_parser_using_declaration (cp_parser* parser,
11108 bool access_declaration_p)
11109 {
11110 cp_token *token;
11111 bool typename_p = false;
11112 bool global_scope_p;
11113 tree decl;
11114 tree identifier;
11115 tree qscope;
11116
11117 if (access_declaration_p)
11118 cp_parser_parse_tentatively (parser);
11119 else
11120 {
11121 /* Look for the `using' keyword. */
11122 cp_parser_require_keyword (parser, RID_USING, "`using'");
11123
11124 /* Peek at the next token. */
11125 token = cp_lexer_peek_token (parser->lexer);
11126 /* See if it's `typename'. */
11127 if (token->keyword == RID_TYPENAME)
11128 {
11129 /* Remember that we've seen it. */
11130 typename_p = true;
11131 /* Consume the `typename' token. */
11132 cp_lexer_consume_token (parser->lexer);
11133 }
11134 }
11135
11136 /* Look for the optional global scope qualification. */
11137 global_scope_p
11138 = (cp_parser_global_scope_opt (parser,
11139 /*current_scope_valid_p=*/false)
11140 != NULL_TREE);
11141
11142 /* If we saw `typename', or didn't see `::', then there must be a
11143 nested-name-specifier present. */
11144 if (typename_p || !global_scope_p)
11145 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11146 /*check_dependency_p=*/true,
11147 /*type_p=*/false,
11148 /*is_declaration=*/true);
11149 /* Otherwise, we could be in either of the two productions. In that
11150 case, treat the nested-name-specifier as optional. */
11151 else
11152 qscope = cp_parser_nested_name_specifier_opt (parser,
11153 /*typename_keyword_p=*/false,
11154 /*check_dependency_p=*/true,
11155 /*type_p=*/false,
11156 /*is_declaration=*/true);
11157 if (!qscope)
11158 qscope = global_namespace;
11159
11160 if (access_declaration_p && cp_parser_error_occurred (parser))
11161 /* Something has already gone wrong; there's no need to parse
11162 further. Since an error has occurred, the return value of
11163 cp_parser_parse_definitely will be false, as required. */
11164 return cp_parser_parse_definitely (parser);
11165
11166 /* Parse the unqualified-id. */
11167 identifier = cp_parser_unqualified_id (parser,
11168 /*template_keyword_p=*/false,
11169 /*check_dependency_p=*/true,
11170 /*declarator_p=*/true,
11171 /*optional_p=*/false);
11172
11173 if (access_declaration_p)
11174 {
11175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11176 cp_parser_simulate_error (parser);
11177 if (!cp_parser_parse_definitely (parser))
11178 return false;
11179 }
11180
11181 /* The function we call to handle a using-declaration is different
11182 depending on what scope we are in. */
11183 if (qscope == error_mark_node || identifier == error_mark_node)
11184 ;
11185 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11186 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11187 /* [namespace.udecl]
11188
11189 A using declaration shall not name a template-id. */
11190 error ("a template-id may not appear in a using-declaration");
11191 else
11192 {
11193 if (at_class_scope_p ())
11194 {
11195 /* Create the USING_DECL. */
11196 decl = do_class_using_decl (parser->scope, identifier);
11197 /* Add it to the list of members in this class. */
11198 finish_member_declaration (decl);
11199 }
11200 else
11201 {
11202 decl = cp_parser_lookup_name_simple (parser, identifier);
11203 if (decl == error_mark_node)
11204 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11205 else if (!at_namespace_scope_p ())
11206 do_local_using_decl (decl, qscope, identifier);
11207 else
11208 do_toplevel_using_decl (decl, qscope, identifier);
11209 }
11210 }
11211
11212 /* Look for the final `;'. */
11213 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11214
11215 return true;
11216 }
11217
11218 /* Parse a using-directive.
11219
11220 using-directive:
11221 using namespace :: [opt] nested-name-specifier [opt]
11222 namespace-name ; */
11223
11224 static void
11225 cp_parser_using_directive (cp_parser* parser)
11226 {
11227 tree namespace_decl;
11228 tree attribs;
11229
11230 /* Look for the `using' keyword. */
11231 cp_parser_require_keyword (parser, RID_USING, "`using'");
11232 /* And the `namespace' keyword. */
11233 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11234 /* Look for the optional `::' operator. */
11235 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11236 /* And the optional nested-name-specifier. */
11237 cp_parser_nested_name_specifier_opt (parser,
11238 /*typename_keyword_p=*/false,
11239 /*check_dependency_p=*/true,
11240 /*type_p=*/false,
11241 /*is_declaration=*/true);
11242 /* Get the namespace being used. */
11243 namespace_decl = cp_parser_namespace_name (parser);
11244 /* And any specified attributes. */
11245 attribs = cp_parser_attributes_opt (parser);
11246 /* Update the symbol table. */
11247 parse_using_directive (namespace_decl, attribs);
11248 /* Look for the final `;'. */
11249 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11250 }
11251
11252 /* Parse an asm-definition.
11253
11254 asm-definition:
11255 asm ( string-literal ) ;
11256
11257 GNU Extension:
11258
11259 asm-definition:
11260 asm volatile [opt] ( string-literal ) ;
11261 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11262 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11263 : asm-operand-list [opt] ) ;
11264 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11265 : asm-operand-list [opt]
11266 : asm-operand-list [opt] ) ; */
11267
11268 static void
11269 cp_parser_asm_definition (cp_parser* parser)
11270 {
11271 tree string;
11272 tree outputs = NULL_TREE;
11273 tree inputs = NULL_TREE;
11274 tree clobbers = NULL_TREE;
11275 tree asm_stmt;
11276 bool volatile_p = false;
11277 bool extended_p = false;
11278
11279 /* Look for the `asm' keyword. */
11280 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11281 /* See if the next token is `volatile'. */
11282 if (cp_parser_allow_gnu_extensions_p (parser)
11283 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11284 {
11285 /* Remember that we saw the `volatile' keyword. */
11286 volatile_p = true;
11287 /* Consume the token. */
11288 cp_lexer_consume_token (parser->lexer);
11289 }
11290 /* Look for the opening `('. */
11291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11292 return;
11293 /* Look for the string. */
11294 string = cp_parser_string_literal (parser, false, false);
11295 if (string == error_mark_node)
11296 {
11297 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11298 /*consume_paren=*/true);
11299 return;
11300 }
11301
11302 /* If we're allowing GNU extensions, check for the extended assembly
11303 syntax. Unfortunately, the `:' tokens need not be separated by
11304 a space in C, and so, for compatibility, we tolerate that here
11305 too. Doing that means that we have to treat the `::' operator as
11306 two `:' tokens. */
11307 if (cp_parser_allow_gnu_extensions_p (parser)
11308 && parser->in_function_body
11309 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11310 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11311 {
11312 bool inputs_p = false;
11313 bool clobbers_p = false;
11314
11315 /* The extended syntax was used. */
11316 extended_p = true;
11317
11318 /* Look for outputs. */
11319 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11320 {
11321 /* Consume the `:'. */
11322 cp_lexer_consume_token (parser->lexer);
11323 /* Parse the output-operands. */
11324 if (cp_lexer_next_token_is_not (parser->lexer,
11325 CPP_COLON)
11326 && cp_lexer_next_token_is_not (parser->lexer,
11327 CPP_SCOPE)
11328 && cp_lexer_next_token_is_not (parser->lexer,
11329 CPP_CLOSE_PAREN))
11330 outputs = cp_parser_asm_operand_list (parser);
11331 }
11332 /* If the next token is `::', there are no outputs, and the
11333 next token is the beginning of the inputs. */
11334 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11335 /* The inputs are coming next. */
11336 inputs_p = true;
11337
11338 /* Look for inputs. */
11339 if (inputs_p
11340 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11341 {
11342 /* Consume the `:' or `::'. */
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_CLOSE_PAREN))
11349 inputs = cp_parser_asm_operand_list (parser);
11350 }
11351 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11352 /* The clobbers are coming next. */
11353 clobbers_p = true;
11354
11355 /* Look for clobbers. */
11356 if (clobbers_p
11357 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11358 {
11359 /* Consume the `:' or `::'. */
11360 cp_lexer_consume_token (parser->lexer);
11361 /* Parse the clobbers. */
11362 if (cp_lexer_next_token_is_not (parser->lexer,
11363 CPP_CLOSE_PAREN))
11364 clobbers = cp_parser_asm_clobber_list (parser);
11365 }
11366 }
11367 /* Look for the closing `)'. */
11368 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11369 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11370 /*consume_paren=*/true);
11371 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11372
11373 /* Create the ASM_EXPR. */
11374 if (parser->in_function_body)
11375 {
11376 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11377 inputs, clobbers);
11378 /* If the extended syntax was not used, mark the ASM_EXPR. */
11379 if (!extended_p)
11380 {
11381 tree temp = asm_stmt;
11382 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11383 temp = TREE_OPERAND (temp, 0);
11384
11385 ASM_INPUT_P (temp) = 1;
11386 }
11387 }
11388 else
11389 cgraph_add_asm_node (string);
11390 }
11391
11392 /* Declarators [gram.dcl.decl] */
11393
11394 /* Parse an init-declarator.
11395
11396 init-declarator:
11397 declarator initializer [opt]
11398
11399 GNU Extension:
11400
11401 init-declarator:
11402 declarator asm-specification [opt] attributes [opt] initializer [opt]
11403
11404 function-definition:
11405 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11406 function-body
11407 decl-specifier-seq [opt] declarator function-try-block
11408
11409 GNU Extension:
11410
11411 function-definition:
11412 __extension__ function-definition
11413
11414 The DECL_SPECIFIERS apply to this declarator. Returns a
11415 representation of the entity declared. If MEMBER_P is TRUE, then
11416 this declarator appears in a class scope. The new DECL created by
11417 this declarator is returned.
11418
11419 The CHECKS are access checks that should be performed once we know
11420 what entity is being declared (and, therefore, what classes have
11421 befriended it).
11422
11423 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11424 for a function-definition here as well. If the declarator is a
11425 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11426 be TRUE upon return. By that point, the function-definition will
11427 have been completely parsed.
11428
11429 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11430 is FALSE. */
11431
11432 static tree
11433 cp_parser_init_declarator (cp_parser* parser,
11434 cp_decl_specifier_seq *decl_specifiers,
11435 VEC (deferred_access_check,gc)* checks,
11436 bool function_definition_allowed_p,
11437 bool member_p,
11438 int declares_class_or_enum,
11439 bool* function_definition_p)
11440 {
11441 cp_token *token;
11442 cp_declarator *declarator;
11443 tree prefix_attributes;
11444 tree attributes;
11445 tree asm_specification;
11446 tree initializer;
11447 tree decl = NULL_TREE;
11448 tree scope;
11449 bool is_initialized;
11450 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11451 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11452 "(...)". */
11453 enum cpp_ttype initialization_kind;
11454 bool is_parenthesized_init = false;
11455 bool is_non_constant_init;
11456 int ctor_dtor_or_conv_p;
11457 bool friend_p;
11458 tree pushed_scope = NULL;
11459
11460 /* Gather the attributes that were provided with the
11461 decl-specifiers. */
11462 prefix_attributes = decl_specifiers->attributes;
11463
11464 /* Assume that this is not the declarator for a function
11465 definition. */
11466 if (function_definition_p)
11467 *function_definition_p = false;
11468
11469 /* Defer access checks while parsing the declarator; we cannot know
11470 what names are accessible until we know what is being
11471 declared. */
11472 resume_deferring_access_checks ();
11473
11474 /* Parse the declarator. */
11475 declarator
11476 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11477 &ctor_dtor_or_conv_p,
11478 /*parenthesized_p=*/NULL,
11479 /*member_p=*/false);
11480 /* Gather up the deferred checks. */
11481 stop_deferring_access_checks ();
11482
11483 /* If the DECLARATOR was erroneous, there's no need to go
11484 further. */
11485 if (declarator == cp_error_declarator)
11486 return error_mark_node;
11487
11488 /* Check that the number of template-parameter-lists is OK. */
11489 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11490 return error_mark_node;
11491
11492 if (declares_class_or_enum & 2)
11493 cp_parser_check_for_definition_in_return_type (declarator,
11494 decl_specifiers->type);
11495
11496 /* Figure out what scope the entity declared by the DECLARATOR is
11497 located in. `grokdeclarator' sometimes changes the scope, so
11498 we compute it now. */
11499 scope = get_scope_of_declarator (declarator);
11500
11501 /* If we're allowing GNU extensions, look for an asm-specification
11502 and attributes. */
11503 if (cp_parser_allow_gnu_extensions_p (parser))
11504 {
11505 /* Look for an asm-specification. */
11506 asm_specification = cp_parser_asm_specification_opt (parser);
11507 /* And attributes. */
11508 attributes = cp_parser_attributes_opt (parser);
11509 }
11510 else
11511 {
11512 asm_specification = NULL_TREE;
11513 attributes = NULL_TREE;
11514 }
11515
11516 /* Peek at the next token. */
11517 token = cp_lexer_peek_token (parser->lexer);
11518 /* Check to see if the token indicates the start of a
11519 function-definition. */
11520 if (cp_parser_token_starts_function_definition_p (token))
11521 {
11522 if (!function_definition_allowed_p)
11523 {
11524 /* If a function-definition should not appear here, issue an
11525 error message. */
11526 cp_parser_error (parser,
11527 "a function-definition is not allowed here");
11528 return error_mark_node;
11529 }
11530 else
11531 {
11532 /* Neither attributes nor an asm-specification are allowed
11533 on a function-definition. */
11534 if (asm_specification)
11535 error ("an asm-specification is not allowed on a function-definition");
11536 if (attributes)
11537 error ("attributes are not allowed on a function-definition");
11538 /* This is a function-definition. */
11539 *function_definition_p = true;
11540
11541 /* Parse the function definition. */
11542 if (member_p)
11543 decl = cp_parser_save_member_function_body (parser,
11544 decl_specifiers,
11545 declarator,
11546 prefix_attributes);
11547 else
11548 decl
11549 = (cp_parser_function_definition_from_specifiers_and_declarator
11550 (parser, decl_specifiers, prefix_attributes, declarator));
11551
11552 return decl;
11553 }
11554 }
11555
11556 /* [dcl.dcl]
11557
11558 Only in function declarations for constructors, destructors, and
11559 type conversions can the decl-specifier-seq be omitted.
11560
11561 We explicitly postpone this check past the point where we handle
11562 function-definitions because we tolerate function-definitions
11563 that are missing their return types in some modes. */
11564 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11565 {
11566 cp_parser_error (parser,
11567 "expected constructor, destructor, or type conversion");
11568 return error_mark_node;
11569 }
11570
11571 /* An `=' or an `(' indicates an initializer. */
11572 if (token->type == CPP_EQ
11573 || token->type == CPP_OPEN_PAREN)
11574 {
11575 is_initialized = true;
11576 initialization_kind = token->type;
11577 }
11578 else
11579 {
11580 /* If the init-declarator isn't initialized and isn't followed by a
11581 `,' or `;', it's not a valid init-declarator. */
11582 if (token->type != CPP_COMMA
11583 && token->type != CPP_SEMICOLON)
11584 {
11585 cp_parser_error (parser, "expected initializer");
11586 return error_mark_node;
11587 }
11588 is_initialized = false;
11589 initialization_kind = CPP_EOF;
11590 }
11591
11592 /* Because start_decl has side-effects, we should only call it if we
11593 know we're going ahead. By this point, we know that we cannot
11594 possibly be looking at any other construct. */
11595 cp_parser_commit_to_tentative_parse (parser);
11596
11597 /* If the decl specifiers were bad, issue an error now that we're
11598 sure this was intended to be a declarator. Then continue
11599 declaring the variable(s), as int, to try to cut down on further
11600 errors. */
11601 if (decl_specifiers->any_specifiers_p
11602 && decl_specifiers->type == error_mark_node)
11603 {
11604 cp_parser_error (parser, "invalid type in declaration");
11605 decl_specifiers->type = integer_type_node;
11606 }
11607
11608 /* Check to see whether or not this declaration is a friend. */
11609 friend_p = cp_parser_friend_p (decl_specifiers);
11610
11611 /* Enter the newly declared entry in the symbol table. If we're
11612 processing a declaration in a class-specifier, we wait until
11613 after processing the initializer. */
11614 if (!member_p)
11615 {
11616 if (parser->in_unbraced_linkage_specification_p)
11617 decl_specifiers->storage_class = sc_extern;
11618 decl = start_decl (declarator, decl_specifiers,
11619 is_initialized, attributes, prefix_attributes,
11620 &pushed_scope);
11621 }
11622 else if (scope)
11623 /* Enter the SCOPE. That way unqualified names appearing in the
11624 initializer will be looked up in SCOPE. */
11625 pushed_scope = push_scope (scope);
11626
11627 /* Perform deferred access control checks, now that we know in which
11628 SCOPE the declared entity resides. */
11629 if (!member_p && decl)
11630 {
11631 tree saved_current_function_decl = NULL_TREE;
11632
11633 /* If the entity being declared is a function, pretend that we
11634 are in its scope. If it is a `friend', it may have access to
11635 things that would not otherwise be accessible. */
11636 if (TREE_CODE (decl) == FUNCTION_DECL)
11637 {
11638 saved_current_function_decl = current_function_decl;
11639 current_function_decl = decl;
11640 }
11641
11642 /* Perform access checks for template parameters. */
11643 cp_parser_perform_template_parameter_access_checks (checks);
11644
11645 /* Perform the access control checks for the declarator and the
11646 the decl-specifiers. */
11647 perform_deferred_access_checks ();
11648
11649 /* Restore the saved value. */
11650 if (TREE_CODE (decl) == FUNCTION_DECL)
11651 current_function_decl = saved_current_function_decl;
11652 }
11653
11654 /* Parse the initializer. */
11655 initializer = NULL_TREE;
11656 is_parenthesized_init = false;
11657 is_non_constant_init = true;
11658 if (is_initialized)
11659 {
11660 if (function_declarator_p (declarator))
11661 {
11662 if (initialization_kind == CPP_EQ)
11663 initializer = cp_parser_pure_specifier (parser);
11664 else
11665 {
11666 /* If the declaration was erroneous, we don't really
11667 know what the user intended, so just silently
11668 consume the initializer. */
11669 if (decl != error_mark_node)
11670 error ("initializer provided for function");
11671 cp_parser_skip_to_closing_parenthesis (parser,
11672 /*recovering=*/true,
11673 /*or_comma=*/false,
11674 /*consume_paren=*/true);
11675 }
11676 }
11677 else
11678 initializer = cp_parser_initializer (parser,
11679 &is_parenthesized_init,
11680 &is_non_constant_init);
11681 }
11682
11683 /* The old parser allows attributes to appear after a parenthesized
11684 initializer. Mark Mitchell proposed removing this functionality
11685 on the GCC mailing lists on 2002-08-13. This parser accepts the
11686 attributes -- but ignores them. */
11687 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11688 if (cp_parser_attributes_opt (parser))
11689 warning (OPT_Wattributes,
11690 "attributes after parenthesized initializer ignored");
11691
11692 /* For an in-class declaration, use `grokfield' to create the
11693 declaration. */
11694 if (member_p)
11695 {
11696 if (pushed_scope)
11697 {
11698 pop_scope (pushed_scope);
11699 pushed_scope = false;
11700 }
11701 decl = grokfield (declarator, decl_specifiers,
11702 initializer, !is_non_constant_init,
11703 /*asmspec=*/NULL_TREE,
11704 prefix_attributes);
11705 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11706 cp_parser_save_default_args (parser, decl);
11707 }
11708
11709 /* Finish processing the declaration. But, skip friend
11710 declarations. */
11711 if (!friend_p && decl && decl != error_mark_node)
11712 {
11713 cp_finish_decl (decl,
11714 initializer, !is_non_constant_init,
11715 asm_specification,
11716 /* If the initializer is in parentheses, then this is
11717 a direct-initialization, which means that an
11718 `explicit' constructor is OK. Otherwise, an
11719 `explicit' constructor cannot be used. */
11720 ((is_parenthesized_init || !is_initialized)
11721 ? 0 : LOOKUP_ONLYCONVERTING));
11722 }
11723 if (!friend_p && pushed_scope)
11724 pop_scope (pushed_scope);
11725
11726 return decl;
11727 }
11728
11729 /* Parse a declarator.
11730
11731 declarator:
11732 direct-declarator
11733 ptr-operator declarator
11734
11735 abstract-declarator:
11736 ptr-operator abstract-declarator [opt]
11737 direct-abstract-declarator
11738
11739 GNU Extensions:
11740
11741 declarator:
11742 attributes [opt] direct-declarator
11743 attributes [opt] ptr-operator declarator
11744
11745 abstract-declarator:
11746 attributes [opt] ptr-operator abstract-declarator [opt]
11747 attributes [opt] direct-abstract-declarator
11748
11749 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11750 detect constructor, destructor or conversion operators. It is set
11751 to -1 if the declarator is a name, and +1 if it is a
11752 function. Otherwise it is set to zero. Usually you just want to
11753 test for >0, but internally the negative value is used.
11754
11755 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11756 a decl-specifier-seq unless it declares a constructor, destructor,
11757 or conversion. It might seem that we could check this condition in
11758 semantic analysis, rather than parsing, but that makes it difficult
11759 to handle something like `f()'. We want to notice that there are
11760 no decl-specifiers, and therefore realize that this is an
11761 expression, not a declaration.)
11762
11763 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11764 the declarator is a direct-declarator of the form "(...)".
11765
11766 MEMBER_P is true iff this declarator is a member-declarator. */
11767
11768 static cp_declarator *
11769 cp_parser_declarator (cp_parser* parser,
11770 cp_parser_declarator_kind dcl_kind,
11771 int* ctor_dtor_or_conv_p,
11772 bool* parenthesized_p,
11773 bool member_p)
11774 {
11775 cp_token *token;
11776 cp_declarator *declarator;
11777 enum tree_code code;
11778 cp_cv_quals cv_quals;
11779 tree class_type;
11780 tree attributes = NULL_TREE;
11781
11782 /* Assume this is not a constructor, destructor, or type-conversion
11783 operator. */
11784 if (ctor_dtor_or_conv_p)
11785 *ctor_dtor_or_conv_p = 0;
11786
11787 if (cp_parser_allow_gnu_extensions_p (parser))
11788 attributes = cp_parser_attributes_opt (parser);
11789
11790 /* Peek at the next token. */
11791 token = cp_lexer_peek_token (parser->lexer);
11792
11793 /* Check for the ptr-operator production. */
11794 cp_parser_parse_tentatively (parser);
11795 /* Parse the ptr-operator. */
11796 code = cp_parser_ptr_operator (parser,
11797 &class_type,
11798 &cv_quals);
11799 /* If that worked, then we have a ptr-operator. */
11800 if (cp_parser_parse_definitely (parser))
11801 {
11802 /* If a ptr-operator was found, then this declarator was not
11803 parenthesized. */
11804 if (parenthesized_p)
11805 *parenthesized_p = true;
11806 /* The dependent declarator is optional if we are parsing an
11807 abstract-declarator. */
11808 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11809 cp_parser_parse_tentatively (parser);
11810
11811 /* Parse the dependent declarator. */
11812 declarator = cp_parser_declarator (parser, dcl_kind,
11813 /*ctor_dtor_or_conv_p=*/NULL,
11814 /*parenthesized_p=*/NULL,
11815 /*member_p=*/false);
11816
11817 /* If we are parsing an abstract-declarator, we must handle the
11818 case where the dependent declarator is absent. */
11819 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11820 && !cp_parser_parse_definitely (parser))
11821 declarator = NULL;
11822
11823 /* Build the representation of the ptr-operator. */
11824 if (class_type)
11825 declarator = make_ptrmem_declarator (cv_quals,
11826 class_type,
11827 declarator);
11828 else if (code == INDIRECT_REF)
11829 declarator = make_pointer_declarator (cv_quals, declarator);
11830 else
11831 declarator = make_reference_declarator (cv_quals, declarator);
11832 }
11833 /* Everything else is a direct-declarator. */
11834 else
11835 {
11836 if (parenthesized_p)
11837 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11838 CPP_OPEN_PAREN);
11839 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11840 ctor_dtor_or_conv_p,
11841 member_p);
11842 }
11843
11844 if (attributes && declarator && declarator != cp_error_declarator)
11845 declarator->attributes = attributes;
11846
11847 return declarator;
11848 }
11849
11850 /* Parse a direct-declarator or direct-abstract-declarator.
11851
11852 direct-declarator:
11853 declarator-id
11854 direct-declarator ( parameter-declaration-clause )
11855 cv-qualifier-seq [opt]
11856 exception-specification [opt]
11857 direct-declarator [ constant-expression [opt] ]
11858 ( declarator )
11859
11860 direct-abstract-declarator:
11861 direct-abstract-declarator [opt]
11862 ( parameter-declaration-clause )
11863 cv-qualifier-seq [opt]
11864 exception-specification [opt]
11865 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11866 ( abstract-declarator )
11867
11868 Returns a representation of the declarator. DCL_KIND is
11869 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11870 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11871 we are parsing a direct-declarator. It is
11872 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11873 of ambiguity we prefer an abstract declarator, as per
11874 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11875 cp_parser_declarator. */
11876
11877 static cp_declarator *
11878 cp_parser_direct_declarator (cp_parser* parser,
11879 cp_parser_declarator_kind dcl_kind,
11880 int* ctor_dtor_or_conv_p,
11881 bool member_p)
11882 {
11883 cp_token *token;
11884 cp_declarator *declarator = NULL;
11885 tree scope = NULL_TREE;
11886 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11887 bool saved_in_declarator_p = parser->in_declarator_p;
11888 bool first = true;
11889 tree pushed_scope = NULL_TREE;
11890
11891 while (true)
11892 {
11893 /* Peek at the next token. */
11894 token = cp_lexer_peek_token (parser->lexer);
11895 if (token->type == CPP_OPEN_PAREN)
11896 {
11897 /* This is either a parameter-declaration-clause, or a
11898 parenthesized declarator. When we know we are parsing a
11899 named declarator, it must be a parenthesized declarator
11900 if FIRST is true. For instance, `(int)' is a
11901 parameter-declaration-clause, with an omitted
11902 direct-abstract-declarator. But `((*))', is a
11903 parenthesized abstract declarator. Finally, when T is a
11904 template parameter `(T)' is a
11905 parameter-declaration-clause, and not a parenthesized
11906 named declarator.
11907
11908 We first try and parse a parameter-declaration-clause,
11909 and then try a nested declarator (if FIRST is true).
11910
11911 It is not an error for it not to be a
11912 parameter-declaration-clause, even when FIRST is
11913 false. Consider,
11914
11915 int i (int);
11916 int i (3);
11917
11918 The first is the declaration of a function while the
11919 second is a the definition of a variable, including its
11920 initializer.
11921
11922 Having seen only the parenthesis, we cannot know which of
11923 these two alternatives should be selected. Even more
11924 complex are examples like:
11925
11926 int i (int (a));
11927 int i (int (3));
11928
11929 The former is a function-declaration; the latter is a
11930 variable initialization.
11931
11932 Thus again, we try a parameter-declaration-clause, and if
11933 that fails, we back out and return. */
11934
11935 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11936 {
11937 cp_parameter_declarator *params;
11938 unsigned saved_num_template_parameter_lists;
11939
11940 /* In a member-declarator, the only valid interpretation
11941 of a parenthesis is the start of a
11942 parameter-declaration-clause. (It is invalid to
11943 initialize a static data member with a parenthesized
11944 initializer; only the "=" form of initialization is
11945 permitted.) */
11946 if (!member_p)
11947 cp_parser_parse_tentatively (parser);
11948
11949 /* Consume the `('. */
11950 cp_lexer_consume_token (parser->lexer);
11951 if (first)
11952 {
11953 /* If this is going to be an abstract declarator, we're
11954 in a declarator and we can't have default args. */
11955 parser->default_arg_ok_p = false;
11956 parser->in_declarator_p = true;
11957 }
11958
11959 /* Inside the function parameter list, surrounding
11960 template-parameter-lists do not apply. */
11961 saved_num_template_parameter_lists
11962 = parser->num_template_parameter_lists;
11963 parser->num_template_parameter_lists = 0;
11964
11965 /* Parse the parameter-declaration-clause. */
11966 params = cp_parser_parameter_declaration_clause (parser);
11967
11968 parser->num_template_parameter_lists
11969 = saved_num_template_parameter_lists;
11970
11971 /* If all went well, parse the cv-qualifier-seq and the
11972 exception-specification. */
11973 if (member_p || cp_parser_parse_definitely (parser))
11974 {
11975 cp_cv_quals cv_quals;
11976 tree exception_specification;
11977
11978 if (ctor_dtor_or_conv_p)
11979 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11980 first = false;
11981 /* Consume the `)'. */
11982 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11983
11984 /* Parse the cv-qualifier-seq. */
11985 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11986 /* And the exception-specification. */
11987 exception_specification
11988 = cp_parser_exception_specification_opt (parser);
11989
11990 /* Create the function-declarator. */
11991 declarator = make_call_declarator (declarator,
11992 params,
11993 cv_quals,
11994 exception_specification);
11995 /* Any subsequent parameter lists are to do with
11996 return type, so are not those of the declared
11997 function. */
11998 parser->default_arg_ok_p = false;
11999
12000 /* Repeat the main loop. */
12001 continue;
12002 }
12003 }
12004
12005 /* If this is the first, we can try a parenthesized
12006 declarator. */
12007 if (first)
12008 {
12009 bool saved_in_type_id_in_expr_p;
12010
12011 parser->default_arg_ok_p = saved_default_arg_ok_p;
12012 parser->in_declarator_p = saved_in_declarator_p;
12013
12014 /* Consume the `('. */
12015 cp_lexer_consume_token (parser->lexer);
12016 /* Parse the nested declarator. */
12017 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12018 parser->in_type_id_in_expr_p = true;
12019 declarator
12020 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12021 /*parenthesized_p=*/NULL,
12022 member_p);
12023 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12024 first = false;
12025 /* Expect a `)'. */
12026 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12027 declarator = cp_error_declarator;
12028 if (declarator == cp_error_declarator)
12029 break;
12030
12031 goto handle_declarator;
12032 }
12033 /* Otherwise, we must be done. */
12034 else
12035 break;
12036 }
12037 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12038 && token->type == CPP_OPEN_SQUARE)
12039 {
12040 /* Parse an array-declarator. */
12041 tree bounds;
12042
12043 if (ctor_dtor_or_conv_p)
12044 *ctor_dtor_or_conv_p = 0;
12045
12046 first = false;
12047 parser->default_arg_ok_p = false;
12048 parser->in_declarator_p = true;
12049 /* Consume the `['. */
12050 cp_lexer_consume_token (parser->lexer);
12051 /* Peek at the next token. */
12052 token = cp_lexer_peek_token (parser->lexer);
12053 /* If the next token is `]', then there is no
12054 constant-expression. */
12055 if (token->type != CPP_CLOSE_SQUARE)
12056 {
12057 bool non_constant_p;
12058
12059 bounds
12060 = cp_parser_constant_expression (parser,
12061 /*allow_non_constant=*/true,
12062 &non_constant_p);
12063 if (!non_constant_p)
12064 bounds = fold_non_dependent_expr (bounds);
12065 /* Normally, the array bound must be an integral constant
12066 expression. However, as an extension, we allow VLAs
12067 in function scopes. */
12068 else if (!parser->in_function_body)
12069 {
12070 error ("array bound is not an integer constant");
12071 bounds = error_mark_node;
12072 }
12073 }
12074 else
12075 bounds = NULL_TREE;
12076 /* Look for the closing `]'. */
12077 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12078 {
12079 declarator = cp_error_declarator;
12080 break;
12081 }
12082
12083 declarator = make_array_declarator (declarator, bounds);
12084 }
12085 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12086 {
12087 tree qualifying_scope;
12088 tree unqualified_name;
12089 special_function_kind sfk;
12090 bool abstract_ok;
12091 bool pack_expansion_p = false;
12092
12093 /* Parse a declarator-id */
12094 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12095 if (abstract_ok)
12096 {
12097 cp_parser_parse_tentatively (parser);
12098
12099 /* If we see an ellipsis, we should be looking at a
12100 parameter pack. */
12101 if (token->type == CPP_ELLIPSIS)
12102 {
12103 /* Consume the `...' */
12104 cp_lexer_consume_token (parser->lexer);
12105
12106 pack_expansion_p = true;
12107 }
12108 }
12109
12110 unqualified_name
12111 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12112 qualifying_scope = parser->scope;
12113 if (abstract_ok)
12114 {
12115 bool okay = false;
12116
12117 if (!unqualified_name && pack_expansion_p)
12118 {
12119 /* Check whether an error occurred. */
12120 okay = !cp_parser_error_occurred (parser);
12121
12122 /* We already consumed the ellipsis to mark a
12123 parameter pack, but we have no way to report it,
12124 so abort the tentative parse. We will be exiting
12125 immediately anyway. */
12126 cp_parser_abort_tentative_parse (parser);
12127 }
12128 else
12129 okay = cp_parser_parse_definitely (parser);
12130
12131 if (!okay)
12132 unqualified_name = error_mark_node;
12133 else if (unqualified_name
12134 && (qualifying_scope
12135 || (TREE_CODE (unqualified_name)
12136 != IDENTIFIER_NODE)))
12137 {
12138 cp_parser_error (parser, "expected unqualified-id");
12139 unqualified_name = error_mark_node;
12140 }
12141 }
12142
12143 if (!unqualified_name)
12144 return NULL;
12145 if (unqualified_name == error_mark_node)
12146 {
12147 declarator = cp_error_declarator;
12148 pack_expansion_p = false;
12149 declarator->parameter_pack_p = false;
12150 break;
12151 }
12152
12153 if (qualifying_scope && at_namespace_scope_p ()
12154 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12155 {
12156 /* In the declaration of a member of a template class
12157 outside of the class itself, the SCOPE will sometimes
12158 be a TYPENAME_TYPE. For example, given:
12159
12160 template <typename T>
12161 int S<T>::R::i = 3;
12162
12163 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12164 this context, we must resolve S<T>::R to an ordinary
12165 type, rather than a typename type.
12166
12167 The reason we normally avoid resolving TYPENAME_TYPEs
12168 is that a specialization of `S' might render
12169 `S<T>::R' not a type. However, if `S' is
12170 specialized, then this `i' will not be used, so there
12171 is no harm in resolving the types here. */
12172 tree type;
12173
12174 /* Resolve the TYPENAME_TYPE. */
12175 type = resolve_typename_type (qualifying_scope,
12176 /*only_current_p=*/false);
12177 /* If that failed, the declarator is invalid. */
12178 if (type == error_mark_node)
12179 error ("%<%T::%E%> is not a type",
12180 TYPE_CONTEXT (qualifying_scope),
12181 TYPE_IDENTIFIER (qualifying_scope));
12182 qualifying_scope = type;
12183 }
12184
12185 sfk = sfk_none;
12186
12187 if (unqualified_name)
12188 {
12189 tree class_type;
12190
12191 if (qualifying_scope
12192 && CLASS_TYPE_P (qualifying_scope))
12193 class_type = qualifying_scope;
12194 else
12195 class_type = current_class_type;
12196
12197 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12198 {
12199 tree name_type = TREE_TYPE (unqualified_name);
12200 if (class_type && same_type_p (name_type, class_type))
12201 {
12202 if (qualifying_scope
12203 && CLASSTYPE_USE_TEMPLATE (name_type))
12204 {
12205 error ("invalid use of constructor as a template");
12206 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12207 "name the constructor in a qualified name",
12208 class_type,
12209 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12210 class_type, name_type);
12211 declarator = cp_error_declarator;
12212 break;
12213 }
12214 else
12215 unqualified_name = constructor_name (class_type);
12216 }
12217 else
12218 {
12219 /* We do not attempt to print the declarator
12220 here because we do not have enough
12221 information about its original syntactic
12222 form. */
12223 cp_parser_error (parser, "invalid declarator");
12224 declarator = cp_error_declarator;
12225 break;
12226 }
12227 }
12228
12229 if (class_type)
12230 {
12231 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12232 sfk = sfk_destructor;
12233 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12234 sfk = sfk_conversion;
12235 else if (/* There's no way to declare a constructor
12236 for an anonymous type, even if the type
12237 got a name for linkage purposes. */
12238 !TYPE_WAS_ANONYMOUS (class_type)
12239 && constructor_name_p (unqualified_name,
12240 class_type))
12241 {
12242 unqualified_name = constructor_name (class_type);
12243 sfk = sfk_constructor;
12244 }
12245
12246 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12247 *ctor_dtor_or_conv_p = -1;
12248 }
12249 }
12250 declarator = make_id_declarator (qualifying_scope,
12251 unqualified_name,
12252 sfk);
12253 declarator->id_loc = token->location;
12254 declarator->parameter_pack_p = pack_expansion_p;
12255
12256 if (pack_expansion_p)
12257 maybe_warn_variadic_templates ();
12258
12259 handle_declarator:;
12260 scope = get_scope_of_declarator (declarator);
12261 if (scope)
12262 /* Any names that appear after the declarator-id for a
12263 member are looked up in the containing scope. */
12264 pushed_scope = push_scope (scope);
12265 parser->in_declarator_p = true;
12266 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12267 || (declarator && declarator->kind == cdk_id))
12268 /* Default args are only allowed on function
12269 declarations. */
12270 parser->default_arg_ok_p = saved_default_arg_ok_p;
12271 else
12272 parser->default_arg_ok_p = false;
12273
12274 first = false;
12275 }
12276 /* We're done. */
12277 else
12278 break;
12279 }
12280
12281 /* For an abstract declarator, we might wind up with nothing at this
12282 point. That's an error; the declarator is not optional. */
12283 if (!declarator)
12284 cp_parser_error (parser, "expected declarator");
12285
12286 /* If we entered a scope, we must exit it now. */
12287 if (pushed_scope)
12288 pop_scope (pushed_scope);
12289
12290 parser->default_arg_ok_p = saved_default_arg_ok_p;
12291 parser->in_declarator_p = saved_in_declarator_p;
12292
12293 return declarator;
12294 }
12295
12296 /* Parse a ptr-operator.
12297
12298 ptr-operator:
12299 * cv-qualifier-seq [opt]
12300 &
12301 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12302
12303 GNU Extension:
12304
12305 ptr-operator:
12306 & cv-qualifier-seq [opt]
12307
12308 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12309 Returns ADDR_EXPR if a reference was used. In the case of a
12310 pointer-to-member, *TYPE is filled in with the TYPE containing the
12311 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12312 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12313 ERROR_MARK if an error occurred. */
12314
12315 static enum tree_code
12316 cp_parser_ptr_operator (cp_parser* parser,
12317 tree* type,
12318 cp_cv_quals *cv_quals)
12319 {
12320 enum tree_code code = ERROR_MARK;
12321 cp_token *token;
12322
12323 /* Assume that it's not a pointer-to-member. */
12324 *type = NULL_TREE;
12325 /* And that there are no cv-qualifiers. */
12326 *cv_quals = TYPE_UNQUALIFIED;
12327
12328 /* Peek at the next token. */
12329 token = cp_lexer_peek_token (parser->lexer);
12330 /* If it's a `*' or `&' we have a pointer or reference. */
12331 if (token->type == CPP_MULT || token->type == CPP_AND)
12332 {
12333 /* Remember which ptr-operator we were processing. */
12334 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12335
12336 /* Consume the `*' or `&'. */
12337 cp_lexer_consume_token (parser->lexer);
12338
12339 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12340 `&', if we are allowing GNU extensions. (The only qualifier
12341 that can legally appear after `&' is `restrict', but that is
12342 enforced during semantic analysis. */
12343 if (code == INDIRECT_REF
12344 || cp_parser_allow_gnu_extensions_p (parser))
12345 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12346 }
12347 else
12348 {
12349 /* Try the pointer-to-member case. */
12350 cp_parser_parse_tentatively (parser);
12351 /* Look for the optional `::' operator. */
12352 cp_parser_global_scope_opt (parser,
12353 /*current_scope_valid_p=*/false);
12354 /* Look for the nested-name specifier. */
12355 cp_parser_nested_name_specifier (parser,
12356 /*typename_keyword_p=*/false,
12357 /*check_dependency_p=*/true,
12358 /*type_p=*/false,
12359 /*is_declaration=*/false);
12360 /* If we found it, and the next token is a `*', then we are
12361 indeed looking at a pointer-to-member operator. */
12362 if (!cp_parser_error_occurred (parser)
12363 && cp_parser_require (parser, CPP_MULT, "`*'"))
12364 {
12365 /* Indicate that the `*' operator was used. */
12366 code = INDIRECT_REF;
12367
12368 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12369 error ("%qD is a namespace", parser->scope);
12370 else
12371 {
12372 /* The type of which the member is a member is given by the
12373 current SCOPE. */
12374 *type = parser->scope;
12375 /* The next name will not be qualified. */
12376 parser->scope = NULL_TREE;
12377 parser->qualifying_scope = NULL_TREE;
12378 parser->object_scope = NULL_TREE;
12379 /* Look for the optional cv-qualifier-seq. */
12380 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12381 }
12382 }
12383 /* If that didn't work we don't have a ptr-operator. */
12384 if (!cp_parser_parse_definitely (parser))
12385 cp_parser_error (parser, "expected ptr-operator");
12386 }
12387
12388 return code;
12389 }
12390
12391 /* Parse an (optional) cv-qualifier-seq.
12392
12393 cv-qualifier-seq:
12394 cv-qualifier cv-qualifier-seq [opt]
12395
12396 cv-qualifier:
12397 const
12398 volatile
12399
12400 GNU Extension:
12401
12402 cv-qualifier:
12403 __restrict__
12404
12405 Returns a bitmask representing the cv-qualifiers. */
12406
12407 static cp_cv_quals
12408 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12409 {
12410 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12411
12412 while (true)
12413 {
12414 cp_token *token;
12415 cp_cv_quals cv_qualifier;
12416
12417 /* Peek at the next token. */
12418 token = cp_lexer_peek_token (parser->lexer);
12419 /* See if it's a cv-qualifier. */
12420 switch (token->keyword)
12421 {
12422 case RID_CONST:
12423 cv_qualifier = TYPE_QUAL_CONST;
12424 break;
12425
12426 case RID_VOLATILE:
12427 cv_qualifier = TYPE_QUAL_VOLATILE;
12428 break;
12429
12430 case RID_RESTRICT:
12431 cv_qualifier = TYPE_QUAL_RESTRICT;
12432 break;
12433
12434 default:
12435 cv_qualifier = TYPE_UNQUALIFIED;
12436 break;
12437 }
12438
12439 if (!cv_qualifier)
12440 break;
12441
12442 if (cv_quals & cv_qualifier)
12443 {
12444 error ("duplicate cv-qualifier");
12445 cp_lexer_purge_token (parser->lexer);
12446 }
12447 else
12448 {
12449 cp_lexer_consume_token (parser->lexer);
12450 cv_quals |= cv_qualifier;
12451 }
12452 }
12453
12454 return cv_quals;
12455 }
12456
12457 /* Parse a declarator-id.
12458
12459 declarator-id:
12460 id-expression
12461 :: [opt] nested-name-specifier [opt] type-name
12462
12463 In the `id-expression' case, the value returned is as for
12464 cp_parser_id_expression if the id-expression was an unqualified-id.
12465 If the id-expression was a qualified-id, then a SCOPE_REF is
12466 returned. The first operand is the scope (either a NAMESPACE_DECL
12467 or TREE_TYPE), but the second is still just a representation of an
12468 unqualified-id. */
12469
12470 static tree
12471 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12472 {
12473 tree id;
12474 /* The expression must be an id-expression. Assume that qualified
12475 names are the names of types so that:
12476
12477 template <class T>
12478 int S<T>::R::i = 3;
12479
12480 will work; we must treat `S<T>::R' as the name of a type.
12481 Similarly, assume that qualified names are templates, where
12482 required, so that:
12483
12484 template <class T>
12485 int S<T>::R<T>::i = 3;
12486
12487 will work, too. */
12488 id = cp_parser_id_expression (parser,
12489 /*template_keyword_p=*/false,
12490 /*check_dependency_p=*/false,
12491 /*template_p=*/NULL,
12492 /*declarator_p=*/true,
12493 optional_p);
12494 if (id && BASELINK_P (id))
12495 id = BASELINK_FUNCTIONS (id);
12496 return id;
12497 }
12498
12499 /* Parse a type-id.
12500
12501 type-id:
12502 type-specifier-seq abstract-declarator [opt]
12503
12504 Returns the TYPE specified. */
12505
12506 static tree
12507 cp_parser_type_id (cp_parser* parser)
12508 {
12509 cp_decl_specifier_seq type_specifier_seq;
12510 cp_declarator *abstract_declarator;
12511
12512 /* Parse the type-specifier-seq. */
12513 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12514 &type_specifier_seq);
12515 if (type_specifier_seq.type == error_mark_node)
12516 return error_mark_node;
12517
12518 /* There might or might not be an abstract declarator. */
12519 cp_parser_parse_tentatively (parser);
12520 /* Look for the declarator. */
12521 abstract_declarator
12522 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12523 /*parenthesized_p=*/NULL,
12524 /*member_p=*/false);
12525 /* Check to see if there really was a declarator. */
12526 if (!cp_parser_parse_definitely (parser))
12527 abstract_declarator = NULL;
12528
12529 return groktypename (&type_specifier_seq, abstract_declarator);
12530 }
12531
12532 /* Parse a type-specifier-seq.
12533
12534 type-specifier-seq:
12535 type-specifier type-specifier-seq [opt]
12536
12537 GNU extension:
12538
12539 type-specifier-seq:
12540 attributes type-specifier-seq [opt]
12541
12542 If IS_CONDITION is true, we are at the start of a "condition",
12543 e.g., we've just seen "if (".
12544
12545 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12546
12547 static void
12548 cp_parser_type_specifier_seq (cp_parser* parser,
12549 bool is_condition,
12550 cp_decl_specifier_seq *type_specifier_seq)
12551 {
12552 bool seen_type_specifier = false;
12553 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12554
12555 /* Clear the TYPE_SPECIFIER_SEQ. */
12556 clear_decl_specs (type_specifier_seq);
12557
12558 /* Parse the type-specifiers and attributes. */
12559 while (true)
12560 {
12561 tree type_specifier;
12562 bool is_cv_qualifier;
12563
12564 /* Check for attributes first. */
12565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12566 {
12567 type_specifier_seq->attributes =
12568 chainon (type_specifier_seq->attributes,
12569 cp_parser_attributes_opt (parser));
12570 continue;
12571 }
12572
12573 /* Look for the type-specifier. */
12574 type_specifier = cp_parser_type_specifier (parser,
12575 flags,
12576 type_specifier_seq,
12577 /*is_declaration=*/false,
12578 NULL,
12579 &is_cv_qualifier);
12580 if (!type_specifier)
12581 {
12582 /* If the first type-specifier could not be found, this is not a
12583 type-specifier-seq at all. */
12584 if (!seen_type_specifier)
12585 {
12586 cp_parser_error (parser, "expected type-specifier");
12587 type_specifier_seq->type = error_mark_node;
12588 return;
12589 }
12590 /* If subsequent type-specifiers could not be found, the
12591 type-specifier-seq is complete. */
12592 break;
12593 }
12594
12595 seen_type_specifier = true;
12596 /* The standard says that a condition can be:
12597
12598 type-specifier-seq declarator = assignment-expression
12599
12600 However, given:
12601
12602 struct S {};
12603 if (int S = ...)
12604
12605 we should treat the "S" as a declarator, not as a
12606 type-specifier. The standard doesn't say that explicitly for
12607 type-specifier-seq, but it does say that for
12608 decl-specifier-seq in an ordinary declaration. Perhaps it
12609 would be clearer just to allow a decl-specifier-seq here, and
12610 then add a semantic restriction that if any decl-specifiers
12611 that are not type-specifiers appear, the program is invalid. */
12612 if (is_condition && !is_cv_qualifier)
12613 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12614 }
12615
12616 cp_parser_check_decl_spec (type_specifier_seq);
12617 }
12618
12619 /* Parse a parameter-declaration-clause.
12620
12621 parameter-declaration-clause:
12622 parameter-declaration-list [opt] ... [opt]
12623 parameter-declaration-list , ...
12624
12625 Returns a representation for the parameter declarations. A return
12626 value of NULL indicates a parameter-declaration-clause consisting
12627 only of an ellipsis. */
12628
12629 static cp_parameter_declarator *
12630 cp_parser_parameter_declaration_clause (cp_parser* parser)
12631 {
12632 cp_parameter_declarator *parameters;
12633 cp_token *token;
12634 bool ellipsis_p;
12635 bool is_error;
12636
12637 /* Peek at the next token. */
12638 token = cp_lexer_peek_token (parser->lexer);
12639 /* Check for trivial parameter-declaration-clauses. */
12640 if (token->type == CPP_ELLIPSIS)
12641 {
12642 /* Consume the `...' token. */
12643 cp_lexer_consume_token (parser->lexer);
12644 return NULL;
12645 }
12646 else if (token->type == CPP_CLOSE_PAREN)
12647 /* There are no parameters. */
12648 {
12649 #ifndef NO_IMPLICIT_EXTERN_C
12650 if (in_system_header && current_class_type == NULL
12651 && current_lang_name == lang_name_c)
12652 return NULL;
12653 else
12654 #endif
12655 return no_parameters;
12656 }
12657 /* Check for `(void)', too, which is a special case. */
12658 else if (token->keyword == RID_VOID
12659 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12660 == CPP_CLOSE_PAREN))
12661 {
12662 /* Consume the `void' token. */
12663 cp_lexer_consume_token (parser->lexer);
12664 /* There are no parameters. */
12665 return no_parameters;
12666 }
12667
12668 /* Parse the parameter-declaration-list. */
12669 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12670 /* If a parse error occurred while parsing the
12671 parameter-declaration-list, then the entire
12672 parameter-declaration-clause is erroneous. */
12673 if (is_error)
12674 return NULL;
12675
12676 /* Peek at the next token. */
12677 token = cp_lexer_peek_token (parser->lexer);
12678 /* If it's a `,', the clause should terminate with an ellipsis. */
12679 if (token->type == CPP_COMMA)
12680 {
12681 /* Consume the `,'. */
12682 cp_lexer_consume_token (parser->lexer);
12683 /* Expect an ellipsis. */
12684 ellipsis_p
12685 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12686 }
12687 /* It might also be `...' if the optional trailing `,' was
12688 omitted. */
12689 else if (token->type == CPP_ELLIPSIS)
12690 {
12691 /* Consume the `...' token. */
12692 cp_lexer_consume_token (parser->lexer);
12693 /* And remember that we saw it. */
12694 ellipsis_p = true;
12695 }
12696 else
12697 ellipsis_p = false;
12698
12699 /* Finish the parameter list. */
12700 if (parameters && ellipsis_p)
12701 parameters->ellipsis_p = true;
12702
12703 return parameters;
12704 }
12705
12706 /* Parse a parameter-declaration-list.
12707
12708 parameter-declaration-list:
12709 parameter-declaration
12710 parameter-declaration-list , parameter-declaration
12711
12712 Returns a representation of the parameter-declaration-list, as for
12713 cp_parser_parameter_declaration_clause. However, the
12714 `void_list_node' is never appended to the list. Upon return,
12715 *IS_ERROR will be true iff an error occurred. */
12716
12717 static cp_parameter_declarator *
12718 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12719 {
12720 cp_parameter_declarator *parameters = NULL;
12721 cp_parameter_declarator **tail = &parameters;
12722 bool saved_in_unbraced_linkage_specification_p;
12723
12724 /* Assume all will go well. */
12725 *is_error = false;
12726 /* The special considerations that apply to a function within an
12727 unbraced linkage specifications do not apply to the parameters
12728 to the function. */
12729 saved_in_unbraced_linkage_specification_p
12730 = parser->in_unbraced_linkage_specification_p;
12731 parser->in_unbraced_linkage_specification_p = false;
12732
12733 /* Look for more parameters. */
12734 while (true)
12735 {
12736 cp_parameter_declarator *parameter;
12737 bool parenthesized_p;
12738 /* Parse the parameter. */
12739 parameter
12740 = cp_parser_parameter_declaration (parser,
12741 /*template_parm_p=*/false,
12742 &parenthesized_p);
12743
12744 /* If a parse error occurred parsing the parameter declaration,
12745 then the entire parameter-declaration-list is erroneous. */
12746 if (!parameter)
12747 {
12748 *is_error = true;
12749 parameters = NULL;
12750 break;
12751 }
12752 /* Add the new parameter to the list. */
12753 *tail = parameter;
12754 tail = &parameter->next;
12755
12756 /* Peek at the next token. */
12757 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12758 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12759 /* These are for Objective-C++ */
12760 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12761 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12762 /* The parameter-declaration-list is complete. */
12763 break;
12764 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12765 {
12766 cp_token *token;
12767
12768 /* Peek at the next token. */
12769 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12770 /* If it's an ellipsis, then the list is complete. */
12771 if (token->type == CPP_ELLIPSIS)
12772 break;
12773 /* Otherwise, there must be more parameters. Consume the
12774 `,'. */
12775 cp_lexer_consume_token (parser->lexer);
12776 /* When parsing something like:
12777
12778 int i(float f, double d)
12779
12780 we can tell after seeing the declaration for "f" that we
12781 are not looking at an initialization of a variable "i",
12782 but rather at the declaration of a function "i".
12783
12784 Due to the fact that the parsing of template arguments
12785 (as specified to a template-id) requires backtracking we
12786 cannot use this technique when inside a template argument
12787 list. */
12788 if (!parser->in_template_argument_list_p
12789 && !parser->in_type_id_in_expr_p
12790 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12791 /* However, a parameter-declaration of the form
12792 "foat(f)" (which is a valid declaration of a
12793 parameter "f") can also be interpreted as an
12794 expression (the conversion of "f" to "float"). */
12795 && !parenthesized_p)
12796 cp_parser_commit_to_tentative_parse (parser);
12797 }
12798 else
12799 {
12800 cp_parser_error (parser, "expected %<,%> or %<...%>");
12801 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12802 cp_parser_skip_to_closing_parenthesis (parser,
12803 /*recovering=*/true,
12804 /*or_comma=*/false,
12805 /*consume_paren=*/false);
12806 break;
12807 }
12808 }
12809
12810 parser->in_unbraced_linkage_specification_p
12811 = saved_in_unbraced_linkage_specification_p;
12812
12813 return parameters;
12814 }
12815
12816 /* Parse a parameter declaration.
12817
12818 parameter-declaration:
12819 decl-specifier-seq ... [opt] declarator
12820 decl-specifier-seq declarator = assignment-expression
12821 decl-specifier-seq ... [opt] abstract-declarator [opt]
12822 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12823
12824 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12825 declares a template parameter. (In that case, a non-nested `>'
12826 token encountered during the parsing of the assignment-expression
12827 is not interpreted as a greater-than operator.)
12828
12829 Returns a representation of the parameter, or NULL if an error
12830 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12831 true iff the declarator is of the form "(p)". */
12832
12833 static cp_parameter_declarator *
12834 cp_parser_parameter_declaration (cp_parser *parser,
12835 bool template_parm_p,
12836 bool *parenthesized_p)
12837 {
12838 int declares_class_or_enum;
12839 bool greater_than_is_operator_p;
12840 cp_decl_specifier_seq decl_specifiers;
12841 cp_declarator *declarator;
12842 tree default_argument;
12843 cp_token *token;
12844 const char *saved_message;
12845
12846 /* In a template parameter, `>' is not an operator.
12847
12848 [temp.param]
12849
12850 When parsing a default template-argument for a non-type
12851 template-parameter, the first non-nested `>' is taken as the end
12852 of the template parameter-list rather than a greater-than
12853 operator. */
12854 greater_than_is_operator_p = !template_parm_p;
12855
12856 /* Type definitions may not appear in parameter types. */
12857 saved_message = parser->type_definition_forbidden_message;
12858 parser->type_definition_forbidden_message
12859 = "types may not be defined in parameter types";
12860
12861 /* Parse the declaration-specifiers. */
12862 cp_parser_decl_specifier_seq (parser,
12863 CP_PARSER_FLAGS_NONE,
12864 &decl_specifiers,
12865 &declares_class_or_enum);
12866 /* If an error occurred, there's no reason to attempt to parse the
12867 rest of the declaration. */
12868 if (cp_parser_error_occurred (parser))
12869 {
12870 parser->type_definition_forbidden_message = saved_message;
12871 return NULL;
12872 }
12873
12874 /* Peek at the next token. */
12875 token = cp_lexer_peek_token (parser->lexer);
12876
12877 /* If the next token is a `)', `,', `=', `>', or `...', then there
12878 is no declarator. However, when variadic templates are enabled,
12879 there may be a declarator following `...'. */
12880 if (token->type == CPP_CLOSE_PAREN
12881 || token->type == CPP_COMMA
12882 || token->type == CPP_EQ
12883 || token->type == CPP_GREATER)
12884 {
12885 declarator = NULL;
12886 if (parenthesized_p)
12887 *parenthesized_p = false;
12888 }
12889 /* Otherwise, there should be a declarator. */
12890 else
12891 {
12892 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12893 parser->default_arg_ok_p = false;
12894
12895 /* After seeing a decl-specifier-seq, if the next token is not a
12896 "(", there is no possibility that the code is a valid
12897 expression. Therefore, if parsing tentatively, we commit at
12898 this point. */
12899 if (!parser->in_template_argument_list_p
12900 /* In an expression context, having seen:
12901
12902 (int((char ...
12903
12904 we cannot be sure whether we are looking at a
12905 function-type (taking a "char" as a parameter) or a cast
12906 of some object of type "char" to "int". */
12907 && !parser->in_type_id_in_expr_p
12908 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12909 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12910 cp_parser_commit_to_tentative_parse (parser);
12911 /* Parse the declarator. */
12912 declarator = cp_parser_declarator (parser,
12913 CP_PARSER_DECLARATOR_EITHER,
12914 /*ctor_dtor_or_conv_p=*/NULL,
12915 parenthesized_p,
12916 /*member_p=*/false);
12917 parser->default_arg_ok_p = saved_default_arg_ok_p;
12918 /* After the declarator, allow more attributes. */
12919 decl_specifiers.attributes
12920 = chainon (decl_specifiers.attributes,
12921 cp_parser_attributes_opt (parser));
12922 }
12923
12924 /* If the next token is an ellipsis, and the type of the declarator
12925 contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12926 we actually have a parameter pack expansion expression. Otherwise,
12927 leave the ellipsis for a C-style variadic function. */
12928 token = cp_lexer_peek_token (parser->lexer);
12929 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12930 {
12931 tree type = decl_specifiers.type;
12932
12933 if (DECL_P (type))
12934 type = TREE_TYPE (type);
12935
12936 if (TREE_CODE (type) != TYPE_PACK_EXPANSION
12937 && (!declarator || !declarator->parameter_pack_p)
12938 && uses_parameter_packs (type))
12939 {
12940 /* Consume the `...'. */
12941 cp_lexer_consume_token (parser->lexer);
12942 maybe_warn_variadic_templates ();
12943
12944 /* Build a pack expansion type */
12945 if (declarator)
12946 declarator->parameter_pack_p = true;
12947 else
12948 decl_specifiers.type = make_pack_expansion (type);
12949 }
12950 }
12951
12952 /* The restriction on defining new types applies only to the type
12953 of the parameter, not to the default argument. */
12954 parser->type_definition_forbidden_message = saved_message;
12955
12956 /* If the next token is `=', then process a default argument. */
12957 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12958 {
12959 bool saved_greater_than_is_operator_p;
12960 /* Consume the `='. */
12961 cp_lexer_consume_token (parser->lexer);
12962
12963 /* If we are defining a class, then the tokens that make up the
12964 default argument must be saved and processed later. */
12965 if (!template_parm_p && at_class_scope_p ()
12966 && TYPE_BEING_DEFINED (current_class_type))
12967 {
12968 unsigned depth = 0;
12969 cp_token *first_token;
12970 cp_token *token;
12971
12972 /* Add tokens until we have processed the entire default
12973 argument. We add the range [first_token, token). */
12974 first_token = cp_lexer_peek_token (parser->lexer);
12975 while (true)
12976 {
12977 bool done = false;
12978
12979 /* Peek at the next token. */
12980 token = cp_lexer_peek_token (parser->lexer);
12981 /* What we do depends on what token we have. */
12982 switch (token->type)
12983 {
12984 /* In valid code, a default argument must be
12985 immediately followed by a `,' `)', or `...'. */
12986 case CPP_COMMA:
12987 case CPP_CLOSE_PAREN:
12988 case CPP_ELLIPSIS:
12989 /* If we run into a non-nested `;', `}', or `]',
12990 then the code is invalid -- but the default
12991 argument is certainly over. */
12992 case CPP_SEMICOLON:
12993 case CPP_CLOSE_BRACE:
12994 case CPP_CLOSE_SQUARE:
12995 if (depth == 0)
12996 done = true;
12997 /* Update DEPTH, if necessary. */
12998 else if (token->type == CPP_CLOSE_PAREN
12999 || token->type == CPP_CLOSE_BRACE
13000 || token->type == CPP_CLOSE_SQUARE)
13001 --depth;
13002 break;
13003
13004 case CPP_OPEN_PAREN:
13005 case CPP_OPEN_SQUARE:
13006 case CPP_OPEN_BRACE:
13007 ++depth;
13008 break;
13009
13010 case CPP_GREATER:
13011 /* If we see a non-nested `>', and `>' is not an
13012 operator, then it marks the end of the default
13013 argument. */
13014 if (!depth && !greater_than_is_operator_p)
13015 done = true;
13016 break;
13017
13018 /* If we run out of tokens, issue an error message. */
13019 case CPP_EOF:
13020 case CPP_PRAGMA_EOL:
13021 error ("file ends in default argument");
13022 done = true;
13023 break;
13024
13025 case CPP_NAME:
13026 case CPP_SCOPE:
13027 /* In these cases, we should look for template-ids.
13028 For example, if the default argument is
13029 `X<int, double>()', we need to do name lookup to
13030 figure out whether or not `X' is a template; if
13031 so, the `,' does not end the default argument.
13032
13033 That is not yet done. */
13034 break;
13035
13036 default:
13037 break;
13038 }
13039
13040 /* If we've reached the end, stop. */
13041 if (done)
13042 break;
13043
13044 /* Add the token to the token block. */
13045 token = cp_lexer_consume_token (parser->lexer);
13046 }
13047
13048 /* Create a DEFAULT_ARG to represented the unparsed default
13049 argument. */
13050 default_argument = make_node (DEFAULT_ARG);
13051 DEFARG_TOKENS (default_argument)
13052 = cp_token_cache_new (first_token, token);
13053 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13054 }
13055 /* Outside of a class definition, we can just parse the
13056 assignment-expression. */
13057 else
13058 {
13059 bool saved_local_variables_forbidden_p;
13060
13061 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13062 set correctly. */
13063 saved_greater_than_is_operator_p
13064 = parser->greater_than_is_operator_p;
13065 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13066 /* Local variable names (and the `this' keyword) may not
13067 appear in a default argument. */
13068 saved_local_variables_forbidden_p
13069 = parser->local_variables_forbidden_p;
13070 parser->local_variables_forbidden_p = true;
13071 /* The default argument expression may cause implicitly
13072 defined member functions to be synthesized, which will
13073 result in garbage collection. We must treat this
13074 situation as if we were within the body of function so as
13075 to avoid collecting live data on the stack. */
13076 ++function_depth;
13077 /* Parse the assignment-expression. */
13078 if (template_parm_p)
13079 push_deferring_access_checks (dk_no_deferred);
13080 default_argument
13081 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13082 if (template_parm_p)
13083 pop_deferring_access_checks ();
13084 /* Restore saved state. */
13085 --function_depth;
13086 parser->greater_than_is_operator_p
13087 = saved_greater_than_is_operator_p;
13088 parser->local_variables_forbidden_p
13089 = saved_local_variables_forbidden_p;
13090 }
13091 if (!parser->default_arg_ok_p)
13092 {
13093 if (!flag_pedantic_errors)
13094 warning (0, "deprecated use of default argument for parameter of non-function");
13095 else
13096 {
13097 error ("default arguments are only permitted for function parameters");
13098 default_argument = NULL_TREE;
13099 }
13100 }
13101 }
13102 else
13103 default_argument = NULL_TREE;
13104
13105 return make_parameter_declarator (&decl_specifiers,
13106 declarator,
13107 default_argument);
13108 }
13109
13110 /* Parse a function-body.
13111
13112 function-body:
13113 compound_statement */
13114
13115 static void
13116 cp_parser_function_body (cp_parser *parser)
13117 {
13118 cp_parser_compound_statement (parser, NULL, false);
13119 }
13120
13121 /* Parse a ctor-initializer-opt followed by a function-body. Return
13122 true if a ctor-initializer was present. */
13123
13124 static bool
13125 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13126 {
13127 tree body;
13128 bool ctor_initializer_p;
13129
13130 /* Begin the function body. */
13131 body = begin_function_body ();
13132 /* Parse the optional ctor-initializer. */
13133 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13134 /* Parse the function-body. */
13135 cp_parser_function_body (parser);
13136 /* Finish the function body. */
13137 finish_function_body (body);
13138
13139 return ctor_initializer_p;
13140 }
13141
13142 /* Parse an initializer.
13143
13144 initializer:
13145 = initializer-clause
13146 ( expression-list )
13147
13148 Returns an expression representing the initializer. If no
13149 initializer is present, NULL_TREE is returned.
13150
13151 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13152 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13153 set to FALSE if there is no initializer present. If there is an
13154 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13155 is set to true; otherwise it is set to false. */
13156
13157 static tree
13158 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13159 bool* non_constant_p)
13160 {
13161 cp_token *token;
13162 tree init;
13163
13164 /* Peek at the next token. */
13165 token = cp_lexer_peek_token (parser->lexer);
13166
13167 /* Let our caller know whether or not this initializer was
13168 parenthesized. */
13169 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13170 /* Assume that the initializer is constant. */
13171 *non_constant_p = false;
13172
13173 if (token->type == CPP_EQ)
13174 {
13175 /* Consume the `='. */
13176 cp_lexer_consume_token (parser->lexer);
13177 /* Parse the initializer-clause. */
13178 init = cp_parser_initializer_clause (parser, non_constant_p);
13179 }
13180 else if (token->type == CPP_OPEN_PAREN)
13181 init = cp_parser_parenthesized_expression_list (parser, false,
13182 /*cast_p=*/false,
13183 /*allow_expansion_p=*/true,
13184 non_constant_p);
13185 else
13186 {
13187 /* Anything else is an error. */
13188 cp_parser_error (parser, "expected initializer");
13189 init = error_mark_node;
13190 }
13191
13192 return init;
13193 }
13194
13195 /* Parse an initializer-clause.
13196
13197 initializer-clause:
13198 assignment-expression
13199 { initializer-list , [opt] }
13200 { }
13201
13202 Returns an expression representing the initializer.
13203
13204 If the `assignment-expression' production is used the value
13205 returned is simply a representation for the expression.
13206
13207 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13208 the elements of the initializer-list (or NULL, if the last
13209 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13210 NULL_TREE. There is no way to detect whether or not the optional
13211 trailing `,' was provided. NON_CONSTANT_P is as for
13212 cp_parser_initializer. */
13213
13214 static tree
13215 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13216 {
13217 tree initializer;
13218
13219 /* Assume the expression is constant. */
13220 *non_constant_p = false;
13221
13222 /* If it is not a `{', then we are looking at an
13223 assignment-expression. */
13224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13225 {
13226 initializer
13227 = cp_parser_constant_expression (parser,
13228 /*allow_non_constant_p=*/true,
13229 non_constant_p);
13230 if (!*non_constant_p)
13231 initializer = fold_non_dependent_expr (initializer);
13232 }
13233 else
13234 {
13235 /* Consume the `{' token. */
13236 cp_lexer_consume_token (parser->lexer);
13237 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13238 initializer = make_node (CONSTRUCTOR);
13239 /* If it's not a `}', then there is a non-trivial initializer. */
13240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13241 {
13242 /* Parse the initializer list. */
13243 CONSTRUCTOR_ELTS (initializer)
13244 = cp_parser_initializer_list (parser, non_constant_p);
13245 /* A trailing `,' token is allowed. */
13246 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13247 cp_lexer_consume_token (parser->lexer);
13248 }
13249 /* Now, there should be a trailing `}'. */
13250 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13251 }
13252
13253 return initializer;
13254 }
13255
13256 /* Parse an initializer-list.
13257
13258 initializer-list:
13259 initializer-clause ... [opt]
13260 initializer-list , initializer-clause ... [opt]
13261
13262 GNU Extension:
13263
13264 initializer-list:
13265 identifier : initializer-clause
13266 initializer-list, identifier : initializer-clause
13267
13268 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13269 for the initializer. If the INDEX of the elt is non-NULL, it is the
13270 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13271 as for cp_parser_initializer. */
13272
13273 static VEC(constructor_elt,gc) *
13274 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13275 {
13276 VEC(constructor_elt,gc) *v = NULL;
13277
13278 /* Assume all of the expressions are constant. */
13279 *non_constant_p = false;
13280
13281 /* Parse the rest of the list. */
13282 while (true)
13283 {
13284 cp_token *token;
13285 tree identifier;
13286 tree initializer;
13287 bool clause_non_constant_p;
13288
13289 /* If the next token is an identifier and the following one is a
13290 colon, we are looking at the GNU designated-initializer
13291 syntax. */
13292 if (cp_parser_allow_gnu_extensions_p (parser)
13293 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13294 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13295 {
13296 /* Warn the user that they are using an extension. */
13297 if (pedantic)
13298 pedwarn ("ISO C++ does not allow designated initializers");
13299 /* Consume the identifier. */
13300 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13301 /* Consume the `:'. */
13302 cp_lexer_consume_token (parser->lexer);
13303 }
13304 else
13305 identifier = NULL_TREE;
13306
13307 /* Parse the initializer. */
13308 initializer = cp_parser_initializer_clause (parser,
13309 &clause_non_constant_p);
13310 /* If any clause is non-constant, so is the entire initializer. */
13311 if (clause_non_constant_p)
13312 *non_constant_p = true;
13313
13314 /* If we have an ellipsis, this is an initializer pack
13315 expansion. */
13316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13317 {
13318 /* Consume the `...'. */
13319 cp_lexer_consume_token (parser->lexer);
13320
13321 /* Turn the initializer into an initializer expansion. */
13322 initializer = make_pack_expansion (initializer);
13323 }
13324
13325 /* Add it to the vector. */
13326 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13327
13328 /* If the next token is not a comma, we have reached the end of
13329 the list. */
13330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13331 break;
13332
13333 /* Peek at the next token. */
13334 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13335 /* If the next token is a `}', then we're still done. An
13336 initializer-clause can have a trailing `,' after the
13337 initializer-list and before the closing `}'. */
13338 if (token->type == CPP_CLOSE_BRACE)
13339 break;
13340
13341 /* Consume the `,' token. */
13342 cp_lexer_consume_token (parser->lexer);
13343 }
13344
13345 return v;
13346 }
13347
13348 /* Classes [gram.class] */
13349
13350 /* Parse a class-name.
13351
13352 class-name:
13353 identifier
13354 template-id
13355
13356 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13357 to indicate that names looked up in dependent types should be
13358 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13359 keyword has been used to indicate that the name that appears next
13360 is a template. TAG_TYPE indicates the explicit tag given before
13361 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13362 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13363 is the class being defined in a class-head.
13364
13365 Returns the TYPE_DECL representing the class. */
13366
13367 static tree
13368 cp_parser_class_name (cp_parser *parser,
13369 bool typename_keyword_p,
13370 bool template_keyword_p,
13371 enum tag_types tag_type,
13372 bool check_dependency_p,
13373 bool class_head_p,
13374 bool is_declaration)
13375 {
13376 tree decl;
13377 tree scope;
13378 bool typename_p;
13379 cp_token *token;
13380
13381 /* All class-names start with an identifier. */
13382 token = cp_lexer_peek_token (parser->lexer);
13383 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13384 {
13385 cp_parser_error (parser, "expected class-name");
13386 return error_mark_node;
13387 }
13388
13389 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13390 to a template-id, so we save it here. */
13391 scope = parser->scope;
13392 if (scope == error_mark_node)
13393 return error_mark_node;
13394
13395 /* Any name names a type if we're following the `typename' keyword
13396 in a qualified name where the enclosing scope is type-dependent. */
13397 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13398 && dependent_type_p (scope));
13399 /* Handle the common case (an identifier, but not a template-id)
13400 efficiently. */
13401 if (token->type == CPP_NAME
13402 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13403 {
13404 cp_token *identifier_token;
13405 tree identifier;
13406 bool ambiguous_p;
13407
13408 /* Look for the identifier. */
13409 identifier_token = cp_lexer_peek_token (parser->lexer);
13410 ambiguous_p = identifier_token->ambiguous_p;
13411 identifier = cp_parser_identifier (parser);
13412 /* If the next token isn't an identifier, we are certainly not
13413 looking at a class-name. */
13414 if (identifier == error_mark_node)
13415 decl = error_mark_node;
13416 /* If we know this is a type-name, there's no need to look it
13417 up. */
13418 else if (typename_p)
13419 decl = identifier;
13420 else
13421 {
13422 tree ambiguous_decls;
13423 /* If we already know that this lookup is ambiguous, then
13424 we've already issued an error message; there's no reason
13425 to check again. */
13426 if (ambiguous_p)
13427 {
13428 cp_parser_simulate_error (parser);
13429 return error_mark_node;
13430 }
13431 /* If the next token is a `::', then the name must be a type
13432 name.
13433
13434 [basic.lookup.qual]
13435
13436 During the lookup for a name preceding the :: scope
13437 resolution operator, object, function, and enumerator
13438 names are ignored. */
13439 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13440 tag_type = typename_type;
13441 /* Look up the name. */
13442 decl = cp_parser_lookup_name (parser, identifier,
13443 tag_type,
13444 /*is_template=*/false,
13445 /*is_namespace=*/false,
13446 check_dependency_p,
13447 &ambiguous_decls);
13448 if (ambiguous_decls)
13449 {
13450 error ("reference to %qD is ambiguous", identifier);
13451 print_candidates (ambiguous_decls);
13452 if (cp_parser_parsing_tentatively (parser))
13453 {
13454 identifier_token->ambiguous_p = true;
13455 cp_parser_simulate_error (parser);
13456 }
13457 return error_mark_node;
13458 }
13459 }
13460 }
13461 else
13462 {
13463 /* Try a template-id. */
13464 decl = cp_parser_template_id (parser, template_keyword_p,
13465 check_dependency_p,
13466 is_declaration);
13467 if (decl == error_mark_node)
13468 return error_mark_node;
13469 }
13470
13471 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13472
13473 /* If this is a typename, create a TYPENAME_TYPE. */
13474 if (typename_p && decl != error_mark_node)
13475 {
13476 decl = make_typename_type (scope, decl, typename_type,
13477 /*complain=*/tf_error);
13478 if (decl != error_mark_node)
13479 decl = TYPE_NAME (decl);
13480 }
13481
13482 /* Check to see that it is really the name of a class. */
13483 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13484 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13485 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13486 /* Situations like this:
13487
13488 template <typename T> struct A {
13489 typename T::template X<int>::I i;
13490 };
13491
13492 are problematic. Is `T::template X<int>' a class-name? The
13493 standard does not seem to be definitive, but there is no other
13494 valid interpretation of the following `::'. Therefore, those
13495 names are considered class-names. */
13496 {
13497 decl = make_typename_type (scope, decl, tag_type, tf_error);
13498 if (decl != error_mark_node)
13499 decl = TYPE_NAME (decl);
13500 }
13501 else if (TREE_CODE (decl) != TYPE_DECL
13502 || TREE_TYPE (decl) == error_mark_node
13503 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13504 decl = error_mark_node;
13505
13506 if (decl == error_mark_node)
13507 cp_parser_error (parser, "expected class-name");
13508
13509 return decl;
13510 }
13511
13512 /* Parse a class-specifier.
13513
13514 class-specifier:
13515 class-head { member-specification [opt] }
13516
13517 Returns the TREE_TYPE representing the class. */
13518
13519 static tree
13520 cp_parser_class_specifier (cp_parser* parser)
13521 {
13522 cp_token *token;
13523 tree type;
13524 tree attributes = NULL_TREE;
13525 int has_trailing_semicolon;
13526 bool nested_name_specifier_p;
13527 unsigned saved_num_template_parameter_lists;
13528 bool saved_in_function_body;
13529 tree old_scope = NULL_TREE;
13530 tree scope = NULL_TREE;
13531 tree bases;
13532
13533 push_deferring_access_checks (dk_no_deferred);
13534
13535 /* Parse the class-head. */
13536 type = cp_parser_class_head (parser,
13537 &nested_name_specifier_p,
13538 &attributes,
13539 &bases);
13540 /* If the class-head was a semantic disaster, skip the entire body
13541 of the class. */
13542 if (!type)
13543 {
13544 cp_parser_skip_to_end_of_block_or_statement (parser);
13545 pop_deferring_access_checks ();
13546 return error_mark_node;
13547 }
13548
13549 /* Look for the `{'. */
13550 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13551 {
13552 pop_deferring_access_checks ();
13553 return error_mark_node;
13554 }
13555
13556 /* Process the base classes. If they're invalid, skip the
13557 entire class body. */
13558 if (!xref_basetypes (type, bases))
13559 {
13560 cp_parser_skip_to_closing_brace (parser);
13561
13562 /* Consuming the closing brace yields better error messages
13563 later on. */
13564 cp_lexer_consume_token (parser->lexer);
13565 pop_deferring_access_checks ();
13566 return error_mark_node;
13567 }
13568
13569 /* Issue an error message if type-definitions are forbidden here. */
13570 cp_parser_check_type_definition (parser);
13571 /* Remember that we are defining one more class. */
13572 ++parser->num_classes_being_defined;
13573 /* Inside the class, surrounding template-parameter-lists do not
13574 apply. */
13575 saved_num_template_parameter_lists
13576 = parser->num_template_parameter_lists;
13577 parser->num_template_parameter_lists = 0;
13578 /* We are not in a function body. */
13579 saved_in_function_body = parser->in_function_body;
13580 parser->in_function_body = false;
13581
13582 /* Start the class. */
13583 if (nested_name_specifier_p)
13584 {
13585 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13586 old_scope = push_inner_scope (scope);
13587 }
13588 type = begin_class_definition (type, attributes);
13589
13590 if (type == error_mark_node)
13591 /* If the type is erroneous, skip the entire body of the class. */
13592 cp_parser_skip_to_closing_brace (parser);
13593 else
13594 /* Parse the member-specification. */
13595 cp_parser_member_specification_opt (parser);
13596
13597 /* Look for the trailing `}'. */
13598 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13599 /* We get better error messages by noticing a common problem: a
13600 missing trailing `;'. */
13601 token = cp_lexer_peek_token (parser->lexer);
13602 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13603 /* Look for trailing attributes to apply to this class. */
13604 if (cp_parser_allow_gnu_extensions_p (parser))
13605 attributes = cp_parser_attributes_opt (parser);
13606 if (type != error_mark_node)
13607 type = finish_struct (type, attributes);
13608 if (nested_name_specifier_p)
13609 pop_inner_scope (old_scope, scope);
13610 /* If this class is not itself within the scope of another class,
13611 then we need to parse the bodies of all of the queued function
13612 definitions. Note that the queued functions defined in a class
13613 are not always processed immediately following the
13614 class-specifier for that class. Consider:
13615
13616 struct A {
13617 struct B { void f() { sizeof (A); } };
13618 };
13619
13620 If `f' were processed before the processing of `A' were
13621 completed, there would be no way to compute the size of `A'.
13622 Note that the nesting we are interested in here is lexical --
13623 not the semantic nesting given by TYPE_CONTEXT. In particular,
13624 for:
13625
13626 struct A { struct B; };
13627 struct A::B { void f() { } };
13628
13629 there is no need to delay the parsing of `A::B::f'. */
13630 if (--parser->num_classes_being_defined == 0)
13631 {
13632 tree queue_entry;
13633 tree fn;
13634 tree class_type = NULL_TREE;
13635 tree pushed_scope = NULL_TREE;
13636
13637 /* In a first pass, parse default arguments to the functions.
13638 Then, in a second pass, parse the bodies of the functions.
13639 This two-phased approach handles cases like:
13640
13641 struct S {
13642 void f() { g(); }
13643 void g(int i = 3);
13644 };
13645
13646 */
13647 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13648 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13649 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13650 TREE_PURPOSE (parser->unparsed_functions_queues)
13651 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13652 {
13653 fn = TREE_VALUE (queue_entry);
13654 /* If there are default arguments that have not yet been processed,
13655 take care of them now. */
13656 if (class_type != TREE_PURPOSE (queue_entry))
13657 {
13658 if (pushed_scope)
13659 pop_scope (pushed_scope);
13660 class_type = TREE_PURPOSE (queue_entry);
13661 pushed_scope = push_scope (class_type);
13662 }
13663 /* Make sure that any template parameters are in scope. */
13664 maybe_begin_member_template_processing (fn);
13665 /* Parse the default argument expressions. */
13666 cp_parser_late_parsing_default_args (parser, fn);
13667 /* Remove any template parameters from the symbol table. */
13668 maybe_end_member_template_processing ();
13669 }
13670 if (pushed_scope)
13671 pop_scope (pushed_scope);
13672 /* Now parse the body of the functions. */
13673 for (TREE_VALUE (parser->unparsed_functions_queues)
13674 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13675 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13676 TREE_VALUE (parser->unparsed_functions_queues)
13677 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13678 {
13679 /* Figure out which function we need to process. */
13680 fn = TREE_VALUE (queue_entry);
13681 /* Parse the function. */
13682 cp_parser_late_parsing_for_member (parser, fn);
13683 }
13684 }
13685
13686 /* Put back any saved access checks. */
13687 pop_deferring_access_checks ();
13688
13689 /* Restore saved state. */
13690 parser->in_function_body = saved_in_function_body;
13691 parser->num_template_parameter_lists
13692 = saved_num_template_parameter_lists;
13693
13694 return type;
13695 }
13696
13697 /* Parse a class-head.
13698
13699 class-head:
13700 class-key identifier [opt] base-clause [opt]
13701 class-key nested-name-specifier identifier base-clause [opt]
13702 class-key nested-name-specifier [opt] template-id
13703 base-clause [opt]
13704
13705 GNU Extensions:
13706 class-key attributes identifier [opt] base-clause [opt]
13707 class-key attributes nested-name-specifier identifier base-clause [opt]
13708 class-key attributes nested-name-specifier [opt] template-id
13709 base-clause [opt]
13710
13711 Upon return BASES is initialized to the list of base classes (or
13712 NULL, if there are none) in the same form returned by
13713 cp_parser_base_clause.
13714
13715 Returns the TYPE of the indicated class. Sets
13716 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13717 involving a nested-name-specifier was used, and FALSE otherwise.
13718
13719 Returns error_mark_node if this is not a class-head.
13720
13721 Returns NULL_TREE if the class-head is syntactically valid, but
13722 semantically invalid in a way that means we should skip the entire
13723 body of the class. */
13724
13725 static tree
13726 cp_parser_class_head (cp_parser* parser,
13727 bool* nested_name_specifier_p,
13728 tree *attributes_p,
13729 tree *bases)
13730 {
13731 tree nested_name_specifier;
13732 enum tag_types class_key;
13733 tree id = NULL_TREE;
13734 tree type = NULL_TREE;
13735 tree attributes;
13736 bool template_id_p = false;
13737 bool qualified_p = false;
13738 bool invalid_nested_name_p = false;
13739 bool invalid_explicit_specialization_p = false;
13740 tree pushed_scope = NULL_TREE;
13741 unsigned num_templates;
13742
13743 /* Assume no nested-name-specifier will be present. */
13744 *nested_name_specifier_p = false;
13745 /* Assume no template parameter lists will be used in defining the
13746 type. */
13747 num_templates = 0;
13748
13749 *bases = NULL_TREE;
13750
13751 /* Look for the class-key. */
13752 class_key = cp_parser_class_key (parser);
13753 if (class_key == none_type)
13754 return error_mark_node;
13755
13756 /* Parse the attributes. */
13757 attributes = cp_parser_attributes_opt (parser);
13758
13759 /* If the next token is `::', that is invalid -- but sometimes
13760 people do try to write:
13761
13762 struct ::S {};
13763
13764 Handle this gracefully by accepting the extra qualifier, and then
13765 issuing an error about it later if this really is a
13766 class-head. If it turns out just to be an elaborated type
13767 specifier, remain silent. */
13768 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13769 qualified_p = true;
13770
13771 push_deferring_access_checks (dk_no_check);
13772
13773 /* Determine the name of the class. Begin by looking for an
13774 optional nested-name-specifier. */
13775 nested_name_specifier
13776 = cp_parser_nested_name_specifier_opt (parser,
13777 /*typename_keyword_p=*/false,
13778 /*check_dependency_p=*/false,
13779 /*type_p=*/false,
13780 /*is_declaration=*/false);
13781 /* If there was a nested-name-specifier, then there *must* be an
13782 identifier. */
13783 if (nested_name_specifier)
13784 {
13785 /* Although the grammar says `identifier', it really means
13786 `class-name' or `template-name'. You are only allowed to
13787 define a class that has already been declared with this
13788 syntax.
13789
13790 The proposed resolution for Core Issue 180 says that wherever
13791 you see `class T::X' you should treat `X' as a type-name.
13792
13793 It is OK to define an inaccessible class; for example:
13794
13795 class A { class B; };
13796 class A::B {};
13797
13798 We do not know if we will see a class-name, or a
13799 template-name. We look for a class-name first, in case the
13800 class-name is a template-id; if we looked for the
13801 template-name first we would stop after the template-name. */
13802 cp_parser_parse_tentatively (parser);
13803 type = cp_parser_class_name (parser,
13804 /*typename_keyword_p=*/false,
13805 /*template_keyword_p=*/false,
13806 class_type,
13807 /*check_dependency_p=*/false,
13808 /*class_head_p=*/true,
13809 /*is_declaration=*/false);
13810 /* If that didn't work, ignore the nested-name-specifier. */
13811 if (!cp_parser_parse_definitely (parser))
13812 {
13813 invalid_nested_name_p = true;
13814 id = cp_parser_identifier (parser);
13815 if (id == error_mark_node)
13816 id = NULL_TREE;
13817 }
13818 /* If we could not find a corresponding TYPE, treat this
13819 declaration like an unqualified declaration. */
13820 if (type == error_mark_node)
13821 nested_name_specifier = NULL_TREE;
13822 /* Otherwise, count the number of templates used in TYPE and its
13823 containing scopes. */
13824 else
13825 {
13826 tree scope;
13827
13828 for (scope = TREE_TYPE (type);
13829 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13830 scope = (TYPE_P (scope)
13831 ? TYPE_CONTEXT (scope)
13832 : DECL_CONTEXT (scope)))
13833 if (TYPE_P (scope)
13834 && CLASS_TYPE_P (scope)
13835 && CLASSTYPE_TEMPLATE_INFO (scope)
13836 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13837 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13838 ++num_templates;
13839 }
13840 }
13841 /* Otherwise, the identifier is optional. */
13842 else
13843 {
13844 /* We don't know whether what comes next is a template-id,
13845 an identifier, or nothing at all. */
13846 cp_parser_parse_tentatively (parser);
13847 /* Check for a template-id. */
13848 id = cp_parser_template_id (parser,
13849 /*template_keyword_p=*/false,
13850 /*check_dependency_p=*/true,
13851 /*is_declaration=*/true);
13852 /* If that didn't work, it could still be an identifier. */
13853 if (!cp_parser_parse_definitely (parser))
13854 {
13855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13856 id = cp_parser_identifier (parser);
13857 else
13858 id = NULL_TREE;
13859 }
13860 else
13861 {
13862 template_id_p = true;
13863 ++num_templates;
13864 }
13865 }
13866
13867 pop_deferring_access_checks ();
13868
13869 if (id)
13870 cp_parser_check_for_invalid_template_id (parser, id);
13871
13872 /* If it's not a `:' or a `{' then we can't really be looking at a
13873 class-head, since a class-head only appears as part of a
13874 class-specifier. We have to detect this situation before calling
13875 xref_tag, since that has irreversible side-effects. */
13876 if (!cp_parser_next_token_starts_class_definition_p (parser))
13877 {
13878 cp_parser_error (parser, "expected %<{%> or %<:%>");
13879 return error_mark_node;
13880 }
13881
13882 /* At this point, we're going ahead with the class-specifier, even
13883 if some other problem occurs. */
13884 cp_parser_commit_to_tentative_parse (parser);
13885 /* Issue the error about the overly-qualified name now. */
13886 if (qualified_p)
13887 cp_parser_error (parser,
13888 "global qualification of class name is invalid");
13889 else if (invalid_nested_name_p)
13890 cp_parser_error (parser,
13891 "qualified name does not name a class");
13892 else if (nested_name_specifier)
13893 {
13894 tree scope;
13895
13896 /* Reject typedef-names in class heads. */
13897 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13898 {
13899 error ("invalid class name in declaration of %qD", type);
13900 type = NULL_TREE;
13901 goto done;
13902 }
13903
13904 /* Figure out in what scope the declaration is being placed. */
13905 scope = current_scope ();
13906 /* If that scope does not contain the scope in which the
13907 class was originally declared, the program is invalid. */
13908 if (scope && !is_ancestor (scope, nested_name_specifier))
13909 {
13910 error ("declaration of %qD in %qD which does not enclose %qD",
13911 type, scope, nested_name_specifier);
13912 type = NULL_TREE;
13913 goto done;
13914 }
13915 /* [dcl.meaning]
13916
13917 A declarator-id shall not be qualified exception of the
13918 definition of a ... nested class outside of its class
13919 ... [or] a the definition or explicit instantiation of a
13920 class member of a namespace outside of its namespace. */
13921 if (scope == nested_name_specifier)
13922 {
13923 pedwarn ("extra qualification ignored");
13924 nested_name_specifier = NULL_TREE;
13925 num_templates = 0;
13926 }
13927 }
13928 /* An explicit-specialization must be preceded by "template <>". If
13929 it is not, try to recover gracefully. */
13930 if (at_namespace_scope_p ()
13931 && parser->num_template_parameter_lists == 0
13932 && template_id_p)
13933 {
13934 error ("an explicit specialization must be preceded by %<template <>%>");
13935 invalid_explicit_specialization_p = true;
13936 /* Take the same action that would have been taken by
13937 cp_parser_explicit_specialization. */
13938 ++parser->num_template_parameter_lists;
13939 begin_specialization ();
13940 }
13941 /* There must be no "return" statements between this point and the
13942 end of this function; set "type "to the correct return value and
13943 use "goto done;" to return. */
13944 /* Make sure that the right number of template parameters were
13945 present. */
13946 if (!cp_parser_check_template_parameters (parser, num_templates))
13947 {
13948 /* If something went wrong, there is no point in even trying to
13949 process the class-definition. */
13950 type = NULL_TREE;
13951 goto done;
13952 }
13953
13954 /* Look up the type. */
13955 if (template_id_p)
13956 {
13957 type = TREE_TYPE (id);
13958 type = maybe_process_partial_specialization (type);
13959 if (nested_name_specifier)
13960 pushed_scope = push_scope (nested_name_specifier);
13961 }
13962 else if (nested_name_specifier)
13963 {
13964 tree class_type;
13965
13966 /* Given:
13967
13968 template <typename T> struct S { struct T };
13969 template <typename T> struct S<T>::T { };
13970
13971 we will get a TYPENAME_TYPE when processing the definition of
13972 `S::T'. We need to resolve it to the actual type before we
13973 try to define it. */
13974 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13975 {
13976 class_type = resolve_typename_type (TREE_TYPE (type),
13977 /*only_current_p=*/false);
13978 if (class_type != error_mark_node)
13979 type = TYPE_NAME (class_type);
13980 else
13981 {
13982 cp_parser_error (parser, "could not resolve typename type");
13983 type = error_mark_node;
13984 }
13985 }
13986
13987 maybe_process_partial_specialization (TREE_TYPE (type));
13988 class_type = current_class_type;
13989 /* Enter the scope indicated by the nested-name-specifier. */
13990 pushed_scope = push_scope (nested_name_specifier);
13991 /* Get the canonical version of this type. */
13992 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13993 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13994 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13995 {
13996 type = push_template_decl (type);
13997 if (type == error_mark_node)
13998 {
13999 type = NULL_TREE;
14000 goto done;
14001 }
14002 }
14003
14004 type = TREE_TYPE (type);
14005 *nested_name_specifier_p = true;
14006 }
14007 else /* The name is not a nested name. */
14008 {
14009 /* If the class was unnamed, create a dummy name. */
14010 if (!id)
14011 id = make_anon_name ();
14012 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14013 parser->num_template_parameter_lists);
14014 }
14015
14016 /* Indicate whether this class was declared as a `class' or as a
14017 `struct'. */
14018 if (TREE_CODE (type) == RECORD_TYPE)
14019 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14020 cp_parser_check_class_key (class_key, type);
14021
14022 /* If this type was already complete, and we see another definition,
14023 that's an error. */
14024 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14025 {
14026 error ("redefinition of %q#T", type);
14027 error ("previous definition of %q+#T", type);
14028 type = NULL_TREE;
14029 goto done;
14030 }
14031 else if (type == error_mark_node)
14032 type = NULL_TREE;
14033
14034 /* We will have entered the scope containing the class; the names of
14035 base classes should be looked up in that context. For example:
14036
14037 struct A { struct B {}; struct C; };
14038 struct A::C : B {};
14039
14040 is valid. */
14041
14042 /* Get the list of base-classes, if there is one. */
14043 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14044 *bases = cp_parser_base_clause (parser);
14045
14046 done:
14047 /* Leave the scope given by the nested-name-specifier. We will
14048 enter the class scope itself while processing the members. */
14049 if (pushed_scope)
14050 pop_scope (pushed_scope);
14051
14052 if (invalid_explicit_specialization_p)
14053 {
14054 end_specialization ();
14055 --parser->num_template_parameter_lists;
14056 }
14057 *attributes_p = attributes;
14058 return type;
14059 }
14060
14061 /* Parse a class-key.
14062
14063 class-key:
14064 class
14065 struct
14066 union
14067
14068 Returns the kind of class-key specified, or none_type to indicate
14069 error. */
14070
14071 static enum tag_types
14072 cp_parser_class_key (cp_parser* parser)
14073 {
14074 cp_token *token;
14075 enum tag_types tag_type;
14076
14077 /* Look for the class-key. */
14078 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14079 if (!token)
14080 return none_type;
14081
14082 /* Check to see if the TOKEN is a class-key. */
14083 tag_type = cp_parser_token_is_class_key (token);
14084 if (!tag_type)
14085 cp_parser_error (parser, "expected class-key");
14086 return tag_type;
14087 }
14088
14089 /* Parse an (optional) member-specification.
14090
14091 member-specification:
14092 member-declaration member-specification [opt]
14093 access-specifier : member-specification [opt] */
14094
14095 static void
14096 cp_parser_member_specification_opt (cp_parser* parser)
14097 {
14098 while (true)
14099 {
14100 cp_token *token;
14101 enum rid keyword;
14102
14103 /* Peek at the next token. */
14104 token = cp_lexer_peek_token (parser->lexer);
14105 /* If it's a `}', or EOF then we've seen all the members. */
14106 if (token->type == CPP_CLOSE_BRACE
14107 || token->type == CPP_EOF
14108 || token->type == CPP_PRAGMA_EOL)
14109 break;
14110
14111 /* See if this token is a keyword. */
14112 keyword = token->keyword;
14113 switch (keyword)
14114 {
14115 case RID_PUBLIC:
14116 case RID_PROTECTED:
14117 case RID_PRIVATE:
14118 /* Consume the access-specifier. */
14119 cp_lexer_consume_token (parser->lexer);
14120 /* Remember which access-specifier is active. */
14121 current_access_specifier = token->u.value;
14122 /* Look for the `:'. */
14123 cp_parser_require (parser, CPP_COLON, "`:'");
14124 break;
14125
14126 default:
14127 /* Accept #pragmas at class scope. */
14128 if (token->type == CPP_PRAGMA)
14129 {
14130 cp_parser_pragma (parser, pragma_external);
14131 break;
14132 }
14133
14134 /* Otherwise, the next construction must be a
14135 member-declaration. */
14136 cp_parser_member_declaration (parser);
14137 }
14138 }
14139 }
14140
14141 /* Parse a member-declaration.
14142
14143 member-declaration:
14144 decl-specifier-seq [opt] member-declarator-list [opt] ;
14145 function-definition ; [opt]
14146 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14147 using-declaration
14148 template-declaration
14149
14150 member-declarator-list:
14151 member-declarator
14152 member-declarator-list , member-declarator
14153
14154 member-declarator:
14155 declarator pure-specifier [opt]
14156 declarator constant-initializer [opt]
14157 identifier [opt] : constant-expression
14158
14159 GNU Extensions:
14160
14161 member-declaration:
14162 __extension__ member-declaration
14163
14164 member-declarator:
14165 declarator attributes [opt] pure-specifier [opt]
14166 declarator attributes [opt] constant-initializer [opt]
14167 identifier [opt] attributes [opt] : constant-expression
14168
14169 C++0x Extensions:
14170
14171 member-declaration:
14172 static_assert-declaration */
14173
14174 static void
14175 cp_parser_member_declaration (cp_parser* parser)
14176 {
14177 cp_decl_specifier_seq decl_specifiers;
14178 tree prefix_attributes;
14179 tree decl;
14180 int declares_class_or_enum;
14181 bool friend_p;
14182 cp_token *token;
14183 int saved_pedantic;
14184
14185 /* Check for the `__extension__' keyword. */
14186 if (cp_parser_extension_opt (parser, &saved_pedantic))
14187 {
14188 /* Recurse. */
14189 cp_parser_member_declaration (parser);
14190 /* Restore the old value of the PEDANTIC flag. */
14191 pedantic = saved_pedantic;
14192
14193 return;
14194 }
14195
14196 /* Check for a template-declaration. */
14197 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14198 {
14199 /* An explicit specialization here is an error condition, and we
14200 expect the specialization handler to detect and report this. */
14201 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14202 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14203 cp_parser_explicit_specialization (parser);
14204 else
14205 cp_parser_template_declaration (parser, /*member_p=*/true);
14206
14207 return;
14208 }
14209
14210 /* Check for a using-declaration. */
14211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14212 {
14213 /* Parse the using-declaration. */
14214 cp_parser_using_declaration (parser,
14215 /*access_declaration_p=*/false);
14216 return;
14217 }
14218
14219 /* Check for @defs. */
14220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14221 {
14222 tree ivar, member;
14223 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14224 ivar = ivar_chains;
14225 while (ivar)
14226 {
14227 member = ivar;
14228 ivar = TREE_CHAIN (member);
14229 TREE_CHAIN (member) = NULL_TREE;
14230 finish_member_declaration (member);
14231 }
14232 return;
14233 }
14234
14235 /* If the next token is `static_assert' we have a static assertion. */
14236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14237 {
14238 cp_parser_static_assert (parser, /*member_p=*/true);
14239 return;
14240 }
14241
14242 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14243 return;
14244
14245 /* Parse the decl-specifier-seq. */
14246 cp_parser_decl_specifier_seq (parser,
14247 CP_PARSER_FLAGS_OPTIONAL,
14248 &decl_specifiers,
14249 &declares_class_or_enum);
14250 prefix_attributes = decl_specifiers.attributes;
14251 decl_specifiers.attributes = NULL_TREE;
14252 /* Check for an invalid type-name. */
14253 if (!decl_specifiers.type
14254 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14255 return;
14256 /* If there is no declarator, then the decl-specifier-seq should
14257 specify a type. */
14258 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14259 {
14260 /* If there was no decl-specifier-seq, and the next token is a
14261 `;', then we have something like:
14262
14263 struct S { ; };
14264
14265 [class.mem]
14266
14267 Each member-declaration shall declare at least one member
14268 name of the class. */
14269 if (!decl_specifiers.any_specifiers_p)
14270 {
14271 cp_token *token = cp_lexer_peek_token (parser->lexer);
14272 if (pedantic && !token->in_system_header)
14273 pedwarn ("%Hextra %<;%>", &token->location);
14274 }
14275 else
14276 {
14277 tree type;
14278
14279 /* See if this declaration is a friend. */
14280 friend_p = cp_parser_friend_p (&decl_specifiers);
14281 /* If there were decl-specifiers, check to see if there was
14282 a class-declaration. */
14283 type = check_tag_decl (&decl_specifiers);
14284 /* Nested classes have already been added to the class, but
14285 a `friend' needs to be explicitly registered. */
14286 if (friend_p)
14287 {
14288 /* If the `friend' keyword was present, the friend must
14289 be introduced with a class-key. */
14290 if (!declares_class_or_enum)
14291 error ("a class-key must be used when declaring a friend");
14292 /* In this case:
14293
14294 template <typename T> struct A {
14295 friend struct A<T>::B;
14296 };
14297
14298 A<T>::B will be represented by a TYPENAME_TYPE, and
14299 therefore not recognized by check_tag_decl. */
14300 if (!type
14301 && decl_specifiers.type
14302 && TYPE_P (decl_specifiers.type))
14303 type = decl_specifiers.type;
14304 if (!type || !TYPE_P (type))
14305 error ("friend declaration does not name a class or "
14306 "function");
14307 else
14308 make_friend_class (current_class_type, type,
14309 /*complain=*/true);
14310 }
14311 /* If there is no TYPE, an error message will already have
14312 been issued. */
14313 else if (!type || type == error_mark_node)
14314 ;
14315 /* An anonymous aggregate has to be handled specially; such
14316 a declaration really declares a data member (with a
14317 particular type), as opposed to a nested class. */
14318 else if (ANON_AGGR_TYPE_P (type))
14319 {
14320 /* Remove constructors and such from TYPE, now that we
14321 know it is an anonymous aggregate. */
14322 fixup_anonymous_aggr (type);
14323 /* And make the corresponding data member. */
14324 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14325 /* Add it to the class. */
14326 finish_member_declaration (decl);
14327 }
14328 else
14329 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14330 }
14331 }
14332 else
14333 {
14334 /* See if these declarations will be friends. */
14335 friend_p = cp_parser_friend_p (&decl_specifiers);
14336
14337 /* Keep going until we hit the `;' at the end of the
14338 declaration. */
14339 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14340 {
14341 tree attributes = NULL_TREE;
14342 tree first_attribute;
14343
14344 /* Peek at the next token. */
14345 token = cp_lexer_peek_token (parser->lexer);
14346
14347 /* Check for a bitfield declaration. */
14348 if (token->type == CPP_COLON
14349 || (token->type == CPP_NAME
14350 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14351 == CPP_COLON))
14352 {
14353 tree identifier;
14354 tree width;
14355
14356 /* Get the name of the bitfield. Note that we cannot just
14357 check TOKEN here because it may have been invalidated by
14358 the call to cp_lexer_peek_nth_token above. */
14359 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14360 identifier = cp_parser_identifier (parser);
14361 else
14362 identifier = NULL_TREE;
14363
14364 /* Consume the `:' token. */
14365 cp_lexer_consume_token (parser->lexer);
14366 /* Get the width of the bitfield. */
14367 width
14368 = cp_parser_constant_expression (parser,
14369 /*allow_non_constant=*/false,
14370 NULL);
14371
14372 /* Look for attributes that apply to the bitfield. */
14373 attributes = cp_parser_attributes_opt (parser);
14374 /* Remember which attributes are prefix attributes and
14375 which are not. */
14376 first_attribute = attributes;
14377 /* Combine the attributes. */
14378 attributes = chainon (prefix_attributes, attributes);
14379
14380 /* Create the bitfield declaration. */
14381 decl = grokbitfield (identifier
14382 ? make_id_declarator (NULL_TREE,
14383 identifier,
14384 sfk_none)
14385 : NULL,
14386 &decl_specifiers,
14387 width);
14388 /* Apply the attributes. */
14389 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14390 }
14391 else
14392 {
14393 cp_declarator *declarator;
14394 tree initializer;
14395 tree asm_specification;
14396 int ctor_dtor_or_conv_p;
14397
14398 /* Parse the declarator. */
14399 declarator
14400 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14401 &ctor_dtor_or_conv_p,
14402 /*parenthesized_p=*/NULL,
14403 /*member_p=*/true);
14404
14405 /* If something went wrong parsing the declarator, make sure
14406 that we at least consume some tokens. */
14407 if (declarator == cp_error_declarator)
14408 {
14409 /* Skip to the end of the statement. */
14410 cp_parser_skip_to_end_of_statement (parser);
14411 /* If the next token is not a semicolon, that is
14412 probably because we just skipped over the body of
14413 a function. So, we consume a semicolon if
14414 present, but do not issue an error message if it
14415 is not present. */
14416 if (cp_lexer_next_token_is (parser->lexer,
14417 CPP_SEMICOLON))
14418 cp_lexer_consume_token (parser->lexer);
14419 return;
14420 }
14421
14422 if (declares_class_or_enum & 2)
14423 cp_parser_check_for_definition_in_return_type
14424 (declarator, decl_specifiers.type);
14425
14426 /* Look for an asm-specification. */
14427 asm_specification = cp_parser_asm_specification_opt (parser);
14428 /* Look for attributes that apply to the declaration. */
14429 attributes = cp_parser_attributes_opt (parser);
14430 /* Remember which attributes are prefix attributes and
14431 which are not. */
14432 first_attribute = attributes;
14433 /* Combine the attributes. */
14434 attributes = chainon (prefix_attributes, attributes);
14435
14436 /* If it's an `=', then we have a constant-initializer or a
14437 pure-specifier. It is not correct to parse the
14438 initializer before registering the member declaration
14439 since the member declaration should be in scope while
14440 its initializer is processed. However, the rest of the
14441 front end does not yet provide an interface that allows
14442 us to handle this correctly. */
14443 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14444 {
14445 /* In [class.mem]:
14446
14447 A pure-specifier shall be used only in the declaration of
14448 a virtual function.
14449
14450 A member-declarator can contain a constant-initializer
14451 only if it declares a static member of integral or
14452 enumeration type.
14453
14454 Therefore, if the DECLARATOR is for a function, we look
14455 for a pure-specifier; otherwise, we look for a
14456 constant-initializer. When we call `grokfield', it will
14457 perform more stringent semantics checks. */
14458 if (function_declarator_p (declarator))
14459 initializer = cp_parser_pure_specifier (parser);
14460 else
14461 /* Parse the initializer. */
14462 initializer = cp_parser_constant_initializer (parser);
14463 }
14464 /* Otherwise, there is no initializer. */
14465 else
14466 initializer = NULL_TREE;
14467
14468 /* See if we are probably looking at a function
14469 definition. We are certainly not looking at a
14470 member-declarator. Calling `grokfield' has
14471 side-effects, so we must not do it unless we are sure
14472 that we are looking at a member-declarator. */
14473 if (cp_parser_token_starts_function_definition_p
14474 (cp_lexer_peek_token (parser->lexer)))
14475 {
14476 /* The grammar does not allow a pure-specifier to be
14477 used when a member function is defined. (It is
14478 possible that this fact is an oversight in the
14479 standard, since a pure function may be defined
14480 outside of the class-specifier. */
14481 if (initializer)
14482 error ("pure-specifier on function-definition");
14483 decl = cp_parser_save_member_function_body (parser,
14484 &decl_specifiers,
14485 declarator,
14486 attributes);
14487 /* If the member was not a friend, declare it here. */
14488 if (!friend_p)
14489 finish_member_declaration (decl);
14490 /* Peek at the next token. */
14491 token = cp_lexer_peek_token (parser->lexer);
14492 /* If the next token is a semicolon, consume it. */
14493 if (token->type == CPP_SEMICOLON)
14494 cp_lexer_consume_token (parser->lexer);
14495 return;
14496 }
14497 else
14498 /* Create the declaration. */
14499 decl = grokfield (declarator, &decl_specifiers,
14500 initializer, /*init_const_expr_p=*/true,
14501 asm_specification,
14502 attributes);
14503 }
14504
14505 /* Reset PREFIX_ATTRIBUTES. */
14506 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14507 attributes = TREE_CHAIN (attributes);
14508 if (attributes)
14509 TREE_CHAIN (attributes) = NULL_TREE;
14510
14511 /* If there is any qualification still in effect, clear it
14512 now; we will be starting fresh with the next declarator. */
14513 parser->scope = NULL_TREE;
14514 parser->qualifying_scope = NULL_TREE;
14515 parser->object_scope = NULL_TREE;
14516 /* If it's a `,', then there are more declarators. */
14517 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14518 cp_lexer_consume_token (parser->lexer);
14519 /* If the next token isn't a `;', then we have a parse error. */
14520 else if (cp_lexer_next_token_is_not (parser->lexer,
14521 CPP_SEMICOLON))
14522 {
14523 cp_parser_error (parser, "expected %<;%>");
14524 /* Skip tokens until we find a `;'. */
14525 cp_parser_skip_to_end_of_statement (parser);
14526
14527 break;
14528 }
14529
14530 if (decl)
14531 {
14532 /* Add DECL to the list of members. */
14533 if (!friend_p)
14534 finish_member_declaration (decl);
14535
14536 if (TREE_CODE (decl) == FUNCTION_DECL)
14537 cp_parser_save_default_args (parser, decl);
14538 }
14539 }
14540 }
14541
14542 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14543 }
14544
14545 /* Parse a pure-specifier.
14546
14547 pure-specifier:
14548 = 0
14549
14550 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14551 Otherwise, ERROR_MARK_NODE is returned. */
14552
14553 static tree
14554 cp_parser_pure_specifier (cp_parser* parser)
14555 {
14556 cp_token *token;
14557
14558 /* Look for the `=' token. */
14559 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14560 return error_mark_node;
14561 /* Look for the `0' token. */
14562 token = cp_lexer_consume_token (parser->lexer);
14563 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14564 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14565 {
14566 cp_parser_error (parser,
14567 "invalid pure specifier (only `= 0' is allowed)");
14568 cp_parser_skip_to_end_of_statement (parser);
14569 return error_mark_node;
14570 }
14571 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14572 {
14573 error ("templates may not be %<virtual%>");
14574 return error_mark_node;
14575 }
14576
14577 return integer_zero_node;
14578 }
14579
14580 /* Parse a constant-initializer.
14581
14582 constant-initializer:
14583 = constant-expression
14584
14585 Returns a representation of the constant-expression. */
14586
14587 static tree
14588 cp_parser_constant_initializer (cp_parser* parser)
14589 {
14590 /* Look for the `=' token. */
14591 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14592 return error_mark_node;
14593
14594 /* It is invalid to write:
14595
14596 struct S { static const int i = { 7 }; };
14597
14598 */
14599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14600 {
14601 cp_parser_error (parser,
14602 "a brace-enclosed initializer is not allowed here");
14603 /* Consume the opening brace. */
14604 cp_lexer_consume_token (parser->lexer);
14605 /* Skip the initializer. */
14606 cp_parser_skip_to_closing_brace (parser);
14607 /* Look for the trailing `}'. */
14608 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14609
14610 return error_mark_node;
14611 }
14612
14613 return cp_parser_constant_expression (parser,
14614 /*allow_non_constant=*/false,
14615 NULL);
14616 }
14617
14618 /* Derived classes [gram.class.derived] */
14619
14620 /* Parse a base-clause.
14621
14622 base-clause:
14623 : base-specifier-list
14624
14625 base-specifier-list:
14626 base-specifier ... [opt]
14627 base-specifier-list , base-specifier ... [opt]
14628
14629 Returns a TREE_LIST representing the base-classes, in the order in
14630 which they were declared. The representation of each node is as
14631 described by cp_parser_base_specifier.
14632
14633 In the case that no bases are specified, this function will return
14634 NULL_TREE, not ERROR_MARK_NODE. */
14635
14636 static tree
14637 cp_parser_base_clause (cp_parser* parser)
14638 {
14639 tree bases = NULL_TREE;
14640
14641 /* Look for the `:' that begins the list. */
14642 cp_parser_require (parser, CPP_COLON, "`:'");
14643
14644 /* Scan the base-specifier-list. */
14645 while (true)
14646 {
14647 cp_token *token;
14648 tree base;
14649 bool pack_expansion_p = false;
14650
14651 /* Look for the base-specifier. */
14652 base = cp_parser_base_specifier (parser);
14653 /* Look for the (optional) ellipsis. */
14654 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14655 {
14656 /* Consume the `...'. */
14657 cp_lexer_consume_token (parser->lexer);
14658
14659 pack_expansion_p = true;
14660 }
14661
14662 /* Add BASE to the front of the list. */
14663 if (base != error_mark_node)
14664 {
14665 if (pack_expansion_p)
14666 /* Make this a pack expansion type. */
14667 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14668 else
14669 check_for_bare_parameter_packs (TREE_VALUE (base));
14670
14671 TREE_CHAIN (base) = bases;
14672 bases = base;
14673 }
14674 /* Peek at the next token. */
14675 token = cp_lexer_peek_token (parser->lexer);
14676 /* If it's not a comma, then the list is complete. */
14677 if (token->type != CPP_COMMA)
14678 break;
14679 /* Consume the `,'. */
14680 cp_lexer_consume_token (parser->lexer);
14681 }
14682
14683 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14684 base class had a qualified name. However, the next name that
14685 appears is certainly not qualified. */
14686 parser->scope = NULL_TREE;
14687 parser->qualifying_scope = NULL_TREE;
14688 parser->object_scope = NULL_TREE;
14689
14690 return nreverse (bases);
14691 }
14692
14693 /* Parse a base-specifier.
14694
14695 base-specifier:
14696 :: [opt] nested-name-specifier [opt] class-name
14697 virtual access-specifier [opt] :: [opt] nested-name-specifier
14698 [opt] class-name
14699 access-specifier virtual [opt] :: [opt] nested-name-specifier
14700 [opt] class-name
14701
14702 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14703 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14704 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14705 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14706
14707 static tree
14708 cp_parser_base_specifier (cp_parser* parser)
14709 {
14710 cp_token *token;
14711 bool done = false;
14712 bool virtual_p = false;
14713 bool duplicate_virtual_error_issued_p = false;
14714 bool duplicate_access_error_issued_p = false;
14715 bool class_scope_p, template_p;
14716 tree access = access_default_node;
14717 tree type;
14718
14719 /* Process the optional `virtual' and `access-specifier'. */
14720 while (!done)
14721 {
14722 /* Peek at the next token. */
14723 token = cp_lexer_peek_token (parser->lexer);
14724 /* Process `virtual'. */
14725 switch (token->keyword)
14726 {
14727 case RID_VIRTUAL:
14728 /* If `virtual' appears more than once, issue an error. */
14729 if (virtual_p && !duplicate_virtual_error_issued_p)
14730 {
14731 cp_parser_error (parser,
14732 "%<virtual%> specified more than once in base-specified");
14733 duplicate_virtual_error_issued_p = true;
14734 }
14735
14736 virtual_p = true;
14737
14738 /* Consume the `virtual' token. */
14739 cp_lexer_consume_token (parser->lexer);
14740
14741 break;
14742
14743 case RID_PUBLIC:
14744 case RID_PROTECTED:
14745 case RID_PRIVATE:
14746 /* If more than one access specifier appears, issue an
14747 error. */
14748 if (access != access_default_node
14749 && !duplicate_access_error_issued_p)
14750 {
14751 cp_parser_error (parser,
14752 "more than one access specifier in base-specified");
14753 duplicate_access_error_issued_p = true;
14754 }
14755
14756 access = ridpointers[(int) token->keyword];
14757
14758 /* Consume the access-specifier. */
14759 cp_lexer_consume_token (parser->lexer);
14760
14761 break;
14762
14763 default:
14764 done = true;
14765 break;
14766 }
14767 }
14768 /* It is not uncommon to see programs mechanically, erroneously, use
14769 the 'typename' keyword to denote (dependent) qualified types
14770 as base classes. */
14771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14772 {
14773 if (!processing_template_decl)
14774 error ("keyword %<typename%> not allowed outside of templates");
14775 else
14776 error ("keyword %<typename%> not allowed in this context "
14777 "(the base class is implicitly a type)");
14778 cp_lexer_consume_token (parser->lexer);
14779 }
14780
14781 /* Look for the optional `::' operator. */
14782 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14783 /* Look for the nested-name-specifier. The simplest way to
14784 implement:
14785
14786 [temp.res]
14787
14788 The keyword `typename' is not permitted in a base-specifier or
14789 mem-initializer; in these contexts a qualified name that
14790 depends on a template-parameter is implicitly assumed to be a
14791 type name.
14792
14793 is to pretend that we have seen the `typename' keyword at this
14794 point. */
14795 cp_parser_nested_name_specifier_opt (parser,
14796 /*typename_keyword_p=*/true,
14797 /*check_dependency_p=*/true,
14798 typename_type,
14799 /*is_declaration=*/true);
14800 /* If the base class is given by a qualified name, assume that names
14801 we see are type names or templates, as appropriate. */
14802 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14803 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14804
14805 /* Finally, look for the class-name. */
14806 type = cp_parser_class_name (parser,
14807 class_scope_p,
14808 template_p,
14809 typename_type,
14810 /*check_dependency_p=*/true,
14811 /*class_head_p=*/false,
14812 /*is_declaration=*/true);
14813
14814 if (type == error_mark_node)
14815 return error_mark_node;
14816
14817 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14818 }
14819
14820 /* Exception handling [gram.exception] */
14821
14822 /* Parse an (optional) exception-specification.
14823
14824 exception-specification:
14825 throw ( type-id-list [opt] )
14826
14827 Returns a TREE_LIST representing the exception-specification. The
14828 TREE_VALUE of each node is a type. */
14829
14830 static tree
14831 cp_parser_exception_specification_opt (cp_parser* parser)
14832 {
14833 cp_token *token;
14834 tree type_id_list;
14835
14836 /* Peek at the next token. */
14837 token = cp_lexer_peek_token (parser->lexer);
14838 /* If it's not `throw', then there's no exception-specification. */
14839 if (!cp_parser_is_keyword (token, RID_THROW))
14840 return NULL_TREE;
14841
14842 /* Consume the `throw'. */
14843 cp_lexer_consume_token (parser->lexer);
14844
14845 /* Look for the `('. */
14846 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14847
14848 /* Peek at the next token. */
14849 token = cp_lexer_peek_token (parser->lexer);
14850 /* If it's not a `)', then there is a type-id-list. */
14851 if (token->type != CPP_CLOSE_PAREN)
14852 {
14853 const char *saved_message;
14854
14855 /* Types may not be defined in an exception-specification. */
14856 saved_message = parser->type_definition_forbidden_message;
14857 parser->type_definition_forbidden_message
14858 = "types may not be defined in an exception-specification";
14859 /* Parse the type-id-list. */
14860 type_id_list = cp_parser_type_id_list (parser);
14861 /* Restore the saved message. */
14862 parser->type_definition_forbidden_message = saved_message;
14863 }
14864 else
14865 type_id_list = empty_except_spec;
14866
14867 /* Look for the `)'. */
14868 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14869
14870 return type_id_list;
14871 }
14872
14873 /* Parse an (optional) type-id-list.
14874
14875 type-id-list:
14876 type-id ... [opt]
14877 type-id-list , type-id ... [opt]
14878
14879 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14880 in the order that the types were presented. */
14881
14882 static tree
14883 cp_parser_type_id_list (cp_parser* parser)
14884 {
14885 tree types = NULL_TREE;
14886
14887 while (true)
14888 {
14889 cp_token *token;
14890 tree type;
14891
14892 /* Get the next type-id. */
14893 type = cp_parser_type_id (parser);
14894 /* Parse the optional ellipsis. */
14895 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14896 {
14897 /* Consume the `...'. */
14898 cp_lexer_consume_token (parser->lexer);
14899
14900 /* Turn the type into a pack expansion expression. */
14901 type = make_pack_expansion (type);
14902 }
14903 /* Add it to the list. */
14904 types = add_exception_specifier (types, type, /*complain=*/1);
14905 /* Peek at the next token. */
14906 token = cp_lexer_peek_token (parser->lexer);
14907 /* If it is not a `,', we are done. */
14908 if (token->type != CPP_COMMA)
14909 break;
14910 /* Consume the `,'. */
14911 cp_lexer_consume_token (parser->lexer);
14912 }
14913
14914 return nreverse (types);
14915 }
14916
14917 /* Parse a try-block.
14918
14919 try-block:
14920 try compound-statement handler-seq */
14921
14922 static tree
14923 cp_parser_try_block (cp_parser* parser)
14924 {
14925 tree try_block;
14926
14927 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14928 try_block = begin_try_block ();
14929 cp_parser_compound_statement (parser, NULL, true);
14930 finish_try_block (try_block);
14931 cp_parser_handler_seq (parser);
14932 finish_handler_sequence (try_block);
14933
14934 return try_block;
14935 }
14936
14937 /* Parse a function-try-block.
14938
14939 function-try-block:
14940 try ctor-initializer [opt] function-body handler-seq */
14941
14942 static bool
14943 cp_parser_function_try_block (cp_parser* parser)
14944 {
14945 tree compound_stmt;
14946 tree try_block;
14947 bool ctor_initializer_p;
14948
14949 /* Look for the `try' keyword. */
14950 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14951 return false;
14952 /* Let the rest of the front end know where we are. */
14953 try_block = begin_function_try_block (&compound_stmt);
14954 /* Parse the function-body. */
14955 ctor_initializer_p
14956 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14957 /* We're done with the `try' part. */
14958 finish_function_try_block (try_block);
14959 /* Parse the handlers. */
14960 cp_parser_handler_seq (parser);
14961 /* We're done with the handlers. */
14962 finish_function_handler_sequence (try_block, compound_stmt);
14963
14964 return ctor_initializer_p;
14965 }
14966
14967 /* Parse a handler-seq.
14968
14969 handler-seq:
14970 handler handler-seq [opt] */
14971
14972 static void
14973 cp_parser_handler_seq (cp_parser* parser)
14974 {
14975 while (true)
14976 {
14977 cp_token *token;
14978
14979 /* Parse the handler. */
14980 cp_parser_handler (parser);
14981 /* Peek at the next token. */
14982 token = cp_lexer_peek_token (parser->lexer);
14983 /* If it's not `catch' then there are no more handlers. */
14984 if (!cp_parser_is_keyword (token, RID_CATCH))
14985 break;
14986 }
14987 }
14988
14989 /* Parse a handler.
14990
14991 handler:
14992 catch ( exception-declaration ) compound-statement */
14993
14994 static void
14995 cp_parser_handler (cp_parser* parser)
14996 {
14997 tree handler;
14998 tree declaration;
14999
15000 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15001 handler = begin_handler ();
15002 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15003 declaration = cp_parser_exception_declaration (parser);
15004 finish_handler_parms (declaration, handler);
15005 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15006 cp_parser_compound_statement (parser, NULL, false);
15007 finish_handler (handler);
15008 }
15009
15010 /* Parse an exception-declaration.
15011
15012 exception-declaration:
15013 type-specifier-seq declarator
15014 type-specifier-seq abstract-declarator
15015 type-specifier-seq
15016 ...
15017
15018 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15019 ellipsis variant is used. */
15020
15021 static tree
15022 cp_parser_exception_declaration (cp_parser* parser)
15023 {
15024 cp_decl_specifier_seq type_specifiers;
15025 cp_declarator *declarator;
15026 const char *saved_message;
15027
15028 /* If it's an ellipsis, it's easy to handle. */
15029 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15030 {
15031 /* Consume the `...' token. */
15032 cp_lexer_consume_token (parser->lexer);
15033 return NULL_TREE;
15034 }
15035
15036 /* Types may not be defined in exception-declarations. */
15037 saved_message = parser->type_definition_forbidden_message;
15038 parser->type_definition_forbidden_message
15039 = "types may not be defined in exception-declarations";
15040
15041 /* Parse the type-specifier-seq. */
15042 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15043 &type_specifiers);
15044 /* If it's a `)', then there is no declarator. */
15045 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15046 declarator = NULL;
15047 else
15048 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15049 /*ctor_dtor_or_conv_p=*/NULL,
15050 /*parenthesized_p=*/NULL,
15051 /*member_p=*/false);
15052
15053 /* Restore the saved message. */
15054 parser->type_definition_forbidden_message = saved_message;
15055
15056 if (!type_specifiers.any_specifiers_p)
15057 return error_mark_node;
15058
15059 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15060 }
15061
15062 /* Parse a throw-expression.
15063
15064 throw-expression:
15065 throw assignment-expression [opt]
15066
15067 Returns a THROW_EXPR representing the throw-expression. */
15068
15069 static tree
15070 cp_parser_throw_expression (cp_parser* parser)
15071 {
15072 tree expression;
15073 cp_token* token;
15074
15075 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15076 token = cp_lexer_peek_token (parser->lexer);
15077 /* Figure out whether or not there is an assignment-expression
15078 following the "throw" keyword. */
15079 if (token->type == CPP_COMMA
15080 || token->type == CPP_SEMICOLON
15081 || token->type == CPP_CLOSE_PAREN
15082 || token->type == CPP_CLOSE_SQUARE
15083 || token->type == CPP_CLOSE_BRACE
15084 || token->type == CPP_COLON)
15085 expression = NULL_TREE;
15086 else
15087 expression = cp_parser_assignment_expression (parser,
15088 /*cast_p=*/false);
15089
15090 return build_throw (expression);
15091 }
15092
15093 /* GNU Extensions */
15094
15095 /* Parse an (optional) asm-specification.
15096
15097 asm-specification:
15098 asm ( string-literal )
15099
15100 If the asm-specification is present, returns a STRING_CST
15101 corresponding to the string-literal. Otherwise, returns
15102 NULL_TREE. */
15103
15104 static tree
15105 cp_parser_asm_specification_opt (cp_parser* parser)
15106 {
15107 cp_token *token;
15108 tree asm_specification;
15109
15110 /* Peek at the next token. */
15111 token = cp_lexer_peek_token (parser->lexer);
15112 /* If the next token isn't the `asm' keyword, then there's no
15113 asm-specification. */
15114 if (!cp_parser_is_keyword (token, RID_ASM))
15115 return NULL_TREE;
15116
15117 /* Consume the `asm' token. */
15118 cp_lexer_consume_token (parser->lexer);
15119 /* Look for the `('. */
15120 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15121
15122 /* Look for the string-literal. */
15123 asm_specification = cp_parser_string_literal (parser, false, false);
15124
15125 /* Look for the `)'. */
15126 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15127
15128 return asm_specification;
15129 }
15130
15131 /* Parse an asm-operand-list.
15132
15133 asm-operand-list:
15134 asm-operand
15135 asm-operand-list , asm-operand
15136
15137 asm-operand:
15138 string-literal ( expression )
15139 [ string-literal ] string-literal ( expression )
15140
15141 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15142 each node is the expression. The TREE_PURPOSE is itself a
15143 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15144 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15145 is a STRING_CST for the string literal before the parenthesis. */
15146
15147 static tree
15148 cp_parser_asm_operand_list (cp_parser* parser)
15149 {
15150 tree asm_operands = NULL_TREE;
15151
15152 while (true)
15153 {
15154 tree string_literal;
15155 tree expression;
15156 tree name;
15157
15158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15159 {
15160 /* Consume the `[' token. */
15161 cp_lexer_consume_token (parser->lexer);
15162 /* Read the operand name. */
15163 name = cp_parser_identifier (parser);
15164 if (name != error_mark_node)
15165 name = build_string (IDENTIFIER_LENGTH (name),
15166 IDENTIFIER_POINTER (name));
15167 /* Look for the closing `]'. */
15168 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15169 }
15170 else
15171 name = NULL_TREE;
15172 /* Look for the string-literal. */
15173 string_literal = cp_parser_string_literal (parser, false, false);
15174
15175 /* Look for the `('. */
15176 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15177 /* Parse the expression. */
15178 expression = cp_parser_expression (parser, /*cast_p=*/false);
15179 /* Look for the `)'. */
15180 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15181
15182 /* Add this operand to the list. */
15183 asm_operands = tree_cons (build_tree_list (name, string_literal),
15184 expression,
15185 asm_operands);
15186 /* If the next token is not a `,', there are no more
15187 operands. */
15188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15189 break;
15190 /* Consume the `,'. */
15191 cp_lexer_consume_token (parser->lexer);
15192 }
15193
15194 return nreverse (asm_operands);
15195 }
15196
15197 /* Parse an asm-clobber-list.
15198
15199 asm-clobber-list:
15200 string-literal
15201 asm-clobber-list , string-literal
15202
15203 Returns a TREE_LIST, indicating the clobbers in the order that they
15204 appeared. The TREE_VALUE of each node is a STRING_CST. */
15205
15206 static tree
15207 cp_parser_asm_clobber_list (cp_parser* parser)
15208 {
15209 tree clobbers = NULL_TREE;
15210
15211 while (true)
15212 {
15213 tree string_literal;
15214
15215 /* Look for the string literal. */
15216 string_literal = cp_parser_string_literal (parser, false, false);
15217 /* Add it to the list. */
15218 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15219 /* If the next token is not a `,', then the list is
15220 complete. */
15221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15222 break;
15223 /* Consume the `,' token. */
15224 cp_lexer_consume_token (parser->lexer);
15225 }
15226
15227 return clobbers;
15228 }
15229
15230 /* Parse an (optional) series of attributes.
15231
15232 attributes:
15233 attributes attribute
15234
15235 attribute:
15236 __attribute__ (( attribute-list [opt] ))
15237
15238 The return value is as for cp_parser_attribute_list. */
15239
15240 static tree
15241 cp_parser_attributes_opt (cp_parser* parser)
15242 {
15243 tree attributes = NULL_TREE;
15244
15245 while (true)
15246 {
15247 cp_token *token;
15248 tree attribute_list;
15249
15250 /* Peek at the next token. */
15251 token = cp_lexer_peek_token (parser->lexer);
15252 /* If it's not `__attribute__', then we're done. */
15253 if (token->keyword != RID_ATTRIBUTE)
15254 break;
15255
15256 /* Consume the `__attribute__' keyword. */
15257 cp_lexer_consume_token (parser->lexer);
15258 /* Look for the two `(' tokens. */
15259 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15260 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15261
15262 /* Peek at the next token. */
15263 token = cp_lexer_peek_token (parser->lexer);
15264 if (token->type != CPP_CLOSE_PAREN)
15265 /* Parse the attribute-list. */
15266 attribute_list = cp_parser_attribute_list (parser);
15267 else
15268 /* If the next token is a `)', then there is no attribute
15269 list. */
15270 attribute_list = NULL;
15271
15272 /* Look for the two `)' tokens. */
15273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15274 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15275
15276 /* Add these new attributes to the list. */
15277 attributes = chainon (attributes, attribute_list);
15278 }
15279
15280 return attributes;
15281 }
15282
15283 /* Parse an attribute-list.
15284
15285 attribute-list:
15286 attribute
15287 attribute-list , attribute
15288
15289 attribute:
15290 identifier
15291 identifier ( identifier )
15292 identifier ( identifier , expression-list )
15293 identifier ( expression-list )
15294
15295 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15296 to an attribute. The TREE_PURPOSE of each node is the identifier
15297 indicating which attribute is in use. The TREE_VALUE represents
15298 the arguments, if any. */
15299
15300 static tree
15301 cp_parser_attribute_list (cp_parser* parser)
15302 {
15303 tree attribute_list = NULL_TREE;
15304 bool save_translate_strings_p = parser->translate_strings_p;
15305
15306 parser->translate_strings_p = false;
15307 while (true)
15308 {
15309 cp_token *token;
15310 tree identifier;
15311 tree attribute;
15312
15313 /* Look for the identifier. We also allow keywords here; for
15314 example `__attribute__ ((const))' is legal. */
15315 token = cp_lexer_peek_token (parser->lexer);
15316 if (token->type == CPP_NAME
15317 || token->type == CPP_KEYWORD)
15318 {
15319 tree arguments = NULL_TREE;
15320
15321 /* Consume the token. */
15322 token = cp_lexer_consume_token (parser->lexer);
15323
15324 /* Save away the identifier that indicates which attribute
15325 this is. */
15326 identifier = token->u.value;
15327 attribute = build_tree_list (identifier, NULL_TREE);
15328
15329 /* Peek at the next token. */
15330 token = cp_lexer_peek_token (parser->lexer);
15331 /* If it's an `(', then parse the attribute arguments. */
15332 if (token->type == CPP_OPEN_PAREN)
15333 {
15334 arguments = cp_parser_parenthesized_expression_list
15335 (parser, true, /*cast_p=*/false,
15336 /*allow_expansion_p=*/false,
15337 /*non_constant_p=*/NULL);
15338 /* Save the arguments away. */
15339 TREE_VALUE (attribute) = arguments;
15340 }
15341
15342 if (arguments != error_mark_node)
15343 {
15344 /* Add this attribute to the list. */
15345 TREE_CHAIN (attribute) = attribute_list;
15346 attribute_list = attribute;
15347 }
15348
15349 token = cp_lexer_peek_token (parser->lexer);
15350 }
15351 /* Now, look for more attributes. If the next token isn't a
15352 `,', we're done. */
15353 if (token->type != CPP_COMMA)
15354 break;
15355
15356 /* Consume the comma and keep going. */
15357 cp_lexer_consume_token (parser->lexer);
15358 }
15359 parser->translate_strings_p = save_translate_strings_p;
15360
15361 /* We built up the list in reverse order. */
15362 return nreverse (attribute_list);
15363 }
15364
15365 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15366 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15367 current value of the PEDANTIC flag, regardless of whether or not
15368 the `__extension__' keyword is present. The caller is responsible
15369 for restoring the value of the PEDANTIC flag. */
15370
15371 static bool
15372 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15373 {
15374 /* Save the old value of the PEDANTIC flag. */
15375 *saved_pedantic = pedantic;
15376
15377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15378 {
15379 /* Consume the `__extension__' token. */
15380 cp_lexer_consume_token (parser->lexer);
15381 /* We're not being pedantic while the `__extension__' keyword is
15382 in effect. */
15383 pedantic = 0;
15384
15385 return true;
15386 }
15387
15388 return false;
15389 }
15390
15391 /* Parse a label declaration.
15392
15393 label-declaration:
15394 __label__ label-declarator-seq ;
15395
15396 label-declarator-seq:
15397 identifier , label-declarator-seq
15398 identifier */
15399
15400 static void
15401 cp_parser_label_declaration (cp_parser* parser)
15402 {
15403 /* Look for the `__label__' keyword. */
15404 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15405
15406 while (true)
15407 {
15408 tree identifier;
15409
15410 /* Look for an identifier. */
15411 identifier = cp_parser_identifier (parser);
15412 /* If we failed, stop. */
15413 if (identifier == error_mark_node)
15414 break;
15415 /* Declare it as a label. */
15416 finish_label_decl (identifier);
15417 /* If the next token is a `;', stop. */
15418 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15419 break;
15420 /* Look for the `,' separating the label declarations. */
15421 cp_parser_require (parser, CPP_COMMA, "`,'");
15422 }
15423
15424 /* Look for the final `;'. */
15425 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15426 }
15427
15428 /* Support Functions */
15429
15430 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15431 NAME should have one of the representations used for an
15432 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15433 is returned. If PARSER->SCOPE is a dependent type, then a
15434 SCOPE_REF is returned.
15435
15436 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15437 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15438 was formed. Abstractly, such entities should not be passed to this
15439 function, because they do not need to be looked up, but it is
15440 simpler to check for this special case here, rather than at the
15441 call-sites.
15442
15443 In cases not explicitly covered above, this function returns a
15444 DECL, OVERLOAD, or baselink representing the result of the lookup.
15445 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15446 is returned.
15447
15448 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15449 (e.g., "struct") that was used. In that case bindings that do not
15450 refer to types are ignored.
15451
15452 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15453 ignored.
15454
15455 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15456 are ignored.
15457
15458 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15459 types.
15460
15461 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15462 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15463 NULL_TREE otherwise. */
15464
15465 static tree
15466 cp_parser_lookup_name (cp_parser *parser, tree name,
15467 enum tag_types tag_type,
15468 bool is_template,
15469 bool is_namespace,
15470 bool check_dependency,
15471 tree *ambiguous_decls)
15472 {
15473 int flags = 0;
15474 tree decl;
15475 tree object_type = parser->context->object_type;
15476
15477 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15478 flags |= LOOKUP_COMPLAIN;
15479
15480 /* Assume that the lookup will be unambiguous. */
15481 if (ambiguous_decls)
15482 *ambiguous_decls = NULL_TREE;
15483
15484 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15485 no longer valid. Note that if we are parsing tentatively, and
15486 the parse fails, OBJECT_TYPE will be automatically restored. */
15487 parser->context->object_type = NULL_TREE;
15488
15489 if (name == error_mark_node)
15490 return error_mark_node;
15491
15492 /* A template-id has already been resolved; there is no lookup to
15493 do. */
15494 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15495 return name;
15496 if (BASELINK_P (name))
15497 {
15498 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15499 == TEMPLATE_ID_EXPR);
15500 return name;
15501 }
15502
15503 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15504 it should already have been checked to make sure that the name
15505 used matches the type being destroyed. */
15506 if (TREE_CODE (name) == BIT_NOT_EXPR)
15507 {
15508 tree type;
15509
15510 /* Figure out to which type this destructor applies. */
15511 if (parser->scope)
15512 type = parser->scope;
15513 else if (object_type)
15514 type = object_type;
15515 else
15516 type = current_class_type;
15517 /* If that's not a class type, there is no destructor. */
15518 if (!type || !CLASS_TYPE_P (type))
15519 return error_mark_node;
15520 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15521 lazily_declare_fn (sfk_destructor, type);
15522 if (!CLASSTYPE_DESTRUCTORS (type))
15523 return error_mark_node;
15524 /* If it was a class type, return the destructor. */
15525 return CLASSTYPE_DESTRUCTORS (type);
15526 }
15527
15528 /* By this point, the NAME should be an ordinary identifier. If
15529 the id-expression was a qualified name, the qualifying scope is
15530 stored in PARSER->SCOPE at this point. */
15531 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15532
15533 /* Perform the lookup. */
15534 if (parser->scope)
15535 {
15536 bool dependent_p;
15537
15538 if (parser->scope == error_mark_node)
15539 return error_mark_node;
15540
15541 /* If the SCOPE is dependent, the lookup must be deferred until
15542 the template is instantiated -- unless we are explicitly
15543 looking up names in uninstantiated templates. Even then, we
15544 cannot look up the name if the scope is not a class type; it
15545 might, for example, be a template type parameter. */
15546 dependent_p = (TYPE_P (parser->scope)
15547 && !(parser->in_declarator_p
15548 && currently_open_class (parser->scope))
15549 && dependent_type_p (parser->scope));
15550 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15551 && dependent_p)
15552 {
15553 if (tag_type)
15554 {
15555 tree type;
15556
15557 /* The resolution to Core Issue 180 says that `struct
15558 A::B' should be considered a type-name, even if `A'
15559 is dependent. */
15560 type = make_typename_type (parser->scope, name, tag_type,
15561 /*complain=*/tf_error);
15562 decl = TYPE_NAME (type);
15563 }
15564 else if (is_template
15565 && (cp_parser_next_token_ends_template_argument_p (parser)
15566 || cp_lexer_next_token_is (parser->lexer,
15567 CPP_CLOSE_PAREN)))
15568 decl = make_unbound_class_template (parser->scope,
15569 name, NULL_TREE,
15570 /*complain=*/tf_error);
15571 else
15572 decl = build_qualified_name (/*type=*/NULL_TREE,
15573 parser->scope, name,
15574 is_template);
15575 }
15576 else
15577 {
15578 tree pushed_scope = NULL_TREE;
15579
15580 /* If PARSER->SCOPE is a dependent type, then it must be a
15581 class type, and we must not be checking dependencies;
15582 otherwise, we would have processed this lookup above. So
15583 that PARSER->SCOPE is not considered a dependent base by
15584 lookup_member, we must enter the scope here. */
15585 if (dependent_p)
15586 pushed_scope = push_scope (parser->scope);
15587 /* If the PARSER->SCOPE is a template specialization, it
15588 may be instantiated during name lookup. In that case,
15589 errors may be issued. Even if we rollback the current
15590 tentative parse, those errors are valid. */
15591 decl = lookup_qualified_name (parser->scope, name,
15592 tag_type != none_type,
15593 /*complain=*/true);
15594 if (pushed_scope)
15595 pop_scope (pushed_scope);
15596 }
15597 parser->qualifying_scope = parser->scope;
15598 parser->object_scope = NULL_TREE;
15599 }
15600 else if (object_type)
15601 {
15602 tree object_decl = NULL_TREE;
15603 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15604 OBJECT_TYPE is not a class. */
15605 if (CLASS_TYPE_P (object_type))
15606 /* If the OBJECT_TYPE is a template specialization, it may
15607 be instantiated during name lookup. In that case, errors
15608 may be issued. Even if we rollback the current tentative
15609 parse, those errors are valid. */
15610 object_decl = lookup_member (object_type,
15611 name,
15612 /*protect=*/0,
15613 tag_type != none_type);
15614 /* Look it up in the enclosing context, too. */
15615 decl = lookup_name_real (name, tag_type != none_type,
15616 /*nonclass=*/0,
15617 /*block_p=*/true, is_namespace, flags);
15618 parser->object_scope = object_type;
15619 parser->qualifying_scope = NULL_TREE;
15620 if (object_decl)
15621 decl = object_decl;
15622 }
15623 else
15624 {
15625 decl = lookup_name_real (name, tag_type != none_type,
15626 /*nonclass=*/0,
15627 /*block_p=*/true, is_namespace, flags);
15628 parser->qualifying_scope = NULL_TREE;
15629 parser->object_scope = NULL_TREE;
15630 }
15631
15632 /* If the lookup failed, let our caller know. */
15633 if (!decl || decl == error_mark_node)
15634 return error_mark_node;
15635
15636 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15637 if (TREE_CODE (decl) == TREE_LIST)
15638 {
15639 if (ambiguous_decls)
15640 *ambiguous_decls = decl;
15641 /* The error message we have to print is too complicated for
15642 cp_parser_error, so we incorporate its actions directly. */
15643 if (!cp_parser_simulate_error (parser))
15644 {
15645 error ("reference to %qD is ambiguous", name);
15646 print_candidates (decl);
15647 }
15648 return error_mark_node;
15649 }
15650
15651 gcc_assert (DECL_P (decl)
15652 || TREE_CODE (decl) == OVERLOAD
15653 || TREE_CODE (decl) == SCOPE_REF
15654 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15655 || BASELINK_P (decl));
15656
15657 /* If we have resolved the name of a member declaration, check to
15658 see if the declaration is accessible. When the name resolves to
15659 set of overloaded functions, accessibility is checked when
15660 overload resolution is done.
15661
15662 During an explicit instantiation, access is not checked at all,
15663 as per [temp.explicit]. */
15664 if (DECL_P (decl))
15665 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15666
15667 return decl;
15668 }
15669
15670 /* Like cp_parser_lookup_name, but for use in the typical case where
15671 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15672 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15673
15674 static tree
15675 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15676 {
15677 return cp_parser_lookup_name (parser, name,
15678 none_type,
15679 /*is_template=*/false,
15680 /*is_namespace=*/false,
15681 /*check_dependency=*/true,
15682 /*ambiguous_decls=*/NULL);
15683 }
15684
15685 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15686 the current context, return the TYPE_DECL. If TAG_NAME_P is
15687 true, the DECL indicates the class being defined in a class-head,
15688 or declared in an elaborated-type-specifier.
15689
15690 Otherwise, return DECL. */
15691
15692 static tree
15693 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15694 {
15695 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15696 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15697
15698 struct A {
15699 template <typename T> struct B;
15700 };
15701
15702 template <typename T> struct A::B {};
15703
15704 Similarly, in an elaborated-type-specifier:
15705
15706 namespace N { struct X{}; }
15707
15708 struct A {
15709 template <typename T> friend struct N::X;
15710 };
15711
15712 However, if the DECL refers to a class type, and we are in
15713 the scope of the class, then the name lookup automatically
15714 finds the TYPE_DECL created by build_self_reference rather
15715 than a TEMPLATE_DECL. For example, in:
15716
15717 template <class T> struct S {
15718 S s;
15719 };
15720
15721 there is no need to handle such case. */
15722
15723 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15724 return DECL_TEMPLATE_RESULT (decl);
15725
15726 return decl;
15727 }
15728
15729 /* If too many, or too few, template-parameter lists apply to the
15730 declarator, issue an error message. Returns TRUE if all went well,
15731 and FALSE otherwise. */
15732
15733 static bool
15734 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15735 cp_declarator *declarator)
15736 {
15737 unsigned num_templates;
15738
15739 /* We haven't seen any classes that involve template parameters yet. */
15740 num_templates = 0;
15741
15742 switch (declarator->kind)
15743 {
15744 case cdk_id:
15745 if (declarator->u.id.qualifying_scope)
15746 {
15747 tree scope;
15748 tree member;
15749
15750 scope = declarator->u.id.qualifying_scope;
15751 member = declarator->u.id.unqualified_name;
15752
15753 while (scope && CLASS_TYPE_P (scope))
15754 {
15755 /* You're supposed to have one `template <...>'
15756 for every template class, but you don't need one
15757 for a full specialization. For example:
15758
15759 template <class T> struct S{};
15760 template <> struct S<int> { void f(); };
15761 void S<int>::f () {}
15762
15763 is correct; there shouldn't be a `template <>' for
15764 the definition of `S<int>::f'. */
15765 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15766 /* If SCOPE does not have template information of any
15767 kind, then it is not a template, nor is it nested
15768 within a template. */
15769 break;
15770 if (explicit_class_specialization_p (scope))
15771 break;
15772 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15773 ++num_templates;
15774
15775 scope = TYPE_CONTEXT (scope);
15776 }
15777 }
15778 else if (TREE_CODE (declarator->u.id.unqualified_name)
15779 == TEMPLATE_ID_EXPR)
15780 /* If the DECLARATOR has the form `X<y>' then it uses one
15781 additional level of template parameters. */
15782 ++num_templates;
15783
15784 return cp_parser_check_template_parameters (parser,
15785 num_templates);
15786
15787 case cdk_function:
15788 case cdk_array:
15789 case cdk_pointer:
15790 case cdk_reference:
15791 case cdk_ptrmem:
15792 return (cp_parser_check_declarator_template_parameters
15793 (parser, declarator->declarator));
15794
15795 case cdk_error:
15796 return true;
15797
15798 default:
15799 gcc_unreachable ();
15800 }
15801 return false;
15802 }
15803
15804 /* NUM_TEMPLATES were used in the current declaration. If that is
15805 invalid, return FALSE and issue an error messages. Otherwise,
15806 return TRUE. */
15807
15808 static bool
15809 cp_parser_check_template_parameters (cp_parser* parser,
15810 unsigned num_templates)
15811 {
15812 /* If there are more template classes than parameter lists, we have
15813 something like:
15814
15815 template <class T> void S<T>::R<T>::f (); */
15816 if (parser->num_template_parameter_lists < num_templates)
15817 {
15818 error ("too few template-parameter-lists");
15819 return false;
15820 }
15821 /* If there are the same number of template classes and parameter
15822 lists, that's OK. */
15823 if (parser->num_template_parameter_lists == num_templates)
15824 return true;
15825 /* If there are more, but only one more, then we are referring to a
15826 member template. That's OK too. */
15827 if (parser->num_template_parameter_lists == num_templates + 1)
15828 return true;
15829 /* Otherwise, there are too many template parameter lists. We have
15830 something like:
15831
15832 template <class T> template <class U> void S::f(); */
15833 error ("too many template-parameter-lists");
15834 return false;
15835 }
15836
15837 /* Parse an optional `::' token indicating that the following name is
15838 from the global namespace. If so, PARSER->SCOPE is set to the
15839 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15840 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15841 Returns the new value of PARSER->SCOPE, if the `::' token is
15842 present, and NULL_TREE otherwise. */
15843
15844 static tree
15845 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15846 {
15847 cp_token *token;
15848
15849 /* Peek at the next token. */
15850 token = cp_lexer_peek_token (parser->lexer);
15851 /* If we're looking at a `::' token then we're starting from the
15852 global namespace, not our current location. */
15853 if (token->type == CPP_SCOPE)
15854 {
15855 /* Consume the `::' token. */
15856 cp_lexer_consume_token (parser->lexer);
15857 /* Set the SCOPE so that we know where to start the lookup. */
15858 parser->scope = global_namespace;
15859 parser->qualifying_scope = global_namespace;
15860 parser->object_scope = NULL_TREE;
15861
15862 return parser->scope;
15863 }
15864 else if (!current_scope_valid_p)
15865 {
15866 parser->scope = NULL_TREE;
15867 parser->qualifying_scope = NULL_TREE;
15868 parser->object_scope = NULL_TREE;
15869 }
15870
15871 return NULL_TREE;
15872 }
15873
15874 /* Returns TRUE if the upcoming token sequence is the start of a
15875 constructor declarator. If FRIEND_P is true, the declarator is
15876 preceded by the `friend' specifier. */
15877
15878 static bool
15879 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15880 {
15881 bool constructor_p;
15882 tree type_decl = NULL_TREE;
15883 bool nested_name_p;
15884 cp_token *next_token;
15885
15886 /* The common case is that this is not a constructor declarator, so
15887 try to avoid doing lots of work if at all possible. It's not
15888 valid declare a constructor at function scope. */
15889 if (parser->in_function_body)
15890 return false;
15891 /* And only certain tokens can begin a constructor declarator. */
15892 next_token = cp_lexer_peek_token (parser->lexer);
15893 if (next_token->type != CPP_NAME
15894 && next_token->type != CPP_SCOPE
15895 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15896 && next_token->type != CPP_TEMPLATE_ID)
15897 return false;
15898
15899 /* Parse tentatively; we are going to roll back all of the tokens
15900 consumed here. */
15901 cp_parser_parse_tentatively (parser);
15902 /* Assume that we are looking at a constructor declarator. */
15903 constructor_p = true;
15904
15905 /* Look for the optional `::' operator. */
15906 cp_parser_global_scope_opt (parser,
15907 /*current_scope_valid_p=*/false);
15908 /* Look for the nested-name-specifier. */
15909 nested_name_p
15910 = (cp_parser_nested_name_specifier_opt (parser,
15911 /*typename_keyword_p=*/false,
15912 /*check_dependency_p=*/false,
15913 /*type_p=*/false,
15914 /*is_declaration=*/false)
15915 != NULL_TREE);
15916 /* Outside of a class-specifier, there must be a
15917 nested-name-specifier. */
15918 if (!nested_name_p &&
15919 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15920 || friend_p))
15921 constructor_p = false;
15922 /* If we still think that this might be a constructor-declarator,
15923 look for a class-name. */
15924 if (constructor_p)
15925 {
15926 /* If we have:
15927
15928 template <typename T> struct S { S(); };
15929 template <typename T> S<T>::S ();
15930
15931 we must recognize that the nested `S' names a class.
15932 Similarly, for:
15933
15934 template <typename T> S<T>::S<T> ();
15935
15936 we must recognize that the nested `S' names a template. */
15937 type_decl = cp_parser_class_name (parser,
15938 /*typename_keyword_p=*/false,
15939 /*template_keyword_p=*/false,
15940 none_type,
15941 /*check_dependency_p=*/false,
15942 /*class_head_p=*/false,
15943 /*is_declaration=*/false);
15944 /* If there was no class-name, then this is not a constructor. */
15945 constructor_p = !cp_parser_error_occurred (parser);
15946 }
15947
15948 /* If we're still considering a constructor, we have to see a `(',
15949 to begin the parameter-declaration-clause, followed by either a
15950 `)', an `...', or a decl-specifier. We need to check for a
15951 type-specifier to avoid being fooled into thinking that:
15952
15953 S::S (f) (int);
15954
15955 is a constructor. (It is actually a function named `f' that
15956 takes one parameter (of type `int') and returns a value of type
15957 `S::S'. */
15958 if (constructor_p
15959 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15960 {
15961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15962 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15963 /* A parameter declaration begins with a decl-specifier,
15964 which is either the "attribute" keyword, a storage class
15965 specifier, or (usually) a type-specifier. */
15966 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15967 {
15968 tree type;
15969 tree pushed_scope = NULL_TREE;
15970 unsigned saved_num_template_parameter_lists;
15971
15972 /* Names appearing in the type-specifier should be looked up
15973 in the scope of the class. */
15974 if (current_class_type)
15975 type = NULL_TREE;
15976 else
15977 {
15978 type = TREE_TYPE (type_decl);
15979 if (TREE_CODE (type) == TYPENAME_TYPE)
15980 {
15981 type = resolve_typename_type (type,
15982 /*only_current_p=*/false);
15983 if (type == error_mark_node)
15984 {
15985 cp_parser_abort_tentative_parse (parser);
15986 return false;
15987 }
15988 }
15989 pushed_scope = push_scope (type);
15990 }
15991
15992 /* Inside the constructor parameter list, surrounding
15993 template-parameter-lists do not apply. */
15994 saved_num_template_parameter_lists
15995 = parser->num_template_parameter_lists;
15996 parser->num_template_parameter_lists = 0;
15997
15998 /* Look for the type-specifier. */
15999 cp_parser_type_specifier (parser,
16000 CP_PARSER_FLAGS_NONE,
16001 /*decl_specs=*/NULL,
16002 /*is_declarator=*/true,
16003 /*declares_class_or_enum=*/NULL,
16004 /*is_cv_qualifier=*/NULL);
16005
16006 parser->num_template_parameter_lists
16007 = saved_num_template_parameter_lists;
16008
16009 /* Leave the scope of the class. */
16010 if (pushed_scope)
16011 pop_scope (pushed_scope);
16012
16013 constructor_p = !cp_parser_error_occurred (parser);
16014 }
16015 }
16016 else
16017 constructor_p = false;
16018 /* We did not really want to consume any tokens. */
16019 cp_parser_abort_tentative_parse (parser);
16020
16021 return constructor_p;
16022 }
16023
16024 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16025 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16026 they must be performed once we are in the scope of the function.
16027
16028 Returns the function defined. */
16029
16030 static tree
16031 cp_parser_function_definition_from_specifiers_and_declarator
16032 (cp_parser* parser,
16033 cp_decl_specifier_seq *decl_specifiers,
16034 tree attributes,
16035 const cp_declarator *declarator)
16036 {
16037 tree fn;
16038 bool success_p;
16039
16040 /* Begin the function-definition. */
16041 success_p = start_function (decl_specifiers, declarator, attributes);
16042
16043 /* The things we're about to see are not directly qualified by any
16044 template headers we've seen thus far. */
16045 reset_specialization ();
16046
16047 /* If there were names looked up in the decl-specifier-seq that we
16048 did not check, check them now. We must wait until we are in the
16049 scope of the function to perform the checks, since the function
16050 might be a friend. */
16051 perform_deferred_access_checks ();
16052
16053 if (!success_p)
16054 {
16055 /* Skip the entire function. */
16056 cp_parser_skip_to_end_of_block_or_statement (parser);
16057 fn = error_mark_node;
16058 }
16059 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16060 {
16061 /* Seen already, skip it. An error message has already been output. */
16062 cp_parser_skip_to_end_of_block_or_statement (parser);
16063 fn = current_function_decl;
16064 current_function_decl = NULL_TREE;
16065 /* If this is a function from a class, pop the nested class. */
16066 if (current_class_name)
16067 pop_nested_class ();
16068 }
16069 else
16070 fn = cp_parser_function_definition_after_declarator (parser,
16071 /*inline_p=*/false);
16072
16073 return fn;
16074 }
16075
16076 /* Parse the part of a function-definition that follows the
16077 declarator. INLINE_P is TRUE iff this function is an inline
16078 function defined with a class-specifier.
16079
16080 Returns the function defined. */
16081
16082 static tree
16083 cp_parser_function_definition_after_declarator (cp_parser* parser,
16084 bool inline_p)
16085 {
16086 tree fn;
16087 bool ctor_initializer_p = false;
16088 bool saved_in_unbraced_linkage_specification_p;
16089 bool saved_in_function_body;
16090 unsigned saved_num_template_parameter_lists;
16091
16092 saved_in_function_body = parser->in_function_body;
16093 parser->in_function_body = true;
16094 /* If the next token is `return', then the code may be trying to
16095 make use of the "named return value" extension that G++ used to
16096 support. */
16097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16098 {
16099 /* Consume the `return' keyword. */
16100 cp_lexer_consume_token (parser->lexer);
16101 /* Look for the identifier that indicates what value is to be
16102 returned. */
16103 cp_parser_identifier (parser);
16104 /* Issue an error message. */
16105 error ("named return values are no longer supported");
16106 /* Skip tokens until we reach the start of the function body. */
16107 while (true)
16108 {
16109 cp_token *token = cp_lexer_peek_token (parser->lexer);
16110 if (token->type == CPP_OPEN_BRACE
16111 || token->type == CPP_EOF
16112 || token->type == CPP_PRAGMA_EOL)
16113 break;
16114 cp_lexer_consume_token (parser->lexer);
16115 }
16116 }
16117 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16118 anything declared inside `f'. */
16119 saved_in_unbraced_linkage_specification_p
16120 = parser->in_unbraced_linkage_specification_p;
16121 parser->in_unbraced_linkage_specification_p = false;
16122 /* Inside the function, surrounding template-parameter-lists do not
16123 apply. */
16124 saved_num_template_parameter_lists
16125 = parser->num_template_parameter_lists;
16126 parser->num_template_parameter_lists = 0;
16127 /* If the next token is `try', then we are looking at a
16128 function-try-block. */
16129 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16130 ctor_initializer_p = cp_parser_function_try_block (parser);
16131 /* A function-try-block includes the function-body, so we only do
16132 this next part if we're not processing a function-try-block. */
16133 else
16134 ctor_initializer_p
16135 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16136
16137 /* Finish the function. */
16138 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16139 (inline_p ? 2 : 0));
16140 /* Generate code for it, if necessary. */
16141 expand_or_defer_fn (fn);
16142 /* Restore the saved values. */
16143 parser->in_unbraced_linkage_specification_p
16144 = saved_in_unbraced_linkage_specification_p;
16145 parser->num_template_parameter_lists
16146 = saved_num_template_parameter_lists;
16147 parser->in_function_body = saved_in_function_body;
16148
16149 return fn;
16150 }
16151
16152 /* Parse a template-declaration, assuming that the `export' (and
16153 `extern') keywords, if present, has already been scanned. MEMBER_P
16154 is as for cp_parser_template_declaration. */
16155
16156 static void
16157 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16158 {
16159 tree decl = NULL_TREE;
16160 VEC (deferred_access_check,gc) *checks;
16161 tree parameter_list;
16162 bool friend_p = false;
16163 bool need_lang_pop;
16164
16165 /* Look for the `template' keyword. */
16166 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16167 return;
16168
16169 /* And the `<'. */
16170 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16171 return;
16172 if (at_class_scope_p () && current_function_decl)
16173 {
16174 /* 14.5.2.2 [temp.mem]
16175
16176 A local class shall not have member templates. */
16177 error ("invalid declaration of member template in local class");
16178 cp_parser_skip_to_end_of_block_or_statement (parser);
16179 return;
16180 }
16181 /* [temp]
16182
16183 A template ... shall not have C linkage. */
16184 if (current_lang_name == lang_name_c)
16185 {
16186 error ("template with C linkage");
16187 /* Give it C++ linkage to avoid confusing other parts of the
16188 front end. */
16189 push_lang_context (lang_name_cplusplus);
16190 need_lang_pop = true;
16191 }
16192 else
16193 need_lang_pop = false;
16194
16195 /* We cannot perform access checks on the template parameter
16196 declarations until we know what is being declared, just as we
16197 cannot check the decl-specifier list. */
16198 push_deferring_access_checks (dk_deferred);
16199
16200 /* If the next token is `>', then we have an invalid
16201 specialization. Rather than complain about an invalid template
16202 parameter, issue an error message here. */
16203 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16204 {
16205 cp_parser_error (parser, "invalid explicit specialization");
16206 begin_specialization ();
16207 parameter_list = NULL_TREE;
16208 }
16209 else
16210 /* Parse the template parameters. */
16211 parameter_list = cp_parser_template_parameter_list (parser);
16212
16213 /* Get the deferred access checks from the parameter list. These
16214 will be checked once we know what is being declared, as for a
16215 member template the checks must be performed in the scope of the
16216 class containing the member. */
16217 checks = get_deferred_access_checks ();
16218
16219 /* Look for the `>'. */
16220 cp_parser_skip_to_end_of_template_parameter_list (parser);
16221 /* We just processed one more parameter list. */
16222 ++parser->num_template_parameter_lists;
16223 /* If the next token is `template', there are more template
16224 parameters. */
16225 if (cp_lexer_next_token_is_keyword (parser->lexer,
16226 RID_TEMPLATE))
16227 cp_parser_template_declaration_after_export (parser, member_p);
16228 else
16229 {
16230 /* There are no access checks when parsing a template, as we do not
16231 know if a specialization will be a friend. */
16232 push_deferring_access_checks (dk_no_check);
16233 decl = cp_parser_single_declaration (parser,
16234 checks,
16235 member_p,
16236 &friend_p);
16237 pop_deferring_access_checks ();
16238
16239 /* If this is a member template declaration, let the front
16240 end know. */
16241 if (member_p && !friend_p && decl)
16242 {
16243 if (TREE_CODE (decl) == TYPE_DECL)
16244 cp_parser_check_access_in_redeclaration (decl);
16245
16246 decl = finish_member_template_decl (decl);
16247 }
16248 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16249 make_friend_class (current_class_type, TREE_TYPE (decl),
16250 /*complain=*/true);
16251 }
16252 /* We are done with the current parameter list. */
16253 --parser->num_template_parameter_lists;
16254
16255 pop_deferring_access_checks ();
16256
16257 /* Finish up. */
16258 finish_template_decl (parameter_list);
16259
16260 /* Register member declarations. */
16261 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16262 finish_member_declaration (decl);
16263 /* For the erroneous case of a template with C linkage, we pushed an
16264 implicit C++ linkage scope; exit that scope now. */
16265 if (need_lang_pop)
16266 pop_lang_context ();
16267 /* If DECL is a function template, we must return to parse it later.
16268 (Even though there is no definition, there might be default
16269 arguments that need handling.) */
16270 if (member_p && decl
16271 && (TREE_CODE (decl) == FUNCTION_DECL
16272 || DECL_FUNCTION_TEMPLATE_P (decl)))
16273 TREE_VALUE (parser->unparsed_functions_queues)
16274 = tree_cons (NULL_TREE, decl,
16275 TREE_VALUE (parser->unparsed_functions_queues));
16276 }
16277
16278 /* Perform the deferred access checks from a template-parameter-list.
16279 CHECKS is a TREE_LIST of access checks, as returned by
16280 get_deferred_access_checks. */
16281
16282 static void
16283 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16284 {
16285 ++processing_template_parmlist;
16286 perform_access_checks (checks);
16287 --processing_template_parmlist;
16288 }
16289
16290 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16291 `function-definition' sequence. MEMBER_P is true, this declaration
16292 appears in a class scope.
16293
16294 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16295 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16296
16297 static tree
16298 cp_parser_single_declaration (cp_parser* parser,
16299 VEC (deferred_access_check,gc)* checks,
16300 bool member_p,
16301 bool* friend_p)
16302 {
16303 int declares_class_or_enum;
16304 tree decl = NULL_TREE;
16305 cp_decl_specifier_seq decl_specifiers;
16306 bool function_definition_p = false;
16307
16308 /* This function is only used when processing a template
16309 declaration. */
16310 gcc_assert (innermost_scope_kind () == sk_template_parms
16311 || innermost_scope_kind () == sk_template_spec);
16312
16313 /* Defer access checks until we know what is being declared. */
16314 push_deferring_access_checks (dk_deferred);
16315
16316 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16317 alternative. */
16318 cp_parser_decl_specifier_seq (parser,
16319 CP_PARSER_FLAGS_OPTIONAL,
16320 &decl_specifiers,
16321 &declares_class_or_enum);
16322 if (friend_p)
16323 *friend_p = cp_parser_friend_p (&decl_specifiers);
16324
16325 /* There are no template typedefs. */
16326 if (decl_specifiers.specs[(int) ds_typedef])
16327 {
16328 error ("template declaration of %qs", "typedef");
16329 decl = error_mark_node;
16330 }
16331
16332 /* Gather up the access checks that occurred the
16333 decl-specifier-seq. */
16334 stop_deferring_access_checks ();
16335
16336 /* Check for the declaration of a template class. */
16337 if (declares_class_or_enum)
16338 {
16339 if (cp_parser_declares_only_class_p (parser))
16340 {
16341 decl = shadow_tag (&decl_specifiers);
16342
16343 /* In this case:
16344
16345 struct C {
16346 friend template <typename T> struct A<T>::B;
16347 };
16348
16349 A<T>::B will be represented by a TYPENAME_TYPE, and
16350 therefore not recognized by shadow_tag. */
16351 if (friend_p && *friend_p
16352 && !decl
16353 && decl_specifiers.type
16354 && TYPE_P (decl_specifiers.type))
16355 decl = decl_specifiers.type;
16356
16357 if (decl && decl != error_mark_node)
16358 decl = TYPE_NAME (decl);
16359 else
16360 decl = error_mark_node;
16361
16362 /* Perform access checks for template parameters. */
16363 cp_parser_perform_template_parameter_access_checks (checks);
16364 }
16365 }
16366 /* If it's not a template class, try for a template function. If
16367 the next token is a `;', then this declaration does not declare
16368 anything. But, if there were errors in the decl-specifiers, then
16369 the error might well have come from an attempted class-specifier.
16370 In that case, there's no need to warn about a missing declarator. */
16371 if (!decl
16372 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16373 || decl_specifiers.type != error_mark_node))
16374 decl = cp_parser_init_declarator (parser,
16375 &decl_specifiers,
16376 checks,
16377 /*function_definition_allowed_p=*/true,
16378 member_p,
16379 declares_class_or_enum,
16380 &function_definition_p);
16381
16382 pop_deferring_access_checks ();
16383
16384 /* Clear any current qualification; whatever comes next is the start
16385 of something new. */
16386 parser->scope = NULL_TREE;
16387 parser->qualifying_scope = NULL_TREE;
16388 parser->object_scope = NULL_TREE;
16389 /* Look for a trailing `;' after the declaration. */
16390 if (!function_definition_p
16391 && (decl == error_mark_node
16392 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16393 cp_parser_skip_to_end_of_block_or_statement (parser);
16394
16395 return decl;
16396 }
16397
16398 /* Parse a cast-expression that is not the operand of a unary "&". */
16399
16400 static tree
16401 cp_parser_simple_cast_expression (cp_parser *parser)
16402 {
16403 return cp_parser_cast_expression (parser, /*address_p=*/false,
16404 /*cast_p=*/false);
16405 }
16406
16407 /* Parse a functional cast to TYPE. Returns an expression
16408 representing the cast. */
16409
16410 static tree
16411 cp_parser_functional_cast (cp_parser* parser, tree type)
16412 {
16413 tree expression_list;
16414 tree cast;
16415
16416 expression_list
16417 = cp_parser_parenthesized_expression_list (parser, false,
16418 /*cast_p=*/true,
16419 /*allow_expansion_p=*/true,
16420 /*non_constant_p=*/NULL);
16421
16422 cast = build_functional_cast (type, expression_list);
16423 /* [expr.const]/1: In an integral constant expression "only type
16424 conversions to integral or enumeration type can be used". */
16425 if (TREE_CODE (type) == TYPE_DECL)
16426 type = TREE_TYPE (type);
16427 if (cast != error_mark_node
16428 && !cast_valid_in_integral_constant_expression_p (type)
16429 && (cp_parser_non_integral_constant_expression
16430 (parser, "a call to a constructor")))
16431 return error_mark_node;
16432 return cast;
16433 }
16434
16435 /* Save the tokens that make up the body of a member function defined
16436 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16437 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16438 specifiers applied to the declaration. Returns the FUNCTION_DECL
16439 for the member function. */
16440
16441 static tree
16442 cp_parser_save_member_function_body (cp_parser* parser,
16443 cp_decl_specifier_seq *decl_specifiers,
16444 cp_declarator *declarator,
16445 tree attributes)
16446 {
16447 cp_token *first;
16448 cp_token *last;
16449 tree fn;
16450
16451 /* Create the function-declaration. */
16452 fn = start_method (decl_specifiers, declarator, attributes);
16453 /* If something went badly wrong, bail out now. */
16454 if (fn == error_mark_node)
16455 {
16456 /* If there's a function-body, skip it. */
16457 if (cp_parser_token_starts_function_definition_p
16458 (cp_lexer_peek_token (parser->lexer)))
16459 cp_parser_skip_to_end_of_block_or_statement (parser);
16460 return error_mark_node;
16461 }
16462
16463 /* Remember it, if there default args to post process. */
16464 cp_parser_save_default_args (parser, fn);
16465
16466 /* Save away the tokens that make up the body of the
16467 function. */
16468 first = parser->lexer->next_token;
16469 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16470 /* Handle function try blocks. */
16471 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16472 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16473 last = parser->lexer->next_token;
16474
16475 /* Save away the inline definition; we will process it when the
16476 class is complete. */
16477 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16478 DECL_PENDING_INLINE_P (fn) = 1;
16479
16480 /* We need to know that this was defined in the class, so that
16481 friend templates are handled correctly. */
16482 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16483
16484 /* We're done with the inline definition. */
16485 finish_method (fn);
16486
16487 /* Add FN to the queue of functions to be parsed later. */
16488 TREE_VALUE (parser->unparsed_functions_queues)
16489 = tree_cons (NULL_TREE, fn,
16490 TREE_VALUE (parser->unparsed_functions_queues));
16491
16492 return fn;
16493 }
16494
16495 /* Parse a template-argument-list, as well as the trailing ">" (but
16496 not the opening ">"). See cp_parser_template_argument_list for the
16497 return value. */
16498
16499 static tree
16500 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16501 {
16502 tree arguments;
16503 tree saved_scope;
16504 tree saved_qualifying_scope;
16505 tree saved_object_scope;
16506 bool saved_greater_than_is_operator_p;
16507 bool saved_skip_evaluation;
16508
16509 /* [temp.names]
16510
16511 When parsing a template-id, the first non-nested `>' is taken as
16512 the end of the template-argument-list rather than a greater-than
16513 operator. */
16514 saved_greater_than_is_operator_p
16515 = parser->greater_than_is_operator_p;
16516 parser->greater_than_is_operator_p = false;
16517 /* Parsing the argument list may modify SCOPE, so we save it
16518 here. */
16519 saved_scope = parser->scope;
16520 saved_qualifying_scope = parser->qualifying_scope;
16521 saved_object_scope = parser->object_scope;
16522 /* We need to evaluate the template arguments, even though this
16523 template-id may be nested within a "sizeof". */
16524 saved_skip_evaluation = skip_evaluation;
16525 skip_evaluation = false;
16526 /* Parse the template-argument-list itself. */
16527 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16528 arguments = NULL_TREE;
16529 else
16530 arguments = cp_parser_template_argument_list (parser);
16531 /* Look for the `>' that ends the template-argument-list. If we find
16532 a '>>' instead, it's probably just a typo. */
16533 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16534 {
16535 if (!saved_greater_than_is_operator_p)
16536 {
16537 /* If we're in a nested template argument list, the '>>' has
16538 to be a typo for '> >'. We emit the error message, but we
16539 continue parsing and we push a '>' as next token, so that
16540 the argument list will be parsed correctly. Note that the
16541 global source location is still on the token before the
16542 '>>', so we need to say explicitly where we want it. */
16543 cp_token *token = cp_lexer_peek_token (parser->lexer);
16544 error ("%H%<>>%> should be %<> >%> "
16545 "within a nested template argument list",
16546 &token->location);
16547
16548 /* ??? Proper recovery should terminate two levels of
16549 template argument list here. */
16550 token->type = CPP_GREATER;
16551 }
16552 else
16553 {
16554 /* If this is not a nested template argument list, the '>>'
16555 is a typo for '>'. Emit an error message and continue.
16556 Same deal about the token location, but here we can get it
16557 right by consuming the '>>' before issuing the diagnostic. */
16558 cp_lexer_consume_token (parser->lexer);
16559 error ("spurious %<>>%>, use %<>%> to terminate "
16560 "a template argument list");
16561 }
16562 }
16563 else
16564 cp_parser_skip_to_end_of_template_parameter_list (parser);
16565 /* The `>' token might be a greater-than operator again now. */
16566 parser->greater_than_is_operator_p
16567 = saved_greater_than_is_operator_p;
16568 /* Restore the SAVED_SCOPE. */
16569 parser->scope = saved_scope;
16570 parser->qualifying_scope = saved_qualifying_scope;
16571 parser->object_scope = saved_object_scope;
16572 skip_evaluation = saved_skip_evaluation;
16573
16574 return arguments;
16575 }
16576
16577 /* MEMBER_FUNCTION is a member function, or a friend. If default
16578 arguments, or the body of the function have not yet been parsed,
16579 parse them now. */
16580
16581 static void
16582 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16583 {
16584 /* If this member is a template, get the underlying
16585 FUNCTION_DECL. */
16586 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16587 member_function = DECL_TEMPLATE_RESULT (member_function);
16588
16589 /* There should not be any class definitions in progress at this
16590 point; the bodies of members are only parsed outside of all class
16591 definitions. */
16592 gcc_assert (parser->num_classes_being_defined == 0);
16593 /* While we're parsing the member functions we might encounter more
16594 classes. We want to handle them right away, but we don't want
16595 them getting mixed up with functions that are currently in the
16596 queue. */
16597 parser->unparsed_functions_queues
16598 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16599
16600 /* Make sure that any template parameters are in scope. */
16601 maybe_begin_member_template_processing (member_function);
16602
16603 /* If the body of the function has not yet been parsed, parse it
16604 now. */
16605 if (DECL_PENDING_INLINE_P (member_function))
16606 {
16607 tree function_scope;
16608 cp_token_cache *tokens;
16609
16610 /* The function is no longer pending; we are processing it. */
16611 tokens = DECL_PENDING_INLINE_INFO (member_function);
16612 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16613 DECL_PENDING_INLINE_P (member_function) = 0;
16614
16615 /* If this is a local class, enter the scope of the containing
16616 function. */
16617 function_scope = current_function_decl;
16618 if (function_scope)
16619 push_function_context_to (function_scope);
16620
16621
16622 /* Push the body of the function onto the lexer stack. */
16623 cp_parser_push_lexer_for_tokens (parser, tokens);
16624
16625 /* Let the front end know that we going to be defining this
16626 function. */
16627 start_preparsed_function (member_function, NULL_TREE,
16628 SF_PRE_PARSED | SF_INCLASS_INLINE);
16629
16630 /* Don't do access checking if it is a templated function. */
16631 if (processing_template_decl)
16632 push_deferring_access_checks (dk_no_check);
16633
16634 /* Now, parse the body of the function. */
16635 cp_parser_function_definition_after_declarator (parser,
16636 /*inline_p=*/true);
16637
16638 if (processing_template_decl)
16639 pop_deferring_access_checks ();
16640
16641 /* Leave the scope of the containing function. */
16642 if (function_scope)
16643 pop_function_context_from (function_scope);
16644 cp_parser_pop_lexer (parser);
16645 }
16646
16647 /* Remove any template parameters from the symbol table. */
16648 maybe_end_member_template_processing ();
16649
16650 /* Restore the queue. */
16651 parser->unparsed_functions_queues
16652 = TREE_CHAIN (parser->unparsed_functions_queues);
16653 }
16654
16655 /* If DECL contains any default args, remember it on the unparsed
16656 functions queue. */
16657
16658 static void
16659 cp_parser_save_default_args (cp_parser* parser, tree decl)
16660 {
16661 tree probe;
16662
16663 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16664 probe;
16665 probe = TREE_CHAIN (probe))
16666 if (TREE_PURPOSE (probe))
16667 {
16668 TREE_PURPOSE (parser->unparsed_functions_queues)
16669 = tree_cons (current_class_type, decl,
16670 TREE_PURPOSE (parser->unparsed_functions_queues));
16671 break;
16672 }
16673 }
16674
16675 /* FN is a FUNCTION_DECL which may contains a parameter with an
16676 unparsed DEFAULT_ARG. Parse the default args now. This function
16677 assumes that the current scope is the scope in which the default
16678 argument should be processed. */
16679
16680 static void
16681 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16682 {
16683 bool saved_local_variables_forbidden_p;
16684 tree parm;
16685
16686 /* While we're parsing the default args, we might (due to the
16687 statement expression extension) encounter more classes. We want
16688 to handle them right away, but we don't want them getting mixed
16689 up with default args that are currently in the queue. */
16690 parser->unparsed_functions_queues
16691 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16692
16693 /* Local variable names (and the `this' keyword) may not appear
16694 in a default argument. */
16695 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16696 parser->local_variables_forbidden_p = true;
16697
16698 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16699 parm;
16700 parm = TREE_CHAIN (parm))
16701 {
16702 cp_token_cache *tokens;
16703 tree default_arg = TREE_PURPOSE (parm);
16704 tree parsed_arg;
16705 VEC(tree,gc) *insts;
16706 tree copy;
16707 unsigned ix;
16708
16709 if (!default_arg)
16710 continue;
16711
16712 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16713 /* This can happen for a friend declaration for a function
16714 already declared with default arguments. */
16715 continue;
16716
16717 /* Push the saved tokens for the default argument onto the parser's
16718 lexer stack. */
16719 tokens = DEFARG_TOKENS (default_arg);
16720 cp_parser_push_lexer_for_tokens (parser, tokens);
16721
16722 /* Parse the assignment-expression. */
16723 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16724
16725 if (!processing_template_decl)
16726 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16727
16728 TREE_PURPOSE (parm) = parsed_arg;
16729
16730 /* Update any instantiations we've already created. */
16731 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16732 VEC_iterate (tree, insts, ix, copy); ix++)
16733 TREE_PURPOSE (copy) = parsed_arg;
16734
16735 /* If the token stream has not been completely used up, then
16736 there was extra junk after the end of the default
16737 argument. */
16738 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16739 cp_parser_error (parser, "expected %<,%>");
16740
16741 /* Revert to the main lexer. */
16742 cp_parser_pop_lexer (parser);
16743 }
16744
16745 /* Make sure no default arg is missing. */
16746 check_default_args (fn);
16747
16748 /* Restore the state of local_variables_forbidden_p. */
16749 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16750
16751 /* Restore the queue. */
16752 parser->unparsed_functions_queues
16753 = TREE_CHAIN (parser->unparsed_functions_queues);
16754 }
16755
16756 /* Parse the operand of `sizeof' (or a similar operator). Returns
16757 either a TYPE or an expression, depending on the form of the
16758 input. The KEYWORD indicates which kind of expression we have
16759 encountered. */
16760
16761 static tree
16762 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16763 {
16764 static const char *format;
16765 tree expr = NULL_TREE;
16766 const char *saved_message;
16767 bool saved_integral_constant_expression_p;
16768 bool saved_non_integral_constant_expression_p;
16769 bool pack_expansion_p = false;
16770
16771 /* Initialize FORMAT the first time we get here. */
16772 if (!format)
16773 format = "types may not be defined in '%s' expressions";
16774
16775 /* Types cannot be defined in a `sizeof' expression. Save away the
16776 old message. */
16777 saved_message = parser->type_definition_forbidden_message;
16778 /* And create the new one. */
16779 parser->type_definition_forbidden_message
16780 = XNEWVEC (const char, strlen (format)
16781 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16782 + 1 /* `\0' */);
16783 sprintf ((char *) parser->type_definition_forbidden_message,
16784 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16785
16786 /* The restrictions on constant-expressions do not apply inside
16787 sizeof expressions. */
16788 saved_integral_constant_expression_p
16789 = parser->integral_constant_expression_p;
16790 saved_non_integral_constant_expression_p
16791 = parser->non_integral_constant_expression_p;
16792 parser->integral_constant_expression_p = false;
16793
16794 /* If it's a `...', then we are computing the length of a parameter
16795 pack. */
16796 if (keyword == RID_SIZEOF
16797 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16798 {
16799 /* Consume the `...'. */
16800 cp_lexer_consume_token (parser->lexer);
16801 maybe_warn_variadic_templates ();
16802
16803 /* Note that this is an expansion. */
16804 pack_expansion_p = true;
16805 }
16806
16807 /* Do not actually evaluate the expression. */
16808 ++skip_evaluation;
16809 /* If it's a `(', then we might be looking at the type-id
16810 construction. */
16811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16812 {
16813 tree type;
16814 bool saved_in_type_id_in_expr_p;
16815
16816 /* We can't be sure yet whether we're looking at a type-id or an
16817 expression. */
16818 cp_parser_parse_tentatively (parser);
16819 /* Consume the `('. */
16820 cp_lexer_consume_token (parser->lexer);
16821 /* Parse the type-id. */
16822 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16823 parser->in_type_id_in_expr_p = true;
16824 type = cp_parser_type_id (parser);
16825 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16826 /* Now, look for the trailing `)'. */
16827 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16828 /* If all went well, then we're done. */
16829 if (cp_parser_parse_definitely (parser))
16830 {
16831 cp_decl_specifier_seq decl_specs;
16832
16833 /* Build a trivial decl-specifier-seq. */
16834 clear_decl_specs (&decl_specs);
16835 decl_specs.type = type;
16836
16837 /* Call grokdeclarator to figure out what type this is. */
16838 expr = grokdeclarator (NULL,
16839 &decl_specs,
16840 TYPENAME,
16841 /*initialized=*/0,
16842 /*attrlist=*/NULL);
16843 }
16844 }
16845
16846 /* If the type-id production did not work out, then we must be
16847 looking at the unary-expression production. */
16848 if (!expr)
16849 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16850 /*cast_p=*/false);
16851
16852 if (pack_expansion_p)
16853 /* Build a pack expansion. */
16854 expr = make_pack_expansion (expr);
16855
16856 /* Go back to evaluating expressions. */
16857 --skip_evaluation;
16858
16859 /* Free the message we created. */
16860 free ((char *) parser->type_definition_forbidden_message);
16861 /* And restore the old one. */
16862 parser->type_definition_forbidden_message = saved_message;
16863 parser->integral_constant_expression_p
16864 = saved_integral_constant_expression_p;
16865 parser->non_integral_constant_expression_p
16866 = saved_non_integral_constant_expression_p;
16867
16868 return expr;
16869 }
16870
16871 /* If the current declaration has no declarator, return true. */
16872
16873 static bool
16874 cp_parser_declares_only_class_p (cp_parser *parser)
16875 {
16876 /* If the next token is a `;' or a `,' then there is no
16877 declarator. */
16878 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16879 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16880 }
16881
16882 /* Update the DECL_SPECS to reflect the storage class indicated by
16883 KEYWORD. */
16884
16885 static void
16886 cp_parser_set_storage_class (cp_parser *parser,
16887 cp_decl_specifier_seq *decl_specs,
16888 enum rid keyword)
16889 {
16890 cp_storage_class storage_class;
16891
16892 if (parser->in_unbraced_linkage_specification_p)
16893 {
16894 error ("invalid use of %qD in linkage specification",
16895 ridpointers[keyword]);
16896 return;
16897 }
16898 else if (decl_specs->storage_class != sc_none)
16899 {
16900 decl_specs->conflicting_specifiers_p = true;
16901 return;
16902 }
16903
16904 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16905 && decl_specs->specs[(int) ds_thread])
16906 {
16907 error ("%<__thread%> before %qD", ridpointers[keyword]);
16908 decl_specs->specs[(int) ds_thread] = 0;
16909 }
16910
16911 switch (keyword)
16912 {
16913 case RID_AUTO:
16914 storage_class = sc_auto;
16915 break;
16916 case RID_REGISTER:
16917 storage_class = sc_register;
16918 break;
16919 case RID_STATIC:
16920 storage_class = sc_static;
16921 break;
16922 case RID_EXTERN:
16923 storage_class = sc_extern;
16924 break;
16925 case RID_MUTABLE:
16926 storage_class = sc_mutable;
16927 break;
16928 default:
16929 gcc_unreachable ();
16930 }
16931 decl_specs->storage_class = storage_class;
16932
16933 /* A storage class specifier cannot be applied alongside a typedef
16934 specifier. If there is a typedef specifier present then set
16935 conflicting_specifiers_p which will trigger an error later
16936 on in grokdeclarator. */
16937 if (decl_specs->specs[(int)ds_typedef])
16938 decl_specs->conflicting_specifiers_p = true;
16939 }
16940
16941 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16942 is true, the type is a user-defined type; otherwise it is a
16943 built-in type specified by a keyword. */
16944
16945 static void
16946 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16947 tree type_spec,
16948 bool user_defined_p)
16949 {
16950 decl_specs->any_specifiers_p = true;
16951
16952 /* If the user tries to redeclare bool or wchar_t (with, for
16953 example, in "typedef int wchar_t;") we remember that this is what
16954 happened. In system headers, we ignore these declarations so
16955 that G++ can work with system headers that are not C++-safe. */
16956 if (decl_specs->specs[(int) ds_typedef]
16957 && !user_defined_p
16958 && (type_spec == boolean_type_node
16959 || type_spec == wchar_type_node)
16960 && (decl_specs->type
16961 || decl_specs->specs[(int) ds_long]
16962 || decl_specs->specs[(int) ds_short]
16963 || decl_specs->specs[(int) ds_unsigned]
16964 || decl_specs->specs[(int) ds_signed]))
16965 {
16966 decl_specs->redefined_builtin_type = type_spec;
16967 if (!decl_specs->type)
16968 {
16969 decl_specs->type = type_spec;
16970 decl_specs->user_defined_type_p = false;
16971 }
16972 }
16973 else if (decl_specs->type)
16974 decl_specs->multiple_types_p = true;
16975 else
16976 {
16977 decl_specs->type = type_spec;
16978 decl_specs->user_defined_type_p = user_defined_p;
16979 decl_specs->redefined_builtin_type = NULL_TREE;
16980 }
16981 }
16982
16983 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16984 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16985
16986 static bool
16987 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16988 {
16989 return decl_specifiers->specs[(int) ds_friend] != 0;
16990 }
16991
16992 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16993 issue an error message indicating that TOKEN_DESC was expected.
16994
16995 Returns the token consumed, if the token had the appropriate type.
16996 Otherwise, returns NULL. */
16997
16998 static cp_token *
16999 cp_parser_require (cp_parser* parser,
17000 enum cpp_ttype type,
17001 const char* token_desc)
17002 {
17003 if (cp_lexer_next_token_is (parser->lexer, type))
17004 return cp_lexer_consume_token (parser->lexer);
17005 else
17006 {
17007 /* Output the MESSAGE -- unless we're parsing tentatively. */
17008 if (!cp_parser_simulate_error (parser))
17009 {
17010 char *message = concat ("expected ", token_desc, NULL);
17011 cp_parser_error (parser, message);
17012 free (message);
17013 }
17014 return NULL;
17015 }
17016 }
17017
17018 /* An error message is produced if the next token is not '>'.
17019 All further tokens are skipped until the desired token is
17020 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17021
17022 static void
17023 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17024 {
17025 /* Current level of '< ... >'. */
17026 unsigned level = 0;
17027 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17028 unsigned nesting_depth = 0;
17029
17030 /* Are we ready, yet? If not, issue error message. */
17031 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17032 return;
17033
17034 /* Skip tokens until the desired token is found. */
17035 while (true)
17036 {
17037 /* Peek at the next token. */
17038 switch (cp_lexer_peek_token (parser->lexer)->type)
17039 {
17040 case CPP_LESS:
17041 if (!nesting_depth)
17042 ++level;
17043 break;
17044
17045 case CPP_GREATER:
17046 if (!nesting_depth && level-- == 0)
17047 {
17048 /* We've reached the token we want, consume it and stop. */
17049 cp_lexer_consume_token (parser->lexer);
17050 return;
17051 }
17052 break;
17053
17054 case CPP_OPEN_PAREN:
17055 case CPP_OPEN_SQUARE:
17056 ++nesting_depth;
17057 break;
17058
17059 case CPP_CLOSE_PAREN:
17060 case CPP_CLOSE_SQUARE:
17061 if (nesting_depth-- == 0)
17062 return;
17063 break;
17064
17065 case CPP_EOF:
17066 case CPP_PRAGMA_EOL:
17067 case CPP_SEMICOLON:
17068 case CPP_OPEN_BRACE:
17069 case CPP_CLOSE_BRACE:
17070 /* The '>' was probably forgotten, don't look further. */
17071 return;
17072
17073 default:
17074 break;
17075 }
17076
17077 /* Consume this token. */
17078 cp_lexer_consume_token (parser->lexer);
17079 }
17080 }
17081
17082 /* If the next token is the indicated keyword, consume it. Otherwise,
17083 issue an error message indicating that TOKEN_DESC was expected.
17084
17085 Returns the token consumed, if the token had the appropriate type.
17086 Otherwise, returns NULL. */
17087
17088 static cp_token *
17089 cp_parser_require_keyword (cp_parser* parser,
17090 enum rid keyword,
17091 const char* token_desc)
17092 {
17093 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17094
17095 if (token && token->keyword != keyword)
17096 {
17097 dyn_string_t error_msg;
17098
17099 /* Format the error message. */
17100 error_msg = dyn_string_new (0);
17101 dyn_string_append_cstr (error_msg, "expected ");
17102 dyn_string_append_cstr (error_msg, token_desc);
17103 cp_parser_error (parser, error_msg->s);
17104 dyn_string_delete (error_msg);
17105 return NULL;
17106 }
17107
17108 return token;
17109 }
17110
17111 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17112 function-definition. */
17113
17114 static bool
17115 cp_parser_token_starts_function_definition_p (cp_token* token)
17116 {
17117 return (/* An ordinary function-body begins with an `{'. */
17118 token->type == CPP_OPEN_BRACE
17119 /* A ctor-initializer begins with a `:'. */
17120 || token->type == CPP_COLON
17121 /* A function-try-block begins with `try'. */
17122 || token->keyword == RID_TRY
17123 /* The named return value extension begins with `return'. */
17124 || token->keyword == RID_RETURN);
17125 }
17126
17127 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17128 definition. */
17129
17130 static bool
17131 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17132 {
17133 cp_token *token;
17134
17135 token = cp_lexer_peek_token (parser->lexer);
17136 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17137 }
17138
17139 /* Returns TRUE iff the next token is the "," or ">" ending a
17140 template-argument. */
17141
17142 static bool
17143 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17144 {
17145 cp_token *token;
17146
17147 token = cp_lexer_peek_token (parser->lexer);
17148 return (token->type == CPP_COMMA
17149 || token->type == CPP_GREATER
17150 || token->type == CPP_ELLIPSIS);
17151 }
17152
17153 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17154 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17155
17156 static bool
17157 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17158 size_t n)
17159 {
17160 cp_token *token;
17161
17162 token = cp_lexer_peek_nth_token (parser->lexer, n);
17163 if (token->type == CPP_LESS)
17164 return true;
17165 /* Check for the sequence `<::' in the original code. It would be lexed as
17166 `[:', where `[' is a digraph, and there is no whitespace before
17167 `:'. */
17168 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17169 {
17170 cp_token *token2;
17171 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17172 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17173 return true;
17174 }
17175 return false;
17176 }
17177
17178 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17179 or none_type otherwise. */
17180
17181 static enum tag_types
17182 cp_parser_token_is_class_key (cp_token* token)
17183 {
17184 switch (token->keyword)
17185 {
17186 case RID_CLASS:
17187 return class_type;
17188 case RID_STRUCT:
17189 return record_type;
17190 case RID_UNION:
17191 return union_type;
17192
17193 default:
17194 return none_type;
17195 }
17196 }
17197
17198 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17199
17200 static void
17201 cp_parser_check_class_key (enum tag_types class_key, tree type)
17202 {
17203 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17204 pedwarn ("%qs tag used in naming %q#T",
17205 class_key == union_type ? "union"
17206 : class_key == record_type ? "struct" : "class",
17207 type);
17208 }
17209
17210 /* Issue an error message if DECL is redeclared with different
17211 access than its original declaration [class.access.spec/3].
17212 This applies to nested classes and nested class templates.
17213 [class.mem/1]. */
17214
17215 static void
17216 cp_parser_check_access_in_redeclaration (tree decl)
17217 {
17218 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17219 return;
17220
17221 if ((TREE_PRIVATE (decl)
17222 != (current_access_specifier == access_private_node))
17223 || (TREE_PROTECTED (decl)
17224 != (current_access_specifier == access_protected_node)))
17225 error ("%qD redeclared with different access", decl);
17226 }
17227
17228 /* Look for the `template' keyword, as a syntactic disambiguator.
17229 Return TRUE iff it is present, in which case it will be
17230 consumed. */
17231
17232 static bool
17233 cp_parser_optional_template_keyword (cp_parser *parser)
17234 {
17235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17236 {
17237 /* The `template' keyword can only be used within templates;
17238 outside templates the parser can always figure out what is a
17239 template and what is not. */
17240 if (!processing_template_decl)
17241 {
17242 error ("%<template%> (as a disambiguator) is only allowed "
17243 "within templates");
17244 /* If this part of the token stream is rescanned, the same
17245 error message would be generated. So, we purge the token
17246 from the stream. */
17247 cp_lexer_purge_token (parser->lexer);
17248 return false;
17249 }
17250 else
17251 {
17252 /* Consume the `template' keyword. */
17253 cp_lexer_consume_token (parser->lexer);
17254 return true;
17255 }
17256 }
17257
17258 return false;
17259 }
17260
17261 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17262 set PARSER->SCOPE, and perform other related actions. */
17263
17264 static void
17265 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17266 {
17267 int i;
17268 struct tree_check *check_value;
17269 deferred_access_check *chk;
17270 VEC (deferred_access_check,gc) *checks;
17271
17272 /* Get the stored value. */
17273 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17274 /* Perform any access checks that were deferred. */
17275 checks = check_value->checks;
17276 if (checks)
17277 {
17278 for (i = 0 ;
17279 VEC_iterate (deferred_access_check, checks, i, chk) ;
17280 ++i)
17281 {
17282 perform_or_defer_access_check (chk->binfo,
17283 chk->decl,
17284 chk->diag_decl);
17285 }
17286 }
17287 /* Set the scope from the stored value. */
17288 parser->scope = check_value->value;
17289 parser->qualifying_scope = check_value->qualifying_scope;
17290 parser->object_scope = NULL_TREE;
17291 }
17292
17293 /* Consume tokens up through a non-nested END token. */
17294
17295 static void
17296 cp_parser_cache_group (cp_parser *parser,
17297 enum cpp_ttype end,
17298 unsigned depth)
17299 {
17300 while (true)
17301 {
17302 cp_token *token;
17303
17304 /* Abort a parenthesized expression if we encounter a brace. */
17305 if ((end == CPP_CLOSE_PAREN || depth == 0)
17306 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17307 return;
17308 /* If we've reached the end of the file, stop. */
17309 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17310 || (end != CPP_PRAGMA_EOL
17311 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17312 return;
17313 /* Consume the next token. */
17314 token = cp_lexer_consume_token (parser->lexer);
17315 /* See if it starts a new group. */
17316 if (token->type == CPP_OPEN_BRACE)
17317 {
17318 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17319 if (depth == 0)
17320 return;
17321 }
17322 else if (token->type == CPP_OPEN_PAREN)
17323 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17324 else if (token->type == CPP_PRAGMA)
17325 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17326 else if (token->type == end)
17327 return;
17328 }
17329 }
17330
17331 /* Begin parsing tentatively. We always save tokens while parsing
17332 tentatively so that if the tentative parsing fails we can restore the
17333 tokens. */
17334
17335 static void
17336 cp_parser_parse_tentatively (cp_parser* parser)
17337 {
17338 /* Enter a new parsing context. */
17339 parser->context = cp_parser_context_new (parser->context);
17340 /* Begin saving tokens. */
17341 cp_lexer_save_tokens (parser->lexer);
17342 /* In order to avoid repetitive access control error messages,
17343 access checks are queued up until we are no longer parsing
17344 tentatively. */
17345 push_deferring_access_checks (dk_deferred);
17346 }
17347
17348 /* Commit to the currently active tentative parse. */
17349
17350 static void
17351 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17352 {
17353 cp_parser_context *context;
17354 cp_lexer *lexer;
17355
17356 /* Mark all of the levels as committed. */
17357 lexer = parser->lexer;
17358 for (context = parser->context; context->next; context = context->next)
17359 {
17360 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17361 break;
17362 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17363 while (!cp_lexer_saving_tokens (lexer))
17364 lexer = lexer->next;
17365 cp_lexer_commit_tokens (lexer);
17366 }
17367 }
17368
17369 /* Abort the currently active tentative parse. All consumed tokens
17370 will be rolled back, and no diagnostics will be issued. */
17371
17372 static void
17373 cp_parser_abort_tentative_parse (cp_parser* parser)
17374 {
17375 cp_parser_simulate_error (parser);
17376 /* Now, pretend that we want to see if the construct was
17377 successfully parsed. */
17378 cp_parser_parse_definitely (parser);
17379 }
17380
17381 /* Stop parsing tentatively. If a parse error has occurred, restore the
17382 token stream. Otherwise, commit to the tokens we have consumed.
17383 Returns true if no error occurred; false otherwise. */
17384
17385 static bool
17386 cp_parser_parse_definitely (cp_parser* parser)
17387 {
17388 bool error_occurred;
17389 cp_parser_context *context;
17390
17391 /* Remember whether or not an error occurred, since we are about to
17392 destroy that information. */
17393 error_occurred = cp_parser_error_occurred (parser);
17394 /* Remove the topmost context from the stack. */
17395 context = parser->context;
17396 parser->context = context->next;
17397 /* If no parse errors occurred, commit to the tentative parse. */
17398 if (!error_occurred)
17399 {
17400 /* Commit to the tokens read tentatively, unless that was
17401 already done. */
17402 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17403 cp_lexer_commit_tokens (parser->lexer);
17404
17405 pop_to_parent_deferring_access_checks ();
17406 }
17407 /* Otherwise, if errors occurred, roll back our state so that things
17408 are just as they were before we began the tentative parse. */
17409 else
17410 {
17411 cp_lexer_rollback_tokens (parser->lexer);
17412 pop_deferring_access_checks ();
17413 }
17414 /* Add the context to the front of the free list. */
17415 context->next = cp_parser_context_free_list;
17416 cp_parser_context_free_list = context;
17417
17418 return !error_occurred;
17419 }
17420
17421 /* Returns true if we are parsing tentatively and are not committed to
17422 this tentative parse. */
17423
17424 static bool
17425 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17426 {
17427 return (cp_parser_parsing_tentatively (parser)
17428 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17429 }
17430
17431 /* Returns nonzero iff an error has occurred during the most recent
17432 tentative parse. */
17433
17434 static bool
17435 cp_parser_error_occurred (cp_parser* parser)
17436 {
17437 return (cp_parser_parsing_tentatively (parser)
17438 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17439 }
17440
17441 /* Returns nonzero if GNU extensions are allowed. */
17442
17443 static bool
17444 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17445 {
17446 return parser->allow_gnu_extensions_p;
17447 }
17448 \f
17449 /* Objective-C++ Productions */
17450
17451
17452 /* Parse an Objective-C expression, which feeds into a primary-expression
17453 above.
17454
17455 objc-expression:
17456 objc-message-expression
17457 objc-string-literal
17458 objc-encode-expression
17459 objc-protocol-expression
17460 objc-selector-expression
17461
17462 Returns a tree representation of the expression. */
17463
17464 static tree
17465 cp_parser_objc_expression (cp_parser* parser)
17466 {
17467 /* Try to figure out what kind of declaration is present. */
17468 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17469
17470 switch (kwd->type)
17471 {
17472 case CPP_OPEN_SQUARE:
17473 return cp_parser_objc_message_expression (parser);
17474
17475 case CPP_OBJC_STRING:
17476 kwd = cp_lexer_consume_token (parser->lexer);
17477 return objc_build_string_object (kwd->u.value);
17478
17479 case CPP_KEYWORD:
17480 switch (kwd->keyword)
17481 {
17482 case RID_AT_ENCODE:
17483 return cp_parser_objc_encode_expression (parser);
17484
17485 case RID_AT_PROTOCOL:
17486 return cp_parser_objc_protocol_expression (parser);
17487
17488 case RID_AT_SELECTOR:
17489 return cp_parser_objc_selector_expression (parser);
17490
17491 default:
17492 break;
17493 }
17494 default:
17495 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17496 cp_parser_skip_to_end_of_block_or_statement (parser);
17497 }
17498
17499 return error_mark_node;
17500 }
17501
17502 /* Parse an Objective-C message expression.
17503
17504 objc-message-expression:
17505 [ objc-message-receiver objc-message-args ]
17506
17507 Returns a representation of an Objective-C message. */
17508
17509 static tree
17510 cp_parser_objc_message_expression (cp_parser* parser)
17511 {
17512 tree receiver, messageargs;
17513
17514 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17515 receiver = cp_parser_objc_message_receiver (parser);
17516 messageargs = cp_parser_objc_message_args (parser);
17517 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17518
17519 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17520 }
17521
17522 /* Parse an objc-message-receiver.
17523
17524 objc-message-receiver:
17525 expression
17526 simple-type-specifier
17527
17528 Returns a representation of the type or expression. */
17529
17530 static tree
17531 cp_parser_objc_message_receiver (cp_parser* parser)
17532 {
17533 tree rcv;
17534
17535 /* An Objective-C message receiver may be either (1) a type
17536 or (2) an expression. */
17537 cp_parser_parse_tentatively (parser);
17538 rcv = cp_parser_expression (parser, false);
17539
17540 if (cp_parser_parse_definitely (parser))
17541 return rcv;
17542
17543 rcv = cp_parser_simple_type_specifier (parser,
17544 /*decl_specs=*/NULL,
17545 CP_PARSER_FLAGS_NONE);
17546
17547 return objc_get_class_reference (rcv);
17548 }
17549
17550 /* Parse the arguments and selectors comprising an Objective-C message.
17551
17552 objc-message-args:
17553 objc-selector
17554 objc-selector-args
17555 objc-selector-args , objc-comma-args
17556
17557 objc-selector-args:
17558 objc-selector [opt] : assignment-expression
17559 objc-selector-args objc-selector [opt] : assignment-expression
17560
17561 objc-comma-args:
17562 assignment-expression
17563 objc-comma-args , assignment-expression
17564
17565 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17566 selector arguments and TREE_VALUE containing a list of comma
17567 arguments. */
17568
17569 static tree
17570 cp_parser_objc_message_args (cp_parser* parser)
17571 {
17572 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17573 bool maybe_unary_selector_p = true;
17574 cp_token *token = cp_lexer_peek_token (parser->lexer);
17575
17576 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17577 {
17578 tree selector = NULL_TREE, arg;
17579
17580 if (token->type != CPP_COLON)
17581 selector = cp_parser_objc_selector (parser);
17582
17583 /* Detect if we have a unary selector. */
17584 if (maybe_unary_selector_p
17585 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17586 return build_tree_list (selector, NULL_TREE);
17587
17588 maybe_unary_selector_p = false;
17589 cp_parser_require (parser, CPP_COLON, "`:'");
17590 arg = cp_parser_assignment_expression (parser, false);
17591
17592 sel_args
17593 = chainon (sel_args,
17594 build_tree_list (selector, arg));
17595
17596 token = cp_lexer_peek_token (parser->lexer);
17597 }
17598
17599 /* Handle non-selector arguments, if any. */
17600 while (token->type == CPP_COMMA)
17601 {
17602 tree arg;
17603
17604 cp_lexer_consume_token (parser->lexer);
17605 arg = cp_parser_assignment_expression (parser, false);
17606
17607 addl_args
17608 = chainon (addl_args,
17609 build_tree_list (NULL_TREE, arg));
17610
17611 token = cp_lexer_peek_token (parser->lexer);
17612 }
17613
17614 return build_tree_list (sel_args, addl_args);
17615 }
17616
17617 /* Parse an Objective-C encode expression.
17618
17619 objc-encode-expression:
17620 @encode objc-typename
17621
17622 Returns an encoded representation of the type argument. */
17623
17624 static tree
17625 cp_parser_objc_encode_expression (cp_parser* parser)
17626 {
17627 tree type;
17628
17629 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17630 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17631 type = complete_type (cp_parser_type_id (parser));
17632 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17633
17634 if (!type)
17635 {
17636 error ("%<@encode%> must specify a type as an argument");
17637 return error_mark_node;
17638 }
17639
17640 return objc_build_encode_expr (type);
17641 }
17642
17643 /* Parse an Objective-C @defs expression. */
17644
17645 static tree
17646 cp_parser_objc_defs_expression (cp_parser *parser)
17647 {
17648 tree name;
17649
17650 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17651 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17652 name = cp_parser_identifier (parser);
17653 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17654
17655 return objc_get_class_ivars (name);
17656 }
17657
17658 /* Parse an Objective-C protocol expression.
17659
17660 objc-protocol-expression:
17661 @protocol ( identifier )
17662
17663 Returns a representation of the protocol expression. */
17664
17665 static tree
17666 cp_parser_objc_protocol_expression (cp_parser* parser)
17667 {
17668 tree proto;
17669
17670 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17671 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17672 proto = cp_parser_identifier (parser);
17673 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17674
17675 return objc_build_protocol_expr (proto);
17676 }
17677
17678 /* Parse an Objective-C selector expression.
17679
17680 objc-selector-expression:
17681 @selector ( objc-method-signature )
17682
17683 objc-method-signature:
17684 objc-selector
17685 objc-selector-seq
17686
17687 objc-selector-seq:
17688 objc-selector :
17689 objc-selector-seq objc-selector :
17690
17691 Returns a representation of the method selector. */
17692
17693 static tree
17694 cp_parser_objc_selector_expression (cp_parser* parser)
17695 {
17696 tree sel_seq = NULL_TREE;
17697 bool maybe_unary_selector_p = true;
17698 cp_token *token;
17699
17700 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17701 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17702 token = cp_lexer_peek_token (parser->lexer);
17703
17704 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17705 || token->type == CPP_SCOPE)
17706 {
17707 tree selector = NULL_TREE;
17708
17709 if (token->type != CPP_COLON
17710 || token->type == CPP_SCOPE)
17711 selector = cp_parser_objc_selector (parser);
17712
17713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17714 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17715 {
17716 /* Detect if we have a unary selector. */
17717 if (maybe_unary_selector_p)
17718 {
17719 sel_seq = selector;
17720 goto finish_selector;
17721 }
17722 else
17723 {
17724 cp_parser_error (parser, "expected %<:%>");
17725 }
17726 }
17727 maybe_unary_selector_p = false;
17728 token = cp_lexer_consume_token (parser->lexer);
17729
17730 if (token->type == CPP_SCOPE)
17731 {
17732 sel_seq
17733 = chainon (sel_seq,
17734 build_tree_list (selector, NULL_TREE));
17735 sel_seq
17736 = chainon (sel_seq,
17737 build_tree_list (NULL_TREE, NULL_TREE));
17738 }
17739 else
17740 sel_seq
17741 = chainon (sel_seq,
17742 build_tree_list (selector, NULL_TREE));
17743
17744 token = cp_lexer_peek_token (parser->lexer);
17745 }
17746
17747 finish_selector:
17748 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17749
17750 return objc_build_selector_expr (sel_seq);
17751 }
17752
17753 /* Parse a list of identifiers.
17754
17755 objc-identifier-list:
17756 identifier
17757 objc-identifier-list , identifier
17758
17759 Returns a TREE_LIST of identifier nodes. */
17760
17761 static tree
17762 cp_parser_objc_identifier_list (cp_parser* parser)
17763 {
17764 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17765 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17766
17767 while (sep->type == CPP_COMMA)
17768 {
17769 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17770 list = chainon (list,
17771 build_tree_list (NULL_TREE,
17772 cp_parser_identifier (parser)));
17773 sep = cp_lexer_peek_token (parser->lexer);
17774 }
17775
17776 return list;
17777 }
17778
17779 /* Parse an Objective-C alias declaration.
17780
17781 objc-alias-declaration:
17782 @compatibility_alias identifier identifier ;
17783
17784 This function registers the alias mapping with the Objective-C front end.
17785 It returns nothing. */
17786
17787 static void
17788 cp_parser_objc_alias_declaration (cp_parser* parser)
17789 {
17790 tree alias, orig;
17791
17792 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17793 alias = cp_parser_identifier (parser);
17794 orig = cp_parser_identifier (parser);
17795 objc_declare_alias (alias, orig);
17796 cp_parser_consume_semicolon_at_end_of_statement (parser);
17797 }
17798
17799 /* Parse an Objective-C class forward-declaration.
17800
17801 objc-class-declaration:
17802 @class objc-identifier-list ;
17803
17804 The function registers the forward declarations with the Objective-C
17805 front end. It returns nothing. */
17806
17807 static void
17808 cp_parser_objc_class_declaration (cp_parser* parser)
17809 {
17810 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17811 objc_declare_class (cp_parser_objc_identifier_list (parser));
17812 cp_parser_consume_semicolon_at_end_of_statement (parser);
17813 }
17814
17815 /* Parse a list of Objective-C protocol references.
17816
17817 objc-protocol-refs-opt:
17818 objc-protocol-refs [opt]
17819
17820 objc-protocol-refs:
17821 < objc-identifier-list >
17822
17823 Returns a TREE_LIST of identifiers, if any. */
17824
17825 static tree
17826 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17827 {
17828 tree protorefs = NULL_TREE;
17829
17830 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17831 {
17832 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17833 protorefs = cp_parser_objc_identifier_list (parser);
17834 cp_parser_require (parser, CPP_GREATER, "`>'");
17835 }
17836
17837 return protorefs;
17838 }
17839
17840 /* Parse a Objective-C visibility specification. */
17841
17842 static void
17843 cp_parser_objc_visibility_spec (cp_parser* parser)
17844 {
17845 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17846
17847 switch (vis->keyword)
17848 {
17849 case RID_AT_PRIVATE:
17850 objc_set_visibility (2);
17851 break;
17852 case RID_AT_PROTECTED:
17853 objc_set_visibility (0);
17854 break;
17855 case RID_AT_PUBLIC:
17856 objc_set_visibility (1);
17857 break;
17858 default:
17859 return;
17860 }
17861
17862 /* Eat '@private'/'@protected'/'@public'. */
17863 cp_lexer_consume_token (parser->lexer);
17864 }
17865
17866 /* Parse an Objective-C method type. */
17867
17868 static void
17869 cp_parser_objc_method_type (cp_parser* parser)
17870 {
17871 objc_set_method_type
17872 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17873 ? PLUS_EXPR
17874 : MINUS_EXPR);
17875 }
17876
17877 /* Parse an Objective-C protocol qualifier. */
17878
17879 static tree
17880 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17881 {
17882 tree quals = NULL_TREE, node;
17883 cp_token *token = cp_lexer_peek_token (parser->lexer);
17884
17885 node = token->u.value;
17886
17887 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17888 && (node == ridpointers [(int) RID_IN]
17889 || node == ridpointers [(int) RID_OUT]
17890 || node == ridpointers [(int) RID_INOUT]
17891 || node == ridpointers [(int) RID_BYCOPY]
17892 || node == ridpointers [(int) RID_BYREF]
17893 || node == ridpointers [(int) RID_ONEWAY]))
17894 {
17895 quals = tree_cons (NULL_TREE, node, quals);
17896 cp_lexer_consume_token (parser->lexer);
17897 token = cp_lexer_peek_token (parser->lexer);
17898 node = token->u.value;
17899 }
17900
17901 return quals;
17902 }
17903
17904 /* Parse an Objective-C typename. */
17905
17906 static tree
17907 cp_parser_objc_typename (cp_parser* parser)
17908 {
17909 tree typename = NULL_TREE;
17910
17911 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17912 {
17913 tree proto_quals, cp_type = NULL_TREE;
17914
17915 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17916 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17917
17918 /* An ObjC type name may consist of just protocol qualifiers, in which
17919 case the type shall default to 'id'. */
17920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17921 cp_type = cp_parser_type_id (parser);
17922
17923 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17924 typename = build_tree_list (proto_quals, cp_type);
17925 }
17926
17927 return typename;
17928 }
17929
17930 /* Check to see if TYPE refers to an Objective-C selector name. */
17931
17932 static bool
17933 cp_parser_objc_selector_p (enum cpp_ttype type)
17934 {
17935 return (type == CPP_NAME || type == CPP_KEYWORD
17936 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17937 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17938 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17939 || type == CPP_XOR || type == CPP_XOR_EQ);
17940 }
17941
17942 /* Parse an Objective-C selector. */
17943
17944 static tree
17945 cp_parser_objc_selector (cp_parser* parser)
17946 {
17947 cp_token *token = cp_lexer_consume_token (parser->lexer);
17948
17949 if (!cp_parser_objc_selector_p (token->type))
17950 {
17951 error ("invalid Objective-C++ selector name");
17952 return error_mark_node;
17953 }
17954
17955 /* C++ operator names are allowed to appear in ObjC selectors. */
17956 switch (token->type)
17957 {
17958 case CPP_AND_AND: return get_identifier ("and");
17959 case CPP_AND_EQ: return get_identifier ("and_eq");
17960 case CPP_AND: return get_identifier ("bitand");
17961 case CPP_OR: return get_identifier ("bitor");
17962 case CPP_COMPL: return get_identifier ("compl");
17963 case CPP_NOT: return get_identifier ("not");
17964 case CPP_NOT_EQ: return get_identifier ("not_eq");
17965 case CPP_OR_OR: return get_identifier ("or");
17966 case CPP_OR_EQ: return get_identifier ("or_eq");
17967 case CPP_XOR: return get_identifier ("xor");
17968 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17969 default: return token->u.value;
17970 }
17971 }
17972
17973 /* Parse an Objective-C params list. */
17974
17975 static tree
17976 cp_parser_objc_method_keyword_params (cp_parser* parser)
17977 {
17978 tree params = NULL_TREE;
17979 bool maybe_unary_selector_p = true;
17980 cp_token *token = cp_lexer_peek_token (parser->lexer);
17981
17982 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17983 {
17984 tree selector = NULL_TREE, typename, identifier;
17985
17986 if (token->type != CPP_COLON)
17987 selector = cp_parser_objc_selector (parser);
17988
17989 /* Detect if we have a unary selector. */
17990 if (maybe_unary_selector_p
17991 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17992 return selector;
17993
17994 maybe_unary_selector_p = false;
17995 cp_parser_require (parser, CPP_COLON, "`:'");
17996 typename = cp_parser_objc_typename (parser);
17997 identifier = cp_parser_identifier (parser);
17998
17999 params
18000 = chainon (params,
18001 objc_build_keyword_decl (selector,
18002 typename,
18003 identifier));
18004
18005 token = cp_lexer_peek_token (parser->lexer);
18006 }
18007
18008 return params;
18009 }
18010
18011 /* Parse the non-keyword Objective-C params. */
18012
18013 static tree
18014 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18015 {
18016 tree params = make_node (TREE_LIST);
18017 cp_token *token = cp_lexer_peek_token (parser->lexer);
18018 *ellipsisp = false; /* Initially, assume no ellipsis. */
18019
18020 while (token->type == CPP_COMMA)
18021 {
18022 cp_parameter_declarator *parmdecl;
18023 tree parm;
18024
18025 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18026 token = cp_lexer_peek_token (parser->lexer);
18027
18028 if (token->type == CPP_ELLIPSIS)
18029 {
18030 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18031 *ellipsisp = true;
18032 break;
18033 }
18034
18035 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18036 parm = grokdeclarator (parmdecl->declarator,
18037 &parmdecl->decl_specifiers,
18038 PARM, /*initialized=*/0,
18039 /*attrlist=*/NULL);
18040
18041 chainon (params, build_tree_list (NULL_TREE, parm));
18042 token = cp_lexer_peek_token (parser->lexer);
18043 }
18044
18045 return params;
18046 }
18047
18048 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18049
18050 static void
18051 cp_parser_objc_interstitial_code (cp_parser* parser)
18052 {
18053 cp_token *token = cp_lexer_peek_token (parser->lexer);
18054
18055 /* If the next token is `extern' and the following token is a string
18056 literal, then we have a linkage specification. */
18057 if (token->keyword == RID_EXTERN
18058 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18059 cp_parser_linkage_specification (parser);
18060 /* Handle #pragma, if any. */
18061 else if (token->type == CPP_PRAGMA)
18062 cp_parser_pragma (parser, pragma_external);
18063 /* Allow stray semicolons. */
18064 else if (token->type == CPP_SEMICOLON)
18065 cp_lexer_consume_token (parser->lexer);
18066 /* Finally, try to parse a block-declaration, or a function-definition. */
18067 else
18068 cp_parser_block_declaration (parser, /*statement_p=*/false);
18069 }
18070
18071 /* Parse a method signature. */
18072
18073 static tree
18074 cp_parser_objc_method_signature (cp_parser* parser)
18075 {
18076 tree rettype, kwdparms, optparms;
18077 bool ellipsis = false;
18078
18079 cp_parser_objc_method_type (parser);
18080 rettype = cp_parser_objc_typename (parser);
18081 kwdparms = cp_parser_objc_method_keyword_params (parser);
18082 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18083
18084 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18085 }
18086
18087 /* Pars an Objective-C method prototype list. */
18088
18089 static void
18090 cp_parser_objc_method_prototype_list (cp_parser* parser)
18091 {
18092 cp_token *token = cp_lexer_peek_token (parser->lexer);
18093
18094 while (token->keyword != RID_AT_END)
18095 {
18096 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18097 {
18098 objc_add_method_declaration
18099 (cp_parser_objc_method_signature (parser));
18100 cp_parser_consume_semicolon_at_end_of_statement (parser);
18101 }
18102 else
18103 /* Allow for interspersed non-ObjC++ code. */
18104 cp_parser_objc_interstitial_code (parser);
18105
18106 token = cp_lexer_peek_token (parser->lexer);
18107 }
18108
18109 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18110 objc_finish_interface ();
18111 }
18112
18113 /* Parse an Objective-C method definition list. */
18114
18115 static void
18116 cp_parser_objc_method_definition_list (cp_parser* parser)
18117 {
18118 cp_token *token = cp_lexer_peek_token (parser->lexer);
18119
18120 while (token->keyword != RID_AT_END)
18121 {
18122 tree meth;
18123
18124 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18125 {
18126 push_deferring_access_checks (dk_deferred);
18127 objc_start_method_definition
18128 (cp_parser_objc_method_signature (parser));
18129
18130 /* For historical reasons, we accept an optional semicolon. */
18131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18132 cp_lexer_consume_token (parser->lexer);
18133
18134 perform_deferred_access_checks ();
18135 stop_deferring_access_checks ();
18136 meth = cp_parser_function_definition_after_declarator (parser,
18137 false);
18138 pop_deferring_access_checks ();
18139 objc_finish_method_definition (meth);
18140 }
18141 else
18142 /* Allow for interspersed non-ObjC++ code. */
18143 cp_parser_objc_interstitial_code (parser);
18144
18145 token = cp_lexer_peek_token (parser->lexer);
18146 }
18147
18148 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18149 objc_finish_implementation ();
18150 }
18151
18152 /* Parse Objective-C ivars. */
18153
18154 static void
18155 cp_parser_objc_class_ivars (cp_parser* parser)
18156 {
18157 cp_token *token = cp_lexer_peek_token (parser->lexer);
18158
18159 if (token->type != CPP_OPEN_BRACE)
18160 return; /* No ivars specified. */
18161
18162 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18163 token = cp_lexer_peek_token (parser->lexer);
18164
18165 while (token->type != CPP_CLOSE_BRACE)
18166 {
18167 cp_decl_specifier_seq declspecs;
18168 int decl_class_or_enum_p;
18169 tree prefix_attributes;
18170
18171 cp_parser_objc_visibility_spec (parser);
18172
18173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18174 break;
18175
18176 cp_parser_decl_specifier_seq (parser,
18177 CP_PARSER_FLAGS_OPTIONAL,
18178 &declspecs,
18179 &decl_class_or_enum_p);
18180 prefix_attributes = declspecs.attributes;
18181 declspecs.attributes = NULL_TREE;
18182
18183 /* Keep going until we hit the `;' at the end of the
18184 declaration. */
18185 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18186 {
18187 tree width = NULL_TREE, attributes, first_attribute, decl;
18188 cp_declarator *declarator = NULL;
18189 int ctor_dtor_or_conv_p;
18190
18191 /* Check for a (possibly unnamed) bitfield declaration. */
18192 token = cp_lexer_peek_token (parser->lexer);
18193 if (token->type == CPP_COLON)
18194 goto eat_colon;
18195
18196 if (token->type == CPP_NAME
18197 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18198 == CPP_COLON))
18199 {
18200 /* Get the name of the bitfield. */
18201 declarator = make_id_declarator (NULL_TREE,
18202 cp_parser_identifier (parser),
18203 sfk_none);
18204
18205 eat_colon:
18206 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18207 /* Get the width of the bitfield. */
18208 width
18209 = cp_parser_constant_expression (parser,
18210 /*allow_non_constant=*/false,
18211 NULL);
18212 }
18213 else
18214 {
18215 /* Parse the declarator. */
18216 declarator
18217 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18218 &ctor_dtor_or_conv_p,
18219 /*parenthesized_p=*/NULL,
18220 /*member_p=*/false);
18221 }
18222
18223 /* Look for attributes that apply to the ivar. */
18224 attributes = cp_parser_attributes_opt (parser);
18225 /* Remember which attributes are prefix attributes and
18226 which are not. */
18227 first_attribute = attributes;
18228 /* Combine the attributes. */
18229 attributes = chainon (prefix_attributes, attributes);
18230
18231 if (width)
18232 {
18233 /* Create the bitfield declaration. */
18234 decl = grokbitfield (declarator, &declspecs, width);
18235 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18236 }
18237 else
18238 decl = grokfield (declarator, &declspecs,
18239 NULL_TREE, /*init_const_expr_p=*/false,
18240 NULL_TREE, attributes);
18241
18242 /* Add the instance variable. */
18243 objc_add_instance_variable (decl);
18244
18245 /* Reset PREFIX_ATTRIBUTES. */
18246 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18247 attributes = TREE_CHAIN (attributes);
18248 if (attributes)
18249 TREE_CHAIN (attributes) = NULL_TREE;
18250
18251 token = cp_lexer_peek_token (parser->lexer);
18252
18253 if (token->type == CPP_COMMA)
18254 {
18255 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18256 continue;
18257 }
18258 break;
18259 }
18260
18261 cp_parser_consume_semicolon_at_end_of_statement (parser);
18262 token = cp_lexer_peek_token (parser->lexer);
18263 }
18264
18265 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18266 /* For historical reasons, we accept an optional semicolon. */
18267 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18268 cp_lexer_consume_token (parser->lexer);
18269 }
18270
18271 /* Parse an Objective-C protocol declaration. */
18272
18273 static void
18274 cp_parser_objc_protocol_declaration (cp_parser* parser)
18275 {
18276 tree proto, protorefs;
18277 cp_token *tok;
18278
18279 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18281 {
18282 error ("identifier expected after %<@protocol%>");
18283 goto finish;
18284 }
18285
18286 /* See if we have a forward declaration or a definition. */
18287 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18288
18289 /* Try a forward declaration first. */
18290 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18291 {
18292 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18293 finish:
18294 cp_parser_consume_semicolon_at_end_of_statement (parser);
18295 }
18296
18297 /* Ok, we got a full-fledged definition (or at least should). */
18298 else
18299 {
18300 proto = cp_parser_identifier (parser);
18301 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18302 objc_start_protocol (proto, protorefs);
18303 cp_parser_objc_method_prototype_list (parser);
18304 }
18305 }
18306
18307 /* Parse an Objective-C superclass or category. */
18308
18309 static void
18310 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18311 tree *categ)
18312 {
18313 cp_token *next = cp_lexer_peek_token (parser->lexer);
18314
18315 *super = *categ = NULL_TREE;
18316 if (next->type == CPP_COLON)
18317 {
18318 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18319 *super = cp_parser_identifier (parser);
18320 }
18321 else if (next->type == CPP_OPEN_PAREN)
18322 {
18323 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18324 *categ = cp_parser_identifier (parser);
18325 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18326 }
18327 }
18328
18329 /* Parse an Objective-C class interface. */
18330
18331 static void
18332 cp_parser_objc_class_interface (cp_parser* parser)
18333 {
18334 tree name, super, categ, protos;
18335
18336 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18337 name = cp_parser_identifier (parser);
18338 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18339 protos = cp_parser_objc_protocol_refs_opt (parser);
18340
18341 /* We have either a class or a category on our hands. */
18342 if (categ)
18343 objc_start_category_interface (name, categ, protos);
18344 else
18345 {
18346 objc_start_class_interface (name, super, protos);
18347 /* Handle instance variable declarations, if any. */
18348 cp_parser_objc_class_ivars (parser);
18349 objc_continue_interface ();
18350 }
18351
18352 cp_parser_objc_method_prototype_list (parser);
18353 }
18354
18355 /* Parse an Objective-C class implementation. */
18356
18357 static void
18358 cp_parser_objc_class_implementation (cp_parser* parser)
18359 {
18360 tree name, super, categ;
18361
18362 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18363 name = cp_parser_identifier (parser);
18364 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18365
18366 /* We have either a class or a category on our hands. */
18367 if (categ)
18368 objc_start_category_implementation (name, categ);
18369 else
18370 {
18371 objc_start_class_implementation (name, super);
18372 /* Handle instance variable declarations, if any. */
18373 cp_parser_objc_class_ivars (parser);
18374 objc_continue_implementation ();
18375 }
18376
18377 cp_parser_objc_method_definition_list (parser);
18378 }
18379
18380 /* Consume the @end token and finish off the implementation. */
18381
18382 static void
18383 cp_parser_objc_end_implementation (cp_parser* parser)
18384 {
18385 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18386 objc_finish_implementation ();
18387 }
18388
18389 /* Parse an Objective-C declaration. */
18390
18391 static void
18392 cp_parser_objc_declaration (cp_parser* parser)
18393 {
18394 /* Try to figure out what kind of declaration is present. */
18395 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18396
18397 switch (kwd->keyword)
18398 {
18399 case RID_AT_ALIAS:
18400 cp_parser_objc_alias_declaration (parser);
18401 break;
18402 case RID_AT_CLASS:
18403 cp_parser_objc_class_declaration (parser);
18404 break;
18405 case RID_AT_PROTOCOL:
18406 cp_parser_objc_protocol_declaration (parser);
18407 break;
18408 case RID_AT_INTERFACE:
18409 cp_parser_objc_class_interface (parser);
18410 break;
18411 case RID_AT_IMPLEMENTATION:
18412 cp_parser_objc_class_implementation (parser);
18413 break;
18414 case RID_AT_END:
18415 cp_parser_objc_end_implementation (parser);
18416 break;
18417 default:
18418 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18419 cp_parser_skip_to_end_of_block_or_statement (parser);
18420 }
18421 }
18422
18423 /* Parse an Objective-C try-catch-finally statement.
18424
18425 objc-try-catch-finally-stmt:
18426 @try compound-statement objc-catch-clause-seq [opt]
18427 objc-finally-clause [opt]
18428
18429 objc-catch-clause-seq:
18430 objc-catch-clause objc-catch-clause-seq [opt]
18431
18432 objc-catch-clause:
18433 @catch ( exception-declaration ) compound-statement
18434
18435 objc-finally-clause
18436 @finally compound-statement
18437
18438 Returns NULL_TREE. */
18439
18440 static tree
18441 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18442 location_t location;
18443 tree stmt;
18444
18445 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18446 location = cp_lexer_peek_token (parser->lexer)->location;
18447 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18448 node, lest it get absorbed into the surrounding block. */
18449 stmt = push_stmt_list ();
18450 cp_parser_compound_statement (parser, NULL, false);
18451 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18452
18453 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18454 {
18455 cp_parameter_declarator *parmdecl;
18456 tree parm;
18457
18458 cp_lexer_consume_token (parser->lexer);
18459 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18460 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18461 parm = grokdeclarator (parmdecl->declarator,
18462 &parmdecl->decl_specifiers,
18463 PARM, /*initialized=*/0,
18464 /*attrlist=*/NULL);
18465 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18466 objc_begin_catch_clause (parm);
18467 cp_parser_compound_statement (parser, NULL, false);
18468 objc_finish_catch_clause ();
18469 }
18470
18471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18472 {
18473 cp_lexer_consume_token (parser->lexer);
18474 location = cp_lexer_peek_token (parser->lexer)->location;
18475 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18476 node, lest it get absorbed into the surrounding block. */
18477 stmt = push_stmt_list ();
18478 cp_parser_compound_statement (parser, NULL, false);
18479 objc_build_finally_clause (location, pop_stmt_list (stmt));
18480 }
18481
18482 return objc_finish_try_stmt ();
18483 }
18484
18485 /* Parse an Objective-C synchronized statement.
18486
18487 objc-synchronized-stmt:
18488 @synchronized ( expression ) compound-statement
18489
18490 Returns NULL_TREE. */
18491
18492 static tree
18493 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18494 location_t location;
18495 tree lock, stmt;
18496
18497 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18498
18499 location = cp_lexer_peek_token (parser->lexer)->location;
18500 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18501 lock = cp_parser_expression (parser, false);
18502 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18503
18504 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18505 node, lest it get absorbed into the surrounding block. */
18506 stmt = push_stmt_list ();
18507 cp_parser_compound_statement (parser, NULL, false);
18508
18509 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18510 }
18511
18512 /* Parse an Objective-C throw statement.
18513
18514 objc-throw-stmt:
18515 @throw assignment-expression [opt] ;
18516
18517 Returns a constructed '@throw' statement. */
18518
18519 static tree
18520 cp_parser_objc_throw_statement (cp_parser *parser) {
18521 tree expr = NULL_TREE;
18522
18523 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18524
18525 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18526 expr = cp_parser_assignment_expression (parser, false);
18527
18528 cp_parser_consume_semicolon_at_end_of_statement (parser);
18529
18530 return objc_build_throw_stmt (expr);
18531 }
18532
18533 /* Parse an Objective-C statement. */
18534
18535 static tree
18536 cp_parser_objc_statement (cp_parser * parser) {
18537 /* Try to figure out what kind of declaration is present. */
18538 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18539
18540 switch (kwd->keyword)
18541 {
18542 case RID_AT_TRY:
18543 return cp_parser_objc_try_catch_finally_statement (parser);
18544 case RID_AT_SYNCHRONIZED:
18545 return cp_parser_objc_synchronized_statement (parser);
18546 case RID_AT_THROW:
18547 return cp_parser_objc_throw_statement (parser);
18548 default:
18549 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18550 cp_parser_skip_to_end_of_block_or_statement (parser);
18551 }
18552
18553 return error_mark_node;
18554 }
18555 \f
18556 /* OpenMP 2.5 parsing routines. */
18557
18558 /* Returns name of the next clause.
18559 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18560 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18561 returned and the token is consumed. */
18562
18563 static pragma_omp_clause
18564 cp_parser_omp_clause_name (cp_parser *parser)
18565 {
18566 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18567
18568 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18569 result = PRAGMA_OMP_CLAUSE_IF;
18570 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18571 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18572 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18573 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18574 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18575 {
18576 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18577 const char *p = IDENTIFIER_POINTER (id);
18578
18579 switch (p[0])
18580 {
18581 case 'c':
18582 if (!strcmp ("copyin", p))
18583 result = PRAGMA_OMP_CLAUSE_COPYIN;
18584 else if (!strcmp ("copyprivate", p))
18585 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18586 break;
18587 case 'f':
18588 if (!strcmp ("firstprivate", p))
18589 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18590 break;
18591 case 'l':
18592 if (!strcmp ("lastprivate", p))
18593 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18594 break;
18595 case 'n':
18596 if (!strcmp ("nowait", p))
18597 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18598 else if (!strcmp ("num_threads", p))
18599 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18600 break;
18601 case 'o':
18602 if (!strcmp ("ordered", p))
18603 result = PRAGMA_OMP_CLAUSE_ORDERED;
18604 break;
18605 case 'r':
18606 if (!strcmp ("reduction", p))
18607 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18608 break;
18609 case 's':
18610 if (!strcmp ("schedule", p))
18611 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18612 else if (!strcmp ("shared", p))
18613 result = PRAGMA_OMP_CLAUSE_SHARED;
18614 break;
18615 }
18616 }
18617
18618 if (result != PRAGMA_OMP_CLAUSE_NONE)
18619 cp_lexer_consume_token (parser->lexer);
18620
18621 return result;
18622 }
18623
18624 /* Validate that a clause of the given type does not already exist. */
18625
18626 static void
18627 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18628 {
18629 tree c;
18630
18631 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18632 if (OMP_CLAUSE_CODE (c) == code)
18633 {
18634 error ("too many %qs clauses", name);
18635 break;
18636 }
18637 }
18638
18639 /* OpenMP 2.5:
18640 variable-list:
18641 identifier
18642 variable-list , identifier
18643
18644 In addition, we match a closing parenthesis. An opening parenthesis
18645 will have been consumed by the caller.
18646
18647 If KIND is nonzero, create the appropriate node and install the decl
18648 in OMP_CLAUSE_DECL and add the node to the head of the list.
18649
18650 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18651 return the list created. */
18652
18653 static tree
18654 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18655 tree list)
18656 {
18657 while (1)
18658 {
18659 tree name, decl;
18660
18661 name = cp_parser_id_expression (parser, /*template_p=*/false,
18662 /*check_dependency_p=*/true,
18663 /*template_p=*/NULL,
18664 /*declarator_p=*/false,
18665 /*optional_p=*/false);
18666 if (name == error_mark_node)
18667 goto skip_comma;
18668
18669 decl = cp_parser_lookup_name_simple (parser, name);
18670 if (decl == error_mark_node)
18671 cp_parser_name_lookup_error (parser, name, decl, NULL);
18672 else if (kind != 0)
18673 {
18674 tree u = build_omp_clause (kind);
18675 OMP_CLAUSE_DECL (u) = decl;
18676 OMP_CLAUSE_CHAIN (u) = list;
18677 list = u;
18678 }
18679 else
18680 list = tree_cons (decl, NULL_TREE, list);
18681
18682 get_comma:
18683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18684 break;
18685 cp_lexer_consume_token (parser->lexer);
18686 }
18687
18688 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18689 {
18690 int ending;
18691
18692 /* Try to resync to an unnested comma. Copied from
18693 cp_parser_parenthesized_expression_list. */
18694 skip_comma:
18695 ending = cp_parser_skip_to_closing_parenthesis (parser,
18696 /*recovering=*/true,
18697 /*or_comma=*/true,
18698 /*consume_paren=*/true);
18699 if (ending < 0)
18700 goto get_comma;
18701 }
18702
18703 return list;
18704 }
18705
18706 /* Similarly, but expect leading and trailing parenthesis. This is a very
18707 common case for omp clauses. */
18708
18709 static tree
18710 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18711 {
18712 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18713 return cp_parser_omp_var_list_no_open (parser, kind, list);
18714 return list;
18715 }
18716
18717 /* OpenMP 2.5:
18718 default ( shared | none ) */
18719
18720 static tree
18721 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18722 {
18723 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18724 tree c;
18725
18726 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18727 return list;
18728 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18729 {
18730 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18731 const char *p = IDENTIFIER_POINTER (id);
18732
18733 switch (p[0])
18734 {
18735 case 'n':
18736 if (strcmp ("none", p) != 0)
18737 goto invalid_kind;
18738 kind = OMP_CLAUSE_DEFAULT_NONE;
18739 break;
18740
18741 case 's':
18742 if (strcmp ("shared", p) != 0)
18743 goto invalid_kind;
18744 kind = OMP_CLAUSE_DEFAULT_SHARED;
18745 break;
18746
18747 default:
18748 goto invalid_kind;
18749 }
18750
18751 cp_lexer_consume_token (parser->lexer);
18752 }
18753 else
18754 {
18755 invalid_kind:
18756 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18757 }
18758
18759 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18760 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18761 /*or_comma=*/false,
18762 /*consume_paren=*/true);
18763
18764 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18765 return list;
18766
18767 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18768 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18769 OMP_CLAUSE_CHAIN (c) = list;
18770 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18771
18772 return c;
18773 }
18774
18775 /* OpenMP 2.5:
18776 if ( expression ) */
18777
18778 static tree
18779 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18780 {
18781 tree t, c;
18782
18783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18784 return list;
18785
18786 t = cp_parser_condition (parser);
18787
18788 if (t == error_mark_node
18789 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18790 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18791 /*or_comma=*/false,
18792 /*consume_paren=*/true);
18793
18794 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18795
18796 c = build_omp_clause (OMP_CLAUSE_IF);
18797 OMP_CLAUSE_IF_EXPR (c) = t;
18798 OMP_CLAUSE_CHAIN (c) = list;
18799
18800 return c;
18801 }
18802
18803 /* OpenMP 2.5:
18804 nowait */
18805
18806 static tree
18807 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18808 {
18809 tree c;
18810
18811 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18812
18813 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18814 OMP_CLAUSE_CHAIN (c) = list;
18815 return c;
18816 }
18817
18818 /* OpenMP 2.5:
18819 num_threads ( expression ) */
18820
18821 static tree
18822 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18823 {
18824 tree t, c;
18825
18826 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18827 return list;
18828
18829 t = cp_parser_expression (parser, false);
18830
18831 if (t == error_mark_node
18832 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18833 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18834 /*or_comma=*/false,
18835 /*consume_paren=*/true);
18836
18837 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18838
18839 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18840 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18841 OMP_CLAUSE_CHAIN (c) = list;
18842
18843 return c;
18844 }
18845
18846 /* OpenMP 2.5:
18847 ordered */
18848
18849 static tree
18850 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18851 {
18852 tree c;
18853
18854 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18855
18856 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18857 OMP_CLAUSE_CHAIN (c) = list;
18858 return c;
18859 }
18860
18861 /* OpenMP 2.5:
18862 reduction ( reduction-operator : variable-list )
18863
18864 reduction-operator:
18865 One of: + * - & ^ | && || */
18866
18867 static tree
18868 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18869 {
18870 enum tree_code code;
18871 tree nlist, c;
18872
18873 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18874 return list;
18875
18876 switch (cp_lexer_peek_token (parser->lexer)->type)
18877 {
18878 case CPP_PLUS:
18879 code = PLUS_EXPR;
18880 break;
18881 case CPP_MULT:
18882 code = MULT_EXPR;
18883 break;
18884 case CPP_MINUS:
18885 code = MINUS_EXPR;
18886 break;
18887 case CPP_AND:
18888 code = BIT_AND_EXPR;
18889 break;
18890 case CPP_XOR:
18891 code = BIT_XOR_EXPR;
18892 break;
18893 case CPP_OR:
18894 code = BIT_IOR_EXPR;
18895 break;
18896 case CPP_AND_AND:
18897 code = TRUTH_ANDIF_EXPR;
18898 break;
18899 case CPP_OR_OR:
18900 code = TRUTH_ORIF_EXPR;
18901 break;
18902 default:
18903 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18904 resync_fail:
18905 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18906 /*or_comma=*/false,
18907 /*consume_paren=*/true);
18908 return list;
18909 }
18910 cp_lexer_consume_token (parser->lexer);
18911
18912 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18913 goto resync_fail;
18914
18915 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18916 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18917 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18918
18919 return nlist;
18920 }
18921
18922 /* OpenMP 2.5:
18923 schedule ( schedule-kind )
18924 schedule ( schedule-kind , expression )
18925
18926 schedule-kind:
18927 static | dynamic | guided | runtime */
18928
18929 static tree
18930 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18931 {
18932 tree c, t;
18933
18934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18935 return list;
18936
18937 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18938
18939 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18940 {
18941 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18942 const char *p = IDENTIFIER_POINTER (id);
18943
18944 switch (p[0])
18945 {
18946 case 'd':
18947 if (strcmp ("dynamic", p) != 0)
18948 goto invalid_kind;
18949 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18950 break;
18951
18952 case 'g':
18953 if (strcmp ("guided", p) != 0)
18954 goto invalid_kind;
18955 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18956 break;
18957
18958 case 'r':
18959 if (strcmp ("runtime", p) != 0)
18960 goto invalid_kind;
18961 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18962 break;
18963
18964 default:
18965 goto invalid_kind;
18966 }
18967 }
18968 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18969 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18970 else
18971 goto invalid_kind;
18972 cp_lexer_consume_token (parser->lexer);
18973
18974 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18975 {
18976 cp_lexer_consume_token (parser->lexer);
18977
18978 t = cp_parser_assignment_expression (parser, false);
18979
18980 if (t == error_mark_node)
18981 goto resync_fail;
18982 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18983 error ("schedule %<runtime%> does not take "
18984 "a %<chunk_size%> parameter");
18985 else
18986 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18987
18988 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18989 goto resync_fail;
18990 }
18991 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18992 goto resync_fail;
18993
18994 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18995 OMP_CLAUSE_CHAIN (c) = list;
18996 return c;
18997
18998 invalid_kind:
18999 cp_parser_error (parser, "invalid schedule kind");
19000 resync_fail:
19001 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19002 /*or_comma=*/false,
19003 /*consume_paren=*/true);
19004 return list;
19005 }
19006
19007 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19008 is a bitmask in MASK. Return the list of clauses found; the result
19009 of clause default goes in *pdefault. */
19010
19011 static tree
19012 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19013 const char *where, cp_token *pragma_tok)
19014 {
19015 tree clauses = NULL;
19016
19017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19018 {
19019 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19020 const char *c_name;
19021 tree prev = clauses;
19022
19023 switch (c_kind)
19024 {
19025 case PRAGMA_OMP_CLAUSE_COPYIN:
19026 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19027 c_name = "copyin";
19028 break;
19029 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19030 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19031 clauses);
19032 c_name = "copyprivate";
19033 break;
19034 case PRAGMA_OMP_CLAUSE_DEFAULT:
19035 clauses = cp_parser_omp_clause_default (parser, clauses);
19036 c_name = "default";
19037 break;
19038 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19039 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19040 clauses);
19041 c_name = "firstprivate";
19042 break;
19043 case PRAGMA_OMP_CLAUSE_IF:
19044 clauses = cp_parser_omp_clause_if (parser, clauses);
19045 c_name = "if";
19046 break;
19047 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19048 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19049 clauses);
19050 c_name = "lastprivate";
19051 break;
19052 case PRAGMA_OMP_CLAUSE_NOWAIT:
19053 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19054 c_name = "nowait";
19055 break;
19056 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19057 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19058 c_name = "num_threads";
19059 break;
19060 case PRAGMA_OMP_CLAUSE_ORDERED:
19061 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19062 c_name = "ordered";
19063 break;
19064 case PRAGMA_OMP_CLAUSE_PRIVATE:
19065 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19066 clauses);
19067 c_name = "private";
19068 break;
19069 case PRAGMA_OMP_CLAUSE_REDUCTION:
19070 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19071 c_name = "reduction";
19072 break;
19073 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19074 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19075 c_name = "schedule";
19076 break;
19077 case PRAGMA_OMP_CLAUSE_SHARED:
19078 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19079 clauses);
19080 c_name = "shared";
19081 break;
19082 default:
19083 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19084 goto saw_error;
19085 }
19086
19087 if (((mask >> c_kind) & 1) == 0)
19088 {
19089 /* Remove the invalid clause(s) from the list to avoid
19090 confusing the rest of the compiler. */
19091 clauses = prev;
19092 error ("%qs is not valid for %qs", c_name, where);
19093 }
19094 }
19095 saw_error:
19096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19097 return finish_omp_clauses (clauses);
19098 }
19099
19100 /* OpenMP 2.5:
19101 structured-block:
19102 statement
19103
19104 In practice, we're also interested in adding the statement to an
19105 outer node. So it is convenient if we work around the fact that
19106 cp_parser_statement calls add_stmt. */
19107
19108 static unsigned
19109 cp_parser_begin_omp_structured_block (cp_parser *parser)
19110 {
19111 unsigned save = parser->in_statement;
19112
19113 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19114 This preserves the "not within loop or switch" style error messages
19115 for nonsense cases like
19116 void foo() {
19117 #pragma omp single
19118 break;
19119 }
19120 */
19121 if (parser->in_statement)
19122 parser->in_statement = IN_OMP_BLOCK;
19123
19124 return save;
19125 }
19126
19127 static void
19128 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19129 {
19130 parser->in_statement = save;
19131 }
19132
19133 static tree
19134 cp_parser_omp_structured_block (cp_parser *parser)
19135 {
19136 tree stmt = begin_omp_structured_block ();
19137 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19138
19139 cp_parser_statement (parser, NULL_TREE, false, NULL);
19140
19141 cp_parser_end_omp_structured_block (parser, save);
19142 return finish_omp_structured_block (stmt);
19143 }
19144
19145 /* OpenMP 2.5:
19146 # pragma omp atomic new-line
19147 expression-stmt
19148
19149 expression-stmt:
19150 x binop= expr | x++ | ++x | x-- | --x
19151 binop:
19152 +, *, -, /, &, ^, |, <<, >>
19153
19154 where x is an lvalue expression with scalar type. */
19155
19156 static void
19157 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19158 {
19159 tree lhs, rhs;
19160 enum tree_code code;
19161
19162 cp_parser_require_pragma_eol (parser, pragma_tok);
19163
19164 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19165 /*cast_p=*/false);
19166 switch (TREE_CODE (lhs))
19167 {
19168 case ERROR_MARK:
19169 goto saw_error;
19170
19171 case PREINCREMENT_EXPR:
19172 case POSTINCREMENT_EXPR:
19173 lhs = TREE_OPERAND (lhs, 0);
19174 code = PLUS_EXPR;
19175 rhs = integer_one_node;
19176 break;
19177
19178 case PREDECREMENT_EXPR:
19179 case POSTDECREMENT_EXPR:
19180 lhs = TREE_OPERAND (lhs, 0);
19181 code = MINUS_EXPR;
19182 rhs = integer_one_node;
19183 break;
19184
19185 default:
19186 switch (cp_lexer_peek_token (parser->lexer)->type)
19187 {
19188 case CPP_MULT_EQ:
19189 code = MULT_EXPR;
19190 break;
19191 case CPP_DIV_EQ:
19192 code = TRUNC_DIV_EXPR;
19193 break;
19194 case CPP_PLUS_EQ:
19195 code = PLUS_EXPR;
19196 break;
19197 case CPP_MINUS_EQ:
19198 code = MINUS_EXPR;
19199 break;
19200 case CPP_LSHIFT_EQ:
19201 code = LSHIFT_EXPR;
19202 break;
19203 case CPP_RSHIFT_EQ:
19204 code = RSHIFT_EXPR;
19205 break;
19206 case CPP_AND_EQ:
19207 code = BIT_AND_EXPR;
19208 break;
19209 case CPP_OR_EQ:
19210 code = BIT_IOR_EXPR;
19211 break;
19212 case CPP_XOR_EQ:
19213 code = BIT_XOR_EXPR;
19214 break;
19215 default:
19216 cp_parser_error (parser,
19217 "invalid operator for %<#pragma omp atomic%>");
19218 goto saw_error;
19219 }
19220 cp_lexer_consume_token (parser->lexer);
19221
19222 rhs = cp_parser_expression (parser, false);
19223 if (rhs == error_mark_node)
19224 goto saw_error;
19225 break;
19226 }
19227 finish_omp_atomic (code, lhs, rhs);
19228 cp_parser_consume_semicolon_at_end_of_statement (parser);
19229 return;
19230
19231 saw_error:
19232 cp_parser_skip_to_end_of_block_or_statement (parser);
19233 }
19234
19235
19236 /* OpenMP 2.5:
19237 # pragma omp barrier new-line */
19238
19239 static void
19240 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19241 {
19242 cp_parser_require_pragma_eol (parser, pragma_tok);
19243 finish_omp_barrier ();
19244 }
19245
19246 /* OpenMP 2.5:
19247 # pragma omp critical [(name)] new-line
19248 structured-block */
19249
19250 static tree
19251 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19252 {
19253 tree stmt, name = NULL;
19254
19255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19256 {
19257 cp_lexer_consume_token (parser->lexer);
19258
19259 name = cp_parser_identifier (parser);
19260
19261 if (name == error_mark_node
19262 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19264 /*or_comma=*/false,
19265 /*consume_paren=*/true);
19266 if (name == error_mark_node)
19267 name = NULL;
19268 }
19269 cp_parser_require_pragma_eol (parser, pragma_tok);
19270
19271 stmt = cp_parser_omp_structured_block (parser);
19272 return c_finish_omp_critical (stmt, name);
19273 }
19274
19275 /* OpenMP 2.5:
19276 # pragma omp flush flush-vars[opt] new-line
19277
19278 flush-vars:
19279 ( variable-list ) */
19280
19281 static void
19282 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19283 {
19284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19285 (void) cp_parser_omp_var_list (parser, 0, NULL);
19286 cp_parser_require_pragma_eol (parser, pragma_tok);
19287
19288 finish_omp_flush ();
19289 }
19290
19291 /* Parse the restricted form of the for statment allowed by OpenMP. */
19292
19293 static tree
19294 cp_parser_omp_for_loop (cp_parser *parser)
19295 {
19296 tree init, cond, incr, body, decl, pre_body;
19297 location_t loc;
19298
19299 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19300 {
19301 cp_parser_error (parser, "for statement expected");
19302 return NULL;
19303 }
19304 loc = cp_lexer_consume_token (parser->lexer)->location;
19305 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19306 return NULL;
19307
19308 init = decl = NULL;
19309 pre_body = push_stmt_list ();
19310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19311 {
19312 cp_decl_specifier_seq type_specifiers;
19313
19314 /* First, try to parse as an initialized declaration. See
19315 cp_parser_condition, from whence the bulk of this is copied. */
19316
19317 cp_parser_parse_tentatively (parser);
19318 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19319 &type_specifiers);
19320 if (!cp_parser_error_occurred (parser))
19321 {
19322 tree asm_specification, attributes;
19323 cp_declarator *declarator;
19324
19325 declarator = cp_parser_declarator (parser,
19326 CP_PARSER_DECLARATOR_NAMED,
19327 /*ctor_dtor_or_conv_p=*/NULL,
19328 /*parenthesized_p=*/NULL,
19329 /*member_p=*/false);
19330 attributes = cp_parser_attributes_opt (parser);
19331 asm_specification = cp_parser_asm_specification_opt (parser);
19332
19333 cp_parser_require (parser, CPP_EQ, "`='");
19334 if (cp_parser_parse_definitely (parser))
19335 {
19336 tree pushed_scope;
19337
19338 decl = start_decl (declarator, &type_specifiers,
19339 /*initialized_p=*/false, attributes,
19340 /*prefix_attributes=*/NULL_TREE,
19341 &pushed_scope);
19342
19343 init = cp_parser_assignment_expression (parser, false);
19344
19345 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19346 asm_specification, LOOKUP_ONLYCONVERTING);
19347
19348 if (pushed_scope)
19349 pop_scope (pushed_scope);
19350 }
19351 }
19352 else
19353 cp_parser_abort_tentative_parse (parser);
19354
19355 /* If parsing as an initialized declaration failed, try again as
19356 a simple expression. */
19357 if (decl == NULL)
19358 init = cp_parser_expression (parser, false);
19359 }
19360 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19361 pre_body = pop_stmt_list (pre_body);
19362
19363 cond = NULL;
19364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19365 cond = cp_parser_condition (parser);
19366 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19367
19368 incr = NULL;
19369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19370 incr = cp_parser_expression (parser, false);
19371
19372 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19374 /*or_comma=*/false,
19375 /*consume_paren=*/true);
19376
19377 /* Note that we saved the original contents of this flag when we entered
19378 the structured block, and so we don't need to re-save it here. */
19379 parser->in_statement = IN_OMP_FOR;
19380
19381 /* Note that the grammar doesn't call for a structured block here,
19382 though the loop as a whole is a structured block. */
19383 body = push_stmt_list ();
19384 cp_parser_statement (parser, NULL_TREE, false, NULL);
19385 body = pop_stmt_list (body);
19386
19387 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19388 }
19389
19390 /* OpenMP 2.5:
19391 #pragma omp for for-clause[optseq] new-line
19392 for-loop */
19393
19394 #define OMP_FOR_CLAUSE_MASK \
19395 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19396 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19397 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19398 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19399 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19400 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19401 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19402
19403 static tree
19404 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19405 {
19406 tree clauses, sb, ret;
19407 unsigned int save;
19408
19409 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19410 "#pragma omp for", pragma_tok);
19411
19412 sb = begin_omp_structured_block ();
19413 save = cp_parser_begin_omp_structured_block (parser);
19414
19415 ret = cp_parser_omp_for_loop (parser);
19416 if (ret)
19417 OMP_FOR_CLAUSES (ret) = clauses;
19418
19419 cp_parser_end_omp_structured_block (parser, save);
19420 add_stmt (finish_omp_structured_block (sb));
19421
19422 return ret;
19423 }
19424
19425 /* OpenMP 2.5:
19426 # pragma omp master new-line
19427 structured-block */
19428
19429 static tree
19430 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19431 {
19432 cp_parser_require_pragma_eol (parser, pragma_tok);
19433 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19434 }
19435
19436 /* OpenMP 2.5:
19437 # pragma omp ordered new-line
19438 structured-block */
19439
19440 static tree
19441 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19442 {
19443 cp_parser_require_pragma_eol (parser, pragma_tok);
19444 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19445 }
19446
19447 /* OpenMP 2.5:
19448
19449 section-scope:
19450 { section-sequence }
19451
19452 section-sequence:
19453 section-directive[opt] structured-block
19454 section-sequence section-directive structured-block */
19455
19456 static tree
19457 cp_parser_omp_sections_scope (cp_parser *parser)
19458 {
19459 tree stmt, substmt;
19460 bool error_suppress = false;
19461 cp_token *tok;
19462
19463 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19464 return NULL_TREE;
19465
19466 stmt = push_stmt_list ();
19467
19468 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19469 {
19470 unsigned save;
19471
19472 substmt = begin_omp_structured_block ();
19473 save = cp_parser_begin_omp_structured_block (parser);
19474
19475 while (1)
19476 {
19477 cp_parser_statement (parser, NULL_TREE, false, NULL);
19478
19479 tok = cp_lexer_peek_token (parser->lexer);
19480 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19481 break;
19482 if (tok->type == CPP_CLOSE_BRACE)
19483 break;
19484 if (tok->type == CPP_EOF)
19485 break;
19486 }
19487
19488 cp_parser_end_omp_structured_block (parser, save);
19489 substmt = finish_omp_structured_block (substmt);
19490 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19491 add_stmt (substmt);
19492 }
19493
19494 while (1)
19495 {
19496 tok = cp_lexer_peek_token (parser->lexer);
19497 if (tok->type == CPP_CLOSE_BRACE)
19498 break;
19499 if (tok->type == CPP_EOF)
19500 break;
19501
19502 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19503 {
19504 cp_lexer_consume_token (parser->lexer);
19505 cp_parser_require_pragma_eol (parser, tok);
19506 error_suppress = false;
19507 }
19508 else if (!error_suppress)
19509 {
19510 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19511 error_suppress = true;
19512 }
19513
19514 substmt = cp_parser_omp_structured_block (parser);
19515 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19516 add_stmt (substmt);
19517 }
19518 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19519
19520 substmt = pop_stmt_list (stmt);
19521
19522 stmt = make_node (OMP_SECTIONS);
19523 TREE_TYPE (stmt) = void_type_node;
19524 OMP_SECTIONS_BODY (stmt) = substmt;
19525
19526 add_stmt (stmt);
19527 return stmt;
19528 }
19529
19530 /* OpenMP 2.5:
19531 # pragma omp sections sections-clause[optseq] newline
19532 sections-scope */
19533
19534 #define OMP_SECTIONS_CLAUSE_MASK \
19535 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19536 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19537 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19538 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19539 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19540
19541 static tree
19542 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19543 {
19544 tree clauses, ret;
19545
19546 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19547 "#pragma omp sections", pragma_tok);
19548
19549 ret = cp_parser_omp_sections_scope (parser);
19550 if (ret)
19551 OMP_SECTIONS_CLAUSES (ret) = clauses;
19552
19553 return ret;
19554 }
19555
19556 /* OpenMP 2.5:
19557 # pragma parallel parallel-clause new-line
19558 # pragma parallel for parallel-for-clause new-line
19559 # pragma parallel sections parallel-sections-clause new-line */
19560
19561 #define OMP_PARALLEL_CLAUSE_MASK \
19562 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19563 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19564 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19565 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19566 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19567 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19568 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19569 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19570
19571 static tree
19572 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19573 {
19574 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19575 const char *p_name = "#pragma omp parallel";
19576 tree stmt, clauses, par_clause, ws_clause, block;
19577 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19578 unsigned int save;
19579
19580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19581 {
19582 cp_lexer_consume_token (parser->lexer);
19583 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19584 p_name = "#pragma omp parallel for";
19585 mask |= OMP_FOR_CLAUSE_MASK;
19586 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19587 }
19588 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19589 {
19590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19591 const char *p = IDENTIFIER_POINTER (id);
19592 if (strcmp (p, "sections") == 0)
19593 {
19594 cp_lexer_consume_token (parser->lexer);
19595 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19596 p_name = "#pragma omp parallel sections";
19597 mask |= OMP_SECTIONS_CLAUSE_MASK;
19598 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19599 }
19600 }
19601
19602 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19603 block = begin_omp_parallel ();
19604 save = cp_parser_begin_omp_structured_block (parser);
19605
19606 switch (p_kind)
19607 {
19608 case PRAGMA_OMP_PARALLEL:
19609 cp_parser_already_scoped_statement (parser);
19610 par_clause = clauses;
19611 break;
19612
19613 case PRAGMA_OMP_PARALLEL_FOR:
19614 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19615 stmt = cp_parser_omp_for_loop (parser);
19616 if (stmt)
19617 OMP_FOR_CLAUSES (stmt) = ws_clause;
19618 break;
19619
19620 case PRAGMA_OMP_PARALLEL_SECTIONS:
19621 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19622 stmt = cp_parser_omp_sections_scope (parser);
19623 if (stmt)
19624 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19625 break;
19626
19627 default:
19628 gcc_unreachable ();
19629 }
19630
19631 cp_parser_end_omp_structured_block (parser, save);
19632 stmt = finish_omp_parallel (par_clause, block);
19633 if (p_kind != PRAGMA_OMP_PARALLEL)
19634 OMP_PARALLEL_COMBINED (stmt) = 1;
19635 return stmt;
19636 }
19637
19638 /* OpenMP 2.5:
19639 # pragma omp single single-clause[optseq] new-line
19640 structured-block */
19641
19642 #define OMP_SINGLE_CLAUSE_MASK \
19643 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19644 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19645 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19646 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19647
19648 static tree
19649 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19650 {
19651 tree stmt = make_node (OMP_SINGLE);
19652 TREE_TYPE (stmt) = void_type_node;
19653
19654 OMP_SINGLE_CLAUSES (stmt)
19655 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19656 "#pragma omp single", pragma_tok);
19657 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19658
19659 return add_stmt (stmt);
19660 }
19661
19662 /* OpenMP 2.5:
19663 # pragma omp threadprivate (variable-list) */
19664
19665 static void
19666 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19667 {
19668 tree vars;
19669
19670 vars = cp_parser_omp_var_list (parser, 0, NULL);
19671 cp_parser_require_pragma_eol (parser, pragma_tok);
19672
19673 finish_omp_threadprivate (vars);
19674 }
19675
19676 /* Main entry point to OpenMP statement pragmas. */
19677
19678 static void
19679 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19680 {
19681 tree stmt;
19682
19683 switch (pragma_tok->pragma_kind)
19684 {
19685 case PRAGMA_OMP_ATOMIC:
19686 cp_parser_omp_atomic (parser, pragma_tok);
19687 return;
19688 case PRAGMA_OMP_CRITICAL:
19689 stmt = cp_parser_omp_critical (parser, pragma_tok);
19690 break;
19691 case PRAGMA_OMP_FOR:
19692 stmt = cp_parser_omp_for (parser, pragma_tok);
19693 break;
19694 case PRAGMA_OMP_MASTER:
19695 stmt = cp_parser_omp_master (parser, pragma_tok);
19696 break;
19697 case PRAGMA_OMP_ORDERED:
19698 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19699 break;
19700 case PRAGMA_OMP_PARALLEL:
19701 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19702 break;
19703 case PRAGMA_OMP_SECTIONS:
19704 stmt = cp_parser_omp_sections (parser, pragma_tok);
19705 break;
19706 case PRAGMA_OMP_SINGLE:
19707 stmt = cp_parser_omp_single (parser, pragma_tok);
19708 break;
19709 default:
19710 gcc_unreachable ();
19711 }
19712
19713 if (stmt)
19714 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19715 }
19716 \f
19717 /* The parser. */
19718
19719 static GTY (()) cp_parser *the_parser;
19720
19721 \f
19722 /* Special handling for the first token or line in the file. The first
19723 thing in the file might be #pragma GCC pch_preprocess, which loads a
19724 PCH file, which is a GC collection point. So we need to handle this
19725 first pragma without benefit of an existing lexer structure.
19726
19727 Always returns one token to the caller in *FIRST_TOKEN. This is
19728 either the true first token of the file, or the first token after
19729 the initial pragma. */
19730
19731 static void
19732 cp_parser_initial_pragma (cp_token *first_token)
19733 {
19734 tree name = NULL;
19735
19736 cp_lexer_get_preprocessor_token (NULL, first_token);
19737 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19738 return;
19739
19740 cp_lexer_get_preprocessor_token (NULL, first_token);
19741 if (first_token->type == CPP_STRING)
19742 {
19743 name = first_token->u.value;
19744
19745 cp_lexer_get_preprocessor_token (NULL, first_token);
19746 if (first_token->type != CPP_PRAGMA_EOL)
19747 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19748 }
19749 else
19750 error ("expected string literal");
19751
19752 /* Skip to the end of the pragma. */
19753 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19754 cp_lexer_get_preprocessor_token (NULL, first_token);
19755
19756 /* Now actually load the PCH file. */
19757 if (name)
19758 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19759
19760 /* Read one more token to return to our caller. We have to do this
19761 after reading the PCH file in, since its pointers have to be
19762 live. */
19763 cp_lexer_get_preprocessor_token (NULL, first_token);
19764 }
19765
19766 /* Normal parsing of a pragma token. Here we can (and must) use the
19767 regular lexer. */
19768
19769 static bool
19770 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19771 {
19772 cp_token *pragma_tok;
19773 unsigned int id;
19774
19775 pragma_tok = cp_lexer_consume_token (parser->lexer);
19776 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19777 parser->lexer->in_pragma = true;
19778
19779 id = pragma_tok->pragma_kind;
19780 switch (id)
19781 {
19782 case PRAGMA_GCC_PCH_PREPROCESS:
19783 error ("%<#pragma GCC pch_preprocess%> must be first");
19784 break;
19785
19786 case PRAGMA_OMP_BARRIER:
19787 switch (context)
19788 {
19789 case pragma_compound:
19790 cp_parser_omp_barrier (parser, pragma_tok);
19791 return false;
19792 case pragma_stmt:
19793 error ("%<#pragma omp barrier%> may only be "
19794 "used in compound statements");
19795 break;
19796 default:
19797 goto bad_stmt;
19798 }
19799 break;
19800
19801 case PRAGMA_OMP_FLUSH:
19802 switch (context)
19803 {
19804 case pragma_compound:
19805 cp_parser_omp_flush (parser, pragma_tok);
19806 return false;
19807 case pragma_stmt:
19808 error ("%<#pragma omp flush%> may only be "
19809 "used in compound statements");
19810 break;
19811 default:
19812 goto bad_stmt;
19813 }
19814 break;
19815
19816 case PRAGMA_OMP_THREADPRIVATE:
19817 cp_parser_omp_threadprivate (parser, pragma_tok);
19818 return false;
19819
19820 case PRAGMA_OMP_ATOMIC:
19821 case PRAGMA_OMP_CRITICAL:
19822 case PRAGMA_OMP_FOR:
19823 case PRAGMA_OMP_MASTER:
19824 case PRAGMA_OMP_ORDERED:
19825 case PRAGMA_OMP_PARALLEL:
19826 case PRAGMA_OMP_SECTIONS:
19827 case PRAGMA_OMP_SINGLE:
19828 if (context == pragma_external)
19829 goto bad_stmt;
19830 cp_parser_omp_construct (parser, pragma_tok);
19831 return true;
19832
19833 case PRAGMA_OMP_SECTION:
19834 error ("%<#pragma omp section%> may only be used in "
19835 "%<#pragma omp sections%> construct");
19836 break;
19837
19838 default:
19839 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19840 c_invoke_pragma_handler (id);
19841 break;
19842
19843 bad_stmt:
19844 cp_parser_error (parser, "expected declaration specifiers");
19845 break;
19846 }
19847
19848 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19849 return false;
19850 }
19851
19852 /* The interface the pragma parsers have to the lexer. */
19853
19854 enum cpp_ttype
19855 pragma_lex (tree *value)
19856 {
19857 cp_token *tok;
19858 enum cpp_ttype ret;
19859
19860 tok = cp_lexer_peek_token (the_parser->lexer);
19861
19862 ret = tok->type;
19863 *value = tok->u.value;
19864
19865 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19866 ret = CPP_EOF;
19867 else if (ret == CPP_STRING)
19868 *value = cp_parser_string_literal (the_parser, false, false);
19869 else
19870 {
19871 cp_lexer_consume_token (the_parser->lexer);
19872 if (ret == CPP_KEYWORD)
19873 ret = CPP_NAME;
19874 }
19875
19876 return ret;
19877 }
19878
19879 \f
19880 /* External interface. */
19881
19882 /* Parse one entire translation unit. */
19883
19884 void
19885 c_parse_file (void)
19886 {
19887 bool error_occurred;
19888 static bool already_called = false;
19889
19890 if (already_called)
19891 {
19892 sorry ("inter-module optimizations not implemented for C++");
19893 return;
19894 }
19895 already_called = true;
19896
19897 the_parser = cp_parser_new ();
19898 push_deferring_access_checks (flag_access_control
19899 ? dk_no_deferred : dk_no_check);
19900 error_occurred = cp_parser_translation_unit (the_parser);
19901 the_parser = NULL;
19902 }
19903
19904 #include "gt-cp-parser.h"
This page took 0.902571 seconds and 6 git commands to generate.