]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
re PR c++/19564 (-Wparentheses does not work with the C++ front-end)
[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 C++ token. */
49
50 typedef struct cp_token GTY (())
51 {
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
57 /* Token flags. */
58 unsigned char flags;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The input file stack index at which this token was found. */
70 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71 /* The value associated with this token, if any. */
72 tree value;
73 /* The location at which this token was found. */
74 location_t location;
75 } cp_token;
76
77 /* We use a stack of token pointer for saving token sets. */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
81
82 static const cp_token eof_token =
83 {
84 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
86 0
87 #else
88 {0, 0}
89 #endif
90 };
91
92 /* The cp_lexer structure represents the C++ lexer. It is responsible
93 for managing the token stream from the preprocessor and supplying
94 it to the parser. Tokens are never added to the cp_lexer after
95 it is created. */
96
97 typedef struct cp_lexer GTY (())
98 {
99 /* The memory allocated for the buffer. NULL if this lexer does not
100 own the token buffer. */
101 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102 /* If the lexer owns the buffer, this is the number of tokens in the
103 buffer. */
104 size_t buffer_length;
105
106 /* A pointer just past the last available token. The tokens
107 in this lexer are [buffer, last_token). */
108 cp_token_position GTY ((skip)) last_token;
109
110 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
111 no more available tokens. */
112 cp_token_position GTY ((skip)) next_token;
113
114 /* A stack indicating positions at which cp_lexer_save_tokens was
115 called. The top entry is the most recent position at which we
116 began saving tokens. If the stack is non-empty, we are saving
117 tokens. */
118 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
119
120 /* The next lexer in a linked list of lexers. */
121 struct cp_lexer *next;
122
123 /* True if we should output debugging information. */
124 bool debugging_p;
125
126 /* True if we're in the context of parsing a pragma, and should not
127 increment past the end-of-line marker. */
128 bool in_pragma;
129 } cp_lexer;
130
131 /* cp_token_cache is a range of tokens. There is no need to represent
132 allocate heap memory for it, since tokens are never removed from the
133 lexer's array. There is also no need for the GC to walk through
134 a cp_token_cache, since everything in here is referenced through
135 a lexer. */
136
137 typedef struct cp_token_cache GTY(())
138 {
139 /* The beginning of the token range. */
140 cp_token * GTY((skip)) first;
141
142 /* Points immediately after the last token in the range. */
143 cp_token * GTY ((skip)) last;
144 } cp_token_cache;
145
146 /* Prototypes. */
147
148 static cp_lexer *cp_lexer_new_main
149 (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151 (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153 (cp_lexer *);
154 static int cp_lexer_saving_tokens
155 (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157 (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159 (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161 (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163 (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165 (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169 (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171 (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173 (cp_lexer *);
174 static void cp_lexer_purge_token
175 (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179 (cp_lexer *);
180 static void cp_lexer_commit_tokens
181 (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183 (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186 (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188 (cp_lexer *);
189 static void cp_lexer_start_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192 (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195 about passing NULL to functions that require non-NULL arguments
196 (fputs, fprintf). It will never be used, so all we need is a value
197 of the right type that's guaranteed not to be NULL. */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
202
203 static cp_token_cache *cp_token_cache_new
204 (cp_token *, cp_token *);
205
206 static void cp_parser_initial_pragma
207 (cp_token *);
208
209 /* Manifest constants. */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
212
213 /* A token type for keywords, as opposed to ordinary identifiers. */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
215
216 /* A token type for template-ids. If a template-id is processed while
217 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218 the value of the CPP_TEMPLATE_ID is whatever was returned by
219 cp_parser_template_id. */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
221
222 /* A token type for nested-name-specifiers. If a
223 nested-name-specifier is processed while parsing tentatively, it is
224 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226 cp_parser_nested_name_specifier_opt. */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
228
229 /* A token type for tokens that are not tokens at all; these are used
230 to represent slots in the array where there used to be a token
231 that has now been deleted. */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
233
234 /* The number of token types, including C++-specific ones. */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236
237 /* Variables. */
238
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
243
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245 preprocessor. */
246
247 static cp_lexer *
248 cp_lexer_new_main (void)
249 {
250 cp_token first_token;
251 cp_lexer *lexer;
252 cp_token *pos;
253 size_t alloc;
254 size_t space;
255 cp_token *buffer;
256
257 /* It's possible that parsing the first pragma will load a PCH file,
258 which is a GC collection point. So we have to do that before
259 allocating any memory. */
260 cp_parser_initial_pragma (&first_token);
261
262 /* Tell c_lex_with_flags not to merge string constants. */
263 c_lex_return_raw_strings = true;
264
265 c_common_no_more_pch ();
266
267 /* Allocate the memory. */
268 lexer = GGC_CNEW (cp_lexer);
269
270 #ifdef ENABLE_CHECKING
271 /* Initially we are not debugging. */
272 lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275 CP_SAVED_TOKEN_STACK);
276
277 /* Create the buffer. */
278 alloc = CP_LEXER_BUFFER_SIZE;
279 buffer = GGC_NEWVEC (cp_token, alloc);
280
281 /* Put the first token in the buffer. */
282 space = alloc;
283 pos = buffer;
284 *pos = first_token;
285
286 /* Get the remaining tokens from the preprocessor. */
287 while (pos->type != CPP_EOF)
288 {
289 pos++;
290 if (!--space)
291 {
292 space = alloc;
293 alloc *= 2;
294 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295 pos = buffer + space;
296 }
297 cp_lexer_get_preprocessor_token (lexer, pos);
298 }
299 lexer->buffer = buffer;
300 lexer->buffer_length = alloc - space;
301 lexer->last_token = pos;
302 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
303
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
328
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
335 #endif
336
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 return lexer;
339 }
340
341 /* Frees all resources associated with LEXER. */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346 if (lexer->buffer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output. */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359 return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369 return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375 return pos;
376 }
377
378 /* nonzero if we are presently saving tokens. */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN. Return true
387 if we reach EOF. */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 cp_token *token)
392 {
393 static int is_extern_c = 0;
394
395 /* Get a new token from the preprocessor. */
396 token->type
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->input_file_stack_index = input_file_stack_tick;
399 token->keyword = RID_MAX;
400 token->pragma_kind = PRAGMA_NONE;
401 token->in_system_header = in_system_header;
402
403 /* On some systems, some header files are surrounded by an
404 implicit extern "C" block. Set a flag in the token if it
405 comes from such a header. */
406 is_extern_c += pending_lang_change;
407 pending_lang_change = 0;
408 token->implicit_extern_c = is_extern_c > 0;
409
410 /* Check to see if this token is a keyword. */
411 if (token->type == CPP_NAME)
412 {
413 if (C_IS_RESERVED_WORD (token->value))
414 {
415 /* Mark this token as a keyword. */
416 token->type = CPP_KEYWORD;
417 /* Record which keyword. */
418 token->keyword = C_RID_CODE (token->value);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token->value = ridpointers[token->keyword];
424 }
425 else
426 {
427 token->ambiguous_p = false;
428 token->keyword = RID_MAX;
429 }
430 }
431 /* Handle Objective-C++ keywords. */
432 else if (token->type == CPP_AT_NAME)
433 {
434 token->type = CPP_KEYWORD;
435 switch (C_RID_CODE (token->value))
436 {
437 /* Map 'class' to '@class', 'private' to '@private', etc. */
438 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442 case RID_THROW: token->keyword = RID_AT_THROW; break;
443 case RID_TRY: token->keyword = RID_AT_TRY; break;
444 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445 default: token->keyword = C_RID_CODE (token->value);
446 }
447 }
448 else if (token->type == CPP_PRAGMA)
449 {
450 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
451 token->pragma_kind = TREE_INT_CST_LOW (token->value);
452 token->value = NULL;
453 }
454 }
455
456 /* Update the globals input_location and in_system_header and the
457 input file stack from TOKEN. */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
460 {
461 if (token->type != CPP_EOF)
462 {
463 input_location = token->location;
464 in_system_header = token->in_system_header;
465 restore_input_file_stack (token->input_file_stack_index);
466 }
467 }
468
469 /* Return a pointer to the next token in the token stream, but do not
470 consume it. */
471
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
474 {
475 if (cp_lexer_debugging_p (lexer))
476 {
477 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479 putc ('\n', cp_lexer_debug_stream);
480 }
481 return lexer->next_token;
482 }
483
484 /* Return true if the next token has the indicated TYPE. */
485
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
488 {
489 return cp_lexer_peek_token (lexer)->type == type;
490 }
491
492 /* Return true if the next token does not have the indicated TYPE. */
493
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
496 {
497 return !cp_lexer_next_token_is (lexer, type);
498 }
499
500 /* Return true if the next token is the indicated KEYWORD. */
501
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
504 {
505 return cp_lexer_peek_token (lexer)->keyword == keyword;
506 }
507
508 /* Return true if the next token is a keyword for a decl-specifier. */
509
510 static bool
511 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
512 {
513 cp_token *token;
514
515 token = cp_lexer_peek_token (lexer);
516 switch (token->keyword)
517 {
518 /* Storage classes. */
519 case RID_AUTO:
520 case RID_REGISTER:
521 case RID_STATIC:
522 case RID_EXTERN:
523 case RID_MUTABLE:
524 case RID_THREAD:
525 /* Elaborated type specifiers. */
526 case RID_ENUM:
527 case RID_CLASS:
528 case RID_STRUCT:
529 case RID_UNION:
530 case RID_TYPENAME:
531 /* Simple type specifiers. */
532 case RID_CHAR:
533 case RID_WCHAR:
534 case RID_BOOL:
535 case RID_SHORT:
536 case RID_INT:
537 case RID_LONG:
538 case RID_SIGNED:
539 case RID_UNSIGNED:
540 case RID_FLOAT:
541 case RID_DOUBLE:
542 case RID_VOID:
543 /* GNU extensions. */
544 case RID_ATTRIBUTE:
545 case RID_TYPEOF:
546 return true;
547
548 default:
549 return false;
550 }
551 }
552
553 /* Return a pointer to the Nth token in the token stream. If N is 1,
554 then this is precisely equivalent to cp_lexer_peek_token (except
555 that it is not inline). One would like to disallow that case, but
556 there is one case (cp_parser_nth_token_starts_template_id) where
557 the caller passes a variable for N and it might be 1. */
558
559 static cp_token *
560 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
561 {
562 cp_token *token;
563
564 /* N is 1-based, not zero-based. */
565 gcc_assert (n > 0);
566
567 if (cp_lexer_debugging_p (lexer))
568 fprintf (cp_lexer_debug_stream,
569 "cp_lexer: peeking ahead %ld at token: ", (long)n);
570
571 --n;
572 token = lexer->next_token;
573 gcc_assert (!n || token != &eof_token);
574 while (n != 0)
575 {
576 ++token;
577 if (token == lexer->last_token)
578 {
579 token = (cp_token *)&eof_token;
580 break;
581 }
582
583 if (token->type != CPP_PURGED)
584 --n;
585 }
586
587 if (cp_lexer_debugging_p (lexer))
588 {
589 cp_lexer_print_token (cp_lexer_debug_stream, token);
590 putc ('\n', cp_lexer_debug_stream);
591 }
592
593 return token;
594 }
595
596 /* Return the next token, and advance the lexer's next_token pointer
597 to point to the next non-purged token. */
598
599 static cp_token *
600 cp_lexer_consume_token (cp_lexer* lexer)
601 {
602 cp_token *token = lexer->next_token;
603
604 gcc_assert (token != &eof_token);
605 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
606
607 do
608 {
609 lexer->next_token++;
610 if (lexer->next_token == lexer->last_token)
611 {
612 lexer->next_token = (cp_token *)&eof_token;
613 break;
614 }
615
616 }
617 while (lexer->next_token->type == CPP_PURGED);
618
619 cp_lexer_set_source_position_from_token (token);
620
621 /* Provide debugging output. */
622 if (cp_lexer_debugging_p (lexer))
623 {
624 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
625 cp_lexer_print_token (cp_lexer_debug_stream, token);
626 putc ('\n', cp_lexer_debug_stream);
627 }
628
629 return token;
630 }
631
632 /* Permanently remove the next token from the token stream, and
633 advance the next_token pointer to refer to the next non-purged
634 token. */
635
636 static void
637 cp_lexer_purge_token (cp_lexer *lexer)
638 {
639 cp_token *tok = lexer->next_token;
640
641 gcc_assert (tok != &eof_token);
642 tok->type = CPP_PURGED;
643 tok->location = UNKNOWN_LOCATION;
644 tok->value = NULL_TREE;
645 tok->keyword = RID_MAX;
646
647 do
648 {
649 tok++;
650 if (tok == lexer->last_token)
651 {
652 tok = (cp_token *)&eof_token;
653 break;
654 }
655 }
656 while (tok->type == CPP_PURGED);
657 lexer->next_token = tok;
658 }
659
660 /* Permanently remove all tokens after TOK, up to, but not
661 including, the token that will be returned next by
662 cp_lexer_peek_token. */
663
664 static void
665 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
666 {
667 cp_token *peek = lexer->next_token;
668
669 if (peek == &eof_token)
670 peek = lexer->last_token;
671
672 gcc_assert (tok < peek);
673
674 for ( tok += 1; tok != peek; tok += 1)
675 {
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->value = NULL_TREE;
679 tok->keyword = RID_MAX;
680 }
681 }
682
683 /* Begin saving tokens. All tokens consumed after this point will be
684 preserved. */
685
686 static void
687 cp_lexer_save_tokens (cp_lexer* lexer)
688 {
689 /* Provide debugging output. */
690 if (cp_lexer_debugging_p (lexer))
691 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
692
693 VEC_safe_push (cp_token_position, heap,
694 lexer->saved_tokens, lexer->next_token);
695 }
696
697 /* Commit to the portion of the token stream most recently saved. */
698
699 static void
700 cp_lexer_commit_tokens (cp_lexer* lexer)
701 {
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer))
704 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
705
706 VEC_pop (cp_token_position, lexer->saved_tokens);
707 }
708
709 /* Return all tokens saved since the last call to cp_lexer_save_tokens
710 to the token stream. Stop saving tokens. */
711
712 static void
713 cp_lexer_rollback_tokens (cp_lexer* lexer)
714 {
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer))
717 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
718
719 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
720 }
721
722 /* Print a representation of the TOKEN on the STREAM. */
723
724 #ifdef ENABLE_CHECKING
725
726 static void
727 cp_lexer_print_token (FILE * stream, cp_token *token)
728 {
729 /* We don't use cpp_type2name here because the parser defines
730 a few tokens of its own. */
731 static const char *const token_names[] = {
732 /* cpplib-defined token types */
733 #define OP(e, s) #e,
734 #define TK(e, s) #e,
735 TTYPE_TABLE
736 #undef OP
737 #undef TK
738 /* C++ parser token types - see "Manifest constants", above. */
739 "KEYWORD",
740 "TEMPLATE_ID",
741 "NESTED_NAME_SPECIFIER",
742 "PURGED"
743 };
744
745 /* If we have a name for the token, print it out. Otherwise, we
746 simply give the numeric code. */
747 gcc_assert (token->type < ARRAY_SIZE(token_names));
748 fputs (token_names[token->type], stream);
749
750 /* For some tokens, print the associated data. */
751 switch (token->type)
752 {
753 case CPP_KEYWORD:
754 /* Some keywords have a value that is not an IDENTIFIER_NODE.
755 For example, `struct' is mapped to an INTEGER_CST. */
756 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
757 break;
758 /* else fall through */
759 case CPP_NAME:
760 fputs (IDENTIFIER_POINTER (token->value), stream);
761 break;
762
763 case CPP_STRING:
764 case CPP_WSTRING:
765 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
766 break;
767
768 default:
769 break;
770 }
771 }
772
773 /* Start emitting debugging information. */
774
775 static void
776 cp_lexer_start_debugging (cp_lexer* lexer)
777 {
778 lexer->debugging_p = true;
779 }
780
781 /* Stop emitting debugging information. */
782
783 static void
784 cp_lexer_stop_debugging (cp_lexer* lexer)
785 {
786 lexer->debugging_p = false;
787 }
788
789 #endif /* ENABLE_CHECKING */
790
791 /* Create a new cp_token_cache, representing a range of tokens. */
792
793 static cp_token_cache *
794 cp_token_cache_new (cp_token *first, cp_token *last)
795 {
796 cp_token_cache *cache = GGC_NEW (cp_token_cache);
797 cache->first = first;
798 cache->last = last;
799 return cache;
800 }
801
802 \f
803 /* Decl-specifiers. */
804
805 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
806
807 static void
808 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
809 {
810 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
811 }
812
813 /* Declarators. */
814
815 /* Nothing other than the parser should be creating declarators;
816 declarators are a semi-syntactic representation of C++ entities.
817 Other parts of the front end that need to create entities (like
818 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
819
820 static cp_declarator *make_call_declarator
821 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
822 static cp_declarator *make_array_declarator
823 (cp_declarator *, tree);
824 static cp_declarator *make_pointer_declarator
825 (cp_cv_quals, cp_declarator *);
826 static cp_declarator *make_reference_declarator
827 (cp_cv_quals, cp_declarator *);
828 static cp_parameter_declarator *make_parameter_declarator
829 (cp_decl_specifier_seq *, cp_declarator *, tree);
830 static cp_declarator *make_ptrmem_declarator
831 (cp_cv_quals, tree, cp_declarator *);
832
833 /* An erroneous declarator. */
834 static cp_declarator *cp_error_declarator;
835
836 /* The obstack on which declarators and related data structures are
837 allocated. */
838 static struct obstack declarator_obstack;
839
840 /* Alloc BYTES from the declarator memory pool. */
841
842 static inline void *
843 alloc_declarator (size_t bytes)
844 {
845 return obstack_alloc (&declarator_obstack, bytes);
846 }
847
848 /* Allocate a declarator of the indicated KIND. Clear fields that are
849 common to all declarators. */
850
851 static cp_declarator *
852 make_declarator (cp_declarator_kind kind)
853 {
854 cp_declarator *declarator;
855
856 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
857 declarator->kind = kind;
858 declarator->attributes = NULL_TREE;
859 declarator->declarator = NULL;
860
861 return declarator;
862 }
863
864 /* Make a declarator for a generalized identifier. If
865 QUALIFYING_SCOPE is non-NULL, the identifier is
866 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
867 UNQUALIFIED_NAME. SFK indicates the kind of special function this
868 is, if any. */
869
870 static cp_declarator *
871 make_id_declarator (tree qualifying_scope, tree unqualified_name,
872 special_function_kind sfk)
873 {
874 cp_declarator *declarator;
875
876 /* It is valid to write:
877
878 class C { void f(); };
879 typedef C D;
880 void D::f();
881
882 The standard is not clear about whether `typedef const C D' is
883 legal; as of 2002-09-15 the committee is considering that
884 question. EDG 3.0 allows that syntax. Therefore, we do as
885 well. */
886 if (qualifying_scope && TYPE_P (qualifying_scope))
887 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
888
889 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
890 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
891 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
892
893 declarator = make_declarator (cdk_id);
894 declarator->u.id.qualifying_scope = qualifying_scope;
895 declarator->u.id.unqualified_name = unqualified_name;
896 declarator->u.id.sfk = sfk;
897
898 return declarator;
899 }
900
901 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
902 of modifiers such as const or volatile to apply to the pointer
903 type, represented as identifiers. */
904
905 cp_declarator *
906 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
907 {
908 cp_declarator *declarator;
909
910 declarator = make_declarator (cdk_pointer);
911 declarator->declarator = target;
912 declarator->u.pointer.qualifiers = cv_qualifiers;
913 declarator->u.pointer.class_type = NULL_TREE;
914
915 return declarator;
916 }
917
918 /* Like make_pointer_declarator -- but for references. */
919
920 cp_declarator *
921 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
922 {
923 cp_declarator *declarator;
924
925 declarator = make_declarator (cdk_reference);
926 declarator->declarator = target;
927 declarator->u.pointer.qualifiers = cv_qualifiers;
928 declarator->u.pointer.class_type = NULL_TREE;
929
930 return declarator;
931 }
932
933 /* Like make_pointer_declarator -- but for a pointer to a non-static
934 member of CLASS_TYPE. */
935
936 cp_declarator *
937 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
938 cp_declarator *pointee)
939 {
940 cp_declarator *declarator;
941
942 declarator = make_declarator (cdk_ptrmem);
943 declarator->declarator = pointee;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = class_type;
946
947 return declarator;
948 }
949
950 /* Make a declarator for the function given by TARGET, with the
951 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
952 "const"-qualified member function. The EXCEPTION_SPECIFICATION
953 indicates what exceptions can be thrown. */
954
955 cp_declarator *
956 make_call_declarator (cp_declarator *target,
957 cp_parameter_declarator *parms,
958 cp_cv_quals cv_qualifiers,
959 tree exception_specification)
960 {
961 cp_declarator *declarator;
962
963 declarator = make_declarator (cdk_function);
964 declarator->declarator = target;
965 declarator->u.function.parameters = parms;
966 declarator->u.function.qualifiers = cv_qualifiers;
967 declarator->u.function.exception_specification = exception_specification;
968
969 return declarator;
970 }
971
972 /* Make a declarator for an array of BOUNDS elements, each of which is
973 defined by ELEMENT. */
974
975 cp_declarator *
976 make_array_declarator (cp_declarator *element, tree bounds)
977 {
978 cp_declarator *declarator;
979
980 declarator = make_declarator (cdk_array);
981 declarator->declarator = element;
982 declarator->u.array.bounds = bounds;
983
984 return declarator;
985 }
986
987 cp_parameter_declarator *no_parameters;
988
989 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
990 DECLARATOR and DEFAULT_ARGUMENT. */
991
992 cp_parameter_declarator *
993 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
994 cp_declarator *declarator,
995 tree default_argument)
996 {
997 cp_parameter_declarator *parameter;
998
999 parameter = ((cp_parameter_declarator *)
1000 alloc_declarator (sizeof (cp_parameter_declarator)));
1001 parameter->next = NULL;
1002 if (decl_specifiers)
1003 parameter->decl_specifiers = *decl_specifiers;
1004 else
1005 clear_decl_specs (&parameter->decl_specifiers);
1006 parameter->declarator = declarator;
1007 parameter->default_argument = default_argument;
1008 parameter->ellipsis_p = false;
1009
1010 return parameter;
1011 }
1012
1013 /* Returns true iff DECLARATOR is a declaration for a function. */
1014
1015 static bool
1016 function_declarator_p (const cp_declarator *declarator)
1017 {
1018 while (declarator)
1019 {
1020 if (declarator->kind == cdk_function
1021 && declarator->declarator->kind == cdk_id)
1022 return true;
1023 if (declarator->kind == cdk_id
1024 || declarator->kind == cdk_error)
1025 return false;
1026 declarator = declarator->declarator;
1027 }
1028 return false;
1029 }
1030
1031 /* The parser. */
1032
1033 /* Overview
1034 --------
1035
1036 A cp_parser parses the token stream as specified by the C++
1037 grammar. Its job is purely parsing, not semantic analysis. For
1038 example, the parser breaks the token stream into declarators,
1039 expressions, statements, and other similar syntactic constructs.
1040 It does not check that the types of the expressions on either side
1041 of an assignment-statement are compatible, or that a function is
1042 not declared with a parameter of type `void'.
1043
1044 The parser invokes routines elsewhere in the compiler to perform
1045 semantic analysis and to build up the abstract syntax tree for the
1046 code processed.
1047
1048 The parser (and the template instantiation code, which is, in a
1049 way, a close relative of parsing) are the only parts of the
1050 compiler that should be calling push_scope and pop_scope, or
1051 related functions. The parser (and template instantiation code)
1052 keeps track of what scope is presently active; everything else
1053 should simply honor that. (The code that generates static
1054 initializers may also need to set the scope, in order to check
1055 access control correctly when emitting the initializers.)
1056
1057 Methodology
1058 -----------
1059
1060 The parser is of the standard recursive-descent variety. Upcoming
1061 tokens in the token stream are examined in order to determine which
1062 production to use when parsing a non-terminal. Some C++ constructs
1063 require arbitrary look ahead to disambiguate. For example, it is
1064 impossible, in the general case, to tell whether a statement is an
1065 expression or declaration without scanning the entire statement.
1066 Therefore, the parser is capable of "parsing tentatively." When the
1067 parser is not sure what construct comes next, it enters this mode.
1068 Then, while we attempt to parse the construct, the parser queues up
1069 error messages, rather than issuing them immediately, and saves the
1070 tokens it consumes. If the construct is parsed successfully, the
1071 parser "commits", i.e., it issues any queued error messages and
1072 the tokens that were being preserved are permanently discarded.
1073 If, however, the construct is not parsed successfully, the parser
1074 rolls back its state completely so that it can resume parsing using
1075 a different alternative.
1076
1077 Future Improvements
1078 -------------------
1079
1080 The performance of the parser could probably be improved substantially.
1081 We could often eliminate the need to parse tentatively by looking ahead
1082 a little bit. In some places, this approach might not entirely eliminate
1083 the need to parse tentatively, but it might still speed up the average
1084 case. */
1085
1086 /* Flags that are passed to some parsing functions. These values can
1087 be bitwise-ored together. */
1088
1089 typedef enum cp_parser_flags
1090 {
1091 /* No flags. */
1092 CP_PARSER_FLAGS_NONE = 0x0,
1093 /* The construct is optional. If it is not present, then no error
1094 should be issued. */
1095 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1096 /* When parsing a type-specifier, do not allow user-defined types. */
1097 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1098 } cp_parser_flags;
1099
1100 /* The different kinds of declarators we want to parse. */
1101
1102 typedef enum cp_parser_declarator_kind
1103 {
1104 /* We want an abstract declarator. */
1105 CP_PARSER_DECLARATOR_ABSTRACT,
1106 /* We want a named declarator. */
1107 CP_PARSER_DECLARATOR_NAMED,
1108 /* We don't mind, but the name must be an unqualified-id. */
1109 CP_PARSER_DECLARATOR_EITHER
1110 } cp_parser_declarator_kind;
1111
1112 /* The precedence values used to parse binary expressions. The minimum value
1113 of PREC must be 1, because zero is reserved to quickly discriminate
1114 binary operators from other tokens. */
1115
1116 enum cp_parser_prec
1117 {
1118 PREC_NOT_OPERATOR,
1119 PREC_LOGICAL_OR_EXPRESSION,
1120 PREC_LOGICAL_AND_EXPRESSION,
1121 PREC_INCLUSIVE_OR_EXPRESSION,
1122 PREC_EXCLUSIVE_OR_EXPRESSION,
1123 PREC_AND_EXPRESSION,
1124 PREC_EQUALITY_EXPRESSION,
1125 PREC_RELATIONAL_EXPRESSION,
1126 PREC_SHIFT_EXPRESSION,
1127 PREC_ADDITIVE_EXPRESSION,
1128 PREC_MULTIPLICATIVE_EXPRESSION,
1129 PREC_PM_EXPRESSION,
1130 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1131 };
1132
1133 /* A mapping from a token type to a corresponding tree node type, with a
1134 precedence value. */
1135
1136 typedef struct cp_parser_binary_operations_map_node
1137 {
1138 /* The token type. */
1139 enum cpp_ttype token_type;
1140 /* The corresponding tree code. */
1141 enum tree_code tree_type;
1142 /* The precedence of this operator. */
1143 enum cp_parser_prec prec;
1144 } cp_parser_binary_operations_map_node;
1145
1146 /* The status of a tentative parse. */
1147
1148 typedef enum cp_parser_status_kind
1149 {
1150 /* No errors have occurred. */
1151 CP_PARSER_STATUS_KIND_NO_ERROR,
1152 /* An error has occurred. */
1153 CP_PARSER_STATUS_KIND_ERROR,
1154 /* We are committed to this tentative parse, whether or not an error
1155 has occurred. */
1156 CP_PARSER_STATUS_KIND_COMMITTED
1157 } cp_parser_status_kind;
1158
1159 typedef struct cp_parser_expression_stack_entry
1160 {
1161 /* Left hand side of the binary operation we are currently
1162 parsing. */
1163 tree lhs;
1164 /* Original tree code for left hand side, if it was a binary
1165 expression itself (used for -Wparentheses). */
1166 enum tree_code lhs_type;
1167 /* Tree code for the binary operation we are parsing. */
1168 enum tree_code tree_type;
1169 /* Precedence of the binary operation we are parsing. */
1170 int prec;
1171 } cp_parser_expression_stack_entry;
1172
1173 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1174 entries because precedence levels on the stack are monotonically
1175 increasing. */
1176 typedef struct cp_parser_expression_stack_entry
1177 cp_parser_expression_stack[NUM_PREC_VALUES];
1178
1179 /* Context that is saved and restored when parsing tentatively. */
1180 typedef struct cp_parser_context GTY (())
1181 {
1182 /* If this is a tentative parsing context, the status of the
1183 tentative parse. */
1184 enum cp_parser_status_kind status;
1185 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1186 that are looked up in this context must be looked up both in the
1187 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1188 the context of the containing expression. */
1189 tree object_type;
1190
1191 /* The next parsing context in the stack. */
1192 struct cp_parser_context *next;
1193 } cp_parser_context;
1194
1195 /* Prototypes. */
1196
1197 /* Constructors and destructors. */
1198
1199 static cp_parser_context *cp_parser_context_new
1200 (cp_parser_context *);
1201
1202 /* Class variables. */
1203
1204 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1205
1206 /* The operator-precedence table used by cp_parser_binary_expression.
1207 Transformed into an associative array (binops_by_token) by
1208 cp_parser_new. */
1209
1210 static const cp_parser_binary_operations_map_node binops[] = {
1211 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1212 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1213
1214 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1215 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1216 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1217
1218 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1219 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1220
1221 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1222 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1223
1224 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1225 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1226 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1227 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1228
1229 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1230 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1231
1232 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1233
1234 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1235
1236 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1237
1238 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1239
1240 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1241 };
1242
1243 /* The same as binops, but initialized by cp_parser_new so that
1244 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1245 for speed. */
1246 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1247
1248 /* Constructors and destructors. */
1249
1250 /* Construct a new context. The context below this one on the stack
1251 is given by NEXT. */
1252
1253 static cp_parser_context *
1254 cp_parser_context_new (cp_parser_context* next)
1255 {
1256 cp_parser_context *context;
1257
1258 /* Allocate the storage. */
1259 if (cp_parser_context_free_list != NULL)
1260 {
1261 /* Pull the first entry from the free list. */
1262 context = cp_parser_context_free_list;
1263 cp_parser_context_free_list = context->next;
1264 memset (context, 0, sizeof (*context));
1265 }
1266 else
1267 context = GGC_CNEW (cp_parser_context);
1268
1269 /* No errors have occurred yet in this context. */
1270 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1271 /* If this is not the bottomost context, copy information that we
1272 need from the previous context. */
1273 if (next)
1274 {
1275 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1276 expression, then we are parsing one in this context, too. */
1277 context->object_type = next->object_type;
1278 /* Thread the stack. */
1279 context->next = next;
1280 }
1281
1282 return context;
1283 }
1284
1285 /* The cp_parser structure represents the C++ parser. */
1286
1287 typedef struct cp_parser GTY(())
1288 {
1289 /* The lexer from which we are obtaining tokens. */
1290 cp_lexer *lexer;
1291
1292 /* The scope in which names should be looked up. If NULL_TREE, then
1293 we look up names in the scope that is currently open in the
1294 source program. If non-NULL, this is either a TYPE or
1295 NAMESPACE_DECL for the scope in which we should look. It can
1296 also be ERROR_MARK, when we've parsed a bogus scope.
1297
1298 This value is not cleared automatically after a name is looked
1299 up, so we must be careful to clear it before starting a new look
1300 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1301 will look up `Z' in the scope of `X', rather than the current
1302 scope.) Unfortunately, it is difficult to tell when name lookup
1303 is complete, because we sometimes peek at a token, look it up,
1304 and then decide not to consume it. */
1305 tree scope;
1306
1307 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1308 last lookup took place. OBJECT_SCOPE is used if an expression
1309 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1310 respectively. QUALIFYING_SCOPE is used for an expression of the
1311 form "X::Y"; it refers to X. */
1312 tree object_scope;
1313 tree qualifying_scope;
1314
1315 /* A stack of parsing contexts. All but the bottom entry on the
1316 stack will be tentative contexts.
1317
1318 We parse tentatively in order to determine which construct is in
1319 use in some situations. For example, in order to determine
1320 whether a statement is an expression-statement or a
1321 declaration-statement we parse it tentatively as a
1322 declaration-statement. If that fails, we then reparse the same
1323 token stream as an expression-statement. */
1324 cp_parser_context *context;
1325
1326 /* True if we are parsing GNU C++. If this flag is not set, then
1327 GNU extensions are not recognized. */
1328 bool allow_gnu_extensions_p;
1329
1330 /* TRUE if the `>' token should be interpreted as the greater-than
1331 operator. FALSE if it is the end of a template-id or
1332 template-parameter-list. */
1333 bool greater_than_is_operator_p;
1334
1335 /* TRUE if default arguments are allowed within a parameter list
1336 that starts at this point. FALSE if only a gnu extension makes
1337 them permissible. */
1338 bool default_arg_ok_p;
1339
1340 /* TRUE if we are parsing an integral constant-expression. See
1341 [expr.const] for a precise definition. */
1342 bool integral_constant_expression_p;
1343
1344 /* TRUE if we are parsing an integral constant-expression -- but a
1345 non-constant expression should be permitted as well. This flag
1346 is used when parsing an array bound so that GNU variable-length
1347 arrays are tolerated. */
1348 bool allow_non_integral_constant_expression_p;
1349
1350 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1351 been seen that makes the expression non-constant. */
1352 bool non_integral_constant_expression_p;
1353
1354 /* TRUE if local variable names and `this' are forbidden in the
1355 current context. */
1356 bool local_variables_forbidden_p;
1357
1358 /* TRUE if the declaration we are parsing is part of a
1359 linkage-specification of the form `extern string-literal
1360 declaration'. */
1361 bool in_unbraced_linkage_specification_p;
1362
1363 /* TRUE if we are presently parsing a declarator, after the
1364 direct-declarator. */
1365 bool in_declarator_p;
1366
1367 /* TRUE if we are presently parsing a template-argument-list. */
1368 bool in_template_argument_list_p;
1369
1370 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1371 to IN_OMP_BLOCK if parsing OpenMP structured block and
1372 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1373 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1374 iteration-statement, OpenMP block or loop within that switch. */
1375 #define IN_SWITCH_STMT 1
1376 #define IN_ITERATION_STMT 2
1377 #define IN_OMP_BLOCK 4
1378 #define IN_OMP_FOR 8
1379 unsigned char in_statement;
1380
1381 /* TRUE if we are presently parsing the body of a switch statement.
1382 Note that this doesn't quite overlap with in_statement above.
1383 The difference relates to giving the right sets of error messages:
1384 "case not in switch" vs "break statement used with OpenMP...". */
1385 bool in_switch_statement_p;
1386
1387 /* TRUE if we are parsing a type-id in an expression context. In
1388 such a situation, both "type (expr)" and "type (type)" are valid
1389 alternatives. */
1390 bool in_type_id_in_expr_p;
1391
1392 /* TRUE if we are currently in a header file where declarations are
1393 implicitly extern "C". */
1394 bool implicit_extern_c;
1395
1396 /* TRUE if strings in expressions should be translated to the execution
1397 character set. */
1398 bool translate_strings_p;
1399
1400 /* TRUE if we are presently parsing the body of a function, but not
1401 a local class. */
1402 bool in_function_body;
1403
1404 /* If non-NULL, then we are parsing a construct where new type
1405 definitions are not permitted. The string stored here will be
1406 issued as an error message if a type is defined. */
1407 const char *type_definition_forbidden_message;
1408
1409 /* A list of lists. The outer list is a stack, used for member
1410 functions of local classes. At each level there are two sub-list,
1411 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1412 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1413 TREE_VALUE's. The functions are chained in reverse declaration
1414 order.
1415
1416 The TREE_PURPOSE sublist contains those functions with default
1417 arguments that need post processing, and the TREE_VALUE sublist
1418 contains those functions with definitions that need post
1419 processing.
1420
1421 These lists can only be processed once the outermost class being
1422 defined is complete. */
1423 tree unparsed_functions_queues;
1424
1425 /* The number of classes whose definitions are currently in
1426 progress. */
1427 unsigned num_classes_being_defined;
1428
1429 /* The number of template parameter lists that apply directly to the
1430 current declaration. */
1431 unsigned num_template_parameter_lists;
1432 } cp_parser;
1433
1434 /* Prototypes. */
1435
1436 /* Constructors and destructors. */
1437
1438 static cp_parser *cp_parser_new
1439 (void);
1440
1441 /* Routines to parse various constructs.
1442
1443 Those that return `tree' will return the error_mark_node (rather
1444 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1445 Sometimes, they will return an ordinary node if error-recovery was
1446 attempted, even though a parse error occurred. So, to check
1447 whether or not a parse error occurred, you should always use
1448 cp_parser_error_occurred. If the construct is optional (indicated
1449 either by an `_opt' in the name of the function that does the
1450 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1451 the construct is not present. */
1452
1453 /* Lexical conventions [gram.lex] */
1454
1455 static tree cp_parser_identifier
1456 (cp_parser *);
1457 static tree cp_parser_string_literal
1458 (cp_parser *, bool, bool);
1459
1460 /* Basic concepts [gram.basic] */
1461
1462 static bool cp_parser_translation_unit
1463 (cp_parser *);
1464
1465 /* Expressions [gram.expr] */
1466
1467 static tree cp_parser_primary_expression
1468 (cp_parser *, bool, bool, bool, cp_id_kind *);
1469 static tree cp_parser_id_expression
1470 (cp_parser *, bool, bool, bool *, bool, bool);
1471 static tree cp_parser_unqualified_id
1472 (cp_parser *, bool, bool, bool, bool);
1473 static tree cp_parser_nested_name_specifier_opt
1474 (cp_parser *, bool, bool, bool, bool);
1475 static tree cp_parser_nested_name_specifier
1476 (cp_parser *, bool, bool, bool, bool);
1477 static tree cp_parser_class_or_namespace_name
1478 (cp_parser *, bool, bool, bool, bool, bool);
1479 static tree cp_parser_postfix_expression
1480 (cp_parser *, bool, bool);
1481 static tree cp_parser_postfix_open_square_expression
1482 (cp_parser *, tree, bool);
1483 static tree cp_parser_postfix_dot_deref_expression
1484 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1485 static tree cp_parser_parenthesized_expression_list
1486 (cp_parser *, bool, bool, bool *);
1487 static void cp_parser_pseudo_destructor_name
1488 (cp_parser *, tree *, tree *);
1489 static tree cp_parser_unary_expression
1490 (cp_parser *, bool, bool);
1491 static enum tree_code cp_parser_unary_operator
1492 (cp_token *);
1493 static tree cp_parser_new_expression
1494 (cp_parser *);
1495 static tree cp_parser_new_placement
1496 (cp_parser *);
1497 static tree cp_parser_new_type_id
1498 (cp_parser *, tree *);
1499 static cp_declarator *cp_parser_new_declarator_opt
1500 (cp_parser *);
1501 static cp_declarator *cp_parser_direct_new_declarator
1502 (cp_parser *);
1503 static tree cp_parser_new_initializer
1504 (cp_parser *);
1505 static tree cp_parser_delete_expression
1506 (cp_parser *);
1507 static tree cp_parser_cast_expression
1508 (cp_parser *, bool, bool);
1509 static tree cp_parser_binary_expression
1510 (cp_parser *, bool);
1511 static tree cp_parser_question_colon_clause
1512 (cp_parser *, tree);
1513 static tree cp_parser_assignment_expression
1514 (cp_parser *, bool);
1515 static enum tree_code cp_parser_assignment_operator_opt
1516 (cp_parser *);
1517 static tree cp_parser_expression
1518 (cp_parser *, bool);
1519 static tree cp_parser_constant_expression
1520 (cp_parser *, bool, bool *);
1521 static tree cp_parser_builtin_offsetof
1522 (cp_parser *);
1523
1524 /* Statements [gram.stmt.stmt] */
1525
1526 static void cp_parser_statement
1527 (cp_parser *, tree, bool, bool *);
1528 static void cp_parser_label_for_labeled_statement
1529 (cp_parser *);
1530 static tree cp_parser_expression_statement
1531 (cp_parser *, tree);
1532 static tree cp_parser_compound_statement
1533 (cp_parser *, tree, bool);
1534 static void cp_parser_statement_seq_opt
1535 (cp_parser *, tree);
1536 static tree cp_parser_selection_statement
1537 (cp_parser *, bool *);
1538 static tree cp_parser_condition
1539 (cp_parser *);
1540 static tree cp_parser_iteration_statement
1541 (cp_parser *);
1542 static void cp_parser_for_init_statement
1543 (cp_parser *);
1544 static tree cp_parser_jump_statement
1545 (cp_parser *);
1546 static void cp_parser_declaration_statement
1547 (cp_parser *);
1548
1549 static tree cp_parser_implicitly_scoped_statement
1550 (cp_parser *, bool *);
1551 static void cp_parser_already_scoped_statement
1552 (cp_parser *);
1553
1554 /* Declarations [gram.dcl.dcl] */
1555
1556 static void cp_parser_declaration_seq_opt
1557 (cp_parser *);
1558 static void cp_parser_declaration
1559 (cp_parser *);
1560 static void cp_parser_block_declaration
1561 (cp_parser *, bool);
1562 static void cp_parser_simple_declaration
1563 (cp_parser *, bool);
1564 static void cp_parser_decl_specifier_seq
1565 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1566 static tree cp_parser_storage_class_specifier_opt
1567 (cp_parser *);
1568 static tree cp_parser_function_specifier_opt
1569 (cp_parser *, cp_decl_specifier_seq *);
1570 static tree cp_parser_type_specifier
1571 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1572 int *, bool *);
1573 static tree cp_parser_simple_type_specifier
1574 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1575 static tree cp_parser_type_name
1576 (cp_parser *);
1577 static tree cp_parser_elaborated_type_specifier
1578 (cp_parser *, bool, bool);
1579 static tree cp_parser_enum_specifier
1580 (cp_parser *);
1581 static void cp_parser_enumerator_list
1582 (cp_parser *, tree);
1583 static void cp_parser_enumerator_definition
1584 (cp_parser *, tree);
1585 static tree cp_parser_namespace_name
1586 (cp_parser *);
1587 static void cp_parser_namespace_definition
1588 (cp_parser *);
1589 static void cp_parser_namespace_body
1590 (cp_parser *);
1591 static tree cp_parser_qualified_namespace_specifier
1592 (cp_parser *);
1593 static void cp_parser_namespace_alias_definition
1594 (cp_parser *);
1595 static bool cp_parser_using_declaration
1596 (cp_parser *, bool);
1597 static void cp_parser_using_directive
1598 (cp_parser *);
1599 static void cp_parser_asm_definition
1600 (cp_parser *);
1601 static void cp_parser_linkage_specification
1602 (cp_parser *);
1603 static void cp_parser_static_assert
1604 (cp_parser *, bool);
1605
1606 /* Declarators [gram.dcl.decl] */
1607
1608 static tree cp_parser_init_declarator
1609 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1610 static cp_declarator *cp_parser_declarator
1611 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1612 static cp_declarator *cp_parser_direct_declarator
1613 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1614 static enum tree_code cp_parser_ptr_operator
1615 (cp_parser *, tree *, cp_cv_quals *);
1616 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1617 (cp_parser *);
1618 static tree cp_parser_declarator_id
1619 (cp_parser *, bool);
1620 static tree cp_parser_type_id
1621 (cp_parser *);
1622 static void cp_parser_type_specifier_seq
1623 (cp_parser *, bool, cp_decl_specifier_seq *);
1624 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1625 (cp_parser *);
1626 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1627 (cp_parser *, bool *);
1628 static cp_parameter_declarator *cp_parser_parameter_declaration
1629 (cp_parser *, bool, bool *);
1630 static void cp_parser_function_body
1631 (cp_parser *);
1632 static tree cp_parser_initializer
1633 (cp_parser *, bool *, bool *);
1634 static tree cp_parser_initializer_clause
1635 (cp_parser *, bool *);
1636 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1637 (cp_parser *, bool *);
1638
1639 static bool cp_parser_ctor_initializer_opt_and_function_body
1640 (cp_parser *);
1641
1642 /* Classes [gram.class] */
1643
1644 static tree cp_parser_class_name
1645 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1646 static tree cp_parser_class_specifier
1647 (cp_parser *);
1648 static tree cp_parser_class_head
1649 (cp_parser *, bool *, tree *, tree *);
1650 static enum tag_types cp_parser_class_key
1651 (cp_parser *);
1652 static void cp_parser_member_specification_opt
1653 (cp_parser *);
1654 static void cp_parser_member_declaration
1655 (cp_parser *);
1656 static tree cp_parser_pure_specifier
1657 (cp_parser *);
1658 static tree cp_parser_constant_initializer
1659 (cp_parser *);
1660
1661 /* Derived classes [gram.class.derived] */
1662
1663 static tree cp_parser_base_clause
1664 (cp_parser *);
1665 static tree cp_parser_base_specifier
1666 (cp_parser *);
1667
1668 /* Special member functions [gram.special] */
1669
1670 static tree cp_parser_conversion_function_id
1671 (cp_parser *);
1672 static tree cp_parser_conversion_type_id
1673 (cp_parser *);
1674 static cp_declarator *cp_parser_conversion_declarator_opt
1675 (cp_parser *);
1676 static bool cp_parser_ctor_initializer_opt
1677 (cp_parser *);
1678 static void cp_parser_mem_initializer_list
1679 (cp_parser *);
1680 static tree cp_parser_mem_initializer
1681 (cp_parser *);
1682 static tree cp_parser_mem_initializer_id
1683 (cp_parser *);
1684
1685 /* Overloading [gram.over] */
1686
1687 static tree cp_parser_operator_function_id
1688 (cp_parser *);
1689 static tree cp_parser_operator
1690 (cp_parser *);
1691
1692 /* Templates [gram.temp] */
1693
1694 static void cp_parser_template_declaration
1695 (cp_parser *, bool);
1696 static tree cp_parser_template_parameter_list
1697 (cp_parser *);
1698 static tree cp_parser_template_parameter
1699 (cp_parser *, bool *);
1700 static tree cp_parser_type_parameter
1701 (cp_parser *);
1702 static tree cp_parser_template_id
1703 (cp_parser *, bool, bool, bool);
1704 static tree cp_parser_template_name
1705 (cp_parser *, bool, bool, bool, bool *);
1706 static tree cp_parser_template_argument_list
1707 (cp_parser *);
1708 static tree cp_parser_template_argument
1709 (cp_parser *);
1710 static void cp_parser_explicit_instantiation
1711 (cp_parser *);
1712 static void cp_parser_explicit_specialization
1713 (cp_parser *);
1714
1715 /* Exception handling [gram.exception] */
1716
1717 static tree cp_parser_try_block
1718 (cp_parser *);
1719 static bool cp_parser_function_try_block
1720 (cp_parser *);
1721 static void cp_parser_handler_seq
1722 (cp_parser *);
1723 static void cp_parser_handler
1724 (cp_parser *);
1725 static tree cp_parser_exception_declaration
1726 (cp_parser *);
1727 static tree cp_parser_throw_expression
1728 (cp_parser *);
1729 static tree cp_parser_exception_specification_opt
1730 (cp_parser *);
1731 static tree cp_parser_type_id_list
1732 (cp_parser *);
1733
1734 /* GNU Extensions */
1735
1736 static tree cp_parser_asm_specification_opt
1737 (cp_parser *);
1738 static tree cp_parser_asm_operand_list
1739 (cp_parser *);
1740 static tree cp_parser_asm_clobber_list
1741 (cp_parser *);
1742 static tree cp_parser_attributes_opt
1743 (cp_parser *);
1744 static tree cp_parser_attribute_list
1745 (cp_parser *);
1746 static bool cp_parser_extension_opt
1747 (cp_parser *, int *);
1748 static void cp_parser_label_declaration
1749 (cp_parser *);
1750
1751 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1752 static bool cp_parser_pragma
1753 (cp_parser *, enum pragma_context);
1754
1755 /* Objective-C++ Productions */
1756
1757 static tree cp_parser_objc_message_receiver
1758 (cp_parser *);
1759 static tree cp_parser_objc_message_args
1760 (cp_parser *);
1761 static tree cp_parser_objc_message_expression
1762 (cp_parser *);
1763 static tree cp_parser_objc_encode_expression
1764 (cp_parser *);
1765 static tree cp_parser_objc_defs_expression
1766 (cp_parser *);
1767 static tree cp_parser_objc_protocol_expression
1768 (cp_parser *);
1769 static tree cp_parser_objc_selector_expression
1770 (cp_parser *);
1771 static tree cp_parser_objc_expression
1772 (cp_parser *);
1773 static bool cp_parser_objc_selector_p
1774 (enum cpp_ttype);
1775 static tree cp_parser_objc_selector
1776 (cp_parser *);
1777 static tree cp_parser_objc_protocol_refs_opt
1778 (cp_parser *);
1779 static void cp_parser_objc_declaration
1780 (cp_parser *);
1781 static tree cp_parser_objc_statement
1782 (cp_parser *);
1783
1784 /* Utility Routines */
1785
1786 static tree cp_parser_lookup_name
1787 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1788 static tree cp_parser_lookup_name_simple
1789 (cp_parser *, tree);
1790 static tree cp_parser_maybe_treat_template_as_class
1791 (tree, bool);
1792 static bool cp_parser_check_declarator_template_parameters
1793 (cp_parser *, cp_declarator *);
1794 static bool cp_parser_check_template_parameters
1795 (cp_parser *, unsigned);
1796 static tree cp_parser_simple_cast_expression
1797 (cp_parser *);
1798 static tree cp_parser_global_scope_opt
1799 (cp_parser *, bool);
1800 static bool cp_parser_constructor_declarator_p
1801 (cp_parser *, bool);
1802 static tree cp_parser_function_definition_from_specifiers_and_declarator
1803 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1804 static tree cp_parser_function_definition_after_declarator
1805 (cp_parser *, bool);
1806 static void cp_parser_template_declaration_after_export
1807 (cp_parser *, bool);
1808 static void cp_parser_perform_template_parameter_access_checks
1809 (tree);
1810 static tree cp_parser_single_declaration
1811 (cp_parser *, tree, bool, bool *);
1812 static tree cp_parser_functional_cast
1813 (cp_parser *, tree);
1814 static tree cp_parser_save_member_function_body
1815 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1816 static tree cp_parser_enclosed_template_argument_list
1817 (cp_parser *);
1818 static void cp_parser_save_default_args
1819 (cp_parser *, tree);
1820 static void cp_parser_late_parsing_for_member
1821 (cp_parser *, tree);
1822 static void cp_parser_late_parsing_default_args
1823 (cp_parser *, tree);
1824 static tree cp_parser_sizeof_operand
1825 (cp_parser *, enum rid);
1826 static bool cp_parser_declares_only_class_p
1827 (cp_parser *);
1828 static void cp_parser_set_storage_class
1829 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1830 static void cp_parser_set_decl_spec_type
1831 (cp_decl_specifier_seq *, tree, bool);
1832 static bool cp_parser_friend_p
1833 (const cp_decl_specifier_seq *);
1834 static cp_token *cp_parser_require
1835 (cp_parser *, enum cpp_ttype, const char *);
1836 static cp_token *cp_parser_require_keyword
1837 (cp_parser *, enum rid, const char *);
1838 static bool cp_parser_token_starts_function_definition_p
1839 (cp_token *);
1840 static bool cp_parser_next_token_starts_class_definition_p
1841 (cp_parser *);
1842 static bool cp_parser_next_token_ends_template_argument_p
1843 (cp_parser *);
1844 static bool cp_parser_nth_token_starts_template_argument_list_p
1845 (cp_parser *, size_t);
1846 static enum tag_types cp_parser_token_is_class_key
1847 (cp_token *);
1848 static void cp_parser_check_class_key
1849 (enum tag_types, tree type);
1850 static void cp_parser_check_access_in_redeclaration
1851 (tree type);
1852 static bool cp_parser_optional_template_keyword
1853 (cp_parser *);
1854 static void cp_parser_pre_parsed_nested_name_specifier
1855 (cp_parser *);
1856 static void cp_parser_cache_group
1857 (cp_parser *, enum cpp_ttype, unsigned);
1858 static void cp_parser_parse_tentatively
1859 (cp_parser *);
1860 static void cp_parser_commit_to_tentative_parse
1861 (cp_parser *);
1862 static void cp_parser_abort_tentative_parse
1863 (cp_parser *);
1864 static bool cp_parser_parse_definitely
1865 (cp_parser *);
1866 static inline bool cp_parser_parsing_tentatively
1867 (cp_parser *);
1868 static bool cp_parser_uncommitted_to_tentative_parse_p
1869 (cp_parser *);
1870 static void cp_parser_error
1871 (cp_parser *, const char *);
1872 static void cp_parser_name_lookup_error
1873 (cp_parser *, tree, tree, const char *);
1874 static bool cp_parser_simulate_error
1875 (cp_parser *);
1876 static bool cp_parser_check_type_definition
1877 (cp_parser *);
1878 static void cp_parser_check_for_definition_in_return_type
1879 (cp_declarator *, tree);
1880 static void cp_parser_check_for_invalid_template_id
1881 (cp_parser *, tree);
1882 static bool cp_parser_non_integral_constant_expression
1883 (cp_parser *, const char *);
1884 static void cp_parser_diagnose_invalid_type_name
1885 (cp_parser *, tree, tree);
1886 static bool cp_parser_parse_and_diagnose_invalid_type_name
1887 (cp_parser *);
1888 static int cp_parser_skip_to_closing_parenthesis
1889 (cp_parser *, bool, bool, bool);
1890 static void cp_parser_skip_to_end_of_statement
1891 (cp_parser *);
1892 static void cp_parser_consume_semicolon_at_end_of_statement
1893 (cp_parser *);
1894 static void cp_parser_skip_to_end_of_block_or_statement
1895 (cp_parser *);
1896 static void cp_parser_skip_to_closing_brace
1897 (cp_parser *);
1898 static void cp_parser_skip_to_end_of_template_parameter_list
1899 (cp_parser *);
1900 static void cp_parser_skip_to_pragma_eol
1901 (cp_parser*, cp_token *);
1902 static bool cp_parser_error_occurred
1903 (cp_parser *);
1904 static bool cp_parser_allow_gnu_extensions_p
1905 (cp_parser *);
1906 static bool cp_parser_is_string_literal
1907 (cp_token *);
1908 static bool cp_parser_is_keyword
1909 (cp_token *, enum rid);
1910 static tree cp_parser_make_typename_type
1911 (cp_parser *, tree, tree);
1912
1913 /* Returns nonzero if we are parsing tentatively. */
1914
1915 static inline bool
1916 cp_parser_parsing_tentatively (cp_parser* parser)
1917 {
1918 return parser->context->next != NULL;
1919 }
1920
1921 /* Returns nonzero if TOKEN is a string literal. */
1922
1923 static bool
1924 cp_parser_is_string_literal (cp_token* token)
1925 {
1926 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1927 }
1928
1929 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1930
1931 static bool
1932 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1933 {
1934 return token->keyword == keyword;
1935 }
1936
1937 /* If not parsing tentatively, issue a diagnostic of the form
1938 FILE:LINE: MESSAGE before TOKEN
1939 where TOKEN is the next token in the input stream. MESSAGE
1940 (specified by the caller) is usually of the form "expected
1941 OTHER-TOKEN". */
1942
1943 static void
1944 cp_parser_error (cp_parser* parser, const char* message)
1945 {
1946 if (!cp_parser_simulate_error (parser))
1947 {
1948 cp_token *token = cp_lexer_peek_token (parser->lexer);
1949 /* This diagnostic makes more sense if it is tagged to the line
1950 of the token we just peeked at. */
1951 cp_lexer_set_source_position_from_token (token);
1952
1953 if (token->type == CPP_PRAGMA)
1954 {
1955 error ("%<#pragma%> is not allowed here");
1956 cp_parser_skip_to_pragma_eol (parser, token);
1957 return;
1958 }
1959
1960 c_parse_error (message,
1961 /* Because c_parser_error does not understand
1962 CPP_KEYWORD, keywords are treated like
1963 identifiers. */
1964 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1965 token->value);
1966 }
1967 }
1968
1969 /* Issue an error about name-lookup failing. NAME is the
1970 IDENTIFIER_NODE DECL is the result of
1971 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1972 the thing that we hoped to find. */
1973
1974 static void
1975 cp_parser_name_lookup_error (cp_parser* parser,
1976 tree name,
1977 tree decl,
1978 const char* desired)
1979 {
1980 /* If name lookup completely failed, tell the user that NAME was not
1981 declared. */
1982 if (decl == error_mark_node)
1983 {
1984 if (parser->scope && parser->scope != global_namespace)
1985 error ("%<%D::%D%> has not been declared",
1986 parser->scope, name);
1987 else if (parser->scope == global_namespace)
1988 error ("%<::%D%> has not been declared", name);
1989 else if (parser->object_scope
1990 && !CLASS_TYPE_P (parser->object_scope))
1991 error ("request for member %qD in non-class type %qT",
1992 name, parser->object_scope);
1993 else if (parser->object_scope)
1994 error ("%<%T::%D%> has not been declared",
1995 parser->object_scope, name);
1996 else
1997 error ("%qD has not been declared", name);
1998 }
1999 else if (parser->scope && parser->scope != global_namespace)
2000 error ("%<%D::%D%> %s", parser->scope, name, desired);
2001 else if (parser->scope == global_namespace)
2002 error ("%<::%D%> %s", name, desired);
2003 else
2004 error ("%qD %s", name, desired);
2005 }
2006
2007 /* If we are parsing tentatively, remember that an error has occurred
2008 during this tentative parse. Returns true if the error was
2009 simulated; false if a message should be issued by the caller. */
2010
2011 static bool
2012 cp_parser_simulate_error (cp_parser* parser)
2013 {
2014 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2015 {
2016 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2017 return true;
2018 }
2019 return false;
2020 }
2021
2022 /* Check for repeated decl-specifiers. */
2023
2024 static void
2025 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2026 {
2027 cp_decl_spec ds;
2028
2029 for (ds = ds_first; ds != ds_last; ++ds)
2030 {
2031 unsigned count = decl_specs->specs[(int)ds];
2032 if (count < 2)
2033 continue;
2034 /* The "long" specifier is a special case because of "long long". */
2035 if (ds == ds_long)
2036 {
2037 if (count > 2)
2038 error ("%<long long long%> is too long for GCC");
2039 else if (pedantic && !in_system_header && warn_long_long)
2040 pedwarn ("ISO C++ does not support %<long long%>");
2041 }
2042 else if (count > 1)
2043 {
2044 static const char *const decl_spec_names[] = {
2045 "signed",
2046 "unsigned",
2047 "short",
2048 "long",
2049 "const",
2050 "volatile",
2051 "restrict",
2052 "inline",
2053 "virtual",
2054 "explicit",
2055 "friend",
2056 "typedef",
2057 "__complex",
2058 "__thread"
2059 };
2060 error ("duplicate %qs", decl_spec_names[(int)ds]);
2061 }
2062 }
2063 }
2064
2065 /* This function is called when a type is defined. If type
2066 definitions are forbidden at this point, an error message is
2067 issued. */
2068
2069 static bool
2070 cp_parser_check_type_definition (cp_parser* parser)
2071 {
2072 /* If types are forbidden here, issue a message. */
2073 if (parser->type_definition_forbidden_message)
2074 {
2075 /* Use `%s' to print the string in case there are any escape
2076 characters in the message. */
2077 error ("%s", parser->type_definition_forbidden_message);
2078 return false;
2079 }
2080 return true;
2081 }
2082
2083 /* This function is called when the DECLARATOR is processed. The TYPE
2084 was a type defined in the decl-specifiers. If it is invalid to
2085 define a type in the decl-specifiers for DECLARATOR, an error is
2086 issued. */
2087
2088 static void
2089 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2090 tree type)
2091 {
2092 /* [dcl.fct] forbids type definitions in return types.
2093 Unfortunately, it's not easy to know whether or not we are
2094 processing a return type until after the fact. */
2095 while (declarator
2096 && (declarator->kind == cdk_pointer
2097 || declarator->kind == cdk_reference
2098 || declarator->kind == cdk_ptrmem))
2099 declarator = declarator->declarator;
2100 if (declarator
2101 && declarator->kind == cdk_function)
2102 {
2103 error ("new types may not be defined in a return type");
2104 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2105 type);
2106 }
2107 }
2108
2109 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2110 "<" in any valid C++ program. If the next token is indeed "<",
2111 issue a message warning the user about what appears to be an
2112 invalid attempt to form a template-id. */
2113
2114 static void
2115 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2116 tree type)
2117 {
2118 cp_token_position start = 0;
2119
2120 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2121 {
2122 if (TYPE_P (type))
2123 error ("%qT is not a template", type);
2124 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2125 error ("%qE is not a template", type);
2126 else
2127 error ("invalid template-id");
2128 /* Remember the location of the invalid "<". */
2129 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2130 start = cp_lexer_token_position (parser->lexer, true);
2131 /* Consume the "<". */
2132 cp_lexer_consume_token (parser->lexer);
2133 /* Parse the template arguments. */
2134 cp_parser_enclosed_template_argument_list (parser);
2135 /* Permanently remove the invalid template arguments so that
2136 this error message is not issued again. */
2137 if (start)
2138 cp_lexer_purge_tokens_after (parser->lexer, start);
2139 }
2140 }
2141
2142 /* If parsing an integral constant-expression, issue an error message
2143 about the fact that THING appeared and return true. Otherwise,
2144 return false. In either case, set
2145 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2146
2147 static bool
2148 cp_parser_non_integral_constant_expression (cp_parser *parser,
2149 const char *thing)
2150 {
2151 parser->non_integral_constant_expression_p = true;
2152 if (parser->integral_constant_expression_p)
2153 {
2154 if (!parser->allow_non_integral_constant_expression_p)
2155 {
2156 error ("%s cannot appear in a constant-expression", thing);
2157 return true;
2158 }
2159 }
2160 return false;
2161 }
2162
2163 /* Emit a diagnostic for an invalid type name. SCOPE is the
2164 qualifying scope (or NULL, if none) for ID. This function commits
2165 to the current active tentative parse, if any. (Otherwise, the
2166 problematic construct might be encountered again later, resulting
2167 in duplicate error messages.) */
2168
2169 static void
2170 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2171 {
2172 tree decl, old_scope;
2173 /* Try to lookup the identifier. */
2174 old_scope = parser->scope;
2175 parser->scope = scope;
2176 decl = cp_parser_lookup_name_simple (parser, id);
2177 parser->scope = old_scope;
2178 /* If the lookup found a template-name, it means that the user forgot
2179 to specify an argument list. Emit a useful error message. */
2180 if (TREE_CODE (decl) == TEMPLATE_DECL)
2181 error ("invalid use of template-name %qE without an argument list", decl);
2182 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2183 error ("invalid use of destructor %qD as a type", id);
2184 else if (TREE_CODE (decl) == TYPE_DECL)
2185 /* Something like 'unsigned A a;' */
2186 error ("invalid combination of multiple type-specifiers");
2187 else if (!parser->scope)
2188 {
2189 /* Issue an error message. */
2190 error ("%qE does not name a type", id);
2191 /* If we're in a template class, it's possible that the user was
2192 referring to a type from a base class. For example:
2193
2194 template <typename T> struct A { typedef T X; };
2195 template <typename T> struct B : public A<T> { X x; };
2196
2197 The user should have said "typename A<T>::X". */
2198 if (processing_template_decl && current_class_type
2199 && TYPE_BINFO (current_class_type))
2200 {
2201 tree b;
2202
2203 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2204 b;
2205 b = TREE_CHAIN (b))
2206 {
2207 tree base_type = BINFO_TYPE (b);
2208 if (CLASS_TYPE_P (base_type)
2209 && dependent_type_p (base_type))
2210 {
2211 tree field;
2212 /* Go from a particular instantiation of the
2213 template (which will have an empty TYPE_FIELDs),
2214 to the main version. */
2215 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2216 for (field = TYPE_FIELDS (base_type);
2217 field;
2218 field = TREE_CHAIN (field))
2219 if (TREE_CODE (field) == TYPE_DECL
2220 && DECL_NAME (field) == id)
2221 {
2222 inform ("(perhaps %<typename %T::%E%> was intended)",
2223 BINFO_TYPE (b), id);
2224 break;
2225 }
2226 if (field)
2227 break;
2228 }
2229 }
2230 }
2231 }
2232 /* Here we diagnose qualified-ids where the scope is actually correct,
2233 but the identifier does not resolve to a valid type name. */
2234 else if (parser->scope != error_mark_node)
2235 {
2236 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2237 error ("%qE in namespace %qE does not name a type",
2238 id, parser->scope);
2239 else if (TYPE_P (parser->scope))
2240 error ("%qE in class %qT does not name a type", id, parser->scope);
2241 else
2242 gcc_unreachable ();
2243 }
2244 cp_parser_commit_to_tentative_parse (parser);
2245 }
2246
2247 /* Check for a common situation where a type-name should be present,
2248 but is not, and issue a sensible error message. Returns true if an
2249 invalid type-name was detected.
2250
2251 The situation handled by this function are variable declarations of the
2252 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2253 Usually, `ID' should name a type, but if we got here it means that it
2254 does not. We try to emit the best possible error message depending on
2255 how exactly the id-expression looks like. */
2256
2257 static bool
2258 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2259 {
2260 tree id;
2261
2262 cp_parser_parse_tentatively (parser);
2263 id = cp_parser_id_expression (parser,
2264 /*template_keyword_p=*/false,
2265 /*check_dependency_p=*/true,
2266 /*template_p=*/NULL,
2267 /*declarator_p=*/true,
2268 /*optional_p=*/false);
2269 /* After the id-expression, there should be a plain identifier,
2270 otherwise this is not a simple variable declaration. Also, if
2271 the scope is dependent, we cannot do much. */
2272 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2273 || (parser->scope && TYPE_P (parser->scope)
2274 && dependent_type_p (parser->scope)))
2275 {
2276 cp_parser_abort_tentative_parse (parser);
2277 return false;
2278 }
2279 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2280 return false;
2281
2282 /* Emit a diagnostic for the invalid type. */
2283 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2284 /* Skip to the end of the declaration; there's no point in
2285 trying to process it. */
2286 cp_parser_skip_to_end_of_block_or_statement (parser);
2287 return true;
2288 }
2289
2290 /* Consume tokens up to, and including, the next non-nested closing `)'.
2291 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2292 are doing error recovery. Returns -1 if OR_COMMA is true and we
2293 found an unnested comma. */
2294
2295 static int
2296 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2297 bool recovering,
2298 bool or_comma,
2299 bool consume_paren)
2300 {
2301 unsigned paren_depth = 0;
2302 unsigned brace_depth = 0;
2303
2304 if (recovering && !or_comma
2305 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2306 return 0;
2307
2308 while (true)
2309 {
2310 cp_token * token = cp_lexer_peek_token (parser->lexer);
2311
2312 switch (token->type)
2313 {
2314 case CPP_EOF:
2315 case CPP_PRAGMA_EOL:
2316 /* If we've run out of tokens, then there is no closing `)'. */
2317 return 0;
2318
2319 case CPP_SEMICOLON:
2320 /* This matches the processing in skip_to_end_of_statement. */
2321 if (!brace_depth)
2322 return 0;
2323 break;
2324
2325 case CPP_OPEN_BRACE:
2326 ++brace_depth;
2327 break;
2328 case CPP_CLOSE_BRACE:
2329 if (!brace_depth--)
2330 return 0;
2331 break;
2332
2333 case CPP_COMMA:
2334 if (recovering && or_comma && !brace_depth && !paren_depth)
2335 return -1;
2336 break;
2337
2338 case CPP_OPEN_PAREN:
2339 if (!brace_depth)
2340 ++paren_depth;
2341 break;
2342
2343 case CPP_CLOSE_PAREN:
2344 if (!brace_depth && !paren_depth--)
2345 {
2346 if (consume_paren)
2347 cp_lexer_consume_token (parser->lexer);
2348 return 1;
2349 }
2350 break;
2351
2352 default:
2353 break;
2354 }
2355
2356 /* Consume the token. */
2357 cp_lexer_consume_token (parser->lexer);
2358 }
2359 }
2360
2361 /* Consume tokens until we reach the end of the current statement.
2362 Normally, that will be just before consuming a `;'. However, if a
2363 non-nested `}' comes first, then we stop before consuming that. */
2364
2365 static void
2366 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2367 {
2368 unsigned nesting_depth = 0;
2369
2370 while (true)
2371 {
2372 cp_token *token = cp_lexer_peek_token (parser->lexer);
2373
2374 switch (token->type)
2375 {
2376 case CPP_EOF:
2377 case CPP_PRAGMA_EOL:
2378 /* If we've run out of tokens, stop. */
2379 return;
2380
2381 case CPP_SEMICOLON:
2382 /* If the next token is a `;', we have reached the end of the
2383 statement. */
2384 if (!nesting_depth)
2385 return;
2386 break;
2387
2388 case CPP_CLOSE_BRACE:
2389 /* If this is a non-nested '}', stop before consuming it.
2390 That way, when confronted with something like:
2391
2392 { 3 + }
2393
2394 we stop before consuming the closing '}', even though we
2395 have not yet reached a `;'. */
2396 if (nesting_depth == 0)
2397 return;
2398
2399 /* If it is the closing '}' for a block that we have
2400 scanned, stop -- but only after consuming the token.
2401 That way given:
2402
2403 void f g () { ... }
2404 typedef int I;
2405
2406 we will stop after the body of the erroneously declared
2407 function, but before consuming the following `typedef'
2408 declaration. */
2409 if (--nesting_depth == 0)
2410 {
2411 cp_lexer_consume_token (parser->lexer);
2412 return;
2413 }
2414
2415 case CPP_OPEN_BRACE:
2416 ++nesting_depth;
2417 break;
2418
2419 default:
2420 break;
2421 }
2422
2423 /* Consume the token. */
2424 cp_lexer_consume_token (parser->lexer);
2425 }
2426 }
2427
2428 /* This function is called at the end of a statement or declaration.
2429 If the next token is a semicolon, it is consumed; otherwise, error
2430 recovery is attempted. */
2431
2432 static void
2433 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2434 {
2435 /* Look for the trailing `;'. */
2436 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2437 {
2438 /* If there is additional (erroneous) input, skip to the end of
2439 the statement. */
2440 cp_parser_skip_to_end_of_statement (parser);
2441 /* If the next token is now a `;', consume it. */
2442 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2443 cp_lexer_consume_token (parser->lexer);
2444 }
2445 }
2446
2447 /* Skip tokens until we have consumed an entire block, or until we
2448 have consumed a non-nested `;'. */
2449
2450 static void
2451 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2452 {
2453 int nesting_depth = 0;
2454
2455 while (nesting_depth >= 0)
2456 {
2457 cp_token *token = cp_lexer_peek_token (parser->lexer);
2458
2459 switch (token->type)
2460 {
2461 case CPP_EOF:
2462 case CPP_PRAGMA_EOL:
2463 /* If we've run out of tokens, stop. */
2464 return;
2465
2466 case CPP_SEMICOLON:
2467 /* Stop if this is an unnested ';'. */
2468 if (!nesting_depth)
2469 nesting_depth = -1;
2470 break;
2471
2472 case CPP_CLOSE_BRACE:
2473 /* Stop if this is an unnested '}', or closes the outermost
2474 nesting level. */
2475 nesting_depth--;
2476 if (!nesting_depth)
2477 nesting_depth = -1;
2478 break;
2479
2480 case CPP_OPEN_BRACE:
2481 /* Nest. */
2482 nesting_depth++;
2483 break;
2484
2485 default:
2486 break;
2487 }
2488
2489 /* Consume the token. */
2490 cp_lexer_consume_token (parser->lexer);
2491 }
2492 }
2493
2494 /* Skip tokens until a non-nested closing curly brace is the next
2495 token. */
2496
2497 static void
2498 cp_parser_skip_to_closing_brace (cp_parser *parser)
2499 {
2500 unsigned nesting_depth = 0;
2501
2502 while (true)
2503 {
2504 cp_token *token = cp_lexer_peek_token (parser->lexer);
2505
2506 switch (token->type)
2507 {
2508 case CPP_EOF:
2509 case CPP_PRAGMA_EOL:
2510 /* If we've run out of tokens, stop. */
2511 return;
2512
2513 case CPP_CLOSE_BRACE:
2514 /* If the next token is a non-nested `}', then we have reached
2515 the end of the current block. */
2516 if (nesting_depth-- == 0)
2517 return;
2518 break;
2519
2520 case CPP_OPEN_BRACE:
2521 /* If it the next token is a `{', then we are entering a new
2522 block. Consume the entire block. */
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 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2536 parameter is the PRAGMA token, allowing us to purge the entire pragma
2537 sequence. */
2538
2539 static void
2540 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2541 {
2542 cp_token *token;
2543
2544 parser->lexer->in_pragma = false;
2545
2546 do
2547 token = cp_lexer_consume_token (parser->lexer);
2548 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2549
2550 /* Ensure that the pragma is not parsed again. */
2551 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2552 }
2553
2554 /* Require pragma end of line, resyncing with it as necessary. The
2555 arguments are as for cp_parser_skip_to_pragma_eol. */
2556
2557 static void
2558 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2559 {
2560 parser->lexer->in_pragma = false;
2561 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2562 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2563 }
2564
2565 /* This is a simple wrapper around make_typename_type. When the id is
2566 an unresolved identifier node, we can provide a superior diagnostic
2567 using cp_parser_diagnose_invalid_type_name. */
2568
2569 static tree
2570 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2571 {
2572 tree result;
2573 if (TREE_CODE (id) == IDENTIFIER_NODE)
2574 {
2575 result = make_typename_type (scope, id, typename_type,
2576 /*complain=*/tf_none);
2577 if (result == error_mark_node)
2578 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2579 return result;
2580 }
2581 return make_typename_type (scope, id, typename_type, tf_error);
2582 }
2583
2584
2585 /* Create a new C++ parser. */
2586
2587 static cp_parser *
2588 cp_parser_new (void)
2589 {
2590 cp_parser *parser;
2591 cp_lexer *lexer;
2592 unsigned i;
2593
2594 /* cp_lexer_new_main is called before calling ggc_alloc because
2595 cp_lexer_new_main might load a PCH file. */
2596 lexer = cp_lexer_new_main ();
2597
2598 /* Initialize the binops_by_token so that we can get the tree
2599 directly from the token. */
2600 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2601 binops_by_token[binops[i].token_type] = binops[i];
2602
2603 parser = GGC_CNEW (cp_parser);
2604 parser->lexer = lexer;
2605 parser->context = cp_parser_context_new (NULL);
2606
2607 /* For now, we always accept GNU extensions. */
2608 parser->allow_gnu_extensions_p = 1;
2609
2610 /* The `>' token is a greater-than operator, not the end of a
2611 template-id. */
2612 parser->greater_than_is_operator_p = true;
2613
2614 parser->default_arg_ok_p = true;
2615
2616 /* We are not parsing a constant-expression. */
2617 parser->integral_constant_expression_p = false;
2618 parser->allow_non_integral_constant_expression_p = false;
2619 parser->non_integral_constant_expression_p = false;
2620
2621 /* Local variable names are not forbidden. */
2622 parser->local_variables_forbidden_p = false;
2623
2624 /* We are not processing an `extern "C"' declaration. */
2625 parser->in_unbraced_linkage_specification_p = false;
2626
2627 /* We are not processing a declarator. */
2628 parser->in_declarator_p = false;
2629
2630 /* We are not processing a template-argument-list. */
2631 parser->in_template_argument_list_p = false;
2632
2633 /* We are not in an iteration statement. */
2634 parser->in_statement = 0;
2635
2636 /* We are not in a switch statement. */
2637 parser->in_switch_statement_p = false;
2638
2639 /* We are not parsing a type-id inside an expression. */
2640 parser->in_type_id_in_expr_p = false;
2641
2642 /* Declarations aren't implicitly extern "C". */
2643 parser->implicit_extern_c = false;
2644
2645 /* String literals should be translated to the execution character set. */
2646 parser->translate_strings_p = true;
2647
2648 /* We are not parsing a function body. */
2649 parser->in_function_body = false;
2650
2651 /* The unparsed function queue is empty. */
2652 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2653
2654 /* There are no classes being defined. */
2655 parser->num_classes_being_defined = 0;
2656
2657 /* No template parameters apply. */
2658 parser->num_template_parameter_lists = 0;
2659
2660 return parser;
2661 }
2662
2663 /* Create a cp_lexer structure which will emit the tokens in CACHE
2664 and push it onto the parser's lexer stack. This is used for delayed
2665 parsing of in-class method bodies and default arguments, and should
2666 not be confused with tentative parsing. */
2667 static void
2668 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2669 {
2670 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2671 lexer->next = parser->lexer;
2672 parser->lexer = lexer;
2673
2674 /* Move the current source position to that of the first token in the
2675 new lexer. */
2676 cp_lexer_set_source_position_from_token (lexer->next_token);
2677 }
2678
2679 /* Pop the top lexer off the parser stack. This is never used for the
2680 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2681 static void
2682 cp_parser_pop_lexer (cp_parser *parser)
2683 {
2684 cp_lexer *lexer = parser->lexer;
2685 parser->lexer = lexer->next;
2686 cp_lexer_destroy (lexer);
2687
2688 /* Put the current source position back where it was before this
2689 lexer was pushed. */
2690 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2691 }
2692
2693 /* Lexical conventions [gram.lex] */
2694
2695 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2696 identifier. */
2697
2698 static tree
2699 cp_parser_identifier (cp_parser* parser)
2700 {
2701 cp_token *token;
2702
2703 /* Look for the identifier. */
2704 token = cp_parser_require (parser, CPP_NAME, "identifier");
2705 /* Return the value. */
2706 return token ? token->value : error_mark_node;
2707 }
2708
2709 /* Parse a sequence of adjacent string constants. Returns a
2710 TREE_STRING representing the combined, nul-terminated string
2711 constant. If TRANSLATE is true, translate the string to the
2712 execution character set. If WIDE_OK is true, a wide string is
2713 invalid here.
2714
2715 C++98 [lex.string] says that if a narrow string literal token is
2716 adjacent to a wide string literal token, the behavior is undefined.
2717 However, C99 6.4.5p4 says that this results in a wide string literal.
2718 We follow C99 here, for consistency with the C front end.
2719
2720 This code is largely lifted from lex_string() in c-lex.c.
2721
2722 FUTURE: ObjC++ will need to handle @-strings here. */
2723 static tree
2724 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2725 {
2726 tree value;
2727 bool wide = false;
2728 size_t count;
2729 struct obstack str_ob;
2730 cpp_string str, istr, *strs;
2731 cp_token *tok;
2732
2733 tok = cp_lexer_peek_token (parser->lexer);
2734 if (!cp_parser_is_string_literal (tok))
2735 {
2736 cp_parser_error (parser, "expected string-literal");
2737 return error_mark_node;
2738 }
2739
2740 /* Try to avoid the overhead of creating and destroying an obstack
2741 for the common case of just one string. */
2742 if (!cp_parser_is_string_literal
2743 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2744 {
2745 cp_lexer_consume_token (parser->lexer);
2746
2747 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2748 str.len = TREE_STRING_LENGTH (tok->value);
2749 count = 1;
2750 if (tok->type == CPP_WSTRING)
2751 wide = true;
2752
2753 strs = &str;
2754 }
2755 else
2756 {
2757 gcc_obstack_init (&str_ob);
2758 count = 0;
2759
2760 do
2761 {
2762 cp_lexer_consume_token (parser->lexer);
2763 count++;
2764 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2765 str.len = TREE_STRING_LENGTH (tok->value);
2766 if (tok->type == CPP_WSTRING)
2767 wide = true;
2768
2769 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2770
2771 tok = cp_lexer_peek_token (parser->lexer);
2772 }
2773 while (cp_parser_is_string_literal (tok));
2774
2775 strs = (cpp_string *) obstack_finish (&str_ob);
2776 }
2777
2778 if (wide && !wide_ok)
2779 {
2780 cp_parser_error (parser, "a wide string is invalid in this context");
2781 wide = false;
2782 }
2783
2784 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2785 (parse_in, strs, count, &istr, wide))
2786 {
2787 value = build_string (istr.len, (char *)istr.text);
2788 free ((void *)istr.text);
2789
2790 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2791 value = fix_string_type (value);
2792 }
2793 else
2794 /* cpp_interpret_string has issued an error. */
2795 value = error_mark_node;
2796
2797 if (count > 1)
2798 obstack_free (&str_ob, 0);
2799
2800 return value;
2801 }
2802
2803
2804 /* Basic concepts [gram.basic] */
2805
2806 /* Parse a translation-unit.
2807
2808 translation-unit:
2809 declaration-seq [opt]
2810
2811 Returns TRUE if all went well. */
2812
2813 static bool
2814 cp_parser_translation_unit (cp_parser* parser)
2815 {
2816 /* The address of the first non-permanent object on the declarator
2817 obstack. */
2818 static void *declarator_obstack_base;
2819
2820 bool success;
2821
2822 /* Create the declarator obstack, if necessary. */
2823 if (!cp_error_declarator)
2824 {
2825 gcc_obstack_init (&declarator_obstack);
2826 /* Create the error declarator. */
2827 cp_error_declarator = make_declarator (cdk_error);
2828 /* Create the empty parameter list. */
2829 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2830 /* Remember where the base of the declarator obstack lies. */
2831 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2832 }
2833
2834 cp_parser_declaration_seq_opt (parser);
2835
2836 /* If there are no tokens left then all went well. */
2837 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2838 {
2839 /* Get rid of the token array; we don't need it any more. */
2840 cp_lexer_destroy (parser->lexer);
2841 parser->lexer = NULL;
2842
2843 /* This file might have been a context that's implicitly extern
2844 "C". If so, pop the lang context. (Only relevant for PCH.) */
2845 if (parser->implicit_extern_c)
2846 {
2847 pop_lang_context ();
2848 parser->implicit_extern_c = false;
2849 }
2850
2851 /* Finish up. */
2852 finish_translation_unit ();
2853
2854 success = true;
2855 }
2856 else
2857 {
2858 cp_parser_error (parser, "expected declaration");
2859 success = false;
2860 }
2861
2862 /* Make sure the declarator obstack was fully cleaned up. */
2863 gcc_assert (obstack_next_free (&declarator_obstack)
2864 == declarator_obstack_base);
2865
2866 /* All went well. */
2867 return success;
2868 }
2869
2870 /* Expressions [gram.expr] */
2871
2872 /* Parse a primary-expression.
2873
2874 primary-expression:
2875 literal
2876 this
2877 ( expression )
2878 id-expression
2879
2880 GNU Extensions:
2881
2882 primary-expression:
2883 ( compound-statement )
2884 __builtin_va_arg ( assignment-expression , type-id )
2885 __builtin_offsetof ( type-id , offsetof-expression )
2886
2887 Objective-C++ Extension:
2888
2889 primary-expression:
2890 objc-expression
2891
2892 literal:
2893 __null
2894
2895 ADDRESS_P is true iff this expression was immediately preceded by
2896 "&" and therefore might denote a pointer-to-member. CAST_P is true
2897 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2898 true iff this expression is a template argument.
2899
2900 Returns a representation of the expression. Upon return, *IDK
2901 indicates what kind of id-expression (if any) was present. */
2902
2903 static tree
2904 cp_parser_primary_expression (cp_parser *parser,
2905 bool address_p,
2906 bool cast_p,
2907 bool template_arg_p,
2908 cp_id_kind *idk)
2909 {
2910 cp_token *token;
2911
2912 /* Assume the primary expression is not an id-expression. */
2913 *idk = CP_ID_KIND_NONE;
2914
2915 /* Peek at the next token. */
2916 token = cp_lexer_peek_token (parser->lexer);
2917 switch (token->type)
2918 {
2919 /* literal:
2920 integer-literal
2921 character-literal
2922 floating-literal
2923 string-literal
2924 boolean-literal */
2925 case CPP_CHAR:
2926 case CPP_WCHAR:
2927 case CPP_NUMBER:
2928 token = cp_lexer_consume_token (parser->lexer);
2929 /* Floating-point literals are only allowed in an integral
2930 constant expression if they are cast to an integral or
2931 enumeration type. */
2932 if (TREE_CODE (token->value) == REAL_CST
2933 && parser->integral_constant_expression_p
2934 && pedantic)
2935 {
2936 /* CAST_P will be set even in invalid code like "int(2.7 +
2937 ...)". Therefore, we have to check that the next token
2938 is sure to end the cast. */
2939 if (cast_p)
2940 {
2941 cp_token *next_token;
2942
2943 next_token = cp_lexer_peek_token (parser->lexer);
2944 if (/* The comma at the end of an
2945 enumerator-definition. */
2946 next_token->type != CPP_COMMA
2947 /* The curly brace at the end of an enum-specifier. */
2948 && next_token->type != CPP_CLOSE_BRACE
2949 /* The end of a statement. */
2950 && next_token->type != CPP_SEMICOLON
2951 /* The end of the cast-expression. */
2952 && next_token->type != CPP_CLOSE_PAREN
2953 /* The end of an array bound. */
2954 && next_token->type != CPP_CLOSE_SQUARE
2955 /* The closing ">" in a template-argument-list. */
2956 && (next_token->type != CPP_GREATER
2957 || parser->greater_than_is_operator_p))
2958 cast_p = false;
2959 }
2960
2961 /* If we are within a cast, then the constraint that the
2962 cast is to an integral or enumeration type will be
2963 checked at that point. If we are not within a cast, then
2964 this code is invalid. */
2965 if (!cast_p)
2966 cp_parser_non_integral_constant_expression
2967 (parser, "floating-point literal");
2968 }
2969 return token->value;
2970
2971 case CPP_STRING:
2972 case CPP_WSTRING:
2973 /* ??? Should wide strings be allowed when parser->translate_strings_p
2974 is false (i.e. in attributes)? If not, we can kill the third
2975 argument to cp_parser_string_literal. */
2976 return cp_parser_string_literal (parser,
2977 parser->translate_strings_p,
2978 true);
2979
2980 case CPP_OPEN_PAREN:
2981 {
2982 tree expr;
2983 bool saved_greater_than_is_operator_p;
2984
2985 /* Consume the `('. */
2986 cp_lexer_consume_token (parser->lexer);
2987 /* Within a parenthesized expression, a `>' token is always
2988 the greater-than operator. */
2989 saved_greater_than_is_operator_p
2990 = parser->greater_than_is_operator_p;
2991 parser->greater_than_is_operator_p = true;
2992 /* If we see `( { ' then we are looking at the beginning of
2993 a GNU statement-expression. */
2994 if (cp_parser_allow_gnu_extensions_p (parser)
2995 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2996 {
2997 /* Statement-expressions are not allowed by the standard. */
2998 if (pedantic)
2999 pedwarn ("ISO C++ forbids braced-groups within expressions");
3000
3001 /* And they're not allowed outside of a function-body; you
3002 cannot, for example, write:
3003
3004 int i = ({ int j = 3; j + 1; });
3005
3006 at class or namespace scope. */
3007 if (!parser->in_function_body)
3008 error ("statement-expressions are allowed only inside functions");
3009 /* Start the statement-expression. */
3010 expr = begin_stmt_expr ();
3011 /* Parse the compound-statement. */
3012 cp_parser_compound_statement (parser, expr, false);
3013 /* Finish up. */
3014 expr = finish_stmt_expr (expr, false);
3015 }
3016 else
3017 {
3018 /* Parse the parenthesized expression. */
3019 expr = cp_parser_expression (parser, cast_p);
3020 /* Let the front end know that this expression was
3021 enclosed in parentheses. This matters in case, for
3022 example, the expression is of the form `A::B', since
3023 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3024 not. */
3025 finish_parenthesized_expr (expr);
3026 }
3027 /* The `>' token might be the end of a template-id or
3028 template-parameter-list now. */
3029 parser->greater_than_is_operator_p
3030 = saved_greater_than_is_operator_p;
3031 /* Consume the `)'. */
3032 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3033 cp_parser_skip_to_end_of_statement (parser);
3034
3035 return expr;
3036 }
3037
3038 case CPP_KEYWORD:
3039 switch (token->keyword)
3040 {
3041 /* These two are the boolean literals. */
3042 case RID_TRUE:
3043 cp_lexer_consume_token (parser->lexer);
3044 return boolean_true_node;
3045 case RID_FALSE:
3046 cp_lexer_consume_token (parser->lexer);
3047 return boolean_false_node;
3048
3049 /* The `__null' literal. */
3050 case RID_NULL:
3051 cp_lexer_consume_token (parser->lexer);
3052 return null_node;
3053
3054 /* Recognize the `this' keyword. */
3055 case RID_THIS:
3056 cp_lexer_consume_token (parser->lexer);
3057 if (parser->local_variables_forbidden_p)
3058 {
3059 error ("%<this%> may not be used in this context");
3060 return error_mark_node;
3061 }
3062 /* Pointers cannot appear in constant-expressions. */
3063 if (cp_parser_non_integral_constant_expression (parser,
3064 "`this'"))
3065 return error_mark_node;
3066 return finish_this_expr ();
3067
3068 /* The `operator' keyword can be the beginning of an
3069 id-expression. */
3070 case RID_OPERATOR:
3071 goto id_expression;
3072
3073 case RID_FUNCTION_NAME:
3074 case RID_PRETTY_FUNCTION_NAME:
3075 case RID_C99_FUNCTION_NAME:
3076 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3077 __func__ are the names of variables -- but they are
3078 treated specially. Therefore, they are handled here,
3079 rather than relying on the generic id-expression logic
3080 below. Grammatically, these names are id-expressions.
3081
3082 Consume the token. */
3083 token = cp_lexer_consume_token (parser->lexer);
3084 /* Look up the name. */
3085 return finish_fname (token->value);
3086
3087 case RID_VA_ARG:
3088 {
3089 tree expression;
3090 tree type;
3091
3092 /* The `__builtin_va_arg' construct is used to handle
3093 `va_arg'. Consume the `__builtin_va_arg' token. */
3094 cp_lexer_consume_token (parser->lexer);
3095 /* Look for the opening `('. */
3096 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3097 /* Now, parse the assignment-expression. */
3098 expression = cp_parser_assignment_expression (parser,
3099 /*cast_p=*/false);
3100 /* Look for the `,'. */
3101 cp_parser_require (parser, CPP_COMMA, "`,'");
3102 /* Parse the type-id. */
3103 type = cp_parser_type_id (parser);
3104 /* Look for the closing `)'. */
3105 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3106 /* Using `va_arg' in a constant-expression is not
3107 allowed. */
3108 if (cp_parser_non_integral_constant_expression (parser,
3109 "`va_arg'"))
3110 return error_mark_node;
3111 return build_x_va_arg (expression, type);
3112 }
3113
3114 case RID_OFFSETOF:
3115 return cp_parser_builtin_offsetof (parser);
3116
3117 /* Objective-C++ expressions. */
3118 case RID_AT_ENCODE:
3119 case RID_AT_PROTOCOL:
3120 case RID_AT_SELECTOR:
3121 return cp_parser_objc_expression (parser);
3122
3123 default:
3124 cp_parser_error (parser, "expected primary-expression");
3125 return error_mark_node;
3126 }
3127
3128 /* An id-expression can start with either an identifier, a
3129 `::' as the beginning of a qualified-id, or the "operator"
3130 keyword. */
3131 case CPP_NAME:
3132 case CPP_SCOPE:
3133 case CPP_TEMPLATE_ID:
3134 case CPP_NESTED_NAME_SPECIFIER:
3135 {
3136 tree id_expression;
3137 tree decl;
3138 const char *error_msg;
3139 bool template_p;
3140 bool done;
3141
3142 id_expression:
3143 /* Parse the id-expression. */
3144 id_expression
3145 = cp_parser_id_expression (parser,
3146 /*template_keyword_p=*/false,
3147 /*check_dependency_p=*/true,
3148 &template_p,
3149 /*declarator_p=*/false,
3150 /*optional_p=*/false);
3151 if (id_expression == error_mark_node)
3152 return error_mark_node;
3153 token = cp_lexer_peek_token (parser->lexer);
3154 done = (token->type != CPP_OPEN_SQUARE
3155 && token->type != CPP_OPEN_PAREN
3156 && token->type != CPP_DOT
3157 && token->type != CPP_DEREF
3158 && token->type != CPP_PLUS_PLUS
3159 && token->type != CPP_MINUS_MINUS);
3160 /* If we have a template-id, then no further lookup is
3161 required. If the template-id was for a template-class, we
3162 will sometimes have a TYPE_DECL at this point. */
3163 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3164 || TREE_CODE (id_expression) == TYPE_DECL)
3165 decl = id_expression;
3166 /* Look up the name. */
3167 else
3168 {
3169 tree ambiguous_decls;
3170
3171 decl = cp_parser_lookup_name (parser, id_expression,
3172 none_type,
3173 template_p,
3174 /*is_namespace=*/false,
3175 /*check_dependency=*/true,
3176 &ambiguous_decls);
3177 /* If the lookup was ambiguous, an error will already have
3178 been issued. */
3179 if (ambiguous_decls)
3180 return error_mark_node;
3181
3182 /* In Objective-C++, an instance variable (ivar) may be preferred
3183 to whatever cp_parser_lookup_name() found. */
3184 decl = objc_lookup_ivar (decl, id_expression);
3185
3186 /* If name lookup gives us a SCOPE_REF, then the
3187 qualifying scope was dependent. */
3188 if (TREE_CODE (decl) == SCOPE_REF)
3189 return decl;
3190 /* Check to see if DECL is a local variable in a context
3191 where that is forbidden. */
3192 if (parser->local_variables_forbidden_p
3193 && local_variable_p (decl))
3194 {
3195 /* It might be that we only found DECL because we are
3196 trying to be generous with pre-ISO scoping rules.
3197 For example, consider:
3198
3199 int i;
3200 void g() {
3201 for (int i = 0; i < 10; ++i) {}
3202 extern void f(int j = i);
3203 }
3204
3205 Here, name look up will originally find the out
3206 of scope `i'. We need to issue a warning message,
3207 but then use the global `i'. */
3208 decl = check_for_out_of_scope_variable (decl);
3209 if (local_variable_p (decl))
3210 {
3211 error ("local variable %qD may not appear in this context",
3212 decl);
3213 return error_mark_node;
3214 }
3215 }
3216 }
3217
3218 decl = (finish_id_expression
3219 (id_expression, decl, parser->scope,
3220 idk,
3221 parser->integral_constant_expression_p,
3222 parser->allow_non_integral_constant_expression_p,
3223 &parser->non_integral_constant_expression_p,
3224 template_p, done, address_p,
3225 template_arg_p,
3226 &error_msg));
3227 if (error_msg)
3228 cp_parser_error (parser, error_msg);
3229 return decl;
3230 }
3231
3232 /* Anything else is an error. */
3233 default:
3234 /* ...unless we have an Objective-C++ message or string literal, that is. */
3235 if (c_dialect_objc ()
3236 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3237 return cp_parser_objc_expression (parser);
3238
3239 cp_parser_error (parser, "expected primary-expression");
3240 return error_mark_node;
3241 }
3242 }
3243
3244 /* Parse an id-expression.
3245
3246 id-expression:
3247 unqualified-id
3248 qualified-id
3249
3250 qualified-id:
3251 :: [opt] nested-name-specifier template [opt] unqualified-id
3252 :: identifier
3253 :: operator-function-id
3254 :: template-id
3255
3256 Return a representation of the unqualified portion of the
3257 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3258 a `::' or nested-name-specifier.
3259
3260 Often, if the id-expression was a qualified-id, the caller will
3261 want to make a SCOPE_REF to represent the qualified-id. This
3262 function does not do this in order to avoid wastefully creating
3263 SCOPE_REFs when they are not required.
3264
3265 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3266 `template' keyword.
3267
3268 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3269 uninstantiated templates.
3270
3271 If *TEMPLATE_P is non-NULL, it is set to true iff the
3272 `template' keyword is used to explicitly indicate that the entity
3273 named is a template.
3274
3275 If DECLARATOR_P is true, the id-expression is appearing as part of
3276 a declarator, rather than as part of an expression. */
3277
3278 static tree
3279 cp_parser_id_expression (cp_parser *parser,
3280 bool template_keyword_p,
3281 bool check_dependency_p,
3282 bool *template_p,
3283 bool declarator_p,
3284 bool optional_p)
3285 {
3286 bool global_scope_p;
3287 bool nested_name_specifier_p;
3288
3289 /* Assume the `template' keyword was not used. */
3290 if (template_p)
3291 *template_p = template_keyword_p;
3292
3293 /* Look for the optional `::' operator. */
3294 global_scope_p
3295 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3296 != NULL_TREE);
3297 /* Look for the optional nested-name-specifier. */
3298 nested_name_specifier_p
3299 = (cp_parser_nested_name_specifier_opt (parser,
3300 /*typename_keyword_p=*/false,
3301 check_dependency_p,
3302 /*type_p=*/false,
3303 declarator_p)
3304 != NULL_TREE);
3305 /* If there is a nested-name-specifier, then we are looking at
3306 the first qualified-id production. */
3307 if (nested_name_specifier_p)
3308 {
3309 tree saved_scope;
3310 tree saved_object_scope;
3311 tree saved_qualifying_scope;
3312 tree unqualified_id;
3313 bool is_template;
3314
3315 /* See if the next token is the `template' keyword. */
3316 if (!template_p)
3317 template_p = &is_template;
3318 *template_p = cp_parser_optional_template_keyword (parser);
3319 /* Name lookup we do during the processing of the
3320 unqualified-id might obliterate SCOPE. */
3321 saved_scope = parser->scope;
3322 saved_object_scope = parser->object_scope;
3323 saved_qualifying_scope = parser->qualifying_scope;
3324 /* Process the final unqualified-id. */
3325 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3326 check_dependency_p,
3327 declarator_p,
3328 /*optional_p=*/false);
3329 /* Restore the SAVED_SCOPE for our caller. */
3330 parser->scope = saved_scope;
3331 parser->object_scope = saved_object_scope;
3332 parser->qualifying_scope = saved_qualifying_scope;
3333
3334 return unqualified_id;
3335 }
3336 /* Otherwise, if we are in global scope, then we are looking at one
3337 of the other qualified-id productions. */
3338 else if (global_scope_p)
3339 {
3340 cp_token *token;
3341 tree id;
3342
3343 /* Peek at the next token. */
3344 token = cp_lexer_peek_token (parser->lexer);
3345
3346 /* If it's an identifier, and the next token is not a "<", then
3347 we can avoid the template-id case. This is an optimization
3348 for this common case. */
3349 if (token->type == CPP_NAME
3350 && !cp_parser_nth_token_starts_template_argument_list_p
3351 (parser, 2))
3352 return cp_parser_identifier (parser);
3353
3354 cp_parser_parse_tentatively (parser);
3355 /* Try a template-id. */
3356 id = cp_parser_template_id (parser,
3357 /*template_keyword_p=*/false,
3358 /*check_dependency_p=*/true,
3359 declarator_p);
3360 /* If that worked, we're done. */
3361 if (cp_parser_parse_definitely (parser))
3362 return id;
3363
3364 /* Peek at the next token. (Changes in the token buffer may
3365 have invalidated the pointer obtained above.) */
3366 token = cp_lexer_peek_token (parser->lexer);
3367
3368 switch (token->type)
3369 {
3370 case CPP_NAME:
3371 return cp_parser_identifier (parser);
3372
3373 case CPP_KEYWORD:
3374 if (token->keyword == RID_OPERATOR)
3375 return cp_parser_operator_function_id (parser);
3376 /* Fall through. */
3377
3378 default:
3379 cp_parser_error (parser, "expected id-expression");
3380 return error_mark_node;
3381 }
3382 }
3383 else
3384 return cp_parser_unqualified_id (parser, template_keyword_p,
3385 /*check_dependency_p=*/true,
3386 declarator_p,
3387 optional_p);
3388 }
3389
3390 /* Parse an unqualified-id.
3391
3392 unqualified-id:
3393 identifier
3394 operator-function-id
3395 conversion-function-id
3396 ~ class-name
3397 template-id
3398
3399 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3400 keyword, in a construct like `A::template ...'.
3401
3402 Returns a representation of unqualified-id. For the `identifier'
3403 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3404 production a BIT_NOT_EXPR is returned; the operand of the
3405 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3406 other productions, see the documentation accompanying the
3407 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3408 names are looked up in uninstantiated templates. If DECLARATOR_P
3409 is true, the unqualified-id is appearing as part of a declarator,
3410 rather than as part of an expression. */
3411
3412 static tree
3413 cp_parser_unqualified_id (cp_parser* parser,
3414 bool template_keyword_p,
3415 bool check_dependency_p,
3416 bool declarator_p,
3417 bool optional_p)
3418 {
3419 cp_token *token;
3420
3421 /* Peek at the next token. */
3422 token = cp_lexer_peek_token (parser->lexer);
3423
3424 switch (token->type)
3425 {
3426 case CPP_NAME:
3427 {
3428 tree id;
3429
3430 /* We don't know yet whether or not this will be a
3431 template-id. */
3432 cp_parser_parse_tentatively (parser);
3433 /* Try a template-id. */
3434 id = cp_parser_template_id (parser, template_keyword_p,
3435 check_dependency_p,
3436 declarator_p);
3437 /* If it worked, we're done. */
3438 if (cp_parser_parse_definitely (parser))
3439 return id;
3440 /* Otherwise, it's an ordinary identifier. */
3441 return cp_parser_identifier (parser);
3442 }
3443
3444 case CPP_TEMPLATE_ID:
3445 return cp_parser_template_id (parser, template_keyword_p,
3446 check_dependency_p,
3447 declarator_p);
3448
3449 case CPP_COMPL:
3450 {
3451 tree type_decl;
3452 tree qualifying_scope;
3453 tree object_scope;
3454 tree scope;
3455 bool done;
3456
3457 /* Consume the `~' token. */
3458 cp_lexer_consume_token (parser->lexer);
3459 /* Parse the class-name. The standard, as written, seems to
3460 say that:
3461
3462 template <typename T> struct S { ~S (); };
3463 template <typename T> S<T>::~S() {}
3464
3465 is invalid, since `~' must be followed by a class-name, but
3466 `S<T>' is dependent, and so not known to be a class.
3467 That's not right; we need to look in uninstantiated
3468 templates. A further complication arises from:
3469
3470 template <typename T> void f(T t) {
3471 t.T::~T();
3472 }
3473
3474 Here, it is not possible to look up `T' in the scope of `T'
3475 itself. We must look in both the current scope, and the
3476 scope of the containing complete expression.
3477
3478 Yet another issue is:
3479
3480 struct S {
3481 int S;
3482 ~S();
3483 };
3484
3485 S::~S() {}
3486
3487 The standard does not seem to say that the `S' in `~S'
3488 should refer to the type `S' and not the data member
3489 `S::S'. */
3490
3491 /* DR 244 says that we look up the name after the "~" in the
3492 same scope as we looked up the qualifying name. That idea
3493 isn't fully worked out; it's more complicated than that. */
3494 scope = parser->scope;
3495 object_scope = parser->object_scope;
3496 qualifying_scope = parser->qualifying_scope;
3497
3498 /* Check for invalid scopes. */
3499 if (scope == error_mark_node)
3500 {
3501 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3502 cp_lexer_consume_token (parser->lexer);
3503 return error_mark_node;
3504 }
3505 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3506 {
3507 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3508 error ("scope %qT before %<~%> is not a class-name", scope);
3509 cp_parser_simulate_error (parser);
3510 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3511 cp_lexer_consume_token (parser->lexer);
3512 return error_mark_node;
3513 }
3514 gcc_assert (!scope || TYPE_P (scope));
3515
3516 /* If the name is of the form "X::~X" it's OK. */
3517 token = cp_lexer_peek_token (parser->lexer);
3518 if (scope
3519 && token->type == CPP_NAME
3520 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3521 == CPP_OPEN_PAREN)
3522 && constructor_name_p (token->value, scope))
3523 {
3524 cp_lexer_consume_token (parser->lexer);
3525 return build_nt (BIT_NOT_EXPR, scope);
3526 }
3527
3528 /* If there was an explicit qualification (S::~T), first look
3529 in the scope given by the qualification (i.e., S). */
3530 done = false;
3531 type_decl = NULL_TREE;
3532 if (scope)
3533 {
3534 cp_parser_parse_tentatively (parser);
3535 type_decl = cp_parser_class_name (parser,
3536 /*typename_keyword_p=*/false,
3537 /*template_keyword_p=*/false,
3538 none_type,
3539 /*check_dependency=*/false,
3540 /*class_head_p=*/false,
3541 declarator_p);
3542 if (cp_parser_parse_definitely (parser))
3543 done = true;
3544 }
3545 /* In "N::S::~S", look in "N" as well. */
3546 if (!done && scope && qualifying_scope)
3547 {
3548 cp_parser_parse_tentatively (parser);
3549 parser->scope = qualifying_scope;
3550 parser->object_scope = NULL_TREE;
3551 parser->qualifying_scope = NULL_TREE;
3552 type_decl
3553 = cp_parser_class_name (parser,
3554 /*typename_keyword_p=*/false,
3555 /*template_keyword_p=*/false,
3556 none_type,
3557 /*check_dependency=*/false,
3558 /*class_head_p=*/false,
3559 declarator_p);
3560 if (cp_parser_parse_definitely (parser))
3561 done = true;
3562 }
3563 /* In "p->S::~T", look in the scope given by "*p" as well. */
3564 else if (!done && object_scope)
3565 {
3566 cp_parser_parse_tentatively (parser);
3567 parser->scope = object_scope;
3568 parser->object_scope = NULL_TREE;
3569 parser->qualifying_scope = NULL_TREE;
3570 type_decl
3571 = cp_parser_class_name (parser,
3572 /*typename_keyword_p=*/false,
3573 /*template_keyword_p=*/false,
3574 none_type,
3575 /*check_dependency=*/false,
3576 /*class_head_p=*/false,
3577 declarator_p);
3578 if (cp_parser_parse_definitely (parser))
3579 done = true;
3580 }
3581 /* Look in the surrounding context. */
3582 if (!done)
3583 {
3584 parser->scope = NULL_TREE;
3585 parser->object_scope = NULL_TREE;
3586 parser->qualifying_scope = NULL_TREE;
3587 type_decl
3588 = cp_parser_class_name (parser,
3589 /*typename_keyword_p=*/false,
3590 /*template_keyword_p=*/false,
3591 none_type,
3592 /*check_dependency=*/false,
3593 /*class_head_p=*/false,
3594 declarator_p);
3595 }
3596 /* If an error occurred, assume that the name of the
3597 destructor is the same as the name of the qualifying
3598 class. That allows us to keep parsing after running
3599 into ill-formed destructor names. */
3600 if (type_decl == error_mark_node && scope)
3601 return build_nt (BIT_NOT_EXPR, scope);
3602 else if (type_decl == error_mark_node)
3603 return error_mark_node;
3604
3605 /* Check that destructor name and scope match. */
3606 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3607 {
3608 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3609 error ("declaration of %<~%T%> as member of %qT",
3610 type_decl, scope);
3611 cp_parser_simulate_error (parser);
3612 return error_mark_node;
3613 }
3614
3615 /* [class.dtor]
3616
3617 A typedef-name that names a class shall not be used as the
3618 identifier in the declarator for a destructor declaration. */
3619 if (declarator_p
3620 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3621 && !DECL_SELF_REFERENCE_P (type_decl)
3622 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3623 error ("typedef-name %qD used as destructor declarator",
3624 type_decl);
3625
3626 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3627 }
3628
3629 case CPP_KEYWORD:
3630 if (token->keyword == RID_OPERATOR)
3631 {
3632 tree id;
3633
3634 /* This could be a template-id, so we try that first. */
3635 cp_parser_parse_tentatively (parser);
3636 /* Try a template-id. */
3637 id = cp_parser_template_id (parser, template_keyword_p,
3638 /*check_dependency_p=*/true,
3639 declarator_p);
3640 /* If that worked, we're done. */
3641 if (cp_parser_parse_definitely (parser))
3642 return id;
3643 /* We still don't know whether we're looking at an
3644 operator-function-id or a conversion-function-id. */
3645 cp_parser_parse_tentatively (parser);
3646 /* Try an operator-function-id. */
3647 id = cp_parser_operator_function_id (parser);
3648 /* If that didn't work, try a conversion-function-id. */
3649 if (!cp_parser_parse_definitely (parser))
3650 id = cp_parser_conversion_function_id (parser);
3651
3652 return id;
3653 }
3654 /* Fall through. */
3655
3656 default:
3657 if (optional_p)
3658 return NULL_TREE;
3659 cp_parser_error (parser, "expected unqualified-id");
3660 return error_mark_node;
3661 }
3662 }
3663
3664 /* Parse an (optional) nested-name-specifier.
3665
3666 nested-name-specifier:
3667 class-or-namespace-name :: nested-name-specifier [opt]
3668 class-or-namespace-name :: template nested-name-specifier [opt]
3669
3670 PARSER->SCOPE should be set appropriately before this function is
3671 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3672 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3673 in name lookups.
3674
3675 Sets PARSER->SCOPE to the class (TYPE) or namespace
3676 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3677 it unchanged if there is no nested-name-specifier. Returns the new
3678 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3679
3680 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3681 part of a declaration and/or decl-specifier. */
3682
3683 static tree
3684 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3685 bool typename_keyword_p,
3686 bool check_dependency_p,
3687 bool type_p,
3688 bool is_declaration)
3689 {
3690 bool success = false;
3691 cp_token_position start = 0;
3692 cp_token *token;
3693
3694 /* Remember where the nested-name-specifier starts. */
3695 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3696 {
3697 start = cp_lexer_token_position (parser->lexer, false);
3698 push_deferring_access_checks (dk_deferred);
3699 }
3700
3701 while (true)
3702 {
3703 tree new_scope;
3704 tree old_scope;
3705 tree saved_qualifying_scope;
3706 bool template_keyword_p;
3707
3708 /* Spot cases that cannot be the beginning of a
3709 nested-name-specifier. */
3710 token = cp_lexer_peek_token (parser->lexer);
3711
3712 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3713 the already parsed nested-name-specifier. */
3714 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3715 {
3716 /* Grab the nested-name-specifier and continue the loop. */
3717 cp_parser_pre_parsed_nested_name_specifier (parser);
3718 /* If we originally encountered this nested-name-specifier
3719 with IS_DECLARATION set to false, we will not have
3720 resolved TYPENAME_TYPEs, so we must do so here. */
3721 if (is_declaration
3722 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3723 {
3724 new_scope = resolve_typename_type (parser->scope,
3725 /*only_current_p=*/false);
3726 if (new_scope != error_mark_node)
3727 parser->scope = new_scope;
3728 }
3729 success = true;
3730 continue;
3731 }
3732
3733 /* Spot cases that cannot be the beginning of a
3734 nested-name-specifier. On the second and subsequent times
3735 through the loop, we look for the `template' keyword. */
3736 if (success && token->keyword == RID_TEMPLATE)
3737 ;
3738 /* A template-id can start a nested-name-specifier. */
3739 else if (token->type == CPP_TEMPLATE_ID)
3740 ;
3741 else
3742 {
3743 /* If the next token is not an identifier, then it is
3744 definitely not a class-or-namespace-name. */
3745 if (token->type != CPP_NAME)
3746 break;
3747 /* If the following token is neither a `<' (to begin a
3748 template-id), nor a `::', then we are not looking at a
3749 nested-name-specifier. */
3750 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3751 if (token->type != CPP_SCOPE
3752 && !cp_parser_nth_token_starts_template_argument_list_p
3753 (parser, 2))
3754 break;
3755 }
3756
3757 /* The nested-name-specifier is optional, so we parse
3758 tentatively. */
3759 cp_parser_parse_tentatively (parser);
3760
3761 /* Look for the optional `template' keyword, if this isn't the
3762 first time through the loop. */
3763 if (success)
3764 template_keyword_p = cp_parser_optional_template_keyword (parser);
3765 else
3766 template_keyword_p = false;
3767
3768 /* Save the old scope since the name lookup we are about to do
3769 might destroy it. */
3770 old_scope = parser->scope;
3771 saved_qualifying_scope = parser->qualifying_scope;
3772 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3773 look up names in "X<T>::I" in order to determine that "Y" is
3774 a template. So, if we have a typename at this point, we make
3775 an effort to look through it. */
3776 if (is_declaration
3777 && !typename_keyword_p
3778 && parser->scope
3779 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3780 parser->scope = resolve_typename_type (parser->scope,
3781 /*only_current_p=*/false);
3782 /* Parse the qualifying entity. */
3783 new_scope
3784 = cp_parser_class_or_namespace_name (parser,
3785 typename_keyword_p,
3786 template_keyword_p,
3787 check_dependency_p,
3788 type_p,
3789 is_declaration);
3790 /* Look for the `::' token. */
3791 cp_parser_require (parser, CPP_SCOPE, "`::'");
3792
3793 /* If we found what we wanted, we keep going; otherwise, we're
3794 done. */
3795 if (!cp_parser_parse_definitely (parser))
3796 {
3797 bool error_p = false;
3798
3799 /* Restore the OLD_SCOPE since it was valid before the
3800 failed attempt at finding the last
3801 class-or-namespace-name. */
3802 parser->scope = old_scope;
3803 parser->qualifying_scope = saved_qualifying_scope;
3804 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3805 break;
3806 /* If the next token is an identifier, and the one after
3807 that is a `::', then any valid interpretation would have
3808 found a class-or-namespace-name. */
3809 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3810 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3811 == CPP_SCOPE)
3812 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3813 != CPP_COMPL))
3814 {
3815 token = cp_lexer_consume_token (parser->lexer);
3816 if (!error_p)
3817 {
3818 if (!token->ambiguous_p)
3819 {
3820 tree decl;
3821 tree ambiguous_decls;
3822
3823 decl = cp_parser_lookup_name (parser, token->value,
3824 none_type,
3825 /*is_template=*/false,
3826 /*is_namespace=*/false,
3827 /*check_dependency=*/true,
3828 &ambiguous_decls);
3829 if (TREE_CODE (decl) == TEMPLATE_DECL)
3830 error ("%qD used without template parameters", decl);
3831 else if (ambiguous_decls)
3832 {
3833 error ("reference to %qD is ambiguous",
3834 token->value);
3835 print_candidates (ambiguous_decls);
3836 decl = error_mark_node;
3837 }
3838 else
3839 cp_parser_name_lookup_error
3840 (parser, token->value, decl,
3841 "is not a class or namespace");
3842 }
3843 parser->scope = error_mark_node;
3844 error_p = true;
3845 /* Treat this as a successful nested-name-specifier
3846 due to:
3847
3848 [basic.lookup.qual]
3849
3850 If the name found is not a class-name (clause
3851 _class_) or namespace-name (_namespace.def_), the
3852 program is ill-formed. */
3853 success = true;
3854 }
3855 cp_lexer_consume_token (parser->lexer);
3856 }
3857 break;
3858 }
3859 /* We've found one valid nested-name-specifier. */
3860 success = true;
3861 /* Name lookup always gives us a DECL. */
3862 if (TREE_CODE (new_scope) == TYPE_DECL)
3863 new_scope = TREE_TYPE (new_scope);
3864 /* Uses of "template" must be followed by actual templates. */
3865 if (template_keyword_p
3866 && !(CLASS_TYPE_P (new_scope)
3867 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3869 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3870 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3871 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3872 == TEMPLATE_ID_EXPR)))
3873 pedwarn (TYPE_P (new_scope)
3874 ? "%qT is not a template"
3875 : "%qD is not a template",
3876 new_scope);
3877 /* If it is a class scope, try to complete it; we are about to
3878 be looking up names inside the class. */
3879 if (TYPE_P (new_scope)
3880 /* Since checking types for dependency can be expensive,
3881 avoid doing it if the type is already complete. */
3882 && !COMPLETE_TYPE_P (new_scope)
3883 /* Do not try to complete dependent types. */
3884 && !dependent_type_p (new_scope))
3885 new_scope = complete_type (new_scope);
3886 /* Make sure we look in the right scope the next time through
3887 the loop. */
3888 parser->scope = new_scope;
3889 }
3890
3891 /* If parsing tentatively, replace the sequence of tokens that makes
3892 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3893 token. That way, should we re-parse the token stream, we will
3894 not have to repeat the effort required to do the parse, nor will
3895 we issue duplicate error messages. */
3896 if (success && start)
3897 {
3898 cp_token *token;
3899 tree access_checks;
3900
3901 token = cp_lexer_token_at (parser->lexer, start);
3902 /* Reset the contents of the START token. */
3903 token->type = CPP_NESTED_NAME_SPECIFIER;
3904 /* Retrieve any deferred checks. Do not pop this access checks yet
3905 so the memory will not be reclaimed during token replacing below. */
3906 access_checks = get_deferred_access_checks ();
3907 token->value = build_tree_list (copy_list (access_checks),
3908 parser->scope);
3909 TREE_TYPE (token->value) = parser->qualifying_scope;
3910 token->keyword = RID_MAX;
3911
3912 /* Purge all subsequent tokens. */
3913 cp_lexer_purge_tokens_after (parser->lexer, start);
3914 }
3915
3916 if (start)
3917 pop_to_parent_deferring_access_checks ();
3918
3919 return success ? parser->scope : NULL_TREE;
3920 }
3921
3922 /* Parse a nested-name-specifier. See
3923 cp_parser_nested_name_specifier_opt for details. This function
3924 behaves identically, except that it will an issue an error if no
3925 nested-name-specifier is present. */
3926
3927 static tree
3928 cp_parser_nested_name_specifier (cp_parser *parser,
3929 bool typename_keyword_p,
3930 bool check_dependency_p,
3931 bool type_p,
3932 bool is_declaration)
3933 {
3934 tree scope;
3935
3936 /* Look for the nested-name-specifier. */
3937 scope = cp_parser_nested_name_specifier_opt (parser,
3938 typename_keyword_p,
3939 check_dependency_p,
3940 type_p,
3941 is_declaration);
3942 /* If it was not present, issue an error message. */
3943 if (!scope)
3944 {
3945 cp_parser_error (parser, "expected nested-name-specifier");
3946 parser->scope = NULL_TREE;
3947 }
3948
3949 return scope;
3950 }
3951
3952 /* Parse a class-or-namespace-name.
3953
3954 class-or-namespace-name:
3955 class-name
3956 namespace-name
3957
3958 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3959 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3960 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3961 TYPE_P is TRUE iff the next name should be taken as a class-name,
3962 even the same name is declared to be another entity in the same
3963 scope.
3964
3965 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3966 specified by the class-or-namespace-name. If neither is found the
3967 ERROR_MARK_NODE is returned. */
3968
3969 static tree
3970 cp_parser_class_or_namespace_name (cp_parser *parser,
3971 bool typename_keyword_p,
3972 bool template_keyword_p,
3973 bool check_dependency_p,
3974 bool type_p,
3975 bool is_declaration)
3976 {
3977 tree saved_scope;
3978 tree saved_qualifying_scope;
3979 tree saved_object_scope;
3980 tree scope;
3981 bool only_class_p;
3982
3983 /* Before we try to parse the class-name, we must save away the
3984 current PARSER->SCOPE since cp_parser_class_name will destroy
3985 it. */
3986 saved_scope = parser->scope;
3987 saved_qualifying_scope = parser->qualifying_scope;
3988 saved_object_scope = parser->object_scope;
3989 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3990 there is no need to look for a namespace-name. */
3991 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3992 if (!only_class_p)
3993 cp_parser_parse_tentatively (parser);
3994 scope = cp_parser_class_name (parser,
3995 typename_keyword_p,
3996 template_keyword_p,
3997 type_p ? class_type : none_type,
3998 check_dependency_p,
3999 /*class_head_p=*/false,
4000 is_declaration);
4001 /* If that didn't work, try for a namespace-name. */
4002 if (!only_class_p && !cp_parser_parse_definitely (parser))
4003 {
4004 /* Restore the saved scope. */
4005 parser->scope = saved_scope;
4006 parser->qualifying_scope = saved_qualifying_scope;
4007 parser->object_scope = saved_object_scope;
4008 /* If we are not looking at an identifier followed by the scope
4009 resolution operator, then this is not part of a
4010 nested-name-specifier. (Note that this function is only used
4011 to parse the components of a nested-name-specifier.) */
4012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4013 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4014 return error_mark_node;
4015 scope = cp_parser_namespace_name (parser);
4016 }
4017
4018 return scope;
4019 }
4020
4021 /* Parse a postfix-expression.
4022
4023 postfix-expression:
4024 primary-expression
4025 postfix-expression [ expression ]
4026 postfix-expression ( expression-list [opt] )
4027 simple-type-specifier ( expression-list [opt] )
4028 typename :: [opt] nested-name-specifier identifier
4029 ( expression-list [opt] )
4030 typename :: [opt] nested-name-specifier template [opt] template-id
4031 ( expression-list [opt] )
4032 postfix-expression . template [opt] id-expression
4033 postfix-expression -> template [opt] id-expression
4034 postfix-expression . pseudo-destructor-name
4035 postfix-expression -> pseudo-destructor-name
4036 postfix-expression ++
4037 postfix-expression --
4038 dynamic_cast < type-id > ( expression )
4039 static_cast < type-id > ( expression )
4040 reinterpret_cast < type-id > ( expression )
4041 const_cast < type-id > ( expression )
4042 typeid ( expression )
4043 typeid ( type-id )
4044
4045 GNU Extension:
4046
4047 postfix-expression:
4048 ( type-id ) { initializer-list , [opt] }
4049
4050 This extension is a GNU version of the C99 compound-literal
4051 construct. (The C99 grammar uses `type-name' instead of `type-id',
4052 but they are essentially the same concept.)
4053
4054 If ADDRESS_P is true, the postfix expression is the operand of the
4055 `&' operator. CAST_P is true if this expression is the target of a
4056 cast.
4057
4058 Returns a representation of the expression. */
4059
4060 static tree
4061 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4062 {
4063 cp_token *token;
4064 enum rid keyword;
4065 cp_id_kind idk = CP_ID_KIND_NONE;
4066 tree postfix_expression = NULL_TREE;
4067
4068 /* Peek at the next token. */
4069 token = cp_lexer_peek_token (parser->lexer);
4070 /* Some of the productions are determined by keywords. */
4071 keyword = token->keyword;
4072 switch (keyword)
4073 {
4074 case RID_DYNCAST:
4075 case RID_STATCAST:
4076 case RID_REINTCAST:
4077 case RID_CONSTCAST:
4078 {
4079 tree type;
4080 tree expression;
4081 const char *saved_message;
4082
4083 /* All of these can be handled in the same way from the point
4084 of view of parsing. Begin by consuming the token
4085 identifying the cast. */
4086 cp_lexer_consume_token (parser->lexer);
4087
4088 /* New types cannot be defined in the cast. */
4089 saved_message = parser->type_definition_forbidden_message;
4090 parser->type_definition_forbidden_message
4091 = "types may not be defined in casts";
4092
4093 /* Look for the opening `<'. */
4094 cp_parser_require (parser, CPP_LESS, "`<'");
4095 /* Parse the type to which we are casting. */
4096 type = cp_parser_type_id (parser);
4097 /* Look for the closing `>'. */
4098 cp_parser_require (parser, CPP_GREATER, "`>'");
4099 /* Restore the old message. */
4100 parser->type_definition_forbidden_message = saved_message;
4101
4102 /* And the expression which is being cast. */
4103 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4104 expression = cp_parser_expression (parser, /*cast_p=*/true);
4105 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4106
4107 /* Only type conversions to integral or enumeration types
4108 can be used in constant-expressions. */
4109 if (!cast_valid_in_integral_constant_expression_p (type)
4110 && (cp_parser_non_integral_constant_expression
4111 (parser,
4112 "a cast to a type other than an integral or "
4113 "enumeration type")))
4114 return error_mark_node;
4115
4116 switch (keyword)
4117 {
4118 case RID_DYNCAST:
4119 postfix_expression
4120 = build_dynamic_cast (type, expression);
4121 break;
4122 case RID_STATCAST:
4123 postfix_expression
4124 = build_static_cast (type, expression);
4125 break;
4126 case RID_REINTCAST:
4127 postfix_expression
4128 = build_reinterpret_cast (type, expression);
4129 break;
4130 case RID_CONSTCAST:
4131 postfix_expression
4132 = build_const_cast (type, expression);
4133 break;
4134 default:
4135 gcc_unreachable ();
4136 }
4137 }
4138 break;
4139
4140 case RID_TYPEID:
4141 {
4142 tree type;
4143 const char *saved_message;
4144 bool saved_in_type_id_in_expr_p;
4145
4146 /* Consume the `typeid' token. */
4147 cp_lexer_consume_token (parser->lexer);
4148 /* Look for the `(' token. */
4149 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4150 /* Types cannot be defined in a `typeid' expression. */
4151 saved_message = parser->type_definition_forbidden_message;
4152 parser->type_definition_forbidden_message
4153 = "types may not be defined in a `typeid\' expression";
4154 /* We can't be sure yet whether we're looking at a type-id or an
4155 expression. */
4156 cp_parser_parse_tentatively (parser);
4157 /* Try a type-id first. */
4158 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4159 parser->in_type_id_in_expr_p = true;
4160 type = cp_parser_type_id (parser);
4161 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4162 /* Look for the `)' token. Otherwise, we can't be sure that
4163 we're not looking at an expression: consider `typeid (int
4164 (3))', for example. */
4165 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4166 /* If all went well, simply lookup the type-id. */
4167 if (cp_parser_parse_definitely (parser))
4168 postfix_expression = get_typeid (type);
4169 /* Otherwise, fall back to the expression variant. */
4170 else
4171 {
4172 tree expression;
4173
4174 /* Look for an expression. */
4175 expression = cp_parser_expression (parser, /*cast_p=*/false);
4176 /* Compute its typeid. */
4177 postfix_expression = build_typeid (expression);
4178 /* Look for the `)' token. */
4179 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4180 }
4181 /* Restore the saved message. */
4182 parser->type_definition_forbidden_message = saved_message;
4183 /* `typeid' may not appear in an integral constant expression. */
4184 if (cp_parser_non_integral_constant_expression(parser,
4185 "`typeid' operator"))
4186 return error_mark_node;
4187 }
4188 break;
4189
4190 case RID_TYPENAME:
4191 {
4192 tree type;
4193 /* The syntax permitted here is the same permitted for an
4194 elaborated-type-specifier. */
4195 type = cp_parser_elaborated_type_specifier (parser,
4196 /*is_friend=*/false,
4197 /*is_declaration=*/false);
4198 postfix_expression = cp_parser_functional_cast (parser, type);
4199 }
4200 break;
4201
4202 default:
4203 {
4204 tree type;
4205
4206 /* If the next thing is a simple-type-specifier, we may be
4207 looking at a functional cast. We could also be looking at
4208 an id-expression. So, we try the functional cast, and if
4209 that doesn't work we fall back to the primary-expression. */
4210 cp_parser_parse_tentatively (parser);
4211 /* Look for the simple-type-specifier. */
4212 type = cp_parser_simple_type_specifier (parser,
4213 /*decl_specs=*/NULL,
4214 CP_PARSER_FLAGS_NONE);
4215 /* Parse the cast itself. */
4216 if (!cp_parser_error_occurred (parser))
4217 postfix_expression
4218 = cp_parser_functional_cast (parser, type);
4219 /* If that worked, we're done. */
4220 if (cp_parser_parse_definitely (parser))
4221 break;
4222
4223 /* If the functional-cast didn't work out, try a
4224 compound-literal. */
4225 if (cp_parser_allow_gnu_extensions_p (parser)
4226 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4227 {
4228 VEC(constructor_elt,gc) *initializer_list = NULL;
4229 bool saved_in_type_id_in_expr_p;
4230
4231 cp_parser_parse_tentatively (parser);
4232 /* Consume the `('. */
4233 cp_lexer_consume_token (parser->lexer);
4234 /* Parse the type. */
4235 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4236 parser->in_type_id_in_expr_p = true;
4237 type = cp_parser_type_id (parser);
4238 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4239 /* Look for the `)'. */
4240 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4241 /* Look for the `{'. */
4242 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4243 /* If things aren't going well, there's no need to
4244 keep going. */
4245 if (!cp_parser_error_occurred (parser))
4246 {
4247 bool non_constant_p;
4248 /* Parse the initializer-list. */
4249 initializer_list
4250 = cp_parser_initializer_list (parser, &non_constant_p);
4251 /* Allow a trailing `,'. */
4252 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4253 cp_lexer_consume_token (parser->lexer);
4254 /* Look for the final `}'. */
4255 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4256 }
4257 /* If that worked, we're definitely looking at a
4258 compound-literal expression. */
4259 if (cp_parser_parse_definitely (parser))
4260 {
4261 /* Warn the user that a compound literal is not
4262 allowed in standard C++. */
4263 if (pedantic)
4264 pedwarn ("ISO C++ forbids compound-literals");
4265 /* Form the representation of the compound-literal. */
4266 postfix_expression
4267 = finish_compound_literal (type, initializer_list);
4268 break;
4269 }
4270 }
4271
4272 /* It must be a primary-expression. */
4273 postfix_expression
4274 = cp_parser_primary_expression (parser, address_p, cast_p,
4275 /*template_arg_p=*/false,
4276 &idk);
4277 }
4278 break;
4279 }
4280
4281 /* Keep looping until the postfix-expression is complete. */
4282 while (true)
4283 {
4284 if (idk == CP_ID_KIND_UNQUALIFIED
4285 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4286 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4287 /* It is not a Koenig lookup function call. */
4288 postfix_expression
4289 = unqualified_name_lookup_error (postfix_expression);
4290
4291 /* Peek at the next token. */
4292 token = cp_lexer_peek_token (parser->lexer);
4293
4294 switch (token->type)
4295 {
4296 case CPP_OPEN_SQUARE:
4297 postfix_expression
4298 = cp_parser_postfix_open_square_expression (parser,
4299 postfix_expression,
4300 false);
4301 idk = CP_ID_KIND_NONE;
4302 break;
4303
4304 case CPP_OPEN_PAREN:
4305 /* postfix-expression ( expression-list [opt] ) */
4306 {
4307 bool koenig_p;
4308 bool is_builtin_constant_p;
4309 bool saved_integral_constant_expression_p = false;
4310 bool saved_non_integral_constant_expression_p = false;
4311 tree args;
4312
4313 is_builtin_constant_p
4314 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4315 if (is_builtin_constant_p)
4316 {
4317 /* The whole point of __builtin_constant_p is to allow
4318 non-constant expressions to appear as arguments. */
4319 saved_integral_constant_expression_p
4320 = parser->integral_constant_expression_p;
4321 saved_non_integral_constant_expression_p
4322 = parser->non_integral_constant_expression_p;
4323 parser->integral_constant_expression_p = false;
4324 }
4325 args = (cp_parser_parenthesized_expression_list
4326 (parser, /*is_attribute_list=*/false,
4327 /*cast_p=*/false,
4328 /*non_constant_p=*/NULL));
4329 if (is_builtin_constant_p)
4330 {
4331 parser->integral_constant_expression_p
4332 = saved_integral_constant_expression_p;
4333 parser->non_integral_constant_expression_p
4334 = saved_non_integral_constant_expression_p;
4335 }
4336
4337 if (args == error_mark_node)
4338 {
4339 postfix_expression = error_mark_node;
4340 break;
4341 }
4342
4343 /* Function calls are not permitted in
4344 constant-expressions. */
4345 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4346 && cp_parser_non_integral_constant_expression (parser,
4347 "a function call"))
4348 {
4349 postfix_expression = error_mark_node;
4350 break;
4351 }
4352
4353 koenig_p = false;
4354 if (idk == CP_ID_KIND_UNQUALIFIED)
4355 {
4356 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4357 {
4358 if (args)
4359 {
4360 koenig_p = true;
4361 postfix_expression
4362 = perform_koenig_lookup (postfix_expression, args);
4363 }
4364 else
4365 postfix_expression
4366 = unqualified_fn_lookup_error (postfix_expression);
4367 }
4368 /* We do not perform argument-dependent lookup if
4369 normal lookup finds a non-function, in accordance
4370 with the expected resolution of DR 218. */
4371 else if (args && is_overloaded_fn (postfix_expression))
4372 {
4373 tree fn = get_first_fn (postfix_expression);
4374
4375 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4376 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4377
4378 /* Only do argument dependent lookup if regular
4379 lookup does not find a set of member functions.
4380 [basic.lookup.koenig]/2a */
4381 if (!DECL_FUNCTION_MEMBER_P (fn))
4382 {
4383 koenig_p = true;
4384 postfix_expression
4385 = perform_koenig_lookup (postfix_expression, args);
4386 }
4387 }
4388 }
4389
4390 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4391 {
4392 tree instance = TREE_OPERAND (postfix_expression, 0);
4393 tree fn = TREE_OPERAND (postfix_expression, 1);
4394
4395 if (processing_template_decl
4396 && (type_dependent_expression_p (instance)
4397 || (!BASELINK_P (fn)
4398 && TREE_CODE (fn) != FIELD_DECL)
4399 || type_dependent_expression_p (fn)
4400 || any_type_dependent_arguments_p (args)))
4401 {
4402 postfix_expression
4403 = build_min_nt (CALL_EXPR, postfix_expression,
4404 args, NULL_TREE);
4405 break;
4406 }
4407
4408 if (BASELINK_P (fn))
4409 postfix_expression
4410 = (build_new_method_call
4411 (instance, fn, args, NULL_TREE,
4412 (idk == CP_ID_KIND_QUALIFIED
4413 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4414 /*fn_p=*/NULL));
4415 else
4416 postfix_expression
4417 = finish_call_expr (postfix_expression, args,
4418 /*disallow_virtual=*/false,
4419 /*koenig_p=*/false);
4420 }
4421 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4422 || TREE_CODE (postfix_expression) == MEMBER_REF
4423 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4424 postfix_expression = (build_offset_ref_call_from_tree
4425 (postfix_expression, args));
4426 else if (idk == CP_ID_KIND_QUALIFIED)
4427 /* A call to a static class member, or a namespace-scope
4428 function. */
4429 postfix_expression
4430 = finish_call_expr (postfix_expression, args,
4431 /*disallow_virtual=*/true,
4432 koenig_p);
4433 else
4434 /* All other function calls. */
4435 postfix_expression
4436 = finish_call_expr (postfix_expression, args,
4437 /*disallow_virtual=*/false,
4438 koenig_p);
4439
4440 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4441 idk = CP_ID_KIND_NONE;
4442 }
4443 break;
4444
4445 case CPP_DOT:
4446 case CPP_DEREF:
4447 /* postfix-expression . template [opt] id-expression
4448 postfix-expression . pseudo-destructor-name
4449 postfix-expression -> template [opt] id-expression
4450 postfix-expression -> pseudo-destructor-name */
4451
4452 /* Consume the `.' or `->' operator. */
4453 cp_lexer_consume_token (parser->lexer);
4454
4455 postfix_expression
4456 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4457 postfix_expression,
4458 false, &idk);
4459 break;
4460
4461 case CPP_PLUS_PLUS:
4462 /* postfix-expression ++ */
4463 /* Consume the `++' token. */
4464 cp_lexer_consume_token (parser->lexer);
4465 /* Generate a representation for the complete expression. */
4466 postfix_expression
4467 = finish_increment_expr (postfix_expression,
4468 POSTINCREMENT_EXPR);
4469 /* Increments may not appear in constant-expressions. */
4470 if (cp_parser_non_integral_constant_expression (parser,
4471 "an increment"))
4472 postfix_expression = error_mark_node;
4473 idk = CP_ID_KIND_NONE;
4474 break;
4475
4476 case CPP_MINUS_MINUS:
4477 /* postfix-expression -- */
4478 /* Consume the `--' token. */
4479 cp_lexer_consume_token (parser->lexer);
4480 /* Generate a representation for the complete expression. */
4481 postfix_expression
4482 = finish_increment_expr (postfix_expression,
4483 POSTDECREMENT_EXPR);
4484 /* Decrements may not appear in constant-expressions. */
4485 if (cp_parser_non_integral_constant_expression (parser,
4486 "a decrement"))
4487 postfix_expression = error_mark_node;
4488 idk = CP_ID_KIND_NONE;
4489 break;
4490
4491 default:
4492 return postfix_expression;
4493 }
4494 }
4495
4496 /* We should never get here. */
4497 gcc_unreachable ();
4498 return error_mark_node;
4499 }
4500
4501 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4502 by cp_parser_builtin_offsetof. We're looking for
4503
4504 postfix-expression [ expression ]
4505
4506 FOR_OFFSETOF is set if we're being called in that context, which
4507 changes how we deal with integer constant expressions. */
4508
4509 static tree
4510 cp_parser_postfix_open_square_expression (cp_parser *parser,
4511 tree postfix_expression,
4512 bool for_offsetof)
4513 {
4514 tree index;
4515
4516 /* Consume the `[' token. */
4517 cp_lexer_consume_token (parser->lexer);
4518
4519 /* Parse the index expression. */
4520 /* ??? For offsetof, there is a question of what to allow here. If
4521 offsetof is not being used in an integral constant expression context,
4522 then we *could* get the right answer by computing the value at runtime.
4523 If we are in an integral constant expression context, then we might
4524 could accept any constant expression; hard to say without analysis.
4525 Rather than open the barn door too wide right away, allow only integer
4526 constant expressions here. */
4527 if (for_offsetof)
4528 index = cp_parser_constant_expression (parser, false, NULL);
4529 else
4530 index = cp_parser_expression (parser, /*cast_p=*/false);
4531
4532 /* Look for the closing `]'. */
4533 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4534
4535 /* Build the ARRAY_REF. */
4536 postfix_expression = grok_array_decl (postfix_expression, index);
4537
4538 /* When not doing offsetof, array references are not permitted in
4539 constant-expressions. */
4540 if (!for_offsetof
4541 && (cp_parser_non_integral_constant_expression
4542 (parser, "an array reference")))
4543 postfix_expression = error_mark_node;
4544
4545 return postfix_expression;
4546 }
4547
4548 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4549 by cp_parser_builtin_offsetof. We're looking for
4550
4551 postfix-expression . template [opt] id-expression
4552 postfix-expression . pseudo-destructor-name
4553 postfix-expression -> template [opt] id-expression
4554 postfix-expression -> pseudo-destructor-name
4555
4556 FOR_OFFSETOF is set if we're being called in that context. That sorta
4557 limits what of the above we'll actually accept, but nevermind.
4558 TOKEN_TYPE is the "." or "->" token, which will already have been
4559 removed from the stream. */
4560
4561 static tree
4562 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4563 enum cpp_ttype token_type,
4564 tree postfix_expression,
4565 bool for_offsetof, cp_id_kind *idk)
4566 {
4567 tree name;
4568 bool dependent_p;
4569 bool pseudo_destructor_p;
4570 tree scope = NULL_TREE;
4571
4572 /* If this is a `->' operator, dereference the pointer. */
4573 if (token_type == CPP_DEREF)
4574 postfix_expression = build_x_arrow (postfix_expression);
4575 /* Check to see whether or not the expression is type-dependent. */
4576 dependent_p = type_dependent_expression_p (postfix_expression);
4577 /* The identifier following the `->' or `.' is not qualified. */
4578 parser->scope = NULL_TREE;
4579 parser->qualifying_scope = NULL_TREE;
4580 parser->object_scope = NULL_TREE;
4581 *idk = CP_ID_KIND_NONE;
4582 /* Enter the scope corresponding to the type of the object
4583 given by the POSTFIX_EXPRESSION. */
4584 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4585 {
4586 scope = TREE_TYPE (postfix_expression);
4587 /* According to the standard, no expression should ever have
4588 reference type. Unfortunately, we do not currently match
4589 the standard in this respect in that our internal representation
4590 of an expression may have reference type even when the standard
4591 says it does not. Therefore, we have to manually obtain the
4592 underlying type here. */
4593 scope = non_reference (scope);
4594 /* The type of the POSTFIX_EXPRESSION must be complete. */
4595 if (scope == unknown_type_node)
4596 {
4597 error ("%qE does not have class type", postfix_expression);
4598 scope = NULL_TREE;
4599 }
4600 else
4601 scope = complete_type_or_else (scope, NULL_TREE);
4602 /* Let the name lookup machinery know that we are processing a
4603 class member access expression. */
4604 parser->context->object_type = scope;
4605 /* If something went wrong, we want to be able to discern that case,
4606 as opposed to the case where there was no SCOPE due to the type
4607 of expression being dependent. */
4608 if (!scope)
4609 scope = error_mark_node;
4610 /* If the SCOPE was erroneous, make the various semantic analysis
4611 functions exit quickly -- and without issuing additional error
4612 messages. */
4613 if (scope == error_mark_node)
4614 postfix_expression = error_mark_node;
4615 }
4616
4617 /* Assume this expression is not a pseudo-destructor access. */
4618 pseudo_destructor_p = false;
4619
4620 /* If the SCOPE is a scalar type, then, if this is a valid program,
4621 we must be looking at a pseudo-destructor-name. */
4622 if (scope && SCALAR_TYPE_P (scope))
4623 {
4624 tree s;
4625 tree type;
4626
4627 cp_parser_parse_tentatively (parser);
4628 /* Parse the pseudo-destructor-name. */
4629 s = NULL_TREE;
4630 cp_parser_pseudo_destructor_name (parser, &s, &type);
4631 if (cp_parser_parse_definitely (parser))
4632 {
4633 pseudo_destructor_p = true;
4634 postfix_expression
4635 = finish_pseudo_destructor_expr (postfix_expression,
4636 s, TREE_TYPE (type));
4637 }
4638 }
4639
4640 if (!pseudo_destructor_p)
4641 {
4642 /* If the SCOPE is not a scalar type, we are looking at an
4643 ordinary class member access expression, rather than a
4644 pseudo-destructor-name. */
4645 bool template_p;
4646 /* Parse the id-expression. */
4647 name = (cp_parser_id_expression
4648 (parser,
4649 cp_parser_optional_template_keyword (parser),
4650 /*check_dependency_p=*/true,
4651 &template_p,
4652 /*declarator_p=*/false,
4653 /*optional_p=*/false));
4654 /* In general, build a SCOPE_REF if the member name is qualified.
4655 However, if the name was not dependent and has already been
4656 resolved; there is no need to build the SCOPE_REF. For example;
4657
4658 struct X { void f(); };
4659 template <typename T> void f(T* t) { t->X::f(); }
4660
4661 Even though "t" is dependent, "X::f" is not and has been resolved
4662 to a BASELINK; there is no need to include scope information. */
4663
4664 /* But we do need to remember that there was an explicit scope for
4665 virtual function calls. */
4666 if (parser->scope)
4667 *idk = CP_ID_KIND_QUALIFIED;
4668
4669 /* If the name is a template-id that names a type, we will get a
4670 TYPE_DECL here. That is invalid code. */
4671 if (TREE_CODE (name) == TYPE_DECL)
4672 {
4673 error ("invalid use of %qD", name);
4674 postfix_expression = error_mark_node;
4675 }
4676 else
4677 {
4678 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4679 {
4680 name = build_qualified_name (/*type=*/NULL_TREE,
4681 parser->scope,
4682 name,
4683 template_p);
4684 parser->scope = NULL_TREE;
4685 parser->qualifying_scope = NULL_TREE;
4686 parser->object_scope = NULL_TREE;
4687 }
4688 if (scope && name && BASELINK_P (name))
4689 adjust_result_of_qualified_name_lookup
4690 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4691 postfix_expression
4692 = finish_class_member_access_expr (postfix_expression, name,
4693 template_p);
4694 }
4695 }
4696
4697 /* We no longer need to look up names in the scope of the object on
4698 the left-hand side of the `.' or `->' operator. */
4699 parser->context->object_type = NULL_TREE;
4700
4701 /* Outside of offsetof, these operators may not appear in
4702 constant-expressions. */
4703 if (!for_offsetof
4704 && (cp_parser_non_integral_constant_expression
4705 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4706 postfix_expression = error_mark_node;
4707
4708 return postfix_expression;
4709 }
4710
4711 /* Parse a parenthesized expression-list.
4712
4713 expression-list:
4714 assignment-expression
4715 expression-list, assignment-expression
4716
4717 attribute-list:
4718 expression-list
4719 identifier
4720 identifier, expression-list
4721
4722 CAST_P is true if this expression is the target of a cast.
4723
4724 Returns a TREE_LIST. The TREE_VALUE of each node is a
4725 representation of an assignment-expression. Note that a TREE_LIST
4726 is returned even if there is only a single expression in the list.
4727 error_mark_node is returned if the ( and or ) are
4728 missing. NULL_TREE is returned on no expressions. The parentheses
4729 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4730 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4731 indicates whether or not all of the expressions in the list were
4732 constant. */
4733
4734 static tree
4735 cp_parser_parenthesized_expression_list (cp_parser* parser,
4736 bool is_attribute_list,
4737 bool cast_p,
4738 bool *non_constant_p)
4739 {
4740 tree expression_list = NULL_TREE;
4741 bool fold_expr_p = is_attribute_list;
4742 tree identifier = NULL_TREE;
4743
4744 /* Assume all the expressions will be constant. */
4745 if (non_constant_p)
4746 *non_constant_p = false;
4747
4748 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4749 return error_mark_node;
4750
4751 /* Consume expressions until there are no more. */
4752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4753 while (true)
4754 {
4755 tree expr;
4756
4757 /* At the beginning of attribute lists, check to see if the
4758 next token is an identifier. */
4759 if (is_attribute_list
4760 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4761 {
4762 cp_token *token;
4763
4764 /* Consume the identifier. */
4765 token = cp_lexer_consume_token (parser->lexer);
4766 /* Save the identifier. */
4767 identifier = token->value;
4768 }
4769 else
4770 {
4771 /* Parse the next assignment-expression. */
4772 if (non_constant_p)
4773 {
4774 bool expr_non_constant_p;
4775 expr = (cp_parser_constant_expression
4776 (parser, /*allow_non_constant_p=*/true,
4777 &expr_non_constant_p));
4778 if (expr_non_constant_p)
4779 *non_constant_p = true;
4780 }
4781 else
4782 expr = cp_parser_assignment_expression (parser, cast_p);
4783
4784 if (fold_expr_p)
4785 expr = fold_non_dependent_expr (expr);
4786
4787 /* Add it to the list. We add error_mark_node
4788 expressions to the list, so that we can still tell if
4789 the correct form for a parenthesized expression-list
4790 is found. That gives better errors. */
4791 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4792
4793 if (expr == error_mark_node)
4794 goto skip_comma;
4795 }
4796
4797 /* After the first item, attribute lists look the same as
4798 expression lists. */
4799 is_attribute_list = false;
4800
4801 get_comma:;
4802 /* If the next token isn't a `,', then we are done. */
4803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4804 break;
4805
4806 /* Otherwise, consume the `,' and keep going. */
4807 cp_lexer_consume_token (parser->lexer);
4808 }
4809
4810 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4811 {
4812 int ending;
4813
4814 skip_comma:;
4815 /* We try and resync to an unnested comma, as that will give the
4816 user better diagnostics. */
4817 ending = cp_parser_skip_to_closing_parenthesis (parser,
4818 /*recovering=*/true,
4819 /*or_comma=*/true,
4820 /*consume_paren=*/true);
4821 if (ending < 0)
4822 goto get_comma;
4823 if (!ending)
4824 return error_mark_node;
4825 }
4826
4827 /* We built up the list in reverse order so we must reverse it now. */
4828 expression_list = nreverse (expression_list);
4829 if (identifier)
4830 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4831
4832 return expression_list;
4833 }
4834
4835 /* Parse a pseudo-destructor-name.
4836
4837 pseudo-destructor-name:
4838 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4839 :: [opt] nested-name-specifier template template-id :: ~ type-name
4840 :: [opt] nested-name-specifier [opt] ~ type-name
4841
4842 If either of the first two productions is used, sets *SCOPE to the
4843 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4844 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4845 or ERROR_MARK_NODE if the parse fails. */
4846
4847 static void
4848 cp_parser_pseudo_destructor_name (cp_parser* parser,
4849 tree* scope,
4850 tree* type)
4851 {
4852 bool nested_name_specifier_p;
4853
4854 /* Assume that things will not work out. */
4855 *type = error_mark_node;
4856
4857 /* Look for the optional `::' operator. */
4858 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4859 /* Look for the optional nested-name-specifier. */
4860 nested_name_specifier_p
4861 = (cp_parser_nested_name_specifier_opt (parser,
4862 /*typename_keyword_p=*/false,
4863 /*check_dependency_p=*/true,
4864 /*type_p=*/false,
4865 /*is_declaration=*/true)
4866 != NULL_TREE);
4867 /* Now, if we saw a nested-name-specifier, we might be doing the
4868 second production. */
4869 if (nested_name_specifier_p
4870 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4871 {
4872 /* Consume the `template' keyword. */
4873 cp_lexer_consume_token (parser->lexer);
4874 /* Parse the template-id. */
4875 cp_parser_template_id (parser,
4876 /*template_keyword_p=*/true,
4877 /*check_dependency_p=*/false,
4878 /*is_declaration=*/true);
4879 /* Look for the `::' token. */
4880 cp_parser_require (parser, CPP_SCOPE, "`::'");
4881 }
4882 /* If the next token is not a `~', then there might be some
4883 additional qualification. */
4884 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4885 {
4886 /* Look for the type-name. */
4887 *scope = TREE_TYPE (cp_parser_type_name (parser));
4888
4889 if (*scope == error_mark_node)
4890 return;
4891
4892 /* If we don't have ::~, then something has gone wrong. Since
4893 the only caller of this function is looking for something
4894 after `.' or `->' after a scalar type, most likely the
4895 program is trying to get a member of a non-aggregate
4896 type. */
4897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4898 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4899 {
4900 cp_parser_error (parser, "request for member of non-aggregate type");
4901 return;
4902 }
4903
4904 /* Look for the `::' token. */
4905 cp_parser_require (parser, CPP_SCOPE, "`::'");
4906 }
4907 else
4908 *scope = NULL_TREE;
4909
4910 /* Look for the `~'. */
4911 cp_parser_require (parser, CPP_COMPL, "`~'");
4912 /* Look for the type-name again. We are not responsible for
4913 checking that it matches the first type-name. */
4914 *type = cp_parser_type_name (parser);
4915 }
4916
4917 /* Parse a unary-expression.
4918
4919 unary-expression:
4920 postfix-expression
4921 ++ cast-expression
4922 -- cast-expression
4923 unary-operator cast-expression
4924 sizeof unary-expression
4925 sizeof ( type-id )
4926 new-expression
4927 delete-expression
4928
4929 GNU Extensions:
4930
4931 unary-expression:
4932 __extension__ cast-expression
4933 __alignof__ unary-expression
4934 __alignof__ ( type-id )
4935 __real__ cast-expression
4936 __imag__ cast-expression
4937 && identifier
4938
4939 ADDRESS_P is true iff the unary-expression is appearing as the
4940 operand of the `&' operator. CAST_P is true if this expression is
4941 the target of a cast.
4942
4943 Returns a representation of the expression. */
4944
4945 static tree
4946 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4947 {
4948 cp_token *token;
4949 enum tree_code unary_operator;
4950
4951 /* Peek at the next token. */
4952 token = cp_lexer_peek_token (parser->lexer);
4953 /* Some keywords give away the kind of expression. */
4954 if (token->type == CPP_KEYWORD)
4955 {
4956 enum rid keyword = token->keyword;
4957
4958 switch (keyword)
4959 {
4960 case RID_ALIGNOF:
4961 case RID_SIZEOF:
4962 {
4963 tree operand;
4964 enum tree_code op;
4965
4966 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4967 /* Consume the token. */
4968 cp_lexer_consume_token (parser->lexer);
4969 /* Parse the operand. */
4970 operand = cp_parser_sizeof_operand (parser, keyword);
4971
4972 if (TYPE_P (operand))
4973 return cxx_sizeof_or_alignof_type (operand, op, true);
4974 else
4975 return cxx_sizeof_or_alignof_expr (operand, op);
4976 }
4977
4978 case RID_NEW:
4979 return cp_parser_new_expression (parser);
4980
4981 case RID_DELETE:
4982 return cp_parser_delete_expression (parser);
4983
4984 case RID_EXTENSION:
4985 {
4986 /* The saved value of the PEDANTIC flag. */
4987 int saved_pedantic;
4988 tree expr;
4989
4990 /* Save away the PEDANTIC flag. */
4991 cp_parser_extension_opt (parser, &saved_pedantic);
4992 /* Parse the cast-expression. */
4993 expr = cp_parser_simple_cast_expression (parser);
4994 /* Restore the PEDANTIC flag. */
4995 pedantic = saved_pedantic;
4996
4997 return expr;
4998 }
4999
5000 case RID_REALPART:
5001 case RID_IMAGPART:
5002 {
5003 tree expression;
5004
5005 /* Consume the `__real__' or `__imag__' token. */
5006 cp_lexer_consume_token (parser->lexer);
5007 /* Parse the cast-expression. */
5008 expression = cp_parser_simple_cast_expression (parser);
5009 /* Create the complete representation. */
5010 return build_x_unary_op ((keyword == RID_REALPART
5011 ? REALPART_EXPR : IMAGPART_EXPR),
5012 expression);
5013 }
5014 break;
5015
5016 default:
5017 break;
5018 }
5019 }
5020
5021 /* Look for the `:: new' and `:: delete', which also signal the
5022 beginning of a new-expression, or delete-expression,
5023 respectively. If the next token is `::', then it might be one of
5024 these. */
5025 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5026 {
5027 enum rid keyword;
5028
5029 /* See if the token after the `::' is one of the keywords in
5030 which we're interested. */
5031 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5032 /* If it's `new', we have a new-expression. */
5033 if (keyword == RID_NEW)
5034 return cp_parser_new_expression (parser);
5035 /* Similarly, for `delete'. */
5036 else if (keyword == RID_DELETE)
5037 return cp_parser_delete_expression (parser);
5038 }
5039
5040 /* Look for a unary operator. */
5041 unary_operator = cp_parser_unary_operator (token);
5042 /* The `++' and `--' operators can be handled similarly, even though
5043 they are not technically unary-operators in the grammar. */
5044 if (unary_operator == ERROR_MARK)
5045 {
5046 if (token->type == CPP_PLUS_PLUS)
5047 unary_operator = PREINCREMENT_EXPR;
5048 else if (token->type == CPP_MINUS_MINUS)
5049 unary_operator = PREDECREMENT_EXPR;
5050 /* Handle the GNU address-of-label extension. */
5051 else if (cp_parser_allow_gnu_extensions_p (parser)
5052 && token->type == CPP_AND_AND)
5053 {
5054 tree identifier;
5055
5056 /* Consume the '&&' token. */
5057 cp_lexer_consume_token (parser->lexer);
5058 /* Look for the identifier. */
5059 identifier = cp_parser_identifier (parser);
5060 /* Create an expression representing the address. */
5061 return finish_label_address_expr (identifier);
5062 }
5063 }
5064 if (unary_operator != ERROR_MARK)
5065 {
5066 tree cast_expression;
5067 tree expression = error_mark_node;
5068 const char *non_constant_p = NULL;
5069
5070 /* Consume the operator token. */
5071 token = cp_lexer_consume_token (parser->lexer);
5072 /* Parse the cast-expression. */
5073 cast_expression
5074 = cp_parser_cast_expression (parser,
5075 unary_operator == ADDR_EXPR,
5076 /*cast_p=*/false);
5077 /* Now, build an appropriate representation. */
5078 switch (unary_operator)
5079 {
5080 case INDIRECT_REF:
5081 non_constant_p = "`*'";
5082 expression = build_x_indirect_ref (cast_expression, "unary *");
5083 break;
5084
5085 case ADDR_EXPR:
5086 non_constant_p = "`&'";
5087 /* Fall through. */
5088 case BIT_NOT_EXPR:
5089 expression = build_x_unary_op (unary_operator, cast_expression);
5090 break;
5091
5092 case PREINCREMENT_EXPR:
5093 case PREDECREMENT_EXPR:
5094 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5095 ? "`++'" : "`--'");
5096 /* Fall through. */
5097 case UNARY_PLUS_EXPR:
5098 case NEGATE_EXPR:
5099 case TRUTH_NOT_EXPR:
5100 expression = finish_unary_op_expr (unary_operator, cast_expression);
5101 break;
5102
5103 default:
5104 gcc_unreachable ();
5105 }
5106
5107 if (non_constant_p
5108 && cp_parser_non_integral_constant_expression (parser,
5109 non_constant_p))
5110 expression = error_mark_node;
5111
5112 return expression;
5113 }
5114
5115 return cp_parser_postfix_expression (parser, address_p, cast_p);
5116 }
5117
5118 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5119 unary-operator, the corresponding tree code is returned. */
5120
5121 static enum tree_code
5122 cp_parser_unary_operator (cp_token* token)
5123 {
5124 switch (token->type)
5125 {
5126 case CPP_MULT:
5127 return INDIRECT_REF;
5128
5129 case CPP_AND:
5130 return ADDR_EXPR;
5131
5132 case CPP_PLUS:
5133 return UNARY_PLUS_EXPR;
5134
5135 case CPP_MINUS:
5136 return NEGATE_EXPR;
5137
5138 case CPP_NOT:
5139 return TRUTH_NOT_EXPR;
5140
5141 case CPP_COMPL:
5142 return BIT_NOT_EXPR;
5143
5144 default:
5145 return ERROR_MARK;
5146 }
5147 }
5148
5149 /* Parse a new-expression.
5150
5151 new-expression:
5152 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5153 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5154
5155 Returns a representation of the expression. */
5156
5157 static tree
5158 cp_parser_new_expression (cp_parser* parser)
5159 {
5160 bool global_scope_p;
5161 tree placement;
5162 tree type;
5163 tree initializer;
5164 tree nelts;
5165
5166 /* Look for the optional `::' operator. */
5167 global_scope_p
5168 = (cp_parser_global_scope_opt (parser,
5169 /*current_scope_valid_p=*/false)
5170 != NULL_TREE);
5171 /* Look for the `new' operator. */
5172 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5173 /* There's no easy way to tell a new-placement from the
5174 `( type-id )' construct. */
5175 cp_parser_parse_tentatively (parser);
5176 /* Look for a new-placement. */
5177 placement = cp_parser_new_placement (parser);
5178 /* If that didn't work out, there's no new-placement. */
5179 if (!cp_parser_parse_definitely (parser))
5180 placement = NULL_TREE;
5181
5182 /* If the next token is a `(', then we have a parenthesized
5183 type-id. */
5184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5185 {
5186 /* Consume the `('. */
5187 cp_lexer_consume_token (parser->lexer);
5188 /* Parse the type-id. */
5189 type = cp_parser_type_id (parser);
5190 /* Look for the closing `)'. */
5191 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5192 /* There should not be a direct-new-declarator in this production,
5193 but GCC used to allowed this, so we check and emit a sensible error
5194 message for this case. */
5195 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5196 {
5197 error ("array bound forbidden after parenthesized type-id");
5198 inform ("try removing the parentheses around the type-id");
5199 cp_parser_direct_new_declarator (parser);
5200 }
5201 nelts = NULL_TREE;
5202 }
5203 /* Otherwise, there must be a new-type-id. */
5204 else
5205 type = cp_parser_new_type_id (parser, &nelts);
5206
5207 /* If the next token is a `(', then we have a new-initializer. */
5208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5209 initializer = cp_parser_new_initializer (parser);
5210 else
5211 initializer = NULL_TREE;
5212
5213 /* A new-expression may not appear in an integral constant
5214 expression. */
5215 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5216 return error_mark_node;
5217
5218 /* Create a representation of the new-expression. */
5219 return build_new (placement, type, nelts, initializer, global_scope_p);
5220 }
5221
5222 /* Parse a new-placement.
5223
5224 new-placement:
5225 ( expression-list )
5226
5227 Returns the same representation as for an expression-list. */
5228
5229 static tree
5230 cp_parser_new_placement (cp_parser* parser)
5231 {
5232 tree expression_list;
5233
5234 /* Parse the expression-list. */
5235 expression_list = (cp_parser_parenthesized_expression_list
5236 (parser, false, /*cast_p=*/false,
5237 /*non_constant_p=*/NULL));
5238
5239 return expression_list;
5240 }
5241
5242 /* Parse a new-type-id.
5243
5244 new-type-id:
5245 type-specifier-seq new-declarator [opt]
5246
5247 Returns the TYPE allocated. If the new-type-id indicates an array
5248 type, *NELTS is set to the number of elements in the last array
5249 bound; the TYPE will not include the last array bound. */
5250
5251 static tree
5252 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5253 {
5254 cp_decl_specifier_seq type_specifier_seq;
5255 cp_declarator *new_declarator;
5256 cp_declarator *declarator;
5257 cp_declarator *outer_declarator;
5258 const char *saved_message;
5259 tree type;
5260
5261 /* The type-specifier sequence must not contain type definitions.
5262 (It cannot contain declarations of new types either, but if they
5263 are not definitions we will catch that because they are not
5264 complete.) */
5265 saved_message = parser->type_definition_forbidden_message;
5266 parser->type_definition_forbidden_message
5267 = "types may not be defined in a new-type-id";
5268 /* Parse the type-specifier-seq. */
5269 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5270 &type_specifier_seq);
5271 /* Restore the old message. */
5272 parser->type_definition_forbidden_message = saved_message;
5273 /* Parse the new-declarator. */
5274 new_declarator = cp_parser_new_declarator_opt (parser);
5275
5276 /* Determine the number of elements in the last array dimension, if
5277 any. */
5278 *nelts = NULL_TREE;
5279 /* Skip down to the last array dimension. */
5280 declarator = new_declarator;
5281 outer_declarator = NULL;
5282 while (declarator && (declarator->kind == cdk_pointer
5283 || declarator->kind == cdk_ptrmem))
5284 {
5285 outer_declarator = declarator;
5286 declarator = declarator->declarator;
5287 }
5288 while (declarator
5289 && declarator->kind == cdk_array
5290 && declarator->declarator
5291 && declarator->declarator->kind == cdk_array)
5292 {
5293 outer_declarator = declarator;
5294 declarator = declarator->declarator;
5295 }
5296
5297 if (declarator && declarator->kind == cdk_array)
5298 {
5299 *nelts = declarator->u.array.bounds;
5300 if (*nelts == error_mark_node)
5301 *nelts = integer_one_node;
5302
5303 if (outer_declarator)
5304 outer_declarator->declarator = declarator->declarator;
5305 else
5306 new_declarator = NULL;
5307 }
5308
5309 type = groktypename (&type_specifier_seq, new_declarator);
5310 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5311 {
5312 *nelts = array_type_nelts_top (type);
5313 type = TREE_TYPE (type);
5314 }
5315 return type;
5316 }
5317
5318 /* Parse an (optional) new-declarator.
5319
5320 new-declarator:
5321 ptr-operator new-declarator [opt]
5322 direct-new-declarator
5323
5324 Returns the declarator. */
5325
5326 static cp_declarator *
5327 cp_parser_new_declarator_opt (cp_parser* parser)
5328 {
5329 enum tree_code code;
5330 tree type;
5331 cp_cv_quals cv_quals;
5332
5333 /* We don't know if there's a ptr-operator next, or not. */
5334 cp_parser_parse_tentatively (parser);
5335 /* Look for a ptr-operator. */
5336 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5337 /* If that worked, look for more new-declarators. */
5338 if (cp_parser_parse_definitely (parser))
5339 {
5340 cp_declarator *declarator;
5341
5342 /* Parse another optional declarator. */
5343 declarator = cp_parser_new_declarator_opt (parser);
5344
5345 /* Create the representation of the declarator. */
5346 if (type)
5347 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5348 else if (code == INDIRECT_REF)
5349 declarator = make_pointer_declarator (cv_quals, declarator);
5350 else
5351 declarator = make_reference_declarator (cv_quals, declarator);
5352
5353 return declarator;
5354 }
5355
5356 /* If the next token is a `[', there is a direct-new-declarator. */
5357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5358 return cp_parser_direct_new_declarator (parser);
5359
5360 return NULL;
5361 }
5362
5363 /* Parse a direct-new-declarator.
5364
5365 direct-new-declarator:
5366 [ expression ]
5367 direct-new-declarator [constant-expression]
5368
5369 */
5370
5371 static cp_declarator *
5372 cp_parser_direct_new_declarator (cp_parser* parser)
5373 {
5374 cp_declarator *declarator = NULL;
5375
5376 while (true)
5377 {
5378 tree expression;
5379
5380 /* Look for the opening `['. */
5381 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5382 /* The first expression is not required to be constant. */
5383 if (!declarator)
5384 {
5385 expression = cp_parser_expression (parser, /*cast_p=*/false);
5386 /* The standard requires that the expression have integral
5387 type. DR 74 adds enumeration types. We believe that the
5388 real intent is that these expressions be handled like the
5389 expression in a `switch' condition, which also allows
5390 classes with a single conversion to integral or
5391 enumeration type. */
5392 if (!processing_template_decl)
5393 {
5394 expression
5395 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5396 expression,
5397 /*complain=*/true);
5398 if (!expression)
5399 {
5400 error ("expression in new-declarator must have integral "
5401 "or enumeration type");
5402 expression = error_mark_node;
5403 }
5404 }
5405 }
5406 /* But all the other expressions must be. */
5407 else
5408 expression
5409 = cp_parser_constant_expression (parser,
5410 /*allow_non_constant=*/false,
5411 NULL);
5412 /* Look for the closing `]'. */
5413 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5414
5415 /* Add this bound to the declarator. */
5416 declarator = make_array_declarator (declarator, expression);
5417
5418 /* If the next token is not a `[', then there are no more
5419 bounds. */
5420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5421 break;
5422 }
5423
5424 return declarator;
5425 }
5426
5427 /* Parse a new-initializer.
5428
5429 new-initializer:
5430 ( expression-list [opt] )
5431
5432 Returns a representation of the expression-list. If there is no
5433 expression-list, VOID_ZERO_NODE is returned. */
5434
5435 static tree
5436 cp_parser_new_initializer (cp_parser* parser)
5437 {
5438 tree expression_list;
5439
5440 expression_list = (cp_parser_parenthesized_expression_list
5441 (parser, false, /*cast_p=*/false,
5442 /*non_constant_p=*/NULL));
5443 if (!expression_list)
5444 expression_list = void_zero_node;
5445
5446 return expression_list;
5447 }
5448
5449 /* Parse a delete-expression.
5450
5451 delete-expression:
5452 :: [opt] delete cast-expression
5453 :: [opt] delete [ ] cast-expression
5454
5455 Returns a representation of the expression. */
5456
5457 static tree
5458 cp_parser_delete_expression (cp_parser* parser)
5459 {
5460 bool global_scope_p;
5461 bool array_p;
5462 tree expression;
5463
5464 /* Look for the optional `::' operator. */
5465 global_scope_p
5466 = (cp_parser_global_scope_opt (parser,
5467 /*current_scope_valid_p=*/false)
5468 != NULL_TREE);
5469 /* Look for the `delete' keyword. */
5470 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5471 /* See if the array syntax is in use. */
5472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5473 {
5474 /* Consume the `[' token. */
5475 cp_lexer_consume_token (parser->lexer);
5476 /* Look for the `]' token. */
5477 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5478 /* Remember that this is the `[]' construct. */
5479 array_p = true;
5480 }
5481 else
5482 array_p = false;
5483
5484 /* Parse the cast-expression. */
5485 expression = cp_parser_simple_cast_expression (parser);
5486
5487 /* A delete-expression may not appear in an integral constant
5488 expression. */
5489 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5490 return error_mark_node;
5491
5492 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5493 }
5494
5495 /* Parse a cast-expression.
5496
5497 cast-expression:
5498 unary-expression
5499 ( type-id ) cast-expression
5500
5501 ADDRESS_P is true iff the unary-expression is appearing as the
5502 operand of the `&' operator. CAST_P is true if this expression is
5503 the target of a cast.
5504
5505 Returns a representation of the expression. */
5506
5507 static tree
5508 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5509 {
5510 /* If it's a `(', then we might be looking at a cast. */
5511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5512 {
5513 tree type = NULL_TREE;
5514 tree expr = NULL_TREE;
5515 bool compound_literal_p;
5516 const char *saved_message;
5517
5518 /* There's no way to know yet whether or not this is a cast.
5519 For example, `(int (3))' is a unary-expression, while `(int)
5520 3' is a cast. So, we resort to parsing tentatively. */
5521 cp_parser_parse_tentatively (parser);
5522 /* Types may not be defined in a cast. */
5523 saved_message = parser->type_definition_forbidden_message;
5524 parser->type_definition_forbidden_message
5525 = "types may not be defined in casts";
5526 /* Consume the `('. */
5527 cp_lexer_consume_token (parser->lexer);
5528 /* A very tricky bit is that `(struct S) { 3 }' is a
5529 compound-literal (which we permit in C++ as an extension).
5530 But, that construct is not a cast-expression -- it is a
5531 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5532 is legal; if the compound-literal were a cast-expression,
5533 you'd need an extra set of parentheses.) But, if we parse
5534 the type-id, and it happens to be a class-specifier, then we
5535 will commit to the parse at that point, because we cannot
5536 undo the action that is done when creating a new class. So,
5537 then we cannot back up and do a postfix-expression.
5538
5539 Therefore, we scan ahead to the closing `)', and check to see
5540 if the token after the `)' is a `{'. If so, we are not
5541 looking at a cast-expression.
5542
5543 Save tokens so that we can put them back. */
5544 cp_lexer_save_tokens (parser->lexer);
5545 /* Skip tokens until the next token is a closing parenthesis.
5546 If we find the closing `)', and the next token is a `{', then
5547 we are looking at a compound-literal. */
5548 compound_literal_p
5549 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5550 /*consume_paren=*/true)
5551 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5552 /* Roll back the tokens we skipped. */
5553 cp_lexer_rollback_tokens (parser->lexer);
5554 /* If we were looking at a compound-literal, simulate an error
5555 so that the call to cp_parser_parse_definitely below will
5556 fail. */
5557 if (compound_literal_p)
5558 cp_parser_simulate_error (parser);
5559 else
5560 {
5561 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5562 parser->in_type_id_in_expr_p = true;
5563 /* Look for the type-id. */
5564 type = cp_parser_type_id (parser);
5565 /* Look for the closing `)'. */
5566 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5567 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5568 }
5569
5570 /* Restore the saved message. */
5571 parser->type_definition_forbidden_message = saved_message;
5572
5573 /* If ok so far, parse the dependent expression. We cannot be
5574 sure it is a cast. Consider `(T ())'. It is a parenthesized
5575 ctor of T, but looks like a cast to function returning T
5576 without a dependent expression. */
5577 if (!cp_parser_error_occurred (parser))
5578 expr = cp_parser_cast_expression (parser,
5579 /*address_p=*/false,
5580 /*cast_p=*/true);
5581
5582 if (cp_parser_parse_definitely (parser))
5583 {
5584 /* Warn about old-style casts, if so requested. */
5585 if (warn_old_style_cast
5586 && !in_system_header
5587 && !VOID_TYPE_P (type)
5588 && current_lang_name != lang_name_c)
5589 warning (OPT_Wold_style_cast, "use of old-style cast");
5590
5591 /* Only type conversions to integral or enumeration types
5592 can be used in constant-expressions. */
5593 if (!cast_valid_in_integral_constant_expression_p (type)
5594 && (cp_parser_non_integral_constant_expression
5595 (parser,
5596 "a cast to a type other than an integral or "
5597 "enumeration type")))
5598 return error_mark_node;
5599
5600 /* Perform the cast. */
5601 expr = build_c_cast (type, expr);
5602 return expr;
5603 }
5604 }
5605
5606 /* If we get here, then it's not a cast, so it must be a
5607 unary-expression. */
5608 return cp_parser_unary_expression (parser, address_p, cast_p);
5609 }
5610
5611 /* Parse a binary expression of the general form:
5612
5613 pm-expression:
5614 cast-expression
5615 pm-expression .* cast-expression
5616 pm-expression ->* cast-expression
5617
5618 multiplicative-expression:
5619 pm-expression
5620 multiplicative-expression * pm-expression
5621 multiplicative-expression / pm-expression
5622 multiplicative-expression % pm-expression
5623
5624 additive-expression:
5625 multiplicative-expression
5626 additive-expression + multiplicative-expression
5627 additive-expression - multiplicative-expression
5628
5629 shift-expression:
5630 additive-expression
5631 shift-expression << additive-expression
5632 shift-expression >> additive-expression
5633
5634 relational-expression:
5635 shift-expression
5636 relational-expression < shift-expression
5637 relational-expression > shift-expression
5638 relational-expression <= shift-expression
5639 relational-expression >= shift-expression
5640
5641 GNU Extension:
5642
5643 relational-expression:
5644 relational-expression <? shift-expression
5645 relational-expression >? shift-expression
5646
5647 equality-expression:
5648 relational-expression
5649 equality-expression == relational-expression
5650 equality-expression != relational-expression
5651
5652 and-expression:
5653 equality-expression
5654 and-expression & equality-expression
5655
5656 exclusive-or-expression:
5657 and-expression
5658 exclusive-or-expression ^ and-expression
5659
5660 inclusive-or-expression:
5661 exclusive-or-expression
5662 inclusive-or-expression | exclusive-or-expression
5663
5664 logical-and-expression:
5665 inclusive-or-expression
5666 logical-and-expression && inclusive-or-expression
5667
5668 logical-or-expression:
5669 logical-and-expression
5670 logical-or-expression || logical-and-expression
5671
5672 All these are implemented with a single function like:
5673
5674 binary-expression:
5675 simple-cast-expression
5676 binary-expression <token> binary-expression
5677
5678 CAST_P is true if this expression is the target of a cast.
5679
5680 The binops_by_token map is used to get the tree codes for each <token> type.
5681 binary-expressions are associated according to a precedence table. */
5682
5683 #define TOKEN_PRECEDENCE(token) \
5684 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5685 ? PREC_NOT_OPERATOR \
5686 : binops_by_token[token->type].prec)
5687
5688 static tree
5689 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5690 {
5691 cp_parser_expression_stack stack;
5692 cp_parser_expression_stack_entry *sp = &stack[0];
5693 tree lhs, rhs;
5694 cp_token *token;
5695 enum tree_code tree_type, lhs_type, rhs_type;
5696 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5697 bool overloaded_p;
5698
5699 /* Parse the first expression. */
5700 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5701 lhs_type = ERROR_MARK;
5702
5703 for (;;)
5704 {
5705 /* Get an operator token. */
5706 token = cp_lexer_peek_token (parser->lexer);
5707
5708 new_prec = TOKEN_PRECEDENCE (token);
5709
5710 /* Popping an entry off the stack means we completed a subexpression:
5711 - either we found a token which is not an operator (`>' where it is not
5712 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5713 will happen repeatedly;
5714 - or, we found an operator which has lower priority. This is the case
5715 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5716 parsing `3 * 4'. */
5717 if (new_prec <= prec)
5718 {
5719 if (sp == stack)
5720 break;
5721 else
5722 goto pop;
5723 }
5724
5725 get_rhs:
5726 tree_type = binops_by_token[token->type].tree_type;
5727
5728 /* We used the operator token. */
5729 cp_lexer_consume_token (parser->lexer);
5730
5731 /* Extract another operand. It may be the RHS of this expression
5732 or the LHS of a new, higher priority expression. */
5733 rhs = cp_parser_simple_cast_expression (parser);
5734 rhs_type = ERROR_MARK;
5735
5736 /* Get another operator token. Look up its precedence to avoid
5737 building a useless (immediately popped) stack entry for common
5738 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5739 token = cp_lexer_peek_token (parser->lexer);
5740 lookahead_prec = TOKEN_PRECEDENCE (token);
5741 if (lookahead_prec > new_prec)
5742 {
5743 /* ... and prepare to parse the RHS of the new, higher priority
5744 expression. Since precedence levels on the stack are
5745 monotonically increasing, we do not have to care about
5746 stack overflows. */
5747 sp->prec = prec;
5748 sp->tree_type = tree_type;
5749 sp->lhs = lhs;
5750 sp->lhs_type = lhs_type;
5751 sp++;
5752 lhs = rhs;
5753 lhs_type = rhs_type;
5754 prec = new_prec;
5755 new_prec = lookahead_prec;
5756 goto get_rhs;
5757
5758 pop:
5759 /* If the stack is not empty, we have parsed into LHS the right side
5760 (`4' in the example above) of an expression we had suspended.
5761 We can use the information on the stack to recover the LHS (`3')
5762 from the stack together with the tree code (`MULT_EXPR'), and
5763 the precedence of the higher level subexpression
5764 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5765 which will be used to actually build the additive expression. */
5766 --sp;
5767 prec = sp->prec;
5768 tree_type = sp->tree_type;
5769 rhs = lhs;
5770 rhs_type = lhs_type;
5771 lhs = sp->lhs;
5772 lhs_type = sp->lhs_type;
5773 }
5774
5775 overloaded_p = false;
5776 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5777 &overloaded_p);
5778 lhs_type = tree_type;
5779
5780 /* If the binary operator required the use of an overloaded operator,
5781 then this expression cannot be an integral constant-expression.
5782 An overloaded operator can be used even if both operands are
5783 otherwise permissible in an integral constant-expression if at
5784 least one of the operands is of enumeration type. */
5785
5786 if (overloaded_p
5787 && (cp_parser_non_integral_constant_expression
5788 (parser, "calls to overloaded operators")))
5789 return error_mark_node;
5790 }
5791
5792 return lhs;
5793 }
5794
5795
5796 /* Parse the `? expression : assignment-expression' part of a
5797 conditional-expression. The LOGICAL_OR_EXPR is the
5798 logical-or-expression that started the conditional-expression.
5799 Returns a representation of the entire conditional-expression.
5800
5801 This routine is used by cp_parser_assignment_expression.
5802
5803 ? expression : assignment-expression
5804
5805 GNU Extensions:
5806
5807 ? : assignment-expression */
5808
5809 static tree
5810 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5811 {
5812 tree expr;
5813 tree assignment_expr;
5814
5815 /* Consume the `?' token. */
5816 cp_lexer_consume_token (parser->lexer);
5817 if (cp_parser_allow_gnu_extensions_p (parser)
5818 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5819 /* Implicit true clause. */
5820 expr = NULL_TREE;
5821 else
5822 /* Parse the expression. */
5823 expr = cp_parser_expression (parser, /*cast_p=*/false);
5824
5825 /* The next token should be a `:'. */
5826 cp_parser_require (parser, CPP_COLON, "`:'");
5827 /* Parse the assignment-expression. */
5828 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5829
5830 /* Build the conditional-expression. */
5831 return build_x_conditional_expr (logical_or_expr,
5832 expr,
5833 assignment_expr);
5834 }
5835
5836 /* Parse an assignment-expression.
5837
5838 assignment-expression:
5839 conditional-expression
5840 logical-or-expression assignment-operator assignment_expression
5841 throw-expression
5842
5843 CAST_P is true if this expression is the target of a cast.
5844
5845 Returns a representation for the expression. */
5846
5847 static tree
5848 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5849 {
5850 tree expr;
5851
5852 /* If the next token is the `throw' keyword, then we're looking at
5853 a throw-expression. */
5854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5855 expr = cp_parser_throw_expression (parser);
5856 /* Otherwise, it must be that we are looking at a
5857 logical-or-expression. */
5858 else
5859 {
5860 /* Parse the binary expressions (logical-or-expression). */
5861 expr = cp_parser_binary_expression (parser, cast_p);
5862 /* If the next token is a `?' then we're actually looking at a
5863 conditional-expression. */
5864 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5865 return cp_parser_question_colon_clause (parser, expr);
5866 else
5867 {
5868 enum tree_code assignment_operator;
5869
5870 /* If it's an assignment-operator, we're using the second
5871 production. */
5872 assignment_operator
5873 = cp_parser_assignment_operator_opt (parser);
5874 if (assignment_operator != ERROR_MARK)
5875 {
5876 tree rhs;
5877
5878 /* Parse the right-hand side of the assignment. */
5879 rhs = cp_parser_assignment_expression (parser, cast_p);
5880 /* An assignment may not appear in a
5881 constant-expression. */
5882 if (cp_parser_non_integral_constant_expression (parser,
5883 "an assignment"))
5884 return error_mark_node;
5885 /* Build the assignment expression. */
5886 expr = build_x_modify_expr (expr,
5887 assignment_operator,
5888 rhs);
5889 }
5890 }
5891 }
5892
5893 return expr;
5894 }
5895
5896 /* Parse an (optional) assignment-operator.
5897
5898 assignment-operator: one of
5899 = *= /= %= += -= >>= <<= &= ^= |=
5900
5901 GNU Extension:
5902
5903 assignment-operator: one of
5904 <?= >?=
5905
5906 If the next token is an assignment operator, the corresponding tree
5907 code is returned, and the token is consumed. For example, for
5908 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5909 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5910 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5911 operator, ERROR_MARK is returned. */
5912
5913 static enum tree_code
5914 cp_parser_assignment_operator_opt (cp_parser* parser)
5915 {
5916 enum tree_code op;
5917 cp_token *token;
5918
5919 /* Peek at the next toen. */
5920 token = cp_lexer_peek_token (parser->lexer);
5921
5922 switch (token->type)
5923 {
5924 case CPP_EQ:
5925 op = NOP_EXPR;
5926 break;
5927
5928 case CPP_MULT_EQ:
5929 op = MULT_EXPR;
5930 break;
5931
5932 case CPP_DIV_EQ:
5933 op = TRUNC_DIV_EXPR;
5934 break;
5935
5936 case CPP_MOD_EQ:
5937 op = TRUNC_MOD_EXPR;
5938 break;
5939
5940 case CPP_PLUS_EQ:
5941 op = PLUS_EXPR;
5942 break;
5943
5944 case CPP_MINUS_EQ:
5945 op = MINUS_EXPR;
5946 break;
5947
5948 case CPP_RSHIFT_EQ:
5949 op = RSHIFT_EXPR;
5950 break;
5951
5952 case CPP_LSHIFT_EQ:
5953 op = LSHIFT_EXPR;
5954 break;
5955
5956 case CPP_AND_EQ:
5957 op = BIT_AND_EXPR;
5958 break;
5959
5960 case CPP_XOR_EQ:
5961 op = BIT_XOR_EXPR;
5962 break;
5963
5964 case CPP_OR_EQ:
5965 op = BIT_IOR_EXPR;
5966 break;
5967
5968 default:
5969 /* Nothing else is an assignment operator. */
5970 op = ERROR_MARK;
5971 }
5972
5973 /* If it was an assignment operator, consume it. */
5974 if (op != ERROR_MARK)
5975 cp_lexer_consume_token (parser->lexer);
5976
5977 return op;
5978 }
5979
5980 /* Parse an expression.
5981
5982 expression:
5983 assignment-expression
5984 expression , assignment-expression
5985
5986 CAST_P is true if this expression is the target of a cast.
5987
5988 Returns a representation of the expression. */
5989
5990 static tree
5991 cp_parser_expression (cp_parser* parser, bool cast_p)
5992 {
5993 tree expression = NULL_TREE;
5994
5995 while (true)
5996 {
5997 tree assignment_expression;
5998
5999 /* Parse the next assignment-expression. */
6000 assignment_expression
6001 = cp_parser_assignment_expression (parser, cast_p);
6002 /* If this is the first assignment-expression, we can just
6003 save it away. */
6004 if (!expression)
6005 expression = assignment_expression;
6006 else
6007 expression = build_x_compound_expr (expression,
6008 assignment_expression);
6009 /* If the next token is not a comma, then we are done with the
6010 expression. */
6011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6012 break;
6013 /* Consume the `,'. */
6014 cp_lexer_consume_token (parser->lexer);
6015 /* A comma operator cannot appear in a constant-expression. */
6016 if (cp_parser_non_integral_constant_expression (parser,
6017 "a comma operator"))
6018 expression = error_mark_node;
6019 }
6020
6021 return expression;
6022 }
6023
6024 /* Parse a constant-expression.
6025
6026 constant-expression:
6027 conditional-expression
6028
6029 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6030 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6031 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6032 is false, NON_CONSTANT_P should be NULL. */
6033
6034 static tree
6035 cp_parser_constant_expression (cp_parser* parser,
6036 bool allow_non_constant_p,
6037 bool *non_constant_p)
6038 {
6039 bool saved_integral_constant_expression_p;
6040 bool saved_allow_non_integral_constant_expression_p;
6041 bool saved_non_integral_constant_expression_p;
6042 tree expression;
6043
6044 /* It might seem that we could simply parse the
6045 conditional-expression, and then check to see if it were
6046 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6047 one that the compiler can figure out is constant, possibly after
6048 doing some simplifications or optimizations. The standard has a
6049 precise definition of constant-expression, and we must honor
6050 that, even though it is somewhat more restrictive.
6051
6052 For example:
6053
6054 int i[(2, 3)];
6055
6056 is not a legal declaration, because `(2, 3)' is not a
6057 constant-expression. The `,' operator is forbidden in a
6058 constant-expression. However, GCC's constant-folding machinery
6059 will fold this operation to an INTEGER_CST for `3'. */
6060
6061 /* Save the old settings. */
6062 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6063 saved_allow_non_integral_constant_expression_p
6064 = parser->allow_non_integral_constant_expression_p;
6065 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6066 /* We are now parsing a constant-expression. */
6067 parser->integral_constant_expression_p = true;
6068 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6069 parser->non_integral_constant_expression_p = false;
6070 /* Although the grammar says "conditional-expression", we parse an
6071 "assignment-expression", which also permits "throw-expression"
6072 and the use of assignment operators. In the case that
6073 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6074 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6075 actually essential that we look for an assignment-expression.
6076 For example, cp_parser_initializer_clauses uses this function to
6077 determine whether a particular assignment-expression is in fact
6078 constant. */
6079 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6080 /* Restore the old settings. */
6081 parser->integral_constant_expression_p
6082 = saved_integral_constant_expression_p;
6083 parser->allow_non_integral_constant_expression_p
6084 = saved_allow_non_integral_constant_expression_p;
6085 if (allow_non_constant_p)
6086 *non_constant_p = parser->non_integral_constant_expression_p;
6087 else if (parser->non_integral_constant_expression_p)
6088 expression = error_mark_node;
6089 parser->non_integral_constant_expression_p
6090 = saved_non_integral_constant_expression_p;
6091
6092 return expression;
6093 }
6094
6095 /* Parse __builtin_offsetof.
6096
6097 offsetof-expression:
6098 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6099
6100 offsetof-member-designator:
6101 id-expression
6102 | offsetof-member-designator "." id-expression
6103 | offsetof-member-designator "[" expression "]" */
6104
6105 static tree
6106 cp_parser_builtin_offsetof (cp_parser *parser)
6107 {
6108 int save_ice_p, save_non_ice_p;
6109 tree type, expr;
6110 cp_id_kind dummy;
6111
6112 /* We're about to accept non-integral-constant things, but will
6113 definitely yield an integral constant expression. Save and
6114 restore these values around our local parsing. */
6115 save_ice_p = parser->integral_constant_expression_p;
6116 save_non_ice_p = parser->non_integral_constant_expression_p;
6117
6118 /* Consume the "__builtin_offsetof" token. */
6119 cp_lexer_consume_token (parser->lexer);
6120 /* Consume the opening `('. */
6121 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6122 /* Parse the type-id. */
6123 type = cp_parser_type_id (parser);
6124 /* Look for the `,'. */
6125 cp_parser_require (parser, CPP_COMMA, "`,'");
6126
6127 /* Build the (type *)null that begins the traditional offsetof macro. */
6128 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6129
6130 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6131 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6132 true, &dummy);
6133 while (true)
6134 {
6135 cp_token *token = cp_lexer_peek_token (parser->lexer);
6136 switch (token->type)
6137 {
6138 case CPP_OPEN_SQUARE:
6139 /* offsetof-member-designator "[" expression "]" */
6140 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6141 break;
6142
6143 case CPP_DOT:
6144 /* offsetof-member-designator "." identifier */
6145 cp_lexer_consume_token (parser->lexer);
6146 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6147 true, &dummy);
6148 break;
6149
6150 case CPP_CLOSE_PAREN:
6151 /* Consume the ")" token. */
6152 cp_lexer_consume_token (parser->lexer);
6153 goto success;
6154
6155 default:
6156 /* Error. We know the following require will fail, but
6157 that gives the proper error message. */
6158 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6159 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6160 expr = error_mark_node;
6161 goto failure;
6162 }
6163 }
6164
6165 success:
6166 /* If we're processing a template, we can't finish the semantics yet.
6167 Otherwise we can fold the entire expression now. */
6168 if (processing_template_decl)
6169 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6170 else
6171 expr = finish_offsetof (expr);
6172
6173 failure:
6174 parser->integral_constant_expression_p = save_ice_p;
6175 parser->non_integral_constant_expression_p = save_non_ice_p;
6176
6177 return expr;
6178 }
6179
6180 /* Statements [gram.stmt.stmt] */
6181
6182 /* Parse a statement.
6183
6184 statement:
6185 labeled-statement
6186 expression-statement
6187 compound-statement
6188 selection-statement
6189 iteration-statement
6190 jump-statement
6191 declaration-statement
6192 try-block
6193
6194 IN_COMPOUND is true when the statement is nested inside a
6195 cp_parser_compound_statement; this matters for certain pragmas.
6196
6197 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6198 is a (possibly labeled) if statement which is not enclosed in braces
6199 and has an else clause. This is used to implement -Wparentheses. */
6200
6201 static void
6202 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6203 bool in_compound, bool *if_p)
6204 {
6205 tree statement;
6206 cp_token *token;
6207 location_t statement_location;
6208
6209 restart:
6210 if (if_p != NULL)
6211 *if_p = false;
6212 /* There is no statement yet. */
6213 statement = NULL_TREE;
6214 /* Peek at the next token. */
6215 token = cp_lexer_peek_token (parser->lexer);
6216 /* Remember the location of the first token in the statement. */
6217 statement_location = token->location;
6218 /* If this is a keyword, then that will often determine what kind of
6219 statement we have. */
6220 if (token->type == CPP_KEYWORD)
6221 {
6222 enum rid keyword = token->keyword;
6223
6224 switch (keyword)
6225 {
6226 case RID_CASE:
6227 case RID_DEFAULT:
6228 /* Looks like a labeled-statement with a case label.
6229 Parse the label, and then use tail recursion to parse
6230 the statement. */
6231 cp_parser_label_for_labeled_statement (parser);
6232 goto restart;
6233
6234 case RID_IF:
6235 case RID_SWITCH:
6236 statement = cp_parser_selection_statement (parser, if_p);
6237 break;
6238
6239 case RID_WHILE:
6240 case RID_DO:
6241 case RID_FOR:
6242 statement = cp_parser_iteration_statement (parser);
6243 break;
6244
6245 case RID_BREAK:
6246 case RID_CONTINUE:
6247 case RID_RETURN:
6248 case RID_GOTO:
6249 statement = cp_parser_jump_statement (parser);
6250 break;
6251
6252 /* Objective-C++ exception-handling constructs. */
6253 case RID_AT_TRY:
6254 case RID_AT_CATCH:
6255 case RID_AT_FINALLY:
6256 case RID_AT_SYNCHRONIZED:
6257 case RID_AT_THROW:
6258 statement = cp_parser_objc_statement (parser);
6259 break;
6260
6261 case RID_TRY:
6262 statement = cp_parser_try_block (parser);
6263 break;
6264
6265 default:
6266 /* It might be a keyword like `int' that can start a
6267 declaration-statement. */
6268 break;
6269 }
6270 }
6271 else if (token->type == CPP_NAME)
6272 {
6273 /* If the next token is a `:', then we are looking at a
6274 labeled-statement. */
6275 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6276 if (token->type == CPP_COLON)
6277 {
6278 /* Looks like a labeled-statement with an ordinary label.
6279 Parse the label, and then use tail recursion to parse
6280 the statement. */
6281 cp_parser_label_for_labeled_statement (parser);
6282 goto restart;
6283 }
6284 }
6285 /* Anything that starts with a `{' must be a compound-statement. */
6286 else if (token->type == CPP_OPEN_BRACE)
6287 statement = cp_parser_compound_statement (parser, NULL, false);
6288 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6289 a statement all its own. */
6290 else if (token->type == CPP_PRAGMA)
6291 {
6292 /* Only certain OpenMP pragmas are attached to statements, and thus
6293 are considered statements themselves. All others are not. In
6294 the context of a compound, accept the pragma as a "statement" and
6295 return so that we can check for a close brace. Otherwise we
6296 require a real statement and must go back and read one. */
6297 if (in_compound)
6298 cp_parser_pragma (parser, pragma_compound);
6299 else if (!cp_parser_pragma (parser, pragma_stmt))
6300 goto restart;
6301 return;
6302 }
6303 else if (token->type == CPP_EOF)
6304 {
6305 cp_parser_error (parser, "expected statement");
6306 return;
6307 }
6308
6309 /* Everything else must be a declaration-statement or an
6310 expression-statement. Try for the declaration-statement
6311 first, unless we are looking at a `;', in which case we know that
6312 we have an expression-statement. */
6313 if (!statement)
6314 {
6315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6316 {
6317 cp_parser_parse_tentatively (parser);
6318 /* Try to parse the declaration-statement. */
6319 cp_parser_declaration_statement (parser);
6320 /* If that worked, we're done. */
6321 if (cp_parser_parse_definitely (parser))
6322 return;
6323 }
6324 /* Look for an expression-statement instead. */
6325 statement = cp_parser_expression_statement (parser, in_statement_expr);
6326 }
6327
6328 /* Set the line number for the statement. */
6329 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6330 SET_EXPR_LOCATION (statement, statement_location);
6331 }
6332
6333 /* Parse the label for a labeled-statement, i.e.
6334
6335 identifier :
6336 case constant-expression :
6337 default :
6338
6339 GNU Extension:
6340 case constant-expression ... constant-expression : statement
6341
6342 When a label is parsed without errors, the label is added to the
6343 parse tree by the finish_* functions, so this function doesn't
6344 have to return the label. */
6345
6346 static void
6347 cp_parser_label_for_labeled_statement (cp_parser* parser)
6348 {
6349 cp_token *token;
6350
6351 /* The next token should be an identifier. */
6352 token = cp_lexer_peek_token (parser->lexer);
6353 if (token->type != CPP_NAME
6354 && token->type != CPP_KEYWORD)
6355 {
6356 cp_parser_error (parser, "expected labeled-statement");
6357 return;
6358 }
6359
6360 switch (token->keyword)
6361 {
6362 case RID_CASE:
6363 {
6364 tree expr, expr_hi;
6365 cp_token *ellipsis;
6366
6367 /* Consume the `case' token. */
6368 cp_lexer_consume_token (parser->lexer);
6369 /* Parse the constant-expression. */
6370 expr = cp_parser_constant_expression (parser,
6371 /*allow_non_constant_p=*/false,
6372 NULL);
6373
6374 ellipsis = cp_lexer_peek_token (parser->lexer);
6375 if (ellipsis->type == CPP_ELLIPSIS)
6376 {
6377 /* Consume the `...' token. */
6378 cp_lexer_consume_token (parser->lexer);
6379 expr_hi =
6380 cp_parser_constant_expression (parser,
6381 /*allow_non_constant_p=*/false,
6382 NULL);
6383 /* We don't need to emit warnings here, as the common code
6384 will do this for us. */
6385 }
6386 else
6387 expr_hi = NULL_TREE;
6388
6389 if (parser->in_switch_statement_p)
6390 finish_case_label (expr, expr_hi);
6391 else
6392 error ("case label %qE not within a switch statement", expr);
6393 }
6394 break;
6395
6396 case RID_DEFAULT:
6397 /* Consume the `default' token. */
6398 cp_lexer_consume_token (parser->lexer);
6399
6400 if (parser->in_switch_statement_p)
6401 finish_case_label (NULL_TREE, NULL_TREE);
6402 else
6403 error ("case label not within a switch statement");
6404 break;
6405
6406 default:
6407 /* Anything else must be an ordinary label. */
6408 finish_label_stmt (cp_parser_identifier (parser));
6409 break;
6410 }
6411
6412 /* Require the `:' token. */
6413 cp_parser_require (parser, CPP_COLON, "`:'");
6414 }
6415
6416 /* Parse an expression-statement.
6417
6418 expression-statement:
6419 expression [opt] ;
6420
6421 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6422 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6423 indicates whether this expression-statement is part of an
6424 expression statement. */
6425
6426 static tree
6427 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6428 {
6429 tree statement = NULL_TREE;
6430
6431 /* If the next token is a ';', then there is no expression
6432 statement. */
6433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6434 statement = cp_parser_expression (parser, /*cast_p=*/false);
6435
6436 /* Consume the final `;'. */
6437 cp_parser_consume_semicolon_at_end_of_statement (parser);
6438
6439 if (in_statement_expr
6440 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6441 /* This is the final expression statement of a statement
6442 expression. */
6443 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6444 else if (statement)
6445 statement = finish_expr_stmt (statement);
6446 else
6447 finish_stmt ();
6448
6449 return statement;
6450 }
6451
6452 /* Parse a compound-statement.
6453
6454 compound-statement:
6455 { statement-seq [opt] }
6456
6457 Returns a tree representing the statement. */
6458
6459 static tree
6460 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6461 bool in_try)
6462 {
6463 tree compound_stmt;
6464
6465 /* Consume the `{'. */
6466 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6467 return error_mark_node;
6468 /* Begin the compound-statement. */
6469 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6470 /* Parse an (optional) statement-seq. */
6471 cp_parser_statement_seq_opt (parser, in_statement_expr);
6472 /* Finish the compound-statement. */
6473 finish_compound_stmt (compound_stmt);
6474 /* Consume the `}'. */
6475 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6476
6477 return compound_stmt;
6478 }
6479
6480 /* Parse an (optional) statement-seq.
6481
6482 statement-seq:
6483 statement
6484 statement-seq [opt] statement */
6485
6486 static void
6487 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6488 {
6489 /* Scan statements until there aren't any more. */
6490 while (true)
6491 {
6492 cp_token *token = cp_lexer_peek_token (parser->lexer);
6493
6494 /* If we're looking at a `}', then we've run out of statements. */
6495 if (token->type == CPP_CLOSE_BRACE
6496 || token->type == CPP_EOF
6497 || token->type == CPP_PRAGMA_EOL)
6498 break;
6499
6500 /* Parse the statement. */
6501 cp_parser_statement (parser, in_statement_expr, true, NULL);
6502 }
6503 }
6504
6505 /* Parse a selection-statement.
6506
6507 selection-statement:
6508 if ( condition ) statement
6509 if ( condition ) statement else statement
6510 switch ( condition ) statement
6511
6512 Returns the new IF_STMT or SWITCH_STMT.
6513
6514 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6515 is a (possibly labeled) if statement which is not enclosed in
6516 braces and has an else clause. This is used to implement
6517 -Wparentheses. */
6518
6519 static tree
6520 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6521 {
6522 cp_token *token;
6523 enum rid keyword;
6524
6525 if (if_p != NULL)
6526 *if_p = false;
6527
6528 /* Peek at the next token. */
6529 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6530
6531 /* See what kind of keyword it is. */
6532 keyword = token->keyword;
6533 switch (keyword)
6534 {
6535 case RID_IF:
6536 case RID_SWITCH:
6537 {
6538 tree statement;
6539 tree condition;
6540
6541 /* Look for the `('. */
6542 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6543 {
6544 cp_parser_skip_to_end_of_statement (parser);
6545 return error_mark_node;
6546 }
6547
6548 /* Begin the selection-statement. */
6549 if (keyword == RID_IF)
6550 statement = begin_if_stmt ();
6551 else
6552 statement = begin_switch_stmt ();
6553
6554 /* Parse the condition. */
6555 condition = cp_parser_condition (parser);
6556 /* Look for the `)'. */
6557 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6558 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6559 /*consume_paren=*/true);
6560
6561 if (keyword == RID_IF)
6562 {
6563 bool nested_if;
6564
6565 /* Add the condition. */
6566 finish_if_stmt_cond (condition, statement);
6567
6568 /* Parse the then-clause. */
6569 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6570 finish_then_clause (statement);
6571
6572 /* If the next token is `else', parse the else-clause. */
6573 if (cp_lexer_next_token_is_keyword (parser->lexer,
6574 RID_ELSE))
6575 {
6576 /* Consume the `else' keyword. */
6577 cp_lexer_consume_token (parser->lexer);
6578 begin_else_clause (statement);
6579 /* Parse the else-clause. */
6580 cp_parser_implicitly_scoped_statement (parser, NULL);
6581 finish_else_clause (statement);
6582
6583 /* If we are currently parsing a then-clause, then
6584 IF_P will not be NULL. We set it to true to
6585 indicate that this if statement has an else clause.
6586 This may trigger the Wparentheses warning below
6587 when we get back up to the parent if statement. */
6588 if (if_p != NULL)
6589 *if_p = true;
6590 }
6591 else
6592 {
6593 /* This if statement does not have an else clause. If
6594 NESTED_IF is true, then the then-clause is an if
6595 statement which does have an else clause. We warn
6596 about the potential ambiguity. */
6597 if (nested_if)
6598 warning (OPT_Wparentheses,
6599 ("%Hsuggest explicit braces "
6600 "to avoid ambiguous %<else%>"),
6601 EXPR_LOCUS (statement));
6602 }
6603
6604 /* Now we're all done with the if-statement. */
6605 finish_if_stmt (statement);
6606 }
6607 else
6608 {
6609 bool in_switch_statement_p;
6610 unsigned char in_statement;
6611
6612 /* Add the condition. */
6613 finish_switch_cond (condition, statement);
6614
6615 /* Parse the body of the switch-statement. */
6616 in_switch_statement_p = parser->in_switch_statement_p;
6617 in_statement = parser->in_statement;
6618 parser->in_switch_statement_p = true;
6619 parser->in_statement |= IN_SWITCH_STMT;
6620 cp_parser_implicitly_scoped_statement (parser, NULL);
6621 parser->in_switch_statement_p = in_switch_statement_p;
6622 parser->in_statement = in_statement;
6623
6624 /* Now we're all done with the switch-statement. */
6625 finish_switch_stmt (statement);
6626 }
6627
6628 return statement;
6629 }
6630 break;
6631
6632 default:
6633 cp_parser_error (parser, "expected selection-statement");
6634 return error_mark_node;
6635 }
6636 }
6637
6638 /* Parse a condition.
6639
6640 condition:
6641 expression
6642 type-specifier-seq declarator = assignment-expression
6643
6644 GNU Extension:
6645
6646 condition:
6647 type-specifier-seq declarator asm-specification [opt]
6648 attributes [opt] = assignment-expression
6649
6650 Returns the expression that should be tested. */
6651
6652 static tree
6653 cp_parser_condition (cp_parser* parser)
6654 {
6655 cp_decl_specifier_seq type_specifiers;
6656 const char *saved_message;
6657
6658 /* Try the declaration first. */
6659 cp_parser_parse_tentatively (parser);
6660 /* New types are not allowed in the type-specifier-seq for a
6661 condition. */
6662 saved_message = parser->type_definition_forbidden_message;
6663 parser->type_definition_forbidden_message
6664 = "types may not be defined in conditions";
6665 /* Parse the type-specifier-seq. */
6666 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6667 &type_specifiers);
6668 /* Restore the saved message. */
6669 parser->type_definition_forbidden_message = saved_message;
6670 /* If all is well, we might be looking at a declaration. */
6671 if (!cp_parser_error_occurred (parser))
6672 {
6673 tree decl;
6674 tree asm_specification;
6675 tree attributes;
6676 cp_declarator *declarator;
6677 tree initializer = NULL_TREE;
6678
6679 /* Parse the declarator. */
6680 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6681 /*ctor_dtor_or_conv_p=*/NULL,
6682 /*parenthesized_p=*/NULL,
6683 /*member_p=*/false);
6684 /* Parse the attributes. */
6685 attributes = cp_parser_attributes_opt (parser);
6686 /* Parse the asm-specification. */
6687 asm_specification = cp_parser_asm_specification_opt (parser);
6688 /* If the next token is not an `=', then we might still be
6689 looking at an expression. For example:
6690
6691 if (A(a).x)
6692
6693 looks like a decl-specifier-seq and a declarator -- but then
6694 there is no `=', so this is an expression. */
6695 cp_parser_require (parser, CPP_EQ, "`='");
6696 /* If we did see an `=', then we are looking at a declaration
6697 for sure. */
6698 if (cp_parser_parse_definitely (parser))
6699 {
6700 tree pushed_scope;
6701 bool non_constant_p;
6702
6703 /* Create the declaration. */
6704 decl = start_decl (declarator, &type_specifiers,
6705 /*initialized_p=*/true,
6706 attributes, /*prefix_attributes=*/NULL_TREE,
6707 &pushed_scope);
6708 /* Parse the assignment-expression. */
6709 initializer
6710 = cp_parser_constant_expression (parser,
6711 /*allow_non_constant_p=*/true,
6712 &non_constant_p);
6713 if (!non_constant_p)
6714 initializer = fold_non_dependent_expr (initializer);
6715
6716 /* Process the initializer. */
6717 cp_finish_decl (decl,
6718 initializer, !non_constant_p,
6719 asm_specification,
6720 LOOKUP_ONLYCONVERTING);
6721
6722 if (pushed_scope)
6723 pop_scope (pushed_scope);
6724
6725 return convert_from_reference (decl);
6726 }
6727 }
6728 /* If we didn't even get past the declarator successfully, we are
6729 definitely not looking at a declaration. */
6730 else
6731 cp_parser_abort_tentative_parse (parser);
6732
6733 /* Otherwise, we are looking at an expression. */
6734 return cp_parser_expression (parser, /*cast_p=*/false);
6735 }
6736
6737 /* Parse an iteration-statement.
6738
6739 iteration-statement:
6740 while ( condition ) statement
6741 do statement while ( expression ) ;
6742 for ( for-init-statement condition [opt] ; expression [opt] )
6743 statement
6744
6745 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6746
6747 static tree
6748 cp_parser_iteration_statement (cp_parser* parser)
6749 {
6750 cp_token *token;
6751 enum rid keyword;
6752 tree statement;
6753 unsigned char in_statement;
6754
6755 /* Peek at the next token. */
6756 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6757 if (!token)
6758 return error_mark_node;
6759
6760 /* Remember whether or not we are already within an iteration
6761 statement. */
6762 in_statement = parser->in_statement;
6763
6764 /* See what kind of keyword it is. */
6765 keyword = token->keyword;
6766 switch (keyword)
6767 {
6768 case RID_WHILE:
6769 {
6770 tree condition;
6771
6772 /* Begin the while-statement. */
6773 statement = begin_while_stmt ();
6774 /* Look for the `('. */
6775 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6776 /* Parse the condition. */
6777 condition = cp_parser_condition (parser);
6778 finish_while_stmt_cond (condition, statement);
6779 /* Look for the `)'. */
6780 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6781 /* Parse the dependent statement. */
6782 parser->in_statement = IN_ITERATION_STMT;
6783 cp_parser_already_scoped_statement (parser);
6784 parser->in_statement = in_statement;
6785 /* We're done with the while-statement. */
6786 finish_while_stmt (statement);
6787 }
6788 break;
6789
6790 case RID_DO:
6791 {
6792 tree expression;
6793
6794 /* Begin the do-statement. */
6795 statement = begin_do_stmt ();
6796 /* Parse the body of the do-statement. */
6797 parser->in_statement = IN_ITERATION_STMT;
6798 cp_parser_implicitly_scoped_statement (parser, NULL);
6799 parser->in_statement = in_statement;
6800 finish_do_body (statement);
6801 /* Look for the `while' keyword. */
6802 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6803 /* Look for the `('. */
6804 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6805 /* Parse the expression. */
6806 expression = cp_parser_expression (parser, /*cast_p=*/false);
6807 /* We're done with the do-statement. */
6808 finish_do_stmt (expression, statement);
6809 /* Look for the `)'. */
6810 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6811 /* Look for the `;'. */
6812 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6813 }
6814 break;
6815
6816 case RID_FOR:
6817 {
6818 tree condition = NULL_TREE;
6819 tree expression = NULL_TREE;
6820
6821 /* Begin the for-statement. */
6822 statement = begin_for_stmt ();
6823 /* Look for the `('. */
6824 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6825 /* Parse the initialization. */
6826 cp_parser_for_init_statement (parser);
6827 finish_for_init_stmt (statement);
6828
6829 /* If there's a condition, process it. */
6830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6831 condition = cp_parser_condition (parser);
6832 finish_for_cond (condition, statement);
6833 /* Look for the `;'. */
6834 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6835
6836 /* If there's an expression, process it. */
6837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6838 expression = cp_parser_expression (parser, /*cast_p=*/false);
6839 finish_for_expr (expression, statement);
6840 /* Look for the `)'. */
6841 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6842
6843 /* Parse the body of the for-statement. */
6844 parser->in_statement = IN_ITERATION_STMT;
6845 cp_parser_already_scoped_statement (parser);
6846 parser->in_statement = in_statement;
6847
6848 /* We're done with the for-statement. */
6849 finish_for_stmt (statement);
6850 }
6851 break;
6852
6853 default:
6854 cp_parser_error (parser, "expected iteration-statement");
6855 statement = error_mark_node;
6856 break;
6857 }
6858
6859 return statement;
6860 }
6861
6862 /* Parse a for-init-statement.
6863
6864 for-init-statement:
6865 expression-statement
6866 simple-declaration */
6867
6868 static void
6869 cp_parser_for_init_statement (cp_parser* parser)
6870 {
6871 /* If the next token is a `;', then we have an empty
6872 expression-statement. Grammatically, this is also a
6873 simple-declaration, but an invalid one, because it does not
6874 declare anything. Therefore, if we did not handle this case
6875 specially, we would issue an error message about an invalid
6876 declaration. */
6877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6878 {
6879 /* We're going to speculatively look for a declaration, falling back
6880 to an expression, if necessary. */
6881 cp_parser_parse_tentatively (parser);
6882 /* Parse the declaration. */
6883 cp_parser_simple_declaration (parser,
6884 /*function_definition_allowed_p=*/false);
6885 /* If the tentative parse failed, then we shall need to look for an
6886 expression-statement. */
6887 if (cp_parser_parse_definitely (parser))
6888 return;
6889 }
6890
6891 cp_parser_expression_statement (parser, false);
6892 }
6893
6894 /* Parse a jump-statement.
6895
6896 jump-statement:
6897 break ;
6898 continue ;
6899 return expression [opt] ;
6900 goto identifier ;
6901
6902 GNU extension:
6903
6904 jump-statement:
6905 goto * expression ;
6906
6907 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6908
6909 static tree
6910 cp_parser_jump_statement (cp_parser* parser)
6911 {
6912 tree statement = error_mark_node;
6913 cp_token *token;
6914 enum rid keyword;
6915
6916 /* Peek at the next token. */
6917 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6918 if (!token)
6919 return error_mark_node;
6920
6921 /* See what kind of keyword it is. */
6922 keyword = token->keyword;
6923 switch (keyword)
6924 {
6925 case RID_BREAK:
6926 switch (parser->in_statement)
6927 {
6928 case 0:
6929 error ("break statement not within loop or switch");
6930 break;
6931 default:
6932 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6933 || parser->in_statement == IN_ITERATION_STMT);
6934 statement = finish_break_stmt ();
6935 break;
6936 case IN_OMP_BLOCK:
6937 error ("invalid exit from OpenMP structured block");
6938 break;
6939 case IN_OMP_FOR:
6940 error ("break statement used with OpenMP for loop");
6941 break;
6942 }
6943 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6944 break;
6945
6946 case RID_CONTINUE:
6947 switch (parser->in_statement & ~IN_SWITCH_STMT)
6948 {
6949 case 0:
6950 error ("continue statement not within a loop");
6951 break;
6952 case IN_ITERATION_STMT:
6953 case IN_OMP_FOR:
6954 statement = finish_continue_stmt ();
6955 break;
6956 case IN_OMP_BLOCK:
6957 error ("invalid exit from OpenMP structured block");
6958 break;
6959 default:
6960 gcc_unreachable ();
6961 }
6962 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6963 break;
6964
6965 case RID_RETURN:
6966 {
6967 tree expr;
6968
6969 /* If the next token is a `;', then there is no
6970 expression. */
6971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6972 expr = cp_parser_expression (parser, /*cast_p=*/false);
6973 else
6974 expr = NULL_TREE;
6975 /* Build the return-statement. */
6976 statement = finish_return_stmt (expr);
6977 /* Look for the final `;'. */
6978 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6979 }
6980 break;
6981
6982 case RID_GOTO:
6983 /* Create the goto-statement. */
6984 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6985 {
6986 /* Issue a warning about this use of a GNU extension. */
6987 if (pedantic)
6988 pedwarn ("ISO C++ forbids computed gotos");
6989 /* Consume the '*' token. */
6990 cp_lexer_consume_token (parser->lexer);
6991 /* Parse the dependent expression. */
6992 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6993 }
6994 else
6995 finish_goto_stmt (cp_parser_identifier (parser));
6996 /* Look for the final `;'. */
6997 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6998 break;
6999
7000 default:
7001 cp_parser_error (parser, "expected jump-statement");
7002 break;
7003 }
7004
7005 return statement;
7006 }
7007
7008 /* Parse a declaration-statement.
7009
7010 declaration-statement:
7011 block-declaration */
7012
7013 static void
7014 cp_parser_declaration_statement (cp_parser* parser)
7015 {
7016 void *p;
7017
7018 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7019 p = obstack_alloc (&declarator_obstack, 0);
7020
7021 /* Parse the block-declaration. */
7022 cp_parser_block_declaration (parser, /*statement_p=*/true);
7023
7024 /* Free any declarators allocated. */
7025 obstack_free (&declarator_obstack, p);
7026
7027 /* Finish off the statement. */
7028 finish_stmt ();
7029 }
7030
7031 /* Some dependent statements (like `if (cond) statement'), are
7032 implicitly in their own scope. In other words, if the statement is
7033 a single statement (as opposed to a compound-statement), it is
7034 none-the-less treated as if it were enclosed in braces. Any
7035 declarations appearing in the dependent statement are out of scope
7036 after control passes that point. This function parses a statement,
7037 but ensures that is in its own scope, even if it is not a
7038 compound-statement.
7039
7040 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7041 is a (possibly labeled) if statement which is not enclosed in
7042 braces and has an else clause. This is used to implement
7043 -Wparentheses.
7044
7045 Returns the new statement. */
7046
7047 static tree
7048 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7049 {
7050 tree statement;
7051
7052 if (if_p != NULL)
7053 *if_p = false;
7054
7055 /* Mark if () ; with a special NOP_EXPR. */
7056 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7057 {
7058 cp_lexer_consume_token (parser->lexer);
7059 statement = add_stmt (build_empty_stmt ());
7060 }
7061 /* if a compound is opened, we simply parse the statement directly. */
7062 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7063 statement = cp_parser_compound_statement (parser, NULL, false);
7064 /* If the token is not a `{', then we must take special action. */
7065 else
7066 {
7067 /* Create a compound-statement. */
7068 statement = begin_compound_stmt (0);
7069 /* Parse the dependent-statement. */
7070 cp_parser_statement (parser, NULL_TREE, false, if_p);
7071 /* Finish the dummy compound-statement. */
7072 finish_compound_stmt (statement);
7073 }
7074
7075 /* Return the statement. */
7076 return statement;
7077 }
7078
7079 /* For some dependent statements (like `while (cond) statement'), we
7080 have already created a scope. Therefore, even if the dependent
7081 statement is a compound-statement, we do not want to create another
7082 scope. */
7083
7084 static void
7085 cp_parser_already_scoped_statement (cp_parser* parser)
7086 {
7087 /* If the token is a `{', then we must take special action. */
7088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7089 cp_parser_statement (parser, NULL_TREE, false, NULL);
7090 else
7091 {
7092 /* Avoid calling cp_parser_compound_statement, so that we
7093 don't create a new scope. Do everything else by hand. */
7094 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7095 cp_parser_statement_seq_opt (parser, NULL_TREE);
7096 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7097 }
7098 }
7099
7100 /* Declarations [gram.dcl.dcl] */
7101
7102 /* Parse an optional declaration-sequence.
7103
7104 declaration-seq:
7105 declaration
7106 declaration-seq declaration */
7107
7108 static void
7109 cp_parser_declaration_seq_opt (cp_parser* parser)
7110 {
7111 while (true)
7112 {
7113 cp_token *token;
7114
7115 token = cp_lexer_peek_token (parser->lexer);
7116
7117 if (token->type == CPP_CLOSE_BRACE
7118 || token->type == CPP_EOF
7119 || token->type == CPP_PRAGMA_EOL)
7120 break;
7121
7122 if (token->type == CPP_SEMICOLON)
7123 {
7124 /* A declaration consisting of a single semicolon is
7125 invalid. Allow it unless we're being pedantic. */
7126 cp_lexer_consume_token (parser->lexer);
7127 if (pedantic && !in_system_header)
7128 pedwarn ("extra %<;%>");
7129 continue;
7130 }
7131
7132 /* If we're entering or exiting a region that's implicitly
7133 extern "C", modify the lang context appropriately. */
7134 if (!parser->implicit_extern_c && token->implicit_extern_c)
7135 {
7136 push_lang_context (lang_name_c);
7137 parser->implicit_extern_c = true;
7138 }
7139 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7140 {
7141 pop_lang_context ();
7142 parser->implicit_extern_c = false;
7143 }
7144
7145 if (token->type == CPP_PRAGMA)
7146 {
7147 /* A top-level declaration can consist solely of a #pragma.
7148 A nested declaration cannot, so this is done here and not
7149 in cp_parser_declaration. (A #pragma at block scope is
7150 handled in cp_parser_statement.) */
7151 cp_parser_pragma (parser, pragma_external);
7152 continue;
7153 }
7154
7155 /* Parse the declaration itself. */
7156 cp_parser_declaration (parser);
7157 }
7158 }
7159
7160 /* Parse a declaration.
7161
7162 declaration:
7163 block-declaration
7164 function-definition
7165 template-declaration
7166 explicit-instantiation
7167 explicit-specialization
7168 linkage-specification
7169 namespace-definition
7170
7171 GNU extension:
7172
7173 declaration:
7174 __extension__ declaration */
7175
7176 static void
7177 cp_parser_declaration (cp_parser* parser)
7178 {
7179 cp_token token1;
7180 cp_token token2;
7181 int saved_pedantic;
7182 void *p;
7183
7184 /* Check for the `__extension__' keyword. */
7185 if (cp_parser_extension_opt (parser, &saved_pedantic))
7186 {
7187 /* Parse the qualified declaration. */
7188 cp_parser_declaration (parser);
7189 /* Restore the PEDANTIC flag. */
7190 pedantic = saved_pedantic;
7191
7192 return;
7193 }
7194
7195 /* Try to figure out what kind of declaration is present. */
7196 token1 = *cp_lexer_peek_token (parser->lexer);
7197
7198 if (token1.type != CPP_EOF)
7199 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7200 else
7201 {
7202 token2.type = CPP_EOF;
7203 token2.keyword = RID_MAX;
7204 }
7205
7206 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7207 p = obstack_alloc (&declarator_obstack, 0);
7208
7209 /* If the next token is `extern' and the following token is a string
7210 literal, then we have a linkage specification. */
7211 if (token1.keyword == RID_EXTERN
7212 && cp_parser_is_string_literal (&token2))
7213 cp_parser_linkage_specification (parser);
7214 /* If the next token is `template', then we have either a template
7215 declaration, an explicit instantiation, or an explicit
7216 specialization. */
7217 else if (token1.keyword == RID_TEMPLATE)
7218 {
7219 /* `template <>' indicates a template specialization. */
7220 if (token2.type == CPP_LESS
7221 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7222 cp_parser_explicit_specialization (parser);
7223 /* `template <' indicates a template declaration. */
7224 else if (token2.type == CPP_LESS)
7225 cp_parser_template_declaration (parser, /*member_p=*/false);
7226 /* Anything else must be an explicit instantiation. */
7227 else
7228 cp_parser_explicit_instantiation (parser);
7229 }
7230 /* If the next token is `export', then we have a template
7231 declaration. */
7232 else if (token1.keyword == RID_EXPORT)
7233 cp_parser_template_declaration (parser, /*member_p=*/false);
7234 /* If the next token is `extern', 'static' or 'inline' and the one
7235 after that is `template', we have a GNU extended explicit
7236 instantiation directive. */
7237 else if (cp_parser_allow_gnu_extensions_p (parser)
7238 && (token1.keyword == RID_EXTERN
7239 || token1.keyword == RID_STATIC
7240 || token1.keyword == RID_INLINE)
7241 && token2.keyword == RID_TEMPLATE)
7242 cp_parser_explicit_instantiation (parser);
7243 /* If the next token is `namespace', check for a named or unnamed
7244 namespace definition. */
7245 else if (token1.keyword == RID_NAMESPACE
7246 && (/* A named namespace definition. */
7247 (token2.type == CPP_NAME
7248 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7249 != CPP_EQ))
7250 /* An unnamed namespace definition. */
7251 || token2.type == CPP_OPEN_BRACE
7252 || token2.keyword == RID_ATTRIBUTE))
7253 cp_parser_namespace_definition (parser);
7254 /* Objective-C++ declaration/definition. */
7255 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7256 cp_parser_objc_declaration (parser);
7257 /* We must have either a block declaration or a function
7258 definition. */
7259 else
7260 /* Try to parse a block-declaration, or a function-definition. */
7261 cp_parser_block_declaration (parser, /*statement_p=*/false);
7262
7263 /* Free any declarators allocated. */
7264 obstack_free (&declarator_obstack, p);
7265 }
7266
7267 /* Parse a block-declaration.
7268
7269 block-declaration:
7270 simple-declaration
7271 asm-definition
7272 namespace-alias-definition
7273 using-declaration
7274 using-directive
7275
7276 GNU Extension:
7277
7278 block-declaration:
7279 __extension__ block-declaration
7280 label-declaration
7281
7282 C++0x Extension:
7283
7284 block-declaration:
7285 static_assert-declaration
7286
7287 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7288 part of a declaration-statement. */
7289
7290 static void
7291 cp_parser_block_declaration (cp_parser *parser,
7292 bool statement_p)
7293 {
7294 cp_token *token1;
7295 int saved_pedantic;
7296
7297 /* Check for the `__extension__' keyword. */
7298 if (cp_parser_extension_opt (parser, &saved_pedantic))
7299 {
7300 /* Parse the qualified declaration. */
7301 cp_parser_block_declaration (parser, statement_p);
7302 /* Restore the PEDANTIC flag. */
7303 pedantic = saved_pedantic;
7304
7305 return;
7306 }
7307
7308 /* Peek at the next token to figure out which kind of declaration is
7309 present. */
7310 token1 = cp_lexer_peek_token (parser->lexer);
7311
7312 /* If the next keyword is `asm', we have an asm-definition. */
7313 if (token1->keyword == RID_ASM)
7314 {
7315 if (statement_p)
7316 cp_parser_commit_to_tentative_parse (parser);
7317 cp_parser_asm_definition (parser);
7318 }
7319 /* If the next keyword is `namespace', we have a
7320 namespace-alias-definition. */
7321 else if (token1->keyword == RID_NAMESPACE)
7322 cp_parser_namespace_alias_definition (parser);
7323 /* If the next keyword is `using', we have either a
7324 using-declaration or a using-directive. */
7325 else if (token1->keyword == RID_USING)
7326 {
7327 cp_token *token2;
7328
7329 if (statement_p)
7330 cp_parser_commit_to_tentative_parse (parser);
7331 /* If the token after `using' is `namespace', then we have a
7332 using-directive. */
7333 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7334 if (token2->keyword == RID_NAMESPACE)
7335 cp_parser_using_directive (parser);
7336 /* Otherwise, it's a using-declaration. */
7337 else
7338 cp_parser_using_declaration (parser,
7339 /*access_declaration_p=*/false);
7340 }
7341 /* If the next keyword is `__label__' we have a label declaration. */
7342 else if (token1->keyword == RID_LABEL)
7343 {
7344 if (statement_p)
7345 cp_parser_commit_to_tentative_parse (parser);
7346 cp_parser_label_declaration (parser);
7347 }
7348 /* If the next token is `static_assert' we have a static assertion. */
7349 else if (token1->keyword == RID_STATIC_ASSERT)
7350 cp_parser_static_assert (parser, /*member_p=*/false);
7351 /* Anything else must be a simple-declaration. */
7352 else
7353 cp_parser_simple_declaration (parser, !statement_p);
7354 }
7355
7356 /* Parse a simple-declaration.
7357
7358 simple-declaration:
7359 decl-specifier-seq [opt] init-declarator-list [opt] ;
7360
7361 init-declarator-list:
7362 init-declarator
7363 init-declarator-list , init-declarator
7364
7365 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7366 function-definition as a simple-declaration. */
7367
7368 static void
7369 cp_parser_simple_declaration (cp_parser* parser,
7370 bool function_definition_allowed_p)
7371 {
7372 cp_decl_specifier_seq decl_specifiers;
7373 int declares_class_or_enum;
7374 bool saw_declarator;
7375
7376 /* Defer access checks until we know what is being declared; the
7377 checks for names appearing in the decl-specifier-seq should be
7378 done as if we were in the scope of the thing being declared. */
7379 push_deferring_access_checks (dk_deferred);
7380
7381 /* Parse the decl-specifier-seq. We have to keep track of whether
7382 or not the decl-specifier-seq declares a named class or
7383 enumeration type, since that is the only case in which the
7384 init-declarator-list is allowed to be empty.
7385
7386 [dcl.dcl]
7387
7388 In a simple-declaration, the optional init-declarator-list can be
7389 omitted only when declaring a class or enumeration, that is when
7390 the decl-specifier-seq contains either a class-specifier, an
7391 elaborated-type-specifier, or an enum-specifier. */
7392 cp_parser_decl_specifier_seq (parser,
7393 CP_PARSER_FLAGS_OPTIONAL,
7394 &decl_specifiers,
7395 &declares_class_or_enum);
7396 /* We no longer need to defer access checks. */
7397 stop_deferring_access_checks ();
7398
7399 /* In a block scope, a valid declaration must always have a
7400 decl-specifier-seq. By not trying to parse declarators, we can
7401 resolve the declaration/expression ambiguity more quickly. */
7402 if (!function_definition_allowed_p
7403 && !decl_specifiers.any_specifiers_p)
7404 {
7405 cp_parser_error (parser, "expected declaration");
7406 goto done;
7407 }
7408
7409 /* If the next two tokens are both identifiers, the code is
7410 erroneous. The usual cause of this situation is code like:
7411
7412 T t;
7413
7414 where "T" should name a type -- but does not. */
7415 if (!decl_specifiers.type
7416 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7417 {
7418 /* If parsing tentatively, we should commit; we really are
7419 looking at a declaration. */
7420 cp_parser_commit_to_tentative_parse (parser);
7421 /* Give up. */
7422 goto done;
7423 }
7424
7425 /* If we have seen at least one decl-specifier, and the next token
7426 is not a parenthesis, then we must be looking at a declaration.
7427 (After "int (" we might be looking at a functional cast.) */
7428 if (decl_specifiers.any_specifiers_p
7429 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7430 cp_parser_commit_to_tentative_parse (parser);
7431
7432 /* Keep going until we hit the `;' at the end of the simple
7433 declaration. */
7434 saw_declarator = false;
7435 while (cp_lexer_next_token_is_not (parser->lexer,
7436 CPP_SEMICOLON))
7437 {
7438 cp_token *token;
7439 bool function_definition_p;
7440 tree decl;
7441
7442 if (saw_declarator)
7443 {
7444 /* If we are processing next declarator, coma is expected */
7445 token = cp_lexer_peek_token (parser->lexer);
7446 gcc_assert (token->type == CPP_COMMA);
7447 cp_lexer_consume_token (parser->lexer);
7448 }
7449 else
7450 saw_declarator = true;
7451
7452 /* Parse the init-declarator. */
7453 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7454 /*checks=*/NULL_TREE,
7455 function_definition_allowed_p,
7456 /*member_p=*/false,
7457 declares_class_or_enum,
7458 &function_definition_p);
7459 /* If an error occurred while parsing tentatively, exit quickly.
7460 (That usually happens when in the body of a function; each
7461 statement is treated as a declaration-statement until proven
7462 otherwise.) */
7463 if (cp_parser_error_occurred (parser))
7464 goto done;
7465 /* Handle function definitions specially. */
7466 if (function_definition_p)
7467 {
7468 /* If the next token is a `,', then we are probably
7469 processing something like:
7470
7471 void f() {}, *p;
7472
7473 which is erroneous. */
7474 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7475 error ("mixing declarations and function-definitions is forbidden");
7476 /* Otherwise, we're done with the list of declarators. */
7477 else
7478 {
7479 pop_deferring_access_checks ();
7480 return;
7481 }
7482 }
7483 /* The next token should be either a `,' or a `;'. */
7484 token = cp_lexer_peek_token (parser->lexer);
7485 /* If it's a `,', there are more declarators to come. */
7486 if (token->type == CPP_COMMA)
7487 /* will be consumed next time around */;
7488 /* If it's a `;', we are done. */
7489 else if (token->type == CPP_SEMICOLON)
7490 break;
7491 /* Anything else is an error. */
7492 else
7493 {
7494 /* If we have already issued an error message we don't need
7495 to issue another one. */
7496 if (decl != error_mark_node
7497 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7498 cp_parser_error (parser, "expected %<,%> or %<;%>");
7499 /* Skip tokens until we reach the end of the statement. */
7500 cp_parser_skip_to_end_of_statement (parser);
7501 /* If the next token is now a `;', consume it. */
7502 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7503 cp_lexer_consume_token (parser->lexer);
7504 goto done;
7505 }
7506 /* After the first time around, a function-definition is not
7507 allowed -- even if it was OK at first. For example:
7508
7509 int i, f() {}
7510
7511 is not valid. */
7512 function_definition_allowed_p = false;
7513 }
7514
7515 /* Issue an error message if no declarators are present, and the
7516 decl-specifier-seq does not itself declare a class or
7517 enumeration. */
7518 if (!saw_declarator)
7519 {
7520 if (cp_parser_declares_only_class_p (parser))
7521 shadow_tag (&decl_specifiers);
7522 /* Perform any deferred access checks. */
7523 perform_deferred_access_checks ();
7524 }
7525
7526 /* Consume the `;'. */
7527 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7528
7529 done:
7530 pop_deferring_access_checks ();
7531 }
7532
7533 /* Parse a decl-specifier-seq.
7534
7535 decl-specifier-seq:
7536 decl-specifier-seq [opt] decl-specifier
7537
7538 decl-specifier:
7539 storage-class-specifier
7540 type-specifier
7541 function-specifier
7542 friend
7543 typedef
7544
7545 GNU Extension:
7546
7547 decl-specifier:
7548 attributes
7549
7550 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7551
7552 The parser flags FLAGS is used to control type-specifier parsing.
7553
7554 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7555 flags:
7556
7557 1: one of the decl-specifiers is an elaborated-type-specifier
7558 (i.e., a type declaration)
7559 2: one of the decl-specifiers is an enum-specifier or a
7560 class-specifier (i.e., a type definition)
7561
7562 */
7563
7564 static void
7565 cp_parser_decl_specifier_seq (cp_parser* parser,
7566 cp_parser_flags flags,
7567 cp_decl_specifier_seq *decl_specs,
7568 int* declares_class_or_enum)
7569 {
7570 bool constructor_possible_p = !parser->in_declarator_p;
7571
7572 /* Clear DECL_SPECS. */
7573 clear_decl_specs (decl_specs);
7574
7575 /* Assume no class or enumeration type is declared. */
7576 *declares_class_or_enum = 0;
7577
7578 /* Keep reading specifiers until there are no more to read. */
7579 while (true)
7580 {
7581 bool constructor_p;
7582 bool found_decl_spec;
7583 cp_token *token;
7584
7585 /* Peek at the next token. */
7586 token = cp_lexer_peek_token (parser->lexer);
7587 /* Handle attributes. */
7588 if (token->keyword == RID_ATTRIBUTE)
7589 {
7590 /* Parse the attributes. */
7591 decl_specs->attributes
7592 = chainon (decl_specs->attributes,
7593 cp_parser_attributes_opt (parser));
7594 continue;
7595 }
7596 /* Assume we will find a decl-specifier keyword. */
7597 found_decl_spec = true;
7598 /* If the next token is an appropriate keyword, we can simply
7599 add it to the list. */
7600 switch (token->keyword)
7601 {
7602 /* decl-specifier:
7603 friend */
7604 case RID_FRIEND:
7605 if (!at_class_scope_p ())
7606 {
7607 error ("%<friend%> used outside of class");
7608 cp_lexer_purge_token (parser->lexer);
7609 }
7610 else
7611 {
7612 ++decl_specs->specs[(int) ds_friend];
7613 /* Consume the token. */
7614 cp_lexer_consume_token (parser->lexer);
7615 }
7616 break;
7617
7618 /* function-specifier:
7619 inline
7620 virtual
7621 explicit */
7622 case RID_INLINE:
7623 case RID_VIRTUAL:
7624 case RID_EXPLICIT:
7625 cp_parser_function_specifier_opt (parser, decl_specs);
7626 break;
7627
7628 /* decl-specifier:
7629 typedef */
7630 case RID_TYPEDEF:
7631 ++decl_specs->specs[(int) ds_typedef];
7632 /* Consume the token. */
7633 cp_lexer_consume_token (parser->lexer);
7634 /* A constructor declarator cannot appear in a typedef. */
7635 constructor_possible_p = false;
7636 /* The "typedef" keyword can only occur in a declaration; we
7637 may as well commit at this point. */
7638 cp_parser_commit_to_tentative_parse (parser);
7639
7640 if (decl_specs->storage_class != sc_none)
7641 decl_specs->conflicting_specifiers_p = true;
7642 break;
7643
7644 /* storage-class-specifier:
7645 auto
7646 register
7647 static
7648 extern
7649 mutable
7650
7651 GNU Extension:
7652 thread */
7653 case RID_AUTO:
7654 case RID_REGISTER:
7655 case RID_STATIC:
7656 case RID_EXTERN:
7657 case RID_MUTABLE:
7658 /* Consume the token. */
7659 cp_lexer_consume_token (parser->lexer);
7660 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7661 break;
7662 case RID_THREAD:
7663 /* Consume the token. */
7664 cp_lexer_consume_token (parser->lexer);
7665 ++decl_specs->specs[(int) ds_thread];
7666 break;
7667
7668 default:
7669 /* We did not yet find a decl-specifier yet. */
7670 found_decl_spec = false;
7671 break;
7672 }
7673
7674 /* Constructors are a special case. The `S' in `S()' is not a
7675 decl-specifier; it is the beginning of the declarator. */
7676 constructor_p
7677 = (!found_decl_spec
7678 && constructor_possible_p
7679 && (cp_parser_constructor_declarator_p
7680 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7681
7682 /* If we don't have a DECL_SPEC yet, then we must be looking at
7683 a type-specifier. */
7684 if (!found_decl_spec && !constructor_p)
7685 {
7686 int decl_spec_declares_class_or_enum;
7687 bool is_cv_qualifier;
7688 tree type_spec;
7689
7690 type_spec
7691 = cp_parser_type_specifier (parser, flags,
7692 decl_specs,
7693 /*is_declaration=*/true,
7694 &decl_spec_declares_class_or_enum,
7695 &is_cv_qualifier);
7696
7697 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7698
7699 /* If this type-specifier referenced a user-defined type
7700 (a typedef, class-name, etc.), then we can't allow any
7701 more such type-specifiers henceforth.
7702
7703 [dcl.spec]
7704
7705 The longest sequence of decl-specifiers that could
7706 possibly be a type name is taken as the
7707 decl-specifier-seq of a declaration. The sequence shall
7708 be self-consistent as described below.
7709
7710 [dcl.type]
7711
7712 As a general rule, at most one type-specifier is allowed
7713 in the complete decl-specifier-seq of a declaration. The
7714 only exceptions are the following:
7715
7716 -- const or volatile can be combined with any other
7717 type-specifier.
7718
7719 -- signed or unsigned can be combined with char, long,
7720 short, or int.
7721
7722 -- ..
7723
7724 Example:
7725
7726 typedef char* Pc;
7727 void g (const int Pc);
7728
7729 Here, Pc is *not* part of the decl-specifier seq; it's
7730 the declarator. Therefore, once we see a type-specifier
7731 (other than a cv-qualifier), we forbid any additional
7732 user-defined types. We *do* still allow things like `int
7733 int' to be considered a decl-specifier-seq, and issue the
7734 error message later. */
7735 if (type_spec && !is_cv_qualifier)
7736 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7737 /* A constructor declarator cannot follow a type-specifier. */
7738 if (type_spec)
7739 {
7740 constructor_possible_p = false;
7741 found_decl_spec = true;
7742 }
7743 }
7744
7745 /* If we still do not have a DECL_SPEC, then there are no more
7746 decl-specifiers. */
7747 if (!found_decl_spec)
7748 break;
7749
7750 decl_specs->any_specifiers_p = true;
7751 /* After we see one decl-specifier, further decl-specifiers are
7752 always optional. */
7753 flags |= CP_PARSER_FLAGS_OPTIONAL;
7754 }
7755
7756 cp_parser_check_decl_spec (decl_specs);
7757
7758 /* Don't allow a friend specifier with a class definition. */
7759 if (decl_specs->specs[(int) ds_friend] != 0
7760 && (*declares_class_or_enum & 2))
7761 error ("class definition may not be declared a friend");
7762 }
7763
7764 /* Parse an (optional) storage-class-specifier.
7765
7766 storage-class-specifier:
7767 auto
7768 register
7769 static
7770 extern
7771 mutable
7772
7773 GNU Extension:
7774
7775 storage-class-specifier:
7776 thread
7777
7778 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7779
7780 static tree
7781 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7782 {
7783 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7784 {
7785 case RID_AUTO:
7786 case RID_REGISTER:
7787 case RID_STATIC:
7788 case RID_EXTERN:
7789 case RID_MUTABLE:
7790 case RID_THREAD:
7791 /* Consume the token. */
7792 return cp_lexer_consume_token (parser->lexer)->value;
7793
7794 default:
7795 return NULL_TREE;
7796 }
7797 }
7798
7799 /* Parse an (optional) function-specifier.
7800
7801 function-specifier:
7802 inline
7803 virtual
7804 explicit
7805
7806 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7807 Updates DECL_SPECS, if it is non-NULL. */
7808
7809 static tree
7810 cp_parser_function_specifier_opt (cp_parser* parser,
7811 cp_decl_specifier_seq *decl_specs)
7812 {
7813 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7814 {
7815 case RID_INLINE:
7816 if (decl_specs)
7817 ++decl_specs->specs[(int) ds_inline];
7818 break;
7819
7820 case RID_VIRTUAL:
7821 /* 14.5.2.3 [temp.mem]
7822
7823 A member function template shall not be virtual. */
7824 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7825 error ("templates may not be %<virtual%>");
7826 else if (decl_specs)
7827 ++decl_specs->specs[(int) ds_virtual];
7828 break;
7829
7830 case RID_EXPLICIT:
7831 if (decl_specs)
7832 ++decl_specs->specs[(int) ds_explicit];
7833 break;
7834
7835 default:
7836 return NULL_TREE;
7837 }
7838
7839 /* Consume the token. */
7840 return cp_lexer_consume_token (parser->lexer)->value;
7841 }
7842
7843 /* Parse a linkage-specification.
7844
7845 linkage-specification:
7846 extern string-literal { declaration-seq [opt] }
7847 extern string-literal declaration */
7848
7849 static void
7850 cp_parser_linkage_specification (cp_parser* parser)
7851 {
7852 tree linkage;
7853
7854 /* Look for the `extern' keyword. */
7855 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7856
7857 /* Look for the string-literal. */
7858 linkage = cp_parser_string_literal (parser, false, false);
7859
7860 /* Transform the literal into an identifier. If the literal is a
7861 wide-character string, or contains embedded NULs, then we can't
7862 handle it as the user wants. */
7863 if (strlen (TREE_STRING_POINTER (linkage))
7864 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7865 {
7866 cp_parser_error (parser, "invalid linkage-specification");
7867 /* Assume C++ linkage. */
7868 linkage = lang_name_cplusplus;
7869 }
7870 else
7871 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7872
7873 /* We're now using the new linkage. */
7874 push_lang_context (linkage);
7875
7876 /* If the next token is a `{', then we're using the first
7877 production. */
7878 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7879 {
7880 /* Consume the `{' token. */
7881 cp_lexer_consume_token (parser->lexer);
7882 /* Parse the declarations. */
7883 cp_parser_declaration_seq_opt (parser);
7884 /* Look for the closing `}'. */
7885 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7886 }
7887 /* Otherwise, there's just one declaration. */
7888 else
7889 {
7890 bool saved_in_unbraced_linkage_specification_p;
7891
7892 saved_in_unbraced_linkage_specification_p
7893 = parser->in_unbraced_linkage_specification_p;
7894 parser->in_unbraced_linkage_specification_p = true;
7895 cp_parser_declaration (parser);
7896 parser->in_unbraced_linkage_specification_p
7897 = saved_in_unbraced_linkage_specification_p;
7898 }
7899
7900 /* We're done with the linkage-specification. */
7901 pop_lang_context ();
7902 }
7903
7904 /* Parse a static_assert-declaration.
7905
7906 static_assert-declaration:
7907 static_assert ( constant-expression , string-literal ) ;
7908
7909 If MEMBER_P, this static_assert is a class member. */
7910
7911 static void
7912 cp_parser_static_assert(cp_parser *parser, bool member_p)
7913 {
7914 tree condition;
7915 tree message;
7916 cp_token *token;
7917 location_t saved_loc;
7918
7919 /* Peek at the `static_assert' token so we can keep track of exactly
7920 where the static assertion started. */
7921 token = cp_lexer_peek_token (parser->lexer);
7922 saved_loc = token->location;
7923
7924 /* Look for the `static_assert' keyword. */
7925 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
7926 "`static_assert'"))
7927 return;
7928
7929 /* We know we are in a static assertion; commit to any tentative
7930 parse. */
7931 if (cp_parser_parsing_tentatively (parser))
7932 cp_parser_commit_to_tentative_parse (parser);
7933
7934 /* Parse the `(' starting the static assertion condition. */
7935 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7936
7937 /* Parse the constant-expression. */
7938 condition =
7939 cp_parser_constant_expression (parser,
7940 /*allow_non_constant_p=*/false,
7941 /*non_constant_p=*/NULL);
7942
7943 /* Parse the separating `,'. */
7944 cp_parser_require (parser, CPP_COMMA, "`,'");
7945
7946 /* Parse the string-literal message. */
7947 message = cp_parser_string_literal (parser,
7948 /*translate=*/false,
7949 /*wide_ok=*/true);
7950
7951 /* A `)' completes the static assertion. */
7952 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7953 cp_parser_skip_to_closing_parenthesis (parser,
7954 /*recovering=*/true,
7955 /*or_comma=*/false,
7956 /*consume_paren=*/true);
7957
7958 /* A semicolon terminates the declaration. */
7959 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7960
7961 /* Complete the static assertion, which may mean either processing
7962 the static assert now or saving it for template instantiation. */
7963 finish_static_assert (condition, message, saved_loc, member_p);
7964 }
7965
7966 /* Special member functions [gram.special] */
7967
7968 /* Parse a conversion-function-id.
7969
7970 conversion-function-id:
7971 operator conversion-type-id
7972
7973 Returns an IDENTIFIER_NODE representing the operator. */
7974
7975 static tree
7976 cp_parser_conversion_function_id (cp_parser* parser)
7977 {
7978 tree type;
7979 tree saved_scope;
7980 tree saved_qualifying_scope;
7981 tree saved_object_scope;
7982 tree pushed_scope = NULL_TREE;
7983
7984 /* Look for the `operator' token. */
7985 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7986 return error_mark_node;
7987 /* When we parse the conversion-type-id, the current scope will be
7988 reset. However, we need that information in able to look up the
7989 conversion function later, so we save it here. */
7990 saved_scope = parser->scope;
7991 saved_qualifying_scope = parser->qualifying_scope;
7992 saved_object_scope = parser->object_scope;
7993 /* We must enter the scope of the class so that the names of
7994 entities declared within the class are available in the
7995 conversion-type-id. For example, consider:
7996
7997 struct S {
7998 typedef int I;
7999 operator I();
8000 };
8001
8002 S::operator I() { ... }
8003
8004 In order to see that `I' is a type-name in the definition, we
8005 must be in the scope of `S'. */
8006 if (saved_scope)
8007 pushed_scope = push_scope (saved_scope);
8008 /* Parse the conversion-type-id. */
8009 type = cp_parser_conversion_type_id (parser);
8010 /* Leave the scope of the class, if any. */
8011 if (pushed_scope)
8012 pop_scope (pushed_scope);
8013 /* Restore the saved scope. */
8014 parser->scope = saved_scope;
8015 parser->qualifying_scope = saved_qualifying_scope;
8016 parser->object_scope = saved_object_scope;
8017 /* If the TYPE is invalid, indicate failure. */
8018 if (type == error_mark_node)
8019 return error_mark_node;
8020 return mangle_conv_op_name_for_type (type);
8021 }
8022
8023 /* Parse a conversion-type-id:
8024
8025 conversion-type-id:
8026 type-specifier-seq conversion-declarator [opt]
8027
8028 Returns the TYPE specified. */
8029
8030 static tree
8031 cp_parser_conversion_type_id (cp_parser* parser)
8032 {
8033 tree attributes;
8034 cp_decl_specifier_seq type_specifiers;
8035 cp_declarator *declarator;
8036 tree type_specified;
8037
8038 /* Parse the attributes. */
8039 attributes = cp_parser_attributes_opt (parser);
8040 /* Parse the type-specifiers. */
8041 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8042 &type_specifiers);
8043 /* If that didn't work, stop. */
8044 if (type_specifiers.type == error_mark_node)
8045 return error_mark_node;
8046 /* Parse the conversion-declarator. */
8047 declarator = cp_parser_conversion_declarator_opt (parser);
8048
8049 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8050 /*initialized=*/0, &attributes);
8051 if (attributes)
8052 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8053 return type_specified;
8054 }
8055
8056 /* Parse an (optional) conversion-declarator.
8057
8058 conversion-declarator:
8059 ptr-operator conversion-declarator [opt]
8060
8061 */
8062
8063 static cp_declarator *
8064 cp_parser_conversion_declarator_opt (cp_parser* parser)
8065 {
8066 enum tree_code code;
8067 tree class_type;
8068 cp_cv_quals cv_quals;
8069
8070 /* We don't know if there's a ptr-operator next, or not. */
8071 cp_parser_parse_tentatively (parser);
8072 /* Try the ptr-operator. */
8073 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8074 /* If it worked, look for more conversion-declarators. */
8075 if (cp_parser_parse_definitely (parser))
8076 {
8077 cp_declarator *declarator;
8078
8079 /* Parse another optional declarator. */
8080 declarator = cp_parser_conversion_declarator_opt (parser);
8081
8082 /* Create the representation of the declarator. */
8083 if (class_type)
8084 declarator = make_ptrmem_declarator (cv_quals, class_type,
8085 declarator);
8086 else if (code == INDIRECT_REF)
8087 declarator = make_pointer_declarator (cv_quals, declarator);
8088 else
8089 declarator = make_reference_declarator (cv_quals, declarator);
8090
8091 return declarator;
8092 }
8093
8094 return NULL;
8095 }
8096
8097 /* Parse an (optional) ctor-initializer.
8098
8099 ctor-initializer:
8100 : mem-initializer-list
8101
8102 Returns TRUE iff the ctor-initializer was actually present. */
8103
8104 static bool
8105 cp_parser_ctor_initializer_opt (cp_parser* parser)
8106 {
8107 /* If the next token is not a `:', then there is no
8108 ctor-initializer. */
8109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8110 {
8111 /* Do default initialization of any bases and members. */
8112 if (DECL_CONSTRUCTOR_P (current_function_decl))
8113 finish_mem_initializers (NULL_TREE);
8114
8115 return false;
8116 }
8117
8118 /* Consume the `:' token. */
8119 cp_lexer_consume_token (parser->lexer);
8120 /* And the mem-initializer-list. */
8121 cp_parser_mem_initializer_list (parser);
8122
8123 return true;
8124 }
8125
8126 /* Parse a mem-initializer-list.
8127
8128 mem-initializer-list:
8129 mem-initializer
8130 mem-initializer , mem-initializer-list */
8131
8132 static void
8133 cp_parser_mem_initializer_list (cp_parser* parser)
8134 {
8135 tree mem_initializer_list = NULL_TREE;
8136
8137 /* Let the semantic analysis code know that we are starting the
8138 mem-initializer-list. */
8139 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8140 error ("only constructors take base initializers");
8141
8142 /* Loop through the list. */
8143 while (true)
8144 {
8145 tree mem_initializer;
8146
8147 /* Parse the mem-initializer. */
8148 mem_initializer = cp_parser_mem_initializer (parser);
8149 /* Add it to the list, unless it was erroneous. */
8150 if (mem_initializer != error_mark_node)
8151 {
8152 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8153 mem_initializer_list = mem_initializer;
8154 }
8155 /* If the next token is not a `,', we're done. */
8156 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8157 break;
8158 /* Consume the `,' token. */
8159 cp_lexer_consume_token (parser->lexer);
8160 }
8161
8162 /* Perform semantic analysis. */
8163 if (DECL_CONSTRUCTOR_P (current_function_decl))
8164 finish_mem_initializers (mem_initializer_list);
8165 }
8166
8167 /* Parse a mem-initializer.
8168
8169 mem-initializer:
8170 mem-initializer-id ( expression-list [opt] )
8171
8172 GNU extension:
8173
8174 mem-initializer:
8175 ( expression-list [opt] )
8176
8177 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8178 class) or FIELD_DECL (for a non-static data member) to initialize;
8179 the TREE_VALUE is the expression-list. An empty initialization
8180 list is represented by void_list_node. */
8181
8182 static tree
8183 cp_parser_mem_initializer (cp_parser* parser)
8184 {
8185 tree mem_initializer_id;
8186 tree expression_list;
8187 tree member;
8188
8189 /* Find out what is being initialized. */
8190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8191 {
8192 pedwarn ("anachronistic old-style base class initializer");
8193 mem_initializer_id = NULL_TREE;
8194 }
8195 else
8196 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8197 member = expand_member_init (mem_initializer_id);
8198 if (member && !DECL_P (member))
8199 in_base_initializer = 1;
8200
8201 expression_list
8202 = cp_parser_parenthesized_expression_list (parser, false,
8203 /*cast_p=*/false,
8204 /*non_constant_p=*/NULL);
8205 if (expression_list == error_mark_node)
8206 return error_mark_node;
8207 if (!expression_list)
8208 expression_list = void_type_node;
8209
8210 in_base_initializer = 0;
8211
8212 return member ? build_tree_list (member, expression_list) : error_mark_node;
8213 }
8214
8215 /* Parse a mem-initializer-id.
8216
8217 mem-initializer-id:
8218 :: [opt] nested-name-specifier [opt] class-name
8219 identifier
8220
8221 Returns a TYPE indicating the class to be initializer for the first
8222 production. Returns an IDENTIFIER_NODE indicating the data member
8223 to be initialized for the second production. */
8224
8225 static tree
8226 cp_parser_mem_initializer_id (cp_parser* parser)
8227 {
8228 bool global_scope_p;
8229 bool nested_name_specifier_p;
8230 bool template_p = false;
8231 tree id;
8232
8233 /* `typename' is not allowed in this context ([temp.res]). */
8234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8235 {
8236 error ("keyword %<typename%> not allowed in this context (a qualified "
8237 "member initializer is implicitly a type)");
8238 cp_lexer_consume_token (parser->lexer);
8239 }
8240 /* Look for the optional `::' operator. */
8241 global_scope_p
8242 = (cp_parser_global_scope_opt (parser,
8243 /*current_scope_valid_p=*/false)
8244 != NULL_TREE);
8245 /* Look for the optional nested-name-specifier. The simplest way to
8246 implement:
8247
8248 [temp.res]
8249
8250 The keyword `typename' is not permitted in a base-specifier or
8251 mem-initializer; in these contexts a qualified name that
8252 depends on a template-parameter is implicitly assumed to be a
8253 type name.
8254
8255 is to assume that we have seen the `typename' keyword at this
8256 point. */
8257 nested_name_specifier_p
8258 = (cp_parser_nested_name_specifier_opt (parser,
8259 /*typename_keyword_p=*/true,
8260 /*check_dependency_p=*/true,
8261 /*type_p=*/true,
8262 /*is_declaration=*/true)
8263 != NULL_TREE);
8264 if (nested_name_specifier_p)
8265 template_p = cp_parser_optional_template_keyword (parser);
8266 /* If there is a `::' operator or a nested-name-specifier, then we
8267 are definitely looking for a class-name. */
8268 if (global_scope_p || nested_name_specifier_p)
8269 return cp_parser_class_name (parser,
8270 /*typename_keyword_p=*/true,
8271 /*template_keyword_p=*/template_p,
8272 none_type,
8273 /*check_dependency_p=*/true,
8274 /*class_head_p=*/false,
8275 /*is_declaration=*/true);
8276 /* Otherwise, we could also be looking for an ordinary identifier. */
8277 cp_parser_parse_tentatively (parser);
8278 /* Try a class-name. */
8279 id = cp_parser_class_name (parser,
8280 /*typename_keyword_p=*/true,
8281 /*template_keyword_p=*/false,
8282 none_type,
8283 /*check_dependency_p=*/true,
8284 /*class_head_p=*/false,
8285 /*is_declaration=*/true);
8286 /* If we found one, we're done. */
8287 if (cp_parser_parse_definitely (parser))
8288 return id;
8289 /* Otherwise, look for an ordinary identifier. */
8290 return cp_parser_identifier (parser);
8291 }
8292
8293 /* Overloading [gram.over] */
8294
8295 /* Parse an operator-function-id.
8296
8297 operator-function-id:
8298 operator operator
8299
8300 Returns an IDENTIFIER_NODE for the operator which is a
8301 human-readable spelling of the identifier, e.g., `operator +'. */
8302
8303 static tree
8304 cp_parser_operator_function_id (cp_parser* parser)
8305 {
8306 /* Look for the `operator' keyword. */
8307 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8308 return error_mark_node;
8309 /* And then the name of the operator itself. */
8310 return cp_parser_operator (parser);
8311 }
8312
8313 /* Parse an operator.
8314
8315 operator:
8316 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8317 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8318 || ++ -- , ->* -> () []
8319
8320 GNU Extensions:
8321
8322 operator:
8323 <? >? <?= >?=
8324
8325 Returns an IDENTIFIER_NODE for the operator which is a
8326 human-readable spelling of the identifier, e.g., `operator +'. */
8327
8328 static tree
8329 cp_parser_operator (cp_parser* parser)
8330 {
8331 tree id = NULL_TREE;
8332 cp_token *token;
8333
8334 /* Peek at the next token. */
8335 token = cp_lexer_peek_token (parser->lexer);
8336 /* Figure out which operator we have. */
8337 switch (token->type)
8338 {
8339 case CPP_KEYWORD:
8340 {
8341 enum tree_code op;
8342
8343 /* The keyword should be either `new' or `delete'. */
8344 if (token->keyword == RID_NEW)
8345 op = NEW_EXPR;
8346 else if (token->keyword == RID_DELETE)
8347 op = DELETE_EXPR;
8348 else
8349 break;
8350
8351 /* Consume the `new' or `delete' token. */
8352 cp_lexer_consume_token (parser->lexer);
8353
8354 /* Peek at the next token. */
8355 token = cp_lexer_peek_token (parser->lexer);
8356 /* If it's a `[' token then this is the array variant of the
8357 operator. */
8358 if (token->type == CPP_OPEN_SQUARE)
8359 {
8360 /* Consume the `[' token. */
8361 cp_lexer_consume_token (parser->lexer);
8362 /* Look for the `]' token. */
8363 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8364 id = ansi_opname (op == NEW_EXPR
8365 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8366 }
8367 /* Otherwise, we have the non-array variant. */
8368 else
8369 id = ansi_opname (op);
8370
8371 return id;
8372 }
8373
8374 case CPP_PLUS:
8375 id = ansi_opname (PLUS_EXPR);
8376 break;
8377
8378 case CPP_MINUS:
8379 id = ansi_opname (MINUS_EXPR);
8380 break;
8381
8382 case CPP_MULT:
8383 id = ansi_opname (MULT_EXPR);
8384 break;
8385
8386 case CPP_DIV:
8387 id = ansi_opname (TRUNC_DIV_EXPR);
8388 break;
8389
8390 case CPP_MOD:
8391 id = ansi_opname (TRUNC_MOD_EXPR);
8392 break;
8393
8394 case CPP_XOR:
8395 id = ansi_opname (BIT_XOR_EXPR);
8396 break;
8397
8398 case CPP_AND:
8399 id = ansi_opname (BIT_AND_EXPR);
8400 break;
8401
8402 case CPP_OR:
8403 id = ansi_opname (BIT_IOR_EXPR);
8404 break;
8405
8406 case CPP_COMPL:
8407 id = ansi_opname (BIT_NOT_EXPR);
8408 break;
8409
8410 case CPP_NOT:
8411 id = ansi_opname (TRUTH_NOT_EXPR);
8412 break;
8413
8414 case CPP_EQ:
8415 id = ansi_assopname (NOP_EXPR);
8416 break;
8417
8418 case CPP_LESS:
8419 id = ansi_opname (LT_EXPR);
8420 break;
8421
8422 case CPP_GREATER:
8423 id = ansi_opname (GT_EXPR);
8424 break;
8425
8426 case CPP_PLUS_EQ:
8427 id = ansi_assopname (PLUS_EXPR);
8428 break;
8429
8430 case CPP_MINUS_EQ:
8431 id = ansi_assopname (MINUS_EXPR);
8432 break;
8433
8434 case CPP_MULT_EQ:
8435 id = ansi_assopname (MULT_EXPR);
8436 break;
8437
8438 case CPP_DIV_EQ:
8439 id = ansi_assopname (TRUNC_DIV_EXPR);
8440 break;
8441
8442 case CPP_MOD_EQ:
8443 id = ansi_assopname (TRUNC_MOD_EXPR);
8444 break;
8445
8446 case CPP_XOR_EQ:
8447 id = ansi_assopname (BIT_XOR_EXPR);
8448 break;
8449
8450 case CPP_AND_EQ:
8451 id = ansi_assopname (BIT_AND_EXPR);
8452 break;
8453
8454 case CPP_OR_EQ:
8455 id = ansi_assopname (BIT_IOR_EXPR);
8456 break;
8457
8458 case CPP_LSHIFT:
8459 id = ansi_opname (LSHIFT_EXPR);
8460 break;
8461
8462 case CPP_RSHIFT:
8463 id = ansi_opname (RSHIFT_EXPR);
8464 break;
8465
8466 case CPP_LSHIFT_EQ:
8467 id = ansi_assopname (LSHIFT_EXPR);
8468 break;
8469
8470 case CPP_RSHIFT_EQ:
8471 id = ansi_assopname (RSHIFT_EXPR);
8472 break;
8473
8474 case CPP_EQ_EQ:
8475 id = ansi_opname (EQ_EXPR);
8476 break;
8477
8478 case CPP_NOT_EQ:
8479 id = ansi_opname (NE_EXPR);
8480 break;
8481
8482 case CPP_LESS_EQ:
8483 id = ansi_opname (LE_EXPR);
8484 break;
8485
8486 case CPP_GREATER_EQ:
8487 id = ansi_opname (GE_EXPR);
8488 break;
8489
8490 case CPP_AND_AND:
8491 id = ansi_opname (TRUTH_ANDIF_EXPR);
8492 break;
8493
8494 case CPP_OR_OR:
8495 id = ansi_opname (TRUTH_ORIF_EXPR);
8496 break;
8497
8498 case CPP_PLUS_PLUS:
8499 id = ansi_opname (POSTINCREMENT_EXPR);
8500 break;
8501
8502 case CPP_MINUS_MINUS:
8503 id = ansi_opname (PREDECREMENT_EXPR);
8504 break;
8505
8506 case CPP_COMMA:
8507 id = ansi_opname (COMPOUND_EXPR);
8508 break;
8509
8510 case CPP_DEREF_STAR:
8511 id = ansi_opname (MEMBER_REF);
8512 break;
8513
8514 case CPP_DEREF:
8515 id = ansi_opname (COMPONENT_REF);
8516 break;
8517
8518 case CPP_OPEN_PAREN:
8519 /* Consume the `('. */
8520 cp_lexer_consume_token (parser->lexer);
8521 /* Look for the matching `)'. */
8522 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8523 return ansi_opname (CALL_EXPR);
8524
8525 case CPP_OPEN_SQUARE:
8526 /* Consume the `['. */
8527 cp_lexer_consume_token (parser->lexer);
8528 /* Look for the matching `]'. */
8529 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8530 return ansi_opname (ARRAY_REF);
8531
8532 default:
8533 /* Anything else is an error. */
8534 break;
8535 }
8536
8537 /* If we have selected an identifier, we need to consume the
8538 operator token. */
8539 if (id)
8540 cp_lexer_consume_token (parser->lexer);
8541 /* Otherwise, no valid operator name was present. */
8542 else
8543 {
8544 cp_parser_error (parser, "expected operator");
8545 id = error_mark_node;
8546 }
8547
8548 return id;
8549 }
8550
8551 /* Parse a template-declaration.
8552
8553 template-declaration:
8554 export [opt] template < template-parameter-list > declaration
8555
8556 If MEMBER_P is TRUE, this template-declaration occurs within a
8557 class-specifier.
8558
8559 The grammar rule given by the standard isn't correct. What
8560 is really meant is:
8561
8562 template-declaration:
8563 export [opt] template-parameter-list-seq
8564 decl-specifier-seq [opt] init-declarator [opt] ;
8565 export [opt] template-parameter-list-seq
8566 function-definition
8567
8568 template-parameter-list-seq:
8569 template-parameter-list-seq [opt]
8570 template < template-parameter-list > */
8571
8572 static void
8573 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8574 {
8575 /* Check for `export'. */
8576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8577 {
8578 /* Consume the `export' token. */
8579 cp_lexer_consume_token (parser->lexer);
8580 /* Warn that we do not support `export'. */
8581 warning (0, "keyword %<export%> not implemented, and will be ignored");
8582 }
8583
8584 cp_parser_template_declaration_after_export (parser, member_p);
8585 }
8586
8587 /* Parse a template-parameter-list.
8588
8589 template-parameter-list:
8590 template-parameter
8591 template-parameter-list , template-parameter
8592
8593 Returns a TREE_LIST. Each node represents a template parameter.
8594 The nodes are connected via their TREE_CHAINs. */
8595
8596 static tree
8597 cp_parser_template_parameter_list (cp_parser* parser)
8598 {
8599 tree parameter_list = NULL_TREE;
8600
8601 begin_template_parm_list ();
8602 while (true)
8603 {
8604 tree parameter;
8605 cp_token *token;
8606 bool is_non_type;
8607
8608 /* Parse the template-parameter. */
8609 parameter = cp_parser_template_parameter (parser, &is_non_type);
8610 /* Add it to the list. */
8611 if (parameter != error_mark_node)
8612 parameter_list = process_template_parm (parameter_list,
8613 parameter,
8614 is_non_type);
8615 else
8616 {
8617 tree err_parm = build_tree_list (parameter, parameter);
8618 TREE_VALUE (err_parm) = error_mark_node;
8619 parameter_list = chainon (parameter_list, err_parm);
8620 }
8621
8622 /* Peek at the next token. */
8623 token = cp_lexer_peek_token (parser->lexer);
8624 /* If it's not a `,', we're done. */
8625 if (token->type != CPP_COMMA)
8626 break;
8627 /* Otherwise, consume the `,' token. */
8628 cp_lexer_consume_token (parser->lexer);
8629 }
8630
8631 return end_template_parm_list (parameter_list);
8632 }
8633
8634 /* Parse a template-parameter.
8635
8636 template-parameter:
8637 type-parameter
8638 parameter-declaration
8639
8640 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8641 the parameter. The TREE_PURPOSE is the default value, if any.
8642 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8643 iff this parameter is a non-type parameter. */
8644
8645 static tree
8646 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8647 {
8648 cp_token *token;
8649 cp_parameter_declarator *parameter_declarator;
8650 tree parm;
8651
8652 /* Assume it is a type parameter or a template parameter. */
8653 *is_non_type = false;
8654 /* Peek at the next token. */
8655 token = cp_lexer_peek_token (parser->lexer);
8656 /* If it is `class' or `template', we have a type-parameter. */
8657 if (token->keyword == RID_TEMPLATE)
8658 return cp_parser_type_parameter (parser);
8659 /* If it is `class' or `typename' we do not know yet whether it is a
8660 type parameter or a non-type parameter. Consider:
8661
8662 template <typename T, typename T::X X> ...
8663
8664 or:
8665
8666 template <class C, class D*> ...
8667
8668 Here, the first parameter is a type parameter, and the second is
8669 a non-type parameter. We can tell by looking at the token after
8670 the identifier -- if it is a `,', `=', or `>' then we have a type
8671 parameter. */
8672 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8673 {
8674 /* Peek at the token after `class' or `typename'. */
8675 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8676 /* If it's an identifier, skip it. */
8677 if (token->type == CPP_NAME)
8678 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8679 /* Now, see if the token looks like the end of a template
8680 parameter. */
8681 if (token->type == CPP_COMMA
8682 || token->type == CPP_EQ
8683 || token->type == CPP_GREATER)
8684 return cp_parser_type_parameter (parser);
8685 }
8686
8687 /* Otherwise, it is a non-type parameter.
8688
8689 [temp.param]
8690
8691 When parsing a default template-argument for a non-type
8692 template-parameter, the first non-nested `>' is taken as the end
8693 of the template parameter-list rather than a greater-than
8694 operator. */
8695 *is_non_type = true;
8696 parameter_declarator
8697 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8698 /*parenthesized_p=*/NULL);
8699 parm = grokdeclarator (parameter_declarator->declarator,
8700 &parameter_declarator->decl_specifiers,
8701 PARM, /*initialized=*/0,
8702 /*attrlist=*/NULL);
8703 if (parm == error_mark_node)
8704 return error_mark_node;
8705 return build_tree_list (parameter_declarator->default_argument, parm);
8706 }
8707
8708 /* Parse a type-parameter.
8709
8710 type-parameter:
8711 class identifier [opt]
8712 class identifier [opt] = type-id
8713 typename identifier [opt]
8714 typename identifier [opt] = type-id
8715 template < template-parameter-list > class identifier [opt]
8716 template < template-parameter-list > class identifier [opt]
8717 = id-expression
8718
8719 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8720 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8721 the declaration of the parameter. */
8722
8723 static tree
8724 cp_parser_type_parameter (cp_parser* parser)
8725 {
8726 cp_token *token;
8727 tree parameter;
8728
8729 /* Look for a keyword to tell us what kind of parameter this is. */
8730 token = cp_parser_require (parser, CPP_KEYWORD,
8731 "`class', `typename', or `template'");
8732 if (!token)
8733 return error_mark_node;
8734
8735 switch (token->keyword)
8736 {
8737 case RID_CLASS:
8738 case RID_TYPENAME:
8739 {
8740 tree identifier;
8741 tree default_argument;
8742
8743 /* If the next token is an identifier, then it names the
8744 parameter. */
8745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8746 identifier = cp_parser_identifier (parser);
8747 else
8748 identifier = NULL_TREE;
8749
8750 /* Create the parameter. */
8751 parameter = finish_template_type_parm (class_type_node, identifier);
8752
8753 /* If the next token is an `=', we have a default argument. */
8754 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8755 {
8756 /* Consume the `=' token. */
8757 cp_lexer_consume_token (parser->lexer);
8758 /* Parse the default-argument. */
8759 push_deferring_access_checks (dk_no_deferred);
8760 default_argument = cp_parser_type_id (parser);
8761 pop_deferring_access_checks ();
8762 }
8763 else
8764 default_argument = NULL_TREE;
8765
8766 /* Create the combined representation of the parameter and the
8767 default argument. */
8768 parameter = build_tree_list (default_argument, parameter);
8769 }
8770 break;
8771
8772 case RID_TEMPLATE:
8773 {
8774 tree parameter_list;
8775 tree identifier;
8776 tree default_argument;
8777
8778 /* Look for the `<'. */
8779 cp_parser_require (parser, CPP_LESS, "`<'");
8780 /* Parse the template-parameter-list. */
8781 parameter_list = cp_parser_template_parameter_list (parser);
8782 /* Look for the `>'. */
8783 cp_parser_require (parser, CPP_GREATER, "`>'");
8784 /* Look for the `class' keyword. */
8785 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8786 /* If the next token is an `=', then there is a
8787 default-argument. If the next token is a `>', we are at
8788 the end of the parameter-list. If the next token is a `,',
8789 then we are at the end of this parameter. */
8790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8791 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8792 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8793 {
8794 identifier = cp_parser_identifier (parser);
8795 /* Treat invalid names as if the parameter were nameless. */
8796 if (identifier == error_mark_node)
8797 identifier = NULL_TREE;
8798 }
8799 else
8800 identifier = NULL_TREE;
8801
8802 /* Create the template parameter. */
8803 parameter = finish_template_template_parm (class_type_node,
8804 identifier);
8805
8806 /* If the next token is an `=', then there is a
8807 default-argument. */
8808 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8809 {
8810 bool is_template;
8811
8812 /* Consume the `='. */
8813 cp_lexer_consume_token (parser->lexer);
8814 /* Parse the id-expression. */
8815 push_deferring_access_checks (dk_no_deferred);
8816 default_argument
8817 = cp_parser_id_expression (parser,
8818 /*template_keyword_p=*/false,
8819 /*check_dependency_p=*/true,
8820 /*template_p=*/&is_template,
8821 /*declarator_p=*/false,
8822 /*optional_p=*/false);
8823 if (TREE_CODE (default_argument) == TYPE_DECL)
8824 /* If the id-expression was a template-id that refers to
8825 a template-class, we already have the declaration here,
8826 so no further lookup is needed. */
8827 ;
8828 else
8829 /* Look up the name. */
8830 default_argument
8831 = cp_parser_lookup_name (parser, default_argument,
8832 none_type,
8833 /*is_template=*/is_template,
8834 /*is_namespace=*/false,
8835 /*check_dependency=*/true,
8836 /*ambiguous_decls=*/NULL);
8837 /* See if the default argument is valid. */
8838 default_argument
8839 = check_template_template_default_arg (default_argument);
8840 pop_deferring_access_checks ();
8841 }
8842 else
8843 default_argument = NULL_TREE;
8844
8845 /* Create the combined representation of the parameter and the
8846 default argument. */
8847 parameter = build_tree_list (default_argument, parameter);
8848 }
8849 break;
8850
8851 default:
8852 gcc_unreachable ();
8853 break;
8854 }
8855
8856 return parameter;
8857 }
8858
8859 /* Parse a template-id.
8860
8861 template-id:
8862 template-name < template-argument-list [opt] >
8863
8864 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8865 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8866 returned. Otherwise, if the template-name names a function, or set
8867 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8868 names a class, returns a TYPE_DECL for the specialization.
8869
8870 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8871 uninstantiated templates. */
8872
8873 static tree
8874 cp_parser_template_id (cp_parser *parser,
8875 bool template_keyword_p,
8876 bool check_dependency_p,
8877 bool is_declaration)
8878 {
8879 tree template;
8880 tree arguments;
8881 tree template_id;
8882 cp_token_position start_of_id = 0;
8883 tree access_check = NULL_TREE;
8884 cp_token *next_token, *next_token_2;
8885 bool is_identifier;
8886
8887 /* If the next token corresponds to a template-id, there is no need
8888 to reparse it. */
8889 next_token = cp_lexer_peek_token (parser->lexer);
8890 if (next_token->type == CPP_TEMPLATE_ID)
8891 {
8892 tree value;
8893 tree check;
8894
8895 /* Get the stored value. */
8896 value = cp_lexer_consume_token (parser->lexer)->value;
8897 /* Perform any access checks that were deferred. */
8898 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8899 perform_or_defer_access_check (TREE_PURPOSE (check),
8900 TREE_VALUE (check),
8901 TREE_VALUE (check));
8902 /* Return the stored value. */
8903 return TREE_VALUE (value);
8904 }
8905
8906 /* Avoid performing name lookup if there is no possibility of
8907 finding a template-id. */
8908 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8909 || (next_token->type == CPP_NAME
8910 && !cp_parser_nth_token_starts_template_argument_list_p
8911 (parser, 2)))
8912 {
8913 cp_parser_error (parser, "expected template-id");
8914 return error_mark_node;
8915 }
8916
8917 /* Remember where the template-id starts. */
8918 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8919 start_of_id = cp_lexer_token_position (parser->lexer, false);
8920
8921 push_deferring_access_checks (dk_deferred);
8922
8923 /* Parse the template-name. */
8924 is_identifier = false;
8925 template = cp_parser_template_name (parser, template_keyword_p,
8926 check_dependency_p,
8927 is_declaration,
8928 &is_identifier);
8929 if (template == error_mark_node || is_identifier)
8930 {
8931 pop_deferring_access_checks ();
8932 return template;
8933 }
8934
8935 /* If we find the sequence `[:' after a template-name, it's probably
8936 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8937 parse correctly the argument list. */
8938 next_token = cp_lexer_peek_token (parser->lexer);
8939 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8940 if (next_token->type == CPP_OPEN_SQUARE
8941 && next_token->flags & DIGRAPH
8942 && next_token_2->type == CPP_COLON
8943 && !(next_token_2->flags & PREV_WHITE))
8944 {
8945 cp_parser_parse_tentatively (parser);
8946 /* Change `:' into `::'. */
8947 next_token_2->type = CPP_SCOPE;
8948 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8949 CPP_LESS. */
8950 cp_lexer_consume_token (parser->lexer);
8951 /* Parse the arguments. */
8952 arguments = cp_parser_enclosed_template_argument_list (parser);
8953 if (!cp_parser_parse_definitely (parser))
8954 {
8955 /* If we couldn't parse an argument list, then we revert our changes
8956 and return simply an error. Maybe this is not a template-id
8957 after all. */
8958 next_token_2->type = CPP_COLON;
8959 cp_parser_error (parser, "expected %<<%>");
8960 pop_deferring_access_checks ();
8961 return error_mark_node;
8962 }
8963 /* Otherwise, emit an error about the invalid digraph, but continue
8964 parsing because we got our argument list. */
8965 pedwarn ("%<<::%> cannot begin a template-argument list");
8966 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8967 "between %<<%> and %<::%>");
8968 if (!flag_permissive)
8969 {
8970 static bool hint;
8971 if (!hint)
8972 {
8973 inform ("(if you use -fpermissive G++ will accept your code)");
8974 hint = true;
8975 }
8976 }
8977 }
8978 else
8979 {
8980 /* Look for the `<' that starts the template-argument-list. */
8981 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8982 {
8983 pop_deferring_access_checks ();
8984 return error_mark_node;
8985 }
8986 /* Parse the arguments. */
8987 arguments = cp_parser_enclosed_template_argument_list (parser);
8988 }
8989
8990 /* Build a representation of the specialization. */
8991 if (TREE_CODE (template) == IDENTIFIER_NODE)
8992 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8993 else if (DECL_CLASS_TEMPLATE_P (template)
8994 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8995 {
8996 bool entering_scope;
8997 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8998 template (rather than some instantiation thereof) only if
8999 is not nested within some other construct. For example, in
9000 "template <typename T> void f(T) { A<T>::", A<T> is just an
9001 instantiation of A. */
9002 entering_scope = (template_parm_scope_p ()
9003 && cp_lexer_next_token_is (parser->lexer,
9004 CPP_SCOPE));
9005 template_id
9006 = finish_template_type (template, arguments, entering_scope);
9007 }
9008 else
9009 {
9010 /* If it's not a class-template or a template-template, it should be
9011 a function-template. */
9012 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9013 || TREE_CODE (template) == OVERLOAD
9014 || BASELINK_P (template)));
9015
9016 template_id = lookup_template_function (template, arguments);
9017 }
9018
9019 /* Retrieve any deferred checks. Do not pop this access checks yet
9020 so the memory will not be reclaimed during token replacing below. */
9021 access_check = get_deferred_access_checks ();
9022
9023 /* If parsing tentatively, replace the sequence of tokens that makes
9024 up the template-id with a CPP_TEMPLATE_ID token. That way,
9025 should we re-parse the token stream, we will not have to repeat
9026 the effort required to do the parse, nor will we issue duplicate
9027 error messages about problems during instantiation of the
9028 template. */
9029 if (start_of_id)
9030 {
9031 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9032
9033 /* Reset the contents of the START_OF_ID token. */
9034 token->type = CPP_TEMPLATE_ID;
9035 token->value = build_tree_list (access_check, template_id);
9036 token->keyword = RID_MAX;
9037
9038 /* Purge all subsequent tokens. */
9039 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9040
9041 /* ??? Can we actually assume that, if template_id ==
9042 error_mark_node, we will have issued a diagnostic to the
9043 user, as opposed to simply marking the tentative parse as
9044 failed? */
9045 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9046 error ("parse error in template argument list");
9047 }
9048
9049 pop_deferring_access_checks ();
9050 return template_id;
9051 }
9052
9053 /* Parse a template-name.
9054
9055 template-name:
9056 identifier
9057
9058 The standard should actually say:
9059
9060 template-name:
9061 identifier
9062 operator-function-id
9063
9064 A defect report has been filed about this issue.
9065
9066 A conversion-function-id cannot be a template name because they cannot
9067 be part of a template-id. In fact, looking at this code:
9068
9069 a.operator K<int>()
9070
9071 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9072 It is impossible to call a templated conversion-function-id with an
9073 explicit argument list, since the only allowed template parameter is
9074 the type to which it is converting.
9075
9076 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9077 `template' keyword, in a construction like:
9078
9079 T::template f<3>()
9080
9081 In that case `f' is taken to be a template-name, even though there
9082 is no way of knowing for sure.
9083
9084 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9085 name refers to a set of overloaded functions, at least one of which
9086 is a template, or an IDENTIFIER_NODE with the name of the template,
9087 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9088 names are looked up inside uninstantiated templates. */
9089
9090 static tree
9091 cp_parser_template_name (cp_parser* parser,
9092 bool template_keyword_p,
9093 bool check_dependency_p,
9094 bool is_declaration,
9095 bool *is_identifier)
9096 {
9097 tree identifier;
9098 tree decl;
9099 tree fns;
9100
9101 /* If the next token is `operator', then we have either an
9102 operator-function-id or a conversion-function-id. */
9103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9104 {
9105 /* We don't know whether we're looking at an
9106 operator-function-id or a conversion-function-id. */
9107 cp_parser_parse_tentatively (parser);
9108 /* Try an operator-function-id. */
9109 identifier = cp_parser_operator_function_id (parser);
9110 /* If that didn't work, try a conversion-function-id. */
9111 if (!cp_parser_parse_definitely (parser))
9112 {
9113 cp_parser_error (parser, "expected template-name");
9114 return error_mark_node;
9115 }
9116 }
9117 /* Look for the identifier. */
9118 else
9119 identifier = cp_parser_identifier (parser);
9120
9121 /* If we didn't find an identifier, we don't have a template-id. */
9122 if (identifier == error_mark_node)
9123 return error_mark_node;
9124
9125 /* If the name immediately followed the `template' keyword, then it
9126 is a template-name. However, if the next token is not `<', then
9127 we do not treat it as a template-name, since it is not being used
9128 as part of a template-id. This enables us to handle constructs
9129 like:
9130
9131 template <typename T> struct S { S(); };
9132 template <typename T> S<T>::S();
9133
9134 correctly. We would treat `S' as a template -- if it were `S<T>'
9135 -- but we do not if there is no `<'. */
9136
9137 if (processing_template_decl
9138 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9139 {
9140 /* In a declaration, in a dependent context, we pretend that the
9141 "template" keyword was present in order to improve error
9142 recovery. For example, given:
9143
9144 template <typename T> void f(T::X<int>);
9145
9146 we want to treat "X<int>" as a template-id. */
9147 if (is_declaration
9148 && !template_keyword_p
9149 && parser->scope && TYPE_P (parser->scope)
9150 && check_dependency_p
9151 && dependent_type_p (parser->scope)
9152 /* Do not do this for dtors (or ctors), since they never
9153 need the template keyword before their name. */
9154 && !constructor_name_p (identifier, parser->scope))
9155 {
9156 cp_token_position start = 0;
9157
9158 /* Explain what went wrong. */
9159 error ("non-template %qD used as template", identifier);
9160 inform ("use %<%T::template %D%> to indicate that it is a template",
9161 parser->scope, identifier);
9162 /* If parsing tentatively, find the location of the "<" token. */
9163 if (cp_parser_simulate_error (parser))
9164 start = cp_lexer_token_position (parser->lexer, true);
9165 /* Parse the template arguments so that we can issue error
9166 messages about them. */
9167 cp_lexer_consume_token (parser->lexer);
9168 cp_parser_enclosed_template_argument_list (parser);
9169 /* Skip tokens until we find a good place from which to
9170 continue parsing. */
9171 cp_parser_skip_to_closing_parenthesis (parser,
9172 /*recovering=*/true,
9173 /*or_comma=*/true,
9174 /*consume_paren=*/false);
9175 /* If parsing tentatively, permanently remove the
9176 template argument list. That will prevent duplicate
9177 error messages from being issued about the missing
9178 "template" keyword. */
9179 if (start)
9180 cp_lexer_purge_tokens_after (parser->lexer, start);
9181 if (is_identifier)
9182 *is_identifier = true;
9183 return identifier;
9184 }
9185
9186 /* If the "template" keyword is present, then there is generally
9187 no point in doing name-lookup, so we just return IDENTIFIER.
9188 But, if the qualifying scope is non-dependent then we can
9189 (and must) do name-lookup normally. */
9190 if (template_keyword_p
9191 && (!parser->scope
9192 || (TYPE_P (parser->scope)
9193 && dependent_type_p (parser->scope))))
9194 return identifier;
9195 }
9196
9197 /* Look up the name. */
9198 decl = cp_parser_lookup_name (parser, identifier,
9199 none_type,
9200 /*is_template=*/false,
9201 /*is_namespace=*/false,
9202 check_dependency_p,
9203 /*ambiguous_decls=*/NULL);
9204 decl = maybe_get_template_decl_from_type_decl (decl);
9205
9206 /* If DECL is a template, then the name was a template-name. */
9207 if (TREE_CODE (decl) == TEMPLATE_DECL)
9208 ;
9209 else
9210 {
9211 tree fn = NULL_TREE;
9212
9213 /* The standard does not explicitly indicate whether a name that
9214 names a set of overloaded declarations, some of which are
9215 templates, is a template-name. However, such a name should
9216 be a template-name; otherwise, there is no way to form a
9217 template-id for the overloaded templates. */
9218 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9219 if (TREE_CODE (fns) == OVERLOAD)
9220 for (fn = fns; fn; fn = OVL_NEXT (fn))
9221 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9222 break;
9223
9224 if (!fn)
9225 {
9226 /* The name does not name a template. */
9227 cp_parser_error (parser, "expected template-name");
9228 return error_mark_node;
9229 }
9230 }
9231
9232 /* If DECL is dependent, and refers to a function, then just return
9233 its name; we will look it up again during template instantiation. */
9234 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9235 {
9236 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9237 if (TYPE_P (scope) && dependent_type_p (scope))
9238 return identifier;
9239 }
9240
9241 return decl;
9242 }
9243
9244 /* Parse a template-argument-list.
9245
9246 template-argument-list:
9247 template-argument
9248 template-argument-list , template-argument
9249
9250 Returns a TREE_VEC containing the arguments. */
9251
9252 static tree
9253 cp_parser_template_argument_list (cp_parser* parser)
9254 {
9255 tree fixed_args[10];
9256 unsigned n_args = 0;
9257 unsigned alloced = 10;
9258 tree *arg_ary = fixed_args;
9259 tree vec;
9260 bool saved_in_template_argument_list_p;
9261 bool saved_ice_p;
9262 bool saved_non_ice_p;
9263
9264 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9265 parser->in_template_argument_list_p = true;
9266 /* Even if the template-id appears in an integral
9267 constant-expression, the contents of the argument list do
9268 not. */
9269 saved_ice_p = parser->integral_constant_expression_p;
9270 parser->integral_constant_expression_p = false;
9271 saved_non_ice_p = parser->non_integral_constant_expression_p;
9272 parser->non_integral_constant_expression_p = false;
9273 /* Parse the arguments. */
9274 do
9275 {
9276 tree argument;
9277
9278 if (n_args)
9279 /* Consume the comma. */
9280 cp_lexer_consume_token (parser->lexer);
9281
9282 /* Parse the template-argument. */
9283 argument = cp_parser_template_argument (parser);
9284 if (n_args == alloced)
9285 {
9286 alloced *= 2;
9287
9288 if (arg_ary == fixed_args)
9289 {
9290 arg_ary = XNEWVEC (tree, alloced);
9291 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9292 }
9293 else
9294 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9295 }
9296 arg_ary[n_args++] = argument;
9297 }
9298 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9299
9300 vec = make_tree_vec (n_args);
9301
9302 while (n_args--)
9303 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9304
9305 if (arg_ary != fixed_args)
9306 free (arg_ary);
9307 parser->non_integral_constant_expression_p = saved_non_ice_p;
9308 parser->integral_constant_expression_p = saved_ice_p;
9309 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9310 return vec;
9311 }
9312
9313 /* Parse a template-argument.
9314
9315 template-argument:
9316 assignment-expression
9317 type-id
9318 id-expression
9319
9320 The representation is that of an assignment-expression, type-id, or
9321 id-expression -- except that the qualified id-expression is
9322 evaluated, so that the value returned is either a DECL or an
9323 OVERLOAD.
9324
9325 Although the standard says "assignment-expression", it forbids
9326 throw-expressions or assignments in the template argument.
9327 Therefore, we use "conditional-expression" instead. */
9328
9329 static tree
9330 cp_parser_template_argument (cp_parser* parser)
9331 {
9332 tree argument;
9333 bool template_p;
9334 bool address_p;
9335 bool maybe_type_id = false;
9336 cp_token *token;
9337 cp_id_kind idk;
9338
9339 /* There's really no way to know what we're looking at, so we just
9340 try each alternative in order.
9341
9342 [temp.arg]
9343
9344 In a template-argument, an ambiguity between a type-id and an
9345 expression is resolved to a type-id, regardless of the form of
9346 the corresponding template-parameter.
9347
9348 Therefore, we try a type-id first. */
9349 cp_parser_parse_tentatively (parser);
9350 argument = cp_parser_type_id (parser);
9351 /* If there was no error parsing the type-id but the next token is a '>>',
9352 we probably found a typo for '> >'. But there are type-id which are
9353 also valid expressions. For instance:
9354
9355 struct X { int operator >> (int); };
9356 template <int V> struct Foo {};
9357 Foo<X () >> 5> r;
9358
9359 Here 'X()' is a valid type-id of a function type, but the user just
9360 wanted to write the expression "X() >> 5". Thus, we remember that we
9361 found a valid type-id, but we still try to parse the argument as an
9362 expression to see what happens. */
9363 if (!cp_parser_error_occurred (parser)
9364 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9365 {
9366 maybe_type_id = true;
9367 cp_parser_abort_tentative_parse (parser);
9368 }
9369 else
9370 {
9371 /* If the next token isn't a `,' or a `>', then this argument wasn't
9372 really finished. This means that the argument is not a valid
9373 type-id. */
9374 if (!cp_parser_next_token_ends_template_argument_p (parser))
9375 cp_parser_error (parser, "expected template-argument");
9376 /* If that worked, we're done. */
9377 if (cp_parser_parse_definitely (parser))
9378 return argument;
9379 }
9380 /* We're still not sure what the argument will be. */
9381 cp_parser_parse_tentatively (parser);
9382 /* Try a template. */
9383 argument = cp_parser_id_expression (parser,
9384 /*template_keyword_p=*/false,
9385 /*check_dependency_p=*/true,
9386 &template_p,
9387 /*declarator_p=*/false,
9388 /*optional_p=*/false);
9389 /* If the next token isn't a `,' or a `>', then this argument wasn't
9390 really finished. */
9391 if (!cp_parser_next_token_ends_template_argument_p (parser))
9392 cp_parser_error (parser, "expected template-argument");
9393 if (!cp_parser_error_occurred (parser))
9394 {
9395 /* Figure out what is being referred to. If the id-expression
9396 was for a class template specialization, then we will have a
9397 TYPE_DECL at this point. There is no need to do name lookup
9398 at this point in that case. */
9399 if (TREE_CODE (argument) != TYPE_DECL)
9400 argument = cp_parser_lookup_name (parser, argument,
9401 none_type,
9402 /*is_template=*/template_p,
9403 /*is_namespace=*/false,
9404 /*check_dependency=*/true,
9405 /*ambiguous_decls=*/NULL);
9406 if (TREE_CODE (argument) != TEMPLATE_DECL
9407 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9408 cp_parser_error (parser, "expected template-name");
9409 }
9410 if (cp_parser_parse_definitely (parser))
9411 return argument;
9412 /* It must be a non-type argument. There permitted cases are given
9413 in [temp.arg.nontype]:
9414
9415 -- an integral constant-expression of integral or enumeration
9416 type; or
9417
9418 -- the name of a non-type template-parameter; or
9419
9420 -- the name of an object or function with external linkage...
9421
9422 -- the address of an object or function with external linkage...
9423
9424 -- a pointer to member... */
9425 /* Look for a non-type template parameter. */
9426 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9427 {
9428 cp_parser_parse_tentatively (parser);
9429 argument = cp_parser_primary_expression (parser,
9430 /*adress_p=*/false,
9431 /*cast_p=*/false,
9432 /*template_arg_p=*/true,
9433 &idk);
9434 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9435 || !cp_parser_next_token_ends_template_argument_p (parser))
9436 cp_parser_simulate_error (parser);
9437 if (cp_parser_parse_definitely (parser))
9438 return argument;
9439 }
9440
9441 /* If the next token is "&", the argument must be the address of an
9442 object or function with external linkage. */
9443 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9444 if (address_p)
9445 cp_lexer_consume_token (parser->lexer);
9446 /* See if we might have an id-expression. */
9447 token = cp_lexer_peek_token (parser->lexer);
9448 if (token->type == CPP_NAME
9449 || token->keyword == RID_OPERATOR
9450 || token->type == CPP_SCOPE
9451 || token->type == CPP_TEMPLATE_ID
9452 || token->type == CPP_NESTED_NAME_SPECIFIER)
9453 {
9454 cp_parser_parse_tentatively (parser);
9455 argument = cp_parser_primary_expression (parser,
9456 address_p,
9457 /*cast_p=*/false,
9458 /*template_arg_p=*/true,
9459 &idk);
9460 if (cp_parser_error_occurred (parser)
9461 || !cp_parser_next_token_ends_template_argument_p (parser))
9462 cp_parser_abort_tentative_parse (parser);
9463 else
9464 {
9465 if (TREE_CODE (argument) == INDIRECT_REF)
9466 {
9467 gcc_assert (REFERENCE_REF_P (argument));
9468 argument = TREE_OPERAND (argument, 0);
9469 }
9470
9471 if (TREE_CODE (argument) == VAR_DECL)
9472 {
9473 /* A variable without external linkage might still be a
9474 valid constant-expression, so no error is issued here
9475 if the external-linkage check fails. */
9476 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9477 cp_parser_simulate_error (parser);
9478 }
9479 else if (is_overloaded_fn (argument))
9480 /* All overloaded functions are allowed; if the external
9481 linkage test does not pass, an error will be issued
9482 later. */
9483 ;
9484 else if (address_p
9485 && (TREE_CODE (argument) == OFFSET_REF
9486 || TREE_CODE (argument) == SCOPE_REF))
9487 /* A pointer-to-member. */
9488 ;
9489 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9490 ;
9491 else
9492 cp_parser_simulate_error (parser);
9493
9494 if (cp_parser_parse_definitely (parser))
9495 {
9496 if (address_p)
9497 argument = build_x_unary_op (ADDR_EXPR, argument);
9498 return argument;
9499 }
9500 }
9501 }
9502 /* If the argument started with "&", there are no other valid
9503 alternatives at this point. */
9504 if (address_p)
9505 {
9506 cp_parser_error (parser, "invalid non-type template argument");
9507 return error_mark_node;
9508 }
9509
9510 /* If the argument wasn't successfully parsed as a type-id followed
9511 by '>>', the argument can only be a constant expression now.
9512 Otherwise, we try parsing the constant-expression tentatively,
9513 because the argument could really be a type-id. */
9514 if (maybe_type_id)
9515 cp_parser_parse_tentatively (parser);
9516 argument = cp_parser_constant_expression (parser,
9517 /*allow_non_constant_p=*/false,
9518 /*non_constant_p=*/NULL);
9519 argument = fold_non_dependent_expr (argument);
9520 if (!maybe_type_id)
9521 return argument;
9522 if (!cp_parser_next_token_ends_template_argument_p (parser))
9523 cp_parser_error (parser, "expected template-argument");
9524 if (cp_parser_parse_definitely (parser))
9525 return argument;
9526 /* We did our best to parse the argument as a non type-id, but that
9527 was the only alternative that matched (albeit with a '>' after
9528 it). We can assume it's just a typo from the user, and a
9529 diagnostic will then be issued. */
9530 return cp_parser_type_id (parser);
9531 }
9532
9533 /* Parse an explicit-instantiation.
9534
9535 explicit-instantiation:
9536 template declaration
9537
9538 Although the standard says `declaration', what it really means is:
9539
9540 explicit-instantiation:
9541 template decl-specifier-seq [opt] declarator [opt] ;
9542
9543 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9544 supposed to be allowed. A defect report has been filed about this
9545 issue.
9546
9547 GNU Extension:
9548
9549 explicit-instantiation:
9550 storage-class-specifier template
9551 decl-specifier-seq [opt] declarator [opt] ;
9552 function-specifier template
9553 decl-specifier-seq [opt] declarator [opt] ; */
9554
9555 static void
9556 cp_parser_explicit_instantiation (cp_parser* parser)
9557 {
9558 int declares_class_or_enum;
9559 cp_decl_specifier_seq decl_specifiers;
9560 tree extension_specifier = NULL_TREE;
9561
9562 /* Look for an (optional) storage-class-specifier or
9563 function-specifier. */
9564 if (cp_parser_allow_gnu_extensions_p (parser))
9565 {
9566 extension_specifier
9567 = cp_parser_storage_class_specifier_opt (parser);
9568 if (!extension_specifier)
9569 extension_specifier
9570 = cp_parser_function_specifier_opt (parser,
9571 /*decl_specs=*/NULL);
9572 }
9573
9574 /* Look for the `template' keyword. */
9575 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9576 /* Let the front end know that we are processing an explicit
9577 instantiation. */
9578 begin_explicit_instantiation ();
9579 /* [temp.explicit] says that we are supposed to ignore access
9580 control while processing explicit instantiation directives. */
9581 push_deferring_access_checks (dk_no_check);
9582 /* Parse a decl-specifier-seq. */
9583 cp_parser_decl_specifier_seq (parser,
9584 CP_PARSER_FLAGS_OPTIONAL,
9585 &decl_specifiers,
9586 &declares_class_or_enum);
9587 /* If there was exactly one decl-specifier, and it declared a class,
9588 and there's no declarator, then we have an explicit type
9589 instantiation. */
9590 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9591 {
9592 tree type;
9593
9594 type = check_tag_decl (&decl_specifiers);
9595 /* Turn access control back on for names used during
9596 template instantiation. */
9597 pop_deferring_access_checks ();
9598 if (type)
9599 do_type_instantiation (type, extension_specifier,
9600 /*complain=*/tf_error);
9601 }
9602 else
9603 {
9604 cp_declarator *declarator;
9605 tree decl;
9606
9607 /* Parse the declarator. */
9608 declarator
9609 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9610 /*ctor_dtor_or_conv_p=*/NULL,
9611 /*parenthesized_p=*/NULL,
9612 /*member_p=*/false);
9613 if (declares_class_or_enum & 2)
9614 cp_parser_check_for_definition_in_return_type (declarator,
9615 decl_specifiers.type);
9616 if (declarator != cp_error_declarator)
9617 {
9618 decl = grokdeclarator (declarator, &decl_specifiers,
9619 NORMAL, 0, &decl_specifiers.attributes);
9620 /* Turn access control back on for names used during
9621 template instantiation. */
9622 pop_deferring_access_checks ();
9623 /* Do the explicit instantiation. */
9624 do_decl_instantiation (decl, extension_specifier);
9625 }
9626 else
9627 {
9628 pop_deferring_access_checks ();
9629 /* Skip the body of the explicit instantiation. */
9630 cp_parser_skip_to_end_of_statement (parser);
9631 }
9632 }
9633 /* We're done with the instantiation. */
9634 end_explicit_instantiation ();
9635
9636 cp_parser_consume_semicolon_at_end_of_statement (parser);
9637 }
9638
9639 /* Parse an explicit-specialization.
9640
9641 explicit-specialization:
9642 template < > declaration
9643
9644 Although the standard says `declaration', what it really means is:
9645
9646 explicit-specialization:
9647 template <> decl-specifier [opt] init-declarator [opt] ;
9648 template <> function-definition
9649 template <> explicit-specialization
9650 template <> template-declaration */
9651
9652 static void
9653 cp_parser_explicit_specialization (cp_parser* parser)
9654 {
9655 bool need_lang_pop;
9656 /* Look for the `template' keyword. */
9657 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9658 /* Look for the `<'. */
9659 cp_parser_require (parser, CPP_LESS, "`<'");
9660 /* Look for the `>'. */
9661 cp_parser_require (parser, CPP_GREATER, "`>'");
9662 /* We have processed another parameter list. */
9663 ++parser->num_template_parameter_lists;
9664 /* [temp]
9665
9666 A template ... explicit specialization ... shall not have C
9667 linkage. */
9668 if (current_lang_name == lang_name_c)
9669 {
9670 error ("template specialization with C linkage");
9671 /* Give it C++ linkage to avoid confusing other parts of the
9672 front end. */
9673 push_lang_context (lang_name_cplusplus);
9674 need_lang_pop = true;
9675 }
9676 else
9677 need_lang_pop = false;
9678 /* Let the front end know that we are beginning a specialization. */
9679 if (!begin_specialization ())
9680 {
9681 end_specialization ();
9682 cp_parser_skip_to_end_of_block_or_statement (parser);
9683 return;
9684 }
9685
9686 /* If the next keyword is `template', we need to figure out whether
9687 or not we're looking a template-declaration. */
9688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9689 {
9690 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9691 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9692 cp_parser_template_declaration_after_export (parser,
9693 /*member_p=*/false);
9694 else
9695 cp_parser_explicit_specialization (parser);
9696 }
9697 else
9698 /* Parse the dependent declaration. */
9699 cp_parser_single_declaration (parser,
9700 /*checks=*/NULL_TREE,
9701 /*member_p=*/false,
9702 /*friend_p=*/NULL);
9703 /* We're done with the specialization. */
9704 end_specialization ();
9705 /* For the erroneous case of a template with C linkage, we pushed an
9706 implicit C++ linkage scope; exit that scope now. */
9707 if (need_lang_pop)
9708 pop_lang_context ();
9709 /* We're done with this parameter list. */
9710 --parser->num_template_parameter_lists;
9711 }
9712
9713 /* Parse a type-specifier.
9714
9715 type-specifier:
9716 simple-type-specifier
9717 class-specifier
9718 enum-specifier
9719 elaborated-type-specifier
9720 cv-qualifier
9721
9722 GNU Extension:
9723
9724 type-specifier:
9725 __complex__
9726
9727 Returns a representation of the type-specifier. For a
9728 class-specifier, enum-specifier, or elaborated-type-specifier, a
9729 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9730
9731 The parser flags FLAGS is used to control type-specifier parsing.
9732
9733 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9734 in a decl-specifier-seq.
9735
9736 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9737 class-specifier, enum-specifier, or elaborated-type-specifier, then
9738 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9739 if a type is declared; 2 if it is defined. Otherwise, it is set to
9740 zero.
9741
9742 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9743 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9744 is set to FALSE. */
9745
9746 static tree
9747 cp_parser_type_specifier (cp_parser* parser,
9748 cp_parser_flags flags,
9749 cp_decl_specifier_seq *decl_specs,
9750 bool is_declaration,
9751 int* declares_class_or_enum,
9752 bool* is_cv_qualifier)
9753 {
9754 tree type_spec = NULL_TREE;
9755 cp_token *token;
9756 enum rid keyword;
9757 cp_decl_spec ds = ds_last;
9758
9759 /* Assume this type-specifier does not declare a new type. */
9760 if (declares_class_or_enum)
9761 *declares_class_or_enum = 0;
9762 /* And that it does not specify a cv-qualifier. */
9763 if (is_cv_qualifier)
9764 *is_cv_qualifier = false;
9765 /* Peek at the next token. */
9766 token = cp_lexer_peek_token (parser->lexer);
9767
9768 /* If we're looking at a keyword, we can use that to guide the
9769 production we choose. */
9770 keyword = token->keyword;
9771 switch (keyword)
9772 {
9773 case RID_ENUM:
9774 /* Look for the enum-specifier. */
9775 type_spec = cp_parser_enum_specifier (parser);
9776 /* If that worked, we're done. */
9777 if (type_spec)
9778 {
9779 if (declares_class_or_enum)
9780 *declares_class_or_enum = 2;
9781 if (decl_specs)
9782 cp_parser_set_decl_spec_type (decl_specs,
9783 type_spec,
9784 /*user_defined_p=*/true);
9785 return type_spec;
9786 }
9787 else
9788 goto elaborated_type_specifier;
9789
9790 /* Any of these indicate either a class-specifier, or an
9791 elaborated-type-specifier. */
9792 case RID_CLASS:
9793 case RID_STRUCT:
9794 case RID_UNION:
9795 /* Parse tentatively so that we can back up if we don't find a
9796 class-specifier. */
9797 cp_parser_parse_tentatively (parser);
9798 /* Look for the class-specifier. */
9799 type_spec = cp_parser_class_specifier (parser);
9800 /* If that worked, we're done. */
9801 if (cp_parser_parse_definitely (parser))
9802 {
9803 if (declares_class_or_enum)
9804 *declares_class_or_enum = 2;
9805 if (decl_specs)
9806 cp_parser_set_decl_spec_type (decl_specs,
9807 type_spec,
9808 /*user_defined_p=*/true);
9809 return type_spec;
9810 }
9811
9812 /* Fall through. */
9813 elaborated_type_specifier:
9814 /* We're declaring (not defining) a class or enum. */
9815 if (declares_class_or_enum)
9816 *declares_class_or_enum = 1;
9817
9818 /* Fall through. */
9819 case RID_TYPENAME:
9820 /* Look for an elaborated-type-specifier. */
9821 type_spec
9822 = (cp_parser_elaborated_type_specifier
9823 (parser,
9824 decl_specs && decl_specs->specs[(int) ds_friend],
9825 is_declaration));
9826 if (decl_specs)
9827 cp_parser_set_decl_spec_type (decl_specs,
9828 type_spec,
9829 /*user_defined_p=*/true);
9830 return type_spec;
9831
9832 case RID_CONST:
9833 ds = ds_const;
9834 if (is_cv_qualifier)
9835 *is_cv_qualifier = true;
9836 break;
9837
9838 case RID_VOLATILE:
9839 ds = ds_volatile;
9840 if (is_cv_qualifier)
9841 *is_cv_qualifier = true;
9842 break;
9843
9844 case RID_RESTRICT:
9845 ds = ds_restrict;
9846 if (is_cv_qualifier)
9847 *is_cv_qualifier = true;
9848 break;
9849
9850 case RID_COMPLEX:
9851 /* The `__complex__' keyword is a GNU extension. */
9852 ds = ds_complex;
9853 break;
9854
9855 default:
9856 break;
9857 }
9858
9859 /* Handle simple keywords. */
9860 if (ds != ds_last)
9861 {
9862 if (decl_specs)
9863 {
9864 ++decl_specs->specs[(int)ds];
9865 decl_specs->any_specifiers_p = true;
9866 }
9867 return cp_lexer_consume_token (parser->lexer)->value;
9868 }
9869
9870 /* If we do not already have a type-specifier, assume we are looking
9871 at a simple-type-specifier. */
9872 type_spec = cp_parser_simple_type_specifier (parser,
9873 decl_specs,
9874 flags);
9875
9876 /* If we didn't find a type-specifier, and a type-specifier was not
9877 optional in this context, issue an error message. */
9878 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9879 {
9880 cp_parser_error (parser, "expected type specifier");
9881 return error_mark_node;
9882 }
9883
9884 return type_spec;
9885 }
9886
9887 /* Parse a simple-type-specifier.
9888
9889 simple-type-specifier:
9890 :: [opt] nested-name-specifier [opt] type-name
9891 :: [opt] nested-name-specifier template template-id
9892 char
9893 wchar_t
9894 bool
9895 short
9896 int
9897 long
9898 signed
9899 unsigned
9900 float
9901 double
9902 void
9903
9904 GNU Extension:
9905
9906 simple-type-specifier:
9907 __typeof__ unary-expression
9908 __typeof__ ( type-id )
9909
9910 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9911 appropriately updated. */
9912
9913 static tree
9914 cp_parser_simple_type_specifier (cp_parser* parser,
9915 cp_decl_specifier_seq *decl_specs,
9916 cp_parser_flags flags)
9917 {
9918 tree type = NULL_TREE;
9919 cp_token *token;
9920
9921 /* Peek at the next token. */
9922 token = cp_lexer_peek_token (parser->lexer);
9923
9924 /* If we're looking at a keyword, things are easy. */
9925 switch (token->keyword)
9926 {
9927 case RID_CHAR:
9928 if (decl_specs)
9929 decl_specs->explicit_char_p = true;
9930 type = char_type_node;
9931 break;
9932 case RID_WCHAR:
9933 type = wchar_type_node;
9934 break;
9935 case RID_BOOL:
9936 type = boolean_type_node;
9937 break;
9938 case RID_SHORT:
9939 if (decl_specs)
9940 ++decl_specs->specs[(int) ds_short];
9941 type = short_integer_type_node;
9942 break;
9943 case RID_INT:
9944 if (decl_specs)
9945 decl_specs->explicit_int_p = true;
9946 type = integer_type_node;
9947 break;
9948 case RID_LONG:
9949 if (decl_specs)
9950 ++decl_specs->specs[(int) ds_long];
9951 type = long_integer_type_node;
9952 break;
9953 case RID_SIGNED:
9954 if (decl_specs)
9955 ++decl_specs->specs[(int) ds_signed];
9956 type = integer_type_node;
9957 break;
9958 case RID_UNSIGNED:
9959 if (decl_specs)
9960 ++decl_specs->specs[(int) ds_unsigned];
9961 type = unsigned_type_node;
9962 break;
9963 case RID_FLOAT:
9964 type = float_type_node;
9965 break;
9966 case RID_DOUBLE:
9967 type = double_type_node;
9968 break;
9969 case RID_VOID:
9970 type = void_type_node;
9971 break;
9972
9973 case RID_TYPEOF:
9974 /* Consume the `typeof' token. */
9975 cp_lexer_consume_token (parser->lexer);
9976 /* Parse the operand to `typeof'. */
9977 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9978 /* If it is not already a TYPE, take its type. */
9979 if (!TYPE_P (type))
9980 type = finish_typeof (type);
9981
9982 if (decl_specs)
9983 cp_parser_set_decl_spec_type (decl_specs, type,
9984 /*user_defined_p=*/true);
9985
9986 return type;
9987
9988 default:
9989 break;
9990 }
9991
9992 /* If the type-specifier was for a built-in type, we're done. */
9993 if (type)
9994 {
9995 tree id;
9996
9997 /* Record the type. */
9998 if (decl_specs
9999 && (token->keyword != RID_SIGNED
10000 && token->keyword != RID_UNSIGNED
10001 && token->keyword != RID_SHORT
10002 && token->keyword != RID_LONG))
10003 cp_parser_set_decl_spec_type (decl_specs,
10004 type,
10005 /*user_defined=*/false);
10006 if (decl_specs)
10007 decl_specs->any_specifiers_p = true;
10008
10009 /* Consume the token. */
10010 id = cp_lexer_consume_token (parser->lexer)->value;
10011
10012 /* There is no valid C++ program where a non-template type is
10013 followed by a "<". That usually indicates that the user thought
10014 that the type was a template. */
10015 cp_parser_check_for_invalid_template_id (parser, type);
10016
10017 return TYPE_NAME (type);
10018 }
10019
10020 /* The type-specifier must be a user-defined type. */
10021 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10022 {
10023 bool qualified_p;
10024 bool global_p;
10025
10026 /* Don't gobble tokens or issue error messages if this is an
10027 optional type-specifier. */
10028 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10029 cp_parser_parse_tentatively (parser);
10030
10031 /* Look for the optional `::' operator. */
10032 global_p
10033 = (cp_parser_global_scope_opt (parser,
10034 /*current_scope_valid_p=*/false)
10035 != NULL_TREE);
10036 /* Look for the nested-name specifier. */
10037 qualified_p
10038 = (cp_parser_nested_name_specifier_opt (parser,
10039 /*typename_keyword_p=*/false,
10040 /*check_dependency_p=*/true,
10041 /*type_p=*/false,
10042 /*is_declaration=*/false)
10043 != NULL_TREE);
10044 /* If we have seen a nested-name-specifier, and the next token
10045 is `template', then we are using the template-id production. */
10046 if (parser->scope
10047 && cp_parser_optional_template_keyword (parser))
10048 {
10049 /* Look for the template-id. */
10050 type = cp_parser_template_id (parser,
10051 /*template_keyword_p=*/true,
10052 /*check_dependency_p=*/true,
10053 /*is_declaration=*/false);
10054 /* If the template-id did not name a type, we are out of
10055 luck. */
10056 if (TREE_CODE (type) != TYPE_DECL)
10057 {
10058 cp_parser_error (parser, "expected template-id for type");
10059 type = NULL_TREE;
10060 }
10061 }
10062 /* Otherwise, look for a type-name. */
10063 else
10064 type = cp_parser_type_name (parser);
10065 /* Keep track of all name-lookups performed in class scopes. */
10066 if (type
10067 && !global_p
10068 && !qualified_p
10069 && TREE_CODE (type) == TYPE_DECL
10070 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10071 maybe_note_name_used_in_class (DECL_NAME (type), type);
10072 /* If it didn't work out, we don't have a TYPE. */
10073 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10074 && !cp_parser_parse_definitely (parser))
10075 type = NULL_TREE;
10076 if (type && decl_specs)
10077 cp_parser_set_decl_spec_type (decl_specs, type,
10078 /*user_defined=*/true);
10079 }
10080
10081 /* If we didn't get a type-name, issue an error message. */
10082 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10083 {
10084 cp_parser_error (parser, "expected type-name");
10085 return error_mark_node;
10086 }
10087
10088 /* There is no valid C++ program where a non-template type is
10089 followed by a "<". That usually indicates that the user thought
10090 that the type was a template. */
10091 if (type && type != error_mark_node)
10092 {
10093 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10094 If it is, then the '<'...'>' enclose protocol names rather than
10095 template arguments, and so everything is fine. */
10096 if (c_dialect_objc ()
10097 && (objc_is_id (type) || objc_is_class_name (type)))
10098 {
10099 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10100 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10101
10102 /* Clobber the "unqualified" type previously entered into
10103 DECL_SPECS with the new, improved protocol-qualified version. */
10104 if (decl_specs)
10105 decl_specs->type = qual_type;
10106
10107 return qual_type;
10108 }
10109
10110 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10111 }
10112
10113 return type;
10114 }
10115
10116 /* Parse a type-name.
10117
10118 type-name:
10119 class-name
10120 enum-name
10121 typedef-name
10122
10123 enum-name:
10124 identifier
10125
10126 typedef-name:
10127 identifier
10128
10129 Returns a TYPE_DECL for the type. */
10130
10131 static tree
10132 cp_parser_type_name (cp_parser* parser)
10133 {
10134 tree type_decl;
10135 tree identifier;
10136
10137 /* We can't know yet whether it is a class-name or not. */
10138 cp_parser_parse_tentatively (parser);
10139 /* Try a class-name. */
10140 type_decl = cp_parser_class_name (parser,
10141 /*typename_keyword_p=*/false,
10142 /*template_keyword_p=*/false,
10143 none_type,
10144 /*check_dependency_p=*/true,
10145 /*class_head_p=*/false,
10146 /*is_declaration=*/false);
10147 /* If it's not a class-name, keep looking. */
10148 if (!cp_parser_parse_definitely (parser))
10149 {
10150 /* It must be a typedef-name or an enum-name. */
10151 identifier = cp_parser_identifier (parser);
10152 if (identifier == error_mark_node)
10153 return error_mark_node;
10154
10155 /* Look up the type-name. */
10156 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10157
10158 if (TREE_CODE (type_decl) != TYPE_DECL
10159 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10160 {
10161 /* See if this is an Objective-C type. */
10162 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10163 tree type = objc_get_protocol_qualified_type (identifier, protos);
10164 if (type)
10165 type_decl = TYPE_NAME (type);
10166 }
10167
10168 /* Issue an error if we did not find a type-name. */
10169 if (TREE_CODE (type_decl) != TYPE_DECL)
10170 {
10171 if (!cp_parser_simulate_error (parser))
10172 cp_parser_name_lookup_error (parser, identifier, type_decl,
10173 "is not a type");
10174 type_decl = error_mark_node;
10175 }
10176 /* Remember that the name was used in the definition of the
10177 current class so that we can check later to see if the
10178 meaning would have been different after the class was
10179 entirely defined. */
10180 else if (type_decl != error_mark_node
10181 && !parser->scope)
10182 maybe_note_name_used_in_class (identifier, type_decl);
10183 }
10184
10185 return type_decl;
10186 }
10187
10188
10189 /* Parse an elaborated-type-specifier. Note that the grammar given
10190 here incorporates the resolution to DR68.
10191
10192 elaborated-type-specifier:
10193 class-key :: [opt] nested-name-specifier [opt] identifier
10194 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10195 enum :: [opt] nested-name-specifier [opt] identifier
10196 typename :: [opt] nested-name-specifier identifier
10197 typename :: [opt] nested-name-specifier template [opt]
10198 template-id
10199
10200 GNU extension:
10201
10202 elaborated-type-specifier:
10203 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10204 class-key attributes :: [opt] nested-name-specifier [opt]
10205 template [opt] template-id
10206 enum attributes :: [opt] nested-name-specifier [opt] identifier
10207
10208 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10209 declared `friend'. If IS_DECLARATION is TRUE, then this
10210 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10211 something is being declared.
10212
10213 Returns the TYPE specified. */
10214
10215 static tree
10216 cp_parser_elaborated_type_specifier (cp_parser* parser,
10217 bool is_friend,
10218 bool is_declaration)
10219 {
10220 enum tag_types tag_type;
10221 tree identifier;
10222 tree type = NULL_TREE;
10223 tree attributes = NULL_TREE;
10224
10225 /* See if we're looking at the `enum' keyword. */
10226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10227 {
10228 /* Consume the `enum' token. */
10229 cp_lexer_consume_token (parser->lexer);
10230 /* Remember that it's an enumeration type. */
10231 tag_type = enum_type;
10232 /* Parse the attributes. */
10233 attributes = cp_parser_attributes_opt (parser);
10234 }
10235 /* Or, it might be `typename'. */
10236 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10237 RID_TYPENAME))
10238 {
10239 /* Consume the `typename' token. */
10240 cp_lexer_consume_token (parser->lexer);
10241 /* Remember that it's a `typename' type. */
10242 tag_type = typename_type;
10243 /* The `typename' keyword is only allowed in templates. */
10244 if (!processing_template_decl)
10245 pedwarn ("using %<typename%> outside of template");
10246 }
10247 /* Otherwise it must be a class-key. */
10248 else
10249 {
10250 tag_type = cp_parser_class_key (parser);
10251 if (tag_type == none_type)
10252 return error_mark_node;
10253 /* Parse the attributes. */
10254 attributes = cp_parser_attributes_opt (parser);
10255 }
10256
10257 /* Look for the `::' operator. */
10258 cp_parser_global_scope_opt (parser,
10259 /*current_scope_valid_p=*/false);
10260 /* Look for the nested-name-specifier. */
10261 if (tag_type == typename_type)
10262 {
10263 if (!cp_parser_nested_name_specifier (parser,
10264 /*typename_keyword_p=*/true,
10265 /*check_dependency_p=*/true,
10266 /*type_p=*/true,
10267 is_declaration))
10268 return error_mark_node;
10269 }
10270 else
10271 /* Even though `typename' is not present, the proposed resolution
10272 to Core Issue 180 says that in `class A<T>::B', `B' should be
10273 considered a type-name, even if `A<T>' is dependent. */
10274 cp_parser_nested_name_specifier_opt (parser,
10275 /*typename_keyword_p=*/true,
10276 /*check_dependency_p=*/true,
10277 /*type_p=*/true,
10278 is_declaration);
10279 /* For everything but enumeration types, consider a template-id. */
10280 /* For an enumeration type, consider only a plain identifier. */
10281 if (tag_type != enum_type)
10282 {
10283 bool template_p = false;
10284 tree decl;
10285
10286 /* Allow the `template' keyword. */
10287 template_p = cp_parser_optional_template_keyword (parser);
10288 /* If we didn't see `template', we don't know if there's a
10289 template-id or not. */
10290 if (!template_p)
10291 cp_parser_parse_tentatively (parser);
10292 /* Parse the template-id. */
10293 decl = cp_parser_template_id (parser, template_p,
10294 /*check_dependency_p=*/true,
10295 is_declaration);
10296 /* If we didn't find a template-id, look for an ordinary
10297 identifier. */
10298 if (!template_p && !cp_parser_parse_definitely (parser))
10299 ;
10300 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10301 in effect, then we must assume that, upon instantiation, the
10302 template will correspond to a class. */
10303 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10304 && tag_type == typename_type)
10305 type = make_typename_type (parser->scope, decl,
10306 typename_type,
10307 /*complain=*/tf_error);
10308 else
10309 type = TREE_TYPE (decl);
10310 }
10311
10312 if (!type)
10313 {
10314 identifier = cp_parser_identifier (parser);
10315
10316 if (identifier == error_mark_node)
10317 {
10318 parser->scope = NULL_TREE;
10319 return error_mark_node;
10320 }
10321
10322 /* For a `typename', we needn't call xref_tag. */
10323 if (tag_type == typename_type
10324 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10325 return cp_parser_make_typename_type (parser, parser->scope,
10326 identifier);
10327 /* Look up a qualified name in the usual way. */
10328 if (parser->scope)
10329 {
10330 tree decl;
10331
10332 decl = cp_parser_lookup_name (parser, identifier,
10333 tag_type,
10334 /*is_template=*/false,
10335 /*is_namespace=*/false,
10336 /*check_dependency=*/true,
10337 /*ambiguous_decls=*/NULL);
10338
10339 /* If we are parsing friend declaration, DECL may be a
10340 TEMPLATE_DECL tree node here. However, we need to check
10341 whether this TEMPLATE_DECL results in valid code. Consider
10342 the following example:
10343
10344 namespace N {
10345 template <class T> class C {};
10346 }
10347 class X {
10348 template <class T> friend class N::C; // #1, valid code
10349 };
10350 template <class T> class Y {
10351 friend class N::C; // #2, invalid code
10352 };
10353
10354 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10355 name lookup of `N::C'. We see that friend declaration must
10356 be template for the code to be valid. Note that
10357 processing_template_decl does not work here since it is
10358 always 1 for the above two cases. */
10359
10360 decl = (cp_parser_maybe_treat_template_as_class
10361 (decl, /*tag_name_p=*/is_friend
10362 && parser->num_template_parameter_lists));
10363
10364 if (TREE_CODE (decl) != TYPE_DECL)
10365 {
10366 cp_parser_diagnose_invalid_type_name (parser,
10367 parser->scope,
10368 identifier);
10369 return error_mark_node;
10370 }
10371
10372 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10373 {
10374 bool allow_template = (parser->num_template_parameter_lists
10375 || DECL_SELF_REFERENCE_P (decl));
10376 type = check_elaborated_type_specifier (tag_type, decl,
10377 allow_template);
10378
10379 if (type == error_mark_node)
10380 return error_mark_node;
10381 }
10382
10383 type = TREE_TYPE (decl);
10384 }
10385 else
10386 {
10387 /* An elaborated-type-specifier sometimes introduces a new type and
10388 sometimes names an existing type. Normally, the rule is that it
10389 introduces a new type only if there is not an existing type of
10390 the same name already in scope. For example, given:
10391
10392 struct S {};
10393 void f() { struct S s; }
10394
10395 the `struct S' in the body of `f' is the same `struct S' as in
10396 the global scope; the existing definition is used. However, if
10397 there were no global declaration, this would introduce a new
10398 local class named `S'.
10399
10400 An exception to this rule applies to the following code:
10401
10402 namespace N { struct S; }
10403
10404 Here, the elaborated-type-specifier names a new type
10405 unconditionally; even if there is already an `S' in the
10406 containing scope this declaration names a new type.
10407 This exception only applies if the elaborated-type-specifier
10408 forms the complete declaration:
10409
10410 [class.name]
10411
10412 A declaration consisting solely of `class-key identifier ;' is
10413 either a redeclaration of the name in the current scope or a
10414 forward declaration of the identifier as a class name. It
10415 introduces the name into the current scope.
10416
10417 We are in this situation precisely when the next token is a `;'.
10418
10419 An exception to the exception is that a `friend' declaration does
10420 *not* name a new type; i.e., given:
10421
10422 struct S { friend struct T; };
10423
10424 `T' is not a new type in the scope of `S'.
10425
10426 Also, `new struct S' or `sizeof (struct S)' never results in the
10427 definition of a new type; a new type can only be declared in a
10428 declaration context. */
10429
10430 tag_scope ts;
10431 bool template_p;
10432
10433 if (is_friend)
10434 /* Friends have special name lookup rules. */
10435 ts = ts_within_enclosing_non_class;
10436 else if (is_declaration
10437 && cp_lexer_next_token_is (parser->lexer,
10438 CPP_SEMICOLON))
10439 /* This is a `class-key identifier ;' */
10440 ts = ts_current;
10441 else
10442 ts = ts_global;
10443
10444 template_p =
10445 (parser->num_template_parameter_lists
10446 && (cp_parser_next_token_starts_class_definition_p (parser)
10447 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10448 /* An unqualified name was used to reference this type, so
10449 there were no qualifying templates. */
10450 if (!cp_parser_check_template_parameters (parser,
10451 /*num_templates=*/0))
10452 return error_mark_node;
10453 type = xref_tag (tag_type, identifier, ts, template_p);
10454 }
10455 }
10456
10457 if (type == error_mark_node)
10458 return error_mark_node;
10459
10460 /* Allow attributes on forward declarations of classes. */
10461 if (attributes)
10462 {
10463 if (TREE_CODE (type) == TYPENAME_TYPE)
10464 warning (OPT_Wattributes,
10465 "attributes ignored on uninstantiated type");
10466 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10467 && ! processing_explicit_instantiation)
10468 warning (OPT_Wattributes,
10469 "attributes ignored on template instantiation");
10470 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10471 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10472 else
10473 warning (OPT_Wattributes,
10474 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10475 }
10476
10477 if (tag_type != enum_type)
10478 cp_parser_check_class_key (tag_type, type);
10479
10480 /* A "<" cannot follow an elaborated type specifier. If that
10481 happens, the user was probably trying to form a template-id. */
10482 cp_parser_check_for_invalid_template_id (parser, type);
10483
10484 return type;
10485 }
10486
10487 /* Parse an enum-specifier.
10488
10489 enum-specifier:
10490 enum identifier [opt] { enumerator-list [opt] }
10491
10492 GNU Extensions:
10493 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10494 attributes[opt]
10495
10496 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10497 if the token stream isn't an enum-specifier after all. */
10498
10499 static tree
10500 cp_parser_enum_specifier (cp_parser* parser)
10501 {
10502 tree identifier;
10503 tree type;
10504 tree attributes;
10505
10506 /* Parse tentatively so that we can back up if we don't find a
10507 enum-specifier. */
10508 cp_parser_parse_tentatively (parser);
10509
10510 /* Caller guarantees that the current token is 'enum', an identifier
10511 possibly follows, and the token after that is an opening brace.
10512 If we don't have an identifier, fabricate an anonymous name for
10513 the enumeration being defined. */
10514 cp_lexer_consume_token (parser->lexer);
10515
10516 attributes = cp_parser_attributes_opt (parser);
10517
10518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10519 identifier = cp_parser_identifier (parser);
10520 else
10521 identifier = make_anon_name ();
10522
10523 /* Look for the `{' but don't consume it yet. */
10524 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10525 cp_parser_simulate_error (parser);
10526
10527 if (!cp_parser_parse_definitely (parser))
10528 return NULL_TREE;
10529
10530 /* Issue an error message if type-definitions are forbidden here. */
10531 if (!cp_parser_check_type_definition (parser))
10532 type = error_mark_node;
10533 else
10534 /* Create the new type. We do this before consuming the opening
10535 brace so the enum will be recorded as being on the line of its
10536 tag (or the 'enum' keyword, if there is no tag). */
10537 type = start_enum (identifier);
10538
10539 /* Consume the opening brace. */
10540 cp_lexer_consume_token (parser->lexer);
10541
10542 if (type == error_mark_node)
10543 {
10544 cp_parser_skip_to_end_of_block_or_statement (parser);
10545 return error_mark_node;
10546 }
10547
10548 /* If the next token is not '}', then there are some enumerators. */
10549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10550 cp_parser_enumerator_list (parser, type);
10551
10552 /* Consume the final '}'. */
10553 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10554
10555 /* Look for trailing attributes to apply to this enumeration, and
10556 apply them if appropriate. */
10557 if (cp_parser_allow_gnu_extensions_p (parser))
10558 {
10559 tree trailing_attr = cp_parser_attributes_opt (parser);
10560 cplus_decl_attributes (&type,
10561 trailing_attr,
10562 (int) ATTR_FLAG_TYPE_IN_PLACE);
10563 }
10564
10565 /* Finish up the enumeration. */
10566 finish_enum (type);
10567
10568 return type;
10569 }
10570
10571 /* Parse an enumerator-list. The enumerators all have the indicated
10572 TYPE.
10573
10574 enumerator-list:
10575 enumerator-definition
10576 enumerator-list , enumerator-definition */
10577
10578 static void
10579 cp_parser_enumerator_list (cp_parser* parser, tree type)
10580 {
10581 while (true)
10582 {
10583 /* Parse an enumerator-definition. */
10584 cp_parser_enumerator_definition (parser, type);
10585
10586 /* If the next token is not a ',', we've reached the end of
10587 the list. */
10588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10589 break;
10590 /* Otherwise, consume the `,' and keep going. */
10591 cp_lexer_consume_token (parser->lexer);
10592 /* If the next token is a `}', there is a trailing comma. */
10593 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10594 {
10595 if (pedantic && !in_system_header)
10596 pedwarn ("comma at end of enumerator list");
10597 break;
10598 }
10599 }
10600 }
10601
10602 /* Parse an enumerator-definition. The enumerator has the indicated
10603 TYPE.
10604
10605 enumerator-definition:
10606 enumerator
10607 enumerator = constant-expression
10608
10609 enumerator:
10610 identifier */
10611
10612 static void
10613 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10614 {
10615 tree identifier;
10616 tree value;
10617
10618 /* Look for the identifier. */
10619 identifier = cp_parser_identifier (parser);
10620 if (identifier == error_mark_node)
10621 return;
10622
10623 /* If the next token is an '=', then there is an explicit value. */
10624 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10625 {
10626 /* Consume the `=' token. */
10627 cp_lexer_consume_token (parser->lexer);
10628 /* Parse the value. */
10629 value = cp_parser_constant_expression (parser,
10630 /*allow_non_constant_p=*/false,
10631 NULL);
10632 }
10633 else
10634 value = NULL_TREE;
10635
10636 /* Create the enumerator. */
10637 build_enumerator (identifier, value, type);
10638 }
10639
10640 /* Parse a namespace-name.
10641
10642 namespace-name:
10643 original-namespace-name
10644 namespace-alias
10645
10646 Returns the NAMESPACE_DECL for the namespace. */
10647
10648 static tree
10649 cp_parser_namespace_name (cp_parser* parser)
10650 {
10651 tree identifier;
10652 tree namespace_decl;
10653
10654 /* Get the name of the namespace. */
10655 identifier = cp_parser_identifier (parser);
10656 if (identifier == error_mark_node)
10657 return error_mark_node;
10658
10659 /* Look up the identifier in the currently active scope. Look only
10660 for namespaces, due to:
10661
10662 [basic.lookup.udir]
10663
10664 When looking up a namespace-name in a using-directive or alias
10665 definition, only namespace names are considered.
10666
10667 And:
10668
10669 [basic.lookup.qual]
10670
10671 During the lookup of a name preceding the :: scope resolution
10672 operator, object, function, and enumerator names are ignored.
10673
10674 (Note that cp_parser_class_or_namespace_name only calls this
10675 function if the token after the name is the scope resolution
10676 operator.) */
10677 namespace_decl = cp_parser_lookup_name (parser, identifier,
10678 none_type,
10679 /*is_template=*/false,
10680 /*is_namespace=*/true,
10681 /*check_dependency=*/true,
10682 /*ambiguous_decls=*/NULL);
10683 /* If it's not a namespace, issue an error. */
10684 if (namespace_decl == error_mark_node
10685 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10686 {
10687 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10688 error ("%qD is not a namespace-name", identifier);
10689 cp_parser_error (parser, "expected namespace-name");
10690 namespace_decl = error_mark_node;
10691 }
10692
10693 return namespace_decl;
10694 }
10695
10696 /* Parse a namespace-definition.
10697
10698 namespace-definition:
10699 named-namespace-definition
10700 unnamed-namespace-definition
10701
10702 named-namespace-definition:
10703 original-namespace-definition
10704 extension-namespace-definition
10705
10706 original-namespace-definition:
10707 namespace identifier { namespace-body }
10708
10709 extension-namespace-definition:
10710 namespace original-namespace-name { namespace-body }
10711
10712 unnamed-namespace-definition:
10713 namespace { namespace-body } */
10714
10715 static void
10716 cp_parser_namespace_definition (cp_parser* parser)
10717 {
10718 tree identifier, attribs;
10719
10720 /* Look for the `namespace' keyword. */
10721 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10722
10723 /* Get the name of the namespace. We do not attempt to distinguish
10724 between an original-namespace-definition and an
10725 extension-namespace-definition at this point. The semantic
10726 analysis routines are responsible for that. */
10727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10728 identifier = cp_parser_identifier (parser);
10729 else
10730 identifier = NULL_TREE;
10731
10732 /* Parse any specified attributes. */
10733 attribs = cp_parser_attributes_opt (parser);
10734
10735 /* Look for the `{' to start the namespace. */
10736 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10737 /* Start the namespace. */
10738 push_namespace_with_attribs (identifier, attribs);
10739 /* Parse the body of the namespace. */
10740 cp_parser_namespace_body (parser);
10741 /* Finish the namespace. */
10742 pop_namespace ();
10743 /* Look for the final `}'. */
10744 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10745 }
10746
10747 /* Parse a namespace-body.
10748
10749 namespace-body:
10750 declaration-seq [opt] */
10751
10752 static void
10753 cp_parser_namespace_body (cp_parser* parser)
10754 {
10755 cp_parser_declaration_seq_opt (parser);
10756 }
10757
10758 /* Parse a namespace-alias-definition.
10759
10760 namespace-alias-definition:
10761 namespace identifier = qualified-namespace-specifier ; */
10762
10763 static void
10764 cp_parser_namespace_alias_definition (cp_parser* parser)
10765 {
10766 tree identifier;
10767 tree namespace_specifier;
10768
10769 /* Look for the `namespace' keyword. */
10770 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10771 /* Look for the identifier. */
10772 identifier = cp_parser_identifier (parser);
10773 if (identifier == error_mark_node)
10774 return;
10775 /* Look for the `=' token. */
10776 cp_parser_require (parser, CPP_EQ, "`='");
10777 /* Look for the qualified-namespace-specifier. */
10778 namespace_specifier
10779 = cp_parser_qualified_namespace_specifier (parser);
10780 /* Look for the `;' token. */
10781 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10782
10783 /* Register the alias in the symbol table. */
10784 do_namespace_alias (identifier, namespace_specifier);
10785 }
10786
10787 /* Parse a qualified-namespace-specifier.
10788
10789 qualified-namespace-specifier:
10790 :: [opt] nested-name-specifier [opt] namespace-name
10791
10792 Returns a NAMESPACE_DECL corresponding to the specified
10793 namespace. */
10794
10795 static tree
10796 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10797 {
10798 /* Look for the optional `::'. */
10799 cp_parser_global_scope_opt (parser,
10800 /*current_scope_valid_p=*/false);
10801
10802 /* Look for the optional nested-name-specifier. */
10803 cp_parser_nested_name_specifier_opt (parser,
10804 /*typename_keyword_p=*/false,
10805 /*check_dependency_p=*/true,
10806 /*type_p=*/false,
10807 /*is_declaration=*/true);
10808
10809 return cp_parser_namespace_name (parser);
10810 }
10811
10812 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10813 access declaration.
10814
10815 using-declaration:
10816 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10817 using :: unqualified-id ;
10818
10819 access-declaration:
10820 qualified-id ;
10821
10822 */
10823
10824 static bool
10825 cp_parser_using_declaration (cp_parser* parser,
10826 bool access_declaration_p)
10827 {
10828 cp_token *token;
10829 bool typename_p = false;
10830 bool global_scope_p;
10831 tree decl;
10832 tree identifier;
10833 tree qscope;
10834
10835 if (access_declaration_p)
10836 cp_parser_parse_tentatively (parser);
10837 else
10838 {
10839 /* Look for the `using' keyword. */
10840 cp_parser_require_keyword (parser, RID_USING, "`using'");
10841
10842 /* Peek at the next token. */
10843 token = cp_lexer_peek_token (parser->lexer);
10844 /* See if it's `typename'. */
10845 if (token->keyword == RID_TYPENAME)
10846 {
10847 /* Remember that we've seen it. */
10848 typename_p = true;
10849 /* Consume the `typename' token. */
10850 cp_lexer_consume_token (parser->lexer);
10851 }
10852 }
10853
10854 /* Look for the optional global scope qualification. */
10855 global_scope_p
10856 = (cp_parser_global_scope_opt (parser,
10857 /*current_scope_valid_p=*/false)
10858 != NULL_TREE);
10859
10860 /* If we saw `typename', or didn't see `::', then there must be a
10861 nested-name-specifier present. */
10862 if (typename_p || !global_scope_p)
10863 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10864 /*check_dependency_p=*/true,
10865 /*type_p=*/false,
10866 /*is_declaration=*/true);
10867 /* Otherwise, we could be in either of the two productions. In that
10868 case, treat the nested-name-specifier as optional. */
10869 else
10870 qscope = cp_parser_nested_name_specifier_opt (parser,
10871 /*typename_keyword_p=*/false,
10872 /*check_dependency_p=*/true,
10873 /*type_p=*/false,
10874 /*is_declaration=*/true);
10875 if (!qscope)
10876 qscope = global_namespace;
10877
10878 if (access_declaration_p && cp_parser_error_occurred (parser))
10879 /* Something has already gone wrong; there's no need to parse
10880 further. Since an error has occurred, the return value of
10881 cp_parser_parse_definitely will be false, as required. */
10882 return cp_parser_parse_definitely (parser);
10883
10884 /* Parse the unqualified-id. */
10885 identifier = cp_parser_unqualified_id (parser,
10886 /*template_keyword_p=*/false,
10887 /*check_dependency_p=*/true,
10888 /*declarator_p=*/true,
10889 /*optional_p=*/false);
10890
10891 if (access_declaration_p)
10892 {
10893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10894 cp_parser_simulate_error (parser);
10895 if (!cp_parser_parse_definitely (parser))
10896 return false;
10897 }
10898
10899 /* The function we call to handle a using-declaration is different
10900 depending on what scope we are in. */
10901 if (qscope == error_mark_node || identifier == error_mark_node)
10902 ;
10903 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10904 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10905 /* [namespace.udecl]
10906
10907 A using declaration shall not name a template-id. */
10908 error ("a template-id may not appear in a using-declaration");
10909 else
10910 {
10911 if (at_class_scope_p ())
10912 {
10913 /* Create the USING_DECL. */
10914 decl = do_class_using_decl (parser->scope, identifier);
10915 /* Add it to the list of members in this class. */
10916 finish_member_declaration (decl);
10917 }
10918 else
10919 {
10920 decl = cp_parser_lookup_name_simple (parser, identifier);
10921 if (decl == error_mark_node)
10922 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10923 else if (!at_namespace_scope_p ())
10924 do_local_using_decl (decl, qscope, identifier);
10925 else
10926 do_toplevel_using_decl (decl, qscope, identifier);
10927 }
10928 }
10929
10930 /* Look for the final `;'. */
10931 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10932
10933 return true;
10934 }
10935
10936 /* Parse a using-directive.
10937
10938 using-directive:
10939 using namespace :: [opt] nested-name-specifier [opt]
10940 namespace-name ; */
10941
10942 static void
10943 cp_parser_using_directive (cp_parser* parser)
10944 {
10945 tree namespace_decl;
10946 tree attribs;
10947
10948 /* Look for the `using' keyword. */
10949 cp_parser_require_keyword (parser, RID_USING, "`using'");
10950 /* And the `namespace' keyword. */
10951 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10952 /* Look for the optional `::' operator. */
10953 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10954 /* And the optional nested-name-specifier. */
10955 cp_parser_nested_name_specifier_opt (parser,
10956 /*typename_keyword_p=*/false,
10957 /*check_dependency_p=*/true,
10958 /*type_p=*/false,
10959 /*is_declaration=*/true);
10960 /* Get the namespace being used. */
10961 namespace_decl = cp_parser_namespace_name (parser);
10962 /* And any specified attributes. */
10963 attribs = cp_parser_attributes_opt (parser);
10964 /* Update the symbol table. */
10965 parse_using_directive (namespace_decl, attribs);
10966 /* Look for the final `;'. */
10967 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10968 }
10969
10970 /* Parse an asm-definition.
10971
10972 asm-definition:
10973 asm ( string-literal ) ;
10974
10975 GNU Extension:
10976
10977 asm-definition:
10978 asm volatile [opt] ( string-literal ) ;
10979 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10980 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10981 : asm-operand-list [opt] ) ;
10982 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10983 : asm-operand-list [opt]
10984 : asm-operand-list [opt] ) ; */
10985
10986 static void
10987 cp_parser_asm_definition (cp_parser* parser)
10988 {
10989 tree string;
10990 tree outputs = NULL_TREE;
10991 tree inputs = NULL_TREE;
10992 tree clobbers = NULL_TREE;
10993 tree asm_stmt;
10994 bool volatile_p = false;
10995 bool extended_p = false;
10996
10997 /* Look for the `asm' keyword. */
10998 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10999 /* See if the next token is `volatile'. */
11000 if (cp_parser_allow_gnu_extensions_p (parser)
11001 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11002 {
11003 /* Remember that we saw the `volatile' keyword. */
11004 volatile_p = true;
11005 /* Consume the token. */
11006 cp_lexer_consume_token (parser->lexer);
11007 }
11008 /* Look for the opening `('. */
11009 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11010 return;
11011 /* Look for the string. */
11012 string = cp_parser_string_literal (parser, false, false);
11013 if (string == error_mark_node)
11014 {
11015 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11016 /*consume_paren=*/true);
11017 return;
11018 }
11019
11020 /* If we're allowing GNU extensions, check for the extended assembly
11021 syntax. Unfortunately, the `:' tokens need not be separated by
11022 a space in C, and so, for compatibility, we tolerate that here
11023 too. Doing that means that we have to treat the `::' operator as
11024 two `:' tokens. */
11025 if (cp_parser_allow_gnu_extensions_p (parser)
11026 && parser->in_function_body
11027 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11028 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11029 {
11030 bool inputs_p = false;
11031 bool clobbers_p = false;
11032
11033 /* The extended syntax was used. */
11034 extended_p = true;
11035
11036 /* Look for outputs. */
11037 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11038 {
11039 /* Consume the `:'. */
11040 cp_lexer_consume_token (parser->lexer);
11041 /* Parse the output-operands. */
11042 if (cp_lexer_next_token_is_not (parser->lexer,
11043 CPP_COLON)
11044 && cp_lexer_next_token_is_not (parser->lexer,
11045 CPP_SCOPE)
11046 && cp_lexer_next_token_is_not (parser->lexer,
11047 CPP_CLOSE_PAREN))
11048 outputs = cp_parser_asm_operand_list (parser);
11049 }
11050 /* If the next token is `::', there are no outputs, and the
11051 next token is the beginning of the inputs. */
11052 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11053 /* The inputs are coming next. */
11054 inputs_p = true;
11055
11056 /* Look for inputs. */
11057 if (inputs_p
11058 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11059 {
11060 /* Consume the `:' or `::'. */
11061 cp_lexer_consume_token (parser->lexer);
11062 /* Parse the output-operands. */
11063 if (cp_lexer_next_token_is_not (parser->lexer,
11064 CPP_COLON)
11065 && cp_lexer_next_token_is_not (parser->lexer,
11066 CPP_CLOSE_PAREN))
11067 inputs = cp_parser_asm_operand_list (parser);
11068 }
11069 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11070 /* The clobbers are coming next. */
11071 clobbers_p = true;
11072
11073 /* Look for clobbers. */
11074 if (clobbers_p
11075 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11076 {
11077 /* Consume the `:' or `::'. */
11078 cp_lexer_consume_token (parser->lexer);
11079 /* Parse the clobbers. */
11080 if (cp_lexer_next_token_is_not (parser->lexer,
11081 CPP_CLOSE_PAREN))
11082 clobbers = cp_parser_asm_clobber_list (parser);
11083 }
11084 }
11085 /* Look for the closing `)'. */
11086 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11087 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11088 /*consume_paren=*/true);
11089 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11090
11091 /* Create the ASM_EXPR. */
11092 if (parser->in_function_body)
11093 {
11094 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11095 inputs, clobbers);
11096 /* If the extended syntax was not used, mark the ASM_EXPR. */
11097 if (!extended_p)
11098 {
11099 tree temp = asm_stmt;
11100 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11101 temp = TREE_OPERAND (temp, 0);
11102
11103 ASM_INPUT_P (temp) = 1;
11104 }
11105 }
11106 else
11107 cgraph_add_asm_node (string);
11108 }
11109
11110 /* Declarators [gram.dcl.decl] */
11111
11112 /* Parse an init-declarator.
11113
11114 init-declarator:
11115 declarator initializer [opt]
11116
11117 GNU Extension:
11118
11119 init-declarator:
11120 declarator asm-specification [opt] attributes [opt] initializer [opt]
11121
11122 function-definition:
11123 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11124 function-body
11125 decl-specifier-seq [opt] declarator function-try-block
11126
11127 GNU Extension:
11128
11129 function-definition:
11130 __extension__ function-definition
11131
11132 The DECL_SPECIFIERS apply to this declarator. Returns a
11133 representation of the entity declared. If MEMBER_P is TRUE, then
11134 this declarator appears in a class scope. The new DECL created by
11135 this declarator is returned.
11136
11137 The CHECKS are access checks that should be performed once we know
11138 what entity is being declared (and, therefore, what classes have
11139 befriended it).
11140
11141 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11142 for a function-definition here as well. If the declarator is a
11143 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11144 be TRUE upon return. By that point, the function-definition will
11145 have been completely parsed.
11146
11147 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11148 is FALSE. */
11149
11150 static tree
11151 cp_parser_init_declarator (cp_parser* parser,
11152 cp_decl_specifier_seq *decl_specifiers,
11153 tree checks,
11154 bool function_definition_allowed_p,
11155 bool member_p,
11156 int declares_class_or_enum,
11157 bool* function_definition_p)
11158 {
11159 cp_token *token;
11160 cp_declarator *declarator;
11161 tree prefix_attributes;
11162 tree attributes;
11163 tree asm_specification;
11164 tree initializer;
11165 tree decl = NULL_TREE;
11166 tree scope;
11167 bool is_initialized;
11168 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11169 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11170 "(...)". */
11171 enum cpp_ttype initialization_kind;
11172 bool is_parenthesized_init = false;
11173 bool is_non_constant_init;
11174 int ctor_dtor_or_conv_p;
11175 bool friend_p;
11176 tree pushed_scope = NULL;
11177
11178 /* Gather the attributes that were provided with the
11179 decl-specifiers. */
11180 prefix_attributes = decl_specifiers->attributes;
11181
11182 /* Assume that this is not the declarator for a function
11183 definition. */
11184 if (function_definition_p)
11185 *function_definition_p = false;
11186
11187 /* Defer access checks while parsing the declarator; we cannot know
11188 what names are accessible until we know what is being
11189 declared. */
11190 resume_deferring_access_checks ();
11191
11192 /* Parse the declarator. */
11193 declarator
11194 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11195 &ctor_dtor_or_conv_p,
11196 /*parenthesized_p=*/NULL,
11197 /*member_p=*/false);
11198 /* Gather up the deferred checks. */
11199 stop_deferring_access_checks ();
11200
11201 /* If the DECLARATOR was erroneous, there's no need to go
11202 further. */
11203 if (declarator == cp_error_declarator)
11204 return error_mark_node;
11205
11206 /* Check that the number of template-parameter-lists is OK. */
11207 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11208 return error_mark_node;
11209
11210 if (declares_class_or_enum & 2)
11211 cp_parser_check_for_definition_in_return_type (declarator,
11212 decl_specifiers->type);
11213
11214 /* Figure out what scope the entity declared by the DECLARATOR is
11215 located in. `grokdeclarator' sometimes changes the scope, so
11216 we compute it now. */
11217 scope = get_scope_of_declarator (declarator);
11218
11219 /* If we're allowing GNU extensions, look for an asm-specification
11220 and attributes. */
11221 if (cp_parser_allow_gnu_extensions_p (parser))
11222 {
11223 /* Look for an asm-specification. */
11224 asm_specification = cp_parser_asm_specification_opt (parser);
11225 /* And attributes. */
11226 attributes = cp_parser_attributes_opt (parser);
11227 }
11228 else
11229 {
11230 asm_specification = NULL_TREE;
11231 attributes = NULL_TREE;
11232 }
11233
11234 /* Peek at the next token. */
11235 token = cp_lexer_peek_token (parser->lexer);
11236 /* Check to see if the token indicates the start of a
11237 function-definition. */
11238 if (cp_parser_token_starts_function_definition_p (token))
11239 {
11240 if (!function_definition_allowed_p)
11241 {
11242 /* If a function-definition should not appear here, issue an
11243 error message. */
11244 cp_parser_error (parser,
11245 "a function-definition is not allowed here");
11246 return error_mark_node;
11247 }
11248 else
11249 {
11250 /* Neither attributes nor an asm-specification are allowed
11251 on a function-definition. */
11252 if (asm_specification)
11253 error ("an asm-specification is not allowed on a function-definition");
11254 if (attributes)
11255 error ("attributes are not allowed on a function-definition");
11256 /* This is a function-definition. */
11257 *function_definition_p = true;
11258
11259 /* Parse the function definition. */
11260 if (member_p)
11261 decl = cp_parser_save_member_function_body (parser,
11262 decl_specifiers,
11263 declarator,
11264 prefix_attributes);
11265 else
11266 decl
11267 = (cp_parser_function_definition_from_specifiers_and_declarator
11268 (parser, decl_specifiers, prefix_attributes, declarator));
11269
11270 return decl;
11271 }
11272 }
11273
11274 /* [dcl.dcl]
11275
11276 Only in function declarations for constructors, destructors, and
11277 type conversions can the decl-specifier-seq be omitted.
11278
11279 We explicitly postpone this check past the point where we handle
11280 function-definitions because we tolerate function-definitions
11281 that are missing their return types in some modes. */
11282 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11283 {
11284 cp_parser_error (parser,
11285 "expected constructor, destructor, or type conversion");
11286 return error_mark_node;
11287 }
11288
11289 /* An `=' or an `(' indicates an initializer. */
11290 if (token->type == CPP_EQ
11291 || token->type == CPP_OPEN_PAREN)
11292 {
11293 is_initialized = true;
11294 initialization_kind = token->type;
11295 }
11296 else
11297 {
11298 /* If the init-declarator isn't initialized and isn't followed by a
11299 `,' or `;', it's not a valid init-declarator. */
11300 if (token->type != CPP_COMMA
11301 && token->type != CPP_SEMICOLON)
11302 {
11303 cp_parser_error (parser, "expected initializer");
11304 return error_mark_node;
11305 }
11306 is_initialized = false;
11307 initialization_kind = CPP_EOF;
11308 }
11309
11310 /* Because start_decl has side-effects, we should only call it if we
11311 know we're going ahead. By this point, we know that we cannot
11312 possibly be looking at any other construct. */
11313 cp_parser_commit_to_tentative_parse (parser);
11314
11315 /* If the decl specifiers were bad, issue an error now that we're
11316 sure this was intended to be a declarator. Then continue
11317 declaring the variable(s), as int, to try to cut down on further
11318 errors. */
11319 if (decl_specifiers->any_specifiers_p
11320 && decl_specifiers->type == error_mark_node)
11321 {
11322 cp_parser_error (parser, "invalid type in declaration");
11323 decl_specifiers->type = integer_type_node;
11324 }
11325
11326 /* Check to see whether or not this declaration is a friend. */
11327 friend_p = cp_parser_friend_p (decl_specifiers);
11328
11329 /* Enter the newly declared entry in the symbol table. If we're
11330 processing a declaration in a class-specifier, we wait until
11331 after processing the initializer. */
11332 if (!member_p)
11333 {
11334 if (parser->in_unbraced_linkage_specification_p)
11335 decl_specifiers->storage_class = sc_extern;
11336 decl = start_decl (declarator, decl_specifiers,
11337 is_initialized, attributes, prefix_attributes,
11338 &pushed_scope);
11339 }
11340 else if (scope)
11341 /* Enter the SCOPE. That way unqualified names appearing in the
11342 initializer will be looked up in SCOPE. */
11343 pushed_scope = push_scope (scope);
11344
11345 /* Perform deferred access control checks, now that we know in which
11346 SCOPE the declared entity resides. */
11347 if (!member_p && decl)
11348 {
11349 tree saved_current_function_decl = NULL_TREE;
11350
11351 /* If the entity being declared is a function, pretend that we
11352 are in its scope. If it is a `friend', it may have access to
11353 things that would not otherwise be accessible. */
11354 if (TREE_CODE (decl) == FUNCTION_DECL)
11355 {
11356 saved_current_function_decl = current_function_decl;
11357 current_function_decl = decl;
11358 }
11359
11360 /* Perform access checks for template parameters. */
11361 cp_parser_perform_template_parameter_access_checks (checks);
11362
11363 /* Perform the access control checks for the declarator and the
11364 the decl-specifiers. */
11365 perform_deferred_access_checks ();
11366
11367 /* Restore the saved value. */
11368 if (TREE_CODE (decl) == FUNCTION_DECL)
11369 current_function_decl = saved_current_function_decl;
11370 }
11371
11372 /* Parse the initializer. */
11373 initializer = NULL_TREE;
11374 is_parenthesized_init = false;
11375 is_non_constant_init = true;
11376 if (is_initialized)
11377 {
11378 if (function_declarator_p (declarator))
11379 {
11380 if (initialization_kind == CPP_EQ)
11381 initializer = cp_parser_pure_specifier (parser);
11382 else
11383 {
11384 /* If the declaration was erroneous, we don't really
11385 know what the user intended, so just silently
11386 consume the initializer. */
11387 if (decl != error_mark_node)
11388 error ("initializer provided for function");
11389 cp_parser_skip_to_closing_parenthesis (parser,
11390 /*recovering=*/true,
11391 /*or_comma=*/false,
11392 /*consume_paren=*/true);
11393 }
11394 }
11395 else
11396 initializer = cp_parser_initializer (parser,
11397 &is_parenthesized_init,
11398 &is_non_constant_init);
11399 }
11400
11401 /* The old parser allows attributes to appear after a parenthesized
11402 initializer. Mark Mitchell proposed removing this functionality
11403 on the GCC mailing lists on 2002-08-13. This parser accepts the
11404 attributes -- but ignores them. */
11405 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11406 if (cp_parser_attributes_opt (parser))
11407 warning (OPT_Wattributes,
11408 "attributes after parenthesized initializer ignored");
11409
11410 /* For an in-class declaration, use `grokfield' to create the
11411 declaration. */
11412 if (member_p)
11413 {
11414 if (pushed_scope)
11415 {
11416 pop_scope (pushed_scope);
11417 pushed_scope = false;
11418 }
11419 decl = grokfield (declarator, decl_specifiers,
11420 initializer, !is_non_constant_init,
11421 /*asmspec=*/NULL_TREE,
11422 prefix_attributes);
11423 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11424 cp_parser_save_default_args (parser, decl);
11425 }
11426
11427 /* Finish processing the declaration. But, skip friend
11428 declarations. */
11429 if (!friend_p && decl && decl != error_mark_node)
11430 {
11431 cp_finish_decl (decl,
11432 initializer, !is_non_constant_init,
11433 asm_specification,
11434 /* If the initializer is in parentheses, then this is
11435 a direct-initialization, which means that an
11436 `explicit' constructor is OK. Otherwise, an
11437 `explicit' constructor cannot be used. */
11438 ((is_parenthesized_init || !is_initialized)
11439 ? 0 : LOOKUP_ONLYCONVERTING));
11440 }
11441 if (!friend_p && pushed_scope)
11442 pop_scope (pushed_scope);
11443
11444 return decl;
11445 }
11446
11447 /* Parse a declarator.
11448
11449 declarator:
11450 direct-declarator
11451 ptr-operator declarator
11452
11453 abstract-declarator:
11454 ptr-operator abstract-declarator [opt]
11455 direct-abstract-declarator
11456
11457 GNU Extensions:
11458
11459 declarator:
11460 attributes [opt] direct-declarator
11461 attributes [opt] ptr-operator declarator
11462
11463 abstract-declarator:
11464 attributes [opt] ptr-operator abstract-declarator [opt]
11465 attributes [opt] direct-abstract-declarator
11466
11467 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11468 detect constructor, destructor or conversion operators. It is set
11469 to -1 if the declarator is a name, and +1 if it is a
11470 function. Otherwise it is set to zero. Usually you just want to
11471 test for >0, but internally the negative value is used.
11472
11473 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11474 a decl-specifier-seq unless it declares a constructor, destructor,
11475 or conversion. It might seem that we could check this condition in
11476 semantic analysis, rather than parsing, but that makes it difficult
11477 to handle something like `f()'. We want to notice that there are
11478 no decl-specifiers, and therefore realize that this is an
11479 expression, not a declaration.)
11480
11481 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11482 the declarator is a direct-declarator of the form "(...)".
11483
11484 MEMBER_P is true iff this declarator is a member-declarator. */
11485
11486 static cp_declarator *
11487 cp_parser_declarator (cp_parser* parser,
11488 cp_parser_declarator_kind dcl_kind,
11489 int* ctor_dtor_or_conv_p,
11490 bool* parenthesized_p,
11491 bool member_p)
11492 {
11493 cp_token *token;
11494 cp_declarator *declarator;
11495 enum tree_code code;
11496 cp_cv_quals cv_quals;
11497 tree class_type;
11498 tree attributes = NULL_TREE;
11499
11500 /* Assume this is not a constructor, destructor, or type-conversion
11501 operator. */
11502 if (ctor_dtor_or_conv_p)
11503 *ctor_dtor_or_conv_p = 0;
11504
11505 if (cp_parser_allow_gnu_extensions_p (parser))
11506 attributes = cp_parser_attributes_opt (parser);
11507
11508 /* Peek at the next token. */
11509 token = cp_lexer_peek_token (parser->lexer);
11510
11511 /* Check for the ptr-operator production. */
11512 cp_parser_parse_tentatively (parser);
11513 /* Parse the ptr-operator. */
11514 code = cp_parser_ptr_operator (parser,
11515 &class_type,
11516 &cv_quals);
11517 /* If that worked, then we have a ptr-operator. */
11518 if (cp_parser_parse_definitely (parser))
11519 {
11520 /* If a ptr-operator was found, then this declarator was not
11521 parenthesized. */
11522 if (parenthesized_p)
11523 *parenthesized_p = true;
11524 /* The dependent declarator is optional if we are parsing an
11525 abstract-declarator. */
11526 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11527 cp_parser_parse_tentatively (parser);
11528
11529 /* Parse the dependent declarator. */
11530 declarator = cp_parser_declarator (parser, dcl_kind,
11531 /*ctor_dtor_or_conv_p=*/NULL,
11532 /*parenthesized_p=*/NULL,
11533 /*member_p=*/false);
11534
11535 /* If we are parsing an abstract-declarator, we must handle the
11536 case where the dependent declarator is absent. */
11537 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11538 && !cp_parser_parse_definitely (parser))
11539 declarator = NULL;
11540
11541 /* Build the representation of the ptr-operator. */
11542 if (class_type)
11543 declarator = make_ptrmem_declarator (cv_quals,
11544 class_type,
11545 declarator);
11546 else if (code == INDIRECT_REF)
11547 declarator = make_pointer_declarator (cv_quals, declarator);
11548 else
11549 declarator = make_reference_declarator (cv_quals, declarator);
11550 }
11551 /* Everything else is a direct-declarator. */
11552 else
11553 {
11554 if (parenthesized_p)
11555 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11556 CPP_OPEN_PAREN);
11557 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11558 ctor_dtor_or_conv_p,
11559 member_p);
11560 }
11561
11562 if (attributes && declarator && declarator != cp_error_declarator)
11563 declarator->attributes = attributes;
11564
11565 return declarator;
11566 }
11567
11568 /* Parse a direct-declarator or direct-abstract-declarator.
11569
11570 direct-declarator:
11571 declarator-id
11572 direct-declarator ( parameter-declaration-clause )
11573 cv-qualifier-seq [opt]
11574 exception-specification [opt]
11575 direct-declarator [ constant-expression [opt] ]
11576 ( declarator )
11577
11578 direct-abstract-declarator:
11579 direct-abstract-declarator [opt]
11580 ( parameter-declaration-clause )
11581 cv-qualifier-seq [opt]
11582 exception-specification [opt]
11583 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11584 ( abstract-declarator )
11585
11586 Returns a representation of the declarator. DCL_KIND is
11587 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11588 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11589 we are parsing a direct-declarator. It is
11590 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11591 of ambiguity we prefer an abstract declarator, as per
11592 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11593 cp_parser_declarator. */
11594
11595 static cp_declarator *
11596 cp_parser_direct_declarator (cp_parser* parser,
11597 cp_parser_declarator_kind dcl_kind,
11598 int* ctor_dtor_or_conv_p,
11599 bool member_p)
11600 {
11601 cp_token *token;
11602 cp_declarator *declarator = NULL;
11603 tree scope = NULL_TREE;
11604 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11605 bool saved_in_declarator_p = parser->in_declarator_p;
11606 bool first = true;
11607 tree pushed_scope = NULL_TREE;
11608
11609 while (true)
11610 {
11611 /* Peek at the next token. */
11612 token = cp_lexer_peek_token (parser->lexer);
11613 if (token->type == CPP_OPEN_PAREN)
11614 {
11615 /* This is either a parameter-declaration-clause, or a
11616 parenthesized declarator. When we know we are parsing a
11617 named declarator, it must be a parenthesized declarator
11618 if FIRST is true. For instance, `(int)' is a
11619 parameter-declaration-clause, with an omitted
11620 direct-abstract-declarator. But `((*))', is a
11621 parenthesized abstract declarator. Finally, when T is a
11622 template parameter `(T)' is a
11623 parameter-declaration-clause, and not a parenthesized
11624 named declarator.
11625
11626 We first try and parse a parameter-declaration-clause,
11627 and then try a nested declarator (if FIRST is true).
11628
11629 It is not an error for it not to be a
11630 parameter-declaration-clause, even when FIRST is
11631 false. Consider,
11632
11633 int i (int);
11634 int i (3);
11635
11636 The first is the declaration of a function while the
11637 second is a the definition of a variable, including its
11638 initializer.
11639
11640 Having seen only the parenthesis, we cannot know which of
11641 these two alternatives should be selected. Even more
11642 complex are examples like:
11643
11644 int i (int (a));
11645 int i (int (3));
11646
11647 The former is a function-declaration; the latter is a
11648 variable initialization.
11649
11650 Thus again, we try a parameter-declaration-clause, and if
11651 that fails, we back out and return. */
11652
11653 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11654 {
11655 cp_parameter_declarator *params;
11656 unsigned saved_num_template_parameter_lists;
11657
11658 /* In a member-declarator, the only valid interpretation
11659 of a parenthesis is the start of a
11660 parameter-declaration-clause. (It is invalid to
11661 initialize a static data member with a parenthesized
11662 initializer; only the "=" form of initialization is
11663 permitted.) */
11664 if (!member_p)
11665 cp_parser_parse_tentatively (parser);
11666
11667 /* Consume the `('. */
11668 cp_lexer_consume_token (parser->lexer);
11669 if (first)
11670 {
11671 /* If this is going to be an abstract declarator, we're
11672 in a declarator and we can't have default args. */
11673 parser->default_arg_ok_p = false;
11674 parser->in_declarator_p = true;
11675 }
11676
11677 /* Inside the function parameter list, surrounding
11678 template-parameter-lists do not apply. */
11679 saved_num_template_parameter_lists
11680 = parser->num_template_parameter_lists;
11681 parser->num_template_parameter_lists = 0;
11682
11683 /* Parse the parameter-declaration-clause. */
11684 params = cp_parser_parameter_declaration_clause (parser);
11685
11686 parser->num_template_parameter_lists
11687 = saved_num_template_parameter_lists;
11688
11689 /* If all went well, parse the cv-qualifier-seq and the
11690 exception-specification. */
11691 if (member_p || cp_parser_parse_definitely (parser))
11692 {
11693 cp_cv_quals cv_quals;
11694 tree exception_specification;
11695
11696 if (ctor_dtor_or_conv_p)
11697 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11698 first = false;
11699 /* Consume the `)'. */
11700 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11701
11702 /* Parse the cv-qualifier-seq. */
11703 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11704 /* And the exception-specification. */
11705 exception_specification
11706 = cp_parser_exception_specification_opt (parser);
11707
11708 /* Create the function-declarator. */
11709 declarator = make_call_declarator (declarator,
11710 params,
11711 cv_quals,
11712 exception_specification);
11713 /* Any subsequent parameter lists are to do with
11714 return type, so are not those of the declared
11715 function. */
11716 parser->default_arg_ok_p = false;
11717
11718 /* Repeat the main loop. */
11719 continue;
11720 }
11721 }
11722
11723 /* If this is the first, we can try a parenthesized
11724 declarator. */
11725 if (first)
11726 {
11727 bool saved_in_type_id_in_expr_p;
11728
11729 parser->default_arg_ok_p = saved_default_arg_ok_p;
11730 parser->in_declarator_p = saved_in_declarator_p;
11731
11732 /* Consume the `('. */
11733 cp_lexer_consume_token (parser->lexer);
11734 /* Parse the nested declarator. */
11735 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11736 parser->in_type_id_in_expr_p = true;
11737 declarator
11738 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11739 /*parenthesized_p=*/NULL,
11740 member_p);
11741 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11742 first = false;
11743 /* Expect a `)'. */
11744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11745 declarator = cp_error_declarator;
11746 if (declarator == cp_error_declarator)
11747 break;
11748
11749 goto handle_declarator;
11750 }
11751 /* Otherwise, we must be done. */
11752 else
11753 break;
11754 }
11755 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11756 && token->type == CPP_OPEN_SQUARE)
11757 {
11758 /* Parse an array-declarator. */
11759 tree bounds;
11760
11761 if (ctor_dtor_or_conv_p)
11762 *ctor_dtor_or_conv_p = 0;
11763
11764 first = false;
11765 parser->default_arg_ok_p = false;
11766 parser->in_declarator_p = true;
11767 /* Consume the `['. */
11768 cp_lexer_consume_token (parser->lexer);
11769 /* Peek at the next token. */
11770 token = cp_lexer_peek_token (parser->lexer);
11771 /* If the next token is `]', then there is no
11772 constant-expression. */
11773 if (token->type != CPP_CLOSE_SQUARE)
11774 {
11775 bool non_constant_p;
11776
11777 bounds
11778 = cp_parser_constant_expression (parser,
11779 /*allow_non_constant=*/true,
11780 &non_constant_p);
11781 if (!non_constant_p)
11782 bounds = fold_non_dependent_expr (bounds);
11783 /* Normally, the array bound must be an integral constant
11784 expression. However, as an extension, we allow VLAs
11785 in function scopes. */
11786 else if (!parser->in_function_body)
11787 {
11788 error ("array bound is not an integer constant");
11789 bounds = error_mark_node;
11790 }
11791 }
11792 else
11793 bounds = NULL_TREE;
11794 /* Look for the closing `]'. */
11795 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11796 {
11797 declarator = cp_error_declarator;
11798 break;
11799 }
11800
11801 declarator = make_array_declarator (declarator, bounds);
11802 }
11803 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11804 {
11805 tree qualifying_scope;
11806 tree unqualified_name;
11807 special_function_kind sfk;
11808 bool abstract_ok;
11809
11810 /* Parse a declarator-id */
11811 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11812 if (abstract_ok)
11813 cp_parser_parse_tentatively (parser);
11814 unqualified_name
11815 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11816 qualifying_scope = parser->scope;
11817 if (abstract_ok)
11818 {
11819 if (!cp_parser_parse_definitely (parser))
11820 unqualified_name = error_mark_node;
11821 else if (unqualified_name
11822 && (qualifying_scope
11823 || (TREE_CODE (unqualified_name)
11824 != IDENTIFIER_NODE)))
11825 {
11826 cp_parser_error (parser, "expected unqualified-id");
11827 unqualified_name = error_mark_node;
11828 }
11829 }
11830
11831 if (!unqualified_name)
11832 return NULL;
11833 if (unqualified_name == error_mark_node)
11834 {
11835 declarator = cp_error_declarator;
11836 break;
11837 }
11838
11839 if (qualifying_scope && at_namespace_scope_p ()
11840 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11841 {
11842 /* In the declaration of a member of a template class
11843 outside of the class itself, the SCOPE will sometimes
11844 be a TYPENAME_TYPE. For example, given:
11845
11846 template <typename T>
11847 int S<T>::R::i = 3;
11848
11849 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11850 this context, we must resolve S<T>::R to an ordinary
11851 type, rather than a typename type.
11852
11853 The reason we normally avoid resolving TYPENAME_TYPEs
11854 is that a specialization of `S' might render
11855 `S<T>::R' not a type. However, if `S' is
11856 specialized, then this `i' will not be used, so there
11857 is no harm in resolving the types here. */
11858 tree type;
11859
11860 /* Resolve the TYPENAME_TYPE. */
11861 type = resolve_typename_type (qualifying_scope,
11862 /*only_current_p=*/false);
11863 /* If that failed, the declarator is invalid. */
11864 if (type == error_mark_node)
11865 error ("%<%T::%D%> is not a type",
11866 TYPE_CONTEXT (qualifying_scope),
11867 TYPE_IDENTIFIER (qualifying_scope));
11868 qualifying_scope = type;
11869 }
11870
11871 sfk = sfk_none;
11872 if (unqualified_name)
11873 {
11874 tree class_type;
11875
11876 if (qualifying_scope
11877 && CLASS_TYPE_P (qualifying_scope))
11878 class_type = qualifying_scope;
11879 else
11880 class_type = current_class_type;
11881
11882 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11883 {
11884 tree name_type = TREE_TYPE (unqualified_name);
11885 if (class_type && same_type_p (name_type, class_type))
11886 {
11887 if (qualifying_scope
11888 && CLASSTYPE_USE_TEMPLATE (name_type))
11889 {
11890 error ("invalid use of constructor as a template");
11891 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11892 "name the constructor in a qualified name",
11893 class_type,
11894 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11895 class_type, name_type);
11896 declarator = cp_error_declarator;
11897 break;
11898 }
11899 else
11900 unqualified_name = constructor_name (class_type);
11901 }
11902 else
11903 {
11904 /* We do not attempt to print the declarator
11905 here because we do not have enough
11906 information about its original syntactic
11907 form. */
11908 cp_parser_error (parser, "invalid declarator");
11909 declarator = cp_error_declarator;
11910 break;
11911 }
11912 }
11913
11914 if (class_type)
11915 {
11916 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11917 sfk = sfk_destructor;
11918 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11919 sfk = sfk_conversion;
11920 else if (/* There's no way to declare a constructor
11921 for an anonymous type, even if the type
11922 got a name for linkage purposes. */
11923 !TYPE_WAS_ANONYMOUS (class_type)
11924 && constructor_name_p (unqualified_name,
11925 class_type))
11926 {
11927 unqualified_name = constructor_name (class_type);
11928 sfk = sfk_constructor;
11929 }
11930
11931 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11932 *ctor_dtor_or_conv_p = -1;
11933 }
11934 }
11935 declarator = make_id_declarator (qualifying_scope,
11936 unqualified_name,
11937 sfk);
11938 declarator->id_loc = token->location;
11939
11940 handle_declarator:;
11941 scope = get_scope_of_declarator (declarator);
11942 if (scope)
11943 /* Any names that appear after the declarator-id for a
11944 member are looked up in the containing scope. */
11945 pushed_scope = push_scope (scope);
11946 parser->in_declarator_p = true;
11947 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11948 || (declarator && declarator->kind == cdk_id))
11949 /* Default args are only allowed on function
11950 declarations. */
11951 parser->default_arg_ok_p = saved_default_arg_ok_p;
11952 else
11953 parser->default_arg_ok_p = false;
11954
11955 first = false;
11956 }
11957 /* We're done. */
11958 else
11959 break;
11960 }
11961
11962 /* For an abstract declarator, we might wind up with nothing at this
11963 point. That's an error; the declarator is not optional. */
11964 if (!declarator)
11965 cp_parser_error (parser, "expected declarator");
11966
11967 /* If we entered a scope, we must exit it now. */
11968 if (pushed_scope)
11969 pop_scope (pushed_scope);
11970
11971 parser->default_arg_ok_p = saved_default_arg_ok_p;
11972 parser->in_declarator_p = saved_in_declarator_p;
11973
11974 return declarator;
11975 }
11976
11977 /* Parse a ptr-operator.
11978
11979 ptr-operator:
11980 * cv-qualifier-seq [opt]
11981 &
11982 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11983
11984 GNU Extension:
11985
11986 ptr-operator:
11987 & cv-qualifier-seq [opt]
11988
11989 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11990 Returns ADDR_EXPR if a reference was used. In the case of a
11991 pointer-to-member, *TYPE is filled in with the TYPE containing the
11992 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11993 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11994 ERROR_MARK if an error occurred. */
11995
11996 static enum tree_code
11997 cp_parser_ptr_operator (cp_parser* parser,
11998 tree* type,
11999 cp_cv_quals *cv_quals)
12000 {
12001 enum tree_code code = ERROR_MARK;
12002 cp_token *token;
12003
12004 /* Assume that it's not a pointer-to-member. */
12005 *type = NULL_TREE;
12006 /* And that there are no cv-qualifiers. */
12007 *cv_quals = TYPE_UNQUALIFIED;
12008
12009 /* Peek at the next token. */
12010 token = cp_lexer_peek_token (parser->lexer);
12011 /* If it's a `*' or `&' we have a pointer or reference. */
12012 if (token->type == CPP_MULT || token->type == CPP_AND)
12013 {
12014 /* Remember which ptr-operator we were processing. */
12015 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12016
12017 /* Consume the `*' or `&'. */
12018 cp_lexer_consume_token (parser->lexer);
12019
12020 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12021 `&', if we are allowing GNU extensions. (The only qualifier
12022 that can legally appear after `&' is `restrict', but that is
12023 enforced during semantic analysis. */
12024 if (code == INDIRECT_REF
12025 || cp_parser_allow_gnu_extensions_p (parser))
12026 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12027 }
12028 else
12029 {
12030 /* Try the pointer-to-member case. */
12031 cp_parser_parse_tentatively (parser);
12032 /* Look for the optional `::' operator. */
12033 cp_parser_global_scope_opt (parser,
12034 /*current_scope_valid_p=*/false);
12035 /* Look for the nested-name specifier. */
12036 cp_parser_nested_name_specifier (parser,
12037 /*typename_keyword_p=*/false,
12038 /*check_dependency_p=*/true,
12039 /*type_p=*/false,
12040 /*is_declaration=*/false);
12041 /* If we found it, and the next token is a `*', then we are
12042 indeed looking at a pointer-to-member operator. */
12043 if (!cp_parser_error_occurred (parser)
12044 && cp_parser_require (parser, CPP_MULT, "`*'"))
12045 {
12046 /* Indicate that the `*' operator was used. */
12047 code = INDIRECT_REF;
12048
12049 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12050 error ("%qD is a namespace", parser->scope);
12051 else
12052 {
12053 /* The type of which the member is a member is given by the
12054 current SCOPE. */
12055 *type = parser->scope;
12056 /* The next name will not be qualified. */
12057 parser->scope = NULL_TREE;
12058 parser->qualifying_scope = NULL_TREE;
12059 parser->object_scope = NULL_TREE;
12060 /* Look for the optional cv-qualifier-seq. */
12061 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12062 }
12063 }
12064 /* If that didn't work we don't have a ptr-operator. */
12065 if (!cp_parser_parse_definitely (parser))
12066 cp_parser_error (parser, "expected ptr-operator");
12067 }
12068
12069 return code;
12070 }
12071
12072 /* Parse an (optional) cv-qualifier-seq.
12073
12074 cv-qualifier-seq:
12075 cv-qualifier cv-qualifier-seq [opt]
12076
12077 cv-qualifier:
12078 const
12079 volatile
12080
12081 GNU Extension:
12082
12083 cv-qualifier:
12084 __restrict__
12085
12086 Returns a bitmask representing the cv-qualifiers. */
12087
12088 static cp_cv_quals
12089 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12090 {
12091 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12092
12093 while (true)
12094 {
12095 cp_token *token;
12096 cp_cv_quals cv_qualifier;
12097
12098 /* Peek at the next token. */
12099 token = cp_lexer_peek_token (parser->lexer);
12100 /* See if it's a cv-qualifier. */
12101 switch (token->keyword)
12102 {
12103 case RID_CONST:
12104 cv_qualifier = TYPE_QUAL_CONST;
12105 break;
12106
12107 case RID_VOLATILE:
12108 cv_qualifier = TYPE_QUAL_VOLATILE;
12109 break;
12110
12111 case RID_RESTRICT:
12112 cv_qualifier = TYPE_QUAL_RESTRICT;
12113 break;
12114
12115 default:
12116 cv_qualifier = TYPE_UNQUALIFIED;
12117 break;
12118 }
12119
12120 if (!cv_qualifier)
12121 break;
12122
12123 if (cv_quals & cv_qualifier)
12124 {
12125 error ("duplicate cv-qualifier");
12126 cp_lexer_purge_token (parser->lexer);
12127 }
12128 else
12129 {
12130 cp_lexer_consume_token (parser->lexer);
12131 cv_quals |= cv_qualifier;
12132 }
12133 }
12134
12135 return cv_quals;
12136 }
12137
12138 /* Parse a declarator-id.
12139
12140 declarator-id:
12141 id-expression
12142 :: [opt] nested-name-specifier [opt] type-name
12143
12144 In the `id-expression' case, the value returned is as for
12145 cp_parser_id_expression if the id-expression was an unqualified-id.
12146 If the id-expression was a qualified-id, then a SCOPE_REF is
12147 returned. The first operand is the scope (either a NAMESPACE_DECL
12148 or TREE_TYPE), but the second is still just a representation of an
12149 unqualified-id. */
12150
12151 static tree
12152 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12153 {
12154 tree id;
12155 /* The expression must be an id-expression. Assume that qualified
12156 names are the names of types so that:
12157
12158 template <class T>
12159 int S<T>::R::i = 3;
12160
12161 will work; we must treat `S<T>::R' as the name of a type.
12162 Similarly, assume that qualified names are templates, where
12163 required, so that:
12164
12165 template <class T>
12166 int S<T>::R<T>::i = 3;
12167
12168 will work, too. */
12169 id = cp_parser_id_expression (parser,
12170 /*template_keyword_p=*/false,
12171 /*check_dependency_p=*/false,
12172 /*template_p=*/NULL,
12173 /*declarator_p=*/true,
12174 optional_p);
12175 if (id && BASELINK_P (id))
12176 id = BASELINK_FUNCTIONS (id);
12177 return id;
12178 }
12179
12180 /* Parse a type-id.
12181
12182 type-id:
12183 type-specifier-seq abstract-declarator [opt]
12184
12185 Returns the TYPE specified. */
12186
12187 static tree
12188 cp_parser_type_id (cp_parser* parser)
12189 {
12190 cp_decl_specifier_seq type_specifier_seq;
12191 cp_declarator *abstract_declarator;
12192
12193 /* Parse the type-specifier-seq. */
12194 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12195 &type_specifier_seq);
12196 if (type_specifier_seq.type == error_mark_node)
12197 return error_mark_node;
12198
12199 /* There might or might not be an abstract declarator. */
12200 cp_parser_parse_tentatively (parser);
12201 /* Look for the declarator. */
12202 abstract_declarator
12203 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12204 /*parenthesized_p=*/NULL,
12205 /*member_p=*/false);
12206 /* Check to see if there really was a declarator. */
12207 if (!cp_parser_parse_definitely (parser))
12208 abstract_declarator = NULL;
12209
12210 return groktypename (&type_specifier_seq, abstract_declarator);
12211 }
12212
12213 /* Parse a type-specifier-seq.
12214
12215 type-specifier-seq:
12216 type-specifier type-specifier-seq [opt]
12217
12218 GNU extension:
12219
12220 type-specifier-seq:
12221 attributes type-specifier-seq [opt]
12222
12223 If IS_CONDITION is true, we are at the start of a "condition",
12224 e.g., we've just seen "if (".
12225
12226 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12227
12228 static void
12229 cp_parser_type_specifier_seq (cp_parser* parser,
12230 bool is_condition,
12231 cp_decl_specifier_seq *type_specifier_seq)
12232 {
12233 bool seen_type_specifier = false;
12234 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12235
12236 /* Clear the TYPE_SPECIFIER_SEQ. */
12237 clear_decl_specs (type_specifier_seq);
12238
12239 /* Parse the type-specifiers and attributes. */
12240 while (true)
12241 {
12242 tree type_specifier;
12243 bool is_cv_qualifier;
12244
12245 /* Check for attributes first. */
12246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12247 {
12248 type_specifier_seq->attributes =
12249 chainon (type_specifier_seq->attributes,
12250 cp_parser_attributes_opt (parser));
12251 continue;
12252 }
12253
12254 /* Look for the type-specifier. */
12255 type_specifier = cp_parser_type_specifier (parser,
12256 flags,
12257 type_specifier_seq,
12258 /*is_declaration=*/false,
12259 NULL,
12260 &is_cv_qualifier);
12261 if (!type_specifier)
12262 {
12263 /* If the first type-specifier could not be found, this is not a
12264 type-specifier-seq at all. */
12265 if (!seen_type_specifier)
12266 {
12267 cp_parser_error (parser, "expected type-specifier");
12268 type_specifier_seq->type = error_mark_node;
12269 return;
12270 }
12271 /* If subsequent type-specifiers could not be found, the
12272 type-specifier-seq is complete. */
12273 break;
12274 }
12275
12276 seen_type_specifier = true;
12277 /* The standard says that a condition can be:
12278
12279 type-specifier-seq declarator = assignment-expression
12280
12281 However, given:
12282
12283 struct S {};
12284 if (int S = ...)
12285
12286 we should treat the "S" as a declarator, not as a
12287 type-specifier. The standard doesn't say that explicitly for
12288 type-specifier-seq, but it does say that for
12289 decl-specifier-seq in an ordinary declaration. Perhaps it
12290 would be clearer just to allow a decl-specifier-seq here, and
12291 then add a semantic restriction that if any decl-specifiers
12292 that are not type-specifiers appear, the program is invalid. */
12293 if (is_condition && !is_cv_qualifier)
12294 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12295 }
12296
12297 cp_parser_check_decl_spec (type_specifier_seq);
12298 }
12299
12300 /* Parse a parameter-declaration-clause.
12301
12302 parameter-declaration-clause:
12303 parameter-declaration-list [opt] ... [opt]
12304 parameter-declaration-list , ...
12305
12306 Returns a representation for the parameter declarations. A return
12307 value of NULL indicates a parameter-declaration-clause consisting
12308 only of an ellipsis. */
12309
12310 static cp_parameter_declarator *
12311 cp_parser_parameter_declaration_clause (cp_parser* parser)
12312 {
12313 cp_parameter_declarator *parameters;
12314 cp_token *token;
12315 bool ellipsis_p;
12316 bool is_error;
12317
12318 /* Peek at the next token. */
12319 token = cp_lexer_peek_token (parser->lexer);
12320 /* Check for trivial parameter-declaration-clauses. */
12321 if (token->type == CPP_ELLIPSIS)
12322 {
12323 /* Consume the `...' token. */
12324 cp_lexer_consume_token (parser->lexer);
12325 return NULL;
12326 }
12327 else if (token->type == CPP_CLOSE_PAREN)
12328 /* There are no parameters. */
12329 {
12330 #ifndef NO_IMPLICIT_EXTERN_C
12331 if (in_system_header && current_class_type == NULL
12332 && current_lang_name == lang_name_c)
12333 return NULL;
12334 else
12335 #endif
12336 return no_parameters;
12337 }
12338 /* Check for `(void)', too, which is a special case. */
12339 else if (token->keyword == RID_VOID
12340 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12341 == CPP_CLOSE_PAREN))
12342 {
12343 /* Consume the `void' token. */
12344 cp_lexer_consume_token (parser->lexer);
12345 /* There are no parameters. */
12346 return no_parameters;
12347 }
12348
12349 /* Parse the parameter-declaration-list. */
12350 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12351 /* If a parse error occurred while parsing the
12352 parameter-declaration-list, then the entire
12353 parameter-declaration-clause is erroneous. */
12354 if (is_error)
12355 return NULL;
12356
12357 /* Peek at the next token. */
12358 token = cp_lexer_peek_token (parser->lexer);
12359 /* If it's a `,', the clause should terminate with an ellipsis. */
12360 if (token->type == CPP_COMMA)
12361 {
12362 /* Consume the `,'. */
12363 cp_lexer_consume_token (parser->lexer);
12364 /* Expect an ellipsis. */
12365 ellipsis_p
12366 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12367 }
12368 /* It might also be `...' if the optional trailing `,' was
12369 omitted. */
12370 else if (token->type == CPP_ELLIPSIS)
12371 {
12372 /* Consume the `...' token. */
12373 cp_lexer_consume_token (parser->lexer);
12374 /* And remember that we saw it. */
12375 ellipsis_p = true;
12376 }
12377 else
12378 ellipsis_p = false;
12379
12380 /* Finish the parameter list. */
12381 if (parameters && ellipsis_p)
12382 parameters->ellipsis_p = true;
12383
12384 return parameters;
12385 }
12386
12387 /* Parse a parameter-declaration-list.
12388
12389 parameter-declaration-list:
12390 parameter-declaration
12391 parameter-declaration-list , parameter-declaration
12392
12393 Returns a representation of the parameter-declaration-list, as for
12394 cp_parser_parameter_declaration_clause. However, the
12395 `void_list_node' is never appended to the list. Upon return,
12396 *IS_ERROR will be true iff an error occurred. */
12397
12398 static cp_parameter_declarator *
12399 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12400 {
12401 cp_parameter_declarator *parameters = NULL;
12402 cp_parameter_declarator **tail = &parameters;
12403 bool saved_in_unbraced_linkage_specification_p;
12404
12405 /* Assume all will go well. */
12406 *is_error = false;
12407 /* The special considerations that apply to a function within an
12408 unbraced linkage specifications do not apply to the parameters
12409 to the function. */
12410 saved_in_unbraced_linkage_specification_p
12411 = parser->in_unbraced_linkage_specification_p;
12412 parser->in_unbraced_linkage_specification_p = false;
12413
12414 /* Look for more parameters. */
12415 while (true)
12416 {
12417 cp_parameter_declarator *parameter;
12418 bool parenthesized_p;
12419 /* Parse the parameter. */
12420 parameter
12421 = cp_parser_parameter_declaration (parser,
12422 /*template_parm_p=*/false,
12423 &parenthesized_p);
12424
12425 /* If a parse error occurred parsing the parameter declaration,
12426 then the entire parameter-declaration-list is erroneous. */
12427 if (!parameter)
12428 {
12429 *is_error = true;
12430 parameters = NULL;
12431 break;
12432 }
12433 /* Add the new parameter to the list. */
12434 *tail = parameter;
12435 tail = &parameter->next;
12436
12437 /* Peek at the next token. */
12438 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12439 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12440 /* These are for Objective-C++ */
12441 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12442 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12443 /* The parameter-declaration-list is complete. */
12444 break;
12445 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12446 {
12447 cp_token *token;
12448
12449 /* Peek at the next token. */
12450 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12451 /* If it's an ellipsis, then the list is complete. */
12452 if (token->type == CPP_ELLIPSIS)
12453 break;
12454 /* Otherwise, there must be more parameters. Consume the
12455 `,'. */
12456 cp_lexer_consume_token (parser->lexer);
12457 /* When parsing something like:
12458
12459 int i(float f, double d)
12460
12461 we can tell after seeing the declaration for "f" that we
12462 are not looking at an initialization of a variable "i",
12463 but rather at the declaration of a function "i".
12464
12465 Due to the fact that the parsing of template arguments
12466 (as specified to a template-id) requires backtracking we
12467 cannot use this technique when inside a template argument
12468 list. */
12469 if (!parser->in_template_argument_list_p
12470 && !parser->in_type_id_in_expr_p
12471 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12472 /* However, a parameter-declaration of the form
12473 "foat(f)" (which is a valid declaration of a
12474 parameter "f") can also be interpreted as an
12475 expression (the conversion of "f" to "float"). */
12476 && !parenthesized_p)
12477 cp_parser_commit_to_tentative_parse (parser);
12478 }
12479 else
12480 {
12481 cp_parser_error (parser, "expected %<,%> or %<...%>");
12482 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12483 cp_parser_skip_to_closing_parenthesis (parser,
12484 /*recovering=*/true,
12485 /*or_comma=*/false,
12486 /*consume_paren=*/false);
12487 break;
12488 }
12489 }
12490
12491 parser->in_unbraced_linkage_specification_p
12492 = saved_in_unbraced_linkage_specification_p;
12493
12494 return parameters;
12495 }
12496
12497 /* Parse a parameter declaration.
12498
12499 parameter-declaration:
12500 decl-specifier-seq declarator
12501 decl-specifier-seq declarator = assignment-expression
12502 decl-specifier-seq abstract-declarator [opt]
12503 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12504
12505 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12506 declares a template parameter. (In that case, a non-nested `>'
12507 token encountered during the parsing of the assignment-expression
12508 is not interpreted as a greater-than operator.)
12509
12510 Returns a representation of the parameter, or NULL if an error
12511 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12512 true iff the declarator is of the form "(p)". */
12513
12514 static cp_parameter_declarator *
12515 cp_parser_parameter_declaration (cp_parser *parser,
12516 bool template_parm_p,
12517 bool *parenthesized_p)
12518 {
12519 int declares_class_or_enum;
12520 bool greater_than_is_operator_p;
12521 cp_decl_specifier_seq decl_specifiers;
12522 cp_declarator *declarator;
12523 tree default_argument;
12524 cp_token *token;
12525 const char *saved_message;
12526
12527 /* In a template parameter, `>' is not an operator.
12528
12529 [temp.param]
12530
12531 When parsing a default template-argument for a non-type
12532 template-parameter, the first non-nested `>' is taken as the end
12533 of the template parameter-list rather than a greater-than
12534 operator. */
12535 greater_than_is_operator_p = !template_parm_p;
12536
12537 /* Type definitions may not appear in parameter types. */
12538 saved_message = parser->type_definition_forbidden_message;
12539 parser->type_definition_forbidden_message
12540 = "types may not be defined in parameter types";
12541
12542 /* Parse the declaration-specifiers. */
12543 cp_parser_decl_specifier_seq (parser,
12544 CP_PARSER_FLAGS_NONE,
12545 &decl_specifiers,
12546 &declares_class_or_enum);
12547 /* If an error occurred, there's no reason to attempt to parse the
12548 rest of the declaration. */
12549 if (cp_parser_error_occurred (parser))
12550 {
12551 parser->type_definition_forbidden_message = saved_message;
12552 return NULL;
12553 }
12554
12555 /* Peek at the next token. */
12556 token = cp_lexer_peek_token (parser->lexer);
12557 /* If the next token is a `)', `,', `=', `>', or `...', then there
12558 is no declarator. */
12559 if (token->type == CPP_CLOSE_PAREN
12560 || token->type == CPP_COMMA
12561 || token->type == CPP_EQ
12562 || token->type == CPP_ELLIPSIS
12563 || token->type == CPP_GREATER)
12564 {
12565 declarator = NULL;
12566 if (parenthesized_p)
12567 *parenthesized_p = false;
12568 }
12569 /* Otherwise, there should be a declarator. */
12570 else
12571 {
12572 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12573 parser->default_arg_ok_p = false;
12574
12575 /* After seeing a decl-specifier-seq, if the next token is not a
12576 "(", there is no possibility that the code is a valid
12577 expression. Therefore, if parsing tentatively, we commit at
12578 this point. */
12579 if (!parser->in_template_argument_list_p
12580 /* In an expression context, having seen:
12581
12582 (int((char ...
12583
12584 we cannot be sure whether we are looking at a
12585 function-type (taking a "char" as a parameter) or a cast
12586 of some object of type "char" to "int". */
12587 && !parser->in_type_id_in_expr_p
12588 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12589 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12590 cp_parser_commit_to_tentative_parse (parser);
12591 /* Parse the declarator. */
12592 declarator = cp_parser_declarator (parser,
12593 CP_PARSER_DECLARATOR_EITHER,
12594 /*ctor_dtor_or_conv_p=*/NULL,
12595 parenthesized_p,
12596 /*member_p=*/false);
12597 parser->default_arg_ok_p = saved_default_arg_ok_p;
12598 /* After the declarator, allow more attributes. */
12599 decl_specifiers.attributes
12600 = chainon (decl_specifiers.attributes,
12601 cp_parser_attributes_opt (parser));
12602 }
12603
12604 /* The restriction on defining new types applies only to the type
12605 of the parameter, not to the default argument. */
12606 parser->type_definition_forbidden_message = saved_message;
12607
12608 /* If the next token is `=', then process a default argument. */
12609 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12610 {
12611 bool saved_greater_than_is_operator_p;
12612 /* Consume the `='. */
12613 cp_lexer_consume_token (parser->lexer);
12614
12615 /* If we are defining a class, then the tokens that make up the
12616 default argument must be saved and processed later. */
12617 if (!template_parm_p && at_class_scope_p ()
12618 && TYPE_BEING_DEFINED (current_class_type))
12619 {
12620 unsigned depth = 0;
12621 cp_token *first_token;
12622 cp_token *token;
12623
12624 /* Add tokens until we have processed the entire default
12625 argument. We add the range [first_token, token). */
12626 first_token = cp_lexer_peek_token (parser->lexer);
12627 while (true)
12628 {
12629 bool done = false;
12630
12631 /* Peek at the next token. */
12632 token = cp_lexer_peek_token (parser->lexer);
12633 /* What we do depends on what token we have. */
12634 switch (token->type)
12635 {
12636 /* In valid code, a default argument must be
12637 immediately followed by a `,' `)', or `...'. */
12638 case CPP_COMMA:
12639 case CPP_CLOSE_PAREN:
12640 case CPP_ELLIPSIS:
12641 /* If we run into a non-nested `;', `}', or `]',
12642 then the code is invalid -- but the default
12643 argument is certainly over. */
12644 case CPP_SEMICOLON:
12645 case CPP_CLOSE_BRACE:
12646 case CPP_CLOSE_SQUARE:
12647 if (depth == 0)
12648 done = true;
12649 /* Update DEPTH, if necessary. */
12650 else if (token->type == CPP_CLOSE_PAREN
12651 || token->type == CPP_CLOSE_BRACE
12652 || token->type == CPP_CLOSE_SQUARE)
12653 --depth;
12654 break;
12655
12656 case CPP_OPEN_PAREN:
12657 case CPP_OPEN_SQUARE:
12658 case CPP_OPEN_BRACE:
12659 ++depth;
12660 break;
12661
12662 case CPP_GREATER:
12663 /* If we see a non-nested `>', and `>' is not an
12664 operator, then it marks the end of the default
12665 argument. */
12666 if (!depth && !greater_than_is_operator_p)
12667 done = true;
12668 break;
12669
12670 /* If we run out of tokens, issue an error message. */
12671 case CPP_EOF:
12672 case CPP_PRAGMA_EOL:
12673 error ("file ends in default argument");
12674 done = true;
12675 break;
12676
12677 case CPP_NAME:
12678 case CPP_SCOPE:
12679 /* In these cases, we should look for template-ids.
12680 For example, if the default argument is
12681 `X<int, double>()', we need to do name lookup to
12682 figure out whether or not `X' is a template; if
12683 so, the `,' does not end the default argument.
12684
12685 That is not yet done. */
12686 break;
12687
12688 default:
12689 break;
12690 }
12691
12692 /* If we've reached the end, stop. */
12693 if (done)
12694 break;
12695
12696 /* Add the token to the token block. */
12697 token = cp_lexer_consume_token (parser->lexer);
12698 }
12699
12700 /* Create a DEFAULT_ARG to represented the unparsed default
12701 argument. */
12702 default_argument = make_node (DEFAULT_ARG);
12703 DEFARG_TOKENS (default_argument)
12704 = cp_token_cache_new (first_token, token);
12705 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12706 }
12707 /* Outside of a class definition, we can just parse the
12708 assignment-expression. */
12709 else
12710 {
12711 bool saved_local_variables_forbidden_p;
12712
12713 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12714 set correctly. */
12715 saved_greater_than_is_operator_p
12716 = parser->greater_than_is_operator_p;
12717 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12718 /* Local variable names (and the `this' keyword) may not
12719 appear in a default argument. */
12720 saved_local_variables_forbidden_p
12721 = parser->local_variables_forbidden_p;
12722 parser->local_variables_forbidden_p = true;
12723 /* The default argument expression may cause implicitly
12724 defined member functions to be synthesized, which will
12725 result in garbage collection. We must treat this
12726 situation as if we were within the body of function so as
12727 to avoid collecting live data on the stack. */
12728 ++function_depth;
12729 /* Parse the assignment-expression. */
12730 if (template_parm_p)
12731 push_deferring_access_checks (dk_no_deferred);
12732 default_argument
12733 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12734 if (template_parm_p)
12735 pop_deferring_access_checks ();
12736 /* Restore saved state. */
12737 --function_depth;
12738 parser->greater_than_is_operator_p
12739 = saved_greater_than_is_operator_p;
12740 parser->local_variables_forbidden_p
12741 = saved_local_variables_forbidden_p;
12742 }
12743 if (!parser->default_arg_ok_p)
12744 {
12745 if (!flag_pedantic_errors)
12746 warning (0, "deprecated use of default argument for parameter of non-function");
12747 else
12748 {
12749 error ("default arguments are only permitted for function parameters");
12750 default_argument = NULL_TREE;
12751 }
12752 }
12753 }
12754 else
12755 default_argument = NULL_TREE;
12756
12757 return make_parameter_declarator (&decl_specifiers,
12758 declarator,
12759 default_argument);
12760 }
12761
12762 /* Parse a function-body.
12763
12764 function-body:
12765 compound_statement */
12766
12767 static void
12768 cp_parser_function_body (cp_parser *parser)
12769 {
12770 cp_parser_compound_statement (parser, NULL, false);
12771 }
12772
12773 /* Parse a ctor-initializer-opt followed by a function-body. Return
12774 true if a ctor-initializer was present. */
12775
12776 static bool
12777 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12778 {
12779 tree body;
12780 bool ctor_initializer_p;
12781
12782 /* Begin the function body. */
12783 body = begin_function_body ();
12784 /* Parse the optional ctor-initializer. */
12785 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12786 /* Parse the function-body. */
12787 cp_parser_function_body (parser);
12788 /* Finish the function body. */
12789 finish_function_body (body);
12790
12791 return ctor_initializer_p;
12792 }
12793
12794 /* Parse an initializer.
12795
12796 initializer:
12797 = initializer-clause
12798 ( expression-list )
12799
12800 Returns an expression representing the initializer. If no
12801 initializer is present, NULL_TREE is returned.
12802
12803 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12804 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12805 set to FALSE if there is no initializer present. If there is an
12806 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12807 is set to true; otherwise it is set to false. */
12808
12809 static tree
12810 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12811 bool* non_constant_p)
12812 {
12813 cp_token *token;
12814 tree init;
12815
12816 /* Peek at the next token. */
12817 token = cp_lexer_peek_token (parser->lexer);
12818
12819 /* Let our caller know whether or not this initializer was
12820 parenthesized. */
12821 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12822 /* Assume that the initializer is constant. */
12823 *non_constant_p = false;
12824
12825 if (token->type == CPP_EQ)
12826 {
12827 /* Consume the `='. */
12828 cp_lexer_consume_token (parser->lexer);
12829 /* Parse the initializer-clause. */
12830 init = cp_parser_initializer_clause (parser, non_constant_p);
12831 }
12832 else if (token->type == CPP_OPEN_PAREN)
12833 init = cp_parser_parenthesized_expression_list (parser, false,
12834 /*cast_p=*/false,
12835 non_constant_p);
12836 else
12837 {
12838 /* Anything else is an error. */
12839 cp_parser_error (parser, "expected initializer");
12840 init = error_mark_node;
12841 }
12842
12843 return init;
12844 }
12845
12846 /* Parse an initializer-clause.
12847
12848 initializer-clause:
12849 assignment-expression
12850 { initializer-list , [opt] }
12851 { }
12852
12853 Returns an expression representing the initializer.
12854
12855 If the `assignment-expression' production is used the value
12856 returned is simply a representation for the expression.
12857
12858 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12859 the elements of the initializer-list (or NULL, if the last
12860 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12861 NULL_TREE. There is no way to detect whether or not the optional
12862 trailing `,' was provided. NON_CONSTANT_P is as for
12863 cp_parser_initializer. */
12864
12865 static tree
12866 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12867 {
12868 tree initializer;
12869
12870 /* Assume the expression is constant. */
12871 *non_constant_p = false;
12872
12873 /* If it is not a `{', then we are looking at an
12874 assignment-expression. */
12875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12876 {
12877 initializer
12878 = cp_parser_constant_expression (parser,
12879 /*allow_non_constant_p=*/true,
12880 non_constant_p);
12881 if (!*non_constant_p)
12882 initializer = fold_non_dependent_expr (initializer);
12883 }
12884 else
12885 {
12886 /* Consume the `{' token. */
12887 cp_lexer_consume_token (parser->lexer);
12888 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12889 initializer = make_node (CONSTRUCTOR);
12890 /* If it's not a `}', then there is a non-trivial initializer. */
12891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12892 {
12893 /* Parse the initializer list. */
12894 CONSTRUCTOR_ELTS (initializer)
12895 = cp_parser_initializer_list (parser, non_constant_p);
12896 /* A trailing `,' token is allowed. */
12897 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12898 cp_lexer_consume_token (parser->lexer);
12899 }
12900 /* Now, there should be a trailing `}'. */
12901 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12902 }
12903
12904 return initializer;
12905 }
12906
12907 /* Parse an initializer-list.
12908
12909 initializer-list:
12910 initializer-clause
12911 initializer-list , initializer-clause
12912
12913 GNU Extension:
12914
12915 initializer-list:
12916 identifier : initializer-clause
12917 initializer-list, identifier : initializer-clause
12918
12919 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12920 for the initializer. If the INDEX of the elt is non-NULL, it is the
12921 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12922 as for cp_parser_initializer. */
12923
12924 static VEC(constructor_elt,gc) *
12925 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12926 {
12927 VEC(constructor_elt,gc) *v = NULL;
12928
12929 /* Assume all of the expressions are constant. */
12930 *non_constant_p = false;
12931
12932 /* Parse the rest of the list. */
12933 while (true)
12934 {
12935 cp_token *token;
12936 tree identifier;
12937 tree initializer;
12938 bool clause_non_constant_p;
12939
12940 /* If the next token is an identifier and the following one is a
12941 colon, we are looking at the GNU designated-initializer
12942 syntax. */
12943 if (cp_parser_allow_gnu_extensions_p (parser)
12944 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12945 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12946 {
12947 /* Warn the user that they are using an extension. */
12948 if (pedantic)
12949 pedwarn ("ISO C++ does not allow designated initializers");
12950 /* Consume the identifier. */
12951 identifier = cp_lexer_consume_token (parser->lexer)->value;
12952 /* Consume the `:'. */
12953 cp_lexer_consume_token (parser->lexer);
12954 }
12955 else
12956 identifier = NULL_TREE;
12957
12958 /* Parse the initializer. */
12959 initializer = cp_parser_initializer_clause (parser,
12960 &clause_non_constant_p);
12961 /* If any clause is non-constant, so is the entire initializer. */
12962 if (clause_non_constant_p)
12963 *non_constant_p = true;
12964
12965 /* Add it to the vector. */
12966 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12967
12968 /* If the next token is not a comma, we have reached the end of
12969 the list. */
12970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12971 break;
12972
12973 /* Peek at the next token. */
12974 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12975 /* If the next token is a `}', then we're still done. An
12976 initializer-clause can have a trailing `,' after the
12977 initializer-list and before the closing `}'. */
12978 if (token->type == CPP_CLOSE_BRACE)
12979 break;
12980
12981 /* Consume the `,' token. */
12982 cp_lexer_consume_token (parser->lexer);
12983 }
12984
12985 return v;
12986 }
12987
12988 /* Classes [gram.class] */
12989
12990 /* Parse a class-name.
12991
12992 class-name:
12993 identifier
12994 template-id
12995
12996 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12997 to indicate that names looked up in dependent types should be
12998 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12999 keyword has been used to indicate that the name that appears next
13000 is a template. TAG_TYPE indicates the explicit tag given before
13001 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13002 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13003 is the class being defined in a class-head.
13004
13005 Returns the TYPE_DECL representing the class. */
13006
13007 static tree
13008 cp_parser_class_name (cp_parser *parser,
13009 bool typename_keyword_p,
13010 bool template_keyword_p,
13011 enum tag_types tag_type,
13012 bool check_dependency_p,
13013 bool class_head_p,
13014 bool is_declaration)
13015 {
13016 tree decl;
13017 tree scope;
13018 bool typename_p;
13019 cp_token *token;
13020
13021 /* All class-names start with an identifier. */
13022 token = cp_lexer_peek_token (parser->lexer);
13023 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13024 {
13025 cp_parser_error (parser, "expected class-name");
13026 return error_mark_node;
13027 }
13028
13029 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13030 to a template-id, so we save it here. */
13031 scope = parser->scope;
13032 if (scope == error_mark_node)
13033 return error_mark_node;
13034
13035 /* Any name names a type if we're following the `typename' keyword
13036 in a qualified name where the enclosing scope is type-dependent. */
13037 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13038 && dependent_type_p (scope));
13039 /* Handle the common case (an identifier, but not a template-id)
13040 efficiently. */
13041 if (token->type == CPP_NAME
13042 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13043 {
13044 cp_token *identifier_token;
13045 tree identifier;
13046 bool ambiguous_p;
13047
13048 /* Look for the identifier. */
13049 identifier_token = cp_lexer_peek_token (parser->lexer);
13050 ambiguous_p = identifier_token->ambiguous_p;
13051 identifier = cp_parser_identifier (parser);
13052 /* If the next token isn't an identifier, we are certainly not
13053 looking at a class-name. */
13054 if (identifier == error_mark_node)
13055 decl = error_mark_node;
13056 /* If we know this is a type-name, there's no need to look it
13057 up. */
13058 else if (typename_p)
13059 decl = identifier;
13060 else
13061 {
13062 tree ambiguous_decls;
13063 /* If we already know that this lookup is ambiguous, then
13064 we've already issued an error message; there's no reason
13065 to check again. */
13066 if (ambiguous_p)
13067 {
13068 cp_parser_simulate_error (parser);
13069 return error_mark_node;
13070 }
13071 /* If the next token is a `::', then the name must be a type
13072 name.
13073
13074 [basic.lookup.qual]
13075
13076 During the lookup for a name preceding the :: scope
13077 resolution operator, object, function, and enumerator
13078 names are ignored. */
13079 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13080 tag_type = typename_type;
13081 /* Look up the name. */
13082 decl = cp_parser_lookup_name (parser, identifier,
13083 tag_type,
13084 /*is_template=*/false,
13085 /*is_namespace=*/false,
13086 check_dependency_p,
13087 &ambiguous_decls);
13088 if (ambiguous_decls)
13089 {
13090 error ("reference to %qD is ambiguous", identifier);
13091 print_candidates (ambiguous_decls);
13092 if (cp_parser_parsing_tentatively (parser))
13093 {
13094 identifier_token->ambiguous_p = true;
13095 cp_parser_simulate_error (parser);
13096 }
13097 return error_mark_node;
13098 }
13099 }
13100 }
13101 else
13102 {
13103 /* Try a template-id. */
13104 decl = cp_parser_template_id (parser, template_keyword_p,
13105 check_dependency_p,
13106 is_declaration);
13107 if (decl == error_mark_node)
13108 return error_mark_node;
13109 }
13110
13111 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13112
13113 /* If this is a typename, create a TYPENAME_TYPE. */
13114 if (typename_p && decl != error_mark_node)
13115 {
13116 decl = make_typename_type (scope, decl, typename_type,
13117 /*complain=*/tf_error);
13118 if (decl != error_mark_node)
13119 decl = TYPE_NAME (decl);
13120 }
13121
13122 /* Check to see that it is really the name of a class. */
13123 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13124 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13125 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13126 /* Situations like this:
13127
13128 template <typename T> struct A {
13129 typename T::template X<int>::I i;
13130 };
13131
13132 are problematic. Is `T::template X<int>' a class-name? The
13133 standard does not seem to be definitive, but there is no other
13134 valid interpretation of the following `::'. Therefore, those
13135 names are considered class-names. */
13136 {
13137 decl = make_typename_type (scope, decl, tag_type, tf_error);
13138 if (decl != error_mark_node)
13139 decl = TYPE_NAME (decl);
13140 }
13141 else if (TREE_CODE (decl) != TYPE_DECL
13142 || TREE_TYPE (decl) == error_mark_node
13143 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13144 decl = error_mark_node;
13145
13146 if (decl == error_mark_node)
13147 cp_parser_error (parser, "expected class-name");
13148
13149 return decl;
13150 }
13151
13152 /* Parse a class-specifier.
13153
13154 class-specifier:
13155 class-head { member-specification [opt] }
13156
13157 Returns the TREE_TYPE representing the class. */
13158
13159 static tree
13160 cp_parser_class_specifier (cp_parser* parser)
13161 {
13162 cp_token *token;
13163 tree type;
13164 tree attributes = NULL_TREE;
13165 int has_trailing_semicolon;
13166 bool nested_name_specifier_p;
13167 unsigned saved_num_template_parameter_lists;
13168 bool saved_in_function_body;
13169 tree old_scope = NULL_TREE;
13170 tree scope = NULL_TREE;
13171 tree bases;
13172
13173 push_deferring_access_checks (dk_no_deferred);
13174
13175 /* Parse the class-head. */
13176 type = cp_parser_class_head (parser,
13177 &nested_name_specifier_p,
13178 &attributes,
13179 &bases);
13180 /* If the class-head was a semantic disaster, skip the entire body
13181 of the class. */
13182 if (!type)
13183 {
13184 cp_parser_skip_to_end_of_block_or_statement (parser);
13185 pop_deferring_access_checks ();
13186 return error_mark_node;
13187 }
13188
13189 /* Look for the `{'. */
13190 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13191 {
13192 pop_deferring_access_checks ();
13193 return error_mark_node;
13194 }
13195
13196 /* Process the base classes. If they're invalid, skip the
13197 entire class body. */
13198 if (!xref_basetypes (type, bases))
13199 {
13200 cp_parser_skip_to_closing_brace (parser);
13201
13202 /* Consuming the closing brace yields better error messages
13203 later on. */
13204 cp_lexer_consume_token (parser->lexer);
13205 pop_deferring_access_checks ();
13206 return error_mark_node;
13207 }
13208
13209 /* Issue an error message if type-definitions are forbidden here. */
13210 cp_parser_check_type_definition (parser);
13211 /* Remember that we are defining one more class. */
13212 ++parser->num_classes_being_defined;
13213 /* Inside the class, surrounding template-parameter-lists do not
13214 apply. */
13215 saved_num_template_parameter_lists
13216 = parser->num_template_parameter_lists;
13217 parser->num_template_parameter_lists = 0;
13218 /* We are not in a function body. */
13219 saved_in_function_body = parser->in_function_body;
13220 parser->in_function_body = false;
13221
13222 /* Start the class. */
13223 if (nested_name_specifier_p)
13224 {
13225 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13226 old_scope = push_inner_scope (scope);
13227 }
13228 type = begin_class_definition (type, attributes);
13229
13230 if (type == error_mark_node)
13231 /* If the type is erroneous, skip the entire body of the class. */
13232 cp_parser_skip_to_closing_brace (parser);
13233 else
13234 /* Parse the member-specification. */
13235 cp_parser_member_specification_opt (parser);
13236
13237 /* Look for the trailing `}'. */
13238 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13239 /* We get better error messages by noticing a common problem: a
13240 missing trailing `;'. */
13241 token = cp_lexer_peek_token (parser->lexer);
13242 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13243 /* Look for trailing attributes to apply to this class. */
13244 if (cp_parser_allow_gnu_extensions_p (parser))
13245 attributes = cp_parser_attributes_opt (parser);
13246 if (type != error_mark_node)
13247 type = finish_struct (type, attributes);
13248 if (nested_name_specifier_p)
13249 pop_inner_scope (old_scope, scope);
13250 /* If this class is not itself within the scope of another class,
13251 then we need to parse the bodies of all of the queued function
13252 definitions. Note that the queued functions defined in a class
13253 are not always processed immediately following the
13254 class-specifier for that class. Consider:
13255
13256 struct A {
13257 struct B { void f() { sizeof (A); } };
13258 };
13259
13260 If `f' were processed before the processing of `A' were
13261 completed, there would be no way to compute the size of `A'.
13262 Note that the nesting we are interested in here is lexical --
13263 not the semantic nesting given by TYPE_CONTEXT. In particular,
13264 for:
13265
13266 struct A { struct B; };
13267 struct A::B { void f() { } };
13268
13269 there is no need to delay the parsing of `A::B::f'. */
13270 if (--parser->num_classes_being_defined == 0)
13271 {
13272 tree queue_entry;
13273 tree fn;
13274 tree class_type = NULL_TREE;
13275 tree pushed_scope = NULL_TREE;
13276
13277 /* In a first pass, parse default arguments to the functions.
13278 Then, in a second pass, parse the bodies of the functions.
13279 This two-phased approach handles cases like:
13280
13281 struct S {
13282 void f() { g(); }
13283 void g(int i = 3);
13284 };
13285
13286 */
13287 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13288 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13289 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13290 TREE_PURPOSE (parser->unparsed_functions_queues)
13291 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13292 {
13293 fn = TREE_VALUE (queue_entry);
13294 /* If there are default arguments that have not yet been processed,
13295 take care of them now. */
13296 if (class_type != TREE_PURPOSE (queue_entry))
13297 {
13298 if (pushed_scope)
13299 pop_scope (pushed_scope);
13300 class_type = TREE_PURPOSE (queue_entry);
13301 pushed_scope = push_scope (class_type);
13302 }
13303 /* Make sure that any template parameters are in scope. */
13304 maybe_begin_member_template_processing (fn);
13305 /* Parse the default argument expressions. */
13306 cp_parser_late_parsing_default_args (parser, fn);
13307 /* Remove any template parameters from the symbol table. */
13308 maybe_end_member_template_processing ();
13309 }
13310 if (pushed_scope)
13311 pop_scope (pushed_scope);
13312 /* Now parse the body of the functions. */
13313 for (TREE_VALUE (parser->unparsed_functions_queues)
13314 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13315 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13316 TREE_VALUE (parser->unparsed_functions_queues)
13317 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13318 {
13319 /* Figure out which function we need to process. */
13320 fn = TREE_VALUE (queue_entry);
13321 /* Parse the function. */
13322 cp_parser_late_parsing_for_member (parser, fn);
13323 }
13324 }
13325
13326 /* Put back any saved access checks. */
13327 pop_deferring_access_checks ();
13328
13329 /* Restore saved state. */
13330 parser->in_function_body = saved_in_function_body;
13331 parser->num_template_parameter_lists
13332 = saved_num_template_parameter_lists;
13333
13334 return type;
13335 }
13336
13337 /* Parse a class-head.
13338
13339 class-head:
13340 class-key identifier [opt] base-clause [opt]
13341 class-key nested-name-specifier identifier base-clause [opt]
13342 class-key nested-name-specifier [opt] template-id
13343 base-clause [opt]
13344
13345 GNU Extensions:
13346 class-key attributes identifier [opt] base-clause [opt]
13347 class-key attributes nested-name-specifier identifier base-clause [opt]
13348 class-key attributes nested-name-specifier [opt] template-id
13349 base-clause [opt]
13350
13351 Returns the TYPE of the indicated class. Sets
13352 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13353 involving a nested-name-specifier was used, and FALSE otherwise.
13354
13355 Returns error_mark_node if this is not a class-head.
13356
13357 Returns NULL_TREE if the class-head is syntactically valid, but
13358 semantically invalid in a way that means we should skip the entire
13359 body of the class. */
13360
13361 static tree
13362 cp_parser_class_head (cp_parser* parser,
13363 bool* nested_name_specifier_p,
13364 tree *attributes_p,
13365 tree *bases)
13366 {
13367 tree nested_name_specifier;
13368 enum tag_types class_key;
13369 tree id = NULL_TREE;
13370 tree type = NULL_TREE;
13371 tree attributes;
13372 bool template_id_p = false;
13373 bool qualified_p = false;
13374 bool invalid_nested_name_p = false;
13375 bool invalid_explicit_specialization_p = false;
13376 tree pushed_scope = NULL_TREE;
13377 unsigned num_templates;
13378
13379 /* Assume no nested-name-specifier will be present. */
13380 *nested_name_specifier_p = false;
13381 /* Assume no template parameter lists will be used in defining the
13382 type. */
13383 num_templates = 0;
13384
13385 /* Look for the class-key. */
13386 class_key = cp_parser_class_key (parser);
13387 if (class_key == none_type)
13388 return error_mark_node;
13389
13390 /* Parse the attributes. */
13391 attributes = cp_parser_attributes_opt (parser);
13392
13393 /* If the next token is `::', that is invalid -- but sometimes
13394 people do try to write:
13395
13396 struct ::S {};
13397
13398 Handle this gracefully by accepting the extra qualifier, and then
13399 issuing an error about it later if this really is a
13400 class-head. If it turns out just to be an elaborated type
13401 specifier, remain silent. */
13402 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13403 qualified_p = true;
13404
13405 push_deferring_access_checks (dk_no_check);
13406
13407 /* Determine the name of the class. Begin by looking for an
13408 optional nested-name-specifier. */
13409 nested_name_specifier
13410 = cp_parser_nested_name_specifier_opt (parser,
13411 /*typename_keyword_p=*/false,
13412 /*check_dependency_p=*/false,
13413 /*type_p=*/false,
13414 /*is_declaration=*/false);
13415 /* If there was a nested-name-specifier, then there *must* be an
13416 identifier. */
13417 if (nested_name_specifier)
13418 {
13419 /* Although the grammar says `identifier', it really means
13420 `class-name' or `template-name'. You are only allowed to
13421 define a class that has already been declared with this
13422 syntax.
13423
13424 The proposed resolution for Core Issue 180 says that wherever
13425 you see `class T::X' you should treat `X' as a type-name.
13426
13427 It is OK to define an inaccessible class; for example:
13428
13429 class A { class B; };
13430 class A::B {};
13431
13432 We do not know if we will see a class-name, or a
13433 template-name. We look for a class-name first, in case the
13434 class-name is a template-id; if we looked for the
13435 template-name first we would stop after the template-name. */
13436 cp_parser_parse_tentatively (parser);
13437 type = cp_parser_class_name (parser,
13438 /*typename_keyword_p=*/false,
13439 /*template_keyword_p=*/false,
13440 class_type,
13441 /*check_dependency_p=*/false,
13442 /*class_head_p=*/true,
13443 /*is_declaration=*/false);
13444 /* If that didn't work, ignore the nested-name-specifier. */
13445 if (!cp_parser_parse_definitely (parser))
13446 {
13447 invalid_nested_name_p = true;
13448 id = cp_parser_identifier (parser);
13449 if (id == error_mark_node)
13450 id = NULL_TREE;
13451 }
13452 /* If we could not find a corresponding TYPE, treat this
13453 declaration like an unqualified declaration. */
13454 if (type == error_mark_node)
13455 nested_name_specifier = NULL_TREE;
13456 /* Otherwise, count the number of templates used in TYPE and its
13457 containing scopes. */
13458 else
13459 {
13460 tree scope;
13461
13462 for (scope = TREE_TYPE (type);
13463 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13464 scope = (TYPE_P (scope)
13465 ? TYPE_CONTEXT (scope)
13466 : DECL_CONTEXT (scope)))
13467 if (TYPE_P (scope)
13468 && CLASS_TYPE_P (scope)
13469 && CLASSTYPE_TEMPLATE_INFO (scope)
13470 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13471 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13472 ++num_templates;
13473 }
13474 }
13475 /* Otherwise, the identifier is optional. */
13476 else
13477 {
13478 /* We don't know whether what comes next is a template-id,
13479 an identifier, or nothing at all. */
13480 cp_parser_parse_tentatively (parser);
13481 /* Check for a template-id. */
13482 id = cp_parser_template_id (parser,
13483 /*template_keyword_p=*/false,
13484 /*check_dependency_p=*/true,
13485 /*is_declaration=*/true);
13486 /* If that didn't work, it could still be an identifier. */
13487 if (!cp_parser_parse_definitely (parser))
13488 {
13489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13490 id = cp_parser_identifier (parser);
13491 else
13492 id = NULL_TREE;
13493 }
13494 else
13495 {
13496 template_id_p = true;
13497 ++num_templates;
13498 }
13499 }
13500
13501 pop_deferring_access_checks ();
13502
13503 if (id)
13504 cp_parser_check_for_invalid_template_id (parser, id);
13505
13506 /* If it's not a `:' or a `{' then we can't really be looking at a
13507 class-head, since a class-head only appears as part of a
13508 class-specifier. We have to detect this situation before calling
13509 xref_tag, since that has irreversible side-effects. */
13510 if (!cp_parser_next_token_starts_class_definition_p (parser))
13511 {
13512 cp_parser_error (parser, "expected %<{%> or %<:%>");
13513 return error_mark_node;
13514 }
13515
13516 /* At this point, we're going ahead with the class-specifier, even
13517 if some other problem occurs. */
13518 cp_parser_commit_to_tentative_parse (parser);
13519 /* Issue the error about the overly-qualified name now. */
13520 if (qualified_p)
13521 cp_parser_error (parser,
13522 "global qualification of class name is invalid");
13523 else if (invalid_nested_name_p)
13524 cp_parser_error (parser,
13525 "qualified name does not name a class");
13526 else if (nested_name_specifier)
13527 {
13528 tree scope;
13529
13530 /* Reject typedef-names in class heads. */
13531 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13532 {
13533 error ("invalid class name in declaration of %qD", type);
13534 type = NULL_TREE;
13535 goto done;
13536 }
13537
13538 /* Figure out in what scope the declaration is being placed. */
13539 scope = current_scope ();
13540 /* If that scope does not contain the scope in which the
13541 class was originally declared, the program is invalid. */
13542 if (scope && !is_ancestor (scope, nested_name_specifier))
13543 {
13544 error ("declaration of %qD in %qD which does not enclose %qD",
13545 type, scope, nested_name_specifier);
13546 type = NULL_TREE;
13547 goto done;
13548 }
13549 /* [dcl.meaning]
13550
13551 A declarator-id shall not be qualified exception of the
13552 definition of a ... nested class outside of its class
13553 ... [or] a the definition or explicit instantiation of a
13554 class member of a namespace outside of its namespace. */
13555 if (scope == nested_name_specifier)
13556 {
13557 pedwarn ("extra qualification ignored");
13558 nested_name_specifier = NULL_TREE;
13559 num_templates = 0;
13560 }
13561 }
13562 /* An explicit-specialization must be preceded by "template <>". If
13563 it is not, try to recover gracefully. */
13564 if (at_namespace_scope_p ()
13565 && parser->num_template_parameter_lists == 0
13566 && template_id_p)
13567 {
13568 error ("an explicit specialization must be preceded by %<template <>%>");
13569 invalid_explicit_specialization_p = true;
13570 /* Take the same action that would have been taken by
13571 cp_parser_explicit_specialization. */
13572 ++parser->num_template_parameter_lists;
13573 begin_specialization ();
13574 }
13575 /* There must be no "return" statements between this point and the
13576 end of this function; set "type "to the correct return value and
13577 use "goto done;" to return. */
13578 /* Make sure that the right number of template parameters were
13579 present. */
13580 if (!cp_parser_check_template_parameters (parser, num_templates))
13581 {
13582 /* If something went wrong, there is no point in even trying to
13583 process the class-definition. */
13584 type = NULL_TREE;
13585 goto done;
13586 }
13587
13588 /* Look up the type. */
13589 if (template_id_p)
13590 {
13591 type = TREE_TYPE (id);
13592 type = maybe_process_partial_specialization (type);
13593 if (nested_name_specifier)
13594 pushed_scope = push_scope (nested_name_specifier);
13595 }
13596 else if (nested_name_specifier)
13597 {
13598 tree class_type;
13599
13600 /* Given:
13601
13602 template <typename T> struct S { struct T };
13603 template <typename T> struct S<T>::T { };
13604
13605 we will get a TYPENAME_TYPE when processing the definition of
13606 `S::T'. We need to resolve it to the actual type before we
13607 try to define it. */
13608 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13609 {
13610 class_type = resolve_typename_type (TREE_TYPE (type),
13611 /*only_current_p=*/false);
13612 if (class_type != error_mark_node)
13613 type = TYPE_NAME (class_type);
13614 else
13615 {
13616 cp_parser_error (parser, "could not resolve typename type");
13617 type = error_mark_node;
13618 }
13619 }
13620
13621 maybe_process_partial_specialization (TREE_TYPE (type));
13622 class_type = current_class_type;
13623 /* Enter the scope indicated by the nested-name-specifier. */
13624 pushed_scope = push_scope (nested_name_specifier);
13625 /* Get the canonical version of this type. */
13626 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13627 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13628 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13629 {
13630 type = push_template_decl (type);
13631 if (type == error_mark_node)
13632 {
13633 type = NULL_TREE;
13634 goto done;
13635 }
13636 }
13637
13638 type = TREE_TYPE (type);
13639 *nested_name_specifier_p = true;
13640 }
13641 else /* The name is not a nested name. */
13642 {
13643 /* If the class was unnamed, create a dummy name. */
13644 if (!id)
13645 id = make_anon_name ();
13646 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13647 parser->num_template_parameter_lists);
13648 }
13649
13650 /* Indicate whether this class was declared as a `class' or as a
13651 `struct'. */
13652 if (TREE_CODE (type) == RECORD_TYPE)
13653 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13654 cp_parser_check_class_key (class_key, type);
13655
13656 /* If this type was already complete, and we see another definition,
13657 that's an error. */
13658 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13659 {
13660 error ("redefinition of %q#T", type);
13661 error ("previous definition of %q+#T", type);
13662 type = NULL_TREE;
13663 goto done;
13664 }
13665 else if (type == error_mark_node)
13666 type = NULL_TREE;
13667
13668 /* We will have entered the scope containing the class; the names of
13669 base classes should be looked up in that context. For example:
13670
13671 struct A { struct B {}; struct C; };
13672 struct A::C : B {};
13673
13674 is valid. */
13675 *bases = NULL_TREE;
13676
13677 /* Get the list of base-classes, if there is one. */
13678 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13679 *bases = cp_parser_base_clause (parser);
13680
13681 done:
13682 /* Leave the scope given by the nested-name-specifier. We will
13683 enter the class scope itself while processing the members. */
13684 if (pushed_scope)
13685 pop_scope (pushed_scope);
13686
13687 if (invalid_explicit_specialization_p)
13688 {
13689 end_specialization ();
13690 --parser->num_template_parameter_lists;
13691 }
13692 *attributes_p = attributes;
13693 return type;
13694 }
13695
13696 /* Parse a class-key.
13697
13698 class-key:
13699 class
13700 struct
13701 union
13702
13703 Returns the kind of class-key specified, or none_type to indicate
13704 error. */
13705
13706 static enum tag_types
13707 cp_parser_class_key (cp_parser* parser)
13708 {
13709 cp_token *token;
13710 enum tag_types tag_type;
13711
13712 /* Look for the class-key. */
13713 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13714 if (!token)
13715 return none_type;
13716
13717 /* Check to see if the TOKEN is a class-key. */
13718 tag_type = cp_parser_token_is_class_key (token);
13719 if (!tag_type)
13720 cp_parser_error (parser, "expected class-key");
13721 return tag_type;
13722 }
13723
13724 /* Parse an (optional) member-specification.
13725
13726 member-specification:
13727 member-declaration member-specification [opt]
13728 access-specifier : member-specification [opt] */
13729
13730 static void
13731 cp_parser_member_specification_opt (cp_parser* parser)
13732 {
13733 while (true)
13734 {
13735 cp_token *token;
13736 enum rid keyword;
13737
13738 /* Peek at the next token. */
13739 token = cp_lexer_peek_token (parser->lexer);
13740 /* If it's a `}', or EOF then we've seen all the members. */
13741 if (token->type == CPP_CLOSE_BRACE
13742 || token->type == CPP_EOF
13743 || token->type == CPP_PRAGMA_EOL)
13744 break;
13745
13746 /* See if this token is a keyword. */
13747 keyword = token->keyword;
13748 switch (keyword)
13749 {
13750 case RID_PUBLIC:
13751 case RID_PROTECTED:
13752 case RID_PRIVATE:
13753 /* Consume the access-specifier. */
13754 cp_lexer_consume_token (parser->lexer);
13755 /* Remember which access-specifier is active. */
13756 current_access_specifier = token->value;
13757 /* Look for the `:'. */
13758 cp_parser_require (parser, CPP_COLON, "`:'");
13759 break;
13760
13761 default:
13762 /* Accept #pragmas at class scope. */
13763 if (token->type == CPP_PRAGMA)
13764 {
13765 cp_parser_pragma (parser, pragma_external);
13766 break;
13767 }
13768
13769 /* Otherwise, the next construction must be a
13770 member-declaration. */
13771 cp_parser_member_declaration (parser);
13772 }
13773 }
13774 }
13775
13776 /* Parse a member-declaration.
13777
13778 member-declaration:
13779 decl-specifier-seq [opt] member-declarator-list [opt] ;
13780 function-definition ; [opt]
13781 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13782 using-declaration
13783 template-declaration
13784
13785 member-declarator-list:
13786 member-declarator
13787 member-declarator-list , member-declarator
13788
13789 member-declarator:
13790 declarator pure-specifier [opt]
13791 declarator constant-initializer [opt]
13792 identifier [opt] : constant-expression
13793
13794 GNU Extensions:
13795
13796 member-declaration:
13797 __extension__ member-declaration
13798
13799 member-declarator:
13800 declarator attributes [opt] pure-specifier [opt]
13801 declarator attributes [opt] constant-initializer [opt]
13802 identifier [opt] attributes [opt] : constant-expression
13803
13804 C++0x Extensions:
13805
13806 member-declaration:
13807 static_assert-declaration */
13808
13809 static void
13810 cp_parser_member_declaration (cp_parser* parser)
13811 {
13812 cp_decl_specifier_seq decl_specifiers;
13813 tree prefix_attributes;
13814 tree decl;
13815 int declares_class_or_enum;
13816 bool friend_p;
13817 cp_token *token;
13818 int saved_pedantic;
13819
13820 /* Check for the `__extension__' keyword. */
13821 if (cp_parser_extension_opt (parser, &saved_pedantic))
13822 {
13823 /* Recurse. */
13824 cp_parser_member_declaration (parser);
13825 /* Restore the old value of the PEDANTIC flag. */
13826 pedantic = saved_pedantic;
13827
13828 return;
13829 }
13830
13831 /* Check for a template-declaration. */
13832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13833 {
13834 /* An explicit specialization here is an error condition, and we
13835 expect the specialization handler to detect and report this. */
13836 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13837 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13838 cp_parser_explicit_specialization (parser);
13839 else
13840 cp_parser_template_declaration (parser, /*member_p=*/true);
13841
13842 return;
13843 }
13844
13845 /* Check for a using-declaration. */
13846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13847 {
13848 /* Parse the using-declaration. */
13849 cp_parser_using_declaration (parser,
13850 /*access_declaration_p=*/false);
13851 return;
13852 }
13853
13854 /* Check for @defs. */
13855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13856 {
13857 tree ivar, member;
13858 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13859 ivar = ivar_chains;
13860 while (ivar)
13861 {
13862 member = ivar;
13863 ivar = TREE_CHAIN (member);
13864 TREE_CHAIN (member) = NULL_TREE;
13865 finish_member_declaration (member);
13866 }
13867 return;
13868 }
13869
13870 /* If the next token is `static_assert' we have a static assertion. */
13871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13872 {
13873 cp_parser_static_assert (parser, /*member_p=*/true);
13874 return;
13875 }
13876
13877 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13878 return;
13879
13880 /* Parse the decl-specifier-seq. */
13881 cp_parser_decl_specifier_seq (parser,
13882 CP_PARSER_FLAGS_OPTIONAL,
13883 &decl_specifiers,
13884 &declares_class_or_enum);
13885 prefix_attributes = decl_specifiers.attributes;
13886 decl_specifiers.attributes = NULL_TREE;
13887 /* Check for an invalid type-name. */
13888 if (!decl_specifiers.type
13889 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13890 return;
13891 /* If there is no declarator, then the decl-specifier-seq should
13892 specify a type. */
13893 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13894 {
13895 /* If there was no decl-specifier-seq, and the next token is a
13896 `;', then we have something like:
13897
13898 struct S { ; };
13899
13900 [class.mem]
13901
13902 Each member-declaration shall declare at least one member
13903 name of the class. */
13904 if (!decl_specifiers.any_specifiers_p)
13905 {
13906 cp_token *token = cp_lexer_peek_token (parser->lexer);
13907 if (pedantic && !token->in_system_header)
13908 pedwarn ("%Hextra %<;%>", &token->location);
13909 }
13910 else
13911 {
13912 tree type;
13913
13914 /* See if this declaration is a friend. */
13915 friend_p = cp_parser_friend_p (&decl_specifiers);
13916 /* If there were decl-specifiers, check to see if there was
13917 a class-declaration. */
13918 type = check_tag_decl (&decl_specifiers);
13919 /* Nested classes have already been added to the class, but
13920 a `friend' needs to be explicitly registered. */
13921 if (friend_p)
13922 {
13923 /* If the `friend' keyword was present, the friend must
13924 be introduced with a class-key. */
13925 if (!declares_class_or_enum)
13926 error ("a class-key must be used when declaring a friend");
13927 /* In this case:
13928
13929 template <typename T> struct A {
13930 friend struct A<T>::B;
13931 };
13932
13933 A<T>::B will be represented by a TYPENAME_TYPE, and
13934 therefore not recognized by check_tag_decl. */
13935 if (!type
13936 && decl_specifiers.type
13937 && TYPE_P (decl_specifiers.type))
13938 type = decl_specifiers.type;
13939 if (!type || !TYPE_P (type))
13940 error ("friend declaration does not name a class or "
13941 "function");
13942 else
13943 make_friend_class (current_class_type, type,
13944 /*complain=*/true);
13945 }
13946 /* If there is no TYPE, an error message will already have
13947 been issued. */
13948 else if (!type || type == error_mark_node)
13949 ;
13950 /* An anonymous aggregate has to be handled specially; such
13951 a declaration really declares a data member (with a
13952 particular type), as opposed to a nested class. */
13953 else if (ANON_AGGR_TYPE_P (type))
13954 {
13955 /* Remove constructors and such from TYPE, now that we
13956 know it is an anonymous aggregate. */
13957 fixup_anonymous_aggr (type);
13958 /* And make the corresponding data member. */
13959 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13960 /* Add it to the class. */
13961 finish_member_declaration (decl);
13962 }
13963 else
13964 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13965 }
13966 }
13967 else
13968 {
13969 /* See if these declarations will be friends. */
13970 friend_p = cp_parser_friend_p (&decl_specifiers);
13971
13972 /* Keep going until we hit the `;' at the end of the
13973 declaration. */
13974 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13975 {
13976 tree attributes = NULL_TREE;
13977 tree first_attribute;
13978
13979 /* Peek at the next token. */
13980 token = cp_lexer_peek_token (parser->lexer);
13981
13982 /* Check for a bitfield declaration. */
13983 if (token->type == CPP_COLON
13984 || (token->type == CPP_NAME
13985 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13986 == CPP_COLON))
13987 {
13988 tree identifier;
13989 tree width;
13990
13991 /* Get the name of the bitfield. Note that we cannot just
13992 check TOKEN here because it may have been invalidated by
13993 the call to cp_lexer_peek_nth_token above. */
13994 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13995 identifier = cp_parser_identifier (parser);
13996 else
13997 identifier = NULL_TREE;
13998
13999 /* Consume the `:' token. */
14000 cp_lexer_consume_token (parser->lexer);
14001 /* Get the width of the bitfield. */
14002 width
14003 = cp_parser_constant_expression (parser,
14004 /*allow_non_constant=*/false,
14005 NULL);
14006
14007 /* Look for attributes that apply to the bitfield. */
14008 attributes = cp_parser_attributes_opt (parser);
14009 /* Remember which attributes are prefix attributes and
14010 which are not. */
14011 first_attribute = attributes;
14012 /* Combine the attributes. */
14013 attributes = chainon (prefix_attributes, attributes);
14014
14015 /* Create the bitfield declaration. */
14016 decl = grokbitfield (identifier
14017 ? make_id_declarator (NULL_TREE,
14018 identifier,
14019 sfk_none)
14020 : NULL,
14021 &decl_specifiers,
14022 width);
14023 /* Apply the attributes. */
14024 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14025 }
14026 else
14027 {
14028 cp_declarator *declarator;
14029 tree initializer;
14030 tree asm_specification;
14031 int ctor_dtor_or_conv_p;
14032
14033 /* Parse the declarator. */
14034 declarator
14035 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14036 &ctor_dtor_or_conv_p,
14037 /*parenthesized_p=*/NULL,
14038 /*member_p=*/true);
14039
14040 /* If something went wrong parsing the declarator, make sure
14041 that we at least consume some tokens. */
14042 if (declarator == cp_error_declarator)
14043 {
14044 /* Skip to the end of the statement. */
14045 cp_parser_skip_to_end_of_statement (parser);
14046 /* If the next token is not a semicolon, that is
14047 probably because we just skipped over the body of
14048 a function. So, we consume a semicolon if
14049 present, but do not issue an error message if it
14050 is not present. */
14051 if (cp_lexer_next_token_is (parser->lexer,
14052 CPP_SEMICOLON))
14053 cp_lexer_consume_token (parser->lexer);
14054 return;
14055 }
14056
14057 if (declares_class_or_enum & 2)
14058 cp_parser_check_for_definition_in_return_type
14059 (declarator, decl_specifiers.type);
14060
14061 /* Look for an asm-specification. */
14062 asm_specification = cp_parser_asm_specification_opt (parser);
14063 /* Look for attributes that apply to the declaration. */
14064 attributes = cp_parser_attributes_opt (parser);
14065 /* Remember which attributes are prefix attributes and
14066 which are not. */
14067 first_attribute = attributes;
14068 /* Combine the attributes. */
14069 attributes = chainon (prefix_attributes, attributes);
14070
14071 /* If it's an `=', then we have a constant-initializer or a
14072 pure-specifier. It is not correct to parse the
14073 initializer before registering the member declaration
14074 since the member declaration should be in scope while
14075 its initializer is processed. However, the rest of the
14076 front end does not yet provide an interface that allows
14077 us to handle this correctly. */
14078 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14079 {
14080 /* In [class.mem]:
14081
14082 A pure-specifier shall be used only in the declaration of
14083 a virtual function.
14084
14085 A member-declarator can contain a constant-initializer
14086 only if it declares a static member of integral or
14087 enumeration type.
14088
14089 Therefore, if the DECLARATOR is for a function, we look
14090 for a pure-specifier; otherwise, we look for a
14091 constant-initializer. When we call `grokfield', it will
14092 perform more stringent semantics checks. */
14093 if (function_declarator_p (declarator))
14094 initializer = cp_parser_pure_specifier (parser);
14095 else
14096 /* Parse the initializer. */
14097 initializer = cp_parser_constant_initializer (parser);
14098 }
14099 /* Otherwise, there is no initializer. */
14100 else
14101 initializer = NULL_TREE;
14102
14103 /* See if we are probably looking at a function
14104 definition. We are certainly not looking at a
14105 member-declarator. Calling `grokfield' has
14106 side-effects, so we must not do it unless we are sure
14107 that we are looking at a member-declarator. */
14108 if (cp_parser_token_starts_function_definition_p
14109 (cp_lexer_peek_token (parser->lexer)))
14110 {
14111 /* The grammar does not allow a pure-specifier to be
14112 used when a member function is defined. (It is
14113 possible that this fact is an oversight in the
14114 standard, since a pure function may be defined
14115 outside of the class-specifier. */
14116 if (initializer)
14117 error ("pure-specifier on function-definition");
14118 decl = cp_parser_save_member_function_body (parser,
14119 &decl_specifiers,
14120 declarator,
14121 attributes);
14122 /* If the member was not a friend, declare it here. */
14123 if (!friend_p)
14124 finish_member_declaration (decl);
14125 /* Peek at the next token. */
14126 token = cp_lexer_peek_token (parser->lexer);
14127 /* If the next token is a semicolon, consume it. */
14128 if (token->type == CPP_SEMICOLON)
14129 cp_lexer_consume_token (parser->lexer);
14130 return;
14131 }
14132 else
14133 /* Create the declaration. */
14134 decl = grokfield (declarator, &decl_specifiers,
14135 initializer, /*init_const_expr_p=*/true,
14136 asm_specification,
14137 attributes);
14138 }
14139
14140 /* Reset PREFIX_ATTRIBUTES. */
14141 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14142 attributes = TREE_CHAIN (attributes);
14143 if (attributes)
14144 TREE_CHAIN (attributes) = NULL_TREE;
14145
14146 /* If there is any qualification still in effect, clear it
14147 now; we will be starting fresh with the next declarator. */
14148 parser->scope = NULL_TREE;
14149 parser->qualifying_scope = NULL_TREE;
14150 parser->object_scope = NULL_TREE;
14151 /* If it's a `,', then there are more declarators. */
14152 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14153 cp_lexer_consume_token (parser->lexer);
14154 /* If the next token isn't a `;', then we have a parse error. */
14155 else if (cp_lexer_next_token_is_not (parser->lexer,
14156 CPP_SEMICOLON))
14157 {
14158 cp_parser_error (parser, "expected %<;%>");
14159 /* Skip tokens until we find a `;'. */
14160 cp_parser_skip_to_end_of_statement (parser);
14161
14162 break;
14163 }
14164
14165 if (decl)
14166 {
14167 /* Add DECL to the list of members. */
14168 if (!friend_p)
14169 finish_member_declaration (decl);
14170
14171 if (TREE_CODE (decl) == FUNCTION_DECL)
14172 cp_parser_save_default_args (parser, decl);
14173 }
14174 }
14175 }
14176
14177 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14178 }
14179
14180 /* Parse a pure-specifier.
14181
14182 pure-specifier:
14183 = 0
14184
14185 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14186 Otherwise, ERROR_MARK_NODE is returned. */
14187
14188 static tree
14189 cp_parser_pure_specifier (cp_parser* parser)
14190 {
14191 cp_token *token;
14192
14193 /* Look for the `=' token. */
14194 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14195 return error_mark_node;
14196 /* Look for the `0' token. */
14197 token = cp_lexer_consume_token (parser->lexer);
14198 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14199 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14200 {
14201 cp_parser_error (parser,
14202 "invalid pure specifier (only `= 0' is allowed)");
14203 cp_parser_skip_to_end_of_statement (parser);
14204 return error_mark_node;
14205 }
14206 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14207 {
14208 error ("templates may not be %<virtual%>");
14209 return error_mark_node;
14210 }
14211
14212 return integer_zero_node;
14213 }
14214
14215 /* Parse a constant-initializer.
14216
14217 constant-initializer:
14218 = constant-expression
14219
14220 Returns a representation of the constant-expression. */
14221
14222 static tree
14223 cp_parser_constant_initializer (cp_parser* parser)
14224 {
14225 /* Look for the `=' token. */
14226 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14227 return error_mark_node;
14228
14229 /* It is invalid to write:
14230
14231 struct S { static const int i = { 7 }; };
14232
14233 */
14234 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14235 {
14236 cp_parser_error (parser,
14237 "a brace-enclosed initializer is not allowed here");
14238 /* Consume the opening brace. */
14239 cp_lexer_consume_token (parser->lexer);
14240 /* Skip the initializer. */
14241 cp_parser_skip_to_closing_brace (parser);
14242 /* Look for the trailing `}'. */
14243 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14244
14245 return error_mark_node;
14246 }
14247
14248 return cp_parser_constant_expression (parser,
14249 /*allow_non_constant=*/false,
14250 NULL);
14251 }
14252
14253 /* Derived classes [gram.class.derived] */
14254
14255 /* Parse a base-clause.
14256
14257 base-clause:
14258 : base-specifier-list
14259
14260 base-specifier-list:
14261 base-specifier
14262 base-specifier-list , base-specifier
14263
14264 Returns a TREE_LIST representing the base-classes, in the order in
14265 which they were declared. The representation of each node is as
14266 described by cp_parser_base_specifier.
14267
14268 In the case that no bases are specified, this function will return
14269 NULL_TREE, not ERROR_MARK_NODE. */
14270
14271 static tree
14272 cp_parser_base_clause (cp_parser* parser)
14273 {
14274 tree bases = NULL_TREE;
14275
14276 /* Look for the `:' that begins the list. */
14277 cp_parser_require (parser, CPP_COLON, "`:'");
14278
14279 /* Scan the base-specifier-list. */
14280 while (true)
14281 {
14282 cp_token *token;
14283 tree base;
14284
14285 /* Look for the base-specifier. */
14286 base = cp_parser_base_specifier (parser);
14287 /* Add BASE to the front of the list. */
14288 if (base != error_mark_node)
14289 {
14290 TREE_CHAIN (base) = bases;
14291 bases = base;
14292 }
14293 /* Peek at the next token. */
14294 token = cp_lexer_peek_token (parser->lexer);
14295 /* If it's not a comma, then the list is complete. */
14296 if (token->type != CPP_COMMA)
14297 break;
14298 /* Consume the `,'. */
14299 cp_lexer_consume_token (parser->lexer);
14300 }
14301
14302 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14303 base class had a qualified name. However, the next name that
14304 appears is certainly not qualified. */
14305 parser->scope = NULL_TREE;
14306 parser->qualifying_scope = NULL_TREE;
14307 parser->object_scope = NULL_TREE;
14308
14309 return nreverse (bases);
14310 }
14311
14312 /* Parse a base-specifier.
14313
14314 base-specifier:
14315 :: [opt] nested-name-specifier [opt] class-name
14316 virtual access-specifier [opt] :: [opt] nested-name-specifier
14317 [opt] class-name
14318 access-specifier virtual [opt] :: [opt] nested-name-specifier
14319 [opt] class-name
14320
14321 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14322 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14323 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14324 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14325
14326 static tree
14327 cp_parser_base_specifier (cp_parser* parser)
14328 {
14329 cp_token *token;
14330 bool done = false;
14331 bool virtual_p = false;
14332 bool duplicate_virtual_error_issued_p = false;
14333 bool duplicate_access_error_issued_p = false;
14334 bool class_scope_p, template_p;
14335 tree access = access_default_node;
14336 tree type;
14337
14338 /* Process the optional `virtual' and `access-specifier'. */
14339 while (!done)
14340 {
14341 /* Peek at the next token. */
14342 token = cp_lexer_peek_token (parser->lexer);
14343 /* Process `virtual'. */
14344 switch (token->keyword)
14345 {
14346 case RID_VIRTUAL:
14347 /* If `virtual' appears more than once, issue an error. */
14348 if (virtual_p && !duplicate_virtual_error_issued_p)
14349 {
14350 cp_parser_error (parser,
14351 "%<virtual%> specified more than once in base-specified");
14352 duplicate_virtual_error_issued_p = true;
14353 }
14354
14355 virtual_p = true;
14356
14357 /* Consume the `virtual' token. */
14358 cp_lexer_consume_token (parser->lexer);
14359
14360 break;
14361
14362 case RID_PUBLIC:
14363 case RID_PROTECTED:
14364 case RID_PRIVATE:
14365 /* If more than one access specifier appears, issue an
14366 error. */
14367 if (access != access_default_node
14368 && !duplicate_access_error_issued_p)
14369 {
14370 cp_parser_error (parser,
14371 "more than one access specifier in base-specified");
14372 duplicate_access_error_issued_p = true;
14373 }
14374
14375 access = ridpointers[(int) token->keyword];
14376
14377 /* Consume the access-specifier. */
14378 cp_lexer_consume_token (parser->lexer);
14379
14380 break;
14381
14382 default:
14383 done = true;
14384 break;
14385 }
14386 }
14387 /* It is not uncommon to see programs mechanically, erroneously, use
14388 the 'typename' keyword to denote (dependent) qualified types
14389 as base classes. */
14390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14391 {
14392 if (!processing_template_decl)
14393 error ("keyword %<typename%> not allowed outside of templates");
14394 else
14395 error ("keyword %<typename%> not allowed in this context "
14396 "(the base class is implicitly a type)");
14397 cp_lexer_consume_token (parser->lexer);
14398 }
14399
14400 /* Look for the optional `::' operator. */
14401 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14402 /* Look for the nested-name-specifier. The simplest way to
14403 implement:
14404
14405 [temp.res]
14406
14407 The keyword `typename' is not permitted in a base-specifier or
14408 mem-initializer; in these contexts a qualified name that
14409 depends on a template-parameter is implicitly assumed to be a
14410 type name.
14411
14412 is to pretend that we have seen the `typename' keyword at this
14413 point. */
14414 cp_parser_nested_name_specifier_opt (parser,
14415 /*typename_keyword_p=*/true,
14416 /*check_dependency_p=*/true,
14417 typename_type,
14418 /*is_declaration=*/true);
14419 /* If the base class is given by a qualified name, assume that names
14420 we see are type names or templates, as appropriate. */
14421 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14422 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14423
14424 /* Finally, look for the class-name. */
14425 type = cp_parser_class_name (parser,
14426 class_scope_p,
14427 template_p,
14428 typename_type,
14429 /*check_dependency_p=*/true,
14430 /*class_head_p=*/false,
14431 /*is_declaration=*/true);
14432
14433 if (type == error_mark_node)
14434 return error_mark_node;
14435
14436 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14437 }
14438
14439 /* Exception handling [gram.exception] */
14440
14441 /* Parse an (optional) exception-specification.
14442
14443 exception-specification:
14444 throw ( type-id-list [opt] )
14445
14446 Returns a TREE_LIST representing the exception-specification. The
14447 TREE_VALUE of each node is a type. */
14448
14449 static tree
14450 cp_parser_exception_specification_opt (cp_parser* parser)
14451 {
14452 cp_token *token;
14453 tree type_id_list;
14454
14455 /* Peek at the next token. */
14456 token = cp_lexer_peek_token (parser->lexer);
14457 /* If it's not `throw', then there's no exception-specification. */
14458 if (!cp_parser_is_keyword (token, RID_THROW))
14459 return NULL_TREE;
14460
14461 /* Consume the `throw'. */
14462 cp_lexer_consume_token (parser->lexer);
14463
14464 /* Look for the `('. */
14465 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14466
14467 /* Peek at the next token. */
14468 token = cp_lexer_peek_token (parser->lexer);
14469 /* If it's not a `)', then there is a type-id-list. */
14470 if (token->type != CPP_CLOSE_PAREN)
14471 {
14472 const char *saved_message;
14473
14474 /* Types may not be defined in an exception-specification. */
14475 saved_message = parser->type_definition_forbidden_message;
14476 parser->type_definition_forbidden_message
14477 = "types may not be defined in an exception-specification";
14478 /* Parse the type-id-list. */
14479 type_id_list = cp_parser_type_id_list (parser);
14480 /* Restore the saved message. */
14481 parser->type_definition_forbidden_message = saved_message;
14482 }
14483 else
14484 type_id_list = empty_except_spec;
14485
14486 /* Look for the `)'. */
14487 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14488
14489 return type_id_list;
14490 }
14491
14492 /* Parse an (optional) type-id-list.
14493
14494 type-id-list:
14495 type-id
14496 type-id-list , type-id
14497
14498 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14499 in the order that the types were presented. */
14500
14501 static tree
14502 cp_parser_type_id_list (cp_parser* parser)
14503 {
14504 tree types = NULL_TREE;
14505
14506 while (true)
14507 {
14508 cp_token *token;
14509 tree type;
14510
14511 /* Get the next type-id. */
14512 type = cp_parser_type_id (parser);
14513 /* Add it to the list. */
14514 types = add_exception_specifier (types, type, /*complain=*/1);
14515 /* Peek at the next token. */
14516 token = cp_lexer_peek_token (parser->lexer);
14517 /* If it is not a `,', we are done. */
14518 if (token->type != CPP_COMMA)
14519 break;
14520 /* Consume the `,'. */
14521 cp_lexer_consume_token (parser->lexer);
14522 }
14523
14524 return nreverse (types);
14525 }
14526
14527 /* Parse a try-block.
14528
14529 try-block:
14530 try compound-statement handler-seq */
14531
14532 static tree
14533 cp_parser_try_block (cp_parser* parser)
14534 {
14535 tree try_block;
14536
14537 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14538 try_block = begin_try_block ();
14539 cp_parser_compound_statement (parser, NULL, true);
14540 finish_try_block (try_block);
14541 cp_parser_handler_seq (parser);
14542 finish_handler_sequence (try_block);
14543
14544 return try_block;
14545 }
14546
14547 /* Parse a function-try-block.
14548
14549 function-try-block:
14550 try ctor-initializer [opt] function-body handler-seq */
14551
14552 static bool
14553 cp_parser_function_try_block (cp_parser* parser)
14554 {
14555 tree compound_stmt;
14556 tree try_block;
14557 bool ctor_initializer_p;
14558
14559 /* Look for the `try' keyword. */
14560 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14561 return false;
14562 /* Let the rest of the front-end know where we are. */
14563 try_block = begin_function_try_block (&compound_stmt);
14564 /* Parse the function-body. */
14565 ctor_initializer_p
14566 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14567 /* We're done with the `try' part. */
14568 finish_function_try_block (try_block);
14569 /* Parse the handlers. */
14570 cp_parser_handler_seq (parser);
14571 /* We're done with the handlers. */
14572 finish_function_handler_sequence (try_block, compound_stmt);
14573
14574 return ctor_initializer_p;
14575 }
14576
14577 /* Parse a handler-seq.
14578
14579 handler-seq:
14580 handler handler-seq [opt] */
14581
14582 static void
14583 cp_parser_handler_seq (cp_parser* parser)
14584 {
14585 while (true)
14586 {
14587 cp_token *token;
14588
14589 /* Parse the handler. */
14590 cp_parser_handler (parser);
14591 /* Peek at the next token. */
14592 token = cp_lexer_peek_token (parser->lexer);
14593 /* If it's not `catch' then there are no more handlers. */
14594 if (!cp_parser_is_keyword (token, RID_CATCH))
14595 break;
14596 }
14597 }
14598
14599 /* Parse a handler.
14600
14601 handler:
14602 catch ( exception-declaration ) compound-statement */
14603
14604 static void
14605 cp_parser_handler (cp_parser* parser)
14606 {
14607 tree handler;
14608 tree declaration;
14609
14610 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14611 handler = begin_handler ();
14612 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14613 declaration = cp_parser_exception_declaration (parser);
14614 finish_handler_parms (declaration, handler);
14615 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14616 cp_parser_compound_statement (parser, NULL, false);
14617 finish_handler (handler);
14618 }
14619
14620 /* Parse an exception-declaration.
14621
14622 exception-declaration:
14623 type-specifier-seq declarator
14624 type-specifier-seq abstract-declarator
14625 type-specifier-seq
14626 ...
14627
14628 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14629 ellipsis variant is used. */
14630
14631 static tree
14632 cp_parser_exception_declaration (cp_parser* parser)
14633 {
14634 cp_decl_specifier_seq type_specifiers;
14635 cp_declarator *declarator;
14636 const char *saved_message;
14637
14638 /* If it's an ellipsis, it's easy to handle. */
14639 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14640 {
14641 /* Consume the `...' token. */
14642 cp_lexer_consume_token (parser->lexer);
14643 return NULL_TREE;
14644 }
14645
14646 /* Types may not be defined in exception-declarations. */
14647 saved_message = parser->type_definition_forbidden_message;
14648 parser->type_definition_forbidden_message
14649 = "types may not be defined in exception-declarations";
14650
14651 /* Parse the type-specifier-seq. */
14652 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14653 &type_specifiers);
14654 /* If it's a `)', then there is no declarator. */
14655 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14656 declarator = NULL;
14657 else
14658 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14659 /*ctor_dtor_or_conv_p=*/NULL,
14660 /*parenthesized_p=*/NULL,
14661 /*member_p=*/false);
14662
14663 /* Restore the saved message. */
14664 parser->type_definition_forbidden_message = saved_message;
14665
14666 if (!type_specifiers.any_specifiers_p)
14667 return error_mark_node;
14668
14669 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14670 }
14671
14672 /* Parse a throw-expression.
14673
14674 throw-expression:
14675 throw assignment-expression [opt]
14676
14677 Returns a THROW_EXPR representing the throw-expression. */
14678
14679 static tree
14680 cp_parser_throw_expression (cp_parser* parser)
14681 {
14682 tree expression;
14683 cp_token* token;
14684
14685 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14686 token = cp_lexer_peek_token (parser->lexer);
14687 /* Figure out whether or not there is an assignment-expression
14688 following the "throw" keyword. */
14689 if (token->type == CPP_COMMA
14690 || token->type == CPP_SEMICOLON
14691 || token->type == CPP_CLOSE_PAREN
14692 || token->type == CPP_CLOSE_SQUARE
14693 || token->type == CPP_CLOSE_BRACE
14694 || token->type == CPP_COLON)
14695 expression = NULL_TREE;
14696 else
14697 expression = cp_parser_assignment_expression (parser,
14698 /*cast_p=*/false);
14699
14700 return build_throw (expression);
14701 }
14702
14703 /* GNU Extensions */
14704
14705 /* Parse an (optional) asm-specification.
14706
14707 asm-specification:
14708 asm ( string-literal )
14709
14710 If the asm-specification is present, returns a STRING_CST
14711 corresponding to the string-literal. Otherwise, returns
14712 NULL_TREE. */
14713
14714 static tree
14715 cp_parser_asm_specification_opt (cp_parser* parser)
14716 {
14717 cp_token *token;
14718 tree asm_specification;
14719
14720 /* Peek at the next token. */
14721 token = cp_lexer_peek_token (parser->lexer);
14722 /* If the next token isn't the `asm' keyword, then there's no
14723 asm-specification. */
14724 if (!cp_parser_is_keyword (token, RID_ASM))
14725 return NULL_TREE;
14726
14727 /* Consume the `asm' token. */
14728 cp_lexer_consume_token (parser->lexer);
14729 /* Look for the `('. */
14730 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14731
14732 /* Look for the string-literal. */
14733 asm_specification = cp_parser_string_literal (parser, false, false);
14734
14735 /* Look for the `)'. */
14736 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14737
14738 return asm_specification;
14739 }
14740
14741 /* Parse an asm-operand-list.
14742
14743 asm-operand-list:
14744 asm-operand
14745 asm-operand-list , asm-operand
14746
14747 asm-operand:
14748 string-literal ( expression )
14749 [ string-literal ] string-literal ( expression )
14750
14751 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14752 each node is the expression. The TREE_PURPOSE is itself a
14753 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14754 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14755 is a STRING_CST for the string literal before the parenthesis. */
14756
14757 static tree
14758 cp_parser_asm_operand_list (cp_parser* parser)
14759 {
14760 tree asm_operands = NULL_TREE;
14761
14762 while (true)
14763 {
14764 tree string_literal;
14765 tree expression;
14766 tree name;
14767
14768 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14769 {
14770 /* Consume the `[' token. */
14771 cp_lexer_consume_token (parser->lexer);
14772 /* Read the operand name. */
14773 name = cp_parser_identifier (parser);
14774 if (name != error_mark_node)
14775 name = build_string (IDENTIFIER_LENGTH (name),
14776 IDENTIFIER_POINTER (name));
14777 /* Look for the closing `]'. */
14778 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14779 }
14780 else
14781 name = NULL_TREE;
14782 /* Look for the string-literal. */
14783 string_literal = cp_parser_string_literal (parser, false, false);
14784
14785 /* Look for the `('. */
14786 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14787 /* Parse the expression. */
14788 expression = cp_parser_expression (parser, /*cast_p=*/false);
14789 /* Look for the `)'. */
14790 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14791
14792 /* Add this operand to the list. */
14793 asm_operands = tree_cons (build_tree_list (name, string_literal),
14794 expression,
14795 asm_operands);
14796 /* If the next token is not a `,', there are no more
14797 operands. */
14798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14799 break;
14800 /* Consume the `,'. */
14801 cp_lexer_consume_token (parser->lexer);
14802 }
14803
14804 return nreverse (asm_operands);
14805 }
14806
14807 /* Parse an asm-clobber-list.
14808
14809 asm-clobber-list:
14810 string-literal
14811 asm-clobber-list , string-literal
14812
14813 Returns a TREE_LIST, indicating the clobbers in the order that they
14814 appeared. The TREE_VALUE of each node is a STRING_CST. */
14815
14816 static tree
14817 cp_parser_asm_clobber_list (cp_parser* parser)
14818 {
14819 tree clobbers = NULL_TREE;
14820
14821 while (true)
14822 {
14823 tree string_literal;
14824
14825 /* Look for the string literal. */
14826 string_literal = cp_parser_string_literal (parser, false, false);
14827 /* Add it to the list. */
14828 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14829 /* If the next token is not a `,', then the list is
14830 complete. */
14831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14832 break;
14833 /* Consume the `,' token. */
14834 cp_lexer_consume_token (parser->lexer);
14835 }
14836
14837 return clobbers;
14838 }
14839
14840 /* Parse an (optional) series of attributes.
14841
14842 attributes:
14843 attributes attribute
14844
14845 attribute:
14846 __attribute__ (( attribute-list [opt] ))
14847
14848 The return value is as for cp_parser_attribute_list. */
14849
14850 static tree
14851 cp_parser_attributes_opt (cp_parser* parser)
14852 {
14853 tree attributes = NULL_TREE;
14854
14855 while (true)
14856 {
14857 cp_token *token;
14858 tree attribute_list;
14859
14860 /* Peek at the next token. */
14861 token = cp_lexer_peek_token (parser->lexer);
14862 /* If it's not `__attribute__', then we're done. */
14863 if (token->keyword != RID_ATTRIBUTE)
14864 break;
14865
14866 /* Consume the `__attribute__' keyword. */
14867 cp_lexer_consume_token (parser->lexer);
14868 /* Look for the two `(' tokens. */
14869 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14870 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14871
14872 /* Peek at the next token. */
14873 token = cp_lexer_peek_token (parser->lexer);
14874 if (token->type != CPP_CLOSE_PAREN)
14875 /* Parse the attribute-list. */
14876 attribute_list = cp_parser_attribute_list (parser);
14877 else
14878 /* If the next token is a `)', then there is no attribute
14879 list. */
14880 attribute_list = NULL;
14881
14882 /* Look for the two `)' tokens. */
14883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14884 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14885
14886 /* Add these new attributes to the list. */
14887 attributes = chainon (attributes, attribute_list);
14888 }
14889
14890 return attributes;
14891 }
14892
14893 /* Parse an attribute-list.
14894
14895 attribute-list:
14896 attribute
14897 attribute-list , attribute
14898
14899 attribute:
14900 identifier
14901 identifier ( identifier )
14902 identifier ( identifier , expression-list )
14903 identifier ( expression-list )
14904
14905 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14906 to an attribute. The TREE_PURPOSE of each node is the identifier
14907 indicating which attribute is in use. The TREE_VALUE represents
14908 the arguments, if any. */
14909
14910 static tree
14911 cp_parser_attribute_list (cp_parser* parser)
14912 {
14913 tree attribute_list = NULL_TREE;
14914 bool save_translate_strings_p = parser->translate_strings_p;
14915
14916 parser->translate_strings_p = false;
14917 while (true)
14918 {
14919 cp_token *token;
14920 tree identifier;
14921 tree attribute;
14922
14923 /* Look for the identifier. We also allow keywords here; for
14924 example `__attribute__ ((const))' is legal. */
14925 token = cp_lexer_peek_token (parser->lexer);
14926 if (token->type == CPP_NAME
14927 || token->type == CPP_KEYWORD)
14928 {
14929 tree arguments = NULL_TREE;
14930
14931 /* Consume the token. */
14932 token = cp_lexer_consume_token (parser->lexer);
14933
14934 /* Save away the identifier that indicates which attribute
14935 this is. */
14936 identifier = token->value;
14937 attribute = build_tree_list (identifier, NULL_TREE);
14938
14939 /* Peek at the next token. */
14940 token = cp_lexer_peek_token (parser->lexer);
14941 /* If it's an `(', then parse the attribute arguments. */
14942 if (token->type == CPP_OPEN_PAREN)
14943 {
14944 arguments = cp_parser_parenthesized_expression_list
14945 (parser, true, /*cast_p=*/false,
14946 /*non_constant_p=*/NULL);
14947 /* Save the arguments away. */
14948 TREE_VALUE (attribute) = arguments;
14949 }
14950
14951 if (arguments != error_mark_node)
14952 {
14953 /* Add this attribute to the list. */
14954 TREE_CHAIN (attribute) = attribute_list;
14955 attribute_list = attribute;
14956 }
14957
14958 token = cp_lexer_peek_token (parser->lexer);
14959 }
14960 /* Now, look for more attributes. If the next token isn't a
14961 `,', we're done. */
14962 if (token->type != CPP_COMMA)
14963 break;
14964
14965 /* Consume the comma and keep going. */
14966 cp_lexer_consume_token (parser->lexer);
14967 }
14968 parser->translate_strings_p = save_translate_strings_p;
14969
14970 /* We built up the list in reverse order. */
14971 return nreverse (attribute_list);
14972 }
14973
14974 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14975 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14976 current value of the PEDANTIC flag, regardless of whether or not
14977 the `__extension__' keyword is present. The caller is responsible
14978 for restoring the value of the PEDANTIC flag. */
14979
14980 static bool
14981 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14982 {
14983 /* Save the old value of the PEDANTIC flag. */
14984 *saved_pedantic = pedantic;
14985
14986 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14987 {
14988 /* Consume the `__extension__' token. */
14989 cp_lexer_consume_token (parser->lexer);
14990 /* We're not being pedantic while the `__extension__' keyword is
14991 in effect. */
14992 pedantic = 0;
14993
14994 return true;
14995 }
14996
14997 return false;
14998 }
14999
15000 /* Parse a label declaration.
15001
15002 label-declaration:
15003 __label__ label-declarator-seq ;
15004
15005 label-declarator-seq:
15006 identifier , label-declarator-seq
15007 identifier */
15008
15009 static void
15010 cp_parser_label_declaration (cp_parser* parser)
15011 {
15012 /* Look for the `__label__' keyword. */
15013 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15014
15015 while (true)
15016 {
15017 tree identifier;
15018
15019 /* Look for an identifier. */
15020 identifier = cp_parser_identifier (parser);
15021 /* If we failed, stop. */
15022 if (identifier == error_mark_node)
15023 break;
15024 /* Declare it as a label. */
15025 finish_label_decl (identifier);
15026 /* If the next token is a `;', stop. */
15027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15028 break;
15029 /* Look for the `,' separating the label declarations. */
15030 cp_parser_require (parser, CPP_COMMA, "`,'");
15031 }
15032
15033 /* Look for the final `;'. */
15034 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15035 }
15036
15037 /* Support Functions */
15038
15039 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15040 NAME should have one of the representations used for an
15041 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15042 is returned. If PARSER->SCOPE is a dependent type, then a
15043 SCOPE_REF is returned.
15044
15045 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15046 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15047 was formed. Abstractly, such entities should not be passed to this
15048 function, because they do not need to be looked up, but it is
15049 simpler to check for this special case here, rather than at the
15050 call-sites.
15051
15052 In cases not explicitly covered above, this function returns a
15053 DECL, OVERLOAD, or baselink representing the result of the lookup.
15054 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15055 is returned.
15056
15057 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15058 (e.g., "struct") that was used. In that case bindings that do not
15059 refer to types are ignored.
15060
15061 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15062 ignored.
15063
15064 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15065 are ignored.
15066
15067 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15068 types.
15069
15070 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15071 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15072 NULL_TREE otherwise. */
15073
15074 static tree
15075 cp_parser_lookup_name (cp_parser *parser, tree name,
15076 enum tag_types tag_type,
15077 bool is_template,
15078 bool is_namespace,
15079 bool check_dependency,
15080 tree *ambiguous_decls)
15081 {
15082 int flags = 0;
15083 tree decl;
15084 tree object_type = parser->context->object_type;
15085
15086 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15087 flags |= LOOKUP_COMPLAIN;
15088
15089 /* Assume that the lookup will be unambiguous. */
15090 if (ambiguous_decls)
15091 *ambiguous_decls = NULL_TREE;
15092
15093 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15094 no longer valid. Note that if we are parsing tentatively, and
15095 the parse fails, OBJECT_TYPE will be automatically restored. */
15096 parser->context->object_type = NULL_TREE;
15097
15098 if (name == error_mark_node)
15099 return error_mark_node;
15100
15101 /* A template-id has already been resolved; there is no lookup to
15102 do. */
15103 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15104 return name;
15105 if (BASELINK_P (name))
15106 {
15107 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15108 == TEMPLATE_ID_EXPR);
15109 return name;
15110 }
15111
15112 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15113 it should already have been checked to make sure that the name
15114 used matches the type being destroyed. */
15115 if (TREE_CODE (name) == BIT_NOT_EXPR)
15116 {
15117 tree type;
15118
15119 /* Figure out to which type this destructor applies. */
15120 if (parser->scope)
15121 type = parser->scope;
15122 else if (object_type)
15123 type = object_type;
15124 else
15125 type = current_class_type;
15126 /* If that's not a class type, there is no destructor. */
15127 if (!type || !CLASS_TYPE_P (type))
15128 return error_mark_node;
15129 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15130 lazily_declare_fn (sfk_destructor, type);
15131 if (!CLASSTYPE_DESTRUCTORS (type))
15132 return error_mark_node;
15133 /* If it was a class type, return the destructor. */
15134 return CLASSTYPE_DESTRUCTORS (type);
15135 }
15136
15137 /* By this point, the NAME should be an ordinary identifier. If
15138 the id-expression was a qualified name, the qualifying scope is
15139 stored in PARSER->SCOPE at this point. */
15140 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15141
15142 /* Perform the lookup. */
15143 if (parser->scope)
15144 {
15145 bool dependent_p;
15146
15147 if (parser->scope == error_mark_node)
15148 return error_mark_node;
15149
15150 /* If the SCOPE is dependent, the lookup must be deferred until
15151 the template is instantiated -- unless we are explicitly
15152 looking up names in uninstantiated templates. Even then, we
15153 cannot look up the name if the scope is not a class type; it
15154 might, for example, be a template type parameter. */
15155 dependent_p = (TYPE_P (parser->scope)
15156 && !(parser->in_declarator_p
15157 && currently_open_class (parser->scope))
15158 && dependent_type_p (parser->scope));
15159 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15160 && dependent_p)
15161 {
15162 if (tag_type)
15163 {
15164 tree type;
15165
15166 /* The resolution to Core Issue 180 says that `struct
15167 A::B' should be considered a type-name, even if `A'
15168 is dependent. */
15169 type = make_typename_type (parser->scope, name, tag_type,
15170 /*complain=*/tf_error);
15171 decl = TYPE_NAME (type);
15172 }
15173 else if (is_template
15174 && (cp_parser_next_token_ends_template_argument_p (parser)
15175 || cp_lexer_next_token_is (parser->lexer,
15176 CPP_CLOSE_PAREN)))
15177 decl = make_unbound_class_template (parser->scope,
15178 name, NULL_TREE,
15179 /*complain=*/tf_error);
15180 else
15181 decl = build_qualified_name (/*type=*/NULL_TREE,
15182 parser->scope, name,
15183 is_template);
15184 }
15185 else
15186 {
15187 tree pushed_scope = NULL_TREE;
15188
15189 /* If PARSER->SCOPE is a dependent type, then it must be a
15190 class type, and we must not be checking dependencies;
15191 otherwise, we would have processed this lookup above. So
15192 that PARSER->SCOPE is not considered a dependent base by
15193 lookup_member, we must enter the scope here. */
15194 if (dependent_p)
15195 pushed_scope = push_scope (parser->scope);
15196 /* If the PARSER->SCOPE is a template specialization, it
15197 may be instantiated during name lookup. In that case,
15198 errors may be issued. Even if we rollback the current
15199 tentative parse, those errors are valid. */
15200 decl = lookup_qualified_name (parser->scope, name,
15201 tag_type != none_type,
15202 /*complain=*/true);
15203 if (pushed_scope)
15204 pop_scope (pushed_scope);
15205 }
15206 parser->qualifying_scope = parser->scope;
15207 parser->object_scope = NULL_TREE;
15208 }
15209 else if (object_type)
15210 {
15211 tree object_decl = NULL_TREE;
15212 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15213 OBJECT_TYPE is not a class. */
15214 if (CLASS_TYPE_P (object_type))
15215 /* If the OBJECT_TYPE is a template specialization, it may
15216 be instantiated during name lookup. In that case, errors
15217 may be issued. Even if we rollback the current tentative
15218 parse, those errors are valid. */
15219 object_decl = lookup_member (object_type,
15220 name,
15221 /*protect=*/0,
15222 tag_type != none_type);
15223 /* Look it up in the enclosing context, too. */
15224 decl = lookup_name_real (name, tag_type != none_type,
15225 /*nonclass=*/0,
15226 /*block_p=*/true, is_namespace, flags);
15227 parser->object_scope = object_type;
15228 parser->qualifying_scope = NULL_TREE;
15229 if (object_decl)
15230 decl = object_decl;
15231 }
15232 else
15233 {
15234 decl = lookup_name_real (name, tag_type != none_type,
15235 /*nonclass=*/0,
15236 /*block_p=*/true, is_namespace, flags);
15237 parser->qualifying_scope = NULL_TREE;
15238 parser->object_scope = NULL_TREE;
15239 }
15240
15241 /* If the lookup failed, let our caller know. */
15242 if (!decl || decl == error_mark_node)
15243 return error_mark_node;
15244
15245 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15246 if (TREE_CODE (decl) == TREE_LIST)
15247 {
15248 if (ambiguous_decls)
15249 *ambiguous_decls = decl;
15250 /* The error message we have to print is too complicated for
15251 cp_parser_error, so we incorporate its actions directly. */
15252 if (!cp_parser_simulate_error (parser))
15253 {
15254 error ("reference to %qD is ambiguous", name);
15255 print_candidates (decl);
15256 }
15257 return error_mark_node;
15258 }
15259
15260 gcc_assert (DECL_P (decl)
15261 || TREE_CODE (decl) == OVERLOAD
15262 || TREE_CODE (decl) == SCOPE_REF
15263 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15264 || BASELINK_P (decl));
15265
15266 /* If we have resolved the name of a member declaration, check to
15267 see if the declaration is accessible. When the name resolves to
15268 set of overloaded functions, accessibility is checked when
15269 overload resolution is done.
15270
15271 During an explicit instantiation, access is not checked at all,
15272 as per [temp.explicit]. */
15273 if (DECL_P (decl))
15274 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15275
15276 return decl;
15277 }
15278
15279 /* Like cp_parser_lookup_name, but for use in the typical case where
15280 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15281 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15282
15283 static tree
15284 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15285 {
15286 return cp_parser_lookup_name (parser, name,
15287 none_type,
15288 /*is_template=*/false,
15289 /*is_namespace=*/false,
15290 /*check_dependency=*/true,
15291 /*ambiguous_decls=*/NULL);
15292 }
15293
15294 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15295 the current context, return the TYPE_DECL. If TAG_NAME_P is
15296 true, the DECL indicates the class being defined in a class-head,
15297 or declared in an elaborated-type-specifier.
15298
15299 Otherwise, return DECL. */
15300
15301 static tree
15302 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15303 {
15304 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15305 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15306
15307 struct A {
15308 template <typename T> struct B;
15309 };
15310
15311 template <typename T> struct A::B {};
15312
15313 Similarly, in an elaborated-type-specifier:
15314
15315 namespace N { struct X{}; }
15316
15317 struct A {
15318 template <typename T> friend struct N::X;
15319 };
15320
15321 However, if the DECL refers to a class type, and we are in
15322 the scope of the class, then the name lookup automatically
15323 finds the TYPE_DECL created by build_self_reference rather
15324 than a TEMPLATE_DECL. For example, in:
15325
15326 template <class T> struct S {
15327 S s;
15328 };
15329
15330 there is no need to handle such case. */
15331
15332 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15333 return DECL_TEMPLATE_RESULT (decl);
15334
15335 return decl;
15336 }
15337
15338 /* If too many, or too few, template-parameter lists apply to the
15339 declarator, issue an error message. Returns TRUE if all went well,
15340 and FALSE otherwise. */
15341
15342 static bool
15343 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15344 cp_declarator *declarator)
15345 {
15346 unsigned num_templates;
15347
15348 /* We haven't seen any classes that involve template parameters yet. */
15349 num_templates = 0;
15350
15351 switch (declarator->kind)
15352 {
15353 case cdk_id:
15354 if (declarator->u.id.qualifying_scope)
15355 {
15356 tree scope;
15357 tree member;
15358
15359 scope = declarator->u.id.qualifying_scope;
15360 member = declarator->u.id.unqualified_name;
15361
15362 while (scope && CLASS_TYPE_P (scope))
15363 {
15364 /* You're supposed to have one `template <...>'
15365 for every template class, but you don't need one
15366 for a full specialization. For example:
15367
15368 template <class T> struct S{};
15369 template <> struct S<int> { void f(); };
15370 void S<int>::f () {}
15371
15372 is correct; there shouldn't be a `template <>' for
15373 the definition of `S<int>::f'. */
15374 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15375 /* If SCOPE does not have template information of any
15376 kind, then it is not a template, nor is it nested
15377 within a template. */
15378 break;
15379 if (explicit_class_specialization_p (scope))
15380 break;
15381 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15382 ++num_templates;
15383
15384 scope = TYPE_CONTEXT (scope);
15385 }
15386 }
15387 else if (TREE_CODE (declarator->u.id.unqualified_name)
15388 == TEMPLATE_ID_EXPR)
15389 /* If the DECLARATOR has the form `X<y>' then it uses one
15390 additional level of template parameters. */
15391 ++num_templates;
15392
15393 return cp_parser_check_template_parameters (parser,
15394 num_templates);
15395
15396 case cdk_function:
15397 case cdk_array:
15398 case cdk_pointer:
15399 case cdk_reference:
15400 case cdk_ptrmem:
15401 return (cp_parser_check_declarator_template_parameters
15402 (parser, declarator->declarator));
15403
15404 case cdk_error:
15405 return true;
15406
15407 default:
15408 gcc_unreachable ();
15409 }
15410 return false;
15411 }
15412
15413 /* NUM_TEMPLATES were used in the current declaration. If that is
15414 invalid, return FALSE and issue an error messages. Otherwise,
15415 return TRUE. */
15416
15417 static bool
15418 cp_parser_check_template_parameters (cp_parser* parser,
15419 unsigned num_templates)
15420 {
15421 /* If there are more template classes than parameter lists, we have
15422 something like:
15423
15424 template <class T> void S<T>::R<T>::f (); */
15425 if (parser->num_template_parameter_lists < num_templates)
15426 {
15427 error ("too few template-parameter-lists");
15428 return false;
15429 }
15430 /* If there are the same number of template classes and parameter
15431 lists, that's OK. */
15432 if (parser->num_template_parameter_lists == num_templates)
15433 return true;
15434 /* If there are more, but only one more, then we are referring to a
15435 member template. That's OK too. */
15436 if (parser->num_template_parameter_lists == num_templates + 1)
15437 return true;
15438 /* Otherwise, there are too many template parameter lists. We have
15439 something like:
15440
15441 template <class T> template <class U> void S::f(); */
15442 error ("too many template-parameter-lists");
15443 return false;
15444 }
15445
15446 /* Parse an optional `::' token indicating that the following name is
15447 from the global namespace. If so, PARSER->SCOPE is set to the
15448 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15449 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15450 Returns the new value of PARSER->SCOPE, if the `::' token is
15451 present, and NULL_TREE otherwise. */
15452
15453 static tree
15454 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15455 {
15456 cp_token *token;
15457
15458 /* Peek at the next token. */
15459 token = cp_lexer_peek_token (parser->lexer);
15460 /* If we're looking at a `::' token then we're starting from the
15461 global namespace, not our current location. */
15462 if (token->type == CPP_SCOPE)
15463 {
15464 /* Consume the `::' token. */
15465 cp_lexer_consume_token (parser->lexer);
15466 /* Set the SCOPE so that we know where to start the lookup. */
15467 parser->scope = global_namespace;
15468 parser->qualifying_scope = global_namespace;
15469 parser->object_scope = NULL_TREE;
15470
15471 return parser->scope;
15472 }
15473 else if (!current_scope_valid_p)
15474 {
15475 parser->scope = NULL_TREE;
15476 parser->qualifying_scope = NULL_TREE;
15477 parser->object_scope = NULL_TREE;
15478 }
15479
15480 return NULL_TREE;
15481 }
15482
15483 /* Returns TRUE if the upcoming token sequence is the start of a
15484 constructor declarator. If FRIEND_P is true, the declarator is
15485 preceded by the `friend' specifier. */
15486
15487 static bool
15488 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15489 {
15490 bool constructor_p;
15491 tree type_decl = NULL_TREE;
15492 bool nested_name_p;
15493 cp_token *next_token;
15494
15495 /* The common case is that this is not a constructor declarator, so
15496 try to avoid doing lots of work if at all possible. It's not
15497 valid declare a constructor at function scope. */
15498 if (parser->in_function_body)
15499 return false;
15500 /* And only certain tokens can begin a constructor declarator. */
15501 next_token = cp_lexer_peek_token (parser->lexer);
15502 if (next_token->type != CPP_NAME
15503 && next_token->type != CPP_SCOPE
15504 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15505 && next_token->type != CPP_TEMPLATE_ID)
15506 return false;
15507
15508 /* Parse tentatively; we are going to roll back all of the tokens
15509 consumed here. */
15510 cp_parser_parse_tentatively (parser);
15511 /* Assume that we are looking at a constructor declarator. */
15512 constructor_p = true;
15513
15514 /* Look for the optional `::' operator. */
15515 cp_parser_global_scope_opt (parser,
15516 /*current_scope_valid_p=*/false);
15517 /* Look for the nested-name-specifier. */
15518 nested_name_p
15519 = (cp_parser_nested_name_specifier_opt (parser,
15520 /*typename_keyword_p=*/false,
15521 /*check_dependency_p=*/false,
15522 /*type_p=*/false,
15523 /*is_declaration=*/false)
15524 != NULL_TREE);
15525 /* Outside of a class-specifier, there must be a
15526 nested-name-specifier. */
15527 if (!nested_name_p &&
15528 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15529 || friend_p))
15530 constructor_p = false;
15531 /* If we still think that this might be a constructor-declarator,
15532 look for a class-name. */
15533 if (constructor_p)
15534 {
15535 /* If we have:
15536
15537 template <typename T> struct S { S(); };
15538 template <typename T> S<T>::S ();
15539
15540 we must recognize that the nested `S' names a class.
15541 Similarly, for:
15542
15543 template <typename T> S<T>::S<T> ();
15544
15545 we must recognize that the nested `S' names a template. */
15546 type_decl = cp_parser_class_name (parser,
15547 /*typename_keyword_p=*/false,
15548 /*template_keyword_p=*/false,
15549 none_type,
15550 /*check_dependency_p=*/false,
15551 /*class_head_p=*/false,
15552 /*is_declaration=*/false);
15553 /* If there was no class-name, then this is not a constructor. */
15554 constructor_p = !cp_parser_error_occurred (parser);
15555 }
15556
15557 /* If we're still considering a constructor, we have to see a `(',
15558 to begin the parameter-declaration-clause, followed by either a
15559 `)', an `...', or a decl-specifier. We need to check for a
15560 type-specifier to avoid being fooled into thinking that:
15561
15562 S::S (f) (int);
15563
15564 is a constructor. (It is actually a function named `f' that
15565 takes one parameter (of type `int') and returns a value of type
15566 `S::S'. */
15567 if (constructor_p
15568 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15569 {
15570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15571 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15572 /* A parameter declaration begins with a decl-specifier,
15573 which is either the "attribute" keyword, a storage class
15574 specifier, or (usually) a type-specifier. */
15575 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15576 {
15577 tree type;
15578 tree pushed_scope = NULL_TREE;
15579 unsigned saved_num_template_parameter_lists;
15580
15581 /* Names appearing in the type-specifier should be looked up
15582 in the scope of the class. */
15583 if (current_class_type)
15584 type = NULL_TREE;
15585 else
15586 {
15587 type = TREE_TYPE (type_decl);
15588 if (TREE_CODE (type) == TYPENAME_TYPE)
15589 {
15590 type = resolve_typename_type (type,
15591 /*only_current_p=*/false);
15592 if (type == error_mark_node)
15593 {
15594 cp_parser_abort_tentative_parse (parser);
15595 return false;
15596 }
15597 }
15598 pushed_scope = push_scope (type);
15599 }
15600
15601 /* Inside the constructor parameter list, surrounding
15602 template-parameter-lists do not apply. */
15603 saved_num_template_parameter_lists
15604 = parser->num_template_parameter_lists;
15605 parser->num_template_parameter_lists = 0;
15606
15607 /* Look for the type-specifier. */
15608 cp_parser_type_specifier (parser,
15609 CP_PARSER_FLAGS_NONE,
15610 /*decl_specs=*/NULL,
15611 /*is_declarator=*/true,
15612 /*declares_class_or_enum=*/NULL,
15613 /*is_cv_qualifier=*/NULL);
15614
15615 parser->num_template_parameter_lists
15616 = saved_num_template_parameter_lists;
15617
15618 /* Leave the scope of the class. */
15619 if (pushed_scope)
15620 pop_scope (pushed_scope);
15621
15622 constructor_p = !cp_parser_error_occurred (parser);
15623 }
15624 }
15625 else
15626 constructor_p = false;
15627 /* We did not really want to consume any tokens. */
15628 cp_parser_abort_tentative_parse (parser);
15629
15630 return constructor_p;
15631 }
15632
15633 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15634 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15635 they must be performed once we are in the scope of the function.
15636
15637 Returns the function defined. */
15638
15639 static tree
15640 cp_parser_function_definition_from_specifiers_and_declarator
15641 (cp_parser* parser,
15642 cp_decl_specifier_seq *decl_specifiers,
15643 tree attributes,
15644 const cp_declarator *declarator)
15645 {
15646 tree fn;
15647 bool success_p;
15648
15649 /* Begin the function-definition. */
15650 success_p = start_function (decl_specifiers, declarator, attributes);
15651
15652 /* The things we're about to see are not directly qualified by any
15653 template headers we've seen thus far. */
15654 reset_specialization ();
15655
15656 /* If there were names looked up in the decl-specifier-seq that we
15657 did not check, check them now. We must wait until we are in the
15658 scope of the function to perform the checks, since the function
15659 might be a friend. */
15660 perform_deferred_access_checks ();
15661
15662 if (!success_p)
15663 {
15664 /* Skip the entire function. */
15665 cp_parser_skip_to_end_of_block_or_statement (parser);
15666 fn = error_mark_node;
15667 }
15668 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15669 {
15670 /* Seen already, skip it. An error message has already been output. */
15671 cp_parser_skip_to_end_of_block_or_statement (parser);
15672 fn = current_function_decl;
15673 current_function_decl = NULL_TREE;
15674 /* If this is a function from a class, pop the nested class. */
15675 if (current_class_name)
15676 pop_nested_class ();
15677 }
15678 else
15679 fn = cp_parser_function_definition_after_declarator (parser,
15680 /*inline_p=*/false);
15681
15682 return fn;
15683 }
15684
15685 /* Parse the part of a function-definition that follows the
15686 declarator. INLINE_P is TRUE iff this function is an inline
15687 function defined with a class-specifier.
15688
15689 Returns the function defined. */
15690
15691 static tree
15692 cp_parser_function_definition_after_declarator (cp_parser* parser,
15693 bool inline_p)
15694 {
15695 tree fn;
15696 bool ctor_initializer_p = false;
15697 bool saved_in_unbraced_linkage_specification_p;
15698 bool saved_in_function_body;
15699 unsigned saved_num_template_parameter_lists;
15700
15701 saved_in_function_body = parser->in_function_body;
15702 parser->in_function_body = true;
15703 /* If the next token is `return', then the code may be trying to
15704 make use of the "named return value" extension that G++ used to
15705 support. */
15706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15707 {
15708 /* Consume the `return' keyword. */
15709 cp_lexer_consume_token (parser->lexer);
15710 /* Look for the identifier that indicates what value is to be
15711 returned. */
15712 cp_parser_identifier (parser);
15713 /* Issue an error message. */
15714 error ("named return values are no longer supported");
15715 /* Skip tokens until we reach the start of the function body. */
15716 while (true)
15717 {
15718 cp_token *token = cp_lexer_peek_token (parser->lexer);
15719 if (token->type == CPP_OPEN_BRACE
15720 || token->type == CPP_EOF
15721 || token->type == CPP_PRAGMA_EOL)
15722 break;
15723 cp_lexer_consume_token (parser->lexer);
15724 }
15725 }
15726 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15727 anything declared inside `f'. */
15728 saved_in_unbraced_linkage_specification_p
15729 = parser->in_unbraced_linkage_specification_p;
15730 parser->in_unbraced_linkage_specification_p = false;
15731 /* Inside the function, surrounding template-parameter-lists do not
15732 apply. */
15733 saved_num_template_parameter_lists
15734 = parser->num_template_parameter_lists;
15735 parser->num_template_parameter_lists = 0;
15736 /* If the next token is `try', then we are looking at a
15737 function-try-block. */
15738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15739 ctor_initializer_p = cp_parser_function_try_block (parser);
15740 /* A function-try-block includes the function-body, so we only do
15741 this next part if we're not processing a function-try-block. */
15742 else
15743 ctor_initializer_p
15744 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15745
15746 /* Finish the function. */
15747 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15748 (inline_p ? 2 : 0));
15749 /* Generate code for it, if necessary. */
15750 expand_or_defer_fn (fn);
15751 /* Restore the saved values. */
15752 parser->in_unbraced_linkage_specification_p
15753 = saved_in_unbraced_linkage_specification_p;
15754 parser->num_template_parameter_lists
15755 = saved_num_template_parameter_lists;
15756 parser->in_function_body = saved_in_function_body;
15757
15758 return fn;
15759 }
15760
15761 /* Parse a template-declaration, assuming that the `export' (and
15762 `extern') keywords, if present, has already been scanned. MEMBER_P
15763 is as for cp_parser_template_declaration. */
15764
15765 static void
15766 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15767 {
15768 tree decl = NULL_TREE;
15769 tree checks;
15770 tree parameter_list;
15771 bool friend_p = false;
15772 bool need_lang_pop;
15773
15774 /* Look for the `template' keyword. */
15775 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15776 return;
15777
15778 /* And the `<'. */
15779 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15780 return;
15781 if (at_class_scope_p () && current_function_decl)
15782 {
15783 /* 14.5.2.2 [temp.mem]
15784
15785 A local class shall not have member templates. */
15786 error ("invalid declaration of member template in local class");
15787 cp_parser_skip_to_end_of_block_or_statement (parser);
15788 return;
15789 }
15790 /* [temp]
15791
15792 A template ... shall not have C linkage. */
15793 if (current_lang_name == lang_name_c)
15794 {
15795 error ("template with C linkage");
15796 /* Give it C++ linkage to avoid confusing other parts of the
15797 front end. */
15798 push_lang_context (lang_name_cplusplus);
15799 need_lang_pop = true;
15800 }
15801 else
15802 need_lang_pop = false;
15803
15804 /* We cannot perform access checks on the template parameter
15805 declarations until we know what is being declared, just as we
15806 cannot check the decl-specifier list. */
15807 push_deferring_access_checks (dk_deferred);
15808
15809 /* If the next token is `>', then we have an invalid
15810 specialization. Rather than complain about an invalid template
15811 parameter, issue an error message here. */
15812 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15813 {
15814 cp_parser_error (parser, "invalid explicit specialization");
15815 begin_specialization ();
15816 parameter_list = NULL_TREE;
15817 }
15818 else
15819 /* Parse the template parameters. */
15820 parameter_list = cp_parser_template_parameter_list (parser);
15821
15822 /* Get the deferred access checks from the parameter list. These
15823 will be checked once we know what is being declared, as for a
15824 member template the checks must be performed in the scope of the
15825 class containing the member. */
15826 checks = get_deferred_access_checks ();
15827
15828 /* Look for the `>'. */
15829 cp_parser_skip_to_end_of_template_parameter_list (parser);
15830 /* We just processed one more parameter list. */
15831 ++parser->num_template_parameter_lists;
15832 /* If the next token is `template', there are more template
15833 parameters. */
15834 if (cp_lexer_next_token_is_keyword (parser->lexer,
15835 RID_TEMPLATE))
15836 cp_parser_template_declaration_after_export (parser, member_p);
15837 else
15838 {
15839 /* There are no access checks when parsing a template, as we do not
15840 know if a specialization will be a friend. */
15841 push_deferring_access_checks (dk_no_check);
15842 decl = cp_parser_single_declaration (parser,
15843 checks,
15844 member_p,
15845 &friend_p);
15846 pop_deferring_access_checks ();
15847
15848 /* If this is a member template declaration, let the front
15849 end know. */
15850 if (member_p && !friend_p && decl)
15851 {
15852 if (TREE_CODE (decl) == TYPE_DECL)
15853 cp_parser_check_access_in_redeclaration (decl);
15854
15855 decl = finish_member_template_decl (decl);
15856 }
15857 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15858 make_friend_class (current_class_type, TREE_TYPE (decl),
15859 /*complain=*/true);
15860 }
15861 /* We are done with the current parameter list. */
15862 --parser->num_template_parameter_lists;
15863
15864 pop_deferring_access_checks ();
15865
15866 /* Finish up. */
15867 finish_template_decl (parameter_list);
15868
15869 /* Register member declarations. */
15870 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15871 finish_member_declaration (decl);
15872 /* For the erroneous case of a template with C linkage, we pushed an
15873 implicit C++ linkage scope; exit that scope now. */
15874 if (need_lang_pop)
15875 pop_lang_context ();
15876 /* If DECL is a function template, we must return to parse it later.
15877 (Even though there is no definition, there might be default
15878 arguments that need handling.) */
15879 if (member_p && decl
15880 && (TREE_CODE (decl) == FUNCTION_DECL
15881 || DECL_FUNCTION_TEMPLATE_P (decl)))
15882 TREE_VALUE (parser->unparsed_functions_queues)
15883 = tree_cons (NULL_TREE, decl,
15884 TREE_VALUE (parser->unparsed_functions_queues));
15885 }
15886
15887 /* Perform the deferred access checks from a template-parameter-list.
15888 CHECKS is a TREE_LIST of access checks, as returned by
15889 get_deferred_access_checks. */
15890
15891 static void
15892 cp_parser_perform_template_parameter_access_checks (tree checks)
15893 {
15894 ++processing_template_parmlist;
15895 perform_access_checks (checks);
15896 --processing_template_parmlist;
15897 }
15898
15899 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15900 `function-definition' sequence. MEMBER_P is true, this declaration
15901 appears in a class scope.
15902
15903 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15904 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15905
15906 static tree
15907 cp_parser_single_declaration (cp_parser* parser,
15908 tree checks,
15909 bool member_p,
15910 bool* friend_p)
15911 {
15912 int declares_class_or_enum;
15913 tree decl = NULL_TREE;
15914 cp_decl_specifier_seq decl_specifiers;
15915 bool function_definition_p = false;
15916
15917 /* This function is only used when processing a template
15918 declaration. */
15919 gcc_assert (innermost_scope_kind () == sk_template_parms
15920 || innermost_scope_kind () == sk_template_spec);
15921
15922 /* Defer access checks until we know what is being declared. */
15923 push_deferring_access_checks (dk_deferred);
15924
15925 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15926 alternative. */
15927 cp_parser_decl_specifier_seq (parser,
15928 CP_PARSER_FLAGS_OPTIONAL,
15929 &decl_specifiers,
15930 &declares_class_or_enum);
15931 if (friend_p)
15932 *friend_p = cp_parser_friend_p (&decl_specifiers);
15933
15934 /* There are no template typedefs. */
15935 if (decl_specifiers.specs[(int) ds_typedef])
15936 {
15937 error ("template declaration of %qs", "typedef");
15938 decl = error_mark_node;
15939 }
15940
15941 /* Gather up the access checks that occurred the
15942 decl-specifier-seq. */
15943 stop_deferring_access_checks ();
15944
15945 /* Check for the declaration of a template class. */
15946 if (declares_class_or_enum)
15947 {
15948 if (cp_parser_declares_only_class_p (parser))
15949 {
15950 decl = shadow_tag (&decl_specifiers);
15951
15952 /* In this case:
15953
15954 struct C {
15955 friend template <typename T> struct A<T>::B;
15956 };
15957
15958 A<T>::B will be represented by a TYPENAME_TYPE, and
15959 therefore not recognized by shadow_tag. */
15960 if (friend_p && *friend_p
15961 && !decl
15962 && decl_specifiers.type
15963 && TYPE_P (decl_specifiers.type))
15964 decl = decl_specifiers.type;
15965
15966 if (decl && decl != error_mark_node)
15967 decl = TYPE_NAME (decl);
15968 else
15969 decl = error_mark_node;
15970
15971 /* Perform access checks for template parameters. */
15972 cp_parser_perform_template_parameter_access_checks (checks);
15973 }
15974 }
15975 /* If it's not a template class, try for a template function. If
15976 the next token is a `;', then this declaration does not declare
15977 anything. But, if there were errors in the decl-specifiers, then
15978 the error might well have come from an attempted class-specifier.
15979 In that case, there's no need to warn about a missing declarator. */
15980 if (!decl
15981 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15982 || decl_specifiers.type != error_mark_node))
15983 decl = cp_parser_init_declarator (parser,
15984 &decl_specifiers,
15985 checks,
15986 /*function_definition_allowed_p=*/true,
15987 member_p,
15988 declares_class_or_enum,
15989 &function_definition_p);
15990
15991 pop_deferring_access_checks ();
15992
15993 /* Clear any current qualification; whatever comes next is the start
15994 of something new. */
15995 parser->scope = NULL_TREE;
15996 parser->qualifying_scope = NULL_TREE;
15997 parser->object_scope = NULL_TREE;
15998 /* Look for a trailing `;' after the declaration. */
15999 if (!function_definition_p
16000 && (decl == error_mark_node
16001 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16002 cp_parser_skip_to_end_of_block_or_statement (parser);
16003
16004 return decl;
16005 }
16006
16007 /* Parse a cast-expression that is not the operand of a unary "&". */
16008
16009 static tree
16010 cp_parser_simple_cast_expression (cp_parser *parser)
16011 {
16012 return cp_parser_cast_expression (parser, /*address_p=*/false,
16013 /*cast_p=*/false);
16014 }
16015
16016 /* Parse a functional cast to TYPE. Returns an expression
16017 representing the cast. */
16018
16019 static tree
16020 cp_parser_functional_cast (cp_parser* parser, tree type)
16021 {
16022 tree expression_list;
16023 tree cast;
16024
16025 expression_list
16026 = cp_parser_parenthesized_expression_list (parser, false,
16027 /*cast_p=*/true,
16028 /*non_constant_p=*/NULL);
16029
16030 cast = build_functional_cast (type, expression_list);
16031 /* [expr.const]/1: In an integral constant expression "only type
16032 conversions to integral or enumeration type can be used". */
16033 if (TREE_CODE (type) == TYPE_DECL)
16034 type = TREE_TYPE (type);
16035 if (cast != error_mark_node
16036 && !cast_valid_in_integral_constant_expression_p (type)
16037 && (cp_parser_non_integral_constant_expression
16038 (parser, "a call to a constructor")))
16039 return error_mark_node;
16040 return cast;
16041 }
16042
16043 /* Save the tokens that make up the body of a member function defined
16044 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16045 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16046 specifiers applied to the declaration. Returns the FUNCTION_DECL
16047 for the member function. */
16048
16049 static tree
16050 cp_parser_save_member_function_body (cp_parser* parser,
16051 cp_decl_specifier_seq *decl_specifiers,
16052 cp_declarator *declarator,
16053 tree attributes)
16054 {
16055 cp_token *first;
16056 cp_token *last;
16057 tree fn;
16058
16059 /* Create the function-declaration. */
16060 fn = start_method (decl_specifiers, declarator, attributes);
16061 /* If something went badly wrong, bail out now. */
16062 if (fn == error_mark_node)
16063 {
16064 /* If there's a function-body, skip it. */
16065 if (cp_parser_token_starts_function_definition_p
16066 (cp_lexer_peek_token (parser->lexer)))
16067 cp_parser_skip_to_end_of_block_or_statement (parser);
16068 return error_mark_node;
16069 }
16070
16071 /* Remember it, if there default args to post process. */
16072 cp_parser_save_default_args (parser, fn);
16073
16074 /* Save away the tokens that make up the body of the
16075 function. */
16076 first = parser->lexer->next_token;
16077 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16078 /* Handle function try blocks. */
16079 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16080 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16081 last = parser->lexer->next_token;
16082
16083 /* Save away the inline definition; we will process it when the
16084 class is complete. */
16085 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16086 DECL_PENDING_INLINE_P (fn) = 1;
16087
16088 /* We need to know that this was defined in the class, so that
16089 friend templates are handled correctly. */
16090 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16091
16092 /* We're done with the inline definition. */
16093 finish_method (fn);
16094
16095 /* Add FN to the queue of functions to be parsed later. */
16096 TREE_VALUE (parser->unparsed_functions_queues)
16097 = tree_cons (NULL_TREE, fn,
16098 TREE_VALUE (parser->unparsed_functions_queues));
16099
16100 return fn;
16101 }
16102
16103 /* Parse a template-argument-list, as well as the trailing ">" (but
16104 not the opening ">"). See cp_parser_template_argument_list for the
16105 return value. */
16106
16107 static tree
16108 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16109 {
16110 tree arguments;
16111 tree saved_scope;
16112 tree saved_qualifying_scope;
16113 tree saved_object_scope;
16114 bool saved_greater_than_is_operator_p;
16115 bool saved_skip_evaluation;
16116
16117 /* [temp.names]
16118
16119 When parsing a template-id, the first non-nested `>' is taken as
16120 the end of the template-argument-list rather than a greater-than
16121 operator. */
16122 saved_greater_than_is_operator_p
16123 = parser->greater_than_is_operator_p;
16124 parser->greater_than_is_operator_p = false;
16125 /* Parsing the argument list may modify SCOPE, so we save it
16126 here. */
16127 saved_scope = parser->scope;
16128 saved_qualifying_scope = parser->qualifying_scope;
16129 saved_object_scope = parser->object_scope;
16130 /* We need to evaluate the template arguments, even though this
16131 template-id may be nested within a "sizeof". */
16132 saved_skip_evaluation = skip_evaluation;
16133 skip_evaluation = false;
16134 /* Parse the template-argument-list itself. */
16135 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16136 arguments = NULL_TREE;
16137 else
16138 arguments = cp_parser_template_argument_list (parser);
16139 /* Look for the `>' that ends the template-argument-list. If we find
16140 a '>>' instead, it's probably just a typo. */
16141 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16142 {
16143 if (!saved_greater_than_is_operator_p)
16144 {
16145 /* If we're in a nested template argument list, the '>>' has
16146 to be a typo for '> >'. We emit the error message, but we
16147 continue parsing and we push a '>' as next token, so that
16148 the argument list will be parsed correctly. Note that the
16149 global source location is still on the token before the
16150 '>>', so we need to say explicitly where we want it. */
16151 cp_token *token = cp_lexer_peek_token (parser->lexer);
16152 error ("%H%<>>%> should be %<> >%> "
16153 "within a nested template argument list",
16154 &token->location);
16155
16156 /* ??? Proper recovery should terminate two levels of
16157 template argument list here. */
16158 token->type = CPP_GREATER;
16159 }
16160 else
16161 {
16162 /* If this is not a nested template argument list, the '>>'
16163 is a typo for '>'. Emit an error message and continue.
16164 Same deal about the token location, but here we can get it
16165 right by consuming the '>>' before issuing the diagnostic. */
16166 cp_lexer_consume_token (parser->lexer);
16167 error ("spurious %<>>%>, use %<>%> to terminate "
16168 "a template argument list");
16169 }
16170 }
16171 else
16172 cp_parser_skip_to_end_of_template_parameter_list (parser);
16173 /* The `>' token might be a greater-than operator again now. */
16174 parser->greater_than_is_operator_p
16175 = saved_greater_than_is_operator_p;
16176 /* Restore the SAVED_SCOPE. */
16177 parser->scope = saved_scope;
16178 parser->qualifying_scope = saved_qualifying_scope;
16179 parser->object_scope = saved_object_scope;
16180 skip_evaluation = saved_skip_evaluation;
16181
16182 return arguments;
16183 }
16184
16185 /* MEMBER_FUNCTION is a member function, or a friend. If default
16186 arguments, or the body of the function have not yet been parsed,
16187 parse them now. */
16188
16189 static void
16190 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16191 {
16192 /* If this member is a template, get the underlying
16193 FUNCTION_DECL. */
16194 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16195 member_function = DECL_TEMPLATE_RESULT (member_function);
16196
16197 /* There should not be any class definitions in progress at this
16198 point; the bodies of members are only parsed outside of all class
16199 definitions. */
16200 gcc_assert (parser->num_classes_being_defined == 0);
16201 /* While we're parsing the member functions we might encounter more
16202 classes. We want to handle them right away, but we don't want
16203 them getting mixed up with functions that are currently in the
16204 queue. */
16205 parser->unparsed_functions_queues
16206 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16207
16208 /* Make sure that any template parameters are in scope. */
16209 maybe_begin_member_template_processing (member_function);
16210
16211 /* If the body of the function has not yet been parsed, parse it
16212 now. */
16213 if (DECL_PENDING_INLINE_P (member_function))
16214 {
16215 tree function_scope;
16216 cp_token_cache *tokens;
16217
16218 /* The function is no longer pending; we are processing it. */
16219 tokens = DECL_PENDING_INLINE_INFO (member_function);
16220 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16221 DECL_PENDING_INLINE_P (member_function) = 0;
16222
16223 /* If this is a local class, enter the scope of the containing
16224 function. */
16225 function_scope = current_function_decl;
16226 if (function_scope)
16227 push_function_context_to (function_scope);
16228
16229
16230 /* Push the body of the function onto the lexer stack. */
16231 cp_parser_push_lexer_for_tokens (parser, tokens);
16232
16233 /* Let the front end know that we going to be defining this
16234 function. */
16235 start_preparsed_function (member_function, NULL_TREE,
16236 SF_PRE_PARSED | SF_INCLASS_INLINE);
16237
16238 /* Don't do access checking if it is a templated function. */
16239 if (processing_template_decl)
16240 push_deferring_access_checks (dk_no_check);
16241
16242 /* Now, parse the body of the function. */
16243 cp_parser_function_definition_after_declarator (parser,
16244 /*inline_p=*/true);
16245
16246 if (processing_template_decl)
16247 pop_deferring_access_checks ();
16248
16249 /* Leave the scope of the containing function. */
16250 if (function_scope)
16251 pop_function_context_from (function_scope);
16252 cp_parser_pop_lexer (parser);
16253 }
16254
16255 /* Remove any template parameters from the symbol table. */
16256 maybe_end_member_template_processing ();
16257
16258 /* Restore the queue. */
16259 parser->unparsed_functions_queues
16260 = TREE_CHAIN (parser->unparsed_functions_queues);
16261 }
16262
16263 /* If DECL contains any default args, remember it on the unparsed
16264 functions queue. */
16265
16266 static void
16267 cp_parser_save_default_args (cp_parser* parser, tree decl)
16268 {
16269 tree probe;
16270
16271 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16272 probe;
16273 probe = TREE_CHAIN (probe))
16274 if (TREE_PURPOSE (probe))
16275 {
16276 TREE_PURPOSE (parser->unparsed_functions_queues)
16277 = tree_cons (current_class_type, decl,
16278 TREE_PURPOSE (parser->unparsed_functions_queues));
16279 break;
16280 }
16281 }
16282
16283 /* FN is a FUNCTION_DECL which may contains a parameter with an
16284 unparsed DEFAULT_ARG. Parse the default args now. This function
16285 assumes that the current scope is the scope in which the default
16286 argument should be processed. */
16287
16288 static void
16289 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16290 {
16291 bool saved_local_variables_forbidden_p;
16292 tree parm;
16293
16294 /* While we're parsing the default args, we might (due to the
16295 statement expression extension) encounter more classes. We want
16296 to handle them right away, but we don't want them getting mixed
16297 up with default args that are currently in the queue. */
16298 parser->unparsed_functions_queues
16299 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16300
16301 /* Local variable names (and the `this' keyword) may not appear
16302 in a default argument. */
16303 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16304 parser->local_variables_forbidden_p = true;
16305
16306 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16307 parm;
16308 parm = TREE_CHAIN (parm))
16309 {
16310 cp_token_cache *tokens;
16311 tree default_arg = TREE_PURPOSE (parm);
16312 tree parsed_arg;
16313 VEC(tree,gc) *insts;
16314 tree copy;
16315 unsigned ix;
16316
16317 if (!default_arg)
16318 continue;
16319
16320 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16321 /* This can happen for a friend declaration for a function
16322 already declared with default arguments. */
16323 continue;
16324
16325 /* Push the saved tokens for the default argument onto the parser's
16326 lexer stack. */
16327 tokens = DEFARG_TOKENS (default_arg);
16328 cp_parser_push_lexer_for_tokens (parser, tokens);
16329
16330 /* Parse the assignment-expression. */
16331 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16332
16333 if (!processing_template_decl)
16334 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16335
16336 TREE_PURPOSE (parm) = parsed_arg;
16337
16338 /* Update any instantiations we've already created. */
16339 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16340 VEC_iterate (tree, insts, ix, copy); ix++)
16341 TREE_PURPOSE (copy) = parsed_arg;
16342
16343 /* If the token stream has not been completely used up, then
16344 there was extra junk after the end of the default
16345 argument. */
16346 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16347 cp_parser_error (parser, "expected %<,%>");
16348
16349 /* Revert to the main lexer. */
16350 cp_parser_pop_lexer (parser);
16351 }
16352
16353 /* Make sure no default arg is missing. */
16354 check_default_args (fn);
16355
16356 /* Restore the state of local_variables_forbidden_p. */
16357 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16358
16359 /* Restore the queue. */
16360 parser->unparsed_functions_queues
16361 = TREE_CHAIN (parser->unparsed_functions_queues);
16362 }
16363
16364 /* Parse the operand of `sizeof' (or a similar operator). Returns
16365 either a TYPE or an expression, depending on the form of the
16366 input. The KEYWORD indicates which kind of expression we have
16367 encountered. */
16368
16369 static tree
16370 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16371 {
16372 static const char *format;
16373 tree expr = NULL_TREE;
16374 const char *saved_message;
16375 bool saved_integral_constant_expression_p;
16376 bool saved_non_integral_constant_expression_p;
16377
16378 /* Initialize FORMAT the first time we get here. */
16379 if (!format)
16380 format = "types may not be defined in '%s' expressions";
16381
16382 /* Types cannot be defined in a `sizeof' expression. Save away the
16383 old message. */
16384 saved_message = parser->type_definition_forbidden_message;
16385 /* And create the new one. */
16386 parser->type_definition_forbidden_message
16387 = XNEWVEC (const char, strlen (format)
16388 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16389 + 1 /* `\0' */);
16390 sprintf ((char *) parser->type_definition_forbidden_message,
16391 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16392
16393 /* The restrictions on constant-expressions do not apply inside
16394 sizeof expressions. */
16395 saved_integral_constant_expression_p
16396 = parser->integral_constant_expression_p;
16397 saved_non_integral_constant_expression_p
16398 = parser->non_integral_constant_expression_p;
16399 parser->integral_constant_expression_p = false;
16400
16401 /* Do not actually evaluate the expression. */
16402 ++skip_evaluation;
16403 /* If it's a `(', then we might be looking at the type-id
16404 construction. */
16405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16406 {
16407 tree type;
16408 bool saved_in_type_id_in_expr_p;
16409
16410 /* We can't be sure yet whether we're looking at a type-id or an
16411 expression. */
16412 cp_parser_parse_tentatively (parser);
16413 /* Consume the `('. */
16414 cp_lexer_consume_token (parser->lexer);
16415 /* Parse the type-id. */
16416 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16417 parser->in_type_id_in_expr_p = true;
16418 type = cp_parser_type_id (parser);
16419 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16420 /* Now, look for the trailing `)'. */
16421 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16422 /* If all went well, then we're done. */
16423 if (cp_parser_parse_definitely (parser))
16424 {
16425 cp_decl_specifier_seq decl_specs;
16426
16427 /* Build a trivial decl-specifier-seq. */
16428 clear_decl_specs (&decl_specs);
16429 decl_specs.type = type;
16430
16431 /* Call grokdeclarator to figure out what type this is. */
16432 expr = grokdeclarator (NULL,
16433 &decl_specs,
16434 TYPENAME,
16435 /*initialized=*/0,
16436 /*attrlist=*/NULL);
16437 }
16438 }
16439
16440 /* If the type-id production did not work out, then we must be
16441 looking at the unary-expression production. */
16442 if (!expr)
16443 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16444 /*cast_p=*/false);
16445 /* Go back to evaluating expressions. */
16446 --skip_evaluation;
16447
16448 /* Free the message we created. */
16449 free ((char *) parser->type_definition_forbidden_message);
16450 /* And restore the old one. */
16451 parser->type_definition_forbidden_message = saved_message;
16452 parser->integral_constant_expression_p
16453 = saved_integral_constant_expression_p;
16454 parser->non_integral_constant_expression_p
16455 = saved_non_integral_constant_expression_p;
16456
16457 return expr;
16458 }
16459
16460 /* If the current declaration has no declarator, return true. */
16461
16462 static bool
16463 cp_parser_declares_only_class_p (cp_parser *parser)
16464 {
16465 /* If the next token is a `;' or a `,' then there is no
16466 declarator. */
16467 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16468 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16469 }
16470
16471 /* Update the DECL_SPECS to reflect the storage class indicated by
16472 KEYWORD. */
16473
16474 static void
16475 cp_parser_set_storage_class (cp_parser *parser,
16476 cp_decl_specifier_seq *decl_specs,
16477 enum rid keyword)
16478 {
16479 cp_storage_class storage_class;
16480
16481 if (parser->in_unbraced_linkage_specification_p)
16482 {
16483 error ("invalid use of %qD in linkage specification",
16484 ridpointers[keyword]);
16485 return;
16486 }
16487 else if (decl_specs->storage_class != sc_none)
16488 {
16489 decl_specs->conflicting_specifiers_p = true;
16490 return;
16491 }
16492
16493 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16494 && decl_specs->specs[(int) ds_thread])
16495 {
16496 error ("%<__thread%> before %qD", ridpointers[keyword]);
16497 decl_specs->specs[(int) ds_thread] = 0;
16498 }
16499
16500 switch (keyword)
16501 {
16502 case RID_AUTO:
16503 storage_class = sc_auto;
16504 break;
16505 case RID_REGISTER:
16506 storage_class = sc_register;
16507 break;
16508 case RID_STATIC:
16509 storage_class = sc_static;
16510 break;
16511 case RID_EXTERN:
16512 storage_class = sc_extern;
16513 break;
16514 case RID_MUTABLE:
16515 storage_class = sc_mutable;
16516 break;
16517 default:
16518 gcc_unreachable ();
16519 }
16520 decl_specs->storage_class = storage_class;
16521
16522 /* A storage class specifier cannot be applied alongside a typedef
16523 specifier. If there is a typedef specifier present then set
16524 conflicting_specifiers_p which will trigger an error later
16525 on in grokdeclarator. */
16526 if (decl_specs->specs[(int)ds_typedef])
16527 decl_specs->conflicting_specifiers_p = true;
16528 }
16529
16530 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16531 is true, the type is a user-defined type; otherwise it is a
16532 built-in type specified by a keyword. */
16533
16534 static void
16535 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16536 tree type_spec,
16537 bool user_defined_p)
16538 {
16539 decl_specs->any_specifiers_p = true;
16540
16541 /* If the user tries to redeclare bool or wchar_t (with, for
16542 example, in "typedef int wchar_t;") we remember that this is what
16543 happened. In system headers, we ignore these declarations so
16544 that G++ can work with system headers that are not C++-safe. */
16545 if (decl_specs->specs[(int) ds_typedef]
16546 && !user_defined_p
16547 && (type_spec == boolean_type_node
16548 || type_spec == wchar_type_node)
16549 && (decl_specs->type
16550 || decl_specs->specs[(int) ds_long]
16551 || decl_specs->specs[(int) ds_short]
16552 || decl_specs->specs[(int) ds_unsigned]
16553 || decl_specs->specs[(int) ds_signed]))
16554 {
16555 decl_specs->redefined_builtin_type = type_spec;
16556 if (!decl_specs->type)
16557 {
16558 decl_specs->type = type_spec;
16559 decl_specs->user_defined_type_p = false;
16560 }
16561 }
16562 else if (decl_specs->type)
16563 decl_specs->multiple_types_p = true;
16564 else
16565 {
16566 decl_specs->type = type_spec;
16567 decl_specs->user_defined_type_p = user_defined_p;
16568 decl_specs->redefined_builtin_type = NULL_TREE;
16569 }
16570 }
16571
16572 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16573 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16574
16575 static bool
16576 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16577 {
16578 return decl_specifiers->specs[(int) ds_friend] != 0;
16579 }
16580
16581 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16582 issue an error message indicating that TOKEN_DESC was expected.
16583
16584 Returns the token consumed, if the token had the appropriate type.
16585 Otherwise, returns NULL. */
16586
16587 static cp_token *
16588 cp_parser_require (cp_parser* parser,
16589 enum cpp_ttype type,
16590 const char* token_desc)
16591 {
16592 if (cp_lexer_next_token_is (parser->lexer, type))
16593 return cp_lexer_consume_token (parser->lexer);
16594 else
16595 {
16596 /* Output the MESSAGE -- unless we're parsing tentatively. */
16597 if (!cp_parser_simulate_error (parser))
16598 {
16599 char *message = concat ("expected ", token_desc, NULL);
16600 cp_parser_error (parser, message);
16601 free (message);
16602 }
16603 return NULL;
16604 }
16605 }
16606
16607 /* An error message is produced if the next token is not '>'.
16608 All further tokens are skipped until the desired token is
16609 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16610
16611 static void
16612 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16613 {
16614 /* Current level of '< ... >'. */
16615 unsigned level = 0;
16616 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16617 unsigned nesting_depth = 0;
16618
16619 /* Are we ready, yet? If not, issue error message. */
16620 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16621 return;
16622
16623 /* Skip tokens until the desired token is found. */
16624 while (true)
16625 {
16626 /* Peek at the next token. */
16627 switch (cp_lexer_peek_token (parser->lexer)->type)
16628 {
16629 case CPP_LESS:
16630 if (!nesting_depth)
16631 ++level;
16632 break;
16633
16634 case CPP_GREATER:
16635 if (!nesting_depth && level-- == 0)
16636 {
16637 /* We've reached the token we want, consume it and stop. */
16638 cp_lexer_consume_token (parser->lexer);
16639 return;
16640 }
16641 break;
16642
16643 case CPP_OPEN_PAREN:
16644 case CPP_OPEN_SQUARE:
16645 ++nesting_depth;
16646 break;
16647
16648 case CPP_CLOSE_PAREN:
16649 case CPP_CLOSE_SQUARE:
16650 if (nesting_depth-- == 0)
16651 return;
16652 break;
16653
16654 case CPP_EOF:
16655 case CPP_PRAGMA_EOL:
16656 case CPP_SEMICOLON:
16657 case CPP_OPEN_BRACE:
16658 case CPP_CLOSE_BRACE:
16659 /* The '>' was probably forgotten, don't look further. */
16660 return;
16661
16662 default:
16663 break;
16664 }
16665
16666 /* Consume this token. */
16667 cp_lexer_consume_token (parser->lexer);
16668 }
16669 }
16670
16671 /* If the next token is the indicated keyword, consume it. Otherwise,
16672 issue an error message indicating that TOKEN_DESC was expected.
16673
16674 Returns the token consumed, if the token had the appropriate type.
16675 Otherwise, returns NULL. */
16676
16677 static cp_token *
16678 cp_parser_require_keyword (cp_parser* parser,
16679 enum rid keyword,
16680 const char* token_desc)
16681 {
16682 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16683
16684 if (token && token->keyword != keyword)
16685 {
16686 dyn_string_t error_msg;
16687
16688 /* Format the error message. */
16689 error_msg = dyn_string_new (0);
16690 dyn_string_append_cstr (error_msg, "expected ");
16691 dyn_string_append_cstr (error_msg, token_desc);
16692 cp_parser_error (parser, error_msg->s);
16693 dyn_string_delete (error_msg);
16694 return NULL;
16695 }
16696
16697 return token;
16698 }
16699
16700 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16701 function-definition. */
16702
16703 static bool
16704 cp_parser_token_starts_function_definition_p (cp_token* token)
16705 {
16706 return (/* An ordinary function-body begins with an `{'. */
16707 token->type == CPP_OPEN_BRACE
16708 /* A ctor-initializer begins with a `:'. */
16709 || token->type == CPP_COLON
16710 /* A function-try-block begins with `try'. */
16711 || token->keyword == RID_TRY
16712 /* The named return value extension begins with `return'. */
16713 || token->keyword == RID_RETURN);
16714 }
16715
16716 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16717 definition. */
16718
16719 static bool
16720 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16721 {
16722 cp_token *token;
16723
16724 token = cp_lexer_peek_token (parser->lexer);
16725 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16726 }
16727
16728 /* Returns TRUE iff the next token is the "," or ">" ending a
16729 template-argument. */
16730
16731 static bool
16732 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16733 {
16734 cp_token *token;
16735
16736 token = cp_lexer_peek_token (parser->lexer);
16737 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16738 }
16739
16740 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16741 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16742
16743 static bool
16744 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16745 size_t n)
16746 {
16747 cp_token *token;
16748
16749 token = cp_lexer_peek_nth_token (parser->lexer, n);
16750 if (token->type == CPP_LESS)
16751 return true;
16752 /* Check for the sequence `<::' in the original code. It would be lexed as
16753 `[:', where `[' is a digraph, and there is no whitespace before
16754 `:'. */
16755 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16756 {
16757 cp_token *token2;
16758 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16759 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16760 return true;
16761 }
16762 return false;
16763 }
16764
16765 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16766 or none_type otherwise. */
16767
16768 static enum tag_types
16769 cp_parser_token_is_class_key (cp_token* token)
16770 {
16771 switch (token->keyword)
16772 {
16773 case RID_CLASS:
16774 return class_type;
16775 case RID_STRUCT:
16776 return record_type;
16777 case RID_UNION:
16778 return union_type;
16779
16780 default:
16781 return none_type;
16782 }
16783 }
16784
16785 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16786
16787 static void
16788 cp_parser_check_class_key (enum tag_types class_key, tree type)
16789 {
16790 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16791 pedwarn ("%qs tag used in naming %q#T",
16792 class_key == union_type ? "union"
16793 : class_key == record_type ? "struct" : "class",
16794 type);
16795 }
16796
16797 /* Issue an error message if DECL is redeclared with different
16798 access than its original declaration [class.access.spec/3].
16799 This applies to nested classes and nested class templates.
16800 [class.mem/1]. */
16801
16802 static void
16803 cp_parser_check_access_in_redeclaration (tree decl)
16804 {
16805 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16806 return;
16807
16808 if ((TREE_PRIVATE (decl)
16809 != (current_access_specifier == access_private_node))
16810 || (TREE_PROTECTED (decl)
16811 != (current_access_specifier == access_protected_node)))
16812 error ("%qD redeclared with different access", decl);
16813 }
16814
16815 /* Look for the `template' keyword, as a syntactic disambiguator.
16816 Return TRUE iff it is present, in which case it will be
16817 consumed. */
16818
16819 static bool
16820 cp_parser_optional_template_keyword (cp_parser *parser)
16821 {
16822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16823 {
16824 /* The `template' keyword can only be used within templates;
16825 outside templates the parser can always figure out what is a
16826 template and what is not. */
16827 if (!processing_template_decl)
16828 {
16829 error ("%<template%> (as a disambiguator) is only allowed "
16830 "within templates");
16831 /* If this part of the token stream is rescanned, the same
16832 error message would be generated. So, we purge the token
16833 from the stream. */
16834 cp_lexer_purge_token (parser->lexer);
16835 return false;
16836 }
16837 else
16838 {
16839 /* Consume the `template' keyword. */
16840 cp_lexer_consume_token (parser->lexer);
16841 return true;
16842 }
16843 }
16844
16845 return false;
16846 }
16847
16848 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16849 set PARSER->SCOPE, and perform other related actions. */
16850
16851 static void
16852 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16853 {
16854 tree value;
16855 tree check;
16856
16857 /* Get the stored value. */
16858 value = cp_lexer_consume_token (parser->lexer)->value;
16859 /* Perform any access checks that were deferred. */
16860 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16861 perform_or_defer_access_check (TREE_PURPOSE (check),
16862 TREE_VALUE (check),
16863 TREE_VALUE (check));
16864 /* Set the scope from the stored value. */
16865 parser->scope = TREE_VALUE (value);
16866 parser->qualifying_scope = TREE_TYPE (value);
16867 parser->object_scope = NULL_TREE;
16868 }
16869
16870 /* Consume tokens up through a non-nested END token. */
16871
16872 static void
16873 cp_parser_cache_group (cp_parser *parser,
16874 enum cpp_ttype end,
16875 unsigned depth)
16876 {
16877 while (true)
16878 {
16879 cp_token *token;
16880
16881 /* Abort a parenthesized expression if we encounter a brace. */
16882 if ((end == CPP_CLOSE_PAREN || depth == 0)
16883 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16884 return;
16885 /* If we've reached the end of the file, stop. */
16886 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16887 || (end != CPP_PRAGMA_EOL
16888 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16889 return;
16890 /* Consume the next token. */
16891 token = cp_lexer_consume_token (parser->lexer);
16892 /* See if it starts a new group. */
16893 if (token->type == CPP_OPEN_BRACE)
16894 {
16895 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16896 if (depth == 0)
16897 return;
16898 }
16899 else if (token->type == CPP_OPEN_PAREN)
16900 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16901 else if (token->type == CPP_PRAGMA)
16902 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16903 else if (token->type == end)
16904 return;
16905 }
16906 }
16907
16908 /* Begin parsing tentatively. We always save tokens while parsing
16909 tentatively so that if the tentative parsing fails we can restore the
16910 tokens. */
16911
16912 static void
16913 cp_parser_parse_tentatively (cp_parser* parser)
16914 {
16915 /* Enter a new parsing context. */
16916 parser->context = cp_parser_context_new (parser->context);
16917 /* Begin saving tokens. */
16918 cp_lexer_save_tokens (parser->lexer);
16919 /* In order to avoid repetitive access control error messages,
16920 access checks are queued up until we are no longer parsing
16921 tentatively. */
16922 push_deferring_access_checks (dk_deferred);
16923 }
16924
16925 /* Commit to the currently active tentative parse. */
16926
16927 static void
16928 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16929 {
16930 cp_parser_context *context;
16931 cp_lexer *lexer;
16932
16933 /* Mark all of the levels as committed. */
16934 lexer = parser->lexer;
16935 for (context = parser->context; context->next; context = context->next)
16936 {
16937 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16938 break;
16939 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16940 while (!cp_lexer_saving_tokens (lexer))
16941 lexer = lexer->next;
16942 cp_lexer_commit_tokens (lexer);
16943 }
16944 }
16945
16946 /* Abort the currently active tentative parse. All consumed tokens
16947 will be rolled back, and no diagnostics will be issued. */
16948
16949 static void
16950 cp_parser_abort_tentative_parse (cp_parser* parser)
16951 {
16952 cp_parser_simulate_error (parser);
16953 /* Now, pretend that we want to see if the construct was
16954 successfully parsed. */
16955 cp_parser_parse_definitely (parser);
16956 }
16957
16958 /* Stop parsing tentatively. If a parse error has occurred, restore the
16959 token stream. Otherwise, commit to the tokens we have consumed.
16960 Returns true if no error occurred; false otherwise. */
16961
16962 static bool
16963 cp_parser_parse_definitely (cp_parser* parser)
16964 {
16965 bool error_occurred;
16966 cp_parser_context *context;
16967
16968 /* Remember whether or not an error occurred, since we are about to
16969 destroy that information. */
16970 error_occurred = cp_parser_error_occurred (parser);
16971 /* Remove the topmost context from the stack. */
16972 context = parser->context;
16973 parser->context = context->next;
16974 /* If no parse errors occurred, commit to the tentative parse. */
16975 if (!error_occurred)
16976 {
16977 /* Commit to the tokens read tentatively, unless that was
16978 already done. */
16979 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16980 cp_lexer_commit_tokens (parser->lexer);
16981
16982 pop_to_parent_deferring_access_checks ();
16983 }
16984 /* Otherwise, if errors occurred, roll back our state so that things
16985 are just as they were before we began the tentative parse. */
16986 else
16987 {
16988 cp_lexer_rollback_tokens (parser->lexer);
16989 pop_deferring_access_checks ();
16990 }
16991 /* Add the context to the front of the free list. */
16992 context->next = cp_parser_context_free_list;
16993 cp_parser_context_free_list = context;
16994
16995 return !error_occurred;
16996 }
16997
16998 /* Returns true if we are parsing tentatively and are not committed to
16999 this tentative parse. */
17000
17001 static bool
17002 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17003 {
17004 return (cp_parser_parsing_tentatively (parser)
17005 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17006 }
17007
17008 /* Returns nonzero iff an error has occurred during the most recent
17009 tentative parse. */
17010
17011 static bool
17012 cp_parser_error_occurred (cp_parser* parser)
17013 {
17014 return (cp_parser_parsing_tentatively (parser)
17015 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17016 }
17017
17018 /* Returns nonzero if GNU extensions are allowed. */
17019
17020 static bool
17021 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17022 {
17023 return parser->allow_gnu_extensions_p;
17024 }
17025 \f
17026 /* Objective-C++ Productions */
17027
17028
17029 /* Parse an Objective-C expression, which feeds into a primary-expression
17030 above.
17031
17032 objc-expression:
17033 objc-message-expression
17034 objc-string-literal
17035 objc-encode-expression
17036 objc-protocol-expression
17037 objc-selector-expression
17038
17039 Returns a tree representation of the expression. */
17040
17041 static tree
17042 cp_parser_objc_expression (cp_parser* parser)
17043 {
17044 /* Try to figure out what kind of declaration is present. */
17045 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17046
17047 switch (kwd->type)
17048 {
17049 case CPP_OPEN_SQUARE:
17050 return cp_parser_objc_message_expression (parser);
17051
17052 case CPP_OBJC_STRING:
17053 kwd = cp_lexer_consume_token (parser->lexer);
17054 return objc_build_string_object (kwd->value);
17055
17056 case CPP_KEYWORD:
17057 switch (kwd->keyword)
17058 {
17059 case RID_AT_ENCODE:
17060 return cp_parser_objc_encode_expression (parser);
17061
17062 case RID_AT_PROTOCOL:
17063 return cp_parser_objc_protocol_expression (parser);
17064
17065 case RID_AT_SELECTOR:
17066 return cp_parser_objc_selector_expression (parser);
17067
17068 default:
17069 break;
17070 }
17071 default:
17072 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17073 cp_parser_skip_to_end_of_block_or_statement (parser);
17074 }
17075
17076 return error_mark_node;
17077 }
17078
17079 /* Parse an Objective-C message expression.
17080
17081 objc-message-expression:
17082 [ objc-message-receiver objc-message-args ]
17083
17084 Returns a representation of an Objective-C message. */
17085
17086 static tree
17087 cp_parser_objc_message_expression (cp_parser* parser)
17088 {
17089 tree receiver, messageargs;
17090
17091 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17092 receiver = cp_parser_objc_message_receiver (parser);
17093 messageargs = cp_parser_objc_message_args (parser);
17094 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17095
17096 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17097 }
17098
17099 /* Parse an objc-message-receiver.
17100
17101 objc-message-receiver:
17102 expression
17103 simple-type-specifier
17104
17105 Returns a representation of the type or expression. */
17106
17107 static tree
17108 cp_parser_objc_message_receiver (cp_parser* parser)
17109 {
17110 tree rcv;
17111
17112 /* An Objective-C message receiver may be either (1) a type
17113 or (2) an expression. */
17114 cp_parser_parse_tentatively (parser);
17115 rcv = cp_parser_expression (parser, false);
17116
17117 if (cp_parser_parse_definitely (parser))
17118 return rcv;
17119
17120 rcv = cp_parser_simple_type_specifier (parser,
17121 /*decl_specs=*/NULL,
17122 CP_PARSER_FLAGS_NONE);
17123
17124 return objc_get_class_reference (rcv);
17125 }
17126
17127 /* Parse the arguments and selectors comprising an Objective-C message.
17128
17129 objc-message-args:
17130 objc-selector
17131 objc-selector-args
17132 objc-selector-args , objc-comma-args
17133
17134 objc-selector-args:
17135 objc-selector [opt] : assignment-expression
17136 objc-selector-args objc-selector [opt] : assignment-expression
17137
17138 objc-comma-args:
17139 assignment-expression
17140 objc-comma-args , assignment-expression
17141
17142 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17143 selector arguments and TREE_VALUE containing a list of comma
17144 arguments. */
17145
17146 static tree
17147 cp_parser_objc_message_args (cp_parser* parser)
17148 {
17149 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17150 bool maybe_unary_selector_p = true;
17151 cp_token *token = cp_lexer_peek_token (parser->lexer);
17152
17153 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17154 {
17155 tree selector = NULL_TREE, arg;
17156
17157 if (token->type != CPP_COLON)
17158 selector = cp_parser_objc_selector (parser);
17159
17160 /* Detect if we have a unary selector. */
17161 if (maybe_unary_selector_p
17162 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17163 return build_tree_list (selector, NULL_TREE);
17164
17165 maybe_unary_selector_p = false;
17166 cp_parser_require (parser, CPP_COLON, "`:'");
17167 arg = cp_parser_assignment_expression (parser, false);
17168
17169 sel_args
17170 = chainon (sel_args,
17171 build_tree_list (selector, arg));
17172
17173 token = cp_lexer_peek_token (parser->lexer);
17174 }
17175
17176 /* Handle non-selector arguments, if any. */
17177 while (token->type == CPP_COMMA)
17178 {
17179 tree arg;
17180
17181 cp_lexer_consume_token (parser->lexer);
17182 arg = cp_parser_assignment_expression (parser, false);
17183
17184 addl_args
17185 = chainon (addl_args,
17186 build_tree_list (NULL_TREE, arg));
17187
17188 token = cp_lexer_peek_token (parser->lexer);
17189 }
17190
17191 return build_tree_list (sel_args, addl_args);
17192 }
17193
17194 /* Parse an Objective-C encode expression.
17195
17196 objc-encode-expression:
17197 @encode objc-typename
17198
17199 Returns an encoded representation of the type argument. */
17200
17201 static tree
17202 cp_parser_objc_encode_expression (cp_parser* parser)
17203 {
17204 tree type;
17205
17206 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17207 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17208 type = complete_type (cp_parser_type_id (parser));
17209 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17210
17211 if (!type)
17212 {
17213 error ("%<@encode%> must specify a type as an argument");
17214 return error_mark_node;
17215 }
17216
17217 return objc_build_encode_expr (type);
17218 }
17219
17220 /* Parse an Objective-C @defs expression. */
17221
17222 static tree
17223 cp_parser_objc_defs_expression (cp_parser *parser)
17224 {
17225 tree name;
17226
17227 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17228 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17229 name = cp_parser_identifier (parser);
17230 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17231
17232 return objc_get_class_ivars (name);
17233 }
17234
17235 /* Parse an Objective-C protocol expression.
17236
17237 objc-protocol-expression:
17238 @protocol ( identifier )
17239
17240 Returns a representation of the protocol expression. */
17241
17242 static tree
17243 cp_parser_objc_protocol_expression (cp_parser* parser)
17244 {
17245 tree proto;
17246
17247 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17248 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17249 proto = cp_parser_identifier (parser);
17250 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17251
17252 return objc_build_protocol_expr (proto);
17253 }
17254
17255 /* Parse an Objective-C selector expression.
17256
17257 objc-selector-expression:
17258 @selector ( objc-method-signature )
17259
17260 objc-method-signature:
17261 objc-selector
17262 objc-selector-seq
17263
17264 objc-selector-seq:
17265 objc-selector :
17266 objc-selector-seq objc-selector :
17267
17268 Returns a representation of the method selector. */
17269
17270 static tree
17271 cp_parser_objc_selector_expression (cp_parser* parser)
17272 {
17273 tree sel_seq = NULL_TREE;
17274 bool maybe_unary_selector_p = true;
17275 cp_token *token;
17276
17277 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17278 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17279 token = cp_lexer_peek_token (parser->lexer);
17280
17281 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17282 || token->type == CPP_SCOPE)
17283 {
17284 tree selector = NULL_TREE;
17285
17286 if (token->type != CPP_COLON
17287 || token->type == CPP_SCOPE)
17288 selector = cp_parser_objc_selector (parser);
17289
17290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17291 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17292 {
17293 /* Detect if we have a unary selector. */
17294 if (maybe_unary_selector_p)
17295 {
17296 sel_seq = selector;
17297 goto finish_selector;
17298 }
17299 else
17300 {
17301 cp_parser_error (parser, "expected %<:%>");
17302 }
17303 }
17304 maybe_unary_selector_p = false;
17305 token = cp_lexer_consume_token (parser->lexer);
17306
17307 if (token->type == CPP_SCOPE)
17308 {
17309 sel_seq
17310 = chainon (sel_seq,
17311 build_tree_list (selector, NULL_TREE));
17312 sel_seq
17313 = chainon (sel_seq,
17314 build_tree_list (NULL_TREE, NULL_TREE));
17315 }
17316 else
17317 sel_seq
17318 = chainon (sel_seq,
17319 build_tree_list (selector, NULL_TREE));
17320
17321 token = cp_lexer_peek_token (parser->lexer);
17322 }
17323
17324 finish_selector:
17325 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17326
17327 return objc_build_selector_expr (sel_seq);
17328 }
17329
17330 /* Parse a list of identifiers.
17331
17332 objc-identifier-list:
17333 identifier
17334 objc-identifier-list , identifier
17335
17336 Returns a TREE_LIST of identifier nodes. */
17337
17338 static tree
17339 cp_parser_objc_identifier_list (cp_parser* parser)
17340 {
17341 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17342 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17343
17344 while (sep->type == CPP_COMMA)
17345 {
17346 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17347 list = chainon (list,
17348 build_tree_list (NULL_TREE,
17349 cp_parser_identifier (parser)));
17350 sep = cp_lexer_peek_token (parser->lexer);
17351 }
17352
17353 return list;
17354 }
17355
17356 /* Parse an Objective-C alias declaration.
17357
17358 objc-alias-declaration:
17359 @compatibility_alias identifier identifier ;
17360
17361 This function registers the alias mapping with the Objective-C front-end.
17362 It returns nothing. */
17363
17364 static void
17365 cp_parser_objc_alias_declaration (cp_parser* parser)
17366 {
17367 tree alias, orig;
17368
17369 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17370 alias = cp_parser_identifier (parser);
17371 orig = cp_parser_identifier (parser);
17372 objc_declare_alias (alias, orig);
17373 cp_parser_consume_semicolon_at_end_of_statement (parser);
17374 }
17375
17376 /* Parse an Objective-C class forward-declaration.
17377
17378 objc-class-declaration:
17379 @class objc-identifier-list ;
17380
17381 The function registers the forward declarations with the Objective-C
17382 front-end. It returns nothing. */
17383
17384 static void
17385 cp_parser_objc_class_declaration (cp_parser* parser)
17386 {
17387 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17388 objc_declare_class (cp_parser_objc_identifier_list (parser));
17389 cp_parser_consume_semicolon_at_end_of_statement (parser);
17390 }
17391
17392 /* Parse a list of Objective-C protocol references.
17393
17394 objc-protocol-refs-opt:
17395 objc-protocol-refs [opt]
17396
17397 objc-protocol-refs:
17398 < objc-identifier-list >
17399
17400 Returns a TREE_LIST of identifiers, if any. */
17401
17402 static tree
17403 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17404 {
17405 tree protorefs = NULL_TREE;
17406
17407 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17408 {
17409 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17410 protorefs = cp_parser_objc_identifier_list (parser);
17411 cp_parser_require (parser, CPP_GREATER, "`>'");
17412 }
17413
17414 return protorefs;
17415 }
17416
17417 /* Parse a Objective-C visibility specification. */
17418
17419 static void
17420 cp_parser_objc_visibility_spec (cp_parser* parser)
17421 {
17422 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17423
17424 switch (vis->keyword)
17425 {
17426 case RID_AT_PRIVATE:
17427 objc_set_visibility (2);
17428 break;
17429 case RID_AT_PROTECTED:
17430 objc_set_visibility (0);
17431 break;
17432 case RID_AT_PUBLIC:
17433 objc_set_visibility (1);
17434 break;
17435 default:
17436 return;
17437 }
17438
17439 /* Eat '@private'/'@protected'/'@public'. */
17440 cp_lexer_consume_token (parser->lexer);
17441 }
17442
17443 /* Parse an Objective-C method type. */
17444
17445 static void
17446 cp_parser_objc_method_type (cp_parser* parser)
17447 {
17448 objc_set_method_type
17449 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17450 ? PLUS_EXPR
17451 : MINUS_EXPR);
17452 }
17453
17454 /* Parse an Objective-C protocol qualifier. */
17455
17456 static tree
17457 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17458 {
17459 tree quals = NULL_TREE, node;
17460 cp_token *token = cp_lexer_peek_token (parser->lexer);
17461
17462 node = token->value;
17463
17464 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17465 && (node == ridpointers [(int) RID_IN]
17466 || node == ridpointers [(int) RID_OUT]
17467 || node == ridpointers [(int) RID_INOUT]
17468 || node == ridpointers [(int) RID_BYCOPY]
17469 || node == ridpointers [(int) RID_BYREF]
17470 || node == ridpointers [(int) RID_ONEWAY]))
17471 {
17472 quals = tree_cons (NULL_TREE, node, quals);
17473 cp_lexer_consume_token (parser->lexer);
17474 token = cp_lexer_peek_token (parser->lexer);
17475 node = token->value;
17476 }
17477
17478 return quals;
17479 }
17480
17481 /* Parse an Objective-C typename. */
17482
17483 static tree
17484 cp_parser_objc_typename (cp_parser* parser)
17485 {
17486 tree typename = NULL_TREE;
17487
17488 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17489 {
17490 tree proto_quals, cp_type = NULL_TREE;
17491
17492 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17493 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17494
17495 /* An ObjC type name may consist of just protocol qualifiers, in which
17496 case the type shall default to 'id'. */
17497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17498 cp_type = cp_parser_type_id (parser);
17499
17500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17501 typename = build_tree_list (proto_quals, cp_type);
17502 }
17503
17504 return typename;
17505 }
17506
17507 /* Check to see if TYPE refers to an Objective-C selector name. */
17508
17509 static bool
17510 cp_parser_objc_selector_p (enum cpp_ttype type)
17511 {
17512 return (type == CPP_NAME || type == CPP_KEYWORD
17513 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17514 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17515 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17516 || type == CPP_XOR || type == CPP_XOR_EQ);
17517 }
17518
17519 /* Parse an Objective-C selector. */
17520
17521 static tree
17522 cp_parser_objc_selector (cp_parser* parser)
17523 {
17524 cp_token *token = cp_lexer_consume_token (parser->lexer);
17525
17526 if (!cp_parser_objc_selector_p (token->type))
17527 {
17528 error ("invalid Objective-C++ selector name");
17529 return error_mark_node;
17530 }
17531
17532 /* C++ operator names are allowed to appear in ObjC selectors. */
17533 switch (token->type)
17534 {
17535 case CPP_AND_AND: return get_identifier ("and");
17536 case CPP_AND_EQ: return get_identifier ("and_eq");
17537 case CPP_AND: return get_identifier ("bitand");
17538 case CPP_OR: return get_identifier ("bitor");
17539 case CPP_COMPL: return get_identifier ("compl");
17540 case CPP_NOT: return get_identifier ("not");
17541 case CPP_NOT_EQ: return get_identifier ("not_eq");
17542 case CPP_OR_OR: return get_identifier ("or");
17543 case CPP_OR_EQ: return get_identifier ("or_eq");
17544 case CPP_XOR: return get_identifier ("xor");
17545 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17546 default: return token->value;
17547 }
17548 }
17549
17550 /* Parse an Objective-C params list. */
17551
17552 static tree
17553 cp_parser_objc_method_keyword_params (cp_parser* parser)
17554 {
17555 tree params = NULL_TREE;
17556 bool maybe_unary_selector_p = true;
17557 cp_token *token = cp_lexer_peek_token (parser->lexer);
17558
17559 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17560 {
17561 tree selector = NULL_TREE, typename, identifier;
17562
17563 if (token->type != CPP_COLON)
17564 selector = cp_parser_objc_selector (parser);
17565
17566 /* Detect if we have a unary selector. */
17567 if (maybe_unary_selector_p
17568 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17569 return selector;
17570
17571 maybe_unary_selector_p = false;
17572 cp_parser_require (parser, CPP_COLON, "`:'");
17573 typename = cp_parser_objc_typename (parser);
17574 identifier = cp_parser_identifier (parser);
17575
17576 params
17577 = chainon (params,
17578 objc_build_keyword_decl (selector,
17579 typename,
17580 identifier));
17581
17582 token = cp_lexer_peek_token (parser->lexer);
17583 }
17584
17585 return params;
17586 }
17587
17588 /* Parse the non-keyword Objective-C params. */
17589
17590 static tree
17591 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17592 {
17593 tree params = make_node (TREE_LIST);
17594 cp_token *token = cp_lexer_peek_token (parser->lexer);
17595 *ellipsisp = false; /* Initially, assume no ellipsis. */
17596
17597 while (token->type == CPP_COMMA)
17598 {
17599 cp_parameter_declarator *parmdecl;
17600 tree parm;
17601
17602 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17603 token = cp_lexer_peek_token (parser->lexer);
17604
17605 if (token->type == CPP_ELLIPSIS)
17606 {
17607 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17608 *ellipsisp = true;
17609 break;
17610 }
17611
17612 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17613 parm = grokdeclarator (parmdecl->declarator,
17614 &parmdecl->decl_specifiers,
17615 PARM, /*initialized=*/0,
17616 /*attrlist=*/NULL);
17617
17618 chainon (params, build_tree_list (NULL_TREE, parm));
17619 token = cp_lexer_peek_token (parser->lexer);
17620 }
17621
17622 return params;
17623 }
17624
17625 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17626
17627 static void
17628 cp_parser_objc_interstitial_code (cp_parser* parser)
17629 {
17630 cp_token *token = cp_lexer_peek_token (parser->lexer);
17631
17632 /* If the next token is `extern' and the following token is a string
17633 literal, then we have a linkage specification. */
17634 if (token->keyword == RID_EXTERN
17635 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17636 cp_parser_linkage_specification (parser);
17637 /* Handle #pragma, if any. */
17638 else if (token->type == CPP_PRAGMA)
17639 cp_parser_pragma (parser, pragma_external);
17640 /* Allow stray semicolons. */
17641 else if (token->type == CPP_SEMICOLON)
17642 cp_lexer_consume_token (parser->lexer);
17643 /* Finally, try to parse a block-declaration, or a function-definition. */
17644 else
17645 cp_parser_block_declaration (parser, /*statement_p=*/false);
17646 }
17647
17648 /* Parse a method signature. */
17649
17650 static tree
17651 cp_parser_objc_method_signature (cp_parser* parser)
17652 {
17653 tree rettype, kwdparms, optparms;
17654 bool ellipsis = false;
17655
17656 cp_parser_objc_method_type (parser);
17657 rettype = cp_parser_objc_typename (parser);
17658 kwdparms = cp_parser_objc_method_keyword_params (parser);
17659 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17660
17661 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17662 }
17663
17664 /* Pars an Objective-C method prototype list. */
17665
17666 static void
17667 cp_parser_objc_method_prototype_list (cp_parser* parser)
17668 {
17669 cp_token *token = cp_lexer_peek_token (parser->lexer);
17670
17671 while (token->keyword != RID_AT_END)
17672 {
17673 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17674 {
17675 objc_add_method_declaration
17676 (cp_parser_objc_method_signature (parser));
17677 cp_parser_consume_semicolon_at_end_of_statement (parser);
17678 }
17679 else
17680 /* Allow for interspersed non-ObjC++ code. */
17681 cp_parser_objc_interstitial_code (parser);
17682
17683 token = cp_lexer_peek_token (parser->lexer);
17684 }
17685
17686 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17687 objc_finish_interface ();
17688 }
17689
17690 /* Parse an Objective-C method definition list. */
17691
17692 static void
17693 cp_parser_objc_method_definition_list (cp_parser* parser)
17694 {
17695 cp_token *token = cp_lexer_peek_token (parser->lexer);
17696
17697 while (token->keyword != RID_AT_END)
17698 {
17699 tree meth;
17700
17701 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17702 {
17703 push_deferring_access_checks (dk_deferred);
17704 objc_start_method_definition
17705 (cp_parser_objc_method_signature (parser));
17706
17707 /* For historical reasons, we accept an optional semicolon. */
17708 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17709 cp_lexer_consume_token (parser->lexer);
17710
17711 perform_deferred_access_checks ();
17712 stop_deferring_access_checks ();
17713 meth = cp_parser_function_definition_after_declarator (parser,
17714 false);
17715 pop_deferring_access_checks ();
17716 objc_finish_method_definition (meth);
17717 }
17718 else
17719 /* Allow for interspersed non-ObjC++ code. */
17720 cp_parser_objc_interstitial_code (parser);
17721
17722 token = cp_lexer_peek_token (parser->lexer);
17723 }
17724
17725 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17726 objc_finish_implementation ();
17727 }
17728
17729 /* Parse Objective-C ivars. */
17730
17731 static void
17732 cp_parser_objc_class_ivars (cp_parser* parser)
17733 {
17734 cp_token *token = cp_lexer_peek_token (parser->lexer);
17735
17736 if (token->type != CPP_OPEN_BRACE)
17737 return; /* No ivars specified. */
17738
17739 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17740 token = cp_lexer_peek_token (parser->lexer);
17741
17742 while (token->type != CPP_CLOSE_BRACE)
17743 {
17744 cp_decl_specifier_seq declspecs;
17745 int decl_class_or_enum_p;
17746 tree prefix_attributes;
17747
17748 cp_parser_objc_visibility_spec (parser);
17749
17750 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17751 break;
17752
17753 cp_parser_decl_specifier_seq (parser,
17754 CP_PARSER_FLAGS_OPTIONAL,
17755 &declspecs,
17756 &decl_class_or_enum_p);
17757 prefix_attributes = declspecs.attributes;
17758 declspecs.attributes = NULL_TREE;
17759
17760 /* Keep going until we hit the `;' at the end of the
17761 declaration. */
17762 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17763 {
17764 tree width = NULL_TREE, attributes, first_attribute, decl;
17765 cp_declarator *declarator = NULL;
17766 int ctor_dtor_or_conv_p;
17767
17768 /* Check for a (possibly unnamed) bitfield declaration. */
17769 token = cp_lexer_peek_token (parser->lexer);
17770 if (token->type == CPP_COLON)
17771 goto eat_colon;
17772
17773 if (token->type == CPP_NAME
17774 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17775 == CPP_COLON))
17776 {
17777 /* Get the name of the bitfield. */
17778 declarator = make_id_declarator (NULL_TREE,
17779 cp_parser_identifier (parser),
17780 sfk_none);
17781
17782 eat_colon:
17783 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17784 /* Get the width of the bitfield. */
17785 width
17786 = cp_parser_constant_expression (parser,
17787 /*allow_non_constant=*/false,
17788 NULL);
17789 }
17790 else
17791 {
17792 /* Parse the declarator. */
17793 declarator
17794 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17795 &ctor_dtor_or_conv_p,
17796 /*parenthesized_p=*/NULL,
17797 /*member_p=*/false);
17798 }
17799
17800 /* Look for attributes that apply to the ivar. */
17801 attributes = cp_parser_attributes_opt (parser);
17802 /* Remember which attributes are prefix attributes and
17803 which are not. */
17804 first_attribute = attributes;
17805 /* Combine the attributes. */
17806 attributes = chainon (prefix_attributes, attributes);
17807
17808 if (width)
17809 {
17810 /* Create the bitfield declaration. */
17811 decl = grokbitfield (declarator, &declspecs, width);
17812 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17813 }
17814 else
17815 decl = grokfield (declarator, &declspecs,
17816 NULL_TREE, /*init_const_expr_p=*/false,
17817 NULL_TREE, attributes);
17818
17819 /* Add the instance variable. */
17820 objc_add_instance_variable (decl);
17821
17822 /* Reset PREFIX_ATTRIBUTES. */
17823 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17824 attributes = TREE_CHAIN (attributes);
17825 if (attributes)
17826 TREE_CHAIN (attributes) = NULL_TREE;
17827
17828 token = cp_lexer_peek_token (parser->lexer);
17829
17830 if (token->type == CPP_COMMA)
17831 {
17832 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17833 continue;
17834 }
17835 break;
17836 }
17837
17838 cp_parser_consume_semicolon_at_end_of_statement (parser);
17839 token = cp_lexer_peek_token (parser->lexer);
17840 }
17841
17842 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17843 /* For historical reasons, we accept an optional semicolon. */
17844 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17845 cp_lexer_consume_token (parser->lexer);
17846 }
17847
17848 /* Parse an Objective-C protocol declaration. */
17849
17850 static void
17851 cp_parser_objc_protocol_declaration (cp_parser* parser)
17852 {
17853 tree proto, protorefs;
17854 cp_token *tok;
17855
17856 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17858 {
17859 error ("identifier expected after %<@protocol%>");
17860 goto finish;
17861 }
17862
17863 /* See if we have a forward declaration or a definition. */
17864 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17865
17866 /* Try a forward declaration first. */
17867 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17868 {
17869 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17870 finish:
17871 cp_parser_consume_semicolon_at_end_of_statement (parser);
17872 }
17873
17874 /* Ok, we got a full-fledged definition (or at least should). */
17875 else
17876 {
17877 proto = cp_parser_identifier (parser);
17878 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17879 objc_start_protocol (proto, protorefs);
17880 cp_parser_objc_method_prototype_list (parser);
17881 }
17882 }
17883
17884 /* Parse an Objective-C superclass or category. */
17885
17886 static void
17887 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17888 tree *categ)
17889 {
17890 cp_token *next = cp_lexer_peek_token (parser->lexer);
17891
17892 *super = *categ = NULL_TREE;
17893 if (next->type == CPP_COLON)
17894 {
17895 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17896 *super = cp_parser_identifier (parser);
17897 }
17898 else if (next->type == CPP_OPEN_PAREN)
17899 {
17900 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17901 *categ = cp_parser_identifier (parser);
17902 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17903 }
17904 }
17905
17906 /* Parse an Objective-C class interface. */
17907
17908 static void
17909 cp_parser_objc_class_interface (cp_parser* parser)
17910 {
17911 tree name, super, categ, protos;
17912
17913 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17914 name = cp_parser_identifier (parser);
17915 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17916 protos = cp_parser_objc_protocol_refs_opt (parser);
17917
17918 /* We have either a class or a category on our hands. */
17919 if (categ)
17920 objc_start_category_interface (name, categ, protos);
17921 else
17922 {
17923 objc_start_class_interface (name, super, protos);
17924 /* Handle instance variable declarations, if any. */
17925 cp_parser_objc_class_ivars (parser);
17926 objc_continue_interface ();
17927 }
17928
17929 cp_parser_objc_method_prototype_list (parser);
17930 }
17931
17932 /* Parse an Objective-C class implementation. */
17933
17934 static void
17935 cp_parser_objc_class_implementation (cp_parser* parser)
17936 {
17937 tree name, super, categ;
17938
17939 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17940 name = cp_parser_identifier (parser);
17941 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17942
17943 /* We have either a class or a category on our hands. */
17944 if (categ)
17945 objc_start_category_implementation (name, categ);
17946 else
17947 {
17948 objc_start_class_implementation (name, super);
17949 /* Handle instance variable declarations, if any. */
17950 cp_parser_objc_class_ivars (parser);
17951 objc_continue_implementation ();
17952 }
17953
17954 cp_parser_objc_method_definition_list (parser);
17955 }
17956
17957 /* Consume the @end token and finish off the implementation. */
17958
17959 static void
17960 cp_parser_objc_end_implementation (cp_parser* parser)
17961 {
17962 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17963 objc_finish_implementation ();
17964 }
17965
17966 /* Parse an Objective-C declaration. */
17967
17968 static void
17969 cp_parser_objc_declaration (cp_parser* parser)
17970 {
17971 /* Try to figure out what kind of declaration is present. */
17972 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17973
17974 switch (kwd->keyword)
17975 {
17976 case RID_AT_ALIAS:
17977 cp_parser_objc_alias_declaration (parser);
17978 break;
17979 case RID_AT_CLASS:
17980 cp_parser_objc_class_declaration (parser);
17981 break;
17982 case RID_AT_PROTOCOL:
17983 cp_parser_objc_protocol_declaration (parser);
17984 break;
17985 case RID_AT_INTERFACE:
17986 cp_parser_objc_class_interface (parser);
17987 break;
17988 case RID_AT_IMPLEMENTATION:
17989 cp_parser_objc_class_implementation (parser);
17990 break;
17991 case RID_AT_END:
17992 cp_parser_objc_end_implementation (parser);
17993 break;
17994 default:
17995 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17996 cp_parser_skip_to_end_of_block_or_statement (parser);
17997 }
17998 }
17999
18000 /* Parse an Objective-C try-catch-finally statement.
18001
18002 objc-try-catch-finally-stmt:
18003 @try compound-statement objc-catch-clause-seq [opt]
18004 objc-finally-clause [opt]
18005
18006 objc-catch-clause-seq:
18007 objc-catch-clause objc-catch-clause-seq [opt]
18008
18009 objc-catch-clause:
18010 @catch ( exception-declaration ) compound-statement
18011
18012 objc-finally-clause
18013 @finally compound-statement
18014
18015 Returns NULL_TREE. */
18016
18017 static tree
18018 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18019 location_t location;
18020 tree stmt;
18021
18022 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18023 location = cp_lexer_peek_token (parser->lexer)->location;
18024 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18025 node, lest it get absorbed into the surrounding block. */
18026 stmt = push_stmt_list ();
18027 cp_parser_compound_statement (parser, NULL, false);
18028 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18029
18030 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18031 {
18032 cp_parameter_declarator *parmdecl;
18033 tree parm;
18034
18035 cp_lexer_consume_token (parser->lexer);
18036 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18037 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18038 parm = grokdeclarator (parmdecl->declarator,
18039 &parmdecl->decl_specifiers,
18040 PARM, /*initialized=*/0,
18041 /*attrlist=*/NULL);
18042 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18043 objc_begin_catch_clause (parm);
18044 cp_parser_compound_statement (parser, NULL, false);
18045 objc_finish_catch_clause ();
18046 }
18047
18048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18049 {
18050 cp_lexer_consume_token (parser->lexer);
18051 location = cp_lexer_peek_token (parser->lexer)->location;
18052 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18053 node, lest it get absorbed into the surrounding block. */
18054 stmt = push_stmt_list ();
18055 cp_parser_compound_statement (parser, NULL, false);
18056 objc_build_finally_clause (location, pop_stmt_list (stmt));
18057 }
18058
18059 return objc_finish_try_stmt ();
18060 }
18061
18062 /* Parse an Objective-C synchronized statement.
18063
18064 objc-synchronized-stmt:
18065 @synchronized ( expression ) compound-statement
18066
18067 Returns NULL_TREE. */
18068
18069 static tree
18070 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18071 location_t location;
18072 tree lock, stmt;
18073
18074 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18075
18076 location = cp_lexer_peek_token (parser->lexer)->location;
18077 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18078 lock = cp_parser_expression (parser, false);
18079 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18080
18081 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18082 node, lest it get absorbed into the surrounding block. */
18083 stmt = push_stmt_list ();
18084 cp_parser_compound_statement (parser, NULL, false);
18085
18086 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18087 }
18088
18089 /* Parse an Objective-C throw statement.
18090
18091 objc-throw-stmt:
18092 @throw assignment-expression [opt] ;
18093
18094 Returns a constructed '@throw' statement. */
18095
18096 static tree
18097 cp_parser_objc_throw_statement (cp_parser *parser) {
18098 tree expr = NULL_TREE;
18099
18100 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18101
18102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18103 expr = cp_parser_assignment_expression (parser, false);
18104
18105 cp_parser_consume_semicolon_at_end_of_statement (parser);
18106
18107 return objc_build_throw_stmt (expr);
18108 }
18109
18110 /* Parse an Objective-C statement. */
18111
18112 static tree
18113 cp_parser_objc_statement (cp_parser * parser) {
18114 /* Try to figure out what kind of declaration is present. */
18115 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18116
18117 switch (kwd->keyword)
18118 {
18119 case RID_AT_TRY:
18120 return cp_parser_objc_try_catch_finally_statement (parser);
18121 case RID_AT_SYNCHRONIZED:
18122 return cp_parser_objc_synchronized_statement (parser);
18123 case RID_AT_THROW:
18124 return cp_parser_objc_throw_statement (parser);
18125 default:
18126 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
18127 cp_parser_skip_to_end_of_block_or_statement (parser);
18128 }
18129
18130 return error_mark_node;
18131 }
18132 \f
18133 /* OpenMP 2.5 parsing routines. */
18134
18135 /* Returns name of the next clause.
18136 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18137 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18138 returned and the token is consumed. */
18139
18140 static pragma_omp_clause
18141 cp_parser_omp_clause_name (cp_parser *parser)
18142 {
18143 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18144
18145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18146 result = PRAGMA_OMP_CLAUSE_IF;
18147 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18148 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18149 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18150 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18151 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18152 {
18153 tree id = cp_lexer_peek_token (parser->lexer)->value;
18154 const char *p = IDENTIFIER_POINTER (id);
18155
18156 switch (p[0])
18157 {
18158 case 'c':
18159 if (!strcmp ("copyin", p))
18160 result = PRAGMA_OMP_CLAUSE_COPYIN;
18161 else if (!strcmp ("copyprivate", p))
18162 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18163 break;
18164 case 'f':
18165 if (!strcmp ("firstprivate", p))
18166 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18167 break;
18168 case 'l':
18169 if (!strcmp ("lastprivate", p))
18170 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18171 break;
18172 case 'n':
18173 if (!strcmp ("nowait", p))
18174 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18175 else if (!strcmp ("num_threads", p))
18176 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18177 break;
18178 case 'o':
18179 if (!strcmp ("ordered", p))
18180 result = PRAGMA_OMP_CLAUSE_ORDERED;
18181 break;
18182 case 'r':
18183 if (!strcmp ("reduction", p))
18184 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18185 break;
18186 case 's':
18187 if (!strcmp ("schedule", p))
18188 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18189 else if (!strcmp ("shared", p))
18190 result = PRAGMA_OMP_CLAUSE_SHARED;
18191 break;
18192 }
18193 }
18194
18195 if (result != PRAGMA_OMP_CLAUSE_NONE)
18196 cp_lexer_consume_token (parser->lexer);
18197
18198 return result;
18199 }
18200
18201 /* Validate that a clause of the given type does not already exist. */
18202
18203 static void
18204 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18205 {
18206 tree c;
18207
18208 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18209 if (OMP_CLAUSE_CODE (c) == code)
18210 {
18211 error ("too many %qs clauses", name);
18212 break;
18213 }
18214 }
18215
18216 /* OpenMP 2.5:
18217 variable-list:
18218 identifier
18219 variable-list , identifier
18220
18221 In addition, we match a closing parenthesis. An opening parenthesis
18222 will have been consumed by the caller.
18223
18224 If KIND is nonzero, create the appropriate node and install the decl
18225 in OMP_CLAUSE_DECL and add the node to the head of the list.
18226
18227 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18228 return the list created. */
18229
18230 static tree
18231 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18232 tree list)
18233 {
18234 while (1)
18235 {
18236 tree name, decl;
18237
18238 name = cp_parser_id_expression (parser, /*template_p=*/false,
18239 /*check_dependency_p=*/true,
18240 /*template_p=*/NULL,
18241 /*declarator_p=*/false,
18242 /*optional_p=*/false);
18243 if (name == error_mark_node)
18244 goto skip_comma;
18245
18246 decl = cp_parser_lookup_name_simple (parser, name);
18247 if (decl == error_mark_node)
18248 cp_parser_name_lookup_error (parser, name, decl, NULL);
18249 else if (kind != 0)
18250 {
18251 tree u = build_omp_clause (kind);
18252 OMP_CLAUSE_DECL (u) = decl;
18253 OMP_CLAUSE_CHAIN (u) = list;
18254 list = u;
18255 }
18256 else
18257 list = tree_cons (decl, NULL_TREE, list);
18258
18259 get_comma:
18260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18261 break;
18262 cp_lexer_consume_token (parser->lexer);
18263 }
18264
18265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18266 {
18267 int ending;
18268
18269 /* Try to resync to an unnested comma. Copied from
18270 cp_parser_parenthesized_expression_list. */
18271 skip_comma:
18272 ending = cp_parser_skip_to_closing_parenthesis (parser,
18273 /*recovering=*/true,
18274 /*or_comma=*/true,
18275 /*consume_paren=*/true);
18276 if (ending < 0)
18277 goto get_comma;
18278 }
18279
18280 return list;
18281 }
18282
18283 /* Similarly, but expect leading and trailing parenthesis. This is a very
18284 common case for omp clauses. */
18285
18286 static tree
18287 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18288 {
18289 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18290 return cp_parser_omp_var_list_no_open (parser, kind, list);
18291 return list;
18292 }
18293
18294 /* OpenMP 2.5:
18295 default ( shared | none ) */
18296
18297 static tree
18298 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18299 {
18300 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18301 tree c;
18302
18303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18304 return list;
18305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18306 {
18307 tree id = cp_lexer_peek_token (parser->lexer)->value;
18308 const char *p = IDENTIFIER_POINTER (id);
18309
18310 switch (p[0])
18311 {
18312 case 'n':
18313 if (strcmp ("none", p) != 0)
18314 goto invalid_kind;
18315 kind = OMP_CLAUSE_DEFAULT_NONE;
18316 break;
18317
18318 case 's':
18319 if (strcmp ("shared", p) != 0)
18320 goto invalid_kind;
18321 kind = OMP_CLAUSE_DEFAULT_SHARED;
18322 break;
18323
18324 default:
18325 goto invalid_kind;
18326 }
18327
18328 cp_lexer_consume_token (parser->lexer);
18329 }
18330 else
18331 {
18332 invalid_kind:
18333 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18334 }
18335
18336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18337 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18338 /*or_comma=*/false,
18339 /*consume_paren=*/true);
18340
18341 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18342 return list;
18343
18344 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18345 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18346 OMP_CLAUSE_CHAIN (c) = list;
18347 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18348
18349 return c;
18350 }
18351
18352 /* OpenMP 2.5:
18353 if ( expression ) */
18354
18355 static tree
18356 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18357 {
18358 tree t, c;
18359
18360 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18361 return list;
18362
18363 t = cp_parser_condition (parser);
18364
18365 if (t == error_mark_node
18366 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18368 /*or_comma=*/false,
18369 /*consume_paren=*/true);
18370
18371 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18372
18373 c = build_omp_clause (OMP_CLAUSE_IF);
18374 OMP_CLAUSE_IF_EXPR (c) = t;
18375 OMP_CLAUSE_CHAIN (c) = list;
18376
18377 return c;
18378 }
18379
18380 /* OpenMP 2.5:
18381 nowait */
18382
18383 static tree
18384 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18385 {
18386 tree c;
18387
18388 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18389
18390 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18391 OMP_CLAUSE_CHAIN (c) = list;
18392 return c;
18393 }
18394
18395 /* OpenMP 2.5:
18396 num_threads ( expression ) */
18397
18398 static tree
18399 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18400 {
18401 tree t, c;
18402
18403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18404 return list;
18405
18406 t = cp_parser_expression (parser, false);
18407
18408 if (t == error_mark_node
18409 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18410 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18411 /*or_comma=*/false,
18412 /*consume_paren=*/true);
18413
18414 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18415
18416 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18417 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18418 OMP_CLAUSE_CHAIN (c) = list;
18419
18420 return c;
18421 }
18422
18423 /* OpenMP 2.5:
18424 ordered */
18425
18426 static tree
18427 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18428 {
18429 tree c;
18430
18431 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18432
18433 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18434 OMP_CLAUSE_CHAIN (c) = list;
18435 return c;
18436 }
18437
18438 /* OpenMP 2.5:
18439 reduction ( reduction-operator : variable-list )
18440
18441 reduction-operator:
18442 One of: + * - & ^ | && || */
18443
18444 static tree
18445 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18446 {
18447 enum tree_code code;
18448 tree nlist, c;
18449
18450 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18451 return list;
18452
18453 switch (cp_lexer_peek_token (parser->lexer)->type)
18454 {
18455 case CPP_PLUS:
18456 code = PLUS_EXPR;
18457 break;
18458 case CPP_MULT:
18459 code = MULT_EXPR;
18460 break;
18461 case CPP_MINUS:
18462 code = MINUS_EXPR;
18463 break;
18464 case CPP_AND:
18465 code = BIT_AND_EXPR;
18466 break;
18467 case CPP_XOR:
18468 code = BIT_XOR_EXPR;
18469 break;
18470 case CPP_OR:
18471 code = BIT_IOR_EXPR;
18472 break;
18473 case CPP_AND_AND:
18474 code = TRUTH_ANDIF_EXPR;
18475 break;
18476 case CPP_OR_OR:
18477 code = TRUTH_ORIF_EXPR;
18478 break;
18479 default:
18480 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18481 resync_fail:
18482 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18483 /*or_comma=*/false,
18484 /*consume_paren=*/true);
18485 return list;
18486 }
18487 cp_lexer_consume_token (parser->lexer);
18488
18489 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18490 goto resync_fail;
18491
18492 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18493 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18494 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18495
18496 return nlist;
18497 }
18498
18499 /* OpenMP 2.5:
18500 schedule ( schedule-kind )
18501 schedule ( schedule-kind , expression )
18502
18503 schedule-kind:
18504 static | dynamic | guided | runtime */
18505
18506 static tree
18507 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18508 {
18509 tree c, t;
18510
18511 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18512 return list;
18513
18514 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18515
18516 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18517 {
18518 tree id = cp_lexer_peek_token (parser->lexer)->value;
18519 const char *p = IDENTIFIER_POINTER (id);
18520
18521 switch (p[0])
18522 {
18523 case 'd':
18524 if (strcmp ("dynamic", p) != 0)
18525 goto invalid_kind;
18526 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18527 break;
18528
18529 case 'g':
18530 if (strcmp ("guided", p) != 0)
18531 goto invalid_kind;
18532 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18533 break;
18534
18535 case 'r':
18536 if (strcmp ("runtime", p) != 0)
18537 goto invalid_kind;
18538 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18539 break;
18540
18541 default:
18542 goto invalid_kind;
18543 }
18544 }
18545 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18546 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18547 else
18548 goto invalid_kind;
18549 cp_lexer_consume_token (parser->lexer);
18550
18551 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18552 {
18553 cp_lexer_consume_token (parser->lexer);
18554
18555 t = cp_parser_assignment_expression (parser, false);
18556
18557 if (t == error_mark_node)
18558 goto resync_fail;
18559 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18560 error ("schedule %<runtime%> does not take "
18561 "a %<chunk_size%> parameter");
18562 else
18563 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18564
18565 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18566 goto resync_fail;
18567 }
18568 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18569 goto resync_fail;
18570
18571 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18572 OMP_CLAUSE_CHAIN (c) = list;
18573 return c;
18574
18575 invalid_kind:
18576 cp_parser_error (parser, "invalid schedule kind");
18577 resync_fail:
18578 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18579 /*or_comma=*/false,
18580 /*consume_paren=*/true);
18581 return list;
18582 }
18583
18584 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18585 is a bitmask in MASK. Return the list of clauses found; the result
18586 of clause default goes in *pdefault. */
18587
18588 static tree
18589 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18590 const char *where, cp_token *pragma_tok)
18591 {
18592 tree clauses = NULL;
18593
18594 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18595 {
18596 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18597 const char *c_name;
18598 tree prev = clauses;
18599
18600 switch (c_kind)
18601 {
18602 case PRAGMA_OMP_CLAUSE_COPYIN:
18603 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18604 c_name = "copyin";
18605 break;
18606 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18607 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18608 clauses);
18609 c_name = "copyprivate";
18610 break;
18611 case PRAGMA_OMP_CLAUSE_DEFAULT:
18612 clauses = cp_parser_omp_clause_default (parser, clauses);
18613 c_name = "default";
18614 break;
18615 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18616 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18617 clauses);
18618 c_name = "firstprivate";
18619 break;
18620 case PRAGMA_OMP_CLAUSE_IF:
18621 clauses = cp_parser_omp_clause_if (parser, clauses);
18622 c_name = "if";
18623 break;
18624 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18625 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18626 clauses);
18627 c_name = "lastprivate";
18628 break;
18629 case PRAGMA_OMP_CLAUSE_NOWAIT:
18630 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18631 c_name = "nowait";
18632 break;
18633 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18634 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18635 c_name = "num_threads";
18636 break;
18637 case PRAGMA_OMP_CLAUSE_ORDERED:
18638 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18639 c_name = "ordered";
18640 break;
18641 case PRAGMA_OMP_CLAUSE_PRIVATE:
18642 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18643 clauses);
18644 c_name = "private";
18645 break;
18646 case PRAGMA_OMP_CLAUSE_REDUCTION:
18647 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18648 c_name = "reduction";
18649 break;
18650 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18651 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18652 c_name = "schedule";
18653 break;
18654 case PRAGMA_OMP_CLAUSE_SHARED:
18655 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18656 clauses);
18657 c_name = "shared";
18658 break;
18659 default:
18660 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18661 goto saw_error;
18662 }
18663
18664 if (((mask >> c_kind) & 1) == 0)
18665 {
18666 /* Remove the invalid clause(s) from the list to avoid
18667 confusing the rest of the compiler. */
18668 clauses = prev;
18669 error ("%qs is not valid for %qs", c_name, where);
18670 }
18671 }
18672 saw_error:
18673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18674 return finish_omp_clauses (clauses);
18675 }
18676
18677 /* OpenMP 2.5:
18678 structured-block:
18679 statement
18680
18681 In practice, we're also interested in adding the statement to an
18682 outer node. So it is convenient if we work around the fact that
18683 cp_parser_statement calls add_stmt. */
18684
18685 static unsigned
18686 cp_parser_begin_omp_structured_block (cp_parser *parser)
18687 {
18688 unsigned save = parser->in_statement;
18689
18690 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18691 This preserves the "not within loop or switch" style error messages
18692 for nonsense cases like
18693 void foo() {
18694 #pragma omp single
18695 break;
18696 }
18697 */
18698 if (parser->in_statement)
18699 parser->in_statement = IN_OMP_BLOCK;
18700
18701 return save;
18702 }
18703
18704 static void
18705 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18706 {
18707 parser->in_statement = save;
18708 }
18709
18710 static tree
18711 cp_parser_omp_structured_block (cp_parser *parser)
18712 {
18713 tree stmt = begin_omp_structured_block ();
18714 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18715
18716 cp_parser_statement (parser, NULL_TREE, false, NULL);
18717
18718 cp_parser_end_omp_structured_block (parser, save);
18719 return finish_omp_structured_block (stmt);
18720 }
18721
18722 /* OpenMP 2.5:
18723 # pragma omp atomic new-line
18724 expression-stmt
18725
18726 expression-stmt:
18727 x binop= expr | x++ | ++x | x-- | --x
18728 binop:
18729 +, *, -, /, &, ^, |, <<, >>
18730
18731 where x is an lvalue expression with scalar type. */
18732
18733 static void
18734 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18735 {
18736 tree lhs, rhs;
18737 enum tree_code code;
18738
18739 cp_parser_require_pragma_eol (parser, pragma_tok);
18740
18741 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18742 /*cast_p=*/false);
18743 switch (TREE_CODE (lhs))
18744 {
18745 case ERROR_MARK:
18746 goto saw_error;
18747
18748 case PREINCREMENT_EXPR:
18749 case POSTINCREMENT_EXPR:
18750 lhs = TREE_OPERAND (lhs, 0);
18751 code = PLUS_EXPR;
18752 rhs = integer_one_node;
18753 break;
18754
18755 case PREDECREMENT_EXPR:
18756 case POSTDECREMENT_EXPR:
18757 lhs = TREE_OPERAND (lhs, 0);
18758 code = MINUS_EXPR;
18759 rhs = integer_one_node;
18760 break;
18761
18762 default:
18763 switch (cp_lexer_peek_token (parser->lexer)->type)
18764 {
18765 case CPP_MULT_EQ:
18766 code = MULT_EXPR;
18767 break;
18768 case CPP_DIV_EQ:
18769 code = TRUNC_DIV_EXPR;
18770 break;
18771 case CPP_PLUS_EQ:
18772 code = PLUS_EXPR;
18773 break;
18774 case CPP_MINUS_EQ:
18775 code = MINUS_EXPR;
18776 break;
18777 case CPP_LSHIFT_EQ:
18778 code = LSHIFT_EXPR;
18779 break;
18780 case CPP_RSHIFT_EQ:
18781 code = RSHIFT_EXPR;
18782 break;
18783 case CPP_AND_EQ:
18784 code = BIT_AND_EXPR;
18785 break;
18786 case CPP_OR_EQ:
18787 code = BIT_IOR_EXPR;
18788 break;
18789 case CPP_XOR_EQ:
18790 code = BIT_XOR_EXPR;
18791 break;
18792 default:
18793 cp_parser_error (parser,
18794 "invalid operator for %<#pragma omp atomic%>");
18795 goto saw_error;
18796 }
18797 cp_lexer_consume_token (parser->lexer);
18798
18799 rhs = cp_parser_expression (parser, false);
18800 if (rhs == error_mark_node)
18801 goto saw_error;
18802 break;
18803 }
18804 finish_omp_atomic (code, lhs, rhs);
18805 cp_parser_consume_semicolon_at_end_of_statement (parser);
18806 return;
18807
18808 saw_error:
18809 cp_parser_skip_to_end_of_block_or_statement (parser);
18810 }
18811
18812
18813 /* OpenMP 2.5:
18814 # pragma omp barrier new-line */
18815
18816 static void
18817 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18818 {
18819 cp_parser_require_pragma_eol (parser, pragma_tok);
18820 finish_omp_barrier ();
18821 }
18822
18823 /* OpenMP 2.5:
18824 # pragma omp critical [(name)] new-line
18825 structured-block */
18826
18827 static tree
18828 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18829 {
18830 tree stmt, name = NULL;
18831
18832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18833 {
18834 cp_lexer_consume_token (parser->lexer);
18835
18836 name = cp_parser_identifier (parser);
18837
18838 if (name == error_mark_node
18839 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18841 /*or_comma=*/false,
18842 /*consume_paren=*/true);
18843 if (name == error_mark_node)
18844 name = NULL;
18845 }
18846 cp_parser_require_pragma_eol (parser, pragma_tok);
18847
18848 stmt = cp_parser_omp_structured_block (parser);
18849 return c_finish_omp_critical (stmt, name);
18850 }
18851
18852 /* OpenMP 2.5:
18853 # pragma omp flush flush-vars[opt] new-line
18854
18855 flush-vars:
18856 ( variable-list ) */
18857
18858 static void
18859 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18860 {
18861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18862 (void) cp_parser_omp_var_list (parser, 0, NULL);
18863 cp_parser_require_pragma_eol (parser, pragma_tok);
18864
18865 finish_omp_flush ();
18866 }
18867
18868 /* Parse the restricted form of the for statment allowed by OpenMP. */
18869
18870 static tree
18871 cp_parser_omp_for_loop (cp_parser *parser)
18872 {
18873 tree init, cond, incr, body, decl, pre_body;
18874 location_t loc;
18875
18876 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18877 {
18878 cp_parser_error (parser, "for statement expected");
18879 return NULL;
18880 }
18881 loc = cp_lexer_consume_token (parser->lexer)->location;
18882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18883 return NULL;
18884
18885 init = decl = NULL;
18886 pre_body = push_stmt_list ();
18887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18888 {
18889 cp_decl_specifier_seq type_specifiers;
18890
18891 /* First, try to parse as an initialized declaration. See
18892 cp_parser_condition, from whence the bulk of this is copied. */
18893
18894 cp_parser_parse_tentatively (parser);
18895 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18896 &type_specifiers);
18897 if (!cp_parser_error_occurred (parser))
18898 {
18899 tree asm_specification, attributes;
18900 cp_declarator *declarator;
18901
18902 declarator = cp_parser_declarator (parser,
18903 CP_PARSER_DECLARATOR_NAMED,
18904 /*ctor_dtor_or_conv_p=*/NULL,
18905 /*parenthesized_p=*/NULL,
18906 /*member_p=*/false);
18907 attributes = cp_parser_attributes_opt (parser);
18908 asm_specification = cp_parser_asm_specification_opt (parser);
18909
18910 cp_parser_require (parser, CPP_EQ, "`='");
18911 if (cp_parser_parse_definitely (parser))
18912 {
18913 tree pushed_scope;
18914
18915 decl = start_decl (declarator, &type_specifiers,
18916 /*initialized_p=*/false, attributes,
18917 /*prefix_attributes=*/NULL_TREE,
18918 &pushed_scope);
18919
18920 init = cp_parser_assignment_expression (parser, false);
18921
18922 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18923 asm_specification, LOOKUP_ONLYCONVERTING);
18924
18925 if (pushed_scope)
18926 pop_scope (pushed_scope);
18927 }
18928 }
18929 else
18930 cp_parser_abort_tentative_parse (parser);
18931
18932 /* If parsing as an initialized declaration failed, try again as
18933 a simple expression. */
18934 if (decl == NULL)
18935 init = cp_parser_expression (parser, false);
18936 }
18937 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18938 pre_body = pop_stmt_list (pre_body);
18939
18940 cond = NULL;
18941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18942 cond = cp_parser_condition (parser);
18943 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18944
18945 incr = NULL;
18946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18947 incr = cp_parser_expression (parser, false);
18948
18949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18950 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18951 /*or_comma=*/false,
18952 /*consume_paren=*/true);
18953
18954 /* Note that we saved the original contents of this flag when we entered
18955 the structured block, and so we don't need to re-save it here. */
18956 parser->in_statement = IN_OMP_FOR;
18957
18958 /* Note that the grammar doesn't call for a structured block here,
18959 though the loop as a whole is a structured block. */
18960 body = push_stmt_list ();
18961 cp_parser_statement (parser, NULL_TREE, false, NULL);
18962 body = pop_stmt_list (body);
18963
18964 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18965 }
18966
18967 /* OpenMP 2.5:
18968 #pragma omp for for-clause[optseq] new-line
18969 for-loop */
18970
18971 #define OMP_FOR_CLAUSE_MASK \
18972 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18973 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18974 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18975 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18976 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18977 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18978 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18979
18980 static tree
18981 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18982 {
18983 tree clauses, sb, ret;
18984 unsigned int save;
18985
18986 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18987 "#pragma omp for", pragma_tok);
18988
18989 sb = begin_omp_structured_block ();
18990 save = cp_parser_begin_omp_structured_block (parser);
18991
18992 ret = cp_parser_omp_for_loop (parser);
18993 if (ret)
18994 OMP_FOR_CLAUSES (ret) = clauses;
18995
18996 cp_parser_end_omp_structured_block (parser, save);
18997 add_stmt (finish_omp_structured_block (sb));
18998
18999 return ret;
19000 }
19001
19002 /* OpenMP 2.5:
19003 # pragma omp master new-line
19004 structured-block */
19005
19006 static tree
19007 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19008 {
19009 cp_parser_require_pragma_eol (parser, pragma_tok);
19010 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19011 }
19012
19013 /* OpenMP 2.5:
19014 # pragma omp ordered new-line
19015 structured-block */
19016
19017 static tree
19018 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19019 {
19020 cp_parser_require_pragma_eol (parser, pragma_tok);
19021 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19022 }
19023
19024 /* OpenMP 2.5:
19025
19026 section-scope:
19027 { section-sequence }
19028
19029 section-sequence:
19030 section-directive[opt] structured-block
19031 section-sequence section-directive structured-block */
19032
19033 static tree
19034 cp_parser_omp_sections_scope (cp_parser *parser)
19035 {
19036 tree stmt, substmt;
19037 bool error_suppress = false;
19038 cp_token *tok;
19039
19040 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19041 return NULL_TREE;
19042
19043 stmt = push_stmt_list ();
19044
19045 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19046 {
19047 unsigned save;
19048
19049 substmt = begin_omp_structured_block ();
19050 save = cp_parser_begin_omp_structured_block (parser);
19051
19052 while (1)
19053 {
19054 cp_parser_statement (parser, NULL_TREE, false, NULL);
19055
19056 tok = cp_lexer_peek_token (parser->lexer);
19057 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19058 break;
19059 if (tok->type == CPP_CLOSE_BRACE)
19060 break;
19061 if (tok->type == CPP_EOF)
19062 break;
19063 }
19064
19065 cp_parser_end_omp_structured_block (parser, save);
19066 substmt = finish_omp_structured_block (substmt);
19067 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19068 add_stmt (substmt);
19069 }
19070
19071 while (1)
19072 {
19073 tok = cp_lexer_peek_token (parser->lexer);
19074 if (tok->type == CPP_CLOSE_BRACE)
19075 break;
19076 if (tok->type == CPP_EOF)
19077 break;
19078
19079 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19080 {
19081 cp_lexer_consume_token (parser->lexer);
19082 cp_parser_require_pragma_eol (parser, tok);
19083 error_suppress = false;
19084 }
19085 else if (!error_suppress)
19086 {
19087 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19088 error_suppress = true;
19089 }
19090
19091 substmt = cp_parser_omp_structured_block (parser);
19092 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19093 add_stmt (substmt);
19094 }
19095 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19096
19097 substmt = pop_stmt_list (stmt);
19098
19099 stmt = make_node (OMP_SECTIONS);
19100 TREE_TYPE (stmt) = void_type_node;
19101 OMP_SECTIONS_BODY (stmt) = substmt;
19102
19103 add_stmt (stmt);
19104 return stmt;
19105 }
19106
19107 /* OpenMP 2.5:
19108 # pragma omp sections sections-clause[optseq] newline
19109 sections-scope */
19110
19111 #define OMP_SECTIONS_CLAUSE_MASK \
19112 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19113 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19114 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19115 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19116 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19117
19118 static tree
19119 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19120 {
19121 tree clauses, ret;
19122
19123 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19124 "#pragma omp sections", pragma_tok);
19125
19126 ret = cp_parser_omp_sections_scope (parser);
19127 if (ret)
19128 OMP_SECTIONS_CLAUSES (ret) = clauses;
19129
19130 return ret;
19131 }
19132
19133 /* OpenMP 2.5:
19134 # pragma parallel parallel-clause new-line
19135 # pragma parallel for parallel-for-clause new-line
19136 # pragma parallel sections parallel-sections-clause new-line */
19137
19138 #define OMP_PARALLEL_CLAUSE_MASK \
19139 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19140 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19141 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19142 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19143 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19144 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19145 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19146 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19147
19148 static tree
19149 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19150 {
19151 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19152 const char *p_name = "#pragma omp parallel";
19153 tree stmt, clauses, par_clause, ws_clause, block;
19154 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19155 unsigned int save;
19156
19157 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19158 {
19159 cp_lexer_consume_token (parser->lexer);
19160 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19161 p_name = "#pragma omp parallel for";
19162 mask |= OMP_FOR_CLAUSE_MASK;
19163 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19164 }
19165 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19166 {
19167 tree id = cp_lexer_peek_token (parser->lexer)->value;
19168 const char *p = IDENTIFIER_POINTER (id);
19169 if (strcmp (p, "sections") == 0)
19170 {
19171 cp_lexer_consume_token (parser->lexer);
19172 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19173 p_name = "#pragma omp parallel sections";
19174 mask |= OMP_SECTIONS_CLAUSE_MASK;
19175 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19176 }
19177 }
19178
19179 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19180 block = begin_omp_parallel ();
19181 save = cp_parser_begin_omp_structured_block (parser);
19182
19183 switch (p_kind)
19184 {
19185 case PRAGMA_OMP_PARALLEL:
19186 cp_parser_already_scoped_statement (parser);
19187 par_clause = clauses;
19188 break;
19189
19190 case PRAGMA_OMP_PARALLEL_FOR:
19191 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19192 stmt = cp_parser_omp_for_loop (parser);
19193 if (stmt)
19194 OMP_FOR_CLAUSES (stmt) = ws_clause;
19195 break;
19196
19197 case PRAGMA_OMP_PARALLEL_SECTIONS:
19198 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19199 stmt = cp_parser_omp_sections_scope (parser);
19200 if (stmt)
19201 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19202 break;
19203
19204 default:
19205 gcc_unreachable ();
19206 }
19207
19208 cp_parser_end_omp_structured_block (parser, save);
19209 stmt = finish_omp_parallel (par_clause, block);
19210 if (p_kind != PRAGMA_OMP_PARALLEL)
19211 OMP_PARALLEL_COMBINED (stmt) = 1;
19212 return stmt;
19213 }
19214
19215 /* OpenMP 2.5:
19216 # pragma omp single single-clause[optseq] new-line
19217 structured-block */
19218
19219 #define OMP_SINGLE_CLAUSE_MASK \
19220 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19221 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19222 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19223 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19224
19225 static tree
19226 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19227 {
19228 tree stmt = make_node (OMP_SINGLE);
19229 TREE_TYPE (stmt) = void_type_node;
19230
19231 OMP_SINGLE_CLAUSES (stmt)
19232 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19233 "#pragma omp single", pragma_tok);
19234 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19235
19236 return add_stmt (stmt);
19237 }
19238
19239 /* OpenMP 2.5:
19240 # pragma omp threadprivate (variable-list) */
19241
19242 static void
19243 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19244 {
19245 tree vars;
19246
19247 vars = cp_parser_omp_var_list (parser, 0, NULL);
19248 cp_parser_require_pragma_eol (parser, pragma_tok);
19249
19250 if (!targetm.have_tls)
19251 sorry ("threadprivate variables not supported in this target");
19252
19253 finish_omp_threadprivate (vars);
19254 }
19255
19256 /* Main entry point to OpenMP statement pragmas. */
19257
19258 static void
19259 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19260 {
19261 tree stmt;
19262
19263 switch (pragma_tok->pragma_kind)
19264 {
19265 case PRAGMA_OMP_ATOMIC:
19266 cp_parser_omp_atomic (parser, pragma_tok);
19267 return;
19268 case PRAGMA_OMP_CRITICAL:
19269 stmt = cp_parser_omp_critical (parser, pragma_tok);
19270 break;
19271 case PRAGMA_OMP_FOR:
19272 stmt = cp_parser_omp_for (parser, pragma_tok);
19273 break;
19274 case PRAGMA_OMP_MASTER:
19275 stmt = cp_parser_omp_master (parser, pragma_tok);
19276 break;
19277 case PRAGMA_OMP_ORDERED:
19278 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19279 break;
19280 case PRAGMA_OMP_PARALLEL:
19281 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19282 break;
19283 case PRAGMA_OMP_SECTIONS:
19284 stmt = cp_parser_omp_sections (parser, pragma_tok);
19285 break;
19286 case PRAGMA_OMP_SINGLE:
19287 stmt = cp_parser_omp_single (parser, pragma_tok);
19288 break;
19289 default:
19290 gcc_unreachable ();
19291 }
19292
19293 if (stmt)
19294 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19295 }
19296 \f
19297 /* The parser. */
19298
19299 static GTY (()) cp_parser *the_parser;
19300
19301 \f
19302 /* Special handling for the first token or line in the file. The first
19303 thing in the file might be #pragma GCC pch_preprocess, which loads a
19304 PCH file, which is a GC collection point. So we need to handle this
19305 first pragma without benefit of an existing lexer structure.
19306
19307 Always returns one token to the caller in *FIRST_TOKEN. This is
19308 either the true first token of the file, or the first token after
19309 the initial pragma. */
19310
19311 static void
19312 cp_parser_initial_pragma (cp_token *first_token)
19313 {
19314 tree name = NULL;
19315
19316 cp_lexer_get_preprocessor_token (NULL, first_token);
19317 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19318 return;
19319
19320 cp_lexer_get_preprocessor_token (NULL, first_token);
19321 if (first_token->type == CPP_STRING)
19322 {
19323 name = first_token->value;
19324
19325 cp_lexer_get_preprocessor_token (NULL, first_token);
19326 if (first_token->type != CPP_PRAGMA_EOL)
19327 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19328 }
19329 else
19330 error ("expected string literal");
19331
19332 /* Skip to the end of the pragma. */
19333 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19334 cp_lexer_get_preprocessor_token (NULL, first_token);
19335
19336 /* Now actually load the PCH file. */
19337 if (name)
19338 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19339
19340 /* Read one more token to return to our caller. We have to do this
19341 after reading the PCH file in, since its pointers have to be
19342 live. */
19343 cp_lexer_get_preprocessor_token (NULL, first_token);
19344 }
19345
19346 /* Normal parsing of a pragma token. Here we can (and must) use the
19347 regular lexer. */
19348
19349 static bool
19350 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19351 {
19352 cp_token *pragma_tok;
19353 unsigned int id;
19354
19355 pragma_tok = cp_lexer_consume_token (parser->lexer);
19356 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19357 parser->lexer->in_pragma = true;
19358
19359 id = pragma_tok->pragma_kind;
19360 switch (id)
19361 {
19362 case PRAGMA_GCC_PCH_PREPROCESS:
19363 error ("%<#pragma GCC pch_preprocess%> must be first");
19364 break;
19365
19366 case PRAGMA_OMP_BARRIER:
19367 switch (context)
19368 {
19369 case pragma_compound:
19370 cp_parser_omp_barrier (parser, pragma_tok);
19371 return false;
19372 case pragma_stmt:
19373 error ("%<#pragma omp barrier%> may only be "
19374 "used in compound statements");
19375 break;
19376 default:
19377 goto bad_stmt;
19378 }
19379 break;
19380
19381 case PRAGMA_OMP_FLUSH:
19382 switch (context)
19383 {
19384 case pragma_compound:
19385 cp_parser_omp_flush (parser, pragma_tok);
19386 return false;
19387 case pragma_stmt:
19388 error ("%<#pragma omp flush%> may only be "
19389 "used in compound statements");
19390 break;
19391 default:
19392 goto bad_stmt;
19393 }
19394 break;
19395
19396 case PRAGMA_OMP_THREADPRIVATE:
19397 cp_parser_omp_threadprivate (parser, pragma_tok);
19398 return false;
19399
19400 case PRAGMA_OMP_ATOMIC:
19401 case PRAGMA_OMP_CRITICAL:
19402 case PRAGMA_OMP_FOR:
19403 case PRAGMA_OMP_MASTER:
19404 case PRAGMA_OMP_ORDERED:
19405 case PRAGMA_OMP_PARALLEL:
19406 case PRAGMA_OMP_SECTIONS:
19407 case PRAGMA_OMP_SINGLE:
19408 if (context == pragma_external)
19409 goto bad_stmt;
19410 cp_parser_omp_construct (parser, pragma_tok);
19411 return true;
19412
19413 case PRAGMA_OMP_SECTION:
19414 error ("%<#pragma omp section%> may only be used in "
19415 "%<#pragma omp sections%> construct");
19416 break;
19417
19418 default:
19419 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19420 c_invoke_pragma_handler (id);
19421 break;
19422
19423 bad_stmt:
19424 cp_parser_error (parser, "expected declaration specifiers");
19425 break;
19426 }
19427
19428 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19429 return false;
19430 }
19431
19432 /* The interface the pragma parsers have to the lexer. */
19433
19434 enum cpp_ttype
19435 pragma_lex (tree *value)
19436 {
19437 cp_token *tok;
19438 enum cpp_ttype ret;
19439
19440 tok = cp_lexer_peek_token (the_parser->lexer);
19441
19442 ret = tok->type;
19443 *value = tok->value;
19444
19445 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19446 ret = CPP_EOF;
19447 else if (ret == CPP_STRING)
19448 *value = cp_parser_string_literal (the_parser, false, false);
19449 else
19450 {
19451 cp_lexer_consume_token (the_parser->lexer);
19452 if (ret == CPP_KEYWORD)
19453 ret = CPP_NAME;
19454 }
19455
19456 return ret;
19457 }
19458
19459 \f
19460 /* External interface. */
19461
19462 /* Parse one entire translation unit. */
19463
19464 void
19465 c_parse_file (void)
19466 {
19467 bool error_occurred;
19468 static bool already_called = false;
19469
19470 if (already_called)
19471 {
19472 sorry ("inter-module optimizations not implemented for C++");
19473 return;
19474 }
19475 already_called = true;
19476
19477 the_parser = cp_parser_new ();
19478 push_deferring_access_checks (flag_access_control
19479 ? dk_no_deferred : dk_no_check);
19480 error_occurred = cp_parser_translation_unit (the_parser);
19481 the_parser = NULL;
19482 }
19483
19484 #include "gt-cp-parser.h"
This page took 1.423806 seconds and 6 git commands to generate.