]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
* parser.c (cp_parser_primary_expression): Reformat overly long lines.
[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
895 return declarator;
896 }
897
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 is, if any. */
903
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
907 {
908 cp_declarator *declarator;
909
910 /* It is valid to write:
911
912 class C { void f(); };
913 typedef C D;
914 void D::f();
915
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
919 well. */
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
931
932 return declarator;
933 }
934
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
938
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942 cp_declarator *declarator;
943
944 declarator = make_declarator (cdk_pointer);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
948
949 return declarator;
950 }
951
952 /* Like make_pointer_declarator -- but for references. */
953
954 cp_declarator *
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
956 {
957 cp_declarator *declarator;
958
959 declarator = make_declarator (cdk_reference);
960 declarator->declarator = target;
961 declarator->u.pointer.qualifiers = cv_qualifiers;
962 declarator->u.pointer.class_type = NULL_TREE;
963
964 return declarator;
965 }
966
967 /* Like make_pointer_declarator -- but for a pointer to a non-static
968 member of CLASS_TYPE. */
969
970 cp_declarator *
971 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
972 cp_declarator *pointee)
973 {
974 cp_declarator *declarator;
975
976 declarator = make_declarator (cdk_ptrmem);
977 declarator->declarator = pointee;
978 declarator->u.pointer.qualifiers = cv_qualifiers;
979 declarator->u.pointer.class_type = class_type;
980
981 return declarator;
982 }
983
984 /* Make a declarator for the function given by TARGET, with the
985 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
986 "const"-qualified member function. The EXCEPTION_SPECIFICATION
987 indicates what exceptions can be thrown. */
988
989 cp_declarator *
990 make_call_declarator (cp_declarator *target,
991 cp_parameter_declarator *parms,
992 cp_cv_quals cv_qualifiers,
993 tree exception_specification)
994 {
995 cp_declarator *declarator;
996
997 declarator = make_declarator (cdk_function);
998 declarator->declarator = target;
999 declarator->u.function.parameters = parms;
1000 declarator->u.function.qualifiers = cv_qualifiers;
1001 declarator->u.function.exception_specification = exception_specification;
1002
1003 return declarator;
1004 }
1005
1006 /* Make a declarator for an array of BOUNDS elements, each of which is
1007 defined by ELEMENT. */
1008
1009 cp_declarator *
1010 make_array_declarator (cp_declarator *element, tree bounds)
1011 {
1012 cp_declarator *declarator;
1013
1014 declarator = make_declarator (cdk_array);
1015 declarator->declarator = element;
1016 declarator->u.array.bounds = bounds;
1017
1018 return declarator;
1019 }
1020
1021 cp_parameter_declarator *no_parameters;
1022
1023 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1024 DECLARATOR and DEFAULT_ARGUMENT. */
1025
1026 cp_parameter_declarator *
1027 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1028 cp_declarator *declarator,
1029 tree default_argument)
1030 {
1031 cp_parameter_declarator *parameter;
1032
1033 parameter = ((cp_parameter_declarator *)
1034 alloc_declarator (sizeof (cp_parameter_declarator)));
1035 parameter->next = NULL;
1036 if (decl_specifiers)
1037 parameter->decl_specifiers = *decl_specifiers;
1038 else
1039 clear_decl_specs (&parameter->decl_specifiers);
1040 parameter->declarator = declarator;
1041 parameter->default_argument = default_argument;
1042 parameter->ellipsis_p = false;
1043
1044 return parameter;
1045 }
1046
1047 /* Returns true iff DECLARATOR is a declaration for a function. */
1048
1049 static bool
1050 function_declarator_p (const cp_declarator *declarator)
1051 {
1052 while (declarator)
1053 {
1054 if (declarator->kind == cdk_function
1055 && declarator->declarator->kind == cdk_id)
1056 return true;
1057 if (declarator->kind == cdk_id
1058 || declarator->kind == cdk_error)
1059 return false;
1060 declarator = declarator->declarator;
1061 }
1062 return false;
1063 }
1064
1065 /* The parser. */
1066
1067 /* Overview
1068 --------
1069
1070 A cp_parser parses the token stream as specified by the C++
1071 grammar. Its job is purely parsing, not semantic analysis. For
1072 example, the parser breaks the token stream into declarators,
1073 expressions, statements, and other similar syntactic constructs.
1074 It does not check that the types of the expressions on either side
1075 of an assignment-statement are compatible, or that a function is
1076 not declared with a parameter of type `void'.
1077
1078 The parser invokes routines elsewhere in the compiler to perform
1079 semantic analysis and to build up the abstract syntax tree for the
1080 code processed.
1081
1082 The parser (and the template instantiation code, which is, in a
1083 way, a close relative of parsing) are the only parts of the
1084 compiler that should be calling push_scope and pop_scope, or
1085 related functions. The parser (and template instantiation code)
1086 keeps track of what scope is presently active; everything else
1087 should simply honor that. (The code that generates static
1088 initializers may also need to set the scope, in order to check
1089 access control correctly when emitting the initializers.)
1090
1091 Methodology
1092 -----------
1093
1094 The parser is of the standard recursive-descent variety. Upcoming
1095 tokens in the token stream are examined in order to determine which
1096 production to use when parsing a non-terminal. Some C++ constructs
1097 require arbitrary look ahead to disambiguate. For example, it is
1098 impossible, in the general case, to tell whether a statement is an
1099 expression or declaration without scanning the entire statement.
1100 Therefore, the parser is capable of "parsing tentatively." When the
1101 parser is not sure what construct comes next, it enters this mode.
1102 Then, while we attempt to parse the construct, the parser queues up
1103 error messages, rather than issuing them immediately, and saves the
1104 tokens it consumes. If the construct is parsed successfully, the
1105 parser "commits", i.e., it issues any queued error messages and
1106 the tokens that were being preserved are permanently discarded.
1107 If, however, the construct is not parsed successfully, the parser
1108 rolls back its state completely so that it can resume parsing using
1109 a different alternative.
1110
1111 Future Improvements
1112 -------------------
1113
1114 The performance of the parser could probably be improved substantially.
1115 We could often eliminate the need to parse tentatively by looking ahead
1116 a little bit. In some places, this approach might not entirely eliminate
1117 the need to parse tentatively, but it might still speed up the average
1118 case. */
1119
1120 /* Flags that are passed to some parsing functions. These values can
1121 be bitwise-ored together. */
1122
1123 typedef enum cp_parser_flags
1124 {
1125 /* No flags. */
1126 CP_PARSER_FLAGS_NONE = 0x0,
1127 /* The construct is optional. If it is not present, then no error
1128 should be issued. */
1129 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1130 /* When parsing a type-specifier, do not allow user-defined types. */
1131 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1132 } cp_parser_flags;
1133
1134 /* The different kinds of declarators we want to parse. */
1135
1136 typedef enum cp_parser_declarator_kind
1137 {
1138 /* We want an abstract declarator. */
1139 CP_PARSER_DECLARATOR_ABSTRACT,
1140 /* We want a named declarator. */
1141 CP_PARSER_DECLARATOR_NAMED,
1142 /* We don't mind, but the name must be an unqualified-id. */
1143 CP_PARSER_DECLARATOR_EITHER
1144 } cp_parser_declarator_kind;
1145
1146 /* The precedence values used to parse binary expressions. The minimum value
1147 of PREC must be 1, because zero is reserved to quickly discriminate
1148 binary operators from other tokens. */
1149
1150 enum cp_parser_prec
1151 {
1152 PREC_NOT_OPERATOR,
1153 PREC_LOGICAL_OR_EXPRESSION,
1154 PREC_LOGICAL_AND_EXPRESSION,
1155 PREC_INCLUSIVE_OR_EXPRESSION,
1156 PREC_EXCLUSIVE_OR_EXPRESSION,
1157 PREC_AND_EXPRESSION,
1158 PREC_EQUALITY_EXPRESSION,
1159 PREC_RELATIONAL_EXPRESSION,
1160 PREC_SHIFT_EXPRESSION,
1161 PREC_ADDITIVE_EXPRESSION,
1162 PREC_MULTIPLICATIVE_EXPRESSION,
1163 PREC_PM_EXPRESSION,
1164 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1165 };
1166
1167 /* A mapping from a token type to a corresponding tree node type, with a
1168 precedence value. */
1169
1170 typedef struct cp_parser_binary_operations_map_node
1171 {
1172 /* The token type. */
1173 enum cpp_ttype token_type;
1174 /* The corresponding tree code. */
1175 enum tree_code tree_type;
1176 /* The precedence of this operator. */
1177 enum cp_parser_prec prec;
1178 } cp_parser_binary_operations_map_node;
1179
1180 /* The status of a tentative parse. */
1181
1182 typedef enum cp_parser_status_kind
1183 {
1184 /* No errors have occurred. */
1185 CP_PARSER_STATUS_KIND_NO_ERROR,
1186 /* An error has occurred. */
1187 CP_PARSER_STATUS_KIND_ERROR,
1188 /* We are committed to this tentative parse, whether or not an error
1189 has occurred. */
1190 CP_PARSER_STATUS_KIND_COMMITTED
1191 } cp_parser_status_kind;
1192
1193 typedef struct cp_parser_expression_stack_entry
1194 {
1195 /* Left hand side of the binary operation we are currently
1196 parsing. */
1197 tree lhs;
1198 /* Original tree code for left hand side, if it was a binary
1199 expression itself (used for -Wparentheses). */
1200 enum tree_code lhs_type;
1201 /* Tree code for the binary operation we are parsing. */
1202 enum tree_code tree_type;
1203 /* Precedence of the binary operation we are parsing. */
1204 int prec;
1205 } cp_parser_expression_stack_entry;
1206
1207 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1208 entries because precedence levels on the stack are monotonically
1209 increasing. */
1210 typedef struct cp_parser_expression_stack_entry
1211 cp_parser_expression_stack[NUM_PREC_VALUES];
1212
1213 /* Context that is saved and restored when parsing tentatively. */
1214 typedef struct cp_parser_context GTY (())
1215 {
1216 /* If this is a tentative parsing context, the status of the
1217 tentative parse. */
1218 enum cp_parser_status_kind status;
1219 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1220 that are looked up in this context must be looked up both in the
1221 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1222 the context of the containing expression. */
1223 tree object_type;
1224
1225 /* The next parsing context in the stack. */
1226 struct cp_parser_context *next;
1227 } cp_parser_context;
1228
1229 /* Prototypes. */
1230
1231 /* Constructors and destructors. */
1232
1233 static cp_parser_context *cp_parser_context_new
1234 (cp_parser_context *);
1235
1236 /* Class variables. */
1237
1238 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1239
1240 /* The operator-precedence table used by cp_parser_binary_expression.
1241 Transformed into an associative array (binops_by_token) by
1242 cp_parser_new. */
1243
1244 static const cp_parser_binary_operations_map_node binops[] = {
1245 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1246 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1247
1248 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1249 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1250 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1251
1252 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1253 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1254
1255 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1256 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1257
1258 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1259 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1260 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1261 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1262
1263 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1264 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1265
1266 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1267
1268 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1269
1270 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1271
1272 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1273
1274 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1275 };
1276
1277 /* The same as binops, but initialized by cp_parser_new so that
1278 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1279 for speed. */
1280 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1281
1282 /* Constructors and destructors. */
1283
1284 /* Construct a new context. The context below this one on the stack
1285 is given by NEXT. */
1286
1287 static cp_parser_context *
1288 cp_parser_context_new (cp_parser_context* next)
1289 {
1290 cp_parser_context *context;
1291
1292 /* Allocate the storage. */
1293 if (cp_parser_context_free_list != NULL)
1294 {
1295 /* Pull the first entry from the free list. */
1296 context = cp_parser_context_free_list;
1297 cp_parser_context_free_list = context->next;
1298 memset (context, 0, sizeof (*context));
1299 }
1300 else
1301 context = GGC_CNEW (cp_parser_context);
1302
1303 /* No errors have occurred yet in this context. */
1304 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1305 /* If this is not the bottomost context, copy information that we
1306 need from the previous context. */
1307 if (next)
1308 {
1309 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1310 expression, then we are parsing one in this context, too. */
1311 context->object_type = next->object_type;
1312 /* Thread the stack. */
1313 context->next = next;
1314 }
1315
1316 return context;
1317 }
1318
1319 /* The cp_parser structure represents the C++ parser. */
1320
1321 typedef struct cp_parser GTY(())
1322 {
1323 /* The lexer from which we are obtaining tokens. */
1324 cp_lexer *lexer;
1325
1326 /* The scope in which names should be looked up. If NULL_TREE, then
1327 we look up names in the scope that is currently open in the
1328 source program. If non-NULL, this is either a TYPE or
1329 NAMESPACE_DECL for the scope in which we should look. It can
1330 also be ERROR_MARK, when we've parsed a bogus scope.
1331
1332 This value is not cleared automatically after a name is looked
1333 up, so we must be careful to clear it before starting a new look
1334 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1335 will look up `Z' in the scope of `X', rather than the current
1336 scope.) Unfortunately, it is difficult to tell when name lookup
1337 is complete, because we sometimes peek at a token, look it up,
1338 and then decide not to consume it. */
1339 tree scope;
1340
1341 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1342 last lookup took place. OBJECT_SCOPE is used if an expression
1343 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1344 respectively. QUALIFYING_SCOPE is used for an expression of the
1345 form "X::Y"; it refers to X. */
1346 tree object_scope;
1347 tree qualifying_scope;
1348
1349 /* A stack of parsing contexts. All but the bottom entry on the
1350 stack will be tentative contexts.
1351
1352 We parse tentatively in order to determine which construct is in
1353 use in some situations. For example, in order to determine
1354 whether a statement is an expression-statement or a
1355 declaration-statement we parse it tentatively as a
1356 declaration-statement. If that fails, we then reparse the same
1357 token stream as an expression-statement. */
1358 cp_parser_context *context;
1359
1360 /* True if we are parsing GNU C++. If this flag is not set, then
1361 GNU extensions are not recognized. */
1362 bool allow_gnu_extensions_p;
1363
1364 /* TRUE if the `>' token should be interpreted as the greater-than
1365 operator. FALSE if it is the end of a template-id or
1366 template-parameter-list. */
1367 bool greater_than_is_operator_p;
1368
1369 /* TRUE if default arguments are allowed within a parameter list
1370 that starts at this point. FALSE if only a gnu extension makes
1371 them permissible. */
1372 bool default_arg_ok_p;
1373
1374 /* TRUE if we are parsing an integral constant-expression. See
1375 [expr.const] for a precise definition. */
1376 bool integral_constant_expression_p;
1377
1378 /* TRUE if we are parsing an integral constant-expression -- but a
1379 non-constant expression should be permitted as well. This flag
1380 is used when parsing an array bound so that GNU variable-length
1381 arrays are tolerated. */
1382 bool allow_non_integral_constant_expression_p;
1383
1384 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1385 been seen that makes the expression non-constant. */
1386 bool non_integral_constant_expression_p;
1387
1388 /* TRUE if local variable names and `this' are forbidden in the
1389 current context. */
1390 bool local_variables_forbidden_p;
1391
1392 /* TRUE if the declaration we are parsing is part of a
1393 linkage-specification of the form `extern string-literal
1394 declaration'. */
1395 bool in_unbraced_linkage_specification_p;
1396
1397 /* TRUE if we are presently parsing a declarator, after the
1398 direct-declarator. */
1399 bool in_declarator_p;
1400
1401 /* TRUE if we are presently parsing a template-argument-list. */
1402 bool in_template_argument_list_p;
1403
1404 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1405 to IN_OMP_BLOCK if parsing OpenMP structured block and
1406 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1407 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1408 iteration-statement, OpenMP block or loop within that switch. */
1409 #define IN_SWITCH_STMT 1
1410 #define IN_ITERATION_STMT 2
1411 #define IN_OMP_BLOCK 4
1412 #define IN_OMP_FOR 8
1413 unsigned char in_statement;
1414
1415 /* TRUE if we are presently parsing the body of a switch statement.
1416 Note that this doesn't quite overlap with in_statement above.
1417 The difference relates to giving the right sets of error messages:
1418 "case not in switch" vs "break statement used with OpenMP...". */
1419 bool in_switch_statement_p;
1420
1421 /* TRUE if we are parsing a type-id in an expression context. In
1422 such a situation, both "type (expr)" and "type (type)" are valid
1423 alternatives. */
1424 bool in_type_id_in_expr_p;
1425
1426 /* TRUE if we are currently in a header file where declarations are
1427 implicitly extern "C". */
1428 bool implicit_extern_c;
1429
1430 /* TRUE if strings in expressions should be translated to the execution
1431 character set. */
1432 bool translate_strings_p;
1433
1434 /* TRUE if we are presently parsing the body of a function, but not
1435 a local class. */
1436 bool in_function_body;
1437
1438 /* If non-NULL, then we are parsing a construct where new type
1439 definitions are not permitted. The string stored here will be
1440 issued as an error message if a type is defined. */
1441 const char *type_definition_forbidden_message;
1442
1443 /* A list of lists. The outer list is a stack, used for member
1444 functions of local classes. At each level there are two sub-list,
1445 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1446 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1447 TREE_VALUE's. The functions are chained in reverse declaration
1448 order.
1449
1450 The TREE_PURPOSE sublist contains those functions with default
1451 arguments that need post processing, and the TREE_VALUE sublist
1452 contains those functions with definitions that need post
1453 processing.
1454
1455 These lists can only be processed once the outermost class being
1456 defined is complete. */
1457 tree unparsed_functions_queues;
1458
1459 /* The number of classes whose definitions are currently in
1460 progress. */
1461 unsigned num_classes_being_defined;
1462
1463 /* The number of template parameter lists that apply directly to the
1464 current declaration. */
1465 unsigned num_template_parameter_lists;
1466 } cp_parser;
1467
1468 /* Prototypes. */
1469
1470 /* Constructors and destructors. */
1471
1472 static cp_parser *cp_parser_new
1473 (void);
1474
1475 /* Routines to parse various constructs.
1476
1477 Those that return `tree' will return the error_mark_node (rather
1478 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1479 Sometimes, they will return an ordinary node if error-recovery was
1480 attempted, even though a parse error occurred. So, to check
1481 whether or not a parse error occurred, you should always use
1482 cp_parser_error_occurred. If the construct is optional (indicated
1483 either by an `_opt' in the name of the function that does the
1484 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1485 the construct is not present. */
1486
1487 /* Lexical conventions [gram.lex] */
1488
1489 static tree cp_parser_identifier
1490 (cp_parser *);
1491 static tree cp_parser_string_literal
1492 (cp_parser *, bool, bool);
1493
1494 /* Basic concepts [gram.basic] */
1495
1496 static bool cp_parser_translation_unit
1497 (cp_parser *);
1498
1499 /* Expressions [gram.expr] */
1500
1501 static tree cp_parser_primary_expression
1502 (cp_parser *, bool, bool, bool, cp_id_kind *);
1503 static tree cp_parser_id_expression
1504 (cp_parser *, bool, bool, bool *, bool, bool);
1505 static tree cp_parser_unqualified_id
1506 (cp_parser *, bool, bool, bool, bool);
1507 static tree cp_parser_nested_name_specifier_opt
1508 (cp_parser *, bool, bool, bool, bool);
1509 static tree cp_parser_nested_name_specifier
1510 (cp_parser *, bool, bool, bool, bool);
1511 static tree cp_parser_class_or_namespace_name
1512 (cp_parser *, bool, bool, bool, bool, bool);
1513 static tree cp_parser_postfix_expression
1514 (cp_parser *, bool, bool);
1515 static tree cp_parser_postfix_open_square_expression
1516 (cp_parser *, tree, bool);
1517 static tree cp_parser_postfix_dot_deref_expression
1518 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1519 static tree cp_parser_parenthesized_expression_list
1520 (cp_parser *, bool, bool, bool *);
1521 static void cp_parser_pseudo_destructor_name
1522 (cp_parser *, tree *, tree *);
1523 static tree cp_parser_unary_expression
1524 (cp_parser *, bool, bool);
1525 static enum tree_code cp_parser_unary_operator
1526 (cp_token *);
1527 static tree cp_parser_new_expression
1528 (cp_parser *);
1529 static tree cp_parser_new_placement
1530 (cp_parser *);
1531 static tree cp_parser_new_type_id
1532 (cp_parser *, tree *);
1533 static cp_declarator *cp_parser_new_declarator_opt
1534 (cp_parser *);
1535 static cp_declarator *cp_parser_direct_new_declarator
1536 (cp_parser *);
1537 static tree cp_parser_new_initializer
1538 (cp_parser *);
1539 static tree cp_parser_delete_expression
1540 (cp_parser *);
1541 static tree cp_parser_cast_expression
1542 (cp_parser *, bool, bool);
1543 static tree cp_parser_binary_expression
1544 (cp_parser *, bool);
1545 static tree cp_parser_question_colon_clause
1546 (cp_parser *, tree);
1547 static tree cp_parser_assignment_expression
1548 (cp_parser *, bool);
1549 static enum tree_code cp_parser_assignment_operator_opt
1550 (cp_parser *);
1551 static tree cp_parser_expression
1552 (cp_parser *, bool);
1553 static tree cp_parser_constant_expression
1554 (cp_parser *, bool, bool *);
1555 static tree cp_parser_builtin_offsetof
1556 (cp_parser *);
1557
1558 /* Statements [gram.stmt.stmt] */
1559
1560 static void cp_parser_statement
1561 (cp_parser *, tree, bool, bool *);
1562 static void cp_parser_label_for_labeled_statement
1563 (cp_parser *);
1564 static tree cp_parser_expression_statement
1565 (cp_parser *, tree);
1566 static tree cp_parser_compound_statement
1567 (cp_parser *, tree, bool);
1568 static void cp_parser_statement_seq_opt
1569 (cp_parser *, tree);
1570 static tree cp_parser_selection_statement
1571 (cp_parser *, bool *);
1572 static tree cp_parser_condition
1573 (cp_parser *);
1574 static tree cp_parser_iteration_statement
1575 (cp_parser *);
1576 static void cp_parser_for_init_statement
1577 (cp_parser *);
1578 static tree cp_parser_jump_statement
1579 (cp_parser *);
1580 static void cp_parser_declaration_statement
1581 (cp_parser *);
1582
1583 static tree cp_parser_implicitly_scoped_statement
1584 (cp_parser *, bool *);
1585 static void cp_parser_already_scoped_statement
1586 (cp_parser *);
1587
1588 /* Declarations [gram.dcl.dcl] */
1589
1590 static void cp_parser_declaration_seq_opt
1591 (cp_parser *);
1592 static void cp_parser_declaration
1593 (cp_parser *);
1594 static void cp_parser_block_declaration
1595 (cp_parser *, bool);
1596 static void cp_parser_simple_declaration
1597 (cp_parser *, bool);
1598 static void cp_parser_decl_specifier_seq
1599 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1600 static tree cp_parser_storage_class_specifier_opt
1601 (cp_parser *);
1602 static tree cp_parser_function_specifier_opt
1603 (cp_parser *, cp_decl_specifier_seq *);
1604 static tree cp_parser_type_specifier
1605 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1606 int *, bool *);
1607 static tree cp_parser_simple_type_specifier
1608 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1609 static tree cp_parser_type_name
1610 (cp_parser *);
1611 static tree cp_parser_elaborated_type_specifier
1612 (cp_parser *, bool, bool);
1613 static tree cp_parser_enum_specifier
1614 (cp_parser *);
1615 static void cp_parser_enumerator_list
1616 (cp_parser *, tree);
1617 static void cp_parser_enumerator_definition
1618 (cp_parser *, tree);
1619 static tree cp_parser_namespace_name
1620 (cp_parser *);
1621 static void cp_parser_namespace_definition
1622 (cp_parser *);
1623 static void cp_parser_namespace_body
1624 (cp_parser *);
1625 static tree cp_parser_qualified_namespace_specifier
1626 (cp_parser *);
1627 static void cp_parser_namespace_alias_definition
1628 (cp_parser *);
1629 static bool cp_parser_using_declaration
1630 (cp_parser *, bool);
1631 static void cp_parser_using_directive
1632 (cp_parser *);
1633 static void cp_parser_asm_definition
1634 (cp_parser *);
1635 static void cp_parser_linkage_specification
1636 (cp_parser *);
1637 static void cp_parser_static_assert
1638 (cp_parser *, bool);
1639
1640 /* Declarators [gram.dcl.decl] */
1641
1642 static tree cp_parser_init_declarator
1643 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1644 static cp_declarator *cp_parser_declarator
1645 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1646 static cp_declarator *cp_parser_direct_declarator
1647 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1648 static enum tree_code cp_parser_ptr_operator
1649 (cp_parser *, tree *, cp_cv_quals *);
1650 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1651 (cp_parser *);
1652 static tree cp_parser_declarator_id
1653 (cp_parser *, bool);
1654 static tree cp_parser_type_id
1655 (cp_parser *);
1656 static void cp_parser_type_specifier_seq
1657 (cp_parser *, bool, cp_decl_specifier_seq *);
1658 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1659 (cp_parser *);
1660 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1661 (cp_parser *, bool *);
1662 static cp_parameter_declarator *cp_parser_parameter_declaration
1663 (cp_parser *, bool, bool *);
1664 static void cp_parser_function_body
1665 (cp_parser *);
1666 static tree cp_parser_initializer
1667 (cp_parser *, bool *, bool *);
1668 static tree cp_parser_initializer_clause
1669 (cp_parser *, bool *);
1670 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1671 (cp_parser *, bool *);
1672
1673 static bool cp_parser_ctor_initializer_opt_and_function_body
1674 (cp_parser *);
1675
1676 /* Classes [gram.class] */
1677
1678 static tree cp_parser_class_name
1679 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1680 static tree cp_parser_class_specifier
1681 (cp_parser *);
1682 static tree cp_parser_class_head
1683 (cp_parser *, bool *, tree *, tree *);
1684 static enum tag_types cp_parser_class_key
1685 (cp_parser *);
1686 static void cp_parser_member_specification_opt
1687 (cp_parser *);
1688 static void cp_parser_member_declaration
1689 (cp_parser *);
1690 static tree cp_parser_pure_specifier
1691 (cp_parser *);
1692 static tree cp_parser_constant_initializer
1693 (cp_parser *);
1694
1695 /* Derived classes [gram.class.derived] */
1696
1697 static tree cp_parser_base_clause
1698 (cp_parser *);
1699 static tree cp_parser_base_specifier
1700 (cp_parser *);
1701
1702 /* Special member functions [gram.special] */
1703
1704 static tree cp_parser_conversion_function_id
1705 (cp_parser *);
1706 static tree cp_parser_conversion_type_id
1707 (cp_parser *);
1708 static cp_declarator *cp_parser_conversion_declarator_opt
1709 (cp_parser *);
1710 static bool cp_parser_ctor_initializer_opt
1711 (cp_parser *);
1712 static void cp_parser_mem_initializer_list
1713 (cp_parser *);
1714 static tree cp_parser_mem_initializer
1715 (cp_parser *);
1716 static tree cp_parser_mem_initializer_id
1717 (cp_parser *);
1718
1719 /* Overloading [gram.over] */
1720
1721 static tree cp_parser_operator_function_id
1722 (cp_parser *);
1723 static tree cp_parser_operator
1724 (cp_parser *);
1725
1726 /* Templates [gram.temp] */
1727
1728 static void cp_parser_template_declaration
1729 (cp_parser *, bool);
1730 static tree cp_parser_template_parameter_list
1731 (cp_parser *);
1732 static tree cp_parser_template_parameter
1733 (cp_parser *, bool *);
1734 static tree cp_parser_type_parameter
1735 (cp_parser *);
1736 static tree cp_parser_template_id
1737 (cp_parser *, bool, bool, bool);
1738 static tree cp_parser_template_name
1739 (cp_parser *, bool, bool, bool, bool *);
1740 static tree cp_parser_template_argument_list
1741 (cp_parser *);
1742 static tree cp_parser_template_argument
1743 (cp_parser *);
1744 static void cp_parser_explicit_instantiation
1745 (cp_parser *);
1746 static void cp_parser_explicit_specialization
1747 (cp_parser *);
1748
1749 /* Exception handling [gram.exception] */
1750
1751 static tree cp_parser_try_block
1752 (cp_parser *);
1753 static bool cp_parser_function_try_block
1754 (cp_parser *);
1755 static void cp_parser_handler_seq
1756 (cp_parser *);
1757 static void cp_parser_handler
1758 (cp_parser *);
1759 static tree cp_parser_exception_declaration
1760 (cp_parser *);
1761 static tree cp_parser_throw_expression
1762 (cp_parser *);
1763 static tree cp_parser_exception_specification_opt
1764 (cp_parser *);
1765 static tree cp_parser_type_id_list
1766 (cp_parser *);
1767
1768 /* GNU Extensions */
1769
1770 static tree cp_parser_asm_specification_opt
1771 (cp_parser *);
1772 static tree cp_parser_asm_operand_list
1773 (cp_parser *);
1774 static tree cp_parser_asm_clobber_list
1775 (cp_parser *);
1776 static tree cp_parser_attributes_opt
1777 (cp_parser *);
1778 static tree cp_parser_attribute_list
1779 (cp_parser *);
1780 static bool cp_parser_extension_opt
1781 (cp_parser *, int *);
1782 static void cp_parser_label_declaration
1783 (cp_parser *);
1784
1785 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1786 static bool cp_parser_pragma
1787 (cp_parser *, enum pragma_context);
1788
1789 /* Objective-C++ Productions */
1790
1791 static tree cp_parser_objc_message_receiver
1792 (cp_parser *);
1793 static tree cp_parser_objc_message_args
1794 (cp_parser *);
1795 static tree cp_parser_objc_message_expression
1796 (cp_parser *);
1797 static tree cp_parser_objc_encode_expression
1798 (cp_parser *);
1799 static tree cp_parser_objc_defs_expression
1800 (cp_parser *);
1801 static tree cp_parser_objc_protocol_expression
1802 (cp_parser *);
1803 static tree cp_parser_objc_selector_expression
1804 (cp_parser *);
1805 static tree cp_parser_objc_expression
1806 (cp_parser *);
1807 static bool cp_parser_objc_selector_p
1808 (enum cpp_ttype);
1809 static tree cp_parser_objc_selector
1810 (cp_parser *);
1811 static tree cp_parser_objc_protocol_refs_opt
1812 (cp_parser *);
1813 static void cp_parser_objc_declaration
1814 (cp_parser *);
1815 static tree cp_parser_objc_statement
1816 (cp_parser *);
1817
1818 /* Utility Routines */
1819
1820 static tree cp_parser_lookup_name
1821 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1822 static tree cp_parser_lookup_name_simple
1823 (cp_parser *, tree);
1824 static tree cp_parser_maybe_treat_template_as_class
1825 (tree, bool);
1826 static bool cp_parser_check_declarator_template_parameters
1827 (cp_parser *, cp_declarator *);
1828 static bool cp_parser_check_template_parameters
1829 (cp_parser *, unsigned);
1830 static tree cp_parser_simple_cast_expression
1831 (cp_parser *);
1832 static tree cp_parser_global_scope_opt
1833 (cp_parser *, bool);
1834 static bool cp_parser_constructor_declarator_p
1835 (cp_parser *, bool);
1836 static tree cp_parser_function_definition_from_specifiers_and_declarator
1837 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1838 static tree cp_parser_function_definition_after_declarator
1839 (cp_parser *, bool);
1840 static void cp_parser_template_declaration_after_export
1841 (cp_parser *, bool);
1842 static void cp_parser_perform_template_parameter_access_checks
1843 (VEC (deferred_access_check,gc)*);
1844 static tree cp_parser_single_declaration
1845 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1846 static tree cp_parser_functional_cast
1847 (cp_parser *, tree);
1848 static tree cp_parser_save_member_function_body
1849 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1850 static tree cp_parser_enclosed_template_argument_list
1851 (cp_parser *);
1852 static void cp_parser_save_default_args
1853 (cp_parser *, tree);
1854 static void cp_parser_late_parsing_for_member
1855 (cp_parser *, tree);
1856 static void cp_parser_late_parsing_default_args
1857 (cp_parser *, tree);
1858 static tree cp_parser_sizeof_operand
1859 (cp_parser *, enum rid);
1860 static bool cp_parser_declares_only_class_p
1861 (cp_parser *);
1862 static void cp_parser_set_storage_class
1863 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1864 static void cp_parser_set_decl_spec_type
1865 (cp_decl_specifier_seq *, tree, bool);
1866 static bool cp_parser_friend_p
1867 (const cp_decl_specifier_seq *);
1868 static cp_token *cp_parser_require
1869 (cp_parser *, enum cpp_ttype, const char *);
1870 static cp_token *cp_parser_require_keyword
1871 (cp_parser *, enum rid, const char *);
1872 static bool cp_parser_token_starts_function_definition_p
1873 (cp_token *);
1874 static bool cp_parser_next_token_starts_class_definition_p
1875 (cp_parser *);
1876 static bool cp_parser_next_token_ends_template_argument_p
1877 (cp_parser *);
1878 static bool cp_parser_nth_token_starts_template_argument_list_p
1879 (cp_parser *, size_t);
1880 static enum tag_types cp_parser_token_is_class_key
1881 (cp_token *);
1882 static void cp_parser_check_class_key
1883 (enum tag_types, tree type);
1884 static void cp_parser_check_access_in_redeclaration
1885 (tree type);
1886 static bool cp_parser_optional_template_keyword
1887 (cp_parser *);
1888 static void cp_parser_pre_parsed_nested_name_specifier
1889 (cp_parser *);
1890 static void cp_parser_cache_group
1891 (cp_parser *, enum cpp_ttype, unsigned);
1892 static void cp_parser_parse_tentatively
1893 (cp_parser *);
1894 static void cp_parser_commit_to_tentative_parse
1895 (cp_parser *);
1896 static void cp_parser_abort_tentative_parse
1897 (cp_parser *);
1898 static bool cp_parser_parse_definitely
1899 (cp_parser *);
1900 static inline bool cp_parser_parsing_tentatively
1901 (cp_parser *);
1902 static bool cp_parser_uncommitted_to_tentative_parse_p
1903 (cp_parser *);
1904 static void cp_parser_error
1905 (cp_parser *, const char *);
1906 static void cp_parser_name_lookup_error
1907 (cp_parser *, tree, tree, const char *);
1908 static bool cp_parser_simulate_error
1909 (cp_parser *);
1910 static bool cp_parser_check_type_definition
1911 (cp_parser *);
1912 static void cp_parser_check_for_definition_in_return_type
1913 (cp_declarator *, tree);
1914 static void cp_parser_check_for_invalid_template_id
1915 (cp_parser *, tree);
1916 static bool cp_parser_non_integral_constant_expression
1917 (cp_parser *, const char *);
1918 static void cp_parser_diagnose_invalid_type_name
1919 (cp_parser *, tree, tree);
1920 static bool cp_parser_parse_and_diagnose_invalid_type_name
1921 (cp_parser *);
1922 static int cp_parser_skip_to_closing_parenthesis
1923 (cp_parser *, bool, bool, bool);
1924 static void cp_parser_skip_to_end_of_statement
1925 (cp_parser *);
1926 static void cp_parser_consume_semicolon_at_end_of_statement
1927 (cp_parser *);
1928 static void cp_parser_skip_to_end_of_block_or_statement
1929 (cp_parser *);
1930 static void cp_parser_skip_to_closing_brace
1931 (cp_parser *);
1932 static void cp_parser_skip_to_end_of_template_parameter_list
1933 (cp_parser *);
1934 static void cp_parser_skip_to_pragma_eol
1935 (cp_parser*, cp_token *);
1936 static bool cp_parser_error_occurred
1937 (cp_parser *);
1938 static bool cp_parser_allow_gnu_extensions_p
1939 (cp_parser *);
1940 static bool cp_parser_is_string_literal
1941 (cp_token *);
1942 static bool cp_parser_is_keyword
1943 (cp_token *, enum rid);
1944 static tree cp_parser_make_typename_type
1945 (cp_parser *, tree, tree);
1946
1947 /* Returns nonzero if we are parsing tentatively. */
1948
1949 static inline bool
1950 cp_parser_parsing_tentatively (cp_parser* parser)
1951 {
1952 return parser->context->next != NULL;
1953 }
1954
1955 /* Returns nonzero if TOKEN is a string literal. */
1956
1957 static bool
1958 cp_parser_is_string_literal (cp_token* token)
1959 {
1960 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1961 }
1962
1963 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1964
1965 static bool
1966 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1967 {
1968 return token->keyword == keyword;
1969 }
1970
1971 /* If not parsing tentatively, issue a diagnostic of the form
1972 FILE:LINE: MESSAGE before TOKEN
1973 where TOKEN is the next token in the input stream. MESSAGE
1974 (specified by the caller) is usually of the form "expected
1975 OTHER-TOKEN". */
1976
1977 static void
1978 cp_parser_error (cp_parser* parser, const char* message)
1979 {
1980 if (!cp_parser_simulate_error (parser))
1981 {
1982 cp_token *token = cp_lexer_peek_token (parser->lexer);
1983 /* This diagnostic makes more sense if it is tagged to the line
1984 of the token we just peeked at. */
1985 cp_lexer_set_source_position_from_token (token);
1986
1987 if (token->type == CPP_PRAGMA)
1988 {
1989 error ("%<#pragma%> is not allowed here");
1990 cp_parser_skip_to_pragma_eol (parser, token);
1991 return;
1992 }
1993
1994 c_parse_error (message,
1995 /* Because c_parser_error does not understand
1996 CPP_KEYWORD, keywords are treated like
1997 identifiers. */
1998 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1999 token->u.value);
2000 }
2001 }
2002
2003 /* Issue an error about name-lookup failing. NAME is the
2004 IDENTIFIER_NODE DECL is the result of
2005 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2006 the thing that we hoped to find. */
2007
2008 static void
2009 cp_parser_name_lookup_error (cp_parser* parser,
2010 tree name,
2011 tree decl,
2012 const char* desired)
2013 {
2014 /* If name lookup completely failed, tell the user that NAME was not
2015 declared. */
2016 if (decl == error_mark_node)
2017 {
2018 if (parser->scope && parser->scope != global_namespace)
2019 error ("%<%D::%D%> has not been declared",
2020 parser->scope, name);
2021 else if (parser->scope == global_namespace)
2022 error ("%<::%D%> has not been declared", name);
2023 else if (parser->object_scope
2024 && !CLASS_TYPE_P (parser->object_scope))
2025 error ("request for member %qD in non-class type %qT",
2026 name, parser->object_scope);
2027 else if (parser->object_scope)
2028 error ("%<%T::%D%> has not been declared",
2029 parser->object_scope, name);
2030 else
2031 error ("%qD has not been declared", name);
2032 }
2033 else if (parser->scope && parser->scope != global_namespace)
2034 error ("%<%D::%D%> %s", parser->scope, name, desired);
2035 else if (parser->scope == global_namespace)
2036 error ("%<::%D%> %s", name, desired);
2037 else
2038 error ("%qD %s", name, desired);
2039 }
2040
2041 /* If we are parsing tentatively, remember that an error has occurred
2042 during this tentative parse. Returns true if the error was
2043 simulated; false if a message should be issued by the caller. */
2044
2045 static bool
2046 cp_parser_simulate_error (cp_parser* parser)
2047 {
2048 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2049 {
2050 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2051 return true;
2052 }
2053 return false;
2054 }
2055
2056 /* Check for repeated decl-specifiers. */
2057
2058 static void
2059 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2060 {
2061 cp_decl_spec ds;
2062
2063 for (ds = ds_first; ds != ds_last; ++ds)
2064 {
2065 unsigned count = decl_specs->specs[(int)ds];
2066 if (count < 2)
2067 continue;
2068 /* The "long" specifier is a special case because of "long long". */
2069 if (ds == ds_long)
2070 {
2071 if (count > 2)
2072 error ("%<long long long%> is too long for GCC");
2073 else if (pedantic && !in_system_header && warn_long_long)
2074 pedwarn ("ISO C++ does not support %<long long%>");
2075 }
2076 else if (count > 1)
2077 {
2078 static const char *const decl_spec_names[] = {
2079 "signed",
2080 "unsigned",
2081 "short",
2082 "long",
2083 "const",
2084 "volatile",
2085 "restrict",
2086 "inline",
2087 "virtual",
2088 "explicit",
2089 "friend",
2090 "typedef",
2091 "__complex",
2092 "__thread"
2093 };
2094 error ("duplicate %qs", decl_spec_names[(int)ds]);
2095 }
2096 }
2097 }
2098
2099 /* This function is called when a type is defined. If type
2100 definitions are forbidden at this point, an error message is
2101 issued. */
2102
2103 static bool
2104 cp_parser_check_type_definition (cp_parser* parser)
2105 {
2106 /* If types are forbidden here, issue a message. */
2107 if (parser->type_definition_forbidden_message)
2108 {
2109 /* Use `%s' to print the string in case there are any escape
2110 characters in the message. */
2111 error ("%s", parser->type_definition_forbidden_message);
2112 return false;
2113 }
2114 return true;
2115 }
2116
2117 /* This function is called when the DECLARATOR is processed. The TYPE
2118 was a type defined in the decl-specifiers. If it is invalid to
2119 define a type in the decl-specifiers for DECLARATOR, an error is
2120 issued. */
2121
2122 static void
2123 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2124 tree type)
2125 {
2126 /* [dcl.fct] forbids type definitions in return types.
2127 Unfortunately, it's not easy to know whether or not we are
2128 processing a return type until after the fact. */
2129 while (declarator
2130 && (declarator->kind == cdk_pointer
2131 || declarator->kind == cdk_reference
2132 || declarator->kind == cdk_ptrmem))
2133 declarator = declarator->declarator;
2134 if (declarator
2135 && declarator->kind == cdk_function)
2136 {
2137 error ("new types may not be defined in a return type");
2138 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2139 type);
2140 }
2141 }
2142
2143 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2144 "<" in any valid C++ program. If the next token is indeed "<",
2145 issue a message warning the user about what appears to be an
2146 invalid attempt to form a template-id. */
2147
2148 static void
2149 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2150 tree type)
2151 {
2152 cp_token_position start = 0;
2153
2154 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2155 {
2156 if (TYPE_P (type))
2157 error ("%qT is not a template", type);
2158 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2159 error ("%qE is not a template", type);
2160 else
2161 error ("invalid template-id");
2162 /* Remember the location of the invalid "<". */
2163 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2164 start = cp_lexer_token_position (parser->lexer, true);
2165 /* Consume the "<". */
2166 cp_lexer_consume_token (parser->lexer);
2167 /* Parse the template arguments. */
2168 cp_parser_enclosed_template_argument_list (parser);
2169 /* Permanently remove the invalid template arguments so that
2170 this error message is not issued again. */
2171 if (start)
2172 cp_lexer_purge_tokens_after (parser->lexer, start);
2173 }
2174 }
2175
2176 /* If parsing an integral constant-expression, issue an error message
2177 about the fact that THING appeared and return true. Otherwise,
2178 return false. In either case, set
2179 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2180
2181 static bool
2182 cp_parser_non_integral_constant_expression (cp_parser *parser,
2183 const char *thing)
2184 {
2185 parser->non_integral_constant_expression_p = true;
2186 if (parser->integral_constant_expression_p)
2187 {
2188 if (!parser->allow_non_integral_constant_expression_p)
2189 {
2190 error ("%s cannot appear in a constant-expression", thing);
2191 return true;
2192 }
2193 }
2194 return false;
2195 }
2196
2197 /* Emit a diagnostic for an invalid type name. SCOPE is the
2198 qualifying scope (or NULL, if none) for ID. This function commits
2199 to the current active tentative parse, if any. (Otherwise, the
2200 problematic construct might be encountered again later, resulting
2201 in duplicate error messages.) */
2202
2203 static void
2204 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2205 {
2206 tree decl, old_scope;
2207 /* Try to lookup the identifier. */
2208 old_scope = parser->scope;
2209 parser->scope = scope;
2210 decl = cp_parser_lookup_name_simple (parser, id);
2211 parser->scope = old_scope;
2212 /* If the lookup found a template-name, it means that the user forgot
2213 to specify an argument list. Emit a useful error message. */
2214 if (TREE_CODE (decl) == TEMPLATE_DECL)
2215 error ("invalid use of template-name %qE without an argument list", decl);
2216 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2217 error ("invalid use of destructor %qD as a type", id);
2218 else if (TREE_CODE (decl) == TYPE_DECL)
2219 /* Something like 'unsigned A a;' */
2220 error ("invalid combination of multiple type-specifiers");
2221 else if (!parser->scope)
2222 {
2223 /* Issue an error message. */
2224 error ("%qE does not name a type", id);
2225 /* If we're in a template class, it's possible that the user was
2226 referring to a type from a base class. For example:
2227
2228 template <typename T> struct A { typedef T X; };
2229 template <typename T> struct B : public A<T> { X x; };
2230
2231 The user should have said "typename A<T>::X". */
2232 if (processing_template_decl && current_class_type
2233 && TYPE_BINFO (current_class_type))
2234 {
2235 tree b;
2236
2237 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2238 b;
2239 b = TREE_CHAIN (b))
2240 {
2241 tree base_type = BINFO_TYPE (b);
2242 if (CLASS_TYPE_P (base_type)
2243 && dependent_type_p (base_type))
2244 {
2245 tree field;
2246 /* Go from a particular instantiation of the
2247 template (which will have an empty TYPE_FIELDs),
2248 to the main version. */
2249 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2250 for (field = TYPE_FIELDS (base_type);
2251 field;
2252 field = TREE_CHAIN (field))
2253 if (TREE_CODE (field) == TYPE_DECL
2254 && DECL_NAME (field) == id)
2255 {
2256 inform ("(perhaps %<typename %T::%E%> was intended)",
2257 BINFO_TYPE (b), id);
2258 break;
2259 }
2260 if (field)
2261 break;
2262 }
2263 }
2264 }
2265 }
2266 /* Here we diagnose qualified-ids where the scope is actually correct,
2267 but the identifier does not resolve to a valid type name. */
2268 else if (parser->scope != error_mark_node)
2269 {
2270 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2271 error ("%qE in namespace %qE does not name a type",
2272 id, parser->scope);
2273 else if (TYPE_P (parser->scope))
2274 error ("%qE in class %qT does not name a type", id, parser->scope);
2275 else
2276 gcc_unreachable ();
2277 }
2278 cp_parser_commit_to_tentative_parse (parser);
2279 }
2280
2281 /* Check for a common situation where a type-name should be present,
2282 but is not, and issue a sensible error message. Returns true if an
2283 invalid type-name was detected.
2284
2285 The situation handled by this function are variable declarations of the
2286 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2287 Usually, `ID' should name a type, but if we got here it means that it
2288 does not. We try to emit the best possible error message depending on
2289 how exactly the id-expression looks like. */
2290
2291 static bool
2292 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2293 {
2294 tree id;
2295
2296 cp_parser_parse_tentatively (parser);
2297 id = cp_parser_id_expression (parser,
2298 /*template_keyword_p=*/false,
2299 /*check_dependency_p=*/true,
2300 /*template_p=*/NULL,
2301 /*declarator_p=*/true,
2302 /*optional_p=*/false);
2303 /* After the id-expression, there should be a plain identifier,
2304 otherwise this is not a simple variable declaration. Also, if
2305 the scope is dependent, we cannot do much. */
2306 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2307 || (parser->scope && TYPE_P (parser->scope)
2308 && dependent_type_p (parser->scope)))
2309 {
2310 cp_parser_abort_tentative_parse (parser);
2311 return false;
2312 }
2313 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2314 return false;
2315
2316 /* Emit a diagnostic for the invalid type. */
2317 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2318 /* Skip to the end of the declaration; there's no point in
2319 trying to process it. */
2320 cp_parser_skip_to_end_of_block_or_statement (parser);
2321 return true;
2322 }
2323
2324 /* Consume tokens up to, and including, the next non-nested closing `)'.
2325 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2326 are doing error recovery. Returns -1 if OR_COMMA is true and we
2327 found an unnested comma. */
2328
2329 static int
2330 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2331 bool recovering,
2332 bool or_comma,
2333 bool consume_paren)
2334 {
2335 unsigned paren_depth = 0;
2336 unsigned brace_depth = 0;
2337
2338 if (recovering && !or_comma
2339 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2340 return 0;
2341
2342 while (true)
2343 {
2344 cp_token * token = cp_lexer_peek_token (parser->lexer);
2345
2346 switch (token->type)
2347 {
2348 case CPP_EOF:
2349 case CPP_PRAGMA_EOL:
2350 /* If we've run out of tokens, then there is no closing `)'. */
2351 return 0;
2352
2353 case CPP_SEMICOLON:
2354 /* This matches the processing in skip_to_end_of_statement. */
2355 if (!brace_depth)
2356 return 0;
2357 break;
2358
2359 case CPP_OPEN_BRACE:
2360 ++brace_depth;
2361 break;
2362 case CPP_CLOSE_BRACE:
2363 if (!brace_depth--)
2364 return 0;
2365 break;
2366
2367 case CPP_COMMA:
2368 if (recovering && or_comma && !brace_depth && !paren_depth)
2369 return -1;
2370 break;
2371
2372 case CPP_OPEN_PAREN:
2373 if (!brace_depth)
2374 ++paren_depth;
2375 break;
2376
2377 case CPP_CLOSE_PAREN:
2378 if (!brace_depth && !paren_depth--)
2379 {
2380 if (consume_paren)
2381 cp_lexer_consume_token (parser->lexer);
2382 return 1;
2383 }
2384 break;
2385
2386 default:
2387 break;
2388 }
2389
2390 /* Consume the token. */
2391 cp_lexer_consume_token (parser->lexer);
2392 }
2393 }
2394
2395 /* Consume tokens until we reach the end of the current statement.
2396 Normally, that will be just before consuming a `;'. However, if a
2397 non-nested `}' comes first, then we stop before consuming that. */
2398
2399 static void
2400 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2401 {
2402 unsigned nesting_depth = 0;
2403
2404 while (true)
2405 {
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408 switch (token->type)
2409 {
2410 case CPP_EOF:
2411 case CPP_PRAGMA_EOL:
2412 /* If we've run out of tokens, stop. */
2413 return;
2414
2415 case CPP_SEMICOLON:
2416 /* If the next token is a `;', we have reached the end of the
2417 statement. */
2418 if (!nesting_depth)
2419 return;
2420 break;
2421
2422 case CPP_CLOSE_BRACE:
2423 /* If this is a non-nested '}', stop before consuming it.
2424 That way, when confronted with something like:
2425
2426 { 3 + }
2427
2428 we stop before consuming the closing '}', even though we
2429 have not yet reached a `;'. */
2430 if (nesting_depth == 0)
2431 return;
2432
2433 /* If it is the closing '}' for a block that we have
2434 scanned, stop -- but only after consuming the token.
2435 That way given:
2436
2437 void f g () { ... }
2438 typedef int I;
2439
2440 we will stop after the body of the erroneously declared
2441 function, but before consuming the following `typedef'
2442 declaration. */
2443 if (--nesting_depth == 0)
2444 {
2445 cp_lexer_consume_token (parser->lexer);
2446 return;
2447 }
2448
2449 case CPP_OPEN_BRACE:
2450 ++nesting_depth;
2451 break;
2452
2453 default:
2454 break;
2455 }
2456
2457 /* Consume the token. */
2458 cp_lexer_consume_token (parser->lexer);
2459 }
2460 }
2461
2462 /* This function is called at the end of a statement or declaration.
2463 If the next token is a semicolon, it is consumed; otherwise, error
2464 recovery is attempted. */
2465
2466 static void
2467 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2468 {
2469 /* Look for the trailing `;'. */
2470 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2471 {
2472 /* If there is additional (erroneous) input, skip to the end of
2473 the statement. */
2474 cp_parser_skip_to_end_of_statement (parser);
2475 /* If the next token is now a `;', consume it. */
2476 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2477 cp_lexer_consume_token (parser->lexer);
2478 }
2479 }
2480
2481 /* Skip tokens until we have consumed an entire block, or until we
2482 have consumed a non-nested `;'. */
2483
2484 static void
2485 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2486 {
2487 int nesting_depth = 0;
2488
2489 while (nesting_depth >= 0)
2490 {
2491 cp_token *token = cp_lexer_peek_token (parser->lexer);
2492
2493 switch (token->type)
2494 {
2495 case CPP_EOF:
2496 case CPP_PRAGMA_EOL:
2497 /* If we've run out of tokens, stop. */
2498 return;
2499
2500 case CPP_SEMICOLON:
2501 /* Stop if this is an unnested ';'. */
2502 if (!nesting_depth)
2503 nesting_depth = -1;
2504 break;
2505
2506 case CPP_CLOSE_BRACE:
2507 /* Stop if this is an unnested '}', or closes the outermost
2508 nesting level. */
2509 nesting_depth--;
2510 if (!nesting_depth)
2511 nesting_depth = -1;
2512 break;
2513
2514 case CPP_OPEN_BRACE:
2515 /* Nest. */
2516 nesting_depth++;
2517 break;
2518
2519 default:
2520 break;
2521 }
2522
2523 /* Consume the token. */
2524 cp_lexer_consume_token (parser->lexer);
2525 }
2526 }
2527
2528 /* Skip tokens until a non-nested closing curly brace is the next
2529 token. */
2530
2531 static void
2532 cp_parser_skip_to_closing_brace (cp_parser *parser)
2533 {
2534 unsigned nesting_depth = 0;
2535
2536 while (true)
2537 {
2538 cp_token *token = cp_lexer_peek_token (parser->lexer);
2539
2540 switch (token->type)
2541 {
2542 case CPP_EOF:
2543 case CPP_PRAGMA_EOL:
2544 /* If we've run out of tokens, stop. */
2545 return;
2546
2547 case CPP_CLOSE_BRACE:
2548 /* If the next token is a non-nested `}', then we have reached
2549 the end of the current block. */
2550 if (nesting_depth-- == 0)
2551 return;
2552 break;
2553
2554 case CPP_OPEN_BRACE:
2555 /* If it the next token is a `{', then we are entering a new
2556 block. Consume the entire block. */
2557 ++nesting_depth;
2558 break;
2559
2560 default:
2561 break;
2562 }
2563
2564 /* Consume the token. */
2565 cp_lexer_consume_token (parser->lexer);
2566 }
2567 }
2568
2569 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2570 parameter is the PRAGMA token, allowing us to purge the entire pragma
2571 sequence. */
2572
2573 static void
2574 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2575 {
2576 cp_token *token;
2577
2578 parser->lexer->in_pragma = false;
2579
2580 do
2581 token = cp_lexer_consume_token (parser->lexer);
2582 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2583
2584 /* Ensure that the pragma is not parsed again. */
2585 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2586 }
2587
2588 /* Require pragma end of line, resyncing with it as necessary. The
2589 arguments are as for cp_parser_skip_to_pragma_eol. */
2590
2591 static void
2592 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2593 {
2594 parser->lexer->in_pragma = false;
2595 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2596 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2597 }
2598
2599 /* This is a simple wrapper around make_typename_type. When the id is
2600 an unresolved identifier node, we can provide a superior diagnostic
2601 using cp_parser_diagnose_invalid_type_name. */
2602
2603 static tree
2604 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2605 {
2606 tree result;
2607 if (TREE_CODE (id) == IDENTIFIER_NODE)
2608 {
2609 result = make_typename_type (scope, id, typename_type,
2610 /*complain=*/tf_none);
2611 if (result == error_mark_node)
2612 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2613 return result;
2614 }
2615 return make_typename_type (scope, id, typename_type, tf_error);
2616 }
2617
2618
2619 /* Create a new C++ parser. */
2620
2621 static cp_parser *
2622 cp_parser_new (void)
2623 {
2624 cp_parser *parser;
2625 cp_lexer *lexer;
2626 unsigned i;
2627
2628 /* cp_lexer_new_main is called before calling ggc_alloc because
2629 cp_lexer_new_main might load a PCH file. */
2630 lexer = cp_lexer_new_main ();
2631
2632 /* Initialize the binops_by_token so that we can get the tree
2633 directly from the token. */
2634 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2635 binops_by_token[binops[i].token_type] = binops[i];
2636
2637 parser = GGC_CNEW (cp_parser);
2638 parser->lexer = lexer;
2639 parser->context = cp_parser_context_new (NULL);
2640
2641 /* For now, we always accept GNU extensions. */
2642 parser->allow_gnu_extensions_p = 1;
2643
2644 /* The `>' token is a greater-than operator, not the end of a
2645 template-id. */
2646 parser->greater_than_is_operator_p = true;
2647
2648 parser->default_arg_ok_p = true;
2649
2650 /* We are not parsing a constant-expression. */
2651 parser->integral_constant_expression_p = false;
2652 parser->allow_non_integral_constant_expression_p = false;
2653 parser->non_integral_constant_expression_p = false;
2654
2655 /* Local variable names are not forbidden. */
2656 parser->local_variables_forbidden_p = false;
2657
2658 /* We are not processing an `extern "C"' declaration. */
2659 parser->in_unbraced_linkage_specification_p = false;
2660
2661 /* We are not processing a declarator. */
2662 parser->in_declarator_p = false;
2663
2664 /* We are not processing a template-argument-list. */
2665 parser->in_template_argument_list_p = false;
2666
2667 /* We are not in an iteration statement. */
2668 parser->in_statement = 0;
2669
2670 /* We are not in a switch statement. */
2671 parser->in_switch_statement_p = false;
2672
2673 /* We are not parsing a type-id inside an expression. */
2674 parser->in_type_id_in_expr_p = false;
2675
2676 /* Declarations aren't implicitly extern "C". */
2677 parser->implicit_extern_c = false;
2678
2679 /* String literals should be translated to the execution character set. */
2680 parser->translate_strings_p = true;
2681
2682 /* We are not parsing a function body. */
2683 parser->in_function_body = false;
2684
2685 /* The unparsed function queue is empty. */
2686 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2687
2688 /* There are no classes being defined. */
2689 parser->num_classes_being_defined = 0;
2690
2691 /* No template parameters apply. */
2692 parser->num_template_parameter_lists = 0;
2693
2694 return parser;
2695 }
2696
2697 /* Create a cp_lexer structure which will emit the tokens in CACHE
2698 and push it onto the parser's lexer stack. This is used for delayed
2699 parsing of in-class method bodies and default arguments, and should
2700 not be confused with tentative parsing. */
2701 static void
2702 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2703 {
2704 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2705 lexer->next = parser->lexer;
2706 parser->lexer = lexer;
2707
2708 /* Move the current source position to that of the first token in the
2709 new lexer. */
2710 cp_lexer_set_source_position_from_token (lexer->next_token);
2711 }
2712
2713 /* Pop the top lexer off the parser stack. This is never used for the
2714 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2715 static void
2716 cp_parser_pop_lexer (cp_parser *parser)
2717 {
2718 cp_lexer *lexer = parser->lexer;
2719 parser->lexer = lexer->next;
2720 cp_lexer_destroy (lexer);
2721
2722 /* Put the current source position back where it was before this
2723 lexer was pushed. */
2724 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2725 }
2726
2727 /* Lexical conventions [gram.lex] */
2728
2729 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2730 identifier. */
2731
2732 static tree
2733 cp_parser_identifier (cp_parser* parser)
2734 {
2735 cp_token *token;
2736
2737 /* Look for the identifier. */
2738 token = cp_parser_require (parser, CPP_NAME, "identifier");
2739 /* Return the value. */
2740 return token ? token->u.value : error_mark_node;
2741 }
2742
2743 /* Parse a sequence of adjacent string constants. Returns a
2744 TREE_STRING representing the combined, nul-terminated string
2745 constant. If TRANSLATE is true, translate the string to the
2746 execution character set. If WIDE_OK is true, a wide string is
2747 invalid here.
2748
2749 C++98 [lex.string] says that if a narrow string literal token is
2750 adjacent to a wide string literal token, the behavior is undefined.
2751 However, C99 6.4.5p4 says that this results in a wide string literal.
2752 We follow C99 here, for consistency with the C front end.
2753
2754 This code is largely lifted from lex_string() in c-lex.c.
2755
2756 FUTURE: ObjC++ will need to handle @-strings here. */
2757 static tree
2758 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2759 {
2760 tree value;
2761 bool wide = false;
2762 size_t count;
2763 struct obstack str_ob;
2764 cpp_string str, istr, *strs;
2765 cp_token *tok;
2766
2767 tok = cp_lexer_peek_token (parser->lexer);
2768 if (!cp_parser_is_string_literal (tok))
2769 {
2770 cp_parser_error (parser, "expected string-literal");
2771 return error_mark_node;
2772 }
2773
2774 /* Try to avoid the overhead of creating and destroying an obstack
2775 for the common case of just one string. */
2776 if (!cp_parser_is_string_literal
2777 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2778 {
2779 cp_lexer_consume_token (parser->lexer);
2780
2781 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2782 str.len = TREE_STRING_LENGTH (tok->u.value);
2783 count = 1;
2784 if (tok->type == CPP_WSTRING)
2785 wide = true;
2786
2787 strs = &str;
2788 }
2789 else
2790 {
2791 gcc_obstack_init (&str_ob);
2792 count = 0;
2793
2794 do
2795 {
2796 cp_lexer_consume_token (parser->lexer);
2797 count++;
2798 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2799 str.len = TREE_STRING_LENGTH (tok->u.value);
2800 if (tok->type == CPP_WSTRING)
2801 wide = true;
2802
2803 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2804
2805 tok = cp_lexer_peek_token (parser->lexer);
2806 }
2807 while (cp_parser_is_string_literal (tok));
2808
2809 strs = (cpp_string *) obstack_finish (&str_ob);
2810 }
2811
2812 if (wide && !wide_ok)
2813 {
2814 cp_parser_error (parser, "a wide string is invalid in this context");
2815 wide = false;
2816 }
2817
2818 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2819 (parse_in, strs, count, &istr, wide))
2820 {
2821 value = build_string (istr.len, (char *)istr.text);
2822 free ((void *)istr.text);
2823
2824 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2825 value = fix_string_type (value);
2826 }
2827 else
2828 /* cpp_interpret_string has issued an error. */
2829 value = error_mark_node;
2830
2831 if (count > 1)
2832 obstack_free (&str_ob, 0);
2833
2834 return value;
2835 }
2836
2837
2838 /* Basic concepts [gram.basic] */
2839
2840 /* Parse a translation-unit.
2841
2842 translation-unit:
2843 declaration-seq [opt]
2844
2845 Returns TRUE if all went well. */
2846
2847 static bool
2848 cp_parser_translation_unit (cp_parser* parser)
2849 {
2850 /* The address of the first non-permanent object on the declarator
2851 obstack. */
2852 static void *declarator_obstack_base;
2853
2854 bool success;
2855
2856 /* Create the declarator obstack, if necessary. */
2857 if (!cp_error_declarator)
2858 {
2859 gcc_obstack_init (&declarator_obstack);
2860 /* Create the error declarator. */
2861 cp_error_declarator = make_declarator (cdk_error);
2862 /* Create the empty parameter list. */
2863 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2864 /* Remember where the base of the declarator obstack lies. */
2865 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2866 }
2867
2868 cp_parser_declaration_seq_opt (parser);
2869
2870 /* If there are no tokens left then all went well. */
2871 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2872 {
2873 /* Get rid of the token array; we don't need it any more. */
2874 cp_lexer_destroy (parser->lexer);
2875 parser->lexer = NULL;
2876
2877 /* This file might have been a context that's implicitly extern
2878 "C". If so, pop the lang context. (Only relevant for PCH.) */
2879 if (parser->implicit_extern_c)
2880 {
2881 pop_lang_context ();
2882 parser->implicit_extern_c = false;
2883 }
2884
2885 /* Finish up. */
2886 finish_translation_unit ();
2887
2888 success = true;
2889 }
2890 else
2891 {
2892 cp_parser_error (parser, "expected declaration");
2893 success = false;
2894 }
2895
2896 /* Make sure the declarator obstack was fully cleaned up. */
2897 gcc_assert (obstack_next_free (&declarator_obstack)
2898 == declarator_obstack_base);
2899
2900 /* All went well. */
2901 return success;
2902 }
2903
2904 /* Expressions [gram.expr] */
2905
2906 /* Parse a primary-expression.
2907
2908 primary-expression:
2909 literal
2910 this
2911 ( expression )
2912 id-expression
2913
2914 GNU Extensions:
2915
2916 primary-expression:
2917 ( compound-statement )
2918 __builtin_va_arg ( assignment-expression , type-id )
2919 __builtin_offsetof ( type-id , offsetof-expression )
2920
2921 Objective-C++ Extension:
2922
2923 primary-expression:
2924 objc-expression
2925
2926 literal:
2927 __null
2928
2929 ADDRESS_P is true iff this expression was immediately preceded by
2930 "&" and therefore might denote a pointer-to-member. CAST_P is true
2931 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2932 true iff this expression is a template argument.
2933
2934 Returns a representation of the expression. Upon return, *IDK
2935 indicates what kind of id-expression (if any) was present. */
2936
2937 static tree
2938 cp_parser_primary_expression (cp_parser *parser,
2939 bool address_p,
2940 bool cast_p,
2941 bool template_arg_p,
2942 cp_id_kind *idk)
2943 {
2944 cp_token *token;
2945
2946 /* Assume the primary expression is not an id-expression. */
2947 *idk = CP_ID_KIND_NONE;
2948
2949 /* Peek at the next token. */
2950 token = cp_lexer_peek_token (parser->lexer);
2951 switch (token->type)
2952 {
2953 /* literal:
2954 integer-literal
2955 character-literal
2956 floating-literal
2957 string-literal
2958 boolean-literal */
2959 case CPP_CHAR:
2960 case CPP_WCHAR:
2961 case CPP_NUMBER:
2962 token = cp_lexer_consume_token (parser->lexer);
2963 /* Floating-point literals are only allowed in an integral
2964 constant expression if they are cast to an integral or
2965 enumeration type. */
2966 if (TREE_CODE (token->u.value) == REAL_CST
2967 && parser->integral_constant_expression_p
2968 && pedantic)
2969 {
2970 /* CAST_P will be set even in invalid code like "int(2.7 +
2971 ...)". Therefore, we have to check that the next token
2972 is sure to end the cast. */
2973 if (cast_p)
2974 {
2975 cp_token *next_token;
2976
2977 next_token = cp_lexer_peek_token (parser->lexer);
2978 if (/* The comma at the end of an
2979 enumerator-definition. */
2980 next_token->type != CPP_COMMA
2981 /* The curly brace at the end of an enum-specifier. */
2982 && next_token->type != CPP_CLOSE_BRACE
2983 /* The end of a statement. */
2984 && next_token->type != CPP_SEMICOLON
2985 /* The end of the cast-expression. */
2986 && next_token->type != CPP_CLOSE_PAREN
2987 /* The end of an array bound. */
2988 && next_token->type != CPP_CLOSE_SQUARE
2989 /* The closing ">" in a template-argument-list. */
2990 && (next_token->type != CPP_GREATER
2991 || parser->greater_than_is_operator_p))
2992 cast_p = false;
2993 }
2994
2995 /* If we are within a cast, then the constraint that the
2996 cast is to an integral or enumeration type will be
2997 checked at that point. If we are not within a cast, then
2998 this code is invalid. */
2999 if (!cast_p)
3000 cp_parser_non_integral_constant_expression
3001 (parser, "floating-point literal");
3002 }
3003 return token->u.value;
3004
3005 case CPP_STRING:
3006 case CPP_WSTRING:
3007 /* ??? Should wide strings be allowed when parser->translate_strings_p
3008 is false (i.e. in attributes)? If not, we can kill the third
3009 argument to cp_parser_string_literal. */
3010 return cp_parser_string_literal (parser,
3011 parser->translate_strings_p,
3012 true);
3013
3014 case CPP_OPEN_PAREN:
3015 {
3016 tree expr;
3017 bool saved_greater_than_is_operator_p;
3018
3019 /* Consume the `('. */
3020 cp_lexer_consume_token (parser->lexer);
3021 /* Within a parenthesized expression, a `>' token is always
3022 the greater-than operator. */
3023 saved_greater_than_is_operator_p
3024 = parser->greater_than_is_operator_p;
3025 parser->greater_than_is_operator_p = true;
3026 /* If we see `( { ' then we are looking at the beginning of
3027 a GNU statement-expression. */
3028 if (cp_parser_allow_gnu_extensions_p (parser)
3029 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3030 {
3031 /* Statement-expressions are not allowed by the standard. */
3032 if (pedantic)
3033 pedwarn ("ISO C++ forbids braced-groups within expressions");
3034
3035 /* And they're not allowed outside of a function-body; you
3036 cannot, for example, write:
3037
3038 int i = ({ int j = 3; j + 1; });
3039
3040 at class or namespace scope. */
3041 if (!parser->in_function_body)
3042 {
3043 error ("statement-expressions are allowed only inside functions");
3044 cp_parser_skip_to_end_of_block_or_statement (parser);
3045 expr = error_mark_node;
3046 }
3047 else
3048 {
3049 /* Start the statement-expression. */
3050 expr = begin_stmt_expr ();
3051 /* Parse the compound-statement. */
3052 cp_parser_compound_statement (parser, expr, false);
3053 /* Finish up. */
3054 expr = finish_stmt_expr (expr, false);
3055 }
3056 }
3057 else
3058 {
3059 /* Parse the parenthesized expression. */
3060 expr = cp_parser_expression (parser, cast_p);
3061 /* Let the front end know that this expression was
3062 enclosed in parentheses. This matters in case, for
3063 example, the expression is of the form `A::B', since
3064 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3065 not. */
3066 finish_parenthesized_expr (expr);
3067 }
3068 /* The `>' token might be the end of a template-id or
3069 template-parameter-list now. */
3070 parser->greater_than_is_operator_p
3071 = saved_greater_than_is_operator_p;
3072 /* Consume the `)'. */
3073 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3074 cp_parser_skip_to_end_of_statement (parser);
3075
3076 return expr;
3077 }
3078
3079 case CPP_KEYWORD:
3080 switch (token->keyword)
3081 {
3082 /* These two are the boolean literals. */
3083 case RID_TRUE:
3084 cp_lexer_consume_token (parser->lexer);
3085 return boolean_true_node;
3086 case RID_FALSE:
3087 cp_lexer_consume_token (parser->lexer);
3088 return boolean_false_node;
3089
3090 /* The `__null' literal. */
3091 case RID_NULL:
3092 cp_lexer_consume_token (parser->lexer);
3093 return null_node;
3094
3095 /* Recognize the `this' keyword. */
3096 case RID_THIS:
3097 cp_lexer_consume_token (parser->lexer);
3098 if (parser->local_variables_forbidden_p)
3099 {
3100 error ("%<this%> may not be used in this context");
3101 return error_mark_node;
3102 }
3103 /* Pointers cannot appear in constant-expressions. */
3104 if (cp_parser_non_integral_constant_expression (parser,
3105 "`this'"))
3106 return error_mark_node;
3107 return finish_this_expr ();
3108
3109 /* The `operator' keyword can be the beginning of an
3110 id-expression. */
3111 case RID_OPERATOR:
3112 goto id_expression;
3113
3114 case RID_FUNCTION_NAME:
3115 case RID_PRETTY_FUNCTION_NAME:
3116 case RID_C99_FUNCTION_NAME:
3117 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3118 __func__ are the names of variables -- but they are
3119 treated specially. Therefore, they are handled here,
3120 rather than relying on the generic id-expression logic
3121 below. Grammatically, these names are id-expressions.
3122
3123 Consume the token. */
3124 token = cp_lexer_consume_token (parser->lexer);
3125 /* Look up the name. */
3126 return finish_fname (token->u.value);
3127
3128 case RID_VA_ARG:
3129 {
3130 tree expression;
3131 tree type;
3132
3133 /* The `__builtin_va_arg' construct is used to handle
3134 `va_arg'. Consume the `__builtin_va_arg' token. */
3135 cp_lexer_consume_token (parser->lexer);
3136 /* Look for the opening `('. */
3137 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3138 /* Now, parse the assignment-expression. */
3139 expression = cp_parser_assignment_expression (parser,
3140 /*cast_p=*/false);
3141 /* Look for the `,'. */
3142 cp_parser_require (parser, CPP_COMMA, "`,'");
3143 /* Parse the type-id. */
3144 type = cp_parser_type_id (parser);
3145 /* Look for the closing `)'. */
3146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3147 /* Using `va_arg' in a constant-expression is not
3148 allowed. */
3149 if (cp_parser_non_integral_constant_expression (parser,
3150 "`va_arg'"))
3151 return error_mark_node;
3152 return build_x_va_arg (expression, type);
3153 }
3154
3155 case RID_OFFSETOF:
3156 return cp_parser_builtin_offsetof (parser);
3157
3158 /* Objective-C++ expressions. */
3159 case RID_AT_ENCODE:
3160 case RID_AT_PROTOCOL:
3161 case RID_AT_SELECTOR:
3162 return cp_parser_objc_expression (parser);
3163
3164 default:
3165 cp_parser_error (parser, "expected primary-expression");
3166 return error_mark_node;
3167 }
3168
3169 /* An id-expression can start with either an identifier, a
3170 `::' as the beginning of a qualified-id, or the "operator"
3171 keyword. */
3172 case CPP_NAME:
3173 case CPP_SCOPE:
3174 case CPP_TEMPLATE_ID:
3175 case CPP_NESTED_NAME_SPECIFIER:
3176 {
3177 tree id_expression;
3178 tree decl;
3179 const char *error_msg;
3180 bool template_p;
3181 bool done;
3182
3183 id_expression:
3184 /* Parse the id-expression. */
3185 id_expression
3186 = cp_parser_id_expression (parser,
3187 /*template_keyword_p=*/false,
3188 /*check_dependency_p=*/true,
3189 &template_p,
3190 /*declarator_p=*/false,
3191 /*optional_p=*/false);
3192 if (id_expression == error_mark_node)
3193 return error_mark_node;
3194 token = cp_lexer_peek_token (parser->lexer);
3195 done = (token->type != CPP_OPEN_SQUARE
3196 && token->type != CPP_OPEN_PAREN
3197 && token->type != CPP_DOT
3198 && token->type != CPP_DEREF
3199 && token->type != CPP_PLUS_PLUS
3200 && token->type != CPP_MINUS_MINUS);
3201 /* If we have a template-id, then no further lookup is
3202 required. If the template-id was for a template-class, we
3203 will sometimes have a TYPE_DECL at this point. */
3204 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3205 || TREE_CODE (id_expression) == TYPE_DECL)
3206 decl = id_expression;
3207 /* Look up the name. */
3208 else
3209 {
3210 tree ambiguous_decls;
3211
3212 decl = cp_parser_lookup_name (parser, id_expression,
3213 none_type,
3214 template_p,
3215 /*is_namespace=*/false,
3216 /*check_dependency=*/true,
3217 &ambiguous_decls);
3218 /* If the lookup was ambiguous, an error will already have
3219 been issued. */
3220 if (ambiguous_decls)
3221 return error_mark_node;
3222
3223 /* In Objective-C++, an instance variable (ivar) may be preferred
3224 to whatever cp_parser_lookup_name() found. */
3225 decl = objc_lookup_ivar (decl, id_expression);
3226
3227 /* If name lookup gives us a SCOPE_REF, then the
3228 qualifying scope was dependent. */
3229 if (TREE_CODE (decl) == SCOPE_REF)
3230 return decl;
3231 /* Check to see if DECL is a local variable in a context
3232 where that is forbidden. */
3233 if (parser->local_variables_forbidden_p
3234 && local_variable_p (decl))
3235 {
3236 /* It might be that we only found DECL because we are
3237 trying to be generous with pre-ISO scoping rules.
3238 For example, consider:
3239
3240 int i;
3241 void g() {
3242 for (int i = 0; i < 10; ++i) {}
3243 extern void f(int j = i);
3244 }
3245
3246 Here, name look up will originally find the out
3247 of scope `i'. We need to issue a warning message,
3248 but then use the global `i'. */
3249 decl = check_for_out_of_scope_variable (decl);
3250 if (local_variable_p (decl))
3251 {
3252 error ("local variable %qD may not appear in this context",
3253 decl);
3254 return error_mark_node;
3255 }
3256 }
3257 }
3258
3259 decl = (finish_id_expression
3260 (id_expression, decl, parser->scope,
3261 idk,
3262 parser->integral_constant_expression_p,
3263 parser->allow_non_integral_constant_expression_p,
3264 &parser->non_integral_constant_expression_p,
3265 template_p, done, address_p,
3266 template_arg_p,
3267 &error_msg));
3268 if (error_msg)
3269 cp_parser_error (parser, error_msg);
3270 return decl;
3271 }
3272
3273 /* Anything else is an error. */
3274 default:
3275 /* ...unless we have an Objective-C++ message or string literal,
3276 that is. */
3277 if (c_dialect_objc ()
3278 && (token->type == CPP_OPEN_SQUARE
3279 || token->type == CPP_OBJC_STRING))
3280 return cp_parser_objc_expression (parser);
3281
3282 cp_parser_error (parser, "expected primary-expression");
3283 return error_mark_node;
3284 }
3285 }
3286
3287 /* Parse an id-expression.
3288
3289 id-expression:
3290 unqualified-id
3291 qualified-id
3292
3293 qualified-id:
3294 :: [opt] nested-name-specifier template [opt] unqualified-id
3295 :: identifier
3296 :: operator-function-id
3297 :: template-id
3298
3299 Return a representation of the unqualified portion of the
3300 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3301 a `::' or nested-name-specifier.
3302
3303 Often, if the id-expression was a qualified-id, the caller will
3304 want to make a SCOPE_REF to represent the qualified-id. This
3305 function does not do this in order to avoid wastefully creating
3306 SCOPE_REFs when they are not required.
3307
3308 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3309 `template' keyword.
3310
3311 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3312 uninstantiated templates.
3313
3314 If *TEMPLATE_P is non-NULL, it is set to true iff the
3315 `template' keyword is used to explicitly indicate that the entity
3316 named is a template.
3317
3318 If DECLARATOR_P is true, the id-expression is appearing as part of
3319 a declarator, rather than as part of an expression. */
3320
3321 static tree
3322 cp_parser_id_expression (cp_parser *parser,
3323 bool template_keyword_p,
3324 bool check_dependency_p,
3325 bool *template_p,
3326 bool declarator_p,
3327 bool optional_p)
3328 {
3329 bool global_scope_p;
3330 bool nested_name_specifier_p;
3331
3332 /* Assume the `template' keyword was not used. */
3333 if (template_p)
3334 *template_p = template_keyword_p;
3335
3336 /* Look for the optional `::' operator. */
3337 global_scope_p
3338 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3339 != NULL_TREE);
3340 /* Look for the optional nested-name-specifier. */
3341 nested_name_specifier_p
3342 = (cp_parser_nested_name_specifier_opt (parser,
3343 /*typename_keyword_p=*/false,
3344 check_dependency_p,
3345 /*type_p=*/false,
3346 declarator_p)
3347 != NULL_TREE);
3348 /* If there is a nested-name-specifier, then we are looking at
3349 the first qualified-id production. */
3350 if (nested_name_specifier_p)
3351 {
3352 tree saved_scope;
3353 tree saved_object_scope;
3354 tree saved_qualifying_scope;
3355 tree unqualified_id;
3356 bool is_template;
3357
3358 /* See if the next token is the `template' keyword. */
3359 if (!template_p)
3360 template_p = &is_template;
3361 *template_p = cp_parser_optional_template_keyword (parser);
3362 /* Name lookup we do during the processing of the
3363 unqualified-id might obliterate SCOPE. */
3364 saved_scope = parser->scope;
3365 saved_object_scope = parser->object_scope;
3366 saved_qualifying_scope = parser->qualifying_scope;
3367 /* Process the final unqualified-id. */
3368 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3369 check_dependency_p,
3370 declarator_p,
3371 /*optional_p=*/false);
3372 /* Restore the SAVED_SCOPE for our caller. */
3373 parser->scope = saved_scope;
3374 parser->object_scope = saved_object_scope;
3375 parser->qualifying_scope = saved_qualifying_scope;
3376
3377 return unqualified_id;
3378 }
3379 /* Otherwise, if we are in global scope, then we are looking at one
3380 of the other qualified-id productions. */
3381 else if (global_scope_p)
3382 {
3383 cp_token *token;
3384 tree id;
3385
3386 /* Peek at the next token. */
3387 token = cp_lexer_peek_token (parser->lexer);
3388
3389 /* If it's an identifier, and the next token is not a "<", then
3390 we can avoid the template-id case. This is an optimization
3391 for this common case. */
3392 if (token->type == CPP_NAME
3393 && !cp_parser_nth_token_starts_template_argument_list_p
3394 (parser, 2))
3395 return cp_parser_identifier (parser);
3396
3397 cp_parser_parse_tentatively (parser);
3398 /* Try a template-id. */
3399 id = cp_parser_template_id (parser,
3400 /*template_keyword_p=*/false,
3401 /*check_dependency_p=*/true,
3402 declarator_p);
3403 /* If that worked, we're done. */
3404 if (cp_parser_parse_definitely (parser))
3405 return id;
3406
3407 /* Peek at the next token. (Changes in the token buffer may
3408 have invalidated the pointer obtained above.) */
3409 token = cp_lexer_peek_token (parser->lexer);
3410
3411 switch (token->type)
3412 {
3413 case CPP_NAME:
3414 return cp_parser_identifier (parser);
3415
3416 case CPP_KEYWORD:
3417 if (token->keyword == RID_OPERATOR)
3418 return cp_parser_operator_function_id (parser);
3419 /* Fall through. */
3420
3421 default:
3422 cp_parser_error (parser, "expected id-expression");
3423 return error_mark_node;
3424 }
3425 }
3426 else
3427 return cp_parser_unqualified_id (parser, template_keyword_p,
3428 /*check_dependency_p=*/true,
3429 declarator_p,
3430 optional_p);
3431 }
3432
3433 /* Parse an unqualified-id.
3434
3435 unqualified-id:
3436 identifier
3437 operator-function-id
3438 conversion-function-id
3439 ~ class-name
3440 template-id
3441
3442 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3443 keyword, in a construct like `A::template ...'.
3444
3445 Returns a representation of unqualified-id. For the `identifier'
3446 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3447 production a BIT_NOT_EXPR is returned; the operand of the
3448 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3449 other productions, see the documentation accompanying the
3450 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3451 names are looked up in uninstantiated templates. If DECLARATOR_P
3452 is true, the unqualified-id is appearing as part of a declarator,
3453 rather than as part of an expression. */
3454
3455 static tree
3456 cp_parser_unqualified_id (cp_parser* parser,
3457 bool template_keyword_p,
3458 bool check_dependency_p,
3459 bool declarator_p,
3460 bool optional_p)
3461 {
3462 cp_token *token;
3463
3464 /* Peek at the next token. */
3465 token = cp_lexer_peek_token (parser->lexer);
3466
3467 switch (token->type)
3468 {
3469 case CPP_NAME:
3470 {
3471 tree id;
3472
3473 /* We don't know yet whether or not this will be a
3474 template-id. */
3475 cp_parser_parse_tentatively (parser);
3476 /* Try a template-id. */
3477 id = cp_parser_template_id (parser, template_keyword_p,
3478 check_dependency_p,
3479 declarator_p);
3480 /* If it worked, we're done. */
3481 if (cp_parser_parse_definitely (parser))
3482 return id;
3483 /* Otherwise, it's an ordinary identifier. */
3484 return cp_parser_identifier (parser);
3485 }
3486
3487 case CPP_TEMPLATE_ID:
3488 return cp_parser_template_id (parser, template_keyword_p,
3489 check_dependency_p,
3490 declarator_p);
3491
3492 case CPP_COMPL:
3493 {
3494 tree type_decl;
3495 tree qualifying_scope;
3496 tree object_scope;
3497 tree scope;
3498 bool done;
3499
3500 /* Consume the `~' token. */
3501 cp_lexer_consume_token (parser->lexer);
3502 /* Parse the class-name. The standard, as written, seems to
3503 say that:
3504
3505 template <typename T> struct S { ~S (); };
3506 template <typename T> S<T>::~S() {}
3507
3508 is invalid, since `~' must be followed by a class-name, but
3509 `S<T>' is dependent, and so not known to be a class.
3510 That's not right; we need to look in uninstantiated
3511 templates. A further complication arises from:
3512
3513 template <typename T> void f(T t) {
3514 t.T::~T();
3515 }
3516
3517 Here, it is not possible to look up `T' in the scope of `T'
3518 itself. We must look in both the current scope, and the
3519 scope of the containing complete expression.
3520
3521 Yet another issue is:
3522
3523 struct S {
3524 int S;
3525 ~S();
3526 };
3527
3528 S::~S() {}
3529
3530 The standard does not seem to say that the `S' in `~S'
3531 should refer to the type `S' and not the data member
3532 `S::S'. */
3533
3534 /* DR 244 says that we look up the name after the "~" in the
3535 same scope as we looked up the qualifying name. That idea
3536 isn't fully worked out; it's more complicated than that. */
3537 scope = parser->scope;
3538 object_scope = parser->object_scope;
3539 qualifying_scope = parser->qualifying_scope;
3540
3541 /* Check for invalid scopes. */
3542 if (scope == error_mark_node)
3543 {
3544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3545 cp_lexer_consume_token (parser->lexer);
3546 return error_mark_node;
3547 }
3548 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3549 {
3550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3551 error ("scope %qT before %<~%> is not a class-name", scope);
3552 cp_parser_simulate_error (parser);
3553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3554 cp_lexer_consume_token (parser->lexer);
3555 return error_mark_node;
3556 }
3557 gcc_assert (!scope || TYPE_P (scope));
3558
3559 /* If the name is of the form "X::~X" it's OK. */
3560 token = cp_lexer_peek_token (parser->lexer);
3561 if (scope
3562 && token->type == CPP_NAME
3563 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3564 == CPP_OPEN_PAREN)
3565 && constructor_name_p (token->u.value, scope))
3566 {
3567 cp_lexer_consume_token (parser->lexer);
3568 return build_nt (BIT_NOT_EXPR, scope);
3569 }
3570
3571 /* If there was an explicit qualification (S::~T), first look
3572 in the scope given by the qualification (i.e., S). */
3573 done = false;
3574 type_decl = NULL_TREE;
3575 if (scope)
3576 {
3577 cp_parser_parse_tentatively (parser);
3578 type_decl = cp_parser_class_name (parser,
3579 /*typename_keyword_p=*/false,
3580 /*template_keyword_p=*/false,
3581 none_type,
3582 /*check_dependency=*/false,
3583 /*class_head_p=*/false,
3584 declarator_p);
3585 if (cp_parser_parse_definitely (parser))
3586 done = true;
3587 }
3588 /* In "N::S::~S", look in "N" as well. */
3589 if (!done && scope && qualifying_scope)
3590 {
3591 cp_parser_parse_tentatively (parser);
3592 parser->scope = qualifying_scope;
3593 parser->object_scope = NULL_TREE;
3594 parser->qualifying_scope = NULL_TREE;
3595 type_decl
3596 = cp_parser_class_name (parser,
3597 /*typename_keyword_p=*/false,
3598 /*template_keyword_p=*/false,
3599 none_type,
3600 /*check_dependency=*/false,
3601 /*class_head_p=*/false,
3602 declarator_p);
3603 if (cp_parser_parse_definitely (parser))
3604 done = true;
3605 }
3606 /* In "p->S::~T", look in the scope given by "*p" as well. */
3607 else if (!done && object_scope)
3608 {
3609 cp_parser_parse_tentatively (parser);
3610 parser->scope = object_scope;
3611 parser->object_scope = NULL_TREE;
3612 parser->qualifying_scope = NULL_TREE;
3613 type_decl
3614 = cp_parser_class_name (parser,
3615 /*typename_keyword_p=*/false,
3616 /*template_keyword_p=*/false,
3617 none_type,
3618 /*check_dependency=*/false,
3619 /*class_head_p=*/false,
3620 declarator_p);
3621 if (cp_parser_parse_definitely (parser))
3622 done = true;
3623 }
3624 /* Look in the surrounding context. */
3625 if (!done)
3626 {
3627 parser->scope = NULL_TREE;
3628 parser->object_scope = NULL_TREE;
3629 parser->qualifying_scope = NULL_TREE;
3630 type_decl
3631 = cp_parser_class_name (parser,
3632 /*typename_keyword_p=*/false,
3633 /*template_keyword_p=*/false,
3634 none_type,
3635 /*check_dependency=*/false,
3636 /*class_head_p=*/false,
3637 declarator_p);
3638 }
3639 /* If an error occurred, assume that the name of the
3640 destructor is the same as the name of the qualifying
3641 class. That allows us to keep parsing after running
3642 into ill-formed destructor names. */
3643 if (type_decl == error_mark_node && scope)
3644 return build_nt (BIT_NOT_EXPR, scope);
3645 else if (type_decl == error_mark_node)
3646 return error_mark_node;
3647
3648 /* Check that destructor name and scope match. */
3649 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3650 {
3651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3652 error ("declaration of %<~%T%> as member of %qT",
3653 type_decl, scope);
3654 cp_parser_simulate_error (parser);
3655 return error_mark_node;
3656 }
3657
3658 /* [class.dtor]
3659
3660 A typedef-name that names a class shall not be used as the
3661 identifier in the declarator for a destructor declaration. */
3662 if (declarator_p
3663 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3664 && !DECL_SELF_REFERENCE_P (type_decl)
3665 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3666 error ("typedef-name %qD used as destructor declarator",
3667 type_decl);
3668
3669 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3670 }
3671
3672 case CPP_KEYWORD:
3673 if (token->keyword == RID_OPERATOR)
3674 {
3675 tree id;
3676
3677 /* This could be a template-id, so we try that first. */
3678 cp_parser_parse_tentatively (parser);
3679 /* Try a template-id. */
3680 id = cp_parser_template_id (parser, template_keyword_p,
3681 /*check_dependency_p=*/true,
3682 declarator_p);
3683 /* If that worked, we're done. */
3684 if (cp_parser_parse_definitely (parser))
3685 return id;
3686 /* We still don't know whether we're looking at an
3687 operator-function-id or a conversion-function-id. */
3688 cp_parser_parse_tentatively (parser);
3689 /* Try an operator-function-id. */
3690 id = cp_parser_operator_function_id (parser);
3691 /* If that didn't work, try a conversion-function-id. */
3692 if (!cp_parser_parse_definitely (parser))
3693 id = cp_parser_conversion_function_id (parser);
3694
3695 return id;
3696 }
3697 /* Fall through. */
3698
3699 default:
3700 if (optional_p)
3701 return NULL_TREE;
3702 cp_parser_error (parser, "expected unqualified-id");
3703 return error_mark_node;
3704 }
3705 }
3706
3707 /* Parse an (optional) nested-name-specifier.
3708
3709 nested-name-specifier:
3710 class-or-namespace-name :: nested-name-specifier [opt]
3711 class-or-namespace-name :: template nested-name-specifier [opt]
3712
3713 PARSER->SCOPE should be set appropriately before this function is
3714 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3715 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3716 in name lookups.
3717
3718 Sets PARSER->SCOPE to the class (TYPE) or namespace
3719 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3720 it unchanged if there is no nested-name-specifier. Returns the new
3721 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3722
3723 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3724 part of a declaration and/or decl-specifier. */
3725
3726 static tree
3727 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3728 bool typename_keyword_p,
3729 bool check_dependency_p,
3730 bool type_p,
3731 bool is_declaration)
3732 {
3733 bool success = false;
3734 cp_token_position start = 0;
3735 cp_token *token;
3736
3737 /* Remember where the nested-name-specifier starts. */
3738 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3739 {
3740 start = cp_lexer_token_position (parser->lexer, false);
3741 push_deferring_access_checks (dk_deferred);
3742 }
3743
3744 while (true)
3745 {
3746 tree new_scope;
3747 tree old_scope;
3748 tree saved_qualifying_scope;
3749 bool template_keyword_p;
3750
3751 /* Spot cases that cannot be the beginning of a
3752 nested-name-specifier. */
3753 token = cp_lexer_peek_token (parser->lexer);
3754
3755 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3756 the already parsed nested-name-specifier. */
3757 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3758 {
3759 /* Grab the nested-name-specifier and continue the loop. */
3760 cp_parser_pre_parsed_nested_name_specifier (parser);
3761 /* If we originally encountered this nested-name-specifier
3762 with IS_DECLARATION set to false, we will not have
3763 resolved TYPENAME_TYPEs, so we must do so here. */
3764 if (is_declaration
3765 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3766 {
3767 new_scope = resolve_typename_type (parser->scope,
3768 /*only_current_p=*/false);
3769 if (new_scope != error_mark_node)
3770 parser->scope = new_scope;
3771 }
3772 success = true;
3773 continue;
3774 }
3775
3776 /* Spot cases that cannot be the beginning of a
3777 nested-name-specifier. On the second and subsequent times
3778 through the loop, we look for the `template' keyword. */
3779 if (success && token->keyword == RID_TEMPLATE)
3780 ;
3781 /* A template-id can start a nested-name-specifier. */
3782 else if (token->type == CPP_TEMPLATE_ID)
3783 ;
3784 else
3785 {
3786 /* If the next token is not an identifier, then it is
3787 definitely not a class-or-namespace-name. */
3788 if (token->type != CPP_NAME)
3789 break;
3790 /* If the following token is neither a `<' (to begin a
3791 template-id), nor a `::', then we are not looking at a
3792 nested-name-specifier. */
3793 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3794 if (token->type != CPP_SCOPE
3795 && !cp_parser_nth_token_starts_template_argument_list_p
3796 (parser, 2))
3797 break;
3798 }
3799
3800 /* The nested-name-specifier is optional, so we parse
3801 tentatively. */
3802 cp_parser_parse_tentatively (parser);
3803
3804 /* Look for the optional `template' keyword, if this isn't the
3805 first time through the loop. */
3806 if (success)
3807 template_keyword_p = cp_parser_optional_template_keyword (parser);
3808 else
3809 template_keyword_p = false;
3810
3811 /* Save the old scope since the name lookup we are about to do
3812 might destroy it. */
3813 old_scope = parser->scope;
3814 saved_qualifying_scope = parser->qualifying_scope;
3815 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3816 look up names in "X<T>::I" in order to determine that "Y" is
3817 a template. So, if we have a typename at this point, we make
3818 an effort to look through it. */
3819 if (is_declaration
3820 && !typename_keyword_p
3821 && parser->scope
3822 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3823 parser->scope = resolve_typename_type (parser->scope,
3824 /*only_current_p=*/false);
3825 /* Parse the qualifying entity. */
3826 new_scope
3827 = cp_parser_class_or_namespace_name (parser,
3828 typename_keyword_p,
3829 template_keyword_p,
3830 check_dependency_p,
3831 type_p,
3832 is_declaration);
3833 /* Look for the `::' token. */
3834 cp_parser_require (parser, CPP_SCOPE, "`::'");
3835
3836 /* If we found what we wanted, we keep going; otherwise, we're
3837 done. */
3838 if (!cp_parser_parse_definitely (parser))
3839 {
3840 bool error_p = false;
3841
3842 /* Restore the OLD_SCOPE since it was valid before the
3843 failed attempt at finding the last
3844 class-or-namespace-name. */
3845 parser->scope = old_scope;
3846 parser->qualifying_scope = saved_qualifying_scope;
3847 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3848 break;
3849 /* If the next token is an identifier, and the one after
3850 that is a `::', then any valid interpretation would have
3851 found a class-or-namespace-name. */
3852 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3853 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3854 == CPP_SCOPE)
3855 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3856 != CPP_COMPL))
3857 {
3858 token = cp_lexer_consume_token (parser->lexer);
3859 if (!error_p)
3860 {
3861 if (!token->ambiguous_p)
3862 {
3863 tree decl;
3864 tree ambiguous_decls;
3865
3866 decl = cp_parser_lookup_name (parser, token->u.value,
3867 none_type,
3868 /*is_template=*/false,
3869 /*is_namespace=*/false,
3870 /*check_dependency=*/true,
3871 &ambiguous_decls);
3872 if (TREE_CODE (decl) == TEMPLATE_DECL)
3873 error ("%qD used without template parameters", decl);
3874 else if (ambiguous_decls)
3875 {
3876 error ("reference to %qD is ambiguous",
3877 token->u.value);
3878 print_candidates (ambiguous_decls);
3879 decl = error_mark_node;
3880 }
3881 else
3882 cp_parser_name_lookup_error
3883 (parser, token->u.value, decl,
3884 "is not a class or namespace");
3885 }
3886 parser->scope = error_mark_node;
3887 error_p = true;
3888 /* Treat this as a successful nested-name-specifier
3889 due to:
3890
3891 [basic.lookup.qual]
3892
3893 If the name found is not a class-name (clause
3894 _class_) or namespace-name (_namespace.def_), the
3895 program is ill-formed. */
3896 success = true;
3897 }
3898 cp_lexer_consume_token (parser->lexer);
3899 }
3900 break;
3901 }
3902 /* We've found one valid nested-name-specifier. */
3903 success = true;
3904 /* Name lookup always gives us a DECL. */
3905 if (TREE_CODE (new_scope) == TYPE_DECL)
3906 new_scope = TREE_TYPE (new_scope);
3907 /* Uses of "template" must be followed by actual templates. */
3908 if (template_keyword_p
3909 && !(CLASS_TYPE_P (new_scope)
3910 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3911 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3912 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3913 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3914 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3915 == TEMPLATE_ID_EXPR)))
3916 pedwarn (TYPE_P (new_scope)
3917 ? "%qT is not a template"
3918 : "%qD is not a template",
3919 new_scope);
3920 /* If it is a class scope, try to complete it; we are about to
3921 be looking up names inside the class. */
3922 if (TYPE_P (new_scope)
3923 /* Since checking types for dependency can be expensive,
3924 avoid doing it if the type is already complete. */
3925 && !COMPLETE_TYPE_P (new_scope)
3926 /* Do not try to complete dependent types. */
3927 && !dependent_type_p (new_scope))
3928 new_scope = complete_type (new_scope);
3929 /* Make sure we look in the right scope the next time through
3930 the loop. */
3931 parser->scope = new_scope;
3932 }
3933
3934 /* If parsing tentatively, replace the sequence of tokens that makes
3935 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3936 token. That way, should we re-parse the token stream, we will
3937 not have to repeat the effort required to do the parse, nor will
3938 we issue duplicate error messages. */
3939 if (success && start)
3940 {
3941 cp_token *token;
3942
3943 token = cp_lexer_token_at (parser->lexer, start);
3944 /* Reset the contents of the START token. */
3945 token->type = CPP_NESTED_NAME_SPECIFIER;
3946 /* Retrieve any deferred checks. Do not pop this access checks yet
3947 so the memory will not be reclaimed during token replacing below. */
3948 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3949 token->u.tree_check_value->value = parser->scope;
3950 token->u.tree_check_value->checks = get_deferred_access_checks ();
3951 token->u.tree_check_value->qualifying_scope =
3952 parser->qualifying_scope;
3953 token->keyword = RID_MAX;
3954
3955 /* Purge all subsequent tokens. */
3956 cp_lexer_purge_tokens_after (parser->lexer, start);
3957 }
3958
3959 if (start)
3960 pop_to_parent_deferring_access_checks ();
3961
3962 return success ? parser->scope : NULL_TREE;
3963 }
3964
3965 /* Parse a nested-name-specifier. See
3966 cp_parser_nested_name_specifier_opt for details. This function
3967 behaves identically, except that it will an issue an error if no
3968 nested-name-specifier is present. */
3969
3970 static tree
3971 cp_parser_nested_name_specifier (cp_parser *parser,
3972 bool typename_keyword_p,
3973 bool check_dependency_p,
3974 bool type_p,
3975 bool is_declaration)
3976 {
3977 tree scope;
3978
3979 /* Look for the nested-name-specifier. */
3980 scope = cp_parser_nested_name_specifier_opt (parser,
3981 typename_keyword_p,
3982 check_dependency_p,
3983 type_p,
3984 is_declaration);
3985 /* If it was not present, issue an error message. */
3986 if (!scope)
3987 {
3988 cp_parser_error (parser, "expected nested-name-specifier");
3989 parser->scope = NULL_TREE;
3990 }
3991
3992 return scope;
3993 }
3994
3995 /* Parse a class-or-namespace-name.
3996
3997 class-or-namespace-name:
3998 class-name
3999 namespace-name
4000
4001 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4002 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4003 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4004 TYPE_P is TRUE iff the next name should be taken as a class-name,
4005 even the same name is declared to be another entity in the same
4006 scope.
4007
4008 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4009 specified by the class-or-namespace-name. If neither is found the
4010 ERROR_MARK_NODE is returned. */
4011
4012 static tree
4013 cp_parser_class_or_namespace_name (cp_parser *parser,
4014 bool typename_keyword_p,
4015 bool template_keyword_p,
4016 bool check_dependency_p,
4017 bool type_p,
4018 bool is_declaration)
4019 {
4020 tree saved_scope;
4021 tree saved_qualifying_scope;
4022 tree saved_object_scope;
4023 tree scope;
4024 bool only_class_p;
4025
4026 /* Before we try to parse the class-name, we must save away the
4027 current PARSER->SCOPE since cp_parser_class_name will destroy
4028 it. */
4029 saved_scope = parser->scope;
4030 saved_qualifying_scope = parser->qualifying_scope;
4031 saved_object_scope = parser->object_scope;
4032 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4033 there is no need to look for a namespace-name. */
4034 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4035 if (!only_class_p)
4036 cp_parser_parse_tentatively (parser);
4037 scope = cp_parser_class_name (parser,
4038 typename_keyword_p,
4039 template_keyword_p,
4040 type_p ? class_type : none_type,
4041 check_dependency_p,
4042 /*class_head_p=*/false,
4043 is_declaration);
4044 /* If that didn't work, try for a namespace-name. */
4045 if (!only_class_p && !cp_parser_parse_definitely (parser))
4046 {
4047 /* Restore the saved scope. */
4048 parser->scope = saved_scope;
4049 parser->qualifying_scope = saved_qualifying_scope;
4050 parser->object_scope = saved_object_scope;
4051 /* If we are not looking at an identifier followed by the scope
4052 resolution operator, then this is not part of a
4053 nested-name-specifier. (Note that this function is only used
4054 to parse the components of a nested-name-specifier.) */
4055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4056 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4057 return error_mark_node;
4058 scope = cp_parser_namespace_name (parser);
4059 }
4060
4061 return scope;
4062 }
4063
4064 /* Parse a postfix-expression.
4065
4066 postfix-expression:
4067 primary-expression
4068 postfix-expression [ expression ]
4069 postfix-expression ( expression-list [opt] )
4070 simple-type-specifier ( expression-list [opt] )
4071 typename :: [opt] nested-name-specifier identifier
4072 ( expression-list [opt] )
4073 typename :: [opt] nested-name-specifier template [opt] template-id
4074 ( expression-list [opt] )
4075 postfix-expression . template [opt] id-expression
4076 postfix-expression -> template [opt] id-expression
4077 postfix-expression . pseudo-destructor-name
4078 postfix-expression -> pseudo-destructor-name
4079 postfix-expression ++
4080 postfix-expression --
4081 dynamic_cast < type-id > ( expression )
4082 static_cast < type-id > ( expression )
4083 reinterpret_cast < type-id > ( expression )
4084 const_cast < type-id > ( expression )
4085 typeid ( expression )
4086 typeid ( type-id )
4087
4088 GNU Extension:
4089
4090 postfix-expression:
4091 ( type-id ) { initializer-list , [opt] }
4092
4093 This extension is a GNU version of the C99 compound-literal
4094 construct. (The C99 grammar uses `type-name' instead of `type-id',
4095 but they are essentially the same concept.)
4096
4097 If ADDRESS_P is true, the postfix expression is the operand of the
4098 `&' operator. CAST_P is true if this expression is the target of a
4099 cast.
4100
4101 Returns a representation of the expression. */
4102
4103 static tree
4104 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4105 {
4106 cp_token *token;
4107 enum rid keyword;
4108 cp_id_kind idk = CP_ID_KIND_NONE;
4109 tree postfix_expression = NULL_TREE;
4110
4111 /* Peek at the next token. */
4112 token = cp_lexer_peek_token (parser->lexer);
4113 /* Some of the productions are determined by keywords. */
4114 keyword = token->keyword;
4115 switch (keyword)
4116 {
4117 case RID_DYNCAST:
4118 case RID_STATCAST:
4119 case RID_REINTCAST:
4120 case RID_CONSTCAST:
4121 {
4122 tree type;
4123 tree expression;
4124 const char *saved_message;
4125
4126 /* All of these can be handled in the same way from the point
4127 of view of parsing. Begin by consuming the token
4128 identifying the cast. */
4129 cp_lexer_consume_token (parser->lexer);
4130
4131 /* New types cannot be defined in the cast. */
4132 saved_message = parser->type_definition_forbidden_message;
4133 parser->type_definition_forbidden_message
4134 = "types may not be defined in casts";
4135
4136 /* Look for the opening `<'. */
4137 cp_parser_require (parser, CPP_LESS, "`<'");
4138 /* Parse the type to which we are casting. */
4139 type = cp_parser_type_id (parser);
4140 /* Look for the closing `>'. */
4141 cp_parser_require (parser, CPP_GREATER, "`>'");
4142 /* Restore the old message. */
4143 parser->type_definition_forbidden_message = saved_message;
4144
4145 /* And the expression which is being cast. */
4146 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4147 expression = cp_parser_expression (parser, /*cast_p=*/true);
4148 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4149
4150 /* Only type conversions to integral or enumeration types
4151 can be used in constant-expressions. */
4152 if (!cast_valid_in_integral_constant_expression_p (type)
4153 && (cp_parser_non_integral_constant_expression
4154 (parser,
4155 "a cast to a type other than an integral or "
4156 "enumeration type")))
4157 return error_mark_node;
4158
4159 switch (keyword)
4160 {
4161 case RID_DYNCAST:
4162 postfix_expression
4163 = build_dynamic_cast (type, expression);
4164 break;
4165 case RID_STATCAST:
4166 postfix_expression
4167 = build_static_cast (type, expression);
4168 break;
4169 case RID_REINTCAST:
4170 postfix_expression
4171 = build_reinterpret_cast (type, expression);
4172 break;
4173 case RID_CONSTCAST:
4174 postfix_expression
4175 = build_const_cast (type, expression);
4176 break;
4177 default:
4178 gcc_unreachable ();
4179 }
4180 }
4181 break;
4182
4183 case RID_TYPEID:
4184 {
4185 tree type;
4186 const char *saved_message;
4187 bool saved_in_type_id_in_expr_p;
4188
4189 /* Consume the `typeid' token. */
4190 cp_lexer_consume_token (parser->lexer);
4191 /* Look for the `(' token. */
4192 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4193 /* Types cannot be defined in a `typeid' expression. */
4194 saved_message = parser->type_definition_forbidden_message;
4195 parser->type_definition_forbidden_message
4196 = "types may not be defined in a `typeid\' expression";
4197 /* We can't be sure yet whether we're looking at a type-id or an
4198 expression. */
4199 cp_parser_parse_tentatively (parser);
4200 /* Try a type-id first. */
4201 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4202 parser->in_type_id_in_expr_p = true;
4203 type = cp_parser_type_id (parser);
4204 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4205 /* Look for the `)' token. Otherwise, we can't be sure that
4206 we're not looking at an expression: consider `typeid (int
4207 (3))', for example. */
4208 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4209 /* If all went well, simply lookup the type-id. */
4210 if (cp_parser_parse_definitely (parser))
4211 postfix_expression = get_typeid (type);
4212 /* Otherwise, fall back to the expression variant. */
4213 else
4214 {
4215 tree expression;
4216
4217 /* Look for an expression. */
4218 expression = cp_parser_expression (parser, /*cast_p=*/false);
4219 /* Compute its typeid. */
4220 postfix_expression = build_typeid (expression);
4221 /* Look for the `)' token. */
4222 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4223 }
4224 /* Restore the saved message. */
4225 parser->type_definition_forbidden_message = saved_message;
4226 /* `typeid' may not appear in an integral constant expression. */
4227 if (cp_parser_non_integral_constant_expression(parser,
4228 "`typeid' operator"))
4229 return error_mark_node;
4230 }
4231 break;
4232
4233 case RID_TYPENAME:
4234 {
4235 tree type;
4236 /* The syntax permitted here is the same permitted for an
4237 elaborated-type-specifier. */
4238 type = cp_parser_elaborated_type_specifier (parser,
4239 /*is_friend=*/false,
4240 /*is_declaration=*/false);
4241 postfix_expression = cp_parser_functional_cast (parser, type);
4242 }
4243 break;
4244
4245 default:
4246 {
4247 tree type;
4248
4249 /* If the next thing is a simple-type-specifier, we may be
4250 looking at a functional cast. We could also be looking at
4251 an id-expression. So, we try the functional cast, and if
4252 that doesn't work we fall back to the primary-expression. */
4253 cp_parser_parse_tentatively (parser);
4254 /* Look for the simple-type-specifier. */
4255 type = cp_parser_simple_type_specifier (parser,
4256 /*decl_specs=*/NULL,
4257 CP_PARSER_FLAGS_NONE);
4258 /* Parse the cast itself. */
4259 if (!cp_parser_error_occurred (parser))
4260 postfix_expression
4261 = cp_parser_functional_cast (parser, type);
4262 /* If that worked, we're done. */
4263 if (cp_parser_parse_definitely (parser))
4264 break;
4265
4266 /* If the functional-cast didn't work out, try a
4267 compound-literal. */
4268 if (cp_parser_allow_gnu_extensions_p (parser)
4269 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4270 {
4271 VEC(constructor_elt,gc) *initializer_list = NULL;
4272 bool saved_in_type_id_in_expr_p;
4273
4274 cp_parser_parse_tentatively (parser);
4275 /* Consume the `('. */
4276 cp_lexer_consume_token (parser->lexer);
4277 /* Parse the type. */
4278 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4279 parser->in_type_id_in_expr_p = true;
4280 type = cp_parser_type_id (parser);
4281 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4282 /* Look for the `)'. */
4283 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4284 /* Look for the `{'. */
4285 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4286 /* If things aren't going well, there's no need to
4287 keep going. */
4288 if (!cp_parser_error_occurred (parser))
4289 {
4290 bool non_constant_p;
4291 /* Parse the initializer-list. */
4292 initializer_list
4293 = cp_parser_initializer_list (parser, &non_constant_p);
4294 /* Allow a trailing `,'. */
4295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4296 cp_lexer_consume_token (parser->lexer);
4297 /* Look for the final `}'. */
4298 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4299 }
4300 /* If that worked, we're definitely looking at a
4301 compound-literal expression. */
4302 if (cp_parser_parse_definitely (parser))
4303 {
4304 /* Warn the user that a compound literal is not
4305 allowed in standard C++. */
4306 if (pedantic)
4307 pedwarn ("ISO C++ forbids compound-literals");
4308 /* Form the representation of the compound-literal. */
4309 postfix_expression
4310 = finish_compound_literal (type, initializer_list);
4311 break;
4312 }
4313 }
4314
4315 /* It must be a primary-expression. */
4316 postfix_expression
4317 = cp_parser_primary_expression (parser, address_p, cast_p,
4318 /*template_arg_p=*/false,
4319 &idk);
4320 }
4321 break;
4322 }
4323
4324 /* Keep looping until the postfix-expression is complete. */
4325 while (true)
4326 {
4327 if (idk == CP_ID_KIND_UNQUALIFIED
4328 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4329 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4330 /* It is not a Koenig lookup function call. */
4331 postfix_expression
4332 = unqualified_name_lookup_error (postfix_expression);
4333
4334 /* Peek at the next token. */
4335 token = cp_lexer_peek_token (parser->lexer);
4336
4337 switch (token->type)
4338 {
4339 case CPP_OPEN_SQUARE:
4340 postfix_expression
4341 = cp_parser_postfix_open_square_expression (parser,
4342 postfix_expression,
4343 false);
4344 idk = CP_ID_KIND_NONE;
4345 break;
4346
4347 case CPP_OPEN_PAREN:
4348 /* postfix-expression ( expression-list [opt] ) */
4349 {
4350 bool koenig_p;
4351 bool is_builtin_constant_p;
4352 bool saved_integral_constant_expression_p = false;
4353 bool saved_non_integral_constant_expression_p = false;
4354 tree args;
4355
4356 is_builtin_constant_p
4357 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4358 if (is_builtin_constant_p)
4359 {
4360 /* The whole point of __builtin_constant_p is to allow
4361 non-constant expressions to appear as arguments. */
4362 saved_integral_constant_expression_p
4363 = parser->integral_constant_expression_p;
4364 saved_non_integral_constant_expression_p
4365 = parser->non_integral_constant_expression_p;
4366 parser->integral_constant_expression_p = false;
4367 }
4368 args = (cp_parser_parenthesized_expression_list
4369 (parser, /*is_attribute_list=*/false,
4370 /*cast_p=*/false,
4371 /*non_constant_p=*/NULL));
4372 if (is_builtin_constant_p)
4373 {
4374 parser->integral_constant_expression_p
4375 = saved_integral_constant_expression_p;
4376 parser->non_integral_constant_expression_p
4377 = saved_non_integral_constant_expression_p;
4378 }
4379
4380 if (args == error_mark_node)
4381 {
4382 postfix_expression = error_mark_node;
4383 break;
4384 }
4385
4386 /* Function calls are not permitted in
4387 constant-expressions. */
4388 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4389 && cp_parser_non_integral_constant_expression (parser,
4390 "a function call"))
4391 {
4392 postfix_expression = error_mark_node;
4393 break;
4394 }
4395
4396 koenig_p = false;
4397 if (idk == CP_ID_KIND_UNQUALIFIED)
4398 {
4399 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4400 {
4401 if (args)
4402 {
4403 koenig_p = true;
4404 postfix_expression
4405 = perform_koenig_lookup (postfix_expression, args);
4406 }
4407 else
4408 postfix_expression
4409 = unqualified_fn_lookup_error (postfix_expression);
4410 }
4411 /* We do not perform argument-dependent lookup if
4412 normal lookup finds a non-function, in accordance
4413 with the expected resolution of DR 218. */
4414 else if (args && is_overloaded_fn (postfix_expression))
4415 {
4416 tree fn = get_first_fn (postfix_expression);
4417
4418 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4419 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4420
4421 /* Only do argument dependent lookup if regular
4422 lookup does not find a set of member functions.
4423 [basic.lookup.koenig]/2a */
4424 if (!DECL_FUNCTION_MEMBER_P (fn))
4425 {
4426 koenig_p = true;
4427 postfix_expression
4428 = perform_koenig_lookup (postfix_expression, args);
4429 }
4430 }
4431 }
4432
4433 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4434 {
4435 tree instance = TREE_OPERAND (postfix_expression, 0);
4436 tree fn = TREE_OPERAND (postfix_expression, 1);
4437
4438 if (processing_template_decl
4439 && (type_dependent_expression_p (instance)
4440 || (!BASELINK_P (fn)
4441 && TREE_CODE (fn) != FIELD_DECL)
4442 || type_dependent_expression_p (fn)
4443 || any_type_dependent_arguments_p (args)))
4444 {
4445 postfix_expression
4446 = build_min_nt (CALL_EXPR, postfix_expression,
4447 args, NULL_TREE);
4448 break;
4449 }
4450
4451 if (BASELINK_P (fn))
4452 postfix_expression
4453 = (build_new_method_call
4454 (instance, fn, args, NULL_TREE,
4455 (idk == CP_ID_KIND_QUALIFIED
4456 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4457 /*fn_p=*/NULL));
4458 else
4459 postfix_expression
4460 = finish_call_expr (postfix_expression, args,
4461 /*disallow_virtual=*/false,
4462 /*koenig_p=*/false);
4463 }
4464 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4465 || TREE_CODE (postfix_expression) == MEMBER_REF
4466 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4467 postfix_expression = (build_offset_ref_call_from_tree
4468 (postfix_expression, args));
4469 else if (idk == CP_ID_KIND_QUALIFIED)
4470 /* A call to a static class member, or a namespace-scope
4471 function. */
4472 postfix_expression
4473 = finish_call_expr (postfix_expression, args,
4474 /*disallow_virtual=*/true,
4475 koenig_p);
4476 else
4477 /* All other function calls. */
4478 postfix_expression
4479 = finish_call_expr (postfix_expression, args,
4480 /*disallow_virtual=*/false,
4481 koenig_p);
4482
4483 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4484 idk = CP_ID_KIND_NONE;
4485 }
4486 break;
4487
4488 case CPP_DOT:
4489 case CPP_DEREF:
4490 /* postfix-expression . template [opt] id-expression
4491 postfix-expression . pseudo-destructor-name
4492 postfix-expression -> template [opt] id-expression
4493 postfix-expression -> pseudo-destructor-name */
4494
4495 /* Consume the `.' or `->' operator. */
4496 cp_lexer_consume_token (parser->lexer);
4497
4498 postfix_expression
4499 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4500 postfix_expression,
4501 false, &idk);
4502 break;
4503
4504 case CPP_PLUS_PLUS:
4505 /* postfix-expression ++ */
4506 /* Consume the `++' token. */
4507 cp_lexer_consume_token (parser->lexer);
4508 /* Generate a representation for the complete expression. */
4509 postfix_expression
4510 = finish_increment_expr (postfix_expression,
4511 POSTINCREMENT_EXPR);
4512 /* Increments may not appear in constant-expressions. */
4513 if (cp_parser_non_integral_constant_expression (parser,
4514 "an increment"))
4515 postfix_expression = error_mark_node;
4516 idk = CP_ID_KIND_NONE;
4517 break;
4518
4519 case CPP_MINUS_MINUS:
4520 /* postfix-expression -- */
4521 /* Consume the `--' token. */
4522 cp_lexer_consume_token (parser->lexer);
4523 /* Generate a representation for the complete expression. */
4524 postfix_expression
4525 = finish_increment_expr (postfix_expression,
4526 POSTDECREMENT_EXPR);
4527 /* Decrements may not appear in constant-expressions. */
4528 if (cp_parser_non_integral_constant_expression (parser,
4529 "a decrement"))
4530 postfix_expression = error_mark_node;
4531 idk = CP_ID_KIND_NONE;
4532 break;
4533
4534 default:
4535 return postfix_expression;
4536 }
4537 }
4538
4539 /* We should never get here. */
4540 gcc_unreachable ();
4541 return error_mark_node;
4542 }
4543
4544 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4545 by cp_parser_builtin_offsetof. We're looking for
4546
4547 postfix-expression [ expression ]
4548
4549 FOR_OFFSETOF is set if we're being called in that context, which
4550 changes how we deal with integer constant expressions. */
4551
4552 static tree
4553 cp_parser_postfix_open_square_expression (cp_parser *parser,
4554 tree postfix_expression,
4555 bool for_offsetof)
4556 {
4557 tree index;
4558
4559 /* Consume the `[' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561
4562 /* Parse the index expression. */
4563 /* ??? For offsetof, there is a question of what to allow here. If
4564 offsetof is not being used in an integral constant expression context,
4565 then we *could* get the right answer by computing the value at runtime.
4566 If we are in an integral constant expression context, then we might
4567 could accept any constant expression; hard to say without analysis.
4568 Rather than open the barn door too wide right away, allow only integer
4569 constant expressions here. */
4570 if (for_offsetof)
4571 index = cp_parser_constant_expression (parser, false, NULL);
4572 else
4573 index = cp_parser_expression (parser, /*cast_p=*/false);
4574
4575 /* Look for the closing `]'. */
4576 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4577
4578 /* Build the ARRAY_REF. */
4579 postfix_expression = grok_array_decl (postfix_expression, index);
4580
4581 /* When not doing offsetof, array references are not permitted in
4582 constant-expressions. */
4583 if (!for_offsetof
4584 && (cp_parser_non_integral_constant_expression
4585 (parser, "an array reference")))
4586 postfix_expression = error_mark_node;
4587
4588 return postfix_expression;
4589 }
4590
4591 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4592 by cp_parser_builtin_offsetof. We're looking for
4593
4594 postfix-expression . template [opt] id-expression
4595 postfix-expression . pseudo-destructor-name
4596 postfix-expression -> template [opt] id-expression
4597 postfix-expression -> pseudo-destructor-name
4598
4599 FOR_OFFSETOF is set if we're being called in that context. That sorta
4600 limits what of the above we'll actually accept, but nevermind.
4601 TOKEN_TYPE is the "." or "->" token, which will already have been
4602 removed from the stream. */
4603
4604 static tree
4605 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4606 enum cpp_ttype token_type,
4607 tree postfix_expression,
4608 bool for_offsetof, cp_id_kind *idk)
4609 {
4610 tree name;
4611 bool dependent_p;
4612 bool pseudo_destructor_p;
4613 tree scope = NULL_TREE;
4614
4615 /* If this is a `->' operator, dereference the pointer. */
4616 if (token_type == CPP_DEREF)
4617 postfix_expression = build_x_arrow (postfix_expression);
4618 /* Check to see whether or not the expression is type-dependent. */
4619 dependent_p = type_dependent_expression_p (postfix_expression);
4620 /* The identifier following the `->' or `.' is not qualified. */
4621 parser->scope = NULL_TREE;
4622 parser->qualifying_scope = NULL_TREE;
4623 parser->object_scope = NULL_TREE;
4624 *idk = CP_ID_KIND_NONE;
4625 /* Enter the scope corresponding to the type of the object
4626 given by the POSTFIX_EXPRESSION. */
4627 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4628 {
4629 scope = TREE_TYPE (postfix_expression);
4630 /* According to the standard, no expression should ever have
4631 reference type. Unfortunately, we do not currently match
4632 the standard in this respect in that our internal representation
4633 of an expression may have reference type even when the standard
4634 says it does not. Therefore, we have to manually obtain the
4635 underlying type here. */
4636 scope = non_reference (scope);
4637 /* The type of the POSTFIX_EXPRESSION must be complete. */
4638 if (scope == unknown_type_node)
4639 {
4640 error ("%qE does not have class type", postfix_expression);
4641 scope = NULL_TREE;
4642 }
4643 else
4644 scope = complete_type_or_else (scope, NULL_TREE);
4645 /* Let the name lookup machinery know that we are processing a
4646 class member access expression. */
4647 parser->context->object_type = scope;
4648 /* If something went wrong, we want to be able to discern that case,
4649 as opposed to the case where there was no SCOPE due to the type
4650 of expression being dependent. */
4651 if (!scope)
4652 scope = error_mark_node;
4653 /* If the SCOPE was erroneous, make the various semantic analysis
4654 functions exit quickly -- and without issuing additional error
4655 messages. */
4656 if (scope == error_mark_node)
4657 postfix_expression = error_mark_node;
4658 }
4659
4660 /* Assume this expression is not a pseudo-destructor access. */
4661 pseudo_destructor_p = false;
4662
4663 /* If the SCOPE is a scalar type, then, if this is a valid program,
4664 we must be looking at a pseudo-destructor-name. */
4665 if (scope && SCALAR_TYPE_P (scope))
4666 {
4667 tree s;
4668 tree type;
4669
4670 cp_parser_parse_tentatively (parser);
4671 /* Parse the pseudo-destructor-name. */
4672 s = NULL_TREE;
4673 cp_parser_pseudo_destructor_name (parser, &s, &type);
4674 if (cp_parser_parse_definitely (parser))
4675 {
4676 pseudo_destructor_p = true;
4677 postfix_expression
4678 = finish_pseudo_destructor_expr (postfix_expression,
4679 s, TREE_TYPE (type));
4680 }
4681 }
4682
4683 if (!pseudo_destructor_p)
4684 {
4685 /* If the SCOPE is not a scalar type, we are looking at an
4686 ordinary class member access expression, rather than a
4687 pseudo-destructor-name. */
4688 bool template_p;
4689 /* Parse the id-expression. */
4690 name = (cp_parser_id_expression
4691 (parser,
4692 cp_parser_optional_template_keyword (parser),
4693 /*check_dependency_p=*/true,
4694 &template_p,
4695 /*declarator_p=*/false,
4696 /*optional_p=*/false));
4697 /* In general, build a SCOPE_REF if the member name is qualified.
4698 However, if the name was not dependent and has already been
4699 resolved; there is no need to build the SCOPE_REF. For example;
4700
4701 struct X { void f(); };
4702 template <typename T> void f(T* t) { t->X::f(); }
4703
4704 Even though "t" is dependent, "X::f" is not and has been resolved
4705 to a BASELINK; there is no need to include scope information. */
4706
4707 /* But we do need to remember that there was an explicit scope for
4708 virtual function calls. */
4709 if (parser->scope)
4710 *idk = CP_ID_KIND_QUALIFIED;
4711
4712 /* If the name is a template-id that names a type, we will get a
4713 TYPE_DECL here. That is invalid code. */
4714 if (TREE_CODE (name) == TYPE_DECL)
4715 {
4716 error ("invalid use of %qD", name);
4717 postfix_expression = error_mark_node;
4718 }
4719 else
4720 {
4721 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4722 {
4723 name = build_qualified_name (/*type=*/NULL_TREE,
4724 parser->scope,
4725 name,
4726 template_p);
4727 parser->scope = NULL_TREE;
4728 parser->qualifying_scope = NULL_TREE;
4729 parser->object_scope = NULL_TREE;
4730 }
4731 if (scope && name && BASELINK_P (name))
4732 adjust_result_of_qualified_name_lookup
4733 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4734 postfix_expression
4735 = finish_class_member_access_expr (postfix_expression, name,
4736 template_p);
4737 }
4738 }
4739
4740 /* We no longer need to look up names in the scope of the object on
4741 the left-hand side of the `.' or `->' operator. */
4742 parser->context->object_type = NULL_TREE;
4743
4744 /* Outside of offsetof, these operators may not appear in
4745 constant-expressions. */
4746 if (!for_offsetof
4747 && (cp_parser_non_integral_constant_expression
4748 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4749 postfix_expression = error_mark_node;
4750
4751 return postfix_expression;
4752 }
4753
4754 /* Parse a parenthesized expression-list.
4755
4756 expression-list:
4757 assignment-expression
4758 expression-list, assignment-expression
4759
4760 attribute-list:
4761 expression-list
4762 identifier
4763 identifier, expression-list
4764
4765 CAST_P is true if this expression is the target of a cast.
4766
4767 Returns a TREE_LIST. The TREE_VALUE of each node is a
4768 representation of an assignment-expression. Note that a TREE_LIST
4769 is returned even if there is only a single expression in the list.
4770 error_mark_node is returned if the ( and or ) are
4771 missing. NULL_TREE is returned on no expressions. The parentheses
4772 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4773 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4774 indicates whether or not all of the expressions in the list were
4775 constant. */
4776
4777 static tree
4778 cp_parser_parenthesized_expression_list (cp_parser* parser,
4779 bool is_attribute_list,
4780 bool cast_p,
4781 bool *non_constant_p)
4782 {
4783 tree expression_list = NULL_TREE;
4784 bool fold_expr_p = is_attribute_list;
4785 tree identifier = NULL_TREE;
4786
4787 /* Assume all the expressions will be constant. */
4788 if (non_constant_p)
4789 *non_constant_p = false;
4790
4791 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4792 return error_mark_node;
4793
4794 /* Consume expressions until there are no more. */
4795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4796 while (true)
4797 {
4798 tree expr;
4799
4800 /* At the beginning of attribute lists, check to see if the
4801 next token is an identifier. */
4802 if (is_attribute_list
4803 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4804 {
4805 cp_token *token;
4806
4807 /* Consume the identifier. */
4808 token = cp_lexer_consume_token (parser->lexer);
4809 /* Save the identifier. */
4810 identifier = token->u.value;
4811 }
4812 else
4813 {
4814 /* Parse the next assignment-expression. */
4815 if (non_constant_p)
4816 {
4817 bool expr_non_constant_p;
4818 expr = (cp_parser_constant_expression
4819 (parser, /*allow_non_constant_p=*/true,
4820 &expr_non_constant_p));
4821 if (expr_non_constant_p)
4822 *non_constant_p = true;
4823 }
4824 else
4825 expr = cp_parser_assignment_expression (parser, cast_p);
4826
4827 if (fold_expr_p)
4828 expr = fold_non_dependent_expr (expr);
4829
4830 /* Add it to the list. We add error_mark_node
4831 expressions to the list, so that we can still tell if
4832 the correct form for a parenthesized expression-list
4833 is found. That gives better errors. */
4834 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4835
4836 if (expr == error_mark_node)
4837 goto skip_comma;
4838 }
4839
4840 /* After the first item, attribute lists look the same as
4841 expression lists. */
4842 is_attribute_list = false;
4843
4844 get_comma:;
4845 /* If the next token isn't a `,', then we are done. */
4846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4847 break;
4848
4849 /* Otherwise, consume the `,' and keep going. */
4850 cp_lexer_consume_token (parser->lexer);
4851 }
4852
4853 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4854 {
4855 int ending;
4856
4857 skip_comma:;
4858 /* We try and resync to an unnested comma, as that will give the
4859 user better diagnostics. */
4860 ending = cp_parser_skip_to_closing_parenthesis (parser,
4861 /*recovering=*/true,
4862 /*or_comma=*/true,
4863 /*consume_paren=*/true);
4864 if (ending < 0)
4865 goto get_comma;
4866 if (!ending)
4867 return error_mark_node;
4868 }
4869
4870 /* We built up the list in reverse order so we must reverse it now. */
4871 expression_list = nreverse (expression_list);
4872 if (identifier)
4873 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4874
4875 return expression_list;
4876 }
4877
4878 /* Parse a pseudo-destructor-name.
4879
4880 pseudo-destructor-name:
4881 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4882 :: [opt] nested-name-specifier template template-id :: ~ type-name
4883 :: [opt] nested-name-specifier [opt] ~ type-name
4884
4885 If either of the first two productions is used, sets *SCOPE to the
4886 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4887 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4888 or ERROR_MARK_NODE if the parse fails. */
4889
4890 static void
4891 cp_parser_pseudo_destructor_name (cp_parser* parser,
4892 tree* scope,
4893 tree* type)
4894 {
4895 bool nested_name_specifier_p;
4896
4897 /* Assume that things will not work out. */
4898 *type = error_mark_node;
4899
4900 /* Look for the optional `::' operator. */
4901 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4902 /* Look for the optional nested-name-specifier. */
4903 nested_name_specifier_p
4904 = (cp_parser_nested_name_specifier_opt (parser,
4905 /*typename_keyword_p=*/false,
4906 /*check_dependency_p=*/true,
4907 /*type_p=*/false,
4908 /*is_declaration=*/true)
4909 != NULL_TREE);
4910 /* Now, if we saw a nested-name-specifier, we might be doing the
4911 second production. */
4912 if (nested_name_specifier_p
4913 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4914 {
4915 /* Consume the `template' keyword. */
4916 cp_lexer_consume_token (parser->lexer);
4917 /* Parse the template-id. */
4918 cp_parser_template_id (parser,
4919 /*template_keyword_p=*/true,
4920 /*check_dependency_p=*/false,
4921 /*is_declaration=*/true);
4922 /* Look for the `::' token. */
4923 cp_parser_require (parser, CPP_SCOPE, "`::'");
4924 }
4925 /* If the next token is not a `~', then there might be some
4926 additional qualification. */
4927 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4928 {
4929 /* Look for the type-name. */
4930 *scope = TREE_TYPE (cp_parser_type_name (parser));
4931
4932 if (*scope == error_mark_node)
4933 return;
4934
4935 /* If we don't have ::~, then something has gone wrong. Since
4936 the only caller of this function is looking for something
4937 after `.' or `->' after a scalar type, most likely the
4938 program is trying to get a member of a non-aggregate
4939 type. */
4940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4941 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4942 {
4943 cp_parser_error (parser, "request for member of non-aggregate type");
4944 return;
4945 }
4946
4947 /* Look for the `::' token. */
4948 cp_parser_require (parser, CPP_SCOPE, "`::'");
4949 }
4950 else
4951 *scope = NULL_TREE;
4952
4953 /* Look for the `~'. */
4954 cp_parser_require (parser, CPP_COMPL, "`~'");
4955 /* Look for the type-name again. We are not responsible for
4956 checking that it matches the first type-name. */
4957 *type = cp_parser_type_name (parser);
4958 }
4959
4960 /* Parse a unary-expression.
4961
4962 unary-expression:
4963 postfix-expression
4964 ++ cast-expression
4965 -- cast-expression
4966 unary-operator cast-expression
4967 sizeof unary-expression
4968 sizeof ( type-id )
4969 new-expression
4970 delete-expression
4971
4972 GNU Extensions:
4973
4974 unary-expression:
4975 __extension__ cast-expression
4976 __alignof__ unary-expression
4977 __alignof__ ( type-id )
4978 __real__ cast-expression
4979 __imag__ cast-expression
4980 && identifier
4981
4982 ADDRESS_P is true iff the unary-expression is appearing as the
4983 operand of the `&' operator. CAST_P is true if this expression is
4984 the target of a cast.
4985
4986 Returns a representation of the expression. */
4987
4988 static tree
4989 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4990 {
4991 cp_token *token;
4992 enum tree_code unary_operator;
4993
4994 /* Peek at the next token. */
4995 token = cp_lexer_peek_token (parser->lexer);
4996 /* Some keywords give away the kind of expression. */
4997 if (token->type == CPP_KEYWORD)
4998 {
4999 enum rid keyword = token->keyword;
5000
5001 switch (keyword)
5002 {
5003 case RID_ALIGNOF:
5004 case RID_SIZEOF:
5005 {
5006 tree operand;
5007 enum tree_code op;
5008
5009 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5010 /* Consume the token. */
5011 cp_lexer_consume_token (parser->lexer);
5012 /* Parse the operand. */
5013 operand = cp_parser_sizeof_operand (parser, keyword);
5014
5015 if (TYPE_P (operand))
5016 return cxx_sizeof_or_alignof_type (operand, op, true);
5017 else
5018 return cxx_sizeof_or_alignof_expr (operand, op);
5019 }
5020
5021 case RID_NEW:
5022 return cp_parser_new_expression (parser);
5023
5024 case RID_DELETE:
5025 return cp_parser_delete_expression (parser);
5026
5027 case RID_EXTENSION:
5028 {
5029 /* The saved value of the PEDANTIC flag. */
5030 int saved_pedantic;
5031 tree expr;
5032
5033 /* Save away the PEDANTIC flag. */
5034 cp_parser_extension_opt (parser, &saved_pedantic);
5035 /* Parse the cast-expression. */
5036 expr = cp_parser_simple_cast_expression (parser);
5037 /* Restore the PEDANTIC flag. */
5038 pedantic = saved_pedantic;
5039
5040 return expr;
5041 }
5042
5043 case RID_REALPART:
5044 case RID_IMAGPART:
5045 {
5046 tree expression;
5047
5048 /* Consume the `__real__' or `__imag__' token. */
5049 cp_lexer_consume_token (parser->lexer);
5050 /* Parse the cast-expression. */
5051 expression = cp_parser_simple_cast_expression (parser);
5052 /* Create the complete representation. */
5053 return build_x_unary_op ((keyword == RID_REALPART
5054 ? REALPART_EXPR : IMAGPART_EXPR),
5055 expression);
5056 }
5057 break;
5058
5059 default:
5060 break;
5061 }
5062 }
5063
5064 /* Look for the `:: new' and `:: delete', which also signal the
5065 beginning of a new-expression, or delete-expression,
5066 respectively. If the next token is `::', then it might be one of
5067 these. */
5068 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5069 {
5070 enum rid keyword;
5071
5072 /* See if the token after the `::' is one of the keywords in
5073 which we're interested. */
5074 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5075 /* If it's `new', we have a new-expression. */
5076 if (keyword == RID_NEW)
5077 return cp_parser_new_expression (parser);
5078 /* Similarly, for `delete'. */
5079 else if (keyword == RID_DELETE)
5080 return cp_parser_delete_expression (parser);
5081 }
5082
5083 /* Look for a unary operator. */
5084 unary_operator = cp_parser_unary_operator (token);
5085 /* The `++' and `--' operators can be handled similarly, even though
5086 they are not technically unary-operators in the grammar. */
5087 if (unary_operator == ERROR_MARK)
5088 {
5089 if (token->type == CPP_PLUS_PLUS)
5090 unary_operator = PREINCREMENT_EXPR;
5091 else if (token->type == CPP_MINUS_MINUS)
5092 unary_operator = PREDECREMENT_EXPR;
5093 /* Handle the GNU address-of-label extension. */
5094 else if (cp_parser_allow_gnu_extensions_p (parser)
5095 && token->type == CPP_AND_AND)
5096 {
5097 tree identifier;
5098
5099 /* Consume the '&&' token. */
5100 cp_lexer_consume_token (parser->lexer);
5101 /* Look for the identifier. */
5102 identifier = cp_parser_identifier (parser);
5103 /* Create an expression representing the address. */
5104 return finish_label_address_expr (identifier);
5105 }
5106 }
5107 if (unary_operator != ERROR_MARK)
5108 {
5109 tree cast_expression;
5110 tree expression = error_mark_node;
5111 const char *non_constant_p = NULL;
5112
5113 /* Consume the operator token. */
5114 token = cp_lexer_consume_token (parser->lexer);
5115 /* Parse the cast-expression. */
5116 cast_expression
5117 = cp_parser_cast_expression (parser,
5118 unary_operator == ADDR_EXPR,
5119 /*cast_p=*/false);
5120 /* Now, build an appropriate representation. */
5121 switch (unary_operator)
5122 {
5123 case INDIRECT_REF:
5124 non_constant_p = "`*'";
5125 expression = build_x_indirect_ref (cast_expression, "unary *");
5126 break;
5127
5128 case ADDR_EXPR:
5129 non_constant_p = "`&'";
5130 /* Fall through. */
5131 case BIT_NOT_EXPR:
5132 expression = build_x_unary_op (unary_operator, cast_expression);
5133 break;
5134
5135 case PREINCREMENT_EXPR:
5136 case PREDECREMENT_EXPR:
5137 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5138 ? "`++'" : "`--'");
5139 /* Fall through. */
5140 case UNARY_PLUS_EXPR:
5141 case NEGATE_EXPR:
5142 case TRUTH_NOT_EXPR:
5143 expression = finish_unary_op_expr (unary_operator, cast_expression);
5144 break;
5145
5146 default:
5147 gcc_unreachable ();
5148 }
5149
5150 if (non_constant_p
5151 && cp_parser_non_integral_constant_expression (parser,
5152 non_constant_p))
5153 expression = error_mark_node;
5154
5155 return expression;
5156 }
5157
5158 return cp_parser_postfix_expression (parser, address_p, cast_p);
5159 }
5160
5161 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5162 unary-operator, the corresponding tree code is returned. */
5163
5164 static enum tree_code
5165 cp_parser_unary_operator (cp_token* token)
5166 {
5167 switch (token->type)
5168 {
5169 case CPP_MULT:
5170 return INDIRECT_REF;
5171
5172 case CPP_AND:
5173 return ADDR_EXPR;
5174
5175 case CPP_PLUS:
5176 return UNARY_PLUS_EXPR;
5177
5178 case CPP_MINUS:
5179 return NEGATE_EXPR;
5180
5181 case CPP_NOT:
5182 return TRUTH_NOT_EXPR;
5183
5184 case CPP_COMPL:
5185 return BIT_NOT_EXPR;
5186
5187 default:
5188 return ERROR_MARK;
5189 }
5190 }
5191
5192 /* Parse a new-expression.
5193
5194 new-expression:
5195 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5196 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5197
5198 Returns a representation of the expression. */
5199
5200 static tree
5201 cp_parser_new_expression (cp_parser* parser)
5202 {
5203 bool global_scope_p;
5204 tree placement;
5205 tree type;
5206 tree initializer;
5207 tree nelts;
5208
5209 /* Look for the optional `::' operator. */
5210 global_scope_p
5211 = (cp_parser_global_scope_opt (parser,
5212 /*current_scope_valid_p=*/false)
5213 != NULL_TREE);
5214 /* Look for the `new' operator. */
5215 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5216 /* There's no easy way to tell a new-placement from the
5217 `( type-id )' construct. */
5218 cp_parser_parse_tentatively (parser);
5219 /* Look for a new-placement. */
5220 placement = cp_parser_new_placement (parser);
5221 /* If that didn't work out, there's no new-placement. */
5222 if (!cp_parser_parse_definitely (parser))
5223 placement = NULL_TREE;
5224
5225 /* If the next token is a `(', then we have a parenthesized
5226 type-id. */
5227 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5228 {
5229 /* Consume the `('. */
5230 cp_lexer_consume_token (parser->lexer);
5231 /* Parse the type-id. */
5232 type = cp_parser_type_id (parser);
5233 /* Look for the closing `)'. */
5234 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5235 /* There should not be a direct-new-declarator in this production,
5236 but GCC used to allowed this, so we check and emit a sensible error
5237 message for this case. */
5238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5239 {
5240 error ("array bound forbidden after parenthesized type-id");
5241 inform ("try removing the parentheses around the type-id");
5242 cp_parser_direct_new_declarator (parser);
5243 }
5244 nelts = NULL_TREE;
5245 }
5246 /* Otherwise, there must be a new-type-id. */
5247 else
5248 type = cp_parser_new_type_id (parser, &nelts);
5249
5250 /* If the next token is a `(', then we have a new-initializer. */
5251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5252 initializer = cp_parser_new_initializer (parser);
5253 else
5254 initializer = NULL_TREE;
5255
5256 /* A new-expression may not appear in an integral constant
5257 expression. */
5258 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5259 return error_mark_node;
5260
5261 /* Create a representation of the new-expression. */
5262 return build_new (placement, type, nelts, initializer, global_scope_p);
5263 }
5264
5265 /* Parse a new-placement.
5266
5267 new-placement:
5268 ( expression-list )
5269
5270 Returns the same representation as for an expression-list. */
5271
5272 static tree
5273 cp_parser_new_placement (cp_parser* parser)
5274 {
5275 tree expression_list;
5276
5277 /* Parse the expression-list. */
5278 expression_list = (cp_parser_parenthesized_expression_list
5279 (parser, false, /*cast_p=*/false,
5280 /*non_constant_p=*/NULL));
5281
5282 return expression_list;
5283 }
5284
5285 /* Parse a new-type-id.
5286
5287 new-type-id:
5288 type-specifier-seq new-declarator [opt]
5289
5290 Returns the TYPE allocated. If the new-type-id indicates an array
5291 type, *NELTS is set to the number of elements in the last array
5292 bound; the TYPE will not include the last array bound. */
5293
5294 static tree
5295 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5296 {
5297 cp_decl_specifier_seq type_specifier_seq;
5298 cp_declarator *new_declarator;
5299 cp_declarator *declarator;
5300 cp_declarator *outer_declarator;
5301 const char *saved_message;
5302 tree type;
5303
5304 /* The type-specifier sequence must not contain type definitions.
5305 (It cannot contain declarations of new types either, but if they
5306 are not definitions we will catch that because they are not
5307 complete.) */
5308 saved_message = parser->type_definition_forbidden_message;
5309 parser->type_definition_forbidden_message
5310 = "types may not be defined in a new-type-id";
5311 /* Parse the type-specifier-seq. */
5312 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5313 &type_specifier_seq);
5314 /* Restore the old message. */
5315 parser->type_definition_forbidden_message = saved_message;
5316 /* Parse the new-declarator. */
5317 new_declarator = cp_parser_new_declarator_opt (parser);
5318
5319 /* Determine the number of elements in the last array dimension, if
5320 any. */
5321 *nelts = NULL_TREE;
5322 /* Skip down to the last array dimension. */
5323 declarator = new_declarator;
5324 outer_declarator = NULL;
5325 while (declarator && (declarator->kind == cdk_pointer
5326 || declarator->kind == cdk_ptrmem))
5327 {
5328 outer_declarator = declarator;
5329 declarator = declarator->declarator;
5330 }
5331 while (declarator
5332 && declarator->kind == cdk_array
5333 && declarator->declarator
5334 && declarator->declarator->kind == cdk_array)
5335 {
5336 outer_declarator = declarator;
5337 declarator = declarator->declarator;
5338 }
5339
5340 if (declarator && declarator->kind == cdk_array)
5341 {
5342 *nelts = declarator->u.array.bounds;
5343 if (*nelts == error_mark_node)
5344 *nelts = integer_one_node;
5345
5346 if (outer_declarator)
5347 outer_declarator->declarator = declarator->declarator;
5348 else
5349 new_declarator = NULL;
5350 }
5351
5352 type = groktypename (&type_specifier_seq, new_declarator);
5353 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5354 {
5355 *nelts = array_type_nelts_top (type);
5356 type = TREE_TYPE (type);
5357 }
5358 return type;
5359 }
5360
5361 /* Parse an (optional) new-declarator.
5362
5363 new-declarator:
5364 ptr-operator new-declarator [opt]
5365 direct-new-declarator
5366
5367 Returns the declarator. */
5368
5369 static cp_declarator *
5370 cp_parser_new_declarator_opt (cp_parser* parser)
5371 {
5372 enum tree_code code;
5373 tree type;
5374 cp_cv_quals cv_quals;
5375
5376 /* We don't know if there's a ptr-operator next, or not. */
5377 cp_parser_parse_tentatively (parser);
5378 /* Look for a ptr-operator. */
5379 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5380 /* If that worked, look for more new-declarators. */
5381 if (cp_parser_parse_definitely (parser))
5382 {
5383 cp_declarator *declarator;
5384
5385 /* Parse another optional declarator. */
5386 declarator = cp_parser_new_declarator_opt (parser);
5387
5388 /* Create the representation of the declarator. */
5389 if (type)
5390 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5391 else if (code == INDIRECT_REF)
5392 declarator = make_pointer_declarator (cv_quals, declarator);
5393 else
5394 declarator = make_reference_declarator (cv_quals, declarator);
5395
5396 return declarator;
5397 }
5398
5399 /* If the next token is a `[', there is a direct-new-declarator. */
5400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5401 return cp_parser_direct_new_declarator (parser);
5402
5403 return NULL;
5404 }
5405
5406 /* Parse a direct-new-declarator.
5407
5408 direct-new-declarator:
5409 [ expression ]
5410 direct-new-declarator [constant-expression]
5411
5412 */
5413
5414 static cp_declarator *
5415 cp_parser_direct_new_declarator (cp_parser* parser)
5416 {
5417 cp_declarator *declarator = NULL;
5418
5419 while (true)
5420 {
5421 tree expression;
5422
5423 /* Look for the opening `['. */
5424 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5425 /* The first expression is not required to be constant. */
5426 if (!declarator)
5427 {
5428 expression = cp_parser_expression (parser, /*cast_p=*/false);
5429 /* The standard requires that the expression have integral
5430 type. DR 74 adds enumeration types. We believe that the
5431 real intent is that these expressions be handled like the
5432 expression in a `switch' condition, which also allows
5433 classes with a single conversion to integral or
5434 enumeration type. */
5435 if (!processing_template_decl)
5436 {
5437 expression
5438 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5439 expression,
5440 /*complain=*/true);
5441 if (!expression)
5442 {
5443 error ("expression in new-declarator must have integral "
5444 "or enumeration type");
5445 expression = error_mark_node;
5446 }
5447 }
5448 }
5449 /* But all the other expressions must be. */
5450 else
5451 expression
5452 = cp_parser_constant_expression (parser,
5453 /*allow_non_constant=*/false,
5454 NULL);
5455 /* Look for the closing `]'. */
5456 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5457
5458 /* Add this bound to the declarator. */
5459 declarator = make_array_declarator (declarator, expression);
5460
5461 /* If the next token is not a `[', then there are no more
5462 bounds. */
5463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5464 break;
5465 }
5466
5467 return declarator;
5468 }
5469
5470 /* Parse a new-initializer.
5471
5472 new-initializer:
5473 ( expression-list [opt] )
5474
5475 Returns a representation of the expression-list. If there is no
5476 expression-list, VOID_ZERO_NODE is returned. */
5477
5478 static tree
5479 cp_parser_new_initializer (cp_parser* parser)
5480 {
5481 tree expression_list;
5482
5483 expression_list = (cp_parser_parenthesized_expression_list
5484 (parser, false, /*cast_p=*/false,
5485 /*non_constant_p=*/NULL));
5486 if (!expression_list)
5487 expression_list = void_zero_node;
5488
5489 return expression_list;
5490 }
5491
5492 /* Parse a delete-expression.
5493
5494 delete-expression:
5495 :: [opt] delete cast-expression
5496 :: [opt] delete [ ] cast-expression
5497
5498 Returns a representation of the expression. */
5499
5500 static tree
5501 cp_parser_delete_expression (cp_parser* parser)
5502 {
5503 bool global_scope_p;
5504 bool array_p;
5505 tree expression;
5506
5507 /* Look for the optional `::' operator. */
5508 global_scope_p
5509 = (cp_parser_global_scope_opt (parser,
5510 /*current_scope_valid_p=*/false)
5511 != NULL_TREE);
5512 /* Look for the `delete' keyword. */
5513 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5514 /* See if the array syntax is in use. */
5515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5516 {
5517 /* Consume the `[' token. */
5518 cp_lexer_consume_token (parser->lexer);
5519 /* Look for the `]' token. */
5520 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5521 /* Remember that this is the `[]' construct. */
5522 array_p = true;
5523 }
5524 else
5525 array_p = false;
5526
5527 /* Parse the cast-expression. */
5528 expression = cp_parser_simple_cast_expression (parser);
5529
5530 /* A delete-expression may not appear in an integral constant
5531 expression. */
5532 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5533 return error_mark_node;
5534
5535 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5536 }
5537
5538 /* Parse a cast-expression.
5539
5540 cast-expression:
5541 unary-expression
5542 ( type-id ) cast-expression
5543
5544 ADDRESS_P is true iff the unary-expression is appearing as the
5545 operand of the `&' operator. CAST_P is true if this expression is
5546 the target of a cast.
5547
5548 Returns a representation of the expression. */
5549
5550 static tree
5551 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5552 {
5553 /* If it's a `(', then we might be looking at a cast. */
5554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5555 {
5556 tree type = NULL_TREE;
5557 tree expr = NULL_TREE;
5558 bool compound_literal_p;
5559 const char *saved_message;
5560
5561 /* There's no way to know yet whether or not this is a cast.
5562 For example, `(int (3))' is a unary-expression, while `(int)
5563 3' is a cast. So, we resort to parsing tentatively. */
5564 cp_parser_parse_tentatively (parser);
5565 /* Types may not be defined in a cast. */
5566 saved_message = parser->type_definition_forbidden_message;
5567 parser->type_definition_forbidden_message
5568 = "types may not be defined in casts";
5569 /* Consume the `('. */
5570 cp_lexer_consume_token (parser->lexer);
5571 /* A very tricky bit is that `(struct S) { 3 }' is a
5572 compound-literal (which we permit in C++ as an extension).
5573 But, that construct is not a cast-expression -- it is a
5574 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5575 is legal; if the compound-literal were a cast-expression,
5576 you'd need an extra set of parentheses.) But, if we parse
5577 the type-id, and it happens to be a class-specifier, then we
5578 will commit to the parse at that point, because we cannot
5579 undo the action that is done when creating a new class. So,
5580 then we cannot back up and do a postfix-expression.
5581
5582 Therefore, we scan ahead to the closing `)', and check to see
5583 if the token after the `)' is a `{'. If so, we are not
5584 looking at a cast-expression.
5585
5586 Save tokens so that we can put them back. */
5587 cp_lexer_save_tokens (parser->lexer);
5588 /* Skip tokens until the next token is a closing parenthesis.
5589 If we find the closing `)', and the next token is a `{', then
5590 we are looking at a compound-literal. */
5591 compound_literal_p
5592 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5593 /*consume_paren=*/true)
5594 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5595 /* Roll back the tokens we skipped. */
5596 cp_lexer_rollback_tokens (parser->lexer);
5597 /* If we were looking at a compound-literal, simulate an error
5598 so that the call to cp_parser_parse_definitely below will
5599 fail. */
5600 if (compound_literal_p)
5601 cp_parser_simulate_error (parser);
5602 else
5603 {
5604 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5605 parser->in_type_id_in_expr_p = true;
5606 /* Look for the type-id. */
5607 type = cp_parser_type_id (parser);
5608 /* Look for the closing `)'. */
5609 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5610 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5611 }
5612
5613 /* Restore the saved message. */
5614 parser->type_definition_forbidden_message = saved_message;
5615
5616 /* If ok so far, parse the dependent expression. We cannot be
5617 sure it is a cast. Consider `(T ())'. It is a parenthesized
5618 ctor of T, but looks like a cast to function returning T
5619 without a dependent expression. */
5620 if (!cp_parser_error_occurred (parser))
5621 expr = cp_parser_cast_expression (parser,
5622 /*address_p=*/false,
5623 /*cast_p=*/true);
5624
5625 if (cp_parser_parse_definitely (parser))
5626 {
5627 /* Warn about old-style casts, if so requested. */
5628 if (warn_old_style_cast
5629 && !in_system_header
5630 && !VOID_TYPE_P (type)
5631 && current_lang_name != lang_name_c)
5632 warning (OPT_Wold_style_cast, "use of old-style cast");
5633
5634 /* Only type conversions to integral or enumeration types
5635 can be used in constant-expressions. */
5636 if (!cast_valid_in_integral_constant_expression_p (type)
5637 && (cp_parser_non_integral_constant_expression
5638 (parser,
5639 "a cast to a type other than an integral or "
5640 "enumeration type")))
5641 return error_mark_node;
5642
5643 /* Perform the cast. */
5644 expr = build_c_cast (type, expr);
5645 return expr;
5646 }
5647 }
5648
5649 /* If we get here, then it's not a cast, so it must be a
5650 unary-expression. */
5651 return cp_parser_unary_expression (parser, address_p, cast_p);
5652 }
5653
5654 /* Parse a binary expression of the general form:
5655
5656 pm-expression:
5657 cast-expression
5658 pm-expression .* cast-expression
5659 pm-expression ->* cast-expression
5660
5661 multiplicative-expression:
5662 pm-expression
5663 multiplicative-expression * pm-expression
5664 multiplicative-expression / pm-expression
5665 multiplicative-expression % pm-expression
5666
5667 additive-expression:
5668 multiplicative-expression
5669 additive-expression + multiplicative-expression
5670 additive-expression - multiplicative-expression
5671
5672 shift-expression:
5673 additive-expression
5674 shift-expression << additive-expression
5675 shift-expression >> additive-expression
5676
5677 relational-expression:
5678 shift-expression
5679 relational-expression < shift-expression
5680 relational-expression > shift-expression
5681 relational-expression <= shift-expression
5682 relational-expression >= shift-expression
5683
5684 GNU Extension:
5685
5686 relational-expression:
5687 relational-expression <? shift-expression
5688 relational-expression >? shift-expression
5689
5690 equality-expression:
5691 relational-expression
5692 equality-expression == relational-expression
5693 equality-expression != relational-expression
5694
5695 and-expression:
5696 equality-expression
5697 and-expression & equality-expression
5698
5699 exclusive-or-expression:
5700 and-expression
5701 exclusive-or-expression ^ and-expression
5702
5703 inclusive-or-expression:
5704 exclusive-or-expression
5705 inclusive-or-expression | exclusive-or-expression
5706
5707 logical-and-expression:
5708 inclusive-or-expression
5709 logical-and-expression && inclusive-or-expression
5710
5711 logical-or-expression:
5712 logical-and-expression
5713 logical-or-expression || logical-and-expression
5714
5715 All these are implemented with a single function like:
5716
5717 binary-expression:
5718 simple-cast-expression
5719 binary-expression <token> binary-expression
5720
5721 CAST_P is true if this expression is the target of a cast.
5722
5723 The binops_by_token map is used to get the tree codes for each <token> type.
5724 binary-expressions are associated according to a precedence table. */
5725
5726 #define TOKEN_PRECEDENCE(token) \
5727 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5728 ? PREC_NOT_OPERATOR \
5729 : binops_by_token[token->type].prec)
5730
5731 static tree
5732 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5733 {
5734 cp_parser_expression_stack stack;
5735 cp_parser_expression_stack_entry *sp = &stack[0];
5736 tree lhs, rhs;
5737 cp_token *token;
5738 enum tree_code tree_type, lhs_type, rhs_type;
5739 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5740 bool overloaded_p;
5741
5742 /* Parse the first expression. */
5743 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5744 lhs_type = ERROR_MARK;
5745
5746 for (;;)
5747 {
5748 /* Get an operator token. */
5749 token = cp_lexer_peek_token (parser->lexer);
5750
5751 new_prec = TOKEN_PRECEDENCE (token);
5752
5753 /* Popping an entry off the stack means we completed a subexpression:
5754 - either we found a token which is not an operator (`>' where it is not
5755 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5756 will happen repeatedly;
5757 - or, we found an operator which has lower priority. This is the case
5758 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5759 parsing `3 * 4'. */
5760 if (new_prec <= prec)
5761 {
5762 if (sp == stack)
5763 break;
5764 else
5765 goto pop;
5766 }
5767
5768 get_rhs:
5769 tree_type = binops_by_token[token->type].tree_type;
5770
5771 /* We used the operator token. */
5772 cp_lexer_consume_token (parser->lexer);
5773
5774 /* Extract another operand. It may be the RHS of this expression
5775 or the LHS of a new, higher priority expression. */
5776 rhs = cp_parser_simple_cast_expression (parser);
5777 rhs_type = ERROR_MARK;
5778
5779 /* Get another operator token. Look up its precedence to avoid
5780 building a useless (immediately popped) stack entry for common
5781 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5782 token = cp_lexer_peek_token (parser->lexer);
5783 lookahead_prec = TOKEN_PRECEDENCE (token);
5784 if (lookahead_prec > new_prec)
5785 {
5786 /* ... and prepare to parse the RHS of the new, higher priority
5787 expression. Since precedence levels on the stack are
5788 monotonically increasing, we do not have to care about
5789 stack overflows. */
5790 sp->prec = prec;
5791 sp->tree_type = tree_type;
5792 sp->lhs = lhs;
5793 sp->lhs_type = lhs_type;
5794 sp++;
5795 lhs = rhs;
5796 lhs_type = rhs_type;
5797 prec = new_prec;
5798 new_prec = lookahead_prec;
5799 goto get_rhs;
5800
5801 pop:
5802 /* If the stack is not empty, we have parsed into LHS the right side
5803 (`4' in the example above) of an expression we had suspended.
5804 We can use the information on the stack to recover the LHS (`3')
5805 from the stack together with the tree code (`MULT_EXPR'), and
5806 the precedence of the higher level subexpression
5807 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5808 which will be used to actually build the additive expression. */
5809 --sp;
5810 prec = sp->prec;
5811 tree_type = sp->tree_type;
5812 rhs = lhs;
5813 rhs_type = lhs_type;
5814 lhs = sp->lhs;
5815 lhs_type = sp->lhs_type;
5816 }
5817
5818 overloaded_p = false;
5819 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5820 &overloaded_p);
5821 lhs_type = tree_type;
5822
5823 /* If the binary operator required the use of an overloaded operator,
5824 then this expression cannot be an integral constant-expression.
5825 An overloaded operator can be used even if both operands are
5826 otherwise permissible in an integral constant-expression if at
5827 least one of the operands is of enumeration type. */
5828
5829 if (overloaded_p
5830 && (cp_parser_non_integral_constant_expression
5831 (parser, "calls to overloaded operators")))
5832 return error_mark_node;
5833 }
5834
5835 return lhs;
5836 }
5837
5838
5839 /* Parse the `? expression : assignment-expression' part of a
5840 conditional-expression. The LOGICAL_OR_EXPR is the
5841 logical-or-expression that started the conditional-expression.
5842 Returns a representation of the entire conditional-expression.
5843
5844 This routine is used by cp_parser_assignment_expression.
5845
5846 ? expression : assignment-expression
5847
5848 GNU Extensions:
5849
5850 ? : assignment-expression */
5851
5852 static tree
5853 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5854 {
5855 tree expr;
5856 tree assignment_expr;
5857
5858 /* Consume the `?' token. */
5859 cp_lexer_consume_token (parser->lexer);
5860 if (cp_parser_allow_gnu_extensions_p (parser)
5861 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5862 /* Implicit true clause. */
5863 expr = NULL_TREE;
5864 else
5865 /* Parse the expression. */
5866 expr = cp_parser_expression (parser, /*cast_p=*/false);
5867
5868 /* The next token should be a `:'. */
5869 cp_parser_require (parser, CPP_COLON, "`:'");
5870 /* Parse the assignment-expression. */
5871 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5872
5873 /* Build the conditional-expression. */
5874 return build_x_conditional_expr (logical_or_expr,
5875 expr,
5876 assignment_expr);
5877 }
5878
5879 /* Parse an assignment-expression.
5880
5881 assignment-expression:
5882 conditional-expression
5883 logical-or-expression assignment-operator assignment_expression
5884 throw-expression
5885
5886 CAST_P is true if this expression is the target of a cast.
5887
5888 Returns a representation for the expression. */
5889
5890 static tree
5891 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5892 {
5893 tree expr;
5894
5895 /* If the next token is the `throw' keyword, then we're looking at
5896 a throw-expression. */
5897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5898 expr = cp_parser_throw_expression (parser);
5899 /* Otherwise, it must be that we are looking at a
5900 logical-or-expression. */
5901 else
5902 {
5903 /* Parse the binary expressions (logical-or-expression). */
5904 expr = cp_parser_binary_expression (parser, cast_p);
5905 /* If the next token is a `?' then we're actually looking at a
5906 conditional-expression. */
5907 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5908 return cp_parser_question_colon_clause (parser, expr);
5909 else
5910 {
5911 enum tree_code assignment_operator;
5912
5913 /* If it's an assignment-operator, we're using the second
5914 production. */
5915 assignment_operator
5916 = cp_parser_assignment_operator_opt (parser);
5917 if (assignment_operator != ERROR_MARK)
5918 {
5919 tree rhs;
5920
5921 /* Parse the right-hand side of the assignment. */
5922 rhs = cp_parser_assignment_expression (parser, cast_p);
5923 /* An assignment may not appear in a
5924 constant-expression. */
5925 if (cp_parser_non_integral_constant_expression (parser,
5926 "an assignment"))
5927 return error_mark_node;
5928 /* Build the assignment expression. */
5929 expr = build_x_modify_expr (expr,
5930 assignment_operator,
5931 rhs);
5932 }
5933 }
5934 }
5935
5936 return expr;
5937 }
5938
5939 /* Parse an (optional) assignment-operator.
5940
5941 assignment-operator: one of
5942 = *= /= %= += -= >>= <<= &= ^= |=
5943
5944 GNU Extension:
5945
5946 assignment-operator: one of
5947 <?= >?=
5948
5949 If the next token is an assignment operator, the corresponding tree
5950 code is returned, and the token is consumed. For example, for
5951 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5952 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5953 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5954 operator, ERROR_MARK is returned. */
5955
5956 static enum tree_code
5957 cp_parser_assignment_operator_opt (cp_parser* parser)
5958 {
5959 enum tree_code op;
5960 cp_token *token;
5961
5962 /* Peek at the next toen. */
5963 token = cp_lexer_peek_token (parser->lexer);
5964
5965 switch (token->type)
5966 {
5967 case CPP_EQ:
5968 op = NOP_EXPR;
5969 break;
5970
5971 case CPP_MULT_EQ:
5972 op = MULT_EXPR;
5973 break;
5974
5975 case CPP_DIV_EQ:
5976 op = TRUNC_DIV_EXPR;
5977 break;
5978
5979 case CPP_MOD_EQ:
5980 op = TRUNC_MOD_EXPR;
5981 break;
5982
5983 case CPP_PLUS_EQ:
5984 op = PLUS_EXPR;
5985 break;
5986
5987 case CPP_MINUS_EQ:
5988 op = MINUS_EXPR;
5989 break;
5990
5991 case CPP_RSHIFT_EQ:
5992 op = RSHIFT_EXPR;
5993 break;
5994
5995 case CPP_LSHIFT_EQ:
5996 op = LSHIFT_EXPR;
5997 break;
5998
5999 case CPP_AND_EQ:
6000 op = BIT_AND_EXPR;
6001 break;
6002
6003 case CPP_XOR_EQ:
6004 op = BIT_XOR_EXPR;
6005 break;
6006
6007 case CPP_OR_EQ:
6008 op = BIT_IOR_EXPR;
6009 break;
6010
6011 default:
6012 /* Nothing else is an assignment operator. */
6013 op = ERROR_MARK;
6014 }
6015
6016 /* If it was an assignment operator, consume it. */
6017 if (op != ERROR_MARK)
6018 cp_lexer_consume_token (parser->lexer);
6019
6020 return op;
6021 }
6022
6023 /* Parse an expression.
6024
6025 expression:
6026 assignment-expression
6027 expression , assignment-expression
6028
6029 CAST_P is true if this expression is the target of a cast.
6030
6031 Returns a representation of the expression. */
6032
6033 static tree
6034 cp_parser_expression (cp_parser* parser, bool cast_p)
6035 {
6036 tree expression = NULL_TREE;
6037
6038 while (true)
6039 {
6040 tree assignment_expression;
6041
6042 /* Parse the next assignment-expression. */
6043 assignment_expression
6044 = cp_parser_assignment_expression (parser, cast_p);
6045 /* If this is the first assignment-expression, we can just
6046 save it away. */
6047 if (!expression)
6048 expression = assignment_expression;
6049 else
6050 expression = build_x_compound_expr (expression,
6051 assignment_expression);
6052 /* If the next token is not a comma, then we are done with the
6053 expression. */
6054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6055 break;
6056 /* Consume the `,'. */
6057 cp_lexer_consume_token (parser->lexer);
6058 /* A comma operator cannot appear in a constant-expression. */
6059 if (cp_parser_non_integral_constant_expression (parser,
6060 "a comma operator"))
6061 expression = error_mark_node;
6062 }
6063
6064 return expression;
6065 }
6066
6067 /* Parse a constant-expression.
6068
6069 constant-expression:
6070 conditional-expression
6071
6072 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6073 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6074 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6075 is false, NON_CONSTANT_P should be NULL. */
6076
6077 static tree
6078 cp_parser_constant_expression (cp_parser* parser,
6079 bool allow_non_constant_p,
6080 bool *non_constant_p)
6081 {
6082 bool saved_integral_constant_expression_p;
6083 bool saved_allow_non_integral_constant_expression_p;
6084 bool saved_non_integral_constant_expression_p;
6085 tree expression;
6086
6087 /* It might seem that we could simply parse the
6088 conditional-expression, and then check to see if it were
6089 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6090 one that the compiler can figure out is constant, possibly after
6091 doing some simplifications or optimizations. The standard has a
6092 precise definition of constant-expression, and we must honor
6093 that, even though it is somewhat more restrictive.
6094
6095 For example:
6096
6097 int i[(2, 3)];
6098
6099 is not a legal declaration, because `(2, 3)' is not a
6100 constant-expression. The `,' operator is forbidden in a
6101 constant-expression. However, GCC's constant-folding machinery
6102 will fold this operation to an INTEGER_CST for `3'. */
6103
6104 /* Save the old settings. */
6105 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6106 saved_allow_non_integral_constant_expression_p
6107 = parser->allow_non_integral_constant_expression_p;
6108 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6109 /* We are now parsing a constant-expression. */
6110 parser->integral_constant_expression_p = true;
6111 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6112 parser->non_integral_constant_expression_p = false;
6113 /* Although the grammar says "conditional-expression", we parse an
6114 "assignment-expression", which also permits "throw-expression"
6115 and the use of assignment operators. In the case that
6116 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6117 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6118 actually essential that we look for an assignment-expression.
6119 For example, cp_parser_initializer_clauses uses this function to
6120 determine whether a particular assignment-expression is in fact
6121 constant. */
6122 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6123 /* Restore the old settings. */
6124 parser->integral_constant_expression_p
6125 = saved_integral_constant_expression_p;
6126 parser->allow_non_integral_constant_expression_p
6127 = saved_allow_non_integral_constant_expression_p;
6128 if (allow_non_constant_p)
6129 *non_constant_p = parser->non_integral_constant_expression_p;
6130 else if (parser->non_integral_constant_expression_p)
6131 expression = error_mark_node;
6132 parser->non_integral_constant_expression_p
6133 = saved_non_integral_constant_expression_p;
6134
6135 return expression;
6136 }
6137
6138 /* Parse __builtin_offsetof.
6139
6140 offsetof-expression:
6141 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6142
6143 offsetof-member-designator:
6144 id-expression
6145 | offsetof-member-designator "." id-expression
6146 | offsetof-member-designator "[" expression "]" */
6147
6148 static tree
6149 cp_parser_builtin_offsetof (cp_parser *parser)
6150 {
6151 int save_ice_p, save_non_ice_p;
6152 tree type, expr;
6153 cp_id_kind dummy;
6154
6155 /* We're about to accept non-integral-constant things, but will
6156 definitely yield an integral constant expression. Save and
6157 restore these values around our local parsing. */
6158 save_ice_p = parser->integral_constant_expression_p;
6159 save_non_ice_p = parser->non_integral_constant_expression_p;
6160
6161 /* Consume the "__builtin_offsetof" token. */
6162 cp_lexer_consume_token (parser->lexer);
6163 /* Consume the opening `('. */
6164 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6165 /* Parse the type-id. */
6166 type = cp_parser_type_id (parser);
6167 /* Look for the `,'. */
6168 cp_parser_require (parser, CPP_COMMA, "`,'");
6169
6170 /* Build the (type *)null that begins the traditional offsetof macro. */
6171 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6172
6173 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6174 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6175 true, &dummy);
6176 while (true)
6177 {
6178 cp_token *token = cp_lexer_peek_token (parser->lexer);
6179 switch (token->type)
6180 {
6181 case CPP_OPEN_SQUARE:
6182 /* offsetof-member-designator "[" expression "]" */
6183 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6184 break;
6185
6186 case CPP_DOT:
6187 /* offsetof-member-designator "." identifier */
6188 cp_lexer_consume_token (parser->lexer);
6189 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6190 true, &dummy);
6191 break;
6192
6193 case CPP_CLOSE_PAREN:
6194 /* Consume the ")" token. */
6195 cp_lexer_consume_token (parser->lexer);
6196 goto success;
6197
6198 default:
6199 /* Error. We know the following require will fail, but
6200 that gives the proper error message. */
6201 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6202 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6203 expr = error_mark_node;
6204 goto failure;
6205 }
6206 }
6207
6208 success:
6209 /* If we're processing a template, we can't finish the semantics yet.
6210 Otherwise we can fold the entire expression now. */
6211 if (processing_template_decl)
6212 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6213 else
6214 expr = finish_offsetof (expr);
6215
6216 failure:
6217 parser->integral_constant_expression_p = save_ice_p;
6218 parser->non_integral_constant_expression_p = save_non_ice_p;
6219
6220 return expr;
6221 }
6222
6223 /* Statements [gram.stmt.stmt] */
6224
6225 /* Parse a statement.
6226
6227 statement:
6228 labeled-statement
6229 expression-statement
6230 compound-statement
6231 selection-statement
6232 iteration-statement
6233 jump-statement
6234 declaration-statement
6235 try-block
6236
6237 IN_COMPOUND is true when the statement is nested inside a
6238 cp_parser_compound_statement; this matters for certain pragmas.
6239
6240 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6241 is a (possibly labeled) if statement which is not enclosed in braces
6242 and has an else clause. This is used to implement -Wparentheses. */
6243
6244 static void
6245 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6246 bool in_compound, bool *if_p)
6247 {
6248 tree statement;
6249 cp_token *token;
6250 location_t statement_location;
6251
6252 restart:
6253 if (if_p != NULL)
6254 *if_p = false;
6255 /* There is no statement yet. */
6256 statement = NULL_TREE;
6257 /* Peek at the next token. */
6258 token = cp_lexer_peek_token (parser->lexer);
6259 /* Remember the location of the first token in the statement. */
6260 statement_location = token->location;
6261 /* If this is a keyword, then that will often determine what kind of
6262 statement we have. */
6263 if (token->type == CPP_KEYWORD)
6264 {
6265 enum rid keyword = token->keyword;
6266
6267 switch (keyword)
6268 {
6269 case RID_CASE:
6270 case RID_DEFAULT:
6271 /* Looks like a labeled-statement with a case label.
6272 Parse the label, and then use tail recursion to parse
6273 the statement. */
6274 cp_parser_label_for_labeled_statement (parser);
6275 goto restart;
6276
6277 case RID_IF:
6278 case RID_SWITCH:
6279 statement = cp_parser_selection_statement (parser, if_p);
6280 break;
6281
6282 case RID_WHILE:
6283 case RID_DO:
6284 case RID_FOR:
6285 statement = cp_parser_iteration_statement (parser);
6286 break;
6287
6288 case RID_BREAK:
6289 case RID_CONTINUE:
6290 case RID_RETURN:
6291 case RID_GOTO:
6292 statement = cp_parser_jump_statement (parser);
6293 break;
6294
6295 /* Objective-C++ exception-handling constructs. */
6296 case RID_AT_TRY:
6297 case RID_AT_CATCH:
6298 case RID_AT_FINALLY:
6299 case RID_AT_SYNCHRONIZED:
6300 case RID_AT_THROW:
6301 statement = cp_parser_objc_statement (parser);
6302 break;
6303
6304 case RID_TRY:
6305 statement = cp_parser_try_block (parser);
6306 break;
6307
6308 default:
6309 /* It might be a keyword like `int' that can start a
6310 declaration-statement. */
6311 break;
6312 }
6313 }
6314 else if (token->type == CPP_NAME)
6315 {
6316 /* If the next token is a `:', then we are looking at a
6317 labeled-statement. */
6318 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6319 if (token->type == CPP_COLON)
6320 {
6321 /* Looks like a labeled-statement with an ordinary label.
6322 Parse the label, and then use tail recursion to parse
6323 the statement. */
6324 cp_parser_label_for_labeled_statement (parser);
6325 goto restart;
6326 }
6327 }
6328 /* Anything that starts with a `{' must be a compound-statement. */
6329 else if (token->type == CPP_OPEN_BRACE)
6330 statement = cp_parser_compound_statement (parser, NULL, false);
6331 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6332 a statement all its own. */
6333 else if (token->type == CPP_PRAGMA)
6334 {
6335 /* Only certain OpenMP pragmas are attached to statements, and thus
6336 are considered statements themselves. All others are not. In
6337 the context of a compound, accept the pragma as a "statement" and
6338 return so that we can check for a close brace. Otherwise we
6339 require a real statement and must go back and read one. */
6340 if (in_compound)
6341 cp_parser_pragma (parser, pragma_compound);
6342 else if (!cp_parser_pragma (parser, pragma_stmt))
6343 goto restart;
6344 return;
6345 }
6346 else if (token->type == CPP_EOF)
6347 {
6348 cp_parser_error (parser, "expected statement");
6349 return;
6350 }
6351
6352 /* Everything else must be a declaration-statement or an
6353 expression-statement. Try for the declaration-statement
6354 first, unless we are looking at a `;', in which case we know that
6355 we have an expression-statement. */
6356 if (!statement)
6357 {
6358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6359 {
6360 cp_parser_parse_tentatively (parser);
6361 /* Try to parse the declaration-statement. */
6362 cp_parser_declaration_statement (parser);
6363 /* If that worked, we're done. */
6364 if (cp_parser_parse_definitely (parser))
6365 return;
6366 }
6367 /* Look for an expression-statement instead. */
6368 statement = cp_parser_expression_statement (parser, in_statement_expr);
6369 }
6370
6371 /* Set the line number for the statement. */
6372 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6373 SET_EXPR_LOCATION (statement, statement_location);
6374 }
6375
6376 /* Parse the label for a labeled-statement, i.e.
6377
6378 identifier :
6379 case constant-expression :
6380 default :
6381
6382 GNU Extension:
6383 case constant-expression ... constant-expression : statement
6384
6385 When a label is parsed without errors, the label is added to the
6386 parse tree by the finish_* functions, so this function doesn't
6387 have to return the label. */
6388
6389 static void
6390 cp_parser_label_for_labeled_statement (cp_parser* parser)
6391 {
6392 cp_token *token;
6393
6394 /* The next token should be an identifier. */
6395 token = cp_lexer_peek_token (parser->lexer);
6396 if (token->type != CPP_NAME
6397 && token->type != CPP_KEYWORD)
6398 {
6399 cp_parser_error (parser, "expected labeled-statement");
6400 return;
6401 }
6402
6403 switch (token->keyword)
6404 {
6405 case RID_CASE:
6406 {
6407 tree expr, expr_hi;
6408 cp_token *ellipsis;
6409
6410 /* Consume the `case' token. */
6411 cp_lexer_consume_token (parser->lexer);
6412 /* Parse the constant-expression. */
6413 expr = cp_parser_constant_expression (parser,
6414 /*allow_non_constant_p=*/false,
6415 NULL);
6416
6417 ellipsis = cp_lexer_peek_token (parser->lexer);
6418 if (ellipsis->type == CPP_ELLIPSIS)
6419 {
6420 /* Consume the `...' token. */
6421 cp_lexer_consume_token (parser->lexer);
6422 expr_hi =
6423 cp_parser_constant_expression (parser,
6424 /*allow_non_constant_p=*/false,
6425 NULL);
6426 /* We don't need to emit warnings here, as the common code
6427 will do this for us. */
6428 }
6429 else
6430 expr_hi = NULL_TREE;
6431
6432 if (parser->in_switch_statement_p)
6433 finish_case_label (expr, expr_hi);
6434 else
6435 error ("case label %qE not within a switch statement", expr);
6436 }
6437 break;
6438
6439 case RID_DEFAULT:
6440 /* Consume the `default' token. */
6441 cp_lexer_consume_token (parser->lexer);
6442
6443 if (parser->in_switch_statement_p)
6444 finish_case_label (NULL_TREE, NULL_TREE);
6445 else
6446 error ("case label not within a switch statement");
6447 break;
6448
6449 default:
6450 /* Anything else must be an ordinary label. */
6451 finish_label_stmt (cp_parser_identifier (parser));
6452 break;
6453 }
6454
6455 /* Require the `:' token. */
6456 cp_parser_require (parser, CPP_COLON, "`:'");
6457 }
6458
6459 /* Parse an expression-statement.
6460
6461 expression-statement:
6462 expression [opt] ;
6463
6464 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6465 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6466 indicates whether this expression-statement is part of an
6467 expression statement. */
6468
6469 static tree
6470 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6471 {
6472 tree statement = NULL_TREE;
6473
6474 /* If the next token is a ';', then there is no expression
6475 statement. */
6476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6477 statement = cp_parser_expression (parser, /*cast_p=*/false);
6478
6479 /* Consume the final `;'. */
6480 cp_parser_consume_semicolon_at_end_of_statement (parser);
6481
6482 if (in_statement_expr
6483 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6484 /* This is the final expression statement of a statement
6485 expression. */
6486 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6487 else if (statement)
6488 statement = finish_expr_stmt (statement);
6489 else
6490 finish_stmt ();
6491
6492 return statement;
6493 }
6494
6495 /* Parse a compound-statement.
6496
6497 compound-statement:
6498 { statement-seq [opt] }
6499
6500 Returns a tree representing the statement. */
6501
6502 static tree
6503 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6504 bool in_try)
6505 {
6506 tree compound_stmt;
6507
6508 /* Consume the `{'. */
6509 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6510 return error_mark_node;
6511 /* Begin the compound-statement. */
6512 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6513 /* Parse an (optional) statement-seq. */
6514 cp_parser_statement_seq_opt (parser, in_statement_expr);
6515 /* Finish the compound-statement. */
6516 finish_compound_stmt (compound_stmt);
6517 /* Consume the `}'. */
6518 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6519
6520 return compound_stmt;
6521 }
6522
6523 /* Parse an (optional) statement-seq.
6524
6525 statement-seq:
6526 statement
6527 statement-seq [opt] statement */
6528
6529 static void
6530 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6531 {
6532 /* Scan statements until there aren't any more. */
6533 while (true)
6534 {
6535 cp_token *token = cp_lexer_peek_token (parser->lexer);
6536
6537 /* If we're looking at a `}', then we've run out of statements. */
6538 if (token->type == CPP_CLOSE_BRACE
6539 || token->type == CPP_EOF
6540 || token->type == CPP_PRAGMA_EOL)
6541 break;
6542
6543 /* Parse the statement. */
6544 cp_parser_statement (parser, in_statement_expr, true, NULL);
6545 }
6546 }
6547
6548 /* Parse a selection-statement.
6549
6550 selection-statement:
6551 if ( condition ) statement
6552 if ( condition ) statement else statement
6553 switch ( condition ) statement
6554
6555 Returns the new IF_STMT or SWITCH_STMT.
6556
6557 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6558 is a (possibly labeled) if statement which is not enclosed in
6559 braces and has an else clause. This is used to implement
6560 -Wparentheses. */
6561
6562 static tree
6563 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6564 {
6565 cp_token *token;
6566 enum rid keyword;
6567
6568 if (if_p != NULL)
6569 *if_p = false;
6570
6571 /* Peek at the next token. */
6572 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6573
6574 /* See what kind of keyword it is. */
6575 keyword = token->keyword;
6576 switch (keyword)
6577 {
6578 case RID_IF:
6579 case RID_SWITCH:
6580 {
6581 tree statement;
6582 tree condition;
6583
6584 /* Look for the `('. */
6585 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6586 {
6587 cp_parser_skip_to_end_of_statement (parser);
6588 return error_mark_node;
6589 }
6590
6591 /* Begin the selection-statement. */
6592 if (keyword == RID_IF)
6593 statement = begin_if_stmt ();
6594 else
6595 statement = begin_switch_stmt ();
6596
6597 /* Parse the condition. */
6598 condition = cp_parser_condition (parser);
6599 /* Look for the `)'. */
6600 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6601 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6602 /*consume_paren=*/true);
6603
6604 if (keyword == RID_IF)
6605 {
6606 bool nested_if;
6607
6608 /* Add the condition. */
6609 finish_if_stmt_cond (condition, statement);
6610
6611 /* Parse the then-clause. */
6612 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6613 finish_then_clause (statement);
6614
6615 /* If the next token is `else', parse the else-clause. */
6616 if (cp_lexer_next_token_is_keyword (parser->lexer,
6617 RID_ELSE))
6618 {
6619 /* Consume the `else' keyword. */
6620 cp_lexer_consume_token (parser->lexer);
6621 begin_else_clause (statement);
6622 /* Parse the else-clause. */
6623 cp_parser_implicitly_scoped_statement (parser, NULL);
6624 finish_else_clause (statement);
6625
6626 /* If we are currently parsing a then-clause, then
6627 IF_P will not be NULL. We set it to true to
6628 indicate that this if statement has an else clause.
6629 This may trigger the Wparentheses warning below
6630 when we get back up to the parent if statement. */
6631 if (if_p != NULL)
6632 *if_p = true;
6633 }
6634 else
6635 {
6636 /* This if statement does not have an else clause. If
6637 NESTED_IF is true, then the then-clause is an if
6638 statement which does have an else clause. We warn
6639 about the potential ambiguity. */
6640 if (nested_if)
6641 warning (OPT_Wparentheses,
6642 ("%Hsuggest explicit braces "
6643 "to avoid ambiguous %<else%>"),
6644 EXPR_LOCUS (statement));
6645 }
6646
6647 /* Now we're all done with the if-statement. */
6648 finish_if_stmt (statement);
6649 }
6650 else
6651 {
6652 bool in_switch_statement_p;
6653 unsigned char in_statement;
6654
6655 /* Add the condition. */
6656 finish_switch_cond (condition, statement);
6657
6658 /* Parse the body of the switch-statement. */
6659 in_switch_statement_p = parser->in_switch_statement_p;
6660 in_statement = parser->in_statement;
6661 parser->in_switch_statement_p = true;
6662 parser->in_statement |= IN_SWITCH_STMT;
6663 cp_parser_implicitly_scoped_statement (parser, NULL);
6664 parser->in_switch_statement_p = in_switch_statement_p;
6665 parser->in_statement = in_statement;
6666
6667 /* Now we're all done with the switch-statement. */
6668 finish_switch_stmt (statement);
6669 }
6670
6671 return statement;
6672 }
6673 break;
6674
6675 default:
6676 cp_parser_error (parser, "expected selection-statement");
6677 return error_mark_node;
6678 }
6679 }
6680
6681 /* Parse a condition.
6682
6683 condition:
6684 expression
6685 type-specifier-seq declarator = assignment-expression
6686
6687 GNU Extension:
6688
6689 condition:
6690 type-specifier-seq declarator asm-specification [opt]
6691 attributes [opt] = assignment-expression
6692
6693 Returns the expression that should be tested. */
6694
6695 static tree
6696 cp_parser_condition (cp_parser* parser)
6697 {
6698 cp_decl_specifier_seq type_specifiers;
6699 const char *saved_message;
6700
6701 /* Try the declaration first. */
6702 cp_parser_parse_tentatively (parser);
6703 /* New types are not allowed in the type-specifier-seq for a
6704 condition. */
6705 saved_message = parser->type_definition_forbidden_message;
6706 parser->type_definition_forbidden_message
6707 = "types may not be defined in conditions";
6708 /* Parse the type-specifier-seq. */
6709 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6710 &type_specifiers);
6711 /* Restore the saved message. */
6712 parser->type_definition_forbidden_message = saved_message;
6713 /* If all is well, we might be looking at a declaration. */
6714 if (!cp_parser_error_occurred (parser))
6715 {
6716 tree decl;
6717 tree asm_specification;
6718 tree attributes;
6719 cp_declarator *declarator;
6720 tree initializer = NULL_TREE;
6721
6722 /* Parse the declarator. */
6723 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6724 /*ctor_dtor_or_conv_p=*/NULL,
6725 /*parenthesized_p=*/NULL,
6726 /*member_p=*/false);
6727 /* Parse the attributes. */
6728 attributes = cp_parser_attributes_opt (parser);
6729 /* Parse the asm-specification. */
6730 asm_specification = cp_parser_asm_specification_opt (parser);
6731 /* If the next token is not an `=', then we might still be
6732 looking at an expression. For example:
6733
6734 if (A(a).x)
6735
6736 looks like a decl-specifier-seq and a declarator -- but then
6737 there is no `=', so this is an expression. */
6738 cp_parser_require (parser, CPP_EQ, "`='");
6739 /* If we did see an `=', then we are looking at a declaration
6740 for sure. */
6741 if (cp_parser_parse_definitely (parser))
6742 {
6743 tree pushed_scope;
6744 bool non_constant_p;
6745
6746 /* Create the declaration. */
6747 decl = start_decl (declarator, &type_specifiers,
6748 /*initialized_p=*/true,
6749 attributes, /*prefix_attributes=*/NULL_TREE,
6750 &pushed_scope);
6751 /* Parse the assignment-expression. */
6752 initializer
6753 = cp_parser_constant_expression (parser,
6754 /*allow_non_constant_p=*/true,
6755 &non_constant_p);
6756 if (!non_constant_p)
6757 initializer = fold_non_dependent_expr (initializer);
6758
6759 /* Process the initializer. */
6760 cp_finish_decl (decl,
6761 initializer, !non_constant_p,
6762 asm_specification,
6763 LOOKUP_ONLYCONVERTING);
6764
6765 if (pushed_scope)
6766 pop_scope (pushed_scope);
6767
6768 return convert_from_reference (decl);
6769 }
6770 }
6771 /* If we didn't even get past the declarator successfully, we are
6772 definitely not looking at a declaration. */
6773 else
6774 cp_parser_abort_tentative_parse (parser);
6775
6776 /* Otherwise, we are looking at an expression. */
6777 return cp_parser_expression (parser, /*cast_p=*/false);
6778 }
6779
6780 /* Parse an iteration-statement.
6781
6782 iteration-statement:
6783 while ( condition ) statement
6784 do statement while ( expression ) ;
6785 for ( for-init-statement condition [opt] ; expression [opt] )
6786 statement
6787
6788 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6789
6790 static tree
6791 cp_parser_iteration_statement (cp_parser* parser)
6792 {
6793 cp_token *token;
6794 enum rid keyword;
6795 tree statement;
6796 unsigned char in_statement;
6797
6798 /* Peek at the next token. */
6799 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6800 if (!token)
6801 return error_mark_node;
6802
6803 /* Remember whether or not we are already within an iteration
6804 statement. */
6805 in_statement = parser->in_statement;
6806
6807 /* See what kind of keyword it is. */
6808 keyword = token->keyword;
6809 switch (keyword)
6810 {
6811 case RID_WHILE:
6812 {
6813 tree condition;
6814
6815 /* Begin the while-statement. */
6816 statement = begin_while_stmt ();
6817 /* Look for the `('. */
6818 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6819 /* Parse the condition. */
6820 condition = cp_parser_condition (parser);
6821 finish_while_stmt_cond (condition, statement);
6822 /* Look for the `)'. */
6823 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6824 /* Parse the dependent statement. */
6825 parser->in_statement = IN_ITERATION_STMT;
6826 cp_parser_already_scoped_statement (parser);
6827 parser->in_statement = in_statement;
6828 /* We're done with the while-statement. */
6829 finish_while_stmt (statement);
6830 }
6831 break;
6832
6833 case RID_DO:
6834 {
6835 tree expression;
6836
6837 /* Begin the do-statement. */
6838 statement = begin_do_stmt ();
6839 /* Parse the body of the do-statement. */
6840 parser->in_statement = IN_ITERATION_STMT;
6841 cp_parser_implicitly_scoped_statement (parser, NULL);
6842 parser->in_statement = in_statement;
6843 finish_do_body (statement);
6844 /* Look for the `while' keyword. */
6845 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6846 /* Look for the `('. */
6847 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6848 /* Parse the expression. */
6849 expression = cp_parser_expression (parser, /*cast_p=*/false);
6850 /* We're done with the do-statement. */
6851 finish_do_stmt (expression, statement);
6852 /* Look for the `)'. */
6853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6854 /* Look for the `;'. */
6855 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6856 }
6857 break;
6858
6859 case RID_FOR:
6860 {
6861 tree condition = NULL_TREE;
6862 tree expression = NULL_TREE;
6863
6864 /* Begin the for-statement. */
6865 statement = begin_for_stmt ();
6866 /* Look for the `('. */
6867 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6868 /* Parse the initialization. */
6869 cp_parser_for_init_statement (parser);
6870 finish_for_init_stmt (statement);
6871
6872 /* If there's a condition, process it. */
6873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6874 condition = cp_parser_condition (parser);
6875 finish_for_cond (condition, statement);
6876 /* Look for the `;'. */
6877 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6878
6879 /* If there's an expression, process it. */
6880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6881 expression = cp_parser_expression (parser, /*cast_p=*/false);
6882 finish_for_expr (expression, statement);
6883 /* Look for the `)'. */
6884 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6885
6886 /* Parse the body of the for-statement. */
6887 parser->in_statement = IN_ITERATION_STMT;
6888 cp_parser_already_scoped_statement (parser);
6889 parser->in_statement = in_statement;
6890
6891 /* We're done with the for-statement. */
6892 finish_for_stmt (statement);
6893 }
6894 break;
6895
6896 default:
6897 cp_parser_error (parser, "expected iteration-statement");
6898 statement = error_mark_node;
6899 break;
6900 }
6901
6902 return statement;
6903 }
6904
6905 /* Parse a for-init-statement.
6906
6907 for-init-statement:
6908 expression-statement
6909 simple-declaration */
6910
6911 static void
6912 cp_parser_for_init_statement (cp_parser* parser)
6913 {
6914 /* If the next token is a `;', then we have an empty
6915 expression-statement. Grammatically, this is also a
6916 simple-declaration, but an invalid one, because it does not
6917 declare anything. Therefore, if we did not handle this case
6918 specially, we would issue an error message about an invalid
6919 declaration. */
6920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6921 {
6922 /* We're going to speculatively look for a declaration, falling back
6923 to an expression, if necessary. */
6924 cp_parser_parse_tentatively (parser);
6925 /* Parse the declaration. */
6926 cp_parser_simple_declaration (parser,
6927 /*function_definition_allowed_p=*/false);
6928 /* If the tentative parse failed, then we shall need to look for an
6929 expression-statement. */
6930 if (cp_parser_parse_definitely (parser))
6931 return;
6932 }
6933
6934 cp_parser_expression_statement (parser, false);
6935 }
6936
6937 /* Parse a jump-statement.
6938
6939 jump-statement:
6940 break ;
6941 continue ;
6942 return expression [opt] ;
6943 goto identifier ;
6944
6945 GNU extension:
6946
6947 jump-statement:
6948 goto * expression ;
6949
6950 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6951
6952 static tree
6953 cp_parser_jump_statement (cp_parser* parser)
6954 {
6955 tree statement = error_mark_node;
6956 cp_token *token;
6957 enum rid keyword;
6958
6959 /* Peek at the next token. */
6960 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6961 if (!token)
6962 return error_mark_node;
6963
6964 /* See what kind of keyword it is. */
6965 keyword = token->keyword;
6966 switch (keyword)
6967 {
6968 case RID_BREAK:
6969 switch (parser->in_statement)
6970 {
6971 case 0:
6972 error ("break statement not within loop or switch");
6973 break;
6974 default:
6975 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6976 || parser->in_statement == IN_ITERATION_STMT);
6977 statement = finish_break_stmt ();
6978 break;
6979 case IN_OMP_BLOCK:
6980 error ("invalid exit from OpenMP structured block");
6981 break;
6982 case IN_OMP_FOR:
6983 error ("break statement used with OpenMP for loop");
6984 break;
6985 }
6986 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6987 break;
6988
6989 case RID_CONTINUE:
6990 switch (parser->in_statement & ~IN_SWITCH_STMT)
6991 {
6992 case 0:
6993 error ("continue statement not within a loop");
6994 break;
6995 case IN_ITERATION_STMT:
6996 case IN_OMP_FOR:
6997 statement = finish_continue_stmt ();
6998 break;
6999 case IN_OMP_BLOCK:
7000 error ("invalid exit from OpenMP structured block");
7001 break;
7002 default:
7003 gcc_unreachable ();
7004 }
7005 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7006 break;
7007
7008 case RID_RETURN:
7009 {
7010 tree expr;
7011
7012 /* If the next token is a `;', then there is no
7013 expression. */
7014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7015 expr = cp_parser_expression (parser, /*cast_p=*/false);
7016 else
7017 expr = NULL_TREE;
7018 /* Build the return-statement. */
7019 statement = finish_return_stmt (expr);
7020 /* Look for the final `;'. */
7021 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7022 }
7023 break;
7024
7025 case RID_GOTO:
7026 /* Create the goto-statement. */
7027 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7028 {
7029 /* Issue a warning about this use of a GNU extension. */
7030 if (pedantic)
7031 pedwarn ("ISO C++ forbids computed gotos");
7032 /* Consume the '*' token. */
7033 cp_lexer_consume_token (parser->lexer);
7034 /* Parse the dependent expression. */
7035 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7036 }
7037 else
7038 finish_goto_stmt (cp_parser_identifier (parser));
7039 /* Look for the final `;'. */
7040 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7041 break;
7042
7043 default:
7044 cp_parser_error (parser, "expected jump-statement");
7045 break;
7046 }
7047
7048 return statement;
7049 }
7050
7051 /* Parse a declaration-statement.
7052
7053 declaration-statement:
7054 block-declaration */
7055
7056 static void
7057 cp_parser_declaration_statement (cp_parser* parser)
7058 {
7059 void *p;
7060
7061 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7062 p = obstack_alloc (&declarator_obstack, 0);
7063
7064 /* Parse the block-declaration. */
7065 cp_parser_block_declaration (parser, /*statement_p=*/true);
7066
7067 /* Free any declarators allocated. */
7068 obstack_free (&declarator_obstack, p);
7069
7070 /* Finish off the statement. */
7071 finish_stmt ();
7072 }
7073
7074 /* Some dependent statements (like `if (cond) statement'), are
7075 implicitly in their own scope. In other words, if the statement is
7076 a single statement (as opposed to a compound-statement), it is
7077 none-the-less treated as if it were enclosed in braces. Any
7078 declarations appearing in the dependent statement are out of scope
7079 after control passes that point. This function parses a statement,
7080 but ensures that is in its own scope, even if it is not a
7081 compound-statement.
7082
7083 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7084 is a (possibly labeled) if statement which is not enclosed in
7085 braces and has an else clause. This is used to implement
7086 -Wparentheses.
7087
7088 Returns the new statement. */
7089
7090 static tree
7091 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7092 {
7093 tree statement;
7094
7095 if (if_p != NULL)
7096 *if_p = false;
7097
7098 /* Mark if () ; with a special NOP_EXPR. */
7099 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7100 {
7101 cp_lexer_consume_token (parser->lexer);
7102 statement = add_stmt (build_empty_stmt ());
7103 }
7104 /* if a compound is opened, we simply parse the statement directly. */
7105 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7106 statement = cp_parser_compound_statement (parser, NULL, false);
7107 /* If the token is not a `{', then we must take special action. */
7108 else
7109 {
7110 /* Create a compound-statement. */
7111 statement = begin_compound_stmt (0);
7112 /* Parse the dependent-statement. */
7113 cp_parser_statement (parser, NULL_TREE, false, if_p);
7114 /* Finish the dummy compound-statement. */
7115 finish_compound_stmt (statement);
7116 }
7117
7118 /* Return the statement. */
7119 return statement;
7120 }
7121
7122 /* For some dependent statements (like `while (cond) statement'), we
7123 have already created a scope. Therefore, even if the dependent
7124 statement is a compound-statement, we do not want to create another
7125 scope. */
7126
7127 static void
7128 cp_parser_already_scoped_statement (cp_parser* parser)
7129 {
7130 /* If the token is a `{', then we must take special action. */
7131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7132 cp_parser_statement (parser, NULL_TREE, false, NULL);
7133 else
7134 {
7135 /* Avoid calling cp_parser_compound_statement, so that we
7136 don't create a new scope. Do everything else by hand. */
7137 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7138 cp_parser_statement_seq_opt (parser, NULL_TREE);
7139 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7140 }
7141 }
7142
7143 /* Declarations [gram.dcl.dcl] */
7144
7145 /* Parse an optional declaration-sequence.
7146
7147 declaration-seq:
7148 declaration
7149 declaration-seq declaration */
7150
7151 static void
7152 cp_parser_declaration_seq_opt (cp_parser* parser)
7153 {
7154 while (true)
7155 {
7156 cp_token *token;
7157
7158 token = cp_lexer_peek_token (parser->lexer);
7159
7160 if (token->type == CPP_CLOSE_BRACE
7161 || token->type == CPP_EOF
7162 || token->type == CPP_PRAGMA_EOL)
7163 break;
7164
7165 if (token->type == CPP_SEMICOLON)
7166 {
7167 /* A declaration consisting of a single semicolon is
7168 invalid. Allow it unless we're being pedantic. */
7169 cp_lexer_consume_token (parser->lexer);
7170 if (pedantic && !in_system_header)
7171 pedwarn ("extra %<;%>");
7172 continue;
7173 }
7174
7175 /* If we're entering or exiting a region that's implicitly
7176 extern "C", modify the lang context appropriately. */
7177 if (!parser->implicit_extern_c && token->implicit_extern_c)
7178 {
7179 push_lang_context (lang_name_c);
7180 parser->implicit_extern_c = true;
7181 }
7182 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7183 {
7184 pop_lang_context ();
7185 parser->implicit_extern_c = false;
7186 }
7187
7188 if (token->type == CPP_PRAGMA)
7189 {
7190 /* A top-level declaration can consist solely of a #pragma.
7191 A nested declaration cannot, so this is done here and not
7192 in cp_parser_declaration. (A #pragma at block scope is
7193 handled in cp_parser_statement.) */
7194 cp_parser_pragma (parser, pragma_external);
7195 continue;
7196 }
7197
7198 /* Parse the declaration itself. */
7199 cp_parser_declaration (parser);
7200 }
7201 }
7202
7203 /* Parse a declaration.
7204
7205 declaration:
7206 block-declaration
7207 function-definition
7208 template-declaration
7209 explicit-instantiation
7210 explicit-specialization
7211 linkage-specification
7212 namespace-definition
7213
7214 GNU extension:
7215
7216 declaration:
7217 __extension__ declaration */
7218
7219 static void
7220 cp_parser_declaration (cp_parser* parser)
7221 {
7222 cp_token token1;
7223 cp_token token2;
7224 int saved_pedantic;
7225 void *p;
7226
7227 /* Check for the `__extension__' keyword. */
7228 if (cp_parser_extension_opt (parser, &saved_pedantic))
7229 {
7230 /* Parse the qualified declaration. */
7231 cp_parser_declaration (parser);
7232 /* Restore the PEDANTIC flag. */
7233 pedantic = saved_pedantic;
7234
7235 return;
7236 }
7237
7238 /* Try to figure out what kind of declaration is present. */
7239 token1 = *cp_lexer_peek_token (parser->lexer);
7240
7241 if (token1.type != CPP_EOF)
7242 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7243 else
7244 {
7245 token2.type = CPP_EOF;
7246 token2.keyword = RID_MAX;
7247 }
7248
7249 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7250 p = obstack_alloc (&declarator_obstack, 0);
7251
7252 /* If the next token is `extern' and the following token is a string
7253 literal, then we have a linkage specification. */
7254 if (token1.keyword == RID_EXTERN
7255 && cp_parser_is_string_literal (&token2))
7256 cp_parser_linkage_specification (parser);
7257 /* If the next token is `template', then we have either a template
7258 declaration, an explicit instantiation, or an explicit
7259 specialization. */
7260 else if (token1.keyword == RID_TEMPLATE)
7261 {
7262 /* `template <>' indicates a template specialization. */
7263 if (token2.type == CPP_LESS
7264 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7265 cp_parser_explicit_specialization (parser);
7266 /* `template <' indicates a template declaration. */
7267 else if (token2.type == CPP_LESS)
7268 cp_parser_template_declaration (parser, /*member_p=*/false);
7269 /* Anything else must be an explicit instantiation. */
7270 else
7271 cp_parser_explicit_instantiation (parser);
7272 }
7273 /* If the next token is `export', then we have a template
7274 declaration. */
7275 else if (token1.keyword == RID_EXPORT)
7276 cp_parser_template_declaration (parser, /*member_p=*/false);
7277 /* If the next token is `extern', 'static' or 'inline' and the one
7278 after that is `template', we have a GNU extended explicit
7279 instantiation directive. */
7280 else if (cp_parser_allow_gnu_extensions_p (parser)
7281 && (token1.keyword == RID_EXTERN
7282 || token1.keyword == RID_STATIC
7283 || token1.keyword == RID_INLINE)
7284 && token2.keyword == RID_TEMPLATE)
7285 cp_parser_explicit_instantiation (parser);
7286 /* If the next token is `namespace', check for a named or unnamed
7287 namespace definition. */
7288 else if (token1.keyword == RID_NAMESPACE
7289 && (/* A named namespace definition. */
7290 (token2.type == CPP_NAME
7291 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7292 != CPP_EQ))
7293 /* An unnamed namespace definition. */
7294 || token2.type == CPP_OPEN_BRACE
7295 || token2.keyword == RID_ATTRIBUTE))
7296 cp_parser_namespace_definition (parser);
7297 /* Objective-C++ declaration/definition. */
7298 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7299 cp_parser_objc_declaration (parser);
7300 /* We must have either a block declaration or a function
7301 definition. */
7302 else
7303 /* Try to parse a block-declaration, or a function-definition. */
7304 cp_parser_block_declaration (parser, /*statement_p=*/false);
7305
7306 /* Free any declarators allocated. */
7307 obstack_free (&declarator_obstack, p);
7308 }
7309
7310 /* Parse a block-declaration.
7311
7312 block-declaration:
7313 simple-declaration
7314 asm-definition
7315 namespace-alias-definition
7316 using-declaration
7317 using-directive
7318
7319 GNU Extension:
7320
7321 block-declaration:
7322 __extension__ block-declaration
7323 label-declaration
7324
7325 C++0x Extension:
7326
7327 block-declaration:
7328 static_assert-declaration
7329
7330 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7331 part of a declaration-statement. */
7332
7333 static void
7334 cp_parser_block_declaration (cp_parser *parser,
7335 bool statement_p)
7336 {
7337 cp_token *token1;
7338 int saved_pedantic;
7339
7340 /* Check for the `__extension__' keyword. */
7341 if (cp_parser_extension_opt (parser, &saved_pedantic))
7342 {
7343 /* Parse the qualified declaration. */
7344 cp_parser_block_declaration (parser, statement_p);
7345 /* Restore the PEDANTIC flag. */
7346 pedantic = saved_pedantic;
7347
7348 return;
7349 }
7350
7351 /* Peek at the next token to figure out which kind of declaration is
7352 present. */
7353 token1 = cp_lexer_peek_token (parser->lexer);
7354
7355 /* If the next keyword is `asm', we have an asm-definition. */
7356 if (token1->keyword == RID_ASM)
7357 {
7358 if (statement_p)
7359 cp_parser_commit_to_tentative_parse (parser);
7360 cp_parser_asm_definition (parser);
7361 }
7362 /* If the next keyword is `namespace', we have a
7363 namespace-alias-definition. */
7364 else if (token1->keyword == RID_NAMESPACE)
7365 cp_parser_namespace_alias_definition (parser);
7366 /* If the next keyword is `using', we have either a
7367 using-declaration or a using-directive. */
7368 else if (token1->keyword == RID_USING)
7369 {
7370 cp_token *token2;
7371
7372 if (statement_p)
7373 cp_parser_commit_to_tentative_parse (parser);
7374 /* If the token after `using' is `namespace', then we have a
7375 using-directive. */
7376 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7377 if (token2->keyword == RID_NAMESPACE)
7378 cp_parser_using_directive (parser);
7379 /* Otherwise, it's a using-declaration. */
7380 else
7381 cp_parser_using_declaration (parser,
7382 /*access_declaration_p=*/false);
7383 }
7384 /* If the next keyword is `__label__' we have a label declaration. */
7385 else if (token1->keyword == RID_LABEL)
7386 {
7387 if (statement_p)
7388 cp_parser_commit_to_tentative_parse (parser);
7389 cp_parser_label_declaration (parser);
7390 }
7391 /* If the next token is `static_assert' we have a static assertion. */
7392 else if (token1->keyword == RID_STATIC_ASSERT)
7393 cp_parser_static_assert (parser, /*member_p=*/false);
7394 /* Anything else must be a simple-declaration. */
7395 else
7396 cp_parser_simple_declaration (parser, !statement_p);
7397 }
7398
7399 /* Parse a simple-declaration.
7400
7401 simple-declaration:
7402 decl-specifier-seq [opt] init-declarator-list [opt] ;
7403
7404 init-declarator-list:
7405 init-declarator
7406 init-declarator-list , init-declarator
7407
7408 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7409 function-definition as a simple-declaration. */
7410
7411 static void
7412 cp_parser_simple_declaration (cp_parser* parser,
7413 bool function_definition_allowed_p)
7414 {
7415 cp_decl_specifier_seq decl_specifiers;
7416 int declares_class_or_enum;
7417 bool saw_declarator;
7418
7419 /* Defer access checks until we know what is being declared; the
7420 checks for names appearing in the decl-specifier-seq should be
7421 done as if we were in the scope of the thing being declared. */
7422 push_deferring_access_checks (dk_deferred);
7423
7424 /* Parse the decl-specifier-seq. We have to keep track of whether
7425 or not the decl-specifier-seq declares a named class or
7426 enumeration type, since that is the only case in which the
7427 init-declarator-list is allowed to be empty.
7428
7429 [dcl.dcl]
7430
7431 In a simple-declaration, the optional init-declarator-list can be
7432 omitted only when declaring a class or enumeration, that is when
7433 the decl-specifier-seq contains either a class-specifier, an
7434 elaborated-type-specifier, or an enum-specifier. */
7435 cp_parser_decl_specifier_seq (parser,
7436 CP_PARSER_FLAGS_OPTIONAL,
7437 &decl_specifiers,
7438 &declares_class_or_enum);
7439 /* We no longer need to defer access checks. */
7440 stop_deferring_access_checks ();
7441
7442 /* In a block scope, a valid declaration must always have a
7443 decl-specifier-seq. By not trying to parse declarators, we can
7444 resolve the declaration/expression ambiguity more quickly. */
7445 if (!function_definition_allowed_p
7446 && !decl_specifiers.any_specifiers_p)
7447 {
7448 cp_parser_error (parser, "expected declaration");
7449 goto done;
7450 }
7451
7452 /* If the next two tokens are both identifiers, the code is
7453 erroneous. The usual cause of this situation is code like:
7454
7455 T t;
7456
7457 where "T" should name a type -- but does not. */
7458 if (!decl_specifiers.type
7459 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7460 {
7461 /* If parsing tentatively, we should commit; we really are
7462 looking at a declaration. */
7463 cp_parser_commit_to_tentative_parse (parser);
7464 /* Give up. */
7465 goto done;
7466 }
7467
7468 /* If we have seen at least one decl-specifier, and the next token
7469 is not a parenthesis, then we must be looking at a declaration.
7470 (After "int (" we might be looking at a functional cast.) */
7471 if (decl_specifiers.any_specifiers_p
7472 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7473 cp_parser_commit_to_tentative_parse (parser);
7474
7475 /* Keep going until we hit the `;' at the end of the simple
7476 declaration. */
7477 saw_declarator = false;
7478 while (cp_lexer_next_token_is_not (parser->lexer,
7479 CPP_SEMICOLON))
7480 {
7481 cp_token *token;
7482 bool function_definition_p;
7483 tree decl;
7484
7485 if (saw_declarator)
7486 {
7487 /* If we are processing next declarator, coma is expected */
7488 token = cp_lexer_peek_token (parser->lexer);
7489 gcc_assert (token->type == CPP_COMMA);
7490 cp_lexer_consume_token (parser->lexer);
7491 }
7492 else
7493 saw_declarator = true;
7494
7495 /* Parse the init-declarator. */
7496 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7497 /*checks=*/NULL,
7498 function_definition_allowed_p,
7499 /*member_p=*/false,
7500 declares_class_or_enum,
7501 &function_definition_p);
7502 /* If an error occurred while parsing tentatively, exit quickly.
7503 (That usually happens when in the body of a function; each
7504 statement is treated as a declaration-statement until proven
7505 otherwise.) */
7506 if (cp_parser_error_occurred (parser))
7507 goto done;
7508 /* Handle function definitions specially. */
7509 if (function_definition_p)
7510 {
7511 /* If the next token is a `,', then we are probably
7512 processing something like:
7513
7514 void f() {}, *p;
7515
7516 which is erroneous. */
7517 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7518 error ("mixing declarations and function-definitions is forbidden");
7519 /* Otherwise, we're done with the list of declarators. */
7520 else
7521 {
7522 pop_deferring_access_checks ();
7523 return;
7524 }
7525 }
7526 /* The next token should be either a `,' or a `;'. */
7527 token = cp_lexer_peek_token (parser->lexer);
7528 /* If it's a `,', there are more declarators to come. */
7529 if (token->type == CPP_COMMA)
7530 /* will be consumed next time around */;
7531 /* If it's a `;', we are done. */
7532 else if (token->type == CPP_SEMICOLON)
7533 break;
7534 /* Anything else is an error. */
7535 else
7536 {
7537 /* If we have already issued an error message we don't need
7538 to issue another one. */
7539 if (decl != error_mark_node
7540 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7541 cp_parser_error (parser, "expected %<,%> or %<;%>");
7542 /* Skip tokens until we reach the end of the statement. */
7543 cp_parser_skip_to_end_of_statement (parser);
7544 /* If the next token is now a `;', consume it. */
7545 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7546 cp_lexer_consume_token (parser->lexer);
7547 goto done;
7548 }
7549 /* After the first time around, a function-definition is not
7550 allowed -- even if it was OK at first. For example:
7551
7552 int i, f() {}
7553
7554 is not valid. */
7555 function_definition_allowed_p = false;
7556 }
7557
7558 /* Issue an error message if no declarators are present, and the
7559 decl-specifier-seq does not itself declare a class or
7560 enumeration. */
7561 if (!saw_declarator)
7562 {
7563 if (cp_parser_declares_only_class_p (parser))
7564 shadow_tag (&decl_specifiers);
7565 /* Perform any deferred access checks. */
7566 perform_deferred_access_checks ();
7567 }
7568
7569 /* Consume the `;'. */
7570 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7571
7572 done:
7573 pop_deferring_access_checks ();
7574 }
7575
7576 /* Parse a decl-specifier-seq.
7577
7578 decl-specifier-seq:
7579 decl-specifier-seq [opt] decl-specifier
7580
7581 decl-specifier:
7582 storage-class-specifier
7583 type-specifier
7584 function-specifier
7585 friend
7586 typedef
7587
7588 GNU Extension:
7589
7590 decl-specifier:
7591 attributes
7592
7593 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7594
7595 The parser flags FLAGS is used to control type-specifier parsing.
7596
7597 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7598 flags:
7599
7600 1: one of the decl-specifiers is an elaborated-type-specifier
7601 (i.e., a type declaration)
7602 2: one of the decl-specifiers is an enum-specifier or a
7603 class-specifier (i.e., a type definition)
7604
7605 */
7606
7607 static void
7608 cp_parser_decl_specifier_seq (cp_parser* parser,
7609 cp_parser_flags flags,
7610 cp_decl_specifier_seq *decl_specs,
7611 int* declares_class_or_enum)
7612 {
7613 bool constructor_possible_p = !parser->in_declarator_p;
7614
7615 /* Clear DECL_SPECS. */
7616 clear_decl_specs (decl_specs);
7617
7618 /* Assume no class or enumeration type is declared. */
7619 *declares_class_or_enum = 0;
7620
7621 /* Keep reading specifiers until there are no more to read. */
7622 while (true)
7623 {
7624 bool constructor_p;
7625 bool found_decl_spec;
7626 cp_token *token;
7627
7628 /* Peek at the next token. */
7629 token = cp_lexer_peek_token (parser->lexer);
7630 /* Handle attributes. */
7631 if (token->keyword == RID_ATTRIBUTE)
7632 {
7633 /* Parse the attributes. */
7634 decl_specs->attributes
7635 = chainon (decl_specs->attributes,
7636 cp_parser_attributes_opt (parser));
7637 continue;
7638 }
7639 /* Assume we will find a decl-specifier keyword. */
7640 found_decl_spec = true;
7641 /* If the next token is an appropriate keyword, we can simply
7642 add it to the list. */
7643 switch (token->keyword)
7644 {
7645 /* decl-specifier:
7646 friend */
7647 case RID_FRIEND:
7648 if (!at_class_scope_p ())
7649 {
7650 error ("%<friend%> used outside of class");
7651 cp_lexer_purge_token (parser->lexer);
7652 }
7653 else
7654 {
7655 ++decl_specs->specs[(int) ds_friend];
7656 /* Consume the token. */
7657 cp_lexer_consume_token (parser->lexer);
7658 }
7659 break;
7660
7661 /* function-specifier:
7662 inline
7663 virtual
7664 explicit */
7665 case RID_INLINE:
7666 case RID_VIRTUAL:
7667 case RID_EXPLICIT:
7668 cp_parser_function_specifier_opt (parser, decl_specs);
7669 break;
7670
7671 /* decl-specifier:
7672 typedef */
7673 case RID_TYPEDEF:
7674 ++decl_specs->specs[(int) ds_typedef];
7675 /* Consume the token. */
7676 cp_lexer_consume_token (parser->lexer);
7677 /* A constructor declarator cannot appear in a typedef. */
7678 constructor_possible_p = false;
7679 /* The "typedef" keyword can only occur in a declaration; we
7680 may as well commit at this point. */
7681 cp_parser_commit_to_tentative_parse (parser);
7682
7683 if (decl_specs->storage_class != sc_none)
7684 decl_specs->conflicting_specifiers_p = true;
7685 break;
7686
7687 /* storage-class-specifier:
7688 auto
7689 register
7690 static
7691 extern
7692 mutable
7693
7694 GNU Extension:
7695 thread */
7696 case RID_AUTO:
7697 case RID_REGISTER:
7698 case RID_STATIC:
7699 case RID_EXTERN:
7700 case RID_MUTABLE:
7701 /* Consume the token. */
7702 cp_lexer_consume_token (parser->lexer);
7703 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7704 break;
7705 case RID_THREAD:
7706 /* Consume the token. */
7707 cp_lexer_consume_token (parser->lexer);
7708 ++decl_specs->specs[(int) ds_thread];
7709 break;
7710
7711 default:
7712 /* We did not yet find a decl-specifier yet. */
7713 found_decl_spec = false;
7714 break;
7715 }
7716
7717 /* Constructors are a special case. The `S' in `S()' is not a
7718 decl-specifier; it is the beginning of the declarator. */
7719 constructor_p
7720 = (!found_decl_spec
7721 && constructor_possible_p
7722 && (cp_parser_constructor_declarator_p
7723 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7724
7725 /* If we don't have a DECL_SPEC yet, then we must be looking at
7726 a type-specifier. */
7727 if (!found_decl_spec && !constructor_p)
7728 {
7729 int decl_spec_declares_class_or_enum;
7730 bool is_cv_qualifier;
7731 tree type_spec;
7732
7733 type_spec
7734 = cp_parser_type_specifier (parser, flags,
7735 decl_specs,
7736 /*is_declaration=*/true,
7737 &decl_spec_declares_class_or_enum,
7738 &is_cv_qualifier);
7739
7740 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7741
7742 /* If this type-specifier referenced a user-defined type
7743 (a typedef, class-name, etc.), then we can't allow any
7744 more such type-specifiers henceforth.
7745
7746 [dcl.spec]
7747
7748 The longest sequence of decl-specifiers that could
7749 possibly be a type name is taken as the
7750 decl-specifier-seq of a declaration. The sequence shall
7751 be self-consistent as described below.
7752
7753 [dcl.type]
7754
7755 As a general rule, at most one type-specifier is allowed
7756 in the complete decl-specifier-seq of a declaration. The
7757 only exceptions are the following:
7758
7759 -- const or volatile can be combined with any other
7760 type-specifier.
7761
7762 -- signed or unsigned can be combined with char, long,
7763 short, or int.
7764
7765 -- ..
7766
7767 Example:
7768
7769 typedef char* Pc;
7770 void g (const int Pc);
7771
7772 Here, Pc is *not* part of the decl-specifier seq; it's
7773 the declarator. Therefore, once we see a type-specifier
7774 (other than a cv-qualifier), we forbid any additional
7775 user-defined types. We *do* still allow things like `int
7776 int' to be considered a decl-specifier-seq, and issue the
7777 error message later. */
7778 if (type_spec && !is_cv_qualifier)
7779 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7780 /* A constructor declarator cannot follow a type-specifier. */
7781 if (type_spec)
7782 {
7783 constructor_possible_p = false;
7784 found_decl_spec = true;
7785 }
7786 }
7787
7788 /* If we still do not have a DECL_SPEC, then there are no more
7789 decl-specifiers. */
7790 if (!found_decl_spec)
7791 break;
7792
7793 decl_specs->any_specifiers_p = true;
7794 /* After we see one decl-specifier, further decl-specifiers are
7795 always optional. */
7796 flags |= CP_PARSER_FLAGS_OPTIONAL;
7797 }
7798
7799 cp_parser_check_decl_spec (decl_specs);
7800
7801 /* Don't allow a friend specifier with a class definition. */
7802 if (decl_specs->specs[(int) ds_friend] != 0
7803 && (*declares_class_or_enum & 2))
7804 error ("class definition may not be declared a friend");
7805 }
7806
7807 /* Parse an (optional) storage-class-specifier.
7808
7809 storage-class-specifier:
7810 auto
7811 register
7812 static
7813 extern
7814 mutable
7815
7816 GNU Extension:
7817
7818 storage-class-specifier:
7819 thread
7820
7821 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7822
7823 static tree
7824 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7825 {
7826 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7827 {
7828 case RID_AUTO:
7829 case RID_REGISTER:
7830 case RID_STATIC:
7831 case RID_EXTERN:
7832 case RID_MUTABLE:
7833 case RID_THREAD:
7834 /* Consume the token. */
7835 return cp_lexer_consume_token (parser->lexer)->u.value;
7836
7837 default:
7838 return NULL_TREE;
7839 }
7840 }
7841
7842 /* Parse an (optional) function-specifier.
7843
7844 function-specifier:
7845 inline
7846 virtual
7847 explicit
7848
7849 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7850 Updates DECL_SPECS, if it is non-NULL. */
7851
7852 static tree
7853 cp_parser_function_specifier_opt (cp_parser* parser,
7854 cp_decl_specifier_seq *decl_specs)
7855 {
7856 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7857 {
7858 case RID_INLINE:
7859 if (decl_specs)
7860 ++decl_specs->specs[(int) ds_inline];
7861 break;
7862
7863 case RID_VIRTUAL:
7864 /* 14.5.2.3 [temp.mem]
7865
7866 A member function template shall not be virtual. */
7867 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7868 error ("templates may not be %<virtual%>");
7869 else if (decl_specs)
7870 ++decl_specs->specs[(int) ds_virtual];
7871 break;
7872
7873 case RID_EXPLICIT:
7874 if (decl_specs)
7875 ++decl_specs->specs[(int) ds_explicit];
7876 break;
7877
7878 default:
7879 return NULL_TREE;
7880 }
7881
7882 /* Consume the token. */
7883 return cp_lexer_consume_token (parser->lexer)->u.value;
7884 }
7885
7886 /* Parse a linkage-specification.
7887
7888 linkage-specification:
7889 extern string-literal { declaration-seq [opt] }
7890 extern string-literal declaration */
7891
7892 static void
7893 cp_parser_linkage_specification (cp_parser* parser)
7894 {
7895 tree linkage;
7896
7897 /* Look for the `extern' keyword. */
7898 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7899
7900 /* Look for the string-literal. */
7901 linkage = cp_parser_string_literal (parser, false, false);
7902
7903 /* Transform the literal into an identifier. If the literal is a
7904 wide-character string, or contains embedded NULs, then we can't
7905 handle it as the user wants. */
7906 if (strlen (TREE_STRING_POINTER (linkage))
7907 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7908 {
7909 cp_parser_error (parser, "invalid linkage-specification");
7910 /* Assume C++ linkage. */
7911 linkage = lang_name_cplusplus;
7912 }
7913 else
7914 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7915
7916 /* We're now using the new linkage. */
7917 push_lang_context (linkage);
7918
7919 /* If the next token is a `{', then we're using the first
7920 production. */
7921 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7922 {
7923 /* Consume the `{' token. */
7924 cp_lexer_consume_token (parser->lexer);
7925 /* Parse the declarations. */
7926 cp_parser_declaration_seq_opt (parser);
7927 /* Look for the closing `}'. */
7928 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7929 }
7930 /* Otherwise, there's just one declaration. */
7931 else
7932 {
7933 bool saved_in_unbraced_linkage_specification_p;
7934
7935 saved_in_unbraced_linkage_specification_p
7936 = parser->in_unbraced_linkage_specification_p;
7937 parser->in_unbraced_linkage_specification_p = true;
7938 cp_parser_declaration (parser);
7939 parser->in_unbraced_linkage_specification_p
7940 = saved_in_unbraced_linkage_specification_p;
7941 }
7942
7943 /* We're done with the linkage-specification. */
7944 pop_lang_context ();
7945 }
7946
7947 /* Parse a static_assert-declaration.
7948
7949 static_assert-declaration:
7950 static_assert ( constant-expression , string-literal ) ;
7951
7952 If MEMBER_P, this static_assert is a class member. */
7953
7954 static void
7955 cp_parser_static_assert(cp_parser *parser, bool member_p)
7956 {
7957 tree condition;
7958 tree message;
7959 cp_token *token;
7960 location_t saved_loc;
7961
7962 /* Peek at the `static_assert' token so we can keep track of exactly
7963 where the static assertion started. */
7964 token = cp_lexer_peek_token (parser->lexer);
7965 saved_loc = token->location;
7966
7967 /* Look for the `static_assert' keyword. */
7968 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
7969 "`static_assert'"))
7970 return;
7971
7972 /* We know we are in a static assertion; commit to any tentative
7973 parse. */
7974 if (cp_parser_parsing_tentatively (parser))
7975 cp_parser_commit_to_tentative_parse (parser);
7976
7977 /* Parse the `(' starting the static assertion condition. */
7978 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7979
7980 /* Parse the constant-expression. */
7981 condition =
7982 cp_parser_constant_expression (parser,
7983 /*allow_non_constant_p=*/false,
7984 /*non_constant_p=*/NULL);
7985
7986 /* Parse the separating `,'. */
7987 cp_parser_require (parser, CPP_COMMA, "`,'");
7988
7989 /* Parse the string-literal message. */
7990 message = cp_parser_string_literal (parser,
7991 /*translate=*/false,
7992 /*wide_ok=*/true);
7993
7994 /* A `)' completes the static assertion. */
7995 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7996 cp_parser_skip_to_closing_parenthesis (parser,
7997 /*recovering=*/true,
7998 /*or_comma=*/false,
7999 /*consume_paren=*/true);
8000
8001 /* A semicolon terminates the declaration. */
8002 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8003
8004 /* Complete the static assertion, which may mean either processing
8005 the static assert now or saving it for template instantiation. */
8006 finish_static_assert (condition, message, saved_loc, member_p);
8007 }
8008
8009 /* Special member functions [gram.special] */
8010
8011 /* Parse a conversion-function-id.
8012
8013 conversion-function-id:
8014 operator conversion-type-id
8015
8016 Returns an IDENTIFIER_NODE representing the operator. */
8017
8018 static tree
8019 cp_parser_conversion_function_id (cp_parser* parser)
8020 {
8021 tree type;
8022 tree saved_scope;
8023 tree saved_qualifying_scope;
8024 tree saved_object_scope;
8025 tree pushed_scope = NULL_TREE;
8026
8027 /* Look for the `operator' token. */
8028 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8029 return error_mark_node;
8030 /* When we parse the conversion-type-id, the current scope will be
8031 reset. However, we need that information in able to look up the
8032 conversion function later, so we save it here. */
8033 saved_scope = parser->scope;
8034 saved_qualifying_scope = parser->qualifying_scope;
8035 saved_object_scope = parser->object_scope;
8036 /* We must enter the scope of the class so that the names of
8037 entities declared within the class are available in the
8038 conversion-type-id. For example, consider:
8039
8040 struct S {
8041 typedef int I;
8042 operator I();
8043 };
8044
8045 S::operator I() { ... }
8046
8047 In order to see that `I' is a type-name in the definition, we
8048 must be in the scope of `S'. */
8049 if (saved_scope)
8050 pushed_scope = push_scope (saved_scope);
8051 /* Parse the conversion-type-id. */
8052 type = cp_parser_conversion_type_id (parser);
8053 /* Leave the scope of the class, if any. */
8054 if (pushed_scope)
8055 pop_scope (pushed_scope);
8056 /* Restore the saved scope. */
8057 parser->scope = saved_scope;
8058 parser->qualifying_scope = saved_qualifying_scope;
8059 parser->object_scope = saved_object_scope;
8060 /* If the TYPE is invalid, indicate failure. */
8061 if (type == error_mark_node)
8062 return error_mark_node;
8063 return mangle_conv_op_name_for_type (type);
8064 }
8065
8066 /* Parse a conversion-type-id:
8067
8068 conversion-type-id:
8069 type-specifier-seq conversion-declarator [opt]
8070
8071 Returns the TYPE specified. */
8072
8073 static tree
8074 cp_parser_conversion_type_id (cp_parser* parser)
8075 {
8076 tree attributes;
8077 cp_decl_specifier_seq type_specifiers;
8078 cp_declarator *declarator;
8079 tree type_specified;
8080
8081 /* Parse the attributes. */
8082 attributes = cp_parser_attributes_opt (parser);
8083 /* Parse the type-specifiers. */
8084 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8085 &type_specifiers);
8086 /* If that didn't work, stop. */
8087 if (type_specifiers.type == error_mark_node)
8088 return error_mark_node;
8089 /* Parse the conversion-declarator. */
8090 declarator = cp_parser_conversion_declarator_opt (parser);
8091
8092 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8093 /*initialized=*/0, &attributes);
8094 if (attributes)
8095 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8096 return type_specified;
8097 }
8098
8099 /* Parse an (optional) conversion-declarator.
8100
8101 conversion-declarator:
8102 ptr-operator conversion-declarator [opt]
8103
8104 */
8105
8106 static cp_declarator *
8107 cp_parser_conversion_declarator_opt (cp_parser* parser)
8108 {
8109 enum tree_code code;
8110 tree class_type;
8111 cp_cv_quals cv_quals;
8112
8113 /* We don't know if there's a ptr-operator next, or not. */
8114 cp_parser_parse_tentatively (parser);
8115 /* Try the ptr-operator. */
8116 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8117 /* If it worked, look for more conversion-declarators. */
8118 if (cp_parser_parse_definitely (parser))
8119 {
8120 cp_declarator *declarator;
8121
8122 /* Parse another optional declarator. */
8123 declarator = cp_parser_conversion_declarator_opt (parser);
8124
8125 /* Create the representation of the declarator. */
8126 if (class_type)
8127 declarator = make_ptrmem_declarator (cv_quals, class_type,
8128 declarator);
8129 else if (code == INDIRECT_REF)
8130 declarator = make_pointer_declarator (cv_quals, declarator);
8131 else
8132 declarator = make_reference_declarator (cv_quals, declarator);
8133
8134 return declarator;
8135 }
8136
8137 return NULL;
8138 }
8139
8140 /* Parse an (optional) ctor-initializer.
8141
8142 ctor-initializer:
8143 : mem-initializer-list
8144
8145 Returns TRUE iff the ctor-initializer was actually present. */
8146
8147 static bool
8148 cp_parser_ctor_initializer_opt (cp_parser* parser)
8149 {
8150 /* If the next token is not a `:', then there is no
8151 ctor-initializer. */
8152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8153 {
8154 /* Do default initialization of any bases and members. */
8155 if (DECL_CONSTRUCTOR_P (current_function_decl))
8156 finish_mem_initializers (NULL_TREE);
8157
8158 return false;
8159 }
8160
8161 /* Consume the `:' token. */
8162 cp_lexer_consume_token (parser->lexer);
8163 /* And the mem-initializer-list. */
8164 cp_parser_mem_initializer_list (parser);
8165
8166 return true;
8167 }
8168
8169 /* Parse a mem-initializer-list.
8170
8171 mem-initializer-list:
8172 mem-initializer
8173 mem-initializer , mem-initializer-list */
8174
8175 static void
8176 cp_parser_mem_initializer_list (cp_parser* parser)
8177 {
8178 tree mem_initializer_list = NULL_TREE;
8179
8180 /* Let the semantic analysis code know that we are starting the
8181 mem-initializer-list. */
8182 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8183 error ("only constructors take base initializers");
8184
8185 /* Loop through the list. */
8186 while (true)
8187 {
8188 tree mem_initializer;
8189
8190 /* Parse the mem-initializer. */
8191 mem_initializer = cp_parser_mem_initializer (parser);
8192 /* Add it to the list, unless it was erroneous. */
8193 if (mem_initializer != error_mark_node)
8194 {
8195 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8196 mem_initializer_list = mem_initializer;
8197 }
8198 /* If the next token is not a `,', we're done. */
8199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8200 break;
8201 /* Consume the `,' token. */
8202 cp_lexer_consume_token (parser->lexer);
8203 }
8204
8205 /* Perform semantic analysis. */
8206 if (DECL_CONSTRUCTOR_P (current_function_decl))
8207 finish_mem_initializers (mem_initializer_list);
8208 }
8209
8210 /* Parse a mem-initializer.
8211
8212 mem-initializer:
8213 mem-initializer-id ( expression-list [opt] )
8214
8215 GNU extension:
8216
8217 mem-initializer:
8218 ( expression-list [opt] )
8219
8220 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8221 class) or FIELD_DECL (for a non-static data member) to initialize;
8222 the TREE_VALUE is the expression-list. An empty initialization
8223 list is represented by void_list_node. */
8224
8225 static tree
8226 cp_parser_mem_initializer (cp_parser* parser)
8227 {
8228 tree mem_initializer_id;
8229 tree expression_list;
8230 tree member;
8231
8232 /* Find out what is being initialized. */
8233 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8234 {
8235 pedwarn ("anachronistic old-style base class initializer");
8236 mem_initializer_id = NULL_TREE;
8237 }
8238 else
8239 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8240 member = expand_member_init (mem_initializer_id);
8241 if (member && !DECL_P (member))
8242 in_base_initializer = 1;
8243
8244 expression_list
8245 = cp_parser_parenthesized_expression_list (parser, false,
8246 /*cast_p=*/false,
8247 /*non_constant_p=*/NULL);
8248 if (expression_list == error_mark_node)
8249 return error_mark_node;
8250 if (!expression_list)
8251 expression_list = void_type_node;
8252
8253 in_base_initializer = 0;
8254
8255 return member ? build_tree_list (member, expression_list) : error_mark_node;
8256 }
8257
8258 /* Parse a mem-initializer-id.
8259
8260 mem-initializer-id:
8261 :: [opt] nested-name-specifier [opt] class-name
8262 identifier
8263
8264 Returns a TYPE indicating the class to be initializer for the first
8265 production. Returns an IDENTIFIER_NODE indicating the data member
8266 to be initialized for the second production. */
8267
8268 static tree
8269 cp_parser_mem_initializer_id (cp_parser* parser)
8270 {
8271 bool global_scope_p;
8272 bool nested_name_specifier_p;
8273 bool template_p = false;
8274 tree id;
8275
8276 /* `typename' is not allowed in this context ([temp.res]). */
8277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8278 {
8279 error ("keyword %<typename%> not allowed in this context (a qualified "
8280 "member initializer is implicitly a type)");
8281 cp_lexer_consume_token (parser->lexer);
8282 }
8283 /* Look for the optional `::' operator. */
8284 global_scope_p
8285 = (cp_parser_global_scope_opt (parser,
8286 /*current_scope_valid_p=*/false)
8287 != NULL_TREE);
8288 /* Look for the optional nested-name-specifier. The simplest way to
8289 implement:
8290
8291 [temp.res]
8292
8293 The keyword `typename' is not permitted in a base-specifier or
8294 mem-initializer; in these contexts a qualified name that
8295 depends on a template-parameter is implicitly assumed to be a
8296 type name.
8297
8298 is to assume that we have seen the `typename' keyword at this
8299 point. */
8300 nested_name_specifier_p
8301 = (cp_parser_nested_name_specifier_opt (parser,
8302 /*typename_keyword_p=*/true,
8303 /*check_dependency_p=*/true,
8304 /*type_p=*/true,
8305 /*is_declaration=*/true)
8306 != NULL_TREE);
8307 if (nested_name_specifier_p)
8308 template_p = cp_parser_optional_template_keyword (parser);
8309 /* If there is a `::' operator or a nested-name-specifier, then we
8310 are definitely looking for a class-name. */
8311 if (global_scope_p || nested_name_specifier_p)
8312 return cp_parser_class_name (parser,
8313 /*typename_keyword_p=*/true,
8314 /*template_keyword_p=*/template_p,
8315 none_type,
8316 /*check_dependency_p=*/true,
8317 /*class_head_p=*/false,
8318 /*is_declaration=*/true);
8319 /* Otherwise, we could also be looking for an ordinary identifier. */
8320 cp_parser_parse_tentatively (parser);
8321 /* Try a class-name. */
8322 id = cp_parser_class_name (parser,
8323 /*typename_keyword_p=*/true,
8324 /*template_keyword_p=*/false,
8325 none_type,
8326 /*check_dependency_p=*/true,
8327 /*class_head_p=*/false,
8328 /*is_declaration=*/true);
8329 /* If we found one, we're done. */
8330 if (cp_parser_parse_definitely (parser))
8331 return id;
8332 /* Otherwise, look for an ordinary identifier. */
8333 return cp_parser_identifier (parser);
8334 }
8335
8336 /* Overloading [gram.over] */
8337
8338 /* Parse an operator-function-id.
8339
8340 operator-function-id:
8341 operator operator
8342
8343 Returns an IDENTIFIER_NODE for the operator which is a
8344 human-readable spelling of the identifier, e.g., `operator +'. */
8345
8346 static tree
8347 cp_parser_operator_function_id (cp_parser* parser)
8348 {
8349 /* Look for the `operator' keyword. */
8350 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8351 return error_mark_node;
8352 /* And then the name of the operator itself. */
8353 return cp_parser_operator (parser);
8354 }
8355
8356 /* Parse an operator.
8357
8358 operator:
8359 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8360 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8361 || ++ -- , ->* -> () []
8362
8363 GNU Extensions:
8364
8365 operator:
8366 <? >? <?= >?=
8367
8368 Returns an IDENTIFIER_NODE for the operator which is a
8369 human-readable spelling of the identifier, e.g., `operator +'. */
8370
8371 static tree
8372 cp_parser_operator (cp_parser* parser)
8373 {
8374 tree id = NULL_TREE;
8375 cp_token *token;
8376
8377 /* Peek at the next token. */
8378 token = cp_lexer_peek_token (parser->lexer);
8379 /* Figure out which operator we have. */
8380 switch (token->type)
8381 {
8382 case CPP_KEYWORD:
8383 {
8384 enum tree_code op;
8385
8386 /* The keyword should be either `new' or `delete'. */
8387 if (token->keyword == RID_NEW)
8388 op = NEW_EXPR;
8389 else if (token->keyword == RID_DELETE)
8390 op = DELETE_EXPR;
8391 else
8392 break;
8393
8394 /* Consume the `new' or `delete' token. */
8395 cp_lexer_consume_token (parser->lexer);
8396
8397 /* Peek at the next token. */
8398 token = cp_lexer_peek_token (parser->lexer);
8399 /* If it's a `[' token then this is the array variant of the
8400 operator. */
8401 if (token->type == CPP_OPEN_SQUARE)
8402 {
8403 /* Consume the `[' token. */
8404 cp_lexer_consume_token (parser->lexer);
8405 /* Look for the `]' token. */
8406 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8407 id = ansi_opname (op == NEW_EXPR
8408 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8409 }
8410 /* Otherwise, we have the non-array variant. */
8411 else
8412 id = ansi_opname (op);
8413
8414 return id;
8415 }
8416
8417 case CPP_PLUS:
8418 id = ansi_opname (PLUS_EXPR);
8419 break;
8420
8421 case CPP_MINUS:
8422 id = ansi_opname (MINUS_EXPR);
8423 break;
8424
8425 case CPP_MULT:
8426 id = ansi_opname (MULT_EXPR);
8427 break;
8428
8429 case CPP_DIV:
8430 id = ansi_opname (TRUNC_DIV_EXPR);
8431 break;
8432
8433 case CPP_MOD:
8434 id = ansi_opname (TRUNC_MOD_EXPR);
8435 break;
8436
8437 case CPP_XOR:
8438 id = ansi_opname (BIT_XOR_EXPR);
8439 break;
8440
8441 case CPP_AND:
8442 id = ansi_opname (BIT_AND_EXPR);
8443 break;
8444
8445 case CPP_OR:
8446 id = ansi_opname (BIT_IOR_EXPR);
8447 break;
8448
8449 case CPP_COMPL:
8450 id = ansi_opname (BIT_NOT_EXPR);
8451 break;
8452
8453 case CPP_NOT:
8454 id = ansi_opname (TRUTH_NOT_EXPR);
8455 break;
8456
8457 case CPP_EQ:
8458 id = ansi_assopname (NOP_EXPR);
8459 break;
8460
8461 case CPP_LESS:
8462 id = ansi_opname (LT_EXPR);
8463 break;
8464
8465 case CPP_GREATER:
8466 id = ansi_opname (GT_EXPR);
8467 break;
8468
8469 case CPP_PLUS_EQ:
8470 id = ansi_assopname (PLUS_EXPR);
8471 break;
8472
8473 case CPP_MINUS_EQ:
8474 id = ansi_assopname (MINUS_EXPR);
8475 break;
8476
8477 case CPP_MULT_EQ:
8478 id = ansi_assopname (MULT_EXPR);
8479 break;
8480
8481 case CPP_DIV_EQ:
8482 id = ansi_assopname (TRUNC_DIV_EXPR);
8483 break;
8484
8485 case CPP_MOD_EQ:
8486 id = ansi_assopname (TRUNC_MOD_EXPR);
8487 break;
8488
8489 case CPP_XOR_EQ:
8490 id = ansi_assopname (BIT_XOR_EXPR);
8491 break;
8492
8493 case CPP_AND_EQ:
8494 id = ansi_assopname (BIT_AND_EXPR);
8495 break;
8496
8497 case CPP_OR_EQ:
8498 id = ansi_assopname (BIT_IOR_EXPR);
8499 break;
8500
8501 case CPP_LSHIFT:
8502 id = ansi_opname (LSHIFT_EXPR);
8503 break;
8504
8505 case CPP_RSHIFT:
8506 id = ansi_opname (RSHIFT_EXPR);
8507 break;
8508
8509 case CPP_LSHIFT_EQ:
8510 id = ansi_assopname (LSHIFT_EXPR);
8511 break;
8512
8513 case CPP_RSHIFT_EQ:
8514 id = ansi_assopname (RSHIFT_EXPR);
8515 break;
8516
8517 case CPP_EQ_EQ:
8518 id = ansi_opname (EQ_EXPR);
8519 break;
8520
8521 case CPP_NOT_EQ:
8522 id = ansi_opname (NE_EXPR);
8523 break;
8524
8525 case CPP_LESS_EQ:
8526 id = ansi_opname (LE_EXPR);
8527 break;
8528
8529 case CPP_GREATER_EQ:
8530 id = ansi_opname (GE_EXPR);
8531 break;
8532
8533 case CPP_AND_AND:
8534 id = ansi_opname (TRUTH_ANDIF_EXPR);
8535 break;
8536
8537 case CPP_OR_OR:
8538 id = ansi_opname (TRUTH_ORIF_EXPR);
8539 break;
8540
8541 case CPP_PLUS_PLUS:
8542 id = ansi_opname (POSTINCREMENT_EXPR);
8543 break;
8544
8545 case CPP_MINUS_MINUS:
8546 id = ansi_opname (PREDECREMENT_EXPR);
8547 break;
8548
8549 case CPP_COMMA:
8550 id = ansi_opname (COMPOUND_EXPR);
8551 break;
8552
8553 case CPP_DEREF_STAR:
8554 id = ansi_opname (MEMBER_REF);
8555 break;
8556
8557 case CPP_DEREF:
8558 id = ansi_opname (COMPONENT_REF);
8559 break;
8560
8561 case CPP_OPEN_PAREN:
8562 /* Consume the `('. */
8563 cp_lexer_consume_token (parser->lexer);
8564 /* Look for the matching `)'. */
8565 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8566 return ansi_opname (CALL_EXPR);
8567
8568 case CPP_OPEN_SQUARE:
8569 /* Consume the `['. */
8570 cp_lexer_consume_token (parser->lexer);
8571 /* Look for the matching `]'. */
8572 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8573 return ansi_opname (ARRAY_REF);
8574
8575 default:
8576 /* Anything else is an error. */
8577 break;
8578 }
8579
8580 /* If we have selected an identifier, we need to consume the
8581 operator token. */
8582 if (id)
8583 cp_lexer_consume_token (parser->lexer);
8584 /* Otherwise, no valid operator name was present. */
8585 else
8586 {
8587 cp_parser_error (parser, "expected operator");
8588 id = error_mark_node;
8589 }
8590
8591 return id;
8592 }
8593
8594 /* Parse a template-declaration.
8595
8596 template-declaration:
8597 export [opt] template < template-parameter-list > declaration
8598
8599 If MEMBER_P is TRUE, this template-declaration occurs within a
8600 class-specifier.
8601
8602 The grammar rule given by the standard isn't correct. What
8603 is really meant is:
8604
8605 template-declaration:
8606 export [opt] template-parameter-list-seq
8607 decl-specifier-seq [opt] init-declarator [opt] ;
8608 export [opt] template-parameter-list-seq
8609 function-definition
8610
8611 template-parameter-list-seq:
8612 template-parameter-list-seq [opt]
8613 template < template-parameter-list > */
8614
8615 static void
8616 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8617 {
8618 /* Check for `export'. */
8619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8620 {
8621 /* Consume the `export' token. */
8622 cp_lexer_consume_token (parser->lexer);
8623 /* Warn that we do not support `export'. */
8624 warning (0, "keyword %<export%> not implemented, and will be ignored");
8625 }
8626
8627 cp_parser_template_declaration_after_export (parser, member_p);
8628 }
8629
8630 /* Parse a template-parameter-list.
8631
8632 template-parameter-list:
8633 template-parameter
8634 template-parameter-list , template-parameter
8635
8636 Returns a TREE_LIST. Each node represents a template parameter.
8637 The nodes are connected via their TREE_CHAINs. */
8638
8639 static tree
8640 cp_parser_template_parameter_list (cp_parser* parser)
8641 {
8642 tree parameter_list = NULL_TREE;
8643
8644 begin_template_parm_list ();
8645 while (true)
8646 {
8647 tree parameter;
8648 cp_token *token;
8649 bool is_non_type;
8650
8651 /* Parse the template-parameter. */
8652 parameter = cp_parser_template_parameter (parser, &is_non_type);
8653 /* Add it to the list. */
8654 if (parameter != error_mark_node)
8655 parameter_list = process_template_parm (parameter_list,
8656 parameter,
8657 is_non_type);
8658 else
8659 {
8660 tree err_parm = build_tree_list (parameter, parameter);
8661 TREE_VALUE (err_parm) = error_mark_node;
8662 parameter_list = chainon (parameter_list, err_parm);
8663 }
8664
8665 /* Peek at the next token. */
8666 token = cp_lexer_peek_token (parser->lexer);
8667 /* If it's not a `,', we're done. */
8668 if (token->type != CPP_COMMA)
8669 break;
8670 /* Otherwise, consume the `,' token. */
8671 cp_lexer_consume_token (parser->lexer);
8672 }
8673
8674 return end_template_parm_list (parameter_list);
8675 }
8676
8677 /* Parse a template-parameter.
8678
8679 template-parameter:
8680 type-parameter
8681 parameter-declaration
8682
8683 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8684 the parameter. The TREE_PURPOSE is the default value, if any.
8685 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8686 iff this parameter is a non-type parameter. */
8687
8688 static tree
8689 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8690 {
8691 cp_token *token;
8692 cp_parameter_declarator *parameter_declarator;
8693 tree parm;
8694
8695 /* Assume it is a type parameter or a template parameter. */
8696 *is_non_type = false;
8697 /* Peek at the next token. */
8698 token = cp_lexer_peek_token (parser->lexer);
8699 /* If it is `class' or `template', we have a type-parameter. */
8700 if (token->keyword == RID_TEMPLATE)
8701 return cp_parser_type_parameter (parser);
8702 /* If it is `class' or `typename' we do not know yet whether it is a
8703 type parameter or a non-type parameter. Consider:
8704
8705 template <typename T, typename T::X X> ...
8706
8707 or:
8708
8709 template <class C, class D*> ...
8710
8711 Here, the first parameter is a type parameter, and the second is
8712 a non-type parameter. We can tell by looking at the token after
8713 the identifier -- if it is a `,', `=', or `>' then we have a type
8714 parameter. */
8715 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8716 {
8717 /* Peek at the token after `class' or `typename'. */
8718 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8719 /* If it's an identifier, skip it. */
8720 if (token->type == CPP_NAME)
8721 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8722 /* Now, see if the token looks like the end of a template
8723 parameter. */
8724 if (token->type == CPP_COMMA
8725 || token->type == CPP_EQ
8726 || token->type == CPP_GREATER)
8727 return cp_parser_type_parameter (parser);
8728 }
8729
8730 /* Otherwise, it is a non-type parameter.
8731
8732 [temp.param]
8733
8734 When parsing a default template-argument for a non-type
8735 template-parameter, the first non-nested `>' is taken as the end
8736 of the template parameter-list rather than a greater-than
8737 operator. */
8738 *is_non_type = true;
8739 parameter_declarator
8740 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8741 /*parenthesized_p=*/NULL);
8742 parm = grokdeclarator (parameter_declarator->declarator,
8743 &parameter_declarator->decl_specifiers,
8744 PARM, /*initialized=*/0,
8745 /*attrlist=*/NULL);
8746 if (parm == error_mark_node)
8747 return error_mark_node;
8748 return build_tree_list (parameter_declarator->default_argument, parm);
8749 }
8750
8751 /* Parse a type-parameter.
8752
8753 type-parameter:
8754 class identifier [opt]
8755 class identifier [opt] = type-id
8756 typename identifier [opt]
8757 typename identifier [opt] = type-id
8758 template < template-parameter-list > class identifier [opt]
8759 template < template-parameter-list > class identifier [opt]
8760 = id-expression
8761
8762 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8763 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8764 the declaration of the parameter. */
8765
8766 static tree
8767 cp_parser_type_parameter (cp_parser* parser)
8768 {
8769 cp_token *token;
8770 tree parameter;
8771
8772 /* Look for a keyword to tell us what kind of parameter this is. */
8773 token = cp_parser_require (parser, CPP_KEYWORD,
8774 "`class', `typename', or `template'");
8775 if (!token)
8776 return error_mark_node;
8777
8778 switch (token->keyword)
8779 {
8780 case RID_CLASS:
8781 case RID_TYPENAME:
8782 {
8783 tree identifier;
8784 tree default_argument;
8785
8786 /* If the next token is an identifier, then it names the
8787 parameter. */
8788 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8789 identifier = cp_parser_identifier (parser);
8790 else
8791 identifier = NULL_TREE;
8792
8793 /* Create the parameter. */
8794 parameter = finish_template_type_parm (class_type_node, identifier);
8795
8796 /* If the next token is an `=', we have a default argument. */
8797 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8798 {
8799 /* Consume the `=' token. */
8800 cp_lexer_consume_token (parser->lexer);
8801 /* Parse the default-argument. */
8802 push_deferring_access_checks (dk_no_deferred);
8803 default_argument = cp_parser_type_id (parser);
8804 pop_deferring_access_checks ();
8805 }
8806 else
8807 default_argument = NULL_TREE;
8808
8809 /* Create the combined representation of the parameter and the
8810 default argument. */
8811 parameter = build_tree_list (default_argument, parameter);
8812 }
8813 break;
8814
8815 case RID_TEMPLATE:
8816 {
8817 tree parameter_list;
8818 tree identifier;
8819 tree default_argument;
8820
8821 /* Look for the `<'. */
8822 cp_parser_require (parser, CPP_LESS, "`<'");
8823 /* Parse the template-parameter-list. */
8824 parameter_list = cp_parser_template_parameter_list (parser);
8825 /* Look for the `>'. */
8826 cp_parser_require (parser, CPP_GREATER, "`>'");
8827 /* Look for the `class' keyword. */
8828 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8829 /* If the next token is an `=', then there is a
8830 default-argument. If the next token is a `>', we are at
8831 the end of the parameter-list. If the next token is a `,',
8832 then we are at the end of this parameter. */
8833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8834 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8835 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8836 {
8837 identifier = cp_parser_identifier (parser);
8838 /* Treat invalid names as if the parameter were nameless. */
8839 if (identifier == error_mark_node)
8840 identifier = NULL_TREE;
8841 }
8842 else
8843 identifier = NULL_TREE;
8844
8845 /* Create the template parameter. */
8846 parameter = finish_template_template_parm (class_type_node,
8847 identifier);
8848
8849 /* If the next token is an `=', then there is a
8850 default-argument. */
8851 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8852 {
8853 bool is_template;
8854
8855 /* Consume the `='. */
8856 cp_lexer_consume_token (parser->lexer);
8857 /* Parse the id-expression. */
8858 push_deferring_access_checks (dk_no_deferred);
8859 default_argument
8860 = cp_parser_id_expression (parser,
8861 /*template_keyword_p=*/false,
8862 /*check_dependency_p=*/true,
8863 /*template_p=*/&is_template,
8864 /*declarator_p=*/false,
8865 /*optional_p=*/false);
8866 if (TREE_CODE (default_argument) == TYPE_DECL)
8867 /* If the id-expression was a template-id that refers to
8868 a template-class, we already have the declaration here,
8869 so no further lookup is needed. */
8870 ;
8871 else
8872 /* Look up the name. */
8873 default_argument
8874 = cp_parser_lookup_name (parser, default_argument,
8875 none_type,
8876 /*is_template=*/is_template,
8877 /*is_namespace=*/false,
8878 /*check_dependency=*/true,
8879 /*ambiguous_decls=*/NULL);
8880 /* See if the default argument is valid. */
8881 default_argument
8882 = check_template_template_default_arg (default_argument);
8883 pop_deferring_access_checks ();
8884 }
8885 else
8886 default_argument = NULL_TREE;
8887
8888 /* Create the combined representation of the parameter and the
8889 default argument. */
8890 parameter = build_tree_list (default_argument, parameter);
8891 }
8892 break;
8893
8894 default:
8895 gcc_unreachable ();
8896 break;
8897 }
8898
8899 return parameter;
8900 }
8901
8902 /* Parse a template-id.
8903
8904 template-id:
8905 template-name < template-argument-list [opt] >
8906
8907 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8908 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8909 returned. Otherwise, if the template-name names a function, or set
8910 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8911 names a class, returns a TYPE_DECL for the specialization.
8912
8913 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8914 uninstantiated templates. */
8915
8916 static tree
8917 cp_parser_template_id (cp_parser *parser,
8918 bool template_keyword_p,
8919 bool check_dependency_p,
8920 bool is_declaration)
8921 {
8922 int i;
8923 tree template;
8924 tree arguments;
8925 tree template_id;
8926 cp_token_position start_of_id = 0;
8927 deferred_access_check *chk;
8928 VEC (deferred_access_check,gc) *access_check;
8929 cp_token *next_token, *next_token_2;
8930 bool is_identifier;
8931
8932 /* If the next token corresponds to a template-id, there is no need
8933 to reparse it. */
8934 next_token = cp_lexer_peek_token (parser->lexer);
8935 if (next_token->type == CPP_TEMPLATE_ID)
8936 {
8937 struct tree_check *check_value;
8938
8939 /* Get the stored value. */
8940 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8941 /* Perform any access checks that were deferred. */
8942 access_check = check_value->checks;
8943 if (access_check)
8944 {
8945 for (i = 0 ;
8946 VEC_iterate (deferred_access_check, access_check, i, chk) ;
8947 ++i)
8948 {
8949 perform_or_defer_access_check (chk->binfo,
8950 chk->decl,
8951 chk->diag_decl);
8952 }
8953 }
8954 /* Return the stored value. */
8955 return check_value->value;
8956 }
8957
8958 /* Avoid performing name lookup if there is no possibility of
8959 finding a template-id. */
8960 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8961 || (next_token->type == CPP_NAME
8962 && !cp_parser_nth_token_starts_template_argument_list_p
8963 (parser, 2)))
8964 {
8965 cp_parser_error (parser, "expected template-id");
8966 return error_mark_node;
8967 }
8968
8969 /* Remember where the template-id starts. */
8970 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8971 start_of_id = cp_lexer_token_position (parser->lexer, false);
8972
8973 push_deferring_access_checks (dk_deferred);
8974
8975 /* Parse the template-name. */
8976 is_identifier = false;
8977 template = cp_parser_template_name (parser, template_keyword_p,
8978 check_dependency_p,
8979 is_declaration,
8980 &is_identifier);
8981 if (template == error_mark_node || is_identifier)
8982 {
8983 pop_deferring_access_checks ();
8984 return template;
8985 }
8986
8987 /* If we find the sequence `[:' after a template-name, it's probably
8988 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8989 parse correctly the argument list. */
8990 next_token = cp_lexer_peek_token (parser->lexer);
8991 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8992 if (next_token->type == CPP_OPEN_SQUARE
8993 && next_token->flags & DIGRAPH
8994 && next_token_2->type == CPP_COLON
8995 && !(next_token_2->flags & PREV_WHITE))
8996 {
8997 cp_parser_parse_tentatively (parser);
8998 /* Change `:' into `::'. */
8999 next_token_2->type = CPP_SCOPE;
9000 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9001 CPP_LESS. */
9002 cp_lexer_consume_token (parser->lexer);
9003 /* Parse the arguments. */
9004 arguments = cp_parser_enclosed_template_argument_list (parser);
9005 if (!cp_parser_parse_definitely (parser))
9006 {
9007 /* If we couldn't parse an argument list, then we revert our changes
9008 and return simply an error. Maybe this is not a template-id
9009 after all. */
9010 next_token_2->type = CPP_COLON;
9011 cp_parser_error (parser, "expected %<<%>");
9012 pop_deferring_access_checks ();
9013 return error_mark_node;
9014 }
9015 /* Otherwise, emit an error about the invalid digraph, but continue
9016 parsing because we got our argument list. */
9017 pedwarn ("%<<::%> cannot begin a template-argument list");
9018 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9019 "between %<<%> and %<::%>");
9020 if (!flag_permissive)
9021 {
9022 static bool hint;
9023 if (!hint)
9024 {
9025 inform ("(if you use -fpermissive G++ will accept your code)");
9026 hint = true;
9027 }
9028 }
9029 }
9030 else
9031 {
9032 /* Look for the `<' that starts the template-argument-list. */
9033 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9034 {
9035 pop_deferring_access_checks ();
9036 return error_mark_node;
9037 }
9038 /* Parse the arguments. */
9039 arguments = cp_parser_enclosed_template_argument_list (parser);
9040 }
9041
9042 /* Build a representation of the specialization. */
9043 if (TREE_CODE (template) == IDENTIFIER_NODE)
9044 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9045 else if (DECL_CLASS_TEMPLATE_P (template)
9046 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9047 {
9048 bool entering_scope;
9049 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9050 template (rather than some instantiation thereof) only if
9051 is not nested within some other construct. For example, in
9052 "template <typename T> void f(T) { A<T>::", A<T> is just an
9053 instantiation of A. */
9054 entering_scope = (template_parm_scope_p ()
9055 && cp_lexer_next_token_is (parser->lexer,
9056 CPP_SCOPE));
9057 template_id
9058 = finish_template_type (template, arguments, entering_scope);
9059 }
9060 else
9061 {
9062 /* If it's not a class-template or a template-template, it should be
9063 a function-template. */
9064 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9065 || TREE_CODE (template) == OVERLOAD
9066 || BASELINK_P (template)));
9067
9068 template_id = lookup_template_function (template, arguments);
9069 }
9070
9071 /* If parsing tentatively, replace the sequence of tokens that makes
9072 up the template-id with a CPP_TEMPLATE_ID token. That way,
9073 should we re-parse the token stream, we will not have to repeat
9074 the effort required to do the parse, nor will we issue duplicate
9075 error messages about problems during instantiation of the
9076 template. */
9077 if (start_of_id)
9078 {
9079 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9080
9081 /* Reset the contents of the START_OF_ID token. */
9082 token->type = CPP_TEMPLATE_ID;
9083 /* Retrieve any deferred checks. Do not pop this access checks yet
9084 so the memory will not be reclaimed during token replacing below. */
9085 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9086 token->u.tree_check_value->value = template_id;
9087 token->u.tree_check_value->checks = get_deferred_access_checks ();
9088 token->keyword = RID_MAX;
9089
9090 /* Purge all subsequent tokens. */
9091 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9092
9093 /* ??? Can we actually assume that, if template_id ==
9094 error_mark_node, we will have issued a diagnostic to the
9095 user, as opposed to simply marking the tentative parse as
9096 failed? */
9097 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9098 error ("parse error in template argument list");
9099 }
9100
9101 pop_deferring_access_checks ();
9102 return template_id;
9103 }
9104
9105 /* Parse a template-name.
9106
9107 template-name:
9108 identifier
9109
9110 The standard should actually say:
9111
9112 template-name:
9113 identifier
9114 operator-function-id
9115
9116 A defect report has been filed about this issue.
9117
9118 A conversion-function-id cannot be a template name because they cannot
9119 be part of a template-id. In fact, looking at this code:
9120
9121 a.operator K<int>()
9122
9123 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9124 It is impossible to call a templated conversion-function-id with an
9125 explicit argument list, since the only allowed template parameter is
9126 the type to which it is converting.
9127
9128 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9129 `template' keyword, in a construction like:
9130
9131 T::template f<3>()
9132
9133 In that case `f' is taken to be a template-name, even though there
9134 is no way of knowing for sure.
9135
9136 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9137 name refers to a set of overloaded functions, at least one of which
9138 is a template, or an IDENTIFIER_NODE with the name of the template,
9139 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9140 names are looked up inside uninstantiated templates. */
9141
9142 static tree
9143 cp_parser_template_name (cp_parser* parser,
9144 bool template_keyword_p,
9145 bool check_dependency_p,
9146 bool is_declaration,
9147 bool *is_identifier)
9148 {
9149 tree identifier;
9150 tree decl;
9151 tree fns;
9152
9153 /* If the next token is `operator', then we have either an
9154 operator-function-id or a conversion-function-id. */
9155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9156 {
9157 /* We don't know whether we're looking at an
9158 operator-function-id or a conversion-function-id. */
9159 cp_parser_parse_tentatively (parser);
9160 /* Try an operator-function-id. */
9161 identifier = cp_parser_operator_function_id (parser);
9162 /* If that didn't work, try a conversion-function-id. */
9163 if (!cp_parser_parse_definitely (parser))
9164 {
9165 cp_parser_error (parser, "expected template-name");
9166 return error_mark_node;
9167 }
9168 }
9169 /* Look for the identifier. */
9170 else
9171 identifier = cp_parser_identifier (parser);
9172
9173 /* If we didn't find an identifier, we don't have a template-id. */
9174 if (identifier == error_mark_node)
9175 return error_mark_node;
9176
9177 /* If the name immediately followed the `template' keyword, then it
9178 is a template-name. However, if the next token is not `<', then
9179 we do not treat it as a template-name, since it is not being used
9180 as part of a template-id. This enables us to handle constructs
9181 like:
9182
9183 template <typename T> struct S { S(); };
9184 template <typename T> S<T>::S();
9185
9186 correctly. We would treat `S' as a template -- if it were `S<T>'
9187 -- but we do not if there is no `<'. */
9188
9189 if (processing_template_decl
9190 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9191 {
9192 /* In a declaration, in a dependent context, we pretend that the
9193 "template" keyword was present in order to improve error
9194 recovery. For example, given:
9195
9196 template <typename T> void f(T::X<int>);
9197
9198 we want to treat "X<int>" as a template-id. */
9199 if (is_declaration
9200 && !template_keyword_p
9201 && parser->scope && TYPE_P (parser->scope)
9202 && check_dependency_p
9203 && dependent_type_p (parser->scope)
9204 /* Do not do this for dtors (or ctors), since they never
9205 need the template keyword before their name. */
9206 && !constructor_name_p (identifier, parser->scope))
9207 {
9208 cp_token_position start = 0;
9209
9210 /* Explain what went wrong. */
9211 error ("non-template %qD used as template", identifier);
9212 inform ("use %<%T::template %D%> to indicate that it is a template",
9213 parser->scope, identifier);
9214 /* If parsing tentatively, find the location of the "<" token. */
9215 if (cp_parser_simulate_error (parser))
9216 start = cp_lexer_token_position (parser->lexer, true);
9217 /* Parse the template arguments so that we can issue error
9218 messages about them. */
9219 cp_lexer_consume_token (parser->lexer);
9220 cp_parser_enclosed_template_argument_list (parser);
9221 /* Skip tokens until we find a good place from which to
9222 continue parsing. */
9223 cp_parser_skip_to_closing_parenthesis (parser,
9224 /*recovering=*/true,
9225 /*or_comma=*/true,
9226 /*consume_paren=*/false);
9227 /* If parsing tentatively, permanently remove the
9228 template argument list. That will prevent duplicate
9229 error messages from being issued about the missing
9230 "template" keyword. */
9231 if (start)
9232 cp_lexer_purge_tokens_after (parser->lexer, start);
9233 if (is_identifier)
9234 *is_identifier = true;
9235 return identifier;
9236 }
9237
9238 /* If the "template" keyword is present, then there is generally
9239 no point in doing name-lookup, so we just return IDENTIFIER.
9240 But, if the qualifying scope is non-dependent then we can
9241 (and must) do name-lookup normally. */
9242 if (template_keyword_p
9243 && (!parser->scope
9244 || (TYPE_P (parser->scope)
9245 && dependent_type_p (parser->scope))))
9246 return identifier;
9247 }
9248
9249 /* Look up the name. */
9250 decl = cp_parser_lookup_name (parser, identifier,
9251 none_type,
9252 /*is_template=*/false,
9253 /*is_namespace=*/false,
9254 check_dependency_p,
9255 /*ambiguous_decls=*/NULL);
9256 decl = maybe_get_template_decl_from_type_decl (decl);
9257
9258 /* If DECL is a template, then the name was a template-name. */
9259 if (TREE_CODE (decl) == TEMPLATE_DECL)
9260 ;
9261 else
9262 {
9263 tree fn = NULL_TREE;
9264
9265 /* The standard does not explicitly indicate whether a name that
9266 names a set of overloaded declarations, some of which are
9267 templates, is a template-name. However, such a name should
9268 be a template-name; otherwise, there is no way to form a
9269 template-id for the overloaded templates. */
9270 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9271 if (TREE_CODE (fns) == OVERLOAD)
9272 for (fn = fns; fn; fn = OVL_NEXT (fn))
9273 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9274 break;
9275
9276 if (!fn)
9277 {
9278 /* The name does not name a template. */
9279 cp_parser_error (parser, "expected template-name");
9280 return error_mark_node;
9281 }
9282 }
9283
9284 /* If DECL is dependent, and refers to a function, then just return
9285 its name; we will look it up again during template instantiation. */
9286 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9287 {
9288 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9289 if (TYPE_P (scope) && dependent_type_p (scope))
9290 return identifier;
9291 }
9292
9293 return decl;
9294 }
9295
9296 /* Parse a template-argument-list.
9297
9298 template-argument-list:
9299 template-argument
9300 template-argument-list , template-argument
9301
9302 Returns a TREE_VEC containing the arguments. */
9303
9304 static tree
9305 cp_parser_template_argument_list (cp_parser* parser)
9306 {
9307 tree fixed_args[10];
9308 unsigned n_args = 0;
9309 unsigned alloced = 10;
9310 tree *arg_ary = fixed_args;
9311 tree vec;
9312 bool saved_in_template_argument_list_p;
9313 bool saved_ice_p;
9314 bool saved_non_ice_p;
9315
9316 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9317 parser->in_template_argument_list_p = true;
9318 /* Even if the template-id appears in an integral
9319 constant-expression, the contents of the argument list do
9320 not. */
9321 saved_ice_p = parser->integral_constant_expression_p;
9322 parser->integral_constant_expression_p = false;
9323 saved_non_ice_p = parser->non_integral_constant_expression_p;
9324 parser->non_integral_constant_expression_p = false;
9325 /* Parse the arguments. */
9326 do
9327 {
9328 tree argument;
9329
9330 if (n_args)
9331 /* Consume the comma. */
9332 cp_lexer_consume_token (parser->lexer);
9333
9334 /* Parse the template-argument. */
9335 argument = cp_parser_template_argument (parser);
9336 if (n_args == alloced)
9337 {
9338 alloced *= 2;
9339
9340 if (arg_ary == fixed_args)
9341 {
9342 arg_ary = XNEWVEC (tree, alloced);
9343 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9344 }
9345 else
9346 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9347 }
9348 arg_ary[n_args++] = argument;
9349 }
9350 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9351
9352 vec = make_tree_vec (n_args);
9353
9354 while (n_args--)
9355 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9356
9357 if (arg_ary != fixed_args)
9358 free (arg_ary);
9359 parser->non_integral_constant_expression_p = saved_non_ice_p;
9360 parser->integral_constant_expression_p = saved_ice_p;
9361 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9362 return vec;
9363 }
9364
9365 /* Parse a template-argument.
9366
9367 template-argument:
9368 assignment-expression
9369 type-id
9370 id-expression
9371
9372 The representation is that of an assignment-expression, type-id, or
9373 id-expression -- except that the qualified id-expression is
9374 evaluated, so that the value returned is either a DECL or an
9375 OVERLOAD.
9376
9377 Although the standard says "assignment-expression", it forbids
9378 throw-expressions or assignments in the template argument.
9379 Therefore, we use "conditional-expression" instead. */
9380
9381 static tree
9382 cp_parser_template_argument (cp_parser* parser)
9383 {
9384 tree argument;
9385 bool template_p;
9386 bool address_p;
9387 bool maybe_type_id = false;
9388 cp_token *token;
9389 cp_id_kind idk;
9390
9391 /* There's really no way to know what we're looking at, so we just
9392 try each alternative in order.
9393
9394 [temp.arg]
9395
9396 In a template-argument, an ambiguity between a type-id and an
9397 expression is resolved to a type-id, regardless of the form of
9398 the corresponding template-parameter.
9399
9400 Therefore, we try a type-id first. */
9401 cp_parser_parse_tentatively (parser);
9402 argument = cp_parser_type_id (parser);
9403 /* If there was no error parsing the type-id but the next token is a '>>',
9404 we probably found a typo for '> >'. But there are type-id which are
9405 also valid expressions. For instance:
9406
9407 struct X { int operator >> (int); };
9408 template <int V> struct Foo {};
9409 Foo<X () >> 5> r;
9410
9411 Here 'X()' is a valid type-id of a function type, but the user just
9412 wanted to write the expression "X() >> 5". Thus, we remember that we
9413 found a valid type-id, but we still try to parse the argument as an
9414 expression to see what happens. */
9415 if (!cp_parser_error_occurred (parser)
9416 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9417 {
9418 maybe_type_id = true;
9419 cp_parser_abort_tentative_parse (parser);
9420 }
9421 else
9422 {
9423 /* If the next token isn't a `,' or a `>', then this argument wasn't
9424 really finished. This means that the argument is not a valid
9425 type-id. */
9426 if (!cp_parser_next_token_ends_template_argument_p (parser))
9427 cp_parser_error (parser, "expected template-argument");
9428 /* If that worked, we're done. */
9429 if (cp_parser_parse_definitely (parser))
9430 return argument;
9431 }
9432 /* We're still not sure what the argument will be. */
9433 cp_parser_parse_tentatively (parser);
9434 /* Try a template. */
9435 argument = cp_parser_id_expression (parser,
9436 /*template_keyword_p=*/false,
9437 /*check_dependency_p=*/true,
9438 &template_p,
9439 /*declarator_p=*/false,
9440 /*optional_p=*/false);
9441 /* If the next token isn't a `,' or a `>', then this argument wasn't
9442 really finished. */
9443 if (!cp_parser_next_token_ends_template_argument_p (parser))
9444 cp_parser_error (parser, "expected template-argument");
9445 if (!cp_parser_error_occurred (parser))
9446 {
9447 /* Figure out what is being referred to. If the id-expression
9448 was for a class template specialization, then we will have a
9449 TYPE_DECL at this point. There is no need to do name lookup
9450 at this point in that case. */
9451 if (TREE_CODE (argument) != TYPE_DECL)
9452 argument = cp_parser_lookup_name (parser, argument,
9453 none_type,
9454 /*is_template=*/template_p,
9455 /*is_namespace=*/false,
9456 /*check_dependency=*/true,
9457 /*ambiguous_decls=*/NULL);
9458 if (TREE_CODE (argument) != TEMPLATE_DECL
9459 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9460 cp_parser_error (parser, "expected template-name");
9461 }
9462 if (cp_parser_parse_definitely (parser))
9463 return argument;
9464 /* It must be a non-type argument. There permitted cases are given
9465 in [temp.arg.nontype]:
9466
9467 -- an integral constant-expression of integral or enumeration
9468 type; or
9469
9470 -- the name of a non-type template-parameter; or
9471
9472 -- the name of an object or function with external linkage...
9473
9474 -- the address of an object or function with external linkage...
9475
9476 -- a pointer to member... */
9477 /* Look for a non-type template parameter. */
9478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9479 {
9480 cp_parser_parse_tentatively (parser);
9481 argument = cp_parser_primary_expression (parser,
9482 /*adress_p=*/false,
9483 /*cast_p=*/false,
9484 /*template_arg_p=*/true,
9485 &idk);
9486 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9487 || !cp_parser_next_token_ends_template_argument_p (parser))
9488 cp_parser_simulate_error (parser);
9489 if (cp_parser_parse_definitely (parser))
9490 return argument;
9491 }
9492
9493 /* If the next token is "&", the argument must be the address of an
9494 object or function with external linkage. */
9495 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9496 if (address_p)
9497 cp_lexer_consume_token (parser->lexer);
9498 /* See if we might have an id-expression. */
9499 token = cp_lexer_peek_token (parser->lexer);
9500 if (token->type == CPP_NAME
9501 || token->keyword == RID_OPERATOR
9502 || token->type == CPP_SCOPE
9503 || token->type == CPP_TEMPLATE_ID
9504 || token->type == CPP_NESTED_NAME_SPECIFIER)
9505 {
9506 cp_parser_parse_tentatively (parser);
9507 argument = cp_parser_primary_expression (parser,
9508 address_p,
9509 /*cast_p=*/false,
9510 /*template_arg_p=*/true,
9511 &idk);
9512 if (cp_parser_error_occurred (parser)
9513 || !cp_parser_next_token_ends_template_argument_p (parser))
9514 cp_parser_abort_tentative_parse (parser);
9515 else
9516 {
9517 if (TREE_CODE (argument) == INDIRECT_REF)
9518 {
9519 gcc_assert (REFERENCE_REF_P (argument));
9520 argument = TREE_OPERAND (argument, 0);
9521 }
9522
9523 if (TREE_CODE (argument) == VAR_DECL)
9524 {
9525 /* A variable without external linkage might still be a
9526 valid constant-expression, so no error is issued here
9527 if the external-linkage check fails. */
9528 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9529 cp_parser_simulate_error (parser);
9530 }
9531 else if (is_overloaded_fn (argument))
9532 /* All overloaded functions are allowed; if the external
9533 linkage test does not pass, an error will be issued
9534 later. */
9535 ;
9536 else if (address_p
9537 && (TREE_CODE (argument) == OFFSET_REF
9538 || TREE_CODE (argument) == SCOPE_REF))
9539 /* A pointer-to-member. */
9540 ;
9541 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9542 ;
9543 else
9544 cp_parser_simulate_error (parser);
9545
9546 if (cp_parser_parse_definitely (parser))
9547 {
9548 if (address_p)
9549 argument = build_x_unary_op (ADDR_EXPR, argument);
9550 return argument;
9551 }
9552 }
9553 }
9554 /* If the argument started with "&", there are no other valid
9555 alternatives at this point. */
9556 if (address_p)
9557 {
9558 cp_parser_error (parser, "invalid non-type template argument");
9559 return error_mark_node;
9560 }
9561
9562 /* If the argument wasn't successfully parsed as a type-id followed
9563 by '>>', the argument can only be a constant expression now.
9564 Otherwise, we try parsing the constant-expression tentatively,
9565 because the argument could really be a type-id. */
9566 if (maybe_type_id)
9567 cp_parser_parse_tentatively (parser);
9568 argument = cp_parser_constant_expression (parser,
9569 /*allow_non_constant_p=*/false,
9570 /*non_constant_p=*/NULL);
9571 argument = fold_non_dependent_expr (argument);
9572 if (!maybe_type_id)
9573 return argument;
9574 if (!cp_parser_next_token_ends_template_argument_p (parser))
9575 cp_parser_error (parser, "expected template-argument");
9576 if (cp_parser_parse_definitely (parser))
9577 return argument;
9578 /* We did our best to parse the argument as a non type-id, but that
9579 was the only alternative that matched (albeit with a '>' after
9580 it). We can assume it's just a typo from the user, and a
9581 diagnostic will then be issued. */
9582 return cp_parser_type_id (parser);
9583 }
9584
9585 /* Parse an explicit-instantiation.
9586
9587 explicit-instantiation:
9588 template declaration
9589
9590 Although the standard says `declaration', what it really means is:
9591
9592 explicit-instantiation:
9593 template decl-specifier-seq [opt] declarator [opt] ;
9594
9595 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9596 supposed to be allowed. A defect report has been filed about this
9597 issue.
9598
9599 GNU Extension:
9600
9601 explicit-instantiation:
9602 storage-class-specifier template
9603 decl-specifier-seq [opt] declarator [opt] ;
9604 function-specifier template
9605 decl-specifier-seq [opt] declarator [opt] ; */
9606
9607 static void
9608 cp_parser_explicit_instantiation (cp_parser* parser)
9609 {
9610 int declares_class_or_enum;
9611 cp_decl_specifier_seq decl_specifiers;
9612 tree extension_specifier = NULL_TREE;
9613
9614 /* Look for an (optional) storage-class-specifier or
9615 function-specifier. */
9616 if (cp_parser_allow_gnu_extensions_p (parser))
9617 {
9618 extension_specifier
9619 = cp_parser_storage_class_specifier_opt (parser);
9620 if (!extension_specifier)
9621 extension_specifier
9622 = cp_parser_function_specifier_opt (parser,
9623 /*decl_specs=*/NULL);
9624 }
9625
9626 /* Look for the `template' keyword. */
9627 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9628 /* Let the front end know that we are processing an explicit
9629 instantiation. */
9630 begin_explicit_instantiation ();
9631 /* [temp.explicit] says that we are supposed to ignore access
9632 control while processing explicit instantiation directives. */
9633 push_deferring_access_checks (dk_no_check);
9634 /* Parse a decl-specifier-seq. */
9635 cp_parser_decl_specifier_seq (parser,
9636 CP_PARSER_FLAGS_OPTIONAL,
9637 &decl_specifiers,
9638 &declares_class_or_enum);
9639 /* If there was exactly one decl-specifier, and it declared a class,
9640 and there's no declarator, then we have an explicit type
9641 instantiation. */
9642 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9643 {
9644 tree type;
9645
9646 type = check_tag_decl (&decl_specifiers);
9647 /* Turn access control back on for names used during
9648 template instantiation. */
9649 pop_deferring_access_checks ();
9650 if (type)
9651 do_type_instantiation (type, extension_specifier,
9652 /*complain=*/tf_error);
9653 }
9654 else
9655 {
9656 cp_declarator *declarator;
9657 tree decl;
9658
9659 /* Parse the declarator. */
9660 declarator
9661 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9662 /*ctor_dtor_or_conv_p=*/NULL,
9663 /*parenthesized_p=*/NULL,
9664 /*member_p=*/false);
9665 if (declares_class_or_enum & 2)
9666 cp_parser_check_for_definition_in_return_type (declarator,
9667 decl_specifiers.type);
9668 if (declarator != cp_error_declarator)
9669 {
9670 decl = grokdeclarator (declarator, &decl_specifiers,
9671 NORMAL, 0, &decl_specifiers.attributes);
9672 /* Turn access control back on for names used during
9673 template instantiation. */
9674 pop_deferring_access_checks ();
9675 /* Do the explicit instantiation. */
9676 do_decl_instantiation (decl, extension_specifier);
9677 }
9678 else
9679 {
9680 pop_deferring_access_checks ();
9681 /* Skip the body of the explicit instantiation. */
9682 cp_parser_skip_to_end_of_statement (parser);
9683 }
9684 }
9685 /* We're done with the instantiation. */
9686 end_explicit_instantiation ();
9687
9688 cp_parser_consume_semicolon_at_end_of_statement (parser);
9689 }
9690
9691 /* Parse an explicit-specialization.
9692
9693 explicit-specialization:
9694 template < > declaration
9695
9696 Although the standard says `declaration', what it really means is:
9697
9698 explicit-specialization:
9699 template <> decl-specifier [opt] init-declarator [opt] ;
9700 template <> function-definition
9701 template <> explicit-specialization
9702 template <> template-declaration */
9703
9704 static void
9705 cp_parser_explicit_specialization (cp_parser* parser)
9706 {
9707 bool need_lang_pop;
9708 /* Look for the `template' keyword. */
9709 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9710 /* Look for the `<'. */
9711 cp_parser_require (parser, CPP_LESS, "`<'");
9712 /* Look for the `>'. */
9713 cp_parser_require (parser, CPP_GREATER, "`>'");
9714 /* We have processed another parameter list. */
9715 ++parser->num_template_parameter_lists;
9716 /* [temp]
9717
9718 A template ... explicit specialization ... shall not have C
9719 linkage. */
9720 if (current_lang_name == lang_name_c)
9721 {
9722 error ("template specialization with C linkage");
9723 /* Give it C++ linkage to avoid confusing other parts of the
9724 front end. */
9725 push_lang_context (lang_name_cplusplus);
9726 need_lang_pop = true;
9727 }
9728 else
9729 need_lang_pop = false;
9730 /* Let the front end know that we are beginning a specialization. */
9731 if (!begin_specialization ())
9732 {
9733 end_specialization ();
9734 cp_parser_skip_to_end_of_block_or_statement (parser);
9735 return;
9736 }
9737
9738 /* If the next keyword is `template', we need to figure out whether
9739 or not we're looking a template-declaration. */
9740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9741 {
9742 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9743 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9744 cp_parser_template_declaration_after_export (parser,
9745 /*member_p=*/false);
9746 else
9747 cp_parser_explicit_specialization (parser);
9748 }
9749 else
9750 /* Parse the dependent declaration. */
9751 cp_parser_single_declaration (parser,
9752 /*checks=*/NULL,
9753 /*member_p=*/false,
9754 /*friend_p=*/NULL);
9755 /* We're done with the specialization. */
9756 end_specialization ();
9757 /* For the erroneous case of a template with C linkage, we pushed an
9758 implicit C++ linkage scope; exit that scope now. */
9759 if (need_lang_pop)
9760 pop_lang_context ();
9761 /* We're done with this parameter list. */
9762 --parser->num_template_parameter_lists;
9763 }
9764
9765 /* Parse a type-specifier.
9766
9767 type-specifier:
9768 simple-type-specifier
9769 class-specifier
9770 enum-specifier
9771 elaborated-type-specifier
9772 cv-qualifier
9773
9774 GNU Extension:
9775
9776 type-specifier:
9777 __complex__
9778
9779 Returns a representation of the type-specifier. For a
9780 class-specifier, enum-specifier, or elaborated-type-specifier, a
9781 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9782
9783 The parser flags FLAGS is used to control type-specifier parsing.
9784
9785 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9786 in a decl-specifier-seq.
9787
9788 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9789 class-specifier, enum-specifier, or elaborated-type-specifier, then
9790 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9791 if a type is declared; 2 if it is defined. Otherwise, it is set to
9792 zero.
9793
9794 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9795 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9796 is set to FALSE. */
9797
9798 static tree
9799 cp_parser_type_specifier (cp_parser* parser,
9800 cp_parser_flags flags,
9801 cp_decl_specifier_seq *decl_specs,
9802 bool is_declaration,
9803 int* declares_class_or_enum,
9804 bool* is_cv_qualifier)
9805 {
9806 tree type_spec = NULL_TREE;
9807 cp_token *token;
9808 enum rid keyword;
9809 cp_decl_spec ds = ds_last;
9810
9811 /* Assume this type-specifier does not declare a new type. */
9812 if (declares_class_or_enum)
9813 *declares_class_or_enum = 0;
9814 /* And that it does not specify a cv-qualifier. */
9815 if (is_cv_qualifier)
9816 *is_cv_qualifier = false;
9817 /* Peek at the next token. */
9818 token = cp_lexer_peek_token (parser->lexer);
9819
9820 /* If we're looking at a keyword, we can use that to guide the
9821 production we choose. */
9822 keyword = token->keyword;
9823 switch (keyword)
9824 {
9825 case RID_ENUM:
9826 /* Look for the enum-specifier. */
9827 type_spec = cp_parser_enum_specifier (parser);
9828 /* If that worked, we're done. */
9829 if (type_spec)
9830 {
9831 if (declares_class_or_enum)
9832 *declares_class_or_enum = 2;
9833 if (decl_specs)
9834 cp_parser_set_decl_spec_type (decl_specs,
9835 type_spec,
9836 /*user_defined_p=*/true);
9837 return type_spec;
9838 }
9839 else
9840 goto elaborated_type_specifier;
9841
9842 /* Any of these indicate either a class-specifier, or an
9843 elaborated-type-specifier. */
9844 case RID_CLASS:
9845 case RID_STRUCT:
9846 case RID_UNION:
9847 /* Parse tentatively so that we can back up if we don't find a
9848 class-specifier. */
9849 cp_parser_parse_tentatively (parser);
9850 /* Look for the class-specifier. */
9851 type_spec = cp_parser_class_specifier (parser);
9852 /* If that worked, we're done. */
9853 if (cp_parser_parse_definitely (parser))
9854 {
9855 if (declares_class_or_enum)
9856 *declares_class_or_enum = 2;
9857 if (decl_specs)
9858 cp_parser_set_decl_spec_type (decl_specs,
9859 type_spec,
9860 /*user_defined_p=*/true);
9861 return type_spec;
9862 }
9863
9864 /* Fall through. */
9865 elaborated_type_specifier:
9866 /* We're declaring (not defining) a class or enum. */
9867 if (declares_class_or_enum)
9868 *declares_class_or_enum = 1;
9869
9870 /* Fall through. */
9871 case RID_TYPENAME:
9872 /* Look for an elaborated-type-specifier. */
9873 type_spec
9874 = (cp_parser_elaborated_type_specifier
9875 (parser,
9876 decl_specs && decl_specs->specs[(int) ds_friend],
9877 is_declaration));
9878 if (decl_specs)
9879 cp_parser_set_decl_spec_type (decl_specs,
9880 type_spec,
9881 /*user_defined_p=*/true);
9882 return type_spec;
9883
9884 case RID_CONST:
9885 ds = ds_const;
9886 if (is_cv_qualifier)
9887 *is_cv_qualifier = true;
9888 break;
9889
9890 case RID_VOLATILE:
9891 ds = ds_volatile;
9892 if (is_cv_qualifier)
9893 *is_cv_qualifier = true;
9894 break;
9895
9896 case RID_RESTRICT:
9897 ds = ds_restrict;
9898 if (is_cv_qualifier)
9899 *is_cv_qualifier = true;
9900 break;
9901
9902 case RID_COMPLEX:
9903 /* The `__complex__' keyword is a GNU extension. */
9904 ds = ds_complex;
9905 break;
9906
9907 default:
9908 break;
9909 }
9910
9911 /* Handle simple keywords. */
9912 if (ds != ds_last)
9913 {
9914 if (decl_specs)
9915 {
9916 ++decl_specs->specs[(int)ds];
9917 decl_specs->any_specifiers_p = true;
9918 }
9919 return cp_lexer_consume_token (parser->lexer)->u.value;
9920 }
9921
9922 /* If we do not already have a type-specifier, assume we are looking
9923 at a simple-type-specifier. */
9924 type_spec = cp_parser_simple_type_specifier (parser,
9925 decl_specs,
9926 flags);
9927
9928 /* If we didn't find a type-specifier, and a type-specifier was not
9929 optional in this context, issue an error message. */
9930 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9931 {
9932 cp_parser_error (parser, "expected type specifier");
9933 return error_mark_node;
9934 }
9935
9936 return type_spec;
9937 }
9938
9939 /* Parse a simple-type-specifier.
9940
9941 simple-type-specifier:
9942 :: [opt] nested-name-specifier [opt] type-name
9943 :: [opt] nested-name-specifier template template-id
9944 char
9945 wchar_t
9946 bool
9947 short
9948 int
9949 long
9950 signed
9951 unsigned
9952 float
9953 double
9954 void
9955
9956 GNU Extension:
9957
9958 simple-type-specifier:
9959 __typeof__ unary-expression
9960 __typeof__ ( type-id )
9961
9962 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9963 appropriately updated. */
9964
9965 static tree
9966 cp_parser_simple_type_specifier (cp_parser* parser,
9967 cp_decl_specifier_seq *decl_specs,
9968 cp_parser_flags flags)
9969 {
9970 tree type = NULL_TREE;
9971 cp_token *token;
9972
9973 /* Peek at the next token. */
9974 token = cp_lexer_peek_token (parser->lexer);
9975
9976 /* If we're looking at a keyword, things are easy. */
9977 switch (token->keyword)
9978 {
9979 case RID_CHAR:
9980 if (decl_specs)
9981 decl_specs->explicit_char_p = true;
9982 type = char_type_node;
9983 break;
9984 case RID_WCHAR:
9985 type = wchar_type_node;
9986 break;
9987 case RID_BOOL:
9988 type = boolean_type_node;
9989 break;
9990 case RID_SHORT:
9991 if (decl_specs)
9992 ++decl_specs->specs[(int) ds_short];
9993 type = short_integer_type_node;
9994 break;
9995 case RID_INT:
9996 if (decl_specs)
9997 decl_specs->explicit_int_p = true;
9998 type = integer_type_node;
9999 break;
10000 case RID_LONG:
10001 if (decl_specs)
10002 ++decl_specs->specs[(int) ds_long];
10003 type = long_integer_type_node;
10004 break;
10005 case RID_SIGNED:
10006 if (decl_specs)
10007 ++decl_specs->specs[(int) ds_signed];
10008 type = integer_type_node;
10009 break;
10010 case RID_UNSIGNED:
10011 if (decl_specs)
10012 ++decl_specs->specs[(int) ds_unsigned];
10013 type = unsigned_type_node;
10014 break;
10015 case RID_FLOAT:
10016 type = float_type_node;
10017 break;
10018 case RID_DOUBLE:
10019 type = double_type_node;
10020 break;
10021 case RID_VOID:
10022 type = void_type_node;
10023 break;
10024
10025 case RID_TYPEOF:
10026 /* Consume the `typeof' token. */
10027 cp_lexer_consume_token (parser->lexer);
10028 /* Parse the operand to `typeof'. */
10029 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10030 /* If it is not already a TYPE, take its type. */
10031 if (!TYPE_P (type))
10032 type = finish_typeof (type);
10033
10034 if (decl_specs)
10035 cp_parser_set_decl_spec_type (decl_specs, type,
10036 /*user_defined_p=*/true);
10037
10038 return type;
10039
10040 default:
10041 break;
10042 }
10043
10044 /* If the type-specifier was for a built-in type, we're done. */
10045 if (type)
10046 {
10047 tree id;
10048
10049 /* Record the type. */
10050 if (decl_specs
10051 && (token->keyword != RID_SIGNED
10052 && token->keyword != RID_UNSIGNED
10053 && token->keyword != RID_SHORT
10054 && token->keyword != RID_LONG))
10055 cp_parser_set_decl_spec_type (decl_specs,
10056 type,
10057 /*user_defined=*/false);
10058 if (decl_specs)
10059 decl_specs->any_specifiers_p = true;
10060
10061 /* Consume the token. */
10062 id = cp_lexer_consume_token (parser->lexer)->u.value;
10063
10064 /* There is no valid C++ program where a non-template type is
10065 followed by a "<". That usually indicates that the user thought
10066 that the type was a template. */
10067 cp_parser_check_for_invalid_template_id (parser, type);
10068
10069 return TYPE_NAME (type);
10070 }
10071
10072 /* The type-specifier must be a user-defined type. */
10073 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10074 {
10075 bool qualified_p;
10076 bool global_p;
10077
10078 /* Don't gobble tokens or issue error messages if this is an
10079 optional type-specifier. */
10080 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10081 cp_parser_parse_tentatively (parser);
10082
10083 /* Look for the optional `::' operator. */
10084 global_p
10085 = (cp_parser_global_scope_opt (parser,
10086 /*current_scope_valid_p=*/false)
10087 != NULL_TREE);
10088 /* Look for the nested-name specifier. */
10089 qualified_p
10090 = (cp_parser_nested_name_specifier_opt (parser,
10091 /*typename_keyword_p=*/false,
10092 /*check_dependency_p=*/true,
10093 /*type_p=*/false,
10094 /*is_declaration=*/false)
10095 != NULL_TREE);
10096 /* If we have seen a nested-name-specifier, and the next token
10097 is `template', then we are using the template-id production. */
10098 if (parser->scope
10099 && cp_parser_optional_template_keyword (parser))
10100 {
10101 /* Look for the template-id. */
10102 type = cp_parser_template_id (parser,
10103 /*template_keyword_p=*/true,
10104 /*check_dependency_p=*/true,
10105 /*is_declaration=*/false);
10106 /* If the template-id did not name a type, we are out of
10107 luck. */
10108 if (TREE_CODE (type) != TYPE_DECL)
10109 {
10110 cp_parser_error (parser, "expected template-id for type");
10111 type = NULL_TREE;
10112 }
10113 }
10114 /* Otherwise, look for a type-name. */
10115 else
10116 type = cp_parser_type_name (parser);
10117 /* Keep track of all name-lookups performed in class scopes. */
10118 if (type
10119 && !global_p
10120 && !qualified_p
10121 && TREE_CODE (type) == TYPE_DECL
10122 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10123 maybe_note_name_used_in_class (DECL_NAME (type), type);
10124 /* If it didn't work out, we don't have a TYPE. */
10125 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10126 && !cp_parser_parse_definitely (parser))
10127 type = NULL_TREE;
10128 if (type && decl_specs)
10129 cp_parser_set_decl_spec_type (decl_specs, type,
10130 /*user_defined=*/true);
10131 }
10132
10133 /* If we didn't get a type-name, issue an error message. */
10134 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10135 {
10136 cp_parser_error (parser, "expected type-name");
10137 return error_mark_node;
10138 }
10139
10140 /* There is no valid C++ program where a non-template type is
10141 followed by a "<". That usually indicates that the user thought
10142 that the type was a template. */
10143 if (type && type != error_mark_node)
10144 {
10145 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10146 If it is, then the '<'...'>' enclose protocol names rather than
10147 template arguments, and so everything is fine. */
10148 if (c_dialect_objc ()
10149 && (objc_is_id (type) || objc_is_class_name (type)))
10150 {
10151 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10152 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10153
10154 /* Clobber the "unqualified" type previously entered into
10155 DECL_SPECS with the new, improved protocol-qualified version. */
10156 if (decl_specs)
10157 decl_specs->type = qual_type;
10158
10159 return qual_type;
10160 }
10161
10162 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10163 }
10164
10165 return type;
10166 }
10167
10168 /* Parse a type-name.
10169
10170 type-name:
10171 class-name
10172 enum-name
10173 typedef-name
10174
10175 enum-name:
10176 identifier
10177
10178 typedef-name:
10179 identifier
10180
10181 Returns a TYPE_DECL for the type. */
10182
10183 static tree
10184 cp_parser_type_name (cp_parser* parser)
10185 {
10186 tree type_decl;
10187 tree identifier;
10188
10189 /* We can't know yet whether it is a class-name or not. */
10190 cp_parser_parse_tentatively (parser);
10191 /* Try a class-name. */
10192 type_decl = cp_parser_class_name (parser,
10193 /*typename_keyword_p=*/false,
10194 /*template_keyword_p=*/false,
10195 none_type,
10196 /*check_dependency_p=*/true,
10197 /*class_head_p=*/false,
10198 /*is_declaration=*/false);
10199 /* If it's not a class-name, keep looking. */
10200 if (!cp_parser_parse_definitely (parser))
10201 {
10202 /* It must be a typedef-name or an enum-name. */
10203 identifier = cp_parser_identifier (parser);
10204 if (identifier == error_mark_node)
10205 return error_mark_node;
10206
10207 /* Look up the type-name. */
10208 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10209
10210 if (TREE_CODE (type_decl) != TYPE_DECL
10211 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10212 {
10213 /* See if this is an Objective-C type. */
10214 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10215 tree type = objc_get_protocol_qualified_type (identifier, protos);
10216 if (type)
10217 type_decl = TYPE_NAME (type);
10218 }
10219
10220 /* Issue an error if we did not find a type-name. */
10221 if (TREE_CODE (type_decl) != TYPE_DECL)
10222 {
10223 if (!cp_parser_simulate_error (parser))
10224 cp_parser_name_lookup_error (parser, identifier, type_decl,
10225 "is not a type");
10226 type_decl = error_mark_node;
10227 }
10228 /* Remember that the name was used in the definition of the
10229 current class so that we can check later to see if the
10230 meaning would have been different after the class was
10231 entirely defined. */
10232 else if (type_decl != error_mark_node
10233 && !parser->scope)
10234 maybe_note_name_used_in_class (identifier, type_decl);
10235 }
10236
10237 return type_decl;
10238 }
10239
10240
10241 /* Parse an elaborated-type-specifier. Note that the grammar given
10242 here incorporates the resolution to DR68.
10243
10244 elaborated-type-specifier:
10245 class-key :: [opt] nested-name-specifier [opt] identifier
10246 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10247 enum :: [opt] nested-name-specifier [opt] identifier
10248 typename :: [opt] nested-name-specifier identifier
10249 typename :: [opt] nested-name-specifier template [opt]
10250 template-id
10251
10252 GNU extension:
10253
10254 elaborated-type-specifier:
10255 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10256 class-key attributes :: [opt] nested-name-specifier [opt]
10257 template [opt] template-id
10258 enum attributes :: [opt] nested-name-specifier [opt] identifier
10259
10260 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10261 declared `friend'. If IS_DECLARATION is TRUE, then this
10262 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10263 something is being declared.
10264
10265 Returns the TYPE specified. */
10266
10267 static tree
10268 cp_parser_elaborated_type_specifier (cp_parser* parser,
10269 bool is_friend,
10270 bool is_declaration)
10271 {
10272 enum tag_types tag_type;
10273 tree identifier;
10274 tree type = NULL_TREE;
10275 tree attributes = NULL_TREE;
10276
10277 /* See if we're looking at the `enum' keyword. */
10278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10279 {
10280 /* Consume the `enum' token. */
10281 cp_lexer_consume_token (parser->lexer);
10282 /* Remember that it's an enumeration type. */
10283 tag_type = enum_type;
10284 /* Parse the attributes. */
10285 attributes = cp_parser_attributes_opt (parser);
10286 }
10287 /* Or, it might be `typename'. */
10288 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10289 RID_TYPENAME))
10290 {
10291 /* Consume the `typename' token. */
10292 cp_lexer_consume_token (parser->lexer);
10293 /* Remember that it's a `typename' type. */
10294 tag_type = typename_type;
10295 /* The `typename' keyword is only allowed in templates. */
10296 if (!processing_template_decl)
10297 pedwarn ("using %<typename%> outside of template");
10298 }
10299 /* Otherwise it must be a class-key. */
10300 else
10301 {
10302 tag_type = cp_parser_class_key (parser);
10303 if (tag_type == none_type)
10304 return error_mark_node;
10305 /* Parse the attributes. */
10306 attributes = cp_parser_attributes_opt (parser);
10307 }
10308
10309 /* Look for the `::' operator. */
10310 cp_parser_global_scope_opt (parser,
10311 /*current_scope_valid_p=*/false);
10312 /* Look for the nested-name-specifier. */
10313 if (tag_type == typename_type)
10314 {
10315 if (!cp_parser_nested_name_specifier (parser,
10316 /*typename_keyword_p=*/true,
10317 /*check_dependency_p=*/true,
10318 /*type_p=*/true,
10319 is_declaration))
10320 return error_mark_node;
10321 }
10322 else
10323 /* Even though `typename' is not present, the proposed resolution
10324 to Core Issue 180 says that in `class A<T>::B', `B' should be
10325 considered a type-name, even if `A<T>' is dependent. */
10326 cp_parser_nested_name_specifier_opt (parser,
10327 /*typename_keyword_p=*/true,
10328 /*check_dependency_p=*/true,
10329 /*type_p=*/true,
10330 is_declaration);
10331 /* For everything but enumeration types, consider a template-id.
10332 For an enumeration type, consider only a plain identifier. */
10333 if (tag_type != enum_type)
10334 {
10335 bool template_p = false;
10336 tree decl;
10337
10338 /* Allow the `template' keyword. */
10339 template_p = cp_parser_optional_template_keyword (parser);
10340 /* If we didn't see `template', we don't know if there's a
10341 template-id or not. */
10342 if (!template_p)
10343 cp_parser_parse_tentatively (parser);
10344 /* Parse the template-id. */
10345 decl = cp_parser_template_id (parser, template_p,
10346 /*check_dependency_p=*/true,
10347 is_declaration);
10348 /* If we didn't find a template-id, look for an ordinary
10349 identifier. */
10350 if (!template_p && !cp_parser_parse_definitely (parser))
10351 ;
10352 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10353 in effect, then we must assume that, upon instantiation, the
10354 template will correspond to a class. */
10355 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10356 && tag_type == typename_type)
10357 type = make_typename_type (parser->scope, decl,
10358 typename_type,
10359 /*complain=*/tf_error);
10360 else
10361 type = TREE_TYPE (decl);
10362 }
10363
10364 if (!type)
10365 {
10366 identifier = cp_parser_identifier (parser);
10367
10368 if (identifier == error_mark_node)
10369 {
10370 parser->scope = NULL_TREE;
10371 return error_mark_node;
10372 }
10373
10374 /* For a `typename', we needn't call xref_tag. */
10375 if (tag_type == typename_type
10376 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10377 return cp_parser_make_typename_type (parser, parser->scope,
10378 identifier);
10379 /* Look up a qualified name in the usual way. */
10380 if (parser->scope)
10381 {
10382 tree decl;
10383
10384 decl = cp_parser_lookup_name (parser, identifier,
10385 tag_type,
10386 /*is_template=*/false,
10387 /*is_namespace=*/false,
10388 /*check_dependency=*/true,
10389 /*ambiguous_decls=*/NULL);
10390
10391 /* If we are parsing friend declaration, DECL may be a
10392 TEMPLATE_DECL tree node here. However, we need to check
10393 whether this TEMPLATE_DECL results in valid code. Consider
10394 the following example:
10395
10396 namespace N {
10397 template <class T> class C {};
10398 }
10399 class X {
10400 template <class T> friend class N::C; // #1, valid code
10401 };
10402 template <class T> class Y {
10403 friend class N::C; // #2, invalid code
10404 };
10405
10406 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10407 name lookup of `N::C'. We see that friend declaration must
10408 be template for the code to be valid. Note that
10409 processing_template_decl does not work here since it is
10410 always 1 for the above two cases. */
10411
10412 decl = (cp_parser_maybe_treat_template_as_class
10413 (decl, /*tag_name_p=*/is_friend
10414 && parser->num_template_parameter_lists));
10415
10416 if (TREE_CODE (decl) != TYPE_DECL)
10417 {
10418 cp_parser_diagnose_invalid_type_name (parser,
10419 parser->scope,
10420 identifier);
10421 return error_mark_node;
10422 }
10423
10424 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10425 {
10426 bool allow_template = (parser->num_template_parameter_lists
10427 || DECL_SELF_REFERENCE_P (decl));
10428 type = check_elaborated_type_specifier (tag_type, decl,
10429 allow_template);
10430
10431 if (type == error_mark_node)
10432 return error_mark_node;
10433 }
10434
10435 type = TREE_TYPE (decl);
10436 }
10437 else
10438 {
10439 /* An elaborated-type-specifier sometimes introduces a new type and
10440 sometimes names an existing type. Normally, the rule is that it
10441 introduces a new type only if there is not an existing type of
10442 the same name already in scope. For example, given:
10443
10444 struct S {};
10445 void f() { struct S s; }
10446
10447 the `struct S' in the body of `f' is the same `struct S' as in
10448 the global scope; the existing definition is used. However, if
10449 there were no global declaration, this would introduce a new
10450 local class named `S'.
10451
10452 An exception to this rule applies to the following code:
10453
10454 namespace N { struct S; }
10455
10456 Here, the elaborated-type-specifier names a new type
10457 unconditionally; even if there is already an `S' in the
10458 containing scope this declaration names a new type.
10459 This exception only applies if the elaborated-type-specifier
10460 forms the complete declaration:
10461
10462 [class.name]
10463
10464 A declaration consisting solely of `class-key identifier ;' is
10465 either a redeclaration of the name in the current scope or a
10466 forward declaration of the identifier as a class name. It
10467 introduces the name into the current scope.
10468
10469 We are in this situation precisely when the next token is a `;'.
10470
10471 An exception to the exception is that a `friend' declaration does
10472 *not* name a new type; i.e., given:
10473
10474 struct S { friend struct T; };
10475
10476 `T' is not a new type in the scope of `S'.
10477
10478 Also, `new struct S' or `sizeof (struct S)' never results in the
10479 definition of a new type; a new type can only be declared in a
10480 declaration context. */
10481
10482 tag_scope ts;
10483 bool template_p;
10484
10485 if (is_friend)
10486 /* Friends have special name lookup rules. */
10487 ts = ts_within_enclosing_non_class;
10488 else if (is_declaration
10489 && cp_lexer_next_token_is (parser->lexer,
10490 CPP_SEMICOLON))
10491 /* This is a `class-key identifier ;' */
10492 ts = ts_current;
10493 else
10494 ts = ts_global;
10495
10496 template_p =
10497 (parser->num_template_parameter_lists
10498 && (cp_parser_next_token_starts_class_definition_p (parser)
10499 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10500 /* An unqualified name was used to reference this type, so
10501 there were no qualifying templates. */
10502 if (!cp_parser_check_template_parameters (parser,
10503 /*num_templates=*/0))
10504 return error_mark_node;
10505 type = xref_tag (tag_type, identifier, ts, template_p);
10506 }
10507 }
10508
10509 if (type == error_mark_node)
10510 return error_mark_node;
10511
10512 /* Allow attributes on forward declarations of classes. */
10513 if (attributes)
10514 {
10515 if (TREE_CODE (type) == TYPENAME_TYPE)
10516 warning (OPT_Wattributes,
10517 "attributes ignored on uninstantiated type");
10518 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10519 && ! processing_explicit_instantiation)
10520 warning (OPT_Wattributes,
10521 "attributes ignored on template instantiation");
10522 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10523 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10524 else
10525 warning (OPT_Wattributes,
10526 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10527 }
10528
10529 if (tag_type != enum_type)
10530 cp_parser_check_class_key (tag_type, type);
10531
10532 /* A "<" cannot follow an elaborated type specifier. If that
10533 happens, the user was probably trying to form a template-id. */
10534 cp_parser_check_for_invalid_template_id (parser, type);
10535
10536 return type;
10537 }
10538
10539 /* Parse an enum-specifier.
10540
10541 enum-specifier:
10542 enum identifier [opt] { enumerator-list [opt] }
10543
10544 GNU Extensions:
10545 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10546 attributes[opt]
10547
10548 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10549 if the token stream isn't an enum-specifier after all. */
10550
10551 static tree
10552 cp_parser_enum_specifier (cp_parser* parser)
10553 {
10554 tree identifier;
10555 tree type;
10556 tree attributes;
10557
10558 /* Parse tentatively so that we can back up if we don't find a
10559 enum-specifier. */
10560 cp_parser_parse_tentatively (parser);
10561
10562 /* Caller guarantees that the current token is 'enum', an identifier
10563 possibly follows, and the token after that is an opening brace.
10564 If we don't have an identifier, fabricate an anonymous name for
10565 the enumeration being defined. */
10566 cp_lexer_consume_token (parser->lexer);
10567
10568 attributes = cp_parser_attributes_opt (parser);
10569
10570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10571 identifier = cp_parser_identifier (parser);
10572 else
10573 identifier = make_anon_name ();
10574
10575 /* Look for the `{' but don't consume it yet. */
10576 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10577 cp_parser_simulate_error (parser);
10578
10579 if (!cp_parser_parse_definitely (parser))
10580 return NULL_TREE;
10581
10582 /* Issue an error message if type-definitions are forbidden here. */
10583 if (!cp_parser_check_type_definition (parser))
10584 type = error_mark_node;
10585 else
10586 /* Create the new type. We do this before consuming the opening
10587 brace so the enum will be recorded as being on the line of its
10588 tag (or the 'enum' keyword, if there is no tag). */
10589 type = start_enum (identifier);
10590
10591 /* Consume the opening brace. */
10592 cp_lexer_consume_token (parser->lexer);
10593
10594 if (type == error_mark_node)
10595 {
10596 cp_parser_skip_to_end_of_block_or_statement (parser);
10597 return error_mark_node;
10598 }
10599
10600 /* If the next token is not '}', then there are some enumerators. */
10601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10602 cp_parser_enumerator_list (parser, type);
10603
10604 /* Consume the final '}'. */
10605 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10606
10607 /* Look for trailing attributes to apply to this enumeration, and
10608 apply them if appropriate. */
10609 if (cp_parser_allow_gnu_extensions_p (parser))
10610 {
10611 tree trailing_attr = cp_parser_attributes_opt (parser);
10612 cplus_decl_attributes (&type,
10613 trailing_attr,
10614 (int) ATTR_FLAG_TYPE_IN_PLACE);
10615 }
10616
10617 /* Finish up the enumeration. */
10618 finish_enum (type);
10619
10620 return type;
10621 }
10622
10623 /* Parse an enumerator-list. The enumerators all have the indicated
10624 TYPE.
10625
10626 enumerator-list:
10627 enumerator-definition
10628 enumerator-list , enumerator-definition */
10629
10630 static void
10631 cp_parser_enumerator_list (cp_parser* parser, tree type)
10632 {
10633 while (true)
10634 {
10635 /* Parse an enumerator-definition. */
10636 cp_parser_enumerator_definition (parser, type);
10637
10638 /* If the next token is not a ',', we've reached the end of
10639 the list. */
10640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10641 break;
10642 /* Otherwise, consume the `,' and keep going. */
10643 cp_lexer_consume_token (parser->lexer);
10644 /* If the next token is a `}', there is a trailing comma. */
10645 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10646 {
10647 if (pedantic && !in_system_header)
10648 pedwarn ("comma at end of enumerator list");
10649 break;
10650 }
10651 }
10652 }
10653
10654 /* Parse an enumerator-definition. The enumerator has the indicated
10655 TYPE.
10656
10657 enumerator-definition:
10658 enumerator
10659 enumerator = constant-expression
10660
10661 enumerator:
10662 identifier */
10663
10664 static void
10665 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10666 {
10667 tree identifier;
10668 tree value;
10669
10670 /* Look for the identifier. */
10671 identifier = cp_parser_identifier (parser);
10672 if (identifier == error_mark_node)
10673 return;
10674
10675 /* If the next token is an '=', then there is an explicit value. */
10676 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10677 {
10678 /* Consume the `=' token. */
10679 cp_lexer_consume_token (parser->lexer);
10680 /* Parse the value. */
10681 value = cp_parser_constant_expression (parser,
10682 /*allow_non_constant_p=*/false,
10683 NULL);
10684 }
10685 else
10686 value = NULL_TREE;
10687
10688 /* Create the enumerator. */
10689 build_enumerator (identifier, value, type);
10690 }
10691
10692 /* Parse a namespace-name.
10693
10694 namespace-name:
10695 original-namespace-name
10696 namespace-alias
10697
10698 Returns the NAMESPACE_DECL for the namespace. */
10699
10700 static tree
10701 cp_parser_namespace_name (cp_parser* parser)
10702 {
10703 tree identifier;
10704 tree namespace_decl;
10705
10706 /* Get the name of the namespace. */
10707 identifier = cp_parser_identifier (parser);
10708 if (identifier == error_mark_node)
10709 return error_mark_node;
10710
10711 /* Look up the identifier in the currently active scope. Look only
10712 for namespaces, due to:
10713
10714 [basic.lookup.udir]
10715
10716 When looking up a namespace-name in a using-directive or alias
10717 definition, only namespace names are considered.
10718
10719 And:
10720
10721 [basic.lookup.qual]
10722
10723 During the lookup of a name preceding the :: scope resolution
10724 operator, object, function, and enumerator names are ignored.
10725
10726 (Note that cp_parser_class_or_namespace_name only calls this
10727 function if the token after the name is the scope resolution
10728 operator.) */
10729 namespace_decl = cp_parser_lookup_name (parser, identifier,
10730 none_type,
10731 /*is_template=*/false,
10732 /*is_namespace=*/true,
10733 /*check_dependency=*/true,
10734 /*ambiguous_decls=*/NULL);
10735 /* If it's not a namespace, issue an error. */
10736 if (namespace_decl == error_mark_node
10737 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10738 {
10739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10740 error ("%qD is not a namespace-name", identifier);
10741 cp_parser_error (parser, "expected namespace-name");
10742 namespace_decl = error_mark_node;
10743 }
10744
10745 return namespace_decl;
10746 }
10747
10748 /* Parse a namespace-definition.
10749
10750 namespace-definition:
10751 named-namespace-definition
10752 unnamed-namespace-definition
10753
10754 named-namespace-definition:
10755 original-namespace-definition
10756 extension-namespace-definition
10757
10758 original-namespace-definition:
10759 namespace identifier { namespace-body }
10760
10761 extension-namespace-definition:
10762 namespace original-namespace-name { namespace-body }
10763
10764 unnamed-namespace-definition:
10765 namespace { namespace-body } */
10766
10767 static void
10768 cp_parser_namespace_definition (cp_parser* parser)
10769 {
10770 tree identifier, attribs;
10771
10772 /* Look for the `namespace' keyword. */
10773 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10774
10775 /* Get the name of the namespace. We do not attempt to distinguish
10776 between an original-namespace-definition and an
10777 extension-namespace-definition at this point. The semantic
10778 analysis routines are responsible for that. */
10779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10780 identifier = cp_parser_identifier (parser);
10781 else
10782 identifier = NULL_TREE;
10783
10784 /* Parse any specified attributes. */
10785 attribs = cp_parser_attributes_opt (parser);
10786
10787 /* Look for the `{' to start the namespace. */
10788 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10789 /* Start the namespace. */
10790 push_namespace_with_attribs (identifier, attribs);
10791 /* Parse the body of the namespace. */
10792 cp_parser_namespace_body (parser);
10793 /* Finish the namespace. */
10794 pop_namespace ();
10795 /* Look for the final `}'. */
10796 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10797 }
10798
10799 /* Parse a namespace-body.
10800
10801 namespace-body:
10802 declaration-seq [opt] */
10803
10804 static void
10805 cp_parser_namespace_body (cp_parser* parser)
10806 {
10807 cp_parser_declaration_seq_opt (parser);
10808 }
10809
10810 /* Parse a namespace-alias-definition.
10811
10812 namespace-alias-definition:
10813 namespace identifier = qualified-namespace-specifier ; */
10814
10815 static void
10816 cp_parser_namespace_alias_definition (cp_parser* parser)
10817 {
10818 tree identifier;
10819 tree namespace_specifier;
10820
10821 /* Look for the `namespace' keyword. */
10822 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10823 /* Look for the identifier. */
10824 identifier = cp_parser_identifier (parser);
10825 if (identifier == error_mark_node)
10826 return;
10827 /* Look for the `=' token. */
10828 cp_parser_require (parser, CPP_EQ, "`='");
10829 /* Look for the qualified-namespace-specifier. */
10830 namespace_specifier
10831 = cp_parser_qualified_namespace_specifier (parser);
10832 /* Look for the `;' token. */
10833 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10834
10835 /* Register the alias in the symbol table. */
10836 do_namespace_alias (identifier, namespace_specifier);
10837 }
10838
10839 /* Parse a qualified-namespace-specifier.
10840
10841 qualified-namespace-specifier:
10842 :: [opt] nested-name-specifier [opt] namespace-name
10843
10844 Returns a NAMESPACE_DECL corresponding to the specified
10845 namespace. */
10846
10847 static tree
10848 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10849 {
10850 /* Look for the optional `::'. */
10851 cp_parser_global_scope_opt (parser,
10852 /*current_scope_valid_p=*/false);
10853
10854 /* Look for the optional nested-name-specifier. */
10855 cp_parser_nested_name_specifier_opt (parser,
10856 /*typename_keyword_p=*/false,
10857 /*check_dependency_p=*/true,
10858 /*type_p=*/false,
10859 /*is_declaration=*/true);
10860
10861 return cp_parser_namespace_name (parser);
10862 }
10863
10864 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10865 access declaration.
10866
10867 using-declaration:
10868 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10869 using :: unqualified-id ;
10870
10871 access-declaration:
10872 qualified-id ;
10873
10874 */
10875
10876 static bool
10877 cp_parser_using_declaration (cp_parser* parser,
10878 bool access_declaration_p)
10879 {
10880 cp_token *token;
10881 bool typename_p = false;
10882 bool global_scope_p;
10883 tree decl;
10884 tree identifier;
10885 tree qscope;
10886
10887 if (access_declaration_p)
10888 cp_parser_parse_tentatively (parser);
10889 else
10890 {
10891 /* Look for the `using' keyword. */
10892 cp_parser_require_keyword (parser, RID_USING, "`using'");
10893
10894 /* Peek at the next token. */
10895 token = cp_lexer_peek_token (parser->lexer);
10896 /* See if it's `typename'. */
10897 if (token->keyword == RID_TYPENAME)
10898 {
10899 /* Remember that we've seen it. */
10900 typename_p = true;
10901 /* Consume the `typename' token. */
10902 cp_lexer_consume_token (parser->lexer);
10903 }
10904 }
10905
10906 /* Look for the optional global scope qualification. */
10907 global_scope_p
10908 = (cp_parser_global_scope_opt (parser,
10909 /*current_scope_valid_p=*/false)
10910 != NULL_TREE);
10911
10912 /* If we saw `typename', or didn't see `::', then there must be a
10913 nested-name-specifier present. */
10914 if (typename_p || !global_scope_p)
10915 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10916 /*check_dependency_p=*/true,
10917 /*type_p=*/false,
10918 /*is_declaration=*/true);
10919 /* Otherwise, we could be in either of the two productions. In that
10920 case, treat the nested-name-specifier as optional. */
10921 else
10922 qscope = cp_parser_nested_name_specifier_opt (parser,
10923 /*typename_keyword_p=*/false,
10924 /*check_dependency_p=*/true,
10925 /*type_p=*/false,
10926 /*is_declaration=*/true);
10927 if (!qscope)
10928 qscope = global_namespace;
10929
10930 if (access_declaration_p && cp_parser_error_occurred (parser))
10931 /* Something has already gone wrong; there's no need to parse
10932 further. Since an error has occurred, the return value of
10933 cp_parser_parse_definitely will be false, as required. */
10934 return cp_parser_parse_definitely (parser);
10935
10936 /* Parse the unqualified-id. */
10937 identifier = cp_parser_unqualified_id (parser,
10938 /*template_keyword_p=*/false,
10939 /*check_dependency_p=*/true,
10940 /*declarator_p=*/true,
10941 /*optional_p=*/false);
10942
10943 if (access_declaration_p)
10944 {
10945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10946 cp_parser_simulate_error (parser);
10947 if (!cp_parser_parse_definitely (parser))
10948 return false;
10949 }
10950
10951 /* The function we call to handle a using-declaration is different
10952 depending on what scope we are in. */
10953 if (qscope == error_mark_node || identifier == error_mark_node)
10954 ;
10955 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10956 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10957 /* [namespace.udecl]
10958
10959 A using declaration shall not name a template-id. */
10960 error ("a template-id may not appear in a using-declaration");
10961 else
10962 {
10963 if (at_class_scope_p ())
10964 {
10965 /* Create the USING_DECL. */
10966 decl = do_class_using_decl (parser->scope, identifier);
10967 /* Add it to the list of members in this class. */
10968 finish_member_declaration (decl);
10969 }
10970 else
10971 {
10972 decl = cp_parser_lookup_name_simple (parser, identifier);
10973 if (decl == error_mark_node)
10974 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10975 else if (!at_namespace_scope_p ())
10976 do_local_using_decl (decl, qscope, identifier);
10977 else
10978 do_toplevel_using_decl (decl, qscope, identifier);
10979 }
10980 }
10981
10982 /* Look for the final `;'. */
10983 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10984
10985 return true;
10986 }
10987
10988 /* Parse a using-directive.
10989
10990 using-directive:
10991 using namespace :: [opt] nested-name-specifier [opt]
10992 namespace-name ; */
10993
10994 static void
10995 cp_parser_using_directive (cp_parser* parser)
10996 {
10997 tree namespace_decl;
10998 tree attribs;
10999
11000 /* Look for the `using' keyword. */
11001 cp_parser_require_keyword (parser, RID_USING, "`using'");
11002 /* And the `namespace' keyword. */
11003 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11004 /* Look for the optional `::' operator. */
11005 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11006 /* And the optional nested-name-specifier. */
11007 cp_parser_nested_name_specifier_opt (parser,
11008 /*typename_keyword_p=*/false,
11009 /*check_dependency_p=*/true,
11010 /*type_p=*/false,
11011 /*is_declaration=*/true);
11012 /* Get the namespace being used. */
11013 namespace_decl = cp_parser_namespace_name (parser);
11014 /* And any specified attributes. */
11015 attribs = cp_parser_attributes_opt (parser);
11016 /* Update the symbol table. */
11017 parse_using_directive (namespace_decl, attribs);
11018 /* Look for the final `;'. */
11019 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11020 }
11021
11022 /* Parse an asm-definition.
11023
11024 asm-definition:
11025 asm ( string-literal ) ;
11026
11027 GNU Extension:
11028
11029 asm-definition:
11030 asm volatile [opt] ( string-literal ) ;
11031 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11032 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11033 : asm-operand-list [opt] ) ;
11034 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11035 : asm-operand-list [opt]
11036 : asm-operand-list [opt] ) ; */
11037
11038 static void
11039 cp_parser_asm_definition (cp_parser* parser)
11040 {
11041 tree string;
11042 tree outputs = NULL_TREE;
11043 tree inputs = NULL_TREE;
11044 tree clobbers = NULL_TREE;
11045 tree asm_stmt;
11046 bool volatile_p = false;
11047 bool extended_p = false;
11048
11049 /* Look for the `asm' keyword. */
11050 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11051 /* See if the next token is `volatile'. */
11052 if (cp_parser_allow_gnu_extensions_p (parser)
11053 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11054 {
11055 /* Remember that we saw the `volatile' keyword. */
11056 volatile_p = true;
11057 /* Consume the token. */
11058 cp_lexer_consume_token (parser->lexer);
11059 }
11060 /* Look for the opening `('. */
11061 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11062 return;
11063 /* Look for the string. */
11064 string = cp_parser_string_literal (parser, false, false);
11065 if (string == error_mark_node)
11066 {
11067 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11068 /*consume_paren=*/true);
11069 return;
11070 }
11071
11072 /* If we're allowing GNU extensions, check for the extended assembly
11073 syntax. Unfortunately, the `:' tokens need not be separated by
11074 a space in C, and so, for compatibility, we tolerate that here
11075 too. Doing that means that we have to treat the `::' operator as
11076 two `:' tokens. */
11077 if (cp_parser_allow_gnu_extensions_p (parser)
11078 && parser->in_function_body
11079 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11080 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11081 {
11082 bool inputs_p = false;
11083 bool clobbers_p = false;
11084
11085 /* The extended syntax was used. */
11086 extended_p = true;
11087
11088 /* Look for outputs. */
11089 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11090 {
11091 /* Consume the `:'. */
11092 cp_lexer_consume_token (parser->lexer);
11093 /* Parse the output-operands. */
11094 if (cp_lexer_next_token_is_not (parser->lexer,
11095 CPP_COLON)
11096 && cp_lexer_next_token_is_not (parser->lexer,
11097 CPP_SCOPE)
11098 && cp_lexer_next_token_is_not (parser->lexer,
11099 CPP_CLOSE_PAREN))
11100 outputs = cp_parser_asm_operand_list (parser);
11101 }
11102 /* If the next token is `::', there are no outputs, and the
11103 next token is the beginning of the inputs. */
11104 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11105 /* The inputs are coming next. */
11106 inputs_p = true;
11107
11108 /* Look for inputs. */
11109 if (inputs_p
11110 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11111 {
11112 /* Consume the `:' or `::'. */
11113 cp_lexer_consume_token (parser->lexer);
11114 /* Parse the output-operands. */
11115 if (cp_lexer_next_token_is_not (parser->lexer,
11116 CPP_COLON)
11117 && cp_lexer_next_token_is_not (parser->lexer,
11118 CPP_CLOSE_PAREN))
11119 inputs = cp_parser_asm_operand_list (parser);
11120 }
11121 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11122 /* The clobbers are coming next. */
11123 clobbers_p = true;
11124
11125 /* Look for clobbers. */
11126 if (clobbers_p
11127 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11128 {
11129 /* Consume the `:' or `::'. */
11130 cp_lexer_consume_token (parser->lexer);
11131 /* Parse the clobbers. */
11132 if (cp_lexer_next_token_is_not (parser->lexer,
11133 CPP_CLOSE_PAREN))
11134 clobbers = cp_parser_asm_clobber_list (parser);
11135 }
11136 }
11137 /* Look for the closing `)'. */
11138 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11139 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11140 /*consume_paren=*/true);
11141 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11142
11143 /* Create the ASM_EXPR. */
11144 if (parser->in_function_body)
11145 {
11146 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11147 inputs, clobbers);
11148 /* If the extended syntax was not used, mark the ASM_EXPR. */
11149 if (!extended_p)
11150 {
11151 tree temp = asm_stmt;
11152 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11153 temp = TREE_OPERAND (temp, 0);
11154
11155 ASM_INPUT_P (temp) = 1;
11156 }
11157 }
11158 else
11159 cgraph_add_asm_node (string);
11160 }
11161
11162 /* Declarators [gram.dcl.decl] */
11163
11164 /* Parse an init-declarator.
11165
11166 init-declarator:
11167 declarator initializer [opt]
11168
11169 GNU Extension:
11170
11171 init-declarator:
11172 declarator asm-specification [opt] attributes [opt] initializer [opt]
11173
11174 function-definition:
11175 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11176 function-body
11177 decl-specifier-seq [opt] declarator function-try-block
11178
11179 GNU Extension:
11180
11181 function-definition:
11182 __extension__ function-definition
11183
11184 The DECL_SPECIFIERS apply to this declarator. Returns a
11185 representation of the entity declared. If MEMBER_P is TRUE, then
11186 this declarator appears in a class scope. The new DECL created by
11187 this declarator is returned.
11188
11189 The CHECKS are access checks that should be performed once we know
11190 what entity is being declared (and, therefore, what classes have
11191 befriended it).
11192
11193 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11194 for a function-definition here as well. If the declarator is a
11195 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11196 be TRUE upon return. By that point, the function-definition will
11197 have been completely parsed.
11198
11199 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11200 is FALSE. */
11201
11202 static tree
11203 cp_parser_init_declarator (cp_parser* parser,
11204 cp_decl_specifier_seq *decl_specifiers,
11205 VEC (deferred_access_check,gc)* checks,
11206 bool function_definition_allowed_p,
11207 bool member_p,
11208 int declares_class_or_enum,
11209 bool* function_definition_p)
11210 {
11211 cp_token *token;
11212 cp_declarator *declarator;
11213 tree prefix_attributes;
11214 tree attributes;
11215 tree asm_specification;
11216 tree initializer;
11217 tree decl = NULL_TREE;
11218 tree scope;
11219 bool is_initialized;
11220 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11221 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11222 "(...)". */
11223 enum cpp_ttype initialization_kind;
11224 bool is_parenthesized_init = false;
11225 bool is_non_constant_init;
11226 int ctor_dtor_or_conv_p;
11227 bool friend_p;
11228 tree pushed_scope = NULL;
11229
11230 /* Gather the attributes that were provided with the
11231 decl-specifiers. */
11232 prefix_attributes = decl_specifiers->attributes;
11233
11234 /* Assume that this is not the declarator for a function
11235 definition. */
11236 if (function_definition_p)
11237 *function_definition_p = false;
11238
11239 /* Defer access checks while parsing the declarator; we cannot know
11240 what names are accessible until we know what is being
11241 declared. */
11242 resume_deferring_access_checks ();
11243
11244 /* Parse the declarator. */
11245 declarator
11246 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11247 &ctor_dtor_or_conv_p,
11248 /*parenthesized_p=*/NULL,
11249 /*member_p=*/false);
11250 /* Gather up the deferred checks. */
11251 stop_deferring_access_checks ();
11252
11253 /* If the DECLARATOR was erroneous, there's no need to go
11254 further. */
11255 if (declarator == cp_error_declarator)
11256 return error_mark_node;
11257
11258 /* Check that the number of template-parameter-lists is OK. */
11259 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11260 return error_mark_node;
11261
11262 if (declares_class_or_enum & 2)
11263 cp_parser_check_for_definition_in_return_type (declarator,
11264 decl_specifiers->type);
11265
11266 /* Figure out what scope the entity declared by the DECLARATOR is
11267 located in. `grokdeclarator' sometimes changes the scope, so
11268 we compute it now. */
11269 scope = get_scope_of_declarator (declarator);
11270
11271 /* If we're allowing GNU extensions, look for an asm-specification
11272 and attributes. */
11273 if (cp_parser_allow_gnu_extensions_p (parser))
11274 {
11275 /* Look for an asm-specification. */
11276 asm_specification = cp_parser_asm_specification_opt (parser);
11277 /* And attributes. */
11278 attributes = cp_parser_attributes_opt (parser);
11279 }
11280 else
11281 {
11282 asm_specification = NULL_TREE;
11283 attributes = NULL_TREE;
11284 }
11285
11286 /* Peek at the next token. */
11287 token = cp_lexer_peek_token (parser->lexer);
11288 /* Check to see if the token indicates the start of a
11289 function-definition. */
11290 if (cp_parser_token_starts_function_definition_p (token))
11291 {
11292 if (!function_definition_allowed_p)
11293 {
11294 /* If a function-definition should not appear here, issue an
11295 error message. */
11296 cp_parser_error (parser,
11297 "a function-definition is not allowed here");
11298 return error_mark_node;
11299 }
11300 else
11301 {
11302 /* Neither attributes nor an asm-specification are allowed
11303 on a function-definition. */
11304 if (asm_specification)
11305 error ("an asm-specification is not allowed on a function-definition");
11306 if (attributes)
11307 error ("attributes are not allowed on a function-definition");
11308 /* This is a function-definition. */
11309 *function_definition_p = true;
11310
11311 /* Parse the function definition. */
11312 if (member_p)
11313 decl = cp_parser_save_member_function_body (parser,
11314 decl_specifiers,
11315 declarator,
11316 prefix_attributes);
11317 else
11318 decl
11319 = (cp_parser_function_definition_from_specifiers_and_declarator
11320 (parser, decl_specifiers, prefix_attributes, declarator));
11321
11322 return decl;
11323 }
11324 }
11325
11326 /* [dcl.dcl]
11327
11328 Only in function declarations for constructors, destructors, and
11329 type conversions can the decl-specifier-seq be omitted.
11330
11331 We explicitly postpone this check past the point where we handle
11332 function-definitions because we tolerate function-definitions
11333 that are missing their return types in some modes. */
11334 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11335 {
11336 cp_parser_error (parser,
11337 "expected constructor, destructor, or type conversion");
11338 return error_mark_node;
11339 }
11340
11341 /* An `=' or an `(' indicates an initializer. */
11342 if (token->type == CPP_EQ
11343 || token->type == CPP_OPEN_PAREN)
11344 {
11345 is_initialized = true;
11346 initialization_kind = token->type;
11347 }
11348 else
11349 {
11350 /* If the init-declarator isn't initialized and isn't followed by a
11351 `,' or `;', it's not a valid init-declarator. */
11352 if (token->type != CPP_COMMA
11353 && token->type != CPP_SEMICOLON)
11354 {
11355 cp_parser_error (parser, "expected initializer");
11356 return error_mark_node;
11357 }
11358 is_initialized = false;
11359 initialization_kind = CPP_EOF;
11360 }
11361
11362 /* Because start_decl has side-effects, we should only call it if we
11363 know we're going ahead. By this point, we know that we cannot
11364 possibly be looking at any other construct. */
11365 cp_parser_commit_to_tentative_parse (parser);
11366
11367 /* If the decl specifiers were bad, issue an error now that we're
11368 sure this was intended to be a declarator. Then continue
11369 declaring the variable(s), as int, to try to cut down on further
11370 errors. */
11371 if (decl_specifiers->any_specifiers_p
11372 && decl_specifiers->type == error_mark_node)
11373 {
11374 cp_parser_error (parser, "invalid type in declaration");
11375 decl_specifiers->type = integer_type_node;
11376 }
11377
11378 /* Check to see whether or not this declaration is a friend. */
11379 friend_p = cp_parser_friend_p (decl_specifiers);
11380
11381 /* Enter the newly declared entry in the symbol table. If we're
11382 processing a declaration in a class-specifier, we wait until
11383 after processing the initializer. */
11384 if (!member_p)
11385 {
11386 if (parser->in_unbraced_linkage_specification_p)
11387 decl_specifiers->storage_class = sc_extern;
11388 decl = start_decl (declarator, decl_specifiers,
11389 is_initialized, attributes, prefix_attributes,
11390 &pushed_scope);
11391 }
11392 else if (scope)
11393 /* Enter the SCOPE. That way unqualified names appearing in the
11394 initializer will be looked up in SCOPE. */
11395 pushed_scope = push_scope (scope);
11396
11397 /* Perform deferred access control checks, now that we know in which
11398 SCOPE the declared entity resides. */
11399 if (!member_p && decl)
11400 {
11401 tree saved_current_function_decl = NULL_TREE;
11402
11403 /* If the entity being declared is a function, pretend that we
11404 are in its scope. If it is a `friend', it may have access to
11405 things that would not otherwise be accessible. */
11406 if (TREE_CODE (decl) == FUNCTION_DECL)
11407 {
11408 saved_current_function_decl = current_function_decl;
11409 current_function_decl = decl;
11410 }
11411
11412 /* Perform access checks for template parameters. */
11413 cp_parser_perform_template_parameter_access_checks (checks);
11414
11415 /* Perform the access control checks for the declarator and the
11416 the decl-specifiers. */
11417 perform_deferred_access_checks ();
11418
11419 /* Restore the saved value. */
11420 if (TREE_CODE (decl) == FUNCTION_DECL)
11421 current_function_decl = saved_current_function_decl;
11422 }
11423
11424 /* Parse the initializer. */
11425 initializer = NULL_TREE;
11426 is_parenthesized_init = false;
11427 is_non_constant_init = true;
11428 if (is_initialized)
11429 {
11430 if (function_declarator_p (declarator))
11431 {
11432 if (initialization_kind == CPP_EQ)
11433 initializer = cp_parser_pure_specifier (parser);
11434 else
11435 {
11436 /* If the declaration was erroneous, we don't really
11437 know what the user intended, so just silently
11438 consume the initializer. */
11439 if (decl != error_mark_node)
11440 error ("initializer provided for function");
11441 cp_parser_skip_to_closing_parenthesis (parser,
11442 /*recovering=*/true,
11443 /*or_comma=*/false,
11444 /*consume_paren=*/true);
11445 }
11446 }
11447 else
11448 initializer = cp_parser_initializer (parser,
11449 &is_parenthesized_init,
11450 &is_non_constant_init);
11451 }
11452
11453 /* The old parser allows attributes to appear after a parenthesized
11454 initializer. Mark Mitchell proposed removing this functionality
11455 on the GCC mailing lists on 2002-08-13. This parser accepts the
11456 attributes -- but ignores them. */
11457 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11458 if (cp_parser_attributes_opt (parser))
11459 warning (OPT_Wattributes,
11460 "attributes after parenthesized initializer ignored");
11461
11462 /* For an in-class declaration, use `grokfield' to create the
11463 declaration. */
11464 if (member_p)
11465 {
11466 if (pushed_scope)
11467 {
11468 pop_scope (pushed_scope);
11469 pushed_scope = false;
11470 }
11471 decl = grokfield (declarator, decl_specifiers,
11472 initializer, !is_non_constant_init,
11473 /*asmspec=*/NULL_TREE,
11474 prefix_attributes);
11475 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11476 cp_parser_save_default_args (parser, decl);
11477 }
11478
11479 /* Finish processing the declaration. But, skip friend
11480 declarations. */
11481 if (!friend_p && decl && decl != error_mark_node)
11482 {
11483 cp_finish_decl (decl,
11484 initializer, !is_non_constant_init,
11485 asm_specification,
11486 /* If the initializer is in parentheses, then this is
11487 a direct-initialization, which means that an
11488 `explicit' constructor is OK. Otherwise, an
11489 `explicit' constructor cannot be used. */
11490 ((is_parenthesized_init || !is_initialized)
11491 ? 0 : LOOKUP_ONLYCONVERTING));
11492 }
11493 if (!friend_p && pushed_scope)
11494 pop_scope (pushed_scope);
11495
11496 return decl;
11497 }
11498
11499 /* Parse a declarator.
11500
11501 declarator:
11502 direct-declarator
11503 ptr-operator declarator
11504
11505 abstract-declarator:
11506 ptr-operator abstract-declarator [opt]
11507 direct-abstract-declarator
11508
11509 GNU Extensions:
11510
11511 declarator:
11512 attributes [opt] direct-declarator
11513 attributes [opt] ptr-operator declarator
11514
11515 abstract-declarator:
11516 attributes [opt] ptr-operator abstract-declarator [opt]
11517 attributes [opt] direct-abstract-declarator
11518
11519 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11520 detect constructor, destructor or conversion operators. It is set
11521 to -1 if the declarator is a name, and +1 if it is a
11522 function. Otherwise it is set to zero. Usually you just want to
11523 test for >0, but internally the negative value is used.
11524
11525 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11526 a decl-specifier-seq unless it declares a constructor, destructor,
11527 or conversion. It might seem that we could check this condition in
11528 semantic analysis, rather than parsing, but that makes it difficult
11529 to handle something like `f()'. We want to notice that there are
11530 no decl-specifiers, and therefore realize that this is an
11531 expression, not a declaration.)
11532
11533 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11534 the declarator is a direct-declarator of the form "(...)".
11535
11536 MEMBER_P is true iff this declarator is a member-declarator. */
11537
11538 static cp_declarator *
11539 cp_parser_declarator (cp_parser* parser,
11540 cp_parser_declarator_kind dcl_kind,
11541 int* ctor_dtor_or_conv_p,
11542 bool* parenthesized_p,
11543 bool member_p)
11544 {
11545 cp_token *token;
11546 cp_declarator *declarator;
11547 enum tree_code code;
11548 cp_cv_quals cv_quals;
11549 tree class_type;
11550 tree attributes = NULL_TREE;
11551
11552 /* Assume this is not a constructor, destructor, or type-conversion
11553 operator. */
11554 if (ctor_dtor_or_conv_p)
11555 *ctor_dtor_or_conv_p = 0;
11556
11557 if (cp_parser_allow_gnu_extensions_p (parser))
11558 attributes = cp_parser_attributes_opt (parser);
11559
11560 /* Peek at the next token. */
11561 token = cp_lexer_peek_token (parser->lexer);
11562
11563 /* Check for the ptr-operator production. */
11564 cp_parser_parse_tentatively (parser);
11565 /* Parse the ptr-operator. */
11566 code = cp_parser_ptr_operator (parser,
11567 &class_type,
11568 &cv_quals);
11569 /* If that worked, then we have a ptr-operator. */
11570 if (cp_parser_parse_definitely (parser))
11571 {
11572 /* If a ptr-operator was found, then this declarator was not
11573 parenthesized. */
11574 if (parenthesized_p)
11575 *parenthesized_p = true;
11576 /* The dependent declarator is optional if we are parsing an
11577 abstract-declarator. */
11578 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11579 cp_parser_parse_tentatively (parser);
11580
11581 /* Parse the dependent declarator. */
11582 declarator = cp_parser_declarator (parser, dcl_kind,
11583 /*ctor_dtor_or_conv_p=*/NULL,
11584 /*parenthesized_p=*/NULL,
11585 /*member_p=*/false);
11586
11587 /* If we are parsing an abstract-declarator, we must handle the
11588 case where the dependent declarator is absent. */
11589 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11590 && !cp_parser_parse_definitely (parser))
11591 declarator = NULL;
11592
11593 /* Build the representation of the ptr-operator. */
11594 if (class_type)
11595 declarator = make_ptrmem_declarator (cv_quals,
11596 class_type,
11597 declarator);
11598 else if (code == INDIRECT_REF)
11599 declarator = make_pointer_declarator (cv_quals, declarator);
11600 else
11601 declarator = make_reference_declarator (cv_quals, declarator);
11602 }
11603 /* Everything else is a direct-declarator. */
11604 else
11605 {
11606 if (parenthesized_p)
11607 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11608 CPP_OPEN_PAREN);
11609 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11610 ctor_dtor_or_conv_p,
11611 member_p);
11612 }
11613
11614 if (attributes && declarator && declarator != cp_error_declarator)
11615 declarator->attributes = attributes;
11616
11617 return declarator;
11618 }
11619
11620 /* Parse a direct-declarator or direct-abstract-declarator.
11621
11622 direct-declarator:
11623 declarator-id
11624 direct-declarator ( parameter-declaration-clause )
11625 cv-qualifier-seq [opt]
11626 exception-specification [opt]
11627 direct-declarator [ constant-expression [opt] ]
11628 ( declarator )
11629
11630 direct-abstract-declarator:
11631 direct-abstract-declarator [opt]
11632 ( parameter-declaration-clause )
11633 cv-qualifier-seq [opt]
11634 exception-specification [opt]
11635 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11636 ( abstract-declarator )
11637
11638 Returns a representation of the declarator. DCL_KIND is
11639 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11640 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11641 we are parsing a direct-declarator. It is
11642 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11643 of ambiguity we prefer an abstract declarator, as per
11644 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11645 cp_parser_declarator. */
11646
11647 static cp_declarator *
11648 cp_parser_direct_declarator (cp_parser* parser,
11649 cp_parser_declarator_kind dcl_kind,
11650 int* ctor_dtor_or_conv_p,
11651 bool member_p)
11652 {
11653 cp_token *token;
11654 cp_declarator *declarator = NULL;
11655 tree scope = NULL_TREE;
11656 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11657 bool saved_in_declarator_p = parser->in_declarator_p;
11658 bool first = true;
11659 tree pushed_scope = NULL_TREE;
11660
11661 while (true)
11662 {
11663 /* Peek at the next token. */
11664 token = cp_lexer_peek_token (parser->lexer);
11665 if (token->type == CPP_OPEN_PAREN)
11666 {
11667 /* This is either a parameter-declaration-clause, or a
11668 parenthesized declarator. When we know we are parsing a
11669 named declarator, it must be a parenthesized declarator
11670 if FIRST is true. For instance, `(int)' is a
11671 parameter-declaration-clause, with an omitted
11672 direct-abstract-declarator. But `((*))', is a
11673 parenthesized abstract declarator. Finally, when T is a
11674 template parameter `(T)' is a
11675 parameter-declaration-clause, and not a parenthesized
11676 named declarator.
11677
11678 We first try and parse a parameter-declaration-clause,
11679 and then try a nested declarator (if FIRST is true).
11680
11681 It is not an error for it not to be a
11682 parameter-declaration-clause, even when FIRST is
11683 false. Consider,
11684
11685 int i (int);
11686 int i (3);
11687
11688 The first is the declaration of a function while the
11689 second is a the definition of a variable, including its
11690 initializer.
11691
11692 Having seen only the parenthesis, we cannot know which of
11693 these two alternatives should be selected. Even more
11694 complex are examples like:
11695
11696 int i (int (a));
11697 int i (int (3));
11698
11699 The former is a function-declaration; the latter is a
11700 variable initialization.
11701
11702 Thus again, we try a parameter-declaration-clause, and if
11703 that fails, we back out and return. */
11704
11705 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11706 {
11707 cp_parameter_declarator *params;
11708 unsigned saved_num_template_parameter_lists;
11709
11710 /* In a member-declarator, the only valid interpretation
11711 of a parenthesis is the start of a
11712 parameter-declaration-clause. (It is invalid to
11713 initialize a static data member with a parenthesized
11714 initializer; only the "=" form of initialization is
11715 permitted.) */
11716 if (!member_p)
11717 cp_parser_parse_tentatively (parser);
11718
11719 /* Consume the `('. */
11720 cp_lexer_consume_token (parser->lexer);
11721 if (first)
11722 {
11723 /* If this is going to be an abstract declarator, we're
11724 in a declarator and we can't have default args. */
11725 parser->default_arg_ok_p = false;
11726 parser->in_declarator_p = true;
11727 }
11728
11729 /* Inside the function parameter list, surrounding
11730 template-parameter-lists do not apply. */
11731 saved_num_template_parameter_lists
11732 = parser->num_template_parameter_lists;
11733 parser->num_template_parameter_lists = 0;
11734
11735 /* Parse the parameter-declaration-clause. */
11736 params = cp_parser_parameter_declaration_clause (parser);
11737
11738 parser->num_template_parameter_lists
11739 = saved_num_template_parameter_lists;
11740
11741 /* If all went well, parse the cv-qualifier-seq and the
11742 exception-specification. */
11743 if (member_p || cp_parser_parse_definitely (parser))
11744 {
11745 cp_cv_quals cv_quals;
11746 tree exception_specification;
11747
11748 if (ctor_dtor_or_conv_p)
11749 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11750 first = false;
11751 /* Consume the `)'. */
11752 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11753
11754 /* Parse the cv-qualifier-seq. */
11755 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11756 /* And the exception-specification. */
11757 exception_specification
11758 = cp_parser_exception_specification_opt (parser);
11759
11760 /* Create the function-declarator. */
11761 declarator = make_call_declarator (declarator,
11762 params,
11763 cv_quals,
11764 exception_specification);
11765 /* Any subsequent parameter lists are to do with
11766 return type, so are not those of the declared
11767 function. */
11768 parser->default_arg_ok_p = false;
11769
11770 /* Repeat the main loop. */
11771 continue;
11772 }
11773 }
11774
11775 /* If this is the first, we can try a parenthesized
11776 declarator. */
11777 if (first)
11778 {
11779 bool saved_in_type_id_in_expr_p;
11780
11781 parser->default_arg_ok_p = saved_default_arg_ok_p;
11782 parser->in_declarator_p = saved_in_declarator_p;
11783
11784 /* Consume the `('. */
11785 cp_lexer_consume_token (parser->lexer);
11786 /* Parse the nested declarator. */
11787 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11788 parser->in_type_id_in_expr_p = true;
11789 declarator
11790 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11791 /*parenthesized_p=*/NULL,
11792 member_p);
11793 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11794 first = false;
11795 /* Expect a `)'. */
11796 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11797 declarator = cp_error_declarator;
11798 if (declarator == cp_error_declarator)
11799 break;
11800
11801 goto handle_declarator;
11802 }
11803 /* Otherwise, we must be done. */
11804 else
11805 break;
11806 }
11807 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11808 && token->type == CPP_OPEN_SQUARE)
11809 {
11810 /* Parse an array-declarator. */
11811 tree bounds;
11812
11813 if (ctor_dtor_or_conv_p)
11814 *ctor_dtor_or_conv_p = 0;
11815
11816 first = false;
11817 parser->default_arg_ok_p = false;
11818 parser->in_declarator_p = true;
11819 /* Consume the `['. */
11820 cp_lexer_consume_token (parser->lexer);
11821 /* Peek at the next token. */
11822 token = cp_lexer_peek_token (parser->lexer);
11823 /* If the next token is `]', then there is no
11824 constant-expression. */
11825 if (token->type != CPP_CLOSE_SQUARE)
11826 {
11827 bool non_constant_p;
11828
11829 bounds
11830 = cp_parser_constant_expression (parser,
11831 /*allow_non_constant=*/true,
11832 &non_constant_p);
11833 if (!non_constant_p)
11834 bounds = fold_non_dependent_expr (bounds);
11835 /* Normally, the array bound must be an integral constant
11836 expression. However, as an extension, we allow VLAs
11837 in function scopes. */
11838 else if (!parser->in_function_body)
11839 {
11840 error ("array bound is not an integer constant");
11841 bounds = error_mark_node;
11842 }
11843 }
11844 else
11845 bounds = NULL_TREE;
11846 /* Look for the closing `]'. */
11847 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11848 {
11849 declarator = cp_error_declarator;
11850 break;
11851 }
11852
11853 declarator = make_array_declarator (declarator, bounds);
11854 }
11855 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11856 {
11857 tree qualifying_scope;
11858 tree unqualified_name;
11859 special_function_kind sfk;
11860 bool abstract_ok;
11861
11862 /* Parse a declarator-id */
11863 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11864 if (abstract_ok)
11865 cp_parser_parse_tentatively (parser);
11866 unqualified_name
11867 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11868 qualifying_scope = parser->scope;
11869 if (abstract_ok)
11870 {
11871 if (!cp_parser_parse_definitely (parser))
11872 unqualified_name = error_mark_node;
11873 else if (unqualified_name
11874 && (qualifying_scope
11875 || (TREE_CODE (unqualified_name)
11876 != IDENTIFIER_NODE)))
11877 {
11878 cp_parser_error (parser, "expected unqualified-id");
11879 unqualified_name = error_mark_node;
11880 }
11881 }
11882
11883 if (!unqualified_name)
11884 return NULL;
11885 if (unqualified_name == error_mark_node)
11886 {
11887 declarator = cp_error_declarator;
11888 break;
11889 }
11890
11891 if (qualifying_scope && at_namespace_scope_p ()
11892 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11893 {
11894 /* In the declaration of a member of a template class
11895 outside of the class itself, the SCOPE will sometimes
11896 be a TYPENAME_TYPE. For example, given:
11897
11898 template <typename T>
11899 int S<T>::R::i = 3;
11900
11901 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11902 this context, we must resolve S<T>::R to an ordinary
11903 type, rather than a typename type.
11904
11905 The reason we normally avoid resolving TYPENAME_TYPEs
11906 is that a specialization of `S' might render
11907 `S<T>::R' not a type. However, if `S' is
11908 specialized, then this `i' will not be used, so there
11909 is no harm in resolving the types here. */
11910 tree type;
11911
11912 /* Resolve the TYPENAME_TYPE. */
11913 type = resolve_typename_type (qualifying_scope,
11914 /*only_current_p=*/false);
11915 /* If that failed, the declarator is invalid. */
11916 if (type == error_mark_node)
11917 error ("%<%T::%D%> is not a type",
11918 TYPE_CONTEXT (qualifying_scope),
11919 TYPE_IDENTIFIER (qualifying_scope));
11920 qualifying_scope = type;
11921 }
11922
11923 sfk = sfk_none;
11924 if (unqualified_name)
11925 {
11926 tree class_type;
11927
11928 if (qualifying_scope
11929 && CLASS_TYPE_P (qualifying_scope))
11930 class_type = qualifying_scope;
11931 else
11932 class_type = current_class_type;
11933
11934 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11935 {
11936 tree name_type = TREE_TYPE (unqualified_name);
11937 if (class_type && same_type_p (name_type, class_type))
11938 {
11939 if (qualifying_scope
11940 && CLASSTYPE_USE_TEMPLATE (name_type))
11941 {
11942 error ("invalid use of constructor as a template");
11943 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11944 "name the constructor in a qualified name",
11945 class_type,
11946 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11947 class_type, name_type);
11948 declarator = cp_error_declarator;
11949 break;
11950 }
11951 else
11952 unqualified_name = constructor_name (class_type);
11953 }
11954 else
11955 {
11956 /* We do not attempt to print the declarator
11957 here because we do not have enough
11958 information about its original syntactic
11959 form. */
11960 cp_parser_error (parser, "invalid declarator");
11961 declarator = cp_error_declarator;
11962 break;
11963 }
11964 }
11965
11966 if (class_type)
11967 {
11968 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11969 sfk = sfk_destructor;
11970 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11971 sfk = sfk_conversion;
11972 else if (/* There's no way to declare a constructor
11973 for an anonymous type, even if the type
11974 got a name for linkage purposes. */
11975 !TYPE_WAS_ANONYMOUS (class_type)
11976 && constructor_name_p (unqualified_name,
11977 class_type))
11978 {
11979 unqualified_name = constructor_name (class_type);
11980 sfk = sfk_constructor;
11981 }
11982
11983 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11984 *ctor_dtor_or_conv_p = -1;
11985 }
11986 }
11987 declarator = make_id_declarator (qualifying_scope,
11988 unqualified_name,
11989 sfk);
11990 declarator->id_loc = token->location;
11991
11992 handle_declarator:;
11993 scope = get_scope_of_declarator (declarator);
11994 if (scope)
11995 /* Any names that appear after the declarator-id for a
11996 member are looked up in the containing scope. */
11997 pushed_scope = push_scope (scope);
11998 parser->in_declarator_p = true;
11999 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12000 || (declarator && declarator->kind == cdk_id))
12001 /* Default args are only allowed on function
12002 declarations. */
12003 parser->default_arg_ok_p = saved_default_arg_ok_p;
12004 else
12005 parser->default_arg_ok_p = false;
12006
12007 first = false;
12008 }
12009 /* We're done. */
12010 else
12011 break;
12012 }
12013
12014 /* For an abstract declarator, we might wind up with nothing at this
12015 point. That's an error; the declarator is not optional. */
12016 if (!declarator)
12017 cp_parser_error (parser, "expected declarator");
12018
12019 /* If we entered a scope, we must exit it now. */
12020 if (pushed_scope)
12021 pop_scope (pushed_scope);
12022
12023 parser->default_arg_ok_p = saved_default_arg_ok_p;
12024 parser->in_declarator_p = saved_in_declarator_p;
12025
12026 return declarator;
12027 }
12028
12029 /* Parse a ptr-operator.
12030
12031 ptr-operator:
12032 * cv-qualifier-seq [opt]
12033 &
12034 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12035
12036 GNU Extension:
12037
12038 ptr-operator:
12039 & cv-qualifier-seq [opt]
12040
12041 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12042 Returns ADDR_EXPR if a reference was used. In the case of a
12043 pointer-to-member, *TYPE is filled in with the TYPE containing the
12044 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12045 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12046 ERROR_MARK if an error occurred. */
12047
12048 static enum tree_code
12049 cp_parser_ptr_operator (cp_parser* parser,
12050 tree* type,
12051 cp_cv_quals *cv_quals)
12052 {
12053 enum tree_code code = ERROR_MARK;
12054 cp_token *token;
12055
12056 /* Assume that it's not a pointer-to-member. */
12057 *type = NULL_TREE;
12058 /* And that there are no cv-qualifiers. */
12059 *cv_quals = TYPE_UNQUALIFIED;
12060
12061 /* Peek at the next token. */
12062 token = cp_lexer_peek_token (parser->lexer);
12063 /* If it's a `*' or `&' we have a pointer or reference. */
12064 if (token->type == CPP_MULT || token->type == CPP_AND)
12065 {
12066 /* Remember which ptr-operator we were processing. */
12067 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12068
12069 /* Consume the `*' or `&'. */
12070 cp_lexer_consume_token (parser->lexer);
12071
12072 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12073 `&', if we are allowing GNU extensions. (The only qualifier
12074 that can legally appear after `&' is `restrict', but that is
12075 enforced during semantic analysis. */
12076 if (code == INDIRECT_REF
12077 || cp_parser_allow_gnu_extensions_p (parser))
12078 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12079 }
12080 else
12081 {
12082 /* Try the pointer-to-member case. */
12083 cp_parser_parse_tentatively (parser);
12084 /* Look for the optional `::' operator. */
12085 cp_parser_global_scope_opt (parser,
12086 /*current_scope_valid_p=*/false);
12087 /* Look for the nested-name specifier. */
12088 cp_parser_nested_name_specifier (parser,
12089 /*typename_keyword_p=*/false,
12090 /*check_dependency_p=*/true,
12091 /*type_p=*/false,
12092 /*is_declaration=*/false);
12093 /* If we found it, and the next token is a `*', then we are
12094 indeed looking at a pointer-to-member operator. */
12095 if (!cp_parser_error_occurred (parser)
12096 && cp_parser_require (parser, CPP_MULT, "`*'"))
12097 {
12098 /* Indicate that the `*' operator was used. */
12099 code = INDIRECT_REF;
12100
12101 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12102 error ("%qD is a namespace", parser->scope);
12103 else
12104 {
12105 /* The type of which the member is a member is given by the
12106 current SCOPE. */
12107 *type = parser->scope;
12108 /* The next name will not be qualified. */
12109 parser->scope = NULL_TREE;
12110 parser->qualifying_scope = NULL_TREE;
12111 parser->object_scope = NULL_TREE;
12112 /* Look for the optional cv-qualifier-seq. */
12113 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12114 }
12115 }
12116 /* If that didn't work we don't have a ptr-operator. */
12117 if (!cp_parser_parse_definitely (parser))
12118 cp_parser_error (parser, "expected ptr-operator");
12119 }
12120
12121 return code;
12122 }
12123
12124 /* Parse an (optional) cv-qualifier-seq.
12125
12126 cv-qualifier-seq:
12127 cv-qualifier cv-qualifier-seq [opt]
12128
12129 cv-qualifier:
12130 const
12131 volatile
12132
12133 GNU Extension:
12134
12135 cv-qualifier:
12136 __restrict__
12137
12138 Returns a bitmask representing the cv-qualifiers. */
12139
12140 static cp_cv_quals
12141 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12142 {
12143 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12144
12145 while (true)
12146 {
12147 cp_token *token;
12148 cp_cv_quals cv_qualifier;
12149
12150 /* Peek at the next token. */
12151 token = cp_lexer_peek_token (parser->lexer);
12152 /* See if it's a cv-qualifier. */
12153 switch (token->keyword)
12154 {
12155 case RID_CONST:
12156 cv_qualifier = TYPE_QUAL_CONST;
12157 break;
12158
12159 case RID_VOLATILE:
12160 cv_qualifier = TYPE_QUAL_VOLATILE;
12161 break;
12162
12163 case RID_RESTRICT:
12164 cv_qualifier = TYPE_QUAL_RESTRICT;
12165 break;
12166
12167 default:
12168 cv_qualifier = TYPE_UNQUALIFIED;
12169 break;
12170 }
12171
12172 if (!cv_qualifier)
12173 break;
12174
12175 if (cv_quals & cv_qualifier)
12176 {
12177 error ("duplicate cv-qualifier");
12178 cp_lexer_purge_token (parser->lexer);
12179 }
12180 else
12181 {
12182 cp_lexer_consume_token (parser->lexer);
12183 cv_quals |= cv_qualifier;
12184 }
12185 }
12186
12187 return cv_quals;
12188 }
12189
12190 /* Parse a declarator-id.
12191
12192 declarator-id:
12193 id-expression
12194 :: [opt] nested-name-specifier [opt] type-name
12195
12196 In the `id-expression' case, the value returned is as for
12197 cp_parser_id_expression if the id-expression was an unqualified-id.
12198 If the id-expression was a qualified-id, then a SCOPE_REF is
12199 returned. The first operand is the scope (either a NAMESPACE_DECL
12200 or TREE_TYPE), but the second is still just a representation of an
12201 unqualified-id. */
12202
12203 static tree
12204 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12205 {
12206 tree id;
12207 /* The expression must be an id-expression. Assume that qualified
12208 names are the names of types so that:
12209
12210 template <class T>
12211 int S<T>::R::i = 3;
12212
12213 will work; we must treat `S<T>::R' as the name of a type.
12214 Similarly, assume that qualified names are templates, where
12215 required, so that:
12216
12217 template <class T>
12218 int S<T>::R<T>::i = 3;
12219
12220 will work, too. */
12221 id = cp_parser_id_expression (parser,
12222 /*template_keyword_p=*/false,
12223 /*check_dependency_p=*/false,
12224 /*template_p=*/NULL,
12225 /*declarator_p=*/true,
12226 optional_p);
12227 if (id && BASELINK_P (id))
12228 id = BASELINK_FUNCTIONS (id);
12229 return id;
12230 }
12231
12232 /* Parse a type-id.
12233
12234 type-id:
12235 type-specifier-seq abstract-declarator [opt]
12236
12237 Returns the TYPE specified. */
12238
12239 static tree
12240 cp_parser_type_id (cp_parser* parser)
12241 {
12242 cp_decl_specifier_seq type_specifier_seq;
12243 cp_declarator *abstract_declarator;
12244
12245 /* Parse the type-specifier-seq. */
12246 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12247 &type_specifier_seq);
12248 if (type_specifier_seq.type == error_mark_node)
12249 return error_mark_node;
12250
12251 /* There might or might not be an abstract declarator. */
12252 cp_parser_parse_tentatively (parser);
12253 /* Look for the declarator. */
12254 abstract_declarator
12255 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12256 /*parenthesized_p=*/NULL,
12257 /*member_p=*/false);
12258 /* Check to see if there really was a declarator. */
12259 if (!cp_parser_parse_definitely (parser))
12260 abstract_declarator = NULL;
12261
12262 return groktypename (&type_specifier_seq, abstract_declarator);
12263 }
12264
12265 /* Parse a type-specifier-seq.
12266
12267 type-specifier-seq:
12268 type-specifier type-specifier-seq [opt]
12269
12270 GNU extension:
12271
12272 type-specifier-seq:
12273 attributes type-specifier-seq [opt]
12274
12275 If IS_CONDITION is true, we are at the start of a "condition",
12276 e.g., we've just seen "if (".
12277
12278 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12279
12280 static void
12281 cp_parser_type_specifier_seq (cp_parser* parser,
12282 bool is_condition,
12283 cp_decl_specifier_seq *type_specifier_seq)
12284 {
12285 bool seen_type_specifier = false;
12286 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12287
12288 /* Clear the TYPE_SPECIFIER_SEQ. */
12289 clear_decl_specs (type_specifier_seq);
12290
12291 /* Parse the type-specifiers and attributes. */
12292 while (true)
12293 {
12294 tree type_specifier;
12295 bool is_cv_qualifier;
12296
12297 /* Check for attributes first. */
12298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12299 {
12300 type_specifier_seq->attributes =
12301 chainon (type_specifier_seq->attributes,
12302 cp_parser_attributes_opt (parser));
12303 continue;
12304 }
12305
12306 /* Look for the type-specifier. */
12307 type_specifier = cp_parser_type_specifier (parser,
12308 flags,
12309 type_specifier_seq,
12310 /*is_declaration=*/false,
12311 NULL,
12312 &is_cv_qualifier);
12313 if (!type_specifier)
12314 {
12315 /* If the first type-specifier could not be found, this is not a
12316 type-specifier-seq at all. */
12317 if (!seen_type_specifier)
12318 {
12319 cp_parser_error (parser, "expected type-specifier");
12320 type_specifier_seq->type = error_mark_node;
12321 return;
12322 }
12323 /* If subsequent type-specifiers could not be found, the
12324 type-specifier-seq is complete. */
12325 break;
12326 }
12327
12328 seen_type_specifier = true;
12329 /* The standard says that a condition can be:
12330
12331 type-specifier-seq declarator = assignment-expression
12332
12333 However, given:
12334
12335 struct S {};
12336 if (int S = ...)
12337
12338 we should treat the "S" as a declarator, not as a
12339 type-specifier. The standard doesn't say that explicitly for
12340 type-specifier-seq, but it does say that for
12341 decl-specifier-seq in an ordinary declaration. Perhaps it
12342 would be clearer just to allow a decl-specifier-seq here, and
12343 then add a semantic restriction that if any decl-specifiers
12344 that are not type-specifiers appear, the program is invalid. */
12345 if (is_condition && !is_cv_qualifier)
12346 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12347 }
12348
12349 cp_parser_check_decl_spec (type_specifier_seq);
12350 }
12351
12352 /* Parse a parameter-declaration-clause.
12353
12354 parameter-declaration-clause:
12355 parameter-declaration-list [opt] ... [opt]
12356 parameter-declaration-list , ...
12357
12358 Returns a representation for the parameter declarations. A return
12359 value of NULL indicates a parameter-declaration-clause consisting
12360 only of an ellipsis. */
12361
12362 static cp_parameter_declarator *
12363 cp_parser_parameter_declaration_clause (cp_parser* parser)
12364 {
12365 cp_parameter_declarator *parameters;
12366 cp_token *token;
12367 bool ellipsis_p;
12368 bool is_error;
12369
12370 /* Peek at the next token. */
12371 token = cp_lexer_peek_token (parser->lexer);
12372 /* Check for trivial parameter-declaration-clauses. */
12373 if (token->type == CPP_ELLIPSIS)
12374 {
12375 /* Consume the `...' token. */
12376 cp_lexer_consume_token (parser->lexer);
12377 return NULL;
12378 }
12379 else if (token->type == CPP_CLOSE_PAREN)
12380 /* There are no parameters. */
12381 {
12382 #ifndef NO_IMPLICIT_EXTERN_C
12383 if (in_system_header && current_class_type == NULL
12384 && current_lang_name == lang_name_c)
12385 return NULL;
12386 else
12387 #endif
12388 return no_parameters;
12389 }
12390 /* Check for `(void)', too, which is a special case. */
12391 else if (token->keyword == RID_VOID
12392 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12393 == CPP_CLOSE_PAREN))
12394 {
12395 /* Consume the `void' token. */
12396 cp_lexer_consume_token (parser->lexer);
12397 /* There are no parameters. */
12398 return no_parameters;
12399 }
12400
12401 /* Parse the parameter-declaration-list. */
12402 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12403 /* If a parse error occurred while parsing the
12404 parameter-declaration-list, then the entire
12405 parameter-declaration-clause is erroneous. */
12406 if (is_error)
12407 return NULL;
12408
12409 /* Peek at the next token. */
12410 token = cp_lexer_peek_token (parser->lexer);
12411 /* If it's a `,', the clause should terminate with an ellipsis. */
12412 if (token->type == CPP_COMMA)
12413 {
12414 /* Consume the `,'. */
12415 cp_lexer_consume_token (parser->lexer);
12416 /* Expect an ellipsis. */
12417 ellipsis_p
12418 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12419 }
12420 /* It might also be `...' if the optional trailing `,' was
12421 omitted. */
12422 else if (token->type == CPP_ELLIPSIS)
12423 {
12424 /* Consume the `...' token. */
12425 cp_lexer_consume_token (parser->lexer);
12426 /* And remember that we saw it. */
12427 ellipsis_p = true;
12428 }
12429 else
12430 ellipsis_p = false;
12431
12432 /* Finish the parameter list. */
12433 if (parameters && ellipsis_p)
12434 parameters->ellipsis_p = true;
12435
12436 return parameters;
12437 }
12438
12439 /* Parse a parameter-declaration-list.
12440
12441 parameter-declaration-list:
12442 parameter-declaration
12443 parameter-declaration-list , parameter-declaration
12444
12445 Returns a representation of the parameter-declaration-list, as for
12446 cp_parser_parameter_declaration_clause. However, the
12447 `void_list_node' is never appended to the list. Upon return,
12448 *IS_ERROR will be true iff an error occurred. */
12449
12450 static cp_parameter_declarator *
12451 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12452 {
12453 cp_parameter_declarator *parameters = NULL;
12454 cp_parameter_declarator **tail = &parameters;
12455 bool saved_in_unbraced_linkage_specification_p;
12456
12457 /* Assume all will go well. */
12458 *is_error = false;
12459 /* The special considerations that apply to a function within an
12460 unbraced linkage specifications do not apply to the parameters
12461 to the function. */
12462 saved_in_unbraced_linkage_specification_p
12463 = parser->in_unbraced_linkage_specification_p;
12464 parser->in_unbraced_linkage_specification_p = false;
12465
12466 /* Look for more parameters. */
12467 while (true)
12468 {
12469 cp_parameter_declarator *parameter;
12470 bool parenthesized_p;
12471 /* Parse the parameter. */
12472 parameter
12473 = cp_parser_parameter_declaration (parser,
12474 /*template_parm_p=*/false,
12475 &parenthesized_p);
12476
12477 /* If a parse error occurred parsing the parameter declaration,
12478 then the entire parameter-declaration-list is erroneous. */
12479 if (!parameter)
12480 {
12481 *is_error = true;
12482 parameters = NULL;
12483 break;
12484 }
12485 /* Add the new parameter to the list. */
12486 *tail = parameter;
12487 tail = &parameter->next;
12488
12489 /* Peek at the next token. */
12490 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12491 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12492 /* These are for Objective-C++ */
12493 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12494 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12495 /* The parameter-declaration-list is complete. */
12496 break;
12497 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12498 {
12499 cp_token *token;
12500
12501 /* Peek at the next token. */
12502 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12503 /* If it's an ellipsis, then the list is complete. */
12504 if (token->type == CPP_ELLIPSIS)
12505 break;
12506 /* Otherwise, there must be more parameters. Consume the
12507 `,'. */
12508 cp_lexer_consume_token (parser->lexer);
12509 /* When parsing something like:
12510
12511 int i(float f, double d)
12512
12513 we can tell after seeing the declaration for "f" that we
12514 are not looking at an initialization of a variable "i",
12515 but rather at the declaration of a function "i".
12516
12517 Due to the fact that the parsing of template arguments
12518 (as specified to a template-id) requires backtracking we
12519 cannot use this technique when inside a template argument
12520 list. */
12521 if (!parser->in_template_argument_list_p
12522 && !parser->in_type_id_in_expr_p
12523 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12524 /* However, a parameter-declaration of the form
12525 "foat(f)" (which is a valid declaration of a
12526 parameter "f") can also be interpreted as an
12527 expression (the conversion of "f" to "float"). */
12528 && !parenthesized_p)
12529 cp_parser_commit_to_tentative_parse (parser);
12530 }
12531 else
12532 {
12533 cp_parser_error (parser, "expected %<,%> or %<...%>");
12534 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12535 cp_parser_skip_to_closing_parenthesis (parser,
12536 /*recovering=*/true,
12537 /*or_comma=*/false,
12538 /*consume_paren=*/false);
12539 break;
12540 }
12541 }
12542
12543 parser->in_unbraced_linkage_specification_p
12544 = saved_in_unbraced_linkage_specification_p;
12545
12546 return parameters;
12547 }
12548
12549 /* Parse a parameter declaration.
12550
12551 parameter-declaration:
12552 decl-specifier-seq declarator
12553 decl-specifier-seq declarator = assignment-expression
12554 decl-specifier-seq abstract-declarator [opt]
12555 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12556
12557 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12558 declares a template parameter. (In that case, a non-nested `>'
12559 token encountered during the parsing of the assignment-expression
12560 is not interpreted as a greater-than operator.)
12561
12562 Returns a representation of the parameter, or NULL if an error
12563 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12564 true iff the declarator is of the form "(p)". */
12565
12566 static cp_parameter_declarator *
12567 cp_parser_parameter_declaration (cp_parser *parser,
12568 bool template_parm_p,
12569 bool *parenthesized_p)
12570 {
12571 int declares_class_or_enum;
12572 bool greater_than_is_operator_p;
12573 cp_decl_specifier_seq decl_specifiers;
12574 cp_declarator *declarator;
12575 tree default_argument;
12576 cp_token *token;
12577 const char *saved_message;
12578
12579 /* In a template parameter, `>' is not an operator.
12580
12581 [temp.param]
12582
12583 When parsing a default template-argument for a non-type
12584 template-parameter, the first non-nested `>' is taken as the end
12585 of the template parameter-list rather than a greater-than
12586 operator. */
12587 greater_than_is_operator_p = !template_parm_p;
12588
12589 /* Type definitions may not appear in parameter types. */
12590 saved_message = parser->type_definition_forbidden_message;
12591 parser->type_definition_forbidden_message
12592 = "types may not be defined in parameter types";
12593
12594 /* Parse the declaration-specifiers. */
12595 cp_parser_decl_specifier_seq (parser,
12596 CP_PARSER_FLAGS_NONE,
12597 &decl_specifiers,
12598 &declares_class_or_enum);
12599 /* If an error occurred, there's no reason to attempt to parse the
12600 rest of the declaration. */
12601 if (cp_parser_error_occurred (parser))
12602 {
12603 parser->type_definition_forbidden_message = saved_message;
12604 return NULL;
12605 }
12606
12607 /* Peek at the next token. */
12608 token = cp_lexer_peek_token (parser->lexer);
12609 /* If the next token is a `)', `,', `=', `>', or `...', then there
12610 is no declarator. */
12611 if (token->type == CPP_CLOSE_PAREN
12612 || token->type == CPP_COMMA
12613 || token->type == CPP_EQ
12614 || token->type == CPP_ELLIPSIS
12615 || token->type == CPP_GREATER)
12616 {
12617 declarator = NULL;
12618 if (parenthesized_p)
12619 *parenthesized_p = false;
12620 }
12621 /* Otherwise, there should be a declarator. */
12622 else
12623 {
12624 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12625 parser->default_arg_ok_p = false;
12626
12627 /* After seeing a decl-specifier-seq, if the next token is not a
12628 "(", there is no possibility that the code is a valid
12629 expression. Therefore, if parsing tentatively, we commit at
12630 this point. */
12631 if (!parser->in_template_argument_list_p
12632 /* In an expression context, having seen:
12633
12634 (int((char ...
12635
12636 we cannot be sure whether we are looking at a
12637 function-type (taking a "char" as a parameter) or a cast
12638 of some object of type "char" to "int". */
12639 && !parser->in_type_id_in_expr_p
12640 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12641 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12642 cp_parser_commit_to_tentative_parse (parser);
12643 /* Parse the declarator. */
12644 declarator = cp_parser_declarator (parser,
12645 CP_PARSER_DECLARATOR_EITHER,
12646 /*ctor_dtor_or_conv_p=*/NULL,
12647 parenthesized_p,
12648 /*member_p=*/false);
12649 parser->default_arg_ok_p = saved_default_arg_ok_p;
12650 /* After the declarator, allow more attributes. */
12651 decl_specifiers.attributes
12652 = chainon (decl_specifiers.attributes,
12653 cp_parser_attributes_opt (parser));
12654 }
12655
12656 /* The restriction on defining new types applies only to the type
12657 of the parameter, not to the default argument. */
12658 parser->type_definition_forbidden_message = saved_message;
12659
12660 /* If the next token is `=', then process a default argument. */
12661 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12662 {
12663 bool saved_greater_than_is_operator_p;
12664 /* Consume the `='. */
12665 cp_lexer_consume_token (parser->lexer);
12666
12667 /* If we are defining a class, then the tokens that make up the
12668 default argument must be saved and processed later. */
12669 if (!template_parm_p && at_class_scope_p ()
12670 && TYPE_BEING_DEFINED (current_class_type))
12671 {
12672 unsigned depth = 0;
12673 cp_token *first_token;
12674 cp_token *token;
12675
12676 /* Add tokens until we have processed the entire default
12677 argument. We add the range [first_token, token). */
12678 first_token = cp_lexer_peek_token (parser->lexer);
12679 while (true)
12680 {
12681 bool done = false;
12682
12683 /* Peek at the next token. */
12684 token = cp_lexer_peek_token (parser->lexer);
12685 /* What we do depends on what token we have. */
12686 switch (token->type)
12687 {
12688 /* In valid code, a default argument must be
12689 immediately followed by a `,' `)', or `...'. */
12690 case CPP_COMMA:
12691 case CPP_CLOSE_PAREN:
12692 case CPP_ELLIPSIS:
12693 /* If we run into a non-nested `;', `}', or `]',
12694 then the code is invalid -- but the default
12695 argument is certainly over. */
12696 case CPP_SEMICOLON:
12697 case CPP_CLOSE_BRACE:
12698 case CPP_CLOSE_SQUARE:
12699 if (depth == 0)
12700 done = true;
12701 /* Update DEPTH, if necessary. */
12702 else if (token->type == CPP_CLOSE_PAREN
12703 || token->type == CPP_CLOSE_BRACE
12704 || token->type == CPP_CLOSE_SQUARE)
12705 --depth;
12706 break;
12707
12708 case CPP_OPEN_PAREN:
12709 case CPP_OPEN_SQUARE:
12710 case CPP_OPEN_BRACE:
12711 ++depth;
12712 break;
12713
12714 case CPP_GREATER:
12715 /* If we see a non-nested `>', and `>' is not an
12716 operator, then it marks the end of the default
12717 argument. */
12718 if (!depth && !greater_than_is_operator_p)
12719 done = true;
12720 break;
12721
12722 /* If we run out of tokens, issue an error message. */
12723 case CPP_EOF:
12724 case CPP_PRAGMA_EOL:
12725 error ("file ends in default argument");
12726 done = true;
12727 break;
12728
12729 case CPP_NAME:
12730 case CPP_SCOPE:
12731 /* In these cases, we should look for template-ids.
12732 For example, if the default argument is
12733 `X<int, double>()', we need to do name lookup to
12734 figure out whether or not `X' is a template; if
12735 so, the `,' does not end the default argument.
12736
12737 That is not yet done. */
12738 break;
12739
12740 default:
12741 break;
12742 }
12743
12744 /* If we've reached the end, stop. */
12745 if (done)
12746 break;
12747
12748 /* Add the token to the token block. */
12749 token = cp_lexer_consume_token (parser->lexer);
12750 }
12751
12752 /* Create a DEFAULT_ARG to represented the unparsed default
12753 argument. */
12754 default_argument = make_node (DEFAULT_ARG);
12755 DEFARG_TOKENS (default_argument)
12756 = cp_token_cache_new (first_token, token);
12757 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12758 }
12759 /* Outside of a class definition, we can just parse the
12760 assignment-expression. */
12761 else
12762 {
12763 bool saved_local_variables_forbidden_p;
12764
12765 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12766 set correctly. */
12767 saved_greater_than_is_operator_p
12768 = parser->greater_than_is_operator_p;
12769 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12770 /* Local variable names (and the `this' keyword) may not
12771 appear in a default argument. */
12772 saved_local_variables_forbidden_p
12773 = parser->local_variables_forbidden_p;
12774 parser->local_variables_forbidden_p = true;
12775 /* The default argument expression may cause implicitly
12776 defined member functions to be synthesized, which will
12777 result in garbage collection. We must treat this
12778 situation as if we were within the body of function so as
12779 to avoid collecting live data on the stack. */
12780 ++function_depth;
12781 /* Parse the assignment-expression. */
12782 if (template_parm_p)
12783 push_deferring_access_checks (dk_no_deferred);
12784 default_argument
12785 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12786 if (template_parm_p)
12787 pop_deferring_access_checks ();
12788 /* Restore saved state. */
12789 --function_depth;
12790 parser->greater_than_is_operator_p
12791 = saved_greater_than_is_operator_p;
12792 parser->local_variables_forbidden_p
12793 = saved_local_variables_forbidden_p;
12794 }
12795 if (!parser->default_arg_ok_p)
12796 {
12797 if (!flag_pedantic_errors)
12798 warning (0, "deprecated use of default argument for parameter of non-function");
12799 else
12800 {
12801 error ("default arguments are only permitted for function parameters");
12802 default_argument = NULL_TREE;
12803 }
12804 }
12805 }
12806 else
12807 default_argument = NULL_TREE;
12808
12809 return make_parameter_declarator (&decl_specifiers,
12810 declarator,
12811 default_argument);
12812 }
12813
12814 /* Parse a function-body.
12815
12816 function-body:
12817 compound_statement */
12818
12819 static void
12820 cp_parser_function_body (cp_parser *parser)
12821 {
12822 cp_parser_compound_statement (parser, NULL, false);
12823 }
12824
12825 /* Parse a ctor-initializer-opt followed by a function-body. Return
12826 true if a ctor-initializer was present. */
12827
12828 static bool
12829 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12830 {
12831 tree body;
12832 bool ctor_initializer_p;
12833
12834 /* Begin the function body. */
12835 body = begin_function_body ();
12836 /* Parse the optional ctor-initializer. */
12837 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12838 /* Parse the function-body. */
12839 cp_parser_function_body (parser);
12840 /* Finish the function body. */
12841 finish_function_body (body);
12842
12843 return ctor_initializer_p;
12844 }
12845
12846 /* Parse an initializer.
12847
12848 initializer:
12849 = initializer-clause
12850 ( expression-list )
12851
12852 Returns an expression representing the initializer. If no
12853 initializer is present, NULL_TREE is returned.
12854
12855 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12856 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12857 set to FALSE if there is no initializer present. If there is an
12858 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12859 is set to true; otherwise it is set to false. */
12860
12861 static tree
12862 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12863 bool* non_constant_p)
12864 {
12865 cp_token *token;
12866 tree init;
12867
12868 /* Peek at the next token. */
12869 token = cp_lexer_peek_token (parser->lexer);
12870
12871 /* Let our caller know whether or not this initializer was
12872 parenthesized. */
12873 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12874 /* Assume that the initializer is constant. */
12875 *non_constant_p = false;
12876
12877 if (token->type == CPP_EQ)
12878 {
12879 /* Consume the `='. */
12880 cp_lexer_consume_token (parser->lexer);
12881 /* Parse the initializer-clause. */
12882 init = cp_parser_initializer_clause (parser, non_constant_p);
12883 }
12884 else if (token->type == CPP_OPEN_PAREN)
12885 init = cp_parser_parenthesized_expression_list (parser, false,
12886 /*cast_p=*/false,
12887 non_constant_p);
12888 else
12889 {
12890 /* Anything else is an error. */
12891 cp_parser_error (parser, "expected initializer");
12892 init = error_mark_node;
12893 }
12894
12895 return init;
12896 }
12897
12898 /* Parse an initializer-clause.
12899
12900 initializer-clause:
12901 assignment-expression
12902 { initializer-list , [opt] }
12903 { }
12904
12905 Returns an expression representing the initializer.
12906
12907 If the `assignment-expression' production is used the value
12908 returned is simply a representation for the expression.
12909
12910 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12911 the elements of the initializer-list (or NULL, if the last
12912 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12913 NULL_TREE. There is no way to detect whether or not the optional
12914 trailing `,' was provided. NON_CONSTANT_P is as for
12915 cp_parser_initializer. */
12916
12917 static tree
12918 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12919 {
12920 tree initializer;
12921
12922 /* Assume the expression is constant. */
12923 *non_constant_p = false;
12924
12925 /* If it is not a `{', then we are looking at an
12926 assignment-expression. */
12927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12928 {
12929 initializer
12930 = cp_parser_constant_expression (parser,
12931 /*allow_non_constant_p=*/true,
12932 non_constant_p);
12933 if (!*non_constant_p)
12934 initializer = fold_non_dependent_expr (initializer);
12935 }
12936 else
12937 {
12938 /* Consume the `{' token. */
12939 cp_lexer_consume_token (parser->lexer);
12940 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12941 initializer = make_node (CONSTRUCTOR);
12942 /* If it's not a `}', then there is a non-trivial initializer. */
12943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12944 {
12945 /* Parse the initializer list. */
12946 CONSTRUCTOR_ELTS (initializer)
12947 = cp_parser_initializer_list (parser, non_constant_p);
12948 /* A trailing `,' token is allowed. */
12949 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12950 cp_lexer_consume_token (parser->lexer);
12951 }
12952 /* Now, there should be a trailing `}'. */
12953 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12954 }
12955
12956 return initializer;
12957 }
12958
12959 /* Parse an initializer-list.
12960
12961 initializer-list:
12962 initializer-clause
12963 initializer-list , initializer-clause
12964
12965 GNU Extension:
12966
12967 initializer-list:
12968 identifier : initializer-clause
12969 initializer-list, identifier : initializer-clause
12970
12971 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12972 for the initializer. If the INDEX of the elt is non-NULL, it is the
12973 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12974 as for cp_parser_initializer. */
12975
12976 static VEC(constructor_elt,gc) *
12977 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12978 {
12979 VEC(constructor_elt,gc) *v = NULL;
12980
12981 /* Assume all of the expressions are constant. */
12982 *non_constant_p = false;
12983
12984 /* Parse the rest of the list. */
12985 while (true)
12986 {
12987 cp_token *token;
12988 tree identifier;
12989 tree initializer;
12990 bool clause_non_constant_p;
12991
12992 /* If the next token is an identifier and the following one is a
12993 colon, we are looking at the GNU designated-initializer
12994 syntax. */
12995 if (cp_parser_allow_gnu_extensions_p (parser)
12996 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12997 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12998 {
12999 /* Warn the user that they are using an extension. */
13000 if (pedantic)
13001 pedwarn ("ISO C++ does not allow designated initializers");
13002 /* Consume the identifier. */
13003 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13004 /* Consume the `:'. */
13005 cp_lexer_consume_token (parser->lexer);
13006 }
13007 else
13008 identifier = NULL_TREE;
13009
13010 /* Parse the initializer. */
13011 initializer = cp_parser_initializer_clause (parser,
13012 &clause_non_constant_p);
13013 /* If any clause is non-constant, so is the entire initializer. */
13014 if (clause_non_constant_p)
13015 *non_constant_p = true;
13016
13017 /* Add it to the vector. */
13018 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13019
13020 /* If the next token is not a comma, we have reached the end of
13021 the list. */
13022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13023 break;
13024
13025 /* Peek at the next token. */
13026 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13027 /* If the next token is a `}', then we're still done. An
13028 initializer-clause can have a trailing `,' after the
13029 initializer-list and before the closing `}'. */
13030 if (token->type == CPP_CLOSE_BRACE)
13031 break;
13032
13033 /* Consume the `,' token. */
13034 cp_lexer_consume_token (parser->lexer);
13035 }
13036
13037 return v;
13038 }
13039
13040 /* Classes [gram.class] */
13041
13042 /* Parse a class-name.
13043
13044 class-name:
13045 identifier
13046 template-id
13047
13048 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13049 to indicate that names looked up in dependent types should be
13050 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13051 keyword has been used to indicate that the name that appears next
13052 is a template. TAG_TYPE indicates the explicit tag given before
13053 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13054 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13055 is the class being defined in a class-head.
13056
13057 Returns the TYPE_DECL representing the class. */
13058
13059 static tree
13060 cp_parser_class_name (cp_parser *parser,
13061 bool typename_keyword_p,
13062 bool template_keyword_p,
13063 enum tag_types tag_type,
13064 bool check_dependency_p,
13065 bool class_head_p,
13066 bool is_declaration)
13067 {
13068 tree decl;
13069 tree scope;
13070 bool typename_p;
13071 cp_token *token;
13072
13073 /* All class-names start with an identifier. */
13074 token = cp_lexer_peek_token (parser->lexer);
13075 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13076 {
13077 cp_parser_error (parser, "expected class-name");
13078 return error_mark_node;
13079 }
13080
13081 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13082 to a template-id, so we save it here. */
13083 scope = parser->scope;
13084 if (scope == error_mark_node)
13085 return error_mark_node;
13086
13087 /* Any name names a type if we're following the `typename' keyword
13088 in a qualified name where the enclosing scope is type-dependent. */
13089 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13090 && dependent_type_p (scope));
13091 /* Handle the common case (an identifier, but not a template-id)
13092 efficiently. */
13093 if (token->type == CPP_NAME
13094 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13095 {
13096 cp_token *identifier_token;
13097 tree identifier;
13098 bool ambiguous_p;
13099
13100 /* Look for the identifier. */
13101 identifier_token = cp_lexer_peek_token (parser->lexer);
13102 ambiguous_p = identifier_token->ambiguous_p;
13103 identifier = cp_parser_identifier (parser);
13104 /* If the next token isn't an identifier, we are certainly not
13105 looking at a class-name. */
13106 if (identifier == error_mark_node)
13107 decl = error_mark_node;
13108 /* If we know this is a type-name, there's no need to look it
13109 up. */
13110 else if (typename_p)
13111 decl = identifier;
13112 else
13113 {
13114 tree ambiguous_decls;
13115 /* If we already know that this lookup is ambiguous, then
13116 we've already issued an error message; there's no reason
13117 to check again. */
13118 if (ambiguous_p)
13119 {
13120 cp_parser_simulate_error (parser);
13121 return error_mark_node;
13122 }
13123 /* If the next token is a `::', then the name must be a type
13124 name.
13125
13126 [basic.lookup.qual]
13127
13128 During the lookup for a name preceding the :: scope
13129 resolution operator, object, function, and enumerator
13130 names are ignored. */
13131 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13132 tag_type = typename_type;
13133 /* Look up the name. */
13134 decl = cp_parser_lookup_name (parser, identifier,
13135 tag_type,
13136 /*is_template=*/false,
13137 /*is_namespace=*/false,
13138 check_dependency_p,
13139 &ambiguous_decls);
13140 if (ambiguous_decls)
13141 {
13142 error ("reference to %qD is ambiguous", identifier);
13143 print_candidates (ambiguous_decls);
13144 if (cp_parser_parsing_tentatively (parser))
13145 {
13146 identifier_token->ambiguous_p = true;
13147 cp_parser_simulate_error (parser);
13148 }
13149 return error_mark_node;
13150 }
13151 }
13152 }
13153 else
13154 {
13155 /* Try a template-id. */
13156 decl = cp_parser_template_id (parser, template_keyword_p,
13157 check_dependency_p,
13158 is_declaration);
13159 if (decl == error_mark_node)
13160 return error_mark_node;
13161 }
13162
13163 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13164
13165 /* If this is a typename, create a TYPENAME_TYPE. */
13166 if (typename_p && decl != error_mark_node)
13167 {
13168 decl = make_typename_type (scope, decl, typename_type,
13169 /*complain=*/tf_error);
13170 if (decl != error_mark_node)
13171 decl = TYPE_NAME (decl);
13172 }
13173
13174 /* Check to see that it is really the name of a class. */
13175 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13176 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13177 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13178 /* Situations like this:
13179
13180 template <typename T> struct A {
13181 typename T::template X<int>::I i;
13182 };
13183
13184 are problematic. Is `T::template X<int>' a class-name? The
13185 standard does not seem to be definitive, but there is no other
13186 valid interpretation of the following `::'. Therefore, those
13187 names are considered class-names. */
13188 {
13189 decl = make_typename_type (scope, decl, tag_type, tf_error);
13190 if (decl != error_mark_node)
13191 decl = TYPE_NAME (decl);
13192 }
13193 else if (TREE_CODE (decl) != TYPE_DECL
13194 || TREE_TYPE (decl) == error_mark_node
13195 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13196 decl = error_mark_node;
13197
13198 if (decl == error_mark_node)
13199 cp_parser_error (parser, "expected class-name");
13200
13201 return decl;
13202 }
13203
13204 /* Parse a class-specifier.
13205
13206 class-specifier:
13207 class-head { member-specification [opt] }
13208
13209 Returns the TREE_TYPE representing the class. */
13210
13211 static tree
13212 cp_parser_class_specifier (cp_parser* parser)
13213 {
13214 cp_token *token;
13215 tree type;
13216 tree attributes = NULL_TREE;
13217 int has_trailing_semicolon;
13218 bool nested_name_specifier_p;
13219 unsigned saved_num_template_parameter_lists;
13220 bool saved_in_function_body;
13221 tree old_scope = NULL_TREE;
13222 tree scope = NULL_TREE;
13223 tree bases;
13224
13225 push_deferring_access_checks (dk_no_deferred);
13226
13227 /* Parse the class-head. */
13228 type = cp_parser_class_head (parser,
13229 &nested_name_specifier_p,
13230 &attributes,
13231 &bases);
13232 /* If the class-head was a semantic disaster, skip the entire body
13233 of the class. */
13234 if (!type)
13235 {
13236 cp_parser_skip_to_end_of_block_or_statement (parser);
13237 pop_deferring_access_checks ();
13238 return error_mark_node;
13239 }
13240
13241 /* Look for the `{'. */
13242 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13243 {
13244 pop_deferring_access_checks ();
13245 return error_mark_node;
13246 }
13247
13248 /* Process the base classes. If they're invalid, skip the
13249 entire class body. */
13250 if (!xref_basetypes (type, bases))
13251 {
13252 cp_parser_skip_to_closing_brace (parser);
13253
13254 /* Consuming the closing brace yields better error messages
13255 later on. */
13256 cp_lexer_consume_token (parser->lexer);
13257 pop_deferring_access_checks ();
13258 return error_mark_node;
13259 }
13260
13261 /* Issue an error message if type-definitions are forbidden here. */
13262 cp_parser_check_type_definition (parser);
13263 /* Remember that we are defining one more class. */
13264 ++parser->num_classes_being_defined;
13265 /* Inside the class, surrounding template-parameter-lists do not
13266 apply. */
13267 saved_num_template_parameter_lists
13268 = parser->num_template_parameter_lists;
13269 parser->num_template_parameter_lists = 0;
13270 /* We are not in a function body. */
13271 saved_in_function_body = parser->in_function_body;
13272 parser->in_function_body = false;
13273
13274 /* Start the class. */
13275 if (nested_name_specifier_p)
13276 {
13277 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13278 old_scope = push_inner_scope (scope);
13279 }
13280 type = begin_class_definition (type, attributes);
13281
13282 if (type == error_mark_node)
13283 /* If the type is erroneous, skip the entire body of the class. */
13284 cp_parser_skip_to_closing_brace (parser);
13285 else
13286 /* Parse the member-specification. */
13287 cp_parser_member_specification_opt (parser);
13288
13289 /* Look for the trailing `}'. */
13290 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13291 /* We get better error messages by noticing a common problem: a
13292 missing trailing `;'. */
13293 token = cp_lexer_peek_token (parser->lexer);
13294 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13295 /* Look for trailing attributes to apply to this class. */
13296 if (cp_parser_allow_gnu_extensions_p (parser))
13297 attributes = cp_parser_attributes_opt (parser);
13298 if (type != error_mark_node)
13299 type = finish_struct (type, attributes);
13300 if (nested_name_specifier_p)
13301 pop_inner_scope (old_scope, scope);
13302 /* If this class is not itself within the scope of another class,
13303 then we need to parse the bodies of all of the queued function
13304 definitions. Note that the queued functions defined in a class
13305 are not always processed immediately following the
13306 class-specifier for that class. Consider:
13307
13308 struct A {
13309 struct B { void f() { sizeof (A); } };
13310 };
13311
13312 If `f' were processed before the processing of `A' were
13313 completed, there would be no way to compute the size of `A'.
13314 Note that the nesting we are interested in here is lexical --
13315 not the semantic nesting given by TYPE_CONTEXT. In particular,
13316 for:
13317
13318 struct A { struct B; };
13319 struct A::B { void f() { } };
13320
13321 there is no need to delay the parsing of `A::B::f'. */
13322 if (--parser->num_classes_being_defined == 0)
13323 {
13324 tree queue_entry;
13325 tree fn;
13326 tree class_type = NULL_TREE;
13327 tree pushed_scope = NULL_TREE;
13328
13329 /* In a first pass, parse default arguments to the functions.
13330 Then, in a second pass, parse the bodies of the functions.
13331 This two-phased approach handles cases like:
13332
13333 struct S {
13334 void f() { g(); }
13335 void g(int i = 3);
13336 };
13337
13338 */
13339 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13340 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13341 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13342 TREE_PURPOSE (parser->unparsed_functions_queues)
13343 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13344 {
13345 fn = TREE_VALUE (queue_entry);
13346 /* If there are default arguments that have not yet been processed,
13347 take care of them now. */
13348 if (class_type != TREE_PURPOSE (queue_entry))
13349 {
13350 if (pushed_scope)
13351 pop_scope (pushed_scope);
13352 class_type = TREE_PURPOSE (queue_entry);
13353 pushed_scope = push_scope (class_type);
13354 }
13355 /* Make sure that any template parameters are in scope. */
13356 maybe_begin_member_template_processing (fn);
13357 /* Parse the default argument expressions. */
13358 cp_parser_late_parsing_default_args (parser, fn);
13359 /* Remove any template parameters from the symbol table. */
13360 maybe_end_member_template_processing ();
13361 }
13362 if (pushed_scope)
13363 pop_scope (pushed_scope);
13364 /* Now parse the body of the functions. */
13365 for (TREE_VALUE (parser->unparsed_functions_queues)
13366 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13367 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13368 TREE_VALUE (parser->unparsed_functions_queues)
13369 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13370 {
13371 /* Figure out which function we need to process. */
13372 fn = TREE_VALUE (queue_entry);
13373 /* Parse the function. */
13374 cp_parser_late_parsing_for_member (parser, fn);
13375 }
13376 }
13377
13378 /* Put back any saved access checks. */
13379 pop_deferring_access_checks ();
13380
13381 /* Restore saved state. */
13382 parser->in_function_body = saved_in_function_body;
13383 parser->num_template_parameter_lists
13384 = saved_num_template_parameter_lists;
13385
13386 return type;
13387 }
13388
13389 /* Parse a class-head.
13390
13391 class-head:
13392 class-key identifier [opt] base-clause [opt]
13393 class-key nested-name-specifier identifier base-clause [opt]
13394 class-key nested-name-specifier [opt] template-id
13395 base-clause [opt]
13396
13397 GNU Extensions:
13398 class-key attributes identifier [opt] base-clause [opt]
13399 class-key attributes nested-name-specifier identifier base-clause [opt]
13400 class-key attributes nested-name-specifier [opt] template-id
13401 base-clause [opt]
13402
13403 Upon return BASES is initialized to the list of base classes (or
13404 NULL, if there are none) in the same form returned by
13405 cp_parser_base_clause.
13406
13407 Returns the TYPE of the indicated class. Sets
13408 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13409 involving a nested-name-specifier was used, and FALSE otherwise.
13410
13411 Returns error_mark_node if this is not a class-head.
13412
13413 Returns NULL_TREE if the class-head is syntactically valid, but
13414 semantically invalid in a way that means we should skip the entire
13415 body of the class. */
13416
13417 static tree
13418 cp_parser_class_head (cp_parser* parser,
13419 bool* nested_name_specifier_p,
13420 tree *attributes_p,
13421 tree *bases)
13422 {
13423 tree nested_name_specifier;
13424 enum tag_types class_key;
13425 tree id = NULL_TREE;
13426 tree type = NULL_TREE;
13427 tree attributes;
13428 bool template_id_p = false;
13429 bool qualified_p = false;
13430 bool invalid_nested_name_p = false;
13431 bool invalid_explicit_specialization_p = false;
13432 tree pushed_scope = NULL_TREE;
13433 unsigned num_templates;
13434
13435 /* Assume no nested-name-specifier will be present. */
13436 *nested_name_specifier_p = false;
13437 /* Assume no template parameter lists will be used in defining the
13438 type. */
13439 num_templates = 0;
13440
13441 *bases = NULL_TREE;
13442
13443 /* Look for the class-key. */
13444 class_key = cp_parser_class_key (parser);
13445 if (class_key == none_type)
13446 return error_mark_node;
13447
13448 /* Parse the attributes. */
13449 attributes = cp_parser_attributes_opt (parser);
13450
13451 /* If the next token is `::', that is invalid -- but sometimes
13452 people do try to write:
13453
13454 struct ::S {};
13455
13456 Handle this gracefully by accepting the extra qualifier, and then
13457 issuing an error about it later if this really is a
13458 class-head. If it turns out just to be an elaborated type
13459 specifier, remain silent. */
13460 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13461 qualified_p = true;
13462
13463 push_deferring_access_checks (dk_no_check);
13464
13465 /* Determine the name of the class. Begin by looking for an
13466 optional nested-name-specifier. */
13467 nested_name_specifier
13468 = cp_parser_nested_name_specifier_opt (parser,
13469 /*typename_keyword_p=*/false,
13470 /*check_dependency_p=*/false,
13471 /*type_p=*/false,
13472 /*is_declaration=*/false);
13473 /* If there was a nested-name-specifier, then there *must* be an
13474 identifier. */
13475 if (nested_name_specifier)
13476 {
13477 /* Although the grammar says `identifier', it really means
13478 `class-name' or `template-name'. You are only allowed to
13479 define a class that has already been declared with this
13480 syntax.
13481
13482 The proposed resolution for Core Issue 180 says that wherever
13483 you see `class T::X' you should treat `X' as a type-name.
13484
13485 It is OK to define an inaccessible class; for example:
13486
13487 class A { class B; };
13488 class A::B {};
13489
13490 We do not know if we will see a class-name, or a
13491 template-name. We look for a class-name first, in case the
13492 class-name is a template-id; if we looked for the
13493 template-name first we would stop after the template-name. */
13494 cp_parser_parse_tentatively (parser);
13495 type = cp_parser_class_name (parser,
13496 /*typename_keyword_p=*/false,
13497 /*template_keyword_p=*/false,
13498 class_type,
13499 /*check_dependency_p=*/false,
13500 /*class_head_p=*/true,
13501 /*is_declaration=*/false);
13502 /* If that didn't work, ignore the nested-name-specifier. */
13503 if (!cp_parser_parse_definitely (parser))
13504 {
13505 invalid_nested_name_p = true;
13506 id = cp_parser_identifier (parser);
13507 if (id == error_mark_node)
13508 id = NULL_TREE;
13509 }
13510 /* If we could not find a corresponding TYPE, treat this
13511 declaration like an unqualified declaration. */
13512 if (type == error_mark_node)
13513 nested_name_specifier = NULL_TREE;
13514 /* Otherwise, count the number of templates used in TYPE and its
13515 containing scopes. */
13516 else
13517 {
13518 tree scope;
13519
13520 for (scope = TREE_TYPE (type);
13521 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13522 scope = (TYPE_P (scope)
13523 ? TYPE_CONTEXT (scope)
13524 : DECL_CONTEXT (scope)))
13525 if (TYPE_P (scope)
13526 && CLASS_TYPE_P (scope)
13527 && CLASSTYPE_TEMPLATE_INFO (scope)
13528 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13529 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13530 ++num_templates;
13531 }
13532 }
13533 /* Otherwise, the identifier is optional. */
13534 else
13535 {
13536 /* We don't know whether what comes next is a template-id,
13537 an identifier, or nothing at all. */
13538 cp_parser_parse_tentatively (parser);
13539 /* Check for a template-id. */
13540 id = cp_parser_template_id (parser,
13541 /*template_keyword_p=*/false,
13542 /*check_dependency_p=*/true,
13543 /*is_declaration=*/true);
13544 /* If that didn't work, it could still be an identifier. */
13545 if (!cp_parser_parse_definitely (parser))
13546 {
13547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13548 id = cp_parser_identifier (parser);
13549 else
13550 id = NULL_TREE;
13551 }
13552 else
13553 {
13554 template_id_p = true;
13555 ++num_templates;
13556 }
13557 }
13558
13559 pop_deferring_access_checks ();
13560
13561 if (id)
13562 cp_parser_check_for_invalid_template_id (parser, id);
13563
13564 /* If it's not a `:' or a `{' then we can't really be looking at a
13565 class-head, since a class-head only appears as part of a
13566 class-specifier. We have to detect this situation before calling
13567 xref_tag, since that has irreversible side-effects. */
13568 if (!cp_parser_next_token_starts_class_definition_p (parser))
13569 {
13570 cp_parser_error (parser, "expected %<{%> or %<:%>");
13571 return error_mark_node;
13572 }
13573
13574 /* At this point, we're going ahead with the class-specifier, even
13575 if some other problem occurs. */
13576 cp_parser_commit_to_tentative_parse (parser);
13577 /* Issue the error about the overly-qualified name now. */
13578 if (qualified_p)
13579 cp_parser_error (parser,
13580 "global qualification of class name is invalid");
13581 else if (invalid_nested_name_p)
13582 cp_parser_error (parser,
13583 "qualified name does not name a class");
13584 else if (nested_name_specifier)
13585 {
13586 tree scope;
13587
13588 /* Reject typedef-names in class heads. */
13589 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13590 {
13591 error ("invalid class name in declaration of %qD", type);
13592 type = NULL_TREE;
13593 goto done;
13594 }
13595
13596 /* Figure out in what scope the declaration is being placed. */
13597 scope = current_scope ();
13598 /* If that scope does not contain the scope in which the
13599 class was originally declared, the program is invalid. */
13600 if (scope && !is_ancestor (scope, nested_name_specifier))
13601 {
13602 error ("declaration of %qD in %qD which does not enclose %qD",
13603 type, scope, nested_name_specifier);
13604 type = NULL_TREE;
13605 goto done;
13606 }
13607 /* [dcl.meaning]
13608
13609 A declarator-id shall not be qualified exception of the
13610 definition of a ... nested class outside of its class
13611 ... [or] a the definition or explicit instantiation of a
13612 class member of a namespace outside of its namespace. */
13613 if (scope == nested_name_specifier)
13614 {
13615 pedwarn ("extra qualification ignored");
13616 nested_name_specifier = NULL_TREE;
13617 num_templates = 0;
13618 }
13619 }
13620 /* An explicit-specialization must be preceded by "template <>". If
13621 it is not, try to recover gracefully. */
13622 if (at_namespace_scope_p ()
13623 && parser->num_template_parameter_lists == 0
13624 && template_id_p)
13625 {
13626 error ("an explicit specialization must be preceded by %<template <>%>");
13627 invalid_explicit_specialization_p = true;
13628 /* Take the same action that would have been taken by
13629 cp_parser_explicit_specialization. */
13630 ++parser->num_template_parameter_lists;
13631 begin_specialization ();
13632 }
13633 /* There must be no "return" statements between this point and the
13634 end of this function; set "type "to the correct return value and
13635 use "goto done;" to return. */
13636 /* Make sure that the right number of template parameters were
13637 present. */
13638 if (!cp_parser_check_template_parameters (parser, num_templates))
13639 {
13640 /* If something went wrong, there is no point in even trying to
13641 process the class-definition. */
13642 type = NULL_TREE;
13643 goto done;
13644 }
13645
13646 /* Look up the type. */
13647 if (template_id_p)
13648 {
13649 type = TREE_TYPE (id);
13650 type = maybe_process_partial_specialization (type);
13651 if (nested_name_specifier)
13652 pushed_scope = push_scope (nested_name_specifier);
13653 }
13654 else if (nested_name_specifier)
13655 {
13656 tree class_type;
13657
13658 /* Given:
13659
13660 template <typename T> struct S { struct T };
13661 template <typename T> struct S<T>::T { };
13662
13663 we will get a TYPENAME_TYPE when processing the definition of
13664 `S::T'. We need to resolve it to the actual type before we
13665 try to define it. */
13666 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13667 {
13668 class_type = resolve_typename_type (TREE_TYPE (type),
13669 /*only_current_p=*/false);
13670 if (class_type != error_mark_node)
13671 type = TYPE_NAME (class_type);
13672 else
13673 {
13674 cp_parser_error (parser, "could not resolve typename type");
13675 type = error_mark_node;
13676 }
13677 }
13678
13679 maybe_process_partial_specialization (TREE_TYPE (type));
13680 class_type = current_class_type;
13681 /* Enter the scope indicated by the nested-name-specifier. */
13682 pushed_scope = push_scope (nested_name_specifier);
13683 /* Get the canonical version of this type. */
13684 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13685 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13686 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13687 {
13688 type = push_template_decl (type);
13689 if (type == error_mark_node)
13690 {
13691 type = NULL_TREE;
13692 goto done;
13693 }
13694 }
13695
13696 type = TREE_TYPE (type);
13697 *nested_name_specifier_p = true;
13698 }
13699 else /* The name is not a nested name. */
13700 {
13701 /* If the class was unnamed, create a dummy name. */
13702 if (!id)
13703 id = make_anon_name ();
13704 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13705 parser->num_template_parameter_lists);
13706 }
13707
13708 /* Indicate whether this class was declared as a `class' or as a
13709 `struct'. */
13710 if (TREE_CODE (type) == RECORD_TYPE)
13711 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13712 cp_parser_check_class_key (class_key, type);
13713
13714 /* If this type was already complete, and we see another definition,
13715 that's an error. */
13716 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13717 {
13718 error ("redefinition of %q#T", type);
13719 error ("previous definition of %q+#T", type);
13720 type = NULL_TREE;
13721 goto done;
13722 }
13723 else if (type == error_mark_node)
13724 type = NULL_TREE;
13725
13726 /* We will have entered the scope containing the class; the names of
13727 base classes should be looked up in that context. For example:
13728
13729 struct A { struct B {}; struct C; };
13730 struct A::C : B {};
13731
13732 is valid. */
13733
13734 /* Get the list of base-classes, if there is one. */
13735 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13736 *bases = cp_parser_base_clause (parser);
13737
13738 done:
13739 /* Leave the scope given by the nested-name-specifier. We will
13740 enter the class scope itself while processing the members. */
13741 if (pushed_scope)
13742 pop_scope (pushed_scope);
13743
13744 if (invalid_explicit_specialization_p)
13745 {
13746 end_specialization ();
13747 --parser->num_template_parameter_lists;
13748 }
13749 *attributes_p = attributes;
13750 return type;
13751 }
13752
13753 /* Parse a class-key.
13754
13755 class-key:
13756 class
13757 struct
13758 union
13759
13760 Returns the kind of class-key specified, or none_type to indicate
13761 error. */
13762
13763 static enum tag_types
13764 cp_parser_class_key (cp_parser* parser)
13765 {
13766 cp_token *token;
13767 enum tag_types tag_type;
13768
13769 /* Look for the class-key. */
13770 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13771 if (!token)
13772 return none_type;
13773
13774 /* Check to see if the TOKEN is a class-key. */
13775 tag_type = cp_parser_token_is_class_key (token);
13776 if (!tag_type)
13777 cp_parser_error (parser, "expected class-key");
13778 return tag_type;
13779 }
13780
13781 /* Parse an (optional) member-specification.
13782
13783 member-specification:
13784 member-declaration member-specification [opt]
13785 access-specifier : member-specification [opt] */
13786
13787 static void
13788 cp_parser_member_specification_opt (cp_parser* parser)
13789 {
13790 while (true)
13791 {
13792 cp_token *token;
13793 enum rid keyword;
13794
13795 /* Peek at the next token. */
13796 token = cp_lexer_peek_token (parser->lexer);
13797 /* If it's a `}', or EOF then we've seen all the members. */
13798 if (token->type == CPP_CLOSE_BRACE
13799 || token->type == CPP_EOF
13800 || token->type == CPP_PRAGMA_EOL)
13801 break;
13802
13803 /* See if this token is a keyword. */
13804 keyword = token->keyword;
13805 switch (keyword)
13806 {
13807 case RID_PUBLIC:
13808 case RID_PROTECTED:
13809 case RID_PRIVATE:
13810 /* Consume the access-specifier. */
13811 cp_lexer_consume_token (parser->lexer);
13812 /* Remember which access-specifier is active. */
13813 current_access_specifier = token->u.value;
13814 /* Look for the `:'. */
13815 cp_parser_require (parser, CPP_COLON, "`:'");
13816 break;
13817
13818 default:
13819 /* Accept #pragmas at class scope. */
13820 if (token->type == CPP_PRAGMA)
13821 {
13822 cp_parser_pragma (parser, pragma_external);
13823 break;
13824 }
13825
13826 /* Otherwise, the next construction must be a
13827 member-declaration. */
13828 cp_parser_member_declaration (parser);
13829 }
13830 }
13831 }
13832
13833 /* Parse a member-declaration.
13834
13835 member-declaration:
13836 decl-specifier-seq [opt] member-declarator-list [opt] ;
13837 function-definition ; [opt]
13838 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13839 using-declaration
13840 template-declaration
13841
13842 member-declarator-list:
13843 member-declarator
13844 member-declarator-list , member-declarator
13845
13846 member-declarator:
13847 declarator pure-specifier [opt]
13848 declarator constant-initializer [opt]
13849 identifier [opt] : constant-expression
13850
13851 GNU Extensions:
13852
13853 member-declaration:
13854 __extension__ member-declaration
13855
13856 member-declarator:
13857 declarator attributes [opt] pure-specifier [opt]
13858 declarator attributes [opt] constant-initializer [opt]
13859 identifier [opt] attributes [opt] : constant-expression
13860
13861 C++0x Extensions:
13862
13863 member-declaration:
13864 static_assert-declaration */
13865
13866 static void
13867 cp_parser_member_declaration (cp_parser* parser)
13868 {
13869 cp_decl_specifier_seq decl_specifiers;
13870 tree prefix_attributes;
13871 tree decl;
13872 int declares_class_or_enum;
13873 bool friend_p;
13874 cp_token *token;
13875 int saved_pedantic;
13876
13877 /* Check for the `__extension__' keyword. */
13878 if (cp_parser_extension_opt (parser, &saved_pedantic))
13879 {
13880 /* Recurse. */
13881 cp_parser_member_declaration (parser);
13882 /* Restore the old value of the PEDANTIC flag. */
13883 pedantic = saved_pedantic;
13884
13885 return;
13886 }
13887
13888 /* Check for a template-declaration. */
13889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13890 {
13891 /* An explicit specialization here is an error condition, and we
13892 expect the specialization handler to detect and report this. */
13893 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13894 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13895 cp_parser_explicit_specialization (parser);
13896 else
13897 cp_parser_template_declaration (parser, /*member_p=*/true);
13898
13899 return;
13900 }
13901
13902 /* Check for a using-declaration. */
13903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13904 {
13905 /* Parse the using-declaration. */
13906 cp_parser_using_declaration (parser,
13907 /*access_declaration_p=*/false);
13908 return;
13909 }
13910
13911 /* Check for @defs. */
13912 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13913 {
13914 tree ivar, member;
13915 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13916 ivar = ivar_chains;
13917 while (ivar)
13918 {
13919 member = ivar;
13920 ivar = TREE_CHAIN (member);
13921 TREE_CHAIN (member) = NULL_TREE;
13922 finish_member_declaration (member);
13923 }
13924 return;
13925 }
13926
13927 /* If the next token is `static_assert' we have a static assertion. */
13928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13929 {
13930 cp_parser_static_assert (parser, /*member_p=*/true);
13931 return;
13932 }
13933
13934 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13935 return;
13936
13937 /* Parse the decl-specifier-seq. */
13938 cp_parser_decl_specifier_seq (parser,
13939 CP_PARSER_FLAGS_OPTIONAL,
13940 &decl_specifiers,
13941 &declares_class_or_enum);
13942 prefix_attributes = decl_specifiers.attributes;
13943 decl_specifiers.attributes = NULL_TREE;
13944 /* Check for an invalid type-name. */
13945 if (!decl_specifiers.type
13946 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13947 return;
13948 /* If there is no declarator, then the decl-specifier-seq should
13949 specify a type. */
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13951 {
13952 /* If there was no decl-specifier-seq, and the next token is a
13953 `;', then we have something like:
13954
13955 struct S { ; };
13956
13957 [class.mem]
13958
13959 Each member-declaration shall declare at least one member
13960 name of the class. */
13961 if (!decl_specifiers.any_specifiers_p)
13962 {
13963 cp_token *token = cp_lexer_peek_token (parser->lexer);
13964 if (pedantic && !token->in_system_header)
13965 pedwarn ("%Hextra %<;%>", &token->location);
13966 }
13967 else
13968 {
13969 tree type;
13970
13971 /* See if this declaration is a friend. */
13972 friend_p = cp_parser_friend_p (&decl_specifiers);
13973 /* If there were decl-specifiers, check to see if there was
13974 a class-declaration. */
13975 type = check_tag_decl (&decl_specifiers);
13976 /* Nested classes have already been added to the class, but
13977 a `friend' needs to be explicitly registered. */
13978 if (friend_p)
13979 {
13980 /* If the `friend' keyword was present, the friend must
13981 be introduced with a class-key. */
13982 if (!declares_class_or_enum)
13983 error ("a class-key must be used when declaring a friend");
13984 /* In this case:
13985
13986 template <typename T> struct A {
13987 friend struct A<T>::B;
13988 };
13989
13990 A<T>::B will be represented by a TYPENAME_TYPE, and
13991 therefore not recognized by check_tag_decl. */
13992 if (!type
13993 && decl_specifiers.type
13994 && TYPE_P (decl_specifiers.type))
13995 type = decl_specifiers.type;
13996 if (!type || !TYPE_P (type))
13997 error ("friend declaration does not name a class or "
13998 "function");
13999 else
14000 make_friend_class (current_class_type, type,
14001 /*complain=*/true);
14002 }
14003 /* If there is no TYPE, an error message will already have
14004 been issued. */
14005 else if (!type || type == error_mark_node)
14006 ;
14007 /* An anonymous aggregate has to be handled specially; such
14008 a declaration really declares a data member (with a
14009 particular type), as opposed to a nested class. */
14010 else if (ANON_AGGR_TYPE_P (type))
14011 {
14012 /* Remove constructors and such from TYPE, now that we
14013 know it is an anonymous aggregate. */
14014 fixup_anonymous_aggr (type);
14015 /* And make the corresponding data member. */
14016 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14017 /* Add it to the class. */
14018 finish_member_declaration (decl);
14019 }
14020 else
14021 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14022 }
14023 }
14024 else
14025 {
14026 /* See if these declarations will be friends. */
14027 friend_p = cp_parser_friend_p (&decl_specifiers);
14028
14029 /* Keep going until we hit the `;' at the end of the
14030 declaration. */
14031 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14032 {
14033 tree attributes = NULL_TREE;
14034 tree first_attribute;
14035
14036 /* Peek at the next token. */
14037 token = cp_lexer_peek_token (parser->lexer);
14038
14039 /* Check for a bitfield declaration. */
14040 if (token->type == CPP_COLON
14041 || (token->type == CPP_NAME
14042 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14043 == CPP_COLON))
14044 {
14045 tree identifier;
14046 tree width;
14047
14048 /* Get the name of the bitfield. Note that we cannot just
14049 check TOKEN here because it may have been invalidated by
14050 the call to cp_lexer_peek_nth_token above. */
14051 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14052 identifier = cp_parser_identifier (parser);
14053 else
14054 identifier = NULL_TREE;
14055
14056 /* Consume the `:' token. */
14057 cp_lexer_consume_token (parser->lexer);
14058 /* Get the width of the bitfield. */
14059 width
14060 = cp_parser_constant_expression (parser,
14061 /*allow_non_constant=*/false,
14062 NULL);
14063
14064 /* Look for attributes that apply to the bitfield. */
14065 attributes = cp_parser_attributes_opt (parser);
14066 /* Remember which attributes are prefix attributes and
14067 which are not. */
14068 first_attribute = attributes;
14069 /* Combine the attributes. */
14070 attributes = chainon (prefix_attributes, attributes);
14071
14072 /* Create the bitfield declaration. */
14073 decl = grokbitfield (identifier
14074 ? make_id_declarator (NULL_TREE,
14075 identifier,
14076 sfk_none)
14077 : NULL,
14078 &decl_specifiers,
14079 width);
14080 /* Apply the attributes. */
14081 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14082 }
14083 else
14084 {
14085 cp_declarator *declarator;
14086 tree initializer;
14087 tree asm_specification;
14088 int ctor_dtor_or_conv_p;
14089
14090 /* Parse the declarator. */
14091 declarator
14092 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14093 &ctor_dtor_or_conv_p,
14094 /*parenthesized_p=*/NULL,
14095 /*member_p=*/true);
14096
14097 /* If something went wrong parsing the declarator, make sure
14098 that we at least consume some tokens. */
14099 if (declarator == cp_error_declarator)
14100 {
14101 /* Skip to the end of the statement. */
14102 cp_parser_skip_to_end_of_statement (parser);
14103 /* If the next token is not a semicolon, that is
14104 probably because we just skipped over the body of
14105 a function. So, we consume a semicolon if
14106 present, but do not issue an error message if it
14107 is not present. */
14108 if (cp_lexer_next_token_is (parser->lexer,
14109 CPP_SEMICOLON))
14110 cp_lexer_consume_token (parser->lexer);
14111 return;
14112 }
14113
14114 if (declares_class_or_enum & 2)
14115 cp_parser_check_for_definition_in_return_type
14116 (declarator, decl_specifiers.type);
14117
14118 /* Look for an asm-specification. */
14119 asm_specification = cp_parser_asm_specification_opt (parser);
14120 /* Look for attributes that apply to the declaration. */
14121 attributes = cp_parser_attributes_opt (parser);
14122 /* Remember which attributes are prefix attributes and
14123 which are not. */
14124 first_attribute = attributes;
14125 /* Combine the attributes. */
14126 attributes = chainon (prefix_attributes, attributes);
14127
14128 /* If it's an `=', then we have a constant-initializer or a
14129 pure-specifier. It is not correct to parse the
14130 initializer before registering the member declaration
14131 since the member declaration should be in scope while
14132 its initializer is processed. However, the rest of the
14133 front end does not yet provide an interface that allows
14134 us to handle this correctly. */
14135 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14136 {
14137 /* In [class.mem]:
14138
14139 A pure-specifier shall be used only in the declaration of
14140 a virtual function.
14141
14142 A member-declarator can contain a constant-initializer
14143 only if it declares a static member of integral or
14144 enumeration type.
14145
14146 Therefore, if the DECLARATOR is for a function, we look
14147 for a pure-specifier; otherwise, we look for a
14148 constant-initializer. When we call `grokfield', it will
14149 perform more stringent semantics checks. */
14150 if (function_declarator_p (declarator))
14151 initializer = cp_parser_pure_specifier (parser);
14152 else
14153 /* Parse the initializer. */
14154 initializer = cp_parser_constant_initializer (parser);
14155 }
14156 /* Otherwise, there is no initializer. */
14157 else
14158 initializer = NULL_TREE;
14159
14160 /* See if we are probably looking at a function
14161 definition. We are certainly not looking at a
14162 member-declarator. Calling `grokfield' has
14163 side-effects, so we must not do it unless we are sure
14164 that we are looking at a member-declarator. */
14165 if (cp_parser_token_starts_function_definition_p
14166 (cp_lexer_peek_token (parser->lexer)))
14167 {
14168 /* The grammar does not allow a pure-specifier to be
14169 used when a member function is defined. (It is
14170 possible that this fact is an oversight in the
14171 standard, since a pure function may be defined
14172 outside of the class-specifier. */
14173 if (initializer)
14174 error ("pure-specifier on function-definition");
14175 decl = cp_parser_save_member_function_body (parser,
14176 &decl_specifiers,
14177 declarator,
14178 attributes);
14179 /* If the member was not a friend, declare it here. */
14180 if (!friend_p)
14181 finish_member_declaration (decl);
14182 /* Peek at the next token. */
14183 token = cp_lexer_peek_token (parser->lexer);
14184 /* If the next token is a semicolon, consume it. */
14185 if (token->type == CPP_SEMICOLON)
14186 cp_lexer_consume_token (parser->lexer);
14187 return;
14188 }
14189 else
14190 /* Create the declaration. */
14191 decl = grokfield (declarator, &decl_specifiers,
14192 initializer, /*init_const_expr_p=*/true,
14193 asm_specification,
14194 attributes);
14195 }
14196
14197 /* Reset PREFIX_ATTRIBUTES. */
14198 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14199 attributes = TREE_CHAIN (attributes);
14200 if (attributes)
14201 TREE_CHAIN (attributes) = NULL_TREE;
14202
14203 /* If there is any qualification still in effect, clear it
14204 now; we will be starting fresh with the next declarator. */
14205 parser->scope = NULL_TREE;
14206 parser->qualifying_scope = NULL_TREE;
14207 parser->object_scope = NULL_TREE;
14208 /* If it's a `,', then there are more declarators. */
14209 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14210 cp_lexer_consume_token (parser->lexer);
14211 /* If the next token isn't a `;', then we have a parse error. */
14212 else if (cp_lexer_next_token_is_not (parser->lexer,
14213 CPP_SEMICOLON))
14214 {
14215 cp_parser_error (parser, "expected %<;%>");
14216 /* Skip tokens until we find a `;'. */
14217 cp_parser_skip_to_end_of_statement (parser);
14218
14219 break;
14220 }
14221
14222 if (decl)
14223 {
14224 /* Add DECL to the list of members. */
14225 if (!friend_p)
14226 finish_member_declaration (decl);
14227
14228 if (TREE_CODE (decl) == FUNCTION_DECL)
14229 cp_parser_save_default_args (parser, decl);
14230 }
14231 }
14232 }
14233
14234 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14235 }
14236
14237 /* Parse a pure-specifier.
14238
14239 pure-specifier:
14240 = 0
14241
14242 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14243 Otherwise, ERROR_MARK_NODE is returned. */
14244
14245 static tree
14246 cp_parser_pure_specifier (cp_parser* parser)
14247 {
14248 cp_token *token;
14249
14250 /* Look for the `=' token. */
14251 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14252 return error_mark_node;
14253 /* Look for the `0' token. */
14254 token = cp_lexer_consume_token (parser->lexer);
14255 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14256 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14257 {
14258 cp_parser_error (parser,
14259 "invalid pure specifier (only `= 0' is allowed)");
14260 cp_parser_skip_to_end_of_statement (parser);
14261 return error_mark_node;
14262 }
14263 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14264 {
14265 error ("templates may not be %<virtual%>");
14266 return error_mark_node;
14267 }
14268
14269 return integer_zero_node;
14270 }
14271
14272 /* Parse a constant-initializer.
14273
14274 constant-initializer:
14275 = constant-expression
14276
14277 Returns a representation of the constant-expression. */
14278
14279 static tree
14280 cp_parser_constant_initializer (cp_parser* parser)
14281 {
14282 /* Look for the `=' token. */
14283 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14284 return error_mark_node;
14285
14286 /* It is invalid to write:
14287
14288 struct S { static const int i = { 7 }; };
14289
14290 */
14291 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14292 {
14293 cp_parser_error (parser,
14294 "a brace-enclosed initializer is not allowed here");
14295 /* Consume the opening brace. */
14296 cp_lexer_consume_token (parser->lexer);
14297 /* Skip the initializer. */
14298 cp_parser_skip_to_closing_brace (parser);
14299 /* Look for the trailing `}'. */
14300 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14301
14302 return error_mark_node;
14303 }
14304
14305 return cp_parser_constant_expression (parser,
14306 /*allow_non_constant=*/false,
14307 NULL);
14308 }
14309
14310 /* Derived classes [gram.class.derived] */
14311
14312 /* Parse a base-clause.
14313
14314 base-clause:
14315 : base-specifier-list
14316
14317 base-specifier-list:
14318 base-specifier
14319 base-specifier-list , base-specifier
14320
14321 Returns a TREE_LIST representing the base-classes, in the order in
14322 which they were declared. The representation of each node is as
14323 described by cp_parser_base_specifier.
14324
14325 In the case that no bases are specified, this function will return
14326 NULL_TREE, not ERROR_MARK_NODE. */
14327
14328 static tree
14329 cp_parser_base_clause (cp_parser* parser)
14330 {
14331 tree bases = NULL_TREE;
14332
14333 /* Look for the `:' that begins the list. */
14334 cp_parser_require (parser, CPP_COLON, "`:'");
14335
14336 /* Scan the base-specifier-list. */
14337 while (true)
14338 {
14339 cp_token *token;
14340 tree base;
14341
14342 /* Look for the base-specifier. */
14343 base = cp_parser_base_specifier (parser);
14344 /* Add BASE to the front of the list. */
14345 if (base != error_mark_node)
14346 {
14347 TREE_CHAIN (base) = bases;
14348 bases = base;
14349 }
14350 /* Peek at the next token. */
14351 token = cp_lexer_peek_token (parser->lexer);
14352 /* If it's not a comma, then the list is complete. */
14353 if (token->type != CPP_COMMA)
14354 break;
14355 /* Consume the `,'. */
14356 cp_lexer_consume_token (parser->lexer);
14357 }
14358
14359 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14360 base class had a qualified name. However, the next name that
14361 appears is certainly not qualified. */
14362 parser->scope = NULL_TREE;
14363 parser->qualifying_scope = NULL_TREE;
14364 parser->object_scope = NULL_TREE;
14365
14366 return nreverse (bases);
14367 }
14368
14369 /* Parse a base-specifier.
14370
14371 base-specifier:
14372 :: [opt] nested-name-specifier [opt] class-name
14373 virtual access-specifier [opt] :: [opt] nested-name-specifier
14374 [opt] class-name
14375 access-specifier virtual [opt] :: [opt] nested-name-specifier
14376 [opt] class-name
14377
14378 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14379 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14380 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14381 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14382
14383 static tree
14384 cp_parser_base_specifier (cp_parser* parser)
14385 {
14386 cp_token *token;
14387 bool done = false;
14388 bool virtual_p = false;
14389 bool duplicate_virtual_error_issued_p = false;
14390 bool duplicate_access_error_issued_p = false;
14391 bool class_scope_p, template_p;
14392 tree access = access_default_node;
14393 tree type;
14394
14395 /* Process the optional `virtual' and `access-specifier'. */
14396 while (!done)
14397 {
14398 /* Peek at the next token. */
14399 token = cp_lexer_peek_token (parser->lexer);
14400 /* Process `virtual'. */
14401 switch (token->keyword)
14402 {
14403 case RID_VIRTUAL:
14404 /* If `virtual' appears more than once, issue an error. */
14405 if (virtual_p && !duplicate_virtual_error_issued_p)
14406 {
14407 cp_parser_error (parser,
14408 "%<virtual%> specified more than once in base-specified");
14409 duplicate_virtual_error_issued_p = true;
14410 }
14411
14412 virtual_p = true;
14413
14414 /* Consume the `virtual' token. */
14415 cp_lexer_consume_token (parser->lexer);
14416
14417 break;
14418
14419 case RID_PUBLIC:
14420 case RID_PROTECTED:
14421 case RID_PRIVATE:
14422 /* If more than one access specifier appears, issue an
14423 error. */
14424 if (access != access_default_node
14425 && !duplicate_access_error_issued_p)
14426 {
14427 cp_parser_error (parser,
14428 "more than one access specifier in base-specified");
14429 duplicate_access_error_issued_p = true;
14430 }
14431
14432 access = ridpointers[(int) token->keyword];
14433
14434 /* Consume the access-specifier. */
14435 cp_lexer_consume_token (parser->lexer);
14436
14437 break;
14438
14439 default:
14440 done = true;
14441 break;
14442 }
14443 }
14444 /* It is not uncommon to see programs mechanically, erroneously, use
14445 the 'typename' keyword to denote (dependent) qualified types
14446 as base classes. */
14447 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14448 {
14449 if (!processing_template_decl)
14450 error ("keyword %<typename%> not allowed outside of templates");
14451 else
14452 error ("keyword %<typename%> not allowed in this context "
14453 "(the base class is implicitly a type)");
14454 cp_lexer_consume_token (parser->lexer);
14455 }
14456
14457 /* Look for the optional `::' operator. */
14458 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14459 /* Look for the nested-name-specifier. The simplest way to
14460 implement:
14461
14462 [temp.res]
14463
14464 The keyword `typename' is not permitted in a base-specifier or
14465 mem-initializer; in these contexts a qualified name that
14466 depends on a template-parameter is implicitly assumed to be a
14467 type name.
14468
14469 is to pretend that we have seen the `typename' keyword at this
14470 point. */
14471 cp_parser_nested_name_specifier_opt (parser,
14472 /*typename_keyword_p=*/true,
14473 /*check_dependency_p=*/true,
14474 typename_type,
14475 /*is_declaration=*/true);
14476 /* If the base class is given by a qualified name, assume that names
14477 we see are type names or templates, as appropriate. */
14478 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14479 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14480
14481 /* Finally, look for the class-name. */
14482 type = cp_parser_class_name (parser,
14483 class_scope_p,
14484 template_p,
14485 typename_type,
14486 /*check_dependency_p=*/true,
14487 /*class_head_p=*/false,
14488 /*is_declaration=*/true);
14489
14490 if (type == error_mark_node)
14491 return error_mark_node;
14492
14493 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14494 }
14495
14496 /* Exception handling [gram.exception] */
14497
14498 /* Parse an (optional) exception-specification.
14499
14500 exception-specification:
14501 throw ( type-id-list [opt] )
14502
14503 Returns a TREE_LIST representing the exception-specification. The
14504 TREE_VALUE of each node is a type. */
14505
14506 static tree
14507 cp_parser_exception_specification_opt (cp_parser* parser)
14508 {
14509 cp_token *token;
14510 tree type_id_list;
14511
14512 /* Peek at the next token. */
14513 token = cp_lexer_peek_token (parser->lexer);
14514 /* If it's not `throw', then there's no exception-specification. */
14515 if (!cp_parser_is_keyword (token, RID_THROW))
14516 return NULL_TREE;
14517
14518 /* Consume the `throw'. */
14519 cp_lexer_consume_token (parser->lexer);
14520
14521 /* Look for the `('. */
14522 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14523
14524 /* Peek at the next token. */
14525 token = cp_lexer_peek_token (parser->lexer);
14526 /* If it's not a `)', then there is a type-id-list. */
14527 if (token->type != CPP_CLOSE_PAREN)
14528 {
14529 const char *saved_message;
14530
14531 /* Types may not be defined in an exception-specification. */
14532 saved_message = parser->type_definition_forbidden_message;
14533 parser->type_definition_forbidden_message
14534 = "types may not be defined in an exception-specification";
14535 /* Parse the type-id-list. */
14536 type_id_list = cp_parser_type_id_list (parser);
14537 /* Restore the saved message. */
14538 parser->type_definition_forbidden_message = saved_message;
14539 }
14540 else
14541 type_id_list = empty_except_spec;
14542
14543 /* Look for the `)'. */
14544 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14545
14546 return type_id_list;
14547 }
14548
14549 /* Parse an (optional) type-id-list.
14550
14551 type-id-list:
14552 type-id
14553 type-id-list , type-id
14554
14555 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14556 in the order that the types were presented. */
14557
14558 static tree
14559 cp_parser_type_id_list (cp_parser* parser)
14560 {
14561 tree types = NULL_TREE;
14562
14563 while (true)
14564 {
14565 cp_token *token;
14566 tree type;
14567
14568 /* Get the next type-id. */
14569 type = cp_parser_type_id (parser);
14570 /* Add it to the list. */
14571 types = add_exception_specifier (types, type, /*complain=*/1);
14572 /* Peek at the next token. */
14573 token = cp_lexer_peek_token (parser->lexer);
14574 /* If it is not a `,', we are done. */
14575 if (token->type != CPP_COMMA)
14576 break;
14577 /* Consume the `,'. */
14578 cp_lexer_consume_token (parser->lexer);
14579 }
14580
14581 return nreverse (types);
14582 }
14583
14584 /* Parse a try-block.
14585
14586 try-block:
14587 try compound-statement handler-seq */
14588
14589 static tree
14590 cp_parser_try_block (cp_parser* parser)
14591 {
14592 tree try_block;
14593
14594 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14595 try_block = begin_try_block ();
14596 cp_parser_compound_statement (parser, NULL, true);
14597 finish_try_block (try_block);
14598 cp_parser_handler_seq (parser);
14599 finish_handler_sequence (try_block);
14600
14601 return try_block;
14602 }
14603
14604 /* Parse a function-try-block.
14605
14606 function-try-block:
14607 try ctor-initializer [opt] function-body handler-seq */
14608
14609 static bool
14610 cp_parser_function_try_block (cp_parser* parser)
14611 {
14612 tree compound_stmt;
14613 tree try_block;
14614 bool ctor_initializer_p;
14615
14616 /* Look for the `try' keyword. */
14617 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14618 return false;
14619 /* Let the rest of the front end know where we are. */
14620 try_block = begin_function_try_block (&compound_stmt);
14621 /* Parse the function-body. */
14622 ctor_initializer_p
14623 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14624 /* We're done with the `try' part. */
14625 finish_function_try_block (try_block);
14626 /* Parse the handlers. */
14627 cp_parser_handler_seq (parser);
14628 /* We're done with the handlers. */
14629 finish_function_handler_sequence (try_block, compound_stmt);
14630
14631 return ctor_initializer_p;
14632 }
14633
14634 /* Parse a handler-seq.
14635
14636 handler-seq:
14637 handler handler-seq [opt] */
14638
14639 static void
14640 cp_parser_handler_seq (cp_parser* parser)
14641 {
14642 while (true)
14643 {
14644 cp_token *token;
14645
14646 /* Parse the handler. */
14647 cp_parser_handler (parser);
14648 /* Peek at the next token. */
14649 token = cp_lexer_peek_token (parser->lexer);
14650 /* If it's not `catch' then there are no more handlers. */
14651 if (!cp_parser_is_keyword (token, RID_CATCH))
14652 break;
14653 }
14654 }
14655
14656 /* Parse a handler.
14657
14658 handler:
14659 catch ( exception-declaration ) compound-statement */
14660
14661 static void
14662 cp_parser_handler (cp_parser* parser)
14663 {
14664 tree handler;
14665 tree declaration;
14666
14667 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14668 handler = begin_handler ();
14669 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14670 declaration = cp_parser_exception_declaration (parser);
14671 finish_handler_parms (declaration, handler);
14672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14673 cp_parser_compound_statement (parser, NULL, false);
14674 finish_handler (handler);
14675 }
14676
14677 /* Parse an exception-declaration.
14678
14679 exception-declaration:
14680 type-specifier-seq declarator
14681 type-specifier-seq abstract-declarator
14682 type-specifier-seq
14683 ...
14684
14685 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14686 ellipsis variant is used. */
14687
14688 static tree
14689 cp_parser_exception_declaration (cp_parser* parser)
14690 {
14691 cp_decl_specifier_seq type_specifiers;
14692 cp_declarator *declarator;
14693 const char *saved_message;
14694
14695 /* If it's an ellipsis, it's easy to handle. */
14696 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14697 {
14698 /* Consume the `...' token. */
14699 cp_lexer_consume_token (parser->lexer);
14700 return NULL_TREE;
14701 }
14702
14703 /* Types may not be defined in exception-declarations. */
14704 saved_message = parser->type_definition_forbidden_message;
14705 parser->type_definition_forbidden_message
14706 = "types may not be defined in exception-declarations";
14707
14708 /* Parse the type-specifier-seq. */
14709 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14710 &type_specifiers);
14711 /* If it's a `)', then there is no declarator. */
14712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14713 declarator = NULL;
14714 else
14715 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14716 /*ctor_dtor_or_conv_p=*/NULL,
14717 /*parenthesized_p=*/NULL,
14718 /*member_p=*/false);
14719
14720 /* Restore the saved message. */
14721 parser->type_definition_forbidden_message = saved_message;
14722
14723 if (!type_specifiers.any_specifiers_p)
14724 return error_mark_node;
14725
14726 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14727 }
14728
14729 /* Parse a throw-expression.
14730
14731 throw-expression:
14732 throw assignment-expression [opt]
14733
14734 Returns a THROW_EXPR representing the throw-expression. */
14735
14736 static tree
14737 cp_parser_throw_expression (cp_parser* parser)
14738 {
14739 tree expression;
14740 cp_token* token;
14741
14742 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14743 token = cp_lexer_peek_token (parser->lexer);
14744 /* Figure out whether or not there is an assignment-expression
14745 following the "throw" keyword. */
14746 if (token->type == CPP_COMMA
14747 || token->type == CPP_SEMICOLON
14748 || token->type == CPP_CLOSE_PAREN
14749 || token->type == CPP_CLOSE_SQUARE
14750 || token->type == CPP_CLOSE_BRACE
14751 || token->type == CPP_COLON)
14752 expression = NULL_TREE;
14753 else
14754 expression = cp_parser_assignment_expression (parser,
14755 /*cast_p=*/false);
14756
14757 return build_throw (expression);
14758 }
14759
14760 /* GNU Extensions */
14761
14762 /* Parse an (optional) asm-specification.
14763
14764 asm-specification:
14765 asm ( string-literal )
14766
14767 If the asm-specification is present, returns a STRING_CST
14768 corresponding to the string-literal. Otherwise, returns
14769 NULL_TREE. */
14770
14771 static tree
14772 cp_parser_asm_specification_opt (cp_parser* parser)
14773 {
14774 cp_token *token;
14775 tree asm_specification;
14776
14777 /* Peek at the next token. */
14778 token = cp_lexer_peek_token (parser->lexer);
14779 /* If the next token isn't the `asm' keyword, then there's no
14780 asm-specification. */
14781 if (!cp_parser_is_keyword (token, RID_ASM))
14782 return NULL_TREE;
14783
14784 /* Consume the `asm' token. */
14785 cp_lexer_consume_token (parser->lexer);
14786 /* Look for the `('. */
14787 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14788
14789 /* Look for the string-literal. */
14790 asm_specification = cp_parser_string_literal (parser, false, false);
14791
14792 /* Look for the `)'. */
14793 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14794
14795 return asm_specification;
14796 }
14797
14798 /* Parse an asm-operand-list.
14799
14800 asm-operand-list:
14801 asm-operand
14802 asm-operand-list , asm-operand
14803
14804 asm-operand:
14805 string-literal ( expression )
14806 [ string-literal ] string-literal ( expression )
14807
14808 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14809 each node is the expression. The TREE_PURPOSE is itself a
14810 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14811 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14812 is a STRING_CST for the string literal before the parenthesis. */
14813
14814 static tree
14815 cp_parser_asm_operand_list (cp_parser* parser)
14816 {
14817 tree asm_operands = NULL_TREE;
14818
14819 while (true)
14820 {
14821 tree string_literal;
14822 tree expression;
14823 tree name;
14824
14825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14826 {
14827 /* Consume the `[' token. */
14828 cp_lexer_consume_token (parser->lexer);
14829 /* Read the operand name. */
14830 name = cp_parser_identifier (parser);
14831 if (name != error_mark_node)
14832 name = build_string (IDENTIFIER_LENGTH (name),
14833 IDENTIFIER_POINTER (name));
14834 /* Look for the closing `]'. */
14835 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14836 }
14837 else
14838 name = NULL_TREE;
14839 /* Look for the string-literal. */
14840 string_literal = cp_parser_string_literal (parser, false, false);
14841
14842 /* Look for the `('. */
14843 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14844 /* Parse the expression. */
14845 expression = cp_parser_expression (parser, /*cast_p=*/false);
14846 /* Look for the `)'. */
14847 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14848
14849 /* Add this operand to the list. */
14850 asm_operands = tree_cons (build_tree_list (name, string_literal),
14851 expression,
14852 asm_operands);
14853 /* If the next token is not a `,', there are no more
14854 operands. */
14855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14856 break;
14857 /* Consume the `,'. */
14858 cp_lexer_consume_token (parser->lexer);
14859 }
14860
14861 return nreverse (asm_operands);
14862 }
14863
14864 /* Parse an asm-clobber-list.
14865
14866 asm-clobber-list:
14867 string-literal
14868 asm-clobber-list , string-literal
14869
14870 Returns a TREE_LIST, indicating the clobbers in the order that they
14871 appeared. The TREE_VALUE of each node is a STRING_CST. */
14872
14873 static tree
14874 cp_parser_asm_clobber_list (cp_parser* parser)
14875 {
14876 tree clobbers = NULL_TREE;
14877
14878 while (true)
14879 {
14880 tree string_literal;
14881
14882 /* Look for the string literal. */
14883 string_literal = cp_parser_string_literal (parser, false, false);
14884 /* Add it to the list. */
14885 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14886 /* If the next token is not a `,', then the list is
14887 complete. */
14888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14889 break;
14890 /* Consume the `,' token. */
14891 cp_lexer_consume_token (parser->lexer);
14892 }
14893
14894 return clobbers;
14895 }
14896
14897 /* Parse an (optional) series of attributes.
14898
14899 attributes:
14900 attributes attribute
14901
14902 attribute:
14903 __attribute__ (( attribute-list [opt] ))
14904
14905 The return value is as for cp_parser_attribute_list. */
14906
14907 static tree
14908 cp_parser_attributes_opt (cp_parser* parser)
14909 {
14910 tree attributes = NULL_TREE;
14911
14912 while (true)
14913 {
14914 cp_token *token;
14915 tree attribute_list;
14916
14917 /* Peek at the next token. */
14918 token = cp_lexer_peek_token (parser->lexer);
14919 /* If it's not `__attribute__', then we're done. */
14920 if (token->keyword != RID_ATTRIBUTE)
14921 break;
14922
14923 /* Consume the `__attribute__' keyword. */
14924 cp_lexer_consume_token (parser->lexer);
14925 /* Look for the two `(' tokens. */
14926 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14927 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14928
14929 /* Peek at the next token. */
14930 token = cp_lexer_peek_token (parser->lexer);
14931 if (token->type != CPP_CLOSE_PAREN)
14932 /* Parse the attribute-list. */
14933 attribute_list = cp_parser_attribute_list (parser);
14934 else
14935 /* If the next token is a `)', then there is no attribute
14936 list. */
14937 attribute_list = NULL;
14938
14939 /* Look for the two `)' tokens. */
14940 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14941 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14942
14943 /* Add these new attributes to the list. */
14944 attributes = chainon (attributes, attribute_list);
14945 }
14946
14947 return attributes;
14948 }
14949
14950 /* Parse an attribute-list.
14951
14952 attribute-list:
14953 attribute
14954 attribute-list , attribute
14955
14956 attribute:
14957 identifier
14958 identifier ( identifier )
14959 identifier ( identifier , expression-list )
14960 identifier ( expression-list )
14961
14962 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14963 to an attribute. The TREE_PURPOSE of each node is the identifier
14964 indicating which attribute is in use. The TREE_VALUE represents
14965 the arguments, if any. */
14966
14967 static tree
14968 cp_parser_attribute_list (cp_parser* parser)
14969 {
14970 tree attribute_list = NULL_TREE;
14971 bool save_translate_strings_p = parser->translate_strings_p;
14972
14973 parser->translate_strings_p = false;
14974 while (true)
14975 {
14976 cp_token *token;
14977 tree identifier;
14978 tree attribute;
14979
14980 /* Look for the identifier. We also allow keywords here; for
14981 example `__attribute__ ((const))' is legal. */
14982 token = cp_lexer_peek_token (parser->lexer);
14983 if (token->type == CPP_NAME
14984 || token->type == CPP_KEYWORD)
14985 {
14986 tree arguments = NULL_TREE;
14987
14988 /* Consume the token. */
14989 token = cp_lexer_consume_token (parser->lexer);
14990
14991 /* Save away the identifier that indicates which attribute
14992 this is. */
14993 identifier = token->u.value;
14994 attribute = build_tree_list (identifier, NULL_TREE);
14995
14996 /* Peek at the next token. */
14997 token = cp_lexer_peek_token (parser->lexer);
14998 /* If it's an `(', then parse the attribute arguments. */
14999 if (token->type == CPP_OPEN_PAREN)
15000 {
15001 arguments = cp_parser_parenthesized_expression_list
15002 (parser, true, /*cast_p=*/false,
15003 /*non_constant_p=*/NULL);
15004 /* Save the arguments away. */
15005 TREE_VALUE (attribute) = arguments;
15006 }
15007
15008 if (arguments != error_mark_node)
15009 {
15010 /* Add this attribute to the list. */
15011 TREE_CHAIN (attribute) = attribute_list;
15012 attribute_list = attribute;
15013 }
15014
15015 token = cp_lexer_peek_token (parser->lexer);
15016 }
15017 /* Now, look for more attributes. If the next token isn't a
15018 `,', we're done. */
15019 if (token->type != CPP_COMMA)
15020 break;
15021
15022 /* Consume the comma and keep going. */
15023 cp_lexer_consume_token (parser->lexer);
15024 }
15025 parser->translate_strings_p = save_translate_strings_p;
15026
15027 /* We built up the list in reverse order. */
15028 return nreverse (attribute_list);
15029 }
15030
15031 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15032 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15033 current value of the PEDANTIC flag, regardless of whether or not
15034 the `__extension__' keyword is present. The caller is responsible
15035 for restoring the value of the PEDANTIC flag. */
15036
15037 static bool
15038 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15039 {
15040 /* Save the old value of the PEDANTIC flag. */
15041 *saved_pedantic = pedantic;
15042
15043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15044 {
15045 /* Consume the `__extension__' token. */
15046 cp_lexer_consume_token (parser->lexer);
15047 /* We're not being pedantic while the `__extension__' keyword is
15048 in effect. */
15049 pedantic = 0;
15050
15051 return true;
15052 }
15053
15054 return false;
15055 }
15056
15057 /* Parse a label declaration.
15058
15059 label-declaration:
15060 __label__ label-declarator-seq ;
15061
15062 label-declarator-seq:
15063 identifier , label-declarator-seq
15064 identifier */
15065
15066 static void
15067 cp_parser_label_declaration (cp_parser* parser)
15068 {
15069 /* Look for the `__label__' keyword. */
15070 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15071
15072 while (true)
15073 {
15074 tree identifier;
15075
15076 /* Look for an identifier. */
15077 identifier = cp_parser_identifier (parser);
15078 /* If we failed, stop. */
15079 if (identifier == error_mark_node)
15080 break;
15081 /* Declare it as a label. */
15082 finish_label_decl (identifier);
15083 /* If the next token is a `;', stop. */
15084 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15085 break;
15086 /* Look for the `,' separating the label declarations. */
15087 cp_parser_require (parser, CPP_COMMA, "`,'");
15088 }
15089
15090 /* Look for the final `;'. */
15091 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15092 }
15093
15094 /* Support Functions */
15095
15096 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15097 NAME should have one of the representations used for an
15098 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15099 is returned. If PARSER->SCOPE is a dependent type, then a
15100 SCOPE_REF is returned.
15101
15102 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15103 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15104 was formed. Abstractly, such entities should not be passed to this
15105 function, because they do not need to be looked up, but it is
15106 simpler to check for this special case here, rather than at the
15107 call-sites.
15108
15109 In cases not explicitly covered above, this function returns a
15110 DECL, OVERLOAD, or baselink representing the result of the lookup.
15111 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15112 is returned.
15113
15114 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15115 (e.g., "struct") that was used. In that case bindings that do not
15116 refer to types are ignored.
15117
15118 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15119 ignored.
15120
15121 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15122 are ignored.
15123
15124 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15125 types.
15126
15127 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15128 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15129 NULL_TREE otherwise. */
15130
15131 static tree
15132 cp_parser_lookup_name (cp_parser *parser, tree name,
15133 enum tag_types tag_type,
15134 bool is_template,
15135 bool is_namespace,
15136 bool check_dependency,
15137 tree *ambiguous_decls)
15138 {
15139 int flags = 0;
15140 tree decl;
15141 tree object_type = parser->context->object_type;
15142
15143 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15144 flags |= LOOKUP_COMPLAIN;
15145
15146 /* Assume that the lookup will be unambiguous. */
15147 if (ambiguous_decls)
15148 *ambiguous_decls = NULL_TREE;
15149
15150 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15151 no longer valid. Note that if we are parsing tentatively, and
15152 the parse fails, OBJECT_TYPE will be automatically restored. */
15153 parser->context->object_type = NULL_TREE;
15154
15155 if (name == error_mark_node)
15156 return error_mark_node;
15157
15158 /* A template-id has already been resolved; there is no lookup to
15159 do. */
15160 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15161 return name;
15162 if (BASELINK_P (name))
15163 {
15164 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15165 == TEMPLATE_ID_EXPR);
15166 return name;
15167 }
15168
15169 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15170 it should already have been checked to make sure that the name
15171 used matches the type being destroyed. */
15172 if (TREE_CODE (name) == BIT_NOT_EXPR)
15173 {
15174 tree type;
15175
15176 /* Figure out to which type this destructor applies. */
15177 if (parser->scope)
15178 type = parser->scope;
15179 else if (object_type)
15180 type = object_type;
15181 else
15182 type = current_class_type;
15183 /* If that's not a class type, there is no destructor. */
15184 if (!type || !CLASS_TYPE_P (type))
15185 return error_mark_node;
15186 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15187 lazily_declare_fn (sfk_destructor, type);
15188 if (!CLASSTYPE_DESTRUCTORS (type))
15189 return error_mark_node;
15190 /* If it was a class type, return the destructor. */
15191 return CLASSTYPE_DESTRUCTORS (type);
15192 }
15193
15194 /* By this point, the NAME should be an ordinary identifier. If
15195 the id-expression was a qualified name, the qualifying scope is
15196 stored in PARSER->SCOPE at this point. */
15197 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15198
15199 /* Perform the lookup. */
15200 if (parser->scope)
15201 {
15202 bool dependent_p;
15203
15204 if (parser->scope == error_mark_node)
15205 return error_mark_node;
15206
15207 /* If the SCOPE is dependent, the lookup must be deferred until
15208 the template is instantiated -- unless we are explicitly
15209 looking up names in uninstantiated templates. Even then, we
15210 cannot look up the name if the scope is not a class type; it
15211 might, for example, be a template type parameter. */
15212 dependent_p = (TYPE_P (parser->scope)
15213 && !(parser->in_declarator_p
15214 && currently_open_class (parser->scope))
15215 && dependent_type_p (parser->scope));
15216 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15217 && dependent_p)
15218 {
15219 if (tag_type)
15220 {
15221 tree type;
15222
15223 /* The resolution to Core Issue 180 says that `struct
15224 A::B' should be considered a type-name, even if `A'
15225 is dependent. */
15226 type = make_typename_type (parser->scope, name, tag_type,
15227 /*complain=*/tf_error);
15228 decl = TYPE_NAME (type);
15229 }
15230 else if (is_template
15231 && (cp_parser_next_token_ends_template_argument_p (parser)
15232 || cp_lexer_next_token_is (parser->lexer,
15233 CPP_CLOSE_PAREN)))
15234 decl = make_unbound_class_template (parser->scope,
15235 name, NULL_TREE,
15236 /*complain=*/tf_error);
15237 else
15238 decl = build_qualified_name (/*type=*/NULL_TREE,
15239 parser->scope, name,
15240 is_template);
15241 }
15242 else
15243 {
15244 tree pushed_scope = NULL_TREE;
15245
15246 /* If PARSER->SCOPE is a dependent type, then it must be a
15247 class type, and we must not be checking dependencies;
15248 otherwise, we would have processed this lookup above. So
15249 that PARSER->SCOPE is not considered a dependent base by
15250 lookup_member, we must enter the scope here. */
15251 if (dependent_p)
15252 pushed_scope = push_scope (parser->scope);
15253 /* If the PARSER->SCOPE is a template specialization, it
15254 may be instantiated during name lookup. In that case,
15255 errors may be issued. Even if we rollback the current
15256 tentative parse, those errors are valid. */
15257 decl = lookup_qualified_name (parser->scope, name,
15258 tag_type != none_type,
15259 /*complain=*/true);
15260 if (pushed_scope)
15261 pop_scope (pushed_scope);
15262 }
15263 parser->qualifying_scope = parser->scope;
15264 parser->object_scope = NULL_TREE;
15265 }
15266 else if (object_type)
15267 {
15268 tree object_decl = NULL_TREE;
15269 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15270 OBJECT_TYPE is not a class. */
15271 if (CLASS_TYPE_P (object_type))
15272 /* If the OBJECT_TYPE is a template specialization, it may
15273 be instantiated during name lookup. In that case, errors
15274 may be issued. Even if we rollback the current tentative
15275 parse, those errors are valid. */
15276 object_decl = lookup_member (object_type,
15277 name,
15278 /*protect=*/0,
15279 tag_type != none_type);
15280 /* Look it up in the enclosing context, too. */
15281 decl = lookup_name_real (name, tag_type != none_type,
15282 /*nonclass=*/0,
15283 /*block_p=*/true, is_namespace, flags);
15284 parser->object_scope = object_type;
15285 parser->qualifying_scope = NULL_TREE;
15286 if (object_decl)
15287 decl = object_decl;
15288 }
15289 else
15290 {
15291 decl = lookup_name_real (name, tag_type != none_type,
15292 /*nonclass=*/0,
15293 /*block_p=*/true, is_namespace, flags);
15294 parser->qualifying_scope = NULL_TREE;
15295 parser->object_scope = NULL_TREE;
15296 }
15297
15298 /* If the lookup failed, let our caller know. */
15299 if (!decl || decl == error_mark_node)
15300 return error_mark_node;
15301
15302 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15303 if (TREE_CODE (decl) == TREE_LIST)
15304 {
15305 if (ambiguous_decls)
15306 *ambiguous_decls = decl;
15307 /* The error message we have to print is too complicated for
15308 cp_parser_error, so we incorporate its actions directly. */
15309 if (!cp_parser_simulate_error (parser))
15310 {
15311 error ("reference to %qD is ambiguous", name);
15312 print_candidates (decl);
15313 }
15314 return error_mark_node;
15315 }
15316
15317 gcc_assert (DECL_P (decl)
15318 || TREE_CODE (decl) == OVERLOAD
15319 || TREE_CODE (decl) == SCOPE_REF
15320 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15321 || BASELINK_P (decl));
15322
15323 /* If we have resolved the name of a member declaration, check to
15324 see if the declaration is accessible. When the name resolves to
15325 set of overloaded functions, accessibility is checked when
15326 overload resolution is done.
15327
15328 During an explicit instantiation, access is not checked at all,
15329 as per [temp.explicit]. */
15330 if (DECL_P (decl))
15331 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15332
15333 return decl;
15334 }
15335
15336 /* Like cp_parser_lookup_name, but for use in the typical case where
15337 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15338 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15339
15340 static tree
15341 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15342 {
15343 return cp_parser_lookup_name (parser, name,
15344 none_type,
15345 /*is_template=*/false,
15346 /*is_namespace=*/false,
15347 /*check_dependency=*/true,
15348 /*ambiguous_decls=*/NULL);
15349 }
15350
15351 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15352 the current context, return the TYPE_DECL. If TAG_NAME_P is
15353 true, the DECL indicates the class being defined in a class-head,
15354 or declared in an elaborated-type-specifier.
15355
15356 Otherwise, return DECL. */
15357
15358 static tree
15359 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15360 {
15361 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15362 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15363
15364 struct A {
15365 template <typename T> struct B;
15366 };
15367
15368 template <typename T> struct A::B {};
15369
15370 Similarly, in an elaborated-type-specifier:
15371
15372 namespace N { struct X{}; }
15373
15374 struct A {
15375 template <typename T> friend struct N::X;
15376 };
15377
15378 However, if the DECL refers to a class type, and we are in
15379 the scope of the class, then the name lookup automatically
15380 finds the TYPE_DECL created by build_self_reference rather
15381 than a TEMPLATE_DECL. For example, in:
15382
15383 template <class T> struct S {
15384 S s;
15385 };
15386
15387 there is no need to handle such case. */
15388
15389 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15390 return DECL_TEMPLATE_RESULT (decl);
15391
15392 return decl;
15393 }
15394
15395 /* If too many, or too few, template-parameter lists apply to the
15396 declarator, issue an error message. Returns TRUE if all went well,
15397 and FALSE otherwise. */
15398
15399 static bool
15400 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15401 cp_declarator *declarator)
15402 {
15403 unsigned num_templates;
15404
15405 /* We haven't seen any classes that involve template parameters yet. */
15406 num_templates = 0;
15407
15408 switch (declarator->kind)
15409 {
15410 case cdk_id:
15411 if (declarator->u.id.qualifying_scope)
15412 {
15413 tree scope;
15414 tree member;
15415
15416 scope = declarator->u.id.qualifying_scope;
15417 member = declarator->u.id.unqualified_name;
15418
15419 while (scope && CLASS_TYPE_P (scope))
15420 {
15421 /* You're supposed to have one `template <...>'
15422 for every template class, but you don't need one
15423 for a full specialization. For example:
15424
15425 template <class T> struct S{};
15426 template <> struct S<int> { void f(); };
15427 void S<int>::f () {}
15428
15429 is correct; there shouldn't be a `template <>' for
15430 the definition of `S<int>::f'. */
15431 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15432 /* If SCOPE does not have template information of any
15433 kind, then it is not a template, nor is it nested
15434 within a template. */
15435 break;
15436 if (explicit_class_specialization_p (scope))
15437 break;
15438 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15439 ++num_templates;
15440
15441 scope = TYPE_CONTEXT (scope);
15442 }
15443 }
15444 else if (TREE_CODE (declarator->u.id.unqualified_name)
15445 == TEMPLATE_ID_EXPR)
15446 /* If the DECLARATOR has the form `X<y>' then it uses one
15447 additional level of template parameters. */
15448 ++num_templates;
15449
15450 return cp_parser_check_template_parameters (parser,
15451 num_templates);
15452
15453 case cdk_function:
15454 case cdk_array:
15455 case cdk_pointer:
15456 case cdk_reference:
15457 case cdk_ptrmem:
15458 return (cp_parser_check_declarator_template_parameters
15459 (parser, declarator->declarator));
15460
15461 case cdk_error:
15462 return true;
15463
15464 default:
15465 gcc_unreachable ();
15466 }
15467 return false;
15468 }
15469
15470 /* NUM_TEMPLATES were used in the current declaration. If that is
15471 invalid, return FALSE and issue an error messages. Otherwise,
15472 return TRUE. */
15473
15474 static bool
15475 cp_parser_check_template_parameters (cp_parser* parser,
15476 unsigned num_templates)
15477 {
15478 /* If there are more template classes than parameter lists, we have
15479 something like:
15480
15481 template <class T> void S<T>::R<T>::f (); */
15482 if (parser->num_template_parameter_lists < num_templates)
15483 {
15484 error ("too few template-parameter-lists");
15485 return false;
15486 }
15487 /* If there are the same number of template classes and parameter
15488 lists, that's OK. */
15489 if (parser->num_template_parameter_lists == num_templates)
15490 return true;
15491 /* If there are more, but only one more, then we are referring to a
15492 member template. That's OK too. */
15493 if (parser->num_template_parameter_lists == num_templates + 1)
15494 return true;
15495 /* Otherwise, there are too many template parameter lists. We have
15496 something like:
15497
15498 template <class T> template <class U> void S::f(); */
15499 error ("too many template-parameter-lists");
15500 return false;
15501 }
15502
15503 /* Parse an optional `::' token indicating that the following name is
15504 from the global namespace. If so, PARSER->SCOPE is set to the
15505 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15506 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15507 Returns the new value of PARSER->SCOPE, if the `::' token is
15508 present, and NULL_TREE otherwise. */
15509
15510 static tree
15511 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15512 {
15513 cp_token *token;
15514
15515 /* Peek at the next token. */
15516 token = cp_lexer_peek_token (parser->lexer);
15517 /* If we're looking at a `::' token then we're starting from the
15518 global namespace, not our current location. */
15519 if (token->type == CPP_SCOPE)
15520 {
15521 /* Consume the `::' token. */
15522 cp_lexer_consume_token (parser->lexer);
15523 /* Set the SCOPE so that we know where to start the lookup. */
15524 parser->scope = global_namespace;
15525 parser->qualifying_scope = global_namespace;
15526 parser->object_scope = NULL_TREE;
15527
15528 return parser->scope;
15529 }
15530 else if (!current_scope_valid_p)
15531 {
15532 parser->scope = NULL_TREE;
15533 parser->qualifying_scope = NULL_TREE;
15534 parser->object_scope = NULL_TREE;
15535 }
15536
15537 return NULL_TREE;
15538 }
15539
15540 /* Returns TRUE if the upcoming token sequence is the start of a
15541 constructor declarator. If FRIEND_P is true, the declarator is
15542 preceded by the `friend' specifier. */
15543
15544 static bool
15545 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15546 {
15547 bool constructor_p;
15548 tree type_decl = NULL_TREE;
15549 bool nested_name_p;
15550 cp_token *next_token;
15551
15552 /* The common case is that this is not a constructor declarator, so
15553 try to avoid doing lots of work if at all possible. It's not
15554 valid declare a constructor at function scope. */
15555 if (parser->in_function_body)
15556 return false;
15557 /* And only certain tokens can begin a constructor declarator. */
15558 next_token = cp_lexer_peek_token (parser->lexer);
15559 if (next_token->type != CPP_NAME
15560 && next_token->type != CPP_SCOPE
15561 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15562 && next_token->type != CPP_TEMPLATE_ID)
15563 return false;
15564
15565 /* Parse tentatively; we are going to roll back all of the tokens
15566 consumed here. */
15567 cp_parser_parse_tentatively (parser);
15568 /* Assume that we are looking at a constructor declarator. */
15569 constructor_p = true;
15570
15571 /* Look for the optional `::' operator. */
15572 cp_parser_global_scope_opt (parser,
15573 /*current_scope_valid_p=*/false);
15574 /* Look for the nested-name-specifier. */
15575 nested_name_p
15576 = (cp_parser_nested_name_specifier_opt (parser,
15577 /*typename_keyword_p=*/false,
15578 /*check_dependency_p=*/false,
15579 /*type_p=*/false,
15580 /*is_declaration=*/false)
15581 != NULL_TREE);
15582 /* Outside of a class-specifier, there must be a
15583 nested-name-specifier. */
15584 if (!nested_name_p &&
15585 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15586 || friend_p))
15587 constructor_p = false;
15588 /* If we still think that this might be a constructor-declarator,
15589 look for a class-name. */
15590 if (constructor_p)
15591 {
15592 /* If we have:
15593
15594 template <typename T> struct S { S(); };
15595 template <typename T> S<T>::S ();
15596
15597 we must recognize that the nested `S' names a class.
15598 Similarly, for:
15599
15600 template <typename T> S<T>::S<T> ();
15601
15602 we must recognize that the nested `S' names a template. */
15603 type_decl = cp_parser_class_name (parser,
15604 /*typename_keyword_p=*/false,
15605 /*template_keyword_p=*/false,
15606 none_type,
15607 /*check_dependency_p=*/false,
15608 /*class_head_p=*/false,
15609 /*is_declaration=*/false);
15610 /* If there was no class-name, then this is not a constructor. */
15611 constructor_p = !cp_parser_error_occurred (parser);
15612 }
15613
15614 /* If we're still considering a constructor, we have to see a `(',
15615 to begin the parameter-declaration-clause, followed by either a
15616 `)', an `...', or a decl-specifier. We need to check for a
15617 type-specifier to avoid being fooled into thinking that:
15618
15619 S::S (f) (int);
15620
15621 is a constructor. (It is actually a function named `f' that
15622 takes one parameter (of type `int') and returns a value of type
15623 `S::S'. */
15624 if (constructor_p
15625 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15626 {
15627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15628 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15629 /* A parameter declaration begins with a decl-specifier,
15630 which is either the "attribute" keyword, a storage class
15631 specifier, or (usually) a type-specifier. */
15632 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15633 {
15634 tree type;
15635 tree pushed_scope = NULL_TREE;
15636 unsigned saved_num_template_parameter_lists;
15637
15638 /* Names appearing in the type-specifier should be looked up
15639 in the scope of the class. */
15640 if (current_class_type)
15641 type = NULL_TREE;
15642 else
15643 {
15644 type = TREE_TYPE (type_decl);
15645 if (TREE_CODE (type) == TYPENAME_TYPE)
15646 {
15647 type = resolve_typename_type (type,
15648 /*only_current_p=*/false);
15649 if (type == error_mark_node)
15650 {
15651 cp_parser_abort_tentative_parse (parser);
15652 return false;
15653 }
15654 }
15655 pushed_scope = push_scope (type);
15656 }
15657
15658 /* Inside the constructor parameter list, surrounding
15659 template-parameter-lists do not apply. */
15660 saved_num_template_parameter_lists
15661 = parser->num_template_parameter_lists;
15662 parser->num_template_parameter_lists = 0;
15663
15664 /* Look for the type-specifier. */
15665 cp_parser_type_specifier (parser,
15666 CP_PARSER_FLAGS_NONE,
15667 /*decl_specs=*/NULL,
15668 /*is_declarator=*/true,
15669 /*declares_class_or_enum=*/NULL,
15670 /*is_cv_qualifier=*/NULL);
15671
15672 parser->num_template_parameter_lists
15673 = saved_num_template_parameter_lists;
15674
15675 /* Leave the scope of the class. */
15676 if (pushed_scope)
15677 pop_scope (pushed_scope);
15678
15679 constructor_p = !cp_parser_error_occurred (parser);
15680 }
15681 }
15682 else
15683 constructor_p = false;
15684 /* We did not really want to consume any tokens. */
15685 cp_parser_abort_tentative_parse (parser);
15686
15687 return constructor_p;
15688 }
15689
15690 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15691 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15692 they must be performed once we are in the scope of the function.
15693
15694 Returns the function defined. */
15695
15696 static tree
15697 cp_parser_function_definition_from_specifiers_and_declarator
15698 (cp_parser* parser,
15699 cp_decl_specifier_seq *decl_specifiers,
15700 tree attributes,
15701 const cp_declarator *declarator)
15702 {
15703 tree fn;
15704 bool success_p;
15705
15706 /* Begin the function-definition. */
15707 success_p = start_function (decl_specifiers, declarator, attributes);
15708
15709 /* The things we're about to see are not directly qualified by any
15710 template headers we've seen thus far. */
15711 reset_specialization ();
15712
15713 /* If there were names looked up in the decl-specifier-seq that we
15714 did not check, check them now. We must wait until we are in the
15715 scope of the function to perform the checks, since the function
15716 might be a friend. */
15717 perform_deferred_access_checks ();
15718
15719 if (!success_p)
15720 {
15721 /* Skip the entire function. */
15722 cp_parser_skip_to_end_of_block_or_statement (parser);
15723 fn = error_mark_node;
15724 }
15725 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15726 {
15727 /* Seen already, skip it. An error message has already been output. */
15728 cp_parser_skip_to_end_of_block_or_statement (parser);
15729 fn = current_function_decl;
15730 current_function_decl = NULL_TREE;
15731 /* If this is a function from a class, pop the nested class. */
15732 if (current_class_name)
15733 pop_nested_class ();
15734 }
15735 else
15736 fn = cp_parser_function_definition_after_declarator (parser,
15737 /*inline_p=*/false);
15738
15739 return fn;
15740 }
15741
15742 /* Parse the part of a function-definition that follows the
15743 declarator. INLINE_P is TRUE iff this function is an inline
15744 function defined with a class-specifier.
15745
15746 Returns the function defined. */
15747
15748 static tree
15749 cp_parser_function_definition_after_declarator (cp_parser* parser,
15750 bool inline_p)
15751 {
15752 tree fn;
15753 bool ctor_initializer_p = false;
15754 bool saved_in_unbraced_linkage_specification_p;
15755 bool saved_in_function_body;
15756 unsigned saved_num_template_parameter_lists;
15757
15758 saved_in_function_body = parser->in_function_body;
15759 parser->in_function_body = true;
15760 /* If the next token is `return', then the code may be trying to
15761 make use of the "named return value" extension that G++ used to
15762 support. */
15763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15764 {
15765 /* Consume the `return' keyword. */
15766 cp_lexer_consume_token (parser->lexer);
15767 /* Look for the identifier that indicates what value is to be
15768 returned. */
15769 cp_parser_identifier (parser);
15770 /* Issue an error message. */
15771 error ("named return values are no longer supported");
15772 /* Skip tokens until we reach the start of the function body. */
15773 while (true)
15774 {
15775 cp_token *token = cp_lexer_peek_token (parser->lexer);
15776 if (token->type == CPP_OPEN_BRACE
15777 || token->type == CPP_EOF
15778 || token->type == CPP_PRAGMA_EOL)
15779 break;
15780 cp_lexer_consume_token (parser->lexer);
15781 }
15782 }
15783 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15784 anything declared inside `f'. */
15785 saved_in_unbraced_linkage_specification_p
15786 = parser->in_unbraced_linkage_specification_p;
15787 parser->in_unbraced_linkage_specification_p = false;
15788 /* Inside the function, surrounding template-parameter-lists do not
15789 apply. */
15790 saved_num_template_parameter_lists
15791 = parser->num_template_parameter_lists;
15792 parser->num_template_parameter_lists = 0;
15793 /* If the next token is `try', then we are looking at a
15794 function-try-block. */
15795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15796 ctor_initializer_p = cp_parser_function_try_block (parser);
15797 /* A function-try-block includes the function-body, so we only do
15798 this next part if we're not processing a function-try-block. */
15799 else
15800 ctor_initializer_p
15801 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15802
15803 /* Finish the function. */
15804 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15805 (inline_p ? 2 : 0));
15806 /* Generate code for it, if necessary. */
15807 expand_or_defer_fn (fn);
15808 /* Restore the saved values. */
15809 parser->in_unbraced_linkage_specification_p
15810 = saved_in_unbraced_linkage_specification_p;
15811 parser->num_template_parameter_lists
15812 = saved_num_template_parameter_lists;
15813 parser->in_function_body = saved_in_function_body;
15814
15815 return fn;
15816 }
15817
15818 /* Parse a template-declaration, assuming that the `export' (and
15819 `extern') keywords, if present, has already been scanned. MEMBER_P
15820 is as for cp_parser_template_declaration. */
15821
15822 static void
15823 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15824 {
15825 tree decl = NULL_TREE;
15826 VEC (deferred_access_check,gc) *checks;
15827 tree parameter_list;
15828 bool friend_p = false;
15829 bool need_lang_pop;
15830
15831 /* Look for the `template' keyword. */
15832 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15833 return;
15834
15835 /* And the `<'. */
15836 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15837 return;
15838 if (at_class_scope_p () && current_function_decl)
15839 {
15840 /* 14.5.2.2 [temp.mem]
15841
15842 A local class shall not have member templates. */
15843 error ("invalid declaration of member template in local class");
15844 cp_parser_skip_to_end_of_block_or_statement (parser);
15845 return;
15846 }
15847 /* [temp]
15848
15849 A template ... shall not have C linkage. */
15850 if (current_lang_name == lang_name_c)
15851 {
15852 error ("template with C linkage");
15853 /* Give it C++ linkage to avoid confusing other parts of the
15854 front end. */
15855 push_lang_context (lang_name_cplusplus);
15856 need_lang_pop = true;
15857 }
15858 else
15859 need_lang_pop = false;
15860
15861 /* We cannot perform access checks on the template parameter
15862 declarations until we know what is being declared, just as we
15863 cannot check the decl-specifier list. */
15864 push_deferring_access_checks (dk_deferred);
15865
15866 /* If the next token is `>', then we have an invalid
15867 specialization. Rather than complain about an invalid template
15868 parameter, issue an error message here. */
15869 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15870 {
15871 cp_parser_error (parser, "invalid explicit specialization");
15872 begin_specialization ();
15873 parameter_list = NULL_TREE;
15874 }
15875 else
15876 /* Parse the template parameters. */
15877 parameter_list = cp_parser_template_parameter_list (parser);
15878
15879 /* Get the deferred access checks from the parameter list. These
15880 will be checked once we know what is being declared, as for a
15881 member template the checks must be performed in the scope of the
15882 class containing the member. */
15883 checks = get_deferred_access_checks ();
15884
15885 /* Look for the `>'. */
15886 cp_parser_skip_to_end_of_template_parameter_list (parser);
15887 /* We just processed one more parameter list. */
15888 ++parser->num_template_parameter_lists;
15889 /* If the next token is `template', there are more template
15890 parameters. */
15891 if (cp_lexer_next_token_is_keyword (parser->lexer,
15892 RID_TEMPLATE))
15893 cp_parser_template_declaration_after_export (parser, member_p);
15894 else
15895 {
15896 /* There are no access checks when parsing a template, as we do not
15897 know if a specialization will be a friend. */
15898 push_deferring_access_checks (dk_no_check);
15899 decl = cp_parser_single_declaration (parser,
15900 checks,
15901 member_p,
15902 &friend_p);
15903 pop_deferring_access_checks ();
15904
15905 /* If this is a member template declaration, let the front
15906 end know. */
15907 if (member_p && !friend_p && decl)
15908 {
15909 if (TREE_CODE (decl) == TYPE_DECL)
15910 cp_parser_check_access_in_redeclaration (decl);
15911
15912 decl = finish_member_template_decl (decl);
15913 }
15914 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15915 make_friend_class (current_class_type, TREE_TYPE (decl),
15916 /*complain=*/true);
15917 }
15918 /* We are done with the current parameter list. */
15919 --parser->num_template_parameter_lists;
15920
15921 pop_deferring_access_checks ();
15922
15923 /* Finish up. */
15924 finish_template_decl (parameter_list);
15925
15926 /* Register member declarations. */
15927 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15928 finish_member_declaration (decl);
15929 /* For the erroneous case of a template with C linkage, we pushed an
15930 implicit C++ linkage scope; exit that scope now. */
15931 if (need_lang_pop)
15932 pop_lang_context ();
15933 /* If DECL is a function template, we must return to parse it later.
15934 (Even though there is no definition, there might be default
15935 arguments that need handling.) */
15936 if (member_p && decl
15937 && (TREE_CODE (decl) == FUNCTION_DECL
15938 || DECL_FUNCTION_TEMPLATE_P (decl)))
15939 TREE_VALUE (parser->unparsed_functions_queues)
15940 = tree_cons (NULL_TREE, decl,
15941 TREE_VALUE (parser->unparsed_functions_queues));
15942 }
15943
15944 /* Perform the deferred access checks from a template-parameter-list.
15945 CHECKS is a TREE_LIST of access checks, as returned by
15946 get_deferred_access_checks. */
15947
15948 static void
15949 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15950 {
15951 ++processing_template_parmlist;
15952 perform_access_checks (checks);
15953 --processing_template_parmlist;
15954 }
15955
15956 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15957 `function-definition' sequence. MEMBER_P is true, this declaration
15958 appears in a class scope.
15959
15960 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15961 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15962
15963 static tree
15964 cp_parser_single_declaration (cp_parser* parser,
15965 VEC (deferred_access_check,gc)* checks,
15966 bool member_p,
15967 bool* friend_p)
15968 {
15969 int declares_class_or_enum;
15970 tree decl = NULL_TREE;
15971 cp_decl_specifier_seq decl_specifiers;
15972 bool function_definition_p = false;
15973
15974 /* This function is only used when processing a template
15975 declaration. */
15976 gcc_assert (innermost_scope_kind () == sk_template_parms
15977 || innermost_scope_kind () == sk_template_spec);
15978
15979 /* Defer access checks until we know what is being declared. */
15980 push_deferring_access_checks (dk_deferred);
15981
15982 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15983 alternative. */
15984 cp_parser_decl_specifier_seq (parser,
15985 CP_PARSER_FLAGS_OPTIONAL,
15986 &decl_specifiers,
15987 &declares_class_or_enum);
15988 if (friend_p)
15989 *friend_p = cp_parser_friend_p (&decl_specifiers);
15990
15991 /* There are no template typedefs. */
15992 if (decl_specifiers.specs[(int) ds_typedef])
15993 {
15994 error ("template declaration of %qs", "typedef");
15995 decl = error_mark_node;
15996 }
15997
15998 /* Gather up the access checks that occurred the
15999 decl-specifier-seq. */
16000 stop_deferring_access_checks ();
16001
16002 /* Check for the declaration of a template class. */
16003 if (declares_class_or_enum)
16004 {
16005 if (cp_parser_declares_only_class_p (parser))
16006 {
16007 decl = shadow_tag (&decl_specifiers);
16008
16009 /* In this case:
16010
16011 struct C {
16012 friend template <typename T> struct A<T>::B;
16013 };
16014
16015 A<T>::B will be represented by a TYPENAME_TYPE, and
16016 therefore not recognized by shadow_tag. */
16017 if (friend_p && *friend_p
16018 && !decl
16019 && decl_specifiers.type
16020 && TYPE_P (decl_specifiers.type))
16021 decl = decl_specifiers.type;
16022
16023 if (decl && decl != error_mark_node)
16024 decl = TYPE_NAME (decl);
16025 else
16026 decl = error_mark_node;
16027
16028 /* Perform access checks for template parameters. */
16029 cp_parser_perform_template_parameter_access_checks (checks);
16030 }
16031 }
16032 /* If it's not a template class, try for a template function. If
16033 the next token is a `;', then this declaration does not declare
16034 anything. But, if there were errors in the decl-specifiers, then
16035 the error might well have come from an attempted class-specifier.
16036 In that case, there's no need to warn about a missing declarator. */
16037 if (!decl
16038 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16039 || decl_specifiers.type != error_mark_node))
16040 decl = cp_parser_init_declarator (parser,
16041 &decl_specifiers,
16042 checks,
16043 /*function_definition_allowed_p=*/true,
16044 member_p,
16045 declares_class_or_enum,
16046 &function_definition_p);
16047
16048 pop_deferring_access_checks ();
16049
16050 /* Clear any current qualification; whatever comes next is the start
16051 of something new. */
16052 parser->scope = NULL_TREE;
16053 parser->qualifying_scope = NULL_TREE;
16054 parser->object_scope = NULL_TREE;
16055 /* Look for a trailing `;' after the declaration. */
16056 if (!function_definition_p
16057 && (decl == error_mark_node
16058 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16059 cp_parser_skip_to_end_of_block_or_statement (parser);
16060
16061 return decl;
16062 }
16063
16064 /* Parse a cast-expression that is not the operand of a unary "&". */
16065
16066 static tree
16067 cp_parser_simple_cast_expression (cp_parser *parser)
16068 {
16069 return cp_parser_cast_expression (parser, /*address_p=*/false,
16070 /*cast_p=*/false);
16071 }
16072
16073 /* Parse a functional cast to TYPE. Returns an expression
16074 representing the cast. */
16075
16076 static tree
16077 cp_parser_functional_cast (cp_parser* parser, tree type)
16078 {
16079 tree expression_list;
16080 tree cast;
16081
16082 expression_list
16083 = cp_parser_parenthesized_expression_list (parser, false,
16084 /*cast_p=*/true,
16085 /*non_constant_p=*/NULL);
16086
16087 cast = build_functional_cast (type, expression_list);
16088 /* [expr.const]/1: In an integral constant expression "only type
16089 conversions to integral or enumeration type can be used". */
16090 if (TREE_CODE (type) == TYPE_DECL)
16091 type = TREE_TYPE (type);
16092 if (cast != error_mark_node
16093 && !cast_valid_in_integral_constant_expression_p (type)
16094 && (cp_parser_non_integral_constant_expression
16095 (parser, "a call to a constructor")))
16096 return error_mark_node;
16097 return cast;
16098 }
16099
16100 /* Save the tokens that make up the body of a member function defined
16101 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16102 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16103 specifiers applied to the declaration. Returns the FUNCTION_DECL
16104 for the member function. */
16105
16106 static tree
16107 cp_parser_save_member_function_body (cp_parser* parser,
16108 cp_decl_specifier_seq *decl_specifiers,
16109 cp_declarator *declarator,
16110 tree attributes)
16111 {
16112 cp_token *first;
16113 cp_token *last;
16114 tree fn;
16115
16116 /* Create the function-declaration. */
16117 fn = start_method (decl_specifiers, declarator, attributes);
16118 /* If something went badly wrong, bail out now. */
16119 if (fn == error_mark_node)
16120 {
16121 /* If there's a function-body, skip it. */
16122 if (cp_parser_token_starts_function_definition_p
16123 (cp_lexer_peek_token (parser->lexer)))
16124 cp_parser_skip_to_end_of_block_or_statement (parser);
16125 return error_mark_node;
16126 }
16127
16128 /* Remember it, if there default args to post process. */
16129 cp_parser_save_default_args (parser, fn);
16130
16131 /* Save away the tokens that make up the body of the
16132 function. */
16133 first = parser->lexer->next_token;
16134 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16135 /* Handle function try blocks. */
16136 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16137 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16138 last = parser->lexer->next_token;
16139
16140 /* Save away the inline definition; we will process it when the
16141 class is complete. */
16142 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16143 DECL_PENDING_INLINE_P (fn) = 1;
16144
16145 /* We need to know that this was defined in the class, so that
16146 friend templates are handled correctly. */
16147 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16148
16149 /* We're done with the inline definition. */
16150 finish_method (fn);
16151
16152 /* Add FN to the queue of functions to be parsed later. */
16153 TREE_VALUE (parser->unparsed_functions_queues)
16154 = tree_cons (NULL_TREE, fn,
16155 TREE_VALUE (parser->unparsed_functions_queues));
16156
16157 return fn;
16158 }
16159
16160 /* Parse a template-argument-list, as well as the trailing ">" (but
16161 not the opening ">"). See cp_parser_template_argument_list for the
16162 return value. */
16163
16164 static tree
16165 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16166 {
16167 tree arguments;
16168 tree saved_scope;
16169 tree saved_qualifying_scope;
16170 tree saved_object_scope;
16171 bool saved_greater_than_is_operator_p;
16172 bool saved_skip_evaluation;
16173
16174 /* [temp.names]
16175
16176 When parsing a template-id, the first non-nested `>' is taken as
16177 the end of the template-argument-list rather than a greater-than
16178 operator. */
16179 saved_greater_than_is_operator_p
16180 = parser->greater_than_is_operator_p;
16181 parser->greater_than_is_operator_p = false;
16182 /* Parsing the argument list may modify SCOPE, so we save it
16183 here. */
16184 saved_scope = parser->scope;
16185 saved_qualifying_scope = parser->qualifying_scope;
16186 saved_object_scope = parser->object_scope;
16187 /* We need to evaluate the template arguments, even though this
16188 template-id may be nested within a "sizeof". */
16189 saved_skip_evaluation = skip_evaluation;
16190 skip_evaluation = false;
16191 /* Parse the template-argument-list itself. */
16192 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16193 arguments = NULL_TREE;
16194 else
16195 arguments = cp_parser_template_argument_list (parser);
16196 /* Look for the `>' that ends the template-argument-list. If we find
16197 a '>>' instead, it's probably just a typo. */
16198 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16199 {
16200 if (!saved_greater_than_is_operator_p)
16201 {
16202 /* If we're in a nested template argument list, the '>>' has
16203 to be a typo for '> >'. We emit the error message, but we
16204 continue parsing and we push a '>' as next token, so that
16205 the argument list will be parsed correctly. Note that the
16206 global source location is still on the token before the
16207 '>>', so we need to say explicitly where we want it. */
16208 cp_token *token = cp_lexer_peek_token (parser->lexer);
16209 error ("%H%<>>%> should be %<> >%> "
16210 "within a nested template argument list",
16211 &token->location);
16212
16213 /* ??? Proper recovery should terminate two levels of
16214 template argument list here. */
16215 token->type = CPP_GREATER;
16216 }
16217 else
16218 {
16219 /* If this is not a nested template argument list, the '>>'
16220 is a typo for '>'. Emit an error message and continue.
16221 Same deal about the token location, but here we can get it
16222 right by consuming the '>>' before issuing the diagnostic. */
16223 cp_lexer_consume_token (parser->lexer);
16224 error ("spurious %<>>%>, use %<>%> to terminate "
16225 "a template argument list");
16226 }
16227 }
16228 else
16229 cp_parser_skip_to_end_of_template_parameter_list (parser);
16230 /* The `>' token might be a greater-than operator again now. */
16231 parser->greater_than_is_operator_p
16232 = saved_greater_than_is_operator_p;
16233 /* Restore the SAVED_SCOPE. */
16234 parser->scope = saved_scope;
16235 parser->qualifying_scope = saved_qualifying_scope;
16236 parser->object_scope = saved_object_scope;
16237 skip_evaluation = saved_skip_evaluation;
16238
16239 return arguments;
16240 }
16241
16242 /* MEMBER_FUNCTION is a member function, or a friend. If default
16243 arguments, or the body of the function have not yet been parsed,
16244 parse them now. */
16245
16246 static void
16247 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16248 {
16249 /* If this member is a template, get the underlying
16250 FUNCTION_DECL. */
16251 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16252 member_function = DECL_TEMPLATE_RESULT (member_function);
16253
16254 /* There should not be any class definitions in progress at this
16255 point; the bodies of members are only parsed outside of all class
16256 definitions. */
16257 gcc_assert (parser->num_classes_being_defined == 0);
16258 /* While we're parsing the member functions we might encounter more
16259 classes. We want to handle them right away, but we don't want
16260 them getting mixed up with functions that are currently in the
16261 queue. */
16262 parser->unparsed_functions_queues
16263 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16264
16265 /* Make sure that any template parameters are in scope. */
16266 maybe_begin_member_template_processing (member_function);
16267
16268 /* If the body of the function has not yet been parsed, parse it
16269 now. */
16270 if (DECL_PENDING_INLINE_P (member_function))
16271 {
16272 tree function_scope;
16273 cp_token_cache *tokens;
16274
16275 /* The function is no longer pending; we are processing it. */
16276 tokens = DECL_PENDING_INLINE_INFO (member_function);
16277 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16278 DECL_PENDING_INLINE_P (member_function) = 0;
16279
16280 /* If this is a local class, enter the scope of the containing
16281 function. */
16282 function_scope = current_function_decl;
16283 if (function_scope)
16284 push_function_context_to (function_scope);
16285
16286
16287 /* Push the body of the function onto the lexer stack. */
16288 cp_parser_push_lexer_for_tokens (parser, tokens);
16289
16290 /* Let the front end know that we going to be defining this
16291 function. */
16292 start_preparsed_function (member_function, NULL_TREE,
16293 SF_PRE_PARSED | SF_INCLASS_INLINE);
16294
16295 /* Don't do access checking if it is a templated function. */
16296 if (processing_template_decl)
16297 push_deferring_access_checks (dk_no_check);
16298
16299 /* Now, parse the body of the function. */
16300 cp_parser_function_definition_after_declarator (parser,
16301 /*inline_p=*/true);
16302
16303 if (processing_template_decl)
16304 pop_deferring_access_checks ();
16305
16306 /* Leave the scope of the containing function. */
16307 if (function_scope)
16308 pop_function_context_from (function_scope);
16309 cp_parser_pop_lexer (parser);
16310 }
16311
16312 /* Remove any template parameters from the symbol table. */
16313 maybe_end_member_template_processing ();
16314
16315 /* Restore the queue. */
16316 parser->unparsed_functions_queues
16317 = TREE_CHAIN (parser->unparsed_functions_queues);
16318 }
16319
16320 /* If DECL contains any default args, remember it on the unparsed
16321 functions queue. */
16322
16323 static void
16324 cp_parser_save_default_args (cp_parser* parser, tree decl)
16325 {
16326 tree probe;
16327
16328 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16329 probe;
16330 probe = TREE_CHAIN (probe))
16331 if (TREE_PURPOSE (probe))
16332 {
16333 TREE_PURPOSE (parser->unparsed_functions_queues)
16334 = tree_cons (current_class_type, decl,
16335 TREE_PURPOSE (parser->unparsed_functions_queues));
16336 break;
16337 }
16338 }
16339
16340 /* FN is a FUNCTION_DECL which may contains a parameter with an
16341 unparsed DEFAULT_ARG. Parse the default args now. This function
16342 assumes that the current scope is the scope in which the default
16343 argument should be processed. */
16344
16345 static void
16346 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16347 {
16348 bool saved_local_variables_forbidden_p;
16349 tree parm;
16350
16351 /* While we're parsing the default args, we might (due to the
16352 statement expression extension) encounter more classes. We want
16353 to handle them right away, but we don't want them getting mixed
16354 up with default args that are currently in the queue. */
16355 parser->unparsed_functions_queues
16356 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16357
16358 /* Local variable names (and the `this' keyword) may not appear
16359 in a default argument. */
16360 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16361 parser->local_variables_forbidden_p = true;
16362
16363 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16364 parm;
16365 parm = TREE_CHAIN (parm))
16366 {
16367 cp_token_cache *tokens;
16368 tree default_arg = TREE_PURPOSE (parm);
16369 tree parsed_arg;
16370 VEC(tree,gc) *insts;
16371 tree copy;
16372 unsigned ix;
16373
16374 if (!default_arg)
16375 continue;
16376
16377 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16378 /* This can happen for a friend declaration for a function
16379 already declared with default arguments. */
16380 continue;
16381
16382 /* Push the saved tokens for the default argument onto the parser's
16383 lexer stack. */
16384 tokens = DEFARG_TOKENS (default_arg);
16385 cp_parser_push_lexer_for_tokens (parser, tokens);
16386
16387 /* Parse the assignment-expression. */
16388 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16389
16390 if (!processing_template_decl)
16391 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16392
16393 TREE_PURPOSE (parm) = parsed_arg;
16394
16395 /* Update any instantiations we've already created. */
16396 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16397 VEC_iterate (tree, insts, ix, copy); ix++)
16398 TREE_PURPOSE (copy) = parsed_arg;
16399
16400 /* If the token stream has not been completely used up, then
16401 there was extra junk after the end of the default
16402 argument. */
16403 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16404 cp_parser_error (parser, "expected %<,%>");
16405
16406 /* Revert to the main lexer. */
16407 cp_parser_pop_lexer (parser);
16408 }
16409
16410 /* Make sure no default arg is missing. */
16411 check_default_args (fn);
16412
16413 /* Restore the state of local_variables_forbidden_p. */
16414 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16415
16416 /* Restore the queue. */
16417 parser->unparsed_functions_queues
16418 = TREE_CHAIN (parser->unparsed_functions_queues);
16419 }
16420
16421 /* Parse the operand of `sizeof' (or a similar operator). Returns
16422 either a TYPE or an expression, depending on the form of the
16423 input. The KEYWORD indicates which kind of expression we have
16424 encountered. */
16425
16426 static tree
16427 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16428 {
16429 static const char *format;
16430 tree expr = NULL_TREE;
16431 const char *saved_message;
16432 bool saved_integral_constant_expression_p;
16433 bool saved_non_integral_constant_expression_p;
16434
16435 /* Initialize FORMAT the first time we get here. */
16436 if (!format)
16437 format = "types may not be defined in '%s' expressions";
16438
16439 /* Types cannot be defined in a `sizeof' expression. Save away the
16440 old message. */
16441 saved_message = parser->type_definition_forbidden_message;
16442 /* And create the new one. */
16443 parser->type_definition_forbidden_message
16444 = XNEWVEC (const char, strlen (format)
16445 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16446 + 1 /* `\0' */);
16447 sprintf ((char *) parser->type_definition_forbidden_message,
16448 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16449
16450 /* The restrictions on constant-expressions do not apply inside
16451 sizeof expressions. */
16452 saved_integral_constant_expression_p
16453 = parser->integral_constant_expression_p;
16454 saved_non_integral_constant_expression_p
16455 = parser->non_integral_constant_expression_p;
16456 parser->integral_constant_expression_p = false;
16457
16458 /* Do not actually evaluate the expression. */
16459 ++skip_evaluation;
16460 /* If it's a `(', then we might be looking at the type-id
16461 construction. */
16462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16463 {
16464 tree type;
16465 bool saved_in_type_id_in_expr_p;
16466
16467 /* We can't be sure yet whether we're looking at a type-id or an
16468 expression. */
16469 cp_parser_parse_tentatively (parser);
16470 /* Consume the `('. */
16471 cp_lexer_consume_token (parser->lexer);
16472 /* Parse the type-id. */
16473 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16474 parser->in_type_id_in_expr_p = true;
16475 type = cp_parser_type_id (parser);
16476 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16477 /* Now, look for the trailing `)'. */
16478 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16479 /* If all went well, then we're done. */
16480 if (cp_parser_parse_definitely (parser))
16481 {
16482 cp_decl_specifier_seq decl_specs;
16483
16484 /* Build a trivial decl-specifier-seq. */
16485 clear_decl_specs (&decl_specs);
16486 decl_specs.type = type;
16487
16488 /* Call grokdeclarator to figure out what type this is. */
16489 expr = grokdeclarator (NULL,
16490 &decl_specs,
16491 TYPENAME,
16492 /*initialized=*/0,
16493 /*attrlist=*/NULL);
16494 }
16495 }
16496
16497 /* If the type-id production did not work out, then we must be
16498 looking at the unary-expression production. */
16499 if (!expr)
16500 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16501 /*cast_p=*/false);
16502 /* Go back to evaluating expressions. */
16503 --skip_evaluation;
16504
16505 /* Free the message we created. */
16506 free ((char *) parser->type_definition_forbidden_message);
16507 /* And restore the old one. */
16508 parser->type_definition_forbidden_message = saved_message;
16509 parser->integral_constant_expression_p
16510 = saved_integral_constant_expression_p;
16511 parser->non_integral_constant_expression_p
16512 = saved_non_integral_constant_expression_p;
16513
16514 return expr;
16515 }
16516
16517 /* If the current declaration has no declarator, return true. */
16518
16519 static bool
16520 cp_parser_declares_only_class_p (cp_parser *parser)
16521 {
16522 /* If the next token is a `;' or a `,' then there is no
16523 declarator. */
16524 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16525 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16526 }
16527
16528 /* Update the DECL_SPECS to reflect the storage class indicated by
16529 KEYWORD. */
16530
16531 static void
16532 cp_parser_set_storage_class (cp_parser *parser,
16533 cp_decl_specifier_seq *decl_specs,
16534 enum rid keyword)
16535 {
16536 cp_storage_class storage_class;
16537
16538 if (parser->in_unbraced_linkage_specification_p)
16539 {
16540 error ("invalid use of %qD in linkage specification",
16541 ridpointers[keyword]);
16542 return;
16543 }
16544 else if (decl_specs->storage_class != sc_none)
16545 {
16546 decl_specs->conflicting_specifiers_p = true;
16547 return;
16548 }
16549
16550 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16551 && decl_specs->specs[(int) ds_thread])
16552 {
16553 error ("%<__thread%> before %qD", ridpointers[keyword]);
16554 decl_specs->specs[(int) ds_thread] = 0;
16555 }
16556
16557 switch (keyword)
16558 {
16559 case RID_AUTO:
16560 storage_class = sc_auto;
16561 break;
16562 case RID_REGISTER:
16563 storage_class = sc_register;
16564 break;
16565 case RID_STATIC:
16566 storage_class = sc_static;
16567 break;
16568 case RID_EXTERN:
16569 storage_class = sc_extern;
16570 break;
16571 case RID_MUTABLE:
16572 storage_class = sc_mutable;
16573 break;
16574 default:
16575 gcc_unreachable ();
16576 }
16577 decl_specs->storage_class = storage_class;
16578
16579 /* A storage class specifier cannot be applied alongside a typedef
16580 specifier. If there is a typedef specifier present then set
16581 conflicting_specifiers_p which will trigger an error later
16582 on in grokdeclarator. */
16583 if (decl_specs->specs[(int)ds_typedef])
16584 decl_specs->conflicting_specifiers_p = true;
16585 }
16586
16587 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16588 is true, the type is a user-defined type; otherwise it is a
16589 built-in type specified by a keyword. */
16590
16591 static void
16592 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16593 tree type_spec,
16594 bool user_defined_p)
16595 {
16596 decl_specs->any_specifiers_p = true;
16597
16598 /* If the user tries to redeclare bool or wchar_t (with, for
16599 example, in "typedef int wchar_t;") we remember that this is what
16600 happened. In system headers, we ignore these declarations so
16601 that G++ can work with system headers that are not C++-safe. */
16602 if (decl_specs->specs[(int) ds_typedef]
16603 && !user_defined_p
16604 && (type_spec == boolean_type_node
16605 || type_spec == wchar_type_node)
16606 && (decl_specs->type
16607 || decl_specs->specs[(int) ds_long]
16608 || decl_specs->specs[(int) ds_short]
16609 || decl_specs->specs[(int) ds_unsigned]
16610 || decl_specs->specs[(int) ds_signed]))
16611 {
16612 decl_specs->redefined_builtin_type = type_spec;
16613 if (!decl_specs->type)
16614 {
16615 decl_specs->type = type_spec;
16616 decl_specs->user_defined_type_p = false;
16617 }
16618 }
16619 else if (decl_specs->type)
16620 decl_specs->multiple_types_p = true;
16621 else
16622 {
16623 decl_specs->type = type_spec;
16624 decl_specs->user_defined_type_p = user_defined_p;
16625 decl_specs->redefined_builtin_type = NULL_TREE;
16626 }
16627 }
16628
16629 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16630 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16631
16632 static bool
16633 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16634 {
16635 return decl_specifiers->specs[(int) ds_friend] != 0;
16636 }
16637
16638 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16639 issue an error message indicating that TOKEN_DESC was expected.
16640
16641 Returns the token consumed, if the token had the appropriate type.
16642 Otherwise, returns NULL. */
16643
16644 static cp_token *
16645 cp_parser_require (cp_parser* parser,
16646 enum cpp_ttype type,
16647 const char* token_desc)
16648 {
16649 if (cp_lexer_next_token_is (parser->lexer, type))
16650 return cp_lexer_consume_token (parser->lexer);
16651 else
16652 {
16653 /* Output the MESSAGE -- unless we're parsing tentatively. */
16654 if (!cp_parser_simulate_error (parser))
16655 {
16656 char *message = concat ("expected ", token_desc, NULL);
16657 cp_parser_error (parser, message);
16658 free (message);
16659 }
16660 return NULL;
16661 }
16662 }
16663
16664 /* An error message is produced if the next token is not '>'.
16665 All further tokens are skipped until the desired token is
16666 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16667
16668 static void
16669 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16670 {
16671 /* Current level of '< ... >'. */
16672 unsigned level = 0;
16673 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16674 unsigned nesting_depth = 0;
16675
16676 /* Are we ready, yet? If not, issue error message. */
16677 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16678 return;
16679
16680 /* Skip tokens until the desired token is found. */
16681 while (true)
16682 {
16683 /* Peek at the next token. */
16684 switch (cp_lexer_peek_token (parser->lexer)->type)
16685 {
16686 case CPP_LESS:
16687 if (!nesting_depth)
16688 ++level;
16689 break;
16690
16691 case CPP_GREATER:
16692 if (!nesting_depth && level-- == 0)
16693 {
16694 /* We've reached the token we want, consume it and stop. */
16695 cp_lexer_consume_token (parser->lexer);
16696 return;
16697 }
16698 break;
16699
16700 case CPP_OPEN_PAREN:
16701 case CPP_OPEN_SQUARE:
16702 ++nesting_depth;
16703 break;
16704
16705 case CPP_CLOSE_PAREN:
16706 case CPP_CLOSE_SQUARE:
16707 if (nesting_depth-- == 0)
16708 return;
16709 break;
16710
16711 case CPP_EOF:
16712 case CPP_PRAGMA_EOL:
16713 case CPP_SEMICOLON:
16714 case CPP_OPEN_BRACE:
16715 case CPP_CLOSE_BRACE:
16716 /* The '>' was probably forgotten, don't look further. */
16717 return;
16718
16719 default:
16720 break;
16721 }
16722
16723 /* Consume this token. */
16724 cp_lexer_consume_token (parser->lexer);
16725 }
16726 }
16727
16728 /* If the next token is the indicated keyword, consume it. Otherwise,
16729 issue an error message indicating that TOKEN_DESC was expected.
16730
16731 Returns the token consumed, if the token had the appropriate type.
16732 Otherwise, returns NULL. */
16733
16734 static cp_token *
16735 cp_parser_require_keyword (cp_parser* parser,
16736 enum rid keyword,
16737 const char* token_desc)
16738 {
16739 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16740
16741 if (token && token->keyword != keyword)
16742 {
16743 dyn_string_t error_msg;
16744
16745 /* Format the error message. */
16746 error_msg = dyn_string_new (0);
16747 dyn_string_append_cstr (error_msg, "expected ");
16748 dyn_string_append_cstr (error_msg, token_desc);
16749 cp_parser_error (parser, error_msg->s);
16750 dyn_string_delete (error_msg);
16751 return NULL;
16752 }
16753
16754 return token;
16755 }
16756
16757 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16758 function-definition. */
16759
16760 static bool
16761 cp_parser_token_starts_function_definition_p (cp_token* token)
16762 {
16763 return (/* An ordinary function-body begins with an `{'. */
16764 token->type == CPP_OPEN_BRACE
16765 /* A ctor-initializer begins with a `:'. */
16766 || token->type == CPP_COLON
16767 /* A function-try-block begins with `try'. */
16768 || token->keyword == RID_TRY
16769 /* The named return value extension begins with `return'. */
16770 || token->keyword == RID_RETURN);
16771 }
16772
16773 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16774 definition. */
16775
16776 static bool
16777 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16778 {
16779 cp_token *token;
16780
16781 token = cp_lexer_peek_token (parser->lexer);
16782 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16783 }
16784
16785 /* Returns TRUE iff the next token is the "," or ">" ending a
16786 template-argument. */
16787
16788 static bool
16789 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16790 {
16791 cp_token *token;
16792
16793 token = cp_lexer_peek_token (parser->lexer);
16794 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16795 }
16796
16797 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16798 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16799
16800 static bool
16801 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16802 size_t n)
16803 {
16804 cp_token *token;
16805
16806 token = cp_lexer_peek_nth_token (parser->lexer, n);
16807 if (token->type == CPP_LESS)
16808 return true;
16809 /* Check for the sequence `<::' in the original code. It would be lexed as
16810 `[:', where `[' is a digraph, and there is no whitespace before
16811 `:'. */
16812 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16813 {
16814 cp_token *token2;
16815 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16816 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16817 return true;
16818 }
16819 return false;
16820 }
16821
16822 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16823 or none_type otherwise. */
16824
16825 static enum tag_types
16826 cp_parser_token_is_class_key (cp_token* token)
16827 {
16828 switch (token->keyword)
16829 {
16830 case RID_CLASS:
16831 return class_type;
16832 case RID_STRUCT:
16833 return record_type;
16834 case RID_UNION:
16835 return union_type;
16836
16837 default:
16838 return none_type;
16839 }
16840 }
16841
16842 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16843
16844 static void
16845 cp_parser_check_class_key (enum tag_types class_key, tree type)
16846 {
16847 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16848 pedwarn ("%qs tag used in naming %q#T",
16849 class_key == union_type ? "union"
16850 : class_key == record_type ? "struct" : "class",
16851 type);
16852 }
16853
16854 /* Issue an error message if DECL is redeclared with different
16855 access than its original declaration [class.access.spec/3].
16856 This applies to nested classes and nested class templates.
16857 [class.mem/1]. */
16858
16859 static void
16860 cp_parser_check_access_in_redeclaration (tree decl)
16861 {
16862 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16863 return;
16864
16865 if ((TREE_PRIVATE (decl)
16866 != (current_access_specifier == access_private_node))
16867 || (TREE_PROTECTED (decl)
16868 != (current_access_specifier == access_protected_node)))
16869 error ("%qD redeclared with different access", decl);
16870 }
16871
16872 /* Look for the `template' keyword, as a syntactic disambiguator.
16873 Return TRUE iff it is present, in which case it will be
16874 consumed. */
16875
16876 static bool
16877 cp_parser_optional_template_keyword (cp_parser *parser)
16878 {
16879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16880 {
16881 /* The `template' keyword can only be used within templates;
16882 outside templates the parser can always figure out what is a
16883 template and what is not. */
16884 if (!processing_template_decl)
16885 {
16886 error ("%<template%> (as a disambiguator) is only allowed "
16887 "within templates");
16888 /* If this part of the token stream is rescanned, the same
16889 error message would be generated. So, we purge the token
16890 from the stream. */
16891 cp_lexer_purge_token (parser->lexer);
16892 return false;
16893 }
16894 else
16895 {
16896 /* Consume the `template' keyword. */
16897 cp_lexer_consume_token (parser->lexer);
16898 return true;
16899 }
16900 }
16901
16902 return false;
16903 }
16904
16905 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16906 set PARSER->SCOPE, and perform other related actions. */
16907
16908 static void
16909 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16910 {
16911 int i;
16912 struct tree_check *check_value;
16913 deferred_access_check *chk;
16914 VEC (deferred_access_check,gc) *checks;
16915
16916 /* Get the stored value. */
16917 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16918 /* Perform any access checks that were deferred. */
16919 checks = check_value->checks;
16920 if (checks)
16921 {
16922 for (i = 0 ;
16923 VEC_iterate (deferred_access_check, checks, i, chk) ;
16924 ++i)
16925 {
16926 perform_or_defer_access_check (chk->binfo,
16927 chk->decl,
16928 chk->diag_decl);
16929 }
16930 }
16931 /* Set the scope from the stored value. */
16932 parser->scope = check_value->value;
16933 parser->qualifying_scope = check_value->qualifying_scope;
16934 parser->object_scope = NULL_TREE;
16935 }
16936
16937 /* Consume tokens up through a non-nested END token. */
16938
16939 static void
16940 cp_parser_cache_group (cp_parser *parser,
16941 enum cpp_ttype end,
16942 unsigned depth)
16943 {
16944 while (true)
16945 {
16946 cp_token *token;
16947
16948 /* Abort a parenthesized expression if we encounter a brace. */
16949 if ((end == CPP_CLOSE_PAREN || depth == 0)
16950 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16951 return;
16952 /* If we've reached the end of the file, stop. */
16953 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16954 || (end != CPP_PRAGMA_EOL
16955 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16956 return;
16957 /* Consume the next token. */
16958 token = cp_lexer_consume_token (parser->lexer);
16959 /* See if it starts a new group. */
16960 if (token->type == CPP_OPEN_BRACE)
16961 {
16962 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16963 if (depth == 0)
16964 return;
16965 }
16966 else if (token->type == CPP_OPEN_PAREN)
16967 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16968 else if (token->type == CPP_PRAGMA)
16969 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16970 else if (token->type == end)
16971 return;
16972 }
16973 }
16974
16975 /* Begin parsing tentatively. We always save tokens while parsing
16976 tentatively so that if the tentative parsing fails we can restore the
16977 tokens. */
16978
16979 static void
16980 cp_parser_parse_tentatively (cp_parser* parser)
16981 {
16982 /* Enter a new parsing context. */
16983 parser->context = cp_parser_context_new (parser->context);
16984 /* Begin saving tokens. */
16985 cp_lexer_save_tokens (parser->lexer);
16986 /* In order to avoid repetitive access control error messages,
16987 access checks are queued up until we are no longer parsing
16988 tentatively. */
16989 push_deferring_access_checks (dk_deferred);
16990 }
16991
16992 /* Commit to the currently active tentative parse. */
16993
16994 static void
16995 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16996 {
16997 cp_parser_context *context;
16998 cp_lexer *lexer;
16999
17000 /* Mark all of the levels as committed. */
17001 lexer = parser->lexer;
17002 for (context = parser->context; context->next; context = context->next)
17003 {
17004 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17005 break;
17006 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17007 while (!cp_lexer_saving_tokens (lexer))
17008 lexer = lexer->next;
17009 cp_lexer_commit_tokens (lexer);
17010 }
17011 }
17012
17013 /* Abort the currently active tentative parse. All consumed tokens
17014 will be rolled back, and no diagnostics will be issued. */
17015
17016 static void
17017 cp_parser_abort_tentative_parse (cp_parser* parser)
17018 {
17019 cp_parser_simulate_error (parser);
17020 /* Now, pretend that we want to see if the construct was
17021 successfully parsed. */
17022 cp_parser_parse_definitely (parser);
17023 }
17024
17025 /* Stop parsing tentatively. If a parse error has occurred, restore the
17026 token stream. Otherwise, commit to the tokens we have consumed.
17027 Returns true if no error occurred; false otherwise. */
17028
17029 static bool
17030 cp_parser_parse_definitely (cp_parser* parser)
17031 {
17032 bool error_occurred;
17033 cp_parser_context *context;
17034
17035 /* Remember whether or not an error occurred, since we are about to
17036 destroy that information. */
17037 error_occurred = cp_parser_error_occurred (parser);
17038 /* Remove the topmost context from the stack. */
17039 context = parser->context;
17040 parser->context = context->next;
17041 /* If no parse errors occurred, commit to the tentative parse. */
17042 if (!error_occurred)
17043 {
17044 /* Commit to the tokens read tentatively, unless that was
17045 already done. */
17046 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17047 cp_lexer_commit_tokens (parser->lexer);
17048
17049 pop_to_parent_deferring_access_checks ();
17050 }
17051 /* Otherwise, if errors occurred, roll back our state so that things
17052 are just as they were before we began the tentative parse. */
17053 else
17054 {
17055 cp_lexer_rollback_tokens (parser->lexer);
17056 pop_deferring_access_checks ();
17057 }
17058 /* Add the context to the front of the free list. */
17059 context->next = cp_parser_context_free_list;
17060 cp_parser_context_free_list = context;
17061
17062 return !error_occurred;
17063 }
17064
17065 /* Returns true if we are parsing tentatively and are not committed to
17066 this tentative parse. */
17067
17068 static bool
17069 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17070 {
17071 return (cp_parser_parsing_tentatively (parser)
17072 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17073 }
17074
17075 /* Returns nonzero iff an error has occurred during the most recent
17076 tentative parse. */
17077
17078 static bool
17079 cp_parser_error_occurred (cp_parser* parser)
17080 {
17081 return (cp_parser_parsing_tentatively (parser)
17082 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17083 }
17084
17085 /* Returns nonzero if GNU extensions are allowed. */
17086
17087 static bool
17088 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17089 {
17090 return parser->allow_gnu_extensions_p;
17091 }
17092 \f
17093 /* Objective-C++ Productions */
17094
17095
17096 /* Parse an Objective-C expression, which feeds into a primary-expression
17097 above.
17098
17099 objc-expression:
17100 objc-message-expression
17101 objc-string-literal
17102 objc-encode-expression
17103 objc-protocol-expression
17104 objc-selector-expression
17105
17106 Returns a tree representation of the expression. */
17107
17108 static tree
17109 cp_parser_objc_expression (cp_parser* parser)
17110 {
17111 /* Try to figure out what kind of declaration is present. */
17112 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17113
17114 switch (kwd->type)
17115 {
17116 case CPP_OPEN_SQUARE:
17117 return cp_parser_objc_message_expression (parser);
17118
17119 case CPP_OBJC_STRING:
17120 kwd = cp_lexer_consume_token (parser->lexer);
17121 return objc_build_string_object (kwd->u.value);
17122
17123 case CPP_KEYWORD:
17124 switch (kwd->keyword)
17125 {
17126 case RID_AT_ENCODE:
17127 return cp_parser_objc_encode_expression (parser);
17128
17129 case RID_AT_PROTOCOL:
17130 return cp_parser_objc_protocol_expression (parser);
17131
17132 case RID_AT_SELECTOR:
17133 return cp_parser_objc_selector_expression (parser);
17134
17135 default:
17136 break;
17137 }
17138 default:
17139 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17140 cp_parser_skip_to_end_of_block_or_statement (parser);
17141 }
17142
17143 return error_mark_node;
17144 }
17145
17146 /* Parse an Objective-C message expression.
17147
17148 objc-message-expression:
17149 [ objc-message-receiver objc-message-args ]
17150
17151 Returns a representation of an Objective-C message. */
17152
17153 static tree
17154 cp_parser_objc_message_expression (cp_parser* parser)
17155 {
17156 tree receiver, messageargs;
17157
17158 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17159 receiver = cp_parser_objc_message_receiver (parser);
17160 messageargs = cp_parser_objc_message_args (parser);
17161 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17162
17163 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17164 }
17165
17166 /* Parse an objc-message-receiver.
17167
17168 objc-message-receiver:
17169 expression
17170 simple-type-specifier
17171
17172 Returns a representation of the type or expression. */
17173
17174 static tree
17175 cp_parser_objc_message_receiver (cp_parser* parser)
17176 {
17177 tree rcv;
17178
17179 /* An Objective-C message receiver may be either (1) a type
17180 or (2) an expression. */
17181 cp_parser_parse_tentatively (parser);
17182 rcv = cp_parser_expression (parser, false);
17183
17184 if (cp_parser_parse_definitely (parser))
17185 return rcv;
17186
17187 rcv = cp_parser_simple_type_specifier (parser,
17188 /*decl_specs=*/NULL,
17189 CP_PARSER_FLAGS_NONE);
17190
17191 return objc_get_class_reference (rcv);
17192 }
17193
17194 /* Parse the arguments and selectors comprising an Objective-C message.
17195
17196 objc-message-args:
17197 objc-selector
17198 objc-selector-args
17199 objc-selector-args , objc-comma-args
17200
17201 objc-selector-args:
17202 objc-selector [opt] : assignment-expression
17203 objc-selector-args objc-selector [opt] : assignment-expression
17204
17205 objc-comma-args:
17206 assignment-expression
17207 objc-comma-args , assignment-expression
17208
17209 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17210 selector arguments and TREE_VALUE containing a list of comma
17211 arguments. */
17212
17213 static tree
17214 cp_parser_objc_message_args (cp_parser* parser)
17215 {
17216 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17217 bool maybe_unary_selector_p = true;
17218 cp_token *token = cp_lexer_peek_token (parser->lexer);
17219
17220 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17221 {
17222 tree selector = NULL_TREE, arg;
17223
17224 if (token->type != CPP_COLON)
17225 selector = cp_parser_objc_selector (parser);
17226
17227 /* Detect if we have a unary selector. */
17228 if (maybe_unary_selector_p
17229 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17230 return build_tree_list (selector, NULL_TREE);
17231
17232 maybe_unary_selector_p = false;
17233 cp_parser_require (parser, CPP_COLON, "`:'");
17234 arg = cp_parser_assignment_expression (parser, false);
17235
17236 sel_args
17237 = chainon (sel_args,
17238 build_tree_list (selector, arg));
17239
17240 token = cp_lexer_peek_token (parser->lexer);
17241 }
17242
17243 /* Handle non-selector arguments, if any. */
17244 while (token->type == CPP_COMMA)
17245 {
17246 tree arg;
17247
17248 cp_lexer_consume_token (parser->lexer);
17249 arg = cp_parser_assignment_expression (parser, false);
17250
17251 addl_args
17252 = chainon (addl_args,
17253 build_tree_list (NULL_TREE, arg));
17254
17255 token = cp_lexer_peek_token (parser->lexer);
17256 }
17257
17258 return build_tree_list (sel_args, addl_args);
17259 }
17260
17261 /* Parse an Objective-C encode expression.
17262
17263 objc-encode-expression:
17264 @encode objc-typename
17265
17266 Returns an encoded representation of the type argument. */
17267
17268 static tree
17269 cp_parser_objc_encode_expression (cp_parser* parser)
17270 {
17271 tree type;
17272
17273 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17274 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17275 type = complete_type (cp_parser_type_id (parser));
17276 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17277
17278 if (!type)
17279 {
17280 error ("%<@encode%> must specify a type as an argument");
17281 return error_mark_node;
17282 }
17283
17284 return objc_build_encode_expr (type);
17285 }
17286
17287 /* Parse an Objective-C @defs expression. */
17288
17289 static tree
17290 cp_parser_objc_defs_expression (cp_parser *parser)
17291 {
17292 tree name;
17293
17294 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17295 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17296 name = cp_parser_identifier (parser);
17297 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17298
17299 return objc_get_class_ivars (name);
17300 }
17301
17302 /* Parse an Objective-C protocol expression.
17303
17304 objc-protocol-expression:
17305 @protocol ( identifier )
17306
17307 Returns a representation of the protocol expression. */
17308
17309 static tree
17310 cp_parser_objc_protocol_expression (cp_parser* parser)
17311 {
17312 tree proto;
17313
17314 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17315 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17316 proto = cp_parser_identifier (parser);
17317 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17318
17319 return objc_build_protocol_expr (proto);
17320 }
17321
17322 /* Parse an Objective-C selector expression.
17323
17324 objc-selector-expression:
17325 @selector ( objc-method-signature )
17326
17327 objc-method-signature:
17328 objc-selector
17329 objc-selector-seq
17330
17331 objc-selector-seq:
17332 objc-selector :
17333 objc-selector-seq objc-selector :
17334
17335 Returns a representation of the method selector. */
17336
17337 static tree
17338 cp_parser_objc_selector_expression (cp_parser* parser)
17339 {
17340 tree sel_seq = NULL_TREE;
17341 bool maybe_unary_selector_p = true;
17342 cp_token *token;
17343
17344 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17345 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17346 token = cp_lexer_peek_token (parser->lexer);
17347
17348 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17349 || token->type == CPP_SCOPE)
17350 {
17351 tree selector = NULL_TREE;
17352
17353 if (token->type != CPP_COLON
17354 || token->type == CPP_SCOPE)
17355 selector = cp_parser_objc_selector (parser);
17356
17357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17358 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17359 {
17360 /* Detect if we have a unary selector. */
17361 if (maybe_unary_selector_p)
17362 {
17363 sel_seq = selector;
17364 goto finish_selector;
17365 }
17366 else
17367 {
17368 cp_parser_error (parser, "expected %<:%>");
17369 }
17370 }
17371 maybe_unary_selector_p = false;
17372 token = cp_lexer_consume_token (parser->lexer);
17373
17374 if (token->type == CPP_SCOPE)
17375 {
17376 sel_seq
17377 = chainon (sel_seq,
17378 build_tree_list (selector, NULL_TREE));
17379 sel_seq
17380 = chainon (sel_seq,
17381 build_tree_list (NULL_TREE, NULL_TREE));
17382 }
17383 else
17384 sel_seq
17385 = chainon (sel_seq,
17386 build_tree_list (selector, NULL_TREE));
17387
17388 token = cp_lexer_peek_token (parser->lexer);
17389 }
17390
17391 finish_selector:
17392 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17393
17394 return objc_build_selector_expr (sel_seq);
17395 }
17396
17397 /* Parse a list of identifiers.
17398
17399 objc-identifier-list:
17400 identifier
17401 objc-identifier-list , identifier
17402
17403 Returns a TREE_LIST of identifier nodes. */
17404
17405 static tree
17406 cp_parser_objc_identifier_list (cp_parser* parser)
17407 {
17408 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17409 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17410
17411 while (sep->type == CPP_COMMA)
17412 {
17413 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17414 list = chainon (list,
17415 build_tree_list (NULL_TREE,
17416 cp_parser_identifier (parser)));
17417 sep = cp_lexer_peek_token (parser->lexer);
17418 }
17419
17420 return list;
17421 }
17422
17423 /* Parse an Objective-C alias declaration.
17424
17425 objc-alias-declaration:
17426 @compatibility_alias identifier identifier ;
17427
17428 This function registers the alias mapping with the Objective-C front end.
17429 It returns nothing. */
17430
17431 static void
17432 cp_parser_objc_alias_declaration (cp_parser* parser)
17433 {
17434 tree alias, orig;
17435
17436 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17437 alias = cp_parser_identifier (parser);
17438 orig = cp_parser_identifier (parser);
17439 objc_declare_alias (alias, orig);
17440 cp_parser_consume_semicolon_at_end_of_statement (parser);
17441 }
17442
17443 /* Parse an Objective-C class forward-declaration.
17444
17445 objc-class-declaration:
17446 @class objc-identifier-list ;
17447
17448 The function registers the forward declarations with the Objective-C
17449 front end. It returns nothing. */
17450
17451 static void
17452 cp_parser_objc_class_declaration (cp_parser* parser)
17453 {
17454 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17455 objc_declare_class (cp_parser_objc_identifier_list (parser));
17456 cp_parser_consume_semicolon_at_end_of_statement (parser);
17457 }
17458
17459 /* Parse a list of Objective-C protocol references.
17460
17461 objc-protocol-refs-opt:
17462 objc-protocol-refs [opt]
17463
17464 objc-protocol-refs:
17465 < objc-identifier-list >
17466
17467 Returns a TREE_LIST of identifiers, if any. */
17468
17469 static tree
17470 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17471 {
17472 tree protorefs = NULL_TREE;
17473
17474 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17475 {
17476 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17477 protorefs = cp_parser_objc_identifier_list (parser);
17478 cp_parser_require (parser, CPP_GREATER, "`>'");
17479 }
17480
17481 return protorefs;
17482 }
17483
17484 /* Parse a Objective-C visibility specification. */
17485
17486 static void
17487 cp_parser_objc_visibility_spec (cp_parser* parser)
17488 {
17489 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17490
17491 switch (vis->keyword)
17492 {
17493 case RID_AT_PRIVATE:
17494 objc_set_visibility (2);
17495 break;
17496 case RID_AT_PROTECTED:
17497 objc_set_visibility (0);
17498 break;
17499 case RID_AT_PUBLIC:
17500 objc_set_visibility (1);
17501 break;
17502 default:
17503 return;
17504 }
17505
17506 /* Eat '@private'/'@protected'/'@public'. */
17507 cp_lexer_consume_token (parser->lexer);
17508 }
17509
17510 /* Parse an Objective-C method type. */
17511
17512 static void
17513 cp_parser_objc_method_type (cp_parser* parser)
17514 {
17515 objc_set_method_type
17516 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17517 ? PLUS_EXPR
17518 : MINUS_EXPR);
17519 }
17520
17521 /* Parse an Objective-C protocol qualifier. */
17522
17523 static tree
17524 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17525 {
17526 tree quals = NULL_TREE, node;
17527 cp_token *token = cp_lexer_peek_token (parser->lexer);
17528
17529 node = token->u.value;
17530
17531 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17532 && (node == ridpointers [(int) RID_IN]
17533 || node == ridpointers [(int) RID_OUT]
17534 || node == ridpointers [(int) RID_INOUT]
17535 || node == ridpointers [(int) RID_BYCOPY]
17536 || node == ridpointers [(int) RID_BYREF]
17537 || node == ridpointers [(int) RID_ONEWAY]))
17538 {
17539 quals = tree_cons (NULL_TREE, node, quals);
17540 cp_lexer_consume_token (parser->lexer);
17541 token = cp_lexer_peek_token (parser->lexer);
17542 node = token->u.value;
17543 }
17544
17545 return quals;
17546 }
17547
17548 /* Parse an Objective-C typename. */
17549
17550 static tree
17551 cp_parser_objc_typename (cp_parser* parser)
17552 {
17553 tree typename = NULL_TREE;
17554
17555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17556 {
17557 tree proto_quals, cp_type = NULL_TREE;
17558
17559 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17560 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17561
17562 /* An ObjC type name may consist of just protocol qualifiers, in which
17563 case the type shall default to 'id'. */
17564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17565 cp_type = cp_parser_type_id (parser);
17566
17567 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17568 typename = build_tree_list (proto_quals, cp_type);
17569 }
17570
17571 return typename;
17572 }
17573
17574 /* Check to see if TYPE refers to an Objective-C selector name. */
17575
17576 static bool
17577 cp_parser_objc_selector_p (enum cpp_ttype type)
17578 {
17579 return (type == CPP_NAME || type == CPP_KEYWORD
17580 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17581 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17582 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17583 || type == CPP_XOR || type == CPP_XOR_EQ);
17584 }
17585
17586 /* Parse an Objective-C selector. */
17587
17588 static tree
17589 cp_parser_objc_selector (cp_parser* parser)
17590 {
17591 cp_token *token = cp_lexer_consume_token (parser->lexer);
17592
17593 if (!cp_parser_objc_selector_p (token->type))
17594 {
17595 error ("invalid Objective-C++ selector name");
17596 return error_mark_node;
17597 }
17598
17599 /* C++ operator names are allowed to appear in ObjC selectors. */
17600 switch (token->type)
17601 {
17602 case CPP_AND_AND: return get_identifier ("and");
17603 case CPP_AND_EQ: return get_identifier ("and_eq");
17604 case CPP_AND: return get_identifier ("bitand");
17605 case CPP_OR: return get_identifier ("bitor");
17606 case CPP_COMPL: return get_identifier ("compl");
17607 case CPP_NOT: return get_identifier ("not");
17608 case CPP_NOT_EQ: return get_identifier ("not_eq");
17609 case CPP_OR_OR: return get_identifier ("or");
17610 case CPP_OR_EQ: return get_identifier ("or_eq");
17611 case CPP_XOR: return get_identifier ("xor");
17612 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17613 default: return token->u.value;
17614 }
17615 }
17616
17617 /* Parse an Objective-C params list. */
17618
17619 static tree
17620 cp_parser_objc_method_keyword_params (cp_parser* parser)
17621 {
17622 tree params = NULL_TREE;
17623 bool maybe_unary_selector_p = true;
17624 cp_token *token = cp_lexer_peek_token (parser->lexer);
17625
17626 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17627 {
17628 tree selector = NULL_TREE, typename, identifier;
17629
17630 if (token->type != CPP_COLON)
17631 selector = cp_parser_objc_selector (parser);
17632
17633 /* Detect if we have a unary selector. */
17634 if (maybe_unary_selector_p
17635 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17636 return selector;
17637
17638 maybe_unary_selector_p = false;
17639 cp_parser_require (parser, CPP_COLON, "`:'");
17640 typename = cp_parser_objc_typename (parser);
17641 identifier = cp_parser_identifier (parser);
17642
17643 params
17644 = chainon (params,
17645 objc_build_keyword_decl (selector,
17646 typename,
17647 identifier));
17648
17649 token = cp_lexer_peek_token (parser->lexer);
17650 }
17651
17652 return params;
17653 }
17654
17655 /* Parse the non-keyword Objective-C params. */
17656
17657 static tree
17658 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17659 {
17660 tree params = make_node (TREE_LIST);
17661 cp_token *token = cp_lexer_peek_token (parser->lexer);
17662 *ellipsisp = false; /* Initially, assume no ellipsis. */
17663
17664 while (token->type == CPP_COMMA)
17665 {
17666 cp_parameter_declarator *parmdecl;
17667 tree parm;
17668
17669 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17670 token = cp_lexer_peek_token (parser->lexer);
17671
17672 if (token->type == CPP_ELLIPSIS)
17673 {
17674 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17675 *ellipsisp = true;
17676 break;
17677 }
17678
17679 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17680 parm = grokdeclarator (parmdecl->declarator,
17681 &parmdecl->decl_specifiers,
17682 PARM, /*initialized=*/0,
17683 /*attrlist=*/NULL);
17684
17685 chainon (params, build_tree_list (NULL_TREE, parm));
17686 token = cp_lexer_peek_token (parser->lexer);
17687 }
17688
17689 return params;
17690 }
17691
17692 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17693
17694 static void
17695 cp_parser_objc_interstitial_code (cp_parser* parser)
17696 {
17697 cp_token *token = cp_lexer_peek_token (parser->lexer);
17698
17699 /* If the next token is `extern' and the following token is a string
17700 literal, then we have a linkage specification. */
17701 if (token->keyword == RID_EXTERN
17702 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17703 cp_parser_linkage_specification (parser);
17704 /* Handle #pragma, if any. */
17705 else if (token->type == CPP_PRAGMA)
17706 cp_parser_pragma (parser, pragma_external);
17707 /* Allow stray semicolons. */
17708 else if (token->type == CPP_SEMICOLON)
17709 cp_lexer_consume_token (parser->lexer);
17710 /* Finally, try to parse a block-declaration, or a function-definition. */
17711 else
17712 cp_parser_block_declaration (parser, /*statement_p=*/false);
17713 }
17714
17715 /* Parse a method signature. */
17716
17717 static tree
17718 cp_parser_objc_method_signature (cp_parser* parser)
17719 {
17720 tree rettype, kwdparms, optparms;
17721 bool ellipsis = false;
17722
17723 cp_parser_objc_method_type (parser);
17724 rettype = cp_parser_objc_typename (parser);
17725 kwdparms = cp_parser_objc_method_keyword_params (parser);
17726 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17727
17728 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17729 }
17730
17731 /* Pars an Objective-C method prototype list. */
17732
17733 static void
17734 cp_parser_objc_method_prototype_list (cp_parser* parser)
17735 {
17736 cp_token *token = cp_lexer_peek_token (parser->lexer);
17737
17738 while (token->keyword != RID_AT_END)
17739 {
17740 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17741 {
17742 objc_add_method_declaration
17743 (cp_parser_objc_method_signature (parser));
17744 cp_parser_consume_semicolon_at_end_of_statement (parser);
17745 }
17746 else
17747 /* Allow for interspersed non-ObjC++ code. */
17748 cp_parser_objc_interstitial_code (parser);
17749
17750 token = cp_lexer_peek_token (parser->lexer);
17751 }
17752
17753 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17754 objc_finish_interface ();
17755 }
17756
17757 /* Parse an Objective-C method definition list. */
17758
17759 static void
17760 cp_parser_objc_method_definition_list (cp_parser* parser)
17761 {
17762 cp_token *token = cp_lexer_peek_token (parser->lexer);
17763
17764 while (token->keyword != RID_AT_END)
17765 {
17766 tree meth;
17767
17768 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17769 {
17770 push_deferring_access_checks (dk_deferred);
17771 objc_start_method_definition
17772 (cp_parser_objc_method_signature (parser));
17773
17774 /* For historical reasons, we accept an optional semicolon. */
17775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17776 cp_lexer_consume_token (parser->lexer);
17777
17778 perform_deferred_access_checks ();
17779 stop_deferring_access_checks ();
17780 meth = cp_parser_function_definition_after_declarator (parser,
17781 false);
17782 pop_deferring_access_checks ();
17783 objc_finish_method_definition (meth);
17784 }
17785 else
17786 /* Allow for interspersed non-ObjC++ code. */
17787 cp_parser_objc_interstitial_code (parser);
17788
17789 token = cp_lexer_peek_token (parser->lexer);
17790 }
17791
17792 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17793 objc_finish_implementation ();
17794 }
17795
17796 /* Parse Objective-C ivars. */
17797
17798 static void
17799 cp_parser_objc_class_ivars (cp_parser* parser)
17800 {
17801 cp_token *token = cp_lexer_peek_token (parser->lexer);
17802
17803 if (token->type != CPP_OPEN_BRACE)
17804 return; /* No ivars specified. */
17805
17806 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17807 token = cp_lexer_peek_token (parser->lexer);
17808
17809 while (token->type != CPP_CLOSE_BRACE)
17810 {
17811 cp_decl_specifier_seq declspecs;
17812 int decl_class_or_enum_p;
17813 tree prefix_attributes;
17814
17815 cp_parser_objc_visibility_spec (parser);
17816
17817 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17818 break;
17819
17820 cp_parser_decl_specifier_seq (parser,
17821 CP_PARSER_FLAGS_OPTIONAL,
17822 &declspecs,
17823 &decl_class_or_enum_p);
17824 prefix_attributes = declspecs.attributes;
17825 declspecs.attributes = NULL_TREE;
17826
17827 /* Keep going until we hit the `;' at the end of the
17828 declaration. */
17829 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17830 {
17831 tree width = NULL_TREE, attributes, first_attribute, decl;
17832 cp_declarator *declarator = NULL;
17833 int ctor_dtor_or_conv_p;
17834
17835 /* Check for a (possibly unnamed) bitfield declaration. */
17836 token = cp_lexer_peek_token (parser->lexer);
17837 if (token->type == CPP_COLON)
17838 goto eat_colon;
17839
17840 if (token->type == CPP_NAME
17841 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17842 == CPP_COLON))
17843 {
17844 /* Get the name of the bitfield. */
17845 declarator = make_id_declarator (NULL_TREE,
17846 cp_parser_identifier (parser),
17847 sfk_none);
17848
17849 eat_colon:
17850 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17851 /* Get the width of the bitfield. */
17852 width
17853 = cp_parser_constant_expression (parser,
17854 /*allow_non_constant=*/false,
17855 NULL);
17856 }
17857 else
17858 {
17859 /* Parse the declarator. */
17860 declarator
17861 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17862 &ctor_dtor_or_conv_p,
17863 /*parenthesized_p=*/NULL,
17864 /*member_p=*/false);
17865 }
17866
17867 /* Look for attributes that apply to the ivar. */
17868 attributes = cp_parser_attributes_opt (parser);
17869 /* Remember which attributes are prefix attributes and
17870 which are not. */
17871 first_attribute = attributes;
17872 /* Combine the attributes. */
17873 attributes = chainon (prefix_attributes, attributes);
17874
17875 if (width)
17876 {
17877 /* Create the bitfield declaration. */
17878 decl = grokbitfield (declarator, &declspecs, width);
17879 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17880 }
17881 else
17882 decl = grokfield (declarator, &declspecs,
17883 NULL_TREE, /*init_const_expr_p=*/false,
17884 NULL_TREE, attributes);
17885
17886 /* Add the instance variable. */
17887 objc_add_instance_variable (decl);
17888
17889 /* Reset PREFIX_ATTRIBUTES. */
17890 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17891 attributes = TREE_CHAIN (attributes);
17892 if (attributes)
17893 TREE_CHAIN (attributes) = NULL_TREE;
17894
17895 token = cp_lexer_peek_token (parser->lexer);
17896
17897 if (token->type == CPP_COMMA)
17898 {
17899 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17900 continue;
17901 }
17902 break;
17903 }
17904
17905 cp_parser_consume_semicolon_at_end_of_statement (parser);
17906 token = cp_lexer_peek_token (parser->lexer);
17907 }
17908
17909 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17910 /* For historical reasons, we accept an optional semicolon. */
17911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17912 cp_lexer_consume_token (parser->lexer);
17913 }
17914
17915 /* Parse an Objective-C protocol declaration. */
17916
17917 static void
17918 cp_parser_objc_protocol_declaration (cp_parser* parser)
17919 {
17920 tree proto, protorefs;
17921 cp_token *tok;
17922
17923 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17925 {
17926 error ("identifier expected after %<@protocol%>");
17927 goto finish;
17928 }
17929
17930 /* See if we have a forward declaration or a definition. */
17931 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17932
17933 /* Try a forward declaration first. */
17934 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17935 {
17936 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17937 finish:
17938 cp_parser_consume_semicolon_at_end_of_statement (parser);
17939 }
17940
17941 /* Ok, we got a full-fledged definition (or at least should). */
17942 else
17943 {
17944 proto = cp_parser_identifier (parser);
17945 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17946 objc_start_protocol (proto, protorefs);
17947 cp_parser_objc_method_prototype_list (parser);
17948 }
17949 }
17950
17951 /* Parse an Objective-C superclass or category. */
17952
17953 static void
17954 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17955 tree *categ)
17956 {
17957 cp_token *next = cp_lexer_peek_token (parser->lexer);
17958
17959 *super = *categ = NULL_TREE;
17960 if (next->type == CPP_COLON)
17961 {
17962 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17963 *super = cp_parser_identifier (parser);
17964 }
17965 else if (next->type == CPP_OPEN_PAREN)
17966 {
17967 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17968 *categ = cp_parser_identifier (parser);
17969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17970 }
17971 }
17972
17973 /* Parse an Objective-C class interface. */
17974
17975 static void
17976 cp_parser_objc_class_interface (cp_parser* parser)
17977 {
17978 tree name, super, categ, protos;
17979
17980 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17981 name = cp_parser_identifier (parser);
17982 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17983 protos = cp_parser_objc_protocol_refs_opt (parser);
17984
17985 /* We have either a class or a category on our hands. */
17986 if (categ)
17987 objc_start_category_interface (name, categ, protos);
17988 else
17989 {
17990 objc_start_class_interface (name, super, protos);
17991 /* Handle instance variable declarations, if any. */
17992 cp_parser_objc_class_ivars (parser);
17993 objc_continue_interface ();
17994 }
17995
17996 cp_parser_objc_method_prototype_list (parser);
17997 }
17998
17999 /* Parse an Objective-C class implementation. */
18000
18001 static void
18002 cp_parser_objc_class_implementation (cp_parser* parser)
18003 {
18004 tree name, super, categ;
18005
18006 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18007 name = cp_parser_identifier (parser);
18008 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18009
18010 /* We have either a class or a category on our hands. */
18011 if (categ)
18012 objc_start_category_implementation (name, categ);
18013 else
18014 {
18015 objc_start_class_implementation (name, super);
18016 /* Handle instance variable declarations, if any. */
18017 cp_parser_objc_class_ivars (parser);
18018 objc_continue_implementation ();
18019 }
18020
18021 cp_parser_objc_method_definition_list (parser);
18022 }
18023
18024 /* Consume the @end token and finish off the implementation. */
18025
18026 static void
18027 cp_parser_objc_end_implementation (cp_parser* parser)
18028 {
18029 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18030 objc_finish_implementation ();
18031 }
18032
18033 /* Parse an Objective-C declaration. */
18034
18035 static void
18036 cp_parser_objc_declaration (cp_parser* parser)
18037 {
18038 /* Try to figure out what kind of declaration is present. */
18039 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18040
18041 switch (kwd->keyword)
18042 {
18043 case RID_AT_ALIAS:
18044 cp_parser_objc_alias_declaration (parser);
18045 break;
18046 case RID_AT_CLASS:
18047 cp_parser_objc_class_declaration (parser);
18048 break;
18049 case RID_AT_PROTOCOL:
18050 cp_parser_objc_protocol_declaration (parser);
18051 break;
18052 case RID_AT_INTERFACE:
18053 cp_parser_objc_class_interface (parser);
18054 break;
18055 case RID_AT_IMPLEMENTATION:
18056 cp_parser_objc_class_implementation (parser);
18057 break;
18058 case RID_AT_END:
18059 cp_parser_objc_end_implementation (parser);
18060 break;
18061 default:
18062 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18063 cp_parser_skip_to_end_of_block_or_statement (parser);
18064 }
18065 }
18066
18067 /* Parse an Objective-C try-catch-finally statement.
18068
18069 objc-try-catch-finally-stmt:
18070 @try compound-statement objc-catch-clause-seq [opt]
18071 objc-finally-clause [opt]
18072
18073 objc-catch-clause-seq:
18074 objc-catch-clause objc-catch-clause-seq [opt]
18075
18076 objc-catch-clause:
18077 @catch ( exception-declaration ) compound-statement
18078
18079 objc-finally-clause
18080 @finally compound-statement
18081
18082 Returns NULL_TREE. */
18083
18084 static tree
18085 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18086 location_t location;
18087 tree stmt;
18088
18089 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18090 location = cp_lexer_peek_token (parser->lexer)->location;
18091 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18092 node, lest it get absorbed into the surrounding block. */
18093 stmt = push_stmt_list ();
18094 cp_parser_compound_statement (parser, NULL, false);
18095 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18096
18097 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18098 {
18099 cp_parameter_declarator *parmdecl;
18100 tree parm;
18101
18102 cp_lexer_consume_token (parser->lexer);
18103 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18104 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18105 parm = grokdeclarator (parmdecl->declarator,
18106 &parmdecl->decl_specifiers,
18107 PARM, /*initialized=*/0,
18108 /*attrlist=*/NULL);
18109 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18110 objc_begin_catch_clause (parm);
18111 cp_parser_compound_statement (parser, NULL, false);
18112 objc_finish_catch_clause ();
18113 }
18114
18115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18116 {
18117 cp_lexer_consume_token (parser->lexer);
18118 location = cp_lexer_peek_token (parser->lexer)->location;
18119 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18120 node, lest it get absorbed into the surrounding block. */
18121 stmt = push_stmt_list ();
18122 cp_parser_compound_statement (parser, NULL, false);
18123 objc_build_finally_clause (location, pop_stmt_list (stmt));
18124 }
18125
18126 return objc_finish_try_stmt ();
18127 }
18128
18129 /* Parse an Objective-C synchronized statement.
18130
18131 objc-synchronized-stmt:
18132 @synchronized ( expression ) compound-statement
18133
18134 Returns NULL_TREE. */
18135
18136 static tree
18137 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18138 location_t location;
18139 tree lock, stmt;
18140
18141 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18142
18143 location = cp_lexer_peek_token (parser->lexer)->location;
18144 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18145 lock = cp_parser_expression (parser, false);
18146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18147
18148 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18149 node, lest it get absorbed into the surrounding block. */
18150 stmt = push_stmt_list ();
18151 cp_parser_compound_statement (parser, NULL, false);
18152
18153 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18154 }
18155
18156 /* Parse an Objective-C throw statement.
18157
18158 objc-throw-stmt:
18159 @throw assignment-expression [opt] ;
18160
18161 Returns a constructed '@throw' statement. */
18162
18163 static tree
18164 cp_parser_objc_throw_statement (cp_parser *parser) {
18165 tree expr = NULL_TREE;
18166
18167 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18168
18169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18170 expr = cp_parser_assignment_expression (parser, false);
18171
18172 cp_parser_consume_semicolon_at_end_of_statement (parser);
18173
18174 return objc_build_throw_stmt (expr);
18175 }
18176
18177 /* Parse an Objective-C statement. */
18178
18179 static tree
18180 cp_parser_objc_statement (cp_parser * parser) {
18181 /* Try to figure out what kind of declaration is present. */
18182 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18183
18184 switch (kwd->keyword)
18185 {
18186 case RID_AT_TRY:
18187 return cp_parser_objc_try_catch_finally_statement (parser);
18188 case RID_AT_SYNCHRONIZED:
18189 return cp_parser_objc_synchronized_statement (parser);
18190 case RID_AT_THROW:
18191 return cp_parser_objc_throw_statement (parser);
18192 default:
18193 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18194 cp_parser_skip_to_end_of_block_or_statement (parser);
18195 }
18196
18197 return error_mark_node;
18198 }
18199 \f
18200 /* OpenMP 2.5 parsing routines. */
18201
18202 /* Returns name of the next clause.
18203 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18204 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18205 returned and the token is consumed. */
18206
18207 static pragma_omp_clause
18208 cp_parser_omp_clause_name (cp_parser *parser)
18209 {
18210 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18211
18212 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18213 result = PRAGMA_OMP_CLAUSE_IF;
18214 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18215 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18216 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18217 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18218 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18219 {
18220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18221 const char *p = IDENTIFIER_POINTER (id);
18222
18223 switch (p[0])
18224 {
18225 case 'c':
18226 if (!strcmp ("copyin", p))
18227 result = PRAGMA_OMP_CLAUSE_COPYIN;
18228 else if (!strcmp ("copyprivate", p))
18229 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18230 break;
18231 case 'f':
18232 if (!strcmp ("firstprivate", p))
18233 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18234 break;
18235 case 'l':
18236 if (!strcmp ("lastprivate", p))
18237 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18238 break;
18239 case 'n':
18240 if (!strcmp ("nowait", p))
18241 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18242 else if (!strcmp ("num_threads", p))
18243 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18244 break;
18245 case 'o':
18246 if (!strcmp ("ordered", p))
18247 result = PRAGMA_OMP_CLAUSE_ORDERED;
18248 break;
18249 case 'r':
18250 if (!strcmp ("reduction", p))
18251 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18252 break;
18253 case 's':
18254 if (!strcmp ("schedule", p))
18255 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18256 else if (!strcmp ("shared", p))
18257 result = PRAGMA_OMP_CLAUSE_SHARED;
18258 break;
18259 }
18260 }
18261
18262 if (result != PRAGMA_OMP_CLAUSE_NONE)
18263 cp_lexer_consume_token (parser->lexer);
18264
18265 return result;
18266 }
18267
18268 /* Validate that a clause of the given type does not already exist. */
18269
18270 static void
18271 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18272 {
18273 tree c;
18274
18275 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18276 if (OMP_CLAUSE_CODE (c) == code)
18277 {
18278 error ("too many %qs clauses", name);
18279 break;
18280 }
18281 }
18282
18283 /* OpenMP 2.5:
18284 variable-list:
18285 identifier
18286 variable-list , identifier
18287
18288 In addition, we match a closing parenthesis. An opening parenthesis
18289 will have been consumed by the caller.
18290
18291 If KIND is nonzero, create the appropriate node and install the decl
18292 in OMP_CLAUSE_DECL and add the node to the head of the list.
18293
18294 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18295 return the list created. */
18296
18297 static tree
18298 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18299 tree list)
18300 {
18301 while (1)
18302 {
18303 tree name, decl;
18304
18305 name = cp_parser_id_expression (parser, /*template_p=*/false,
18306 /*check_dependency_p=*/true,
18307 /*template_p=*/NULL,
18308 /*declarator_p=*/false,
18309 /*optional_p=*/false);
18310 if (name == error_mark_node)
18311 goto skip_comma;
18312
18313 decl = cp_parser_lookup_name_simple (parser, name);
18314 if (decl == error_mark_node)
18315 cp_parser_name_lookup_error (parser, name, decl, NULL);
18316 else if (kind != 0)
18317 {
18318 tree u = build_omp_clause (kind);
18319 OMP_CLAUSE_DECL (u) = decl;
18320 OMP_CLAUSE_CHAIN (u) = list;
18321 list = u;
18322 }
18323 else
18324 list = tree_cons (decl, NULL_TREE, list);
18325
18326 get_comma:
18327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18328 break;
18329 cp_lexer_consume_token (parser->lexer);
18330 }
18331
18332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18333 {
18334 int ending;
18335
18336 /* Try to resync to an unnested comma. Copied from
18337 cp_parser_parenthesized_expression_list. */
18338 skip_comma:
18339 ending = cp_parser_skip_to_closing_parenthesis (parser,
18340 /*recovering=*/true,
18341 /*or_comma=*/true,
18342 /*consume_paren=*/true);
18343 if (ending < 0)
18344 goto get_comma;
18345 }
18346
18347 return list;
18348 }
18349
18350 /* Similarly, but expect leading and trailing parenthesis. This is a very
18351 common case for omp clauses. */
18352
18353 static tree
18354 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18355 {
18356 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18357 return cp_parser_omp_var_list_no_open (parser, kind, list);
18358 return list;
18359 }
18360
18361 /* OpenMP 2.5:
18362 default ( shared | none ) */
18363
18364 static tree
18365 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18366 {
18367 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18368 tree c;
18369
18370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18371 return list;
18372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18373 {
18374 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18375 const char *p = IDENTIFIER_POINTER (id);
18376
18377 switch (p[0])
18378 {
18379 case 'n':
18380 if (strcmp ("none", p) != 0)
18381 goto invalid_kind;
18382 kind = OMP_CLAUSE_DEFAULT_NONE;
18383 break;
18384
18385 case 's':
18386 if (strcmp ("shared", p) != 0)
18387 goto invalid_kind;
18388 kind = OMP_CLAUSE_DEFAULT_SHARED;
18389 break;
18390
18391 default:
18392 goto invalid_kind;
18393 }
18394
18395 cp_lexer_consume_token (parser->lexer);
18396 }
18397 else
18398 {
18399 invalid_kind:
18400 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18401 }
18402
18403 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18405 /*or_comma=*/false,
18406 /*consume_paren=*/true);
18407
18408 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18409 return list;
18410
18411 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18412 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18413 OMP_CLAUSE_CHAIN (c) = list;
18414 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18415
18416 return c;
18417 }
18418
18419 /* OpenMP 2.5:
18420 if ( expression ) */
18421
18422 static tree
18423 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18424 {
18425 tree t, c;
18426
18427 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18428 return list;
18429
18430 t = cp_parser_condition (parser);
18431
18432 if (t == error_mark_node
18433 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18435 /*or_comma=*/false,
18436 /*consume_paren=*/true);
18437
18438 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18439
18440 c = build_omp_clause (OMP_CLAUSE_IF);
18441 OMP_CLAUSE_IF_EXPR (c) = t;
18442 OMP_CLAUSE_CHAIN (c) = list;
18443
18444 return c;
18445 }
18446
18447 /* OpenMP 2.5:
18448 nowait */
18449
18450 static tree
18451 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18452 {
18453 tree c;
18454
18455 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18456
18457 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18458 OMP_CLAUSE_CHAIN (c) = list;
18459 return c;
18460 }
18461
18462 /* OpenMP 2.5:
18463 num_threads ( expression ) */
18464
18465 static tree
18466 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18467 {
18468 tree t, c;
18469
18470 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18471 return list;
18472
18473 t = cp_parser_expression (parser, false);
18474
18475 if (t == error_mark_node
18476 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18478 /*or_comma=*/false,
18479 /*consume_paren=*/true);
18480
18481 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18482
18483 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18484 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18485 OMP_CLAUSE_CHAIN (c) = list;
18486
18487 return c;
18488 }
18489
18490 /* OpenMP 2.5:
18491 ordered */
18492
18493 static tree
18494 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18495 {
18496 tree c;
18497
18498 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18499
18500 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18501 OMP_CLAUSE_CHAIN (c) = list;
18502 return c;
18503 }
18504
18505 /* OpenMP 2.5:
18506 reduction ( reduction-operator : variable-list )
18507
18508 reduction-operator:
18509 One of: + * - & ^ | && || */
18510
18511 static tree
18512 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18513 {
18514 enum tree_code code;
18515 tree nlist, c;
18516
18517 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18518 return list;
18519
18520 switch (cp_lexer_peek_token (parser->lexer)->type)
18521 {
18522 case CPP_PLUS:
18523 code = PLUS_EXPR;
18524 break;
18525 case CPP_MULT:
18526 code = MULT_EXPR;
18527 break;
18528 case CPP_MINUS:
18529 code = MINUS_EXPR;
18530 break;
18531 case CPP_AND:
18532 code = BIT_AND_EXPR;
18533 break;
18534 case CPP_XOR:
18535 code = BIT_XOR_EXPR;
18536 break;
18537 case CPP_OR:
18538 code = BIT_IOR_EXPR;
18539 break;
18540 case CPP_AND_AND:
18541 code = TRUTH_ANDIF_EXPR;
18542 break;
18543 case CPP_OR_OR:
18544 code = TRUTH_ORIF_EXPR;
18545 break;
18546 default:
18547 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18548 resync_fail:
18549 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18550 /*or_comma=*/false,
18551 /*consume_paren=*/true);
18552 return list;
18553 }
18554 cp_lexer_consume_token (parser->lexer);
18555
18556 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18557 goto resync_fail;
18558
18559 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18560 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18561 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18562
18563 return nlist;
18564 }
18565
18566 /* OpenMP 2.5:
18567 schedule ( schedule-kind )
18568 schedule ( schedule-kind , expression )
18569
18570 schedule-kind:
18571 static | dynamic | guided | runtime */
18572
18573 static tree
18574 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18575 {
18576 tree c, t;
18577
18578 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18579 return list;
18580
18581 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18582
18583 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18584 {
18585 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18586 const char *p = IDENTIFIER_POINTER (id);
18587
18588 switch (p[0])
18589 {
18590 case 'd':
18591 if (strcmp ("dynamic", p) != 0)
18592 goto invalid_kind;
18593 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18594 break;
18595
18596 case 'g':
18597 if (strcmp ("guided", p) != 0)
18598 goto invalid_kind;
18599 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18600 break;
18601
18602 case 'r':
18603 if (strcmp ("runtime", p) != 0)
18604 goto invalid_kind;
18605 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18606 break;
18607
18608 default:
18609 goto invalid_kind;
18610 }
18611 }
18612 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18613 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18614 else
18615 goto invalid_kind;
18616 cp_lexer_consume_token (parser->lexer);
18617
18618 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18619 {
18620 cp_lexer_consume_token (parser->lexer);
18621
18622 t = cp_parser_assignment_expression (parser, false);
18623
18624 if (t == error_mark_node)
18625 goto resync_fail;
18626 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18627 error ("schedule %<runtime%> does not take "
18628 "a %<chunk_size%> parameter");
18629 else
18630 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18631
18632 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18633 goto resync_fail;
18634 }
18635 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18636 goto resync_fail;
18637
18638 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18639 OMP_CLAUSE_CHAIN (c) = list;
18640 return c;
18641
18642 invalid_kind:
18643 cp_parser_error (parser, "invalid schedule kind");
18644 resync_fail:
18645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18646 /*or_comma=*/false,
18647 /*consume_paren=*/true);
18648 return list;
18649 }
18650
18651 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18652 is a bitmask in MASK. Return the list of clauses found; the result
18653 of clause default goes in *pdefault. */
18654
18655 static tree
18656 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18657 const char *where, cp_token *pragma_tok)
18658 {
18659 tree clauses = NULL;
18660
18661 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18662 {
18663 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18664 const char *c_name;
18665 tree prev = clauses;
18666
18667 switch (c_kind)
18668 {
18669 case PRAGMA_OMP_CLAUSE_COPYIN:
18670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18671 c_name = "copyin";
18672 break;
18673 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18674 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18675 clauses);
18676 c_name = "copyprivate";
18677 break;
18678 case PRAGMA_OMP_CLAUSE_DEFAULT:
18679 clauses = cp_parser_omp_clause_default (parser, clauses);
18680 c_name = "default";
18681 break;
18682 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18684 clauses);
18685 c_name = "firstprivate";
18686 break;
18687 case PRAGMA_OMP_CLAUSE_IF:
18688 clauses = cp_parser_omp_clause_if (parser, clauses);
18689 c_name = "if";
18690 break;
18691 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18692 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18693 clauses);
18694 c_name = "lastprivate";
18695 break;
18696 case PRAGMA_OMP_CLAUSE_NOWAIT:
18697 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18698 c_name = "nowait";
18699 break;
18700 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18701 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18702 c_name = "num_threads";
18703 break;
18704 case PRAGMA_OMP_CLAUSE_ORDERED:
18705 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18706 c_name = "ordered";
18707 break;
18708 case PRAGMA_OMP_CLAUSE_PRIVATE:
18709 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18710 clauses);
18711 c_name = "private";
18712 break;
18713 case PRAGMA_OMP_CLAUSE_REDUCTION:
18714 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18715 c_name = "reduction";
18716 break;
18717 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18718 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18719 c_name = "schedule";
18720 break;
18721 case PRAGMA_OMP_CLAUSE_SHARED:
18722 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18723 clauses);
18724 c_name = "shared";
18725 break;
18726 default:
18727 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18728 goto saw_error;
18729 }
18730
18731 if (((mask >> c_kind) & 1) == 0)
18732 {
18733 /* Remove the invalid clause(s) from the list to avoid
18734 confusing the rest of the compiler. */
18735 clauses = prev;
18736 error ("%qs is not valid for %qs", c_name, where);
18737 }
18738 }
18739 saw_error:
18740 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18741 return finish_omp_clauses (clauses);
18742 }
18743
18744 /* OpenMP 2.5:
18745 structured-block:
18746 statement
18747
18748 In practice, we're also interested in adding the statement to an
18749 outer node. So it is convenient if we work around the fact that
18750 cp_parser_statement calls add_stmt. */
18751
18752 static unsigned
18753 cp_parser_begin_omp_structured_block (cp_parser *parser)
18754 {
18755 unsigned save = parser->in_statement;
18756
18757 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18758 This preserves the "not within loop or switch" style error messages
18759 for nonsense cases like
18760 void foo() {
18761 #pragma omp single
18762 break;
18763 }
18764 */
18765 if (parser->in_statement)
18766 parser->in_statement = IN_OMP_BLOCK;
18767
18768 return save;
18769 }
18770
18771 static void
18772 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18773 {
18774 parser->in_statement = save;
18775 }
18776
18777 static tree
18778 cp_parser_omp_structured_block (cp_parser *parser)
18779 {
18780 tree stmt = begin_omp_structured_block ();
18781 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18782
18783 cp_parser_statement (parser, NULL_TREE, false, NULL);
18784
18785 cp_parser_end_omp_structured_block (parser, save);
18786 return finish_omp_structured_block (stmt);
18787 }
18788
18789 /* OpenMP 2.5:
18790 # pragma omp atomic new-line
18791 expression-stmt
18792
18793 expression-stmt:
18794 x binop= expr | x++ | ++x | x-- | --x
18795 binop:
18796 +, *, -, /, &, ^, |, <<, >>
18797
18798 where x is an lvalue expression with scalar type. */
18799
18800 static void
18801 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18802 {
18803 tree lhs, rhs;
18804 enum tree_code code;
18805
18806 cp_parser_require_pragma_eol (parser, pragma_tok);
18807
18808 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18809 /*cast_p=*/false);
18810 switch (TREE_CODE (lhs))
18811 {
18812 case ERROR_MARK:
18813 goto saw_error;
18814
18815 case PREINCREMENT_EXPR:
18816 case POSTINCREMENT_EXPR:
18817 lhs = TREE_OPERAND (lhs, 0);
18818 code = PLUS_EXPR;
18819 rhs = integer_one_node;
18820 break;
18821
18822 case PREDECREMENT_EXPR:
18823 case POSTDECREMENT_EXPR:
18824 lhs = TREE_OPERAND (lhs, 0);
18825 code = MINUS_EXPR;
18826 rhs = integer_one_node;
18827 break;
18828
18829 default:
18830 switch (cp_lexer_peek_token (parser->lexer)->type)
18831 {
18832 case CPP_MULT_EQ:
18833 code = MULT_EXPR;
18834 break;
18835 case CPP_DIV_EQ:
18836 code = TRUNC_DIV_EXPR;
18837 break;
18838 case CPP_PLUS_EQ:
18839 code = PLUS_EXPR;
18840 break;
18841 case CPP_MINUS_EQ:
18842 code = MINUS_EXPR;
18843 break;
18844 case CPP_LSHIFT_EQ:
18845 code = LSHIFT_EXPR;
18846 break;
18847 case CPP_RSHIFT_EQ:
18848 code = RSHIFT_EXPR;
18849 break;
18850 case CPP_AND_EQ:
18851 code = BIT_AND_EXPR;
18852 break;
18853 case CPP_OR_EQ:
18854 code = BIT_IOR_EXPR;
18855 break;
18856 case CPP_XOR_EQ:
18857 code = BIT_XOR_EXPR;
18858 break;
18859 default:
18860 cp_parser_error (parser,
18861 "invalid operator for %<#pragma omp atomic%>");
18862 goto saw_error;
18863 }
18864 cp_lexer_consume_token (parser->lexer);
18865
18866 rhs = cp_parser_expression (parser, false);
18867 if (rhs == error_mark_node)
18868 goto saw_error;
18869 break;
18870 }
18871 finish_omp_atomic (code, lhs, rhs);
18872 cp_parser_consume_semicolon_at_end_of_statement (parser);
18873 return;
18874
18875 saw_error:
18876 cp_parser_skip_to_end_of_block_or_statement (parser);
18877 }
18878
18879
18880 /* OpenMP 2.5:
18881 # pragma omp barrier new-line */
18882
18883 static void
18884 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18885 {
18886 cp_parser_require_pragma_eol (parser, pragma_tok);
18887 finish_omp_barrier ();
18888 }
18889
18890 /* OpenMP 2.5:
18891 # pragma omp critical [(name)] new-line
18892 structured-block */
18893
18894 static tree
18895 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18896 {
18897 tree stmt, name = NULL;
18898
18899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18900 {
18901 cp_lexer_consume_token (parser->lexer);
18902
18903 name = cp_parser_identifier (parser);
18904
18905 if (name == error_mark_node
18906 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18908 /*or_comma=*/false,
18909 /*consume_paren=*/true);
18910 if (name == error_mark_node)
18911 name = NULL;
18912 }
18913 cp_parser_require_pragma_eol (parser, pragma_tok);
18914
18915 stmt = cp_parser_omp_structured_block (parser);
18916 return c_finish_omp_critical (stmt, name);
18917 }
18918
18919 /* OpenMP 2.5:
18920 # pragma omp flush flush-vars[opt] new-line
18921
18922 flush-vars:
18923 ( variable-list ) */
18924
18925 static void
18926 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18927 {
18928 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18929 (void) cp_parser_omp_var_list (parser, 0, NULL);
18930 cp_parser_require_pragma_eol (parser, pragma_tok);
18931
18932 finish_omp_flush ();
18933 }
18934
18935 /* Parse the restricted form of the for statment allowed by OpenMP. */
18936
18937 static tree
18938 cp_parser_omp_for_loop (cp_parser *parser)
18939 {
18940 tree init, cond, incr, body, decl, pre_body;
18941 location_t loc;
18942
18943 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18944 {
18945 cp_parser_error (parser, "for statement expected");
18946 return NULL;
18947 }
18948 loc = cp_lexer_consume_token (parser->lexer)->location;
18949 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18950 return NULL;
18951
18952 init = decl = NULL;
18953 pre_body = push_stmt_list ();
18954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18955 {
18956 cp_decl_specifier_seq type_specifiers;
18957
18958 /* First, try to parse as an initialized declaration. See
18959 cp_parser_condition, from whence the bulk of this is copied. */
18960
18961 cp_parser_parse_tentatively (parser);
18962 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18963 &type_specifiers);
18964 if (!cp_parser_error_occurred (parser))
18965 {
18966 tree asm_specification, attributes;
18967 cp_declarator *declarator;
18968
18969 declarator = cp_parser_declarator (parser,
18970 CP_PARSER_DECLARATOR_NAMED,
18971 /*ctor_dtor_or_conv_p=*/NULL,
18972 /*parenthesized_p=*/NULL,
18973 /*member_p=*/false);
18974 attributes = cp_parser_attributes_opt (parser);
18975 asm_specification = cp_parser_asm_specification_opt (parser);
18976
18977 cp_parser_require (parser, CPP_EQ, "`='");
18978 if (cp_parser_parse_definitely (parser))
18979 {
18980 tree pushed_scope;
18981
18982 decl = start_decl (declarator, &type_specifiers,
18983 /*initialized_p=*/false, attributes,
18984 /*prefix_attributes=*/NULL_TREE,
18985 &pushed_scope);
18986
18987 init = cp_parser_assignment_expression (parser, false);
18988
18989 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18990 asm_specification, LOOKUP_ONLYCONVERTING);
18991
18992 if (pushed_scope)
18993 pop_scope (pushed_scope);
18994 }
18995 }
18996 else
18997 cp_parser_abort_tentative_parse (parser);
18998
18999 /* If parsing as an initialized declaration failed, try again as
19000 a simple expression. */
19001 if (decl == NULL)
19002 init = cp_parser_expression (parser, false);
19003 }
19004 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19005 pre_body = pop_stmt_list (pre_body);
19006
19007 cond = NULL;
19008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19009 cond = cp_parser_condition (parser);
19010 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19011
19012 incr = NULL;
19013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19014 incr = cp_parser_expression (parser, false);
19015
19016 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19017 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19018 /*or_comma=*/false,
19019 /*consume_paren=*/true);
19020
19021 /* Note that we saved the original contents of this flag when we entered
19022 the structured block, and so we don't need to re-save it here. */
19023 parser->in_statement = IN_OMP_FOR;
19024
19025 /* Note that the grammar doesn't call for a structured block here,
19026 though the loop as a whole is a structured block. */
19027 body = push_stmt_list ();
19028 cp_parser_statement (parser, NULL_TREE, false, NULL);
19029 body = pop_stmt_list (body);
19030
19031 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19032 }
19033
19034 /* OpenMP 2.5:
19035 #pragma omp for for-clause[optseq] new-line
19036 for-loop */
19037
19038 #define OMP_FOR_CLAUSE_MASK \
19039 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19040 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19041 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19042 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19043 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19044 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19045 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19046
19047 static tree
19048 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19049 {
19050 tree clauses, sb, ret;
19051 unsigned int save;
19052
19053 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19054 "#pragma omp for", pragma_tok);
19055
19056 sb = begin_omp_structured_block ();
19057 save = cp_parser_begin_omp_structured_block (parser);
19058
19059 ret = cp_parser_omp_for_loop (parser);
19060 if (ret)
19061 OMP_FOR_CLAUSES (ret) = clauses;
19062
19063 cp_parser_end_omp_structured_block (parser, save);
19064 add_stmt (finish_omp_structured_block (sb));
19065
19066 return ret;
19067 }
19068
19069 /* OpenMP 2.5:
19070 # pragma omp master new-line
19071 structured-block */
19072
19073 static tree
19074 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19075 {
19076 cp_parser_require_pragma_eol (parser, pragma_tok);
19077 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19078 }
19079
19080 /* OpenMP 2.5:
19081 # pragma omp ordered new-line
19082 structured-block */
19083
19084 static tree
19085 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19086 {
19087 cp_parser_require_pragma_eol (parser, pragma_tok);
19088 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19089 }
19090
19091 /* OpenMP 2.5:
19092
19093 section-scope:
19094 { section-sequence }
19095
19096 section-sequence:
19097 section-directive[opt] structured-block
19098 section-sequence section-directive structured-block */
19099
19100 static tree
19101 cp_parser_omp_sections_scope (cp_parser *parser)
19102 {
19103 tree stmt, substmt;
19104 bool error_suppress = false;
19105 cp_token *tok;
19106
19107 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19108 return NULL_TREE;
19109
19110 stmt = push_stmt_list ();
19111
19112 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19113 {
19114 unsigned save;
19115
19116 substmt = begin_omp_structured_block ();
19117 save = cp_parser_begin_omp_structured_block (parser);
19118
19119 while (1)
19120 {
19121 cp_parser_statement (parser, NULL_TREE, false, NULL);
19122
19123 tok = cp_lexer_peek_token (parser->lexer);
19124 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19125 break;
19126 if (tok->type == CPP_CLOSE_BRACE)
19127 break;
19128 if (tok->type == CPP_EOF)
19129 break;
19130 }
19131
19132 cp_parser_end_omp_structured_block (parser, save);
19133 substmt = finish_omp_structured_block (substmt);
19134 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19135 add_stmt (substmt);
19136 }
19137
19138 while (1)
19139 {
19140 tok = cp_lexer_peek_token (parser->lexer);
19141 if (tok->type == CPP_CLOSE_BRACE)
19142 break;
19143 if (tok->type == CPP_EOF)
19144 break;
19145
19146 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19147 {
19148 cp_lexer_consume_token (parser->lexer);
19149 cp_parser_require_pragma_eol (parser, tok);
19150 error_suppress = false;
19151 }
19152 else if (!error_suppress)
19153 {
19154 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19155 error_suppress = true;
19156 }
19157
19158 substmt = cp_parser_omp_structured_block (parser);
19159 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19160 add_stmt (substmt);
19161 }
19162 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19163
19164 substmt = pop_stmt_list (stmt);
19165
19166 stmt = make_node (OMP_SECTIONS);
19167 TREE_TYPE (stmt) = void_type_node;
19168 OMP_SECTIONS_BODY (stmt) = substmt;
19169
19170 add_stmt (stmt);
19171 return stmt;
19172 }
19173
19174 /* OpenMP 2.5:
19175 # pragma omp sections sections-clause[optseq] newline
19176 sections-scope */
19177
19178 #define OMP_SECTIONS_CLAUSE_MASK \
19179 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19180 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19181 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19182 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19183 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19184
19185 static tree
19186 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19187 {
19188 tree clauses, ret;
19189
19190 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19191 "#pragma omp sections", pragma_tok);
19192
19193 ret = cp_parser_omp_sections_scope (parser);
19194 if (ret)
19195 OMP_SECTIONS_CLAUSES (ret) = clauses;
19196
19197 return ret;
19198 }
19199
19200 /* OpenMP 2.5:
19201 # pragma parallel parallel-clause new-line
19202 # pragma parallel for parallel-for-clause new-line
19203 # pragma parallel sections parallel-sections-clause new-line */
19204
19205 #define OMP_PARALLEL_CLAUSE_MASK \
19206 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19207 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19208 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19209 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19210 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19211 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19212 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19213 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19214
19215 static tree
19216 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19217 {
19218 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19219 const char *p_name = "#pragma omp parallel";
19220 tree stmt, clauses, par_clause, ws_clause, block;
19221 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19222 unsigned int save;
19223
19224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19225 {
19226 cp_lexer_consume_token (parser->lexer);
19227 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19228 p_name = "#pragma omp parallel for";
19229 mask |= OMP_FOR_CLAUSE_MASK;
19230 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19231 }
19232 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19233 {
19234 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19235 const char *p = IDENTIFIER_POINTER (id);
19236 if (strcmp (p, "sections") == 0)
19237 {
19238 cp_lexer_consume_token (parser->lexer);
19239 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19240 p_name = "#pragma omp parallel sections";
19241 mask |= OMP_SECTIONS_CLAUSE_MASK;
19242 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19243 }
19244 }
19245
19246 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19247 block = begin_omp_parallel ();
19248 save = cp_parser_begin_omp_structured_block (parser);
19249
19250 switch (p_kind)
19251 {
19252 case PRAGMA_OMP_PARALLEL:
19253 cp_parser_already_scoped_statement (parser);
19254 par_clause = clauses;
19255 break;
19256
19257 case PRAGMA_OMP_PARALLEL_FOR:
19258 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19259 stmt = cp_parser_omp_for_loop (parser);
19260 if (stmt)
19261 OMP_FOR_CLAUSES (stmt) = ws_clause;
19262 break;
19263
19264 case PRAGMA_OMP_PARALLEL_SECTIONS:
19265 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19266 stmt = cp_parser_omp_sections_scope (parser);
19267 if (stmt)
19268 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19269 break;
19270
19271 default:
19272 gcc_unreachable ();
19273 }
19274
19275 cp_parser_end_omp_structured_block (parser, save);
19276 stmt = finish_omp_parallel (par_clause, block);
19277 if (p_kind != PRAGMA_OMP_PARALLEL)
19278 OMP_PARALLEL_COMBINED (stmt) = 1;
19279 return stmt;
19280 }
19281
19282 /* OpenMP 2.5:
19283 # pragma omp single single-clause[optseq] new-line
19284 structured-block */
19285
19286 #define OMP_SINGLE_CLAUSE_MASK \
19287 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19288 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19289 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19290 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19291
19292 static tree
19293 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19294 {
19295 tree stmt = make_node (OMP_SINGLE);
19296 TREE_TYPE (stmt) = void_type_node;
19297
19298 OMP_SINGLE_CLAUSES (stmt)
19299 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19300 "#pragma omp single", pragma_tok);
19301 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19302
19303 return add_stmt (stmt);
19304 }
19305
19306 /* OpenMP 2.5:
19307 # pragma omp threadprivate (variable-list) */
19308
19309 static void
19310 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19311 {
19312 tree vars;
19313
19314 vars = cp_parser_omp_var_list (parser, 0, NULL);
19315 cp_parser_require_pragma_eol (parser, pragma_tok);
19316
19317 finish_omp_threadprivate (vars);
19318 }
19319
19320 /* Main entry point to OpenMP statement pragmas. */
19321
19322 static void
19323 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19324 {
19325 tree stmt;
19326
19327 switch (pragma_tok->pragma_kind)
19328 {
19329 case PRAGMA_OMP_ATOMIC:
19330 cp_parser_omp_atomic (parser, pragma_tok);
19331 return;
19332 case PRAGMA_OMP_CRITICAL:
19333 stmt = cp_parser_omp_critical (parser, pragma_tok);
19334 break;
19335 case PRAGMA_OMP_FOR:
19336 stmt = cp_parser_omp_for (parser, pragma_tok);
19337 break;
19338 case PRAGMA_OMP_MASTER:
19339 stmt = cp_parser_omp_master (parser, pragma_tok);
19340 break;
19341 case PRAGMA_OMP_ORDERED:
19342 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19343 break;
19344 case PRAGMA_OMP_PARALLEL:
19345 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19346 break;
19347 case PRAGMA_OMP_SECTIONS:
19348 stmt = cp_parser_omp_sections (parser, pragma_tok);
19349 break;
19350 case PRAGMA_OMP_SINGLE:
19351 stmt = cp_parser_omp_single (parser, pragma_tok);
19352 break;
19353 default:
19354 gcc_unreachable ();
19355 }
19356
19357 if (stmt)
19358 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19359 }
19360 \f
19361 /* The parser. */
19362
19363 static GTY (()) cp_parser *the_parser;
19364
19365 \f
19366 /* Special handling for the first token or line in the file. The first
19367 thing in the file might be #pragma GCC pch_preprocess, which loads a
19368 PCH file, which is a GC collection point. So we need to handle this
19369 first pragma without benefit of an existing lexer structure.
19370
19371 Always returns one token to the caller in *FIRST_TOKEN. This is
19372 either the true first token of the file, or the first token after
19373 the initial pragma. */
19374
19375 static void
19376 cp_parser_initial_pragma (cp_token *first_token)
19377 {
19378 tree name = NULL;
19379
19380 cp_lexer_get_preprocessor_token (NULL, first_token);
19381 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19382 return;
19383
19384 cp_lexer_get_preprocessor_token (NULL, first_token);
19385 if (first_token->type == CPP_STRING)
19386 {
19387 name = first_token->u.value;
19388
19389 cp_lexer_get_preprocessor_token (NULL, first_token);
19390 if (first_token->type != CPP_PRAGMA_EOL)
19391 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19392 }
19393 else
19394 error ("expected string literal");
19395
19396 /* Skip to the end of the pragma. */
19397 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19398 cp_lexer_get_preprocessor_token (NULL, first_token);
19399
19400 /* Now actually load the PCH file. */
19401 if (name)
19402 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19403
19404 /* Read one more token to return to our caller. We have to do this
19405 after reading the PCH file in, since its pointers have to be
19406 live. */
19407 cp_lexer_get_preprocessor_token (NULL, first_token);
19408 }
19409
19410 /* Normal parsing of a pragma token. Here we can (and must) use the
19411 regular lexer. */
19412
19413 static bool
19414 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19415 {
19416 cp_token *pragma_tok;
19417 unsigned int id;
19418
19419 pragma_tok = cp_lexer_consume_token (parser->lexer);
19420 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19421 parser->lexer->in_pragma = true;
19422
19423 id = pragma_tok->pragma_kind;
19424 switch (id)
19425 {
19426 case PRAGMA_GCC_PCH_PREPROCESS:
19427 error ("%<#pragma GCC pch_preprocess%> must be first");
19428 break;
19429
19430 case PRAGMA_OMP_BARRIER:
19431 switch (context)
19432 {
19433 case pragma_compound:
19434 cp_parser_omp_barrier (parser, pragma_tok);
19435 return false;
19436 case pragma_stmt:
19437 error ("%<#pragma omp barrier%> may only be "
19438 "used in compound statements");
19439 break;
19440 default:
19441 goto bad_stmt;
19442 }
19443 break;
19444
19445 case PRAGMA_OMP_FLUSH:
19446 switch (context)
19447 {
19448 case pragma_compound:
19449 cp_parser_omp_flush (parser, pragma_tok);
19450 return false;
19451 case pragma_stmt:
19452 error ("%<#pragma omp flush%> may only be "
19453 "used in compound statements");
19454 break;
19455 default:
19456 goto bad_stmt;
19457 }
19458 break;
19459
19460 case PRAGMA_OMP_THREADPRIVATE:
19461 cp_parser_omp_threadprivate (parser, pragma_tok);
19462 return false;
19463
19464 case PRAGMA_OMP_ATOMIC:
19465 case PRAGMA_OMP_CRITICAL:
19466 case PRAGMA_OMP_FOR:
19467 case PRAGMA_OMP_MASTER:
19468 case PRAGMA_OMP_ORDERED:
19469 case PRAGMA_OMP_PARALLEL:
19470 case PRAGMA_OMP_SECTIONS:
19471 case PRAGMA_OMP_SINGLE:
19472 if (context == pragma_external)
19473 goto bad_stmt;
19474 cp_parser_omp_construct (parser, pragma_tok);
19475 return true;
19476
19477 case PRAGMA_OMP_SECTION:
19478 error ("%<#pragma omp section%> may only be used in "
19479 "%<#pragma omp sections%> construct");
19480 break;
19481
19482 default:
19483 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19484 c_invoke_pragma_handler (id);
19485 break;
19486
19487 bad_stmt:
19488 cp_parser_error (parser, "expected declaration specifiers");
19489 break;
19490 }
19491
19492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19493 return false;
19494 }
19495
19496 /* The interface the pragma parsers have to the lexer. */
19497
19498 enum cpp_ttype
19499 pragma_lex (tree *value)
19500 {
19501 cp_token *tok;
19502 enum cpp_ttype ret;
19503
19504 tok = cp_lexer_peek_token (the_parser->lexer);
19505
19506 ret = tok->type;
19507 *value = tok->u.value;
19508
19509 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19510 ret = CPP_EOF;
19511 else if (ret == CPP_STRING)
19512 *value = cp_parser_string_literal (the_parser, false, false);
19513 else
19514 {
19515 cp_lexer_consume_token (the_parser->lexer);
19516 if (ret == CPP_KEYWORD)
19517 ret = CPP_NAME;
19518 }
19519
19520 return ret;
19521 }
19522
19523 \f
19524 /* External interface. */
19525
19526 /* Parse one entire translation unit. */
19527
19528 void
19529 c_parse_file (void)
19530 {
19531 bool error_occurred;
19532 static bool already_called = false;
19533
19534 if (already_called)
19535 {
19536 sorry ("inter-module optimizations not implemented for C++");
19537 return;
19538 }
19539 already_called = true;
19540
19541 the_parser = cp_parser_new ();
19542 push_deferring_access_checks (flag_access_control
19543 ? dk_no_deferred : dk_no_check);
19544 error_occurred = cp_parser_translation_unit (the_parser);
19545 the_parser = NULL;
19546 }
19547
19548 #include "gt-cp-parser.h"
This page took 0.932009 seconds and 6 git commands to generate.