]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
re PR c++/31745 (ICE on invalid use of namespace)
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
50
51 struct tree_check GTY(())
52 {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
60 };
61
62 /* A C++ token. */
63
64 typedef struct cp_token GTY (())
65 {
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
71 /* Token flags. */
72 unsigned char flags;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
93 location_t location;
94 } cp_token;
95
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
100
101 static const cp_token eof_token =
102 {
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
105 0
106 #else
107 {0, 0}
108 #endif
109 };
110
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
114 it is created. */
115
116 typedef struct cp_lexer GTY (())
117 {
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
122 buffer. */
123 size_t buffer_length;
124
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
128
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
132
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
136 tokens. */
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
141
142 /* True if we should output debugging information. */
143 bool debugging_p;
144
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
147 bool in_pragma;
148 } cp_lexer;
149
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
154 a lexer. */
155
156 typedef struct cp_token_cache GTY(())
157 {
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
160
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
163 } cp_token_cache;
164
165 /* Prototypes. */
166
167 static cp_lexer *cp_lexer_new_main
168 (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172 (cp_lexer *);
173 static int cp_lexer_saving_tokens
174 (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176 (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182 (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192 (cp_lexer *);
193 static void cp_lexer_purge_token
194 (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198 (cp_lexer *);
199 static void cp_lexer_commit_tokens
200 (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202 (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207 (cp_lexer *);
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
221
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
224
225 static void cp_parser_initial_pragma
226 (cp_token *);
227
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
231
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255
256 /* Variables. */
257
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
262
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264 preprocessor. */
265
266 static cp_lexer *
267 cp_lexer_new_main (void)
268 {
269 cp_token first_token;
270 cp_lexer *lexer;
271 cp_token *pos;
272 size_t alloc;
273 size_t space;
274 cp_token *buffer;
275
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
280
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
283
284 c_common_no_more_pch ();
285
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
288
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
295
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
299
300 /* Put the first token in the buffer. */
301 space = alloc;
302 pos = buffer;
303 *pos = first_token;
304
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
307 {
308 pos++;
309 if (!--space)
310 {
311 space = alloc;
312 alloc *= 2;
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
315 }
316 cp_lexer_get_preprocessor_token (lexer, pos);
317 }
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
330 }
331
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
334
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
337 {
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
341
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
347
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
350
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
354 #endif
355
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
357 return lexer;
358 }
359
360 /* Frees all resources associated with LEXER. */
361
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
364 {
365 if (lexer->buffer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
368 ggc_free (lexer);
369 }
370
371 /* Returns nonzero if debugging information should be output. */
372
373 #ifdef ENABLE_CHECKING
374
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
377 {
378 return lexer->debugging_p;
379 }
380
381 #endif /* ENABLE_CHECKING */
382
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385 {
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
387
388 return lexer->next_token - previous_p;
389 }
390
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393 {
394 return pos;
395 }
396
397 /* nonzero if we are presently saving tokens. */
398
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
401 {
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403 }
404
405 /* Store the next token from the preprocessor in *TOKEN. Return true
406 if we reach EOF. */
407
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410 cp_token *token)
411 {
412 static int is_extern_c = 0;
413
414 /* Get a new token from the preprocessor. */
415 token->type
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
421
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
428
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
431 {
432 if (C_IS_RESERVED_WORD (token->u.value))
433 {
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
443 }
444 else
445 {
446 if (warn_cxx0x_compat
447 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
448 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
449 {
450 /* Warn about the C++0x keyword (but still treat it as
451 an identifier). */
452 warning (OPT_Wc__0x_compat,
453 "identifier %<%s%> will become a keyword in C++0x",
454 IDENTIFIER_POINTER (token->u.value));
455
456 /* Clear out the C_RID_CODE so we don't warn about this
457 particular identifier-turned-keyword again. */
458 C_RID_CODE (token->u.value) = RID_MAX;
459 }
460
461 token->ambiguous_p = false;
462 token->keyword = RID_MAX;
463 }
464 }
465 /* Handle Objective-C++ keywords. */
466 else if (token->type == CPP_AT_NAME)
467 {
468 token->type = CPP_KEYWORD;
469 switch (C_RID_CODE (token->u.value))
470 {
471 /* Map 'class' to '@class', 'private' to '@private', etc. */
472 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
473 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
474 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
475 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
476 case RID_THROW: token->keyword = RID_AT_THROW; break;
477 case RID_TRY: token->keyword = RID_AT_TRY; break;
478 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
479 default: token->keyword = C_RID_CODE (token->u.value);
480 }
481 }
482 else if (token->type == CPP_PRAGMA)
483 {
484 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
485 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
486 token->u.value = NULL_TREE;
487 }
488 }
489
490 /* Update the globals input_location and in_system_header and the
491 input file stack from TOKEN. */
492 static inline void
493 cp_lexer_set_source_position_from_token (cp_token *token)
494 {
495 if (token->type != CPP_EOF)
496 {
497 input_location = token->location;
498 in_system_header = token->in_system_header;
499 restore_input_file_stack (token->input_file_stack_index);
500 }
501 }
502
503 /* Return a pointer to the next token in the token stream, but do not
504 consume it. */
505
506 static inline cp_token *
507 cp_lexer_peek_token (cp_lexer *lexer)
508 {
509 if (cp_lexer_debugging_p (lexer))
510 {
511 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
512 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
513 putc ('\n', cp_lexer_debug_stream);
514 }
515 return lexer->next_token;
516 }
517
518 /* Return true if the next token has the indicated TYPE. */
519
520 static inline bool
521 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
522 {
523 return cp_lexer_peek_token (lexer)->type == type;
524 }
525
526 /* Return true if the next token does not have the indicated TYPE. */
527
528 static inline bool
529 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
530 {
531 return !cp_lexer_next_token_is (lexer, type);
532 }
533
534 /* Return true if the next token is the indicated KEYWORD. */
535
536 static inline bool
537 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
538 {
539 return cp_lexer_peek_token (lexer)->keyword == keyword;
540 }
541
542 /* Return true if the next token is a keyword for a decl-specifier. */
543
544 static bool
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
546 {
547 cp_token *token;
548
549 token = cp_lexer_peek_token (lexer);
550 switch (token->keyword)
551 {
552 /* Storage classes. */
553 case RID_AUTO:
554 case RID_REGISTER:
555 case RID_STATIC:
556 case RID_EXTERN:
557 case RID_MUTABLE:
558 case RID_THREAD:
559 /* Elaborated type specifiers. */
560 case RID_ENUM:
561 case RID_CLASS:
562 case RID_STRUCT:
563 case RID_UNION:
564 case RID_TYPENAME:
565 /* Simple type specifiers. */
566 case RID_CHAR:
567 case RID_WCHAR:
568 case RID_BOOL:
569 case RID_SHORT:
570 case RID_INT:
571 case RID_LONG:
572 case RID_SIGNED:
573 case RID_UNSIGNED:
574 case RID_FLOAT:
575 case RID_DOUBLE:
576 case RID_VOID:
577 /* GNU extensions. */
578 case RID_ATTRIBUTE:
579 case RID_TYPEOF:
580 return true;
581
582 default:
583 return false;
584 }
585 }
586
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
592
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 {
596 cp_token *token;
597
598 /* N is 1-based, not zero-based. */
599 gcc_assert (n > 0);
600
601 if (cp_lexer_debugging_p (lexer))
602 fprintf (cp_lexer_debug_stream,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604
605 --n;
606 token = lexer->next_token;
607 gcc_assert (!n || token != &eof_token);
608 while (n != 0)
609 {
610 ++token;
611 if (token == lexer->last_token)
612 {
613 token = (cp_token *)&eof_token;
614 break;
615 }
616
617 if (token->type != CPP_PURGED)
618 --n;
619 }
620
621 if (cp_lexer_debugging_p (lexer))
622 {
623 cp_lexer_print_token (cp_lexer_debug_stream, token);
624 putc ('\n', cp_lexer_debug_stream);
625 }
626
627 return token;
628 }
629
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
632
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
635 {
636 cp_token *token = lexer->next_token;
637
638 gcc_assert (token != &eof_token);
639 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640
641 do
642 {
643 lexer->next_token++;
644 if (lexer->next_token == lexer->last_token)
645 {
646 lexer->next_token = (cp_token *)&eof_token;
647 break;
648 }
649
650 }
651 while (lexer->next_token->type == CPP_PURGED);
652
653 cp_lexer_set_source_position_from_token (token);
654
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
657 {
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659 cp_lexer_print_token (cp_lexer_debug_stream, token);
660 putc ('\n', cp_lexer_debug_stream);
661 }
662
663 return token;
664 }
665
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
668 token. */
669
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
672 {
673 cp_token *tok = lexer->next_token;
674
675 gcc_assert (tok != &eof_token);
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->u.value = NULL_TREE;
679 tok->keyword = RID_MAX;
680
681 do
682 {
683 tok++;
684 if (tok == lexer->last_token)
685 {
686 tok = (cp_token *)&eof_token;
687 break;
688 }
689 }
690 while (tok->type == CPP_PURGED);
691 lexer->next_token = tok;
692 }
693
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
697
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 {
701 cp_token *peek = lexer->next_token;
702
703 if (peek == &eof_token)
704 peek = lexer->last_token;
705
706 gcc_assert (tok < peek);
707
708 for ( tok += 1; tok != peek; tok += 1)
709 {
710 tok->type = CPP_PURGED;
711 tok->location = UNKNOWN_LOCATION;
712 tok->u.value = NULL_TREE;
713 tok->keyword = RID_MAX;
714 }
715 }
716
717 /* Begin saving tokens. All tokens consumed after this point will be
718 preserved. */
719
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
722 {
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726
727 VEC_safe_push (cp_token_position, heap,
728 lexer->saved_tokens, lexer->next_token);
729 }
730
731 /* Commit to the portion of the token stream most recently saved. */
732
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
735 {
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739
740 VEC_pop (cp_token_position, lexer->saved_tokens);
741 }
742
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
745
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 {
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752
753 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 }
755
756 /* Print a representation of the TOKEN on the STREAM. */
757
758 #ifdef ENABLE_CHECKING
759
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
762 {
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names[] = {
766 /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769 TTYPE_TABLE
770 #undef OP
771 #undef TK
772 /* C++ parser token types - see "Manifest constants", above. */
773 "KEYWORD",
774 "TEMPLATE_ID",
775 "NESTED_NAME_SPECIFIER",
776 "PURGED"
777 };
778
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token->type < ARRAY_SIZE(token_names));
782 fputs (token_names[token->type], stream);
783
784 /* For some tokens, print the associated data. */
785 switch (token->type)
786 {
787 case CPP_KEYWORD:
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 break;
792 /* else fall through */
793 case CPP_NAME:
794 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 break;
796
797 case CPP_STRING:
798 case CPP_WSTRING:
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800 break;
801
802 default:
803 break;
804 }
805 }
806
807 /* Start emitting debugging information. */
808
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
811 {
812 lexer->debugging_p = true;
813 }
814
815 /* Stop emitting debugging information. */
816
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
819 {
820 lexer->debugging_p = false;
821 }
822
823 #endif /* ENABLE_CHECKING */
824
825 /* Create a new cp_token_cache, representing a range of tokens. */
826
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
829 {
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
832 cache->last = last;
833 return cache;
834 }
835
836 \f
837 /* Decl-specifiers. */
838
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
840
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 {
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 }
846
847 /* Declarators. */
848
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
853
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
866
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
869
870 /* The obstack on which declarators and related data structures are
871 allocated. */
872 static struct obstack declarator_obstack;
873
874 /* Alloc BYTES from the declarator memory pool. */
875
876 static inline void *
877 alloc_declarator (size_t bytes)
878 {
879 return obstack_alloc (&declarator_obstack, bytes);
880 }
881
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
884
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
887 {
888 cp_declarator *declarator;
889
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
895
896 return declarator;
897 }
898
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 is, if any. */
904
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
908 {
909 cp_declarator *declarator;
910
911 /* It is valid to write:
912
913 class C { void f(); };
914 typedef C D;
915 void D::f();
916
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
920 well. */
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
932
933 return declarator;
934 }
935
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
939
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 {
943 cp_declarator *declarator;
944
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
949 if (target)
950 {
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
953 }
954 else
955 declarator->parameter_pack_p = false;
956
957 return declarator;
958 }
959
960 /* Like make_pointer_declarator -- but for references. */
961
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
964 {
965 cp_declarator *declarator;
966
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.pointer.qualifiers = cv_qualifiers;
970 declarator->u.pointer.class_type = NULL_TREE;
971 if (target)
972 {
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
975 }
976 else
977 declarator->parameter_pack_p = false;
978
979 return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
988 {
989 cp_declarator *declarator;
990
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
995
996 if (pointee)
997 {
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1000 }
1001 else
1002 declarator->parameter_pack_p = false;
1003
1004 return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014 cp_parameter_declarator *parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification)
1017 {
1018 cp_declarator *declarator;
1019
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 if (target)
1026 {
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1029 }
1030 else
1031 declarator->parameter_pack_p = false;
1032
1033 return declarator;
1034 }
1035
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1038
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1041 {
1042 cp_declarator *declarator;
1043
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1047 if (element)
1048 {
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1051 }
1052 else
1053 declarator->parameter_pack_p = false;
1054
1055 return declarator;
1056 }
1057
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1060 static bool
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 {
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1067 bool found = false;
1068 while (declarator && !found)
1069 {
1070 switch ((int)declarator->kind)
1071 {
1072 case cdk_id:
1073 case cdk_error:
1074 case cdk_array:
1075 case cdk_ptrmem:
1076 found = true;
1077 break;
1078
1079 default:
1080 declarator = declarator->declarator;
1081 break;
1082 }
1083 }
1084
1085 return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1097 {
1098 cp_parameter_declarator *parameter;
1099
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1105 else
1106 clear_decl_specs (&parameter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1110
1111 return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119 while (declarator)
1120 {
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1123 return true;
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1126 return false;
1127 declarator = declarator->declarator;
1128 }
1129 return false;
1130 }
1131
1132 /* The parser. */
1133
1134 /* Overview
1135 --------
1136
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1144
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1147 code processed.
1148
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1157
1158 Methodology
1159 -----------
1160
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1177
1178 Future Improvements
1179 -------------------
1180
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1185 case. */
1186
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1189
1190 typedef enum cp_parser_flags
1191 {
1192 /* No flags. */
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 } cp_parser_flags;
1200
1201 /* The different kinds of declarators we want to parse. */
1202
1203 typedef enum cp_parser_declarator_kind
1204 {
1205 /* We want an abstract declarator. */
1206 CP_PARSER_DECLARATOR_ABSTRACT,
1207 /* We want a named declarator. */
1208 CP_PARSER_DECLARATOR_NAMED,
1209 /* We don't mind, but the name must be an unqualified-id. */
1210 CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1212
1213 /* The precedence values used to parse binary expressions. The minimum value
1214 of PREC must be 1, because zero is reserved to quickly discriminate
1215 binary operators from other tokens. */
1216
1217 enum cp_parser_prec
1218 {
1219 PREC_NOT_OPERATOR,
1220 PREC_LOGICAL_OR_EXPRESSION,
1221 PREC_LOGICAL_AND_EXPRESSION,
1222 PREC_INCLUSIVE_OR_EXPRESSION,
1223 PREC_EXCLUSIVE_OR_EXPRESSION,
1224 PREC_AND_EXPRESSION,
1225 PREC_EQUALITY_EXPRESSION,
1226 PREC_RELATIONAL_EXPRESSION,
1227 PREC_SHIFT_EXPRESSION,
1228 PREC_ADDITIVE_EXPRESSION,
1229 PREC_MULTIPLICATIVE_EXPRESSION,
1230 PREC_PM_EXPRESSION,
1231 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1232 };
1233
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235 precedence value. */
1236
1237 typedef struct cp_parser_binary_operations_map_node
1238 {
1239 /* The token type. */
1240 enum cpp_ttype token_type;
1241 /* The corresponding tree code. */
1242 enum tree_code tree_type;
1243 /* The precedence of this operator. */
1244 enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1246
1247 /* The status of a tentative parse. */
1248
1249 typedef enum cp_parser_status_kind
1250 {
1251 /* No errors have occurred. */
1252 CP_PARSER_STATUS_KIND_NO_ERROR,
1253 /* An error has occurred. */
1254 CP_PARSER_STATUS_KIND_ERROR,
1255 /* We are committed to this tentative parse, whether or not an error
1256 has occurred. */
1257 CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1259
1260 typedef struct cp_parser_expression_stack_entry
1261 {
1262 /* Left hand side of the binary operation we are currently
1263 parsing. */
1264 tree lhs;
1265 /* Original tree code for left hand side, if it was a binary
1266 expression itself (used for -Wparentheses). */
1267 enum tree_code lhs_type;
1268 /* Tree code for the binary operation we are parsing. */
1269 enum tree_code tree_type;
1270 /* Precedence of the binary operation we are parsing. */
1271 int prec;
1272 } cp_parser_expression_stack_entry;
1273
1274 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1275 entries because precedence levels on the stack are monotonically
1276 increasing. */
1277 typedef struct cp_parser_expression_stack_entry
1278 cp_parser_expression_stack[NUM_PREC_VALUES];
1279
1280 /* Context that is saved and restored when parsing tentatively. */
1281 typedef struct cp_parser_context GTY (())
1282 {
1283 /* If this is a tentative parsing context, the status of the
1284 tentative parse. */
1285 enum cp_parser_status_kind status;
1286 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1287 that are looked up in this context must be looked up both in the
1288 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1289 the context of the containing expression. */
1290 tree object_type;
1291
1292 /* The next parsing context in the stack. */
1293 struct cp_parser_context *next;
1294 } cp_parser_context;
1295
1296 /* Prototypes. */
1297
1298 /* Constructors and destructors. */
1299
1300 static cp_parser_context *cp_parser_context_new
1301 (cp_parser_context *);
1302
1303 /* Class variables. */
1304
1305 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1306
1307 /* The operator-precedence table used by cp_parser_binary_expression.
1308 Transformed into an associative array (binops_by_token) by
1309 cp_parser_new. */
1310
1311 static const cp_parser_binary_operations_map_node binops[] = {
1312 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1313 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1314
1315 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318
1319 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321
1322 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324
1325 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329
1330 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1331 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1332
1333 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1334
1335 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1336
1337 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1338
1339 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1340
1341 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1342 };
1343
1344 /* The same as binops, but initialized by cp_parser_new so that
1345 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1346 for speed. */
1347 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1348
1349 /* Constructors and destructors. */
1350
1351 /* Construct a new context. The context below this one on the stack
1352 is given by NEXT. */
1353
1354 static cp_parser_context *
1355 cp_parser_context_new (cp_parser_context* next)
1356 {
1357 cp_parser_context *context;
1358
1359 /* Allocate the storage. */
1360 if (cp_parser_context_free_list != NULL)
1361 {
1362 /* Pull the first entry from the free list. */
1363 context = cp_parser_context_free_list;
1364 cp_parser_context_free_list = context->next;
1365 memset (context, 0, sizeof (*context));
1366 }
1367 else
1368 context = GGC_CNEW (cp_parser_context);
1369
1370 /* No errors have occurred yet in this context. */
1371 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1372 /* If this is not the bottomost context, copy information that we
1373 need from the previous context. */
1374 if (next)
1375 {
1376 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1377 expression, then we are parsing one in this context, too. */
1378 context->object_type = next->object_type;
1379 /* Thread the stack. */
1380 context->next = next;
1381 }
1382
1383 return context;
1384 }
1385
1386 /* The cp_parser structure represents the C++ parser. */
1387
1388 typedef struct cp_parser GTY(())
1389 {
1390 /* The lexer from which we are obtaining tokens. */
1391 cp_lexer *lexer;
1392
1393 /* The scope in which names should be looked up. If NULL_TREE, then
1394 we look up names in the scope that is currently open in the
1395 source program. If non-NULL, this is either a TYPE or
1396 NAMESPACE_DECL for the scope in which we should look. It can
1397 also be ERROR_MARK, when we've parsed a bogus scope.
1398
1399 This value is not cleared automatically after a name is looked
1400 up, so we must be careful to clear it before starting a new look
1401 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1402 will look up `Z' in the scope of `X', rather than the current
1403 scope.) Unfortunately, it is difficult to tell when name lookup
1404 is complete, because we sometimes peek at a token, look it up,
1405 and then decide not to consume it. */
1406 tree scope;
1407
1408 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1409 last lookup took place. OBJECT_SCOPE is used if an expression
1410 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1411 respectively. QUALIFYING_SCOPE is used for an expression of the
1412 form "X::Y"; it refers to X. */
1413 tree object_scope;
1414 tree qualifying_scope;
1415
1416 /* A stack of parsing contexts. All but the bottom entry on the
1417 stack will be tentative contexts.
1418
1419 We parse tentatively in order to determine which construct is in
1420 use in some situations. For example, in order to determine
1421 whether a statement is an expression-statement or a
1422 declaration-statement we parse it tentatively as a
1423 declaration-statement. If that fails, we then reparse the same
1424 token stream as an expression-statement. */
1425 cp_parser_context *context;
1426
1427 /* True if we are parsing GNU C++. If this flag is not set, then
1428 GNU extensions are not recognized. */
1429 bool allow_gnu_extensions_p;
1430
1431 /* TRUE if the `>' token should be interpreted as the greater-than
1432 operator. FALSE if it is the end of a template-id or
1433 template-parameter-list. In C++0x mode, this flag also applies to
1434 `>>' tokens, which are viewed as two consecutive `>' tokens when
1435 this flag is FALSE. */
1436 bool greater_than_is_operator_p;
1437
1438 /* TRUE if default arguments are allowed within a parameter list
1439 that starts at this point. FALSE if only a gnu extension makes
1440 them permissible. */
1441 bool default_arg_ok_p;
1442
1443 /* TRUE if we are parsing an integral constant-expression. See
1444 [expr.const] for a precise definition. */
1445 bool integral_constant_expression_p;
1446
1447 /* TRUE if we are parsing an integral constant-expression -- but a
1448 non-constant expression should be permitted as well. This flag
1449 is used when parsing an array bound so that GNU variable-length
1450 arrays are tolerated. */
1451 bool allow_non_integral_constant_expression_p;
1452
1453 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1454 been seen that makes the expression non-constant. */
1455 bool non_integral_constant_expression_p;
1456
1457 /* TRUE if local variable names and `this' are forbidden in the
1458 current context. */
1459 bool local_variables_forbidden_p;
1460
1461 /* TRUE if the declaration we are parsing is part of a
1462 linkage-specification of the form `extern string-literal
1463 declaration'. */
1464 bool in_unbraced_linkage_specification_p;
1465
1466 /* TRUE if we are presently parsing a declarator, after the
1467 direct-declarator. */
1468 bool in_declarator_p;
1469
1470 /* TRUE if we are presently parsing a template-argument-list. */
1471 bool in_template_argument_list_p;
1472
1473 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1474 to IN_OMP_BLOCK if parsing OpenMP structured block and
1475 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1476 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1477 iteration-statement, OpenMP block or loop within that switch. */
1478 #define IN_SWITCH_STMT 1
1479 #define IN_ITERATION_STMT 2
1480 #define IN_OMP_BLOCK 4
1481 #define IN_OMP_FOR 8
1482 #define IN_IF_STMT 16
1483 unsigned char in_statement;
1484
1485 /* TRUE if we are presently parsing the body of a switch statement.
1486 Note that this doesn't quite overlap with in_statement above.
1487 The difference relates to giving the right sets of error messages:
1488 "case not in switch" vs "break statement used with OpenMP...". */
1489 bool in_switch_statement_p;
1490
1491 /* TRUE if we are parsing a type-id in an expression context. In
1492 such a situation, both "type (expr)" and "type (type)" are valid
1493 alternatives. */
1494 bool in_type_id_in_expr_p;
1495
1496 /* TRUE if we are currently in a header file where declarations are
1497 implicitly extern "C". */
1498 bool implicit_extern_c;
1499
1500 /* TRUE if strings in expressions should be translated to the execution
1501 character set. */
1502 bool translate_strings_p;
1503
1504 /* TRUE if we are presently parsing the body of a function, but not
1505 a local class. */
1506 bool in_function_body;
1507
1508 /* If non-NULL, then we are parsing a construct where new type
1509 definitions are not permitted. The string stored here will be
1510 issued as an error message if a type is defined. */
1511 const char *type_definition_forbidden_message;
1512
1513 /* A list of lists. The outer list is a stack, used for member
1514 functions of local classes. At each level there are two sub-list,
1515 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1516 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1517 TREE_VALUE's. The functions are chained in reverse declaration
1518 order.
1519
1520 The TREE_PURPOSE sublist contains those functions with default
1521 arguments that need post processing, and the TREE_VALUE sublist
1522 contains those functions with definitions that need post
1523 processing.
1524
1525 These lists can only be processed once the outermost class being
1526 defined is complete. */
1527 tree unparsed_functions_queues;
1528
1529 /* The number of classes whose definitions are currently in
1530 progress. */
1531 unsigned num_classes_being_defined;
1532
1533 /* The number of template parameter lists that apply directly to the
1534 current declaration. */
1535 unsigned num_template_parameter_lists;
1536 } cp_parser;
1537
1538 /* Prototypes. */
1539
1540 /* Constructors and destructors. */
1541
1542 static cp_parser *cp_parser_new
1543 (void);
1544
1545 /* Routines to parse various constructs.
1546
1547 Those that return `tree' will return the error_mark_node (rather
1548 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1549 Sometimes, they will return an ordinary node if error-recovery was
1550 attempted, even though a parse error occurred. So, to check
1551 whether or not a parse error occurred, you should always use
1552 cp_parser_error_occurred. If the construct is optional (indicated
1553 either by an `_opt' in the name of the function that does the
1554 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1555 the construct is not present. */
1556
1557 /* Lexical conventions [gram.lex] */
1558
1559 static tree cp_parser_identifier
1560 (cp_parser *);
1561 static tree cp_parser_string_literal
1562 (cp_parser *, bool, bool);
1563
1564 /* Basic concepts [gram.basic] */
1565
1566 static bool cp_parser_translation_unit
1567 (cp_parser *);
1568
1569 /* Expressions [gram.expr] */
1570
1571 static tree cp_parser_primary_expression
1572 (cp_parser *, bool, bool, bool, cp_id_kind *);
1573 static tree cp_parser_id_expression
1574 (cp_parser *, bool, bool, bool *, bool, bool);
1575 static tree cp_parser_unqualified_id
1576 (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier_opt
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_class_or_namespace_name
1582 (cp_parser *, bool, bool, bool, bool, bool);
1583 static tree cp_parser_postfix_expression
1584 (cp_parser *, bool, bool);
1585 static tree cp_parser_postfix_open_square_expression
1586 (cp_parser *, tree, bool);
1587 static tree cp_parser_postfix_dot_deref_expression
1588 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1589 static tree cp_parser_parenthesized_expression_list
1590 (cp_parser *, bool, bool, bool, bool *);
1591 static void cp_parser_pseudo_destructor_name
1592 (cp_parser *, tree *, tree *);
1593 static tree cp_parser_unary_expression
1594 (cp_parser *, bool, bool);
1595 static enum tree_code cp_parser_unary_operator
1596 (cp_token *);
1597 static tree cp_parser_new_expression
1598 (cp_parser *);
1599 static tree cp_parser_new_placement
1600 (cp_parser *);
1601 static tree cp_parser_new_type_id
1602 (cp_parser *, tree *);
1603 static cp_declarator *cp_parser_new_declarator_opt
1604 (cp_parser *);
1605 static cp_declarator *cp_parser_direct_new_declarator
1606 (cp_parser *);
1607 static tree cp_parser_new_initializer
1608 (cp_parser *);
1609 static tree cp_parser_delete_expression
1610 (cp_parser *);
1611 static tree cp_parser_cast_expression
1612 (cp_parser *, bool, bool);
1613 static tree cp_parser_binary_expression
1614 (cp_parser *, bool);
1615 static tree cp_parser_question_colon_clause
1616 (cp_parser *, tree);
1617 static tree cp_parser_assignment_expression
1618 (cp_parser *, bool);
1619 static enum tree_code cp_parser_assignment_operator_opt
1620 (cp_parser *);
1621 static tree cp_parser_expression
1622 (cp_parser *, bool);
1623 static tree cp_parser_constant_expression
1624 (cp_parser *, bool, bool *);
1625 static tree cp_parser_builtin_offsetof
1626 (cp_parser *);
1627
1628 /* Statements [gram.stmt.stmt] */
1629
1630 static void cp_parser_statement
1631 (cp_parser *, tree, bool, bool *);
1632 static void cp_parser_label_for_labeled_statement
1633 (cp_parser *);
1634 static tree cp_parser_expression_statement
1635 (cp_parser *, tree);
1636 static tree cp_parser_compound_statement
1637 (cp_parser *, tree, bool);
1638 static void cp_parser_statement_seq_opt
1639 (cp_parser *, tree);
1640 static tree cp_parser_selection_statement
1641 (cp_parser *, bool *);
1642 static tree cp_parser_condition
1643 (cp_parser *);
1644 static tree cp_parser_iteration_statement
1645 (cp_parser *);
1646 static void cp_parser_for_init_statement
1647 (cp_parser *);
1648 static tree cp_parser_jump_statement
1649 (cp_parser *);
1650 static void cp_parser_declaration_statement
1651 (cp_parser *);
1652
1653 static tree cp_parser_implicitly_scoped_statement
1654 (cp_parser *, bool *);
1655 static void cp_parser_already_scoped_statement
1656 (cp_parser *);
1657
1658 /* Declarations [gram.dcl.dcl] */
1659
1660 static void cp_parser_declaration_seq_opt
1661 (cp_parser *);
1662 static void cp_parser_declaration
1663 (cp_parser *);
1664 static void cp_parser_block_declaration
1665 (cp_parser *, bool);
1666 static void cp_parser_simple_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_decl_specifier_seq
1669 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1670 static tree cp_parser_storage_class_specifier_opt
1671 (cp_parser *);
1672 static tree cp_parser_function_specifier_opt
1673 (cp_parser *, cp_decl_specifier_seq *);
1674 static tree cp_parser_type_specifier
1675 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1676 int *, bool *);
1677 static tree cp_parser_simple_type_specifier
1678 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1679 static tree cp_parser_type_name
1680 (cp_parser *);
1681 static tree cp_parser_elaborated_type_specifier
1682 (cp_parser *, bool, bool);
1683 static tree cp_parser_enum_specifier
1684 (cp_parser *);
1685 static void cp_parser_enumerator_list
1686 (cp_parser *, tree);
1687 static void cp_parser_enumerator_definition
1688 (cp_parser *, tree);
1689 static tree cp_parser_namespace_name
1690 (cp_parser *);
1691 static void cp_parser_namespace_definition
1692 (cp_parser *);
1693 static void cp_parser_namespace_body
1694 (cp_parser *);
1695 static tree cp_parser_qualified_namespace_specifier
1696 (cp_parser *);
1697 static void cp_parser_namespace_alias_definition
1698 (cp_parser *);
1699 static bool cp_parser_using_declaration
1700 (cp_parser *, bool);
1701 static void cp_parser_using_directive
1702 (cp_parser *);
1703 static void cp_parser_asm_definition
1704 (cp_parser *);
1705 static void cp_parser_linkage_specification
1706 (cp_parser *);
1707 static void cp_parser_static_assert
1708 (cp_parser *, bool);
1709
1710 /* Declarators [gram.dcl.decl] */
1711
1712 static tree cp_parser_init_declarator
1713 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1714 static cp_declarator *cp_parser_declarator
1715 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1716 static cp_declarator *cp_parser_direct_declarator
1717 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1718 static enum tree_code cp_parser_ptr_operator
1719 (cp_parser *, tree *, cp_cv_quals *);
1720 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1721 (cp_parser *);
1722 static tree cp_parser_declarator_id
1723 (cp_parser *, bool);
1724 static tree cp_parser_type_id
1725 (cp_parser *);
1726 static void cp_parser_type_specifier_seq
1727 (cp_parser *, bool, cp_decl_specifier_seq *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1729 (cp_parser *);
1730 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1731 (cp_parser *, bool *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration
1733 (cp_parser *, bool, bool *);
1734 static void cp_parser_function_body
1735 (cp_parser *);
1736 static tree cp_parser_initializer
1737 (cp_parser *, bool *, bool *);
1738 static tree cp_parser_initializer_clause
1739 (cp_parser *, bool *);
1740 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1741 (cp_parser *, bool *);
1742
1743 static bool cp_parser_ctor_initializer_opt_and_function_body
1744 (cp_parser *);
1745
1746 /* Classes [gram.class] */
1747
1748 static tree cp_parser_class_name
1749 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1750 static tree cp_parser_class_specifier
1751 (cp_parser *);
1752 static tree cp_parser_class_head
1753 (cp_parser *, bool *, tree *, tree *);
1754 static enum tag_types cp_parser_class_key
1755 (cp_parser *);
1756 static void cp_parser_member_specification_opt
1757 (cp_parser *);
1758 static void cp_parser_member_declaration
1759 (cp_parser *);
1760 static tree cp_parser_pure_specifier
1761 (cp_parser *);
1762 static tree cp_parser_constant_initializer
1763 (cp_parser *);
1764
1765 /* Derived classes [gram.class.derived] */
1766
1767 static tree cp_parser_base_clause
1768 (cp_parser *);
1769 static tree cp_parser_base_specifier
1770 (cp_parser *);
1771
1772 /* Special member functions [gram.special] */
1773
1774 static tree cp_parser_conversion_function_id
1775 (cp_parser *);
1776 static tree cp_parser_conversion_type_id
1777 (cp_parser *);
1778 static cp_declarator *cp_parser_conversion_declarator_opt
1779 (cp_parser *);
1780 static bool cp_parser_ctor_initializer_opt
1781 (cp_parser *);
1782 static void cp_parser_mem_initializer_list
1783 (cp_parser *);
1784 static tree cp_parser_mem_initializer
1785 (cp_parser *);
1786 static tree cp_parser_mem_initializer_id
1787 (cp_parser *);
1788
1789 /* Overloading [gram.over] */
1790
1791 static tree cp_parser_operator_function_id
1792 (cp_parser *);
1793 static tree cp_parser_operator
1794 (cp_parser *);
1795
1796 /* Templates [gram.temp] */
1797
1798 static void cp_parser_template_declaration
1799 (cp_parser *, bool);
1800 static tree cp_parser_template_parameter_list
1801 (cp_parser *);
1802 static tree cp_parser_template_parameter
1803 (cp_parser *, bool *, bool *);
1804 static tree cp_parser_type_parameter
1805 (cp_parser *, bool *);
1806 static tree cp_parser_template_id
1807 (cp_parser *, bool, bool, bool);
1808 static tree cp_parser_template_name
1809 (cp_parser *, bool, bool, bool, bool *);
1810 static tree cp_parser_template_argument_list
1811 (cp_parser *);
1812 static tree cp_parser_template_argument
1813 (cp_parser *);
1814 static void cp_parser_explicit_instantiation
1815 (cp_parser *);
1816 static void cp_parser_explicit_specialization
1817 (cp_parser *);
1818
1819 /* Exception handling [gram.exception] */
1820
1821 static tree cp_parser_try_block
1822 (cp_parser *);
1823 static bool cp_parser_function_try_block
1824 (cp_parser *);
1825 static void cp_parser_handler_seq
1826 (cp_parser *);
1827 static void cp_parser_handler
1828 (cp_parser *);
1829 static tree cp_parser_exception_declaration
1830 (cp_parser *);
1831 static tree cp_parser_throw_expression
1832 (cp_parser *);
1833 static tree cp_parser_exception_specification_opt
1834 (cp_parser *);
1835 static tree cp_parser_type_id_list
1836 (cp_parser *);
1837
1838 /* GNU Extensions */
1839
1840 static tree cp_parser_asm_specification_opt
1841 (cp_parser *);
1842 static tree cp_parser_asm_operand_list
1843 (cp_parser *);
1844 static tree cp_parser_asm_clobber_list
1845 (cp_parser *);
1846 static tree cp_parser_attributes_opt
1847 (cp_parser *);
1848 static tree cp_parser_attribute_list
1849 (cp_parser *);
1850 static bool cp_parser_extension_opt
1851 (cp_parser *, int *);
1852 static void cp_parser_label_declaration
1853 (cp_parser *);
1854
1855 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1856 static bool cp_parser_pragma
1857 (cp_parser *, enum pragma_context);
1858
1859 /* Objective-C++ Productions */
1860
1861 static tree cp_parser_objc_message_receiver
1862 (cp_parser *);
1863 static tree cp_parser_objc_message_args
1864 (cp_parser *);
1865 static tree cp_parser_objc_message_expression
1866 (cp_parser *);
1867 static tree cp_parser_objc_encode_expression
1868 (cp_parser *);
1869 static tree cp_parser_objc_defs_expression
1870 (cp_parser *);
1871 static tree cp_parser_objc_protocol_expression
1872 (cp_parser *);
1873 static tree cp_parser_objc_selector_expression
1874 (cp_parser *);
1875 static tree cp_parser_objc_expression
1876 (cp_parser *);
1877 static bool cp_parser_objc_selector_p
1878 (enum cpp_ttype);
1879 static tree cp_parser_objc_selector
1880 (cp_parser *);
1881 static tree cp_parser_objc_protocol_refs_opt
1882 (cp_parser *);
1883 static void cp_parser_objc_declaration
1884 (cp_parser *);
1885 static tree cp_parser_objc_statement
1886 (cp_parser *);
1887
1888 /* Utility Routines */
1889
1890 static tree cp_parser_lookup_name
1891 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1892 static tree cp_parser_lookup_name_simple
1893 (cp_parser *, tree);
1894 static tree cp_parser_maybe_treat_template_as_class
1895 (tree, bool);
1896 static bool cp_parser_check_declarator_template_parameters
1897 (cp_parser *, cp_declarator *);
1898 static bool cp_parser_check_template_parameters
1899 (cp_parser *, unsigned);
1900 static tree cp_parser_simple_cast_expression
1901 (cp_parser *);
1902 static tree cp_parser_global_scope_opt
1903 (cp_parser *, bool);
1904 static bool cp_parser_constructor_declarator_p
1905 (cp_parser *, bool);
1906 static tree cp_parser_function_definition_from_specifiers_and_declarator
1907 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1908 static tree cp_parser_function_definition_after_declarator
1909 (cp_parser *, bool);
1910 static void cp_parser_template_declaration_after_export
1911 (cp_parser *, bool);
1912 static void cp_parser_perform_template_parameter_access_checks
1913 (VEC (deferred_access_check,gc)*);
1914 static tree cp_parser_single_declaration
1915 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1916 static tree cp_parser_functional_cast
1917 (cp_parser *, tree);
1918 static tree cp_parser_save_member_function_body
1919 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1920 static tree cp_parser_enclosed_template_argument_list
1921 (cp_parser *);
1922 static void cp_parser_save_default_args
1923 (cp_parser *, tree);
1924 static void cp_parser_late_parsing_for_member
1925 (cp_parser *, tree);
1926 static void cp_parser_late_parsing_default_args
1927 (cp_parser *, tree);
1928 static tree cp_parser_sizeof_operand
1929 (cp_parser *, enum rid);
1930 static tree cp_parser_trait_expr
1931 (cp_parser *, enum rid);
1932 static bool cp_parser_declares_only_class_p
1933 (cp_parser *);
1934 static void cp_parser_set_storage_class
1935 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1936 static void cp_parser_set_decl_spec_type
1937 (cp_decl_specifier_seq *, tree, bool);
1938 static bool cp_parser_friend_p
1939 (const cp_decl_specifier_seq *);
1940 static cp_token *cp_parser_require
1941 (cp_parser *, enum cpp_ttype, const char *);
1942 static cp_token *cp_parser_require_keyword
1943 (cp_parser *, enum rid, const char *);
1944 static bool cp_parser_token_starts_function_definition_p
1945 (cp_token *);
1946 static bool cp_parser_next_token_starts_class_definition_p
1947 (cp_parser *);
1948 static bool cp_parser_next_token_ends_template_argument_p
1949 (cp_parser *);
1950 static bool cp_parser_nth_token_starts_template_argument_list_p
1951 (cp_parser *, size_t);
1952 static enum tag_types cp_parser_token_is_class_key
1953 (cp_token *);
1954 static void cp_parser_check_class_key
1955 (enum tag_types, tree type);
1956 static void cp_parser_check_access_in_redeclaration
1957 (tree type);
1958 static bool cp_parser_optional_template_keyword
1959 (cp_parser *);
1960 static void cp_parser_pre_parsed_nested_name_specifier
1961 (cp_parser *);
1962 static void cp_parser_cache_group
1963 (cp_parser *, enum cpp_ttype, unsigned);
1964 static void cp_parser_parse_tentatively
1965 (cp_parser *);
1966 static void cp_parser_commit_to_tentative_parse
1967 (cp_parser *);
1968 static void cp_parser_abort_tentative_parse
1969 (cp_parser *);
1970 static bool cp_parser_parse_definitely
1971 (cp_parser *);
1972 static inline bool cp_parser_parsing_tentatively
1973 (cp_parser *);
1974 static bool cp_parser_uncommitted_to_tentative_parse_p
1975 (cp_parser *);
1976 static void cp_parser_error
1977 (cp_parser *, const char *);
1978 static void cp_parser_name_lookup_error
1979 (cp_parser *, tree, tree, const char *);
1980 static bool cp_parser_simulate_error
1981 (cp_parser *);
1982 static bool cp_parser_check_type_definition
1983 (cp_parser *);
1984 static void cp_parser_check_for_definition_in_return_type
1985 (cp_declarator *, tree);
1986 static void cp_parser_check_for_invalid_template_id
1987 (cp_parser *, tree);
1988 static bool cp_parser_non_integral_constant_expression
1989 (cp_parser *, const char *);
1990 static void cp_parser_diagnose_invalid_type_name
1991 (cp_parser *, tree, tree);
1992 static bool cp_parser_parse_and_diagnose_invalid_type_name
1993 (cp_parser *);
1994 static int cp_parser_skip_to_closing_parenthesis
1995 (cp_parser *, bool, bool, bool);
1996 static void cp_parser_skip_to_end_of_statement
1997 (cp_parser *);
1998 static void cp_parser_consume_semicolon_at_end_of_statement
1999 (cp_parser *);
2000 static void cp_parser_skip_to_end_of_block_or_statement
2001 (cp_parser *);
2002 static bool cp_parser_skip_to_closing_brace
2003 (cp_parser *);
2004 static void cp_parser_skip_to_end_of_template_parameter_list
2005 (cp_parser *);
2006 static void cp_parser_skip_to_pragma_eol
2007 (cp_parser*, cp_token *);
2008 static bool cp_parser_error_occurred
2009 (cp_parser *);
2010 static bool cp_parser_allow_gnu_extensions_p
2011 (cp_parser *);
2012 static bool cp_parser_is_string_literal
2013 (cp_token *);
2014 static bool cp_parser_is_keyword
2015 (cp_token *, enum rid);
2016 static tree cp_parser_make_typename_type
2017 (cp_parser *, tree, tree);
2018
2019 /* Returns nonzero if we are parsing tentatively. */
2020
2021 static inline bool
2022 cp_parser_parsing_tentatively (cp_parser* parser)
2023 {
2024 return parser->context->next != NULL;
2025 }
2026
2027 /* Returns nonzero if TOKEN is a string literal. */
2028
2029 static bool
2030 cp_parser_is_string_literal (cp_token* token)
2031 {
2032 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2033 }
2034
2035 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2036
2037 static bool
2038 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2039 {
2040 return token->keyword == keyword;
2041 }
2042
2043 /* If not parsing tentatively, issue a diagnostic of the form
2044 FILE:LINE: MESSAGE before TOKEN
2045 where TOKEN is the next token in the input stream. MESSAGE
2046 (specified by the caller) is usually of the form "expected
2047 OTHER-TOKEN". */
2048
2049 static void
2050 cp_parser_error (cp_parser* parser, const char* message)
2051 {
2052 if (!cp_parser_simulate_error (parser))
2053 {
2054 cp_token *token = cp_lexer_peek_token (parser->lexer);
2055 /* This diagnostic makes more sense if it is tagged to the line
2056 of the token we just peeked at. */
2057 cp_lexer_set_source_position_from_token (token);
2058
2059 if (token->type == CPP_PRAGMA)
2060 {
2061 error ("%<#pragma%> is not allowed here");
2062 cp_parser_skip_to_pragma_eol (parser, token);
2063 return;
2064 }
2065
2066 c_parse_error (message,
2067 /* Because c_parser_error does not understand
2068 CPP_KEYWORD, keywords are treated like
2069 identifiers. */
2070 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2071 token->u.value);
2072 }
2073 }
2074
2075 /* Issue an error about name-lookup failing. NAME is the
2076 IDENTIFIER_NODE DECL is the result of
2077 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2078 the thing that we hoped to find. */
2079
2080 static void
2081 cp_parser_name_lookup_error (cp_parser* parser,
2082 tree name,
2083 tree decl,
2084 const char* desired)
2085 {
2086 /* If name lookup completely failed, tell the user that NAME was not
2087 declared. */
2088 if (decl == error_mark_node)
2089 {
2090 if (parser->scope && parser->scope != global_namespace)
2091 error ("%<%E::%E%> has not been declared",
2092 parser->scope, name);
2093 else if (parser->scope == global_namespace)
2094 error ("%<::%E%> has not been declared", name);
2095 else if (parser->object_scope
2096 && !CLASS_TYPE_P (parser->object_scope))
2097 error ("request for member %qE in non-class type %qT",
2098 name, parser->object_scope);
2099 else if (parser->object_scope)
2100 error ("%<%T::%E%> has not been declared",
2101 parser->object_scope, name);
2102 else
2103 error ("%qE has not been declared", name);
2104 }
2105 else if (parser->scope && parser->scope != global_namespace)
2106 error ("%<%E::%E%> %s", parser->scope, name, desired);
2107 else if (parser->scope == global_namespace)
2108 error ("%<::%E%> %s", name, desired);
2109 else
2110 error ("%qE %s", name, desired);
2111 }
2112
2113 /* If we are parsing tentatively, remember that an error has occurred
2114 during this tentative parse. Returns true if the error was
2115 simulated; false if a message should be issued by the caller. */
2116
2117 static bool
2118 cp_parser_simulate_error (cp_parser* parser)
2119 {
2120 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2121 {
2122 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2123 return true;
2124 }
2125 return false;
2126 }
2127
2128 /* Check for repeated decl-specifiers. */
2129
2130 static void
2131 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2132 {
2133 cp_decl_spec ds;
2134
2135 for (ds = ds_first; ds != ds_last; ++ds)
2136 {
2137 unsigned count = decl_specs->specs[(int)ds];
2138 if (count < 2)
2139 continue;
2140 /* The "long" specifier is a special case because of "long long". */
2141 if (ds == ds_long)
2142 {
2143 if (count > 2)
2144 error ("%<long long long%> is too long for GCC");
2145 else if (pedantic && !in_system_header && warn_long_long)
2146 pedwarn ("ISO C++ does not support %<long long%>");
2147 }
2148 else if (count > 1)
2149 {
2150 static const char *const decl_spec_names[] = {
2151 "signed",
2152 "unsigned",
2153 "short",
2154 "long",
2155 "const",
2156 "volatile",
2157 "restrict",
2158 "inline",
2159 "virtual",
2160 "explicit",
2161 "friend",
2162 "typedef",
2163 "__complex",
2164 "__thread"
2165 };
2166 error ("duplicate %qs", decl_spec_names[(int)ds]);
2167 }
2168 }
2169 }
2170
2171 /* This function is called when a type is defined. If type
2172 definitions are forbidden at this point, an error message is
2173 issued. */
2174
2175 static bool
2176 cp_parser_check_type_definition (cp_parser* parser)
2177 {
2178 /* If types are forbidden here, issue a message. */
2179 if (parser->type_definition_forbidden_message)
2180 {
2181 /* Use `%s' to print the string in case there are any escape
2182 characters in the message. */
2183 error ("%s", parser->type_definition_forbidden_message);
2184 return false;
2185 }
2186 return true;
2187 }
2188
2189 /* This function is called when the DECLARATOR is processed. The TYPE
2190 was a type defined in the decl-specifiers. If it is invalid to
2191 define a type in the decl-specifiers for DECLARATOR, an error is
2192 issued. */
2193
2194 static void
2195 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2196 tree type)
2197 {
2198 /* [dcl.fct] forbids type definitions in return types.
2199 Unfortunately, it's not easy to know whether or not we are
2200 processing a return type until after the fact. */
2201 while (declarator
2202 && (declarator->kind == cdk_pointer
2203 || declarator->kind == cdk_reference
2204 || declarator->kind == cdk_ptrmem))
2205 declarator = declarator->declarator;
2206 if (declarator
2207 && declarator->kind == cdk_function)
2208 {
2209 error ("new types may not be defined in a return type");
2210 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2211 type);
2212 }
2213 }
2214
2215 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2216 "<" in any valid C++ program. If the next token is indeed "<",
2217 issue a message warning the user about what appears to be an
2218 invalid attempt to form a template-id. */
2219
2220 static void
2221 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2222 tree type)
2223 {
2224 cp_token_position start = 0;
2225
2226 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2227 {
2228 if (TYPE_P (type))
2229 error ("%qT is not a template", type);
2230 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2231 error ("%qE is not a template", type);
2232 else
2233 error ("invalid template-id");
2234 /* Remember the location of the invalid "<". */
2235 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2236 start = cp_lexer_token_position (parser->lexer, true);
2237 /* Consume the "<". */
2238 cp_lexer_consume_token (parser->lexer);
2239 /* Parse the template arguments. */
2240 cp_parser_enclosed_template_argument_list (parser);
2241 /* Permanently remove the invalid template arguments so that
2242 this error message is not issued again. */
2243 if (start)
2244 cp_lexer_purge_tokens_after (parser->lexer, start);
2245 }
2246 }
2247
2248 /* If parsing an integral constant-expression, issue an error message
2249 about the fact that THING appeared and return true. Otherwise,
2250 return false. In either case, set
2251 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2252
2253 static bool
2254 cp_parser_non_integral_constant_expression (cp_parser *parser,
2255 const char *thing)
2256 {
2257 parser->non_integral_constant_expression_p = true;
2258 if (parser->integral_constant_expression_p)
2259 {
2260 if (!parser->allow_non_integral_constant_expression_p)
2261 {
2262 error ("%s cannot appear in a constant-expression", thing);
2263 return true;
2264 }
2265 }
2266 return false;
2267 }
2268
2269 /* Emit a diagnostic for an invalid type name. SCOPE is the
2270 qualifying scope (or NULL, if none) for ID. This function commits
2271 to the current active tentative parse, if any. (Otherwise, the
2272 problematic construct might be encountered again later, resulting
2273 in duplicate error messages.) */
2274
2275 static void
2276 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2277 {
2278 tree decl, old_scope;
2279 /* Try to lookup the identifier. */
2280 old_scope = parser->scope;
2281 parser->scope = scope;
2282 decl = cp_parser_lookup_name_simple (parser, id);
2283 parser->scope = old_scope;
2284 /* If the lookup found a template-name, it means that the user forgot
2285 to specify an argument list. Emit a useful error message. */
2286 if (TREE_CODE (decl) == TEMPLATE_DECL)
2287 error ("invalid use of template-name %qE without an argument list", decl);
2288 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2289 error ("invalid use of destructor %qD as a type", id);
2290 else if (TREE_CODE (decl) == TYPE_DECL)
2291 /* Something like 'unsigned A a;' */
2292 error ("invalid combination of multiple type-specifiers");
2293 else if (!parser->scope)
2294 {
2295 /* Issue an error message. */
2296 error ("%qE does not name a type", id);
2297 /* If we're in a template class, it's possible that the user was
2298 referring to a type from a base class. For example:
2299
2300 template <typename T> struct A { typedef T X; };
2301 template <typename T> struct B : public A<T> { X x; };
2302
2303 The user should have said "typename A<T>::X". */
2304 if (processing_template_decl && current_class_type
2305 && TYPE_BINFO (current_class_type))
2306 {
2307 tree b;
2308
2309 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2310 b;
2311 b = TREE_CHAIN (b))
2312 {
2313 tree base_type = BINFO_TYPE (b);
2314 if (CLASS_TYPE_P (base_type)
2315 && dependent_type_p (base_type))
2316 {
2317 tree field;
2318 /* Go from a particular instantiation of the
2319 template (which will have an empty TYPE_FIELDs),
2320 to the main version. */
2321 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2322 for (field = TYPE_FIELDS (base_type);
2323 field;
2324 field = TREE_CHAIN (field))
2325 if (TREE_CODE (field) == TYPE_DECL
2326 && DECL_NAME (field) == id)
2327 {
2328 inform ("(perhaps %<typename %T::%E%> was intended)",
2329 BINFO_TYPE (b), id);
2330 break;
2331 }
2332 if (field)
2333 break;
2334 }
2335 }
2336 }
2337 }
2338 /* Here we diagnose qualified-ids where the scope is actually correct,
2339 but the identifier does not resolve to a valid type name. */
2340 else if (parser->scope != error_mark_node)
2341 {
2342 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2343 error ("%qE in namespace %qE does not name a type",
2344 id, parser->scope);
2345 else if (TYPE_P (parser->scope))
2346 error ("%qE in class %qT does not name a type", id, parser->scope);
2347 else
2348 gcc_unreachable ();
2349 }
2350 cp_parser_commit_to_tentative_parse (parser);
2351 }
2352
2353 /* Check for a common situation where a type-name should be present,
2354 but is not, and issue a sensible error message. Returns true if an
2355 invalid type-name was detected.
2356
2357 The situation handled by this function are variable declarations of the
2358 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2359 Usually, `ID' should name a type, but if we got here it means that it
2360 does not. We try to emit the best possible error message depending on
2361 how exactly the id-expression looks like. */
2362
2363 static bool
2364 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2365 {
2366 tree id;
2367
2368 cp_parser_parse_tentatively (parser);
2369 id = cp_parser_id_expression (parser,
2370 /*template_keyword_p=*/false,
2371 /*check_dependency_p=*/true,
2372 /*template_p=*/NULL,
2373 /*declarator_p=*/true,
2374 /*optional_p=*/false);
2375 /* After the id-expression, there should be a plain identifier,
2376 otherwise this is not a simple variable declaration. Also, if
2377 the scope is dependent, we cannot do much. */
2378 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2379 || (parser->scope && TYPE_P (parser->scope)
2380 && dependent_type_p (parser->scope))
2381 || TREE_CODE (id) == TYPE_DECL)
2382 {
2383 cp_parser_abort_tentative_parse (parser);
2384 return false;
2385 }
2386 if (!cp_parser_parse_definitely (parser))
2387 return false;
2388
2389 /* Emit a diagnostic for the invalid type. */
2390 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2391 /* Skip to the end of the declaration; there's no point in
2392 trying to process it. */
2393 cp_parser_skip_to_end_of_block_or_statement (parser);
2394 return true;
2395 }
2396
2397 /* Consume tokens up to, and including, the next non-nested closing `)'.
2398 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2399 are doing error recovery. Returns -1 if OR_COMMA is true and we
2400 found an unnested comma. */
2401
2402 static int
2403 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2404 bool recovering,
2405 bool or_comma,
2406 bool consume_paren)
2407 {
2408 unsigned paren_depth = 0;
2409 unsigned brace_depth = 0;
2410
2411 if (recovering && !or_comma
2412 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2413 return 0;
2414
2415 while (true)
2416 {
2417 cp_token * token = cp_lexer_peek_token (parser->lexer);
2418
2419 switch (token->type)
2420 {
2421 case CPP_EOF:
2422 case CPP_PRAGMA_EOL:
2423 /* If we've run out of tokens, then there is no closing `)'. */
2424 return 0;
2425
2426 case CPP_SEMICOLON:
2427 /* This matches the processing in skip_to_end_of_statement. */
2428 if (!brace_depth)
2429 return 0;
2430 break;
2431
2432 case CPP_OPEN_BRACE:
2433 ++brace_depth;
2434 break;
2435 case CPP_CLOSE_BRACE:
2436 if (!brace_depth--)
2437 return 0;
2438 break;
2439
2440 case CPP_COMMA:
2441 if (recovering && or_comma && !brace_depth && !paren_depth)
2442 return -1;
2443 break;
2444
2445 case CPP_OPEN_PAREN:
2446 if (!brace_depth)
2447 ++paren_depth;
2448 break;
2449
2450 case CPP_CLOSE_PAREN:
2451 if (!brace_depth && !paren_depth--)
2452 {
2453 if (consume_paren)
2454 cp_lexer_consume_token (parser->lexer);
2455 return 1;
2456 }
2457 break;
2458
2459 default:
2460 break;
2461 }
2462
2463 /* Consume the token. */
2464 cp_lexer_consume_token (parser->lexer);
2465 }
2466 }
2467
2468 /* Consume tokens until we reach the end of the current statement.
2469 Normally, that will be just before consuming a `;'. However, if a
2470 non-nested `}' comes first, then we stop before consuming that. */
2471
2472 static void
2473 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2474 {
2475 unsigned nesting_depth = 0;
2476
2477 while (true)
2478 {
2479 cp_token *token = cp_lexer_peek_token (parser->lexer);
2480
2481 switch (token->type)
2482 {
2483 case CPP_EOF:
2484 case CPP_PRAGMA_EOL:
2485 /* If we've run out of tokens, stop. */
2486 return;
2487
2488 case CPP_SEMICOLON:
2489 /* If the next token is a `;', we have reached the end of the
2490 statement. */
2491 if (!nesting_depth)
2492 return;
2493 break;
2494
2495 case CPP_CLOSE_BRACE:
2496 /* If this is a non-nested '}', stop before consuming it.
2497 That way, when confronted with something like:
2498
2499 { 3 + }
2500
2501 we stop before consuming the closing '}', even though we
2502 have not yet reached a `;'. */
2503 if (nesting_depth == 0)
2504 return;
2505
2506 /* If it is the closing '}' for a block that we have
2507 scanned, stop -- but only after consuming the token.
2508 That way given:
2509
2510 void f g () { ... }
2511 typedef int I;
2512
2513 we will stop after the body of the erroneously declared
2514 function, but before consuming the following `typedef'
2515 declaration. */
2516 if (--nesting_depth == 0)
2517 {
2518 cp_lexer_consume_token (parser->lexer);
2519 return;
2520 }
2521
2522 case CPP_OPEN_BRACE:
2523 ++nesting_depth;
2524 break;
2525
2526 default:
2527 break;
2528 }
2529
2530 /* Consume the token. */
2531 cp_lexer_consume_token (parser->lexer);
2532 }
2533 }
2534
2535 /* This function is called at the end of a statement or declaration.
2536 If the next token is a semicolon, it is consumed; otherwise, error
2537 recovery is attempted. */
2538
2539 static void
2540 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2541 {
2542 /* Look for the trailing `;'. */
2543 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2544 {
2545 /* If there is additional (erroneous) input, skip to the end of
2546 the statement. */
2547 cp_parser_skip_to_end_of_statement (parser);
2548 /* If the next token is now a `;', consume it. */
2549 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2550 cp_lexer_consume_token (parser->lexer);
2551 }
2552 }
2553
2554 /* Skip tokens until we have consumed an entire block, or until we
2555 have consumed a non-nested `;'. */
2556
2557 static void
2558 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2559 {
2560 int nesting_depth = 0;
2561
2562 while (nesting_depth >= 0)
2563 {
2564 cp_token *token = cp_lexer_peek_token (parser->lexer);
2565
2566 switch (token->type)
2567 {
2568 case CPP_EOF:
2569 case CPP_PRAGMA_EOL:
2570 /* If we've run out of tokens, stop. */
2571 return;
2572
2573 case CPP_SEMICOLON:
2574 /* Stop if this is an unnested ';'. */
2575 if (!nesting_depth)
2576 nesting_depth = -1;
2577 break;
2578
2579 case CPP_CLOSE_BRACE:
2580 /* Stop if this is an unnested '}', or closes the outermost
2581 nesting level. */
2582 nesting_depth--;
2583 if (!nesting_depth)
2584 nesting_depth = -1;
2585 break;
2586
2587 case CPP_OPEN_BRACE:
2588 /* Nest. */
2589 nesting_depth++;
2590 break;
2591
2592 default:
2593 break;
2594 }
2595
2596 /* Consume the token. */
2597 cp_lexer_consume_token (parser->lexer);
2598 }
2599 }
2600
2601 /* Skip tokens until a non-nested closing curly brace is the next
2602 token, or there are no more tokens. Return true in the first case,
2603 false otherwise. */
2604
2605 static bool
2606 cp_parser_skip_to_closing_brace (cp_parser *parser)
2607 {
2608 unsigned nesting_depth = 0;
2609
2610 while (true)
2611 {
2612 cp_token *token = cp_lexer_peek_token (parser->lexer);
2613
2614 switch (token->type)
2615 {
2616 case CPP_EOF:
2617 case CPP_PRAGMA_EOL:
2618 /* If we've run out of tokens, stop. */
2619 return false;
2620
2621 case CPP_CLOSE_BRACE:
2622 /* If the next token is a non-nested `}', then we have reached
2623 the end of the current block. */
2624 if (nesting_depth-- == 0)
2625 return true;
2626 break;
2627
2628 case CPP_OPEN_BRACE:
2629 /* If it the next token is a `{', then we are entering a new
2630 block. Consume the entire block. */
2631 ++nesting_depth;
2632 break;
2633
2634 default:
2635 break;
2636 }
2637
2638 /* Consume the token. */
2639 cp_lexer_consume_token (parser->lexer);
2640 }
2641 }
2642
2643 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2644 parameter is the PRAGMA token, allowing us to purge the entire pragma
2645 sequence. */
2646
2647 static void
2648 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2649 {
2650 cp_token *token;
2651
2652 parser->lexer->in_pragma = false;
2653
2654 do
2655 token = cp_lexer_consume_token (parser->lexer);
2656 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2657
2658 /* Ensure that the pragma is not parsed again. */
2659 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2660 }
2661
2662 /* Require pragma end of line, resyncing with it as necessary. The
2663 arguments are as for cp_parser_skip_to_pragma_eol. */
2664
2665 static void
2666 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2667 {
2668 parser->lexer->in_pragma = false;
2669 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2670 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2671 }
2672
2673 /* This is a simple wrapper around make_typename_type. When the id is
2674 an unresolved identifier node, we can provide a superior diagnostic
2675 using cp_parser_diagnose_invalid_type_name. */
2676
2677 static tree
2678 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2679 {
2680 tree result;
2681 if (TREE_CODE (id) == IDENTIFIER_NODE)
2682 {
2683 result = make_typename_type (scope, id, typename_type,
2684 /*complain=*/tf_none);
2685 if (result == error_mark_node)
2686 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2687 return result;
2688 }
2689 return make_typename_type (scope, id, typename_type, tf_error);
2690 }
2691
2692
2693 /* Create a new C++ parser. */
2694
2695 static cp_parser *
2696 cp_parser_new (void)
2697 {
2698 cp_parser *parser;
2699 cp_lexer *lexer;
2700 unsigned i;
2701
2702 /* cp_lexer_new_main is called before calling ggc_alloc because
2703 cp_lexer_new_main might load a PCH file. */
2704 lexer = cp_lexer_new_main ();
2705
2706 /* Initialize the binops_by_token so that we can get the tree
2707 directly from the token. */
2708 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2709 binops_by_token[binops[i].token_type] = binops[i];
2710
2711 parser = GGC_CNEW (cp_parser);
2712 parser->lexer = lexer;
2713 parser->context = cp_parser_context_new (NULL);
2714
2715 /* For now, we always accept GNU extensions. */
2716 parser->allow_gnu_extensions_p = 1;
2717
2718 /* The `>' token is a greater-than operator, not the end of a
2719 template-id. */
2720 parser->greater_than_is_operator_p = true;
2721
2722 parser->default_arg_ok_p = true;
2723
2724 /* We are not parsing a constant-expression. */
2725 parser->integral_constant_expression_p = false;
2726 parser->allow_non_integral_constant_expression_p = false;
2727 parser->non_integral_constant_expression_p = false;
2728
2729 /* Local variable names are not forbidden. */
2730 parser->local_variables_forbidden_p = false;
2731
2732 /* We are not processing an `extern "C"' declaration. */
2733 parser->in_unbraced_linkage_specification_p = false;
2734
2735 /* We are not processing a declarator. */
2736 parser->in_declarator_p = false;
2737
2738 /* We are not processing a template-argument-list. */
2739 parser->in_template_argument_list_p = false;
2740
2741 /* We are not in an iteration statement. */
2742 parser->in_statement = 0;
2743
2744 /* We are not in a switch statement. */
2745 parser->in_switch_statement_p = false;
2746
2747 /* We are not parsing a type-id inside an expression. */
2748 parser->in_type_id_in_expr_p = false;
2749
2750 /* Declarations aren't implicitly extern "C". */
2751 parser->implicit_extern_c = false;
2752
2753 /* String literals should be translated to the execution character set. */
2754 parser->translate_strings_p = true;
2755
2756 /* We are not parsing a function body. */
2757 parser->in_function_body = false;
2758
2759 /* The unparsed function queue is empty. */
2760 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2761
2762 /* There are no classes being defined. */
2763 parser->num_classes_being_defined = 0;
2764
2765 /* No template parameters apply. */
2766 parser->num_template_parameter_lists = 0;
2767
2768 return parser;
2769 }
2770
2771 /* Create a cp_lexer structure which will emit the tokens in CACHE
2772 and push it onto the parser's lexer stack. This is used for delayed
2773 parsing of in-class method bodies and default arguments, and should
2774 not be confused with tentative parsing. */
2775 static void
2776 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2777 {
2778 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2779 lexer->next = parser->lexer;
2780 parser->lexer = lexer;
2781
2782 /* Move the current source position to that of the first token in the
2783 new lexer. */
2784 cp_lexer_set_source_position_from_token (lexer->next_token);
2785 }
2786
2787 /* Pop the top lexer off the parser stack. This is never used for the
2788 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2789 static void
2790 cp_parser_pop_lexer (cp_parser *parser)
2791 {
2792 cp_lexer *lexer = parser->lexer;
2793 parser->lexer = lexer->next;
2794 cp_lexer_destroy (lexer);
2795
2796 /* Put the current source position back where it was before this
2797 lexer was pushed. */
2798 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2799 }
2800
2801 /* Lexical conventions [gram.lex] */
2802
2803 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2804 identifier. */
2805
2806 static tree
2807 cp_parser_identifier (cp_parser* parser)
2808 {
2809 cp_token *token;
2810
2811 /* Look for the identifier. */
2812 token = cp_parser_require (parser, CPP_NAME, "identifier");
2813 /* Return the value. */
2814 return token ? token->u.value : error_mark_node;
2815 }
2816
2817 /* Parse a sequence of adjacent string constants. Returns a
2818 TREE_STRING representing the combined, nul-terminated string
2819 constant. If TRANSLATE is true, translate the string to the
2820 execution character set. If WIDE_OK is true, a wide string is
2821 invalid here.
2822
2823 C++98 [lex.string] says that if a narrow string literal token is
2824 adjacent to a wide string literal token, the behavior is undefined.
2825 However, C99 6.4.5p4 says that this results in a wide string literal.
2826 We follow C99 here, for consistency with the C front end.
2827
2828 This code is largely lifted from lex_string() in c-lex.c.
2829
2830 FUTURE: ObjC++ will need to handle @-strings here. */
2831 static tree
2832 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2833 {
2834 tree value;
2835 bool wide = false;
2836 size_t count;
2837 struct obstack str_ob;
2838 cpp_string str, istr, *strs;
2839 cp_token *tok;
2840
2841 tok = cp_lexer_peek_token (parser->lexer);
2842 if (!cp_parser_is_string_literal (tok))
2843 {
2844 cp_parser_error (parser, "expected string-literal");
2845 return error_mark_node;
2846 }
2847
2848 /* Try to avoid the overhead of creating and destroying an obstack
2849 for the common case of just one string. */
2850 if (!cp_parser_is_string_literal
2851 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2852 {
2853 cp_lexer_consume_token (parser->lexer);
2854
2855 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2856 str.len = TREE_STRING_LENGTH (tok->u.value);
2857 count = 1;
2858 if (tok->type == CPP_WSTRING)
2859 wide = true;
2860
2861 strs = &str;
2862 }
2863 else
2864 {
2865 gcc_obstack_init (&str_ob);
2866 count = 0;
2867
2868 do
2869 {
2870 cp_lexer_consume_token (parser->lexer);
2871 count++;
2872 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2873 str.len = TREE_STRING_LENGTH (tok->u.value);
2874 if (tok->type == CPP_WSTRING)
2875 wide = true;
2876
2877 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2878
2879 tok = cp_lexer_peek_token (parser->lexer);
2880 }
2881 while (cp_parser_is_string_literal (tok));
2882
2883 strs = (cpp_string *) obstack_finish (&str_ob);
2884 }
2885
2886 if (wide && !wide_ok)
2887 {
2888 cp_parser_error (parser, "a wide string is invalid in this context");
2889 wide = false;
2890 }
2891
2892 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2893 (parse_in, strs, count, &istr, wide))
2894 {
2895 value = build_string (istr.len, (char *)istr.text);
2896 free ((void *)istr.text);
2897
2898 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2899 value = fix_string_type (value);
2900 }
2901 else
2902 /* cpp_interpret_string has issued an error. */
2903 value = error_mark_node;
2904
2905 if (count > 1)
2906 obstack_free (&str_ob, 0);
2907
2908 return value;
2909 }
2910
2911
2912 /* Basic concepts [gram.basic] */
2913
2914 /* Parse a translation-unit.
2915
2916 translation-unit:
2917 declaration-seq [opt]
2918
2919 Returns TRUE if all went well. */
2920
2921 static bool
2922 cp_parser_translation_unit (cp_parser* parser)
2923 {
2924 /* The address of the first non-permanent object on the declarator
2925 obstack. */
2926 static void *declarator_obstack_base;
2927
2928 bool success;
2929
2930 /* Create the declarator obstack, if necessary. */
2931 if (!cp_error_declarator)
2932 {
2933 gcc_obstack_init (&declarator_obstack);
2934 /* Create the error declarator. */
2935 cp_error_declarator = make_declarator (cdk_error);
2936 /* Create the empty parameter list. */
2937 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2938 /* Remember where the base of the declarator obstack lies. */
2939 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2940 }
2941
2942 cp_parser_declaration_seq_opt (parser);
2943
2944 /* If there are no tokens left then all went well. */
2945 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2946 {
2947 /* Get rid of the token array; we don't need it any more. */
2948 cp_lexer_destroy (parser->lexer);
2949 parser->lexer = NULL;
2950
2951 /* This file might have been a context that's implicitly extern
2952 "C". If so, pop the lang context. (Only relevant for PCH.) */
2953 if (parser->implicit_extern_c)
2954 {
2955 pop_lang_context ();
2956 parser->implicit_extern_c = false;
2957 }
2958
2959 /* Finish up. */
2960 finish_translation_unit ();
2961
2962 success = true;
2963 }
2964 else
2965 {
2966 cp_parser_error (parser, "expected declaration");
2967 success = false;
2968 }
2969
2970 /* Make sure the declarator obstack was fully cleaned up. */
2971 gcc_assert (obstack_next_free (&declarator_obstack)
2972 == declarator_obstack_base);
2973
2974 /* All went well. */
2975 return success;
2976 }
2977
2978 /* Expressions [gram.expr] */
2979
2980 /* Parse a primary-expression.
2981
2982 primary-expression:
2983 literal
2984 this
2985 ( expression )
2986 id-expression
2987
2988 GNU Extensions:
2989
2990 primary-expression:
2991 ( compound-statement )
2992 __builtin_va_arg ( assignment-expression , type-id )
2993 __builtin_offsetof ( type-id , offsetof-expression )
2994
2995 C++ Extensions:
2996 __has_nothrow_assign ( type-id )
2997 __has_nothrow_constructor ( type-id )
2998 __has_nothrow_copy ( type-id )
2999 __has_trivial_assign ( type-id )
3000 __has_trivial_constructor ( type-id )
3001 __has_trivial_copy ( type-id )
3002 __has_trivial_destructor ( type-id )
3003 __has_virtual_destructor ( type-id )
3004 __is_abstract ( type-id )
3005 __is_base_of ( type-id , type-id )
3006 __is_class ( type-id )
3007 __is_convertible_to ( type-id , type-id )
3008 __is_empty ( type-id )
3009 __is_enum ( type-id )
3010 __is_pod ( type-id )
3011 __is_polymorphic ( type-id )
3012 __is_union ( type-id )
3013
3014 Objective-C++ Extension:
3015
3016 primary-expression:
3017 objc-expression
3018
3019 literal:
3020 __null
3021
3022 ADDRESS_P is true iff this expression was immediately preceded by
3023 "&" and therefore might denote a pointer-to-member. CAST_P is true
3024 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3025 true iff this expression is a template argument.
3026
3027 Returns a representation of the expression. Upon return, *IDK
3028 indicates what kind of id-expression (if any) was present. */
3029
3030 static tree
3031 cp_parser_primary_expression (cp_parser *parser,
3032 bool address_p,
3033 bool cast_p,
3034 bool template_arg_p,
3035 cp_id_kind *idk)
3036 {
3037 cp_token *token;
3038
3039 /* Assume the primary expression is not an id-expression. */
3040 *idk = CP_ID_KIND_NONE;
3041
3042 /* Peek at the next token. */
3043 token = cp_lexer_peek_token (parser->lexer);
3044 switch (token->type)
3045 {
3046 /* literal:
3047 integer-literal
3048 character-literal
3049 floating-literal
3050 string-literal
3051 boolean-literal */
3052 case CPP_CHAR:
3053 case CPP_WCHAR:
3054 case CPP_NUMBER:
3055 token = cp_lexer_consume_token (parser->lexer);
3056 /* Floating-point literals are only allowed in an integral
3057 constant expression if they are cast to an integral or
3058 enumeration type. */
3059 if (TREE_CODE (token->u.value) == REAL_CST
3060 && parser->integral_constant_expression_p
3061 && pedantic)
3062 {
3063 /* CAST_P will be set even in invalid code like "int(2.7 +
3064 ...)". Therefore, we have to check that the next token
3065 is sure to end the cast. */
3066 if (cast_p)
3067 {
3068 cp_token *next_token;
3069
3070 next_token = cp_lexer_peek_token (parser->lexer);
3071 if (/* The comma at the end of an
3072 enumerator-definition. */
3073 next_token->type != CPP_COMMA
3074 /* The curly brace at the end of an enum-specifier. */
3075 && next_token->type != CPP_CLOSE_BRACE
3076 /* The end of a statement. */
3077 && next_token->type != CPP_SEMICOLON
3078 /* The end of the cast-expression. */
3079 && next_token->type != CPP_CLOSE_PAREN
3080 /* The end of an array bound. */
3081 && next_token->type != CPP_CLOSE_SQUARE
3082 /* The closing ">" in a template-argument-list. */
3083 && (next_token->type != CPP_GREATER
3084 || parser->greater_than_is_operator_p)
3085 /* C++0x only: A ">>" treated like two ">" tokens,
3086 in a template-argument-list. */
3087 && (next_token->type != CPP_RSHIFT
3088 || !flag_cpp0x
3089 || parser->greater_than_is_operator_p))
3090 cast_p = false;
3091 }
3092
3093 /* If we are within a cast, then the constraint that the
3094 cast is to an integral or enumeration type will be
3095 checked at that point. If we are not within a cast, then
3096 this code is invalid. */
3097 if (!cast_p)
3098 cp_parser_non_integral_constant_expression
3099 (parser, "floating-point literal");
3100 }
3101 return token->u.value;
3102
3103 case CPP_STRING:
3104 case CPP_WSTRING:
3105 /* ??? Should wide strings be allowed when parser->translate_strings_p
3106 is false (i.e. in attributes)? If not, we can kill the third
3107 argument to cp_parser_string_literal. */
3108 return cp_parser_string_literal (parser,
3109 parser->translate_strings_p,
3110 true);
3111
3112 case CPP_OPEN_PAREN:
3113 {
3114 tree expr;
3115 bool saved_greater_than_is_operator_p;
3116
3117 /* Consume the `('. */
3118 cp_lexer_consume_token (parser->lexer);
3119 /* Within a parenthesized expression, a `>' token is always
3120 the greater-than operator. */
3121 saved_greater_than_is_operator_p
3122 = parser->greater_than_is_operator_p;
3123 parser->greater_than_is_operator_p = true;
3124 /* If we see `( { ' then we are looking at the beginning of
3125 a GNU statement-expression. */
3126 if (cp_parser_allow_gnu_extensions_p (parser)
3127 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3128 {
3129 /* Statement-expressions are not allowed by the standard. */
3130 if (pedantic)
3131 pedwarn ("ISO C++ forbids braced-groups within expressions");
3132
3133 /* And they're not allowed outside of a function-body; you
3134 cannot, for example, write:
3135
3136 int i = ({ int j = 3; j + 1; });
3137
3138 at class or namespace scope. */
3139 if (!parser->in_function_body)
3140 {
3141 error ("statement-expressions are allowed only inside functions");
3142 cp_parser_skip_to_end_of_block_or_statement (parser);
3143 expr = error_mark_node;
3144 }
3145 else
3146 {
3147 /* Start the statement-expression. */
3148 expr = begin_stmt_expr ();
3149 /* Parse the compound-statement. */
3150 cp_parser_compound_statement (parser, expr, false);
3151 /* Finish up. */
3152 expr = finish_stmt_expr (expr, false);
3153 }
3154 }
3155 else
3156 {
3157 /* Parse the parenthesized expression. */
3158 expr = cp_parser_expression (parser, cast_p);
3159 /* Let the front end know that this expression was
3160 enclosed in parentheses. This matters in case, for
3161 example, the expression is of the form `A::B', since
3162 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3163 not. */
3164 finish_parenthesized_expr (expr);
3165 }
3166 /* The `>' token might be the end of a template-id or
3167 template-parameter-list now. */
3168 parser->greater_than_is_operator_p
3169 = saved_greater_than_is_operator_p;
3170 /* Consume the `)'. */
3171 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3172 cp_parser_skip_to_end_of_statement (parser);
3173
3174 return expr;
3175 }
3176
3177 case CPP_KEYWORD:
3178 switch (token->keyword)
3179 {
3180 /* These two are the boolean literals. */
3181 case RID_TRUE:
3182 cp_lexer_consume_token (parser->lexer);
3183 return boolean_true_node;
3184 case RID_FALSE:
3185 cp_lexer_consume_token (parser->lexer);
3186 return boolean_false_node;
3187
3188 /* The `__null' literal. */
3189 case RID_NULL:
3190 cp_lexer_consume_token (parser->lexer);
3191 return null_node;
3192
3193 /* Recognize the `this' keyword. */
3194 case RID_THIS:
3195 cp_lexer_consume_token (parser->lexer);
3196 if (parser->local_variables_forbidden_p)
3197 {
3198 error ("%<this%> may not be used in this context");
3199 return error_mark_node;
3200 }
3201 /* Pointers cannot appear in constant-expressions. */
3202 if (cp_parser_non_integral_constant_expression (parser,
3203 "`this'"))
3204 return error_mark_node;
3205 return finish_this_expr ();
3206
3207 /* The `operator' keyword can be the beginning of an
3208 id-expression. */
3209 case RID_OPERATOR:
3210 goto id_expression;
3211
3212 case RID_FUNCTION_NAME:
3213 case RID_PRETTY_FUNCTION_NAME:
3214 case RID_C99_FUNCTION_NAME:
3215 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3216 __func__ are the names of variables -- but they are
3217 treated specially. Therefore, they are handled here,
3218 rather than relying on the generic id-expression logic
3219 below. Grammatically, these names are id-expressions.
3220
3221 Consume the token. */
3222 token = cp_lexer_consume_token (parser->lexer);
3223 /* Look up the name. */
3224 return finish_fname (token->u.value);
3225
3226 case RID_VA_ARG:
3227 {
3228 tree expression;
3229 tree type;
3230
3231 /* The `__builtin_va_arg' construct is used to handle
3232 `va_arg'. Consume the `__builtin_va_arg' token. */
3233 cp_lexer_consume_token (parser->lexer);
3234 /* Look for the opening `('. */
3235 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3236 /* Now, parse the assignment-expression. */
3237 expression = cp_parser_assignment_expression (parser,
3238 /*cast_p=*/false);
3239 /* Look for the `,'. */
3240 cp_parser_require (parser, CPP_COMMA, "`,'");
3241 /* Parse the type-id. */
3242 type = cp_parser_type_id (parser);
3243 /* Look for the closing `)'. */
3244 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3245 /* Using `va_arg' in a constant-expression is not
3246 allowed. */
3247 if (cp_parser_non_integral_constant_expression (parser,
3248 "`va_arg'"))
3249 return error_mark_node;
3250 return build_x_va_arg (expression, type);
3251 }
3252
3253 case RID_OFFSETOF:
3254 return cp_parser_builtin_offsetof (parser);
3255
3256 case RID_HAS_NOTHROW_ASSIGN:
3257 case RID_HAS_NOTHROW_CONSTRUCTOR:
3258 case RID_HAS_NOTHROW_COPY:
3259 case RID_HAS_TRIVIAL_ASSIGN:
3260 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3261 case RID_HAS_TRIVIAL_COPY:
3262 case RID_HAS_TRIVIAL_DESTRUCTOR:
3263 case RID_HAS_VIRTUAL_DESTRUCTOR:
3264 case RID_IS_ABSTRACT:
3265 case RID_IS_BASE_OF:
3266 case RID_IS_CLASS:
3267 case RID_IS_CONVERTIBLE_TO:
3268 case RID_IS_EMPTY:
3269 case RID_IS_ENUM:
3270 case RID_IS_POD:
3271 case RID_IS_POLYMORPHIC:
3272 case RID_IS_UNION:
3273 return cp_parser_trait_expr (parser, token->keyword);
3274
3275 /* Objective-C++ expressions. */
3276 case RID_AT_ENCODE:
3277 case RID_AT_PROTOCOL:
3278 case RID_AT_SELECTOR:
3279 return cp_parser_objc_expression (parser);
3280
3281 default:
3282 cp_parser_error (parser, "expected primary-expression");
3283 return error_mark_node;
3284 }
3285
3286 /* An id-expression can start with either an identifier, a
3287 `::' as the beginning of a qualified-id, or the "operator"
3288 keyword. */
3289 case CPP_NAME:
3290 case CPP_SCOPE:
3291 case CPP_TEMPLATE_ID:
3292 case CPP_NESTED_NAME_SPECIFIER:
3293 {
3294 tree id_expression;
3295 tree decl;
3296 const char *error_msg;
3297 bool template_p;
3298 bool done;
3299
3300 id_expression:
3301 /* Parse the id-expression. */
3302 id_expression
3303 = cp_parser_id_expression (parser,
3304 /*template_keyword_p=*/false,
3305 /*check_dependency_p=*/true,
3306 &template_p,
3307 /*declarator_p=*/false,
3308 /*optional_p=*/false);
3309 if (id_expression == error_mark_node)
3310 return error_mark_node;
3311 token = cp_lexer_peek_token (parser->lexer);
3312 done = (token->type != CPP_OPEN_SQUARE
3313 && token->type != CPP_OPEN_PAREN
3314 && token->type != CPP_DOT
3315 && token->type != CPP_DEREF
3316 && token->type != CPP_PLUS_PLUS
3317 && token->type != CPP_MINUS_MINUS);
3318 /* If we have a template-id, then no further lookup is
3319 required. If the template-id was for a template-class, we
3320 will sometimes have a TYPE_DECL at this point. */
3321 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3322 || TREE_CODE (id_expression) == TYPE_DECL)
3323 decl = id_expression;
3324 /* Look up the name. */
3325 else
3326 {
3327 tree ambiguous_decls;
3328
3329 decl = cp_parser_lookup_name (parser, id_expression,
3330 none_type,
3331 template_p,
3332 /*is_namespace=*/false,
3333 /*check_dependency=*/true,
3334 &ambiguous_decls);
3335 /* If the lookup was ambiguous, an error will already have
3336 been issued. */
3337 if (ambiguous_decls)
3338 return error_mark_node;
3339
3340 /* In Objective-C++, an instance variable (ivar) may be preferred
3341 to whatever cp_parser_lookup_name() found. */
3342 decl = objc_lookup_ivar (decl, id_expression);
3343
3344 /* If name lookup gives us a SCOPE_REF, then the
3345 qualifying scope was dependent. */
3346 if (TREE_CODE (decl) == SCOPE_REF)
3347 return decl;
3348 /* Check to see if DECL is a local variable in a context
3349 where that is forbidden. */
3350 if (parser->local_variables_forbidden_p
3351 && local_variable_p (decl))
3352 {
3353 /* It might be that we only found DECL because we are
3354 trying to be generous with pre-ISO scoping rules.
3355 For example, consider:
3356
3357 int i;
3358 void g() {
3359 for (int i = 0; i < 10; ++i) {}
3360 extern void f(int j = i);
3361 }
3362
3363 Here, name look up will originally find the out
3364 of scope `i'. We need to issue a warning message,
3365 but then use the global `i'. */
3366 decl = check_for_out_of_scope_variable (decl);
3367 if (local_variable_p (decl))
3368 {
3369 error ("local variable %qD may not appear in this context",
3370 decl);
3371 return error_mark_node;
3372 }
3373 }
3374 }
3375
3376 decl = (finish_id_expression
3377 (id_expression, decl, parser->scope,
3378 idk,
3379 parser->integral_constant_expression_p,
3380 parser->allow_non_integral_constant_expression_p,
3381 &parser->non_integral_constant_expression_p,
3382 template_p, done, address_p,
3383 template_arg_p,
3384 &error_msg));
3385 if (error_msg)
3386 cp_parser_error (parser, error_msg);
3387 return decl;
3388 }
3389
3390 /* Anything else is an error. */
3391 default:
3392 /* ...unless we have an Objective-C++ message or string literal,
3393 that is. */
3394 if (c_dialect_objc ()
3395 && (token->type == CPP_OPEN_SQUARE
3396 || token->type == CPP_OBJC_STRING))
3397 return cp_parser_objc_expression (parser);
3398
3399 cp_parser_error (parser, "expected primary-expression");
3400 return error_mark_node;
3401 }
3402 }
3403
3404 /* Parse an id-expression.
3405
3406 id-expression:
3407 unqualified-id
3408 qualified-id
3409
3410 qualified-id:
3411 :: [opt] nested-name-specifier template [opt] unqualified-id
3412 :: identifier
3413 :: operator-function-id
3414 :: template-id
3415
3416 Return a representation of the unqualified portion of the
3417 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3418 a `::' or nested-name-specifier.
3419
3420 Often, if the id-expression was a qualified-id, the caller will
3421 want to make a SCOPE_REF to represent the qualified-id. This
3422 function does not do this in order to avoid wastefully creating
3423 SCOPE_REFs when they are not required.
3424
3425 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3426 `template' keyword.
3427
3428 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3429 uninstantiated templates.
3430
3431 If *TEMPLATE_P is non-NULL, it is set to true iff the
3432 `template' keyword is used to explicitly indicate that the entity
3433 named is a template.
3434
3435 If DECLARATOR_P is true, the id-expression is appearing as part of
3436 a declarator, rather than as part of an expression. */
3437
3438 static tree
3439 cp_parser_id_expression (cp_parser *parser,
3440 bool template_keyword_p,
3441 bool check_dependency_p,
3442 bool *template_p,
3443 bool declarator_p,
3444 bool optional_p)
3445 {
3446 bool global_scope_p;
3447 bool nested_name_specifier_p;
3448
3449 /* Assume the `template' keyword was not used. */
3450 if (template_p)
3451 *template_p = template_keyword_p;
3452
3453 /* Look for the optional `::' operator. */
3454 global_scope_p
3455 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3456 != NULL_TREE);
3457 /* Look for the optional nested-name-specifier. */
3458 nested_name_specifier_p
3459 = (cp_parser_nested_name_specifier_opt (parser,
3460 /*typename_keyword_p=*/false,
3461 check_dependency_p,
3462 /*type_p=*/false,
3463 declarator_p)
3464 != NULL_TREE);
3465 /* If there is a nested-name-specifier, then we are looking at
3466 the first qualified-id production. */
3467 if (nested_name_specifier_p)
3468 {
3469 tree saved_scope;
3470 tree saved_object_scope;
3471 tree saved_qualifying_scope;
3472 tree unqualified_id;
3473 bool is_template;
3474
3475 /* See if the next token is the `template' keyword. */
3476 if (!template_p)
3477 template_p = &is_template;
3478 *template_p = cp_parser_optional_template_keyword (parser);
3479 /* Name lookup we do during the processing of the
3480 unqualified-id might obliterate SCOPE. */
3481 saved_scope = parser->scope;
3482 saved_object_scope = parser->object_scope;
3483 saved_qualifying_scope = parser->qualifying_scope;
3484 /* Process the final unqualified-id. */
3485 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3486 check_dependency_p,
3487 declarator_p,
3488 /*optional_p=*/false);
3489 /* Restore the SAVED_SCOPE for our caller. */
3490 parser->scope = saved_scope;
3491 parser->object_scope = saved_object_scope;
3492 parser->qualifying_scope = saved_qualifying_scope;
3493
3494 return unqualified_id;
3495 }
3496 /* Otherwise, if we are in global scope, then we are looking at one
3497 of the other qualified-id productions. */
3498 else if (global_scope_p)
3499 {
3500 cp_token *token;
3501 tree id;
3502
3503 /* Peek at the next token. */
3504 token = cp_lexer_peek_token (parser->lexer);
3505
3506 /* If it's an identifier, and the next token is not a "<", then
3507 we can avoid the template-id case. This is an optimization
3508 for this common case. */
3509 if (token->type == CPP_NAME
3510 && !cp_parser_nth_token_starts_template_argument_list_p
3511 (parser, 2))
3512 return cp_parser_identifier (parser);
3513
3514 cp_parser_parse_tentatively (parser);
3515 /* Try a template-id. */
3516 id = cp_parser_template_id (parser,
3517 /*template_keyword_p=*/false,
3518 /*check_dependency_p=*/true,
3519 declarator_p);
3520 /* If that worked, we're done. */
3521 if (cp_parser_parse_definitely (parser))
3522 return id;
3523
3524 /* Peek at the next token. (Changes in the token buffer may
3525 have invalidated the pointer obtained above.) */
3526 token = cp_lexer_peek_token (parser->lexer);
3527
3528 switch (token->type)
3529 {
3530 case CPP_NAME:
3531 return cp_parser_identifier (parser);
3532
3533 case CPP_KEYWORD:
3534 if (token->keyword == RID_OPERATOR)
3535 return cp_parser_operator_function_id (parser);
3536 /* Fall through. */
3537
3538 default:
3539 cp_parser_error (parser, "expected id-expression");
3540 return error_mark_node;
3541 }
3542 }
3543 else
3544 return cp_parser_unqualified_id (parser, template_keyword_p,
3545 /*check_dependency_p=*/true,
3546 declarator_p,
3547 optional_p);
3548 }
3549
3550 /* Parse an unqualified-id.
3551
3552 unqualified-id:
3553 identifier
3554 operator-function-id
3555 conversion-function-id
3556 ~ class-name
3557 template-id
3558
3559 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3560 keyword, in a construct like `A::template ...'.
3561
3562 Returns a representation of unqualified-id. For the `identifier'
3563 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3564 production a BIT_NOT_EXPR is returned; the operand of the
3565 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3566 other productions, see the documentation accompanying the
3567 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3568 names are looked up in uninstantiated templates. If DECLARATOR_P
3569 is true, the unqualified-id is appearing as part of a declarator,
3570 rather than as part of an expression. */
3571
3572 static tree
3573 cp_parser_unqualified_id (cp_parser* parser,
3574 bool template_keyword_p,
3575 bool check_dependency_p,
3576 bool declarator_p,
3577 bool optional_p)
3578 {
3579 cp_token *token;
3580
3581 /* Peek at the next token. */
3582 token = cp_lexer_peek_token (parser->lexer);
3583
3584 switch (token->type)
3585 {
3586 case CPP_NAME:
3587 {
3588 tree id;
3589
3590 /* We don't know yet whether or not this will be a
3591 template-id. */
3592 cp_parser_parse_tentatively (parser);
3593 /* Try a template-id. */
3594 id = cp_parser_template_id (parser, template_keyword_p,
3595 check_dependency_p,
3596 declarator_p);
3597 /* If it worked, we're done. */
3598 if (cp_parser_parse_definitely (parser))
3599 return id;
3600 /* Otherwise, it's an ordinary identifier. */
3601 return cp_parser_identifier (parser);
3602 }
3603
3604 case CPP_TEMPLATE_ID:
3605 return cp_parser_template_id (parser, template_keyword_p,
3606 check_dependency_p,
3607 declarator_p);
3608
3609 case CPP_COMPL:
3610 {
3611 tree type_decl;
3612 tree qualifying_scope;
3613 tree object_scope;
3614 tree scope;
3615 bool done;
3616
3617 /* Consume the `~' token. */
3618 cp_lexer_consume_token (parser->lexer);
3619 /* Parse the class-name. The standard, as written, seems to
3620 say that:
3621
3622 template <typename T> struct S { ~S (); };
3623 template <typename T> S<T>::~S() {}
3624
3625 is invalid, since `~' must be followed by a class-name, but
3626 `S<T>' is dependent, and so not known to be a class.
3627 That's not right; we need to look in uninstantiated
3628 templates. A further complication arises from:
3629
3630 template <typename T> void f(T t) {
3631 t.T::~T();
3632 }
3633
3634 Here, it is not possible to look up `T' in the scope of `T'
3635 itself. We must look in both the current scope, and the
3636 scope of the containing complete expression.
3637
3638 Yet another issue is:
3639
3640 struct S {
3641 int S;
3642 ~S();
3643 };
3644
3645 S::~S() {}
3646
3647 The standard does not seem to say that the `S' in `~S'
3648 should refer to the type `S' and not the data member
3649 `S::S'. */
3650
3651 /* DR 244 says that we look up the name after the "~" in the
3652 same scope as we looked up the qualifying name. That idea
3653 isn't fully worked out; it's more complicated than that. */
3654 scope = parser->scope;
3655 object_scope = parser->object_scope;
3656 qualifying_scope = parser->qualifying_scope;
3657
3658 /* Check for invalid scopes. */
3659 if (scope == error_mark_node)
3660 {
3661 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3662 cp_lexer_consume_token (parser->lexer);
3663 return error_mark_node;
3664 }
3665 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3666 {
3667 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3668 error ("scope %qT before %<~%> is not a class-name", scope);
3669 cp_parser_simulate_error (parser);
3670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3671 cp_lexer_consume_token (parser->lexer);
3672 return error_mark_node;
3673 }
3674 gcc_assert (!scope || TYPE_P (scope));
3675
3676 /* If the name is of the form "X::~X" it's OK. */
3677 token = cp_lexer_peek_token (parser->lexer);
3678 if (scope
3679 && token->type == CPP_NAME
3680 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3681 == CPP_OPEN_PAREN)
3682 && constructor_name_p (token->u.value, scope))
3683 {
3684 cp_lexer_consume_token (parser->lexer);
3685 return build_nt (BIT_NOT_EXPR, scope);
3686 }
3687
3688 /* If there was an explicit qualification (S::~T), first look
3689 in the scope given by the qualification (i.e., S). */
3690 done = false;
3691 type_decl = NULL_TREE;
3692 if (scope)
3693 {
3694 cp_parser_parse_tentatively (parser);
3695 type_decl = cp_parser_class_name (parser,
3696 /*typename_keyword_p=*/false,
3697 /*template_keyword_p=*/false,
3698 none_type,
3699 /*check_dependency=*/false,
3700 /*class_head_p=*/false,
3701 declarator_p);
3702 if (cp_parser_parse_definitely (parser))
3703 done = true;
3704 }
3705 /* In "N::S::~S", look in "N" as well. */
3706 if (!done && scope && qualifying_scope)
3707 {
3708 cp_parser_parse_tentatively (parser);
3709 parser->scope = qualifying_scope;
3710 parser->object_scope = NULL_TREE;
3711 parser->qualifying_scope = NULL_TREE;
3712 type_decl
3713 = cp_parser_class_name (parser,
3714 /*typename_keyword_p=*/false,
3715 /*template_keyword_p=*/false,
3716 none_type,
3717 /*check_dependency=*/false,
3718 /*class_head_p=*/false,
3719 declarator_p);
3720 if (cp_parser_parse_definitely (parser))
3721 done = true;
3722 }
3723 /* In "p->S::~T", look in the scope given by "*p" as well. */
3724 else if (!done && object_scope)
3725 {
3726 cp_parser_parse_tentatively (parser);
3727 parser->scope = object_scope;
3728 parser->object_scope = NULL_TREE;
3729 parser->qualifying_scope = NULL_TREE;
3730 type_decl
3731 = cp_parser_class_name (parser,
3732 /*typename_keyword_p=*/false,
3733 /*template_keyword_p=*/false,
3734 none_type,
3735 /*check_dependency=*/false,
3736 /*class_head_p=*/false,
3737 declarator_p);
3738 if (cp_parser_parse_definitely (parser))
3739 done = true;
3740 }
3741 /* Look in the surrounding context. */
3742 if (!done)
3743 {
3744 parser->scope = NULL_TREE;
3745 parser->object_scope = NULL_TREE;
3746 parser->qualifying_scope = NULL_TREE;
3747 type_decl
3748 = cp_parser_class_name (parser,
3749 /*typename_keyword_p=*/false,
3750 /*template_keyword_p=*/false,
3751 none_type,
3752 /*check_dependency=*/false,
3753 /*class_head_p=*/false,
3754 declarator_p);
3755 }
3756 /* If an error occurred, assume that the name of the
3757 destructor is the same as the name of the qualifying
3758 class. That allows us to keep parsing after running
3759 into ill-formed destructor names. */
3760 if (type_decl == error_mark_node && scope)
3761 return build_nt (BIT_NOT_EXPR, scope);
3762 else if (type_decl == error_mark_node)
3763 return error_mark_node;
3764
3765 /* Check that destructor name and scope match. */
3766 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3767 {
3768 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3769 error ("declaration of %<~%T%> as member of %qT",
3770 type_decl, scope);
3771 cp_parser_simulate_error (parser);
3772 return error_mark_node;
3773 }
3774
3775 /* [class.dtor]
3776
3777 A typedef-name that names a class shall not be used as the
3778 identifier in the declarator for a destructor declaration. */
3779 if (declarator_p
3780 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3781 && !DECL_SELF_REFERENCE_P (type_decl)
3782 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3783 error ("typedef-name %qD used as destructor declarator",
3784 type_decl);
3785
3786 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3787 }
3788
3789 case CPP_KEYWORD:
3790 if (token->keyword == RID_OPERATOR)
3791 {
3792 tree id;
3793
3794 /* This could be a template-id, so we try that first. */
3795 cp_parser_parse_tentatively (parser);
3796 /* Try a template-id. */
3797 id = cp_parser_template_id (parser, template_keyword_p,
3798 /*check_dependency_p=*/true,
3799 declarator_p);
3800 /* If that worked, we're done. */
3801 if (cp_parser_parse_definitely (parser))
3802 return id;
3803 /* We still don't know whether we're looking at an
3804 operator-function-id or a conversion-function-id. */
3805 cp_parser_parse_tentatively (parser);
3806 /* Try an operator-function-id. */
3807 id = cp_parser_operator_function_id (parser);
3808 /* If that didn't work, try a conversion-function-id. */
3809 if (!cp_parser_parse_definitely (parser))
3810 id = cp_parser_conversion_function_id (parser);
3811
3812 return id;
3813 }
3814 /* Fall through. */
3815
3816 default:
3817 if (optional_p)
3818 return NULL_TREE;
3819 cp_parser_error (parser, "expected unqualified-id");
3820 return error_mark_node;
3821 }
3822 }
3823
3824 /* Parse an (optional) nested-name-specifier.
3825
3826 nested-name-specifier:
3827 class-or-namespace-name :: nested-name-specifier [opt]
3828 class-or-namespace-name :: template nested-name-specifier [opt]
3829
3830 PARSER->SCOPE should be set appropriately before this function is
3831 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3832 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3833 in name lookups.
3834
3835 Sets PARSER->SCOPE to the class (TYPE) or namespace
3836 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3837 it unchanged if there is no nested-name-specifier. Returns the new
3838 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3839
3840 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3841 part of a declaration and/or decl-specifier. */
3842
3843 static tree
3844 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3845 bool typename_keyword_p,
3846 bool check_dependency_p,
3847 bool type_p,
3848 bool is_declaration)
3849 {
3850 bool success = false;
3851 cp_token_position start = 0;
3852 cp_token *token;
3853
3854 /* Remember where the nested-name-specifier starts. */
3855 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3856 {
3857 start = cp_lexer_token_position (parser->lexer, false);
3858 push_deferring_access_checks (dk_deferred);
3859 }
3860
3861 while (true)
3862 {
3863 tree new_scope;
3864 tree old_scope;
3865 tree saved_qualifying_scope;
3866 bool template_keyword_p;
3867
3868 /* Spot cases that cannot be the beginning of a
3869 nested-name-specifier. */
3870 token = cp_lexer_peek_token (parser->lexer);
3871
3872 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3873 the already parsed nested-name-specifier. */
3874 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3875 {
3876 /* Grab the nested-name-specifier and continue the loop. */
3877 cp_parser_pre_parsed_nested_name_specifier (parser);
3878 /* If we originally encountered this nested-name-specifier
3879 with IS_DECLARATION set to false, we will not have
3880 resolved TYPENAME_TYPEs, so we must do so here. */
3881 if (is_declaration
3882 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3883 {
3884 new_scope = resolve_typename_type (parser->scope,
3885 /*only_current_p=*/false);
3886 if (new_scope != error_mark_node)
3887 parser->scope = new_scope;
3888 }
3889 success = true;
3890 continue;
3891 }
3892
3893 /* Spot cases that cannot be the beginning of a
3894 nested-name-specifier. On the second and subsequent times
3895 through the loop, we look for the `template' keyword. */
3896 if (success && token->keyword == RID_TEMPLATE)
3897 ;
3898 /* A template-id can start a nested-name-specifier. */
3899 else if (token->type == CPP_TEMPLATE_ID)
3900 ;
3901 else
3902 {
3903 /* If the next token is not an identifier, then it is
3904 definitely not a class-or-namespace-name. */
3905 if (token->type != CPP_NAME)
3906 break;
3907 /* If the following token is neither a `<' (to begin a
3908 template-id), nor a `::', then we are not looking at a
3909 nested-name-specifier. */
3910 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3911 if (token->type != CPP_SCOPE
3912 && !cp_parser_nth_token_starts_template_argument_list_p
3913 (parser, 2))
3914 break;
3915 }
3916
3917 /* The nested-name-specifier is optional, so we parse
3918 tentatively. */
3919 cp_parser_parse_tentatively (parser);
3920
3921 /* Look for the optional `template' keyword, if this isn't the
3922 first time through the loop. */
3923 if (success)
3924 template_keyword_p = cp_parser_optional_template_keyword (parser);
3925 else
3926 template_keyword_p = false;
3927
3928 /* Save the old scope since the name lookup we are about to do
3929 might destroy it. */
3930 old_scope = parser->scope;
3931 saved_qualifying_scope = parser->qualifying_scope;
3932 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3933 look up names in "X<T>::I" in order to determine that "Y" is
3934 a template. So, if we have a typename at this point, we make
3935 an effort to look through it. */
3936 if (is_declaration
3937 && !typename_keyword_p
3938 && parser->scope
3939 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3940 parser->scope = resolve_typename_type (parser->scope,
3941 /*only_current_p=*/false);
3942 /* Parse the qualifying entity. */
3943 new_scope
3944 = cp_parser_class_or_namespace_name (parser,
3945 typename_keyword_p,
3946 template_keyword_p,
3947 check_dependency_p,
3948 type_p,
3949 is_declaration);
3950 /* Look for the `::' token. */
3951 cp_parser_require (parser, CPP_SCOPE, "`::'");
3952
3953 /* If we found what we wanted, we keep going; otherwise, we're
3954 done. */
3955 if (!cp_parser_parse_definitely (parser))
3956 {
3957 bool error_p = false;
3958
3959 /* Restore the OLD_SCOPE since it was valid before the
3960 failed attempt at finding the last
3961 class-or-namespace-name. */
3962 parser->scope = old_scope;
3963 parser->qualifying_scope = saved_qualifying_scope;
3964 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3965 break;
3966 /* If the next token is an identifier, and the one after
3967 that is a `::', then any valid interpretation would have
3968 found a class-or-namespace-name. */
3969 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3970 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3971 == CPP_SCOPE)
3972 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3973 != CPP_COMPL))
3974 {
3975 token = cp_lexer_consume_token (parser->lexer);
3976 if (!error_p)
3977 {
3978 if (!token->ambiguous_p)
3979 {
3980 tree decl;
3981 tree ambiguous_decls;
3982
3983 decl = cp_parser_lookup_name (parser, token->u.value,
3984 none_type,
3985 /*is_template=*/false,
3986 /*is_namespace=*/false,
3987 /*check_dependency=*/true,
3988 &ambiguous_decls);
3989 if (TREE_CODE (decl) == TEMPLATE_DECL)
3990 error ("%qD used without template parameters", decl);
3991 else if (ambiguous_decls)
3992 {
3993 error ("reference to %qD is ambiguous",
3994 token->u.value);
3995 print_candidates (ambiguous_decls);
3996 decl = error_mark_node;
3997 }
3998 else
3999 cp_parser_name_lookup_error
4000 (parser, token->u.value, decl,
4001 "is not a class or namespace");
4002 }
4003 parser->scope = error_mark_node;
4004 error_p = true;
4005 /* Treat this as a successful nested-name-specifier
4006 due to:
4007
4008 [basic.lookup.qual]
4009
4010 If the name found is not a class-name (clause
4011 _class_) or namespace-name (_namespace.def_), the
4012 program is ill-formed. */
4013 success = true;
4014 }
4015 cp_lexer_consume_token (parser->lexer);
4016 }
4017 break;
4018 }
4019 /* We've found one valid nested-name-specifier. */
4020 success = true;
4021 /* Name lookup always gives us a DECL. */
4022 if (TREE_CODE (new_scope) == TYPE_DECL)
4023 new_scope = TREE_TYPE (new_scope);
4024 /* Uses of "template" must be followed by actual templates. */
4025 if (template_keyword_p
4026 && !(CLASS_TYPE_P (new_scope)
4027 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4028 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4029 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4030 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4031 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4032 == TEMPLATE_ID_EXPR)))
4033 pedwarn (TYPE_P (new_scope)
4034 ? "%qT is not a template"
4035 : "%qD is not a template",
4036 new_scope);
4037 /* If it is a class scope, try to complete it; we are about to
4038 be looking up names inside the class. */
4039 if (TYPE_P (new_scope)
4040 /* Since checking types for dependency can be expensive,
4041 avoid doing it if the type is already complete. */
4042 && !COMPLETE_TYPE_P (new_scope)
4043 /* Do not try to complete dependent types. */
4044 && !dependent_type_p (new_scope))
4045 new_scope = complete_type (new_scope);
4046 /* Make sure we look in the right scope the next time through
4047 the loop. */
4048 parser->scope = new_scope;
4049 }
4050
4051 /* If parsing tentatively, replace the sequence of tokens that makes
4052 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4053 token. That way, should we re-parse the token stream, we will
4054 not have to repeat the effort required to do the parse, nor will
4055 we issue duplicate error messages. */
4056 if (success && start)
4057 {
4058 cp_token *token;
4059
4060 token = cp_lexer_token_at (parser->lexer, start);
4061 /* Reset the contents of the START token. */
4062 token->type = CPP_NESTED_NAME_SPECIFIER;
4063 /* Retrieve any deferred checks. Do not pop this access checks yet
4064 so the memory will not be reclaimed during token replacing below. */
4065 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4066 token->u.tree_check_value->value = parser->scope;
4067 token->u.tree_check_value->checks = get_deferred_access_checks ();
4068 token->u.tree_check_value->qualifying_scope =
4069 parser->qualifying_scope;
4070 token->keyword = RID_MAX;
4071
4072 /* Purge all subsequent tokens. */
4073 cp_lexer_purge_tokens_after (parser->lexer, start);
4074 }
4075
4076 if (start)
4077 pop_to_parent_deferring_access_checks ();
4078
4079 return success ? parser->scope : NULL_TREE;
4080 }
4081
4082 /* Parse a nested-name-specifier. See
4083 cp_parser_nested_name_specifier_opt for details. This function
4084 behaves identically, except that it will an issue an error if no
4085 nested-name-specifier is present. */
4086
4087 static tree
4088 cp_parser_nested_name_specifier (cp_parser *parser,
4089 bool typename_keyword_p,
4090 bool check_dependency_p,
4091 bool type_p,
4092 bool is_declaration)
4093 {
4094 tree scope;
4095
4096 /* Look for the nested-name-specifier. */
4097 scope = cp_parser_nested_name_specifier_opt (parser,
4098 typename_keyword_p,
4099 check_dependency_p,
4100 type_p,
4101 is_declaration);
4102 /* If it was not present, issue an error message. */
4103 if (!scope)
4104 {
4105 cp_parser_error (parser, "expected nested-name-specifier");
4106 parser->scope = NULL_TREE;
4107 }
4108
4109 return scope;
4110 }
4111
4112 /* Parse a class-or-namespace-name.
4113
4114 class-or-namespace-name:
4115 class-name
4116 namespace-name
4117
4118 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4119 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4120 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4121 TYPE_P is TRUE iff the next name should be taken as a class-name,
4122 even the same name is declared to be another entity in the same
4123 scope.
4124
4125 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4126 specified by the class-or-namespace-name. If neither is found the
4127 ERROR_MARK_NODE is returned. */
4128
4129 static tree
4130 cp_parser_class_or_namespace_name (cp_parser *parser,
4131 bool typename_keyword_p,
4132 bool template_keyword_p,
4133 bool check_dependency_p,
4134 bool type_p,
4135 bool is_declaration)
4136 {
4137 tree saved_scope;
4138 tree saved_qualifying_scope;
4139 tree saved_object_scope;
4140 tree scope;
4141 bool only_class_p;
4142
4143 /* Before we try to parse the class-name, we must save away the
4144 current PARSER->SCOPE since cp_parser_class_name will destroy
4145 it. */
4146 saved_scope = parser->scope;
4147 saved_qualifying_scope = parser->qualifying_scope;
4148 saved_object_scope = parser->object_scope;
4149 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4150 there is no need to look for a namespace-name. */
4151 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4152 if (!only_class_p)
4153 cp_parser_parse_tentatively (parser);
4154 scope = cp_parser_class_name (parser,
4155 typename_keyword_p,
4156 template_keyword_p,
4157 type_p ? class_type : none_type,
4158 check_dependency_p,
4159 /*class_head_p=*/false,
4160 is_declaration);
4161 /* If that didn't work, try for a namespace-name. */
4162 if (!only_class_p && !cp_parser_parse_definitely (parser))
4163 {
4164 /* Restore the saved scope. */
4165 parser->scope = saved_scope;
4166 parser->qualifying_scope = saved_qualifying_scope;
4167 parser->object_scope = saved_object_scope;
4168 /* If we are not looking at an identifier followed by the scope
4169 resolution operator, then this is not part of a
4170 nested-name-specifier. (Note that this function is only used
4171 to parse the components of a nested-name-specifier.) */
4172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4173 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4174 return error_mark_node;
4175 scope = cp_parser_namespace_name (parser);
4176 }
4177
4178 return scope;
4179 }
4180
4181 /* Parse a postfix-expression.
4182
4183 postfix-expression:
4184 primary-expression
4185 postfix-expression [ expression ]
4186 postfix-expression ( expression-list [opt] )
4187 simple-type-specifier ( expression-list [opt] )
4188 typename :: [opt] nested-name-specifier identifier
4189 ( expression-list [opt] )
4190 typename :: [opt] nested-name-specifier template [opt] template-id
4191 ( expression-list [opt] )
4192 postfix-expression . template [opt] id-expression
4193 postfix-expression -> template [opt] id-expression
4194 postfix-expression . pseudo-destructor-name
4195 postfix-expression -> pseudo-destructor-name
4196 postfix-expression ++
4197 postfix-expression --
4198 dynamic_cast < type-id > ( expression )
4199 static_cast < type-id > ( expression )
4200 reinterpret_cast < type-id > ( expression )
4201 const_cast < type-id > ( expression )
4202 typeid ( expression )
4203 typeid ( type-id )
4204
4205 GNU Extension:
4206
4207 postfix-expression:
4208 ( type-id ) { initializer-list , [opt] }
4209
4210 This extension is a GNU version of the C99 compound-literal
4211 construct. (The C99 grammar uses `type-name' instead of `type-id',
4212 but they are essentially the same concept.)
4213
4214 If ADDRESS_P is true, the postfix expression is the operand of the
4215 `&' operator. CAST_P is true if this expression is the target of a
4216 cast.
4217
4218 Returns a representation of the expression. */
4219
4220 static tree
4221 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4222 {
4223 cp_token *token;
4224 enum rid keyword;
4225 cp_id_kind idk = CP_ID_KIND_NONE;
4226 tree postfix_expression = NULL_TREE;
4227
4228 /* Peek at the next token. */
4229 token = cp_lexer_peek_token (parser->lexer);
4230 /* Some of the productions are determined by keywords. */
4231 keyword = token->keyword;
4232 switch (keyword)
4233 {
4234 case RID_DYNCAST:
4235 case RID_STATCAST:
4236 case RID_REINTCAST:
4237 case RID_CONSTCAST:
4238 {
4239 tree type;
4240 tree expression;
4241 const char *saved_message;
4242
4243 /* All of these can be handled in the same way from the point
4244 of view of parsing. Begin by consuming the token
4245 identifying the cast. */
4246 cp_lexer_consume_token (parser->lexer);
4247
4248 /* New types cannot be defined in the cast. */
4249 saved_message = parser->type_definition_forbidden_message;
4250 parser->type_definition_forbidden_message
4251 = "types may not be defined in casts";
4252
4253 /* Look for the opening `<'. */
4254 cp_parser_require (parser, CPP_LESS, "`<'");
4255 /* Parse the type to which we are casting. */
4256 type = cp_parser_type_id (parser);
4257 /* Look for the closing `>'. */
4258 cp_parser_require (parser, CPP_GREATER, "`>'");
4259 /* Restore the old message. */
4260 parser->type_definition_forbidden_message = saved_message;
4261
4262 /* And the expression which is being cast. */
4263 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4264 expression = cp_parser_expression (parser, /*cast_p=*/true);
4265 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4266
4267 /* Only type conversions to integral or enumeration types
4268 can be used in constant-expressions. */
4269 if (!cast_valid_in_integral_constant_expression_p (type)
4270 && (cp_parser_non_integral_constant_expression
4271 (parser,
4272 "a cast to a type other than an integral or "
4273 "enumeration type")))
4274 return error_mark_node;
4275
4276 switch (keyword)
4277 {
4278 case RID_DYNCAST:
4279 postfix_expression
4280 = build_dynamic_cast (type, expression);
4281 break;
4282 case RID_STATCAST:
4283 postfix_expression
4284 = build_static_cast (type, expression);
4285 break;
4286 case RID_REINTCAST:
4287 postfix_expression
4288 = build_reinterpret_cast (type, expression);
4289 break;
4290 case RID_CONSTCAST:
4291 postfix_expression
4292 = build_const_cast (type, expression);
4293 break;
4294 default:
4295 gcc_unreachable ();
4296 }
4297 }
4298 break;
4299
4300 case RID_TYPEID:
4301 {
4302 tree type;
4303 const char *saved_message;
4304 bool saved_in_type_id_in_expr_p;
4305
4306 /* Consume the `typeid' token. */
4307 cp_lexer_consume_token (parser->lexer);
4308 /* Look for the `(' token. */
4309 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4310 /* Types cannot be defined in a `typeid' expression. */
4311 saved_message = parser->type_definition_forbidden_message;
4312 parser->type_definition_forbidden_message
4313 = "types may not be defined in a `typeid\' expression";
4314 /* We can't be sure yet whether we're looking at a type-id or an
4315 expression. */
4316 cp_parser_parse_tentatively (parser);
4317 /* Try a type-id first. */
4318 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4319 parser->in_type_id_in_expr_p = true;
4320 type = cp_parser_type_id (parser);
4321 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4322 /* Look for the `)' token. Otherwise, we can't be sure that
4323 we're not looking at an expression: consider `typeid (int
4324 (3))', for example. */
4325 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4326 /* If all went well, simply lookup the type-id. */
4327 if (cp_parser_parse_definitely (parser))
4328 postfix_expression = get_typeid (type);
4329 /* Otherwise, fall back to the expression variant. */
4330 else
4331 {
4332 tree expression;
4333
4334 /* Look for an expression. */
4335 expression = cp_parser_expression (parser, /*cast_p=*/false);
4336 /* Compute its typeid. */
4337 postfix_expression = build_typeid (expression);
4338 /* Look for the `)' token. */
4339 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4340 }
4341 /* Restore the saved message. */
4342 parser->type_definition_forbidden_message = saved_message;
4343 /* `typeid' may not appear in an integral constant expression. */
4344 if (cp_parser_non_integral_constant_expression(parser,
4345 "`typeid' operator"))
4346 return error_mark_node;
4347 }
4348 break;
4349
4350 case RID_TYPENAME:
4351 {
4352 tree type;
4353 /* The syntax permitted here is the same permitted for an
4354 elaborated-type-specifier. */
4355 type = cp_parser_elaborated_type_specifier (parser,
4356 /*is_friend=*/false,
4357 /*is_declaration=*/false);
4358 postfix_expression = cp_parser_functional_cast (parser, type);
4359 }
4360 break;
4361
4362 default:
4363 {
4364 tree type;
4365
4366 /* If the next thing is a simple-type-specifier, we may be
4367 looking at a functional cast. We could also be looking at
4368 an id-expression. So, we try the functional cast, and if
4369 that doesn't work we fall back to the primary-expression. */
4370 cp_parser_parse_tentatively (parser);
4371 /* Look for the simple-type-specifier. */
4372 type = cp_parser_simple_type_specifier (parser,
4373 /*decl_specs=*/NULL,
4374 CP_PARSER_FLAGS_NONE);
4375 /* Parse the cast itself. */
4376 if (!cp_parser_error_occurred (parser))
4377 postfix_expression
4378 = cp_parser_functional_cast (parser, type);
4379 /* If that worked, we're done. */
4380 if (cp_parser_parse_definitely (parser))
4381 break;
4382
4383 /* If the functional-cast didn't work out, try a
4384 compound-literal. */
4385 if (cp_parser_allow_gnu_extensions_p (parser)
4386 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4387 {
4388 VEC(constructor_elt,gc) *initializer_list = NULL;
4389 bool saved_in_type_id_in_expr_p;
4390
4391 cp_parser_parse_tentatively (parser);
4392 /* Consume the `('. */
4393 cp_lexer_consume_token (parser->lexer);
4394 /* Parse the type. */
4395 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4396 parser->in_type_id_in_expr_p = true;
4397 type = cp_parser_type_id (parser);
4398 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4399 /* Look for the `)'. */
4400 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4401 /* Look for the `{'. */
4402 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4403 /* If things aren't going well, there's no need to
4404 keep going. */
4405 if (!cp_parser_error_occurred (parser))
4406 {
4407 bool non_constant_p;
4408 /* Parse the initializer-list. */
4409 initializer_list
4410 = cp_parser_initializer_list (parser, &non_constant_p);
4411 /* Allow a trailing `,'. */
4412 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4413 cp_lexer_consume_token (parser->lexer);
4414 /* Look for the final `}'. */
4415 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4416 }
4417 /* If that worked, we're definitely looking at a
4418 compound-literal expression. */
4419 if (cp_parser_parse_definitely (parser))
4420 {
4421 /* Warn the user that a compound literal is not
4422 allowed in standard C++. */
4423 if (pedantic)
4424 pedwarn ("ISO C++ forbids compound-literals");
4425 /* For simplicity, we disallow compound literals in
4426 constant-expressions. We could
4427 allow compound literals of integer type, whose
4428 initializer was a constant, in constant
4429 expressions. Permitting that usage, as a further
4430 extension, would not change the meaning of any
4431 currently accepted programs. (Of course, as
4432 compound literals are not part of ISO C++, the
4433 standard has nothing to say.) */
4434 if (cp_parser_non_integral_constant_expression
4435 (parser, "non-constant compound literals"))
4436 {
4437 postfix_expression = error_mark_node;
4438 break;
4439 }
4440 /* Form the representation of the compound-literal. */
4441 postfix_expression
4442 = finish_compound_literal (type, initializer_list);
4443 break;
4444 }
4445 }
4446
4447 /* It must be a primary-expression. */
4448 postfix_expression
4449 = cp_parser_primary_expression (parser, address_p, cast_p,
4450 /*template_arg_p=*/false,
4451 &idk);
4452 }
4453 break;
4454 }
4455
4456 /* Keep looping until the postfix-expression is complete. */
4457 while (true)
4458 {
4459 if (idk == CP_ID_KIND_UNQUALIFIED
4460 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4461 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4462 /* It is not a Koenig lookup function call. */
4463 postfix_expression
4464 = unqualified_name_lookup_error (postfix_expression);
4465
4466 /* Peek at the next token. */
4467 token = cp_lexer_peek_token (parser->lexer);
4468
4469 switch (token->type)
4470 {
4471 case CPP_OPEN_SQUARE:
4472 postfix_expression
4473 = cp_parser_postfix_open_square_expression (parser,
4474 postfix_expression,
4475 false);
4476 idk = CP_ID_KIND_NONE;
4477 break;
4478
4479 case CPP_OPEN_PAREN:
4480 /* postfix-expression ( expression-list [opt] ) */
4481 {
4482 bool koenig_p;
4483 bool is_builtin_constant_p;
4484 bool saved_integral_constant_expression_p = false;
4485 bool saved_non_integral_constant_expression_p = false;
4486 tree args;
4487
4488 is_builtin_constant_p
4489 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4490 if (is_builtin_constant_p)
4491 {
4492 /* The whole point of __builtin_constant_p is to allow
4493 non-constant expressions to appear as arguments. */
4494 saved_integral_constant_expression_p
4495 = parser->integral_constant_expression_p;
4496 saved_non_integral_constant_expression_p
4497 = parser->non_integral_constant_expression_p;
4498 parser->integral_constant_expression_p = false;
4499 }
4500 args = (cp_parser_parenthesized_expression_list
4501 (parser, /*is_attribute_list=*/false,
4502 /*cast_p=*/false, /*allow_expansion_p=*/true,
4503 /*non_constant_p=*/NULL));
4504 if (is_builtin_constant_p)
4505 {
4506 parser->integral_constant_expression_p
4507 = saved_integral_constant_expression_p;
4508 parser->non_integral_constant_expression_p
4509 = saved_non_integral_constant_expression_p;
4510 }
4511
4512 if (args == error_mark_node)
4513 {
4514 postfix_expression = error_mark_node;
4515 break;
4516 }
4517
4518 /* Function calls are not permitted in
4519 constant-expressions. */
4520 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4521 && cp_parser_non_integral_constant_expression (parser,
4522 "a function call"))
4523 {
4524 postfix_expression = error_mark_node;
4525 break;
4526 }
4527
4528 koenig_p = false;
4529 if (idk == CP_ID_KIND_UNQUALIFIED)
4530 {
4531 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4532 {
4533 if (args)
4534 {
4535 koenig_p = true;
4536 postfix_expression
4537 = perform_koenig_lookup (postfix_expression, args);
4538 }
4539 else
4540 postfix_expression
4541 = unqualified_fn_lookup_error (postfix_expression);
4542 }
4543 /* We do not perform argument-dependent lookup if
4544 normal lookup finds a non-function, in accordance
4545 with the expected resolution of DR 218. */
4546 else if (args && is_overloaded_fn (postfix_expression))
4547 {
4548 tree fn = get_first_fn (postfix_expression);
4549
4550 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4551 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4552
4553 /* Only do argument dependent lookup if regular
4554 lookup does not find a set of member functions.
4555 [basic.lookup.koenig]/2a */
4556 if (!DECL_FUNCTION_MEMBER_P (fn))
4557 {
4558 koenig_p = true;
4559 postfix_expression
4560 = perform_koenig_lookup (postfix_expression, args);
4561 }
4562 }
4563 }
4564
4565 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4566 {
4567 tree instance = TREE_OPERAND (postfix_expression, 0);
4568 tree fn = TREE_OPERAND (postfix_expression, 1);
4569
4570 if (processing_template_decl
4571 && (type_dependent_expression_p (instance)
4572 || (!BASELINK_P (fn)
4573 && TREE_CODE (fn) != FIELD_DECL)
4574 || type_dependent_expression_p (fn)
4575 || any_type_dependent_arguments_p (args)))
4576 {
4577 postfix_expression
4578 = build_nt_call_list (postfix_expression, args);
4579 break;
4580 }
4581
4582 if (BASELINK_P (fn))
4583 postfix_expression
4584 = (build_new_method_call
4585 (instance, fn, args, NULL_TREE,
4586 (idk == CP_ID_KIND_QUALIFIED
4587 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4588 /*fn_p=*/NULL));
4589 else
4590 postfix_expression
4591 = finish_call_expr (postfix_expression, args,
4592 /*disallow_virtual=*/false,
4593 /*koenig_p=*/false);
4594 }
4595 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4596 || TREE_CODE (postfix_expression) == MEMBER_REF
4597 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4598 postfix_expression = (build_offset_ref_call_from_tree
4599 (postfix_expression, args));
4600 else if (idk == CP_ID_KIND_QUALIFIED)
4601 /* A call to a static class member, or a namespace-scope
4602 function. */
4603 postfix_expression
4604 = finish_call_expr (postfix_expression, args,
4605 /*disallow_virtual=*/true,
4606 koenig_p);
4607 else
4608 /* All other function calls. */
4609 postfix_expression
4610 = finish_call_expr (postfix_expression, args,
4611 /*disallow_virtual=*/false,
4612 koenig_p);
4613
4614 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4615 idk = CP_ID_KIND_NONE;
4616 }
4617 break;
4618
4619 case CPP_DOT:
4620 case CPP_DEREF:
4621 /* postfix-expression . template [opt] id-expression
4622 postfix-expression . pseudo-destructor-name
4623 postfix-expression -> template [opt] id-expression
4624 postfix-expression -> pseudo-destructor-name */
4625
4626 /* Consume the `.' or `->' operator. */
4627 cp_lexer_consume_token (parser->lexer);
4628
4629 postfix_expression
4630 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4631 postfix_expression,
4632 false, &idk);
4633 break;
4634
4635 case CPP_PLUS_PLUS:
4636 /* postfix-expression ++ */
4637 /* Consume the `++' token. */
4638 cp_lexer_consume_token (parser->lexer);
4639 /* Generate a representation for the complete expression. */
4640 postfix_expression
4641 = finish_increment_expr (postfix_expression,
4642 POSTINCREMENT_EXPR);
4643 /* Increments may not appear in constant-expressions. */
4644 if (cp_parser_non_integral_constant_expression (parser,
4645 "an increment"))
4646 postfix_expression = error_mark_node;
4647 idk = CP_ID_KIND_NONE;
4648 break;
4649
4650 case CPP_MINUS_MINUS:
4651 /* postfix-expression -- */
4652 /* Consume the `--' token. */
4653 cp_lexer_consume_token (parser->lexer);
4654 /* Generate a representation for the complete expression. */
4655 postfix_expression
4656 = finish_increment_expr (postfix_expression,
4657 POSTDECREMENT_EXPR);
4658 /* Decrements may not appear in constant-expressions. */
4659 if (cp_parser_non_integral_constant_expression (parser,
4660 "a decrement"))
4661 postfix_expression = error_mark_node;
4662 idk = CP_ID_KIND_NONE;
4663 break;
4664
4665 default:
4666 return postfix_expression;
4667 }
4668 }
4669
4670 /* We should never get here. */
4671 gcc_unreachable ();
4672 return error_mark_node;
4673 }
4674
4675 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4676 by cp_parser_builtin_offsetof. We're looking for
4677
4678 postfix-expression [ expression ]
4679
4680 FOR_OFFSETOF is set if we're being called in that context, which
4681 changes how we deal with integer constant expressions. */
4682
4683 static tree
4684 cp_parser_postfix_open_square_expression (cp_parser *parser,
4685 tree postfix_expression,
4686 bool for_offsetof)
4687 {
4688 tree index;
4689
4690 /* Consume the `[' token. */
4691 cp_lexer_consume_token (parser->lexer);
4692
4693 /* Parse the index expression. */
4694 /* ??? For offsetof, there is a question of what to allow here. If
4695 offsetof is not being used in an integral constant expression context,
4696 then we *could* get the right answer by computing the value at runtime.
4697 If we are in an integral constant expression context, then we might
4698 could accept any constant expression; hard to say without analysis.
4699 Rather than open the barn door too wide right away, allow only integer
4700 constant expressions here. */
4701 if (for_offsetof)
4702 index = cp_parser_constant_expression (parser, false, NULL);
4703 else
4704 index = cp_parser_expression (parser, /*cast_p=*/false);
4705
4706 /* Look for the closing `]'. */
4707 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4708
4709 /* Build the ARRAY_REF. */
4710 postfix_expression = grok_array_decl (postfix_expression, index);
4711
4712 /* When not doing offsetof, array references are not permitted in
4713 constant-expressions. */
4714 if (!for_offsetof
4715 && (cp_parser_non_integral_constant_expression
4716 (parser, "an array reference")))
4717 postfix_expression = error_mark_node;
4718
4719 return postfix_expression;
4720 }
4721
4722 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4723 by cp_parser_builtin_offsetof. We're looking for
4724
4725 postfix-expression . template [opt] id-expression
4726 postfix-expression . pseudo-destructor-name
4727 postfix-expression -> template [opt] id-expression
4728 postfix-expression -> pseudo-destructor-name
4729
4730 FOR_OFFSETOF is set if we're being called in that context. That sorta
4731 limits what of the above we'll actually accept, but nevermind.
4732 TOKEN_TYPE is the "." or "->" token, which will already have been
4733 removed from the stream. */
4734
4735 static tree
4736 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4737 enum cpp_ttype token_type,
4738 tree postfix_expression,
4739 bool for_offsetof, cp_id_kind *idk)
4740 {
4741 tree name;
4742 bool dependent_p;
4743 bool pseudo_destructor_p;
4744 tree scope = NULL_TREE;
4745
4746 /* If this is a `->' operator, dereference the pointer. */
4747 if (token_type == CPP_DEREF)
4748 postfix_expression = build_x_arrow (postfix_expression);
4749 /* Check to see whether or not the expression is type-dependent. */
4750 dependent_p = type_dependent_expression_p (postfix_expression);
4751 /* The identifier following the `->' or `.' is not qualified. */
4752 parser->scope = NULL_TREE;
4753 parser->qualifying_scope = NULL_TREE;
4754 parser->object_scope = NULL_TREE;
4755 *idk = CP_ID_KIND_NONE;
4756 /* Enter the scope corresponding to the type of the object
4757 given by the POSTFIX_EXPRESSION. */
4758 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4759 {
4760 scope = TREE_TYPE (postfix_expression);
4761 /* According to the standard, no expression should ever have
4762 reference type. Unfortunately, we do not currently match
4763 the standard in this respect in that our internal representation
4764 of an expression may have reference type even when the standard
4765 says it does not. Therefore, we have to manually obtain the
4766 underlying type here. */
4767 scope = non_reference (scope);
4768 /* The type of the POSTFIX_EXPRESSION must be complete. */
4769 if (scope == unknown_type_node)
4770 {
4771 error ("%qE does not have class type", postfix_expression);
4772 scope = NULL_TREE;
4773 }
4774 else
4775 scope = complete_type_or_else (scope, NULL_TREE);
4776 /* Let the name lookup machinery know that we are processing a
4777 class member access expression. */
4778 parser->context->object_type = scope;
4779 /* If something went wrong, we want to be able to discern that case,
4780 as opposed to the case where there was no SCOPE due to the type
4781 of expression being dependent. */
4782 if (!scope)
4783 scope = error_mark_node;
4784 /* If the SCOPE was erroneous, make the various semantic analysis
4785 functions exit quickly -- and without issuing additional error
4786 messages. */
4787 if (scope == error_mark_node)
4788 postfix_expression = error_mark_node;
4789 }
4790
4791 /* Assume this expression is not a pseudo-destructor access. */
4792 pseudo_destructor_p = false;
4793
4794 /* If the SCOPE is a scalar type, then, if this is a valid program,
4795 we must be looking at a pseudo-destructor-name. */
4796 if (scope && SCALAR_TYPE_P (scope))
4797 {
4798 tree s;
4799 tree type;
4800
4801 cp_parser_parse_tentatively (parser);
4802 /* Parse the pseudo-destructor-name. */
4803 s = NULL_TREE;
4804 cp_parser_pseudo_destructor_name (parser, &s, &type);
4805 if (cp_parser_parse_definitely (parser))
4806 {
4807 pseudo_destructor_p = true;
4808 postfix_expression
4809 = finish_pseudo_destructor_expr (postfix_expression,
4810 s, TREE_TYPE (type));
4811 }
4812 }
4813
4814 if (!pseudo_destructor_p)
4815 {
4816 /* If the SCOPE is not a scalar type, we are looking at an
4817 ordinary class member access expression, rather than a
4818 pseudo-destructor-name. */
4819 bool template_p;
4820 /* Parse the id-expression. */
4821 name = (cp_parser_id_expression
4822 (parser,
4823 cp_parser_optional_template_keyword (parser),
4824 /*check_dependency_p=*/true,
4825 &template_p,
4826 /*declarator_p=*/false,
4827 /*optional_p=*/false));
4828 /* In general, build a SCOPE_REF if the member name is qualified.
4829 However, if the name was not dependent and has already been
4830 resolved; there is no need to build the SCOPE_REF. For example;
4831
4832 struct X { void f(); };
4833 template <typename T> void f(T* t) { t->X::f(); }
4834
4835 Even though "t" is dependent, "X::f" is not and has been resolved
4836 to a BASELINK; there is no need to include scope information. */
4837
4838 /* But we do need to remember that there was an explicit scope for
4839 virtual function calls. */
4840 if (parser->scope)
4841 *idk = CP_ID_KIND_QUALIFIED;
4842
4843 /* If the name is a template-id that names a type, we will get a
4844 TYPE_DECL here. That is invalid code. */
4845 if (TREE_CODE (name) == TYPE_DECL)
4846 {
4847 error ("invalid use of %qD", name);
4848 postfix_expression = error_mark_node;
4849 }
4850 else
4851 {
4852 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4853 {
4854 name = build_qualified_name (/*type=*/NULL_TREE,
4855 parser->scope,
4856 name,
4857 template_p);
4858 parser->scope = NULL_TREE;
4859 parser->qualifying_scope = NULL_TREE;
4860 parser->object_scope = NULL_TREE;
4861 }
4862 if (scope && name && BASELINK_P (name))
4863 adjust_result_of_qualified_name_lookup
4864 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4865 postfix_expression
4866 = finish_class_member_access_expr (postfix_expression, name,
4867 template_p);
4868 }
4869 }
4870
4871 /* We no longer need to look up names in the scope of the object on
4872 the left-hand side of the `.' or `->' operator. */
4873 parser->context->object_type = NULL_TREE;
4874
4875 /* Outside of offsetof, these operators may not appear in
4876 constant-expressions. */
4877 if (!for_offsetof
4878 && (cp_parser_non_integral_constant_expression
4879 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4880 postfix_expression = error_mark_node;
4881
4882 return postfix_expression;
4883 }
4884
4885 /* Parse a parenthesized expression-list.
4886
4887 expression-list:
4888 assignment-expression
4889 expression-list, assignment-expression
4890
4891 attribute-list:
4892 expression-list
4893 identifier
4894 identifier, expression-list
4895
4896 CAST_P is true if this expression is the target of a cast.
4897
4898 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4899 argument pack.
4900
4901 Returns a TREE_LIST. The TREE_VALUE of each node is a
4902 representation of an assignment-expression. Note that a TREE_LIST
4903 is returned even if there is only a single expression in the list.
4904 error_mark_node is returned if the ( and or ) are
4905 missing. NULL_TREE is returned on no expressions. The parentheses
4906 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4907 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4908 indicates whether or not all of the expressions in the list were
4909 constant. */
4910
4911 static tree
4912 cp_parser_parenthesized_expression_list (cp_parser* parser,
4913 bool is_attribute_list,
4914 bool cast_p,
4915 bool allow_expansion_p,
4916 bool *non_constant_p)
4917 {
4918 tree expression_list = NULL_TREE;
4919 bool fold_expr_p = is_attribute_list;
4920 tree identifier = NULL_TREE;
4921
4922 /* Assume all the expressions will be constant. */
4923 if (non_constant_p)
4924 *non_constant_p = false;
4925
4926 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4927 return error_mark_node;
4928
4929 /* Consume expressions until there are no more. */
4930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4931 while (true)
4932 {
4933 tree expr;
4934
4935 /* At the beginning of attribute lists, check to see if the
4936 next token is an identifier. */
4937 if (is_attribute_list
4938 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4939 {
4940 cp_token *token;
4941
4942 /* Consume the identifier. */
4943 token = cp_lexer_consume_token (parser->lexer);
4944 /* Save the identifier. */
4945 identifier = token->u.value;
4946 }
4947 else
4948 {
4949 /* Parse the next assignment-expression. */
4950 if (non_constant_p)
4951 {
4952 bool expr_non_constant_p;
4953 expr = (cp_parser_constant_expression
4954 (parser, /*allow_non_constant_p=*/true,
4955 &expr_non_constant_p));
4956 if (expr_non_constant_p)
4957 *non_constant_p = true;
4958 }
4959 else
4960 expr = cp_parser_assignment_expression (parser, cast_p);
4961
4962 if (fold_expr_p)
4963 expr = fold_non_dependent_expr (expr);
4964
4965 /* If we have an ellipsis, then this is an expression
4966 expansion. */
4967 if (allow_expansion_p
4968 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4969 {
4970 /* Consume the `...'. */
4971 cp_lexer_consume_token (parser->lexer);
4972
4973 /* Build the argument pack. */
4974 expr = make_pack_expansion (expr);
4975 }
4976
4977 /* Add it to the list. We add error_mark_node
4978 expressions to the list, so that we can still tell if
4979 the correct form for a parenthesized expression-list
4980 is found. That gives better errors. */
4981 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4982
4983 if (expr == error_mark_node)
4984 goto skip_comma;
4985 }
4986
4987 /* After the first item, attribute lists look the same as
4988 expression lists. */
4989 is_attribute_list = false;
4990
4991 get_comma:;
4992 /* If the next token isn't a `,', then we are done. */
4993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4994 break;
4995
4996 /* Otherwise, consume the `,' and keep going. */
4997 cp_lexer_consume_token (parser->lexer);
4998 }
4999
5000 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5001 {
5002 int ending;
5003
5004 skip_comma:;
5005 /* We try and resync to an unnested comma, as that will give the
5006 user better diagnostics. */
5007 ending = cp_parser_skip_to_closing_parenthesis (parser,
5008 /*recovering=*/true,
5009 /*or_comma=*/true,
5010 /*consume_paren=*/true);
5011 if (ending < 0)
5012 goto get_comma;
5013 if (!ending)
5014 return error_mark_node;
5015 }
5016
5017 /* We built up the list in reverse order so we must reverse it now. */
5018 expression_list = nreverse (expression_list);
5019 if (identifier)
5020 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5021
5022 return expression_list;
5023 }
5024
5025 /* Parse a pseudo-destructor-name.
5026
5027 pseudo-destructor-name:
5028 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5029 :: [opt] nested-name-specifier template template-id :: ~ type-name
5030 :: [opt] nested-name-specifier [opt] ~ type-name
5031
5032 If either of the first two productions is used, sets *SCOPE to the
5033 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5034 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5035 or ERROR_MARK_NODE if the parse fails. */
5036
5037 static void
5038 cp_parser_pseudo_destructor_name (cp_parser* parser,
5039 tree* scope,
5040 tree* type)
5041 {
5042 bool nested_name_specifier_p;
5043
5044 /* Assume that things will not work out. */
5045 *type = error_mark_node;
5046
5047 /* Look for the optional `::' operator. */
5048 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5049 /* Look for the optional nested-name-specifier. */
5050 nested_name_specifier_p
5051 = (cp_parser_nested_name_specifier_opt (parser,
5052 /*typename_keyword_p=*/false,
5053 /*check_dependency_p=*/true,
5054 /*type_p=*/false,
5055 /*is_declaration=*/true)
5056 != NULL_TREE);
5057 /* Now, if we saw a nested-name-specifier, we might be doing the
5058 second production. */
5059 if (nested_name_specifier_p
5060 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5061 {
5062 /* Consume the `template' keyword. */
5063 cp_lexer_consume_token (parser->lexer);
5064 /* Parse the template-id. */
5065 cp_parser_template_id (parser,
5066 /*template_keyword_p=*/true,
5067 /*check_dependency_p=*/false,
5068 /*is_declaration=*/true);
5069 /* Look for the `::' token. */
5070 cp_parser_require (parser, CPP_SCOPE, "`::'");
5071 }
5072 /* If the next token is not a `~', then there might be some
5073 additional qualification. */
5074 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5075 {
5076 /* Look for the type-name. */
5077 *scope = TREE_TYPE (cp_parser_type_name (parser));
5078
5079 if (*scope == error_mark_node)
5080 return;
5081
5082 /* If we don't have ::~, then something has gone wrong. Since
5083 the only caller of this function is looking for something
5084 after `.' or `->' after a scalar type, most likely the
5085 program is trying to get a member of a non-aggregate
5086 type. */
5087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5088 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5089 {
5090 cp_parser_error (parser, "request for member of non-aggregate type");
5091 return;
5092 }
5093
5094 /* Look for the `::' token. */
5095 cp_parser_require (parser, CPP_SCOPE, "`::'");
5096 }
5097 else
5098 *scope = NULL_TREE;
5099
5100 /* Look for the `~'. */
5101 cp_parser_require (parser, CPP_COMPL, "`~'");
5102 /* Look for the type-name again. We are not responsible for
5103 checking that it matches the first type-name. */
5104 *type = cp_parser_type_name (parser);
5105 }
5106
5107 /* Parse a unary-expression.
5108
5109 unary-expression:
5110 postfix-expression
5111 ++ cast-expression
5112 -- cast-expression
5113 unary-operator cast-expression
5114 sizeof unary-expression
5115 sizeof ( type-id )
5116 new-expression
5117 delete-expression
5118
5119 GNU Extensions:
5120
5121 unary-expression:
5122 __extension__ cast-expression
5123 __alignof__ unary-expression
5124 __alignof__ ( type-id )
5125 __real__ cast-expression
5126 __imag__ cast-expression
5127 && identifier
5128
5129 ADDRESS_P is true iff the unary-expression is appearing as the
5130 operand of the `&' operator. CAST_P is true if this expression is
5131 the target of a cast.
5132
5133 Returns a representation of the expression. */
5134
5135 static tree
5136 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5137 {
5138 cp_token *token;
5139 enum tree_code unary_operator;
5140
5141 /* Peek at the next token. */
5142 token = cp_lexer_peek_token (parser->lexer);
5143 /* Some keywords give away the kind of expression. */
5144 if (token->type == CPP_KEYWORD)
5145 {
5146 enum rid keyword = token->keyword;
5147
5148 switch (keyword)
5149 {
5150 case RID_ALIGNOF:
5151 case RID_SIZEOF:
5152 {
5153 tree operand;
5154 enum tree_code op;
5155
5156 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5157 /* Consume the token. */
5158 cp_lexer_consume_token (parser->lexer);
5159 /* Parse the operand. */
5160 operand = cp_parser_sizeof_operand (parser, keyword);
5161
5162 if (TYPE_P (operand))
5163 return cxx_sizeof_or_alignof_type (operand, op, true);
5164 else
5165 return cxx_sizeof_or_alignof_expr (operand, op);
5166 }
5167
5168 case RID_NEW:
5169 return cp_parser_new_expression (parser);
5170
5171 case RID_DELETE:
5172 return cp_parser_delete_expression (parser);
5173
5174 case RID_EXTENSION:
5175 {
5176 /* The saved value of the PEDANTIC flag. */
5177 int saved_pedantic;
5178 tree expr;
5179
5180 /* Save away the PEDANTIC flag. */
5181 cp_parser_extension_opt (parser, &saved_pedantic);
5182 /* Parse the cast-expression. */
5183 expr = cp_parser_simple_cast_expression (parser);
5184 /* Restore the PEDANTIC flag. */
5185 pedantic = saved_pedantic;
5186
5187 return expr;
5188 }
5189
5190 case RID_REALPART:
5191 case RID_IMAGPART:
5192 {
5193 tree expression;
5194
5195 /* Consume the `__real__' or `__imag__' token. */
5196 cp_lexer_consume_token (parser->lexer);
5197 /* Parse the cast-expression. */
5198 expression = cp_parser_simple_cast_expression (parser);
5199 /* Create the complete representation. */
5200 return build_x_unary_op ((keyword == RID_REALPART
5201 ? REALPART_EXPR : IMAGPART_EXPR),
5202 expression);
5203 }
5204 break;
5205
5206 default:
5207 break;
5208 }
5209 }
5210
5211 /* Look for the `:: new' and `:: delete', which also signal the
5212 beginning of a new-expression, or delete-expression,
5213 respectively. If the next token is `::', then it might be one of
5214 these. */
5215 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5216 {
5217 enum rid keyword;
5218
5219 /* See if the token after the `::' is one of the keywords in
5220 which we're interested. */
5221 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5222 /* If it's `new', we have a new-expression. */
5223 if (keyword == RID_NEW)
5224 return cp_parser_new_expression (parser);
5225 /* Similarly, for `delete'. */
5226 else if (keyword == RID_DELETE)
5227 return cp_parser_delete_expression (parser);
5228 }
5229
5230 /* Look for a unary operator. */
5231 unary_operator = cp_parser_unary_operator (token);
5232 /* The `++' and `--' operators can be handled similarly, even though
5233 they are not technically unary-operators in the grammar. */
5234 if (unary_operator == ERROR_MARK)
5235 {
5236 if (token->type == CPP_PLUS_PLUS)
5237 unary_operator = PREINCREMENT_EXPR;
5238 else if (token->type == CPP_MINUS_MINUS)
5239 unary_operator = PREDECREMENT_EXPR;
5240 /* Handle the GNU address-of-label extension. */
5241 else if (cp_parser_allow_gnu_extensions_p (parser)
5242 && token->type == CPP_AND_AND)
5243 {
5244 tree identifier;
5245
5246 /* Consume the '&&' token. */
5247 cp_lexer_consume_token (parser->lexer);
5248 /* Look for the identifier. */
5249 identifier = cp_parser_identifier (parser);
5250 /* Create an expression representing the address. */
5251 return finish_label_address_expr (identifier);
5252 }
5253 }
5254 if (unary_operator != ERROR_MARK)
5255 {
5256 tree cast_expression;
5257 tree expression = error_mark_node;
5258 const char *non_constant_p = NULL;
5259
5260 /* Consume the operator token. */
5261 token = cp_lexer_consume_token (parser->lexer);
5262 /* Parse the cast-expression. */
5263 cast_expression
5264 = cp_parser_cast_expression (parser,
5265 unary_operator == ADDR_EXPR,
5266 /*cast_p=*/false);
5267 /* Now, build an appropriate representation. */
5268 switch (unary_operator)
5269 {
5270 case INDIRECT_REF:
5271 non_constant_p = "`*'";
5272 expression = build_x_indirect_ref (cast_expression, "unary *");
5273 break;
5274
5275 case ADDR_EXPR:
5276 non_constant_p = "`&'";
5277 /* Fall through. */
5278 case BIT_NOT_EXPR:
5279 expression = build_x_unary_op (unary_operator, cast_expression);
5280 break;
5281
5282 case PREINCREMENT_EXPR:
5283 case PREDECREMENT_EXPR:
5284 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5285 ? "`++'" : "`--'");
5286 /* Fall through. */
5287 case UNARY_PLUS_EXPR:
5288 case NEGATE_EXPR:
5289 case TRUTH_NOT_EXPR:
5290 expression = finish_unary_op_expr (unary_operator, cast_expression);
5291 break;
5292
5293 default:
5294 gcc_unreachable ();
5295 }
5296
5297 if (non_constant_p
5298 && cp_parser_non_integral_constant_expression (parser,
5299 non_constant_p))
5300 expression = error_mark_node;
5301
5302 return expression;
5303 }
5304
5305 return cp_parser_postfix_expression (parser, address_p, cast_p);
5306 }
5307
5308 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5309 unary-operator, the corresponding tree code is returned. */
5310
5311 static enum tree_code
5312 cp_parser_unary_operator (cp_token* token)
5313 {
5314 switch (token->type)
5315 {
5316 case CPP_MULT:
5317 return INDIRECT_REF;
5318
5319 case CPP_AND:
5320 return ADDR_EXPR;
5321
5322 case CPP_PLUS:
5323 return UNARY_PLUS_EXPR;
5324
5325 case CPP_MINUS:
5326 return NEGATE_EXPR;
5327
5328 case CPP_NOT:
5329 return TRUTH_NOT_EXPR;
5330
5331 case CPP_COMPL:
5332 return BIT_NOT_EXPR;
5333
5334 default:
5335 return ERROR_MARK;
5336 }
5337 }
5338
5339 /* Parse a new-expression.
5340
5341 new-expression:
5342 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5343 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5344
5345 Returns a representation of the expression. */
5346
5347 static tree
5348 cp_parser_new_expression (cp_parser* parser)
5349 {
5350 bool global_scope_p;
5351 tree placement;
5352 tree type;
5353 tree initializer;
5354 tree nelts;
5355
5356 /* Look for the optional `::' operator. */
5357 global_scope_p
5358 = (cp_parser_global_scope_opt (parser,
5359 /*current_scope_valid_p=*/false)
5360 != NULL_TREE);
5361 /* Look for the `new' operator. */
5362 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5363 /* There's no easy way to tell a new-placement from the
5364 `( type-id )' construct. */
5365 cp_parser_parse_tentatively (parser);
5366 /* Look for a new-placement. */
5367 placement = cp_parser_new_placement (parser);
5368 /* If that didn't work out, there's no new-placement. */
5369 if (!cp_parser_parse_definitely (parser))
5370 placement = NULL_TREE;
5371
5372 /* If the next token is a `(', then we have a parenthesized
5373 type-id. */
5374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5375 {
5376 /* Consume the `('. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* Parse the type-id. */
5379 type = cp_parser_type_id (parser);
5380 /* Look for the closing `)'. */
5381 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5382 /* There should not be a direct-new-declarator in this production,
5383 but GCC used to allowed this, so we check and emit a sensible error
5384 message for this case. */
5385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5386 {
5387 error ("array bound forbidden after parenthesized type-id");
5388 inform ("try removing the parentheses around the type-id");
5389 cp_parser_direct_new_declarator (parser);
5390 }
5391 nelts = NULL_TREE;
5392 }
5393 /* Otherwise, there must be a new-type-id. */
5394 else
5395 type = cp_parser_new_type_id (parser, &nelts);
5396
5397 /* If the next token is a `(', then we have a new-initializer. */
5398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5399 initializer = cp_parser_new_initializer (parser);
5400 else
5401 initializer = NULL_TREE;
5402
5403 /* A new-expression may not appear in an integral constant
5404 expression. */
5405 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5406 return error_mark_node;
5407
5408 /* Create a representation of the new-expression. */
5409 return build_new (placement, type, nelts, initializer, global_scope_p);
5410 }
5411
5412 /* Parse a new-placement.
5413
5414 new-placement:
5415 ( expression-list )
5416
5417 Returns the same representation as for an expression-list. */
5418
5419 static tree
5420 cp_parser_new_placement (cp_parser* parser)
5421 {
5422 tree expression_list;
5423
5424 /* Parse the expression-list. */
5425 expression_list = (cp_parser_parenthesized_expression_list
5426 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5427 /*non_constant_p=*/NULL));
5428
5429 return expression_list;
5430 }
5431
5432 /* Parse a new-type-id.
5433
5434 new-type-id:
5435 type-specifier-seq new-declarator [opt]
5436
5437 Returns the TYPE allocated. If the new-type-id indicates an array
5438 type, *NELTS is set to the number of elements in the last array
5439 bound; the TYPE will not include the last array bound. */
5440
5441 static tree
5442 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5443 {
5444 cp_decl_specifier_seq type_specifier_seq;
5445 cp_declarator *new_declarator;
5446 cp_declarator *declarator;
5447 cp_declarator *outer_declarator;
5448 const char *saved_message;
5449 tree type;
5450
5451 /* The type-specifier sequence must not contain type definitions.
5452 (It cannot contain declarations of new types either, but if they
5453 are not definitions we will catch that because they are not
5454 complete.) */
5455 saved_message = parser->type_definition_forbidden_message;
5456 parser->type_definition_forbidden_message
5457 = "types may not be defined in a new-type-id";
5458 /* Parse the type-specifier-seq. */
5459 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5460 &type_specifier_seq);
5461 /* Restore the old message. */
5462 parser->type_definition_forbidden_message = saved_message;
5463 /* Parse the new-declarator. */
5464 new_declarator = cp_parser_new_declarator_opt (parser);
5465
5466 /* Determine the number of elements in the last array dimension, if
5467 any. */
5468 *nelts = NULL_TREE;
5469 /* Skip down to the last array dimension. */
5470 declarator = new_declarator;
5471 outer_declarator = NULL;
5472 while (declarator && (declarator->kind == cdk_pointer
5473 || declarator->kind == cdk_ptrmem))
5474 {
5475 outer_declarator = declarator;
5476 declarator = declarator->declarator;
5477 }
5478 while (declarator
5479 && declarator->kind == cdk_array
5480 && declarator->declarator
5481 && declarator->declarator->kind == cdk_array)
5482 {
5483 outer_declarator = declarator;
5484 declarator = declarator->declarator;
5485 }
5486
5487 if (declarator && declarator->kind == cdk_array)
5488 {
5489 *nelts = declarator->u.array.bounds;
5490 if (*nelts == error_mark_node)
5491 *nelts = integer_one_node;
5492
5493 if (outer_declarator)
5494 outer_declarator->declarator = declarator->declarator;
5495 else
5496 new_declarator = NULL;
5497 }
5498
5499 type = groktypename (&type_specifier_seq, new_declarator);
5500 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5501 {
5502 *nelts = array_type_nelts_top (type);
5503 type = TREE_TYPE (type);
5504 }
5505 return type;
5506 }
5507
5508 /* Parse an (optional) new-declarator.
5509
5510 new-declarator:
5511 ptr-operator new-declarator [opt]
5512 direct-new-declarator
5513
5514 Returns the declarator. */
5515
5516 static cp_declarator *
5517 cp_parser_new_declarator_opt (cp_parser* parser)
5518 {
5519 enum tree_code code;
5520 tree type;
5521 cp_cv_quals cv_quals;
5522
5523 /* We don't know if there's a ptr-operator next, or not. */
5524 cp_parser_parse_tentatively (parser);
5525 /* Look for a ptr-operator. */
5526 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5527 /* If that worked, look for more new-declarators. */
5528 if (cp_parser_parse_definitely (parser))
5529 {
5530 cp_declarator *declarator;
5531
5532 /* Parse another optional declarator. */
5533 declarator = cp_parser_new_declarator_opt (parser);
5534
5535 /* Create the representation of the declarator. */
5536 if (type)
5537 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5538 else if (code == INDIRECT_REF)
5539 declarator = make_pointer_declarator (cv_quals, declarator);
5540 else
5541 declarator = make_reference_declarator (cv_quals, declarator);
5542
5543 return declarator;
5544 }
5545
5546 /* If the next token is a `[', there is a direct-new-declarator. */
5547 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5548 return cp_parser_direct_new_declarator (parser);
5549
5550 return NULL;
5551 }
5552
5553 /* Parse a direct-new-declarator.
5554
5555 direct-new-declarator:
5556 [ expression ]
5557 direct-new-declarator [constant-expression]
5558
5559 */
5560
5561 static cp_declarator *
5562 cp_parser_direct_new_declarator (cp_parser* parser)
5563 {
5564 cp_declarator *declarator = NULL;
5565
5566 while (true)
5567 {
5568 tree expression;
5569
5570 /* Look for the opening `['. */
5571 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5572 /* The first expression is not required to be constant. */
5573 if (!declarator)
5574 {
5575 expression = cp_parser_expression (parser, /*cast_p=*/false);
5576 /* The standard requires that the expression have integral
5577 type. DR 74 adds enumeration types. We believe that the
5578 real intent is that these expressions be handled like the
5579 expression in a `switch' condition, which also allows
5580 classes with a single conversion to integral or
5581 enumeration type. */
5582 if (!processing_template_decl)
5583 {
5584 expression
5585 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5586 expression,
5587 /*complain=*/true);
5588 if (!expression)
5589 {
5590 error ("expression in new-declarator must have integral "
5591 "or enumeration type");
5592 expression = error_mark_node;
5593 }
5594 }
5595 }
5596 /* But all the other expressions must be. */
5597 else
5598 expression
5599 = cp_parser_constant_expression (parser,
5600 /*allow_non_constant=*/false,
5601 NULL);
5602 /* Look for the closing `]'. */
5603 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5604
5605 /* Add this bound to the declarator. */
5606 declarator = make_array_declarator (declarator, expression);
5607
5608 /* If the next token is not a `[', then there are no more
5609 bounds. */
5610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5611 break;
5612 }
5613
5614 return declarator;
5615 }
5616
5617 /* Parse a new-initializer.
5618
5619 new-initializer:
5620 ( expression-list [opt] )
5621
5622 Returns a representation of the expression-list. If there is no
5623 expression-list, VOID_ZERO_NODE is returned. */
5624
5625 static tree
5626 cp_parser_new_initializer (cp_parser* parser)
5627 {
5628 tree expression_list;
5629
5630 expression_list = (cp_parser_parenthesized_expression_list
5631 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5632 /*non_constant_p=*/NULL));
5633 if (!expression_list)
5634 expression_list = void_zero_node;
5635
5636 return expression_list;
5637 }
5638
5639 /* Parse a delete-expression.
5640
5641 delete-expression:
5642 :: [opt] delete cast-expression
5643 :: [opt] delete [ ] cast-expression
5644
5645 Returns a representation of the expression. */
5646
5647 static tree
5648 cp_parser_delete_expression (cp_parser* parser)
5649 {
5650 bool global_scope_p;
5651 bool array_p;
5652 tree expression;
5653
5654 /* Look for the optional `::' operator. */
5655 global_scope_p
5656 = (cp_parser_global_scope_opt (parser,
5657 /*current_scope_valid_p=*/false)
5658 != NULL_TREE);
5659 /* Look for the `delete' keyword. */
5660 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5661 /* See if the array syntax is in use. */
5662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5663 {
5664 /* Consume the `[' token. */
5665 cp_lexer_consume_token (parser->lexer);
5666 /* Look for the `]' token. */
5667 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5668 /* Remember that this is the `[]' construct. */
5669 array_p = true;
5670 }
5671 else
5672 array_p = false;
5673
5674 /* Parse the cast-expression. */
5675 expression = cp_parser_simple_cast_expression (parser);
5676
5677 /* A delete-expression may not appear in an integral constant
5678 expression. */
5679 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5680 return error_mark_node;
5681
5682 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5683 }
5684
5685 /* Parse a cast-expression.
5686
5687 cast-expression:
5688 unary-expression
5689 ( type-id ) cast-expression
5690
5691 ADDRESS_P is true iff the unary-expression is appearing as the
5692 operand of the `&' operator. CAST_P is true if this expression is
5693 the target of a cast.
5694
5695 Returns a representation of the expression. */
5696
5697 static tree
5698 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5699 {
5700 /* If it's a `(', then we might be looking at a cast. */
5701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5702 {
5703 tree type = NULL_TREE;
5704 tree expr = NULL_TREE;
5705 bool compound_literal_p;
5706 const char *saved_message;
5707
5708 /* There's no way to know yet whether or not this is a cast.
5709 For example, `(int (3))' is a unary-expression, while `(int)
5710 3' is a cast. So, we resort to parsing tentatively. */
5711 cp_parser_parse_tentatively (parser);
5712 /* Types may not be defined in a cast. */
5713 saved_message = parser->type_definition_forbidden_message;
5714 parser->type_definition_forbidden_message
5715 = "types may not be defined in casts";
5716 /* Consume the `('. */
5717 cp_lexer_consume_token (parser->lexer);
5718 /* A very tricky bit is that `(struct S) { 3 }' is a
5719 compound-literal (which we permit in C++ as an extension).
5720 But, that construct is not a cast-expression -- it is a
5721 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5722 is legal; if the compound-literal were a cast-expression,
5723 you'd need an extra set of parentheses.) But, if we parse
5724 the type-id, and it happens to be a class-specifier, then we
5725 will commit to the parse at that point, because we cannot
5726 undo the action that is done when creating a new class. So,
5727 then we cannot back up and do a postfix-expression.
5728
5729 Therefore, we scan ahead to the closing `)', and check to see
5730 if the token after the `)' is a `{'. If so, we are not
5731 looking at a cast-expression.
5732
5733 Save tokens so that we can put them back. */
5734 cp_lexer_save_tokens (parser->lexer);
5735 /* Skip tokens until the next token is a closing parenthesis.
5736 If we find the closing `)', and the next token is a `{', then
5737 we are looking at a compound-literal. */
5738 compound_literal_p
5739 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5740 /*consume_paren=*/true)
5741 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5742 /* Roll back the tokens we skipped. */
5743 cp_lexer_rollback_tokens (parser->lexer);
5744 /* If we were looking at a compound-literal, simulate an error
5745 so that the call to cp_parser_parse_definitely below will
5746 fail. */
5747 if (compound_literal_p)
5748 cp_parser_simulate_error (parser);
5749 else
5750 {
5751 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5752 parser->in_type_id_in_expr_p = true;
5753 /* Look for the type-id. */
5754 type = cp_parser_type_id (parser);
5755 /* Look for the closing `)'. */
5756 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5757 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5758 }
5759
5760 /* Restore the saved message. */
5761 parser->type_definition_forbidden_message = saved_message;
5762
5763 /* If ok so far, parse the dependent expression. We cannot be
5764 sure it is a cast. Consider `(T ())'. It is a parenthesized
5765 ctor of T, but looks like a cast to function returning T
5766 without a dependent expression. */
5767 if (!cp_parser_error_occurred (parser))
5768 expr = cp_parser_cast_expression (parser,
5769 /*address_p=*/false,
5770 /*cast_p=*/true);
5771
5772 if (cp_parser_parse_definitely (parser))
5773 {
5774 /* Warn about old-style casts, if so requested. */
5775 if (warn_old_style_cast
5776 && !in_system_header
5777 && !VOID_TYPE_P (type)
5778 && current_lang_name != lang_name_c)
5779 warning (OPT_Wold_style_cast, "use of old-style cast");
5780
5781 /* Only type conversions to integral or enumeration types
5782 can be used in constant-expressions. */
5783 if (!cast_valid_in_integral_constant_expression_p (type)
5784 && (cp_parser_non_integral_constant_expression
5785 (parser,
5786 "a cast to a type other than an integral or "
5787 "enumeration type")))
5788 return error_mark_node;
5789
5790 /* Perform the cast. */
5791 expr = build_c_cast (type, expr);
5792 return expr;
5793 }
5794 }
5795
5796 /* If we get here, then it's not a cast, so it must be a
5797 unary-expression. */
5798 return cp_parser_unary_expression (parser, address_p, cast_p);
5799 }
5800
5801 /* Parse a binary expression of the general form:
5802
5803 pm-expression:
5804 cast-expression
5805 pm-expression .* cast-expression
5806 pm-expression ->* cast-expression
5807
5808 multiplicative-expression:
5809 pm-expression
5810 multiplicative-expression * pm-expression
5811 multiplicative-expression / pm-expression
5812 multiplicative-expression % pm-expression
5813
5814 additive-expression:
5815 multiplicative-expression
5816 additive-expression + multiplicative-expression
5817 additive-expression - multiplicative-expression
5818
5819 shift-expression:
5820 additive-expression
5821 shift-expression << additive-expression
5822 shift-expression >> additive-expression
5823
5824 relational-expression:
5825 shift-expression
5826 relational-expression < shift-expression
5827 relational-expression > shift-expression
5828 relational-expression <= shift-expression
5829 relational-expression >= shift-expression
5830
5831 GNU Extension:
5832
5833 relational-expression:
5834 relational-expression <? shift-expression
5835 relational-expression >? shift-expression
5836
5837 equality-expression:
5838 relational-expression
5839 equality-expression == relational-expression
5840 equality-expression != relational-expression
5841
5842 and-expression:
5843 equality-expression
5844 and-expression & equality-expression
5845
5846 exclusive-or-expression:
5847 and-expression
5848 exclusive-or-expression ^ and-expression
5849
5850 inclusive-or-expression:
5851 exclusive-or-expression
5852 inclusive-or-expression | exclusive-or-expression
5853
5854 logical-and-expression:
5855 inclusive-or-expression
5856 logical-and-expression && inclusive-or-expression
5857
5858 logical-or-expression:
5859 logical-and-expression
5860 logical-or-expression || logical-and-expression
5861
5862 All these are implemented with a single function like:
5863
5864 binary-expression:
5865 simple-cast-expression
5866 binary-expression <token> binary-expression
5867
5868 CAST_P is true if this expression is the target of a cast.
5869
5870 The binops_by_token map is used to get the tree codes for each <token> type.
5871 binary-expressions are associated according to a precedence table. */
5872
5873 #define TOKEN_PRECEDENCE(token) \
5874 (((token->type == CPP_GREATER \
5875 || (flag_cpp0x && token->type == CPP_RSHIFT)) \
5876 && !parser->greater_than_is_operator_p) \
5877 ? PREC_NOT_OPERATOR \
5878 : binops_by_token[token->type].prec)
5879
5880 static tree
5881 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5882 {
5883 cp_parser_expression_stack stack;
5884 cp_parser_expression_stack_entry *sp = &stack[0];
5885 tree lhs, rhs;
5886 cp_token *token;
5887 enum tree_code tree_type, lhs_type, rhs_type;
5888 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5889 bool overloaded_p;
5890
5891 /* Parse the first expression. */
5892 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5893 lhs_type = ERROR_MARK;
5894
5895 for (;;)
5896 {
5897 /* Get an operator token. */
5898 token = cp_lexer_peek_token (parser->lexer);
5899
5900 if (warn_cxx0x_compat
5901 && token->type == CPP_RSHIFT
5902 && !parser->greater_than_is_operator_p)
5903 {
5904 warning (OPT_Wc__0x_compat,
5905 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5906 &token->location);
5907 warning (OPT_Wc__0x_compat,
5908 "suggest parentheses around %<>>%> expression");
5909 }
5910
5911 new_prec = TOKEN_PRECEDENCE (token);
5912
5913 /* Popping an entry off the stack means we completed a subexpression:
5914 - either we found a token which is not an operator (`>' where it is not
5915 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5916 will happen repeatedly;
5917 - or, we found an operator which has lower priority. This is the case
5918 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5919 parsing `3 * 4'. */
5920 if (new_prec <= prec)
5921 {
5922 if (sp == stack)
5923 break;
5924 else
5925 goto pop;
5926 }
5927
5928 get_rhs:
5929 tree_type = binops_by_token[token->type].tree_type;
5930
5931 /* We used the operator token. */
5932 cp_lexer_consume_token (parser->lexer);
5933
5934 /* Extract another operand. It may be the RHS of this expression
5935 or the LHS of a new, higher priority expression. */
5936 rhs = cp_parser_simple_cast_expression (parser);
5937 rhs_type = ERROR_MARK;
5938
5939 /* Get another operator token. Look up its precedence to avoid
5940 building a useless (immediately popped) stack entry for common
5941 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5942 token = cp_lexer_peek_token (parser->lexer);
5943 lookahead_prec = TOKEN_PRECEDENCE (token);
5944 if (lookahead_prec > new_prec)
5945 {
5946 /* ... and prepare to parse the RHS of the new, higher priority
5947 expression. Since precedence levels on the stack are
5948 monotonically increasing, we do not have to care about
5949 stack overflows. */
5950 sp->prec = prec;
5951 sp->tree_type = tree_type;
5952 sp->lhs = lhs;
5953 sp->lhs_type = lhs_type;
5954 sp++;
5955 lhs = rhs;
5956 lhs_type = rhs_type;
5957 prec = new_prec;
5958 new_prec = lookahead_prec;
5959 goto get_rhs;
5960
5961 pop:
5962 /* If the stack is not empty, we have parsed into LHS the right side
5963 (`4' in the example above) of an expression we had suspended.
5964 We can use the information on the stack to recover the LHS (`3')
5965 from the stack together with the tree code (`MULT_EXPR'), and
5966 the precedence of the higher level subexpression
5967 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5968 which will be used to actually build the additive expression. */
5969 --sp;
5970 prec = sp->prec;
5971 tree_type = sp->tree_type;
5972 rhs = lhs;
5973 rhs_type = lhs_type;
5974 lhs = sp->lhs;
5975 lhs_type = sp->lhs_type;
5976 }
5977
5978 overloaded_p = false;
5979 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5980 &overloaded_p);
5981 lhs_type = tree_type;
5982
5983 /* If the binary operator required the use of an overloaded operator,
5984 then this expression cannot be an integral constant-expression.
5985 An overloaded operator can be used even if both operands are
5986 otherwise permissible in an integral constant-expression if at
5987 least one of the operands is of enumeration type. */
5988
5989 if (overloaded_p
5990 && (cp_parser_non_integral_constant_expression
5991 (parser, "calls to overloaded operators")))
5992 return error_mark_node;
5993 }
5994
5995 return lhs;
5996 }
5997
5998
5999 /* Parse the `? expression : assignment-expression' part of a
6000 conditional-expression. The LOGICAL_OR_EXPR is the
6001 logical-or-expression that started the conditional-expression.
6002 Returns a representation of the entire conditional-expression.
6003
6004 This routine is used by cp_parser_assignment_expression.
6005
6006 ? expression : assignment-expression
6007
6008 GNU Extensions:
6009
6010 ? : assignment-expression */
6011
6012 static tree
6013 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6014 {
6015 tree expr;
6016 tree assignment_expr;
6017
6018 /* Consume the `?' token. */
6019 cp_lexer_consume_token (parser->lexer);
6020 if (cp_parser_allow_gnu_extensions_p (parser)
6021 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6022 /* Implicit true clause. */
6023 expr = NULL_TREE;
6024 else
6025 /* Parse the expression. */
6026 expr = cp_parser_expression (parser, /*cast_p=*/false);
6027
6028 /* The next token should be a `:'. */
6029 cp_parser_require (parser, CPP_COLON, "`:'");
6030 /* Parse the assignment-expression. */
6031 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6032
6033 /* Build the conditional-expression. */
6034 return build_x_conditional_expr (logical_or_expr,
6035 expr,
6036 assignment_expr);
6037 }
6038
6039 /* Parse an assignment-expression.
6040
6041 assignment-expression:
6042 conditional-expression
6043 logical-or-expression assignment-operator assignment_expression
6044 throw-expression
6045
6046 CAST_P is true if this expression is the target of a cast.
6047
6048 Returns a representation for the expression. */
6049
6050 static tree
6051 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6052 {
6053 tree expr;
6054
6055 /* If the next token is the `throw' keyword, then we're looking at
6056 a throw-expression. */
6057 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6058 expr = cp_parser_throw_expression (parser);
6059 /* Otherwise, it must be that we are looking at a
6060 logical-or-expression. */
6061 else
6062 {
6063 /* Parse the binary expressions (logical-or-expression). */
6064 expr = cp_parser_binary_expression (parser, cast_p);
6065 /* If the next token is a `?' then we're actually looking at a
6066 conditional-expression. */
6067 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6068 return cp_parser_question_colon_clause (parser, expr);
6069 else
6070 {
6071 enum tree_code assignment_operator;
6072
6073 /* If it's an assignment-operator, we're using the second
6074 production. */
6075 assignment_operator
6076 = cp_parser_assignment_operator_opt (parser);
6077 if (assignment_operator != ERROR_MARK)
6078 {
6079 tree rhs;
6080
6081 /* Parse the right-hand side of the assignment. */
6082 rhs = cp_parser_assignment_expression (parser, cast_p);
6083 /* An assignment may not appear in a
6084 constant-expression. */
6085 if (cp_parser_non_integral_constant_expression (parser,
6086 "an assignment"))
6087 return error_mark_node;
6088 /* Build the assignment expression. */
6089 expr = build_x_modify_expr (expr,
6090 assignment_operator,
6091 rhs);
6092 }
6093 }
6094 }
6095
6096 return expr;
6097 }
6098
6099 /* Parse an (optional) assignment-operator.
6100
6101 assignment-operator: one of
6102 = *= /= %= += -= >>= <<= &= ^= |=
6103
6104 GNU Extension:
6105
6106 assignment-operator: one of
6107 <?= >?=
6108
6109 If the next token is an assignment operator, the corresponding tree
6110 code is returned, and the token is consumed. For example, for
6111 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6112 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6113 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6114 operator, ERROR_MARK is returned. */
6115
6116 static enum tree_code
6117 cp_parser_assignment_operator_opt (cp_parser* parser)
6118 {
6119 enum tree_code op;
6120 cp_token *token;
6121
6122 /* Peek at the next toen. */
6123 token = cp_lexer_peek_token (parser->lexer);
6124
6125 switch (token->type)
6126 {
6127 case CPP_EQ:
6128 op = NOP_EXPR;
6129 break;
6130
6131 case CPP_MULT_EQ:
6132 op = MULT_EXPR;
6133 break;
6134
6135 case CPP_DIV_EQ:
6136 op = TRUNC_DIV_EXPR;
6137 break;
6138
6139 case CPP_MOD_EQ:
6140 op = TRUNC_MOD_EXPR;
6141 break;
6142
6143 case CPP_PLUS_EQ:
6144 op = PLUS_EXPR;
6145 break;
6146
6147 case CPP_MINUS_EQ:
6148 op = MINUS_EXPR;
6149 break;
6150
6151 case CPP_RSHIFT_EQ:
6152 op = RSHIFT_EXPR;
6153 break;
6154
6155 case CPP_LSHIFT_EQ:
6156 op = LSHIFT_EXPR;
6157 break;
6158
6159 case CPP_AND_EQ:
6160 op = BIT_AND_EXPR;
6161 break;
6162
6163 case CPP_XOR_EQ:
6164 op = BIT_XOR_EXPR;
6165 break;
6166
6167 case CPP_OR_EQ:
6168 op = BIT_IOR_EXPR;
6169 break;
6170
6171 default:
6172 /* Nothing else is an assignment operator. */
6173 op = ERROR_MARK;
6174 }
6175
6176 /* If it was an assignment operator, consume it. */
6177 if (op != ERROR_MARK)
6178 cp_lexer_consume_token (parser->lexer);
6179
6180 return op;
6181 }
6182
6183 /* Parse an expression.
6184
6185 expression:
6186 assignment-expression
6187 expression , assignment-expression
6188
6189 CAST_P is true if this expression is the target of a cast.
6190
6191 Returns a representation of the expression. */
6192
6193 static tree
6194 cp_parser_expression (cp_parser* parser, bool cast_p)
6195 {
6196 tree expression = NULL_TREE;
6197
6198 while (true)
6199 {
6200 tree assignment_expression;
6201
6202 /* Parse the next assignment-expression. */
6203 assignment_expression
6204 = cp_parser_assignment_expression (parser, cast_p);
6205 /* If this is the first assignment-expression, we can just
6206 save it away. */
6207 if (!expression)
6208 expression = assignment_expression;
6209 else
6210 expression = build_x_compound_expr (expression,
6211 assignment_expression);
6212 /* If the next token is not a comma, then we are done with the
6213 expression. */
6214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6215 break;
6216 /* Consume the `,'. */
6217 cp_lexer_consume_token (parser->lexer);
6218 /* A comma operator cannot appear in a constant-expression. */
6219 if (cp_parser_non_integral_constant_expression (parser,
6220 "a comma operator"))
6221 expression = error_mark_node;
6222 }
6223
6224 return expression;
6225 }
6226
6227 /* Parse a constant-expression.
6228
6229 constant-expression:
6230 conditional-expression
6231
6232 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6233 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6234 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6235 is false, NON_CONSTANT_P should be NULL. */
6236
6237 static tree
6238 cp_parser_constant_expression (cp_parser* parser,
6239 bool allow_non_constant_p,
6240 bool *non_constant_p)
6241 {
6242 bool saved_integral_constant_expression_p;
6243 bool saved_allow_non_integral_constant_expression_p;
6244 bool saved_non_integral_constant_expression_p;
6245 tree expression;
6246
6247 /* It might seem that we could simply parse the
6248 conditional-expression, and then check to see if it were
6249 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6250 one that the compiler can figure out is constant, possibly after
6251 doing some simplifications or optimizations. The standard has a
6252 precise definition of constant-expression, and we must honor
6253 that, even though it is somewhat more restrictive.
6254
6255 For example:
6256
6257 int i[(2, 3)];
6258
6259 is not a legal declaration, because `(2, 3)' is not a
6260 constant-expression. The `,' operator is forbidden in a
6261 constant-expression. However, GCC's constant-folding machinery
6262 will fold this operation to an INTEGER_CST for `3'. */
6263
6264 /* Save the old settings. */
6265 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6266 saved_allow_non_integral_constant_expression_p
6267 = parser->allow_non_integral_constant_expression_p;
6268 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6269 /* We are now parsing a constant-expression. */
6270 parser->integral_constant_expression_p = true;
6271 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6272 parser->non_integral_constant_expression_p = false;
6273 /* Although the grammar says "conditional-expression", we parse an
6274 "assignment-expression", which also permits "throw-expression"
6275 and the use of assignment operators. In the case that
6276 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6277 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6278 actually essential that we look for an assignment-expression.
6279 For example, cp_parser_initializer_clauses uses this function to
6280 determine whether a particular assignment-expression is in fact
6281 constant. */
6282 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6283 /* Restore the old settings. */
6284 parser->integral_constant_expression_p
6285 = saved_integral_constant_expression_p;
6286 parser->allow_non_integral_constant_expression_p
6287 = saved_allow_non_integral_constant_expression_p;
6288 if (allow_non_constant_p)
6289 *non_constant_p = parser->non_integral_constant_expression_p;
6290 else if (parser->non_integral_constant_expression_p)
6291 expression = error_mark_node;
6292 parser->non_integral_constant_expression_p
6293 = saved_non_integral_constant_expression_p;
6294
6295 return expression;
6296 }
6297
6298 /* Parse __builtin_offsetof.
6299
6300 offsetof-expression:
6301 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6302
6303 offsetof-member-designator:
6304 id-expression
6305 | offsetof-member-designator "." id-expression
6306 | offsetof-member-designator "[" expression "]" */
6307
6308 static tree
6309 cp_parser_builtin_offsetof (cp_parser *parser)
6310 {
6311 int save_ice_p, save_non_ice_p;
6312 tree type, expr;
6313 cp_id_kind dummy;
6314
6315 /* We're about to accept non-integral-constant things, but will
6316 definitely yield an integral constant expression. Save and
6317 restore these values around our local parsing. */
6318 save_ice_p = parser->integral_constant_expression_p;
6319 save_non_ice_p = parser->non_integral_constant_expression_p;
6320
6321 /* Consume the "__builtin_offsetof" token. */
6322 cp_lexer_consume_token (parser->lexer);
6323 /* Consume the opening `('. */
6324 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6325 /* Parse the type-id. */
6326 type = cp_parser_type_id (parser);
6327 /* Look for the `,'. */
6328 cp_parser_require (parser, CPP_COMMA, "`,'");
6329
6330 /* Build the (type *)null that begins the traditional offsetof macro. */
6331 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6332
6333 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6334 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6335 true, &dummy);
6336 while (true)
6337 {
6338 cp_token *token = cp_lexer_peek_token (parser->lexer);
6339 switch (token->type)
6340 {
6341 case CPP_OPEN_SQUARE:
6342 /* offsetof-member-designator "[" expression "]" */
6343 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6344 break;
6345
6346 case CPP_DOT:
6347 /* offsetof-member-designator "." identifier */
6348 cp_lexer_consume_token (parser->lexer);
6349 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6350 true, &dummy);
6351 break;
6352
6353 case CPP_CLOSE_PAREN:
6354 /* Consume the ")" token. */
6355 cp_lexer_consume_token (parser->lexer);
6356 goto success;
6357
6358 default:
6359 /* Error. We know the following require will fail, but
6360 that gives the proper error message. */
6361 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6362 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6363 expr = error_mark_node;
6364 goto failure;
6365 }
6366 }
6367
6368 success:
6369 /* If we're processing a template, we can't finish the semantics yet.
6370 Otherwise we can fold the entire expression now. */
6371 if (processing_template_decl)
6372 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6373 else
6374 expr = finish_offsetof (expr);
6375
6376 failure:
6377 parser->integral_constant_expression_p = save_ice_p;
6378 parser->non_integral_constant_expression_p = save_non_ice_p;
6379
6380 return expr;
6381 }
6382
6383 /* Parse a trait expression. */
6384
6385 static tree
6386 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6387 {
6388 cp_trait_kind kind;
6389 tree type1, type2 = NULL_TREE;
6390 bool binary = false;
6391 cp_decl_specifier_seq decl_specs;
6392
6393 switch (keyword)
6394 {
6395 case RID_HAS_NOTHROW_ASSIGN:
6396 kind = CPTK_HAS_NOTHROW_ASSIGN;
6397 break;
6398 case RID_HAS_NOTHROW_CONSTRUCTOR:
6399 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6400 break;
6401 case RID_HAS_NOTHROW_COPY:
6402 kind = CPTK_HAS_NOTHROW_COPY;
6403 break;
6404 case RID_HAS_TRIVIAL_ASSIGN:
6405 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6406 break;
6407 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6408 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6409 break;
6410 case RID_HAS_TRIVIAL_COPY:
6411 kind = CPTK_HAS_TRIVIAL_COPY;
6412 break;
6413 case RID_HAS_TRIVIAL_DESTRUCTOR:
6414 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6415 break;
6416 case RID_HAS_VIRTUAL_DESTRUCTOR:
6417 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6418 break;
6419 case RID_IS_ABSTRACT:
6420 kind = CPTK_IS_ABSTRACT;
6421 break;
6422 case RID_IS_BASE_OF:
6423 kind = CPTK_IS_BASE_OF;
6424 binary = true;
6425 break;
6426 case RID_IS_CLASS:
6427 kind = CPTK_IS_CLASS;
6428 break;
6429 case RID_IS_CONVERTIBLE_TO:
6430 kind = CPTK_IS_CONVERTIBLE_TO;
6431 binary = true;
6432 break;
6433 case RID_IS_EMPTY:
6434 kind = CPTK_IS_EMPTY;
6435 break;
6436 case RID_IS_ENUM:
6437 kind = CPTK_IS_ENUM;
6438 break;
6439 case RID_IS_POD:
6440 kind = CPTK_IS_POD;
6441 break;
6442 case RID_IS_POLYMORPHIC:
6443 kind = CPTK_IS_POLYMORPHIC;
6444 break;
6445 case RID_IS_UNION:
6446 kind = CPTK_IS_UNION;
6447 break;
6448 default:
6449 gcc_unreachable ();
6450 }
6451
6452 /* Consume the token. */
6453 cp_lexer_consume_token (parser->lexer);
6454
6455 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6456
6457 type1 = cp_parser_type_id (parser);
6458
6459 /* Build a trivial decl-specifier-seq. */
6460 clear_decl_specs (&decl_specs);
6461 decl_specs.type = type1;
6462
6463 /* Call grokdeclarator to figure out what type this is. */
6464 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6465 /*initialized=*/0, /*attrlist=*/NULL);
6466
6467 if (binary)
6468 {
6469 cp_parser_require (parser, CPP_COMMA, "`,'");
6470
6471 type2 = cp_parser_type_id (parser);
6472
6473 /* Build a trivial decl-specifier-seq. */
6474 clear_decl_specs (&decl_specs);
6475 decl_specs.type = type2;
6476
6477 /* Call grokdeclarator to figure out what type this is. */
6478 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6479 /*initialized=*/0, /*attrlist=*/NULL);
6480 }
6481
6482 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6483
6484 /* Complete the trait expr, which may mean either processing the
6485 static assert now or saving it for template instantiation. */
6486 return finish_trait_expr (kind, type1, type2);
6487 }
6488
6489 /* Statements [gram.stmt.stmt] */
6490
6491 /* Parse a statement.
6492
6493 statement:
6494 labeled-statement
6495 expression-statement
6496 compound-statement
6497 selection-statement
6498 iteration-statement
6499 jump-statement
6500 declaration-statement
6501 try-block
6502
6503 IN_COMPOUND is true when the statement is nested inside a
6504 cp_parser_compound_statement; this matters for certain pragmas.
6505
6506 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6507 is a (possibly labeled) if statement which is not enclosed in braces
6508 and has an else clause. This is used to implement -Wparentheses. */
6509
6510 static void
6511 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6512 bool in_compound, bool *if_p)
6513 {
6514 tree statement;
6515 cp_token *token;
6516 location_t statement_location;
6517
6518 restart:
6519 if (if_p != NULL)
6520 *if_p = false;
6521 /* There is no statement yet. */
6522 statement = NULL_TREE;
6523 /* Peek at the next token. */
6524 token = cp_lexer_peek_token (parser->lexer);
6525 /* Remember the location of the first token in the statement. */
6526 statement_location = token->location;
6527 /* If this is a keyword, then that will often determine what kind of
6528 statement we have. */
6529 if (token->type == CPP_KEYWORD)
6530 {
6531 enum rid keyword = token->keyword;
6532
6533 switch (keyword)
6534 {
6535 case RID_CASE:
6536 case RID_DEFAULT:
6537 /* Looks like a labeled-statement with a case label.
6538 Parse the label, and then use tail recursion to parse
6539 the statement. */
6540 cp_parser_label_for_labeled_statement (parser);
6541 goto restart;
6542
6543 case RID_IF:
6544 case RID_SWITCH:
6545 statement = cp_parser_selection_statement (parser, if_p);
6546 break;
6547
6548 case RID_WHILE:
6549 case RID_DO:
6550 case RID_FOR:
6551 statement = cp_parser_iteration_statement (parser);
6552 break;
6553
6554 case RID_BREAK:
6555 case RID_CONTINUE:
6556 case RID_RETURN:
6557 case RID_GOTO:
6558 statement = cp_parser_jump_statement (parser);
6559 break;
6560
6561 /* Objective-C++ exception-handling constructs. */
6562 case RID_AT_TRY:
6563 case RID_AT_CATCH:
6564 case RID_AT_FINALLY:
6565 case RID_AT_SYNCHRONIZED:
6566 case RID_AT_THROW:
6567 statement = cp_parser_objc_statement (parser);
6568 break;
6569
6570 case RID_TRY:
6571 statement = cp_parser_try_block (parser);
6572 break;
6573
6574 case RID_NAMESPACE:
6575 /* This must be a namespace alias definition. */
6576 cp_parser_declaration_statement (parser);
6577 return;
6578
6579 default:
6580 /* It might be a keyword like `int' that can start a
6581 declaration-statement. */
6582 break;
6583 }
6584 }
6585 else if (token->type == CPP_NAME)
6586 {
6587 /* If the next token is a `:', then we are looking at a
6588 labeled-statement. */
6589 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6590 if (token->type == CPP_COLON)
6591 {
6592 /* Looks like a labeled-statement with an ordinary label.
6593 Parse the label, and then use tail recursion to parse
6594 the statement. */
6595 cp_parser_label_for_labeled_statement (parser);
6596 goto restart;
6597 }
6598 }
6599 /* Anything that starts with a `{' must be a compound-statement. */
6600 else if (token->type == CPP_OPEN_BRACE)
6601 statement = cp_parser_compound_statement (parser, NULL, false);
6602 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6603 a statement all its own. */
6604 else if (token->type == CPP_PRAGMA)
6605 {
6606 /* Only certain OpenMP pragmas are attached to statements, and thus
6607 are considered statements themselves. All others are not. In
6608 the context of a compound, accept the pragma as a "statement" and
6609 return so that we can check for a close brace. Otherwise we
6610 require a real statement and must go back and read one. */
6611 if (in_compound)
6612 cp_parser_pragma (parser, pragma_compound);
6613 else if (!cp_parser_pragma (parser, pragma_stmt))
6614 goto restart;
6615 return;
6616 }
6617 else if (token->type == CPP_EOF)
6618 {
6619 cp_parser_error (parser, "expected statement");
6620 return;
6621 }
6622
6623 /* Everything else must be a declaration-statement or an
6624 expression-statement. Try for the declaration-statement
6625 first, unless we are looking at a `;', in which case we know that
6626 we have an expression-statement. */
6627 if (!statement)
6628 {
6629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6630 {
6631 cp_parser_parse_tentatively (parser);
6632 /* Try to parse the declaration-statement. */
6633 cp_parser_declaration_statement (parser);
6634 /* If that worked, we're done. */
6635 if (cp_parser_parse_definitely (parser))
6636 return;
6637 }
6638 /* Look for an expression-statement instead. */
6639 statement = cp_parser_expression_statement (parser, in_statement_expr);
6640 }
6641
6642 /* Set the line number for the statement. */
6643 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6644 SET_EXPR_LOCATION (statement, statement_location);
6645 }
6646
6647 /* Parse the label for a labeled-statement, i.e.
6648
6649 identifier :
6650 case constant-expression :
6651 default :
6652
6653 GNU Extension:
6654 case constant-expression ... constant-expression : statement
6655
6656 When a label is parsed without errors, the label is added to the
6657 parse tree by the finish_* functions, so this function doesn't
6658 have to return the label. */
6659
6660 static void
6661 cp_parser_label_for_labeled_statement (cp_parser* parser)
6662 {
6663 cp_token *token;
6664
6665 /* The next token should be an identifier. */
6666 token = cp_lexer_peek_token (parser->lexer);
6667 if (token->type != CPP_NAME
6668 && token->type != CPP_KEYWORD)
6669 {
6670 cp_parser_error (parser, "expected labeled-statement");
6671 return;
6672 }
6673
6674 switch (token->keyword)
6675 {
6676 case RID_CASE:
6677 {
6678 tree expr, expr_hi;
6679 cp_token *ellipsis;
6680
6681 /* Consume the `case' token. */
6682 cp_lexer_consume_token (parser->lexer);
6683 /* Parse the constant-expression. */
6684 expr = cp_parser_constant_expression (parser,
6685 /*allow_non_constant_p=*/false,
6686 NULL);
6687
6688 ellipsis = cp_lexer_peek_token (parser->lexer);
6689 if (ellipsis->type == CPP_ELLIPSIS)
6690 {
6691 /* Consume the `...' token. */
6692 cp_lexer_consume_token (parser->lexer);
6693 expr_hi =
6694 cp_parser_constant_expression (parser,
6695 /*allow_non_constant_p=*/false,
6696 NULL);
6697 /* We don't need to emit warnings here, as the common code
6698 will do this for us. */
6699 }
6700 else
6701 expr_hi = NULL_TREE;
6702
6703 if (parser->in_switch_statement_p)
6704 finish_case_label (expr, expr_hi);
6705 else
6706 error ("case label %qE not within a switch statement", expr);
6707 }
6708 break;
6709
6710 case RID_DEFAULT:
6711 /* Consume the `default' token. */
6712 cp_lexer_consume_token (parser->lexer);
6713
6714 if (parser->in_switch_statement_p)
6715 finish_case_label (NULL_TREE, NULL_TREE);
6716 else
6717 error ("case label not within a switch statement");
6718 break;
6719
6720 default:
6721 /* Anything else must be an ordinary label. */
6722 finish_label_stmt (cp_parser_identifier (parser));
6723 break;
6724 }
6725
6726 /* Require the `:' token. */
6727 cp_parser_require (parser, CPP_COLON, "`:'");
6728 }
6729
6730 /* Parse an expression-statement.
6731
6732 expression-statement:
6733 expression [opt] ;
6734
6735 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6736 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6737 indicates whether this expression-statement is part of an
6738 expression statement. */
6739
6740 static tree
6741 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6742 {
6743 tree statement = NULL_TREE;
6744
6745 /* If the next token is a ';', then there is no expression
6746 statement. */
6747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6748 statement = cp_parser_expression (parser, /*cast_p=*/false);
6749
6750 /* Consume the final `;'. */
6751 cp_parser_consume_semicolon_at_end_of_statement (parser);
6752
6753 if (in_statement_expr
6754 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6755 /* This is the final expression statement of a statement
6756 expression. */
6757 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6758 else if (statement)
6759 statement = finish_expr_stmt (statement);
6760 else
6761 finish_stmt ();
6762
6763 return statement;
6764 }
6765
6766 /* Parse a compound-statement.
6767
6768 compound-statement:
6769 { statement-seq [opt] }
6770
6771 Returns a tree representing the statement. */
6772
6773 static tree
6774 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6775 bool in_try)
6776 {
6777 tree compound_stmt;
6778
6779 /* Consume the `{'. */
6780 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6781 return error_mark_node;
6782 /* Begin the compound-statement. */
6783 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6784 /* Parse an (optional) statement-seq. */
6785 cp_parser_statement_seq_opt (parser, in_statement_expr);
6786 /* Finish the compound-statement. */
6787 finish_compound_stmt (compound_stmt);
6788 /* Consume the `}'. */
6789 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6790
6791 return compound_stmt;
6792 }
6793
6794 /* Parse an (optional) statement-seq.
6795
6796 statement-seq:
6797 statement
6798 statement-seq [opt] statement */
6799
6800 static void
6801 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6802 {
6803 /* Scan statements until there aren't any more. */
6804 while (true)
6805 {
6806 cp_token *token = cp_lexer_peek_token (parser->lexer);
6807
6808 /* If we're looking at a `}', then we've run out of statements. */
6809 if (token->type == CPP_CLOSE_BRACE
6810 || token->type == CPP_EOF
6811 || token->type == CPP_PRAGMA_EOL)
6812 break;
6813
6814 /* If we are in a compound statement and find 'else' then
6815 something went wrong. */
6816 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6817 {
6818 if (parser->in_statement & IN_IF_STMT)
6819 break;
6820 else
6821 {
6822 token = cp_lexer_consume_token (parser->lexer);
6823 error ("%<else%> without a previous %<if%>");
6824 }
6825 }
6826
6827 /* Parse the statement. */
6828 cp_parser_statement (parser, in_statement_expr, true, NULL);
6829 }
6830 }
6831
6832 /* Parse a selection-statement.
6833
6834 selection-statement:
6835 if ( condition ) statement
6836 if ( condition ) statement else statement
6837 switch ( condition ) statement
6838
6839 Returns the new IF_STMT or SWITCH_STMT.
6840
6841 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6842 is a (possibly labeled) if statement which is not enclosed in
6843 braces and has an else clause. This is used to implement
6844 -Wparentheses. */
6845
6846 static tree
6847 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6848 {
6849 cp_token *token;
6850 enum rid keyword;
6851
6852 if (if_p != NULL)
6853 *if_p = false;
6854
6855 /* Peek at the next token. */
6856 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6857
6858 /* See what kind of keyword it is. */
6859 keyword = token->keyword;
6860 switch (keyword)
6861 {
6862 case RID_IF:
6863 case RID_SWITCH:
6864 {
6865 tree statement;
6866 tree condition;
6867
6868 /* Look for the `('. */
6869 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6870 {
6871 cp_parser_skip_to_end_of_statement (parser);
6872 return error_mark_node;
6873 }
6874
6875 /* Begin the selection-statement. */
6876 if (keyword == RID_IF)
6877 statement = begin_if_stmt ();
6878 else
6879 statement = begin_switch_stmt ();
6880
6881 /* Parse the condition. */
6882 condition = cp_parser_condition (parser);
6883 /* Look for the `)'. */
6884 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6885 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6886 /*consume_paren=*/true);
6887
6888 if (keyword == RID_IF)
6889 {
6890 bool nested_if;
6891 unsigned char in_statement;
6892
6893 /* Add the condition. */
6894 finish_if_stmt_cond (condition, statement);
6895
6896 /* Parse the then-clause. */
6897 in_statement = parser->in_statement;
6898 parser->in_statement |= IN_IF_STMT;
6899 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6900 parser->in_statement = in_statement;
6901
6902 finish_then_clause (statement);
6903
6904 /* If the next token is `else', parse the else-clause. */
6905 if (cp_lexer_next_token_is_keyword (parser->lexer,
6906 RID_ELSE))
6907 {
6908 /* Consume the `else' keyword. */
6909 cp_lexer_consume_token (parser->lexer);
6910 begin_else_clause (statement);
6911 /* Parse the else-clause. */
6912 cp_parser_implicitly_scoped_statement (parser, NULL);
6913 finish_else_clause (statement);
6914
6915 /* If we are currently parsing a then-clause, then
6916 IF_P will not be NULL. We set it to true to
6917 indicate that this if statement has an else clause.
6918 This may trigger the Wparentheses warning below
6919 when we get back up to the parent if statement. */
6920 if (if_p != NULL)
6921 *if_p = true;
6922 }
6923 else
6924 {
6925 /* This if statement does not have an else clause. If
6926 NESTED_IF is true, then the then-clause is an if
6927 statement which does have an else clause. We warn
6928 about the potential ambiguity. */
6929 if (nested_if)
6930 warning (OPT_Wparentheses,
6931 ("%Hsuggest explicit braces "
6932 "to avoid ambiguous %<else%>"),
6933 EXPR_LOCUS (statement));
6934 }
6935
6936 /* Now we're all done with the if-statement. */
6937 finish_if_stmt (statement);
6938 }
6939 else
6940 {
6941 bool in_switch_statement_p;
6942 unsigned char in_statement;
6943
6944 /* Add the condition. */
6945 finish_switch_cond (condition, statement);
6946
6947 /* Parse the body of the switch-statement. */
6948 in_switch_statement_p = parser->in_switch_statement_p;
6949 in_statement = parser->in_statement;
6950 parser->in_switch_statement_p = true;
6951 parser->in_statement |= IN_SWITCH_STMT;
6952 cp_parser_implicitly_scoped_statement (parser, NULL);
6953 parser->in_switch_statement_p = in_switch_statement_p;
6954 parser->in_statement = in_statement;
6955
6956 /* Now we're all done with the switch-statement. */
6957 finish_switch_stmt (statement);
6958 }
6959
6960 return statement;
6961 }
6962 break;
6963
6964 default:
6965 cp_parser_error (parser, "expected selection-statement");
6966 return error_mark_node;
6967 }
6968 }
6969
6970 /* Parse a condition.
6971
6972 condition:
6973 expression
6974 type-specifier-seq declarator = assignment-expression
6975
6976 GNU Extension:
6977
6978 condition:
6979 type-specifier-seq declarator asm-specification [opt]
6980 attributes [opt] = assignment-expression
6981
6982 Returns the expression that should be tested. */
6983
6984 static tree
6985 cp_parser_condition (cp_parser* parser)
6986 {
6987 cp_decl_specifier_seq type_specifiers;
6988 const char *saved_message;
6989
6990 /* Try the declaration first. */
6991 cp_parser_parse_tentatively (parser);
6992 /* New types are not allowed in the type-specifier-seq for a
6993 condition. */
6994 saved_message = parser->type_definition_forbidden_message;
6995 parser->type_definition_forbidden_message
6996 = "types may not be defined in conditions";
6997 /* Parse the type-specifier-seq. */
6998 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6999 &type_specifiers);
7000 /* Restore the saved message. */
7001 parser->type_definition_forbidden_message = saved_message;
7002 /* If all is well, we might be looking at a declaration. */
7003 if (!cp_parser_error_occurred (parser))
7004 {
7005 tree decl;
7006 tree asm_specification;
7007 tree attributes;
7008 cp_declarator *declarator;
7009 tree initializer = NULL_TREE;
7010
7011 /* Parse the declarator. */
7012 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7013 /*ctor_dtor_or_conv_p=*/NULL,
7014 /*parenthesized_p=*/NULL,
7015 /*member_p=*/false);
7016 /* Parse the attributes. */
7017 attributes = cp_parser_attributes_opt (parser);
7018 /* Parse the asm-specification. */
7019 asm_specification = cp_parser_asm_specification_opt (parser);
7020 /* If the next token is not an `=', then we might still be
7021 looking at an expression. For example:
7022
7023 if (A(a).x)
7024
7025 looks like a decl-specifier-seq and a declarator -- but then
7026 there is no `=', so this is an expression. */
7027 cp_parser_require (parser, CPP_EQ, "`='");
7028 /* If we did see an `=', then we are looking at a declaration
7029 for sure. */
7030 if (cp_parser_parse_definitely (parser))
7031 {
7032 tree pushed_scope;
7033 bool non_constant_p;
7034
7035 /* Create the declaration. */
7036 decl = start_decl (declarator, &type_specifiers,
7037 /*initialized_p=*/true,
7038 attributes, /*prefix_attributes=*/NULL_TREE,
7039 &pushed_scope);
7040 /* Parse the assignment-expression. */
7041 initializer
7042 = cp_parser_constant_expression (parser,
7043 /*allow_non_constant_p=*/true,
7044 &non_constant_p);
7045 if (!non_constant_p)
7046 initializer = fold_non_dependent_expr (initializer);
7047
7048 /* Process the initializer. */
7049 cp_finish_decl (decl,
7050 initializer, !non_constant_p,
7051 asm_specification,
7052 LOOKUP_ONLYCONVERTING);
7053
7054 if (pushed_scope)
7055 pop_scope (pushed_scope);
7056
7057 return convert_from_reference (decl);
7058 }
7059 }
7060 /* If we didn't even get past the declarator successfully, we are
7061 definitely not looking at a declaration. */
7062 else
7063 cp_parser_abort_tentative_parse (parser);
7064
7065 /* Otherwise, we are looking at an expression. */
7066 return cp_parser_expression (parser, /*cast_p=*/false);
7067 }
7068
7069 /* We check for a ) immediately followed by ; with no whitespacing
7070 between. This is used to issue a warning for:
7071
7072 while (...);
7073
7074 and:
7075
7076 for (...);
7077
7078 as the semicolon is probably extraneous.
7079
7080 On parse errors, the next token might not be a ), so do nothing in
7081 that case. */
7082
7083 static void
7084 check_empty_body (cp_parser* parser, const char* type)
7085 {
7086 cp_token *token;
7087 cp_token *close_paren;
7088 expanded_location close_loc;
7089 expanded_location semi_loc;
7090
7091 close_paren = cp_lexer_peek_token (parser->lexer);
7092 if (close_paren->type != CPP_CLOSE_PAREN)
7093 return;
7094
7095 close_loc = expand_location (close_paren->location);
7096 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7097
7098 if (token->type != CPP_SEMICOLON
7099 || (token->flags & PREV_WHITE))
7100 return;
7101
7102 semi_loc = expand_location (token->location);
7103 if (close_loc.line == semi_loc.line
7104 #ifdef USE_MAPPED_LOCATION
7105 && close_loc.column+1 == semi_loc.column
7106 #endif
7107 )
7108 warning (OPT_Wempty_body,
7109 "suggest a space before %<;%> or explicit braces around empty "
7110 "body in %<%s%> statement",
7111 type);
7112 }
7113
7114 /* Parse an iteration-statement.
7115
7116 iteration-statement:
7117 while ( condition ) statement
7118 do statement while ( expression ) ;
7119 for ( for-init-statement condition [opt] ; expression [opt] )
7120 statement
7121
7122 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7123
7124 static tree
7125 cp_parser_iteration_statement (cp_parser* parser)
7126 {
7127 cp_token *token;
7128 enum rid keyword;
7129 tree statement;
7130 unsigned char in_statement;
7131
7132 /* Peek at the next token. */
7133 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7134 if (!token)
7135 return error_mark_node;
7136
7137 /* Remember whether or not we are already within an iteration
7138 statement. */
7139 in_statement = parser->in_statement;
7140
7141 /* See what kind of keyword it is. */
7142 keyword = token->keyword;
7143 switch (keyword)
7144 {
7145 case RID_WHILE:
7146 {
7147 tree condition;
7148
7149 /* Begin the while-statement. */
7150 statement = begin_while_stmt ();
7151 /* Look for the `('. */
7152 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7153 /* Parse the condition. */
7154 condition = cp_parser_condition (parser);
7155 finish_while_stmt_cond (condition, statement);
7156 check_empty_body (parser, "while");
7157 /* Look for the `)'. */
7158 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7159 /* Parse the dependent statement. */
7160 parser->in_statement = IN_ITERATION_STMT;
7161 cp_parser_already_scoped_statement (parser);
7162 parser->in_statement = in_statement;
7163 /* We're done with the while-statement. */
7164 finish_while_stmt (statement);
7165 }
7166 break;
7167
7168 case RID_DO:
7169 {
7170 tree expression;
7171
7172 /* Begin the do-statement. */
7173 statement = begin_do_stmt ();
7174 /* Parse the body of the do-statement. */
7175 parser->in_statement = IN_ITERATION_STMT;
7176 cp_parser_implicitly_scoped_statement (parser, NULL);
7177 parser->in_statement = in_statement;
7178 finish_do_body (statement);
7179 /* Look for the `while' keyword. */
7180 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7181 /* Look for the `('. */
7182 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7183 /* Parse the expression. */
7184 expression = cp_parser_expression (parser, /*cast_p=*/false);
7185 /* We're done with the do-statement. */
7186 finish_do_stmt (expression, statement);
7187 /* Look for the `)'. */
7188 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7189 /* Look for the `;'. */
7190 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7191 }
7192 break;
7193
7194 case RID_FOR:
7195 {
7196 tree condition = NULL_TREE;
7197 tree expression = NULL_TREE;
7198
7199 /* Begin the for-statement. */
7200 statement = begin_for_stmt ();
7201 /* Look for the `('. */
7202 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7203 /* Parse the initialization. */
7204 cp_parser_for_init_statement (parser);
7205 finish_for_init_stmt (statement);
7206
7207 /* If there's a condition, process it. */
7208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7209 condition = cp_parser_condition (parser);
7210 finish_for_cond (condition, statement);
7211 /* Look for the `;'. */
7212 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7213
7214 /* If there's an expression, process it. */
7215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7216 expression = cp_parser_expression (parser, /*cast_p=*/false);
7217 finish_for_expr (expression, statement);
7218 check_empty_body (parser, "for");
7219 /* Look for the `)'. */
7220 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7221
7222 /* Parse the body of the for-statement. */
7223 parser->in_statement = IN_ITERATION_STMT;
7224 cp_parser_already_scoped_statement (parser);
7225 parser->in_statement = in_statement;
7226
7227 /* We're done with the for-statement. */
7228 finish_for_stmt (statement);
7229 }
7230 break;
7231
7232 default:
7233 cp_parser_error (parser, "expected iteration-statement");
7234 statement = error_mark_node;
7235 break;
7236 }
7237
7238 return statement;
7239 }
7240
7241 /* Parse a for-init-statement.
7242
7243 for-init-statement:
7244 expression-statement
7245 simple-declaration */
7246
7247 static void
7248 cp_parser_for_init_statement (cp_parser* parser)
7249 {
7250 /* If the next token is a `;', then we have an empty
7251 expression-statement. Grammatically, this is also a
7252 simple-declaration, but an invalid one, because it does not
7253 declare anything. Therefore, if we did not handle this case
7254 specially, we would issue an error message about an invalid
7255 declaration. */
7256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7257 {
7258 /* We're going to speculatively look for a declaration, falling back
7259 to an expression, if necessary. */
7260 cp_parser_parse_tentatively (parser);
7261 /* Parse the declaration. */
7262 cp_parser_simple_declaration (parser,
7263 /*function_definition_allowed_p=*/false);
7264 /* If the tentative parse failed, then we shall need to look for an
7265 expression-statement. */
7266 if (cp_parser_parse_definitely (parser))
7267 return;
7268 }
7269
7270 cp_parser_expression_statement (parser, false);
7271 }
7272
7273 /* Parse a jump-statement.
7274
7275 jump-statement:
7276 break ;
7277 continue ;
7278 return expression [opt] ;
7279 goto identifier ;
7280
7281 GNU extension:
7282
7283 jump-statement:
7284 goto * expression ;
7285
7286 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7287
7288 static tree
7289 cp_parser_jump_statement (cp_parser* parser)
7290 {
7291 tree statement = error_mark_node;
7292 cp_token *token;
7293 enum rid keyword;
7294 unsigned char in_statement;
7295
7296 /* Peek at the next token. */
7297 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7298 if (!token)
7299 return error_mark_node;
7300
7301 /* See what kind of keyword it is. */
7302 keyword = token->keyword;
7303 switch (keyword)
7304 {
7305 case RID_BREAK:
7306 in_statement = parser->in_statement & ~IN_IF_STMT;
7307 switch (in_statement)
7308 {
7309 case 0:
7310 error ("break statement not within loop or switch");
7311 break;
7312 default:
7313 gcc_assert ((in_statement & IN_SWITCH_STMT)
7314 || in_statement == IN_ITERATION_STMT);
7315 statement = finish_break_stmt ();
7316 break;
7317 case IN_OMP_BLOCK:
7318 error ("invalid exit from OpenMP structured block");
7319 break;
7320 case IN_OMP_FOR:
7321 error ("break statement used with OpenMP for loop");
7322 break;
7323 }
7324 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7325 break;
7326
7327 case RID_CONTINUE:
7328 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7329 {
7330 case 0:
7331 error ("continue statement not within a loop");
7332 break;
7333 case IN_ITERATION_STMT:
7334 case IN_OMP_FOR:
7335 statement = finish_continue_stmt ();
7336 break;
7337 case IN_OMP_BLOCK:
7338 error ("invalid exit from OpenMP structured block");
7339 break;
7340 default:
7341 gcc_unreachable ();
7342 }
7343 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7344 break;
7345
7346 case RID_RETURN:
7347 {
7348 tree expr;
7349
7350 /* If the next token is a `;', then there is no
7351 expression. */
7352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7353 expr = cp_parser_expression (parser, /*cast_p=*/false);
7354 else
7355 expr = NULL_TREE;
7356 /* Build the return-statement. */
7357 statement = finish_return_stmt (expr);
7358 /* Look for the final `;'. */
7359 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7360 }
7361 break;
7362
7363 case RID_GOTO:
7364 /* Create the goto-statement. */
7365 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7366 {
7367 /* Issue a warning about this use of a GNU extension. */
7368 if (pedantic)
7369 pedwarn ("ISO C++ forbids computed gotos");
7370 /* Consume the '*' token. */
7371 cp_lexer_consume_token (parser->lexer);
7372 /* Parse the dependent expression. */
7373 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7374 }
7375 else
7376 finish_goto_stmt (cp_parser_identifier (parser));
7377 /* Look for the final `;'. */
7378 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7379 break;
7380
7381 default:
7382 cp_parser_error (parser, "expected jump-statement");
7383 break;
7384 }
7385
7386 return statement;
7387 }
7388
7389 /* Parse a declaration-statement.
7390
7391 declaration-statement:
7392 block-declaration */
7393
7394 static void
7395 cp_parser_declaration_statement (cp_parser* parser)
7396 {
7397 void *p;
7398
7399 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7400 p = obstack_alloc (&declarator_obstack, 0);
7401
7402 /* Parse the block-declaration. */
7403 cp_parser_block_declaration (parser, /*statement_p=*/true);
7404
7405 /* Free any declarators allocated. */
7406 obstack_free (&declarator_obstack, p);
7407
7408 /* Finish off the statement. */
7409 finish_stmt ();
7410 }
7411
7412 /* Some dependent statements (like `if (cond) statement'), are
7413 implicitly in their own scope. In other words, if the statement is
7414 a single statement (as opposed to a compound-statement), it is
7415 none-the-less treated as if it were enclosed in braces. Any
7416 declarations appearing in the dependent statement are out of scope
7417 after control passes that point. This function parses a statement,
7418 but ensures that is in its own scope, even if it is not a
7419 compound-statement.
7420
7421 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7422 is a (possibly labeled) if statement which is not enclosed in
7423 braces and has an else clause. This is used to implement
7424 -Wparentheses.
7425
7426 Returns the new statement. */
7427
7428 static tree
7429 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7430 {
7431 tree statement;
7432
7433 if (if_p != NULL)
7434 *if_p = false;
7435
7436 /* Mark if () ; with a special NOP_EXPR. */
7437 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7438 {
7439 cp_lexer_consume_token (parser->lexer);
7440 statement = add_stmt (build_empty_stmt ());
7441 }
7442 /* if a compound is opened, we simply parse the statement directly. */
7443 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7444 statement = cp_parser_compound_statement (parser, NULL, false);
7445 /* If the token is not a `{', then we must take special action. */
7446 else
7447 {
7448 /* Create a compound-statement. */
7449 statement = begin_compound_stmt (0);
7450 /* Parse the dependent-statement. */
7451 cp_parser_statement (parser, NULL_TREE, false, if_p);
7452 /* Finish the dummy compound-statement. */
7453 finish_compound_stmt (statement);
7454 }
7455
7456 /* Return the statement. */
7457 return statement;
7458 }
7459
7460 /* For some dependent statements (like `while (cond) statement'), we
7461 have already created a scope. Therefore, even if the dependent
7462 statement is a compound-statement, we do not want to create another
7463 scope. */
7464
7465 static void
7466 cp_parser_already_scoped_statement (cp_parser* parser)
7467 {
7468 /* If the token is a `{', then we must take special action. */
7469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7470 cp_parser_statement (parser, NULL_TREE, false, NULL);
7471 else
7472 {
7473 /* Avoid calling cp_parser_compound_statement, so that we
7474 don't create a new scope. Do everything else by hand. */
7475 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7476 cp_parser_statement_seq_opt (parser, NULL_TREE);
7477 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7478 }
7479 }
7480
7481 /* Declarations [gram.dcl.dcl] */
7482
7483 /* Parse an optional declaration-sequence.
7484
7485 declaration-seq:
7486 declaration
7487 declaration-seq declaration */
7488
7489 static void
7490 cp_parser_declaration_seq_opt (cp_parser* parser)
7491 {
7492 while (true)
7493 {
7494 cp_token *token;
7495
7496 token = cp_lexer_peek_token (parser->lexer);
7497
7498 if (token->type == CPP_CLOSE_BRACE
7499 || token->type == CPP_EOF
7500 || token->type == CPP_PRAGMA_EOL)
7501 break;
7502
7503 if (token->type == CPP_SEMICOLON)
7504 {
7505 /* A declaration consisting of a single semicolon is
7506 invalid. Allow it unless we're being pedantic. */
7507 cp_lexer_consume_token (parser->lexer);
7508 if (pedantic && !in_system_header)
7509 pedwarn ("extra %<;%>");
7510 continue;
7511 }
7512
7513 /* If we're entering or exiting a region that's implicitly
7514 extern "C", modify the lang context appropriately. */
7515 if (!parser->implicit_extern_c && token->implicit_extern_c)
7516 {
7517 push_lang_context (lang_name_c);
7518 parser->implicit_extern_c = true;
7519 }
7520 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7521 {
7522 pop_lang_context ();
7523 parser->implicit_extern_c = false;
7524 }
7525
7526 if (token->type == CPP_PRAGMA)
7527 {
7528 /* A top-level declaration can consist solely of a #pragma.
7529 A nested declaration cannot, so this is done here and not
7530 in cp_parser_declaration. (A #pragma at block scope is
7531 handled in cp_parser_statement.) */
7532 cp_parser_pragma (parser, pragma_external);
7533 continue;
7534 }
7535
7536 /* Parse the declaration itself. */
7537 cp_parser_declaration (parser);
7538 }
7539 }
7540
7541 /* Parse a declaration.
7542
7543 declaration:
7544 block-declaration
7545 function-definition
7546 template-declaration
7547 explicit-instantiation
7548 explicit-specialization
7549 linkage-specification
7550 namespace-definition
7551
7552 GNU extension:
7553
7554 declaration:
7555 __extension__ declaration */
7556
7557 static void
7558 cp_parser_declaration (cp_parser* parser)
7559 {
7560 cp_token token1;
7561 cp_token token2;
7562 int saved_pedantic;
7563 void *p;
7564
7565 /* Check for the `__extension__' keyword. */
7566 if (cp_parser_extension_opt (parser, &saved_pedantic))
7567 {
7568 /* Parse the qualified declaration. */
7569 cp_parser_declaration (parser);
7570 /* Restore the PEDANTIC flag. */
7571 pedantic = saved_pedantic;
7572
7573 return;
7574 }
7575
7576 /* Try to figure out what kind of declaration is present. */
7577 token1 = *cp_lexer_peek_token (parser->lexer);
7578
7579 if (token1.type != CPP_EOF)
7580 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7581 else
7582 {
7583 token2.type = CPP_EOF;
7584 token2.keyword = RID_MAX;
7585 }
7586
7587 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7588 p = obstack_alloc (&declarator_obstack, 0);
7589
7590 /* If the next token is `extern' and the following token is a string
7591 literal, then we have a linkage specification. */
7592 if (token1.keyword == RID_EXTERN
7593 && cp_parser_is_string_literal (&token2))
7594 cp_parser_linkage_specification (parser);
7595 /* If the next token is `template', then we have either a template
7596 declaration, an explicit instantiation, or an explicit
7597 specialization. */
7598 else if (token1.keyword == RID_TEMPLATE)
7599 {
7600 /* `template <>' indicates a template specialization. */
7601 if (token2.type == CPP_LESS
7602 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7603 cp_parser_explicit_specialization (parser);
7604 /* `template <' indicates a template declaration. */
7605 else if (token2.type == CPP_LESS)
7606 cp_parser_template_declaration (parser, /*member_p=*/false);
7607 /* Anything else must be an explicit instantiation. */
7608 else
7609 cp_parser_explicit_instantiation (parser);
7610 }
7611 /* If the next token is `export', then we have a template
7612 declaration. */
7613 else if (token1.keyword == RID_EXPORT)
7614 cp_parser_template_declaration (parser, /*member_p=*/false);
7615 /* If the next token is `extern', 'static' or 'inline' and the one
7616 after that is `template', we have a GNU extended explicit
7617 instantiation directive. */
7618 else if (cp_parser_allow_gnu_extensions_p (parser)
7619 && (token1.keyword == RID_EXTERN
7620 || token1.keyword == RID_STATIC
7621 || token1.keyword == RID_INLINE)
7622 && token2.keyword == RID_TEMPLATE)
7623 cp_parser_explicit_instantiation (parser);
7624 /* If the next token is `namespace', check for a named or unnamed
7625 namespace definition. */
7626 else if (token1.keyword == RID_NAMESPACE
7627 && (/* A named namespace definition. */
7628 (token2.type == CPP_NAME
7629 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7630 != CPP_EQ))
7631 /* An unnamed namespace definition. */
7632 || token2.type == CPP_OPEN_BRACE
7633 || token2.keyword == RID_ATTRIBUTE))
7634 cp_parser_namespace_definition (parser);
7635 /* Objective-C++ declaration/definition. */
7636 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7637 cp_parser_objc_declaration (parser);
7638 /* We must have either a block declaration or a function
7639 definition. */
7640 else
7641 /* Try to parse a block-declaration, or a function-definition. */
7642 cp_parser_block_declaration (parser, /*statement_p=*/false);
7643
7644 /* Free any declarators allocated. */
7645 obstack_free (&declarator_obstack, p);
7646 }
7647
7648 /* Parse a block-declaration.
7649
7650 block-declaration:
7651 simple-declaration
7652 asm-definition
7653 namespace-alias-definition
7654 using-declaration
7655 using-directive
7656
7657 GNU Extension:
7658
7659 block-declaration:
7660 __extension__ block-declaration
7661 label-declaration
7662
7663 C++0x Extension:
7664
7665 block-declaration:
7666 static_assert-declaration
7667
7668 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7669 part of a declaration-statement. */
7670
7671 static void
7672 cp_parser_block_declaration (cp_parser *parser,
7673 bool statement_p)
7674 {
7675 cp_token *token1;
7676 int saved_pedantic;
7677
7678 /* Check for the `__extension__' keyword. */
7679 if (cp_parser_extension_opt (parser, &saved_pedantic))
7680 {
7681 /* Parse the qualified declaration. */
7682 cp_parser_block_declaration (parser, statement_p);
7683 /* Restore the PEDANTIC flag. */
7684 pedantic = saved_pedantic;
7685
7686 return;
7687 }
7688
7689 /* Peek at the next token to figure out which kind of declaration is
7690 present. */
7691 token1 = cp_lexer_peek_token (parser->lexer);
7692
7693 /* If the next keyword is `asm', we have an asm-definition. */
7694 if (token1->keyword == RID_ASM)
7695 {
7696 if (statement_p)
7697 cp_parser_commit_to_tentative_parse (parser);
7698 cp_parser_asm_definition (parser);
7699 }
7700 /* If the next keyword is `namespace', we have a
7701 namespace-alias-definition. */
7702 else if (token1->keyword == RID_NAMESPACE)
7703 cp_parser_namespace_alias_definition (parser);
7704 /* If the next keyword is `using', we have either a
7705 using-declaration or a using-directive. */
7706 else if (token1->keyword == RID_USING)
7707 {
7708 cp_token *token2;
7709
7710 if (statement_p)
7711 cp_parser_commit_to_tentative_parse (parser);
7712 /* If the token after `using' is `namespace', then we have a
7713 using-directive. */
7714 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7715 if (token2->keyword == RID_NAMESPACE)
7716 cp_parser_using_directive (parser);
7717 /* Otherwise, it's a using-declaration. */
7718 else
7719 cp_parser_using_declaration (parser,
7720 /*access_declaration_p=*/false);
7721 }
7722 /* If the next keyword is `__label__' we have a label declaration. */
7723 else if (token1->keyword == RID_LABEL)
7724 {
7725 if (statement_p)
7726 cp_parser_commit_to_tentative_parse (parser);
7727 cp_parser_label_declaration (parser);
7728 }
7729 /* If the next token is `static_assert' we have a static assertion. */
7730 else if (token1->keyword == RID_STATIC_ASSERT)
7731 cp_parser_static_assert (parser, /*member_p=*/false);
7732 /* Anything else must be a simple-declaration. */
7733 else
7734 cp_parser_simple_declaration (parser, !statement_p);
7735 }
7736
7737 /* Parse a simple-declaration.
7738
7739 simple-declaration:
7740 decl-specifier-seq [opt] init-declarator-list [opt] ;
7741
7742 init-declarator-list:
7743 init-declarator
7744 init-declarator-list , init-declarator
7745
7746 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7747 function-definition as a simple-declaration. */
7748
7749 static void
7750 cp_parser_simple_declaration (cp_parser* parser,
7751 bool function_definition_allowed_p)
7752 {
7753 cp_decl_specifier_seq decl_specifiers;
7754 int declares_class_or_enum;
7755 bool saw_declarator;
7756
7757 /* Defer access checks until we know what is being declared; the
7758 checks for names appearing in the decl-specifier-seq should be
7759 done as if we were in the scope of the thing being declared. */
7760 push_deferring_access_checks (dk_deferred);
7761
7762 /* Parse the decl-specifier-seq. We have to keep track of whether
7763 or not the decl-specifier-seq declares a named class or
7764 enumeration type, since that is the only case in which the
7765 init-declarator-list is allowed to be empty.
7766
7767 [dcl.dcl]
7768
7769 In a simple-declaration, the optional init-declarator-list can be
7770 omitted only when declaring a class or enumeration, that is when
7771 the decl-specifier-seq contains either a class-specifier, an
7772 elaborated-type-specifier, or an enum-specifier. */
7773 cp_parser_decl_specifier_seq (parser,
7774 CP_PARSER_FLAGS_OPTIONAL,
7775 &decl_specifiers,
7776 &declares_class_or_enum);
7777 /* We no longer need to defer access checks. */
7778 stop_deferring_access_checks ();
7779
7780 /* In a block scope, a valid declaration must always have a
7781 decl-specifier-seq. By not trying to parse declarators, we can
7782 resolve the declaration/expression ambiguity more quickly. */
7783 if (!function_definition_allowed_p
7784 && !decl_specifiers.any_specifiers_p)
7785 {
7786 cp_parser_error (parser, "expected declaration");
7787 goto done;
7788 }
7789
7790 /* If the next two tokens are both identifiers, the code is
7791 erroneous. The usual cause of this situation is code like:
7792
7793 T t;
7794
7795 where "T" should name a type -- but does not. */
7796 if (!decl_specifiers.type
7797 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7798 {
7799 /* If parsing tentatively, we should commit; we really are
7800 looking at a declaration. */
7801 cp_parser_commit_to_tentative_parse (parser);
7802 /* Give up. */
7803 goto done;
7804 }
7805
7806 /* If we have seen at least one decl-specifier, and the next token
7807 is not a parenthesis, then we must be looking at a declaration.
7808 (After "int (" we might be looking at a functional cast.) */
7809 if (decl_specifiers.any_specifiers_p
7810 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7811 cp_parser_commit_to_tentative_parse (parser);
7812
7813 /* Keep going until we hit the `;' at the end of the simple
7814 declaration. */
7815 saw_declarator = false;
7816 while (cp_lexer_next_token_is_not (parser->lexer,
7817 CPP_SEMICOLON))
7818 {
7819 cp_token *token;
7820 bool function_definition_p;
7821 tree decl;
7822
7823 if (saw_declarator)
7824 {
7825 /* If we are processing next declarator, coma is expected */
7826 token = cp_lexer_peek_token (parser->lexer);
7827 gcc_assert (token->type == CPP_COMMA);
7828 cp_lexer_consume_token (parser->lexer);
7829 }
7830 else
7831 saw_declarator = true;
7832
7833 /* Parse the init-declarator. */
7834 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7835 /*checks=*/NULL,
7836 function_definition_allowed_p,
7837 /*member_p=*/false,
7838 declares_class_or_enum,
7839 &function_definition_p);
7840 /* If an error occurred while parsing tentatively, exit quickly.
7841 (That usually happens when in the body of a function; each
7842 statement is treated as a declaration-statement until proven
7843 otherwise.) */
7844 if (cp_parser_error_occurred (parser))
7845 goto done;
7846 /* Handle function definitions specially. */
7847 if (function_definition_p)
7848 {
7849 /* If the next token is a `,', then we are probably
7850 processing something like:
7851
7852 void f() {}, *p;
7853
7854 which is erroneous. */
7855 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7856 error ("mixing declarations and function-definitions is forbidden");
7857 /* Otherwise, we're done with the list of declarators. */
7858 else
7859 {
7860 pop_deferring_access_checks ();
7861 return;
7862 }
7863 }
7864 /* The next token should be either a `,' or a `;'. */
7865 token = cp_lexer_peek_token (parser->lexer);
7866 /* If it's a `,', there are more declarators to come. */
7867 if (token->type == CPP_COMMA)
7868 /* will be consumed next time around */;
7869 /* If it's a `;', we are done. */
7870 else if (token->type == CPP_SEMICOLON)
7871 break;
7872 /* Anything else is an error. */
7873 else
7874 {
7875 /* If we have already issued an error message we don't need
7876 to issue another one. */
7877 if (decl != error_mark_node
7878 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7879 cp_parser_error (parser, "expected %<,%> or %<;%>");
7880 /* Skip tokens until we reach the end of the statement. */
7881 cp_parser_skip_to_end_of_statement (parser);
7882 /* If the next token is now a `;', consume it. */
7883 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7884 cp_lexer_consume_token (parser->lexer);
7885 goto done;
7886 }
7887 /* After the first time around, a function-definition is not
7888 allowed -- even if it was OK at first. For example:
7889
7890 int i, f() {}
7891
7892 is not valid. */
7893 function_definition_allowed_p = false;
7894 }
7895
7896 /* Issue an error message if no declarators are present, and the
7897 decl-specifier-seq does not itself declare a class or
7898 enumeration. */
7899 if (!saw_declarator)
7900 {
7901 if (cp_parser_declares_only_class_p (parser))
7902 shadow_tag (&decl_specifiers);
7903 /* Perform any deferred access checks. */
7904 perform_deferred_access_checks ();
7905 }
7906
7907 /* Consume the `;'. */
7908 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7909
7910 done:
7911 pop_deferring_access_checks ();
7912 }
7913
7914 /* Parse a decl-specifier-seq.
7915
7916 decl-specifier-seq:
7917 decl-specifier-seq [opt] decl-specifier
7918
7919 decl-specifier:
7920 storage-class-specifier
7921 type-specifier
7922 function-specifier
7923 friend
7924 typedef
7925
7926 GNU Extension:
7927
7928 decl-specifier:
7929 attributes
7930
7931 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7932
7933 The parser flags FLAGS is used to control type-specifier parsing.
7934
7935 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7936 flags:
7937
7938 1: one of the decl-specifiers is an elaborated-type-specifier
7939 (i.e., a type declaration)
7940 2: one of the decl-specifiers is an enum-specifier or a
7941 class-specifier (i.e., a type definition)
7942
7943 */
7944
7945 static void
7946 cp_parser_decl_specifier_seq (cp_parser* parser,
7947 cp_parser_flags flags,
7948 cp_decl_specifier_seq *decl_specs,
7949 int* declares_class_or_enum)
7950 {
7951 bool constructor_possible_p = !parser->in_declarator_p;
7952
7953 /* Clear DECL_SPECS. */
7954 clear_decl_specs (decl_specs);
7955
7956 /* Assume no class or enumeration type is declared. */
7957 *declares_class_or_enum = 0;
7958
7959 /* Keep reading specifiers until there are no more to read. */
7960 while (true)
7961 {
7962 bool constructor_p;
7963 bool found_decl_spec;
7964 cp_token *token;
7965
7966 /* Peek at the next token. */
7967 token = cp_lexer_peek_token (parser->lexer);
7968 /* Handle attributes. */
7969 if (token->keyword == RID_ATTRIBUTE)
7970 {
7971 /* Parse the attributes. */
7972 decl_specs->attributes
7973 = chainon (decl_specs->attributes,
7974 cp_parser_attributes_opt (parser));
7975 continue;
7976 }
7977 /* Assume we will find a decl-specifier keyword. */
7978 found_decl_spec = true;
7979 /* If the next token is an appropriate keyword, we can simply
7980 add it to the list. */
7981 switch (token->keyword)
7982 {
7983 /* decl-specifier:
7984 friend */
7985 case RID_FRIEND:
7986 if (!at_class_scope_p ())
7987 {
7988 error ("%<friend%> used outside of class");
7989 cp_lexer_purge_token (parser->lexer);
7990 }
7991 else
7992 {
7993 ++decl_specs->specs[(int) ds_friend];
7994 /* Consume the token. */
7995 cp_lexer_consume_token (parser->lexer);
7996 }
7997 break;
7998
7999 /* function-specifier:
8000 inline
8001 virtual
8002 explicit */
8003 case RID_INLINE:
8004 case RID_VIRTUAL:
8005 case RID_EXPLICIT:
8006 cp_parser_function_specifier_opt (parser, decl_specs);
8007 break;
8008
8009 /* decl-specifier:
8010 typedef */
8011 case RID_TYPEDEF:
8012 ++decl_specs->specs[(int) ds_typedef];
8013 /* Consume the token. */
8014 cp_lexer_consume_token (parser->lexer);
8015 /* A constructor declarator cannot appear in a typedef. */
8016 constructor_possible_p = false;
8017 /* The "typedef" keyword can only occur in a declaration; we
8018 may as well commit at this point. */
8019 cp_parser_commit_to_tentative_parse (parser);
8020
8021 if (decl_specs->storage_class != sc_none)
8022 decl_specs->conflicting_specifiers_p = true;
8023 break;
8024
8025 /* storage-class-specifier:
8026 auto
8027 register
8028 static
8029 extern
8030 mutable
8031
8032 GNU Extension:
8033 thread */
8034 case RID_AUTO:
8035 case RID_REGISTER:
8036 case RID_STATIC:
8037 case RID_EXTERN:
8038 case RID_MUTABLE:
8039 /* Consume the token. */
8040 cp_lexer_consume_token (parser->lexer);
8041 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8042 break;
8043 case RID_THREAD:
8044 /* Consume the token. */
8045 cp_lexer_consume_token (parser->lexer);
8046 ++decl_specs->specs[(int) ds_thread];
8047 break;
8048
8049 default:
8050 /* We did not yet find a decl-specifier yet. */
8051 found_decl_spec = false;
8052 break;
8053 }
8054
8055 /* Constructors are a special case. The `S' in `S()' is not a
8056 decl-specifier; it is the beginning of the declarator. */
8057 constructor_p
8058 = (!found_decl_spec
8059 && constructor_possible_p
8060 && (cp_parser_constructor_declarator_p
8061 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8062
8063 /* If we don't have a DECL_SPEC yet, then we must be looking at
8064 a type-specifier. */
8065 if (!found_decl_spec && !constructor_p)
8066 {
8067 int decl_spec_declares_class_or_enum;
8068 bool is_cv_qualifier;
8069 tree type_spec;
8070
8071 type_spec
8072 = cp_parser_type_specifier (parser, flags,
8073 decl_specs,
8074 /*is_declaration=*/true,
8075 &decl_spec_declares_class_or_enum,
8076 &is_cv_qualifier);
8077
8078 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8079
8080 /* If this type-specifier referenced a user-defined type
8081 (a typedef, class-name, etc.), then we can't allow any
8082 more such type-specifiers henceforth.
8083
8084 [dcl.spec]
8085
8086 The longest sequence of decl-specifiers that could
8087 possibly be a type name is taken as the
8088 decl-specifier-seq of a declaration. The sequence shall
8089 be self-consistent as described below.
8090
8091 [dcl.type]
8092
8093 As a general rule, at most one type-specifier is allowed
8094 in the complete decl-specifier-seq of a declaration. The
8095 only exceptions are the following:
8096
8097 -- const or volatile can be combined with any other
8098 type-specifier.
8099
8100 -- signed or unsigned can be combined with char, long,
8101 short, or int.
8102
8103 -- ..
8104
8105 Example:
8106
8107 typedef char* Pc;
8108 void g (const int Pc);
8109
8110 Here, Pc is *not* part of the decl-specifier seq; it's
8111 the declarator. Therefore, once we see a type-specifier
8112 (other than a cv-qualifier), we forbid any additional
8113 user-defined types. We *do* still allow things like `int
8114 int' to be considered a decl-specifier-seq, and issue the
8115 error message later. */
8116 if (type_spec && !is_cv_qualifier)
8117 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8118 /* A constructor declarator cannot follow a type-specifier. */
8119 if (type_spec)
8120 {
8121 constructor_possible_p = false;
8122 found_decl_spec = true;
8123 }
8124 }
8125
8126 /* If we still do not have a DECL_SPEC, then there are no more
8127 decl-specifiers. */
8128 if (!found_decl_spec)
8129 break;
8130
8131 decl_specs->any_specifiers_p = true;
8132 /* After we see one decl-specifier, further decl-specifiers are
8133 always optional. */
8134 flags |= CP_PARSER_FLAGS_OPTIONAL;
8135 }
8136
8137 cp_parser_check_decl_spec (decl_specs);
8138
8139 /* Don't allow a friend specifier with a class definition. */
8140 if (decl_specs->specs[(int) ds_friend] != 0
8141 && (*declares_class_or_enum & 2))
8142 error ("class definition may not be declared a friend");
8143 }
8144
8145 /* Parse an (optional) storage-class-specifier.
8146
8147 storage-class-specifier:
8148 auto
8149 register
8150 static
8151 extern
8152 mutable
8153
8154 GNU Extension:
8155
8156 storage-class-specifier:
8157 thread
8158
8159 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8160
8161 static tree
8162 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8163 {
8164 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8165 {
8166 case RID_AUTO:
8167 case RID_REGISTER:
8168 case RID_STATIC:
8169 case RID_EXTERN:
8170 case RID_MUTABLE:
8171 case RID_THREAD:
8172 /* Consume the token. */
8173 return cp_lexer_consume_token (parser->lexer)->u.value;
8174
8175 default:
8176 return NULL_TREE;
8177 }
8178 }
8179
8180 /* Parse an (optional) function-specifier.
8181
8182 function-specifier:
8183 inline
8184 virtual
8185 explicit
8186
8187 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8188 Updates DECL_SPECS, if it is non-NULL. */
8189
8190 static tree
8191 cp_parser_function_specifier_opt (cp_parser* parser,
8192 cp_decl_specifier_seq *decl_specs)
8193 {
8194 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8195 {
8196 case RID_INLINE:
8197 if (decl_specs)
8198 ++decl_specs->specs[(int) ds_inline];
8199 break;
8200
8201 case RID_VIRTUAL:
8202 /* 14.5.2.3 [temp.mem]
8203
8204 A member function template shall not be virtual. */
8205 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8206 error ("templates may not be %<virtual%>");
8207 else if (decl_specs)
8208 ++decl_specs->specs[(int) ds_virtual];
8209 break;
8210
8211 case RID_EXPLICIT:
8212 if (decl_specs)
8213 ++decl_specs->specs[(int) ds_explicit];
8214 break;
8215
8216 default:
8217 return NULL_TREE;
8218 }
8219
8220 /* Consume the token. */
8221 return cp_lexer_consume_token (parser->lexer)->u.value;
8222 }
8223
8224 /* Parse a linkage-specification.
8225
8226 linkage-specification:
8227 extern string-literal { declaration-seq [opt] }
8228 extern string-literal declaration */
8229
8230 static void
8231 cp_parser_linkage_specification (cp_parser* parser)
8232 {
8233 tree linkage;
8234
8235 /* Look for the `extern' keyword. */
8236 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8237
8238 /* Look for the string-literal. */
8239 linkage = cp_parser_string_literal (parser, false, false);
8240
8241 /* Transform the literal into an identifier. If the literal is a
8242 wide-character string, or contains embedded NULs, then we can't
8243 handle it as the user wants. */
8244 if (strlen (TREE_STRING_POINTER (linkage))
8245 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8246 {
8247 cp_parser_error (parser, "invalid linkage-specification");
8248 /* Assume C++ linkage. */
8249 linkage = lang_name_cplusplus;
8250 }
8251 else
8252 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8253
8254 /* We're now using the new linkage. */
8255 push_lang_context (linkage);
8256
8257 /* If the next token is a `{', then we're using the first
8258 production. */
8259 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8260 {
8261 /* Consume the `{' token. */
8262 cp_lexer_consume_token (parser->lexer);
8263 /* Parse the declarations. */
8264 cp_parser_declaration_seq_opt (parser);
8265 /* Look for the closing `}'. */
8266 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8267 }
8268 /* Otherwise, there's just one declaration. */
8269 else
8270 {
8271 bool saved_in_unbraced_linkage_specification_p;
8272
8273 saved_in_unbraced_linkage_specification_p
8274 = parser->in_unbraced_linkage_specification_p;
8275 parser->in_unbraced_linkage_specification_p = true;
8276 cp_parser_declaration (parser);
8277 parser->in_unbraced_linkage_specification_p
8278 = saved_in_unbraced_linkage_specification_p;
8279 }
8280
8281 /* We're done with the linkage-specification. */
8282 pop_lang_context ();
8283 }
8284
8285 /* Parse a static_assert-declaration.
8286
8287 static_assert-declaration:
8288 static_assert ( constant-expression , string-literal ) ;
8289
8290 If MEMBER_P, this static_assert is a class member. */
8291
8292 static void
8293 cp_parser_static_assert(cp_parser *parser, bool member_p)
8294 {
8295 tree condition;
8296 tree message;
8297 cp_token *token;
8298 location_t saved_loc;
8299
8300 /* Peek at the `static_assert' token so we can keep track of exactly
8301 where the static assertion started. */
8302 token = cp_lexer_peek_token (parser->lexer);
8303 saved_loc = token->location;
8304
8305 /* Look for the `static_assert' keyword. */
8306 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8307 "`static_assert'"))
8308 return;
8309
8310 /* We know we are in a static assertion; commit to any tentative
8311 parse. */
8312 if (cp_parser_parsing_tentatively (parser))
8313 cp_parser_commit_to_tentative_parse (parser);
8314
8315 /* Parse the `(' starting the static assertion condition. */
8316 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8317
8318 /* Parse the constant-expression. */
8319 condition =
8320 cp_parser_constant_expression (parser,
8321 /*allow_non_constant_p=*/false,
8322 /*non_constant_p=*/NULL);
8323
8324 /* Parse the separating `,'. */
8325 cp_parser_require (parser, CPP_COMMA, "`,'");
8326
8327 /* Parse the string-literal message. */
8328 message = cp_parser_string_literal (parser,
8329 /*translate=*/false,
8330 /*wide_ok=*/true);
8331
8332 /* A `)' completes the static assertion. */
8333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8334 cp_parser_skip_to_closing_parenthesis (parser,
8335 /*recovering=*/true,
8336 /*or_comma=*/false,
8337 /*consume_paren=*/true);
8338
8339 /* A semicolon terminates the declaration. */
8340 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8341
8342 /* Complete the static assertion, which may mean either processing
8343 the static assert now or saving it for template instantiation. */
8344 finish_static_assert (condition, message, saved_loc, member_p);
8345 }
8346
8347 /* Special member functions [gram.special] */
8348
8349 /* Parse a conversion-function-id.
8350
8351 conversion-function-id:
8352 operator conversion-type-id
8353
8354 Returns an IDENTIFIER_NODE representing the operator. */
8355
8356 static tree
8357 cp_parser_conversion_function_id (cp_parser* parser)
8358 {
8359 tree type;
8360 tree saved_scope;
8361 tree saved_qualifying_scope;
8362 tree saved_object_scope;
8363 tree pushed_scope = NULL_TREE;
8364
8365 /* Look for the `operator' token. */
8366 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8367 return error_mark_node;
8368 /* When we parse the conversion-type-id, the current scope will be
8369 reset. However, we need that information in able to look up the
8370 conversion function later, so we save it here. */
8371 saved_scope = parser->scope;
8372 saved_qualifying_scope = parser->qualifying_scope;
8373 saved_object_scope = parser->object_scope;
8374 /* We must enter the scope of the class so that the names of
8375 entities declared within the class are available in the
8376 conversion-type-id. For example, consider:
8377
8378 struct S {
8379 typedef int I;
8380 operator I();
8381 };
8382
8383 S::operator I() { ... }
8384
8385 In order to see that `I' is a type-name in the definition, we
8386 must be in the scope of `S'. */
8387 if (saved_scope)
8388 pushed_scope = push_scope (saved_scope);
8389 /* Parse the conversion-type-id. */
8390 type = cp_parser_conversion_type_id (parser);
8391 /* Leave the scope of the class, if any. */
8392 if (pushed_scope)
8393 pop_scope (pushed_scope);
8394 /* Restore the saved scope. */
8395 parser->scope = saved_scope;
8396 parser->qualifying_scope = saved_qualifying_scope;
8397 parser->object_scope = saved_object_scope;
8398 /* If the TYPE is invalid, indicate failure. */
8399 if (type == error_mark_node)
8400 return error_mark_node;
8401 return mangle_conv_op_name_for_type (type);
8402 }
8403
8404 /* Parse a conversion-type-id:
8405
8406 conversion-type-id:
8407 type-specifier-seq conversion-declarator [opt]
8408
8409 Returns the TYPE specified. */
8410
8411 static tree
8412 cp_parser_conversion_type_id (cp_parser* parser)
8413 {
8414 tree attributes;
8415 cp_decl_specifier_seq type_specifiers;
8416 cp_declarator *declarator;
8417 tree type_specified;
8418
8419 /* Parse the attributes. */
8420 attributes = cp_parser_attributes_opt (parser);
8421 /* Parse the type-specifiers. */
8422 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8423 &type_specifiers);
8424 /* If that didn't work, stop. */
8425 if (type_specifiers.type == error_mark_node)
8426 return error_mark_node;
8427 /* Parse the conversion-declarator. */
8428 declarator = cp_parser_conversion_declarator_opt (parser);
8429
8430 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8431 /*initialized=*/0, &attributes);
8432 if (attributes)
8433 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8434 return type_specified;
8435 }
8436
8437 /* Parse an (optional) conversion-declarator.
8438
8439 conversion-declarator:
8440 ptr-operator conversion-declarator [opt]
8441
8442 */
8443
8444 static cp_declarator *
8445 cp_parser_conversion_declarator_opt (cp_parser* parser)
8446 {
8447 enum tree_code code;
8448 tree class_type;
8449 cp_cv_quals cv_quals;
8450
8451 /* We don't know if there's a ptr-operator next, or not. */
8452 cp_parser_parse_tentatively (parser);
8453 /* Try the ptr-operator. */
8454 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8455 /* If it worked, look for more conversion-declarators. */
8456 if (cp_parser_parse_definitely (parser))
8457 {
8458 cp_declarator *declarator;
8459
8460 /* Parse another optional declarator. */
8461 declarator = cp_parser_conversion_declarator_opt (parser);
8462
8463 /* Create the representation of the declarator. */
8464 if (class_type)
8465 declarator = make_ptrmem_declarator (cv_quals, class_type,
8466 declarator);
8467 else if (code == INDIRECT_REF)
8468 declarator = make_pointer_declarator (cv_quals, declarator);
8469 else
8470 declarator = make_reference_declarator (cv_quals, declarator);
8471
8472 return declarator;
8473 }
8474
8475 return NULL;
8476 }
8477
8478 /* Parse an (optional) ctor-initializer.
8479
8480 ctor-initializer:
8481 : mem-initializer-list
8482
8483 Returns TRUE iff the ctor-initializer was actually present. */
8484
8485 static bool
8486 cp_parser_ctor_initializer_opt (cp_parser* parser)
8487 {
8488 /* If the next token is not a `:', then there is no
8489 ctor-initializer. */
8490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8491 {
8492 /* Do default initialization of any bases and members. */
8493 if (DECL_CONSTRUCTOR_P (current_function_decl))
8494 finish_mem_initializers (NULL_TREE);
8495
8496 return false;
8497 }
8498
8499 /* Consume the `:' token. */
8500 cp_lexer_consume_token (parser->lexer);
8501 /* And the mem-initializer-list. */
8502 cp_parser_mem_initializer_list (parser);
8503
8504 return true;
8505 }
8506
8507 /* Parse a mem-initializer-list.
8508
8509 mem-initializer-list:
8510 mem-initializer ... [opt]
8511 mem-initializer ... [opt] , mem-initializer-list */
8512
8513 static void
8514 cp_parser_mem_initializer_list (cp_parser* parser)
8515 {
8516 tree mem_initializer_list = NULL_TREE;
8517
8518 /* Let the semantic analysis code know that we are starting the
8519 mem-initializer-list. */
8520 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8521 error ("only constructors take base initializers");
8522
8523 /* Loop through the list. */
8524 while (true)
8525 {
8526 tree mem_initializer;
8527
8528 /* Parse the mem-initializer. */
8529 mem_initializer = cp_parser_mem_initializer (parser);
8530 /* If the next token is a `...', we're expanding member initializers. */
8531 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8532 {
8533 /* Consume the `...'. */
8534 cp_lexer_consume_token (parser->lexer);
8535
8536 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8537 can be expanded but members cannot. */
8538 if (mem_initializer != error_mark_node
8539 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8540 {
8541 error ("cannot expand initializer for member %<%D%>",
8542 TREE_PURPOSE (mem_initializer));
8543 mem_initializer = error_mark_node;
8544 }
8545
8546 /* Construct the pack expansion type. */
8547 if (mem_initializer != error_mark_node)
8548 mem_initializer = make_pack_expansion (mem_initializer);
8549 }
8550 /* Add it to the list, unless it was erroneous. */
8551 if (mem_initializer != error_mark_node)
8552 {
8553 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8554 mem_initializer_list = mem_initializer;
8555 }
8556 /* If the next token is not a `,', we're done. */
8557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8558 break;
8559 /* Consume the `,' token. */
8560 cp_lexer_consume_token (parser->lexer);
8561 }
8562
8563 /* Perform semantic analysis. */
8564 if (DECL_CONSTRUCTOR_P (current_function_decl))
8565 finish_mem_initializers (mem_initializer_list);
8566 }
8567
8568 /* Parse a mem-initializer.
8569
8570 mem-initializer:
8571 mem-initializer-id ( expression-list [opt] )
8572
8573 GNU extension:
8574
8575 mem-initializer:
8576 ( expression-list [opt] )
8577
8578 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8579 class) or FIELD_DECL (for a non-static data member) to initialize;
8580 the TREE_VALUE is the expression-list. An empty initialization
8581 list is represented by void_list_node. */
8582
8583 static tree
8584 cp_parser_mem_initializer (cp_parser* parser)
8585 {
8586 tree mem_initializer_id;
8587 tree expression_list;
8588 tree member;
8589
8590 /* Find out what is being initialized. */
8591 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8592 {
8593 pedwarn ("anachronistic old-style base class initializer");
8594 mem_initializer_id = NULL_TREE;
8595 }
8596 else
8597 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8598 member = expand_member_init (mem_initializer_id);
8599 if (member && !DECL_P (member))
8600 in_base_initializer = 1;
8601
8602 expression_list
8603 = cp_parser_parenthesized_expression_list (parser, false,
8604 /*cast_p=*/false,
8605 /*allow_expansion_p=*/true,
8606 /*non_constant_p=*/NULL);
8607 if (expression_list == error_mark_node)
8608 return error_mark_node;
8609 if (!expression_list)
8610 expression_list = void_type_node;
8611
8612 in_base_initializer = 0;
8613
8614 return member ? build_tree_list (member, expression_list) : error_mark_node;
8615 }
8616
8617 /* Parse a mem-initializer-id.
8618
8619 mem-initializer-id:
8620 :: [opt] nested-name-specifier [opt] class-name
8621 identifier
8622
8623 Returns a TYPE indicating the class to be initializer for the first
8624 production. Returns an IDENTIFIER_NODE indicating the data member
8625 to be initialized for the second production. */
8626
8627 static tree
8628 cp_parser_mem_initializer_id (cp_parser* parser)
8629 {
8630 bool global_scope_p;
8631 bool nested_name_specifier_p;
8632 bool template_p = false;
8633 tree id;
8634
8635 /* `typename' is not allowed in this context ([temp.res]). */
8636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8637 {
8638 error ("keyword %<typename%> not allowed in this context (a qualified "
8639 "member initializer is implicitly a type)");
8640 cp_lexer_consume_token (parser->lexer);
8641 }
8642 /* Look for the optional `::' operator. */
8643 global_scope_p
8644 = (cp_parser_global_scope_opt (parser,
8645 /*current_scope_valid_p=*/false)
8646 != NULL_TREE);
8647 /* Look for the optional nested-name-specifier. The simplest way to
8648 implement:
8649
8650 [temp.res]
8651
8652 The keyword `typename' is not permitted in a base-specifier or
8653 mem-initializer; in these contexts a qualified name that
8654 depends on a template-parameter is implicitly assumed to be a
8655 type name.
8656
8657 is to assume that we have seen the `typename' keyword at this
8658 point. */
8659 nested_name_specifier_p
8660 = (cp_parser_nested_name_specifier_opt (parser,
8661 /*typename_keyword_p=*/true,
8662 /*check_dependency_p=*/true,
8663 /*type_p=*/true,
8664 /*is_declaration=*/true)
8665 != NULL_TREE);
8666 if (nested_name_specifier_p)
8667 template_p = cp_parser_optional_template_keyword (parser);
8668 /* If there is a `::' operator or a nested-name-specifier, then we
8669 are definitely looking for a class-name. */
8670 if (global_scope_p || nested_name_specifier_p)
8671 return cp_parser_class_name (parser,
8672 /*typename_keyword_p=*/true,
8673 /*template_keyword_p=*/template_p,
8674 none_type,
8675 /*check_dependency_p=*/true,
8676 /*class_head_p=*/false,
8677 /*is_declaration=*/true);
8678 /* Otherwise, we could also be looking for an ordinary identifier. */
8679 cp_parser_parse_tentatively (parser);
8680 /* Try a class-name. */
8681 id = cp_parser_class_name (parser,
8682 /*typename_keyword_p=*/true,
8683 /*template_keyword_p=*/false,
8684 none_type,
8685 /*check_dependency_p=*/true,
8686 /*class_head_p=*/false,
8687 /*is_declaration=*/true);
8688 /* If we found one, we're done. */
8689 if (cp_parser_parse_definitely (parser))
8690 return id;
8691 /* Otherwise, look for an ordinary identifier. */
8692 return cp_parser_identifier (parser);
8693 }
8694
8695 /* Overloading [gram.over] */
8696
8697 /* Parse an operator-function-id.
8698
8699 operator-function-id:
8700 operator operator
8701
8702 Returns an IDENTIFIER_NODE for the operator which is a
8703 human-readable spelling of the identifier, e.g., `operator +'. */
8704
8705 static tree
8706 cp_parser_operator_function_id (cp_parser* parser)
8707 {
8708 /* Look for the `operator' keyword. */
8709 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8710 return error_mark_node;
8711 /* And then the name of the operator itself. */
8712 return cp_parser_operator (parser);
8713 }
8714
8715 /* Parse an operator.
8716
8717 operator:
8718 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8719 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8720 || ++ -- , ->* -> () []
8721
8722 GNU Extensions:
8723
8724 operator:
8725 <? >? <?= >?=
8726
8727 Returns an IDENTIFIER_NODE for the operator which is a
8728 human-readable spelling of the identifier, e.g., `operator +'. */
8729
8730 static tree
8731 cp_parser_operator (cp_parser* parser)
8732 {
8733 tree id = NULL_TREE;
8734 cp_token *token;
8735
8736 /* Peek at the next token. */
8737 token = cp_lexer_peek_token (parser->lexer);
8738 /* Figure out which operator we have. */
8739 switch (token->type)
8740 {
8741 case CPP_KEYWORD:
8742 {
8743 enum tree_code op;
8744
8745 /* The keyword should be either `new' or `delete'. */
8746 if (token->keyword == RID_NEW)
8747 op = NEW_EXPR;
8748 else if (token->keyword == RID_DELETE)
8749 op = DELETE_EXPR;
8750 else
8751 break;
8752
8753 /* Consume the `new' or `delete' token. */
8754 cp_lexer_consume_token (parser->lexer);
8755
8756 /* Peek at the next token. */
8757 token = cp_lexer_peek_token (parser->lexer);
8758 /* If it's a `[' token then this is the array variant of the
8759 operator. */
8760 if (token->type == CPP_OPEN_SQUARE)
8761 {
8762 /* Consume the `[' token. */
8763 cp_lexer_consume_token (parser->lexer);
8764 /* Look for the `]' token. */
8765 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8766 id = ansi_opname (op == NEW_EXPR
8767 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8768 }
8769 /* Otherwise, we have the non-array variant. */
8770 else
8771 id = ansi_opname (op);
8772
8773 return id;
8774 }
8775
8776 case CPP_PLUS:
8777 id = ansi_opname (PLUS_EXPR);
8778 break;
8779
8780 case CPP_MINUS:
8781 id = ansi_opname (MINUS_EXPR);
8782 break;
8783
8784 case CPP_MULT:
8785 id = ansi_opname (MULT_EXPR);
8786 break;
8787
8788 case CPP_DIV:
8789 id = ansi_opname (TRUNC_DIV_EXPR);
8790 break;
8791
8792 case CPP_MOD:
8793 id = ansi_opname (TRUNC_MOD_EXPR);
8794 break;
8795
8796 case CPP_XOR:
8797 id = ansi_opname (BIT_XOR_EXPR);
8798 break;
8799
8800 case CPP_AND:
8801 id = ansi_opname (BIT_AND_EXPR);
8802 break;
8803
8804 case CPP_OR:
8805 id = ansi_opname (BIT_IOR_EXPR);
8806 break;
8807
8808 case CPP_COMPL:
8809 id = ansi_opname (BIT_NOT_EXPR);
8810 break;
8811
8812 case CPP_NOT:
8813 id = ansi_opname (TRUTH_NOT_EXPR);
8814 break;
8815
8816 case CPP_EQ:
8817 id = ansi_assopname (NOP_EXPR);
8818 break;
8819
8820 case CPP_LESS:
8821 id = ansi_opname (LT_EXPR);
8822 break;
8823
8824 case CPP_GREATER:
8825 id = ansi_opname (GT_EXPR);
8826 break;
8827
8828 case CPP_PLUS_EQ:
8829 id = ansi_assopname (PLUS_EXPR);
8830 break;
8831
8832 case CPP_MINUS_EQ:
8833 id = ansi_assopname (MINUS_EXPR);
8834 break;
8835
8836 case CPP_MULT_EQ:
8837 id = ansi_assopname (MULT_EXPR);
8838 break;
8839
8840 case CPP_DIV_EQ:
8841 id = ansi_assopname (TRUNC_DIV_EXPR);
8842 break;
8843
8844 case CPP_MOD_EQ:
8845 id = ansi_assopname (TRUNC_MOD_EXPR);
8846 break;
8847
8848 case CPP_XOR_EQ:
8849 id = ansi_assopname (BIT_XOR_EXPR);
8850 break;
8851
8852 case CPP_AND_EQ:
8853 id = ansi_assopname (BIT_AND_EXPR);
8854 break;
8855
8856 case CPP_OR_EQ:
8857 id = ansi_assopname (BIT_IOR_EXPR);
8858 break;
8859
8860 case CPP_LSHIFT:
8861 id = ansi_opname (LSHIFT_EXPR);
8862 break;
8863
8864 case CPP_RSHIFT:
8865 id = ansi_opname (RSHIFT_EXPR);
8866 break;
8867
8868 case CPP_LSHIFT_EQ:
8869 id = ansi_assopname (LSHIFT_EXPR);
8870 break;
8871
8872 case CPP_RSHIFT_EQ:
8873 id = ansi_assopname (RSHIFT_EXPR);
8874 break;
8875
8876 case CPP_EQ_EQ:
8877 id = ansi_opname (EQ_EXPR);
8878 break;
8879
8880 case CPP_NOT_EQ:
8881 id = ansi_opname (NE_EXPR);
8882 break;
8883
8884 case CPP_LESS_EQ:
8885 id = ansi_opname (LE_EXPR);
8886 break;
8887
8888 case CPP_GREATER_EQ:
8889 id = ansi_opname (GE_EXPR);
8890 break;
8891
8892 case CPP_AND_AND:
8893 id = ansi_opname (TRUTH_ANDIF_EXPR);
8894 break;
8895
8896 case CPP_OR_OR:
8897 id = ansi_opname (TRUTH_ORIF_EXPR);
8898 break;
8899
8900 case CPP_PLUS_PLUS:
8901 id = ansi_opname (POSTINCREMENT_EXPR);
8902 break;
8903
8904 case CPP_MINUS_MINUS:
8905 id = ansi_opname (PREDECREMENT_EXPR);
8906 break;
8907
8908 case CPP_COMMA:
8909 id = ansi_opname (COMPOUND_EXPR);
8910 break;
8911
8912 case CPP_DEREF_STAR:
8913 id = ansi_opname (MEMBER_REF);
8914 break;
8915
8916 case CPP_DEREF:
8917 id = ansi_opname (COMPONENT_REF);
8918 break;
8919
8920 case CPP_OPEN_PAREN:
8921 /* Consume the `('. */
8922 cp_lexer_consume_token (parser->lexer);
8923 /* Look for the matching `)'. */
8924 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8925 return ansi_opname (CALL_EXPR);
8926
8927 case CPP_OPEN_SQUARE:
8928 /* Consume the `['. */
8929 cp_lexer_consume_token (parser->lexer);
8930 /* Look for the matching `]'. */
8931 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8932 return ansi_opname (ARRAY_REF);
8933
8934 default:
8935 /* Anything else is an error. */
8936 break;
8937 }
8938
8939 /* If we have selected an identifier, we need to consume the
8940 operator token. */
8941 if (id)
8942 cp_lexer_consume_token (parser->lexer);
8943 /* Otherwise, no valid operator name was present. */
8944 else
8945 {
8946 cp_parser_error (parser, "expected operator");
8947 id = error_mark_node;
8948 }
8949
8950 return id;
8951 }
8952
8953 /* Parse a template-declaration.
8954
8955 template-declaration:
8956 export [opt] template < template-parameter-list > declaration
8957
8958 If MEMBER_P is TRUE, this template-declaration occurs within a
8959 class-specifier.
8960
8961 The grammar rule given by the standard isn't correct. What
8962 is really meant is:
8963
8964 template-declaration:
8965 export [opt] template-parameter-list-seq
8966 decl-specifier-seq [opt] init-declarator [opt] ;
8967 export [opt] template-parameter-list-seq
8968 function-definition
8969
8970 template-parameter-list-seq:
8971 template-parameter-list-seq [opt]
8972 template < template-parameter-list > */
8973
8974 static void
8975 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8976 {
8977 /* Check for `export'. */
8978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8979 {
8980 /* Consume the `export' token. */
8981 cp_lexer_consume_token (parser->lexer);
8982 /* Warn that we do not support `export'. */
8983 warning (0, "keyword %<export%> not implemented, and will be ignored");
8984 }
8985
8986 cp_parser_template_declaration_after_export (parser, member_p);
8987 }
8988
8989 /* Parse a template-parameter-list.
8990
8991 template-parameter-list:
8992 template-parameter
8993 template-parameter-list , template-parameter
8994
8995 Returns a TREE_LIST. Each node represents a template parameter.
8996 The nodes are connected via their TREE_CHAINs. */
8997
8998 static tree
8999 cp_parser_template_parameter_list (cp_parser* parser)
9000 {
9001 tree parameter_list = NULL_TREE;
9002
9003 begin_template_parm_list ();
9004 while (true)
9005 {
9006 tree parameter;
9007 cp_token *token;
9008 bool is_non_type;
9009 bool is_parameter_pack;
9010
9011 /* Parse the template-parameter. */
9012 parameter = cp_parser_template_parameter (parser,
9013 &is_non_type,
9014 &is_parameter_pack);
9015 /* Add it to the list. */
9016 if (parameter != error_mark_node)
9017 parameter_list = process_template_parm (parameter_list,
9018 parameter,
9019 is_non_type,
9020 is_parameter_pack);
9021 else
9022 {
9023 tree err_parm = build_tree_list (parameter, parameter);
9024 TREE_VALUE (err_parm) = error_mark_node;
9025 parameter_list = chainon (parameter_list, err_parm);
9026 }
9027
9028 /* Peek at the next token. */
9029 token = cp_lexer_peek_token (parser->lexer);
9030 /* If it's not a `,', we're done. */
9031 if (token->type != CPP_COMMA)
9032 break;
9033 /* Otherwise, consume the `,' token. */
9034 cp_lexer_consume_token (parser->lexer);
9035 }
9036
9037 return end_template_parm_list (parameter_list);
9038 }
9039
9040 /* Parse a template-parameter.
9041
9042 template-parameter:
9043 type-parameter
9044 parameter-declaration
9045
9046 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9047 the parameter. The TREE_PURPOSE is the default value, if any.
9048 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9049 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9050 set to true iff this parameter is a parameter pack. */
9051
9052 static tree
9053 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9054 bool *is_parameter_pack)
9055 {
9056 cp_token *token;
9057 cp_parameter_declarator *parameter_declarator;
9058 tree parm;
9059
9060 /* Assume it is a type parameter or a template parameter. */
9061 *is_non_type = false;
9062 /* Assume it not a parameter pack. */
9063 *is_parameter_pack = false;
9064 /* Peek at the next token. */
9065 token = cp_lexer_peek_token (parser->lexer);
9066 /* If it is `class' or `template', we have a type-parameter. */
9067 if (token->keyword == RID_TEMPLATE)
9068 return cp_parser_type_parameter (parser, is_parameter_pack);
9069 /* If it is `class' or `typename' we do not know yet whether it is a
9070 type parameter or a non-type parameter. Consider:
9071
9072 template <typename T, typename T::X X> ...
9073
9074 or:
9075
9076 template <class C, class D*> ...
9077
9078 Here, the first parameter is a type parameter, and the second is
9079 a non-type parameter. We can tell by looking at the token after
9080 the identifier -- if it is a `,', `=', or `>' then we have a type
9081 parameter. */
9082 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9083 {
9084 /* Peek at the token after `class' or `typename'. */
9085 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9086 /* If it's an ellipsis, we have a template type parameter
9087 pack. */
9088 if (token->type == CPP_ELLIPSIS)
9089 return cp_parser_type_parameter (parser, is_parameter_pack);
9090 /* If it's an identifier, skip it. */
9091 if (token->type == CPP_NAME)
9092 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9093 /* Now, see if the token looks like the end of a template
9094 parameter. */
9095 if (token->type == CPP_COMMA
9096 || token->type == CPP_EQ
9097 || token->type == CPP_GREATER)
9098 return cp_parser_type_parameter (parser, is_parameter_pack);
9099 }
9100
9101 /* Otherwise, it is a non-type parameter.
9102
9103 [temp.param]
9104
9105 When parsing a default template-argument for a non-type
9106 template-parameter, the first non-nested `>' is taken as the end
9107 of the template parameter-list rather than a greater-than
9108 operator. */
9109 *is_non_type = true;
9110 parameter_declarator
9111 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9112 /*parenthesized_p=*/NULL);
9113
9114 /* If the parameter declaration is marked as a parameter pack, set
9115 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9116 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9117 grokdeclarator. */
9118 if (parameter_declarator
9119 && parameter_declarator->declarator
9120 && parameter_declarator->declarator->parameter_pack_p)
9121 {
9122 *is_parameter_pack = true;
9123 parameter_declarator->declarator->parameter_pack_p = false;
9124 }
9125
9126 /* If the next token is an ellipsis, and we don't already have it
9127 marked as a parameter pack, then we have a parameter pack (that
9128 has no declarator); */
9129 if (!*is_parameter_pack
9130 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9131 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9132 {
9133 /* Consume the `...'. */
9134 cp_lexer_consume_token (parser->lexer);
9135 maybe_warn_variadic_templates ();
9136
9137 *is_parameter_pack = true;
9138 }
9139
9140 parm = grokdeclarator (parameter_declarator->declarator,
9141 &parameter_declarator->decl_specifiers,
9142 PARM, /*initialized=*/0,
9143 /*attrlist=*/NULL);
9144 if (parm == error_mark_node)
9145 return error_mark_node;
9146
9147 return build_tree_list (parameter_declarator->default_argument, parm);
9148 }
9149
9150 /* Parse a type-parameter.
9151
9152 type-parameter:
9153 class identifier [opt]
9154 class identifier [opt] = type-id
9155 typename identifier [opt]
9156 typename identifier [opt] = type-id
9157 template < template-parameter-list > class identifier [opt]
9158 template < template-parameter-list > class identifier [opt]
9159 = id-expression
9160
9161 GNU Extension (variadic templates):
9162
9163 type-parameter:
9164 class ... identifier [opt]
9165 typename ... identifier [opt]
9166
9167 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9168 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9169 the declaration of the parameter.
9170
9171 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9172
9173 static tree
9174 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9175 {
9176 cp_token *token;
9177 tree parameter;
9178
9179 /* Look for a keyword to tell us what kind of parameter this is. */
9180 token = cp_parser_require (parser, CPP_KEYWORD,
9181 "`class', `typename', or `template'");
9182 if (!token)
9183 return error_mark_node;
9184
9185 switch (token->keyword)
9186 {
9187 case RID_CLASS:
9188 case RID_TYPENAME:
9189 {
9190 tree identifier;
9191 tree default_argument;
9192
9193 /* If the next token is an ellipsis, we have a template
9194 argument pack. */
9195 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9196 {
9197 /* Consume the `...' token. */
9198 cp_lexer_consume_token (parser->lexer);
9199 maybe_warn_variadic_templates ();
9200
9201 *is_parameter_pack = true;
9202 }
9203
9204 /* If the next token is an identifier, then it names the
9205 parameter. */
9206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9207 identifier = cp_parser_identifier (parser);
9208 else
9209 identifier = NULL_TREE;
9210
9211 /* Create the parameter. */
9212 parameter = finish_template_type_parm (class_type_node, identifier);
9213
9214 /* If the next token is an `=', we have a default argument. */
9215 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9216 {
9217 /* Consume the `=' token. */
9218 cp_lexer_consume_token (parser->lexer);
9219 /* Parse the default-argument. */
9220 push_deferring_access_checks (dk_no_deferred);
9221 default_argument = cp_parser_type_id (parser);
9222
9223 /* Template parameter packs cannot have default
9224 arguments. */
9225 if (*is_parameter_pack)
9226 {
9227 if (identifier)
9228 error ("template parameter pack %qD cannot have a default argument",
9229 identifier);
9230 else
9231 error ("template parameter packs cannot have default arguments");
9232 default_argument = NULL_TREE;
9233 }
9234 pop_deferring_access_checks ();
9235 }
9236 else
9237 default_argument = NULL_TREE;
9238
9239 /* Create the combined representation of the parameter and the
9240 default argument. */
9241 parameter = build_tree_list (default_argument, parameter);
9242 }
9243 break;
9244
9245 case RID_TEMPLATE:
9246 {
9247 tree parameter_list;
9248 tree identifier;
9249 tree default_argument;
9250
9251 /* Look for the `<'. */
9252 cp_parser_require (parser, CPP_LESS, "`<'");
9253 /* Parse the template-parameter-list. */
9254 parameter_list = cp_parser_template_parameter_list (parser);
9255 /* Look for the `>'. */
9256 cp_parser_require (parser, CPP_GREATER, "`>'");
9257 /* Look for the `class' keyword. */
9258 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9259 /* If the next token is an ellipsis, we have a template
9260 argument pack. */
9261 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9262 {
9263 /* Consume the `...' token. */
9264 cp_lexer_consume_token (parser->lexer);
9265 maybe_warn_variadic_templates ();
9266
9267 *is_parameter_pack = true;
9268 }
9269 /* If the next token is an `=', then there is a
9270 default-argument. If the next token is a `>', we are at
9271 the end of the parameter-list. If the next token is a `,',
9272 then we are at the end of this parameter. */
9273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9274 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9275 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9276 {
9277 identifier = cp_parser_identifier (parser);
9278 /* Treat invalid names as if the parameter were nameless. */
9279 if (identifier == error_mark_node)
9280 identifier = NULL_TREE;
9281 }
9282 else
9283 identifier = NULL_TREE;
9284
9285 /* Create the template parameter. */
9286 parameter = finish_template_template_parm (class_type_node,
9287 identifier);
9288
9289 /* If the next token is an `=', then there is a
9290 default-argument. */
9291 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9292 {
9293 bool is_template;
9294
9295 /* Consume the `='. */
9296 cp_lexer_consume_token (parser->lexer);
9297 /* Parse the id-expression. */
9298 push_deferring_access_checks (dk_no_deferred);
9299 default_argument
9300 = cp_parser_id_expression (parser,
9301 /*template_keyword_p=*/false,
9302 /*check_dependency_p=*/true,
9303 /*template_p=*/&is_template,
9304 /*declarator_p=*/false,
9305 /*optional_p=*/false);
9306 if (TREE_CODE (default_argument) == TYPE_DECL)
9307 /* If the id-expression was a template-id that refers to
9308 a template-class, we already have the declaration here,
9309 so no further lookup is needed. */
9310 ;
9311 else
9312 /* Look up the name. */
9313 default_argument
9314 = cp_parser_lookup_name (parser, default_argument,
9315 none_type,
9316 /*is_template=*/is_template,
9317 /*is_namespace=*/false,
9318 /*check_dependency=*/true,
9319 /*ambiguous_decls=*/NULL);
9320 /* See if the default argument is valid. */
9321 default_argument
9322 = check_template_template_default_arg (default_argument);
9323
9324 /* Template parameter packs cannot have default
9325 arguments. */
9326 if (*is_parameter_pack)
9327 {
9328 if (identifier)
9329 error ("template parameter pack %qD cannot have a default argument",
9330 identifier);
9331 else
9332 error ("template parameter packs cannot have default arguments");
9333 default_argument = NULL_TREE;
9334 }
9335 pop_deferring_access_checks ();
9336 }
9337 else
9338 default_argument = NULL_TREE;
9339
9340 /* Create the combined representation of the parameter and the
9341 default argument. */
9342 parameter = build_tree_list (default_argument, parameter);
9343 }
9344 break;
9345
9346 default:
9347 gcc_unreachable ();
9348 break;
9349 }
9350
9351 return parameter;
9352 }
9353
9354 /* Parse a template-id.
9355
9356 template-id:
9357 template-name < template-argument-list [opt] >
9358
9359 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9360 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9361 returned. Otherwise, if the template-name names a function, or set
9362 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9363 names a class, returns a TYPE_DECL for the specialization.
9364
9365 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9366 uninstantiated templates. */
9367
9368 static tree
9369 cp_parser_template_id (cp_parser *parser,
9370 bool template_keyword_p,
9371 bool check_dependency_p,
9372 bool is_declaration)
9373 {
9374 int i;
9375 tree template;
9376 tree arguments;
9377 tree template_id;
9378 cp_token_position start_of_id = 0;
9379 deferred_access_check *chk;
9380 VEC (deferred_access_check,gc) *access_check;
9381 cp_token *next_token, *next_token_2;
9382 bool is_identifier;
9383
9384 /* If the next token corresponds to a template-id, there is no need
9385 to reparse it. */
9386 next_token = cp_lexer_peek_token (parser->lexer);
9387 if (next_token->type == CPP_TEMPLATE_ID)
9388 {
9389 struct tree_check *check_value;
9390
9391 /* Get the stored value. */
9392 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9393 /* Perform any access checks that were deferred. */
9394 access_check = check_value->checks;
9395 if (access_check)
9396 {
9397 for (i = 0 ;
9398 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9399 ++i)
9400 {
9401 perform_or_defer_access_check (chk->binfo,
9402 chk->decl,
9403 chk->diag_decl);
9404 }
9405 }
9406 /* Return the stored value. */
9407 return check_value->value;
9408 }
9409
9410 /* Avoid performing name lookup if there is no possibility of
9411 finding a template-id. */
9412 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9413 || (next_token->type == CPP_NAME
9414 && !cp_parser_nth_token_starts_template_argument_list_p
9415 (parser, 2)))
9416 {
9417 cp_parser_error (parser, "expected template-id");
9418 return error_mark_node;
9419 }
9420
9421 /* Remember where the template-id starts. */
9422 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9423 start_of_id = cp_lexer_token_position (parser->lexer, false);
9424
9425 push_deferring_access_checks (dk_deferred);
9426
9427 /* Parse the template-name. */
9428 is_identifier = false;
9429 template = cp_parser_template_name (parser, template_keyword_p,
9430 check_dependency_p,
9431 is_declaration,
9432 &is_identifier);
9433 if (template == error_mark_node || is_identifier)
9434 {
9435 pop_deferring_access_checks ();
9436 return template;
9437 }
9438
9439 /* If we find the sequence `[:' after a template-name, it's probably
9440 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9441 parse correctly the argument list. */
9442 next_token = cp_lexer_peek_token (parser->lexer);
9443 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9444 if (next_token->type == CPP_OPEN_SQUARE
9445 && next_token->flags & DIGRAPH
9446 && next_token_2->type == CPP_COLON
9447 && !(next_token_2->flags & PREV_WHITE))
9448 {
9449 cp_parser_parse_tentatively (parser);
9450 /* Change `:' into `::'. */
9451 next_token_2->type = CPP_SCOPE;
9452 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9453 CPP_LESS. */
9454 cp_lexer_consume_token (parser->lexer);
9455 /* Parse the arguments. */
9456 arguments = cp_parser_enclosed_template_argument_list (parser);
9457 if (!cp_parser_parse_definitely (parser))
9458 {
9459 /* If we couldn't parse an argument list, then we revert our changes
9460 and return simply an error. Maybe this is not a template-id
9461 after all. */
9462 next_token_2->type = CPP_COLON;
9463 cp_parser_error (parser, "expected %<<%>");
9464 pop_deferring_access_checks ();
9465 return error_mark_node;
9466 }
9467 /* Otherwise, emit an error about the invalid digraph, but continue
9468 parsing because we got our argument list. */
9469 pedwarn ("%<<::%> cannot begin a template-argument list");
9470 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9471 "between %<<%> and %<::%>");
9472 if (!flag_permissive)
9473 {
9474 static bool hint;
9475 if (!hint)
9476 {
9477 inform ("(if you use -fpermissive G++ will accept your code)");
9478 hint = true;
9479 }
9480 }
9481 }
9482 else
9483 {
9484 /* Look for the `<' that starts the template-argument-list. */
9485 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9486 {
9487 pop_deferring_access_checks ();
9488 return error_mark_node;
9489 }
9490 /* Parse the arguments. */
9491 arguments = cp_parser_enclosed_template_argument_list (parser);
9492 }
9493
9494 /* Build a representation of the specialization. */
9495 if (TREE_CODE (template) == IDENTIFIER_NODE)
9496 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9497 else if (DECL_CLASS_TEMPLATE_P (template)
9498 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9499 {
9500 bool entering_scope;
9501 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9502 template (rather than some instantiation thereof) only if
9503 is not nested within some other construct. For example, in
9504 "template <typename T> void f(T) { A<T>::", A<T> is just an
9505 instantiation of A. */
9506 entering_scope = (template_parm_scope_p ()
9507 && cp_lexer_next_token_is (parser->lexer,
9508 CPP_SCOPE));
9509 template_id
9510 = finish_template_type (template, arguments, entering_scope);
9511 }
9512 else
9513 {
9514 /* If it's not a class-template or a template-template, it should be
9515 a function-template. */
9516 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9517 || TREE_CODE (template) == OVERLOAD
9518 || BASELINK_P (template)));
9519
9520 template_id = lookup_template_function (template, arguments);
9521 }
9522
9523 /* If parsing tentatively, replace the sequence of tokens that makes
9524 up the template-id with a CPP_TEMPLATE_ID token. That way,
9525 should we re-parse the token stream, we will not have to repeat
9526 the effort required to do the parse, nor will we issue duplicate
9527 error messages about problems during instantiation of the
9528 template. */
9529 if (start_of_id)
9530 {
9531 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9532
9533 /* Reset the contents of the START_OF_ID token. */
9534 token->type = CPP_TEMPLATE_ID;
9535 /* Retrieve any deferred checks. Do not pop this access checks yet
9536 so the memory will not be reclaimed during token replacing below. */
9537 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9538 token->u.tree_check_value->value = template_id;
9539 token->u.tree_check_value->checks = get_deferred_access_checks ();
9540 token->keyword = RID_MAX;
9541
9542 /* Purge all subsequent tokens. */
9543 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9544
9545 /* ??? Can we actually assume that, if template_id ==
9546 error_mark_node, we will have issued a diagnostic to the
9547 user, as opposed to simply marking the tentative parse as
9548 failed? */
9549 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9550 error ("parse error in template argument list");
9551 }
9552
9553 pop_deferring_access_checks ();
9554 return template_id;
9555 }
9556
9557 /* Parse a template-name.
9558
9559 template-name:
9560 identifier
9561
9562 The standard should actually say:
9563
9564 template-name:
9565 identifier
9566 operator-function-id
9567
9568 A defect report has been filed about this issue.
9569
9570 A conversion-function-id cannot be a template name because they cannot
9571 be part of a template-id. In fact, looking at this code:
9572
9573 a.operator K<int>()
9574
9575 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9576 It is impossible to call a templated conversion-function-id with an
9577 explicit argument list, since the only allowed template parameter is
9578 the type to which it is converting.
9579
9580 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9581 `template' keyword, in a construction like:
9582
9583 T::template f<3>()
9584
9585 In that case `f' is taken to be a template-name, even though there
9586 is no way of knowing for sure.
9587
9588 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9589 name refers to a set of overloaded functions, at least one of which
9590 is a template, or an IDENTIFIER_NODE with the name of the template,
9591 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9592 names are looked up inside uninstantiated templates. */
9593
9594 static tree
9595 cp_parser_template_name (cp_parser* parser,
9596 bool template_keyword_p,
9597 bool check_dependency_p,
9598 bool is_declaration,
9599 bool *is_identifier)
9600 {
9601 tree identifier;
9602 tree decl;
9603 tree fns;
9604
9605 /* If the next token is `operator', then we have either an
9606 operator-function-id or a conversion-function-id. */
9607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9608 {
9609 /* We don't know whether we're looking at an
9610 operator-function-id or a conversion-function-id. */
9611 cp_parser_parse_tentatively (parser);
9612 /* Try an operator-function-id. */
9613 identifier = cp_parser_operator_function_id (parser);
9614 /* If that didn't work, try a conversion-function-id. */
9615 if (!cp_parser_parse_definitely (parser))
9616 {
9617 cp_parser_error (parser, "expected template-name");
9618 return error_mark_node;
9619 }
9620 }
9621 /* Look for the identifier. */
9622 else
9623 identifier = cp_parser_identifier (parser);
9624
9625 /* If we didn't find an identifier, we don't have a template-id. */
9626 if (identifier == error_mark_node)
9627 return error_mark_node;
9628
9629 /* If the name immediately followed the `template' keyword, then it
9630 is a template-name. However, if the next token is not `<', then
9631 we do not treat it as a template-name, since it is not being used
9632 as part of a template-id. This enables us to handle constructs
9633 like:
9634
9635 template <typename T> struct S { S(); };
9636 template <typename T> S<T>::S();
9637
9638 correctly. We would treat `S' as a template -- if it were `S<T>'
9639 -- but we do not if there is no `<'. */
9640
9641 if (processing_template_decl
9642 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9643 {
9644 /* In a declaration, in a dependent context, we pretend that the
9645 "template" keyword was present in order to improve error
9646 recovery. For example, given:
9647
9648 template <typename T> void f(T::X<int>);
9649
9650 we want to treat "X<int>" as a template-id. */
9651 if (is_declaration
9652 && !template_keyword_p
9653 && parser->scope && TYPE_P (parser->scope)
9654 && check_dependency_p
9655 && dependent_type_p (parser->scope)
9656 /* Do not do this for dtors (or ctors), since they never
9657 need the template keyword before their name. */
9658 && !constructor_name_p (identifier, parser->scope))
9659 {
9660 cp_token_position start = 0;
9661
9662 /* Explain what went wrong. */
9663 error ("non-template %qD used as template", identifier);
9664 inform ("use %<%T::template %D%> to indicate that it is a template",
9665 parser->scope, identifier);
9666 /* If parsing tentatively, find the location of the "<" token. */
9667 if (cp_parser_simulate_error (parser))
9668 start = cp_lexer_token_position (parser->lexer, true);
9669 /* Parse the template arguments so that we can issue error
9670 messages about them. */
9671 cp_lexer_consume_token (parser->lexer);
9672 cp_parser_enclosed_template_argument_list (parser);
9673 /* Skip tokens until we find a good place from which to
9674 continue parsing. */
9675 cp_parser_skip_to_closing_parenthesis (parser,
9676 /*recovering=*/true,
9677 /*or_comma=*/true,
9678 /*consume_paren=*/false);
9679 /* If parsing tentatively, permanently remove the
9680 template argument list. That will prevent duplicate
9681 error messages from being issued about the missing
9682 "template" keyword. */
9683 if (start)
9684 cp_lexer_purge_tokens_after (parser->lexer, start);
9685 if (is_identifier)
9686 *is_identifier = true;
9687 return identifier;
9688 }
9689
9690 /* If the "template" keyword is present, then there is generally
9691 no point in doing name-lookup, so we just return IDENTIFIER.
9692 But, if the qualifying scope is non-dependent then we can
9693 (and must) do name-lookup normally. */
9694 if (template_keyword_p
9695 && (!parser->scope
9696 || (TYPE_P (parser->scope)
9697 && dependent_type_p (parser->scope))))
9698 return identifier;
9699 }
9700
9701 /* Look up the name. */
9702 decl = cp_parser_lookup_name (parser, identifier,
9703 none_type,
9704 /*is_template=*/false,
9705 /*is_namespace=*/false,
9706 check_dependency_p,
9707 /*ambiguous_decls=*/NULL);
9708 decl = maybe_get_template_decl_from_type_decl (decl);
9709
9710 /* If DECL is a template, then the name was a template-name. */
9711 if (TREE_CODE (decl) == TEMPLATE_DECL)
9712 ;
9713 else
9714 {
9715 tree fn = NULL_TREE;
9716
9717 /* The standard does not explicitly indicate whether a name that
9718 names a set of overloaded declarations, some of which are
9719 templates, is a template-name. However, such a name should
9720 be a template-name; otherwise, there is no way to form a
9721 template-id for the overloaded templates. */
9722 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9723 if (TREE_CODE (fns) == OVERLOAD)
9724 for (fn = fns; fn; fn = OVL_NEXT (fn))
9725 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9726 break;
9727
9728 if (!fn)
9729 {
9730 /* The name does not name a template. */
9731 cp_parser_error (parser, "expected template-name");
9732 return error_mark_node;
9733 }
9734 }
9735
9736 /* If DECL is dependent, and refers to a function, then just return
9737 its name; we will look it up again during template instantiation. */
9738 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9739 {
9740 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9741 if (TYPE_P (scope) && dependent_type_p (scope))
9742 return identifier;
9743 }
9744
9745 return decl;
9746 }
9747
9748 /* Parse a template-argument-list.
9749
9750 template-argument-list:
9751 template-argument ... [opt]
9752 template-argument-list , template-argument ... [opt]
9753
9754 Returns a TREE_VEC containing the arguments. */
9755
9756 static tree
9757 cp_parser_template_argument_list (cp_parser* parser)
9758 {
9759 tree fixed_args[10];
9760 unsigned n_args = 0;
9761 unsigned alloced = 10;
9762 tree *arg_ary = fixed_args;
9763 tree vec;
9764 bool saved_in_template_argument_list_p;
9765 bool saved_ice_p;
9766 bool saved_non_ice_p;
9767
9768 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9769 parser->in_template_argument_list_p = true;
9770 /* Even if the template-id appears in an integral
9771 constant-expression, the contents of the argument list do
9772 not. */
9773 saved_ice_p = parser->integral_constant_expression_p;
9774 parser->integral_constant_expression_p = false;
9775 saved_non_ice_p = parser->non_integral_constant_expression_p;
9776 parser->non_integral_constant_expression_p = false;
9777 /* Parse the arguments. */
9778 do
9779 {
9780 tree argument;
9781
9782 if (n_args)
9783 /* Consume the comma. */
9784 cp_lexer_consume_token (parser->lexer);
9785
9786 /* Parse the template-argument. */
9787 argument = cp_parser_template_argument (parser);
9788
9789 /* If the next token is an ellipsis, we're expanding a template
9790 argument pack. */
9791 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9792 {
9793 /* Consume the `...' token. */
9794 cp_lexer_consume_token (parser->lexer);
9795
9796 /* Make the argument into a TYPE_PACK_EXPANSION or
9797 EXPR_PACK_EXPANSION. */
9798 argument = make_pack_expansion (argument);
9799 }
9800
9801 if (n_args == alloced)
9802 {
9803 alloced *= 2;
9804
9805 if (arg_ary == fixed_args)
9806 {
9807 arg_ary = XNEWVEC (tree, alloced);
9808 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9809 }
9810 else
9811 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9812 }
9813 arg_ary[n_args++] = argument;
9814 }
9815 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9816
9817 vec = make_tree_vec (n_args);
9818
9819 while (n_args--)
9820 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9821
9822 if (arg_ary != fixed_args)
9823 free (arg_ary);
9824 parser->non_integral_constant_expression_p = saved_non_ice_p;
9825 parser->integral_constant_expression_p = saved_ice_p;
9826 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9827 return vec;
9828 }
9829
9830 /* Parse a template-argument.
9831
9832 template-argument:
9833 assignment-expression
9834 type-id
9835 id-expression
9836
9837 The representation is that of an assignment-expression, type-id, or
9838 id-expression -- except that the qualified id-expression is
9839 evaluated, so that the value returned is either a DECL or an
9840 OVERLOAD.
9841
9842 Although the standard says "assignment-expression", it forbids
9843 throw-expressions or assignments in the template argument.
9844 Therefore, we use "conditional-expression" instead. */
9845
9846 static tree
9847 cp_parser_template_argument (cp_parser* parser)
9848 {
9849 tree argument;
9850 bool template_p;
9851 bool address_p;
9852 bool maybe_type_id = false;
9853 cp_token *token;
9854 cp_id_kind idk;
9855
9856 /* There's really no way to know what we're looking at, so we just
9857 try each alternative in order.
9858
9859 [temp.arg]
9860
9861 In a template-argument, an ambiguity between a type-id and an
9862 expression is resolved to a type-id, regardless of the form of
9863 the corresponding template-parameter.
9864
9865 Therefore, we try a type-id first. */
9866 cp_parser_parse_tentatively (parser);
9867 argument = cp_parser_type_id (parser);
9868 /* If there was no error parsing the type-id but the next token is a '>>',
9869 we probably found a typo for '> >'. But there are type-id which are
9870 also valid expressions. For instance:
9871
9872 struct X { int operator >> (int); };
9873 template <int V> struct Foo {};
9874 Foo<X () >> 5> r;
9875
9876 Here 'X()' is a valid type-id of a function type, but the user just
9877 wanted to write the expression "X() >> 5". Thus, we remember that we
9878 found a valid type-id, but we still try to parse the argument as an
9879 expression to see what happens. */
9880 if (!cp_parser_error_occurred (parser)
9881 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9882 {
9883 maybe_type_id = true;
9884 cp_parser_abort_tentative_parse (parser);
9885 }
9886 else
9887 {
9888 /* If the next token isn't a `,' or a `>', then this argument wasn't
9889 really finished. This means that the argument is not a valid
9890 type-id. */
9891 if (!cp_parser_next_token_ends_template_argument_p (parser))
9892 cp_parser_error (parser, "expected template-argument");
9893 /* If that worked, we're done. */
9894 if (cp_parser_parse_definitely (parser))
9895 return argument;
9896 }
9897 /* We're still not sure what the argument will be. */
9898 cp_parser_parse_tentatively (parser);
9899 /* Try a template. */
9900 argument = cp_parser_id_expression (parser,
9901 /*template_keyword_p=*/false,
9902 /*check_dependency_p=*/true,
9903 &template_p,
9904 /*declarator_p=*/false,
9905 /*optional_p=*/false);
9906 /* If the next token isn't a `,' or a `>', then this argument wasn't
9907 really finished. */
9908 if (!cp_parser_next_token_ends_template_argument_p (parser))
9909 cp_parser_error (parser, "expected template-argument");
9910 if (!cp_parser_error_occurred (parser))
9911 {
9912 /* Figure out what is being referred to. If the id-expression
9913 was for a class template specialization, then we will have a
9914 TYPE_DECL at this point. There is no need to do name lookup
9915 at this point in that case. */
9916 if (TREE_CODE (argument) != TYPE_DECL)
9917 argument = cp_parser_lookup_name (parser, argument,
9918 none_type,
9919 /*is_template=*/template_p,
9920 /*is_namespace=*/false,
9921 /*check_dependency=*/true,
9922 /*ambiguous_decls=*/NULL);
9923 if (TREE_CODE (argument) != TEMPLATE_DECL
9924 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9925 cp_parser_error (parser, "expected template-name");
9926 }
9927 if (cp_parser_parse_definitely (parser))
9928 return argument;
9929 /* It must be a non-type argument. There permitted cases are given
9930 in [temp.arg.nontype]:
9931
9932 -- an integral constant-expression of integral or enumeration
9933 type; or
9934
9935 -- the name of a non-type template-parameter; or
9936
9937 -- the name of an object or function with external linkage...
9938
9939 -- the address of an object or function with external linkage...
9940
9941 -- a pointer to member... */
9942 /* Look for a non-type template parameter. */
9943 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9944 {
9945 cp_parser_parse_tentatively (parser);
9946 argument = cp_parser_primary_expression (parser,
9947 /*adress_p=*/false,
9948 /*cast_p=*/false,
9949 /*template_arg_p=*/true,
9950 &idk);
9951 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9952 || !cp_parser_next_token_ends_template_argument_p (parser))
9953 cp_parser_simulate_error (parser);
9954 if (cp_parser_parse_definitely (parser))
9955 return argument;
9956 }
9957
9958 /* If the next token is "&", the argument must be the address of an
9959 object or function with external linkage. */
9960 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9961 if (address_p)
9962 cp_lexer_consume_token (parser->lexer);
9963 /* See if we might have an id-expression. */
9964 token = cp_lexer_peek_token (parser->lexer);
9965 if (token->type == CPP_NAME
9966 || token->keyword == RID_OPERATOR
9967 || token->type == CPP_SCOPE
9968 || token->type == CPP_TEMPLATE_ID
9969 || token->type == CPP_NESTED_NAME_SPECIFIER)
9970 {
9971 cp_parser_parse_tentatively (parser);
9972 argument = cp_parser_primary_expression (parser,
9973 address_p,
9974 /*cast_p=*/false,
9975 /*template_arg_p=*/true,
9976 &idk);
9977 if (cp_parser_error_occurred (parser)
9978 || !cp_parser_next_token_ends_template_argument_p (parser))
9979 cp_parser_abort_tentative_parse (parser);
9980 else
9981 {
9982 if (TREE_CODE (argument) == INDIRECT_REF)
9983 {
9984 gcc_assert (REFERENCE_REF_P (argument));
9985 argument = TREE_OPERAND (argument, 0);
9986 }
9987
9988 if (TREE_CODE (argument) == VAR_DECL)
9989 {
9990 /* A variable without external linkage might still be a
9991 valid constant-expression, so no error is issued here
9992 if the external-linkage check fails. */
9993 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9994 cp_parser_simulate_error (parser);
9995 }
9996 else if (is_overloaded_fn (argument))
9997 /* All overloaded functions are allowed; if the external
9998 linkage test does not pass, an error will be issued
9999 later. */
10000 ;
10001 else if (address_p
10002 && (TREE_CODE (argument) == OFFSET_REF
10003 || TREE_CODE (argument) == SCOPE_REF))
10004 /* A pointer-to-member. */
10005 ;
10006 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10007 ;
10008 else
10009 cp_parser_simulate_error (parser);
10010
10011 if (cp_parser_parse_definitely (parser))
10012 {
10013 if (address_p)
10014 argument = build_x_unary_op (ADDR_EXPR, argument);
10015 return argument;
10016 }
10017 }
10018 }
10019 /* If the argument started with "&", there are no other valid
10020 alternatives at this point. */
10021 if (address_p)
10022 {
10023 cp_parser_error (parser, "invalid non-type template argument");
10024 return error_mark_node;
10025 }
10026
10027 /* If the argument wasn't successfully parsed as a type-id followed
10028 by '>>', the argument can only be a constant expression now.
10029 Otherwise, we try parsing the constant-expression tentatively,
10030 because the argument could really be a type-id. */
10031 if (maybe_type_id)
10032 cp_parser_parse_tentatively (parser);
10033 argument = cp_parser_constant_expression (parser,
10034 /*allow_non_constant_p=*/false,
10035 /*non_constant_p=*/NULL);
10036 argument = fold_non_dependent_expr (argument);
10037 if (!maybe_type_id)
10038 return argument;
10039 if (!cp_parser_next_token_ends_template_argument_p (parser))
10040 cp_parser_error (parser, "expected template-argument");
10041 if (cp_parser_parse_definitely (parser))
10042 return argument;
10043 /* We did our best to parse the argument as a non type-id, but that
10044 was the only alternative that matched (albeit with a '>' after
10045 it). We can assume it's just a typo from the user, and a
10046 diagnostic will then be issued. */
10047 return cp_parser_type_id (parser);
10048 }
10049
10050 /* Parse an explicit-instantiation.
10051
10052 explicit-instantiation:
10053 template declaration
10054
10055 Although the standard says `declaration', what it really means is:
10056
10057 explicit-instantiation:
10058 template decl-specifier-seq [opt] declarator [opt] ;
10059
10060 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10061 supposed to be allowed. A defect report has been filed about this
10062 issue.
10063
10064 GNU Extension:
10065
10066 explicit-instantiation:
10067 storage-class-specifier template
10068 decl-specifier-seq [opt] declarator [opt] ;
10069 function-specifier template
10070 decl-specifier-seq [opt] declarator [opt] ; */
10071
10072 static void
10073 cp_parser_explicit_instantiation (cp_parser* parser)
10074 {
10075 int declares_class_or_enum;
10076 cp_decl_specifier_seq decl_specifiers;
10077 tree extension_specifier = NULL_TREE;
10078
10079 /* Look for an (optional) storage-class-specifier or
10080 function-specifier. */
10081 if (cp_parser_allow_gnu_extensions_p (parser))
10082 {
10083 extension_specifier
10084 = cp_parser_storage_class_specifier_opt (parser);
10085 if (!extension_specifier)
10086 extension_specifier
10087 = cp_parser_function_specifier_opt (parser,
10088 /*decl_specs=*/NULL);
10089 }
10090
10091 /* Look for the `template' keyword. */
10092 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10093 /* Let the front end know that we are processing an explicit
10094 instantiation. */
10095 begin_explicit_instantiation ();
10096 /* [temp.explicit] says that we are supposed to ignore access
10097 control while processing explicit instantiation directives. */
10098 push_deferring_access_checks (dk_no_check);
10099 /* Parse a decl-specifier-seq. */
10100 cp_parser_decl_specifier_seq (parser,
10101 CP_PARSER_FLAGS_OPTIONAL,
10102 &decl_specifiers,
10103 &declares_class_or_enum);
10104 /* If there was exactly one decl-specifier, and it declared a class,
10105 and there's no declarator, then we have an explicit type
10106 instantiation. */
10107 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10108 {
10109 tree type;
10110
10111 type = check_tag_decl (&decl_specifiers);
10112 /* Turn access control back on for names used during
10113 template instantiation. */
10114 pop_deferring_access_checks ();
10115 if (type)
10116 do_type_instantiation (type, extension_specifier,
10117 /*complain=*/tf_error);
10118 }
10119 else
10120 {
10121 cp_declarator *declarator;
10122 tree decl;
10123
10124 /* Parse the declarator. */
10125 declarator
10126 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10127 /*ctor_dtor_or_conv_p=*/NULL,
10128 /*parenthesized_p=*/NULL,
10129 /*member_p=*/false);
10130 if (declares_class_or_enum & 2)
10131 cp_parser_check_for_definition_in_return_type (declarator,
10132 decl_specifiers.type);
10133 if (declarator != cp_error_declarator)
10134 {
10135 decl = grokdeclarator (declarator, &decl_specifiers,
10136 NORMAL, 0, &decl_specifiers.attributes);
10137 /* Turn access control back on for names used during
10138 template instantiation. */
10139 pop_deferring_access_checks ();
10140 /* Do the explicit instantiation. */
10141 do_decl_instantiation (decl, extension_specifier);
10142 }
10143 else
10144 {
10145 pop_deferring_access_checks ();
10146 /* Skip the body of the explicit instantiation. */
10147 cp_parser_skip_to_end_of_statement (parser);
10148 }
10149 }
10150 /* We're done with the instantiation. */
10151 end_explicit_instantiation ();
10152
10153 cp_parser_consume_semicolon_at_end_of_statement (parser);
10154 }
10155
10156 /* Parse an explicit-specialization.
10157
10158 explicit-specialization:
10159 template < > declaration
10160
10161 Although the standard says `declaration', what it really means is:
10162
10163 explicit-specialization:
10164 template <> decl-specifier [opt] init-declarator [opt] ;
10165 template <> function-definition
10166 template <> explicit-specialization
10167 template <> template-declaration */
10168
10169 static void
10170 cp_parser_explicit_specialization (cp_parser* parser)
10171 {
10172 bool need_lang_pop;
10173 /* Look for the `template' keyword. */
10174 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10175 /* Look for the `<'. */
10176 cp_parser_require (parser, CPP_LESS, "`<'");
10177 /* Look for the `>'. */
10178 cp_parser_require (parser, CPP_GREATER, "`>'");
10179 /* We have processed another parameter list. */
10180 ++parser->num_template_parameter_lists;
10181 /* [temp]
10182
10183 A template ... explicit specialization ... shall not have C
10184 linkage. */
10185 if (current_lang_name == lang_name_c)
10186 {
10187 error ("template specialization with C linkage");
10188 /* Give it C++ linkage to avoid confusing other parts of the
10189 front end. */
10190 push_lang_context (lang_name_cplusplus);
10191 need_lang_pop = true;
10192 }
10193 else
10194 need_lang_pop = false;
10195 /* Let the front end know that we are beginning a specialization. */
10196 if (!begin_specialization ())
10197 {
10198 end_specialization ();
10199 cp_parser_skip_to_end_of_block_or_statement (parser);
10200 return;
10201 }
10202
10203 /* If the next keyword is `template', we need to figure out whether
10204 or not we're looking a template-declaration. */
10205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10206 {
10207 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10208 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10209 cp_parser_template_declaration_after_export (parser,
10210 /*member_p=*/false);
10211 else
10212 cp_parser_explicit_specialization (parser);
10213 }
10214 else
10215 /* Parse the dependent declaration. */
10216 cp_parser_single_declaration (parser,
10217 /*checks=*/NULL,
10218 /*member_p=*/false,
10219 /*friend_p=*/NULL);
10220 /* We're done with the specialization. */
10221 end_specialization ();
10222 /* For the erroneous case of a template with C linkage, we pushed an
10223 implicit C++ linkage scope; exit that scope now. */
10224 if (need_lang_pop)
10225 pop_lang_context ();
10226 /* We're done with this parameter list. */
10227 --parser->num_template_parameter_lists;
10228 }
10229
10230 /* Parse a type-specifier.
10231
10232 type-specifier:
10233 simple-type-specifier
10234 class-specifier
10235 enum-specifier
10236 elaborated-type-specifier
10237 cv-qualifier
10238
10239 GNU Extension:
10240
10241 type-specifier:
10242 __complex__
10243
10244 Returns a representation of the type-specifier. For a
10245 class-specifier, enum-specifier, or elaborated-type-specifier, a
10246 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10247
10248 The parser flags FLAGS is used to control type-specifier parsing.
10249
10250 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10251 in a decl-specifier-seq.
10252
10253 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10254 class-specifier, enum-specifier, or elaborated-type-specifier, then
10255 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10256 if a type is declared; 2 if it is defined. Otherwise, it is set to
10257 zero.
10258
10259 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10260 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10261 is set to FALSE. */
10262
10263 static tree
10264 cp_parser_type_specifier (cp_parser* parser,
10265 cp_parser_flags flags,
10266 cp_decl_specifier_seq *decl_specs,
10267 bool is_declaration,
10268 int* declares_class_or_enum,
10269 bool* is_cv_qualifier)
10270 {
10271 tree type_spec = NULL_TREE;
10272 cp_token *token;
10273 enum rid keyword;
10274 cp_decl_spec ds = ds_last;
10275
10276 /* Assume this type-specifier does not declare a new type. */
10277 if (declares_class_or_enum)
10278 *declares_class_or_enum = 0;
10279 /* And that it does not specify a cv-qualifier. */
10280 if (is_cv_qualifier)
10281 *is_cv_qualifier = false;
10282 /* Peek at the next token. */
10283 token = cp_lexer_peek_token (parser->lexer);
10284
10285 /* If we're looking at a keyword, we can use that to guide the
10286 production we choose. */
10287 keyword = token->keyword;
10288 switch (keyword)
10289 {
10290 case RID_ENUM:
10291 /* Look for the enum-specifier. */
10292 type_spec = cp_parser_enum_specifier (parser);
10293 /* If that worked, we're done. */
10294 if (type_spec)
10295 {
10296 if (declares_class_or_enum)
10297 *declares_class_or_enum = 2;
10298 if (decl_specs)
10299 cp_parser_set_decl_spec_type (decl_specs,
10300 type_spec,
10301 /*user_defined_p=*/true);
10302 return type_spec;
10303 }
10304 else
10305 goto elaborated_type_specifier;
10306
10307 /* Any of these indicate either a class-specifier, or an
10308 elaborated-type-specifier. */
10309 case RID_CLASS:
10310 case RID_STRUCT:
10311 case RID_UNION:
10312 /* Parse tentatively so that we can back up if we don't find a
10313 class-specifier. */
10314 cp_parser_parse_tentatively (parser);
10315 /* Look for the class-specifier. */
10316 type_spec = cp_parser_class_specifier (parser);
10317 /* If that worked, we're done. */
10318 if (cp_parser_parse_definitely (parser))
10319 {
10320 if (declares_class_or_enum)
10321 *declares_class_or_enum = 2;
10322 if (decl_specs)
10323 cp_parser_set_decl_spec_type (decl_specs,
10324 type_spec,
10325 /*user_defined_p=*/true);
10326 return type_spec;
10327 }
10328
10329 /* Fall through. */
10330 elaborated_type_specifier:
10331 /* We're declaring (not defining) a class or enum. */
10332 if (declares_class_or_enum)
10333 *declares_class_or_enum = 1;
10334
10335 /* Fall through. */
10336 case RID_TYPENAME:
10337 /* Look for an elaborated-type-specifier. */
10338 type_spec
10339 = (cp_parser_elaborated_type_specifier
10340 (parser,
10341 decl_specs && decl_specs->specs[(int) ds_friend],
10342 is_declaration));
10343 if (decl_specs)
10344 cp_parser_set_decl_spec_type (decl_specs,
10345 type_spec,
10346 /*user_defined_p=*/true);
10347 return type_spec;
10348
10349 case RID_CONST:
10350 ds = ds_const;
10351 if (is_cv_qualifier)
10352 *is_cv_qualifier = true;
10353 break;
10354
10355 case RID_VOLATILE:
10356 ds = ds_volatile;
10357 if (is_cv_qualifier)
10358 *is_cv_qualifier = true;
10359 break;
10360
10361 case RID_RESTRICT:
10362 ds = ds_restrict;
10363 if (is_cv_qualifier)
10364 *is_cv_qualifier = true;
10365 break;
10366
10367 case RID_COMPLEX:
10368 /* The `__complex__' keyword is a GNU extension. */
10369 ds = ds_complex;
10370 break;
10371
10372 default:
10373 break;
10374 }
10375
10376 /* Handle simple keywords. */
10377 if (ds != ds_last)
10378 {
10379 if (decl_specs)
10380 {
10381 ++decl_specs->specs[(int)ds];
10382 decl_specs->any_specifiers_p = true;
10383 }
10384 return cp_lexer_consume_token (parser->lexer)->u.value;
10385 }
10386
10387 /* If we do not already have a type-specifier, assume we are looking
10388 at a simple-type-specifier. */
10389 type_spec = cp_parser_simple_type_specifier (parser,
10390 decl_specs,
10391 flags);
10392
10393 /* If we didn't find a type-specifier, and a type-specifier was not
10394 optional in this context, issue an error message. */
10395 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10396 {
10397 cp_parser_error (parser, "expected type specifier");
10398 return error_mark_node;
10399 }
10400
10401 return type_spec;
10402 }
10403
10404 /* Parse a simple-type-specifier.
10405
10406 simple-type-specifier:
10407 :: [opt] nested-name-specifier [opt] type-name
10408 :: [opt] nested-name-specifier template template-id
10409 char
10410 wchar_t
10411 bool
10412 short
10413 int
10414 long
10415 signed
10416 unsigned
10417 float
10418 double
10419 void
10420
10421 GNU Extension:
10422
10423 simple-type-specifier:
10424 __typeof__ unary-expression
10425 __typeof__ ( type-id )
10426
10427 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10428 appropriately updated. */
10429
10430 static tree
10431 cp_parser_simple_type_specifier (cp_parser* parser,
10432 cp_decl_specifier_seq *decl_specs,
10433 cp_parser_flags flags)
10434 {
10435 tree type = NULL_TREE;
10436 cp_token *token;
10437
10438 /* Peek at the next token. */
10439 token = cp_lexer_peek_token (parser->lexer);
10440
10441 /* If we're looking at a keyword, things are easy. */
10442 switch (token->keyword)
10443 {
10444 case RID_CHAR:
10445 if (decl_specs)
10446 decl_specs->explicit_char_p = true;
10447 type = char_type_node;
10448 break;
10449 case RID_WCHAR:
10450 type = wchar_type_node;
10451 break;
10452 case RID_BOOL:
10453 type = boolean_type_node;
10454 break;
10455 case RID_SHORT:
10456 if (decl_specs)
10457 ++decl_specs->specs[(int) ds_short];
10458 type = short_integer_type_node;
10459 break;
10460 case RID_INT:
10461 if (decl_specs)
10462 decl_specs->explicit_int_p = true;
10463 type = integer_type_node;
10464 break;
10465 case RID_LONG:
10466 if (decl_specs)
10467 ++decl_specs->specs[(int) ds_long];
10468 type = long_integer_type_node;
10469 break;
10470 case RID_SIGNED:
10471 if (decl_specs)
10472 ++decl_specs->specs[(int) ds_signed];
10473 type = integer_type_node;
10474 break;
10475 case RID_UNSIGNED:
10476 if (decl_specs)
10477 ++decl_specs->specs[(int) ds_unsigned];
10478 type = unsigned_type_node;
10479 break;
10480 case RID_FLOAT:
10481 type = float_type_node;
10482 break;
10483 case RID_DOUBLE:
10484 type = double_type_node;
10485 break;
10486 case RID_VOID:
10487 type = void_type_node;
10488 break;
10489
10490 case RID_TYPEOF:
10491 /* Consume the `typeof' token. */
10492 cp_lexer_consume_token (parser->lexer);
10493 /* Parse the operand to `typeof'. */
10494 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10495 /* If it is not already a TYPE, take its type. */
10496 if (!TYPE_P (type))
10497 type = finish_typeof (type);
10498
10499 if (decl_specs)
10500 cp_parser_set_decl_spec_type (decl_specs, type,
10501 /*user_defined_p=*/true);
10502
10503 return type;
10504
10505 default:
10506 break;
10507 }
10508
10509 /* If the type-specifier was for a built-in type, we're done. */
10510 if (type)
10511 {
10512 tree id;
10513
10514 /* Record the type. */
10515 if (decl_specs
10516 && (token->keyword != RID_SIGNED
10517 && token->keyword != RID_UNSIGNED
10518 && token->keyword != RID_SHORT
10519 && token->keyword != RID_LONG))
10520 cp_parser_set_decl_spec_type (decl_specs,
10521 type,
10522 /*user_defined=*/false);
10523 if (decl_specs)
10524 decl_specs->any_specifiers_p = true;
10525
10526 /* Consume the token. */
10527 id = cp_lexer_consume_token (parser->lexer)->u.value;
10528
10529 /* There is no valid C++ program where a non-template type is
10530 followed by a "<". That usually indicates that the user thought
10531 that the type was a template. */
10532 cp_parser_check_for_invalid_template_id (parser, type);
10533
10534 return TYPE_NAME (type);
10535 }
10536
10537 /* The type-specifier must be a user-defined type. */
10538 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10539 {
10540 bool qualified_p;
10541 bool global_p;
10542
10543 /* Don't gobble tokens or issue error messages if this is an
10544 optional type-specifier. */
10545 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10546 cp_parser_parse_tentatively (parser);
10547
10548 /* Look for the optional `::' operator. */
10549 global_p
10550 = (cp_parser_global_scope_opt (parser,
10551 /*current_scope_valid_p=*/false)
10552 != NULL_TREE);
10553 /* Look for the nested-name specifier. */
10554 qualified_p
10555 = (cp_parser_nested_name_specifier_opt (parser,
10556 /*typename_keyword_p=*/false,
10557 /*check_dependency_p=*/true,
10558 /*type_p=*/false,
10559 /*is_declaration=*/false)
10560 != NULL_TREE);
10561 /* If we have seen a nested-name-specifier, and the next token
10562 is `template', then we are using the template-id production. */
10563 if (parser->scope
10564 && cp_parser_optional_template_keyword (parser))
10565 {
10566 /* Look for the template-id. */
10567 type = cp_parser_template_id (parser,
10568 /*template_keyword_p=*/true,
10569 /*check_dependency_p=*/true,
10570 /*is_declaration=*/false);
10571 /* If the template-id did not name a type, we are out of
10572 luck. */
10573 if (TREE_CODE (type) != TYPE_DECL)
10574 {
10575 cp_parser_error (parser, "expected template-id for type");
10576 type = NULL_TREE;
10577 }
10578 }
10579 /* Otherwise, look for a type-name. */
10580 else
10581 type = cp_parser_type_name (parser);
10582 /* Keep track of all name-lookups performed in class scopes. */
10583 if (type
10584 && !global_p
10585 && !qualified_p
10586 && TREE_CODE (type) == TYPE_DECL
10587 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10588 maybe_note_name_used_in_class (DECL_NAME (type), type);
10589 /* If it didn't work out, we don't have a TYPE. */
10590 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10591 && !cp_parser_parse_definitely (parser))
10592 type = NULL_TREE;
10593 if (type && decl_specs)
10594 cp_parser_set_decl_spec_type (decl_specs, type,
10595 /*user_defined=*/true);
10596 }
10597
10598 /* If we didn't get a type-name, issue an error message. */
10599 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10600 {
10601 cp_parser_error (parser, "expected type-name");
10602 return error_mark_node;
10603 }
10604
10605 /* There is no valid C++ program where a non-template type is
10606 followed by a "<". That usually indicates that the user thought
10607 that the type was a template. */
10608 if (type && type != error_mark_node)
10609 {
10610 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10611 If it is, then the '<'...'>' enclose protocol names rather than
10612 template arguments, and so everything is fine. */
10613 if (c_dialect_objc ()
10614 && (objc_is_id (type) || objc_is_class_name (type)))
10615 {
10616 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10617 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10618
10619 /* Clobber the "unqualified" type previously entered into
10620 DECL_SPECS with the new, improved protocol-qualified version. */
10621 if (decl_specs)
10622 decl_specs->type = qual_type;
10623
10624 return qual_type;
10625 }
10626
10627 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10628 }
10629
10630 return type;
10631 }
10632
10633 /* Parse a type-name.
10634
10635 type-name:
10636 class-name
10637 enum-name
10638 typedef-name
10639
10640 enum-name:
10641 identifier
10642
10643 typedef-name:
10644 identifier
10645
10646 Returns a TYPE_DECL for the type. */
10647
10648 static tree
10649 cp_parser_type_name (cp_parser* parser)
10650 {
10651 tree type_decl;
10652 tree identifier;
10653
10654 /* We can't know yet whether it is a class-name or not. */
10655 cp_parser_parse_tentatively (parser);
10656 /* Try a class-name. */
10657 type_decl = cp_parser_class_name (parser,
10658 /*typename_keyword_p=*/false,
10659 /*template_keyword_p=*/false,
10660 none_type,
10661 /*check_dependency_p=*/true,
10662 /*class_head_p=*/false,
10663 /*is_declaration=*/false);
10664 /* If it's not a class-name, keep looking. */
10665 if (!cp_parser_parse_definitely (parser))
10666 {
10667 /* It must be a typedef-name or an enum-name. */
10668 identifier = cp_parser_identifier (parser);
10669 if (identifier == error_mark_node)
10670 return error_mark_node;
10671
10672 /* Look up the type-name. */
10673 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10674
10675 if (TREE_CODE (type_decl) != TYPE_DECL
10676 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10677 {
10678 /* See if this is an Objective-C type. */
10679 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10680 tree type = objc_get_protocol_qualified_type (identifier, protos);
10681 if (type)
10682 type_decl = TYPE_NAME (type);
10683 }
10684
10685 /* Issue an error if we did not find a type-name. */
10686 if (TREE_CODE (type_decl) != TYPE_DECL)
10687 {
10688 if (!cp_parser_simulate_error (parser))
10689 cp_parser_name_lookup_error (parser, identifier, type_decl,
10690 "is not a type");
10691 type_decl = error_mark_node;
10692 }
10693 /* Remember that the name was used in the definition of the
10694 current class so that we can check later to see if the
10695 meaning would have been different after the class was
10696 entirely defined. */
10697 else if (type_decl != error_mark_node
10698 && !parser->scope)
10699 maybe_note_name_used_in_class (identifier, type_decl);
10700 }
10701
10702 return type_decl;
10703 }
10704
10705
10706 /* Parse an elaborated-type-specifier. Note that the grammar given
10707 here incorporates the resolution to DR68.
10708
10709 elaborated-type-specifier:
10710 class-key :: [opt] nested-name-specifier [opt] identifier
10711 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10712 enum :: [opt] nested-name-specifier [opt] identifier
10713 typename :: [opt] nested-name-specifier identifier
10714 typename :: [opt] nested-name-specifier template [opt]
10715 template-id
10716
10717 GNU extension:
10718
10719 elaborated-type-specifier:
10720 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10721 class-key attributes :: [opt] nested-name-specifier [opt]
10722 template [opt] template-id
10723 enum attributes :: [opt] nested-name-specifier [opt] identifier
10724
10725 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10726 declared `friend'. If IS_DECLARATION is TRUE, then this
10727 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10728 something is being declared.
10729
10730 Returns the TYPE specified. */
10731
10732 static tree
10733 cp_parser_elaborated_type_specifier (cp_parser* parser,
10734 bool is_friend,
10735 bool is_declaration)
10736 {
10737 enum tag_types tag_type;
10738 tree identifier;
10739 tree type = NULL_TREE;
10740 tree attributes = NULL_TREE;
10741
10742 /* See if we're looking at the `enum' keyword. */
10743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10744 {
10745 /* Consume the `enum' token. */
10746 cp_lexer_consume_token (parser->lexer);
10747 /* Remember that it's an enumeration type. */
10748 tag_type = enum_type;
10749 /* Parse the attributes. */
10750 attributes = cp_parser_attributes_opt (parser);
10751 }
10752 /* Or, it might be `typename'. */
10753 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10754 RID_TYPENAME))
10755 {
10756 /* Consume the `typename' token. */
10757 cp_lexer_consume_token (parser->lexer);
10758 /* Remember that it's a `typename' type. */
10759 tag_type = typename_type;
10760 /* The `typename' keyword is only allowed in templates. */
10761 if (!processing_template_decl)
10762 pedwarn ("using %<typename%> outside of template");
10763 }
10764 /* Otherwise it must be a class-key. */
10765 else
10766 {
10767 tag_type = cp_parser_class_key (parser);
10768 if (tag_type == none_type)
10769 return error_mark_node;
10770 /* Parse the attributes. */
10771 attributes = cp_parser_attributes_opt (parser);
10772 }
10773
10774 /* Look for the `::' operator. */
10775 cp_parser_global_scope_opt (parser,
10776 /*current_scope_valid_p=*/false);
10777 /* Look for the nested-name-specifier. */
10778 if (tag_type == typename_type)
10779 {
10780 if (!cp_parser_nested_name_specifier (parser,
10781 /*typename_keyword_p=*/true,
10782 /*check_dependency_p=*/true,
10783 /*type_p=*/true,
10784 is_declaration))
10785 return error_mark_node;
10786 }
10787 else
10788 /* Even though `typename' is not present, the proposed resolution
10789 to Core Issue 180 says that in `class A<T>::B', `B' should be
10790 considered a type-name, even if `A<T>' is dependent. */
10791 cp_parser_nested_name_specifier_opt (parser,
10792 /*typename_keyword_p=*/true,
10793 /*check_dependency_p=*/true,
10794 /*type_p=*/true,
10795 is_declaration);
10796 /* For everything but enumeration types, consider a template-id.
10797 For an enumeration type, consider only a plain identifier. */
10798 if (tag_type != enum_type)
10799 {
10800 bool template_p = false;
10801 tree decl;
10802
10803 /* Allow the `template' keyword. */
10804 template_p = cp_parser_optional_template_keyword (parser);
10805 /* If we didn't see `template', we don't know if there's a
10806 template-id or not. */
10807 if (!template_p)
10808 cp_parser_parse_tentatively (parser);
10809 /* Parse the template-id. */
10810 decl = cp_parser_template_id (parser, template_p,
10811 /*check_dependency_p=*/true,
10812 is_declaration);
10813 /* If we didn't find a template-id, look for an ordinary
10814 identifier. */
10815 if (!template_p && !cp_parser_parse_definitely (parser))
10816 ;
10817 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10818 in effect, then we must assume that, upon instantiation, the
10819 template will correspond to a class. */
10820 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10821 && tag_type == typename_type)
10822 type = make_typename_type (parser->scope, decl,
10823 typename_type,
10824 /*complain=*/tf_error);
10825 else
10826 type = TREE_TYPE (decl);
10827 }
10828
10829 if (!type)
10830 {
10831 identifier = cp_parser_identifier (parser);
10832
10833 if (identifier == error_mark_node)
10834 {
10835 parser->scope = NULL_TREE;
10836 return error_mark_node;
10837 }
10838
10839 /* For a `typename', we needn't call xref_tag. */
10840 if (tag_type == typename_type
10841 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10842 return cp_parser_make_typename_type (parser, parser->scope,
10843 identifier);
10844 /* Look up a qualified name in the usual way. */
10845 if (parser->scope)
10846 {
10847 tree decl;
10848
10849 decl = cp_parser_lookup_name (parser, identifier,
10850 tag_type,
10851 /*is_template=*/false,
10852 /*is_namespace=*/false,
10853 /*check_dependency=*/true,
10854 /*ambiguous_decls=*/NULL);
10855
10856 /* If we are parsing friend declaration, DECL may be a
10857 TEMPLATE_DECL tree node here. However, we need to check
10858 whether this TEMPLATE_DECL results in valid code. Consider
10859 the following example:
10860
10861 namespace N {
10862 template <class T> class C {};
10863 }
10864 class X {
10865 template <class T> friend class N::C; // #1, valid code
10866 };
10867 template <class T> class Y {
10868 friend class N::C; // #2, invalid code
10869 };
10870
10871 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10872 name lookup of `N::C'. We see that friend declaration must
10873 be template for the code to be valid. Note that
10874 processing_template_decl does not work here since it is
10875 always 1 for the above two cases. */
10876
10877 decl = (cp_parser_maybe_treat_template_as_class
10878 (decl, /*tag_name_p=*/is_friend
10879 && parser->num_template_parameter_lists));
10880
10881 if (TREE_CODE (decl) != TYPE_DECL)
10882 {
10883 cp_parser_diagnose_invalid_type_name (parser,
10884 parser->scope,
10885 identifier);
10886 return error_mark_node;
10887 }
10888
10889 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10890 {
10891 bool allow_template = (parser->num_template_parameter_lists
10892 || DECL_SELF_REFERENCE_P (decl));
10893 type = check_elaborated_type_specifier (tag_type, decl,
10894 allow_template);
10895
10896 if (type == error_mark_node)
10897 return error_mark_node;
10898 }
10899
10900 type = TREE_TYPE (decl);
10901 }
10902 else
10903 {
10904 /* An elaborated-type-specifier sometimes introduces a new type and
10905 sometimes names an existing type. Normally, the rule is that it
10906 introduces a new type only if there is not an existing type of
10907 the same name already in scope. For example, given:
10908
10909 struct S {};
10910 void f() { struct S s; }
10911
10912 the `struct S' in the body of `f' is the same `struct S' as in
10913 the global scope; the existing definition is used. However, if
10914 there were no global declaration, this would introduce a new
10915 local class named `S'.
10916
10917 An exception to this rule applies to the following code:
10918
10919 namespace N { struct S; }
10920
10921 Here, the elaborated-type-specifier names a new type
10922 unconditionally; even if there is already an `S' in the
10923 containing scope this declaration names a new type.
10924 This exception only applies if the elaborated-type-specifier
10925 forms the complete declaration:
10926
10927 [class.name]
10928
10929 A declaration consisting solely of `class-key identifier ;' is
10930 either a redeclaration of the name in the current scope or a
10931 forward declaration of the identifier as a class name. It
10932 introduces the name into the current scope.
10933
10934 We are in this situation precisely when the next token is a `;'.
10935
10936 An exception to the exception is that a `friend' declaration does
10937 *not* name a new type; i.e., given:
10938
10939 struct S { friend struct T; };
10940
10941 `T' is not a new type in the scope of `S'.
10942
10943 Also, `new struct S' or `sizeof (struct S)' never results in the
10944 definition of a new type; a new type can only be declared in a
10945 declaration context. */
10946
10947 tag_scope ts;
10948 bool template_p;
10949
10950 if (is_friend)
10951 /* Friends have special name lookup rules. */
10952 ts = ts_within_enclosing_non_class;
10953 else if (is_declaration
10954 && cp_lexer_next_token_is (parser->lexer,
10955 CPP_SEMICOLON))
10956 /* This is a `class-key identifier ;' */
10957 ts = ts_current;
10958 else
10959 ts = ts_global;
10960
10961 template_p =
10962 (parser->num_template_parameter_lists
10963 && (cp_parser_next_token_starts_class_definition_p (parser)
10964 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10965 /* An unqualified name was used to reference this type, so
10966 there were no qualifying templates. */
10967 if (!cp_parser_check_template_parameters (parser,
10968 /*num_templates=*/0))
10969 return error_mark_node;
10970 type = xref_tag (tag_type, identifier, ts, template_p);
10971 }
10972 }
10973
10974 if (type == error_mark_node)
10975 return error_mark_node;
10976
10977 /* Allow attributes on forward declarations of classes. */
10978 if (attributes)
10979 {
10980 if (TREE_CODE (type) == TYPENAME_TYPE)
10981 warning (OPT_Wattributes,
10982 "attributes ignored on uninstantiated type");
10983 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10984 && ! processing_explicit_instantiation)
10985 warning (OPT_Wattributes,
10986 "attributes ignored on template instantiation");
10987 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10988 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10989 else
10990 warning (OPT_Wattributes,
10991 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10992 }
10993
10994 if (tag_type != enum_type)
10995 cp_parser_check_class_key (tag_type, type);
10996
10997 /* A "<" cannot follow an elaborated type specifier. If that
10998 happens, the user was probably trying to form a template-id. */
10999 cp_parser_check_for_invalid_template_id (parser, type);
11000
11001 return type;
11002 }
11003
11004 /* Parse an enum-specifier.
11005
11006 enum-specifier:
11007 enum identifier [opt] { enumerator-list [opt] }
11008
11009 GNU Extensions:
11010 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11011 attributes[opt]
11012
11013 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11014 if the token stream isn't an enum-specifier after all. */
11015
11016 static tree
11017 cp_parser_enum_specifier (cp_parser* parser)
11018 {
11019 tree identifier;
11020 tree type;
11021 tree attributes;
11022
11023 /* Parse tentatively so that we can back up if we don't find a
11024 enum-specifier. */
11025 cp_parser_parse_tentatively (parser);
11026
11027 /* Caller guarantees that the current token is 'enum', an identifier
11028 possibly follows, and the token after that is an opening brace.
11029 If we don't have an identifier, fabricate an anonymous name for
11030 the enumeration being defined. */
11031 cp_lexer_consume_token (parser->lexer);
11032
11033 attributes = cp_parser_attributes_opt (parser);
11034
11035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11036 identifier = cp_parser_identifier (parser);
11037 else
11038 identifier = make_anon_name ();
11039
11040 /* Look for the `{' but don't consume it yet. */
11041 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11042 cp_parser_simulate_error (parser);
11043
11044 if (!cp_parser_parse_definitely (parser))
11045 return NULL_TREE;
11046
11047 /* Issue an error message if type-definitions are forbidden here. */
11048 if (!cp_parser_check_type_definition (parser))
11049 type = error_mark_node;
11050 else
11051 /* Create the new type. We do this before consuming the opening
11052 brace so the enum will be recorded as being on the line of its
11053 tag (or the 'enum' keyword, if there is no tag). */
11054 type = start_enum (identifier);
11055
11056 /* Consume the opening brace. */
11057 cp_lexer_consume_token (parser->lexer);
11058
11059 if (type == error_mark_node)
11060 {
11061 cp_parser_skip_to_end_of_block_or_statement (parser);
11062 return error_mark_node;
11063 }
11064
11065 /* If the next token is not '}', then there are some enumerators. */
11066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11067 cp_parser_enumerator_list (parser, type);
11068
11069 /* Consume the final '}'. */
11070 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11071
11072 /* Look for trailing attributes to apply to this enumeration, and
11073 apply them if appropriate. */
11074 if (cp_parser_allow_gnu_extensions_p (parser))
11075 {
11076 tree trailing_attr = cp_parser_attributes_opt (parser);
11077 cplus_decl_attributes (&type,
11078 trailing_attr,
11079 (int) ATTR_FLAG_TYPE_IN_PLACE);
11080 }
11081
11082 /* Finish up the enumeration. */
11083 finish_enum (type);
11084
11085 return type;
11086 }
11087
11088 /* Parse an enumerator-list. The enumerators all have the indicated
11089 TYPE.
11090
11091 enumerator-list:
11092 enumerator-definition
11093 enumerator-list , enumerator-definition */
11094
11095 static void
11096 cp_parser_enumerator_list (cp_parser* parser, tree type)
11097 {
11098 while (true)
11099 {
11100 /* Parse an enumerator-definition. */
11101 cp_parser_enumerator_definition (parser, type);
11102
11103 /* If the next token is not a ',', we've reached the end of
11104 the list. */
11105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11106 break;
11107 /* Otherwise, consume the `,' and keep going. */
11108 cp_lexer_consume_token (parser->lexer);
11109 /* If the next token is a `}', there is a trailing comma. */
11110 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11111 {
11112 if (pedantic && !in_system_header)
11113 pedwarn ("comma at end of enumerator list");
11114 break;
11115 }
11116 }
11117 }
11118
11119 /* Parse an enumerator-definition. The enumerator has the indicated
11120 TYPE.
11121
11122 enumerator-definition:
11123 enumerator
11124 enumerator = constant-expression
11125
11126 enumerator:
11127 identifier */
11128
11129 static void
11130 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11131 {
11132 tree identifier;
11133 tree value;
11134
11135 /* Look for the identifier. */
11136 identifier = cp_parser_identifier (parser);
11137 if (identifier == error_mark_node)
11138 return;
11139
11140 /* If the next token is an '=', then there is an explicit value. */
11141 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11142 {
11143 /* Consume the `=' token. */
11144 cp_lexer_consume_token (parser->lexer);
11145 /* Parse the value. */
11146 value = cp_parser_constant_expression (parser,
11147 /*allow_non_constant_p=*/false,
11148 NULL);
11149 }
11150 else
11151 value = NULL_TREE;
11152
11153 /* Create the enumerator. */
11154 build_enumerator (identifier, value, type);
11155 }
11156
11157 /* Parse a namespace-name.
11158
11159 namespace-name:
11160 original-namespace-name
11161 namespace-alias
11162
11163 Returns the NAMESPACE_DECL for the namespace. */
11164
11165 static tree
11166 cp_parser_namespace_name (cp_parser* parser)
11167 {
11168 tree identifier;
11169 tree namespace_decl;
11170
11171 /* Get the name of the namespace. */
11172 identifier = cp_parser_identifier (parser);
11173 if (identifier == error_mark_node)
11174 return error_mark_node;
11175
11176 /* Look up the identifier in the currently active scope. Look only
11177 for namespaces, due to:
11178
11179 [basic.lookup.udir]
11180
11181 When looking up a namespace-name in a using-directive or alias
11182 definition, only namespace names are considered.
11183
11184 And:
11185
11186 [basic.lookup.qual]
11187
11188 During the lookup of a name preceding the :: scope resolution
11189 operator, object, function, and enumerator names are ignored.
11190
11191 (Note that cp_parser_class_or_namespace_name only calls this
11192 function if the token after the name is the scope resolution
11193 operator.) */
11194 namespace_decl = cp_parser_lookup_name (parser, identifier,
11195 none_type,
11196 /*is_template=*/false,
11197 /*is_namespace=*/true,
11198 /*check_dependency=*/true,
11199 /*ambiguous_decls=*/NULL);
11200 /* If it's not a namespace, issue an error. */
11201 if (namespace_decl == error_mark_node
11202 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11203 {
11204 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11205 error ("%qD is not a namespace-name", identifier);
11206 cp_parser_error (parser, "expected namespace-name");
11207 namespace_decl = error_mark_node;
11208 }
11209
11210 return namespace_decl;
11211 }
11212
11213 /* Parse a namespace-definition.
11214
11215 namespace-definition:
11216 named-namespace-definition
11217 unnamed-namespace-definition
11218
11219 named-namespace-definition:
11220 original-namespace-definition
11221 extension-namespace-definition
11222
11223 original-namespace-definition:
11224 namespace identifier { namespace-body }
11225
11226 extension-namespace-definition:
11227 namespace original-namespace-name { namespace-body }
11228
11229 unnamed-namespace-definition:
11230 namespace { namespace-body } */
11231
11232 static void
11233 cp_parser_namespace_definition (cp_parser* parser)
11234 {
11235 tree identifier, attribs;
11236
11237 /* Look for the `namespace' keyword. */
11238 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11239
11240 /* Get the name of the namespace. We do not attempt to distinguish
11241 between an original-namespace-definition and an
11242 extension-namespace-definition at this point. The semantic
11243 analysis routines are responsible for that. */
11244 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11245 identifier = cp_parser_identifier (parser);
11246 else
11247 identifier = NULL_TREE;
11248
11249 /* Parse any specified attributes. */
11250 attribs = cp_parser_attributes_opt (parser);
11251
11252 /* Look for the `{' to start the namespace. */
11253 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11254 /* Start the namespace. */
11255 push_namespace_with_attribs (identifier, attribs);
11256 /* Parse the body of the namespace. */
11257 cp_parser_namespace_body (parser);
11258 /* Finish the namespace. */
11259 pop_namespace ();
11260 /* Look for the final `}'. */
11261 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11262 }
11263
11264 /* Parse a namespace-body.
11265
11266 namespace-body:
11267 declaration-seq [opt] */
11268
11269 static void
11270 cp_parser_namespace_body (cp_parser* parser)
11271 {
11272 cp_parser_declaration_seq_opt (parser);
11273 }
11274
11275 /* Parse a namespace-alias-definition.
11276
11277 namespace-alias-definition:
11278 namespace identifier = qualified-namespace-specifier ; */
11279
11280 static void
11281 cp_parser_namespace_alias_definition (cp_parser* parser)
11282 {
11283 tree identifier;
11284 tree namespace_specifier;
11285
11286 /* Look for the `namespace' keyword. */
11287 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11288 /* Look for the identifier. */
11289 identifier = cp_parser_identifier (parser);
11290 if (identifier == error_mark_node)
11291 return;
11292 /* Look for the `=' token. */
11293 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11294 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11295 {
11296 error ("%<namespace%> definition is not allowed here");
11297 /* Skip the definition. */
11298 cp_lexer_consume_token (parser->lexer);
11299 if (cp_parser_skip_to_closing_brace (parser))
11300 cp_lexer_consume_token (parser->lexer);
11301 return;
11302 }
11303 cp_parser_require (parser, CPP_EQ, "`='");
11304 /* Look for the qualified-namespace-specifier. */
11305 namespace_specifier
11306 = cp_parser_qualified_namespace_specifier (parser);
11307 /* Look for the `;' token. */
11308 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11309
11310 /* Register the alias in the symbol table. */
11311 do_namespace_alias (identifier, namespace_specifier);
11312 }
11313
11314 /* Parse a qualified-namespace-specifier.
11315
11316 qualified-namespace-specifier:
11317 :: [opt] nested-name-specifier [opt] namespace-name
11318
11319 Returns a NAMESPACE_DECL corresponding to the specified
11320 namespace. */
11321
11322 static tree
11323 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11324 {
11325 /* Look for the optional `::'. */
11326 cp_parser_global_scope_opt (parser,
11327 /*current_scope_valid_p=*/false);
11328
11329 /* Look for the optional nested-name-specifier. */
11330 cp_parser_nested_name_specifier_opt (parser,
11331 /*typename_keyword_p=*/false,
11332 /*check_dependency_p=*/true,
11333 /*type_p=*/false,
11334 /*is_declaration=*/true);
11335
11336 return cp_parser_namespace_name (parser);
11337 }
11338
11339 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11340 access declaration.
11341
11342 using-declaration:
11343 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11344 using :: unqualified-id ;
11345
11346 access-declaration:
11347 qualified-id ;
11348
11349 */
11350
11351 static bool
11352 cp_parser_using_declaration (cp_parser* parser,
11353 bool access_declaration_p)
11354 {
11355 cp_token *token;
11356 bool typename_p = false;
11357 bool global_scope_p;
11358 tree decl;
11359 tree identifier;
11360 tree qscope;
11361
11362 if (access_declaration_p)
11363 cp_parser_parse_tentatively (parser);
11364 else
11365 {
11366 /* Look for the `using' keyword. */
11367 cp_parser_require_keyword (parser, RID_USING, "`using'");
11368
11369 /* Peek at the next token. */
11370 token = cp_lexer_peek_token (parser->lexer);
11371 /* See if it's `typename'. */
11372 if (token->keyword == RID_TYPENAME)
11373 {
11374 /* Remember that we've seen it. */
11375 typename_p = true;
11376 /* Consume the `typename' token. */
11377 cp_lexer_consume_token (parser->lexer);
11378 }
11379 }
11380
11381 /* Look for the optional global scope qualification. */
11382 global_scope_p
11383 = (cp_parser_global_scope_opt (parser,
11384 /*current_scope_valid_p=*/false)
11385 != NULL_TREE);
11386
11387 /* If we saw `typename', or didn't see `::', then there must be a
11388 nested-name-specifier present. */
11389 if (typename_p || !global_scope_p)
11390 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11391 /*check_dependency_p=*/true,
11392 /*type_p=*/false,
11393 /*is_declaration=*/true);
11394 /* Otherwise, we could be in either of the two productions. In that
11395 case, treat the nested-name-specifier as optional. */
11396 else
11397 qscope = cp_parser_nested_name_specifier_opt (parser,
11398 /*typename_keyword_p=*/false,
11399 /*check_dependency_p=*/true,
11400 /*type_p=*/false,
11401 /*is_declaration=*/true);
11402 if (!qscope)
11403 qscope = global_namespace;
11404
11405 if (access_declaration_p && cp_parser_error_occurred (parser))
11406 /* Something has already gone wrong; there's no need to parse
11407 further. Since an error has occurred, the return value of
11408 cp_parser_parse_definitely will be false, as required. */
11409 return cp_parser_parse_definitely (parser);
11410
11411 /* Parse the unqualified-id. */
11412 identifier = cp_parser_unqualified_id (parser,
11413 /*template_keyword_p=*/false,
11414 /*check_dependency_p=*/true,
11415 /*declarator_p=*/true,
11416 /*optional_p=*/false);
11417
11418 if (access_declaration_p)
11419 {
11420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11421 cp_parser_simulate_error (parser);
11422 if (!cp_parser_parse_definitely (parser))
11423 return false;
11424 }
11425
11426 /* The function we call to handle a using-declaration is different
11427 depending on what scope we are in. */
11428 if (qscope == error_mark_node || identifier == error_mark_node)
11429 ;
11430 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11431 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11432 /* [namespace.udecl]
11433
11434 A using declaration shall not name a template-id. */
11435 error ("a template-id may not appear in a using-declaration");
11436 else
11437 {
11438 if (at_class_scope_p ())
11439 {
11440 /* Create the USING_DECL. */
11441 decl = do_class_using_decl (parser->scope, identifier);
11442 /* Add it to the list of members in this class. */
11443 finish_member_declaration (decl);
11444 }
11445 else
11446 {
11447 decl = cp_parser_lookup_name_simple (parser, identifier);
11448 if (decl == error_mark_node)
11449 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11450 else if (!at_namespace_scope_p ())
11451 do_local_using_decl (decl, qscope, identifier);
11452 else
11453 do_toplevel_using_decl (decl, qscope, identifier);
11454 }
11455 }
11456
11457 /* Look for the final `;'. */
11458 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11459
11460 return true;
11461 }
11462
11463 /* Parse a using-directive.
11464
11465 using-directive:
11466 using namespace :: [opt] nested-name-specifier [opt]
11467 namespace-name ; */
11468
11469 static void
11470 cp_parser_using_directive (cp_parser* parser)
11471 {
11472 tree namespace_decl;
11473 tree attribs;
11474
11475 /* Look for the `using' keyword. */
11476 cp_parser_require_keyword (parser, RID_USING, "`using'");
11477 /* And the `namespace' keyword. */
11478 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11479 /* Look for the optional `::' operator. */
11480 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11481 /* And the optional nested-name-specifier. */
11482 cp_parser_nested_name_specifier_opt (parser,
11483 /*typename_keyword_p=*/false,
11484 /*check_dependency_p=*/true,
11485 /*type_p=*/false,
11486 /*is_declaration=*/true);
11487 /* Get the namespace being used. */
11488 namespace_decl = cp_parser_namespace_name (parser);
11489 /* And any specified attributes. */
11490 attribs = cp_parser_attributes_opt (parser);
11491 /* Update the symbol table. */
11492 parse_using_directive (namespace_decl, attribs);
11493 /* Look for the final `;'. */
11494 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11495 }
11496
11497 /* Parse an asm-definition.
11498
11499 asm-definition:
11500 asm ( string-literal ) ;
11501
11502 GNU Extension:
11503
11504 asm-definition:
11505 asm volatile [opt] ( string-literal ) ;
11506 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11507 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11508 : asm-operand-list [opt] ) ;
11509 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11510 : asm-operand-list [opt]
11511 : asm-operand-list [opt] ) ; */
11512
11513 static void
11514 cp_parser_asm_definition (cp_parser* parser)
11515 {
11516 tree string;
11517 tree outputs = NULL_TREE;
11518 tree inputs = NULL_TREE;
11519 tree clobbers = NULL_TREE;
11520 tree asm_stmt;
11521 bool volatile_p = false;
11522 bool extended_p = false;
11523
11524 /* Look for the `asm' keyword. */
11525 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11526 /* See if the next token is `volatile'. */
11527 if (cp_parser_allow_gnu_extensions_p (parser)
11528 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11529 {
11530 /* Remember that we saw the `volatile' keyword. */
11531 volatile_p = true;
11532 /* Consume the token. */
11533 cp_lexer_consume_token (parser->lexer);
11534 }
11535 /* Look for the opening `('. */
11536 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11537 return;
11538 /* Look for the string. */
11539 string = cp_parser_string_literal (parser, false, false);
11540 if (string == error_mark_node)
11541 {
11542 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11543 /*consume_paren=*/true);
11544 return;
11545 }
11546
11547 /* If we're allowing GNU extensions, check for the extended assembly
11548 syntax. Unfortunately, the `:' tokens need not be separated by
11549 a space in C, and so, for compatibility, we tolerate that here
11550 too. Doing that means that we have to treat the `::' operator as
11551 two `:' tokens. */
11552 if (cp_parser_allow_gnu_extensions_p (parser)
11553 && parser->in_function_body
11554 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11555 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11556 {
11557 bool inputs_p = false;
11558 bool clobbers_p = false;
11559
11560 /* The extended syntax was used. */
11561 extended_p = true;
11562
11563 /* Look for outputs. */
11564 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11565 {
11566 /* Consume the `:'. */
11567 cp_lexer_consume_token (parser->lexer);
11568 /* Parse the output-operands. */
11569 if (cp_lexer_next_token_is_not (parser->lexer,
11570 CPP_COLON)
11571 && cp_lexer_next_token_is_not (parser->lexer,
11572 CPP_SCOPE)
11573 && cp_lexer_next_token_is_not (parser->lexer,
11574 CPP_CLOSE_PAREN))
11575 outputs = cp_parser_asm_operand_list (parser);
11576 }
11577 /* If the next token is `::', there are no outputs, and the
11578 next token is the beginning of the inputs. */
11579 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11580 /* The inputs are coming next. */
11581 inputs_p = true;
11582
11583 /* Look for inputs. */
11584 if (inputs_p
11585 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11586 {
11587 /* Consume the `:' or `::'. */
11588 cp_lexer_consume_token (parser->lexer);
11589 /* Parse the output-operands. */
11590 if (cp_lexer_next_token_is_not (parser->lexer,
11591 CPP_COLON)
11592 && cp_lexer_next_token_is_not (parser->lexer,
11593 CPP_CLOSE_PAREN))
11594 inputs = cp_parser_asm_operand_list (parser);
11595 }
11596 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11597 /* The clobbers are coming next. */
11598 clobbers_p = true;
11599
11600 /* Look for clobbers. */
11601 if (clobbers_p
11602 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11603 {
11604 /* Consume the `:' or `::'. */
11605 cp_lexer_consume_token (parser->lexer);
11606 /* Parse the clobbers. */
11607 if (cp_lexer_next_token_is_not (parser->lexer,
11608 CPP_CLOSE_PAREN))
11609 clobbers = cp_parser_asm_clobber_list (parser);
11610 }
11611 }
11612 /* Look for the closing `)'. */
11613 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11614 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11615 /*consume_paren=*/true);
11616 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11617
11618 /* Create the ASM_EXPR. */
11619 if (parser->in_function_body)
11620 {
11621 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11622 inputs, clobbers);
11623 /* If the extended syntax was not used, mark the ASM_EXPR. */
11624 if (!extended_p)
11625 {
11626 tree temp = asm_stmt;
11627 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11628 temp = TREE_OPERAND (temp, 0);
11629
11630 ASM_INPUT_P (temp) = 1;
11631 }
11632 }
11633 else
11634 cgraph_add_asm_node (string);
11635 }
11636
11637 /* Declarators [gram.dcl.decl] */
11638
11639 /* Parse an init-declarator.
11640
11641 init-declarator:
11642 declarator initializer [opt]
11643
11644 GNU Extension:
11645
11646 init-declarator:
11647 declarator asm-specification [opt] attributes [opt] initializer [opt]
11648
11649 function-definition:
11650 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11651 function-body
11652 decl-specifier-seq [opt] declarator function-try-block
11653
11654 GNU Extension:
11655
11656 function-definition:
11657 __extension__ function-definition
11658
11659 The DECL_SPECIFIERS apply to this declarator. Returns a
11660 representation of the entity declared. If MEMBER_P is TRUE, then
11661 this declarator appears in a class scope. The new DECL created by
11662 this declarator is returned.
11663
11664 The CHECKS are access checks that should be performed once we know
11665 what entity is being declared (and, therefore, what classes have
11666 befriended it).
11667
11668 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11669 for a function-definition here as well. If the declarator is a
11670 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11671 be TRUE upon return. By that point, the function-definition will
11672 have been completely parsed.
11673
11674 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11675 is FALSE. */
11676
11677 static tree
11678 cp_parser_init_declarator (cp_parser* parser,
11679 cp_decl_specifier_seq *decl_specifiers,
11680 VEC (deferred_access_check,gc)* checks,
11681 bool function_definition_allowed_p,
11682 bool member_p,
11683 int declares_class_or_enum,
11684 bool* function_definition_p)
11685 {
11686 cp_token *token;
11687 cp_declarator *declarator;
11688 tree prefix_attributes;
11689 tree attributes;
11690 tree asm_specification;
11691 tree initializer;
11692 tree decl = NULL_TREE;
11693 tree scope;
11694 bool is_initialized;
11695 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11696 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11697 "(...)". */
11698 enum cpp_ttype initialization_kind;
11699 bool is_parenthesized_init = false;
11700 bool is_non_constant_init;
11701 int ctor_dtor_or_conv_p;
11702 bool friend_p;
11703 tree pushed_scope = NULL;
11704
11705 /* Gather the attributes that were provided with the
11706 decl-specifiers. */
11707 prefix_attributes = decl_specifiers->attributes;
11708
11709 /* Assume that this is not the declarator for a function
11710 definition. */
11711 if (function_definition_p)
11712 *function_definition_p = false;
11713
11714 /* Defer access checks while parsing the declarator; we cannot know
11715 what names are accessible until we know what is being
11716 declared. */
11717 resume_deferring_access_checks ();
11718
11719 /* Parse the declarator. */
11720 declarator
11721 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11722 &ctor_dtor_or_conv_p,
11723 /*parenthesized_p=*/NULL,
11724 /*member_p=*/false);
11725 /* Gather up the deferred checks. */
11726 stop_deferring_access_checks ();
11727
11728 /* If the DECLARATOR was erroneous, there's no need to go
11729 further. */
11730 if (declarator == cp_error_declarator)
11731 return error_mark_node;
11732
11733 /* Check that the number of template-parameter-lists is OK. */
11734 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11735 return error_mark_node;
11736
11737 if (declares_class_or_enum & 2)
11738 cp_parser_check_for_definition_in_return_type (declarator,
11739 decl_specifiers->type);
11740
11741 /* Figure out what scope the entity declared by the DECLARATOR is
11742 located in. `grokdeclarator' sometimes changes the scope, so
11743 we compute it now. */
11744 scope = get_scope_of_declarator (declarator);
11745
11746 /* If we're allowing GNU extensions, look for an asm-specification
11747 and attributes. */
11748 if (cp_parser_allow_gnu_extensions_p (parser))
11749 {
11750 /* Look for an asm-specification. */
11751 asm_specification = cp_parser_asm_specification_opt (parser);
11752 /* And attributes. */
11753 attributes = cp_parser_attributes_opt (parser);
11754 }
11755 else
11756 {
11757 asm_specification = NULL_TREE;
11758 attributes = NULL_TREE;
11759 }
11760
11761 /* Peek at the next token. */
11762 token = cp_lexer_peek_token (parser->lexer);
11763 /* Check to see if the token indicates the start of a
11764 function-definition. */
11765 if (cp_parser_token_starts_function_definition_p (token))
11766 {
11767 if (!function_definition_allowed_p)
11768 {
11769 /* If a function-definition should not appear here, issue an
11770 error message. */
11771 cp_parser_error (parser,
11772 "a function-definition is not allowed here");
11773 return error_mark_node;
11774 }
11775 else
11776 {
11777 /* Neither attributes nor an asm-specification are allowed
11778 on a function-definition. */
11779 if (asm_specification)
11780 error ("an asm-specification is not allowed on a function-definition");
11781 if (attributes)
11782 error ("attributes are not allowed on a function-definition");
11783 /* This is a function-definition. */
11784 *function_definition_p = true;
11785
11786 /* Parse the function definition. */
11787 if (member_p)
11788 decl = cp_parser_save_member_function_body (parser,
11789 decl_specifiers,
11790 declarator,
11791 prefix_attributes);
11792 else
11793 decl
11794 = (cp_parser_function_definition_from_specifiers_and_declarator
11795 (parser, decl_specifiers, prefix_attributes, declarator));
11796
11797 return decl;
11798 }
11799 }
11800
11801 /* [dcl.dcl]
11802
11803 Only in function declarations for constructors, destructors, and
11804 type conversions can the decl-specifier-seq be omitted.
11805
11806 We explicitly postpone this check past the point where we handle
11807 function-definitions because we tolerate function-definitions
11808 that are missing their return types in some modes. */
11809 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11810 {
11811 cp_parser_error (parser,
11812 "expected constructor, destructor, or type conversion");
11813 return error_mark_node;
11814 }
11815
11816 /* An `=' or an `(' indicates an initializer. */
11817 if (token->type == CPP_EQ
11818 || token->type == CPP_OPEN_PAREN)
11819 {
11820 is_initialized = true;
11821 initialization_kind = token->type;
11822 }
11823 else
11824 {
11825 /* If the init-declarator isn't initialized and isn't followed by a
11826 `,' or `;', it's not a valid init-declarator. */
11827 if (token->type != CPP_COMMA
11828 && token->type != CPP_SEMICOLON)
11829 {
11830 cp_parser_error (parser, "expected initializer");
11831 return error_mark_node;
11832 }
11833 is_initialized = false;
11834 initialization_kind = CPP_EOF;
11835 }
11836
11837 /* Because start_decl has side-effects, we should only call it if we
11838 know we're going ahead. By this point, we know that we cannot
11839 possibly be looking at any other construct. */
11840 cp_parser_commit_to_tentative_parse (parser);
11841
11842 /* If the decl specifiers were bad, issue an error now that we're
11843 sure this was intended to be a declarator. Then continue
11844 declaring the variable(s), as int, to try to cut down on further
11845 errors. */
11846 if (decl_specifiers->any_specifiers_p
11847 && decl_specifiers->type == error_mark_node)
11848 {
11849 cp_parser_error (parser, "invalid type in declaration");
11850 decl_specifiers->type = integer_type_node;
11851 }
11852
11853 /* Check to see whether or not this declaration is a friend. */
11854 friend_p = cp_parser_friend_p (decl_specifiers);
11855
11856 /* Enter the newly declared entry in the symbol table. If we're
11857 processing a declaration in a class-specifier, we wait until
11858 after processing the initializer. */
11859 if (!member_p)
11860 {
11861 if (parser->in_unbraced_linkage_specification_p)
11862 decl_specifiers->storage_class = sc_extern;
11863 decl = start_decl (declarator, decl_specifiers,
11864 is_initialized, attributes, prefix_attributes,
11865 &pushed_scope);
11866 }
11867 else if (scope)
11868 /* Enter the SCOPE. That way unqualified names appearing in the
11869 initializer will be looked up in SCOPE. */
11870 pushed_scope = push_scope (scope);
11871
11872 /* Perform deferred access control checks, now that we know in which
11873 SCOPE the declared entity resides. */
11874 if (!member_p && decl)
11875 {
11876 tree saved_current_function_decl = NULL_TREE;
11877
11878 /* If the entity being declared is a function, pretend that we
11879 are in its scope. If it is a `friend', it may have access to
11880 things that would not otherwise be accessible. */
11881 if (TREE_CODE (decl) == FUNCTION_DECL)
11882 {
11883 saved_current_function_decl = current_function_decl;
11884 current_function_decl = decl;
11885 }
11886
11887 /* Perform access checks for template parameters. */
11888 cp_parser_perform_template_parameter_access_checks (checks);
11889
11890 /* Perform the access control checks for the declarator and the
11891 the decl-specifiers. */
11892 perform_deferred_access_checks ();
11893
11894 /* Restore the saved value. */
11895 if (TREE_CODE (decl) == FUNCTION_DECL)
11896 current_function_decl = saved_current_function_decl;
11897 }
11898
11899 /* Parse the initializer. */
11900 initializer = NULL_TREE;
11901 is_parenthesized_init = false;
11902 is_non_constant_init = true;
11903 if (is_initialized)
11904 {
11905 if (function_declarator_p (declarator))
11906 {
11907 if (initialization_kind == CPP_EQ)
11908 initializer = cp_parser_pure_specifier (parser);
11909 else
11910 {
11911 /* If the declaration was erroneous, we don't really
11912 know what the user intended, so just silently
11913 consume the initializer. */
11914 if (decl != error_mark_node)
11915 error ("initializer provided for function");
11916 cp_parser_skip_to_closing_parenthesis (parser,
11917 /*recovering=*/true,
11918 /*or_comma=*/false,
11919 /*consume_paren=*/true);
11920 }
11921 }
11922 else
11923 initializer = cp_parser_initializer (parser,
11924 &is_parenthesized_init,
11925 &is_non_constant_init);
11926 }
11927
11928 /* The old parser allows attributes to appear after a parenthesized
11929 initializer. Mark Mitchell proposed removing this functionality
11930 on the GCC mailing lists on 2002-08-13. This parser accepts the
11931 attributes -- but ignores them. */
11932 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11933 if (cp_parser_attributes_opt (parser))
11934 warning (OPT_Wattributes,
11935 "attributes after parenthesized initializer ignored");
11936
11937 /* For an in-class declaration, use `grokfield' to create the
11938 declaration. */
11939 if (member_p)
11940 {
11941 if (pushed_scope)
11942 {
11943 pop_scope (pushed_scope);
11944 pushed_scope = false;
11945 }
11946 decl = grokfield (declarator, decl_specifiers,
11947 initializer, !is_non_constant_init,
11948 /*asmspec=*/NULL_TREE,
11949 prefix_attributes);
11950 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11951 cp_parser_save_default_args (parser, decl);
11952 }
11953
11954 /* Finish processing the declaration. But, skip friend
11955 declarations. */
11956 if (!friend_p && decl && decl != error_mark_node)
11957 {
11958 cp_finish_decl (decl,
11959 initializer, !is_non_constant_init,
11960 asm_specification,
11961 /* If the initializer is in parentheses, then this is
11962 a direct-initialization, which means that an
11963 `explicit' constructor is OK. Otherwise, an
11964 `explicit' constructor cannot be used. */
11965 ((is_parenthesized_init || !is_initialized)
11966 ? 0 : LOOKUP_ONLYCONVERTING));
11967 }
11968 else if (flag_cpp0x && friend_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
11969 /* Core issue #226 (C++0x only): A default template-argument
11970 shall not be specified in a friend class template
11971 declaration. */
11972 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
11973 /*is_partial=*/0, /*is_friend_decl=*/1);
11974
11975 if (!friend_p && pushed_scope)
11976 pop_scope (pushed_scope);
11977
11978 return decl;
11979 }
11980
11981 /* Parse a declarator.
11982
11983 declarator:
11984 direct-declarator
11985 ptr-operator declarator
11986
11987 abstract-declarator:
11988 ptr-operator abstract-declarator [opt]
11989 direct-abstract-declarator
11990
11991 GNU Extensions:
11992
11993 declarator:
11994 attributes [opt] direct-declarator
11995 attributes [opt] ptr-operator declarator
11996
11997 abstract-declarator:
11998 attributes [opt] ptr-operator abstract-declarator [opt]
11999 attributes [opt] direct-abstract-declarator
12000
12001 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12002 detect constructor, destructor or conversion operators. It is set
12003 to -1 if the declarator is a name, and +1 if it is a
12004 function. Otherwise it is set to zero. Usually you just want to
12005 test for >0, but internally the negative value is used.
12006
12007 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12008 a decl-specifier-seq unless it declares a constructor, destructor,
12009 or conversion. It might seem that we could check this condition in
12010 semantic analysis, rather than parsing, but that makes it difficult
12011 to handle something like `f()'. We want to notice that there are
12012 no decl-specifiers, and therefore realize that this is an
12013 expression, not a declaration.)
12014
12015 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12016 the declarator is a direct-declarator of the form "(...)".
12017
12018 MEMBER_P is true iff this declarator is a member-declarator. */
12019
12020 static cp_declarator *
12021 cp_parser_declarator (cp_parser* parser,
12022 cp_parser_declarator_kind dcl_kind,
12023 int* ctor_dtor_or_conv_p,
12024 bool* parenthesized_p,
12025 bool member_p)
12026 {
12027 cp_token *token;
12028 cp_declarator *declarator;
12029 enum tree_code code;
12030 cp_cv_quals cv_quals;
12031 tree class_type;
12032 tree attributes = NULL_TREE;
12033
12034 /* Assume this is not a constructor, destructor, or type-conversion
12035 operator. */
12036 if (ctor_dtor_or_conv_p)
12037 *ctor_dtor_or_conv_p = 0;
12038
12039 if (cp_parser_allow_gnu_extensions_p (parser))
12040 attributes = cp_parser_attributes_opt (parser);
12041
12042 /* Peek at the next token. */
12043 token = cp_lexer_peek_token (parser->lexer);
12044
12045 /* Check for the ptr-operator production. */
12046 cp_parser_parse_tentatively (parser);
12047 /* Parse the ptr-operator. */
12048 code = cp_parser_ptr_operator (parser,
12049 &class_type,
12050 &cv_quals);
12051 /* If that worked, then we have a ptr-operator. */
12052 if (cp_parser_parse_definitely (parser))
12053 {
12054 /* If a ptr-operator was found, then this declarator was not
12055 parenthesized. */
12056 if (parenthesized_p)
12057 *parenthesized_p = true;
12058 /* The dependent declarator is optional if we are parsing an
12059 abstract-declarator. */
12060 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12061 cp_parser_parse_tentatively (parser);
12062
12063 /* Parse the dependent declarator. */
12064 declarator = cp_parser_declarator (parser, dcl_kind,
12065 /*ctor_dtor_or_conv_p=*/NULL,
12066 /*parenthesized_p=*/NULL,
12067 /*member_p=*/false);
12068
12069 /* If we are parsing an abstract-declarator, we must handle the
12070 case where the dependent declarator is absent. */
12071 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12072 && !cp_parser_parse_definitely (parser))
12073 declarator = NULL;
12074
12075 /* Build the representation of the ptr-operator. */
12076 if (class_type)
12077 declarator = make_ptrmem_declarator (cv_quals,
12078 class_type,
12079 declarator);
12080 else if (code == INDIRECT_REF)
12081 declarator = make_pointer_declarator (cv_quals, declarator);
12082 else
12083 declarator = make_reference_declarator (cv_quals, declarator);
12084 }
12085 /* Everything else is a direct-declarator. */
12086 else
12087 {
12088 if (parenthesized_p)
12089 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12090 CPP_OPEN_PAREN);
12091 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12092 ctor_dtor_or_conv_p,
12093 member_p);
12094 }
12095
12096 if (attributes && declarator && declarator != cp_error_declarator)
12097 declarator->attributes = attributes;
12098
12099 return declarator;
12100 }
12101
12102 /* Parse a direct-declarator or direct-abstract-declarator.
12103
12104 direct-declarator:
12105 declarator-id
12106 direct-declarator ( parameter-declaration-clause )
12107 cv-qualifier-seq [opt]
12108 exception-specification [opt]
12109 direct-declarator [ constant-expression [opt] ]
12110 ( declarator )
12111
12112 direct-abstract-declarator:
12113 direct-abstract-declarator [opt]
12114 ( parameter-declaration-clause )
12115 cv-qualifier-seq [opt]
12116 exception-specification [opt]
12117 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12118 ( abstract-declarator )
12119
12120 Returns a representation of the declarator. DCL_KIND is
12121 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12122 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12123 we are parsing a direct-declarator. It is
12124 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12125 of ambiguity we prefer an abstract declarator, as per
12126 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12127 cp_parser_declarator. */
12128
12129 static cp_declarator *
12130 cp_parser_direct_declarator (cp_parser* parser,
12131 cp_parser_declarator_kind dcl_kind,
12132 int* ctor_dtor_or_conv_p,
12133 bool member_p)
12134 {
12135 cp_token *token;
12136 cp_declarator *declarator = NULL;
12137 tree scope = NULL_TREE;
12138 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12139 bool saved_in_declarator_p = parser->in_declarator_p;
12140 bool first = true;
12141 tree pushed_scope = NULL_TREE;
12142
12143 while (true)
12144 {
12145 /* Peek at the next token. */
12146 token = cp_lexer_peek_token (parser->lexer);
12147 if (token->type == CPP_OPEN_PAREN)
12148 {
12149 /* This is either a parameter-declaration-clause, or a
12150 parenthesized declarator. When we know we are parsing a
12151 named declarator, it must be a parenthesized declarator
12152 if FIRST is true. For instance, `(int)' is a
12153 parameter-declaration-clause, with an omitted
12154 direct-abstract-declarator. But `((*))', is a
12155 parenthesized abstract declarator. Finally, when T is a
12156 template parameter `(T)' is a
12157 parameter-declaration-clause, and not a parenthesized
12158 named declarator.
12159
12160 We first try and parse a parameter-declaration-clause,
12161 and then try a nested declarator (if FIRST is true).
12162
12163 It is not an error for it not to be a
12164 parameter-declaration-clause, even when FIRST is
12165 false. Consider,
12166
12167 int i (int);
12168 int i (3);
12169
12170 The first is the declaration of a function while the
12171 second is a the definition of a variable, including its
12172 initializer.
12173
12174 Having seen only the parenthesis, we cannot know which of
12175 these two alternatives should be selected. Even more
12176 complex are examples like:
12177
12178 int i (int (a));
12179 int i (int (3));
12180
12181 The former is a function-declaration; the latter is a
12182 variable initialization.
12183
12184 Thus again, we try a parameter-declaration-clause, and if
12185 that fails, we back out and return. */
12186
12187 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12188 {
12189 cp_parameter_declarator *params;
12190 unsigned saved_num_template_parameter_lists;
12191
12192 /* In a member-declarator, the only valid interpretation
12193 of a parenthesis is the start of a
12194 parameter-declaration-clause. (It is invalid to
12195 initialize a static data member with a parenthesized
12196 initializer; only the "=" form of initialization is
12197 permitted.) */
12198 if (!member_p)
12199 cp_parser_parse_tentatively (parser);
12200
12201 /* Consume the `('. */
12202 cp_lexer_consume_token (parser->lexer);
12203 if (first)
12204 {
12205 /* If this is going to be an abstract declarator, we're
12206 in a declarator and we can't have default args. */
12207 parser->default_arg_ok_p = false;
12208 parser->in_declarator_p = true;
12209 }
12210
12211 /* Inside the function parameter list, surrounding
12212 template-parameter-lists do not apply. */
12213 saved_num_template_parameter_lists
12214 = parser->num_template_parameter_lists;
12215 parser->num_template_parameter_lists = 0;
12216
12217 /* Parse the parameter-declaration-clause. */
12218 params = cp_parser_parameter_declaration_clause (parser);
12219
12220 parser->num_template_parameter_lists
12221 = saved_num_template_parameter_lists;
12222
12223 /* If all went well, parse the cv-qualifier-seq and the
12224 exception-specification. */
12225 if (member_p || cp_parser_parse_definitely (parser))
12226 {
12227 cp_cv_quals cv_quals;
12228 tree exception_specification;
12229
12230 if (ctor_dtor_or_conv_p)
12231 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12232 first = false;
12233 /* Consume the `)'. */
12234 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12235
12236 /* Parse the cv-qualifier-seq. */
12237 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12238 /* And the exception-specification. */
12239 exception_specification
12240 = cp_parser_exception_specification_opt (parser);
12241
12242 /* Create the function-declarator. */
12243 declarator = make_call_declarator (declarator,
12244 params,
12245 cv_quals,
12246 exception_specification);
12247 /* Any subsequent parameter lists are to do with
12248 return type, so are not those of the declared
12249 function. */
12250 parser->default_arg_ok_p = false;
12251
12252 /* Repeat the main loop. */
12253 continue;
12254 }
12255 }
12256
12257 /* If this is the first, we can try a parenthesized
12258 declarator. */
12259 if (first)
12260 {
12261 bool saved_in_type_id_in_expr_p;
12262
12263 parser->default_arg_ok_p = saved_default_arg_ok_p;
12264 parser->in_declarator_p = saved_in_declarator_p;
12265
12266 /* Consume the `('. */
12267 cp_lexer_consume_token (parser->lexer);
12268 /* Parse the nested declarator. */
12269 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12270 parser->in_type_id_in_expr_p = true;
12271 declarator
12272 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12273 /*parenthesized_p=*/NULL,
12274 member_p);
12275 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12276 first = false;
12277 /* Expect a `)'. */
12278 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12279 declarator = cp_error_declarator;
12280 if (declarator == cp_error_declarator)
12281 break;
12282
12283 goto handle_declarator;
12284 }
12285 /* Otherwise, we must be done. */
12286 else
12287 break;
12288 }
12289 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12290 && token->type == CPP_OPEN_SQUARE)
12291 {
12292 /* Parse an array-declarator. */
12293 tree bounds;
12294
12295 if (ctor_dtor_or_conv_p)
12296 *ctor_dtor_or_conv_p = 0;
12297
12298 first = false;
12299 parser->default_arg_ok_p = false;
12300 parser->in_declarator_p = true;
12301 /* Consume the `['. */
12302 cp_lexer_consume_token (parser->lexer);
12303 /* Peek at the next token. */
12304 token = cp_lexer_peek_token (parser->lexer);
12305 /* If the next token is `]', then there is no
12306 constant-expression. */
12307 if (token->type != CPP_CLOSE_SQUARE)
12308 {
12309 bool non_constant_p;
12310
12311 bounds
12312 = cp_parser_constant_expression (parser,
12313 /*allow_non_constant=*/true,
12314 &non_constant_p);
12315 if (!non_constant_p)
12316 bounds = fold_non_dependent_expr (bounds);
12317 /* Normally, the array bound must be an integral constant
12318 expression. However, as an extension, we allow VLAs
12319 in function scopes. */
12320 else if (!parser->in_function_body)
12321 {
12322 error ("array bound is not an integer constant");
12323 bounds = error_mark_node;
12324 }
12325 }
12326 else
12327 bounds = NULL_TREE;
12328 /* Look for the closing `]'. */
12329 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12330 {
12331 declarator = cp_error_declarator;
12332 break;
12333 }
12334
12335 declarator = make_array_declarator (declarator, bounds);
12336 }
12337 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12338 {
12339 tree qualifying_scope;
12340 tree unqualified_name;
12341 special_function_kind sfk;
12342 bool abstract_ok;
12343 bool pack_expansion_p = false;
12344
12345 /* Parse a declarator-id */
12346 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12347 if (abstract_ok)
12348 {
12349 cp_parser_parse_tentatively (parser);
12350
12351 /* If we see an ellipsis, we should be looking at a
12352 parameter pack. */
12353 if (token->type == CPP_ELLIPSIS)
12354 {
12355 /* Consume the `...' */
12356 cp_lexer_consume_token (parser->lexer);
12357
12358 pack_expansion_p = true;
12359 }
12360 }
12361
12362 unqualified_name
12363 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12364 qualifying_scope = parser->scope;
12365 if (abstract_ok)
12366 {
12367 bool okay = false;
12368
12369 if (!unqualified_name && pack_expansion_p)
12370 {
12371 /* Check whether an error occurred. */
12372 okay = !cp_parser_error_occurred (parser);
12373
12374 /* We already consumed the ellipsis to mark a
12375 parameter pack, but we have no way to report it,
12376 so abort the tentative parse. We will be exiting
12377 immediately anyway. */
12378 cp_parser_abort_tentative_parse (parser);
12379 }
12380 else
12381 okay = cp_parser_parse_definitely (parser);
12382
12383 if (!okay)
12384 unqualified_name = error_mark_node;
12385 else if (unqualified_name
12386 && (qualifying_scope
12387 || (TREE_CODE (unqualified_name)
12388 != IDENTIFIER_NODE)))
12389 {
12390 cp_parser_error (parser, "expected unqualified-id");
12391 unqualified_name = error_mark_node;
12392 }
12393 }
12394
12395 if (!unqualified_name)
12396 return NULL;
12397 if (unqualified_name == error_mark_node)
12398 {
12399 declarator = cp_error_declarator;
12400 pack_expansion_p = false;
12401 declarator->parameter_pack_p = false;
12402 break;
12403 }
12404
12405 if (qualifying_scope && at_namespace_scope_p ()
12406 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12407 {
12408 /* In the declaration of a member of a template class
12409 outside of the class itself, the SCOPE will sometimes
12410 be a TYPENAME_TYPE. For example, given:
12411
12412 template <typename T>
12413 int S<T>::R::i = 3;
12414
12415 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12416 this context, we must resolve S<T>::R to an ordinary
12417 type, rather than a typename type.
12418
12419 The reason we normally avoid resolving TYPENAME_TYPEs
12420 is that a specialization of `S' might render
12421 `S<T>::R' not a type. However, if `S' is
12422 specialized, then this `i' will not be used, so there
12423 is no harm in resolving the types here. */
12424 tree type;
12425
12426 /* Resolve the TYPENAME_TYPE. */
12427 type = resolve_typename_type (qualifying_scope,
12428 /*only_current_p=*/false);
12429 /* If that failed, the declarator is invalid. */
12430 if (type == error_mark_node)
12431 error ("%<%T::%E%> is not a type",
12432 TYPE_CONTEXT (qualifying_scope),
12433 TYPE_IDENTIFIER (qualifying_scope));
12434 qualifying_scope = type;
12435 }
12436
12437 sfk = sfk_none;
12438
12439 if (unqualified_name)
12440 {
12441 tree class_type;
12442
12443 if (qualifying_scope
12444 && CLASS_TYPE_P (qualifying_scope))
12445 class_type = qualifying_scope;
12446 else
12447 class_type = current_class_type;
12448
12449 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12450 {
12451 tree name_type = TREE_TYPE (unqualified_name);
12452 if (class_type && same_type_p (name_type, class_type))
12453 {
12454 if (qualifying_scope
12455 && CLASSTYPE_USE_TEMPLATE (name_type))
12456 {
12457 error ("invalid use of constructor as a template");
12458 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12459 "name the constructor in a qualified name",
12460 class_type,
12461 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12462 class_type, name_type);
12463 declarator = cp_error_declarator;
12464 break;
12465 }
12466 else
12467 unqualified_name = constructor_name (class_type);
12468 }
12469 else
12470 {
12471 /* We do not attempt to print the declarator
12472 here because we do not have enough
12473 information about its original syntactic
12474 form. */
12475 cp_parser_error (parser, "invalid declarator");
12476 declarator = cp_error_declarator;
12477 break;
12478 }
12479 }
12480
12481 if (class_type)
12482 {
12483 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12484 sfk = sfk_destructor;
12485 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12486 sfk = sfk_conversion;
12487 else if (/* There's no way to declare a constructor
12488 for an anonymous type, even if the type
12489 got a name for linkage purposes. */
12490 !TYPE_WAS_ANONYMOUS (class_type)
12491 && constructor_name_p (unqualified_name,
12492 class_type))
12493 {
12494 unqualified_name = constructor_name (class_type);
12495 sfk = sfk_constructor;
12496 }
12497
12498 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12499 *ctor_dtor_or_conv_p = -1;
12500 }
12501 }
12502 declarator = make_id_declarator (qualifying_scope,
12503 unqualified_name,
12504 sfk);
12505 declarator->id_loc = token->location;
12506 declarator->parameter_pack_p = pack_expansion_p;
12507
12508 if (pack_expansion_p)
12509 maybe_warn_variadic_templates ();
12510
12511 handle_declarator:;
12512 scope = get_scope_of_declarator (declarator);
12513 if (scope)
12514 /* Any names that appear after the declarator-id for a
12515 member are looked up in the containing scope. */
12516 pushed_scope = push_scope (scope);
12517 parser->in_declarator_p = true;
12518 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12519 || (declarator && declarator->kind == cdk_id))
12520 /* Default args are only allowed on function
12521 declarations. */
12522 parser->default_arg_ok_p = saved_default_arg_ok_p;
12523 else
12524 parser->default_arg_ok_p = false;
12525
12526 first = false;
12527 }
12528 /* We're done. */
12529 else
12530 break;
12531 }
12532
12533 /* For an abstract declarator, we might wind up with nothing at this
12534 point. That's an error; the declarator is not optional. */
12535 if (!declarator)
12536 cp_parser_error (parser, "expected declarator");
12537
12538 /* If we entered a scope, we must exit it now. */
12539 if (pushed_scope)
12540 pop_scope (pushed_scope);
12541
12542 parser->default_arg_ok_p = saved_default_arg_ok_p;
12543 parser->in_declarator_p = saved_in_declarator_p;
12544
12545 return declarator;
12546 }
12547
12548 /* Parse a ptr-operator.
12549
12550 ptr-operator:
12551 * cv-qualifier-seq [opt]
12552 &
12553 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12554
12555 GNU Extension:
12556
12557 ptr-operator:
12558 & cv-qualifier-seq [opt]
12559
12560 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12561 Returns ADDR_EXPR if a reference was used. In the case of a
12562 pointer-to-member, *TYPE is filled in with the TYPE containing the
12563 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12564 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12565 ERROR_MARK if an error occurred. */
12566
12567 static enum tree_code
12568 cp_parser_ptr_operator (cp_parser* parser,
12569 tree* type,
12570 cp_cv_quals *cv_quals)
12571 {
12572 enum tree_code code = ERROR_MARK;
12573 cp_token *token;
12574
12575 /* Assume that it's not a pointer-to-member. */
12576 *type = NULL_TREE;
12577 /* And that there are no cv-qualifiers. */
12578 *cv_quals = TYPE_UNQUALIFIED;
12579
12580 /* Peek at the next token. */
12581 token = cp_lexer_peek_token (parser->lexer);
12582 /* If it's a `*' or `&' we have a pointer or reference. */
12583 if (token->type == CPP_MULT || token->type == CPP_AND)
12584 {
12585 /* Remember which ptr-operator we were processing. */
12586 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12587
12588 /* Consume the `*' or `&'. */
12589 cp_lexer_consume_token (parser->lexer);
12590
12591 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12592 `&', if we are allowing GNU extensions. (The only qualifier
12593 that can legally appear after `&' is `restrict', but that is
12594 enforced during semantic analysis. */
12595 if (code == INDIRECT_REF
12596 || cp_parser_allow_gnu_extensions_p (parser))
12597 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12598 }
12599 else
12600 {
12601 /* Try the pointer-to-member case. */
12602 cp_parser_parse_tentatively (parser);
12603 /* Look for the optional `::' operator. */
12604 cp_parser_global_scope_opt (parser,
12605 /*current_scope_valid_p=*/false);
12606 /* Look for the nested-name specifier. */
12607 cp_parser_nested_name_specifier (parser,
12608 /*typename_keyword_p=*/false,
12609 /*check_dependency_p=*/true,
12610 /*type_p=*/false,
12611 /*is_declaration=*/false);
12612 /* If we found it, and the next token is a `*', then we are
12613 indeed looking at a pointer-to-member operator. */
12614 if (!cp_parser_error_occurred (parser)
12615 && cp_parser_require (parser, CPP_MULT, "`*'"))
12616 {
12617 /* Indicate that the `*' operator was used. */
12618 code = INDIRECT_REF;
12619
12620 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12621 error ("%qD is a namespace", parser->scope);
12622 else
12623 {
12624 /* The type of which the member is a member is given by the
12625 current SCOPE. */
12626 *type = parser->scope;
12627 /* The next name will not be qualified. */
12628 parser->scope = NULL_TREE;
12629 parser->qualifying_scope = NULL_TREE;
12630 parser->object_scope = NULL_TREE;
12631 /* Look for the optional cv-qualifier-seq. */
12632 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12633 }
12634 }
12635 /* If that didn't work we don't have a ptr-operator. */
12636 if (!cp_parser_parse_definitely (parser))
12637 cp_parser_error (parser, "expected ptr-operator");
12638 }
12639
12640 return code;
12641 }
12642
12643 /* Parse an (optional) cv-qualifier-seq.
12644
12645 cv-qualifier-seq:
12646 cv-qualifier cv-qualifier-seq [opt]
12647
12648 cv-qualifier:
12649 const
12650 volatile
12651
12652 GNU Extension:
12653
12654 cv-qualifier:
12655 __restrict__
12656
12657 Returns a bitmask representing the cv-qualifiers. */
12658
12659 static cp_cv_quals
12660 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12661 {
12662 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12663
12664 while (true)
12665 {
12666 cp_token *token;
12667 cp_cv_quals cv_qualifier;
12668
12669 /* Peek at the next token. */
12670 token = cp_lexer_peek_token (parser->lexer);
12671 /* See if it's a cv-qualifier. */
12672 switch (token->keyword)
12673 {
12674 case RID_CONST:
12675 cv_qualifier = TYPE_QUAL_CONST;
12676 break;
12677
12678 case RID_VOLATILE:
12679 cv_qualifier = TYPE_QUAL_VOLATILE;
12680 break;
12681
12682 case RID_RESTRICT:
12683 cv_qualifier = TYPE_QUAL_RESTRICT;
12684 break;
12685
12686 default:
12687 cv_qualifier = TYPE_UNQUALIFIED;
12688 break;
12689 }
12690
12691 if (!cv_qualifier)
12692 break;
12693
12694 if (cv_quals & cv_qualifier)
12695 {
12696 error ("duplicate cv-qualifier");
12697 cp_lexer_purge_token (parser->lexer);
12698 }
12699 else
12700 {
12701 cp_lexer_consume_token (parser->lexer);
12702 cv_quals |= cv_qualifier;
12703 }
12704 }
12705
12706 return cv_quals;
12707 }
12708
12709 /* Parse a declarator-id.
12710
12711 declarator-id:
12712 id-expression
12713 :: [opt] nested-name-specifier [opt] type-name
12714
12715 In the `id-expression' case, the value returned is as for
12716 cp_parser_id_expression if the id-expression was an unqualified-id.
12717 If the id-expression was a qualified-id, then a SCOPE_REF is
12718 returned. The first operand is the scope (either a NAMESPACE_DECL
12719 or TREE_TYPE), but the second is still just a representation of an
12720 unqualified-id. */
12721
12722 static tree
12723 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12724 {
12725 tree id;
12726 /* The expression must be an id-expression. Assume that qualified
12727 names are the names of types so that:
12728
12729 template <class T>
12730 int S<T>::R::i = 3;
12731
12732 will work; we must treat `S<T>::R' as the name of a type.
12733 Similarly, assume that qualified names are templates, where
12734 required, so that:
12735
12736 template <class T>
12737 int S<T>::R<T>::i = 3;
12738
12739 will work, too. */
12740 id = cp_parser_id_expression (parser,
12741 /*template_keyword_p=*/false,
12742 /*check_dependency_p=*/false,
12743 /*template_p=*/NULL,
12744 /*declarator_p=*/true,
12745 optional_p);
12746 if (id && BASELINK_P (id))
12747 id = BASELINK_FUNCTIONS (id);
12748 return id;
12749 }
12750
12751 /* Parse a type-id.
12752
12753 type-id:
12754 type-specifier-seq abstract-declarator [opt]
12755
12756 Returns the TYPE specified. */
12757
12758 static tree
12759 cp_parser_type_id (cp_parser* parser)
12760 {
12761 cp_decl_specifier_seq type_specifier_seq;
12762 cp_declarator *abstract_declarator;
12763
12764 /* Parse the type-specifier-seq. */
12765 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12766 &type_specifier_seq);
12767 if (type_specifier_seq.type == error_mark_node)
12768 return error_mark_node;
12769
12770 /* There might or might not be an abstract declarator. */
12771 cp_parser_parse_tentatively (parser);
12772 /* Look for the declarator. */
12773 abstract_declarator
12774 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12775 /*parenthesized_p=*/NULL,
12776 /*member_p=*/false);
12777 /* Check to see if there really was a declarator. */
12778 if (!cp_parser_parse_definitely (parser))
12779 abstract_declarator = NULL;
12780
12781 return groktypename (&type_specifier_seq, abstract_declarator);
12782 }
12783
12784 /* Parse a type-specifier-seq.
12785
12786 type-specifier-seq:
12787 type-specifier type-specifier-seq [opt]
12788
12789 GNU extension:
12790
12791 type-specifier-seq:
12792 attributes type-specifier-seq [opt]
12793
12794 If IS_CONDITION is true, we are at the start of a "condition",
12795 e.g., we've just seen "if (".
12796
12797 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12798
12799 static void
12800 cp_parser_type_specifier_seq (cp_parser* parser,
12801 bool is_condition,
12802 cp_decl_specifier_seq *type_specifier_seq)
12803 {
12804 bool seen_type_specifier = false;
12805 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12806
12807 /* Clear the TYPE_SPECIFIER_SEQ. */
12808 clear_decl_specs (type_specifier_seq);
12809
12810 /* Parse the type-specifiers and attributes. */
12811 while (true)
12812 {
12813 tree type_specifier;
12814 bool is_cv_qualifier;
12815
12816 /* Check for attributes first. */
12817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12818 {
12819 type_specifier_seq->attributes =
12820 chainon (type_specifier_seq->attributes,
12821 cp_parser_attributes_opt (parser));
12822 continue;
12823 }
12824
12825 /* Look for the type-specifier. */
12826 type_specifier = cp_parser_type_specifier (parser,
12827 flags,
12828 type_specifier_seq,
12829 /*is_declaration=*/false,
12830 NULL,
12831 &is_cv_qualifier);
12832 if (!type_specifier)
12833 {
12834 /* If the first type-specifier could not be found, this is not a
12835 type-specifier-seq at all. */
12836 if (!seen_type_specifier)
12837 {
12838 cp_parser_error (parser, "expected type-specifier");
12839 type_specifier_seq->type = error_mark_node;
12840 return;
12841 }
12842 /* If subsequent type-specifiers could not be found, the
12843 type-specifier-seq is complete. */
12844 break;
12845 }
12846
12847 seen_type_specifier = true;
12848 /* The standard says that a condition can be:
12849
12850 type-specifier-seq declarator = assignment-expression
12851
12852 However, given:
12853
12854 struct S {};
12855 if (int S = ...)
12856
12857 we should treat the "S" as a declarator, not as a
12858 type-specifier. The standard doesn't say that explicitly for
12859 type-specifier-seq, but it does say that for
12860 decl-specifier-seq in an ordinary declaration. Perhaps it
12861 would be clearer just to allow a decl-specifier-seq here, and
12862 then add a semantic restriction that if any decl-specifiers
12863 that are not type-specifiers appear, the program is invalid. */
12864 if (is_condition && !is_cv_qualifier)
12865 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12866 }
12867
12868 cp_parser_check_decl_spec (type_specifier_seq);
12869 }
12870
12871 /* Parse a parameter-declaration-clause.
12872
12873 parameter-declaration-clause:
12874 parameter-declaration-list [opt] ... [opt]
12875 parameter-declaration-list , ...
12876
12877 Returns a representation for the parameter declarations. A return
12878 value of NULL indicates a parameter-declaration-clause consisting
12879 only of an ellipsis. */
12880
12881 static cp_parameter_declarator *
12882 cp_parser_parameter_declaration_clause (cp_parser* parser)
12883 {
12884 cp_parameter_declarator *parameters;
12885 cp_token *token;
12886 bool ellipsis_p;
12887 bool is_error;
12888
12889 /* Peek at the next token. */
12890 token = cp_lexer_peek_token (parser->lexer);
12891 /* Check for trivial parameter-declaration-clauses. */
12892 if (token->type == CPP_ELLIPSIS)
12893 {
12894 /* Consume the `...' token. */
12895 cp_lexer_consume_token (parser->lexer);
12896 return NULL;
12897 }
12898 else if (token->type == CPP_CLOSE_PAREN)
12899 /* There are no parameters. */
12900 {
12901 #ifndef NO_IMPLICIT_EXTERN_C
12902 if (in_system_header && current_class_type == NULL
12903 && current_lang_name == lang_name_c)
12904 return NULL;
12905 else
12906 #endif
12907 return no_parameters;
12908 }
12909 /* Check for `(void)', too, which is a special case. */
12910 else if (token->keyword == RID_VOID
12911 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12912 == CPP_CLOSE_PAREN))
12913 {
12914 /* Consume the `void' token. */
12915 cp_lexer_consume_token (parser->lexer);
12916 /* There are no parameters. */
12917 return no_parameters;
12918 }
12919
12920 /* Parse the parameter-declaration-list. */
12921 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12922 /* If a parse error occurred while parsing the
12923 parameter-declaration-list, then the entire
12924 parameter-declaration-clause is erroneous. */
12925 if (is_error)
12926 return NULL;
12927
12928 /* Peek at the next token. */
12929 token = cp_lexer_peek_token (parser->lexer);
12930 /* If it's a `,', the clause should terminate with an ellipsis. */
12931 if (token->type == CPP_COMMA)
12932 {
12933 /* Consume the `,'. */
12934 cp_lexer_consume_token (parser->lexer);
12935 /* Expect an ellipsis. */
12936 ellipsis_p
12937 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12938 }
12939 /* It might also be `...' if the optional trailing `,' was
12940 omitted. */
12941 else if (token->type == CPP_ELLIPSIS)
12942 {
12943 /* Consume the `...' token. */
12944 cp_lexer_consume_token (parser->lexer);
12945 /* And remember that we saw it. */
12946 ellipsis_p = true;
12947 }
12948 else
12949 ellipsis_p = false;
12950
12951 /* Finish the parameter list. */
12952 if (parameters && ellipsis_p)
12953 parameters->ellipsis_p = true;
12954
12955 return parameters;
12956 }
12957
12958 /* Parse a parameter-declaration-list.
12959
12960 parameter-declaration-list:
12961 parameter-declaration
12962 parameter-declaration-list , parameter-declaration
12963
12964 Returns a representation of the parameter-declaration-list, as for
12965 cp_parser_parameter_declaration_clause. However, the
12966 `void_list_node' is never appended to the list. Upon return,
12967 *IS_ERROR will be true iff an error occurred. */
12968
12969 static cp_parameter_declarator *
12970 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12971 {
12972 cp_parameter_declarator *parameters = NULL;
12973 cp_parameter_declarator **tail = &parameters;
12974 bool saved_in_unbraced_linkage_specification_p;
12975
12976 /* Assume all will go well. */
12977 *is_error = false;
12978 /* The special considerations that apply to a function within an
12979 unbraced linkage specifications do not apply to the parameters
12980 to the function. */
12981 saved_in_unbraced_linkage_specification_p
12982 = parser->in_unbraced_linkage_specification_p;
12983 parser->in_unbraced_linkage_specification_p = false;
12984
12985 /* Look for more parameters. */
12986 while (true)
12987 {
12988 cp_parameter_declarator *parameter;
12989 bool parenthesized_p;
12990 /* Parse the parameter. */
12991 parameter
12992 = cp_parser_parameter_declaration (parser,
12993 /*template_parm_p=*/false,
12994 &parenthesized_p);
12995
12996 /* If a parse error occurred parsing the parameter declaration,
12997 then the entire parameter-declaration-list is erroneous. */
12998 if (!parameter)
12999 {
13000 *is_error = true;
13001 parameters = NULL;
13002 break;
13003 }
13004 /* Add the new parameter to the list. */
13005 *tail = parameter;
13006 tail = &parameter->next;
13007
13008 /* Peek at the next token. */
13009 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13010 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13011 /* These are for Objective-C++ */
13012 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13013 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13014 /* The parameter-declaration-list is complete. */
13015 break;
13016 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13017 {
13018 cp_token *token;
13019
13020 /* Peek at the next token. */
13021 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13022 /* If it's an ellipsis, then the list is complete. */
13023 if (token->type == CPP_ELLIPSIS)
13024 break;
13025 /* Otherwise, there must be more parameters. Consume the
13026 `,'. */
13027 cp_lexer_consume_token (parser->lexer);
13028 /* When parsing something like:
13029
13030 int i(float f, double d)
13031
13032 we can tell after seeing the declaration for "f" that we
13033 are not looking at an initialization of a variable "i",
13034 but rather at the declaration of a function "i".
13035
13036 Due to the fact that the parsing of template arguments
13037 (as specified to a template-id) requires backtracking we
13038 cannot use this technique when inside a template argument
13039 list. */
13040 if (!parser->in_template_argument_list_p
13041 && !parser->in_type_id_in_expr_p
13042 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13043 /* However, a parameter-declaration of the form
13044 "foat(f)" (which is a valid declaration of a
13045 parameter "f") can also be interpreted as an
13046 expression (the conversion of "f" to "float"). */
13047 && !parenthesized_p)
13048 cp_parser_commit_to_tentative_parse (parser);
13049 }
13050 else
13051 {
13052 cp_parser_error (parser, "expected %<,%> or %<...%>");
13053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13054 cp_parser_skip_to_closing_parenthesis (parser,
13055 /*recovering=*/true,
13056 /*or_comma=*/false,
13057 /*consume_paren=*/false);
13058 break;
13059 }
13060 }
13061
13062 parser->in_unbraced_linkage_specification_p
13063 = saved_in_unbraced_linkage_specification_p;
13064
13065 return parameters;
13066 }
13067
13068 /* Parse a parameter declaration.
13069
13070 parameter-declaration:
13071 decl-specifier-seq ... [opt] declarator
13072 decl-specifier-seq declarator = assignment-expression
13073 decl-specifier-seq ... [opt] abstract-declarator [opt]
13074 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13075
13076 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13077 declares a template parameter. (In that case, a non-nested `>'
13078 token encountered during the parsing of the assignment-expression
13079 is not interpreted as a greater-than operator.)
13080
13081 Returns a representation of the parameter, or NULL if an error
13082 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13083 true iff the declarator is of the form "(p)". */
13084
13085 static cp_parameter_declarator *
13086 cp_parser_parameter_declaration (cp_parser *parser,
13087 bool template_parm_p,
13088 bool *parenthesized_p)
13089 {
13090 int declares_class_or_enum;
13091 bool greater_than_is_operator_p;
13092 cp_decl_specifier_seq decl_specifiers;
13093 cp_declarator *declarator;
13094 tree default_argument;
13095 cp_token *token;
13096 const char *saved_message;
13097
13098 /* In a template parameter, `>' is not an operator.
13099
13100 [temp.param]
13101
13102 When parsing a default template-argument for a non-type
13103 template-parameter, the first non-nested `>' is taken as the end
13104 of the template parameter-list rather than a greater-than
13105 operator. */
13106 greater_than_is_operator_p = !template_parm_p;
13107
13108 /* Type definitions may not appear in parameter types. */
13109 saved_message = parser->type_definition_forbidden_message;
13110 parser->type_definition_forbidden_message
13111 = "types may not be defined in parameter types";
13112
13113 /* Parse the declaration-specifiers. */
13114 cp_parser_decl_specifier_seq (parser,
13115 CP_PARSER_FLAGS_NONE,
13116 &decl_specifiers,
13117 &declares_class_or_enum);
13118 /* If an error occurred, there's no reason to attempt to parse the
13119 rest of the declaration. */
13120 if (cp_parser_error_occurred (parser))
13121 {
13122 parser->type_definition_forbidden_message = saved_message;
13123 return NULL;
13124 }
13125
13126 /* Peek at the next token. */
13127 token = cp_lexer_peek_token (parser->lexer);
13128
13129 /* If the next token is a `)', `,', `=', `>', or `...', then there
13130 is no declarator. However, when variadic templates are enabled,
13131 there may be a declarator following `...'. */
13132 if (token->type == CPP_CLOSE_PAREN
13133 || token->type == CPP_COMMA
13134 || token->type == CPP_EQ
13135 || token->type == CPP_GREATER)
13136 {
13137 declarator = NULL;
13138 if (parenthesized_p)
13139 *parenthesized_p = false;
13140 }
13141 /* Otherwise, there should be a declarator. */
13142 else
13143 {
13144 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13145 parser->default_arg_ok_p = false;
13146
13147 /* After seeing a decl-specifier-seq, if the next token is not a
13148 "(", there is no possibility that the code is a valid
13149 expression. Therefore, if parsing tentatively, we commit at
13150 this point. */
13151 if (!parser->in_template_argument_list_p
13152 /* In an expression context, having seen:
13153
13154 (int((char ...
13155
13156 we cannot be sure whether we are looking at a
13157 function-type (taking a "char" as a parameter) or a cast
13158 of some object of type "char" to "int". */
13159 && !parser->in_type_id_in_expr_p
13160 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13161 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13162 cp_parser_commit_to_tentative_parse (parser);
13163 /* Parse the declarator. */
13164 declarator = cp_parser_declarator (parser,
13165 CP_PARSER_DECLARATOR_EITHER,
13166 /*ctor_dtor_or_conv_p=*/NULL,
13167 parenthesized_p,
13168 /*member_p=*/false);
13169 parser->default_arg_ok_p = saved_default_arg_ok_p;
13170 /* After the declarator, allow more attributes. */
13171 decl_specifiers.attributes
13172 = chainon (decl_specifiers.attributes,
13173 cp_parser_attributes_opt (parser));
13174 }
13175
13176 /* If the next token is an ellipsis, and we have not seen a
13177 declarator name, and the type of the declarator contains parameter
13178 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13179 a parameter pack expansion expression. Otherwise, leave the
13180 ellipsis for a C-style variadic function. */
13181 token = cp_lexer_peek_token (parser->lexer);
13182 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13183 {
13184 tree type = decl_specifiers.type;
13185
13186 if (type && DECL_P (type))
13187 type = TREE_TYPE (type);
13188
13189 if (type
13190 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13191 && declarator_can_be_parameter_pack (declarator)
13192 && (!declarator || !declarator->parameter_pack_p)
13193 && uses_parameter_packs (type))
13194 {
13195 /* Consume the `...'. */
13196 cp_lexer_consume_token (parser->lexer);
13197 maybe_warn_variadic_templates ();
13198
13199 /* Build a pack expansion type */
13200 if (declarator)
13201 declarator->parameter_pack_p = true;
13202 else
13203 decl_specifiers.type = make_pack_expansion (type);
13204 }
13205 }
13206
13207 /* The restriction on defining new types applies only to the type
13208 of the parameter, not to the default argument. */
13209 parser->type_definition_forbidden_message = saved_message;
13210
13211 /* If the next token is `=', then process a default argument. */
13212 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13213 {
13214 bool saved_greater_than_is_operator_p;
13215 /* Consume the `='. */
13216 cp_lexer_consume_token (parser->lexer);
13217
13218 /* If we are defining a class, then the tokens that make up the
13219 default argument must be saved and processed later. */
13220 if (!template_parm_p && at_class_scope_p ()
13221 && TYPE_BEING_DEFINED (current_class_type))
13222 {
13223 unsigned depth = 0;
13224 cp_token *first_token;
13225 cp_token *token;
13226
13227 /* Add tokens until we have processed the entire default
13228 argument. We add the range [first_token, token). */
13229 first_token = cp_lexer_peek_token (parser->lexer);
13230 while (true)
13231 {
13232 bool done = false;
13233
13234 /* Peek at the next token. */
13235 token = cp_lexer_peek_token (parser->lexer);
13236 /* What we do depends on what token we have. */
13237 switch (token->type)
13238 {
13239 /* In valid code, a default argument must be
13240 immediately followed by a `,' `)', or `...'. */
13241 case CPP_COMMA:
13242 case CPP_CLOSE_PAREN:
13243 case CPP_ELLIPSIS:
13244 /* If we run into a non-nested `;', `}', or `]',
13245 then the code is invalid -- but the default
13246 argument is certainly over. */
13247 case CPP_SEMICOLON:
13248 case CPP_CLOSE_BRACE:
13249 case CPP_CLOSE_SQUARE:
13250 if (depth == 0)
13251 done = true;
13252 /* Update DEPTH, if necessary. */
13253 else if (token->type == CPP_CLOSE_PAREN
13254 || token->type == CPP_CLOSE_BRACE
13255 || token->type == CPP_CLOSE_SQUARE)
13256 --depth;
13257 break;
13258
13259 case CPP_OPEN_PAREN:
13260 case CPP_OPEN_SQUARE:
13261 case CPP_OPEN_BRACE:
13262 ++depth;
13263 break;
13264
13265 case CPP_RSHIFT:
13266 if (!flag_cpp0x)
13267 break;
13268 /* Fall through for C++0x, which treats the `>>'
13269 operator like two `>' tokens in certain
13270 cases. */
13271
13272 case CPP_GREATER:
13273 /* If we see a non-nested `>', and `>' is not an
13274 operator, then it marks the end of the default
13275 argument. */
13276 if (!depth && !greater_than_is_operator_p)
13277 done = true;
13278 break;
13279
13280 /* If we run out of tokens, issue an error message. */
13281 case CPP_EOF:
13282 case CPP_PRAGMA_EOL:
13283 error ("file ends in default argument");
13284 done = true;
13285 break;
13286
13287 case CPP_NAME:
13288 case CPP_SCOPE:
13289 /* In these cases, we should look for template-ids.
13290 For example, if the default argument is
13291 `X<int, double>()', we need to do name lookup to
13292 figure out whether or not `X' is a template; if
13293 so, the `,' does not end the default argument.
13294
13295 That is not yet done. */
13296 break;
13297
13298 default:
13299 break;
13300 }
13301
13302 /* If we've reached the end, stop. */
13303 if (done)
13304 break;
13305
13306 /* Add the token to the token block. */
13307 token = cp_lexer_consume_token (parser->lexer);
13308 }
13309
13310 /* Create a DEFAULT_ARG to represented the unparsed default
13311 argument. */
13312 default_argument = make_node (DEFAULT_ARG);
13313 DEFARG_TOKENS (default_argument)
13314 = cp_token_cache_new (first_token, token);
13315 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13316 }
13317 /* Outside of a class definition, we can just parse the
13318 assignment-expression. */
13319 else
13320 {
13321 bool saved_local_variables_forbidden_p;
13322
13323 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13324 set correctly. */
13325 saved_greater_than_is_operator_p
13326 = parser->greater_than_is_operator_p;
13327 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13328 /* Local variable names (and the `this' keyword) may not
13329 appear in a default argument. */
13330 saved_local_variables_forbidden_p
13331 = parser->local_variables_forbidden_p;
13332 parser->local_variables_forbidden_p = true;
13333 /* The default argument expression may cause implicitly
13334 defined member functions to be synthesized, which will
13335 result in garbage collection. We must treat this
13336 situation as if we were within the body of function so as
13337 to avoid collecting live data on the stack. */
13338 ++function_depth;
13339 /* Parse the assignment-expression. */
13340 if (template_parm_p)
13341 push_deferring_access_checks (dk_no_deferred);
13342 default_argument
13343 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13344 if (template_parm_p)
13345 pop_deferring_access_checks ();
13346 /* Restore saved state. */
13347 --function_depth;
13348 parser->greater_than_is_operator_p
13349 = saved_greater_than_is_operator_p;
13350 parser->local_variables_forbidden_p
13351 = saved_local_variables_forbidden_p;
13352 }
13353 if (!parser->default_arg_ok_p)
13354 {
13355 if (!flag_pedantic_errors)
13356 warning (0, "deprecated use of default argument for parameter of non-function");
13357 else
13358 {
13359 error ("default arguments are only permitted for function parameters");
13360 default_argument = NULL_TREE;
13361 }
13362 }
13363 }
13364 else
13365 default_argument = NULL_TREE;
13366
13367 return make_parameter_declarator (&decl_specifiers,
13368 declarator,
13369 default_argument);
13370 }
13371
13372 /* Parse a function-body.
13373
13374 function-body:
13375 compound_statement */
13376
13377 static void
13378 cp_parser_function_body (cp_parser *parser)
13379 {
13380 cp_parser_compound_statement (parser, NULL, false);
13381 }
13382
13383 /* Parse a ctor-initializer-opt followed by a function-body. Return
13384 true if a ctor-initializer was present. */
13385
13386 static bool
13387 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13388 {
13389 tree body;
13390 bool ctor_initializer_p;
13391
13392 /* Begin the function body. */
13393 body = begin_function_body ();
13394 /* Parse the optional ctor-initializer. */
13395 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13396 /* Parse the function-body. */
13397 cp_parser_function_body (parser);
13398 /* Finish the function body. */
13399 finish_function_body (body);
13400
13401 return ctor_initializer_p;
13402 }
13403
13404 /* Parse an initializer.
13405
13406 initializer:
13407 = initializer-clause
13408 ( expression-list )
13409
13410 Returns an expression representing the initializer. If no
13411 initializer is present, NULL_TREE is returned.
13412
13413 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13414 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13415 set to FALSE if there is no initializer present. If there is an
13416 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13417 is set to true; otherwise it is set to false. */
13418
13419 static tree
13420 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13421 bool* non_constant_p)
13422 {
13423 cp_token *token;
13424 tree init;
13425
13426 /* Peek at the next token. */
13427 token = cp_lexer_peek_token (parser->lexer);
13428
13429 /* Let our caller know whether or not this initializer was
13430 parenthesized. */
13431 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13432 /* Assume that the initializer is constant. */
13433 *non_constant_p = false;
13434
13435 if (token->type == CPP_EQ)
13436 {
13437 /* Consume the `='. */
13438 cp_lexer_consume_token (parser->lexer);
13439 /* Parse the initializer-clause. */
13440 init = cp_parser_initializer_clause (parser, non_constant_p);
13441 }
13442 else if (token->type == CPP_OPEN_PAREN)
13443 init = cp_parser_parenthesized_expression_list (parser, false,
13444 /*cast_p=*/false,
13445 /*allow_expansion_p=*/true,
13446 non_constant_p);
13447 else
13448 {
13449 /* Anything else is an error. */
13450 cp_parser_error (parser, "expected initializer");
13451 init = error_mark_node;
13452 }
13453
13454 return init;
13455 }
13456
13457 /* Parse an initializer-clause.
13458
13459 initializer-clause:
13460 assignment-expression
13461 { initializer-list , [opt] }
13462 { }
13463
13464 Returns an expression representing the initializer.
13465
13466 If the `assignment-expression' production is used the value
13467 returned is simply a representation for the expression.
13468
13469 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13470 the elements of the initializer-list (or NULL, if the last
13471 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13472 NULL_TREE. There is no way to detect whether or not the optional
13473 trailing `,' was provided. NON_CONSTANT_P is as for
13474 cp_parser_initializer. */
13475
13476 static tree
13477 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13478 {
13479 tree initializer;
13480
13481 /* Assume the expression is constant. */
13482 *non_constant_p = false;
13483
13484 /* If it is not a `{', then we are looking at an
13485 assignment-expression. */
13486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13487 {
13488 initializer
13489 = cp_parser_constant_expression (parser,
13490 /*allow_non_constant_p=*/true,
13491 non_constant_p);
13492 if (!*non_constant_p)
13493 initializer = fold_non_dependent_expr (initializer);
13494 }
13495 else
13496 {
13497 /* Consume the `{' token. */
13498 cp_lexer_consume_token (parser->lexer);
13499 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13500 initializer = make_node (CONSTRUCTOR);
13501 /* If it's not a `}', then there is a non-trivial initializer. */
13502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13503 {
13504 /* Parse the initializer list. */
13505 CONSTRUCTOR_ELTS (initializer)
13506 = cp_parser_initializer_list (parser, non_constant_p);
13507 /* A trailing `,' token is allowed. */
13508 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13509 cp_lexer_consume_token (parser->lexer);
13510 }
13511 /* Now, there should be a trailing `}'. */
13512 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13513 }
13514
13515 return initializer;
13516 }
13517
13518 /* Parse an initializer-list.
13519
13520 initializer-list:
13521 initializer-clause ... [opt]
13522 initializer-list , initializer-clause ... [opt]
13523
13524 GNU Extension:
13525
13526 initializer-list:
13527 identifier : initializer-clause
13528 initializer-list, identifier : initializer-clause
13529
13530 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13531 for the initializer. If the INDEX of the elt is non-NULL, it is the
13532 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13533 as for cp_parser_initializer. */
13534
13535 static VEC(constructor_elt,gc) *
13536 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13537 {
13538 VEC(constructor_elt,gc) *v = NULL;
13539
13540 /* Assume all of the expressions are constant. */
13541 *non_constant_p = false;
13542
13543 /* Parse the rest of the list. */
13544 while (true)
13545 {
13546 cp_token *token;
13547 tree identifier;
13548 tree initializer;
13549 bool clause_non_constant_p;
13550
13551 /* If the next token is an identifier and the following one is a
13552 colon, we are looking at the GNU designated-initializer
13553 syntax. */
13554 if (cp_parser_allow_gnu_extensions_p (parser)
13555 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13557 {
13558 /* Warn the user that they are using an extension. */
13559 if (pedantic)
13560 pedwarn ("ISO C++ does not allow designated initializers");
13561 /* Consume the identifier. */
13562 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13563 /* Consume the `:'. */
13564 cp_lexer_consume_token (parser->lexer);
13565 }
13566 else
13567 identifier = NULL_TREE;
13568
13569 /* Parse the initializer. */
13570 initializer = cp_parser_initializer_clause (parser,
13571 &clause_non_constant_p);
13572 /* If any clause is non-constant, so is the entire initializer. */
13573 if (clause_non_constant_p)
13574 *non_constant_p = true;
13575
13576 /* If we have an ellipsis, this is an initializer pack
13577 expansion. */
13578 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13579 {
13580 /* Consume the `...'. */
13581 cp_lexer_consume_token (parser->lexer);
13582
13583 /* Turn the initializer into an initializer expansion. */
13584 initializer = make_pack_expansion (initializer);
13585 }
13586
13587 /* Add it to the vector. */
13588 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13589
13590 /* If the next token is not a comma, we have reached the end of
13591 the list. */
13592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13593 break;
13594
13595 /* Peek at the next token. */
13596 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13597 /* If the next token is a `}', then we're still done. An
13598 initializer-clause can have a trailing `,' after the
13599 initializer-list and before the closing `}'. */
13600 if (token->type == CPP_CLOSE_BRACE)
13601 break;
13602
13603 /* Consume the `,' token. */
13604 cp_lexer_consume_token (parser->lexer);
13605 }
13606
13607 return v;
13608 }
13609
13610 /* Classes [gram.class] */
13611
13612 /* Parse a class-name.
13613
13614 class-name:
13615 identifier
13616 template-id
13617
13618 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13619 to indicate that names looked up in dependent types should be
13620 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13621 keyword has been used to indicate that the name that appears next
13622 is a template. TAG_TYPE indicates the explicit tag given before
13623 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13624 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13625 is the class being defined in a class-head.
13626
13627 Returns the TYPE_DECL representing the class. */
13628
13629 static tree
13630 cp_parser_class_name (cp_parser *parser,
13631 bool typename_keyword_p,
13632 bool template_keyword_p,
13633 enum tag_types tag_type,
13634 bool check_dependency_p,
13635 bool class_head_p,
13636 bool is_declaration)
13637 {
13638 tree decl;
13639 tree scope;
13640 bool typename_p;
13641 cp_token *token;
13642
13643 /* All class-names start with an identifier. */
13644 token = cp_lexer_peek_token (parser->lexer);
13645 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13646 {
13647 cp_parser_error (parser, "expected class-name");
13648 return error_mark_node;
13649 }
13650
13651 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13652 to a template-id, so we save it here. */
13653 scope = parser->scope;
13654 if (scope == error_mark_node)
13655 return error_mark_node;
13656
13657 /* Any name names a type if we're following the `typename' keyword
13658 in a qualified name where the enclosing scope is type-dependent. */
13659 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13660 && dependent_type_p (scope));
13661 /* Handle the common case (an identifier, but not a template-id)
13662 efficiently. */
13663 if (token->type == CPP_NAME
13664 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13665 {
13666 cp_token *identifier_token;
13667 tree identifier;
13668 bool ambiguous_p;
13669
13670 /* Look for the identifier. */
13671 identifier_token = cp_lexer_peek_token (parser->lexer);
13672 ambiguous_p = identifier_token->ambiguous_p;
13673 identifier = cp_parser_identifier (parser);
13674 /* If the next token isn't an identifier, we are certainly not
13675 looking at a class-name. */
13676 if (identifier == error_mark_node)
13677 decl = error_mark_node;
13678 /* If we know this is a type-name, there's no need to look it
13679 up. */
13680 else if (typename_p)
13681 decl = identifier;
13682 else
13683 {
13684 tree ambiguous_decls;
13685 /* If we already know that this lookup is ambiguous, then
13686 we've already issued an error message; there's no reason
13687 to check again. */
13688 if (ambiguous_p)
13689 {
13690 cp_parser_simulate_error (parser);
13691 return error_mark_node;
13692 }
13693 /* If the next token is a `::', then the name must be a type
13694 name.
13695
13696 [basic.lookup.qual]
13697
13698 During the lookup for a name preceding the :: scope
13699 resolution operator, object, function, and enumerator
13700 names are ignored. */
13701 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13702 tag_type = typename_type;
13703 /* Look up the name. */
13704 decl = cp_parser_lookup_name (parser, identifier,
13705 tag_type,
13706 /*is_template=*/false,
13707 /*is_namespace=*/false,
13708 check_dependency_p,
13709 &ambiguous_decls);
13710 if (ambiguous_decls)
13711 {
13712 error ("reference to %qD is ambiguous", identifier);
13713 print_candidates (ambiguous_decls);
13714 if (cp_parser_parsing_tentatively (parser))
13715 {
13716 identifier_token->ambiguous_p = true;
13717 cp_parser_simulate_error (parser);
13718 }
13719 return error_mark_node;
13720 }
13721 }
13722 }
13723 else
13724 {
13725 /* Try a template-id. */
13726 decl = cp_parser_template_id (parser, template_keyword_p,
13727 check_dependency_p,
13728 is_declaration);
13729 if (decl == error_mark_node)
13730 return error_mark_node;
13731 }
13732
13733 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13734
13735 /* If this is a typename, create a TYPENAME_TYPE. */
13736 if (typename_p && decl != error_mark_node)
13737 {
13738 decl = make_typename_type (scope, decl, typename_type,
13739 /*complain=*/tf_error);
13740 if (decl != error_mark_node)
13741 decl = TYPE_NAME (decl);
13742 }
13743
13744 /* Check to see that it is really the name of a class. */
13745 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13746 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13747 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13748 /* Situations like this:
13749
13750 template <typename T> struct A {
13751 typename T::template X<int>::I i;
13752 };
13753
13754 are problematic. Is `T::template X<int>' a class-name? The
13755 standard does not seem to be definitive, but there is no other
13756 valid interpretation of the following `::'. Therefore, those
13757 names are considered class-names. */
13758 {
13759 decl = make_typename_type (scope, decl, tag_type, tf_error);
13760 if (decl != error_mark_node)
13761 decl = TYPE_NAME (decl);
13762 }
13763 else if (TREE_CODE (decl) != TYPE_DECL
13764 || TREE_TYPE (decl) == error_mark_node
13765 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13766 decl = error_mark_node;
13767
13768 if (decl == error_mark_node)
13769 cp_parser_error (parser, "expected class-name");
13770
13771 return decl;
13772 }
13773
13774 /* Parse a class-specifier.
13775
13776 class-specifier:
13777 class-head { member-specification [opt] }
13778
13779 Returns the TREE_TYPE representing the class. */
13780
13781 static tree
13782 cp_parser_class_specifier (cp_parser* parser)
13783 {
13784 cp_token *token;
13785 tree type;
13786 tree attributes = NULL_TREE;
13787 int has_trailing_semicolon;
13788 bool nested_name_specifier_p;
13789 unsigned saved_num_template_parameter_lists;
13790 bool saved_in_function_body;
13791 tree old_scope = NULL_TREE;
13792 tree scope = NULL_TREE;
13793 tree bases;
13794
13795 push_deferring_access_checks (dk_no_deferred);
13796
13797 /* Parse the class-head. */
13798 type = cp_parser_class_head (parser,
13799 &nested_name_specifier_p,
13800 &attributes,
13801 &bases);
13802 /* If the class-head was a semantic disaster, skip the entire body
13803 of the class. */
13804 if (!type)
13805 {
13806 cp_parser_skip_to_end_of_block_or_statement (parser);
13807 pop_deferring_access_checks ();
13808 return error_mark_node;
13809 }
13810
13811 /* Look for the `{'. */
13812 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13813 {
13814 pop_deferring_access_checks ();
13815 return error_mark_node;
13816 }
13817
13818 /* Process the base classes. If they're invalid, skip the
13819 entire class body. */
13820 if (!xref_basetypes (type, bases))
13821 {
13822 /* Consuming the closing brace yields better error messages
13823 later on. */
13824 if (cp_parser_skip_to_closing_brace (parser))
13825 cp_lexer_consume_token (parser->lexer);
13826 pop_deferring_access_checks ();
13827 return error_mark_node;
13828 }
13829
13830 /* Issue an error message if type-definitions are forbidden here. */
13831 cp_parser_check_type_definition (parser);
13832 /* Remember that we are defining one more class. */
13833 ++parser->num_classes_being_defined;
13834 /* Inside the class, surrounding template-parameter-lists do not
13835 apply. */
13836 saved_num_template_parameter_lists
13837 = parser->num_template_parameter_lists;
13838 parser->num_template_parameter_lists = 0;
13839 /* We are not in a function body. */
13840 saved_in_function_body = parser->in_function_body;
13841 parser->in_function_body = false;
13842
13843 /* Start the class. */
13844 if (nested_name_specifier_p)
13845 {
13846 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13847 old_scope = push_inner_scope (scope);
13848 }
13849 type = begin_class_definition (type, attributes);
13850
13851 if (type == error_mark_node)
13852 /* If the type is erroneous, skip the entire body of the class. */
13853 cp_parser_skip_to_closing_brace (parser);
13854 else
13855 /* Parse the member-specification. */
13856 cp_parser_member_specification_opt (parser);
13857
13858 /* Look for the trailing `}'. */
13859 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13860 /* We get better error messages by noticing a common problem: a
13861 missing trailing `;'. */
13862 token = cp_lexer_peek_token (parser->lexer);
13863 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13864 /* Look for trailing attributes to apply to this class. */
13865 if (cp_parser_allow_gnu_extensions_p (parser))
13866 attributes = cp_parser_attributes_opt (parser);
13867 if (type != error_mark_node)
13868 type = finish_struct (type, attributes);
13869 if (nested_name_specifier_p)
13870 pop_inner_scope (old_scope, scope);
13871 /* If this class is not itself within the scope of another class,
13872 then we need to parse the bodies of all of the queued function
13873 definitions. Note that the queued functions defined in a class
13874 are not always processed immediately following the
13875 class-specifier for that class. Consider:
13876
13877 struct A {
13878 struct B { void f() { sizeof (A); } };
13879 };
13880
13881 If `f' were processed before the processing of `A' were
13882 completed, there would be no way to compute the size of `A'.
13883 Note that the nesting we are interested in here is lexical --
13884 not the semantic nesting given by TYPE_CONTEXT. In particular,
13885 for:
13886
13887 struct A { struct B; };
13888 struct A::B { void f() { } };
13889
13890 there is no need to delay the parsing of `A::B::f'. */
13891 if (--parser->num_classes_being_defined == 0)
13892 {
13893 tree queue_entry;
13894 tree fn;
13895 tree class_type = NULL_TREE;
13896 tree pushed_scope = NULL_TREE;
13897
13898 /* In a first pass, parse default arguments to the functions.
13899 Then, in a second pass, parse the bodies of the functions.
13900 This two-phased approach handles cases like:
13901
13902 struct S {
13903 void f() { g(); }
13904 void g(int i = 3);
13905 };
13906
13907 */
13908 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13909 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13910 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13911 TREE_PURPOSE (parser->unparsed_functions_queues)
13912 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13913 {
13914 fn = TREE_VALUE (queue_entry);
13915 /* If there are default arguments that have not yet been processed,
13916 take care of them now. */
13917 if (class_type != TREE_PURPOSE (queue_entry))
13918 {
13919 if (pushed_scope)
13920 pop_scope (pushed_scope);
13921 class_type = TREE_PURPOSE (queue_entry);
13922 pushed_scope = push_scope (class_type);
13923 }
13924 /* Make sure that any template parameters are in scope. */
13925 maybe_begin_member_template_processing (fn);
13926 /* Parse the default argument expressions. */
13927 cp_parser_late_parsing_default_args (parser, fn);
13928 /* Remove any template parameters from the symbol table. */
13929 maybe_end_member_template_processing ();
13930 }
13931 if (pushed_scope)
13932 pop_scope (pushed_scope);
13933 /* Now parse the body of the functions. */
13934 for (TREE_VALUE (parser->unparsed_functions_queues)
13935 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13936 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13937 TREE_VALUE (parser->unparsed_functions_queues)
13938 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13939 {
13940 /* Figure out which function we need to process. */
13941 fn = TREE_VALUE (queue_entry);
13942 /* Parse the function. */
13943 cp_parser_late_parsing_for_member (parser, fn);
13944 }
13945 }
13946
13947 /* Put back any saved access checks. */
13948 pop_deferring_access_checks ();
13949
13950 /* Restore saved state. */
13951 parser->in_function_body = saved_in_function_body;
13952 parser->num_template_parameter_lists
13953 = saved_num_template_parameter_lists;
13954
13955 return type;
13956 }
13957
13958 /* Parse a class-head.
13959
13960 class-head:
13961 class-key identifier [opt] base-clause [opt]
13962 class-key nested-name-specifier identifier base-clause [opt]
13963 class-key nested-name-specifier [opt] template-id
13964 base-clause [opt]
13965
13966 GNU Extensions:
13967 class-key attributes identifier [opt] base-clause [opt]
13968 class-key attributes nested-name-specifier identifier base-clause [opt]
13969 class-key attributes nested-name-specifier [opt] template-id
13970 base-clause [opt]
13971
13972 Upon return BASES is initialized to the list of base classes (or
13973 NULL, if there are none) in the same form returned by
13974 cp_parser_base_clause.
13975
13976 Returns the TYPE of the indicated class. Sets
13977 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13978 involving a nested-name-specifier was used, and FALSE otherwise.
13979
13980 Returns error_mark_node if this is not a class-head.
13981
13982 Returns NULL_TREE if the class-head is syntactically valid, but
13983 semantically invalid in a way that means we should skip the entire
13984 body of the class. */
13985
13986 static tree
13987 cp_parser_class_head (cp_parser* parser,
13988 bool* nested_name_specifier_p,
13989 tree *attributes_p,
13990 tree *bases)
13991 {
13992 tree nested_name_specifier;
13993 enum tag_types class_key;
13994 tree id = NULL_TREE;
13995 tree type = NULL_TREE;
13996 tree attributes;
13997 bool template_id_p = false;
13998 bool qualified_p = false;
13999 bool invalid_nested_name_p = false;
14000 bool invalid_explicit_specialization_p = false;
14001 tree pushed_scope = NULL_TREE;
14002 unsigned num_templates;
14003
14004 /* Assume no nested-name-specifier will be present. */
14005 *nested_name_specifier_p = false;
14006 /* Assume no template parameter lists will be used in defining the
14007 type. */
14008 num_templates = 0;
14009
14010 *bases = NULL_TREE;
14011
14012 /* Look for the class-key. */
14013 class_key = cp_parser_class_key (parser);
14014 if (class_key == none_type)
14015 return error_mark_node;
14016
14017 /* Parse the attributes. */
14018 attributes = cp_parser_attributes_opt (parser);
14019
14020 /* If the next token is `::', that is invalid -- but sometimes
14021 people do try to write:
14022
14023 struct ::S {};
14024
14025 Handle this gracefully by accepting the extra qualifier, and then
14026 issuing an error about it later if this really is a
14027 class-head. If it turns out just to be an elaborated type
14028 specifier, remain silent. */
14029 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14030 qualified_p = true;
14031
14032 push_deferring_access_checks (dk_no_check);
14033
14034 /* Determine the name of the class. Begin by looking for an
14035 optional nested-name-specifier. */
14036 nested_name_specifier
14037 = cp_parser_nested_name_specifier_opt (parser,
14038 /*typename_keyword_p=*/false,
14039 /*check_dependency_p=*/false,
14040 /*type_p=*/false,
14041 /*is_declaration=*/false);
14042 /* If there was a nested-name-specifier, then there *must* be an
14043 identifier. */
14044 if (nested_name_specifier)
14045 {
14046 /* Although the grammar says `identifier', it really means
14047 `class-name' or `template-name'. You are only allowed to
14048 define a class that has already been declared with this
14049 syntax.
14050
14051 The proposed resolution for Core Issue 180 says that wherever
14052 you see `class T::X' you should treat `X' as a type-name.
14053
14054 It is OK to define an inaccessible class; for example:
14055
14056 class A { class B; };
14057 class A::B {};
14058
14059 We do not know if we will see a class-name, or a
14060 template-name. We look for a class-name first, in case the
14061 class-name is a template-id; if we looked for the
14062 template-name first we would stop after the template-name. */
14063 cp_parser_parse_tentatively (parser);
14064 type = cp_parser_class_name (parser,
14065 /*typename_keyword_p=*/false,
14066 /*template_keyword_p=*/false,
14067 class_type,
14068 /*check_dependency_p=*/false,
14069 /*class_head_p=*/true,
14070 /*is_declaration=*/false);
14071 /* If that didn't work, ignore the nested-name-specifier. */
14072 if (!cp_parser_parse_definitely (parser))
14073 {
14074 invalid_nested_name_p = true;
14075 id = cp_parser_identifier (parser);
14076 if (id == error_mark_node)
14077 id = NULL_TREE;
14078 }
14079 /* If we could not find a corresponding TYPE, treat this
14080 declaration like an unqualified declaration. */
14081 if (type == error_mark_node)
14082 nested_name_specifier = NULL_TREE;
14083 /* Otherwise, count the number of templates used in TYPE and its
14084 containing scopes. */
14085 else
14086 {
14087 tree scope;
14088
14089 for (scope = TREE_TYPE (type);
14090 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14091 scope = (TYPE_P (scope)
14092 ? TYPE_CONTEXT (scope)
14093 : DECL_CONTEXT (scope)))
14094 if (TYPE_P (scope)
14095 && CLASS_TYPE_P (scope)
14096 && CLASSTYPE_TEMPLATE_INFO (scope)
14097 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14098 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14099 ++num_templates;
14100 }
14101 }
14102 /* Otherwise, the identifier is optional. */
14103 else
14104 {
14105 /* We don't know whether what comes next is a template-id,
14106 an identifier, or nothing at all. */
14107 cp_parser_parse_tentatively (parser);
14108 /* Check for a template-id. */
14109 id = cp_parser_template_id (parser,
14110 /*template_keyword_p=*/false,
14111 /*check_dependency_p=*/true,
14112 /*is_declaration=*/true);
14113 /* If that didn't work, it could still be an identifier. */
14114 if (!cp_parser_parse_definitely (parser))
14115 {
14116 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14117 id = cp_parser_identifier (parser);
14118 else
14119 id = NULL_TREE;
14120 }
14121 else
14122 {
14123 template_id_p = true;
14124 ++num_templates;
14125 }
14126 }
14127
14128 pop_deferring_access_checks ();
14129
14130 if (id)
14131 cp_parser_check_for_invalid_template_id (parser, id);
14132
14133 /* If it's not a `:' or a `{' then we can't really be looking at a
14134 class-head, since a class-head only appears as part of a
14135 class-specifier. We have to detect this situation before calling
14136 xref_tag, since that has irreversible side-effects. */
14137 if (!cp_parser_next_token_starts_class_definition_p (parser))
14138 {
14139 cp_parser_error (parser, "expected %<{%> or %<:%>");
14140 return error_mark_node;
14141 }
14142
14143 /* At this point, we're going ahead with the class-specifier, even
14144 if some other problem occurs. */
14145 cp_parser_commit_to_tentative_parse (parser);
14146 /* Issue the error about the overly-qualified name now. */
14147 if (qualified_p)
14148 cp_parser_error (parser,
14149 "global qualification of class name is invalid");
14150 else if (invalid_nested_name_p)
14151 cp_parser_error (parser,
14152 "qualified name does not name a class");
14153 else if (nested_name_specifier)
14154 {
14155 tree scope;
14156
14157 /* Reject typedef-names in class heads. */
14158 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14159 {
14160 error ("invalid class name in declaration of %qD", type);
14161 type = NULL_TREE;
14162 goto done;
14163 }
14164
14165 /* Figure out in what scope the declaration is being placed. */
14166 scope = current_scope ();
14167 /* If that scope does not contain the scope in which the
14168 class was originally declared, the program is invalid. */
14169 if (scope && !is_ancestor (scope, nested_name_specifier))
14170 {
14171 error ("declaration of %qD in %qD which does not enclose %qD",
14172 type, scope, nested_name_specifier);
14173 type = NULL_TREE;
14174 goto done;
14175 }
14176 /* [dcl.meaning]
14177
14178 A declarator-id shall not be qualified exception of the
14179 definition of a ... nested class outside of its class
14180 ... [or] a the definition or explicit instantiation of a
14181 class member of a namespace outside of its namespace. */
14182 if (scope == nested_name_specifier)
14183 {
14184 pedwarn ("extra qualification ignored");
14185 nested_name_specifier = NULL_TREE;
14186 num_templates = 0;
14187 }
14188 }
14189 /* An explicit-specialization must be preceded by "template <>". If
14190 it is not, try to recover gracefully. */
14191 if (at_namespace_scope_p ()
14192 && parser->num_template_parameter_lists == 0
14193 && template_id_p)
14194 {
14195 error ("an explicit specialization must be preceded by %<template <>%>");
14196 invalid_explicit_specialization_p = true;
14197 /* Take the same action that would have been taken by
14198 cp_parser_explicit_specialization. */
14199 ++parser->num_template_parameter_lists;
14200 begin_specialization ();
14201 }
14202 /* There must be no "return" statements between this point and the
14203 end of this function; set "type "to the correct return value and
14204 use "goto done;" to return. */
14205 /* Make sure that the right number of template parameters were
14206 present. */
14207 if (!cp_parser_check_template_parameters (parser, num_templates))
14208 {
14209 /* If something went wrong, there is no point in even trying to
14210 process the class-definition. */
14211 type = NULL_TREE;
14212 goto done;
14213 }
14214
14215 /* Look up the type. */
14216 if (template_id_p)
14217 {
14218 type = TREE_TYPE (id);
14219 type = maybe_process_partial_specialization (type);
14220 if (nested_name_specifier)
14221 pushed_scope = push_scope (nested_name_specifier);
14222 }
14223 else if (nested_name_specifier)
14224 {
14225 tree class_type;
14226
14227 /* Given:
14228
14229 template <typename T> struct S { struct T };
14230 template <typename T> struct S<T>::T { };
14231
14232 we will get a TYPENAME_TYPE when processing the definition of
14233 `S::T'. We need to resolve it to the actual type before we
14234 try to define it. */
14235 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14236 {
14237 class_type = resolve_typename_type (TREE_TYPE (type),
14238 /*only_current_p=*/false);
14239 if (class_type != error_mark_node)
14240 type = TYPE_NAME (class_type);
14241 else
14242 {
14243 cp_parser_error (parser, "could not resolve typename type");
14244 type = error_mark_node;
14245 }
14246 }
14247
14248 maybe_process_partial_specialization (TREE_TYPE (type));
14249 class_type = current_class_type;
14250 /* Enter the scope indicated by the nested-name-specifier. */
14251 pushed_scope = push_scope (nested_name_specifier);
14252 /* Get the canonical version of this type. */
14253 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14254 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14255 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14256 {
14257 type = push_template_decl (type);
14258 if (type == error_mark_node)
14259 {
14260 type = NULL_TREE;
14261 goto done;
14262 }
14263 }
14264
14265 type = TREE_TYPE (type);
14266 *nested_name_specifier_p = true;
14267 }
14268 else /* The name is not a nested name. */
14269 {
14270 /* If the class was unnamed, create a dummy name. */
14271 if (!id)
14272 id = make_anon_name ();
14273 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14274 parser->num_template_parameter_lists);
14275 }
14276
14277 /* Indicate whether this class was declared as a `class' or as a
14278 `struct'. */
14279 if (TREE_CODE (type) == RECORD_TYPE)
14280 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14281 cp_parser_check_class_key (class_key, type);
14282
14283 /* If this type was already complete, and we see another definition,
14284 that's an error. */
14285 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14286 {
14287 error ("redefinition of %q#T", type);
14288 error ("previous definition of %q+#T", type);
14289 type = NULL_TREE;
14290 goto done;
14291 }
14292 else if (type == error_mark_node)
14293 type = NULL_TREE;
14294
14295 /* We will have entered the scope containing the class; the names of
14296 base classes should be looked up in that context. For example:
14297
14298 struct A { struct B {}; struct C; };
14299 struct A::C : B {};
14300
14301 is valid. */
14302
14303 /* Get the list of base-classes, if there is one. */
14304 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14305 *bases = cp_parser_base_clause (parser);
14306
14307 done:
14308 /* Leave the scope given by the nested-name-specifier. We will
14309 enter the class scope itself while processing the members. */
14310 if (pushed_scope)
14311 pop_scope (pushed_scope);
14312
14313 if (invalid_explicit_specialization_p)
14314 {
14315 end_specialization ();
14316 --parser->num_template_parameter_lists;
14317 }
14318 *attributes_p = attributes;
14319 return type;
14320 }
14321
14322 /* Parse a class-key.
14323
14324 class-key:
14325 class
14326 struct
14327 union
14328
14329 Returns the kind of class-key specified, or none_type to indicate
14330 error. */
14331
14332 static enum tag_types
14333 cp_parser_class_key (cp_parser* parser)
14334 {
14335 cp_token *token;
14336 enum tag_types tag_type;
14337
14338 /* Look for the class-key. */
14339 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14340 if (!token)
14341 return none_type;
14342
14343 /* Check to see if the TOKEN is a class-key. */
14344 tag_type = cp_parser_token_is_class_key (token);
14345 if (!tag_type)
14346 cp_parser_error (parser, "expected class-key");
14347 return tag_type;
14348 }
14349
14350 /* Parse an (optional) member-specification.
14351
14352 member-specification:
14353 member-declaration member-specification [opt]
14354 access-specifier : member-specification [opt] */
14355
14356 static void
14357 cp_parser_member_specification_opt (cp_parser* parser)
14358 {
14359 while (true)
14360 {
14361 cp_token *token;
14362 enum rid keyword;
14363
14364 /* Peek at the next token. */
14365 token = cp_lexer_peek_token (parser->lexer);
14366 /* If it's a `}', or EOF then we've seen all the members. */
14367 if (token->type == CPP_CLOSE_BRACE
14368 || token->type == CPP_EOF
14369 || token->type == CPP_PRAGMA_EOL)
14370 break;
14371
14372 /* See if this token is a keyword. */
14373 keyword = token->keyword;
14374 switch (keyword)
14375 {
14376 case RID_PUBLIC:
14377 case RID_PROTECTED:
14378 case RID_PRIVATE:
14379 /* Consume the access-specifier. */
14380 cp_lexer_consume_token (parser->lexer);
14381 /* Remember which access-specifier is active. */
14382 current_access_specifier = token->u.value;
14383 /* Look for the `:'. */
14384 cp_parser_require (parser, CPP_COLON, "`:'");
14385 break;
14386
14387 default:
14388 /* Accept #pragmas at class scope. */
14389 if (token->type == CPP_PRAGMA)
14390 {
14391 cp_parser_pragma (parser, pragma_external);
14392 break;
14393 }
14394
14395 /* Otherwise, the next construction must be a
14396 member-declaration. */
14397 cp_parser_member_declaration (parser);
14398 }
14399 }
14400 }
14401
14402 /* Parse a member-declaration.
14403
14404 member-declaration:
14405 decl-specifier-seq [opt] member-declarator-list [opt] ;
14406 function-definition ; [opt]
14407 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14408 using-declaration
14409 template-declaration
14410
14411 member-declarator-list:
14412 member-declarator
14413 member-declarator-list , member-declarator
14414
14415 member-declarator:
14416 declarator pure-specifier [opt]
14417 declarator constant-initializer [opt]
14418 identifier [opt] : constant-expression
14419
14420 GNU Extensions:
14421
14422 member-declaration:
14423 __extension__ member-declaration
14424
14425 member-declarator:
14426 declarator attributes [opt] pure-specifier [opt]
14427 declarator attributes [opt] constant-initializer [opt]
14428 identifier [opt] attributes [opt] : constant-expression
14429
14430 C++0x Extensions:
14431
14432 member-declaration:
14433 static_assert-declaration */
14434
14435 static void
14436 cp_parser_member_declaration (cp_parser* parser)
14437 {
14438 cp_decl_specifier_seq decl_specifiers;
14439 tree prefix_attributes;
14440 tree decl;
14441 int declares_class_or_enum;
14442 bool friend_p;
14443 cp_token *token;
14444 int saved_pedantic;
14445
14446 /* Check for the `__extension__' keyword. */
14447 if (cp_parser_extension_opt (parser, &saved_pedantic))
14448 {
14449 /* Recurse. */
14450 cp_parser_member_declaration (parser);
14451 /* Restore the old value of the PEDANTIC flag. */
14452 pedantic = saved_pedantic;
14453
14454 return;
14455 }
14456
14457 /* Check for a template-declaration. */
14458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14459 {
14460 /* An explicit specialization here is an error condition, and we
14461 expect the specialization handler to detect and report this. */
14462 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14463 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14464 cp_parser_explicit_specialization (parser);
14465 else
14466 cp_parser_template_declaration (parser, /*member_p=*/true);
14467
14468 return;
14469 }
14470
14471 /* Check for a using-declaration. */
14472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14473 {
14474 /* Parse the using-declaration. */
14475 cp_parser_using_declaration (parser,
14476 /*access_declaration_p=*/false);
14477 return;
14478 }
14479
14480 /* Check for @defs. */
14481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14482 {
14483 tree ivar, member;
14484 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14485 ivar = ivar_chains;
14486 while (ivar)
14487 {
14488 member = ivar;
14489 ivar = TREE_CHAIN (member);
14490 TREE_CHAIN (member) = NULL_TREE;
14491 finish_member_declaration (member);
14492 }
14493 return;
14494 }
14495
14496 /* If the next token is `static_assert' we have a static assertion. */
14497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14498 {
14499 cp_parser_static_assert (parser, /*member_p=*/true);
14500 return;
14501 }
14502
14503 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14504 return;
14505
14506 /* Parse the decl-specifier-seq. */
14507 cp_parser_decl_specifier_seq (parser,
14508 CP_PARSER_FLAGS_OPTIONAL,
14509 &decl_specifiers,
14510 &declares_class_or_enum);
14511 prefix_attributes = decl_specifiers.attributes;
14512 decl_specifiers.attributes = NULL_TREE;
14513 /* Check for an invalid type-name. */
14514 if (!decl_specifiers.type
14515 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14516 return;
14517 /* If there is no declarator, then the decl-specifier-seq should
14518 specify a type. */
14519 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14520 {
14521 /* If there was no decl-specifier-seq, and the next token is a
14522 `;', then we have something like:
14523
14524 struct S { ; };
14525
14526 [class.mem]
14527
14528 Each member-declaration shall declare at least one member
14529 name of the class. */
14530 if (!decl_specifiers.any_specifiers_p)
14531 {
14532 cp_token *token = cp_lexer_peek_token (parser->lexer);
14533 if (pedantic && !token->in_system_header)
14534 pedwarn ("%Hextra %<;%>", &token->location);
14535 }
14536 else
14537 {
14538 tree type;
14539
14540 /* See if this declaration is a friend. */
14541 friend_p = cp_parser_friend_p (&decl_specifiers);
14542 /* If there were decl-specifiers, check to see if there was
14543 a class-declaration. */
14544 type = check_tag_decl (&decl_specifiers);
14545 /* Nested classes have already been added to the class, but
14546 a `friend' needs to be explicitly registered. */
14547 if (friend_p)
14548 {
14549 /* If the `friend' keyword was present, the friend must
14550 be introduced with a class-key. */
14551 if (!declares_class_or_enum)
14552 error ("a class-key must be used when declaring a friend");
14553 /* In this case:
14554
14555 template <typename T> struct A {
14556 friend struct A<T>::B;
14557 };
14558
14559 A<T>::B will be represented by a TYPENAME_TYPE, and
14560 therefore not recognized by check_tag_decl. */
14561 if (!type
14562 && decl_specifiers.type
14563 && TYPE_P (decl_specifiers.type))
14564 type = decl_specifiers.type;
14565 if (!type || !TYPE_P (type))
14566 error ("friend declaration does not name a class or "
14567 "function");
14568 else
14569 make_friend_class (current_class_type, type,
14570 /*complain=*/true);
14571 }
14572 /* If there is no TYPE, an error message will already have
14573 been issued. */
14574 else if (!type || type == error_mark_node)
14575 ;
14576 /* An anonymous aggregate has to be handled specially; such
14577 a declaration really declares a data member (with a
14578 particular type), as opposed to a nested class. */
14579 else if (ANON_AGGR_TYPE_P (type))
14580 {
14581 /* Remove constructors and such from TYPE, now that we
14582 know it is an anonymous aggregate. */
14583 fixup_anonymous_aggr (type);
14584 /* And make the corresponding data member. */
14585 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14586 /* Add it to the class. */
14587 finish_member_declaration (decl);
14588 }
14589 else
14590 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14591 }
14592 }
14593 else
14594 {
14595 /* See if these declarations will be friends. */
14596 friend_p = cp_parser_friend_p (&decl_specifiers);
14597
14598 /* Keep going until we hit the `;' at the end of the
14599 declaration. */
14600 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14601 {
14602 tree attributes = NULL_TREE;
14603 tree first_attribute;
14604
14605 /* Peek at the next token. */
14606 token = cp_lexer_peek_token (parser->lexer);
14607
14608 /* Check for a bitfield declaration. */
14609 if (token->type == CPP_COLON
14610 || (token->type == CPP_NAME
14611 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14612 == CPP_COLON))
14613 {
14614 tree identifier;
14615 tree width;
14616
14617 /* Get the name of the bitfield. Note that we cannot just
14618 check TOKEN here because it may have been invalidated by
14619 the call to cp_lexer_peek_nth_token above. */
14620 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14621 identifier = cp_parser_identifier (parser);
14622 else
14623 identifier = NULL_TREE;
14624
14625 /* Consume the `:' token. */
14626 cp_lexer_consume_token (parser->lexer);
14627 /* Get the width of the bitfield. */
14628 width
14629 = cp_parser_constant_expression (parser,
14630 /*allow_non_constant=*/false,
14631 NULL);
14632
14633 /* Look for attributes that apply to the bitfield. */
14634 attributes = cp_parser_attributes_opt (parser);
14635 /* Remember which attributes are prefix attributes and
14636 which are not. */
14637 first_attribute = attributes;
14638 /* Combine the attributes. */
14639 attributes = chainon (prefix_attributes, attributes);
14640
14641 /* Create the bitfield declaration. */
14642 decl = grokbitfield (identifier
14643 ? make_id_declarator (NULL_TREE,
14644 identifier,
14645 sfk_none)
14646 : NULL,
14647 &decl_specifiers,
14648 width);
14649 /* Apply the attributes. */
14650 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14651 }
14652 else
14653 {
14654 cp_declarator *declarator;
14655 tree initializer;
14656 tree asm_specification;
14657 int ctor_dtor_or_conv_p;
14658
14659 /* Parse the declarator. */
14660 declarator
14661 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14662 &ctor_dtor_or_conv_p,
14663 /*parenthesized_p=*/NULL,
14664 /*member_p=*/true);
14665
14666 /* If something went wrong parsing the declarator, make sure
14667 that we at least consume some tokens. */
14668 if (declarator == cp_error_declarator)
14669 {
14670 /* Skip to the end of the statement. */
14671 cp_parser_skip_to_end_of_statement (parser);
14672 /* If the next token is not a semicolon, that is
14673 probably because we just skipped over the body of
14674 a function. So, we consume a semicolon if
14675 present, but do not issue an error message if it
14676 is not present. */
14677 if (cp_lexer_next_token_is (parser->lexer,
14678 CPP_SEMICOLON))
14679 cp_lexer_consume_token (parser->lexer);
14680 return;
14681 }
14682
14683 if (declares_class_or_enum & 2)
14684 cp_parser_check_for_definition_in_return_type
14685 (declarator, decl_specifiers.type);
14686
14687 /* Look for an asm-specification. */
14688 asm_specification = cp_parser_asm_specification_opt (parser);
14689 /* Look for attributes that apply to the declaration. */
14690 attributes = cp_parser_attributes_opt (parser);
14691 /* Remember which attributes are prefix attributes and
14692 which are not. */
14693 first_attribute = attributes;
14694 /* Combine the attributes. */
14695 attributes = chainon (prefix_attributes, attributes);
14696
14697 /* If it's an `=', then we have a constant-initializer or a
14698 pure-specifier. It is not correct to parse the
14699 initializer before registering the member declaration
14700 since the member declaration should be in scope while
14701 its initializer is processed. However, the rest of the
14702 front end does not yet provide an interface that allows
14703 us to handle this correctly. */
14704 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14705 {
14706 /* In [class.mem]:
14707
14708 A pure-specifier shall be used only in the declaration of
14709 a virtual function.
14710
14711 A member-declarator can contain a constant-initializer
14712 only if it declares a static member of integral or
14713 enumeration type.
14714
14715 Therefore, if the DECLARATOR is for a function, we look
14716 for a pure-specifier; otherwise, we look for a
14717 constant-initializer. When we call `grokfield', it will
14718 perform more stringent semantics checks. */
14719 if (function_declarator_p (declarator))
14720 initializer = cp_parser_pure_specifier (parser);
14721 else
14722 /* Parse the initializer. */
14723 initializer = cp_parser_constant_initializer (parser);
14724 }
14725 /* Otherwise, there is no initializer. */
14726 else
14727 initializer = NULL_TREE;
14728
14729 /* See if we are probably looking at a function
14730 definition. We are certainly not looking at a
14731 member-declarator. Calling `grokfield' has
14732 side-effects, so we must not do it unless we are sure
14733 that we are looking at a member-declarator. */
14734 if (cp_parser_token_starts_function_definition_p
14735 (cp_lexer_peek_token (parser->lexer)))
14736 {
14737 /* The grammar does not allow a pure-specifier to be
14738 used when a member function is defined. (It is
14739 possible that this fact is an oversight in the
14740 standard, since a pure function may be defined
14741 outside of the class-specifier. */
14742 if (initializer)
14743 error ("pure-specifier on function-definition");
14744 decl = cp_parser_save_member_function_body (parser,
14745 &decl_specifiers,
14746 declarator,
14747 attributes);
14748 /* If the member was not a friend, declare it here. */
14749 if (!friend_p)
14750 finish_member_declaration (decl);
14751 /* Peek at the next token. */
14752 token = cp_lexer_peek_token (parser->lexer);
14753 /* If the next token is a semicolon, consume it. */
14754 if (token->type == CPP_SEMICOLON)
14755 {
14756 if (pedantic && !in_system_header)
14757 pedwarn ("extra %<;%>");
14758 cp_lexer_consume_token (parser->lexer);
14759 }
14760 return;
14761 }
14762 else
14763 /* Create the declaration. */
14764 decl = grokfield (declarator, &decl_specifiers,
14765 initializer, /*init_const_expr_p=*/true,
14766 asm_specification,
14767 attributes);
14768 }
14769
14770 /* Reset PREFIX_ATTRIBUTES. */
14771 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14772 attributes = TREE_CHAIN (attributes);
14773 if (attributes)
14774 TREE_CHAIN (attributes) = NULL_TREE;
14775
14776 /* If there is any qualification still in effect, clear it
14777 now; we will be starting fresh with the next declarator. */
14778 parser->scope = NULL_TREE;
14779 parser->qualifying_scope = NULL_TREE;
14780 parser->object_scope = NULL_TREE;
14781 /* If it's a `,', then there are more declarators. */
14782 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14783 cp_lexer_consume_token (parser->lexer);
14784 /* If the next token isn't a `;', then we have a parse error. */
14785 else if (cp_lexer_next_token_is_not (parser->lexer,
14786 CPP_SEMICOLON))
14787 {
14788 cp_parser_error (parser, "expected %<;%>");
14789 /* Skip tokens until we find a `;'. */
14790 cp_parser_skip_to_end_of_statement (parser);
14791
14792 break;
14793 }
14794
14795 if (decl)
14796 {
14797 /* Add DECL to the list of members. */
14798 if (!friend_p)
14799 finish_member_declaration (decl);
14800
14801 if (TREE_CODE (decl) == FUNCTION_DECL)
14802 cp_parser_save_default_args (parser, decl);
14803 }
14804 }
14805 }
14806
14807 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14808 }
14809
14810 /* Parse a pure-specifier.
14811
14812 pure-specifier:
14813 = 0
14814
14815 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14816 Otherwise, ERROR_MARK_NODE is returned. */
14817
14818 static tree
14819 cp_parser_pure_specifier (cp_parser* parser)
14820 {
14821 cp_token *token;
14822
14823 /* Look for the `=' token. */
14824 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14825 return error_mark_node;
14826 /* Look for the `0' token. */
14827 token = cp_lexer_consume_token (parser->lexer);
14828 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14829 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14830 {
14831 cp_parser_error (parser,
14832 "invalid pure specifier (only `= 0' is allowed)");
14833 cp_parser_skip_to_end_of_statement (parser);
14834 return error_mark_node;
14835 }
14836 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14837 {
14838 error ("templates may not be %<virtual%>");
14839 return error_mark_node;
14840 }
14841
14842 return integer_zero_node;
14843 }
14844
14845 /* Parse a constant-initializer.
14846
14847 constant-initializer:
14848 = constant-expression
14849
14850 Returns a representation of the constant-expression. */
14851
14852 static tree
14853 cp_parser_constant_initializer (cp_parser* parser)
14854 {
14855 /* Look for the `=' token. */
14856 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14857 return error_mark_node;
14858
14859 /* It is invalid to write:
14860
14861 struct S { static const int i = { 7 }; };
14862
14863 */
14864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14865 {
14866 cp_parser_error (parser,
14867 "a brace-enclosed initializer is not allowed here");
14868 /* Consume the opening brace. */
14869 cp_lexer_consume_token (parser->lexer);
14870 /* Skip the initializer. */
14871 cp_parser_skip_to_closing_brace (parser);
14872 /* Look for the trailing `}'. */
14873 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14874
14875 return error_mark_node;
14876 }
14877
14878 return cp_parser_constant_expression (parser,
14879 /*allow_non_constant=*/false,
14880 NULL);
14881 }
14882
14883 /* Derived classes [gram.class.derived] */
14884
14885 /* Parse a base-clause.
14886
14887 base-clause:
14888 : base-specifier-list
14889
14890 base-specifier-list:
14891 base-specifier ... [opt]
14892 base-specifier-list , base-specifier ... [opt]
14893
14894 Returns a TREE_LIST representing the base-classes, in the order in
14895 which they were declared. The representation of each node is as
14896 described by cp_parser_base_specifier.
14897
14898 In the case that no bases are specified, this function will return
14899 NULL_TREE, not ERROR_MARK_NODE. */
14900
14901 static tree
14902 cp_parser_base_clause (cp_parser* parser)
14903 {
14904 tree bases = NULL_TREE;
14905
14906 /* Look for the `:' that begins the list. */
14907 cp_parser_require (parser, CPP_COLON, "`:'");
14908
14909 /* Scan the base-specifier-list. */
14910 while (true)
14911 {
14912 cp_token *token;
14913 tree base;
14914 bool pack_expansion_p = false;
14915
14916 /* Look for the base-specifier. */
14917 base = cp_parser_base_specifier (parser);
14918 /* Look for the (optional) ellipsis. */
14919 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14920 {
14921 /* Consume the `...'. */
14922 cp_lexer_consume_token (parser->lexer);
14923
14924 pack_expansion_p = true;
14925 }
14926
14927 /* Add BASE to the front of the list. */
14928 if (base != error_mark_node)
14929 {
14930 if (pack_expansion_p)
14931 /* Make this a pack expansion type. */
14932 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14933 else
14934 check_for_bare_parameter_packs (TREE_VALUE (base));
14935
14936 TREE_CHAIN (base) = bases;
14937 bases = base;
14938 }
14939 /* Peek at the next token. */
14940 token = cp_lexer_peek_token (parser->lexer);
14941 /* If it's not a comma, then the list is complete. */
14942 if (token->type != CPP_COMMA)
14943 break;
14944 /* Consume the `,'. */
14945 cp_lexer_consume_token (parser->lexer);
14946 }
14947
14948 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14949 base class had a qualified name. However, the next name that
14950 appears is certainly not qualified. */
14951 parser->scope = NULL_TREE;
14952 parser->qualifying_scope = NULL_TREE;
14953 parser->object_scope = NULL_TREE;
14954
14955 return nreverse (bases);
14956 }
14957
14958 /* Parse a base-specifier.
14959
14960 base-specifier:
14961 :: [opt] nested-name-specifier [opt] class-name
14962 virtual access-specifier [opt] :: [opt] nested-name-specifier
14963 [opt] class-name
14964 access-specifier virtual [opt] :: [opt] nested-name-specifier
14965 [opt] class-name
14966
14967 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14968 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14969 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14970 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14971
14972 static tree
14973 cp_parser_base_specifier (cp_parser* parser)
14974 {
14975 cp_token *token;
14976 bool done = false;
14977 bool virtual_p = false;
14978 bool duplicate_virtual_error_issued_p = false;
14979 bool duplicate_access_error_issued_p = false;
14980 bool class_scope_p, template_p;
14981 tree access = access_default_node;
14982 tree type;
14983
14984 /* Process the optional `virtual' and `access-specifier'. */
14985 while (!done)
14986 {
14987 /* Peek at the next token. */
14988 token = cp_lexer_peek_token (parser->lexer);
14989 /* Process `virtual'. */
14990 switch (token->keyword)
14991 {
14992 case RID_VIRTUAL:
14993 /* If `virtual' appears more than once, issue an error. */
14994 if (virtual_p && !duplicate_virtual_error_issued_p)
14995 {
14996 cp_parser_error (parser,
14997 "%<virtual%> specified more than once in base-specified");
14998 duplicate_virtual_error_issued_p = true;
14999 }
15000
15001 virtual_p = true;
15002
15003 /* Consume the `virtual' token. */
15004 cp_lexer_consume_token (parser->lexer);
15005
15006 break;
15007
15008 case RID_PUBLIC:
15009 case RID_PROTECTED:
15010 case RID_PRIVATE:
15011 /* If more than one access specifier appears, issue an
15012 error. */
15013 if (access != access_default_node
15014 && !duplicate_access_error_issued_p)
15015 {
15016 cp_parser_error (parser,
15017 "more than one access specifier in base-specified");
15018 duplicate_access_error_issued_p = true;
15019 }
15020
15021 access = ridpointers[(int) token->keyword];
15022
15023 /* Consume the access-specifier. */
15024 cp_lexer_consume_token (parser->lexer);
15025
15026 break;
15027
15028 default:
15029 done = true;
15030 break;
15031 }
15032 }
15033 /* It is not uncommon to see programs mechanically, erroneously, use
15034 the 'typename' keyword to denote (dependent) qualified types
15035 as base classes. */
15036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15037 {
15038 if (!processing_template_decl)
15039 error ("keyword %<typename%> not allowed outside of templates");
15040 else
15041 error ("keyword %<typename%> not allowed in this context "
15042 "(the base class is implicitly a type)");
15043 cp_lexer_consume_token (parser->lexer);
15044 }
15045
15046 /* Look for the optional `::' operator. */
15047 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15048 /* Look for the nested-name-specifier. The simplest way to
15049 implement:
15050
15051 [temp.res]
15052
15053 The keyword `typename' is not permitted in a base-specifier or
15054 mem-initializer; in these contexts a qualified name that
15055 depends on a template-parameter is implicitly assumed to be a
15056 type name.
15057
15058 is to pretend that we have seen the `typename' keyword at this
15059 point. */
15060 cp_parser_nested_name_specifier_opt (parser,
15061 /*typename_keyword_p=*/true,
15062 /*check_dependency_p=*/true,
15063 typename_type,
15064 /*is_declaration=*/true);
15065 /* If the base class is given by a qualified name, assume that names
15066 we see are type names or templates, as appropriate. */
15067 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15068 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15069
15070 /* Finally, look for the class-name. */
15071 type = cp_parser_class_name (parser,
15072 class_scope_p,
15073 template_p,
15074 typename_type,
15075 /*check_dependency_p=*/true,
15076 /*class_head_p=*/false,
15077 /*is_declaration=*/true);
15078
15079 if (type == error_mark_node)
15080 return error_mark_node;
15081
15082 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15083 }
15084
15085 /* Exception handling [gram.exception] */
15086
15087 /* Parse an (optional) exception-specification.
15088
15089 exception-specification:
15090 throw ( type-id-list [opt] )
15091
15092 Returns a TREE_LIST representing the exception-specification. The
15093 TREE_VALUE of each node is a type. */
15094
15095 static tree
15096 cp_parser_exception_specification_opt (cp_parser* parser)
15097 {
15098 cp_token *token;
15099 tree type_id_list;
15100
15101 /* Peek at the next token. */
15102 token = cp_lexer_peek_token (parser->lexer);
15103 /* If it's not `throw', then there's no exception-specification. */
15104 if (!cp_parser_is_keyword (token, RID_THROW))
15105 return NULL_TREE;
15106
15107 /* Consume the `throw'. */
15108 cp_lexer_consume_token (parser->lexer);
15109
15110 /* Look for the `('. */
15111 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15112
15113 /* Peek at the next token. */
15114 token = cp_lexer_peek_token (parser->lexer);
15115 /* If it's not a `)', then there is a type-id-list. */
15116 if (token->type != CPP_CLOSE_PAREN)
15117 {
15118 const char *saved_message;
15119
15120 /* Types may not be defined in an exception-specification. */
15121 saved_message = parser->type_definition_forbidden_message;
15122 parser->type_definition_forbidden_message
15123 = "types may not be defined in an exception-specification";
15124 /* Parse the type-id-list. */
15125 type_id_list = cp_parser_type_id_list (parser);
15126 /* Restore the saved message. */
15127 parser->type_definition_forbidden_message = saved_message;
15128 }
15129 else
15130 type_id_list = empty_except_spec;
15131
15132 /* Look for the `)'. */
15133 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15134
15135 return type_id_list;
15136 }
15137
15138 /* Parse an (optional) type-id-list.
15139
15140 type-id-list:
15141 type-id ... [opt]
15142 type-id-list , type-id ... [opt]
15143
15144 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15145 in the order that the types were presented. */
15146
15147 static tree
15148 cp_parser_type_id_list (cp_parser* parser)
15149 {
15150 tree types = NULL_TREE;
15151
15152 while (true)
15153 {
15154 cp_token *token;
15155 tree type;
15156
15157 /* Get the next type-id. */
15158 type = cp_parser_type_id (parser);
15159 /* Parse the optional ellipsis. */
15160 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15161 {
15162 /* Consume the `...'. */
15163 cp_lexer_consume_token (parser->lexer);
15164
15165 /* Turn the type into a pack expansion expression. */
15166 type = make_pack_expansion (type);
15167 }
15168 /* Add it to the list. */
15169 types = add_exception_specifier (types, type, /*complain=*/1);
15170 /* Peek at the next token. */
15171 token = cp_lexer_peek_token (parser->lexer);
15172 /* If it is not a `,', we are done. */
15173 if (token->type != CPP_COMMA)
15174 break;
15175 /* Consume the `,'. */
15176 cp_lexer_consume_token (parser->lexer);
15177 }
15178
15179 return nreverse (types);
15180 }
15181
15182 /* Parse a try-block.
15183
15184 try-block:
15185 try compound-statement handler-seq */
15186
15187 static tree
15188 cp_parser_try_block (cp_parser* parser)
15189 {
15190 tree try_block;
15191
15192 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15193 try_block = begin_try_block ();
15194 cp_parser_compound_statement (parser, NULL, true);
15195 finish_try_block (try_block);
15196 cp_parser_handler_seq (parser);
15197 finish_handler_sequence (try_block);
15198
15199 return try_block;
15200 }
15201
15202 /* Parse a function-try-block.
15203
15204 function-try-block:
15205 try ctor-initializer [opt] function-body handler-seq */
15206
15207 static bool
15208 cp_parser_function_try_block (cp_parser* parser)
15209 {
15210 tree compound_stmt;
15211 tree try_block;
15212 bool ctor_initializer_p;
15213
15214 /* Look for the `try' keyword. */
15215 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15216 return false;
15217 /* Let the rest of the front end know where we are. */
15218 try_block = begin_function_try_block (&compound_stmt);
15219 /* Parse the function-body. */
15220 ctor_initializer_p
15221 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15222 /* We're done with the `try' part. */
15223 finish_function_try_block (try_block);
15224 /* Parse the handlers. */
15225 cp_parser_handler_seq (parser);
15226 /* We're done with the handlers. */
15227 finish_function_handler_sequence (try_block, compound_stmt);
15228
15229 return ctor_initializer_p;
15230 }
15231
15232 /* Parse a handler-seq.
15233
15234 handler-seq:
15235 handler handler-seq [opt] */
15236
15237 static void
15238 cp_parser_handler_seq (cp_parser* parser)
15239 {
15240 while (true)
15241 {
15242 cp_token *token;
15243
15244 /* Parse the handler. */
15245 cp_parser_handler (parser);
15246 /* Peek at the next token. */
15247 token = cp_lexer_peek_token (parser->lexer);
15248 /* If it's not `catch' then there are no more handlers. */
15249 if (!cp_parser_is_keyword (token, RID_CATCH))
15250 break;
15251 }
15252 }
15253
15254 /* Parse a handler.
15255
15256 handler:
15257 catch ( exception-declaration ) compound-statement */
15258
15259 static void
15260 cp_parser_handler (cp_parser* parser)
15261 {
15262 tree handler;
15263 tree declaration;
15264
15265 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15266 handler = begin_handler ();
15267 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15268 declaration = cp_parser_exception_declaration (parser);
15269 finish_handler_parms (declaration, handler);
15270 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15271 cp_parser_compound_statement (parser, NULL, false);
15272 finish_handler (handler);
15273 }
15274
15275 /* Parse an exception-declaration.
15276
15277 exception-declaration:
15278 type-specifier-seq declarator
15279 type-specifier-seq abstract-declarator
15280 type-specifier-seq
15281 ...
15282
15283 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15284 ellipsis variant is used. */
15285
15286 static tree
15287 cp_parser_exception_declaration (cp_parser* parser)
15288 {
15289 cp_decl_specifier_seq type_specifiers;
15290 cp_declarator *declarator;
15291 const char *saved_message;
15292
15293 /* If it's an ellipsis, it's easy to handle. */
15294 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15295 {
15296 /* Consume the `...' token. */
15297 cp_lexer_consume_token (parser->lexer);
15298 return NULL_TREE;
15299 }
15300
15301 /* Types may not be defined in exception-declarations. */
15302 saved_message = parser->type_definition_forbidden_message;
15303 parser->type_definition_forbidden_message
15304 = "types may not be defined in exception-declarations";
15305
15306 /* Parse the type-specifier-seq. */
15307 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15308 &type_specifiers);
15309 /* If it's a `)', then there is no declarator. */
15310 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15311 declarator = NULL;
15312 else
15313 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15314 /*ctor_dtor_or_conv_p=*/NULL,
15315 /*parenthesized_p=*/NULL,
15316 /*member_p=*/false);
15317
15318 /* Restore the saved message. */
15319 parser->type_definition_forbidden_message = saved_message;
15320
15321 if (!type_specifiers.any_specifiers_p)
15322 return error_mark_node;
15323
15324 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15325 }
15326
15327 /* Parse a throw-expression.
15328
15329 throw-expression:
15330 throw assignment-expression [opt]
15331
15332 Returns a THROW_EXPR representing the throw-expression. */
15333
15334 static tree
15335 cp_parser_throw_expression (cp_parser* parser)
15336 {
15337 tree expression;
15338 cp_token* token;
15339
15340 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15341 token = cp_lexer_peek_token (parser->lexer);
15342 /* Figure out whether or not there is an assignment-expression
15343 following the "throw" keyword. */
15344 if (token->type == CPP_COMMA
15345 || token->type == CPP_SEMICOLON
15346 || token->type == CPP_CLOSE_PAREN
15347 || token->type == CPP_CLOSE_SQUARE
15348 || token->type == CPP_CLOSE_BRACE
15349 || token->type == CPP_COLON)
15350 expression = NULL_TREE;
15351 else
15352 expression = cp_parser_assignment_expression (parser,
15353 /*cast_p=*/false);
15354
15355 return build_throw (expression);
15356 }
15357
15358 /* GNU Extensions */
15359
15360 /* Parse an (optional) asm-specification.
15361
15362 asm-specification:
15363 asm ( string-literal )
15364
15365 If the asm-specification is present, returns a STRING_CST
15366 corresponding to the string-literal. Otherwise, returns
15367 NULL_TREE. */
15368
15369 static tree
15370 cp_parser_asm_specification_opt (cp_parser* parser)
15371 {
15372 cp_token *token;
15373 tree asm_specification;
15374
15375 /* Peek at the next token. */
15376 token = cp_lexer_peek_token (parser->lexer);
15377 /* If the next token isn't the `asm' keyword, then there's no
15378 asm-specification. */
15379 if (!cp_parser_is_keyword (token, RID_ASM))
15380 return NULL_TREE;
15381
15382 /* Consume the `asm' token. */
15383 cp_lexer_consume_token (parser->lexer);
15384 /* Look for the `('. */
15385 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15386
15387 /* Look for the string-literal. */
15388 asm_specification = cp_parser_string_literal (parser, false, false);
15389
15390 /* Look for the `)'. */
15391 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15392
15393 return asm_specification;
15394 }
15395
15396 /* Parse an asm-operand-list.
15397
15398 asm-operand-list:
15399 asm-operand
15400 asm-operand-list , asm-operand
15401
15402 asm-operand:
15403 string-literal ( expression )
15404 [ string-literal ] string-literal ( expression )
15405
15406 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15407 each node is the expression. The TREE_PURPOSE is itself a
15408 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15409 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15410 is a STRING_CST for the string literal before the parenthesis. */
15411
15412 static tree
15413 cp_parser_asm_operand_list (cp_parser* parser)
15414 {
15415 tree asm_operands = NULL_TREE;
15416
15417 while (true)
15418 {
15419 tree string_literal;
15420 tree expression;
15421 tree name;
15422
15423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15424 {
15425 /* Consume the `[' token. */
15426 cp_lexer_consume_token (parser->lexer);
15427 /* Read the operand name. */
15428 name = cp_parser_identifier (parser);
15429 if (name != error_mark_node)
15430 name = build_string (IDENTIFIER_LENGTH (name),
15431 IDENTIFIER_POINTER (name));
15432 /* Look for the closing `]'. */
15433 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15434 }
15435 else
15436 name = NULL_TREE;
15437 /* Look for the string-literal. */
15438 string_literal = cp_parser_string_literal (parser, false, false);
15439
15440 /* Look for the `('. */
15441 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15442 /* Parse the expression. */
15443 expression = cp_parser_expression (parser, /*cast_p=*/false);
15444 /* Look for the `)'. */
15445 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15446
15447 /* Add this operand to the list. */
15448 asm_operands = tree_cons (build_tree_list (name, string_literal),
15449 expression,
15450 asm_operands);
15451 /* If the next token is not a `,', there are no more
15452 operands. */
15453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15454 break;
15455 /* Consume the `,'. */
15456 cp_lexer_consume_token (parser->lexer);
15457 }
15458
15459 return nreverse (asm_operands);
15460 }
15461
15462 /* Parse an asm-clobber-list.
15463
15464 asm-clobber-list:
15465 string-literal
15466 asm-clobber-list , string-literal
15467
15468 Returns a TREE_LIST, indicating the clobbers in the order that they
15469 appeared. The TREE_VALUE of each node is a STRING_CST. */
15470
15471 static tree
15472 cp_parser_asm_clobber_list (cp_parser* parser)
15473 {
15474 tree clobbers = NULL_TREE;
15475
15476 while (true)
15477 {
15478 tree string_literal;
15479
15480 /* Look for the string literal. */
15481 string_literal = cp_parser_string_literal (parser, false, false);
15482 /* Add it to the list. */
15483 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15484 /* If the next token is not a `,', then the list is
15485 complete. */
15486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15487 break;
15488 /* Consume the `,' token. */
15489 cp_lexer_consume_token (parser->lexer);
15490 }
15491
15492 return clobbers;
15493 }
15494
15495 /* Parse an (optional) series of attributes.
15496
15497 attributes:
15498 attributes attribute
15499
15500 attribute:
15501 __attribute__ (( attribute-list [opt] ))
15502
15503 The return value is as for cp_parser_attribute_list. */
15504
15505 static tree
15506 cp_parser_attributes_opt (cp_parser* parser)
15507 {
15508 tree attributes = NULL_TREE;
15509
15510 while (true)
15511 {
15512 cp_token *token;
15513 tree attribute_list;
15514
15515 /* Peek at the next token. */
15516 token = cp_lexer_peek_token (parser->lexer);
15517 /* If it's not `__attribute__', then we're done. */
15518 if (token->keyword != RID_ATTRIBUTE)
15519 break;
15520
15521 /* Consume the `__attribute__' keyword. */
15522 cp_lexer_consume_token (parser->lexer);
15523 /* Look for the two `(' tokens. */
15524 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15525 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15526
15527 /* Peek at the next token. */
15528 token = cp_lexer_peek_token (parser->lexer);
15529 if (token->type != CPP_CLOSE_PAREN)
15530 /* Parse the attribute-list. */
15531 attribute_list = cp_parser_attribute_list (parser);
15532 else
15533 /* If the next token is a `)', then there is no attribute
15534 list. */
15535 attribute_list = NULL;
15536
15537 /* Look for the two `)' tokens. */
15538 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15539 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15540
15541 /* Add these new attributes to the list. */
15542 attributes = chainon (attributes, attribute_list);
15543 }
15544
15545 return attributes;
15546 }
15547
15548 /* Parse an attribute-list.
15549
15550 attribute-list:
15551 attribute
15552 attribute-list , attribute
15553
15554 attribute:
15555 identifier
15556 identifier ( identifier )
15557 identifier ( identifier , expression-list )
15558 identifier ( expression-list )
15559
15560 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15561 to an attribute. The TREE_PURPOSE of each node is the identifier
15562 indicating which attribute is in use. The TREE_VALUE represents
15563 the arguments, if any. */
15564
15565 static tree
15566 cp_parser_attribute_list (cp_parser* parser)
15567 {
15568 tree attribute_list = NULL_TREE;
15569 bool save_translate_strings_p = parser->translate_strings_p;
15570
15571 parser->translate_strings_p = false;
15572 while (true)
15573 {
15574 cp_token *token;
15575 tree identifier;
15576 tree attribute;
15577
15578 /* Look for the identifier. We also allow keywords here; for
15579 example `__attribute__ ((const))' is legal. */
15580 token = cp_lexer_peek_token (parser->lexer);
15581 if (token->type == CPP_NAME
15582 || token->type == CPP_KEYWORD)
15583 {
15584 tree arguments = NULL_TREE;
15585
15586 /* Consume the token. */
15587 token = cp_lexer_consume_token (parser->lexer);
15588
15589 /* Save away the identifier that indicates which attribute
15590 this is. */
15591 identifier = token->u.value;
15592 attribute = build_tree_list (identifier, NULL_TREE);
15593
15594 /* Peek at the next token. */
15595 token = cp_lexer_peek_token (parser->lexer);
15596 /* If it's an `(', then parse the attribute arguments. */
15597 if (token->type == CPP_OPEN_PAREN)
15598 {
15599 arguments = cp_parser_parenthesized_expression_list
15600 (parser, true, /*cast_p=*/false,
15601 /*allow_expansion_p=*/false,
15602 /*non_constant_p=*/NULL);
15603 /* Save the arguments away. */
15604 TREE_VALUE (attribute) = arguments;
15605 }
15606
15607 if (arguments != error_mark_node)
15608 {
15609 /* Add this attribute to the list. */
15610 TREE_CHAIN (attribute) = attribute_list;
15611 attribute_list = attribute;
15612 }
15613
15614 token = cp_lexer_peek_token (parser->lexer);
15615 }
15616 /* Now, look for more attributes. If the next token isn't a
15617 `,', we're done. */
15618 if (token->type != CPP_COMMA)
15619 break;
15620
15621 /* Consume the comma and keep going. */
15622 cp_lexer_consume_token (parser->lexer);
15623 }
15624 parser->translate_strings_p = save_translate_strings_p;
15625
15626 /* We built up the list in reverse order. */
15627 return nreverse (attribute_list);
15628 }
15629
15630 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15631 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15632 current value of the PEDANTIC flag, regardless of whether or not
15633 the `__extension__' keyword is present. The caller is responsible
15634 for restoring the value of the PEDANTIC flag. */
15635
15636 static bool
15637 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15638 {
15639 /* Save the old value of the PEDANTIC flag. */
15640 *saved_pedantic = pedantic;
15641
15642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15643 {
15644 /* Consume the `__extension__' token. */
15645 cp_lexer_consume_token (parser->lexer);
15646 /* We're not being pedantic while the `__extension__' keyword is
15647 in effect. */
15648 pedantic = 0;
15649
15650 return true;
15651 }
15652
15653 return false;
15654 }
15655
15656 /* Parse a label declaration.
15657
15658 label-declaration:
15659 __label__ label-declarator-seq ;
15660
15661 label-declarator-seq:
15662 identifier , label-declarator-seq
15663 identifier */
15664
15665 static void
15666 cp_parser_label_declaration (cp_parser* parser)
15667 {
15668 /* Look for the `__label__' keyword. */
15669 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15670
15671 while (true)
15672 {
15673 tree identifier;
15674
15675 /* Look for an identifier. */
15676 identifier = cp_parser_identifier (parser);
15677 /* If we failed, stop. */
15678 if (identifier == error_mark_node)
15679 break;
15680 /* Declare it as a label. */
15681 finish_label_decl (identifier);
15682 /* If the next token is a `;', stop. */
15683 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15684 break;
15685 /* Look for the `,' separating the label declarations. */
15686 cp_parser_require (parser, CPP_COMMA, "`,'");
15687 }
15688
15689 /* Look for the final `;'. */
15690 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15691 }
15692
15693 /* Support Functions */
15694
15695 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15696 NAME should have one of the representations used for an
15697 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15698 is returned. If PARSER->SCOPE is a dependent type, then a
15699 SCOPE_REF is returned.
15700
15701 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15702 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15703 was formed. Abstractly, such entities should not be passed to this
15704 function, because they do not need to be looked up, but it is
15705 simpler to check for this special case here, rather than at the
15706 call-sites.
15707
15708 In cases not explicitly covered above, this function returns a
15709 DECL, OVERLOAD, or baselink representing the result of the lookup.
15710 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15711 is returned.
15712
15713 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15714 (e.g., "struct") that was used. In that case bindings that do not
15715 refer to types are ignored.
15716
15717 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15718 ignored.
15719
15720 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15721 are ignored.
15722
15723 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15724 types.
15725
15726 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15727 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15728 NULL_TREE otherwise. */
15729
15730 static tree
15731 cp_parser_lookup_name (cp_parser *parser, tree name,
15732 enum tag_types tag_type,
15733 bool is_template,
15734 bool is_namespace,
15735 bool check_dependency,
15736 tree *ambiguous_decls)
15737 {
15738 int flags = 0;
15739 tree decl;
15740 tree object_type = parser->context->object_type;
15741
15742 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15743 flags |= LOOKUP_COMPLAIN;
15744
15745 /* Assume that the lookup will be unambiguous. */
15746 if (ambiguous_decls)
15747 *ambiguous_decls = NULL_TREE;
15748
15749 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15750 no longer valid. Note that if we are parsing tentatively, and
15751 the parse fails, OBJECT_TYPE will be automatically restored. */
15752 parser->context->object_type = NULL_TREE;
15753
15754 if (name == error_mark_node)
15755 return error_mark_node;
15756
15757 /* A template-id has already been resolved; there is no lookup to
15758 do. */
15759 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15760 return name;
15761 if (BASELINK_P (name))
15762 {
15763 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15764 == TEMPLATE_ID_EXPR);
15765 return name;
15766 }
15767
15768 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15769 it should already have been checked to make sure that the name
15770 used matches the type being destroyed. */
15771 if (TREE_CODE (name) == BIT_NOT_EXPR)
15772 {
15773 tree type;
15774
15775 /* Figure out to which type this destructor applies. */
15776 if (parser->scope)
15777 type = parser->scope;
15778 else if (object_type)
15779 type = object_type;
15780 else
15781 type = current_class_type;
15782 /* If that's not a class type, there is no destructor. */
15783 if (!type || !CLASS_TYPE_P (type))
15784 return error_mark_node;
15785 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15786 lazily_declare_fn (sfk_destructor, type);
15787 if (!CLASSTYPE_DESTRUCTORS (type))
15788 return error_mark_node;
15789 /* If it was a class type, return the destructor. */
15790 return CLASSTYPE_DESTRUCTORS (type);
15791 }
15792
15793 /* By this point, the NAME should be an ordinary identifier. If
15794 the id-expression was a qualified name, the qualifying scope is
15795 stored in PARSER->SCOPE at this point. */
15796 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15797
15798 /* Perform the lookup. */
15799 if (parser->scope)
15800 {
15801 bool dependent_p;
15802
15803 if (parser->scope == error_mark_node)
15804 return error_mark_node;
15805
15806 /* If the SCOPE is dependent, the lookup must be deferred until
15807 the template is instantiated -- unless we are explicitly
15808 looking up names in uninstantiated templates. Even then, we
15809 cannot look up the name if the scope is not a class type; it
15810 might, for example, be a template type parameter. */
15811 dependent_p = (TYPE_P (parser->scope)
15812 && !(parser->in_declarator_p
15813 && currently_open_class (parser->scope))
15814 && dependent_type_p (parser->scope));
15815 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15816 && dependent_p)
15817 {
15818 if (tag_type)
15819 {
15820 tree type;
15821
15822 /* The resolution to Core Issue 180 says that `struct
15823 A::B' should be considered a type-name, even if `A'
15824 is dependent. */
15825 type = make_typename_type (parser->scope, name, tag_type,
15826 /*complain=*/tf_error);
15827 decl = TYPE_NAME (type);
15828 }
15829 else if (is_template
15830 && (cp_parser_next_token_ends_template_argument_p (parser)
15831 || cp_lexer_next_token_is (parser->lexer,
15832 CPP_CLOSE_PAREN)))
15833 decl = make_unbound_class_template (parser->scope,
15834 name, NULL_TREE,
15835 /*complain=*/tf_error);
15836 else
15837 decl = build_qualified_name (/*type=*/NULL_TREE,
15838 parser->scope, name,
15839 is_template);
15840 }
15841 else
15842 {
15843 tree pushed_scope = NULL_TREE;
15844
15845 /* If PARSER->SCOPE is a dependent type, then it must be a
15846 class type, and we must not be checking dependencies;
15847 otherwise, we would have processed this lookup above. So
15848 that PARSER->SCOPE is not considered a dependent base by
15849 lookup_member, we must enter the scope here. */
15850 if (dependent_p)
15851 pushed_scope = push_scope (parser->scope);
15852 /* If the PARSER->SCOPE is a template specialization, it
15853 may be instantiated during name lookup. In that case,
15854 errors may be issued. Even if we rollback the current
15855 tentative parse, those errors are valid. */
15856 decl = lookup_qualified_name (parser->scope, name,
15857 tag_type != none_type,
15858 /*complain=*/true);
15859 if (pushed_scope)
15860 pop_scope (pushed_scope);
15861 }
15862 parser->qualifying_scope = parser->scope;
15863 parser->object_scope = NULL_TREE;
15864 }
15865 else if (object_type)
15866 {
15867 tree object_decl = NULL_TREE;
15868 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15869 OBJECT_TYPE is not a class. */
15870 if (CLASS_TYPE_P (object_type))
15871 /* If the OBJECT_TYPE is a template specialization, it may
15872 be instantiated during name lookup. In that case, errors
15873 may be issued. Even if we rollback the current tentative
15874 parse, those errors are valid. */
15875 object_decl = lookup_member (object_type,
15876 name,
15877 /*protect=*/0,
15878 tag_type != none_type);
15879 /* Look it up in the enclosing context, too. */
15880 decl = lookup_name_real (name, tag_type != none_type,
15881 /*nonclass=*/0,
15882 /*block_p=*/true, is_namespace, flags);
15883 parser->object_scope = object_type;
15884 parser->qualifying_scope = NULL_TREE;
15885 if (object_decl)
15886 decl = object_decl;
15887 }
15888 else
15889 {
15890 decl = lookup_name_real (name, tag_type != none_type,
15891 /*nonclass=*/0,
15892 /*block_p=*/true, is_namespace, flags);
15893 parser->qualifying_scope = NULL_TREE;
15894 parser->object_scope = NULL_TREE;
15895 }
15896
15897 /* If the lookup failed, let our caller know. */
15898 if (!decl || decl == error_mark_node)
15899 return error_mark_node;
15900
15901 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15902 if (TREE_CODE (decl) == TREE_LIST)
15903 {
15904 if (ambiguous_decls)
15905 *ambiguous_decls = decl;
15906 /* The error message we have to print is too complicated for
15907 cp_parser_error, so we incorporate its actions directly. */
15908 if (!cp_parser_simulate_error (parser))
15909 {
15910 error ("reference to %qD is ambiguous", name);
15911 print_candidates (decl);
15912 }
15913 return error_mark_node;
15914 }
15915
15916 gcc_assert (DECL_P (decl)
15917 || TREE_CODE (decl) == OVERLOAD
15918 || TREE_CODE (decl) == SCOPE_REF
15919 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15920 || BASELINK_P (decl));
15921
15922 /* If we have resolved the name of a member declaration, check to
15923 see if the declaration is accessible. When the name resolves to
15924 set of overloaded functions, accessibility is checked when
15925 overload resolution is done.
15926
15927 During an explicit instantiation, access is not checked at all,
15928 as per [temp.explicit]. */
15929 if (DECL_P (decl))
15930 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15931
15932 return decl;
15933 }
15934
15935 /* Like cp_parser_lookup_name, but for use in the typical case where
15936 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15937 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15938
15939 static tree
15940 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15941 {
15942 return cp_parser_lookup_name (parser, name,
15943 none_type,
15944 /*is_template=*/false,
15945 /*is_namespace=*/false,
15946 /*check_dependency=*/true,
15947 /*ambiguous_decls=*/NULL);
15948 }
15949
15950 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15951 the current context, return the TYPE_DECL. If TAG_NAME_P is
15952 true, the DECL indicates the class being defined in a class-head,
15953 or declared in an elaborated-type-specifier.
15954
15955 Otherwise, return DECL. */
15956
15957 static tree
15958 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15959 {
15960 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15961 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15962
15963 struct A {
15964 template <typename T> struct B;
15965 };
15966
15967 template <typename T> struct A::B {};
15968
15969 Similarly, in an elaborated-type-specifier:
15970
15971 namespace N { struct X{}; }
15972
15973 struct A {
15974 template <typename T> friend struct N::X;
15975 };
15976
15977 However, if the DECL refers to a class type, and we are in
15978 the scope of the class, then the name lookup automatically
15979 finds the TYPE_DECL created by build_self_reference rather
15980 than a TEMPLATE_DECL. For example, in:
15981
15982 template <class T> struct S {
15983 S s;
15984 };
15985
15986 there is no need to handle such case. */
15987
15988 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15989 return DECL_TEMPLATE_RESULT (decl);
15990
15991 return decl;
15992 }
15993
15994 /* If too many, or too few, template-parameter lists apply to the
15995 declarator, issue an error message. Returns TRUE if all went well,
15996 and FALSE otherwise. */
15997
15998 static bool
15999 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16000 cp_declarator *declarator)
16001 {
16002 unsigned num_templates;
16003
16004 /* We haven't seen any classes that involve template parameters yet. */
16005 num_templates = 0;
16006
16007 switch (declarator->kind)
16008 {
16009 case cdk_id:
16010 if (declarator->u.id.qualifying_scope)
16011 {
16012 tree scope;
16013 tree member;
16014
16015 scope = declarator->u.id.qualifying_scope;
16016 member = declarator->u.id.unqualified_name;
16017
16018 while (scope && CLASS_TYPE_P (scope))
16019 {
16020 /* You're supposed to have one `template <...>'
16021 for every template class, but you don't need one
16022 for a full specialization. For example:
16023
16024 template <class T> struct S{};
16025 template <> struct S<int> { void f(); };
16026 void S<int>::f () {}
16027
16028 is correct; there shouldn't be a `template <>' for
16029 the definition of `S<int>::f'. */
16030 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16031 /* If SCOPE does not have template information of any
16032 kind, then it is not a template, nor is it nested
16033 within a template. */
16034 break;
16035 if (explicit_class_specialization_p (scope))
16036 break;
16037 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16038 ++num_templates;
16039
16040 scope = TYPE_CONTEXT (scope);
16041 }
16042 }
16043 else if (TREE_CODE (declarator->u.id.unqualified_name)
16044 == TEMPLATE_ID_EXPR)
16045 /* If the DECLARATOR has the form `X<y>' then it uses one
16046 additional level of template parameters. */
16047 ++num_templates;
16048
16049 return cp_parser_check_template_parameters (parser,
16050 num_templates);
16051
16052 case cdk_function:
16053 case cdk_array:
16054 case cdk_pointer:
16055 case cdk_reference:
16056 case cdk_ptrmem:
16057 return (cp_parser_check_declarator_template_parameters
16058 (parser, declarator->declarator));
16059
16060 case cdk_error:
16061 return true;
16062
16063 default:
16064 gcc_unreachable ();
16065 }
16066 return false;
16067 }
16068
16069 /* NUM_TEMPLATES were used in the current declaration. If that is
16070 invalid, return FALSE and issue an error messages. Otherwise,
16071 return TRUE. */
16072
16073 static bool
16074 cp_parser_check_template_parameters (cp_parser* parser,
16075 unsigned num_templates)
16076 {
16077 /* If there are more template classes than parameter lists, we have
16078 something like:
16079
16080 template <class T> void S<T>::R<T>::f (); */
16081 if (parser->num_template_parameter_lists < num_templates)
16082 {
16083 error ("too few template-parameter-lists");
16084 return false;
16085 }
16086 /* If there are the same number of template classes and parameter
16087 lists, that's OK. */
16088 if (parser->num_template_parameter_lists == num_templates)
16089 return true;
16090 /* If there are more, but only one more, then we are referring to a
16091 member template. That's OK too. */
16092 if (parser->num_template_parameter_lists == num_templates + 1)
16093 return true;
16094 /* Otherwise, there are too many template parameter lists. We have
16095 something like:
16096
16097 template <class T> template <class U> void S::f(); */
16098 error ("too many template-parameter-lists");
16099 return false;
16100 }
16101
16102 /* Parse an optional `::' token indicating that the following name is
16103 from the global namespace. If so, PARSER->SCOPE is set to the
16104 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16105 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16106 Returns the new value of PARSER->SCOPE, if the `::' token is
16107 present, and NULL_TREE otherwise. */
16108
16109 static tree
16110 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16111 {
16112 cp_token *token;
16113
16114 /* Peek at the next token. */
16115 token = cp_lexer_peek_token (parser->lexer);
16116 /* If we're looking at a `::' token then we're starting from the
16117 global namespace, not our current location. */
16118 if (token->type == CPP_SCOPE)
16119 {
16120 /* Consume the `::' token. */
16121 cp_lexer_consume_token (parser->lexer);
16122 /* Set the SCOPE so that we know where to start the lookup. */
16123 parser->scope = global_namespace;
16124 parser->qualifying_scope = global_namespace;
16125 parser->object_scope = NULL_TREE;
16126
16127 return parser->scope;
16128 }
16129 else if (!current_scope_valid_p)
16130 {
16131 parser->scope = NULL_TREE;
16132 parser->qualifying_scope = NULL_TREE;
16133 parser->object_scope = NULL_TREE;
16134 }
16135
16136 return NULL_TREE;
16137 }
16138
16139 /* Returns TRUE if the upcoming token sequence is the start of a
16140 constructor declarator. If FRIEND_P is true, the declarator is
16141 preceded by the `friend' specifier. */
16142
16143 static bool
16144 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16145 {
16146 bool constructor_p;
16147 tree type_decl = NULL_TREE;
16148 bool nested_name_p;
16149 cp_token *next_token;
16150
16151 /* The common case is that this is not a constructor declarator, so
16152 try to avoid doing lots of work if at all possible. It's not
16153 valid declare a constructor at function scope. */
16154 if (parser->in_function_body)
16155 return false;
16156 /* And only certain tokens can begin a constructor declarator. */
16157 next_token = cp_lexer_peek_token (parser->lexer);
16158 if (next_token->type != CPP_NAME
16159 && next_token->type != CPP_SCOPE
16160 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16161 && next_token->type != CPP_TEMPLATE_ID)
16162 return false;
16163
16164 /* Parse tentatively; we are going to roll back all of the tokens
16165 consumed here. */
16166 cp_parser_parse_tentatively (parser);
16167 /* Assume that we are looking at a constructor declarator. */
16168 constructor_p = true;
16169
16170 /* Look for the optional `::' operator. */
16171 cp_parser_global_scope_opt (parser,
16172 /*current_scope_valid_p=*/false);
16173 /* Look for the nested-name-specifier. */
16174 nested_name_p
16175 = (cp_parser_nested_name_specifier_opt (parser,
16176 /*typename_keyword_p=*/false,
16177 /*check_dependency_p=*/false,
16178 /*type_p=*/false,
16179 /*is_declaration=*/false)
16180 != NULL_TREE);
16181 /* Outside of a class-specifier, there must be a
16182 nested-name-specifier. */
16183 if (!nested_name_p &&
16184 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16185 || friend_p))
16186 constructor_p = false;
16187 /* If we still think that this might be a constructor-declarator,
16188 look for a class-name. */
16189 if (constructor_p)
16190 {
16191 /* If we have:
16192
16193 template <typename T> struct S { S(); };
16194 template <typename T> S<T>::S ();
16195
16196 we must recognize that the nested `S' names a class.
16197 Similarly, for:
16198
16199 template <typename T> S<T>::S<T> ();
16200
16201 we must recognize that the nested `S' names a template. */
16202 type_decl = cp_parser_class_name (parser,
16203 /*typename_keyword_p=*/false,
16204 /*template_keyword_p=*/false,
16205 none_type,
16206 /*check_dependency_p=*/false,
16207 /*class_head_p=*/false,
16208 /*is_declaration=*/false);
16209 /* If there was no class-name, then this is not a constructor. */
16210 constructor_p = !cp_parser_error_occurred (parser);
16211 }
16212
16213 /* If we're still considering a constructor, we have to see a `(',
16214 to begin the parameter-declaration-clause, followed by either a
16215 `)', an `...', or a decl-specifier. We need to check for a
16216 type-specifier to avoid being fooled into thinking that:
16217
16218 S::S (f) (int);
16219
16220 is a constructor. (It is actually a function named `f' that
16221 takes one parameter (of type `int') and returns a value of type
16222 `S::S'. */
16223 if (constructor_p
16224 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16225 {
16226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16227 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16228 /* A parameter declaration begins with a decl-specifier,
16229 which is either the "attribute" keyword, a storage class
16230 specifier, or (usually) a type-specifier. */
16231 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16232 {
16233 tree type;
16234 tree pushed_scope = NULL_TREE;
16235 unsigned saved_num_template_parameter_lists;
16236
16237 /* Names appearing in the type-specifier should be looked up
16238 in the scope of the class. */
16239 if (current_class_type)
16240 type = NULL_TREE;
16241 else
16242 {
16243 type = TREE_TYPE (type_decl);
16244 if (TREE_CODE (type) == TYPENAME_TYPE)
16245 {
16246 type = resolve_typename_type (type,
16247 /*only_current_p=*/false);
16248 if (type == error_mark_node)
16249 {
16250 cp_parser_abort_tentative_parse (parser);
16251 return false;
16252 }
16253 }
16254 pushed_scope = push_scope (type);
16255 }
16256
16257 /* Inside the constructor parameter list, surrounding
16258 template-parameter-lists do not apply. */
16259 saved_num_template_parameter_lists
16260 = parser->num_template_parameter_lists;
16261 parser->num_template_parameter_lists = 0;
16262
16263 /* Look for the type-specifier. */
16264 cp_parser_type_specifier (parser,
16265 CP_PARSER_FLAGS_NONE,
16266 /*decl_specs=*/NULL,
16267 /*is_declarator=*/true,
16268 /*declares_class_or_enum=*/NULL,
16269 /*is_cv_qualifier=*/NULL);
16270
16271 parser->num_template_parameter_lists
16272 = saved_num_template_parameter_lists;
16273
16274 /* Leave the scope of the class. */
16275 if (pushed_scope)
16276 pop_scope (pushed_scope);
16277
16278 constructor_p = !cp_parser_error_occurred (parser);
16279 }
16280 }
16281 else
16282 constructor_p = false;
16283 /* We did not really want to consume any tokens. */
16284 cp_parser_abort_tentative_parse (parser);
16285
16286 return constructor_p;
16287 }
16288
16289 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16290 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16291 they must be performed once we are in the scope of the function.
16292
16293 Returns the function defined. */
16294
16295 static tree
16296 cp_parser_function_definition_from_specifiers_and_declarator
16297 (cp_parser* parser,
16298 cp_decl_specifier_seq *decl_specifiers,
16299 tree attributes,
16300 const cp_declarator *declarator)
16301 {
16302 tree fn;
16303 bool success_p;
16304
16305 /* Begin the function-definition. */
16306 success_p = start_function (decl_specifiers, declarator, attributes);
16307
16308 /* The things we're about to see are not directly qualified by any
16309 template headers we've seen thus far. */
16310 reset_specialization ();
16311
16312 /* If there were names looked up in the decl-specifier-seq that we
16313 did not check, check them now. We must wait until we are in the
16314 scope of the function to perform the checks, since the function
16315 might be a friend. */
16316 perform_deferred_access_checks ();
16317
16318 if (!success_p)
16319 {
16320 /* Skip the entire function. */
16321 cp_parser_skip_to_end_of_block_or_statement (parser);
16322 fn = error_mark_node;
16323 }
16324 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16325 {
16326 /* Seen already, skip it. An error message has already been output. */
16327 cp_parser_skip_to_end_of_block_or_statement (parser);
16328 fn = current_function_decl;
16329 current_function_decl = NULL_TREE;
16330 /* If this is a function from a class, pop the nested class. */
16331 if (current_class_name)
16332 pop_nested_class ();
16333 }
16334 else
16335 fn = cp_parser_function_definition_after_declarator (parser,
16336 /*inline_p=*/false);
16337
16338 return fn;
16339 }
16340
16341 /* Parse the part of a function-definition that follows the
16342 declarator. INLINE_P is TRUE iff this function is an inline
16343 function defined with a class-specifier.
16344
16345 Returns the function defined. */
16346
16347 static tree
16348 cp_parser_function_definition_after_declarator (cp_parser* parser,
16349 bool inline_p)
16350 {
16351 tree fn;
16352 bool ctor_initializer_p = false;
16353 bool saved_in_unbraced_linkage_specification_p;
16354 bool saved_in_function_body;
16355 unsigned saved_num_template_parameter_lists;
16356
16357 saved_in_function_body = parser->in_function_body;
16358 parser->in_function_body = true;
16359 /* If the next token is `return', then the code may be trying to
16360 make use of the "named return value" extension that G++ used to
16361 support. */
16362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16363 {
16364 /* Consume the `return' keyword. */
16365 cp_lexer_consume_token (parser->lexer);
16366 /* Look for the identifier that indicates what value is to be
16367 returned. */
16368 cp_parser_identifier (parser);
16369 /* Issue an error message. */
16370 error ("named return values are no longer supported");
16371 /* Skip tokens until we reach the start of the function body. */
16372 while (true)
16373 {
16374 cp_token *token = cp_lexer_peek_token (parser->lexer);
16375 if (token->type == CPP_OPEN_BRACE
16376 || token->type == CPP_EOF
16377 || token->type == CPP_PRAGMA_EOL)
16378 break;
16379 cp_lexer_consume_token (parser->lexer);
16380 }
16381 }
16382 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16383 anything declared inside `f'. */
16384 saved_in_unbraced_linkage_specification_p
16385 = parser->in_unbraced_linkage_specification_p;
16386 parser->in_unbraced_linkage_specification_p = false;
16387 /* Inside the function, surrounding template-parameter-lists do not
16388 apply. */
16389 saved_num_template_parameter_lists
16390 = parser->num_template_parameter_lists;
16391 parser->num_template_parameter_lists = 0;
16392 /* If the next token is `try', then we are looking at a
16393 function-try-block. */
16394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16395 ctor_initializer_p = cp_parser_function_try_block (parser);
16396 /* A function-try-block includes the function-body, so we only do
16397 this next part if we're not processing a function-try-block. */
16398 else
16399 ctor_initializer_p
16400 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16401
16402 /* Finish the function. */
16403 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16404 (inline_p ? 2 : 0));
16405 /* Generate code for it, if necessary. */
16406 expand_or_defer_fn (fn);
16407 /* Restore the saved values. */
16408 parser->in_unbraced_linkage_specification_p
16409 = saved_in_unbraced_linkage_specification_p;
16410 parser->num_template_parameter_lists
16411 = saved_num_template_parameter_lists;
16412 parser->in_function_body = saved_in_function_body;
16413
16414 return fn;
16415 }
16416
16417 /* Parse a template-declaration, assuming that the `export' (and
16418 `extern') keywords, if present, has already been scanned. MEMBER_P
16419 is as for cp_parser_template_declaration. */
16420
16421 static void
16422 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16423 {
16424 tree decl = NULL_TREE;
16425 VEC (deferred_access_check,gc) *checks;
16426 tree parameter_list;
16427 bool friend_p = false;
16428 bool need_lang_pop;
16429
16430 /* Look for the `template' keyword. */
16431 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16432 return;
16433
16434 /* And the `<'. */
16435 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16436 return;
16437 if (at_class_scope_p () && current_function_decl)
16438 {
16439 /* 14.5.2.2 [temp.mem]
16440
16441 A local class shall not have member templates. */
16442 error ("invalid declaration of member template in local class");
16443 cp_parser_skip_to_end_of_block_or_statement (parser);
16444 return;
16445 }
16446 /* [temp]
16447
16448 A template ... shall not have C linkage. */
16449 if (current_lang_name == lang_name_c)
16450 {
16451 error ("template with C linkage");
16452 /* Give it C++ linkage to avoid confusing other parts of the
16453 front end. */
16454 push_lang_context (lang_name_cplusplus);
16455 need_lang_pop = true;
16456 }
16457 else
16458 need_lang_pop = false;
16459
16460 /* We cannot perform access checks on the template parameter
16461 declarations until we know what is being declared, just as we
16462 cannot check the decl-specifier list. */
16463 push_deferring_access_checks (dk_deferred);
16464
16465 /* If the next token is `>', then we have an invalid
16466 specialization. Rather than complain about an invalid template
16467 parameter, issue an error message here. */
16468 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16469 {
16470 cp_parser_error (parser, "invalid explicit specialization");
16471 begin_specialization ();
16472 parameter_list = NULL_TREE;
16473 }
16474 else
16475 /* Parse the template parameters. */
16476 parameter_list = cp_parser_template_parameter_list (parser);
16477
16478 /* Get the deferred access checks from the parameter list. These
16479 will be checked once we know what is being declared, as for a
16480 member template the checks must be performed in the scope of the
16481 class containing the member. */
16482 checks = get_deferred_access_checks ();
16483
16484 /* Look for the `>'. */
16485 cp_parser_skip_to_end_of_template_parameter_list (parser);
16486 /* We just processed one more parameter list. */
16487 ++parser->num_template_parameter_lists;
16488 /* If the next token is `template', there are more template
16489 parameters. */
16490 if (cp_lexer_next_token_is_keyword (parser->lexer,
16491 RID_TEMPLATE))
16492 cp_parser_template_declaration_after_export (parser, member_p);
16493 else
16494 {
16495 /* There are no access checks when parsing a template, as we do not
16496 know if a specialization will be a friend. */
16497 push_deferring_access_checks (dk_no_check);
16498 decl = cp_parser_single_declaration (parser,
16499 checks,
16500 member_p,
16501 &friend_p);
16502 pop_deferring_access_checks ();
16503
16504 /* If this is a member template declaration, let the front
16505 end know. */
16506 if (member_p && !friend_p && decl)
16507 {
16508 if (TREE_CODE (decl) == TYPE_DECL)
16509 cp_parser_check_access_in_redeclaration (decl);
16510
16511 decl = finish_member_template_decl (decl);
16512 }
16513 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16514 make_friend_class (current_class_type, TREE_TYPE (decl),
16515 /*complain=*/true);
16516 }
16517 /* We are done with the current parameter list. */
16518 --parser->num_template_parameter_lists;
16519
16520 pop_deferring_access_checks ();
16521
16522 /* Finish up. */
16523 finish_template_decl (parameter_list);
16524
16525 /* Register member declarations. */
16526 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16527 finish_member_declaration (decl);
16528 /* For the erroneous case of a template with C linkage, we pushed an
16529 implicit C++ linkage scope; exit that scope now. */
16530 if (need_lang_pop)
16531 pop_lang_context ();
16532 /* If DECL is a function template, we must return to parse it later.
16533 (Even though there is no definition, there might be default
16534 arguments that need handling.) */
16535 if (member_p && decl
16536 && (TREE_CODE (decl) == FUNCTION_DECL
16537 || DECL_FUNCTION_TEMPLATE_P (decl)))
16538 TREE_VALUE (parser->unparsed_functions_queues)
16539 = tree_cons (NULL_TREE, decl,
16540 TREE_VALUE (parser->unparsed_functions_queues));
16541 }
16542
16543 /* Perform the deferred access checks from a template-parameter-list.
16544 CHECKS is a TREE_LIST of access checks, as returned by
16545 get_deferred_access_checks. */
16546
16547 static void
16548 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16549 {
16550 ++processing_template_parmlist;
16551 perform_access_checks (checks);
16552 --processing_template_parmlist;
16553 }
16554
16555 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16556 `function-definition' sequence. MEMBER_P is true, this declaration
16557 appears in a class scope.
16558
16559 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16560 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16561
16562 static tree
16563 cp_parser_single_declaration (cp_parser* parser,
16564 VEC (deferred_access_check,gc)* checks,
16565 bool member_p,
16566 bool* friend_p)
16567 {
16568 int declares_class_or_enum;
16569 tree decl = NULL_TREE;
16570 cp_decl_specifier_seq decl_specifiers;
16571 bool function_definition_p = false;
16572
16573 /* This function is only used when processing a template
16574 declaration. */
16575 gcc_assert (innermost_scope_kind () == sk_template_parms
16576 || innermost_scope_kind () == sk_template_spec);
16577
16578 /* Defer access checks until we know what is being declared. */
16579 push_deferring_access_checks (dk_deferred);
16580
16581 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16582 alternative. */
16583 cp_parser_decl_specifier_seq (parser,
16584 CP_PARSER_FLAGS_OPTIONAL,
16585 &decl_specifiers,
16586 &declares_class_or_enum);
16587 if (friend_p)
16588 *friend_p = cp_parser_friend_p (&decl_specifiers);
16589
16590 /* There are no template typedefs. */
16591 if (decl_specifiers.specs[(int) ds_typedef])
16592 {
16593 error ("template declaration of %qs", "typedef");
16594 decl = error_mark_node;
16595 }
16596
16597 /* Gather up the access checks that occurred the
16598 decl-specifier-seq. */
16599 stop_deferring_access_checks ();
16600
16601 /* Check for the declaration of a template class. */
16602 if (declares_class_or_enum)
16603 {
16604 if (cp_parser_declares_only_class_p (parser))
16605 {
16606 decl = shadow_tag (&decl_specifiers);
16607
16608 /* In this case:
16609
16610 struct C {
16611 friend template <typename T> struct A<T>::B;
16612 };
16613
16614 A<T>::B will be represented by a TYPENAME_TYPE, and
16615 therefore not recognized by shadow_tag. */
16616 if (friend_p && *friend_p
16617 && !decl
16618 && decl_specifiers.type
16619 && TYPE_P (decl_specifiers.type))
16620 decl = decl_specifiers.type;
16621
16622 if (decl && decl != error_mark_node)
16623 decl = TYPE_NAME (decl);
16624 else
16625 decl = error_mark_node;
16626
16627 /* Perform access checks for template parameters. */
16628 cp_parser_perform_template_parameter_access_checks (checks);
16629 }
16630 }
16631 /* If it's not a template class, try for a template function. If
16632 the next token is a `;', then this declaration does not declare
16633 anything. But, if there were errors in the decl-specifiers, then
16634 the error might well have come from an attempted class-specifier.
16635 In that case, there's no need to warn about a missing declarator. */
16636 if (!decl
16637 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16638 || decl_specifiers.type != error_mark_node))
16639 decl = cp_parser_init_declarator (parser,
16640 &decl_specifiers,
16641 checks,
16642 /*function_definition_allowed_p=*/true,
16643 member_p,
16644 declares_class_or_enum,
16645 &function_definition_p);
16646
16647 pop_deferring_access_checks ();
16648
16649 /* Clear any current qualification; whatever comes next is the start
16650 of something new. */
16651 parser->scope = NULL_TREE;
16652 parser->qualifying_scope = NULL_TREE;
16653 parser->object_scope = NULL_TREE;
16654 /* Look for a trailing `;' after the declaration. */
16655 if (!function_definition_p
16656 && (decl == error_mark_node
16657 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16658 cp_parser_skip_to_end_of_block_or_statement (parser);
16659
16660 return decl;
16661 }
16662
16663 /* Parse a cast-expression that is not the operand of a unary "&". */
16664
16665 static tree
16666 cp_parser_simple_cast_expression (cp_parser *parser)
16667 {
16668 return cp_parser_cast_expression (parser, /*address_p=*/false,
16669 /*cast_p=*/false);
16670 }
16671
16672 /* Parse a functional cast to TYPE. Returns an expression
16673 representing the cast. */
16674
16675 static tree
16676 cp_parser_functional_cast (cp_parser* parser, tree type)
16677 {
16678 tree expression_list;
16679 tree cast;
16680
16681 expression_list
16682 = cp_parser_parenthesized_expression_list (parser, false,
16683 /*cast_p=*/true,
16684 /*allow_expansion_p=*/true,
16685 /*non_constant_p=*/NULL);
16686
16687 cast = build_functional_cast (type, expression_list);
16688 /* [expr.const]/1: In an integral constant expression "only type
16689 conversions to integral or enumeration type can be used". */
16690 if (TREE_CODE (type) == TYPE_DECL)
16691 type = TREE_TYPE (type);
16692 if (cast != error_mark_node
16693 && !cast_valid_in_integral_constant_expression_p (type)
16694 && (cp_parser_non_integral_constant_expression
16695 (parser, "a call to a constructor")))
16696 return error_mark_node;
16697 return cast;
16698 }
16699
16700 /* Save the tokens that make up the body of a member function defined
16701 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16702 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16703 specifiers applied to the declaration. Returns the FUNCTION_DECL
16704 for the member function. */
16705
16706 static tree
16707 cp_parser_save_member_function_body (cp_parser* parser,
16708 cp_decl_specifier_seq *decl_specifiers,
16709 cp_declarator *declarator,
16710 tree attributes)
16711 {
16712 cp_token *first;
16713 cp_token *last;
16714 tree fn;
16715
16716 /* Create the function-declaration. */
16717 fn = start_method (decl_specifiers, declarator, attributes);
16718 /* If something went badly wrong, bail out now. */
16719 if (fn == error_mark_node)
16720 {
16721 /* If there's a function-body, skip it. */
16722 if (cp_parser_token_starts_function_definition_p
16723 (cp_lexer_peek_token (parser->lexer)))
16724 cp_parser_skip_to_end_of_block_or_statement (parser);
16725 return error_mark_node;
16726 }
16727
16728 /* Remember it, if there default args to post process. */
16729 cp_parser_save_default_args (parser, fn);
16730
16731 /* Save away the tokens that make up the body of the
16732 function. */
16733 first = parser->lexer->next_token;
16734 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16735 /* Handle function try blocks. */
16736 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16737 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16738 last = parser->lexer->next_token;
16739
16740 /* Save away the inline definition; we will process it when the
16741 class is complete. */
16742 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16743 DECL_PENDING_INLINE_P (fn) = 1;
16744
16745 /* We need to know that this was defined in the class, so that
16746 friend templates are handled correctly. */
16747 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16748
16749 /* We're done with the inline definition. */
16750 finish_method (fn);
16751
16752 /* Add FN to the queue of functions to be parsed later. */
16753 TREE_VALUE (parser->unparsed_functions_queues)
16754 = tree_cons (NULL_TREE, fn,
16755 TREE_VALUE (parser->unparsed_functions_queues));
16756
16757 return fn;
16758 }
16759
16760 /* Parse a template-argument-list, as well as the trailing ">" (but
16761 not the opening ">"). See cp_parser_template_argument_list for the
16762 return value. */
16763
16764 static tree
16765 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16766 {
16767 tree arguments;
16768 tree saved_scope;
16769 tree saved_qualifying_scope;
16770 tree saved_object_scope;
16771 bool saved_greater_than_is_operator_p;
16772 bool saved_skip_evaluation;
16773
16774 /* [temp.names]
16775
16776 When parsing a template-id, the first non-nested `>' is taken as
16777 the end of the template-argument-list rather than a greater-than
16778 operator. */
16779 saved_greater_than_is_operator_p
16780 = parser->greater_than_is_operator_p;
16781 parser->greater_than_is_operator_p = false;
16782 /* Parsing the argument list may modify SCOPE, so we save it
16783 here. */
16784 saved_scope = parser->scope;
16785 saved_qualifying_scope = parser->qualifying_scope;
16786 saved_object_scope = parser->object_scope;
16787 /* We need to evaluate the template arguments, even though this
16788 template-id may be nested within a "sizeof". */
16789 saved_skip_evaluation = skip_evaluation;
16790 skip_evaluation = false;
16791 /* Parse the template-argument-list itself. */
16792 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16793 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16794 arguments = NULL_TREE;
16795 else
16796 arguments = cp_parser_template_argument_list (parser);
16797 /* Look for the `>' that ends the template-argument-list. If we find
16798 a '>>' instead, it's probably just a typo. */
16799 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16800 {
16801 if (flag_cpp0x)
16802 {
16803 /* In C++0x, a `>>' in a template argument list or cast
16804 expression is considered to be two separate `>'
16805 tokens. So, change the current token to a `>', but don't
16806 consume it: it will be consumed later when the outer
16807 template argument list (or cast expression) is parsed.
16808 Note that this replacement of `>' for `>>' is necessary
16809 even if we are parsing tentatively: in the tentative
16810 case, after calling
16811 cp_parser_enclosed_template_argument_list we will always
16812 throw away all of the template arguments and the first
16813 closing `>', either because the template argument list
16814 was erroneous or because we are replacing those tokens
16815 with a CPP_TEMPLATE_ID token. The second `>' (which will
16816 not have been thrown away) is needed either to close an
16817 outer template argument list or to complete a new-style
16818 cast. */
16819 cp_token *token = cp_lexer_peek_token (parser->lexer);
16820 token->type = CPP_GREATER;
16821 }
16822 else if (!saved_greater_than_is_operator_p)
16823 {
16824 /* If we're in a nested template argument list, the '>>' has
16825 to be a typo for '> >'. We emit the error message, but we
16826 continue parsing and we push a '>' as next token, so that
16827 the argument list will be parsed correctly. Note that the
16828 global source location is still on the token before the
16829 '>>', so we need to say explicitly where we want it. */
16830 cp_token *token = cp_lexer_peek_token (parser->lexer);
16831 error ("%H%<>>%> should be %<> >%> "
16832 "within a nested template argument list",
16833 &token->location);
16834
16835 token->type = CPP_GREATER;
16836 }
16837 else
16838 {
16839 /* If this is not a nested template argument list, the '>>'
16840 is a typo for '>'. Emit an error message and continue.
16841 Same deal about the token location, but here we can get it
16842 right by consuming the '>>' before issuing the diagnostic. */
16843 cp_lexer_consume_token (parser->lexer);
16844 error ("spurious %<>>%>, use %<>%> to terminate "
16845 "a template argument list");
16846 }
16847 }
16848 else
16849 cp_parser_skip_to_end_of_template_parameter_list (parser);
16850 /* The `>' token might be a greater-than operator again now. */
16851 parser->greater_than_is_operator_p
16852 = saved_greater_than_is_operator_p;
16853 /* Restore the SAVED_SCOPE. */
16854 parser->scope = saved_scope;
16855 parser->qualifying_scope = saved_qualifying_scope;
16856 parser->object_scope = saved_object_scope;
16857 skip_evaluation = saved_skip_evaluation;
16858
16859 return arguments;
16860 }
16861
16862 /* MEMBER_FUNCTION is a member function, or a friend. If default
16863 arguments, or the body of the function have not yet been parsed,
16864 parse them now. */
16865
16866 static void
16867 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16868 {
16869 /* If this member is a template, get the underlying
16870 FUNCTION_DECL. */
16871 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16872 member_function = DECL_TEMPLATE_RESULT (member_function);
16873
16874 /* There should not be any class definitions in progress at this
16875 point; the bodies of members are only parsed outside of all class
16876 definitions. */
16877 gcc_assert (parser->num_classes_being_defined == 0);
16878 /* While we're parsing the member functions we might encounter more
16879 classes. We want to handle them right away, but we don't want
16880 them getting mixed up with functions that are currently in the
16881 queue. */
16882 parser->unparsed_functions_queues
16883 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16884
16885 /* Make sure that any template parameters are in scope. */
16886 maybe_begin_member_template_processing (member_function);
16887
16888 /* If the body of the function has not yet been parsed, parse it
16889 now. */
16890 if (DECL_PENDING_INLINE_P (member_function))
16891 {
16892 tree function_scope;
16893 cp_token_cache *tokens;
16894
16895 /* The function is no longer pending; we are processing it. */
16896 tokens = DECL_PENDING_INLINE_INFO (member_function);
16897 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16898 DECL_PENDING_INLINE_P (member_function) = 0;
16899
16900 /* If this is a local class, enter the scope of the containing
16901 function. */
16902 function_scope = current_function_decl;
16903 if (function_scope)
16904 push_function_context_to (function_scope);
16905
16906
16907 /* Push the body of the function onto the lexer stack. */
16908 cp_parser_push_lexer_for_tokens (parser, tokens);
16909
16910 /* Let the front end know that we going to be defining this
16911 function. */
16912 start_preparsed_function (member_function, NULL_TREE,
16913 SF_PRE_PARSED | SF_INCLASS_INLINE);
16914
16915 /* Don't do access checking if it is a templated function. */
16916 if (processing_template_decl)
16917 push_deferring_access_checks (dk_no_check);
16918
16919 /* Now, parse the body of the function. */
16920 cp_parser_function_definition_after_declarator (parser,
16921 /*inline_p=*/true);
16922
16923 if (processing_template_decl)
16924 pop_deferring_access_checks ();
16925
16926 /* Leave the scope of the containing function. */
16927 if (function_scope)
16928 pop_function_context_from (function_scope);
16929 cp_parser_pop_lexer (parser);
16930 }
16931
16932 /* Remove any template parameters from the symbol table. */
16933 maybe_end_member_template_processing ();
16934
16935 /* Restore the queue. */
16936 parser->unparsed_functions_queues
16937 = TREE_CHAIN (parser->unparsed_functions_queues);
16938 }
16939
16940 /* If DECL contains any default args, remember it on the unparsed
16941 functions queue. */
16942
16943 static void
16944 cp_parser_save_default_args (cp_parser* parser, tree decl)
16945 {
16946 tree probe;
16947
16948 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16949 probe;
16950 probe = TREE_CHAIN (probe))
16951 if (TREE_PURPOSE (probe))
16952 {
16953 TREE_PURPOSE (parser->unparsed_functions_queues)
16954 = tree_cons (current_class_type, decl,
16955 TREE_PURPOSE (parser->unparsed_functions_queues));
16956 break;
16957 }
16958 }
16959
16960 /* FN is a FUNCTION_DECL which may contains a parameter with an
16961 unparsed DEFAULT_ARG. Parse the default args now. This function
16962 assumes that the current scope is the scope in which the default
16963 argument should be processed. */
16964
16965 static void
16966 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16967 {
16968 bool saved_local_variables_forbidden_p;
16969 tree parm;
16970
16971 /* While we're parsing the default args, we might (due to the
16972 statement expression extension) encounter more classes. We want
16973 to handle them right away, but we don't want them getting mixed
16974 up with default args that are currently in the queue. */
16975 parser->unparsed_functions_queues
16976 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16977
16978 /* Local variable names (and the `this' keyword) may not appear
16979 in a default argument. */
16980 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16981 parser->local_variables_forbidden_p = true;
16982
16983 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16984 parm;
16985 parm = TREE_CHAIN (parm))
16986 {
16987 cp_token_cache *tokens;
16988 tree default_arg = TREE_PURPOSE (parm);
16989 tree parsed_arg;
16990 VEC(tree,gc) *insts;
16991 tree copy;
16992 unsigned ix;
16993
16994 if (!default_arg)
16995 continue;
16996
16997 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16998 /* This can happen for a friend declaration for a function
16999 already declared with default arguments. */
17000 continue;
17001
17002 /* Push the saved tokens for the default argument onto the parser's
17003 lexer stack. */
17004 tokens = DEFARG_TOKENS (default_arg);
17005 cp_parser_push_lexer_for_tokens (parser, tokens);
17006
17007 /* Parse the assignment-expression. */
17008 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17009
17010 if (!processing_template_decl)
17011 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17012
17013 TREE_PURPOSE (parm) = parsed_arg;
17014
17015 /* Update any instantiations we've already created. */
17016 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17017 VEC_iterate (tree, insts, ix, copy); ix++)
17018 TREE_PURPOSE (copy) = parsed_arg;
17019
17020 /* If the token stream has not been completely used up, then
17021 there was extra junk after the end of the default
17022 argument. */
17023 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17024 cp_parser_error (parser, "expected %<,%>");
17025
17026 /* Revert to the main lexer. */
17027 cp_parser_pop_lexer (parser);
17028 }
17029
17030 /* Make sure no default arg is missing. */
17031 check_default_args (fn);
17032
17033 /* Restore the state of local_variables_forbidden_p. */
17034 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17035
17036 /* Restore the queue. */
17037 parser->unparsed_functions_queues
17038 = TREE_CHAIN (parser->unparsed_functions_queues);
17039 }
17040
17041 /* Parse the operand of `sizeof' (or a similar operator). Returns
17042 either a TYPE or an expression, depending on the form of the
17043 input. The KEYWORD indicates which kind of expression we have
17044 encountered. */
17045
17046 static tree
17047 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17048 {
17049 static const char *format;
17050 tree expr = NULL_TREE;
17051 const char *saved_message;
17052 bool saved_integral_constant_expression_p;
17053 bool saved_non_integral_constant_expression_p;
17054 bool pack_expansion_p = false;
17055
17056 /* Initialize FORMAT the first time we get here. */
17057 if (!format)
17058 format = "types may not be defined in '%s' expressions";
17059
17060 /* Types cannot be defined in a `sizeof' expression. Save away the
17061 old message. */
17062 saved_message = parser->type_definition_forbidden_message;
17063 /* And create the new one. */
17064 parser->type_definition_forbidden_message
17065 = XNEWVEC (const char, strlen (format)
17066 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17067 + 1 /* `\0' */);
17068 sprintf ((char *) parser->type_definition_forbidden_message,
17069 format, IDENTIFIER_POINTER (ridpointers[keyword]));
17070
17071 /* The restrictions on constant-expressions do not apply inside
17072 sizeof expressions. */
17073 saved_integral_constant_expression_p
17074 = parser->integral_constant_expression_p;
17075 saved_non_integral_constant_expression_p
17076 = parser->non_integral_constant_expression_p;
17077 parser->integral_constant_expression_p = false;
17078
17079 /* If it's a `...', then we are computing the length of a parameter
17080 pack. */
17081 if (keyword == RID_SIZEOF
17082 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17083 {
17084 /* Consume the `...'. */
17085 cp_lexer_consume_token (parser->lexer);
17086 maybe_warn_variadic_templates ();
17087
17088 /* Note that this is an expansion. */
17089 pack_expansion_p = true;
17090 }
17091
17092 /* Do not actually evaluate the expression. */
17093 ++skip_evaluation;
17094 /* If it's a `(', then we might be looking at the type-id
17095 construction. */
17096 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17097 {
17098 tree type;
17099 bool saved_in_type_id_in_expr_p;
17100
17101 /* We can't be sure yet whether we're looking at a type-id or an
17102 expression. */
17103 cp_parser_parse_tentatively (parser);
17104 /* Consume the `('. */
17105 cp_lexer_consume_token (parser->lexer);
17106 /* Parse the type-id. */
17107 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17108 parser->in_type_id_in_expr_p = true;
17109 type = cp_parser_type_id (parser);
17110 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17111 /* Now, look for the trailing `)'. */
17112 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17113 /* If all went well, then we're done. */
17114 if (cp_parser_parse_definitely (parser))
17115 {
17116 cp_decl_specifier_seq decl_specs;
17117
17118 /* Build a trivial decl-specifier-seq. */
17119 clear_decl_specs (&decl_specs);
17120 decl_specs.type = type;
17121
17122 /* Call grokdeclarator to figure out what type this is. */
17123 expr = grokdeclarator (NULL,
17124 &decl_specs,
17125 TYPENAME,
17126 /*initialized=*/0,
17127 /*attrlist=*/NULL);
17128 }
17129 }
17130
17131 /* If the type-id production did not work out, then we must be
17132 looking at the unary-expression production. */
17133 if (!expr)
17134 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17135 /*cast_p=*/false);
17136
17137 if (pack_expansion_p)
17138 /* Build a pack expansion. */
17139 expr = make_pack_expansion (expr);
17140
17141 /* Go back to evaluating expressions. */
17142 --skip_evaluation;
17143
17144 /* Free the message we created. */
17145 free ((char *) parser->type_definition_forbidden_message);
17146 /* And restore the old one. */
17147 parser->type_definition_forbidden_message = saved_message;
17148 parser->integral_constant_expression_p
17149 = saved_integral_constant_expression_p;
17150 parser->non_integral_constant_expression_p
17151 = saved_non_integral_constant_expression_p;
17152
17153 return expr;
17154 }
17155
17156 /* If the current declaration has no declarator, return true. */
17157
17158 static bool
17159 cp_parser_declares_only_class_p (cp_parser *parser)
17160 {
17161 /* If the next token is a `;' or a `,' then there is no
17162 declarator. */
17163 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17164 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17165 }
17166
17167 /* Update the DECL_SPECS to reflect the storage class indicated by
17168 KEYWORD. */
17169
17170 static void
17171 cp_parser_set_storage_class (cp_parser *parser,
17172 cp_decl_specifier_seq *decl_specs,
17173 enum rid keyword)
17174 {
17175 cp_storage_class storage_class;
17176
17177 if (parser->in_unbraced_linkage_specification_p)
17178 {
17179 error ("invalid use of %qD in linkage specification",
17180 ridpointers[keyword]);
17181 return;
17182 }
17183 else if (decl_specs->storage_class != sc_none)
17184 {
17185 decl_specs->conflicting_specifiers_p = true;
17186 return;
17187 }
17188
17189 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17190 && decl_specs->specs[(int) ds_thread])
17191 {
17192 error ("%<__thread%> before %qD", ridpointers[keyword]);
17193 decl_specs->specs[(int) ds_thread] = 0;
17194 }
17195
17196 switch (keyword)
17197 {
17198 case RID_AUTO:
17199 storage_class = sc_auto;
17200 break;
17201 case RID_REGISTER:
17202 storage_class = sc_register;
17203 break;
17204 case RID_STATIC:
17205 storage_class = sc_static;
17206 break;
17207 case RID_EXTERN:
17208 storage_class = sc_extern;
17209 break;
17210 case RID_MUTABLE:
17211 storage_class = sc_mutable;
17212 break;
17213 default:
17214 gcc_unreachable ();
17215 }
17216 decl_specs->storage_class = storage_class;
17217
17218 /* A storage class specifier cannot be applied alongside a typedef
17219 specifier. If there is a typedef specifier present then set
17220 conflicting_specifiers_p which will trigger an error later
17221 on in grokdeclarator. */
17222 if (decl_specs->specs[(int)ds_typedef])
17223 decl_specs->conflicting_specifiers_p = true;
17224 }
17225
17226 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17227 is true, the type is a user-defined type; otherwise it is a
17228 built-in type specified by a keyword. */
17229
17230 static void
17231 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17232 tree type_spec,
17233 bool user_defined_p)
17234 {
17235 decl_specs->any_specifiers_p = true;
17236
17237 /* If the user tries to redeclare bool or wchar_t (with, for
17238 example, in "typedef int wchar_t;") we remember that this is what
17239 happened. In system headers, we ignore these declarations so
17240 that G++ can work with system headers that are not C++-safe. */
17241 if (decl_specs->specs[(int) ds_typedef]
17242 && !user_defined_p
17243 && (type_spec == boolean_type_node
17244 || type_spec == wchar_type_node)
17245 && (decl_specs->type
17246 || decl_specs->specs[(int) ds_long]
17247 || decl_specs->specs[(int) ds_short]
17248 || decl_specs->specs[(int) ds_unsigned]
17249 || decl_specs->specs[(int) ds_signed]))
17250 {
17251 decl_specs->redefined_builtin_type = type_spec;
17252 if (!decl_specs->type)
17253 {
17254 decl_specs->type = type_spec;
17255 decl_specs->user_defined_type_p = false;
17256 }
17257 }
17258 else if (decl_specs->type)
17259 decl_specs->multiple_types_p = true;
17260 else
17261 {
17262 decl_specs->type = type_spec;
17263 decl_specs->user_defined_type_p = user_defined_p;
17264 decl_specs->redefined_builtin_type = NULL_TREE;
17265 }
17266 }
17267
17268 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17269 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17270
17271 static bool
17272 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17273 {
17274 return decl_specifiers->specs[(int) ds_friend] != 0;
17275 }
17276
17277 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17278 issue an error message indicating that TOKEN_DESC was expected.
17279
17280 Returns the token consumed, if the token had the appropriate type.
17281 Otherwise, returns NULL. */
17282
17283 static cp_token *
17284 cp_parser_require (cp_parser* parser,
17285 enum cpp_ttype type,
17286 const char* token_desc)
17287 {
17288 if (cp_lexer_next_token_is (parser->lexer, type))
17289 return cp_lexer_consume_token (parser->lexer);
17290 else
17291 {
17292 /* Output the MESSAGE -- unless we're parsing tentatively. */
17293 if (!cp_parser_simulate_error (parser))
17294 {
17295 char *message = concat ("expected ", token_desc, NULL);
17296 cp_parser_error (parser, message);
17297 free (message);
17298 }
17299 return NULL;
17300 }
17301 }
17302
17303 /* An error message is produced if the next token is not '>'.
17304 All further tokens are skipped until the desired token is
17305 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17306
17307 static void
17308 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17309 {
17310 /* Current level of '< ... >'. */
17311 unsigned level = 0;
17312 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17313 unsigned nesting_depth = 0;
17314
17315 /* Are we ready, yet? If not, issue error message. */
17316 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17317 return;
17318
17319 /* Skip tokens until the desired token is found. */
17320 while (true)
17321 {
17322 /* Peek at the next token. */
17323 switch (cp_lexer_peek_token (parser->lexer)->type)
17324 {
17325 case CPP_LESS:
17326 if (!nesting_depth)
17327 ++level;
17328 break;
17329
17330 case CPP_RSHIFT:
17331 if (!flag_cpp0x)
17332 /* C++0x views the `>>' operator as two `>' tokens, but
17333 C++98 does not. */
17334 break;
17335 else if (!nesting_depth && level-- == 0)
17336 {
17337 /* We've hit a `>>' where the first `>' closes the
17338 template argument list, and the second `>' is
17339 spurious. Just consume the `>>' and stop; we've
17340 already produced at least one error. */
17341 cp_lexer_consume_token (parser->lexer);
17342 return;
17343 }
17344 /* Fall through for C++0x, so we handle the second `>' in
17345 the `>>'. */
17346
17347 case CPP_GREATER:
17348 if (!nesting_depth && level-- == 0)
17349 {
17350 /* We've reached the token we want, consume it and stop. */
17351 cp_lexer_consume_token (parser->lexer);
17352 return;
17353 }
17354 break;
17355
17356 case CPP_OPEN_PAREN:
17357 case CPP_OPEN_SQUARE:
17358 ++nesting_depth;
17359 break;
17360
17361 case CPP_CLOSE_PAREN:
17362 case CPP_CLOSE_SQUARE:
17363 if (nesting_depth-- == 0)
17364 return;
17365 break;
17366
17367 case CPP_EOF:
17368 case CPP_PRAGMA_EOL:
17369 case CPP_SEMICOLON:
17370 case CPP_OPEN_BRACE:
17371 case CPP_CLOSE_BRACE:
17372 /* The '>' was probably forgotten, don't look further. */
17373 return;
17374
17375 default:
17376 break;
17377 }
17378
17379 /* Consume this token. */
17380 cp_lexer_consume_token (parser->lexer);
17381 }
17382 }
17383
17384 /* If the next token is the indicated keyword, consume it. Otherwise,
17385 issue an error message indicating that TOKEN_DESC was expected.
17386
17387 Returns the token consumed, if the token had the appropriate type.
17388 Otherwise, returns NULL. */
17389
17390 static cp_token *
17391 cp_parser_require_keyword (cp_parser* parser,
17392 enum rid keyword,
17393 const char* token_desc)
17394 {
17395 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17396
17397 if (token && token->keyword != keyword)
17398 {
17399 dyn_string_t error_msg;
17400
17401 /* Format the error message. */
17402 error_msg = dyn_string_new (0);
17403 dyn_string_append_cstr (error_msg, "expected ");
17404 dyn_string_append_cstr (error_msg, token_desc);
17405 cp_parser_error (parser, error_msg->s);
17406 dyn_string_delete (error_msg);
17407 return NULL;
17408 }
17409
17410 return token;
17411 }
17412
17413 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17414 function-definition. */
17415
17416 static bool
17417 cp_parser_token_starts_function_definition_p (cp_token* token)
17418 {
17419 return (/* An ordinary function-body begins with an `{'. */
17420 token->type == CPP_OPEN_BRACE
17421 /* A ctor-initializer begins with a `:'. */
17422 || token->type == CPP_COLON
17423 /* A function-try-block begins with `try'. */
17424 || token->keyword == RID_TRY
17425 /* The named return value extension begins with `return'. */
17426 || token->keyword == RID_RETURN);
17427 }
17428
17429 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17430 definition. */
17431
17432 static bool
17433 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17434 {
17435 cp_token *token;
17436
17437 token = cp_lexer_peek_token (parser->lexer);
17438 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17439 }
17440
17441 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17442 C++0x) ending a template-argument. */
17443
17444 static bool
17445 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17446 {
17447 cp_token *token;
17448
17449 token = cp_lexer_peek_token (parser->lexer);
17450 return (token->type == CPP_COMMA
17451 || token->type == CPP_GREATER
17452 || token->type == CPP_ELLIPSIS
17453 || (flag_cpp0x && token->type == CPP_RSHIFT));
17454 }
17455
17456 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17457 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17458
17459 static bool
17460 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17461 size_t n)
17462 {
17463 cp_token *token;
17464
17465 token = cp_lexer_peek_nth_token (parser->lexer, n);
17466 if (token->type == CPP_LESS)
17467 return true;
17468 /* Check for the sequence `<::' in the original code. It would be lexed as
17469 `[:', where `[' is a digraph, and there is no whitespace before
17470 `:'. */
17471 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17472 {
17473 cp_token *token2;
17474 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17475 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17476 return true;
17477 }
17478 return false;
17479 }
17480
17481 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17482 or none_type otherwise. */
17483
17484 static enum tag_types
17485 cp_parser_token_is_class_key (cp_token* token)
17486 {
17487 switch (token->keyword)
17488 {
17489 case RID_CLASS:
17490 return class_type;
17491 case RID_STRUCT:
17492 return record_type;
17493 case RID_UNION:
17494 return union_type;
17495
17496 default:
17497 return none_type;
17498 }
17499 }
17500
17501 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17502
17503 static void
17504 cp_parser_check_class_key (enum tag_types class_key, tree type)
17505 {
17506 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17507 pedwarn ("%qs tag used in naming %q#T",
17508 class_key == union_type ? "union"
17509 : class_key == record_type ? "struct" : "class",
17510 type);
17511 }
17512
17513 /* Issue an error message if DECL is redeclared with different
17514 access than its original declaration [class.access.spec/3].
17515 This applies to nested classes and nested class templates.
17516 [class.mem/1]. */
17517
17518 static void
17519 cp_parser_check_access_in_redeclaration (tree decl)
17520 {
17521 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17522 return;
17523
17524 if ((TREE_PRIVATE (decl)
17525 != (current_access_specifier == access_private_node))
17526 || (TREE_PROTECTED (decl)
17527 != (current_access_specifier == access_protected_node)))
17528 error ("%qD redeclared with different access", decl);
17529 }
17530
17531 /* Look for the `template' keyword, as a syntactic disambiguator.
17532 Return TRUE iff it is present, in which case it will be
17533 consumed. */
17534
17535 static bool
17536 cp_parser_optional_template_keyword (cp_parser *parser)
17537 {
17538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17539 {
17540 /* The `template' keyword can only be used within templates;
17541 outside templates the parser can always figure out what is a
17542 template and what is not. */
17543 if (!processing_template_decl)
17544 {
17545 error ("%<template%> (as a disambiguator) is only allowed "
17546 "within templates");
17547 /* If this part of the token stream is rescanned, the same
17548 error message would be generated. So, we purge the token
17549 from the stream. */
17550 cp_lexer_purge_token (parser->lexer);
17551 return false;
17552 }
17553 else
17554 {
17555 /* Consume the `template' keyword. */
17556 cp_lexer_consume_token (parser->lexer);
17557 return true;
17558 }
17559 }
17560
17561 return false;
17562 }
17563
17564 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17565 set PARSER->SCOPE, and perform other related actions. */
17566
17567 static void
17568 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17569 {
17570 int i;
17571 struct tree_check *check_value;
17572 deferred_access_check *chk;
17573 VEC (deferred_access_check,gc) *checks;
17574
17575 /* Get the stored value. */
17576 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17577 /* Perform any access checks that were deferred. */
17578 checks = check_value->checks;
17579 if (checks)
17580 {
17581 for (i = 0 ;
17582 VEC_iterate (deferred_access_check, checks, i, chk) ;
17583 ++i)
17584 {
17585 perform_or_defer_access_check (chk->binfo,
17586 chk->decl,
17587 chk->diag_decl);
17588 }
17589 }
17590 /* Set the scope from the stored value. */
17591 parser->scope = check_value->value;
17592 parser->qualifying_scope = check_value->qualifying_scope;
17593 parser->object_scope = NULL_TREE;
17594 }
17595
17596 /* Consume tokens up through a non-nested END token. */
17597
17598 static void
17599 cp_parser_cache_group (cp_parser *parser,
17600 enum cpp_ttype end,
17601 unsigned depth)
17602 {
17603 while (true)
17604 {
17605 cp_token *token;
17606
17607 /* Abort a parenthesized expression if we encounter a brace. */
17608 if ((end == CPP_CLOSE_PAREN || depth == 0)
17609 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17610 return;
17611 /* If we've reached the end of the file, stop. */
17612 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17613 || (end != CPP_PRAGMA_EOL
17614 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17615 return;
17616 /* Consume the next token. */
17617 token = cp_lexer_consume_token (parser->lexer);
17618 /* See if it starts a new group. */
17619 if (token->type == CPP_OPEN_BRACE)
17620 {
17621 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17622 if (depth == 0)
17623 return;
17624 }
17625 else if (token->type == CPP_OPEN_PAREN)
17626 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17627 else if (token->type == CPP_PRAGMA)
17628 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17629 else if (token->type == end)
17630 return;
17631 }
17632 }
17633
17634 /* Begin parsing tentatively. We always save tokens while parsing
17635 tentatively so that if the tentative parsing fails we can restore the
17636 tokens. */
17637
17638 static void
17639 cp_parser_parse_tentatively (cp_parser* parser)
17640 {
17641 /* Enter a new parsing context. */
17642 parser->context = cp_parser_context_new (parser->context);
17643 /* Begin saving tokens. */
17644 cp_lexer_save_tokens (parser->lexer);
17645 /* In order to avoid repetitive access control error messages,
17646 access checks are queued up until we are no longer parsing
17647 tentatively. */
17648 push_deferring_access_checks (dk_deferred);
17649 }
17650
17651 /* Commit to the currently active tentative parse. */
17652
17653 static void
17654 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17655 {
17656 cp_parser_context *context;
17657 cp_lexer *lexer;
17658
17659 /* Mark all of the levels as committed. */
17660 lexer = parser->lexer;
17661 for (context = parser->context; context->next; context = context->next)
17662 {
17663 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17664 break;
17665 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17666 while (!cp_lexer_saving_tokens (lexer))
17667 lexer = lexer->next;
17668 cp_lexer_commit_tokens (lexer);
17669 }
17670 }
17671
17672 /* Abort the currently active tentative parse. All consumed tokens
17673 will be rolled back, and no diagnostics will be issued. */
17674
17675 static void
17676 cp_parser_abort_tentative_parse (cp_parser* parser)
17677 {
17678 cp_parser_simulate_error (parser);
17679 /* Now, pretend that we want to see if the construct was
17680 successfully parsed. */
17681 cp_parser_parse_definitely (parser);
17682 }
17683
17684 /* Stop parsing tentatively. If a parse error has occurred, restore the
17685 token stream. Otherwise, commit to the tokens we have consumed.
17686 Returns true if no error occurred; false otherwise. */
17687
17688 static bool
17689 cp_parser_parse_definitely (cp_parser* parser)
17690 {
17691 bool error_occurred;
17692 cp_parser_context *context;
17693
17694 /* Remember whether or not an error occurred, since we are about to
17695 destroy that information. */
17696 error_occurred = cp_parser_error_occurred (parser);
17697 /* Remove the topmost context from the stack. */
17698 context = parser->context;
17699 parser->context = context->next;
17700 /* If no parse errors occurred, commit to the tentative parse. */
17701 if (!error_occurred)
17702 {
17703 /* Commit to the tokens read tentatively, unless that was
17704 already done. */
17705 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17706 cp_lexer_commit_tokens (parser->lexer);
17707
17708 pop_to_parent_deferring_access_checks ();
17709 }
17710 /* Otherwise, if errors occurred, roll back our state so that things
17711 are just as they were before we began the tentative parse. */
17712 else
17713 {
17714 cp_lexer_rollback_tokens (parser->lexer);
17715 pop_deferring_access_checks ();
17716 }
17717 /* Add the context to the front of the free list. */
17718 context->next = cp_parser_context_free_list;
17719 cp_parser_context_free_list = context;
17720
17721 return !error_occurred;
17722 }
17723
17724 /* Returns true if we are parsing tentatively and are not committed to
17725 this tentative parse. */
17726
17727 static bool
17728 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17729 {
17730 return (cp_parser_parsing_tentatively (parser)
17731 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17732 }
17733
17734 /* Returns nonzero iff an error has occurred during the most recent
17735 tentative parse. */
17736
17737 static bool
17738 cp_parser_error_occurred (cp_parser* parser)
17739 {
17740 return (cp_parser_parsing_tentatively (parser)
17741 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17742 }
17743
17744 /* Returns nonzero if GNU extensions are allowed. */
17745
17746 static bool
17747 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17748 {
17749 return parser->allow_gnu_extensions_p;
17750 }
17751 \f
17752 /* Objective-C++ Productions */
17753
17754
17755 /* Parse an Objective-C expression, which feeds into a primary-expression
17756 above.
17757
17758 objc-expression:
17759 objc-message-expression
17760 objc-string-literal
17761 objc-encode-expression
17762 objc-protocol-expression
17763 objc-selector-expression
17764
17765 Returns a tree representation of the expression. */
17766
17767 static tree
17768 cp_parser_objc_expression (cp_parser* parser)
17769 {
17770 /* Try to figure out what kind of declaration is present. */
17771 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17772
17773 switch (kwd->type)
17774 {
17775 case CPP_OPEN_SQUARE:
17776 return cp_parser_objc_message_expression (parser);
17777
17778 case CPP_OBJC_STRING:
17779 kwd = cp_lexer_consume_token (parser->lexer);
17780 return objc_build_string_object (kwd->u.value);
17781
17782 case CPP_KEYWORD:
17783 switch (kwd->keyword)
17784 {
17785 case RID_AT_ENCODE:
17786 return cp_parser_objc_encode_expression (parser);
17787
17788 case RID_AT_PROTOCOL:
17789 return cp_parser_objc_protocol_expression (parser);
17790
17791 case RID_AT_SELECTOR:
17792 return cp_parser_objc_selector_expression (parser);
17793
17794 default:
17795 break;
17796 }
17797 default:
17798 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17799 cp_parser_skip_to_end_of_block_or_statement (parser);
17800 }
17801
17802 return error_mark_node;
17803 }
17804
17805 /* Parse an Objective-C message expression.
17806
17807 objc-message-expression:
17808 [ objc-message-receiver objc-message-args ]
17809
17810 Returns a representation of an Objective-C message. */
17811
17812 static tree
17813 cp_parser_objc_message_expression (cp_parser* parser)
17814 {
17815 tree receiver, messageargs;
17816
17817 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17818 receiver = cp_parser_objc_message_receiver (parser);
17819 messageargs = cp_parser_objc_message_args (parser);
17820 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17821
17822 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17823 }
17824
17825 /* Parse an objc-message-receiver.
17826
17827 objc-message-receiver:
17828 expression
17829 simple-type-specifier
17830
17831 Returns a representation of the type or expression. */
17832
17833 static tree
17834 cp_parser_objc_message_receiver (cp_parser* parser)
17835 {
17836 tree rcv;
17837
17838 /* An Objective-C message receiver may be either (1) a type
17839 or (2) an expression. */
17840 cp_parser_parse_tentatively (parser);
17841 rcv = cp_parser_expression (parser, false);
17842
17843 if (cp_parser_parse_definitely (parser))
17844 return rcv;
17845
17846 rcv = cp_parser_simple_type_specifier (parser,
17847 /*decl_specs=*/NULL,
17848 CP_PARSER_FLAGS_NONE);
17849
17850 return objc_get_class_reference (rcv);
17851 }
17852
17853 /* Parse the arguments and selectors comprising an Objective-C message.
17854
17855 objc-message-args:
17856 objc-selector
17857 objc-selector-args
17858 objc-selector-args , objc-comma-args
17859
17860 objc-selector-args:
17861 objc-selector [opt] : assignment-expression
17862 objc-selector-args objc-selector [opt] : assignment-expression
17863
17864 objc-comma-args:
17865 assignment-expression
17866 objc-comma-args , assignment-expression
17867
17868 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17869 selector arguments and TREE_VALUE containing a list of comma
17870 arguments. */
17871
17872 static tree
17873 cp_parser_objc_message_args (cp_parser* parser)
17874 {
17875 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17876 bool maybe_unary_selector_p = true;
17877 cp_token *token = cp_lexer_peek_token (parser->lexer);
17878
17879 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17880 {
17881 tree selector = NULL_TREE, arg;
17882
17883 if (token->type != CPP_COLON)
17884 selector = cp_parser_objc_selector (parser);
17885
17886 /* Detect if we have a unary selector. */
17887 if (maybe_unary_selector_p
17888 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17889 return build_tree_list (selector, NULL_TREE);
17890
17891 maybe_unary_selector_p = false;
17892 cp_parser_require (parser, CPP_COLON, "`:'");
17893 arg = cp_parser_assignment_expression (parser, false);
17894
17895 sel_args
17896 = chainon (sel_args,
17897 build_tree_list (selector, arg));
17898
17899 token = cp_lexer_peek_token (parser->lexer);
17900 }
17901
17902 /* Handle non-selector arguments, if any. */
17903 while (token->type == CPP_COMMA)
17904 {
17905 tree arg;
17906
17907 cp_lexer_consume_token (parser->lexer);
17908 arg = cp_parser_assignment_expression (parser, false);
17909
17910 addl_args
17911 = chainon (addl_args,
17912 build_tree_list (NULL_TREE, arg));
17913
17914 token = cp_lexer_peek_token (parser->lexer);
17915 }
17916
17917 return build_tree_list (sel_args, addl_args);
17918 }
17919
17920 /* Parse an Objective-C encode expression.
17921
17922 objc-encode-expression:
17923 @encode objc-typename
17924
17925 Returns an encoded representation of the type argument. */
17926
17927 static tree
17928 cp_parser_objc_encode_expression (cp_parser* parser)
17929 {
17930 tree type;
17931
17932 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17933 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17934 type = complete_type (cp_parser_type_id (parser));
17935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17936
17937 if (!type)
17938 {
17939 error ("%<@encode%> must specify a type as an argument");
17940 return error_mark_node;
17941 }
17942
17943 return objc_build_encode_expr (type);
17944 }
17945
17946 /* Parse an Objective-C @defs expression. */
17947
17948 static tree
17949 cp_parser_objc_defs_expression (cp_parser *parser)
17950 {
17951 tree name;
17952
17953 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17954 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17955 name = cp_parser_identifier (parser);
17956 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17957
17958 return objc_get_class_ivars (name);
17959 }
17960
17961 /* Parse an Objective-C protocol expression.
17962
17963 objc-protocol-expression:
17964 @protocol ( identifier )
17965
17966 Returns a representation of the protocol expression. */
17967
17968 static tree
17969 cp_parser_objc_protocol_expression (cp_parser* parser)
17970 {
17971 tree proto;
17972
17973 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17974 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17975 proto = cp_parser_identifier (parser);
17976 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17977
17978 return objc_build_protocol_expr (proto);
17979 }
17980
17981 /* Parse an Objective-C selector expression.
17982
17983 objc-selector-expression:
17984 @selector ( objc-method-signature )
17985
17986 objc-method-signature:
17987 objc-selector
17988 objc-selector-seq
17989
17990 objc-selector-seq:
17991 objc-selector :
17992 objc-selector-seq objc-selector :
17993
17994 Returns a representation of the method selector. */
17995
17996 static tree
17997 cp_parser_objc_selector_expression (cp_parser* parser)
17998 {
17999 tree sel_seq = NULL_TREE;
18000 bool maybe_unary_selector_p = true;
18001 cp_token *token;
18002
18003 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18004 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18005 token = cp_lexer_peek_token (parser->lexer);
18006
18007 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18008 || token->type == CPP_SCOPE)
18009 {
18010 tree selector = NULL_TREE;
18011
18012 if (token->type != CPP_COLON
18013 || token->type == CPP_SCOPE)
18014 selector = cp_parser_objc_selector (parser);
18015
18016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18017 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18018 {
18019 /* Detect if we have a unary selector. */
18020 if (maybe_unary_selector_p)
18021 {
18022 sel_seq = selector;
18023 goto finish_selector;
18024 }
18025 else
18026 {
18027 cp_parser_error (parser, "expected %<:%>");
18028 }
18029 }
18030 maybe_unary_selector_p = false;
18031 token = cp_lexer_consume_token (parser->lexer);
18032
18033 if (token->type == CPP_SCOPE)
18034 {
18035 sel_seq
18036 = chainon (sel_seq,
18037 build_tree_list (selector, NULL_TREE));
18038 sel_seq
18039 = chainon (sel_seq,
18040 build_tree_list (NULL_TREE, NULL_TREE));
18041 }
18042 else
18043 sel_seq
18044 = chainon (sel_seq,
18045 build_tree_list (selector, NULL_TREE));
18046
18047 token = cp_lexer_peek_token (parser->lexer);
18048 }
18049
18050 finish_selector:
18051 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18052
18053 return objc_build_selector_expr (sel_seq);
18054 }
18055
18056 /* Parse a list of identifiers.
18057
18058 objc-identifier-list:
18059 identifier
18060 objc-identifier-list , identifier
18061
18062 Returns a TREE_LIST of identifier nodes. */
18063
18064 static tree
18065 cp_parser_objc_identifier_list (cp_parser* parser)
18066 {
18067 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18068 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18069
18070 while (sep->type == CPP_COMMA)
18071 {
18072 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18073 list = chainon (list,
18074 build_tree_list (NULL_TREE,
18075 cp_parser_identifier (parser)));
18076 sep = cp_lexer_peek_token (parser->lexer);
18077 }
18078
18079 return list;
18080 }
18081
18082 /* Parse an Objective-C alias declaration.
18083
18084 objc-alias-declaration:
18085 @compatibility_alias identifier identifier ;
18086
18087 This function registers the alias mapping with the Objective-C front end.
18088 It returns nothing. */
18089
18090 static void
18091 cp_parser_objc_alias_declaration (cp_parser* parser)
18092 {
18093 tree alias, orig;
18094
18095 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18096 alias = cp_parser_identifier (parser);
18097 orig = cp_parser_identifier (parser);
18098 objc_declare_alias (alias, orig);
18099 cp_parser_consume_semicolon_at_end_of_statement (parser);
18100 }
18101
18102 /* Parse an Objective-C class forward-declaration.
18103
18104 objc-class-declaration:
18105 @class objc-identifier-list ;
18106
18107 The function registers the forward declarations with the Objective-C
18108 front end. It returns nothing. */
18109
18110 static void
18111 cp_parser_objc_class_declaration (cp_parser* parser)
18112 {
18113 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18114 objc_declare_class (cp_parser_objc_identifier_list (parser));
18115 cp_parser_consume_semicolon_at_end_of_statement (parser);
18116 }
18117
18118 /* Parse a list of Objective-C protocol references.
18119
18120 objc-protocol-refs-opt:
18121 objc-protocol-refs [opt]
18122
18123 objc-protocol-refs:
18124 < objc-identifier-list >
18125
18126 Returns a TREE_LIST of identifiers, if any. */
18127
18128 static tree
18129 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18130 {
18131 tree protorefs = NULL_TREE;
18132
18133 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18134 {
18135 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18136 protorefs = cp_parser_objc_identifier_list (parser);
18137 cp_parser_require (parser, CPP_GREATER, "`>'");
18138 }
18139
18140 return protorefs;
18141 }
18142
18143 /* Parse a Objective-C visibility specification. */
18144
18145 static void
18146 cp_parser_objc_visibility_spec (cp_parser* parser)
18147 {
18148 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18149
18150 switch (vis->keyword)
18151 {
18152 case RID_AT_PRIVATE:
18153 objc_set_visibility (2);
18154 break;
18155 case RID_AT_PROTECTED:
18156 objc_set_visibility (0);
18157 break;
18158 case RID_AT_PUBLIC:
18159 objc_set_visibility (1);
18160 break;
18161 default:
18162 return;
18163 }
18164
18165 /* Eat '@private'/'@protected'/'@public'. */
18166 cp_lexer_consume_token (parser->lexer);
18167 }
18168
18169 /* Parse an Objective-C method type. */
18170
18171 static void
18172 cp_parser_objc_method_type (cp_parser* parser)
18173 {
18174 objc_set_method_type
18175 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18176 ? PLUS_EXPR
18177 : MINUS_EXPR);
18178 }
18179
18180 /* Parse an Objective-C protocol qualifier. */
18181
18182 static tree
18183 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18184 {
18185 tree quals = NULL_TREE, node;
18186 cp_token *token = cp_lexer_peek_token (parser->lexer);
18187
18188 node = token->u.value;
18189
18190 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18191 && (node == ridpointers [(int) RID_IN]
18192 || node == ridpointers [(int) RID_OUT]
18193 || node == ridpointers [(int) RID_INOUT]
18194 || node == ridpointers [(int) RID_BYCOPY]
18195 || node == ridpointers [(int) RID_BYREF]
18196 || node == ridpointers [(int) RID_ONEWAY]))
18197 {
18198 quals = tree_cons (NULL_TREE, node, quals);
18199 cp_lexer_consume_token (parser->lexer);
18200 token = cp_lexer_peek_token (parser->lexer);
18201 node = token->u.value;
18202 }
18203
18204 return quals;
18205 }
18206
18207 /* Parse an Objective-C typename. */
18208
18209 static tree
18210 cp_parser_objc_typename (cp_parser* parser)
18211 {
18212 tree typename = NULL_TREE;
18213
18214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18215 {
18216 tree proto_quals, cp_type = NULL_TREE;
18217
18218 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18219 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18220
18221 /* An ObjC type name may consist of just protocol qualifiers, in which
18222 case the type shall default to 'id'. */
18223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18224 cp_type = cp_parser_type_id (parser);
18225
18226 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18227 typename = build_tree_list (proto_quals, cp_type);
18228 }
18229
18230 return typename;
18231 }
18232
18233 /* Check to see if TYPE refers to an Objective-C selector name. */
18234
18235 static bool
18236 cp_parser_objc_selector_p (enum cpp_ttype type)
18237 {
18238 return (type == CPP_NAME || type == CPP_KEYWORD
18239 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18240 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18241 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18242 || type == CPP_XOR || type == CPP_XOR_EQ);
18243 }
18244
18245 /* Parse an Objective-C selector. */
18246
18247 static tree
18248 cp_parser_objc_selector (cp_parser* parser)
18249 {
18250 cp_token *token = cp_lexer_consume_token (parser->lexer);
18251
18252 if (!cp_parser_objc_selector_p (token->type))
18253 {
18254 error ("invalid Objective-C++ selector name");
18255 return error_mark_node;
18256 }
18257
18258 /* C++ operator names are allowed to appear in ObjC selectors. */
18259 switch (token->type)
18260 {
18261 case CPP_AND_AND: return get_identifier ("and");
18262 case CPP_AND_EQ: return get_identifier ("and_eq");
18263 case CPP_AND: return get_identifier ("bitand");
18264 case CPP_OR: return get_identifier ("bitor");
18265 case CPP_COMPL: return get_identifier ("compl");
18266 case CPP_NOT: return get_identifier ("not");
18267 case CPP_NOT_EQ: return get_identifier ("not_eq");
18268 case CPP_OR_OR: return get_identifier ("or");
18269 case CPP_OR_EQ: return get_identifier ("or_eq");
18270 case CPP_XOR: return get_identifier ("xor");
18271 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18272 default: return token->u.value;
18273 }
18274 }
18275
18276 /* Parse an Objective-C params list. */
18277
18278 static tree
18279 cp_parser_objc_method_keyword_params (cp_parser* parser)
18280 {
18281 tree params = NULL_TREE;
18282 bool maybe_unary_selector_p = true;
18283 cp_token *token = cp_lexer_peek_token (parser->lexer);
18284
18285 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18286 {
18287 tree selector = NULL_TREE, typename, identifier;
18288
18289 if (token->type != CPP_COLON)
18290 selector = cp_parser_objc_selector (parser);
18291
18292 /* Detect if we have a unary selector. */
18293 if (maybe_unary_selector_p
18294 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18295 return selector;
18296
18297 maybe_unary_selector_p = false;
18298 cp_parser_require (parser, CPP_COLON, "`:'");
18299 typename = cp_parser_objc_typename (parser);
18300 identifier = cp_parser_identifier (parser);
18301
18302 params
18303 = chainon (params,
18304 objc_build_keyword_decl (selector,
18305 typename,
18306 identifier));
18307
18308 token = cp_lexer_peek_token (parser->lexer);
18309 }
18310
18311 return params;
18312 }
18313
18314 /* Parse the non-keyword Objective-C params. */
18315
18316 static tree
18317 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18318 {
18319 tree params = make_node (TREE_LIST);
18320 cp_token *token = cp_lexer_peek_token (parser->lexer);
18321 *ellipsisp = false; /* Initially, assume no ellipsis. */
18322
18323 while (token->type == CPP_COMMA)
18324 {
18325 cp_parameter_declarator *parmdecl;
18326 tree parm;
18327
18328 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18329 token = cp_lexer_peek_token (parser->lexer);
18330
18331 if (token->type == CPP_ELLIPSIS)
18332 {
18333 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18334 *ellipsisp = true;
18335 break;
18336 }
18337
18338 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18339 parm = grokdeclarator (parmdecl->declarator,
18340 &parmdecl->decl_specifiers,
18341 PARM, /*initialized=*/0,
18342 /*attrlist=*/NULL);
18343
18344 chainon (params, build_tree_list (NULL_TREE, parm));
18345 token = cp_lexer_peek_token (parser->lexer);
18346 }
18347
18348 return params;
18349 }
18350
18351 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18352
18353 static void
18354 cp_parser_objc_interstitial_code (cp_parser* parser)
18355 {
18356 cp_token *token = cp_lexer_peek_token (parser->lexer);
18357
18358 /* If the next token is `extern' and the following token is a string
18359 literal, then we have a linkage specification. */
18360 if (token->keyword == RID_EXTERN
18361 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18362 cp_parser_linkage_specification (parser);
18363 /* Handle #pragma, if any. */
18364 else if (token->type == CPP_PRAGMA)
18365 cp_parser_pragma (parser, pragma_external);
18366 /* Allow stray semicolons. */
18367 else if (token->type == CPP_SEMICOLON)
18368 cp_lexer_consume_token (parser->lexer);
18369 /* Finally, try to parse a block-declaration, or a function-definition. */
18370 else
18371 cp_parser_block_declaration (parser, /*statement_p=*/false);
18372 }
18373
18374 /* Parse a method signature. */
18375
18376 static tree
18377 cp_parser_objc_method_signature (cp_parser* parser)
18378 {
18379 tree rettype, kwdparms, optparms;
18380 bool ellipsis = false;
18381
18382 cp_parser_objc_method_type (parser);
18383 rettype = cp_parser_objc_typename (parser);
18384 kwdparms = cp_parser_objc_method_keyword_params (parser);
18385 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18386
18387 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18388 }
18389
18390 /* Pars an Objective-C method prototype list. */
18391
18392 static void
18393 cp_parser_objc_method_prototype_list (cp_parser* parser)
18394 {
18395 cp_token *token = cp_lexer_peek_token (parser->lexer);
18396
18397 while (token->keyword != RID_AT_END)
18398 {
18399 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18400 {
18401 objc_add_method_declaration
18402 (cp_parser_objc_method_signature (parser));
18403 cp_parser_consume_semicolon_at_end_of_statement (parser);
18404 }
18405 else
18406 /* Allow for interspersed non-ObjC++ code. */
18407 cp_parser_objc_interstitial_code (parser);
18408
18409 token = cp_lexer_peek_token (parser->lexer);
18410 }
18411
18412 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18413 objc_finish_interface ();
18414 }
18415
18416 /* Parse an Objective-C method definition list. */
18417
18418 static void
18419 cp_parser_objc_method_definition_list (cp_parser* parser)
18420 {
18421 cp_token *token = cp_lexer_peek_token (parser->lexer);
18422
18423 while (token->keyword != RID_AT_END)
18424 {
18425 tree meth;
18426
18427 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18428 {
18429 push_deferring_access_checks (dk_deferred);
18430 objc_start_method_definition
18431 (cp_parser_objc_method_signature (parser));
18432
18433 /* For historical reasons, we accept an optional semicolon. */
18434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18435 cp_lexer_consume_token (parser->lexer);
18436
18437 perform_deferred_access_checks ();
18438 stop_deferring_access_checks ();
18439 meth = cp_parser_function_definition_after_declarator (parser,
18440 false);
18441 pop_deferring_access_checks ();
18442 objc_finish_method_definition (meth);
18443 }
18444 else
18445 /* Allow for interspersed non-ObjC++ code. */
18446 cp_parser_objc_interstitial_code (parser);
18447
18448 token = cp_lexer_peek_token (parser->lexer);
18449 }
18450
18451 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18452 objc_finish_implementation ();
18453 }
18454
18455 /* Parse Objective-C ivars. */
18456
18457 static void
18458 cp_parser_objc_class_ivars (cp_parser* parser)
18459 {
18460 cp_token *token = cp_lexer_peek_token (parser->lexer);
18461
18462 if (token->type != CPP_OPEN_BRACE)
18463 return; /* No ivars specified. */
18464
18465 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18466 token = cp_lexer_peek_token (parser->lexer);
18467
18468 while (token->type != CPP_CLOSE_BRACE)
18469 {
18470 cp_decl_specifier_seq declspecs;
18471 int decl_class_or_enum_p;
18472 tree prefix_attributes;
18473
18474 cp_parser_objc_visibility_spec (parser);
18475
18476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18477 break;
18478
18479 cp_parser_decl_specifier_seq (parser,
18480 CP_PARSER_FLAGS_OPTIONAL,
18481 &declspecs,
18482 &decl_class_or_enum_p);
18483 prefix_attributes = declspecs.attributes;
18484 declspecs.attributes = NULL_TREE;
18485
18486 /* Keep going until we hit the `;' at the end of the
18487 declaration. */
18488 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18489 {
18490 tree width = NULL_TREE, attributes, first_attribute, decl;
18491 cp_declarator *declarator = NULL;
18492 int ctor_dtor_or_conv_p;
18493
18494 /* Check for a (possibly unnamed) bitfield declaration. */
18495 token = cp_lexer_peek_token (parser->lexer);
18496 if (token->type == CPP_COLON)
18497 goto eat_colon;
18498
18499 if (token->type == CPP_NAME
18500 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18501 == CPP_COLON))
18502 {
18503 /* Get the name of the bitfield. */
18504 declarator = make_id_declarator (NULL_TREE,
18505 cp_parser_identifier (parser),
18506 sfk_none);
18507
18508 eat_colon:
18509 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18510 /* Get the width of the bitfield. */
18511 width
18512 = cp_parser_constant_expression (parser,
18513 /*allow_non_constant=*/false,
18514 NULL);
18515 }
18516 else
18517 {
18518 /* Parse the declarator. */
18519 declarator
18520 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18521 &ctor_dtor_or_conv_p,
18522 /*parenthesized_p=*/NULL,
18523 /*member_p=*/false);
18524 }
18525
18526 /* Look for attributes that apply to the ivar. */
18527 attributes = cp_parser_attributes_opt (parser);
18528 /* Remember which attributes are prefix attributes and
18529 which are not. */
18530 first_attribute = attributes;
18531 /* Combine the attributes. */
18532 attributes = chainon (prefix_attributes, attributes);
18533
18534 if (width)
18535 {
18536 /* Create the bitfield declaration. */
18537 decl = grokbitfield (declarator, &declspecs, width);
18538 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18539 }
18540 else
18541 decl = grokfield (declarator, &declspecs,
18542 NULL_TREE, /*init_const_expr_p=*/false,
18543 NULL_TREE, attributes);
18544
18545 /* Add the instance variable. */
18546 objc_add_instance_variable (decl);
18547
18548 /* Reset PREFIX_ATTRIBUTES. */
18549 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18550 attributes = TREE_CHAIN (attributes);
18551 if (attributes)
18552 TREE_CHAIN (attributes) = NULL_TREE;
18553
18554 token = cp_lexer_peek_token (parser->lexer);
18555
18556 if (token->type == CPP_COMMA)
18557 {
18558 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18559 continue;
18560 }
18561 break;
18562 }
18563
18564 cp_parser_consume_semicolon_at_end_of_statement (parser);
18565 token = cp_lexer_peek_token (parser->lexer);
18566 }
18567
18568 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18569 /* For historical reasons, we accept an optional semicolon. */
18570 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18571 cp_lexer_consume_token (parser->lexer);
18572 }
18573
18574 /* Parse an Objective-C protocol declaration. */
18575
18576 static void
18577 cp_parser_objc_protocol_declaration (cp_parser* parser)
18578 {
18579 tree proto, protorefs;
18580 cp_token *tok;
18581
18582 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18584 {
18585 error ("identifier expected after %<@protocol%>");
18586 goto finish;
18587 }
18588
18589 /* See if we have a forward declaration or a definition. */
18590 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18591
18592 /* Try a forward declaration first. */
18593 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18594 {
18595 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18596 finish:
18597 cp_parser_consume_semicolon_at_end_of_statement (parser);
18598 }
18599
18600 /* Ok, we got a full-fledged definition (or at least should). */
18601 else
18602 {
18603 proto = cp_parser_identifier (parser);
18604 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18605 objc_start_protocol (proto, protorefs);
18606 cp_parser_objc_method_prototype_list (parser);
18607 }
18608 }
18609
18610 /* Parse an Objective-C superclass or category. */
18611
18612 static void
18613 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18614 tree *categ)
18615 {
18616 cp_token *next = cp_lexer_peek_token (parser->lexer);
18617
18618 *super = *categ = NULL_TREE;
18619 if (next->type == CPP_COLON)
18620 {
18621 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18622 *super = cp_parser_identifier (parser);
18623 }
18624 else if (next->type == CPP_OPEN_PAREN)
18625 {
18626 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18627 *categ = cp_parser_identifier (parser);
18628 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18629 }
18630 }
18631
18632 /* Parse an Objective-C class interface. */
18633
18634 static void
18635 cp_parser_objc_class_interface (cp_parser* parser)
18636 {
18637 tree name, super, categ, protos;
18638
18639 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18640 name = cp_parser_identifier (parser);
18641 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18642 protos = cp_parser_objc_protocol_refs_opt (parser);
18643
18644 /* We have either a class or a category on our hands. */
18645 if (categ)
18646 objc_start_category_interface (name, categ, protos);
18647 else
18648 {
18649 objc_start_class_interface (name, super, protos);
18650 /* Handle instance variable declarations, if any. */
18651 cp_parser_objc_class_ivars (parser);
18652 objc_continue_interface ();
18653 }
18654
18655 cp_parser_objc_method_prototype_list (parser);
18656 }
18657
18658 /* Parse an Objective-C class implementation. */
18659
18660 static void
18661 cp_parser_objc_class_implementation (cp_parser* parser)
18662 {
18663 tree name, super, categ;
18664
18665 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18666 name = cp_parser_identifier (parser);
18667 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18668
18669 /* We have either a class or a category on our hands. */
18670 if (categ)
18671 objc_start_category_implementation (name, categ);
18672 else
18673 {
18674 objc_start_class_implementation (name, super);
18675 /* Handle instance variable declarations, if any. */
18676 cp_parser_objc_class_ivars (parser);
18677 objc_continue_implementation ();
18678 }
18679
18680 cp_parser_objc_method_definition_list (parser);
18681 }
18682
18683 /* Consume the @end token and finish off the implementation. */
18684
18685 static void
18686 cp_parser_objc_end_implementation (cp_parser* parser)
18687 {
18688 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18689 objc_finish_implementation ();
18690 }
18691
18692 /* Parse an Objective-C declaration. */
18693
18694 static void
18695 cp_parser_objc_declaration (cp_parser* parser)
18696 {
18697 /* Try to figure out what kind of declaration is present. */
18698 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18699
18700 switch (kwd->keyword)
18701 {
18702 case RID_AT_ALIAS:
18703 cp_parser_objc_alias_declaration (parser);
18704 break;
18705 case RID_AT_CLASS:
18706 cp_parser_objc_class_declaration (parser);
18707 break;
18708 case RID_AT_PROTOCOL:
18709 cp_parser_objc_protocol_declaration (parser);
18710 break;
18711 case RID_AT_INTERFACE:
18712 cp_parser_objc_class_interface (parser);
18713 break;
18714 case RID_AT_IMPLEMENTATION:
18715 cp_parser_objc_class_implementation (parser);
18716 break;
18717 case RID_AT_END:
18718 cp_parser_objc_end_implementation (parser);
18719 break;
18720 default:
18721 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18722 cp_parser_skip_to_end_of_block_or_statement (parser);
18723 }
18724 }
18725
18726 /* Parse an Objective-C try-catch-finally statement.
18727
18728 objc-try-catch-finally-stmt:
18729 @try compound-statement objc-catch-clause-seq [opt]
18730 objc-finally-clause [opt]
18731
18732 objc-catch-clause-seq:
18733 objc-catch-clause objc-catch-clause-seq [opt]
18734
18735 objc-catch-clause:
18736 @catch ( exception-declaration ) compound-statement
18737
18738 objc-finally-clause
18739 @finally compound-statement
18740
18741 Returns NULL_TREE. */
18742
18743 static tree
18744 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18745 location_t location;
18746 tree stmt;
18747
18748 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18749 location = cp_lexer_peek_token (parser->lexer)->location;
18750 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18751 node, lest it get absorbed into the surrounding block. */
18752 stmt = push_stmt_list ();
18753 cp_parser_compound_statement (parser, NULL, false);
18754 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18755
18756 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18757 {
18758 cp_parameter_declarator *parmdecl;
18759 tree parm;
18760
18761 cp_lexer_consume_token (parser->lexer);
18762 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18763 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18764 parm = grokdeclarator (parmdecl->declarator,
18765 &parmdecl->decl_specifiers,
18766 PARM, /*initialized=*/0,
18767 /*attrlist=*/NULL);
18768 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18769 objc_begin_catch_clause (parm);
18770 cp_parser_compound_statement (parser, NULL, false);
18771 objc_finish_catch_clause ();
18772 }
18773
18774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18775 {
18776 cp_lexer_consume_token (parser->lexer);
18777 location = cp_lexer_peek_token (parser->lexer)->location;
18778 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18779 node, lest it get absorbed into the surrounding block. */
18780 stmt = push_stmt_list ();
18781 cp_parser_compound_statement (parser, NULL, false);
18782 objc_build_finally_clause (location, pop_stmt_list (stmt));
18783 }
18784
18785 return objc_finish_try_stmt ();
18786 }
18787
18788 /* Parse an Objective-C synchronized statement.
18789
18790 objc-synchronized-stmt:
18791 @synchronized ( expression ) compound-statement
18792
18793 Returns NULL_TREE. */
18794
18795 static tree
18796 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18797 location_t location;
18798 tree lock, stmt;
18799
18800 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18801
18802 location = cp_lexer_peek_token (parser->lexer)->location;
18803 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18804 lock = cp_parser_expression (parser, false);
18805 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18806
18807 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18808 node, lest it get absorbed into the surrounding block. */
18809 stmt = push_stmt_list ();
18810 cp_parser_compound_statement (parser, NULL, false);
18811
18812 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18813 }
18814
18815 /* Parse an Objective-C throw statement.
18816
18817 objc-throw-stmt:
18818 @throw assignment-expression [opt] ;
18819
18820 Returns a constructed '@throw' statement. */
18821
18822 static tree
18823 cp_parser_objc_throw_statement (cp_parser *parser) {
18824 tree expr = NULL_TREE;
18825
18826 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18827
18828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18829 expr = cp_parser_assignment_expression (parser, false);
18830
18831 cp_parser_consume_semicolon_at_end_of_statement (parser);
18832
18833 return objc_build_throw_stmt (expr);
18834 }
18835
18836 /* Parse an Objective-C statement. */
18837
18838 static tree
18839 cp_parser_objc_statement (cp_parser * parser) {
18840 /* Try to figure out what kind of declaration is present. */
18841 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18842
18843 switch (kwd->keyword)
18844 {
18845 case RID_AT_TRY:
18846 return cp_parser_objc_try_catch_finally_statement (parser);
18847 case RID_AT_SYNCHRONIZED:
18848 return cp_parser_objc_synchronized_statement (parser);
18849 case RID_AT_THROW:
18850 return cp_parser_objc_throw_statement (parser);
18851 default:
18852 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18853 cp_parser_skip_to_end_of_block_or_statement (parser);
18854 }
18855
18856 return error_mark_node;
18857 }
18858 \f
18859 /* OpenMP 2.5 parsing routines. */
18860
18861 /* Returns name of the next clause.
18862 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18863 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18864 returned and the token is consumed. */
18865
18866 static pragma_omp_clause
18867 cp_parser_omp_clause_name (cp_parser *parser)
18868 {
18869 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18870
18871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18872 result = PRAGMA_OMP_CLAUSE_IF;
18873 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18874 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18875 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18876 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18877 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18878 {
18879 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18880 const char *p = IDENTIFIER_POINTER (id);
18881
18882 switch (p[0])
18883 {
18884 case 'c':
18885 if (!strcmp ("copyin", p))
18886 result = PRAGMA_OMP_CLAUSE_COPYIN;
18887 else if (!strcmp ("copyprivate", p))
18888 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18889 break;
18890 case 'f':
18891 if (!strcmp ("firstprivate", p))
18892 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18893 break;
18894 case 'l':
18895 if (!strcmp ("lastprivate", p))
18896 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18897 break;
18898 case 'n':
18899 if (!strcmp ("nowait", p))
18900 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18901 else if (!strcmp ("num_threads", p))
18902 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18903 break;
18904 case 'o':
18905 if (!strcmp ("ordered", p))
18906 result = PRAGMA_OMP_CLAUSE_ORDERED;
18907 break;
18908 case 'r':
18909 if (!strcmp ("reduction", p))
18910 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18911 break;
18912 case 's':
18913 if (!strcmp ("schedule", p))
18914 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18915 else if (!strcmp ("shared", p))
18916 result = PRAGMA_OMP_CLAUSE_SHARED;
18917 break;
18918 }
18919 }
18920
18921 if (result != PRAGMA_OMP_CLAUSE_NONE)
18922 cp_lexer_consume_token (parser->lexer);
18923
18924 return result;
18925 }
18926
18927 /* Validate that a clause of the given type does not already exist. */
18928
18929 static void
18930 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18931 {
18932 tree c;
18933
18934 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18935 if (OMP_CLAUSE_CODE (c) == code)
18936 {
18937 error ("too many %qs clauses", name);
18938 break;
18939 }
18940 }
18941
18942 /* OpenMP 2.5:
18943 variable-list:
18944 identifier
18945 variable-list , identifier
18946
18947 In addition, we match a closing parenthesis. An opening parenthesis
18948 will have been consumed by the caller.
18949
18950 If KIND is nonzero, create the appropriate node and install the decl
18951 in OMP_CLAUSE_DECL and add the node to the head of the list.
18952
18953 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18954 return the list created. */
18955
18956 static tree
18957 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18958 tree list)
18959 {
18960 while (1)
18961 {
18962 tree name, decl;
18963
18964 name = cp_parser_id_expression (parser, /*template_p=*/false,
18965 /*check_dependency_p=*/true,
18966 /*template_p=*/NULL,
18967 /*declarator_p=*/false,
18968 /*optional_p=*/false);
18969 if (name == error_mark_node)
18970 goto skip_comma;
18971
18972 decl = cp_parser_lookup_name_simple (parser, name);
18973 if (decl == error_mark_node)
18974 cp_parser_name_lookup_error (parser, name, decl, NULL);
18975 else if (kind != 0)
18976 {
18977 tree u = build_omp_clause (kind);
18978 OMP_CLAUSE_DECL (u) = decl;
18979 OMP_CLAUSE_CHAIN (u) = list;
18980 list = u;
18981 }
18982 else
18983 list = tree_cons (decl, NULL_TREE, list);
18984
18985 get_comma:
18986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18987 break;
18988 cp_lexer_consume_token (parser->lexer);
18989 }
18990
18991 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18992 {
18993 int ending;
18994
18995 /* Try to resync to an unnested comma. Copied from
18996 cp_parser_parenthesized_expression_list. */
18997 skip_comma:
18998 ending = cp_parser_skip_to_closing_parenthesis (parser,
18999 /*recovering=*/true,
19000 /*or_comma=*/true,
19001 /*consume_paren=*/true);
19002 if (ending < 0)
19003 goto get_comma;
19004 }
19005
19006 return list;
19007 }
19008
19009 /* Similarly, but expect leading and trailing parenthesis. This is a very
19010 common case for omp clauses. */
19011
19012 static tree
19013 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19014 {
19015 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19016 return cp_parser_omp_var_list_no_open (parser, kind, list);
19017 return list;
19018 }
19019
19020 /* OpenMP 2.5:
19021 default ( shared | none ) */
19022
19023 static tree
19024 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19025 {
19026 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19027 tree c;
19028
19029 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19030 return list;
19031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19032 {
19033 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19034 const char *p = IDENTIFIER_POINTER (id);
19035
19036 switch (p[0])
19037 {
19038 case 'n':
19039 if (strcmp ("none", p) != 0)
19040 goto invalid_kind;
19041 kind = OMP_CLAUSE_DEFAULT_NONE;
19042 break;
19043
19044 case 's':
19045 if (strcmp ("shared", p) != 0)
19046 goto invalid_kind;
19047 kind = OMP_CLAUSE_DEFAULT_SHARED;
19048 break;
19049
19050 default:
19051 goto invalid_kind;
19052 }
19053
19054 cp_lexer_consume_token (parser->lexer);
19055 }
19056 else
19057 {
19058 invalid_kind:
19059 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19060 }
19061
19062 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19063 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19064 /*or_comma=*/false,
19065 /*consume_paren=*/true);
19066
19067 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19068 return list;
19069
19070 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19071 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19072 OMP_CLAUSE_CHAIN (c) = list;
19073 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19074
19075 return c;
19076 }
19077
19078 /* OpenMP 2.5:
19079 if ( expression ) */
19080
19081 static tree
19082 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19083 {
19084 tree t, c;
19085
19086 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19087 return list;
19088
19089 t = cp_parser_condition (parser);
19090
19091 if (t == error_mark_node
19092 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19093 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19094 /*or_comma=*/false,
19095 /*consume_paren=*/true);
19096
19097 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19098
19099 c = build_omp_clause (OMP_CLAUSE_IF);
19100 OMP_CLAUSE_IF_EXPR (c) = t;
19101 OMP_CLAUSE_CHAIN (c) = list;
19102
19103 return c;
19104 }
19105
19106 /* OpenMP 2.5:
19107 nowait */
19108
19109 static tree
19110 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19111 {
19112 tree c;
19113
19114 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19115
19116 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19117 OMP_CLAUSE_CHAIN (c) = list;
19118 return c;
19119 }
19120
19121 /* OpenMP 2.5:
19122 num_threads ( expression ) */
19123
19124 static tree
19125 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19126 {
19127 tree t, c;
19128
19129 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19130 return list;
19131
19132 t = cp_parser_expression (parser, false);
19133
19134 if (t == error_mark_node
19135 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19136 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19137 /*or_comma=*/false,
19138 /*consume_paren=*/true);
19139
19140 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19141
19142 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19143 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19144 OMP_CLAUSE_CHAIN (c) = list;
19145
19146 return c;
19147 }
19148
19149 /* OpenMP 2.5:
19150 ordered */
19151
19152 static tree
19153 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19154 {
19155 tree c;
19156
19157 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19158
19159 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19160 OMP_CLAUSE_CHAIN (c) = list;
19161 return c;
19162 }
19163
19164 /* OpenMP 2.5:
19165 reduction ( reduction-operator : variable-list )
19166
19167 reduction-operator:
19168 One of: + * - & ^ | && || */
19169
19170 static tree
19171 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19172 {
19173 enum tree_code code;
19174 tree nlist, c;
19175
19176 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19177 return list;
19178
19179 switch (cp_lexer_peek_token (parser->lexer)->type)
19180 {
19181 case CPP_PLUS:
19182 code = PLUS_EXPR;
19183 break;
19184 case CPP_MULT:
19185 code = MULT_EXPR;
19186 break;
19187 case CPP_MINUS:
19188 code = MINUS_EXPR;
19189 break;
19190 case CPP_AND:
19191 code = BIT_AND_EXPR;
19192 break;
19193 case CPP_XOR:
19194 code = BIT_XOR_EXPR;
19195 break;
19196 case CPP_OR:
19197 code = BIT_IOR_EXPR;
19198 break;
19199 case CPP_AND_AND:
19200 code = TRUTH_ANDIF_EXPR;
19201 break;
19202 case CPP_OR_OR:
19203 code = TRUTH_ORIF_EXPR;
19204 break;
19205 default:
19206 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19207 resync_fail:
19208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19209 /*or_comma=*/false,
19210 /*consume_paren=*/true);
19211 return list;
19212 }
19213 cp_lexer_consume_token (parser->lexer);
19214
19215 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19216 goto resync_fail;
19217
19218 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19219 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19220 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19221
19222 return nlist;
19223 }
19224
19225 /* OpenMP 2.5:
19226 schedule ( schedule-kind )
19227 schedule ( schedule-kind , expression )
19228
19229 schedule-kind:
19230 static | dynamic | guided | runtime */
19231
19232 static tree
19233 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19234 {
19235 tree c, t;
19236
19237 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19238 return list;
19239
19240 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19241
19242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19243 {
19244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19245 const char *p = IDENTIFIER_POINTER (id);
19246
19247 switch (p[0])
19248 {
19249 case 'd':
19250 if (strcmp ("dynamic", p) != 0)
19251 goto invalid_kind;
19252 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19253 break;
19254
19255 case 'g':
19256 if (strcmp ("guided", p) != 0)
19257 goto invalid_kind;
19258 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19259 break;
19260
19261 case 'r':
19262 if (strcmp ("runtime", p) != 0)
19263 goto invalid_kind;
19264 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19265 break;
19266
19267 default:
19268 goto invalid_kind;
19269 }
19270 }
19271 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19272 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19273 else
19274 goto invalid_kind;
19275 cp_lexer_consume_token (parser->lexer);
19276
19277 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19278 {
19279 cp_lexer_consume_token (parser->lexer);
19280
19281 t = cp_parser_assignment_expression (parser, false);
19282
19283 if (t == error_mark_node)
19284 goto resync_fail;
19285 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19286 error ("schedule %<runtime%> does not take "
19287 "a %<chunk_size%> parameter");
19288 else
19289 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19290
19291 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19292 goto resync_fail;
19293 }
19294 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19295 goto resync_fail;
19296
19297 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19298 OMP_CLAUSE_CHAIN (c) = list;
19299 return c;
19300
19301 invalid_kind:
19302 cp_parser_error (parser, "invalid schedule kind");
19303 resync_fail:
19304 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19305 /*or_comma=*/false,
19306 /*consume_paren=*/true);
19307 return list;
19308 }
19309
19310 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19311 is a bitmask in MASK. Return the list of clauses found; the result
19312 of clause default goes in *pdefault. */
19313
19314 static tree
19315 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19316 const char *where, cp_token *pragma_tok)
19317 {
19318 tree clauses = NULL;
19319
19320 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19321 {
19322 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19323 const char *c_name;
19324 tree prev = clauses;
19325
19326 switch (c_kind)
19327 {
19328 case PRAGMA_OMP_CLAUSE_COPYIN:
19329 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19330 c_name = "copyin";
19331 break;
19332 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19333 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19334 clauses);
19335 c_name = "copyprivate";
19336 break;
19337 case PRAGMA_OMP_CLAUSE_DEFAULT:
19338 clauses = cp_parser_omp_clause_default (parser, clauses);
19339 c_name = "default";
19340 break;
19341 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19342 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19343 clauses);
19344 c_name = "firstprivate";
19345 break;
19346 case PRAGMA_OMP_CLAUSE_IF:
19347 clauses = cp_parser_omp_clause_if (parser, clauses);
19348 c_name = "if";
19349 break;
19350 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19351 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19352 clauses);
19353 c_name = "lastprivate";
19354 break;
19355 case PRAGMA_OMP_CLAUSE_NOWAIT:
19356 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19357 c_name = "nowait";
19358 break;
19359 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19360 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19361 c_name = "num_threads";
19362 break;
19363 case PRAGMA_OMP_CLAUSE_ORDERED:
19364 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19365 c_name = "ordered";
19366 break;
19367 case PRAGMA_OMP_CLAUSE_PRIVATE:
19368 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19369 clauses);
19370 c_name = "private";
19371 break;
19372 case PRAGMA_OMP_CLAUSE_REDUCTION:
19373 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19374 c_name = "reduction";
19375 break;
19376 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19377 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19378 c_name = "schedule";
19379 break;
19380 case PRAGMA_OMP_CLAUSE_SHARED:
19381 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19382 clauses);
19383 c_name = "shared";
19384 break;
19385 default:
19386 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19387 goto saw_error;
19388 }
19389
19390 if (((mask >> c_kind) & 1) == 0)
19391 {
19392 /* Remove the invalid clause(s) from the list to avoid
19393 confusing the rest of the compiler. */
19394 clauses = prev;
19395 error ("%qs is not valid for %qs", c_name, where);
19396 }
19397 }
19398 saw_error:
19399 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19400 return finish_omp_clauses (clauses);
19401 }
19402
19403 /* OpenMP 2.5:
19404 structured-block:
19405 statement
19406
19407 In practice, we're also interested in adding the statement to an
19408 outer node. So it is convenient if we work around the fact that
19409 cp_parser_statement calls add_stmt. */
19410
19411 static unsigned
19412 cp_parser_begin_omp_structured_block (cp_parser *parser)
19413 {
19414 unsigned save = parser->in_statement;
19415
19416 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19417 This preserves the "not within loop or switch" style error messages
19418 for nonsense cases like
19419 void foo() {
19420 #pragma omp single
19421 break;
19422 }
19423 */
19424 if (parser->in_statement)
19425 parser->in_statement = IN_OMP_BLOCK;
19426
19427 return save;
19428 }
19429
19430 static void
19431 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19432 {
19433 parser->in_statement = save;
19434 }
19435
19436 static tree
19437 cp_parser_omp_structured_block (cp_parser *parser)
19438 {
19439 tree stmt = begin_omp_structured_block ();
19440 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19441
19442 cp_parser_statement (parser, NULL_TREE, false, NULL);
19443
19444 cp_parser_end_omp_structured_block (parser, save);
19445 return finish_omp_structured_block (stmt);
19446 }
19447
19448 /* OpenMP 2.5:
19449 # pragma omp atomic new-line
19450 expression-stmt
19451
19452 expression-stmt:
19453 x binop= expr | x++ | ++x | x-- | --x
19454 binop:
19455 +, *, -, /, &, ^, |, <<, >>
19456
19457 where x is an lvalue expression with scalar type. */
19458
19459 static void
19460 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19461 {
19462 tree lhs, rhs;
19463 enum tree_code code;
19464
19465 cp_parser_require_pragma_eol (parser, pragma_tok);
19466
19467 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19468 /*cast_p=*/false);
19469 switch (TREE_CODE (lhs))
19470 {
19471 case ERROR_MARK:
19472 goto saw_error;
19473
19474 case PREINCREMENT_EXPR:
19475 case POSTINCREMENT_EXPR:
19476 lhs = TREE_OPERAND (lhs, 0);
19477 code = PLUS_EXPR;
19478 rhs = integer_one_node;
19479 break;
19480
19481 case PREDECREMENT_EXPR:
19482 case POSTDECREMENT_EXPR:
19483 lhs = TREE_OPERAND (lhs, 0);
19484 code = MINUS_EXPR;
19485 rhs = integer_one_node;
19486 break;
19487
19488 default:
19489 switch (cp_lexer_peek_token (parser->lexer)->type)
19490 {
19491 case CPP_MULT_EQ:
19492 code = MULT_EXPR;
19493 break;
19494 case CPP_DIV_EQ:
19495 code = TRUNC_DIV_EXPR;
19496 break;
19497 case CPP_PLUS_EQ:
19498 code = PLUS_EXPR;
19499 break;
19500 case CPP_MINUS_EQ:
19501 code = MINUS_EXPR;
19502 break;
19503 case CPP_LSHIFT_EQ:
19504 code = LSHIFT_EXPR;
19505 break;
19506 case CPP_RSHIFT_EQ:
19507 code = RSHIFT_EXPR;
19508 break;
19509 case CPP_AND_EQ:
19510 code = BIT_AND_EXPR;
19511 break;
19512 case CPP_OR_EQ:
19513 code = BIT_IOR_EXPR;
19514 break;
19515 case CPP_XOR_EQ:
19516 code = BIT_XOR_EXPR;
19517 break;
19518 default:
19519 cp_parser_error (parser,
19520 "invalid operator for %<#pragma omp atomic%>");
19521 goto saw_error;
19522 }
19523 cp_lexer_consume_token (parser->lexer);
19524
19525 rhs = cp_parser_expression (parser, false);
19526 if (rhs == error_mark_node)
19527 goto saw_error;
19528 break;
19529 }
19530 finish_omp_atomic (code, lhs, rhs);
19531 cp_parser_consume_semicolon_at_end_of_statement (parser);
19532 return;
19533
19534 saw_error:
19535 cp_parser_skip_to_end_of_block_or_statement (parser);
19536 }
19537
19538
19539 /* OpenMP 2.5:
19540 # pragma omp barrier new-line */
19541
19542 static void
19543 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19544 {
19545 cp_parser_require_pragma_eol (parser, pragma_tok);
19546 finish_omp_barrier ();
19547 }
19548
19549 /* OpenMP 2.5:
19550 # pragma omp critical [(name)] new-line
19551 structured-block */
19552
19553 static tree
19554 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19555 {
19556 tree stmt, name = NULL;
19557
19558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19559 {
19560 cp_lexer_consume_token (parser->lexer);
19561
19562 name = cp_parser_identifier (parser);
19563
19564 if (name == error_mark_node
19565 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19566 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19567 /*or_comma=*/false,
19568 /*consume_paren=*/true);
19569 if (name == error_mark_node)
19570 name = NULL;
19571 }
19572 cp_parser_require_pragma_eol (parser, pragma_tok);
19573
19574 stmt = cp_parser_omp_structured_block (parser);
19575 return c_finish_omp_critical (stmt, name);
19576 }
19577
19578 /* OpenMP 2.5:
19579 # pragma omp flush flush-vars[opt] new-line
19580
19581 flush-vars:
19582 ( variable-list ) */
19583
19584 static void
19585 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19586 {
19587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19588 (void) cp_parser_omp_var_list (parser, 0, NULL);
19589 cp_parser_require_pragma_eol (parser, pragma_tok);
19590
19591 finish_omp_flush ();
19592 }
19593
19594 /* Parse the restricted form of the for statment allowed by OpenMP. */
19595
19596 static tree
19597 cp_parser_omp_for_loop (cp_parser *parser)
19598 {
19599 tree init, cond, incr, body, decl, pre_body;
19600 location_t loc;
19601
19602 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19603 {
19604 cp_parser_error (parser, "for statement expected");
19605 return NULL;
19606 }
19607 loc = cp_lexer_consume_token (parser->lexer)->location;
19608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19609 return NULL;
19610
19611 init = decl = NULL;
19612 pre_body = push_stmt_list ();
19613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19614 {
19615 cp_decl_specifier_seq type_specifiers;
19616
19617 /* First, try to parse as an initialized declaration. See
19618 cp_parser_condition, from whence the bulk of this is copied. */
19619
19620 cp_parser_parse_tentatively (parser);
19621 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19622 &type_specifiers);
19623 if (!cp_parser_error_occurred (parser))
19624 {
19625 tree asm_specification, attributes;
19626 cp_declarator *declarator;
19627
19628 declarator = cp_parser_declarator (parser,
19629 CP_PARSER_DECLARATOR_NAMED,
19630 /*ctor_dtor_or_conv_p=*/NULL,
19631 /*parenthesized_p=*/NULL,
19632 /*member_p=*/false);
19633 attributes = cp_parser_attributes_opt (parser);
19634 asm_specification = cp_parser_asm_specification_opt (parser);
19635
19636 cp_parser_require (parser, CPP_EQ, "`='");
19637 if (cp_parser_parse_definitely (parser))
19638 {
19639 tree pushed_scope;
19640
19641 decl = start_decl (declarator, &type_specifiers,
19642 /*initialized_p=*/false, attributes,
19643 /*prefix_attributes=*/NULL_TREE,
19644 &pushed_scope);
19645
19646 init = cp_parser_assignment_expression (parser, false);
19647
19648 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19649 asm_specification, LOOKUP_ONLYCONVERTING);
19650
19651 if (pushed_scope)
19652 pop_scope (pushed_scope);
19653 }
19654 }
19655 else
19656 cp_parser_abort_tentative_parse (parser);
19657
19658 /* If parsing as an initialized declaration failed, try again as
19659 a simple expression. */
19660 if (decl == NULL)
19661 init = cp_parser_expression (parser, false);
19662 }
19663 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19664 pre_body = pop_stmt_list (pre_body);
19665
19666 cond = NULL;
19667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19668 cond = cp_parser_condition (parser);
19669 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19670
19671 incr = NULL;
19672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19673 incr = cp_parser_expression (parser, false);
19674
19675 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19677 /*or_comma=*/false,
19678 /*consume_paren=*/true);
19679
19680 /* Note that we saved the original contents of this flag when we entered
19681 the structured block, and so we don't need to re-save it here. */
19682 parser->in_statement = IN_OMP_FOR;
19683
19684 /* Note that the grammar doesn't call for a structured block here,
19685 though the loop as a whole is a structured block. */
19686 body = push_stmt_list ();
19687 cp_parser_statement (parser, NULL_TREE, false, NULL);
19688 body = pop_stmt_list (body);
19689
19690 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19691 }
19692
19693 /* OpenMP 2.5:
19694 #pragma omp for for-clause[optseq] new-line
19695 for-loop */
19696
19697 #define OMP_FOR_CLAUSE_MASK \
19698 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19699 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19700 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19701 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19702 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19703 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19704 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19705
19706 static tree
19707 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19708 {
19709 tree clauses, sb, ret;
19710 unsigned int save;
19711
19712 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19713 "#pragma omp for", pragma_tok);
19714
19715 sb = begin_omp_structured_block ();
19716 save = cp_parser_begin_omp_structured_block (parser);
19717
19718 ret = cp_parser_omp_for_loop (parser);
19719 if (ret)
19720 OMP_FOR_CLAUSES (ret) = clauses;
19721
19722 cp_parser_end_omp_structured_block (parser, save);
19723 add_stmt (finish_omp_structured_block (sb));
19724
19725 return ret;
19726 }
19727
19728 /* OpenMP 2.5:
19729 # pragma omp master new-line
19730 structured-block */
19731
19732 static tree
19733 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19734 {
19735 cp_parser_require_pragma_eol (parser, pragma_tok);
19736 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19737 }
19738
19739 /* OpenMP 2.5:
19740 # pragma omp ordered new-line
19741 structured-block */
19742
19743 static tree
19744 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19745 {
19746 cp_parser_require_pragma_eol (parser, pragma_tok);
19747 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19748 }
19749
19750 /* OpenMP 2.5:
19751
19752 section-scope:
19753 { section-sequence }
19754
19755 section-sequence:
19756 section-directive[opt] structured-block
19757 section-sequence section-directive structured-block */
19758
19759 static tree
19760 cp_parser_omp_sections_scope (cp_parser *parser)
19761 {
19762 tree stmt, substmt;
19763 bool error_suppress = false;
19764 cp_token *tok;
19765
19766 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19767 return NULL_TREE;
19768
19769 stmt = push_stmt_list ();
19770
19771 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19772 {
19773 unsigned save;
19774
19775 substmt = begin_omp_structured_block ();
19776 save = cp_parser_begin_omp_structured_block (parser);
19777
19778 while (1)
19779 {
19780 cp_parser_statement (parser, NULL_TREE, false, NULL);
19781
19782 tok = cp_lexer_peek_token (parser->lexer);
19783 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19784 break;
19785 if (tok->type == CPP_CLOSE_BRACE)
19786 break;
19787 if (tok->type == CPP_EOF)
19788 break;
19789 }
19790
19791 cp_parser_end_omp_structured_block (parser, save);
19792 substmt = finish_omp_structured_block (substmt);
19793 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19794 add_stmt (substmt);
19795 }
19796
19797 while (1)
19798 {
19799 tok = cp_lexer_peek_token (parser->lexer);
19800 if (tok->type == CPP_CLOSE_BRACE)
19801 break;
19802 if (tok->type == CPP_EOF)
19803 break;
19804
19805 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19806 {
19807 cp_lexer_consume_token (parser->lexer);
19808 cp_parser_require_pragma_eol (parser, tok);
19809 error_suppress = false;
19810 }
19811 else if (!error_suppress)
19812 {
19813 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19814 error_suppress = true;
19815 }
19816
19817 substmt = cp_parser_omp_structured_block (parser);
19818 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19819 add_stmt (substmt);
19820 }
19821 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19822
19823 substmt = pop_stmt_list (stmt);
19824
19825 stmt = make_node (OMP_SECTIONS);
19826 TREE_TYPE (stmt) = void_type_node;
19827 OMP_SECTIONS_BODY (stmt) = substmt;
19828
19829 add_stmt (stmt);
19830 return stmt;
19831 }
19832
19833 /* OpenMP 2.5:
19834 # pragma omp sections sections-clause[optseq] newline
19835 sections-scope */
19836
19837 #define OMP_SECTIONS_CLAUSE_MASK \
19838 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19839 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19840 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19841 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19842 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19843
19844 static tree
19845 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19846 {
19847 tree clauses, ret;
19848
19849 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19850 "#pragma omp sections", pragma_tok);
19851
19852 ret = cp_parser_omp_sections_scope (parser);
19853 if (ret)
19854 OMP_SECTIONS_CLAUSES (ret) = clauses;
19855
19856 return ret;
19857 }
19858
19859 /* OpenMP 2.5:
19860 # pragma parallel parallel-clause new-line
19861 # pragma parallel for parallel-for-clause new-line
19862 # pragma parallel sections parallel-sections-clause new-line */
19863
19864 #define OMP_PARALLEL_CLAUSE_MASK \
19865 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19866 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19867 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19868 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19869 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19870 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19871 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19872 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19873
19874 static tree
19875 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19876 {
19877 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19878 const char *p_name = "#pragma omp parallel";
19879 tree stmt, clauses, par_clause, ws_clause, block;
19880 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19881 unsigned int save;
19882
19883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19884 {
19885 cp_lexer_consume_token (parser->lexer);
19886 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19887 p_name = "#pragma omp parallel for";
19888 mask |= OMP_FOR_CLAUSE_MASK;
19889 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19890 }
19891 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19892 {
19893 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19894 const char *p = IDENTIFIER_POINTER (id);
19895 if (strcmp (p, "sections") == 0)
19896 {
19897 cp_lexer_consume_token (parser->lexer);
19898 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19899 p_name = "#pragma omp parallel sections";
19900 mask |= OMP_SECTIONS_CLAUSE_MASK;
19901 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19902 }
19903 }
19904
19905 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19906 block = begin_omp_parallel ();
19907 save = cp_parser_begin_omp_structured_block (parser);
19908
19909 switch (p_kind)
19910 {
19911 case PRAGMA_OMP_PARALLEL:
19912 cp_parser_already_scoped_statement (parser);
19913 par_clause = clauses;
19914 break;
19915
19916 case PRAGMA_OMP_PARALLEL_FOR:
19917 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19918 stmt = cp_parser_omp_for_loop (parser);
19919 if (stmt)
19920 OMP_FOR_CLAUSES (stmt) = ws_clause;
19921 break;
19922
19923 case PRAGMA_OMP_PARALLEL_SECTIONS:
19924 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19925 stmt = cp_parser_omp_sections_scope (parser);
19926 if (stmt)
19927 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19928 break;
19929
19930 default:
19931 gcc_unreachable ();
19932 }
19933
19934 cp_parser_end_omp_structured_block (parser, save);
19935 stmt = finish_omp_parallel (par_clause, block);
19936 if (p_kind != PRAGMA_OMP_PARALLEL)
19937 OMP_PARALLEL_COMBINED (stmt) = 1;
19938 return stmt;
19939 }
19940
19941 /* OpenMP 2.5:
19942 # pragma omp single single-clause[optseq] new-line
19943 structured-block */
19944
19945 #define OMP_SINGLE_CLAUSE_MASK \
19946 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19947 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19948 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19949 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19950
19951 static tree
19952 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19953 {
19954 tree stmt = make_node (OMP_SINGLE);
19955 TREE_TYPE (stmt) = void_type_node;
19956
19957 OMP_SINGLE_CLAUSES (stmt)
19958 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19959 "#pragma omp single", pragma_tok);
19960 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19961
19962 return add_stmt (stmt);
19963 }
19964
19965 /* OpenMP 2.5:
19966 # pragma omp threadprivate (variable-list) */
19967
19968 static void
19969 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19970 {
19971 tree vars;
19972
19973 vars = cp_parser_omp_var_list (parser, 0, NULL);
19974 cp_parser_require_pragma_eol (parser, pragma_tok);
19975
19976 finish_omp_threadprivate (vars);
19977 }
19978
19979 /* Main entry point to OpenMP statement pragmas. */
19980
19981 static void
19982 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19983 {
19984 tree stmt;
19985
19986 switch (pragma_tok->pragma_kind)
19987 {
19988 case PRAGMA_OMP_ATOMIC:
19989 cp_parser_omp_atomic (parser, pragma_tok);
19990 return;
19991 case PRAGMA_OMP_CRITICAL:
19992 stmt = cp_parser_omp_critical (parser, pragma_tok);
19993 break;
19994 case PRAGMA_OMP_FOR:
19995 stmt = cp_parser_omp_for (parser, pragma_tok);
19996 break;
19997 case PRAGMA_OMP_MASTER:
19998 stmt = cp_parser_omp_master (parser, pragma_tok);
19999 break;
20000 case PRAGMA_OMP_ORDERED:
20001 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20002 break;
20003 case PRAGMA_OMP_PARALLEL:
20004 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20005 break;
20006 case PRAGMA_OMP_SECTIONS:
20007 stmt = cp_parser_omp_sections (parser, pragma_tok);
20008 break;
20009 case PRAGMA_OMP_SINGLE:
20010 stmt = cp_parser_omp_single (parser, pragma_tok);
20011 break;
20012 default:
20013 gcc_unreachable ();
20014 }
20015
20016 if (stmt)
20017 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20018 }
20019 \f
20020 /* The parser. */
20021
20022 static GTY (()) cp_parser *the_parser;
20023
20024 \f
20025 /* Special handling for the first token or line in the file. The first
20026 thing in the file might be #pragma GCC pch_preprocess, which loads a
20027 PCH file, which is a GC collection point. So we need to handle this
20028 first pragma without benefit of an existing lexer structure.
20029
20030 Always returns one token to the caller in *FIRST_TOKEN. This is
20031 either the true first token of the file, or the first token after
20032 the initial pragma. */
20033
20034 static void
20035 cp_parser_initial_pragma (cp_token *first_token)
20036 {
20037 tree name = NULL;
20038
20039 cp_lexer_get_preprocessor_token (NULL, first_token);
20040 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20041 return;
20042
20043 cp_lexer_get_preprocessor_token (NULL, first_token);
20044 if (first_token->type == CPP_STRING)
20045 {
20046 name = first_token->u.value;
20047
20048 cp_lexer_get_preprocessor_token (NULL, first_token);
20049 if (first_token->type != CPP_PRAGMA_EOL)
20050 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20051 }
20052 else
20053 error ("expected string literal");
20054
20055 /* Skip to the end of the pragma. */
20056 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20057 cp_lexer_get_preprocessor_token (NULL, first_token);
20058
20059 /* Now actually load the PCH file. */
20060 if (name)
20061 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20062
20063 /* Read one more token to return to our caller. We have to do this
20064 after reading the PCH file in, since its pointers have to be
20065 live. */
20066 cp_lexer_get_preprocessor_token (NULL, first_token);
20067 }
20068
20069 /* Normal parsing of a pragma token. Here we can (and must) use the
20070 regular lexer. */
20071
20072 static bool
20073 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20074 {
20075 cp_token *pragma_tok;
20076 unsigned int id;
20077
20078 pragma_tok = cp_lexer_consume_token (parser->lexer);
20079 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20080 parser->lexer->in_pragma = true;
20081
20082 id = pragma_tok->pragma_kind;
20083 switch (id)
20084 {
20085 case PRAGMA_GCC_PCH_PREPROCESS:
20086 error ("%<#pragma GCC pch_preprocess%> must be first");
20087 break;
20088
20089 case PRAGMA_OMP_BARRIER:
20090 switch (context)
20091 {
20092 case pragma_compound:
20093 cp_parser_omp_barrier (parser, pragma_tok);
20094 return false;
20095 case pragma_stmt:
20096 error ("%<#pragma omp barrier%> may only be "
20097 "used in compound statements");
20098 break;
20099 default:
20100 goto bad_stmt;
20101 }
20102 break;
20103
20104 case PRAGMA_OMP_FLUSH:
20105 switch (context)
20106 {
20107 case pragma_compound:
20108 cp_parser_omp_flush (parser, pragma_tok);
20109 return false;
20110 case pragma_stmt:
20111 error ("%<#pragma omp flush%> may only be "
20112 "used in compound statements");
20113 break;
20114 default:
20115 goto bad_stmt;
20116 }
20117 break;
20118
20119 case PRAGMA_OMP_THREADPRIVATE:
20120 cp_parser_omp_threadprivate (parser, pragma_tok);
20121 return false;
20122
20123 case PRAGMA_OMP_ATOMIC:
20124 case PRAGMA_OMP_CRITICAL:
20125 case PRAGMA_OMP_FOR:
20126 case PRAGMA_OMP_MASTER:
20127 case PRAGMA_OMP_ORDERED:
20128 case PRAGMA_OMP_PARALLEL:
20129 case PRAGMA_OMP_SECTIONS:
20130 case PRAGMA_OMP_SINGLE:
20131 if (context == pragma_external)
20132 goto bad_stmt;
20133 cp_parser_omp_construct (parser, pragma_tok);
20134 return true;
20135
20136 case PRAGMA_OMP_SECTION:
20137 error ("%<#pragma omp section%> may only be used in "
20138 "%<#pragma omp sections%> construct");
20139 break;
20140
20141 default:
20142 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20143 c_invoke_pragma_handler (id);
20144 break;
20145
20146 bad_stmt:
20147 cp_parser_error (parser, "expected declaration specifiers");
20148 break;
20149 }
20150
20151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20152 return false;
20153 }
20154
20155 /* The interface the pragma parsers have to the lexer. */
20156
20157 enum cpp_ttype
20158 pragma_lex (tree *value)
20159 {
20160 cp_token *tok;
20161 enum cpp_ttype ret;
20162
20163 tok = cp_lexer_peek_token (the_parser->lexer);
20164
20165 ret = tok->type;
20166 *value = tok->u.value;
20167
20168 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20169 ret = CPP_EOF;
20170 else if (ret == CPP_STRING)
20171 *value = cp_parser_string_literal (the_parser, false, false);
20172 else
20173 {
20174 cp_lexer_consume_token (the_parser->lexer);
20175 if (ret == CPP_KEYWORD)
20176 ret = CPP_NAME;
20177 }
20178
20179 return ret;
20180 }
20181
20182 \f
20183 /* External interface. */
20184
20185 /* Parse one entire translation unit. */
20186
20187 void
20188 c_parse_file (void)
20189 {
20190 bool error_occurred;
20191 static bool already_called = false;
20192
20193 if (already_called)
20194 {
20195 sorry ("inter-module optimizations not implemented for C++");
20196 return;
20197 }
20198 already_called = true;
20199
20200 the_parser = cp_parser_new ();
20201 push_deferring_access_checks (flag_access_control
20202 ? dk_no_deferred : dk_no_check);
20203 error_occurred = cp_parser_translation_unit (the_parser);
20204 the_parser = NULL;
20205 }
20206
20207 #include "gt-cp-parser.h"
This page took 0.942149 seconds and 5 git commands to generate.