]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
re PR c++/27270 (ICE in process_init_constructor_array, at cp/typeck2.c:788)
[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 a pointer to the Nth token in the token stream. If N is 1,
509 then this is precisely equivalent to cp_lexer_peek_token (except
510 that it is not inline). One would like to disallow that case, but
511 there is one case (cp_parser_nth_token_starts_template_id) where
512 the caller passes a variable for N and it might be 1. */
513
514 static cp_token *
515 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
516 {
517 cp_token *token;
518
519 /* N is 1-based, not zero-based. */
520 gcc_assert (n > 0);
521
522 if (cp_lexer_debugging_p (lexer))
523 fprintf (cp_lexer_debug_stream,
524 "cp_lexer: peeking ahead %ld at token: ", (long)n);
525
526 --n;
527 token = lexer->next_token;
528 gcc_assert (!n || token != &eof_token);
529 while (n != 0)
530 {
531 ++token;
532 if (token == lexer->last_token)
533 {
534 token = (cp_token *)&eof_token;
535 break;
536 }
537
538 if (token->type != CPP_PURGED)
539 --n;
540 }
541
542 if (cp_lexer_debugging_p (lexer))
543 {
544 cp_lexer_print_token (cp_lexer_debug_stream, token);
545 putc ('\n', cp_lexer_debug_stream);
546 }
547
548 return token;
549 }
550
551 /* Return the next token, and advance the lexer's next_token pointer
552 to point to the next non-purged token. */
553
554 static cp_token *
555 cp_lexer_consume_token (cp_lexer* lexer)
556 {
557 cp_token *token = lexer->next_token;
558
559 gcc_assert (token != &eof_token);
560 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
561
562 do
563 {
564 lexer->next_token++;
565 if (lexer->next_token == lexer->last_token)
566 {
567 lexer->next_token = (cp_token *)&eof_token;
568 break;
569 }
570
571 }
572 while (lexer->next_token->type == CPP_PURGED);
573
574 cp_lexer_set_source_position_from_token (token);
575
576 /* Provide debugging output. */
577 if (cp_lexer_debugging_p (lexer))
578 {
579 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
580 cp_lexer_print_token (cp_lexer_debug_stream, token);
581 putc ('\n', cp_lexer_debug_stream);
582 }
583
584 return token;
585 }
586
587 /* Permanently remove the next token from the token stream, and
588 advance the next_token pointer to refer to the next non-purged
589 token. */
590
591 static void
592 cp_lexer_purge_token (cp_lexer *lexer)
593 {
594 cp_token *tok = lexer->next_token;
595
596 gcc_assert (tok != &eof_token);
597 tok->type = CPP_PURGED;
598 tok->location = UNKNOWN_LOCATION;
599 tok->value = NULL_TREE;
600 tok->keyword = RID_MAX;
601
602 do
603 {
604 tok++;
605 if (tok == lexer->last_token)
606 {
607 tok = (cp_token *)&eof_token;
608 break;
609 }
610 }
611 while (tok->type == CPP_PURGED);
612 lexer->next_token = tok;
613 }
614
615 /* Permanently remove all tokens after TOK, up to, but not
616 including, the token that will be returned next by
617 cp_lexer_peek_token. */
618
619 static void
620 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
621 {
622 cp_token *peek = lexer->next_token;
623
624 if (peek == &eof_token)
625 peek = lexer->last_token;
626
627 gcc_assert (tok < peek);
628
629 for ( tok += 1; tok != peek; tok += 1)
630 {
631 tok->type = CPP_PURGED;
632 tok->location = UNKNOWN_LOCATION;
633 tok->value = NULL_TREE;
634 tok->keyword = RID_MAX;
635 }
636 }
637
638 /* Begin saving tokens. All tokens consumed after this point will be
639 preserved. */
640
641 static void
642 cp_lexer_save_tokens (cp_lexer* lexer)
643 {
644 /* Provide debugging output. */
645 if (cp_lexer_debugging_p (lexer))
646 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
647
648 VEC_safe_push (cp_token_position, heap,
649 lexer->saved_tokens, lexer->next_token);
650 }
651
652 /* Commit to the portion of the token stream most recently saved. */
653
654 static void
655 cp_lexer_commit_tokens (cp_lexer* lexer)
656 {
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
659 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
660
661 VEC_pop (cp_token_position, lexer->saved_tokens);
662 }
663
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665 to the token stream. Stop saving tokens. */
666
667 static void
668 cp_lexer_rollback_tokens (cp_lexer* lexer)
669 {
670 /* Provide debugging output. */
671 if (cp_lexer_debugging_p (lexer))
672 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
673
674 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
675 }
676
677 /* Print a representation of the TOKEN on the STREAM. */
678
679 #ifdef ENABLE_CHECKING
680
681 static void
682 cp_lexer_print_token (FILE * stream, cp_token *token)
683 {
684 /* We don't use cpp_type2name here because the parser defines
685 a few tokens of its own. */
686 static const char *const token_names[] = {
687 /* cpplib-defined token types */
688 #define OP(e, s) #e,
689 #define TK(e, s) #e,
690 TTYPE_TABLE
691 #undef OP
692 #undef TK
693 /* C++ parser token types - see "Manifest constants", above. */
694 "KEYWORD",
695 "TEMPLATE_ID",
696 "NESTED_NAME_SPECIFIER",
697 "PURGED"
698 };
699
700 /* If we have a name for the token, print it out. Otherwise, we
701 simply give the numeric code. */
702 gcc_assert (token->type < ARRAY_SIZE(token_names));
703 fputs (token_names[token->type], stream);
704
705 /* For some tokens, print the associated data. */
706 switch (token->type)
707 {
708 case CPP_KEYWORD:
709 /* Some keywords have a value that is not an IDENTIFIER_NODE.
710 For example, `struct' is mapped to an INTEGER_CST. */
711 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
712 break;
713 /* else fall through */
714 case CPP_NAME:
715 fputs (IDENTIFIER_POINTER (token->value), stream);
716 break;
717
718 case CPP_STRING:
719 case CPP_WSTRING:
720 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
721 break;
722
723 default:
724 break;
725 }
726 }
727
728 /* Start emitting debugging information. */
729
730 static void
731 cp_lexer_start_debugging (cp_lexer* lexer)
732 {
733 lexer->debugging_p = true;
734 }
735
736 /* Stop emitting debugging information. */
737
738 static void
739 cp_lexer_stop_debugging (cp_lexer* lexer)
740 {
741 lexer->debugging_p = false;
742 }
743
744 #endif /* ENABLE_CHECKING */
745
746 /* Create a new cp_token_cache, representing a range of tokens. */
747
748 static cp_token_cache *
749 cp_token_cache_new (cp_token *first, cp_token *last)
750 {
751 cp_token_cache *cache = GGC_NEW (cp_token_cache);
752 cache->first = first;
753 cache->last = last;
754 return cache;
755 }
756
757 \f
758 /* Decl-specifiers. */
759
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
761
762 static void
763 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
764 {
765 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
766 }
767
768 /* Declarators. */
769
770 /* Nothing other than the parser should be creating declarators;
771 declarators are a semi-syntactic representation of C++ entities.
772 Other parts of the front end that need to create entities (like
773 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
774
775 static cp_declarator *make_call_declarator
776 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
777 static cp_declarator *make_array_declarator
778 (cp_declarator *, tree);
779 static cp_declarator *make_pointer_declarator
780 (cp_cv_quals, cp_declarator *);
781 static cp_declarator *make_reference_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_parameter_declarator *make_parameter_declarator
784 (cp_decl_specifier_seq *, cp_declarator *, tree);
785 static cp_declarator *make_ptrmem_declarator
786 (cp_cv_quals, tree, cp_declarator *);
787
788 /* An erroneous declarator. */
789 static cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792 allocated. */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool. */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800 return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809 cp_declarator *declarator;
810
811 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812 declarator->kind = kind;
813 declarator->attributes = NULL_TREE;
814 declarator->declarator = NULL;
815
816 return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier. If
820 QUALIFYING_SCOPE is non-NULL, the identifier is
821 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822 UNQUALIFIED_NAME. SFK indicates the kind of special function this
823 is, if any. */
824
825 static cp_declarator *
826 make_id_declarator (tree qualifying_scope, tree unqualified_name,
827 special_function_kind sfk)
828 {
829 cp_declarator *declarator;
830
831 /* It is valid to write:
832
833 class C { void f(); };
834 typedef C D;
835 void D::f();
836
837 The standard is not clear about whether `typedef const C D' is
838 legal; as of 2002-09-15 the committee is considering that
839 question. EDG 3.0 allows that syntax. Therefore, we do as
840 well. */
841 if (qualifying_scope && TYPE_P (qualifying_scope))
842 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
843
844 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
845 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
846 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
847
848 declarator = make_declarator (cdk_id);
849 declarator->u.id.qualifying_scope = qualifying_scope;
850 declarator->u.id.unqualified_name = unqualified_name;
851 declarator->u.id.sfk = sfk;
852
853 return declarator;
854 }
855
856 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
857 of modifiers such as const or volatile to apply to the pointer
858 type, represented as identifiers. */
859
860 cp_declarator *
861 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
862 {
863 cp_declarator *declarator;
864
865 declarator = make_declarator (cdk_pointer);
866 declarator->declarator = target;
867 declarator->u.pointer.qualifiers = cv_qualifiers;
868 declarator->u.pointer.class_type = NULL_TREE;
869
870 return declarator;
871 }
872
873 /* Like make_pointer_declarator -- but for references. */
874
875 cp_declarator *
876 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
877 {
878 cp_declarator *declarator;
879
880 declarator = make_declarator (cdk_reference);
881 declarator->declarator = target;
882 declarator->u.pointer.qualifiers = cv_qualifiers;
883 declarator->u.pointer.class_type = NULL_TREE;
884
885 return declarator;
886 }
887
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889 member of CLASS_TYPE. */
890
891 cp_declarator *
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
893 cp_declarator *pointee)
894 {
895 cp_declarator *declarator;
896
897 declarator = make_declarator (cdk_ptrmem);
898 declarator->declarator = pointee;
899 declarator->u.pointer.qualifiers = cv_qualifiers;
900 declarator->u.pointer.class_type = class_type;
901
902 return declarator;
903 }
904
905 /* Make a declarator for the function given by TARGET, with the
906 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
907 "const"-qualified member function. The EXCEPTION_SPECIFICATION
908 indicates what exceptions can be thrown. */
909
910 cp_declarator *
911 make_call_declarator (cp_declarator *target,
912 cp_parameter_declarator *parms,
913 cp_cv_quals cv_qualifiers,
914 tree exception_specification)
915 {
916 cp_declarator *declarator;
917
918 declarator = make_declarator (cdk_function);
919 declarator->declarator = target;
920 declarator->u.function.parameters = parms;
921 declarator->u.function.qualifiers = cv_qualifiers;
922 declarator->u.function.exception_specification = exception_specification;
923
924 return declarator;
925 }
926
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928 defined by ELEMENT. */
929
930 cp_declarator *
931 make_array_declarator (cp_declarator *element, tree bounds)
932 {
933 cp_declarator *declarator;
934
935 declarator = make_declarator (cdk_array);
936 declarator->declarator = element;
937 declarator->u.array.bounds = bounds;
938
939 return declarator;
940 }
941
942 cp_parameter_declarator *no_parameters;
943
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945 DECLARATOR and DEFAULT_ARGUMENT. */
946
947 cp_parameter_declarator *
948 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
949 cp_declarator *declarator,
950 tree default_argument)
951 {
952 cp_parameter_declarator *parameter;
953
954 parameter = ((cp_parameter_declarator *)
955 alloc_declarator (sizeof (cp_parameter_declarator)));
956 parameter->next = NULL;
957 if (decl_specifiers)
958 parameter->decl_specifiers = *decl_specifiers;
959 else
960 clear_decl_specs (&parameter->decl_specifiers);
961 parameter->declarator = declarator;
962 parameter->default_argument = default_argument;
963 parameter->ellipsis_p = false;
964
965 return parameter;
966 }
967
968 /* Returns true iff DECLARATOR is a declaration for a function. */
969
970 static bool
971 function_declarator_p (const cp_declarator *declarator)
972 {
973 while (declarator)
974 {
975 if (declarator->kind == cdk_function
976 && declarator->declarator->kind == cdk_id)
977 return true;
978 if (declarator->kind == cdk_id
979 || declarator->kind == cdk_error)
980 return false;
981 declarator = declarator->declarator;
982 }
983 return false;
984 }
985
986 /* The parser. */
987
988 /* Overview
989 --------
990
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
998
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1001 code processed.
1002
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1011
1012 Methodology
1013 -----------
1014
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1031
1032 Future Improvements
1033 -------------------
1034
1035 The performance of the parser could probably be improved substantially.
1036 We could often eliminate the need to parse tentatively by looking ahead
1037 a little bit. In some places, this approach might not entirely eliminate
1038 the need to parse tentatively, but it might still speed up the average
1039 case. */
1040
1041 /* Flags that are passed to some parsing functions. These values can
1042 be bitwise-ored together. */
1043
1044 typedef enum cp_parser_flags
1045 {
1046 /* No flags. */
1047 CP_PARSER_FLAGS_NONE = 0x0,
1048 /* The construct is optional. If it is not present, then no error
1049 should be issued. */
1050 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1051 /* When parsing a type-specifier, do not allow user-defined types. */
1052 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1053 } cp_parser_flags;
1054
1055 /* The different kinds of declarators we want to parse. */
1056
1057 typedef enum cp_parser_declarator_kind
1058 {
1059 /* We want an abstract declarator. */
1060 CP_PARSER_DECLARATOR_ABSTRACT,
1061 /* We want a named declarator. */
1062 CP_PARSER_DECLARATOR_NAMED,
1063 /* We don't mind, but the name must be an unqualified-id. */
1064 CP_PARSER_DECLARATOR_EITHER
1065 } cp_parser_declarator_kind;
1066
1067 /* The precedence values used to parse binary expressions. The minimum value
1068 of PREC must be 1, because zero is reserved to quickly discriminate
1069 binary operators from other tokens. */
1070
1071 enum cp_parser_prec
1072 {
1073 PREC_NOT_OPERATOR,
1074 PREC_LOGICAL_OR_EXPRESSION,
1075 PREC_LOGICAL_AND_EXPRESSION,
1076 PREC_INCLUSIVE_OR_EXPRESSION,
1077 PREC_EXCLUSIVE_OR_EXPRESSION,
1078 PREC_AND_EXPRESSION,
1079 PREC_EQUALITY_EXPRESSION,
1080 PREC_RELATIONAL_EXPRESSION,
1081 PREC_SHIFT_EXPRESSION,
1082 PREC_ADDITIVE_EXPRESSION,
1083 PREC_MULTIPLICATIVE_EXPRESSION,
1084 PREC_PM_EXPRESSION,
1085 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1086 };
1087
1088 /* A mapping from a token type to a corresponding tree node type, with a
1089 precedence value. */
1090
1091 typedef struct cp_parser_binary_operations_map_node
1092 {
1093 /* The token type. */
1094 enum cpp_ttype token_type;
1095 /* The corresponding tree code. */
1096 enum tree_code tree_type;
1097 /* The precedence of this operator. */
1098 enum cp_parser_prec prec;
1099 } cp_parser_binary_operations_map_node;
1100
1101 /* The status of a tentative parse. */
1102
1103 typedef enum cp_parser_status_kind
1104 {
1105 /* No errors have occurred. */
1106 CP_PARSER_STATUS_KIND_NO_ERROR,
1107 /* An error has occurred. */
1108 CP_PARSER_STATUS_KIND_ERROR,
1109 /* We are committed to this tentative parse, whether or not an error
1110 has occurred. */
1111 CP_PARSER_STATUS_KIND_COMMITTED
1112 } cp_parser_status_kind;
1113
1114 typedef struct cp_parser_expression_stack_entry
1115 {
1116 tree lhs;
1117 enum tree_code tree_type;
1118 int prec;
1119 } cp_parser_expression_stack_entry;
1120
1121 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1122 entries because precedence levels on the stack are monotonically
1123 increasing. */
1124 typedef struct cp_parser_expression_stack_entry
1125 cp_parser_expression_stack[NUM_PREC_VALUES];
1126
1127 /* Context that is saved and restored when parsing tentatively. */
1128 typedef struct cp_parser_context GTY (())
1129 {
1130 /* If this is a tentative parsing context, the status of the
1131 tentative parse. */
1132 enum cp_parser_status_kind status;
1133 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1134 that are looked up in this context must be looked up both in the
1135 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1136 the context of the containing expression. */
1137 tree object_type;
1138
1139 /* The next parsing context in the stack. */
1140 struct cp_parser_context *next;
1141 } cp_parser_context;
1142
1143 /* Prototypes. */
1144
1145 /* Constructors and destructors. */
1146
1147 static cp_parser_context *cp_parser_context_new
1148 (cp_parser_context *);
1149
1150 /* Class variables. */
1151
1152 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1153
1154 /* The operator-precedence table used by cp_parser_binary_expression.
1155 Transformed into an associative array (binops_by_token) by
1156 cp_parser_new. */
1157
1158 static const cp_parser_binary_operations_map_node binops[] = {
1159 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1160 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1161
1162 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1163 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1164 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1165
1166 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1167 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1168
1169 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1170 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1171
1172 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1173 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1174 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1175 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1176
1177 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1178 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1179
1180 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1181
1182 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1183
1184 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1185
1186 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1187
1188 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1189 };
1190
1191 /* The same as binops, but initialized by cp_parser_new so that
1192 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1193 for speed. */
1194 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1195
1196 /* Constructors and destructors. */
1197
1198 /* Construct a new context. The context below this one on the stack
1199 is given by NEXT. */
1200
1201 static cp_parser_context *
1202 cp_parser_context_new (cp_parser_context* next)
1203 {
1204 cp_parser_context *context;
1205
1206 /* Allocate the storage. */
1207 if (cp_parser_context_free_list != NULL)
1208 {
1209 /* Pull the first entry from the free list. */
1210 context = cp_parser_context_free_list;
1211 cp_parser_context_free_list = context->next;
1212 memset (context, 0, sizeof (*context));
1213 }
1214 else
1215 context = GGC_CNEW (cp_parser_context);
1216
1217 /* No errors have occurred yet in this context. */
1218 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1219 /* If this is not the bottomost context, copy information that we
1220 need from the previous context. */
1221 if (next)
1222 {
1223 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224 expression, then we are parsing one in this context, too. */
1225 context->object_type = next->object_type;
1226 /* Thread the stack. */
1227 context->next = next;
1228 }
1229
1230 return context;
1231 }
1232
1233 /* The cp_parser structure represents the C++ parser. */
1234
1235 typedef struct cp_parser GTY(())
1236 {
1237 /* The lexer from which we are obtaining tokens. */
1238 cp_lexer *lexer;
1239
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look. It can
1244 also be ERROR_MARK, when we've parsed a bogus scope.
1245
1246 This value is not cleared automatically after a name is looked
1247 up, so we must be careful to clear it before starting a new look
1248 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1249 will look up `Z' in the scope of `X', rather than the current
1250 scope.) Unfortunately, it is difficult to tell when name lookup
1251 is complete, because we sometimes peek at a token, look it up,
1252 and then decide not to consume it. */
1253 tree scope;
1254
1255 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1256 last lookup took place. OBJECT_SCOPE is used if an expression
1257 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1258 respectively. QUALIFYING_SCOPE is used for an expression of the
1259 form "X::Y"; it refers to X. */
1260 tree object_scope;
1261 tree qualifying_scope;
1262
1263 /* A stack of parsing contexts. All but the bottom entry on the
1264 stack will be tentative contexts.
1265
1266 We parse tentatively in order to determine which construct is in
1267 use in some situations. For example, in order to determine
1268 whether a statement is an expression-statement or a
1269 declaration-statement we parse it tentatively as a
1270 declaration-statement. If that fails, we then reparse the same
1271 token stream as an expression-statement. */
1272 cp_parser_context *context;
1273
1274 /* True if we are parsing GNU C++. If this flag is not set, then
1275 GNU extensions are not recognized. */
1276 bool allow_gnu_extensions_p;
1277
1278 /* TRUE if the `>' token should be interpreted as the greater-than
1279 operator. FALSE if it is the end of a template-id or
1280 template-parameter-list. */
1281 bool greater_than_is_operator_p;
1282
1283 /* TRUE if default arguments are allowed within a parameter list
1284 that starts at this point. FALSE if only a gnu extension makes
1285 them permissible. */
1286 bool default_arg_ok_p;
1287
1288 /* TRUE if we are parsing an integral constant-expression. See
1289 [expr.const] for a precise definition. */
1290 bool integral_constant_expression_p;
1291
1292 /* TRUE if we are parsing an integral constant-expression -- but a
1293 non-constant expression should be permitted as well. This flag
1294 is used when parsing an array bound so that GNU variable-length
1295 arrays are tolerated. */
1296 bool allow_non_integral_constant_expression_p;
1297
1298 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1299 been seen that makes the expression non-constant. */
1300 bool non_integral_constant_expression_p;
1301
1302 /* TRUE if local variable names and `this' are forbidden in the
1303 current context. */
1304 bool local_variables_forbidden_p;
1305
1306 /* TRUE if the declaration we are parsing is part of a
1307 linkage-specification of the form `extern string-literal
1308 declaration'. */
1309 bool in_unbraced_linkage_specification_p;
1310
1311 /* TRUE if we are presently parsing a declarator, after the
1312 direct-declarator. */
1313 bool in_declarator_p;
1314
1315 /* TRUE if we are presently parsing a template-argument-list. */
1316 bool in_template_argument_list_p;
1317
1318 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1319 to IN_OMP_BLOCK if parsing OpenMP structured block and
1320 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1321 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1322 iteration-statement, OpenMP block or loop within that switch. */
1323 #define IN_SWITCH_STMT 1
1324 #define IN_ITERATION_STMT 2
1325 #define IN_OMP_BLOCK 4
1326 #define IN_OMP_FOR 8
1327 unsigned char in_statement;
1328
1329 /* TRUE if we are presently parsing the body of a switch statement.
1330 Note that this doesn't quite overlap with in_statement above.
1331 The difference relates to giving the right sets of error messages:
1332 "case not in switch" vs "break statement used with OpenMP...". */
1333 bool in_switch_statement_p;
1334
1335 /* TRUE if we are parsing a type-id in an expression context. In
1336 such a situation, both "type (expr)" and "type (type)" are valid
1337 alternatives. */
1338 bool in_type_id_in_expr_p;
1339
1340 /* TRUE if we are currently in a header file where declarations are
1341 implicitly extern "C". */
1342 bool implicit_extern_c;
1343
1344 /* TRUE if strings in expressions should be translated to the execution
1345 character set. */
1346 bool translate_strings_p;
1347
1348 /* If non-NULL, then we are parsing a construct where new type
1349 definitions are not permitted. The string stored here will be
1350 issued as an error message if a type is defined. */
1351 const char *type_definition_forbidden_message;
1352
1353 /* A list of lists. The outer list is a stack, used for member
1354 functions of local classes. At each level there are two sub-list,
1355 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1356 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1357 TREE_VALUE's. The functions are chained in reverse declaration
1358 order.
1359
1360 The TREE_PURPOSE sublist contains those functions with default
1361 arguments that need post processing, and the TREE_VALUE sublist
1362 contains those functions with definitions that need post
1363 processing.
1364
1365 These lists can only be processed once the outermost class being
1366 defined is complete. */
1367 tree unparsed_functions_queues;
1368
1369 /* The number of classes whose definitions are currently in
1370 progress. */
1371 unsigned num_classes_being_defined;
1372
1373 /* The number of template parameter lists that apply directly to the
1374 current declaration. */
1375 unsigned num_template_parameter_lists;
1376 } cp_parser;
1377
1378 /* Prototypes. */
1379
1380 /* Constructors and destructors. */
1381
1382 static cp_parser *cp_parser_new
1383 (void);
1384
1385 /* Routines to parse various constructs.
1386
1387 Those that return `tree' will return the error_mark_node (rather
1388 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1389 Sometimes, they will return an ordinary node if error-recovery was
1390 attempted, even though a parse error occurred. So, to check
1391 whether or not a parse error occurred, you should always use
1392 cp_parser_error_occurred. If the construct is optional (indicated
1393 either by an `_opt' in the name of the function that does the
1394 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1395 the construct is not present. */
1396
1397 /* Lexical conventions [gram.lex] */
1398
1399 static tree cp_parser_identifier
1400 (cp_parser *);
1401 static tree cp_parser_string_literal
1402 (cp_parser *, bool, bool);
1403
1404 /* Basic concepts [gram.basic] */
1405
1406 static bool cp_parser_translation_unit
1407 (cp_parser *);
1408
1409 /* Expressions [gram.expr] */
1410
1411 static tree cp_parser_primary_expression
1412 (cp_parser *, bool, bool, bool, cp_id_kind *);
1413 static tree cp_parser_id_expression
1414 (cp_parser *, bool, bool, bool *, bool, bool);
1415 static tree cp_parser_unqualified_id
1416 (cp_parser *, bool, bool, bool, bool);
1417 static tree cp_parser_nested_name_specifier_opt
1418 (cp_parser *, bool, bool, bool, bool);
1419 static tree cp_parser_nested_name_specifier
1420 (cp_parser *, bool, bool, bool, bool);
1421 static tree cp_parser_class_or_namespace_name
1422 (cp_parser *, bool, bool, bool, bool, bool);
1423 static tree cp_parser_postfix_expression
1424 (cp_parser *, bool, bool);
1425 static tree cp_parser_postfix_open_square_expression
1426 (cp_parser *, tree, bool);
1427 static tree cp_parser_postfix_dot_deref_expression
1428 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1429 static tree cp_parser_parenthesized_expression_list
1430 (cp_parser *, bool, bool, bool *);
1431 static void cp_parser_pseudo_destructor_name
1432 (cp_parser *, tree *, tree *);
1433 static tree cp_parser_unary_expression
1434 (cp_parser *, bool, bool);
1435 static enum tree_code cp_parser_unary_operator
1436 (cp_token *);
1437 static tree cp_parser_new_expression
1438 (cp_parser *);
1439 static tree cp_parser_new_placement
1440 (cp_parser *);
1441 static tree cp_parser_new_type_id
1442 (cp_parser *, tree *);
1443 static cp_declarator *cp_parser_new_declarator_opt
1444 (cp_parser *);
1445 static cp_declarator *cp_parser_direct_new_declarator
1446 (cp_parser *);
1447 static tree cp_parser_new_initializer
1448 (cp_parser *);
1449 static tree cp_parser_delete_expression
1450 (cp_parser *);
1451 static tree cp_parser_cast_expression
1452 (cp_parser *, bool, bool);
1453 static tree cp_parser_binary_expression
1454 (cp_parser *, bool);
1455 static tree cp_parser_question_colon_clause
1456 (cp_parser *, tree);
1457 static tree cp_parser_assignment_expression
1458 (cp_parser *, bool);
1459 static enum tree_code cp_parser_assignment_operator_opt
1460 (cp_parser *);
1461 static tree cp_parser_expression
1462 (cp_parser *, bool);
1463 static tree cp_parser_constant_expression
1464 (cp_parser *, bool, bool *);
1465 static tree cp_parser_builtin_offsetof
1466 (cp_parser *);
1467
1468 /* Statements [gram.stmt.stmt] */
1469
1470 static void cp_parser_statement
1471 (cp_parser *, tree, bool);
1472 static void cp_parser_label_for_labeled_statement
1473 (cp_parser *);
1474 static tree cp_parser_expression_statement
1475 (cp_parser *, tree);
1476 static tree cp_parser_compound_statement
1477 (cp_parser *, tree, bool);
1478 static void cp_parser_statement_seq_opt
1479 (cp_parser *, tree);
1480 static tree cp_parser_selection_statement
1481 (cp_parser *);
1482 static tree cp_parser_condition
1483 (cp_parser *);
1484 static tree cp_parser_iteration_statement
1485 (cp_parser *);
1486 static void cp_parser_for_init_statement
1487 (cp_parser *);
1488 static tree cp_parser_jump_statement
1489 (cp_parser *);
1490 static void cp_parser_declaration_statement
1491 (cp_parser *);
1492
1493 static tree cp_parser_implicitly_scoped_statement
1494 (cp_parser *);
1495 static void cp_parser_already_scoped_statement
1496 (cp_parser *);
1497
1498 /* Declarations [gram.dcl.dcl] */
1499
1500 static void cp_parser_declaration_seq_opt
1501 (cp_parser *);
1502 static void cp_parser_declaration
1503 (cp_parser *);
1504 static void cp_parser_block_declaration
1505 (cp_parser *, bool);
1506 static void cp_parser_simple_declaration
1507 (cp_parser *, bool);
1508 static void cp_parser_decl_specifier_seq
1509 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1510 static tree cp_parser_storage_class_specifier_opt
1511 (cp_parser *);
1512 static tree cp_parser_function_specifier_opt
1513 (cp_parser *, cp_decl_specifier_seq *);
1514 static tree cp_parser_type_specifier
1515 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1516 int *, bool *);
1517 static tree cp_parser_simple_type_specifier
1518 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1519 static tree cp_parser_type_name
1520 (cp_parser *);
1521 static tree cp_parser_elaborated_type_specifier
1522 (cp_parser *, bool, bool);
1523 static tree cp_parser_enum_specifier
1524 (cp_parser *);
1525 static void cp_parser_enumerator_list
1526 (cp_parser *, tree);
1527 static void cp_parser_enumerator_definition
1528 (cp_parser *, tree);
1529 static tree cp_parser_namespace_name
1530 (cp_parser *);
1531 static void cp_parser_namespace_definition
1532 (cp_parser *);
1533 static void cp_parser_namespace_body
1534 (cp_parser *);
1535 static tree cp_parser_qualified_namespace_specifier
1536 (cp_parser *);
1537 static void cp_parser_namespace_alias_definition
1538 (cp_parser *);
1539 static bool cp_parser_using_declaration
1540 (cp_parser *, bool);
1541 static void cp_parser_using_directive
1542 (cp_parser *);
1543 static void cp_parser_asm_definition
1544 (cp_parser *);
1545 static void cp_parser_linkage_specification
1546 (cp_parser *);
1547
1548 /* Declarators [gram.dcl.decl] */
1549
1550 static tree cp_parser_init_declarator
1551 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1552 static cp_declarator *cp_parser_declarator
1553 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1554 static cp_declarator *cp_parser_direct_declarator
1555 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1556 static enum tree_code cp_parser_ptr_operator
1557 (cp_parser *, tree *, cp_cv_quals *);
1558 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1559 (cp_parser *);
1560 static tree cp_parser_declarator_id
1561 (cp_parser *, bool);
1562 static tree cp_parser_type_id
1563 (cp_parser *);
1564 static void cp_parser_type_specifier_seq
1565 (cp_parser *, bool, cp_decl_specifier_seq *);
1566 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1567 (cp_parser *);
1568 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1569 (cp_parser *, bool *);
1570 static cp_parameter_declarator *cp_parser_parameter_declaration
1571 (cp_parser *, bool, bool *);
1572 static void cp_parser_function_body
1573 (cp_parser *);
1574 static tree cp_parser_initializer
1575 (cp_parser *, bool *, bool *);
1576 static tree cp_parser_initializer_clause
1577 (cp_parser *, bool *);
1578 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1579 (cp_parser *, bool *);
1580
1581 static bool cp_parser_ctor_initializer_opt_and_function_body
1582 (cp_parser *);
1583
1584 /* Classes [gram.class] */
1585
1586 static tree cp_parser_class_name
1587 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1588 static tree cp_parser_class_specifier
1589 (cp_parser *);
1590 static tree cp_parser_class_head
1591 (cp_parser *, bool *, tree *);
1592 static enum tag_types cp_parser_class_key
1593 (cp_parser *);
1594 static void cp_parser_member_specification_opt
1595 (cp_parser *);
1596 static void cp_parser_member_declaration
1597 (cp_parser *);
1598 static tree cp_parser_pure_specifier
1599 (cp_parser *);
1600 static tree cp_parser_constant_initializer
1601 (cp_parser *);
1602
1603 /* Derived classes [gram.class.derived] */
1604
1605 static tree cp_parser_base_clause
1606 (cp_parser *);
1607 static tree cp_parser_base_specifier
1608 (cp_parser *);
1609
1610 /* Special member functions [gram.special] */
1611
1612 static tree cp_parser_conversion_function_id
1613 (cp_parser *);
1614 static tree cp_parser_conversion_type_id
1615 (cp_parser *);
1616 static cp_declarator *cp_parser_conversion_declarator_opt
1617 (cp_parser *);
1618 static bool cp_parser_ctor_initializer_opt
1619 (cp_parser *);
1620 static void cp_parser_mem_initializer_list
1621 (cp_parser *);
1622 static tree cp_parser_mem_initializer
1623 (cp_parser *);
1624 static tree cp_parser_mem_initializer_id
1625 (cp_parser *);
1626
1627 /* Overloading [gram.over] */
1628
1629 static tree cp_parser_operator_function_id
1630 (cp_parser *);
1631 static tree cp_parser_operator
1632 (cp_parser *);
1633
1634 /* Templates [gram.temp] */
1635
1636 static void cp_parser_template_declaration
1637 (cp_parser *, bool);
1638 static tree cp_parser_template_parameter_list
1639 (cp_parser *);
1640 static tree cp_parser_template_parameter
1641 (cp_parser *, bool *);
1642 static tree cp_parser_type_parameter
1643 (cp_parser *);
1644 static tree cp_parser_template_id
1645 (cp_parser *, bool, bool, bool);
1646 static tree cp_parser_template_name
1647 (cp_parser *, bool, bool, bool, bool *);
1648 static tree cp_parser_template_argument_list
1649 (cp_parser *);
1650 static tree cp_parser_template_argument
1651 (cp_parser *);
1652 static void cp_parser_explicit_instantiation
1653 (cp_parser *);
1654 static void cp_parser_explicit_specialization
1655 (cp_parser *);
1656
1657 /* Exception handling [gram.exception] */
1658
1659 static tree cp_parser_try_block
1660 (cp_parser *);
1661 static bool cp_parser_function_try_block
1662 (cp_parser *);
1663 static void cp_parser_handler_seq
1664 (cp_parser *);
1665 static void cp_parser_handler
1666 (cp_parser *);
1667 static tree cp_parser_exception_declaration
1668 (cp_parser *);
1669 static tree cp_parser_throw_expression
1670 (cp_parser *);
1671 static tree cp_parser_exception_specification_opt
1672 (cp_parser *);
1673 static tree cp_parser_type_id_list
1674 (cp_parser *);
1675
1676 /* GNU Extensions */
1677
1678 static tree cp_parser_asm_specification_opt
1679 (cp_parser *);
1680 static tree cp_parser_asm_operand_list
1681 (cp_parser *);
1682 static tree cp_parser_asm_clobber_list
1683 (cp_parser *);
1684 static tree cp_parser_attributes_opt
1685 (cp_parser *);
1686 static tree cp_parser_attribute_list
1687 (cp_parser *);
1688 static bool cp_parser_extension_opt
1689 (cp_parser *, int *);
1690 static void cp_parser_label_declaration
1691 (cp_parser *);
1692
1693 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1694 static bool cp_parser_pragma
1695 (cp_parser *, enum pragma_context);
1696
1697 /* Objective-C++ Productions */
1698
1699 static tree cp_parser_objc_message_receiver
1700 (cp_parser *);
1701 static tree cp_parser_objc_message_args
1702 (cp_parser *);
1703 static tree cp_parser_objc_message_expression
1704 (cp_parser *);
1705 static tree cp_parser_objc_encode_expression
1706 (cp_parser *);
1707 static tree cp_parser_objc_defs_expression
1708 (cp_parser *);
1709 static tree cp_parser_objc_protocol_expression
1710 (cp_parser *);
1711 static tree cp_parser_objc_selector_expression
1712 (cp_parser *);
1713 static tree cp_parser_objc_expression
1714 (cp_parser *);
1715 static bool cp_parser_objc_selector_p
1716 (enum cpp_ttype);
1717 static tree cp_parser_objc_selector
1718 (cp_parser *);
1719 static tree cp_parser_objc_protocol_refs_opt
1720 (cp_parser *);
1721 static void cp_parser_objc_declaration
1722 (cp_parser *);
1723 static tree cp_parser_objc_statement
1724 (cp_parser *);
1725
1726 /* Utility Routines */
1727
1728 static tree cp_parser_lookup_name
1729 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1730 static tree cp_parser_lookup_name_simple
1731 (cp_parser *, tree);
1732 static tree cp_parser_maybe_treat_template_as_class
1733 (tree, bool);
1734 static bool cp_parser_check_declarator_template_parameters
1735 (cp_parser *, cp_declarator *);
1736 static bool cp_parser_check_template_parameters
1737 (cp_parser *, unsigned);
1738 static tree cp_parser_simple_cast_expression
1739 (cp_parser *);
1740 static tree cp_parser_global_scope_opt
1741 (cp_parser *, bool);
1742 static bool cp_parser_constructor_declarator_p
1743 (cp_parser *, bool);
1744 static tree cp_parser_function_definition_from_specifiers_and_declarator
1745 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1746 static tree cp_parser_function_definition_after_declarator
1747 (cp_parser *, bool);
1748 static void cp_parser_template_declaration_after_export
1749 (cp_parser *, bool);
1750 static void cp_parser_perform_template_parameter_access_checks
1751 (tree);
1752 static tree cp_parser_single_declaration
1753 (cp_parser *, tree, bool, bool *);
1754 static tree cp_parser_functional_cast
1755 (cp_parser *, tree);
1756 static tree cp_parser_save_member_function_body
1757 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1758 static tree cp_parser_enclosed_template_argument_list
1759 (cp_parser *);
1760 static void cp_parser_save_default_args
1761 (cp_parser *, tree);
1762 static void cp_parser_late_parsing_for_member
1763 (cp_parser *, tree);
1764 static void cp_parser_late_parsing_default_args
1765 (cp_parser *, tree);
1766 static tree cp_parser_sizeof_operand
1767 (cp_parser *, enum rid);
1768 static bool cp_parser_declares_only_class_p
1769 (cp_parser *);
1770 static void cp_parser_set_storage_class
1771 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1772 static void cp_parser_set_decl_spec_type
1773 (cp_decl_specifier_seq *, tree, bool);
1774 static bool cp_parser_friend_p
1775 (const cp_decl_specifier_seq *);
1776 static cp_token *cp_parser_require
1777 (cp_parser *, enum cpp_ttype, const char *);
1778 static cp_token *cp_parser_require_keyword
1779 (cp_parser *, enum rid, const char *);
1780 static bool cp_parser_token_starts_function_definition_p
1781 (cp_token *);
1782 static bool cp_parser_next_token_starts_class_definition_p
1783 (cp_parser *);
1784 static bool cp_parser_next_token_ends_template_argument_p
1785 (cp_parser *);
1786 static bool cp_parser_nth_token_starts_template_argument_list_p
1787 (cp_parser *, size_t);
1788 static enum tag_types cp_parser_token_is_class_key
1789 (cp_token *);
1790 static void cp_parser_check_class_key
1791 (enum tag_types, tree type);
1792 static void cp_parser_check_access_in_redeclaration
1793 (tree type);
1794 static bool cp_parser_optional_template_keyword
1795 (cp_parser *);
1796 static void cp_parser_pre_parsed_nested_name_specifier
1797 (cp_parser *);
1798 static void cp_parser_cache_group
1799 (cp_parser *, enum cpp_ttype, unsigned);
1800 static void cp_parser_parse_tentatively
1801 (cp_parser *);
1802 static void cp_parser_commit_to_tentative_parse
1803 (cp_parser *);
1804 static void cp_parser_abort_tentative_parse
1805 (cp_parser *);
1806 static bool cp_parser_parse_definitely
1807 (cp_parser *);
1808 static inline bool cp_parser_parsing_tentatively
1809 (cp_parser *);
1810 static bool cp_parser_uncommitted_to_tentative_parse_p
1811 (cp_parser *);
1812 static void cp_parser_error
1813 (cp_parser *, const char *);
1814 static void cp_parser_name_lookup_error
1815 (cp_parser *, tree, tree, const char *);
1816 static bool cp_parser_simulate_error
1817 (cp_parser *);
1818 static void cp_parser_check_type_definition
1819 (cp_parser *);
1820 static void cp_parser_check_for_definition_in_return_type
1821 (cp_declarator *, tree);
1822 static void cp_parser_check_for_invalid_template_id
1823 (cp_parser *, tree);
1824 static bool cp_parser_non_integral_constant_expression
1825 (cp_parser *, const char *);
1826 static void cp_parser_diagnose_invalid_type_name
1827 (cp_parser *, tree, tree);
1828 static bool cp_parser_parse_and_diagnose_invalid_type_name
1829 (cp_parser *);
1830 static int cp_parser_skip_to_closing_parenthesis
1831 (cp_parser *, bool, bool, bool);
1832 static void cp_parser_skip_to_end_of_statement
1833 (cp_parser *);
1834 static void cp_parser_consume_semicolon_at_end_of_statement
1835 (cp_parser *);
1836 static void cp_parser_skip_to_end_of_block_or_statement
1837 (cp_parser *);
1838 static void cp_parser_skip_to_closing_brace
1839 (cp_parser *);
1840 static void cp_parser_skip_to_end_of_template_parameter_list
1841 (cp_parser *);
1842 static void cp_parser_skip_to_pragma_eol
1843 (cp_parser*, cp_token *);
1844 static bool cp_parser_error_occurred
1845 (cp_parser *);
1846 static bool cp_parser_allow_gnu_extensions_p
1847 (cp_parser *);
1848 static bool cp_parser_is_string_literal
1849 (cp_token *);
1850 static bool cp_parser_is_keyword
1851 (cp_token *, enum rid);
1852 static tree cp_parser_make_typename_type
1853 (cp_parser *, tree, tree);
1854
1855 /* Returns nonzero if we are parsing tentatively. */
1856
1857 static inline bool
1858 cp_parser_parsing_tentatively (cp_parser* parser)
1859 {
1860 return parser->context->next != NULL;
1861 }
1862
1863 /* Returns nonzero if TOKEN is a string literal. */
1864
1865 static bool
1866 cp_parser_is_string_literal (cp_token* token)
1867 {
1868 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1869 }
1870
1871 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1872
1873 static bool
1874 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1875 {
1876 return token->keyword == keyword;
1877 }
1878
1879 /* If not parsing tentatively, issue a diagnostic of the form
1880 FILE:LINE: MESSAGE before TOKEN
1881 where TOKEN is the next token in the input stream. MESSAGE
1882 (specified by the caller) is usually of the form "expected
1883 OTHER-TOKEN". */
1884
1885 static void
1886 cp_parser_error (cp_parser* parser, const char* message)
1887 {
1888 if (!cp_parser_simulate_error (parser))
1889 {
1890 cp_token *token = cp_lexer_peek_token (parser->lexer);
1891 /* This diagnostic makes more sense if it is tagged to the line
1892 of the token we just peeked at. */
1893 cp_lexer_set_source_position_from_token (token);
1894
1895 if (token->type == CPP_PRAGMA)
1896 {
1897 error ("%<#pragma%> is not allowed here");
1898 cp_parser_skip_to_pragma_eol (parser, token);
1899 return;
1900 }
1901
1902 c_parse_error (message,
1903 /* Because c_parser_error does not understand
1904 CPP_KEYWORD, keywords are treated like
1905 identifiers. */
1906 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1907 token->value);
1908 }
1909 }
1910
1911 /* Issue an error about name-lookup failing. NAME is the
1912 IDENTIFIER_NODE DECL is the result of
1913 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1914 the thing that we hoped to find. */
1915
1916 static void
1917 cp_parser_name_lookup_error (cp_parser* parser,
1918 tree name,
1919 tree decl,
1920 const char* desired)
1921 {
1922 /* If name lookup completely failed, tell the user that NAME was not
1923 declared. */
1924 if (decl == error_mark_node)
1925 {
1926 if (parser->scope && parser->scope != global_namespace)
1927 error ("%<%D::%D%> has not been declared",
1928 parser->scope, name);
1929 else if (parser->scope == global_namespace)
1930 error ("%<::%D%> has not been declared", name);
1931 else if (parser->object_scope
1932 && !CLASS_TYPE_P (parser->object_scope))
1933 error ("request for member %qD in non-class type %qT",
1934 name, parser->object_scope);
1935 else if (parser->object_scope)
1936 error ("%<%T::%D%> has not been declared",
1937 parser->object_scope, name);
1938 else
1939 error ("%qD has not been declared", name);
1940 }
1941 else if (parser->scope && parser->scope != global_namespace)
1942 error ("%<%D::%D%> %s", parser->scope, name, desired);
1943 else if (parser->scope == global_namespace)
1944 error ("%<::%D%> %s", name, desired);
1945 else
1946 error ("%qD %s", name, desired);
1947 }
1948
1949 /* If we are parsing tentatively, remember that an error has occurred
1950 during this tentative parse. Returns true if the error was
1951 simulated; false if a message should be issued by the caller. */
1952
1953 static bool
1954 cp_parser_simulate_error (cp_parser* parser)
1955 {
1956 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1957 {
1958 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1959 return true;
1960 }
1961 return false;
1962 }
1963
1964 /* Check for repeated decl-specifiers. */
1965
1966 static void
1967 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1968 {
1969 cp_decl_spec ds;
1970
1971 for (ds = ds_first; ds != ds_last; ++ds)
1972 {
1973 unsigned count = decl_specs->specs[(int)ds];
1974 if (count < 2)
1975 continue;
1976 /* The "long" specifier is a special case because of "long long". */
1977 if (ds == ds_long)
1978 {
1979 if (count > 2)
1980 error ("%<long long long%> is too long for GCC");
1981 else if (pedantic && !in_system_header && warn_long_long)
1982 pedwarn ("ISO C++ does not support %<long long%>");
1983 }
1984 else if (count > 1)
1985 {
1986 static const char *const decl_spec_names[] = {
1987 "signed",
1988 "unsigned",
1989 "short",
1990 "long",
1991 "const",
1992 "volatile",
1993 "restrict",
1994 "inline",
1995 "virtual",
1996 "explicit",
1997 "friend",
1998 "typedef",
1999 "__complex",
2000 "__thread"
2001 };
2002 error ("duplicate %qs", decl_spec_names[(int)ds]);
2003 }
2004 }
2005 }
2006
2007 /* This function is called when a type is defined. If type
2008 definitions are forbidden at this point, an error message is
2009 issued. */
2010
2011 static void
2012 cp_parser_check_type_definition (cp_parser* parser)
2013 {
2014 /* If types are forbidden here, issue a message. */
2015 if (parser->type_definition_forbidden_message)
2016 /* Use `%s' to print the string in case there are any escape
2017 characters in the message. */
2018 error ("%s", parser->type_definition_forbidden_message);
2019 }
2020
2021 /* This function is called when the DECLARATOR is processed. The TYPE
2022 was a type defined in the decl-specifiers. If it is invalid to
2023 define a type in the decl-specifiers for DECLARATOR, an error is
2024 issued. */
2025
2026 static void
2027 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2028 tree type)
2029 {
2030 /* [dcl.fct] forbids type definitions in return types.
2031 Unfortunately, it's not easy to know whether or not we are
2032 processing a return type until after the fact. */
2033 while (declarator
2034 && (declarator->kind == cdk_pointer
2035 || declarator->kind == cdk_reference
2036 || declarator->kind == cdk_ptrmem))
2037 declarator = declarator->declarator;
2038 if (declarator
2039 && declarator->kind == cdk_function)
2040 {
2041 error ("new types may not be defined in a return type");
2042 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2043 type);
2044 }
2045 }
2046
2047 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2048 "<" in any valid C++ program. If the next token is indeed "<",
2049 issue a message warning the user about what appears to be an
2050 invalid attempt to form a template-id. */
2051
2052 static void
2053 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2054 tree type)
2055 {
2056 cp_token_position start = 0;
2057
2058 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2059 {
2060 if (TYPE_P (type))
2061 error ("%qT is not a template", type);
2062 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2063 error ("%qE is not a template", type);
2064 else
2065 error ("invalid template-id");
2066 /* Remember the location of the invalid "<". */
2067 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2068 start = cp_lexer_token_position (parser->lexer, true);
2069 /* Consume the "<". */
2070 cp_lexer_consume_token (parser->lexer);
2071 /* Parse the template arguments. */
2072 cp_parser_enclosed_template_argument_list (parser);
2073 /* Permanently remove the invalid template arguments so that
2074 this error message is not issued again. */
2075 if (start)
2076 cp_lexer_purge_tokens_after (parser->lexer, start);
2077 }
2078 }
2079
2080 /* If parsing an integral constant-expression, issue an error message
2081 about the fact that THING appeared and return true. Otherwise,
2082 return false. In either case, set
2083 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2084
2085 static bool
2086 cp_parser_non_integral_constant_expression (cp_parser *parser,
2087 const char *thing)
2088 {
2089 parser->non_integral_constant_expression_p = true;
2090 if (parser->integral_constant_expression_p)
2091 {
2092 if (!parser->allow_non_integral_constant_expression_p)
2093 {
2094 error ("%s cannot appear in a constant-expression", thing);
2095 return true;
2096 }
2097 }
2098 return false;
2099 }
2100
2101 /* Emit a diagnostic for an invalid type name. SCOPE is the
2102 qualifying scope (or NULL, if none) for ID. This function commits
2103 to the current active tentative parse, if any. (Otherwise, the
2104 problematic construct might be encountered again later, resulting
2105 in duplicate error messages.) */
2106
2107 static void
2108 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2109 {
2110 tree decl, old_scope;
2111 /* Try to lookup the identifier. */
2112 old_scope = parser->scope;
2113 parser->scope = scope;
2114 decl = cp_parser_lookup_name_simple (parser, id);
2115 parser->scope = old_scope;
2116 /* If the lookup found a template-name, it means that the user forgot
2117 to specify an argument list. Emit a useful error message. */
2118 if (TREE_CODE (decl) == TEMPLATE_DECL)
2119 error ("invalid use of template-name %qE without an argument list", decl);
2120 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2121 error ("invalid use of destructor %qD as a type", id);
2122 else if (TREE_CODE (decl) == TYPE_DECL)
2123 /* Something like 'unsigned A a;' */
2124 error ("invalid combination of multiple type-specifiers");
2125 else if (!parser->scope)
2126 {
2127 /* Issue an error message. */
2128 error ("%qE does not name a type", id);
2129 /* If we're in a template class, it's possible that the user was
2130 referring to a type from a base class. For example:
2131
2132 template <typename T> struct A { typedef T X; };
2133 template <typename T> struct B : public A<T> { X x; };
2134
2135 The user should have said "typename A<T>::X". */
2136 if (processing_template_decl && current_class_type
2137 && TYPE_BINFO (current_class_type))
2138 {
2139 tree b;
2140
2141 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2142 b;
2143 b = TREE_CHAIN (b))
2144 {
2145 tree base_type = BINFO_TYPE (b);
2146 if (CLASS_TYPE_P (base_type)
2147 && dependent_type_p (base_type))
2148 {
2149 tree field;
2150 /* Go from a particular instantiation of the
2151 template (which will have an empty TYPE_FIELDs),
2152 to the main version. */
2153 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2154 for (field = TYPE_FIELDS (base_type);
2155 field;
2156 field = TREE_CHAIN (field))
2157 if (TREE_CODE (field) == TYPE_DECL
2158 && DECL_NAME (field) == id)
2159 {
2160 inform ("(perhaps %<typename %T::%E%> was intended)",
2161 BINFO_TYPE (b), id);
2162 break;
2163 }
2164 if (field)
2165 break;
2166 }
2167 }
2168 }
2169 }
2170 /* Here we diagnose qualified-ids where the scope is actually correct,
2171 but the identifier does not resolve to a valid type name. */
2172 else if (parser->scope != error_mark_node)
2173 {
2174 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2175 error ("%qE in namespace %qE does not name a type",
2176 id, parser->scope);
2177 else if (TYPE_P (parser->scope))
2178 error ("%qE in class %qT does not name a type", id, parser->scope);
2179 else
2180 gcc_unreachable ();
2181 }
2182 cp_parser_commit_to_tentative_parse (parser);
2183 }
2184
2185 /* Check for a common situation where a type-name should be present,
2186 but is not, and issue a sensible error message. Returns true if an
2187 invalid type-name was detected.
2188
2189 The situation handled by this function are variable declarations of the
2190 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2191 Usually, `ID' should name a type, but if we got here it means that it
2192 does not. We try to emit the best possible error message depending on
2193 how exactly the id-expression looks like. */
2194
2195 static bool
2196 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2197 {
2198 tree id;
2199
2200 cp_parser_parse_tentatively (parser);
2201 id = cp_parser_id_expression (parser,
2202 /*template_keyword_p=*/false,
2203 /*check_dependency_p=*/true,
2204 /*template_p=*/NULL,
2205 /*declarator_p=*/true,
2206 /*optional_p=*/false);
2207 /* After the id-expression, there should be a plain identifier,
2208 otherwise this is not a simple variable declaration. Also, if
2209 the scope is dependent, we cannot do much. */
2210 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2211 || (parser->scope && TYPE_P (parser->scope)
2212 && dependent_type_p (parser->scope)))
2213 {
2214 cp_parser_abort_tentative_parse (parser);
2215 return false;
2216 }
2217 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2218 return false;
2219
2220 /* Emit a diagnostic for the invalid type. */
2221 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2222 /* Skip to the end of the declaration; there's no point in
2223 trying to process it. */
2224 cp_parser_skip_to_end_of_block_or_statement (parser);
2225 return true;
2226 }
2227
2228 /* Consume tokens up to, and including, the next non-nested closing `)'.
2229 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2230 are doing error recovery. Returns -1 if OR_COMMA is true and we
2231 found an unnested comma. */
2232
2233 static int
2234 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2235 bool recovering,
2236 bool or_comma,
2237 bool consume_paren)
2238 {
2239 unsigned paren_depth = 0;
2240 unsigned brace_depth = 0;
2241
2242 if (recovering && !or_comma
2243 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2244 return 0;
2245
2246 while (true)
2247 {
2248 cp_token * token = cp_lexer_peek_token (parser->lexer);
2249
2250 switch (token->type)
2251 {
2252 case CPP_EOF:
2253 case CPP_PRAGMA_EOL:
2254 /* If we've run out of tokens, then there is no closing `)'. */
2255 return 0;
2256
2257 case CPP_SEMICOLON:
2258 /* This matches the processing in skip_to_end_of_statement. */
2259 if (!brace_depth)
2260 return 0;
2261 break;
2262
2263 case CPP_OPEN_BRACE:
2264 ++brace_depth;
2265 break;
2266 case CPP_CLOSE_BRACE:
2267 if (!brace_depth--)
2268 return 0;
2269 break;
2270
2271 case CPP_COMMA:
2272 if (recovering && or_comma && !brace_depth && !paren_depth)
2273 return -1;
2274 break;
2275
2276 case CPP_OPEN_PAREN:
2277 if (!brace_depth)
2278 ++paren_depth;
2279 break;
2280
2281 case CPP_CLOSE_PAREN:
2282 if (!brace_depth && !paren_depth--)
2283 {
2284 if (consume_paren)
2285 cp_lexer_consume_token (parser->lexer);
2286 return 1;
2287 }
2288 break;
2289
2290 default:
2291 break;
2292 }
2293
2294 /* Consume the token. */
2295 cp_lexer_consume_token (parser->lexer);
2296 }
2297 }
2298
2299 /* Consume tokens until we reach the end of the current statement.
2300 Normally, that will be just before consuming a `;'. However, if a
2301 non-nested `}' comes first, then we stop before consuming that. */
2302
2303 static void
2304 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2305 {
2306 unsigned nesting_depth = 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, stop. */
2317 return;
2318
2319 case CPP_SEMICOLON:
2320 /* If the next token is a `;', we have reached the end of the
2321 statement. */
2322 if (!nesting_depth)
2323 return;
2324 break;
2325
2326 case CPP_CLOSE_BRACE:
2327 /* If this is a non-nested '}', stop before consuming it.
2328 That way, when confronted with something like:
2329
2330 { 3 + }
2331
2332 we stop before consuming the closing '}', even though we
2333 have not yet reached a `;'. */
2334 if (nesting_depth == 0)
2335 return;
2336
2337 /* If it is the closing '}' for a block that we have
2338 scanned, stop -- but only after consuming the token.
2339 That way given:
2340
2341 void f g () { ... }
2342 typedef int I;
2343
2344 we will stop after the body of the erroneously declared
2345 function, but before consuming the following `typedef'
2346 declaration. */
2347 if (--nesting_depth == 0)
2348 {
2349 cp_lexer_consume_token (parser->lexer);
2350 return;
2351 }
2352
2353 case CPP_OPEN_BRACE:
2354 ++nesting_depth;
2355 break;
2356
2357 default:
2358 break;
2359 }
2360
2361 /* Consume the token. */
2362 cp_lexer_consume_token (parser->lexer);
2363 }
2364 }
2365
2366 /* This function is called at the end of a statement or declaration.
2367 If the next token is a semicolon, it is consumed; otherwise, error
2368 recovery is attempted. */
2369
2370 static void
2371 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2372 {
2373 /* Look for the trailing `;'. */
2374 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2375 {
2376 /* If there is additional (erroneous) input, skip to the end of
2377 the statement. */
2378 cp_parser_skip_to_end_of_statement (parser);
2379 /* If the next token is now a `;', consume it. */
2380 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2381 cp_lexer_consume_token (parser->lexer);
2382 }
2383 }
2384
2385 /* Skip tokens until we have consumed an entire block, or until we
2386 have consumed a non-nested `;'. */
2387
2388 static void
2389 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2390 {
2391 int nesting_depth = 0;
2392
2393 while (nesting_depth >= 0)
2394 {
2395 cp_token *token = cp_lexer_peek_token (parser->lexer);
2396
2397 switch (token->type)
2398 {
2399 case CPP_EOF:
2400 case CPP_PRAGMA_EOL:
2401 /* If we've run out of tokens, stop. */
2402 return;
2403
2404 case CPP_SEMICOLON:
2405 /* Stop if this is an unnested ';'. */
2406 if (!nesting_depth)
2407 nesting_depth = -1;
2408 break;
2409
2410 case CPP_CLOSE_BRACE:
2411 /* Stop if this is an unnested '}', or closes the outermost
2412 nesting level. */
2413 nesting_depth--;
2414 if (!nesting_depth)
2415 nesting_depth = -1;
2416 break;
2417
2418 case CPP_OPEN_BRACE:
2419 /* Nest. */
2420 nesting_depth++;
2421 break;
2422
2423 default:
2424 break;
2425 }
2426
2427 /* Consume the token. */
2428 cp_lexer_consume_token (parser->lexer);
2429 }
2430 }
2431
2432 /* Skip tokens until a non-nested closing curly brace is the next
2433 token. */
2434
2435 static void
2436 cp_parser_skip_to_closing_brace (cp_parser *parser)
2437 {
2438 unsigned nesting_depth = 0;
2439
2440 while (true)
2441 {
2442 cp_token *token = cp_lexer_peek_token (parser->lexer);
2443
2444 switch (token->type)
2445 {
2446 case CPP_EOF:
2447 case CPP_PRAGMA_EOL:
2448 /* If we've run out of tokens, stop. */
2449 return;
2450
2451 case CPP_CLOSE_BRACE:
2452 /* If the next token is a non-nested `}', then we have reached
2453 the end of the current block. */
2454 if (nesting_depth-- == 0)
2455 return;
2456 break;
2457
2458 case CPP_OPEN_BRACE:
2459 /* If it the next token is a `{', then we are entering a new
2460 block. Consume the entire block. */
2461 ++nesting_depth;
2462 break;
2463
2464 default:
2465 break;
2466 }
2467
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser->lexer);
2470 }
2471 }
2472
2473 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2474 parameter is the PRAGMA token, allowing us to purge the entire pragma
2475 sequence. */
2476
2477 static void
2478 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2479 {
2480 cp_token *token;
2481
2482 parser->lexer->in_pragma = false;
2483
2484 do
2485 token = cp_lexer_consume_token (parser->lexer);
2486 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2487
2488 /* Ensure that the pragma is not parsed again. */
2489 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2490 }
2491
2492 /* Require pragma end of line, resyncing with it as necessary. The
2493 arguments are as for cp_parser_skip_to_pragma_eol. */
2494
2495 static void
2496 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2497 {
2498 parser->lexer->in_pragma = false;
2499 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2501 }
2502
2503 /* This is a simple wrapper around make_typename_type. When the id is
2504 an unresolved identifier node, we can provide a superior diagnostic
2505 using cp_parser_diagnose_invalid_type_name. */
2506
2507 static tree
2508 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2509 {
2510 tree result;
2511 if (TREE_CODE (id) == IDENTIFIER_NODE)
2512 {
2513 result = make_typename_type (scope, id, typename_type,
2514 /*complain=*/tf_none);
2515 if (result == error_mark_node)
2516 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2517 return result;
2518 }
2519 return make_typename_type (scope, id, typename_type, tf_error);
2520 }
2521
2522
2523 /* Create a new C++ parser. */
2524
2525 static cp_parser *
2526 cp_parser_new (void)
2527 {
2528 cp_parser *parser;
2529 cp_lexer *lexer;
2530 unsigned i;
2531
2532 /* cp_lexer_new_main is called before calling ggc_alloc because
2533 cp_lexer_new_main might load a PCH file. */
2534 lexer = cp_lexer_new_main ();
2535
2536 /* Initialize the binops_by_token so that we can get the tree
2537 directly from the token. */
2538 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2539 binops_by_token[binops[i].token_type] = binops[i];
2540
2541 parser = GGC_CNEW (cp_parser);
2542 parser->lexer = lexer;
2543 parser->context = cp_parser_context_new (NULL);
2544
2545 /* For now, we always accept GNU extensions. */
2546 parser->allow_gnu_extensions_p = 1;
2547
2548 /* The `>' token is a greater-than operator, not the end of a
2549 template-id. */
2550 parser->greater_than_is_operator_p = true;
2551
2552 parser->default_arg_ok_p = true;
2553
2554 /* We are not parsing a constant-expression. */
2555 parser->integral_constant_expression_p = false;
2556 parser->allow_non_integral_constant_expression_p = false;
2557 parser->non_integral_constant_expression_p = false;
2558
2559 /* Local variable names are not forbidden. */
2560 parser->local_variables_forbidden_p = false;
2561
2562 /* We are not processing an `extern "C"' declaration. */
2563 parser->in_unbraced_linkage_specification_p = false;
2564
2565 /* We are not processing a declarator. */
2566 parser->in_declarator_p = false;
2567
2568 /* We are not processing a template-argument-list. */
2569 parser->in_template_argument_list_p = false;
2570
2571 /* We are not in an iteration statement. */
2572 parser->in_statement = 0;
2573
2574 /* We are not in a switch statement. */
2575 parser->in_switch_statement_p = false;
2576
2577 /* We are not parsing a type-id inside an expression. */
2578 parser->in_type_id_in_expr_p = false;
2579
2580 /* Declarations aren't implicitly extern "C". */
2581 parser->implicit_extern_c = false;
2582
2583 /* String literals should be translated to the execution character set. */
2584 parser->translate_strings_p = true;
2585
2586 /* The unparsed function queue is empty. */
2587 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2588
2589 /* There are no classes being defined. */
2590 parser->num_classes_being_defined = 0;
2591
2592 /* No template parameters apply. */
2593 parser->num_template_parameter_lists = 0;
2594
2595 return parser;
2596 }
2597
2598 /* Create a cp_lexer structure which will emit the tokens in CACHE
2599 and push it onto the parser's lexer stack. This is used for delayed
2600 parsing of in-class method bodies and default arguments, and should
2601 not be confused with tentative parsing. */
2602 static void
2603 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2604 {
2605 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2606 lexer->next = parser->lexer;
2607 parser->lexer = lexer;
2608
2609 /* Move the current source position to that of the first token in the
2610 new lexer. */
2611 cp_lexer_set_source_position_from_token (lexer->next_token);
2612 }
2613
2614 /* Pop the top lexer off the parser stack. This is never used for the
2615 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2616 static void
2617 cp_parser_pop_lexer (cp_parser *parser)
2618 {
2619 cp_lexer *lexer = parser->lexer;
2620 parser->lexer = lexer->next;
2621 cp_lexer_destroy (lexer);
2622
2623 /* Put the current source position back where it was before this
2624 lexer was pushed. */
2625 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2626 }
2627
2628 /* Lexical conventions [gram.lex] */
2629
2630 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2631 identifier. */
2632
2633 static tree
2634 cp_parser_identifier (cp_parser* parser)
2635 {
2636 cp_token *token;
2637
2638 /* Look for the identifier. */
2639 token = cp_parser_require (parser, CPP_NAME, "identifier");
2640 /* Return the value. */
2641 return token ? token->value : error_mark_node;
2642 }
2643
2644 /* Parse a sequence of adjacent string constants. Returns a
2645 TREE_STRING representing the combined, nul-terminated string
2646 constant. If TRANSLATE is true, translate the string to the
2647 execution character set. If WIDE_OK is true, a wide string is
2648 invalid here.
2649
2650 C++98 [lex.string] says that if a narrow string literal token is
2651 adjacent to a wide string literal token, the behavior is undefined.
2652 However, C99 6.4.5p4 says that this results in a wide string literal.
2653 We follow C99 here, for consistency with the C front end.
2654
2655 This code is largely lifted from lex_string() in c-lex.c.
2656
2657 FUTURE: ObjC++ will need to handle @-strings here. */
2658 static tree
2659 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2660 {
2661 tree value;
2662 bool wide = false;
2663 size_t count;
2664 struct obstack str_ob;
2665 cpp_string str, istr, *strs;
2666 cp_token *tok;
2667
2668 tok = cp_lexer_peek_token (parser->lexer);
2669 if (!cp_parser_is_string_literal (tok))
2670 {
2671 cp_parser_error (parser, "expected string-literal");
2672 return error_mark_node;
2673 }
2674
2675 /* Try to avoid the overhead of creating and destroying an obstack
2676 for the common case of just one string. */
2677 if (!cp_parser_is_string_literal
2678 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2679 {
2680 cp_lexer_consume_token (parser->lexer);
2681
2682 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2683 str.len = TREE_STRING_LENGTH (tok->value);
2684 count = 1;
2685 if (tok->type == CPP_WSTRING)
2686 wide = true;
2687
2688 strs = &str;
2689 }
2690 else
2691 {
2692 gcc_obstack_init (&str_ob);
2693 count = 0;
2694
2695 do
2696 {
2697 cp_lexer_consume_token (parser->lexer);
2698 count++;
2699 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2700 str.len = TREE_STRING_LENGTH (tok->value);
2701 if (tok->type == CPP_WSTRING)
2702 wide = true;
2703
2704 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2705
2706 tok = cp_lexer_peek_token (parser->lexer);
2707 }
2708 while (cp_parser_is_string_literal (tok));
2709
2710 strs = (cpp_string *) obstack_finish (&str_ob);
2711 }
2712
2713 if (wide && !wide_ok)
2714 {
2715 cp_parser_error (parser, "a wide string is invalid in this context");
2716 wide = false;
2717 }
2718
2719 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2720 (parse_in, strs, count, &istr, wide))
2721 {
2722 value = build_string (istr.len, (char *)istr.text);
2723 free ((void *)istr.text);
2724
2725 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2726 value = fix_string_type (value);
2727 }
2728 else
2729 /* cpp_interpret_string has issued an error. */
2730 value = error_mark_node;
2731
2732 if (count > 1)
2733 obstack_free (&str_ob, 0);
2734
2735 return value;
2736 }
2737
2738
2739 /* Basic concepts [gram.basic] */
2740
2741 /* Parse a translation-unit.
2742
2743 translation-unit:
2744 declaration-seq [opt]
2745
2746 Returns TRUE if all went well. */
2747
2748 static bool
2749 cp_parser_translation_unit (cp_parser* parser)
2750 {
2751 /* The address of the first non-permanent object on the declarator
2752 obstack. */
2753 static void *declarator_obstack_base;
2754
2755 bool success;
2756
2757 /* Create the declarator obstack, if necessary. */
2758 if (!cp_error_declarator)
2759 {
2760 gcc_obstack_init (&declarator_obstack);
2761 /* Create the error declarator. */
2762 cp_error_declarator = make_declarator (cdk_error);
2763 /* Create the empty parameter list. */
2764 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2765 /* Remember where the base of the declarator obstack lies. */
2766 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2767 }
2768
2769 cp_parser_declaration_seq_opt (parser);
2770
2771 /* If there are no tokens left then all went well. */
2772 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2773 {
2774 /* Get rid of the token array; we don't need it any more. */
2775 cp_lexer_destroy (parser->lexer);
2776 parser->lexer = NULL;
2777
2778 /* This file might have been a context that's implicitly extern
2779 "C". If so, pop the lang context. (Only relevant for PCH.) */
2780 if (parser->implicit_extern_c)
2781 {
2782 pop_lang_context ();
2783 parser->implicit_extern_c = false;
2784 }
2785
2786 /* Finish up. */
2787 finish_translation_unit ();
2788
2789 success = true;
2790 }
2791 else
2792 {
2793 cp_parser_error (parser, "expected declaration");
2794 success = false;
2795 }
2796
2797 /* Make sure the declarator obstack was fully cleaned up. */
2798 gcc_assert (obstack_next_free (&declarator_obstack)
2799 == declarator_obstack_base);
2800
2801 /* All went well. */
2802 return success;
2803 }
2804
2805 /* Expressions [gram.expr] */
2806
2807 /* Parse a primary-expression.
2808
2809 primary-expression:
2810 literal
2811 this
2812 ( expression )
2813 id-expression
2814
2815 GNU Extensions:
2816
2817 primary-expression:
2818 ( compound-statement )
2819 __builtin_va_arg ( assignment-expression , type-id )
2820 __builtin_offsetof ( type-id , offsetof-expression )
2821
2822 Objective-C++ Extension:
2823
2824 primary-expression:
2825 objc-expression
2826
2827 literal:
2828 __null
2829
2830 ADDRESS_P is true iff this expression was immediately preceded by
2831 "&" and therefore might denote a pointer-to-member. CAST_P is true
2832 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2833 true iff this expression is a template argument.
2834
2835 Returns a representation of the expression. Upon return, *IDK
2836 indicates what kind of id-expression (if any) was present. */
2837
2838 static tree
2839 cp_parser_primary_expression (cp_parser *parser,
2840 bool address_p,
2841 bool cast_p,
2842 bool template_arg_p,
2843 cp_id_kind *idk)
2844 {
2845 cp_token *token;
2846
2847 /* Assume the primary expression is not an id-expression. */
2848 *idk = CP_ID_KIND_NONE;
2849
2850 /* Peek at the next token. */
2851 token = cp_lexer_peek_token (parser->lexer);
2852 switch (token->type)
2853 {
2854 /* literal:
2855 integer-literal
2856 character-literal
2857 floating-literal
2858 string-literal
2859 boolean-literal */
2860 case CPP_CHAR:
2861 case CPP_WCHAR:
2862 case CPP_NUMBER:
2863 token = cp_lexer_consume_token (parser->lexer);
2864 /* Floating-point literals are only allowed in an integral
2865 constant expression if they are cast to an integral or
2866 enumeration type. */
2867 if (TREE_CODE (token->value) == REAL_CST
2868 && parser->integral_constant_expression_p
2869 && pedantic)
2870 {
2871 /* CAST_P will be set even in invalid code like "int(2.7 +
2872 ...)". Therefore, we have to check that the next token
2873 is sure to end the cast. */
2874 if (cast_p)
2875 {
2876 cp_token *next_token;
2877
2878 next_token = cp_lexer_peek_token (parser->lexer);
2879 if (/* The comma at the end of an
2880 enumerator-definition. */
2881 next_token->type != CPP_COMMA
2882 /* The curly brace at the end of an enum-specifier. */
2883 && next_token->type != CPP_CLOSE_BRACE
2884 /* The end of a statement. */
2885 && next_token->type != CPP_SEMICOLON
2886 /* The end of the cast-expression. */
2887 && next_token->type != CPP_CLOSE_PAREN
2888 /* The end of an array bound. */
2889 && next_token->type != CPP_CLOSE_SQUARE
2890 /* The closing ">" in a template-argument-list. */
2891 && (next_token->type != CPP_GREATER
2892 || parser->greater_than_is_operator_p))
2893 cast_p = false;
2894 }
2895
2896 /* If we are within a cast, then the constraint that the
2897 cast is to an integral or enumeration type will be
2898 checked at that point. If we are not within a cast, then
2899 this code is invalid. */
2900 if (!cast_p)
2901 cp_parser_non_integral_constant_expression
2902 (parser, "floating-point literal");
2903 }
2904 return token->value;
2905
2906 case CPP_STRING:
2907 case CPP_WSTRING:
2908 /* ??? Should wide strings be allowed when parser->translate_strings_p
2909 is false (i.e. in attributes)? If not, we can kill the third
2910 argument to cp_parser_string_literal. */
2911 return cp_parser_string_literal (parser,
2912 parser->translate_strings_p,
2913 true);
2914
2915 case CPP_OPEN_PAREN:
2916 {
2917 tree expr;
2918 bool saved_greater_than_is_operator_p;
2919
2920 /* Consume the `('. */
2921 cp_lexer_consume_token (parser->lexer);
2922 /* Within a parenthesized expression, a `>' token is always
2923 the greater-than operator. */
2924 saved_greater_than_is_operator_p
2925 = parser->greater_than_is_operator_p;
2926 parser->greater_than_is_operator_p = true;
2927 /* If we see `( { ' then we are looking at the beginning of
2928 a GNU statement-expression. */
2929 if (cp_parser_allow_gnu_extensions_p (parser)
2930 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2931 {
2932 /* Statement-expressions are not allowed by the standard. */
2933 if (pedantic)
2934 pedwarn ("ISO C++ forbids braced-groups within expressions");
2935
2936 /* And they're not allowed outside of a function-body; you
2937 cannot, for example, write:
2938
2939 int i = ({ int j = 3; j + 1; });
2940
2941 at class or namespace scope. */
2942 if (!at_function_scope_p ())
2943 error ("statement-expressions are allowed only inside functions");
2944 /* Start the statement-expression. */
2945 expr = begin_stmt_expr ();
2946 /* Parse the compound-statement. */
2947 cp_parser_compound_statement (parser, expr, false);
2948 /* Finish up. */
2949 expr = finish_stmt_expr (expr, false);
2950 }
2951 else
2952 {
2953 /* Parse the parenthesized expression. */
2954 expr = cp_parser_expression (parser, cast_p);
2955 /* Let the front end know that this expression was
2956 enclosed in parentheses. This matters in case, for
2957 example, the expression is of the form `A::B', since
2958 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2959 not. */
2960 finish_parenthesized_expr (expr);
2961 }
2962 /* The `>' token might be the end of a template-id or
2963 template-parameter-list now. */
2964 parser->greater_than_is_operator_p
2965 = saved_greater_than_is_operator_p;
2966 /* Consume the `)'. */
2967 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2968 cp_parser_skip_to_end_of_statement (parser);
2969
2970 return expr;
2971 }
2972
2973 case CPP_KEYWORD:
2974 switch (token->keyword)
2975 {
2976 /* These two are the boolean literals. */
2977 case RID_TRUE:
2978 cp_lexer_consume_token (parser->lexer);
2979 return boolean_true_node;
2980 case RID_FALSE:
2981 cp_lexer_consume_token (parser->lexer);
2982 return boolean_false_node;
2983
2984 /* The `__null' literal. */
2985 case RID_NULL:
2986 cp_lexer_consume_token (parser->lexer);
2987 return null_node;
2988
2989 /* Recognize the `this' keyword. */
2990 case RID_THIS:
2991 cp_lexer_consume_token (parser->lexer);
2992 if (parser->local_variables_forbidden_p)
2993 {
2994 error ("%<this%> may not be used in this context");
2995 return error_mark_node;
2996 }
2997 /* Pointers cannot appear in constant-expressions. */
2998 if (cp_parser_non_integral_constant_expression (parser,
2999 "`this'"))
3000 return error_mark_node;
3001 return finish_this_expr ();
3002
3003 /* The `operator' keyword can be the beginning of an
3004 id-expression. */
3005 case RID_OPERATOR:
3006 goto id_expression;
3007
3008 case RID_FUNCTION_NAME:
3009 case RID_PRETTY_FUNCTION_NAME:
3010 case RID_C99_FUNCTION_NAME:
3011 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3012 __func__ are the names of variables -- but they are
3013 treated specially. Therefore, they are handled here,
3014 rather than relying on the generic id-expression logic
3015 below. Grammatically, these names are id-expressions.
3016
3017 Consume the token. */
3018 token = cp_lexer_consume_token (parser->lexer);
3019 /* Look up the name. */
3020 return finish_fname (token->value);
3021
3022 case RID_VA_ARG:
3023 {
3024 tree expression;
3025 tree type;
3026
3027 /* The `__builtin_va_arg' construct is used to handle
3028 `va_arg'. Consume the `__builtin_va_arg' token. */
3029 cp_lexer_consume_token (parser->lexer);
3030 /* Look for the opening `('. */
3031 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3032 /* Now, parse the assignment-expression. */
3033 expression = cp_parser_assignment_expression (parser,
3034 /*cast_p=*/false);
3035 /* Look for the `,'. */
3036 cp_parser_require (parser, CPP_COMMA, "`,'");
3037 /* Parse the type-id. */
3038 type = cp_parser_type_id (parser);
3039 /* Look for the closing `)'. */
3040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3041 /* Using `va_arg' in a constant-expression is not
3042 allowed. */
3043 if (cp_parser_non_integral_constant_expression (parser,
3044 "`va_arg'"))
3045 return error_mark_node;
3046 return build_x_va_arg (expression, type);
3047 }
3048
3049 case RID_OFFSETOF:
3050 return cp_parser_builtin_offsetof (parser);
3051
3052 /* Objective-C++ expressions. */
3053 case RID_AT_ENCODE:
3054 case RID_AT_PROTOCOL:
3055 case RID_AT_SELECTOR:
3056 return cp_parser_objc_expression (parser);
3057
3058 default:
3059 cp_parser_error (parser, "expected primary-expression");
3060 return error_mark_node;
3061 }
3062
3063 /* An id-expression can start with either an identifier, a
3064 `::' as the beginning of a qualified-id, or the "operator"
3065 keyword. */
3066 case CPP_NAME:
3067 case CPP_SCOPE:
3068 case CPP_TEMPLATE_ID:
3069 case CPP_NESTED_NAME_SPECIFIER:
3070 {
3071 tree id_expression;
3072 tree decl;
3073 const char *error_msg;
3074 bool template_p;
3075 bool done;
3076
3077 id_expression:
3078 /* Parse the id-expression. */
3079 id_expression
3080 = cp_parser_id_expression (parser,
3081 /*template_keyword_p=*/false,
3082 /*check_dependency_p=*/true,
3083 &template_p,
3084 /*declarator_p=*/false,
3085 /*optional_p=*/false);
3086 if (id_expression == error_mark_node)
3087 return error_mark_node;
3088 token = cp_lexer_peek_token (parser->lexer);
3089 done = (token->type != CPP_OPEN_SQUARE
3090 && token->type != CPP_OPEN_PAREN
3091 && token->type != CPP_DOT
3092 && token->type != CPP_DEREF
3093 && token->type != CPP_PLUS_PLUS
3094 && token->type != CPP_MINUS_MINUS);
3095 /* If we have a template-id, then no further lookup is
3096 required. If the template-id was for a template-class, we
3097 will sometimes have a TYPE_DECL at this point. */
3098 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3099 || TREE_CODE (id_expression) == TYPE_DECL)
3100 decl = id_expression;
3101 /* Look up the name. */
3102 else
3103 {
3104 tree ambiguous_decls;
3105
3106 decl = cp_parser_lookup_name (parser, id_expression,
3107 none_type,
3108 template_p,
3109 /*is_namespace=*/false,
3110 /*check_dependency=*/true,
3111 &ambiguous_decls);
3112 /* If the lookup was ambiguous, an error will already have
3113 been issued. */
3114 if (ambiguous_decls)
3115 return error_mark_node;
3116
3117 /* In Objective-C++, an instance variable (ivar) may be preferred
3118 to whatever cp_parser_lookup_name() found. */
3119 decl = objc_lookup_ivar (decl, id_expression);
3120
3121 /* If name lookup gives us a SCOPE_REF, then the
3122 qualifying scope was dependent. */
3123 if (TREE_CODE (decl) == SCOPE_REF)
3124 return decl;
3125 /* Check to see if DECL is a local variable in a context
3126 where that is forbidden. */
3127 if (parser->local_variables_forbidden_p
3128 && local_variable_p (decl))
3129 {
3130 /* It might be that we only found DECL because we are
3131 trying to be generous with pre-ISO scoping rules.
3132 For example, consider:
3133
3134 int i;
3135 void g() {
3136 for (int i = 0; i < 10; ++i) {}
3137 extern void f(int j = i);
3138 }
3139
3140 Here, name look up will originally find the out
3141 of scope `i'. We need to issue a warning message,
3142 but then use the global `i'. */
3143 decl = check_for_out_of_scope_variable (decl);
3144 if (local_variable_p (decl))
3145 {
3146 error ("local variable %qD may not appear in this context",
3147 decl);
3148 return error_mark_node;
3149 }
3150 }
3151 }
3152
3153 decl = (finish_id_expression
3154 (id_expression, decl, parser->scope,
3155 idk,
3156 parser->integral_constant_expression_p,
3157 parser->allow_non_integral_constant_expression_p,
3158 &parser->non_integral_constant_expression_p,
3159 template_p, done, address_p,
3160 template_arg_p,
3161 &error_msg));
3162 if (error_msg)
3163 cp_parser_error (parser, error_msg);
3164 return decl;
3165 }
3166
3167 /* Anything else is an error. */
3168 default:
3169 /* ...unless we have an Objective-C++ message or string literal, that is. */
3170 if (c_dialect_objc ()
3171 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3172 return cp_parser_objc_expression (parser);
3173
3174 cp_parser_error (parser, "expected primary-expression");
3175 return error_mark_node;
3176 }
3177 }
3178
3179 /* Parse an id-expression.
3180
3181 id-expression:
3182 unqualified-id
3183 qualified-id
3184
3185 qualified-id:
3186 :: [opt] nested-name-specifier template [opt] unqualified-id
3187 :: identifier
3188 :: operator-function-id
3189 :: template-id
3190
3191 Return a representation of the unqualified portion of the
3192 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3193 a `::' or nested-name-specifier.
3194
3195 Often, if the id-expression was a qualified-id, the caller will
3196 want to make a SCOPE_REF to represent the qualified-id. This
3197 function does not do this in order to avoid wastefully creating
3198 SCOPE_REFs when they are not required.
3199
3200 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3201 `template' keyword.
3202
3203 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3204 uninstantiated templates.
3205
3206 If *TEMPLATE_P is non-NULL, it is set to true iff the
3207 `template' keyword is used to explicitly indicate that the entity
3208 named is a template.
3209
3210 If DECLARATOR_P is true, the id-expression is appearing as part of
3211 a declarator, rather than as part of an expression. */
3212
3213 static tree
3214 cp_parser_id_expression (cp_parser *parser,
3215 bool template_keyword_p,
3216 bool check_dependency_p,
3217 bool *template_p,
3218 bool declarator_p,
3219 bool optional_p)
3220 {
3221 bool global_scope_p;
3222 bool nested_name_specifier_p;
3223
3224 /* Assume the `template' keyword was not used. */
3225 if (template_p)
3226 *template_p = template_keyword_p;
3227
3228 /* Look for the optional `::' operator. */
3229 global_scope_p
3230 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3231 != NULL_TREE);
3232 /* Look for the optional nested-name-specifier. */
3233 nested_name_specifier_p
3234 = (cp_parser_nested_name_specifier_opt (parser,
3235 /*typename_keyword_p=*/false,
3236 check_dependency_p,
3237 /*type_p=*/false,
3238 declarator_p)
3239 != NULL_TREE);
3240 /* If there is a nested-name-specifier, then we are looking at
3241 the first qualified-id production. */
3242 if (nested_name_specifier_p)
3243 {
3244 tree saved_scope;
3245 tree saved_object_scope;
3246 tree saved_qualifying_scope;
3247 tree unqualified_id;
3248 bool is_template;
3249
3250 /* See if the next token is the `template' keyword. */
3251 if (!template_p)
3252 template_p = &is_template;
3253 *template_p = cp_parser_optional_template_keyword (parser);
3254 /* Name lookup we do during the processing of the
3255 unqualified-id might obliterate SCOPE. */
3256 saved_scope = parser->scope;
3257 saved_object_scope = parser->object_scope;
3258 saved_qualifying_scope = parser->qualifying_scope;
3259 /* Process the final unqualified-id. */
3260 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3261 check_dependency_p,
3262 declarator_p,
3263 /*optional_p=*/false);
3264 /* Restore the SAVED_SCOPE for our caller. */
3265 parser->scope = saved_scope;
3266 parser->object_scope = saved_object_scope;
3267 parser->qualifying_scope = saved_qualifying_scope;
3268
3269 return unqualified_id;
3270 }
3271 /* Otherwise, if we are in global scope, then we are looking at one
3272 of the other qualified-id productions. */
3273 else if (global_scope_p)
3274 {
3275 cp_token *token;
3276 tree id;
3277
3278 /* Peek at the next token. */
3279 token = cp_lexer_peek_token (parser->lexer);
3280
3281 /* If it's an identifier, and the next token is not a "<", then
3282 we can avoid the template-id case. This is an optimization
3283 for this common case. */
3284 if (token->type == CPP_NAME
3285 && !cp_parser_nth_token_starts_template_argument_list_p
3286 (parser, 2))
3287 return cp_parser_identifier (parser);
3288
3289 cp_parser_parse_tentatively (parser);
3290 /* Try a template-id. */
3291 id = cp_parser_template_id (parser,
3292 /*template_keyword_p=*/false,
3293 /*check_dependency_p=*/true,
3294 declarator_p);
3295 /* If that worked, we're done. */
3296 if (cp_parser_parse_definitely (parser))
3297 return id;
3298
3299 /* Peek at the next token. (Changes in the token buffer may
3300 have invalidated the pointer obtained above.) */
3301 token = cp_lexer_peek_token (parser->lexer);
3302
3303 switch (token->type)
3304 {
3305 case CPP_NAME:
3306 return cp_parser_identifier (parser);
3307
3308 case CPP_KEYWORD:
3309 if (token->keyword == RID_OPERATOR)
3310 return cp_parser_operator_function_id (parser);
3311 /* Fall through. */
3312
3313 default:
3314 cp_parser_error (parser, "expected id-expression");
3315 return error_mark_node;
3316 }
3317 }
3318 else
3319 return cp_parser_unqualified_id (parser, template_keyword_p,
3320 /*check_dependency_p=*/true,
3321 declarator_p,
3322 optional_p);
3323 }
3324
3325 /* Parse an unqualified-id.
3326
3327 unqualified-id:
3328 identifier
3329 operator-function-id
3330 conversion-function-id
3331 ~ class-name
3332 template-id
3333
3334 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3335 keyword, in a construct like `A::template ...'.
3336
3337 Returns a representation of unqualified-id. For the `identifier'
3338 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3339 production a BIT_NOT_EXPR is returned; the operand of the
3340 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3341 other productions, see the documentation accompanying the
3342 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3343 names are looked up in uninstantiated templates. If DECLARATOR_P
3344 is true, the unqualified-id is appearing as part of a declarator,
3345 rather than as part of an expression. */
3346
3347 static tree
3348 cp_parser_unqualified_id (cp_parser* parser,
3349 bool template_keyword_p,
3350 bool check_dependency_p,
3351 bool declarator_p,
3352 bool optional_p)
3353 {
3354 cp_token *token;
3355
3356 /* Peek at the next token. */
3357 token = cp_lexer_peek_token (parser->lexer);
3358
3359 switch (token->type)
3360 {
3361 case CPP_NAME:
3362 {
3363 tree id;
3364
3365 /* We don't know yet whether or not this will be a
3366 template-id. */
3367 cp_parser_parse_tentatively (parser);
3368 /* Try a template-id. */
3369 id = cp_parser_template_id (parser, template_keyword_p,
3370 check_dependency_p,
3371 declarator_p);
3372 /* If it worked, we're done. */
3373 if (cp_parser_parse_definitely (parser))
3374 return id;
3375 /* Otherwise, it's an ordinary identifier. */
3376 return cp_parser_identifier (parser);
3377 }
3378
3379 case CPP_TEMPLATE_ID:
3380 return cp_parser_template_id (parser, template_keyword_p,
3381 check_dependency_p,
3382 declarator_p);
3383
3384 case CPP_COMPL:
3385 {
3386 tree type_decl;
3387 tree qualifying_scope;
3388 tree object_scope;
3389 tree scope;
3390 bool done;
3391
3392 /* Consume the `~' token. */
3393 cp_lexer_consume_token (parser->lexer);
3394 /* Parse the class-name. The standard, as written, seems to
3395 say that:
3396
3397 template <typename T> struct S { ~S (); };
3398 template <typename T> S<T>::~S() {}
3399
3400 is invalid, since `~' must be followed by a class-name, but
3401 `S<T>' is dependent, and so not known to be a class.
3402 That's not right; we need to look in uninstantiated
3403 templates. A further complication arises from:
3404
3405 template <typename T> void f(T t) {
3406 t.T::~T();
3407 }
3408
3409 Here, it is not possible to look up `T' in the scope of `T'
3410 itself. We must look in both the current scope, and the
3411 scope of the containing complete expression.
3412
3413 Yet another issue is:
3414
3415 struct S {
3416 int S;
3417 ~S();
3418 };
3419
3420 S::~S() {}
3421
3422 The standard does not seem to say that the `S' in `~S'
3423 should refer to the type `S' and not the data member
3424 `S::S'. */
3425
3426 /* DR 244 says that we look up the name after the "~" in the
3427 same scope as we looked up the qualifying name. That idea
3428 isn't fully worked out; it's more complicated than that. */
3429 scope = parser->scope;
3430 object_scope = parser->object_scope;
3431 qualifying_scope = parser->qualifying_scope;
3432
3433 /* Check for invalid scopes. */
3434 if (scope == error_mark_node)
3435 {
3436 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3437 cp_lexer_consume_token (parser->lexer);
3438 return error_mark_node;
3439 }
3440 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3441 {
3442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3443 error ("scope %qT before %<~%> is not a class-name", scope);
3444 cp_parser_simulate_error (parser);
3445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3446 cp_lexer_consume_token (parser->lexer);
3447 return error_mark_node;
3448 }
3449 gcc_assert (!scope || TYPE_P (scope));
3450
3451 /* If the name is of the form "X::~X" it's OK. */
3452 token = cp_lexer_peek_token (parser->lexer);
3453 if (scope
3454 && token->type == CPP_NAME
3455 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3456 == CPP_OPEN_PAREN)
3457 && constructor_name_p (token->value, scope))
3458 {
3459 cp_lexer_consume_token (parser->lexer);
3460 return build_nt (BIT_NOT_EXPR, scope);
3461 }
3462
3463 /* If there was an explicit qualification (S::~T), first look
3464 in the scope given by the qualification (i.e., S). */
3465 done = false;
3466 type_decl = NULL_TREE;
3467 if (scope)
3468 {
3469 cp_parser_parse_tentatively (parser);
3470 type_decl = cp_parser_class_name (parser,
3471 /*typename_keyword_p=*/false,
3472 /*template_keyword_p=*/false,
3473 none_type,
3474 /*check_dependency=*/false,
3475 /*class_head_p=*/false,
3476 declarator_p);
3477 if (cp_parser_parse_definitely (parser))
3478 done = true;
3479 }
3480 /* In "N::S::~S", look in "N" as well. */
3481 if (!done && scope && qualifying_scope)
3482 {
3483 cp_parser_parse_tentatively (parser);
3484 parser->scope = qualifying_scope;
3485 parser->object_scope = NULL_TREE;
3486 parser->qualifying_scope = NULL_TREE;
3487 type_decl
3488 = cp_parser_class_name (parser,
3489 /*typename_keyword_p=*/false,
3490 /*template_keyword_p=*/false,
3491 none_type,
3492 /*check_dependency=*/false,
3493 /*class_head_p=*/false,
3494 declarator_p);
3495 if (cp_parser_parse_definitely (parser))
3496 done = true;
3497 }
3498 /* In "p->S::~T", look in the scope given by "*p" as well. */
3499 else if (!done && object_scope)
3500 {
3501 cp_parser_parse_tentatively (parser);
3502 parser->scope = object_scope;
3503 parser->object_scope = NULL_TREE;
3504 parser->qualifying_scope = NULL_TREE;
3505 type_decl
3506 = cp_parser_class_name (parser,
3507 /*typename_keyword_p=*/false,
3508 /*template_keyword_p=*/false,
3509 none_type,
3510 /*check_dependency=*/false,
3511 /*class_head_p=*/false,
3512 declarator_p);
3513 if (cp_parser_parse_definitely (parser))
3514 done = true;
3515 }
3516 /* Look in the surrounding context. */
3517 if (!done)
3518 {
3519 parser->scope = NULL_TREE;
3520 parser->object_scope = NULL_TREE;
3521 parser->qualifying_scope = NULL_TREE;
3522 type_decl
3523 = cp_parser_class_name (parser,
3524 /*typename_keyword_p=*/false,
3525 /*template_keyword_p=*/false,
3526 none_type,
3527 /*check_dependency=*/false,
3528 /*class_head_p=*/false,
3529 declarator_p);
3530 }
3531 /* If an error occurred, assume that the name of the
3532 destructor is the same as the name of the qualifying
3533 class. That allows us to keep parsing after running
3534 into ill-formed destructor names. */
3535 if (type_decl == error_mark_node && scope)
3536 return build_nt (BIT_NOT_EXPR, scope);
3537 else if (type_decl == error_mark_node)
3538 return error_mark_node;
3539
3540 /* Check that destructor name and scope match. */
3541 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3542 {
3543 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3544 error ("declaration of %<~%T%> as member of %qT",
3545 type_decl, scope);
3546 cp_parser_simulate_error (parser);
3547 return error_mark_node;
3548 }
3549
3550 /* [class.dtor]
3551
3552 A typedef-name that names a class shall not be used as the
3553 identifier in the declarator for a destructor declaration. */
3554 if (declarator_p
3555 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3556 && !DECL_SELF_REFERENCE_P (type_decl)
3557 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3558 error ("typedef-name %qD used as destructor declarator",
3559 type_decl);
3560
3561 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3562 }
3563
3564 case CPP_KEYWORD:
3565 if (token->keyword == RID_OPERATOR)
3566 {
3567 tree id;
3568
3569 /* This could be a template-id, so we try that first. */
3570 cp_parser_parse_tentatively (parser);
3571 /* Try a template-id. */
3572 id = cp_parser_template_id (parser, template_keyword_p,
3573 /*check_dependency_p=*/true,
3574 declarator_p);
3575 /* If that worked, we're done. */
3576 if (cp_parser_parse_definitely (parser))
3577 return id;
3578 /* We still don't know whether we're looking at an
3579 operator-function-id or a conversion-function-id. */
3580 cp_parser_parse_tentatively (parser);
3581 /* Try an operator-function-id. */
3582 id = cp_parser_operator_function_id (parser);
3583 /* If that didn't work, try a conversion-function-id. */
3584 if (!cp_parser_parse_definitely (parser))
3585 id = cp_parser_conversion_function_id (parser);
3586
3587 return id;
3588 }
3589 /* Fall through. */
3590
3591 default:
3592 if (optional_p)
3593 return NULL_TREE;
3594 cp_parser_error (parser, "expected unqualified-id");
3595 return error_mark_node;
3596 }
3597 }
3598
3599 /* Parse an (optional) nested-name-specifier.
3600
3601 nested-name-specifier:
3602 class-or-namespace-name :: nested-name-specifier [opt]
3603 class-or-namespace-name :: template nested-name-specifier [opt]
3604
3605 PARSER->SCOPE should be set appropriately before this function is
3606 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3607 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3608 in name lookups.
3609
3610 Sets PARSER->SCOPE to the class (TYPE) or namespace
3611 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3612 it unchanged if there is no nested-name-specifier. Returns the new
3613 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3614
3615 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3616 part of a declaration and/or decl-specifier. */
3617
3618 static tree
3619 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3620 bool typename_keyword_p,
3621 bool check_dependency_p,
3622 bool type_p,
3623 bool is_declaration)
3624 {
3625 bool success = false;
3626 cp_token_position start = 0;
3627 cp_token *token;
3628
3629 /* Remember where the nested-name-specifier starts. */
3630 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3631 {
3632 start = cp_lexer_token_position (parser->lexer, false);
3633 push_deferring_access_checks (dk_deferred);
3634 }
3635
3636 while (true)
3637 {
3638 tree new_scope;
3639 tree old_scope;
3640 tree saved_qualifying_scope;
3641 bool template_keyword_p;
3642
3643 /* Spot cases that cannot be the beginning of a
3644 nested-name-specifier. */
3645 token = cp_lexer_peek_token (parser->lexer);
3646
3647 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3648 the already parsed nested-name-specifier. */
3649 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3650 {
3651 /* Grab the nested-name-specifier and continue the loop. */
3652 cp_parser_pre_parsed_nested_name_specifier (parser);
3653 /* If we originally encountered this nested-name-specifier
3654 with IS_DECLARATION set to false, we will not have
3655 resolved TYPENAME_TYPEs, so we must do so here. */
3656 if (is_declaration
3657 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3658 {
3659 new_scope = resolve_typename_type (parser->scope,
3660 /*only_current_p=*/false);
3661 if (new_scope != error_mark_node)
3662 parser->scope = new_scope;
3663 }
3664 success = true;
3665 continue;
3666 }
3667
3668 /* Spot cases that cannot be the beginning of a
3669 nested-name-specifier. On the second and subsequent times
3670 through the loop, we look for the `template' keyword. */
3671 if (success && token->keyword == RID_TEMPLATE)
3672 ;
3673 /* A template-id can start a nested-name-specifier. */
3674 else if (token->type == CPP_TEMPLATE_ID)
3675 ;
3676 else
3677 {
3678 /* If the next token is not an identifier, then it is
3679 definitely not a class-or-namespace-name. */
3680 if (token->type != CPP_NAME)
3681 break;
3682 /* If the following token is neither a `<' (to begin a
3683 template-id), nor a `::', then we are not looking at a
3684 nested-name-specifier. */
3685 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3686 if (token->type != CPP_SCOPE
3687 && !cp_parser_nth_token_starts_template_argument_list_p
3688 (parser, 2))
3689 break;
3690 }
3691
3692 /* The nested-name-specifier is optional, so we parse
3693 tentatively. */
3694 cp_parser_parse_tentatively (parser);
3695
3696 /* Look for the optional `template' keyword, if this isn't the
3697 first time through the loop. */
3698 if (success)
3699 template_keyword_p = cp_parser_optional_template_keyword (parser);
3700 else
3701 template_keyword_p = false;
3702
3703 /* Save the old scope since the name lookup we are about to do
3704 might destroy it. */
3705 old_scope = parser->scope;
3706 saved_qualifying_scope = parser->qualifying_scope;
3707 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3708 look up names in "X<T>::I" in order to determine that "Y" is
3709 a template. So, if we have a typename at this point, we make
3710 an effort to look through it. */
3711 if (is_declaration
3712 && !typename_keyword_p
3713 && parser->scope
3714 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3715 parser->scope = resolve_typename_type (parser->scope,
3716 /*only_current_p=*/false);
3717 /* Parse the qualifying entity. */
3718 new_scope
3719 = cp_parser_class_or_namespace_name (parser,
3720 typename_keyword_p,
3721 template_keyword_p,
3722 check_dependency_p,
3723 type_p,
3724 is_declaration);
3725 /* Look for the `::' token. */
3726 cp_parser_require (parser, CPP_SCOPE, "`::'");
3727
3728 /* If we found what we wanted, we keep going; otherwise, we're
3729 done. */
3730 if (!cp_parser_parse_definitely (parser))
3731 {
3732 bool error_p = false;
3733
3734 /* Restore the OLD_SCOPE since it was valid before the
3735 failed attempt at finding the last
3736 class-or-namespace-name. */
3737 parser->scope = old_scope;
3738 parser->qualifying_scope = saved_qualifying_scope;
3739 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3740 break;
3741 /* If the next token is an identifier, and the one after
3742 that is a `::', then any valid interpretation would have
3743 found a class-or-namespace-name. */
3744 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3745 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3746 == CPP_SCOPE)
3747 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3748 != CPP_COMPL))
3749 {
3750 token = cp_lexer_consume_token (parser->lexer);
3751 if (!error_p)
3752 {
3753 if (!token->ambiguous_p)
3754 {
3755 tree decl;
3756 tree ambiguous_decls;
3757
3758 decl = cp_parser_lookup_name (parser, token->value,
3759 none_type,
3760 /*is_template=*/false,
3761 /*is_namespace=*/false,
3762 /*check_dependency=*/true,
3763 &ambiguous_decls);
3764 if (TREE_CODE (decl) == TEMPLATE_DECL)
3765 error ("%qD used without template parameters", decl);
3766 else if (ambiguous_decls)
3767 {
3768 error ("reference to %qD is ambiguous",
3769 token->value);
3770 print_candidates (ambiguous_decls);
3771 decl = error_mark_node;
3772 }
3773 else
3774 cp_parser_name_lookup_error
3775 (parser, token->value, decl,
3776 "is not a class or namespace");
3777 }
3778 parser->scope = error_mark_node;
3779 error_p = true;
3780 /* Treat this as a successful nested-name-specifier
3781 due to:
3782
3783 [basic.lookup.qual]
3784
3785 If the name found is not a class-name (clause
3786 _class_) or namespace-name (_namespace.def_), the
3787 program is ill-formed. */
3788 success = true;
3789 }
3790 cp_lexer_consume_token (parser->lexer);
3791 }
3792 break;
3793 }
3794 /* We've found one valid nested-name-specifier. */
3795 success = true;
3796 /* Name lookup always gives us a DECL. */
3797 if (TREE_CODE (new_scope) == TYPE_DECL)
3798 new_scope = TREE_TYPE (new_scope);
3799 /* Uses of "template" must be followed by actual templates. */
3800 if (template_keyword_p
3801 && !(CLASS_TYPE_P (new_scope)
3802 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3803 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3804 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3805 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3806 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3807 == TEMPLATE_ID_EXPR)))
3808 pedwarn (TYPE_P (new_scope)
3809 ? "%qT is not a template"
3810 : "%qD is not a template",
3811 new_scope);
3812 /* If it is a class scope, try to complete it; we are about to
3813 be looking up names inside the class. */
3814 if (TYPE_P (new_scope)
3815 /* Since checking types for dependency can be expensive,
3816 avoid doing it if the type is already complete. */
3817 && !COMPLETE_TYPE_P (new_scope)
3818 /* Do not try to complete dependent types. */
3819 && !dependent_type_p (new_scope))
3820 new_scope = complete_type (new_scope);
3821 /* Make sure we look in the right scope the next time through
3822 the loop. */
3823 parser->scope = new_scope;
3824 }
3825
3826 /* If parsing tentatively, replace the sequence of tokens that makes
3827 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3828 token. That way, should we re-parse the token stream, we will
3829 not have to repeat the effort required to do the parse, nor will
3830 we issue duplicate error messages. */
3831 if (success && start)
3832 {
3833 cp_token *token;
3834 tree access_checks;
3835
3836 token = cp_lexer_token_at (parser->lexer, start);
3837 /* Reset the contents of the START token. */
3838 token->type = CPP_NESTED_NAME_SPECIFIER;
3839 /* Retrieve any deferred checks. Do not pop this access checks yet
3840 so the memory will not be reclaimed during token replacing below. */
3841 access_checks = get_deferred_access_checks ();
3842 token->value = build_tree_list (copy_list (access_checks),
3843 parser->scope);
3844 TREE_TYPE (token->value) = parser->qualifying_scope;
3845 token->keyword = RID_MAX;
3846
3847 /* Purge all subsequent tokens. */
3848 cp_lexer_purge_tokens_after (parser->lexer, start);
3849 }
3850
3851 if (start)
3852 pop_to_parent_deferring_access_checks ();
3853
3854 return success ? parser->scope : NULL_TREE;
3855 }
3856
3857 /* Parse a nested-name-specifier. See
3858 cp_parser_nested_name_specifier_opt for details. This function
3859 behaves identically, except that it will an issue an error if no
3860 nested-name-specifier is present. */
3861
3862 static tree
3863 cp_parser_nested_name_specifier (cp_parser *parser,
3864 bool typename_keyword_p,
3865 bool check_dependency_p,
3866 bool type_p,
3867 bool is_declaration)
3868 {
3869 tree scope;
3870
3871 /* Look for the nested-name-specifier. */
3872 scope = cp_parser_nested_name_specifier_opt (parser,
3873 typename_keyword_p,
3874 check_dependency_p,
3875 type_p,
3876 is_declaration);
3877 /* If it was not present, issue an error message. */
3878 if (!scope)
3879 {
3880 cp_parser_error (parser, "expected nested-name-specifier");
3881 parser->scope = NULL_TREE;
3882 }
3883
3884 return scope;
3885 }
3886
3887 /* Parse a class-or-namespace-name.
3888
3889 class-or-namespace-name:
3890 class-name
3891 namespace-name
3892
3893 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3894 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3895 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3896 TYPE_P is TRUE iff the next name should be taken as a class-name,
3897 even the same name is declared to be another entity in the same
3898 scope.
3899
3900 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3901 specified by the class-or-namespace-name. If neither is found the
3902 ERROR_MARK_NODE is returned. */
3903
3904 static tree
3905 cp_parser_class_or_namespace_name (cp_parser *parser,
3906 bool typename_keyword_p,
3907 bool template_keyword_p,
3908 bool check_dependency_p,
3909 bool type_p,
3910 bool is_declaration)
3911 {
3912 tree saved_scope;
3913 tree saved_qualifying_scope;
3914 tree saved_object_scope;
3915 tree scope;
3916 bool only_class_p;
3917
3918 /* Before we try to parse the class-name, we must save away the
3919 current PARSER->SCOPE since cp_parser_class_name will destroy
3920 it. */
3921 saved_scope = parser->scope;
3922 saved_qualifying_scope = parser->qualifying_scope;
3923 saved_object_scope = parser->object_scope;
3924 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3925 there is no need to look for a namespace-name. */
3926 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3927 if (!only_class_p)
3928 cp_parser_parse_tentatively (parser);
3929 scope = cp_parser_class_name (parser,
3930 typename_keyword_p,
3931 template_keyword_p,
3932 type_p ? class_type : none_type,
3933 check_dependency_p,
3934 /*class_head_p=*/false,
3935 is_declaration);
3936 /* If that didn't work, try for a namespace-name. */
3937 if (!only_class_p && !cp_parser_parse_definitely (parser))
3938 {
3939 /* Restore the saved scope. */
3940 parser->scope = saved_scope;
3941 parser->qualifying_scope = saved_qualifying_scope;
3942 parser->object_scope = saved_object_scope;
3943 /* If we are not looking at an identifier followed by the scope
3944 resolution operator, then this is not part of a
3945 nested-name-specifier. (Note that this function is only used
3946 to parse the components of a nested-name-specifier.) */
3947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3948 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3949 return error_mark_node;
3950 scope = cp_parser_namespace_name (parser);
3951 }
3952
3953 return scope;
3954 }
3955
3956 /* Parse a postfix-expression.
3957
3958 postfix-expression:
3959 primary-expression
3960 postfix-expression [ expression ]
3961 postfix-expression ( expression-list [opt] )
3962 simple-type-specifier ( expression-list [opt] )
3963 typename :: [opt] nested-name-specifier identifier
3964 ( expression-list [opt] )
3965 typename :: [opt] nested-name-specifier template [opt] template-id
3966 ( expression-list [opt] )
3967 postfix-expression . template [opt] id-expression
3968 postfix-expression -> template [opt] id-expression
3969 postfix-expression . pseudo-destructor-name
3970 postfix-expression -> pseudo-destructor-name
3971 postfix-expression ++
3972 postfix-expression --
3973 dynamic_cast < type-id > ( expression )
3974 static_cast < type-id > ( expression )
3975 reinterpret_cast < type-id > ( expression )
3976 const_cast < type-id > ( expression )
3977 typeid ( expression )
3978 typeid ( type-id )
3979
3980 GNU Extension:
3981
3982 postfix-expression:
3983 ( type-id ) { initializer-list , [opt] }
3984
3985 This extension is a GNU version of the C99 compound-literal
3986 construct. (The C99 grammar uses `type-name' instead of `type-id',
3987 but they are essentially the same concept.)
3988
3989 If ADDRESS_P is true, the postfix expression is the operand of the
3990 `&' operator. CAST_P is true if this expression is the target of a
3991 cast.
3992
3993 Returns a representation of the expression. */
3994
3995 static tree
3996 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3997 {
3998 cp_token *token;
3999 enum rid keyword;
4000 cp_id_kind idk = CP_ID_KIND_NONE;
4001 tree postfix_expression = NULL_TREE;
4002
4003 /* Peek at the next token. */
4004 token = cp_lexer_peek_token (parser->lexer);
4005 /* Some of the productions are determined by keywords. */
4006 keyword = token->keyword;
4007 switch (keyword)
4008 {
4009 case RID_DYNCAST:
4010 case RID_STATCAST:
4011 case RID_REINTCAST:
4012 case RID_CONSTCAST:
4013 {
4014 tree type;
4015 tree expression;
4016 const char *saved_message;
4017
4018 /* All of these can be handled in the same way from the point
4019 of view of parsing. Begin by consuming the token
4020 identifying the cast. */
4021 cp_lexer_consume_token (parser->lexer);
4022
4023 /* New types cannot be defined in the cast. */
4024 saved_message = parser->type_definition_forbidden_message;
4025 parser->type_definition_forbidden_message
4026 = "types may not be defined in casts";
4027
4028 /* Look for the opening `<'. */
4029 cp_parser_require (parser, CPP_LESS, "`<'");
4030 /* Parse the type to which we are casting. */
4031 type = cp_parser_type_id (parser);
4032 /* Look for the closing `>'. */
4033 cp_parser_require (parser, CPP_GREATER, "`>'");
4034 /* Restore the old message. */
4035 parser->type_definition_forbidden_message = saved_message;
4036
4037 /* And the expression which is being cast. */
4038 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4039 expression = cp_parser_expression (parser, /*cast_p=*/true);
4040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4041
4042 /* Only type conversions to integral or enumeration types
4043 can be used in constant-expressions. */
4044 if (!cast_valid_in_integral_constant_expression_p (type)
4045 && (cp_parser_non_integral_constant_expression
4046 (parser,
4047 "a cast to a type other than an integral or "
4048 "enumeration type")))
4049 return error_mark_node;
4050
4051 switch (keyword)
4052 {
4053 case RID_DYNCAST:
4054 postfix_expression
4055 = build_dynamic_cast (type, expression);
4056 break;
4057 case RID_STATCAST:
4058 postfix_expression
4059 = build_static_cast (type, expression);
4060 break;
4061 case RID_REINTCAST:
4062 postfix_expression
4063 = build_reinterpret_cast (type, expression);
4064 break;
4065 case RID_CONSTCAST:
4066 postfix_expression
4067 = build_const_cast (type, expression);
4068 break;
4069 default:
4070 gcc_unreachable ();
4071 }
4072 }
4073 break;
4074
4075 case RID_TYPEID:
4076 {
4077 tree type;
4078 const char *saved_message;
4079 bool saved_in_type_id_in_expr_p;
4080
4081 /* Consume the `typeid' token. */
4082 cp_lexer_consume_token (parser->lexer);
4083 /* Look for the `(' token. */
4084 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4085 /* Types cannot be defined in a `typeid' expression. */
4086 saved_message = parser->type_definition_forbidden_message;
4087 parser->type_definition_forbidden_message
4088 = "types may not be defined in a `typeid\' expression";
4089 /* We can't be sure yet whether we're looking at a type-id or an
4090 expression. */
4091 cp_parser_parse_tentatively (parser);
4092 /* Try a type-id first. */
4093 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4094 parser->in_type_id_in_expr_p = true;
4095 type = cp_parser_type_id (parser);
4096 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4097 /* Look for the `)' token. Otherwise, we can't be sure that
4098 we're not looking at an expression: consider `typeid (int
4099 (3))', for example. */
4100 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4101 /* If all went well, simply lookup the type-id. */
4102 if (cp_parser_parse_definitely (parser))
4103 postfix_expression = get_typeid (type);
4104 /* Otherwise, fall back to the expression variant. */
4105 else
4106 {
4107 tree expression;
4108
4109 /* Look for an expression. */
4110 expression = cp_parser_expression (parser, /*cast_p=*/false);
4111 /* Compute its typeid. */
4112 postfix_expression = build_typeid (expression);
4113 /* Look for the `)' token. */
4114 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4115 }
4116 /* Restore the saved message. */
4117 parser->type_definition_forbidden_message = saved_message;
4118 /* `typeid' may not appear in an integral constant expression. */
4119 if (cp_parser_non_integral_constant_expression(parser,
4120 "`typeid' operator"))
4121 return error_mark_node;
4122 }
4123 break;
4124
4125 case RID_TYPENAME:
4126 {
4127 tree type;
4128 /* The syntax permitted here is the same permitted for an
4129 elaborated-type-specifier. */
4130 type = cp_parser_elaborated_type_specifier (parser,
4131 /*is_friend=*/false,
4132 /*is_declaration=*/false);
4133 postfix_expression = cp_parser_functional_cast (parser, type);
4134 }
4135 break;
4136
4137 default:
4138 {
4139 tree type;
4140
4141 /* If the next thing is a simple-type-specifier, we may be
4142 looking at a functional cast. We could also be looking at
4143 an id-expression. So, we try the functional cast, and if
4144 that doesn't work we fall back to the primary-expression. */
4145 cp_parser_parse_tentatively (parser);
4146 /* Look for the simple-type-specifier. */
4147 type = cp_parser_simple_type_specifier (parser,
4148 /*decl_specs=*/NULL,
4149 CP_PARSER_FLAGS_NONE);
4150 /* Parse the cast itself. */
4151 if (!cp_parser_error_occurred (parser))
4152 postfix_expression
4153 = cp_parser_functional_cast (parser, type);
4154 /* If that worked, we're done. */
4155 if (cp_parser_parse_definitely (parser))
4156 break;
4157
4158 /* If the functional-cast didn't work out, try a
4159 compound-literal. */
4160 if (cp_parser_allow_gnu_extensions_p (parser)
4161 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4162 {
4163 VEC(constructor_elt,gc) *initializer_list = NULL;
4164 bool saved_in_type_id_in_expr_p;
4165
4166 cp_parser_parse_tentatively (parser);
4167 /* Consume the `('. */
4168 cp_lexer_consume_token (parser->lexer);
4169 /* Parse the type. */
4170 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4171 parser->in_type_id_in_expr_p = true;
4172 type = cp_parser_type_id (parser);
4173 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4174 /* Look for the `)'. */
4175 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4176 /* Look for the `{'. */
4177 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4178 /* If things aren't going well, there's no need to
4179 keep going. */
4180 if (!cp_parser_error_occurred (parser))
4181 {
4182 bool non_constant_p;
4183 /* Parse the initializer-list. */
4184 initializer_list
4185 = cp_parser_initializer_list (parser, &non_constant_p);
4186 /* Allow a trailing `,'. */
4187 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4188 cp_lexer_consume_token (parser->lexer);
4189 /* Look for the final `}'. */
4190 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4191 }
4192 /* If that worked, we're definitely looking at a
4193 compound-literal expression. */
4194 if (cp_parser_parse_definitely (parser))
4195 {
4196 /* Warn the user that a compound literal is not
4197 allowed in standard C++. */
4198 if (pedantic)
4199 pedwarn ("ISO C++ forbids compound-literals");
4200 /* Form the representation of the compound-literal. */
4201 postfix_expression
4202 = finish_compound_literal (type, initializer_list);
4203 break;
4204 }
4205 }
4206
4207 /* It must be a primary-expression. */
4208 postfix_expression
4209 = cp_parser_primary_expression (parser, address_p, cast_p,
4210 /*template_arg_p=*/false,
4211 &idk);
4212 }
4213 break;
4214 }
4215
4216 /* Keep looping until the postfix-expression is complete. */
4217 while (true)
4218 {
4219 if (idk == CP_ID_KIND_UNQUALIFIED
4220 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4221 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4222 /* It is not a Koenig lookup function call. */
4223 postfix_expression
4224 = unqualified_name_lookup_error (postfix_expression);
4225
4226 /* Peek at the next token. */
4227 token = cp_lexer_peek_token (parser->lexer);
4228
4229 switch (token->type)
4230 {
4231 case CPP_OPEN_SQUARE:
4232 postfix_expression
4233 = cp_parser_postfix_open_square_expression (parser,
4234 postfix_expression,
4235 false);
4236 idk = CP_ID_KIND_NONE;
4237 break;
4238
4239 case CPP_OPEN_PAREN:
4240 /* postfix-expression ( expression-list [opt] ) */
4241 {
4242 bool koenig_p;
4243 bool is_builtin_constant_p;
4244 bool saved_integral_constant_expression_p = false;
4245 bool saved_non_integral_constant_expression_p = false;
4246 tree args;
4247
4248 is_builtin_constant_p
4249 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4250 if (is_builtin_constant_p)
4251 {
4252 /* The whole point of __builtin_constant_p is to allow
4253 non-constant expressions to appear as arguments. */
4254 saved_integral_constant_expression_p
4255 = parser->integral_constant_expression_p;
4256 saved_non_integral_constant_expression_p
4257 = parser->non_integral_constant_expression_p;
4258 parser->integral_constant_expression_p = false;
4259 }
4260 args = (cp_parser_parenthesized_expression_list
4261 (parser, /*is_attribute_list=*/false,
4262 /*cast_p=*/false,
4263 /*non_constant_p=*/NULL));
4264 if (is_builtin_constant_p)
4265 {
4266 parser->integral_constant_expression_p
4267 = saved_integral_constant_expression_p;
4268 parser->non_integral_constant_expression_p
4269 = saved_non_integral_constant_expression_p;
4270 }
4271
4272 if (args == error_mark_node)
4273 {
4274 postfix_expression = error_mark_node;
4275 break;
4276 }
4277
4278 /* Function calls are not permitted in
4279 constant-expressions. */
4280 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4281 && cp_parser_non_integral_constant_expression (parser,
4282 "a function call"))
4283 {
4284 postfix_expression = error_mark_node;
4285 break;
4286 }
4287
4288 koenig_p = false;
4289 if (idk == CP_ID_KIND_UNQUALIFIED)
4290 {
4291 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4292 {
4293 if (args)
4294 {
4295 koenig_p = true;
4296 postfix_expression
4297 = perform_koenig_lookup (postfix_expression, args);
4298 }
4299 else
4300 postfix_expression
4301 = unqualified_fn_lookup_error (postfix_expression);
4302 }
4303 /* We do not perform argument-dependent lookup if
4304 normal lookup finds a non-function, in accordance
4305 with the expected resolution of DR 218. */
4306 else if (args && is_overloaded_fn (postfix_expression))
4307 {
4308 tree fn = get_first_fn (postfix_expression);
4309
4310 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4311 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4312
4313 /* Only do argument dependent lookup if regular
4314 lookup does not find a set of member functions.
4315 [basic.lookup.koenig]/2a */
4316 if (!DECL_FUNCTION_MEMBER_P (fn))
4317 {
4318 koenig_p = true;
4319 postfix_expression
4320 = perform_koenig_lookup (postfix_expression, args);
4321 }
4322 }
4323 }
4324
4325 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4326 {
4327 tree instance = TREE_OPERAND (postfix_expression, 0);
4328 tree fn = TREE_OPERAND (postfix_expression, 1);
4329
4330 if (processing_template_decl
4331 && (type_dependent_expression_p (instance)
4332 || (!BASELINK_P (fn)
4333 && TREE_CODE (fn) != FIELD_DECL)
4334 || type_dependent_expression_p (fn)
4335 || any_type_dependent_arguments_p (args)))
4336 {
4337 postfix_expression
4338 = build_min_nt (CALL_EXPR, postfix_expression,
4339 args, NULL_TREE);
4340 break;
4341 }
4342
4343 if (BASELINK_P (fn))
4344 postfix_expression
4345 = (build_new_method_call
4346 (instance, fn, args, NULL_TREE,
4347 (idk == CP_ID_KIND_QUALIFIED
4348 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4349 /*fn_p=*/NULL));
4350 else
4351 postfix_expression
4352 = finish_call_expr (postfix_expression, args,
4353 /*disallow_virtual=*/false,
4354 /*koenig_p=*/false);
4355 }
4356 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4357 || TREE_CODE (postfix_expression) == MEMBER_REF
4358 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4359 postfix_expression = (build_offset_ref_call_from_tree
4360 (postfix_expression, args));
4361 else if (idk == CP_ID_KIND_QUALIFIED)
4362 /* A call to a static class member, or a namespace-scope
4363 function. */
4364 postfix_expression
4365 = finish_call_expr (postfix_expression, args,
4366 /*disallow_virtual=*/true,
4367 koenig_p);
4368 else
4369 /* All other function calls. */
4370 postfix_expression
4371 = finish_call_expr (postfix_expression, args,
4372 /*disallow_virtual=*/false,
4373 koenig_p);
4374
4375 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4376 idk = CP_ID_KIND_NONE;
4377 }
4378 break;
4379
4380 case CPP_DOT:
4381 case CPP_DEREF:
4382 /* postfix-expression . template [opt] id-expression
4383 postfix-expression . pseudo-destructor-name
4384 postfix-expression -> template [opt] id-expression
4385 postfix-expression -> pseudo-destructor-name */
4386
4387 /* Consume the `.' or `->' operator. */
4388 cp_lexer_consume_token (parser->lexer);
4389
4390 postfix_expression
4391 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4392 postfix_expression,
4393 false, &idk);
4394 break;
4395
4396 case CPP_PLUS_PLUS:
4397 /* postfix-expression ++ */
4398 /* Consume the `++' token. */
4399 cp_lexer_consume_token (parser->lexer);
4400 /* Generate a representation for the complete expression. */
4401 postfix_expression
4402 = finish_increment_expr (postfix_expression,
4403 POSTINCREMENT_EXPR);
4404 /* Increments may not appear in constant-expressions. */
4405 if (cp_parser_non_integral_constant_expression (parser,
4406 "an increment"))
4407 postfix_expression = error_mark_node;
4408 idk = CP_ID_KIND_NONE;
4409 break;
4410
4411 case CPP_MINUS_MINUS:
4412 /* postfix-expression -- */
4413 /* Consume the `--' token. */
4414 cp_lexer_consume_token (parser->lexer);
4415 /* Generate a representation for the complete expression. */
4416 postfix_expression
4417 = finish_increment_expr (postfix_expression,
4418 POSTDECREMENT_EXPR);
4419 /* Decrements may not appear in constant-expressions. */
4420 if (cp_parser_non_integral_constant_expression (parser,
4421 "a decrement"))
4422 postfix_expression = error_mark_node;
4423 idk = CP_ID_KIND_NONE;
4424 break;
4425
4426 default:
4427 return postfix_expression;
4428 }
4429 }
4430
4431 /* We should never get here. */
4432 gcc_unreachable ();
4433 return error_mark_node;
4434 }
4435
4436 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4437 by cp_parser_builtin_offsetof. We're looking for
4438
4439 postfix-expression [ expression ]
4440
4441 FOR_OFFSETOF is set if we're being called in that context, which
4442 changes how we deal with integer constant expressions. */
4443
4444 static tree
4445 cp_parser_postfix_open_square_expression (cp_parser *parser,
4446 tree postfix_expression,
4447 bool for_offsetof)
4448 {
4449 tree index;
4450
4451 /* Consume the `[' token. */
4452 cp_lexer_consume_token (parser->lexer);
4453
4454 /* Parse the index expression. */
4455 /* ??? For offsetof, there is a question of what to allow here. If
4456 offsetof is not being used in an integral constant expression context,
4457 then we *could* get the right answer by computing the value at runtime.
4458 If we are in an integral constant expression context, then we might
4459 could accept any constant expression; hard to say without analysis.
4460 Rather than open the barn door too wide right away, allow only integer
4461 constant expressions here. */
4462 if (for_offsetof)
4463 index = cp_parser_constant_expression (parser, false, NULL);
4464 else
4465 index = cp_parser_expression (parser, /*cast_p=*/false);
4466
4467 /* Look for the closing `]'. */
4468 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4469
4470 /* Build the ARRAY_REF. */
4471 postfix_expression = grok_array_decl (postfix_expression, index);
4472
4473 /* When not doing offsetof, array references are not permitted in
4474 constant-expressions. */
4475 if (!for_offsetof
4476 && (cp_parser_non_integral_constant_expression
4477 (parser, "an array reference")))
4478 postfix_expression = error_mark_node;
4479
4480 return postfix_expression;
4481 }
4482
4483 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4484 by cp_parser_builtin_offsetof. We're looking for
4485
4486 postfix-expression . template [opt] id-expression
4487 postfix-expression . pseudo-destructor-name
4488 postfix-expression -> template [opt] id-expression
4489 postfix-expression -> pseudo-destructor-name
4490
4491 FOR_OFFSETOF is set if we're being called in that context. That sorta
4492 limits what of the above we'll actually accept, but nevermind.
4493 TOKEN_TYPE is the "." or "->" token, which will already have been
4494 removed from the stream. */
4495
4496 static tree
4497 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4498 enum cpp_ttype token_type,
4499 tree postfix_expression,
4500 bool for_offsetof, cp_id_kind *idk)
4501 {
4502 tree name;
4503 bool dependent_p;
4504 bool pseudo_destructor_p;
4505 tree scope = NULL_TREE;
4506
4507 /* If this is a `->' operator, dereference the pointer. */
4508 if (token_type == CPP_DEREF)
4509 postfix_expression = build_x_arrow (postfix_expression);
4510 /* Check to see whether or not the expression is type-dependent. */
4511 dependent_p = type_dependent_expression_p (postfix_expression);
4512 /* The identifier following the `->' or `.' is not qualified. */
4513 parser->scope = NULL_TREE;
4514 parser->qualifying_scope = NULL_TREE;
4515 parser->object_scope = NULL_TREE;
4516 *idk = CP_ID_KIND_NONE;
4517 /* Enter the scope corresponding to the type of the object
4518 given by the POSTFIX_EXPRESSION. */
4519 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4520 {
4521 scope = TREE_TYPE (postfix_expression);
4522 /* According to the standard, no expression should ever have
4523 reference type. Unfortunately, we do not currently match
4524 the standard in this respect in that our internal representation
4525 of an expression may have reference type even when the standard
4526 says it does not. Therefore, we have to manually obtain the
4527 underlying type here. */
4528 scope = non_reference (scope);
4529 /* The type of the POSTFIX_EXPRESSION must be complete. */
4530 if (scope == unknown_type_node)
4531 {
4532 error ("%qE does not have class type", postfix_expression);
4533 scope = NULL_TREE;
4534 }
4535 else
4536 scope = complete_type_or_else (scope, NULL_TREE);
4537 /* Let the name lookup machinery know that we are processing a
4538 class member access expression. */
4539 parser->context->object_type = scope;
4540 /* If something went wrong, we want to be able to discern that case,
4541 as opposed to the case where there was no SCOPE due to the type
4542 of expression being dependent. */
4543 if (!scope)
4544 scope = error_mark_node;
4545 /* If the SCOPE was erroneous, make the various semantic analysis
4546 functions exit quickly -- and without issuing additional error
4547 messages. */
4548 if (scope == error_mark_node)
4549 postfix_expression = error_mark_node;
4550 }
4551
4552 /* Assume this expression is not a pseudo-destructor access. */
4553 pseudo_destructor_p = false;
4554
4555 /* If the SCOPE is a scalar type, then, if this is a valid program,
4556 we must be looking at a pseudo-destructor-name. */
4557 if (scope && SCALAR_TYPE_P (scope))
4558 {
4559 tree s;
4560 tree type;
4561
4562 cp_parser_parse_tentatively (parser);
4563 /* Parse the pseudo-destructor-name. */
4564 s = NULL_TREE;
4565 cp_parser_pseudo_destructor_name (parser, &s, &type);
4566 if (cp_parser_parse_definitely (parser))
4567 {
4568 pseudo_destructor_p = true;
4569 postfix_expression
4570 = finish_pseudo_destructor_expr (postfix_expression,
4571 s, TREE_TYPE (type));
4572 }
4573 }
4574
4575 if (!pseudo_destructor_p)
4576 {
4577 /* If the SCOPE is not a scalar type, we are looking at an
4578 ordinary class member access expression, rather than a
4579 pseudo-destructor-name. */
4580 bool template_p;
4581 /* Parse the id-expression. */
4582 name = (cp_parser_id_expression
4583 (parser,
4584 cp_parser_optional_template_keyword (parser),
4585 /*check_dependency_p=*/true,
4586 &template_p,
4587 /*declarator_p=*/false,
4588 /*optional_p=*/false));
4589 /* In general, build a SCOPE_REF if the member name is qualified.
4590 However, if the name was not dependent and has already been
4591 resolved; there is no need to build the SCOPE_REF. For example;
4592
4593 struct X { void f(); };
4594 template <typename T> void f(T* t) { t->X::f(); }
4595
4596 Even though "t" is dependent, "X::f" is not and has been resolved
4597 to a BASELINK; there is no need to include scope information. */
4598
4599 /* But we do need to remember that there was an explicit scope for
4600 virtual function calls. */
4601 if (parser->scope)
4602 *idk = CP_ID_KIND_QUALIFIED;
4603
4604 /* If the name is a template-id that names a type, we will get a
4605 TYPE_DECL here. That is invalid code. */
4606 if (TREE_CODE (name) == TYPE_DECL)
4607 {
4608 error ("invalid use of %qD", name);
4609 postfix_expression = error_mark_node;
4610 }
4611 else
4612 {
4613 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4614 {
4615 name = build_qualified_name (/*type=*/NULL_TREE,
4616 parser->scope,
4617 name,
4618 template_p);
4619 parser->scope = NULL_TREE;
4620 parser->qualifying_scope = NULL_TREE;
4621 parser->object_scope = NULL_TREE;
4622 }
4623 if (scope && name && BASELINK_P (name))
4624 adjust_result_of_qualified_name_lookup
4625 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4626 postfix_expression
4627 = finish_class_member_access_expr (postfix_expression, name,
4628 template_p);
4629 }
4630 }
4631
4632 /* We no longer need to look up names in the scope of the object on
4633 the left-hand side of the `.' or `->' operator. */
4634 parser->context->object_type = NULL_TREE;
4635
4636 /* Outside of offsetof, these operators may not appear in
4637 constant-expressions. */
4638 if (!for_offsetof
4639 && (cp_parser_non_integral_constant_expression
4640 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4641 postfix_expression = error_mark_node;
4642
4643 return postfix_expression;
4644 }
4645
4646 /* Parse a parenthesized expression-list.
4647
4648 expression-list:
4649 assignment-expression
4650 expression-list, assignment-expression
4651
4652 attribute-list:
4653 expression-list
4654 identifier
4655 identifier, expression-list
4656
4657 CAST_P is true if this expression is the target of a cast.
4658
4659 Returns a TREE_LIST. The TREE_VALUE of each node is a
4660 representation of an assignment-expression. Note that a TREE_LIST
4661 is returned even if there is only a single expression in the list.
4662 error_mark_node is returned if the ( and or ) are
4663 missing. NULL_TREE is returned on no expressions. The parentheses
4664 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4665 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4666 indicates whether or not all of the expressions in the list were
4667 constant. */
4668
4669 static tree
4670 cp_parser_parenthesized_expression_list (cp_parser* parser,
4671 bool is_attribute_list,
4672 bool cast_p,
4673 bool *non_constant_p)
4674 {
4675 tree expression_list = NULL_TREE;
4676 bool fold_expr_p = is_attribute_list;
4677 tree identifier = NULL_TREE;
4678
4679 /* Assume all the expressions will be constant. */
4680 if (non_constant_p)
4681 *non_constant_p = false;
4682
4683 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4684 return error_mark_node;
4685
4686 /* Consume expressions until there are no more. */
4687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4688 while (true)
4689 {
4690 tree expr;
4691
4692 /* At the beginning of attribute lists, check to see if the
4693 next token is an identifier. */
4694 if (is_attribute_list
4695 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4696 {
4697 cp_token *token;
4698
4699 /* Consume the identifier. */
4700 token = cp_lexer_consume_token (parser->lexer);
4701 /* Save the identifier. */
4702 identifier = token->value;
4703 }
4704 else
4705 {
4706 /* Parse the next assignment-expression. */
4707 if (non_constant_p)
4708 {
4709 bool expr_non_constant_p;
4710 expr = (cp_parser_constant_expression
4711 (parser, /*allow_non_constant_p=*/true,
4712 &expr_non_constant_p));
4713 if (expr_non_constant_p)
4714 *non_constant_p = true;
4715 }
4716 else
4717 expr = cp_parser_assignment_expression (parser, cast_p);
4718
4719 if (fold_expr_p)
4720 expr = fold_non_dependent_expr (expr);
4721
4722 /* Add it to the list. We add error_mark_node
4723 expressions to the list, so that we can still tell if
4724 the correct form for a parenthesized expression-list
4725 is found. That gives better errors. */
4726 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4727
4728 if (expr == error_mark_node)
4729 goto skip_comma;
4730 }
4731
4732 /* After the first item, attribute lists look the same as
4733 expression lists. */
4734 is_attribute_list = false;
4735
4736 get_comma:;
4737 /* If the next token isn't a `,', then we are done. */
4738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4739 break;
4740
4741 /* Otherwise, consume the `,' and keep going. */
4742 cp_lexer_consume_token (parser->lexer);
4743 }
4744
4745 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4746 {
4747 int ending;
4748
4749 skip_comma:;
4750 /* We try and resync to an unnested comma, as that will give the
4751 user better diagnostics. */
4752 ending = cp_parser_skip_to_closing_parenthesis (parser,
4753 /*recovering=*/true,
4754 /*or_comma=*/true,
4755 /*consume_paren=*/true);
4756 if (ending < 0)
4757 goto get_comma;
4758 if (!ending)
4759 return error_mark_node;
4760 }
4761
4762 /* We built up the list in reverse order so we must reverse it now. */
4763 expression_list = nreverse (expression_list);
4764 if (identifier)
4765 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4766
4767 return expression_list;
4768 }
4769
4770 /* Parse a pseudo-destructor-name.
4771
4772 pseudo-destructor-name:
4773 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4774 :: [opt] nested-name-specifier template template-id :: ~ type-name
4775 :: [opt] nested-name-specifier [opt] ~ type-name
4776
4777 If either of the first two productions is used, sets *SCOPE to the
4778 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4779 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4780 or ERROR_MARK_NODE if the parse fails. */
4781
4782 static void
4783 cp_parser_pseudo_destructor_name (cp_parser* parser,
4784 tree* scope,
4785 tree* type)
4786 {
4787 bool nested_name_specifier_p;
4788
4789 /* Assume that things will not work out. */
4790 *type = error_mark_node;
4791
4792 /* Look for the optional `::' operator. */
4793 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4794 /* Look for the optional nested-name-specifier. */
4795 nested_name_specifier_p
4796 = (cp_parser_nested_name_specifier_opt (parser,
4797 /*typename_keyword_p=*/false,
4798 /*check_dependency_p=*/true,
4799 /*type_p=*/false,
4800 /*is_declaration=*/true)
4801 != NULL_TREE);
4802 /* Now, if we saw a nested-name-specifier, we might be doing the
4803 second production. */
4804 if (nested_name_specifier_p
4805 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4806 {
4807 /* Consume the `template' keyword. */
4808 cp_lexer_consume_token (parser->lexer);
4809 /* Parse the template-id. */
4810 cp_parser_template_id (parser,
4811 /*template_keyword_p=*/true,
4812 /*check_dependency_p=*/false,
4813 /*is_declaration=*/true);
4814 /* Look for the `::' token. */
4815 cp_parser_require (parser, CPP_SCOPE, "`::'");
4816 }
4817 /* If the next token is not a `~', then there might be some
4818 additional qualification. */
4819 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4820 {
4821 /* Look for the type-name. */
4822 *scope = TREE_TYPE (cp_parser_type_name (parser));
4823
4824 if (*scope == error_mark_node)
4825 return;
4826
4827 /* If we don't have ::~, then something has gone wrong. Since
4828 the only caller of this function is looking for something
4829 after `.' or `->' after a scalar type, most likely the
4830 program is trying to get a member of a non-aggregate
4831 type. */
4832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4833 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4834 {
4835 cp_parser_error (parser, "request for member of non-aggregate type");
4836 return;
4837 }
4838
4839 /* Look for the `::' token. */
4840 cp_parser_require (parser, CPP_SCOPE, "`::'");
4841 }
4842 else
4843 *scope = NULL_TREE;
4844
4845 /* Look for the `~'. */
4846 cp_parser_require (parser, CPP_COMPL, "`~'");
4847 /* Look for the type-name again. We are not responsible for
4848 checking that it matches the first type-name. */
4849 *type = cp_parser_type_name (parser);
4850 }
4851
4852 /* Parse a unary-expression.
4853
4854 unary-expression:
4855 postfix-expression
4856 ++ cast-expression
4857 -- cast-expression
4858 unary-operator cast-expression
4859 sizeof unary-expression
4860 sizeof ( type-id )
4861 new-expression
4862 delete-expression
4863
4864 GNU Extensions:
4865
4866 unary-expression:
4867 __extension__ cast-expression
4868 __alignof__ unary-expression
4869 __alignof__ ( type-id )
4870 __real__ cast-expression
4871 __imag__ cast-expression
4872 && identifier
4873
4874 ADDRESS_P is true iff the unary-expression is appearing as the
4875 operand of the `&' operator. CAST_P is true if this expression is
4876 the target of a cast.
4877
4878 Returns a representation of the expression. */
4879
4880 static tree
4881 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4882 {
4883 cp_token *token;
4884 enum tree_code unary_operator;
4885
4886 /* Peek at the next token. */
4887 token = cp_lexer_peek_token (parser->lexer);
4888 /* Some keywords give away the kind of expression. */
4889 if (token->type == CPP_KEYWORD)
4890 {
4891 enum rid keyword = token->keyword;
4892
4893 switch (keyword)
4894 {
4895 case RID_ALIGNOF:
4896 case RID_SIZEOF:
4897 {
4898 tree operand;
4899 enum tree_code op;
4900
4901 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4902 /* Consume the token. */
4903 cp_lexer_consume_token (parser->lexer);
4904 /* Parse the operand. */
4905 operand = cp_parser_sizeof_operand (parser, keyword);
4906
4907 if (TYPE_P (operand))
4908 return cxx_sizeof_or_alignof_type (operand, op, true);
4909 else
4910 return cxx_sizeof_or_alignof_expr (operand, op);
4911 }
4912
4913 case RID_NEW:
4914 return cp_parser_new_expression (parser);
4915
4916 case RID_DELETE:
4917 return cp_parser_delete_expression (parser);
4918
4919 case RID_EXTENSION:
4920 {
4921 /* The saved value of the PEDANTIC flag. */
4922 int saved_pedantic;
4923 tree expr;
4924
4925 /* Save away the PEDANTIC flag. */
4926 cp_parser_extension_opt (parser, &saved_pedantic);
4927 /* Parse the cast-expression. */
4928 expr = cp_parser_simple_cast_expression (parser);
4929 /* Restore the PEDANTIC flag. */
4930 pedantic = saved_pedantic;
4931
4932 return expr;
4933 }
4934
4935 case RID_REALPART:
4936 case RID_IMAGPART:
4937 {
4938 tree expression;
4939
4940 /* Consume the `__real__' or `__imag__' token. */
4941 cp_lexer_consume_token (parser->lexer);
4942 /* Parse the cast-expression. */
4943 expression = cp_parser_simple_cast_expression (parser);
4944 /* Create the complete representation. */
4945 return build_x_unary_op ((keyword == RID_REALPART
4946 ? REALPART_EXPR : IMAGPART_EXPR),
4947 expression);
4948 }
4949 break;
4950
4951 default:
4952 break;
4953 }
4954 }
4955
4956 /* Look for the `:: new' and `:: delete', which also signal the
4957 beginning of a new-expression, or delete-expression,
4958 respectively. If the next token is `::', then it might be one of
4959 these. */
4960 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4961 {
4962 enum rid keyword;
4963
4964 /* See if the token after the `::' is one of the keywords in
4965 which we're interested. */
4966 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4967 /* If it's `new', we have a new-expression. */
4968 if (keyword == RID_NEW)
4969 return cp_parser_new_expression (parser);
4970 /* Similarly, for `delete'. */
4971 else if (keyword == RID_DELETE)
4972 return cp_parser_delete_expression (parser);
4973 }
4974
4975 /* Look for a unary operator. */
4976 unary_operator = cp_parser_unary_operator (token);
4977 /* The `++' and `--' operators can be handled similarly, even though
4978 they are not technically unary-operators in the grammar. */
4979 if (unary_operator == ERROR_MARK)
4980 {
4981 if (token->type == CPP_PLUS_PLUS)
4982 unary_operator = PREINCREMENT_EXPR;
4983 else if (token->type == CPP_MINUS_MINUS)
4984 unary_operator = PREDECREMENT_EXPR;
4985 /* Handle the GNU address-of-label extension. */
4986 else if (cp_parser_allow_gnu_extensions_p (parser)
4987 && token->type == CPP_AND_AND)
4988 {
4989 tree identifier;
4990
4991 /* Consume the '&&' token. */
4992 cp_lexer_consume_token (parser->lexer);
4993 /* Look for the identifier. */
4994 identifier = cp_parser_identifier (parser);
4995 /* Create an expression representing the address. */
4996 return finish_label_address_expr (identifier);
4997 }
4998 }
4999 if (unary_operator != ERROR_MARK)
5000 {
5001 tree cast_expression;
5002 tree expression = error_mark_node;
5003 const char *non_constant_p = NULL;
5004
5005 /* Consume the operator token. */
5006 token = cp_lexer_consume_token (parser->lexer);
5007 /* Parse the cast-expression. */
5008 cast_expression
5009 = cp_parser_cast_expression (parser,
5010 unary_operator == ADDR_EXPR,
5011 /*cast_p=*/false);
5012 /* Now, build an appropriate representation. */
5013 switch (unary_operator)
5014 {
5015 case INDIRECT_REF:
5016 non_constant_p = "`*'";
5017 expression = build_x_indirect_ref (cast_expression, "unary *");
5018 break;
5019
5020 case ADDR_EXPR:
5021 non_constant_p = "`&'";
5022 /* Fall through. */
5023 case BIT_NOT_EXPR:
5024 expression = build_x_unary_op (unary_operator, cast_expression);
5025 break;
5026
5027 case PREINCREMENT_EXPR:
5028 case PREDECREMENT_EXPR:
5029 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5030 ? "`++'" : "`--'");
5031 /* Fall through. */
5032 case UNARY_PLUS_EXPR:
5033 case NEGATE_EXPR:
5034 case TRUTH_NOT_EXPR:
5035 expression = finish_unary_op_expr (unary_operator, cast_expression);
5036 break;
5037
5038 default:
5039 gcc_unreachable ();
5040 }
5041
5042 if (non_constant_p
5043 && cp_parser_non_integral_constant_expression (parser,
5044 non_constant_p))
5045 expression = error_mark_node;
5046
5047 return expression;
5048 }
5049
5050 return cp_parser_postfix_expression (parser, address_p, cast_p);
5051 }
5052
5053 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5054 unary-operator, the corresponding tree code is returned. */
5055
5056 static enum tree_code
5057 cp_parser_unary_operator (cp_token* token)
5058 {
5059 switch (token->type)
5060 {
5061 case CPP_MULT:
5062 return INDIRECT_REF;
5063
5064 case CPP_AND:
5065 return ADDR_EXPR;
5066
5067 case CPP_PLUS:
5068 return UNARY_PLUS_EXPR;
5069
5070 case CPP_MINUS:
5071 return NEGATE_EXPR;
5072
5073 case CPP_NOT:
5074 return TRUTH_NOT_EXPR;
5075
5076 case CPP_COMPL:
5077 return BIT_NOT_EXPR;
5078
5079 default:
5080 return ERROR_MARK;
5081 }
5082 }
5083
5084 /* Parse a new-expression.
5085
5086 new-expression:
5087 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5088 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5089
5090 Returns a representation of the expression. */
5091
5092 static tree
5093 cp_parser_new_expression (cp_parser* parser)
5094 {
5095 bool global_scope_p;
5096 tree placement;
5097 tree type;
5098 tree initializer;
5099 tree nelts;
5100
5101 /* Look for the optional `::' operator. */
5102 global_scope_p
5103 = (cp_parser_global_scope_opt (parser,
5104 /*current_scope_valid_p=*/false)
5105 != NULL_TREE);
5106 /* Look for the `new' operator. */
5107 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5108 /* There's no easy way to tell a new-placement from the
5109 `( type-id )' construct. */
5110 cp_parser_parse_tentatively (parser);
5111 /* Look for a new-placement. */
5112 placement = cp_parser_new_placement (parser);
5113 /* If that didn't work out, there's no new-placement. */
5114 if (!cp_parser_parse_definitely (parser))
5115 placement = NULL_TREE;
5116
5117 /* If the next token is a `(', then we have a parenthesized
5118 type-id. */
5119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5120 {
5121 /* Consume the `('. */
5122 cp_lexer_consume_token (parser->lexer);
5123 /* Parse the type-id. */
5124 type = cp_parser_type_id (parser);
5125 /* Look for the closing `)'. */
5126 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5127 /* There should not be a direct-new-declarator in this production,
5128 but GCC used to allowed this, so we check and emit a sensible error
5129 message for this case. */
5130 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5131 {
5132 error ("array bound forbidden after parenthesized type-id");
5133 inform ("try removing the parentheses around the type-id");
5134 cp_parser_direct_new_declarator (parser);
5135 }
5136 nelts = NULL_TREE;
5137 }
5138 /* Otherwise, there must be a new-type-id. */
5139 else
5140 type = cp_parser_new_type_id (parser, &nelts);
5141
5142 /* If the next token is a `(', then we have a new-initializer. */
5143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5144 initializer = cp_parser_new_initializer (parser);
5145 else
5146 initializer = NULL_TREE;
5147
5148 /* A new-expression may not appear in an integral constant
5149 expression. */
5150 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5151 return error_mark_node;
5152
5153 /* Create a representation of the new-expression. */
5154 return build_new (placement, type, nelts, initializer, global_scope_p);
5155 }
5156
5157 /* Parse a new-placement.
5158
5159 new-placement:
5160 ( expression-list )
5161
5162 Returns the same representation as for an expression-list. */
5163
5164 static tree
5165 cp_parser_new_placement (cp_parser* parser)
5166 {
5167 tree expression_list;
5168
5169 /* Parse the expression-list. */
5170 expression_list = (cp_parser_parenthesized_expression_list
5171 (parser, false, /*cast_p=*/false,
5172 /*non_constant_p=*/NULL));
5173
5174 return expression_list;
5175 }
5176
5177 /* Parse a new-type-id.
5178
5179 new-type-id:
5180 type-specifier-seq new-declarator [opt]
5181
5182 Returns the TYPE allocated. If the new-type-id indicates an array
5183 type, *NELTS is set to the number of elements in the last array
5184 bound; the TYPE will not include the last array bound. */
5185
5186 static tree
5187 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5188 {
5189 cp_decl_specifier_seq type_specifier_seq;
5190 cp_declarator *new_declarator;
5191 cp_declarator *declarator;
5192 cp_declarator *outer_declarator;
5193 const char *saved_message;
5194 tree type;
5195
5196 /* The type-specifier sequence must not contain type definitions.
5197 (It cannot contain declarations of new types either, but if they
5198 are not definitions we will catch that because they are not
5199 complete.) */
5200 saved_message = parser->type_definition_forbidden_message;
5201 parser->type_definition_forbidden_message
5202 = "types may not be defined in a new-type-id";
5203 /* Parse the type-specifier-seq. */
5204 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5205 &type_specifier_seq);
5206 /* Restore the old message. */
5207 parser->type_definition_forbidden_message = saved_message;
5208 /* Parse the new-declarator. */
5209 new_declarator = cp_parser_new_declarator_opt (parser);
5210
5211 /* Determine the number of elements in the last array dimension, if
5212 any. */
5213 *nelts = NULL_TREE;
5214 /* Skip down to the last array dimension. */
5215 declarator = new_declarator;
5216 outer_declarator = NULL;
5217 while (declarator && (declarator->kind == cdk_pointer
5218 || declarator->kind == cdk_ptrmem))
5219 {
5220 outer_declarator = declarator;
5221 declarator = declarator->declarator;
5222 }
5223 while (declarator
5224 && declarator->kind == cdk_array
5225 && declarator->declarator
5226 && declarator->declarator->kind == cdk_array)
5227 {
5228 outer_declarator = declarator;
5229 declarator = declarator->declarator;
5230 }
5231
5232 if (declarator && declarator->kind == cdk_array)
5233 {
5234 *nelts = declarator->u.array.bounds;
5235 if (*nelts == error_mark_node)
5236 *nelts = integer_one_node;
5237
5238 if (outer_declarator)
5239 outer_declarator->declarator = declarator->declarator;
5240 else
5241 new_declarator = NULL;
5242 }
5243
5244 type = groktypename (&type_specifier_seq, new_declarator);
5245 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5246 {
5247 *nelts = array_type_nelts_top (type);
5248 type = TREE_TYPE (type);
5249 }
5250 return type;
5251 }
5252
5253 /* Parse an (optional) new-declarator.
5254
5255 new-declarator:
5256 ptr-operator new-declarator [opt]
5257 direct-new-declarator
5258
5259 Returns the declarator. */
5260
5261 static cp_declarator *
5262 cp_parser_new_declarator_opt (cp_parser* parser)
5263 {
5264 enum tree_code code;
5265 tree type;
5266 cp_cv_quals cv_quals;
5267
5268 /* We don't know if there's a ptr-operator next, or not. */
5269 cp_parser_parse_tentatively (parser);
5270 /* Look for a ptr-operator. */
5271 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5272 /* If that worked, look for more new-declarators. */
5273 if (cp_parser_parse_definitely (parser))
5274 {
5275 cp_declarator *declarator;
5276
5277 /* Parse another optional declarator. */
5278 declarator = cp_parser_new_declarator_opt (parser);
5279
5280 /* Create the representation of the declarator. */
5281 if (type)
5282 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5283 else if (code == INDIRECT_REF)
5284 declarator = make_pointer_declarator (cv_quals, declarator);
5285 else
5286 declarator = make_reference_declarator (cv_quals, declarator);
5287
5288 return declarator;
5289 }
5290
5291 /* If the next token is a `[', there is a direct-new-declarator. */
5292 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5293 return cp_parser_direct_new_declarator (parser);
5294
5295 return NULL;
5296 }
5297
5298 /* Parse a direct-new-declarator.
5299
5300 direct-new-declarator:
5301 [ expression ]
5302 direct-new-declarator [constant-expression]
5303
5304 */
5305
5306 static cp_declarator *
5307 cp_parser_direct_new_declarator (cp_parser* parser)
5308 {
5309 cp_declarator *declarator = NULL;
5310
5311 while (true)
5312 {
5313 tree expression;
5314
5315 /* Look for the opening `['. */
5316 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5317 /* The first expression is not required to be constant. */
5318 if (!declarator)
5319 {
5320 expression = cp_parser_expression (parser, /*cast_p=*/false);
5321 /* The standard requires that the expression have integral
5322 type. DR 74 adds enumeration types. We believe that the
5323 real intent is that these expressions be handled like the
5324 expression in a `switch' condition, which also allows
5325 classes with a single conversion to integral or
5326 enumeration type. */
5327 if (!processing_template_decl)
5328 {
5329 expression
5330 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5331 expression,
5332 /*complain=*/true);
5333 if (!expression)
5334 {
5335 error ("expression in new-declarator must have integral "
5336 "or enumeration type");
5337 expression = error_mark_node;
5338 }
5339 }
5340 }
5341 /* But all the other expressions must be. */
5342 else
5343 expression
5344 = cp_parser_constant_expression (parser,
5345 /*allow_non_constant=*/false,
5346 NULL);
5347 /* Look for the closing `]'. */
5348 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5349
5350 /* Add this bound to the declarator. */
5351 declarator = make_array_declarator (declarator, expression);
5352
5353 /* If the next token is not a `[', then there are no more
5354 bounds. */
5355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5356 break;
5357 }
5358
5359 return declarator;
5360 }
5361
5362 /* Parse a new-initializer.
5363
5364 new-initializer:
5365 ( expression-list [opt] )
5366
5367 Returns a representation of the expression-list. If there is no
5368 expression-list, VOID_ZERO_NODE is returned. */
5369
5370 static tree
5371 cp_parser_new_initializer (cp_parser* parser)
5372 {
5373 tree expression_list;
5374
5375 expression_list = (cp_parser_parenthesized_expression_list
5376 (parser, false, /*cast_p=*/false,
5377 /*non_constant_p=*/NULL));
5378 if (!expression_list)
5379 expression_list = void_zero_node;
5380
5381 return expression_list;
5382 }
5383
5384 /* Parse a delete-expression.
5385
5386 delete-expression:
5387 :: [opt] delete cast-expression
5388 :: [opt] delete [ ] cast-expression
5389
5390 Returns a representation of the expression. */
5391
5392 static tree
5393 cp_parser_delete_expression (cp_parser* parser)
5394 {
5395 bool global_scope_p;
5396 bool array_p;
5397 tree expression;
5398
5399 /* Look for the optional `::' operator. */
5400 global_scope_p
5401 = (cp_parser_global_scope_opt (parser,
5402 /*current_scope_valid_p=*/false)
5403 != NULL_TREE);
5404 /* Look for the `delete' keyword. */
5405 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5406 /* See if the array syntax is in use. */
5407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5408 {
5409 /* Consume the `[' token. */
5410 cp_lexer_consume_token (parser->lexer);
5411 /* Look for the `]' token. */
5412 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5413 /* Remember that this is the `[]' construct. */
5414 array_p = true;
5415 }
5416 else
5417 array_p = false;
5418
5419 /* Parse the cast-expression. */
5420 expression = cp_parser_simple_cast_expression (parser);
5421
5422 /* A delete-expression may not appear in an integral constant
5423 expression. */
5424 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5425 return error_mark_node;
5426
5427 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5428 }
5429
5430 /* Parse a cast-expression.
5431
5432 cast-expression:
5433 unary-expression
5434 ( type-id ) cast-expression
5435
5436 ADDRESS_P is true iff the unary-expression is appearing as the
5437 operand of the `&' operator. CAST_P is true if this expression is
5438 the target of a cast.
5439
5440 Returns a representation of the expression. */
5441
5442 static tree
5443 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5444 {
5445 /* If it's a `(', then we might be looking at a cast. */
5446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5447 {
5448 tree type = NULL_TREE;
5449 tree expr = NULL_TREE;
5450 bool compound_literal_p;
5451 const char *saved_message;
5452
5453 /* There's no way to know yet whether or not this is a cast.
5454 For example, `(int (3))' is a unary-expression, while `(int)
5455 3' is a cast. So, we resort to parsing tentatively. */
5456 cp_parser_parse_tentatively (parser);
5457 /* Types may not be defined in a cast. */
5458 saved_message = parser->type_definition_forbidden_message;
5459 parser->type_definition_forbidden_message
5460 = "types may not be defined in casts";
5461 /* Consume the `('. */
5462 cp_lexer_consume_token (parser->lexer);
5463 /* A very tricky bit is that `(struct S) { 3 }' is a
5464 compound-literal (which we permit in C++ as an extension).
5465 But, that construct is not a cast-expression -- it is a
5466 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5467 is legal; if the compound-literal were a cast-expression,
5468 you'd need an extra set of parentheses.) But, if we parse
5469 the type-id, and it happens to be a class-specifier, then we
5470 will commit to the parse at that point, because we cannot
5471 undo the action that is done when creating a new class. So,
5472 then we cannot back up and do a postfix-expression.
5473
5474 Therefore, we scan ahead to the closing `)', and check to see
5475 if the token after the `)' is a `{'. If so, we are not
5476 looking at a cast-expression.
5477
5478 Save tokens so that we can put them back. */
5479 cp_lexer_save_tokens (parser->lexer);
5480 /* Skip tokens until the next token is a closing parenthesis.
5481 If we find the closing `)', and the next token is a `{', then
5482 we are looking at a compound-literal. */
5483 compound_literal_p
5484 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5485 /*consume_paren=*/true)
5486 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5487 /* Roll back the tokens we skipped. */
5488 cp_lexer_rollback_tokens (parser->lexer);
5489 /* If we were looking at a compound-literal, simulate an error
5490 so that the call to cp_parser_parse_definitely below will
5491 fail. */
5492 if (compound_literal_p)
5493 cp_parser_simulate_error (parser);
5494 else
5495 {
5496 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5497 parser->in_type_id_in_expr_p = true;
5498 /* Look for the type-id. */
5499 type = cp_parser_type_id (parser);
5500 /* Look for the closing `)'. */
5501 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5502 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5503 }
5504
5505 /* Restore the saved message. */
5506 parser->type_definition_forbidden_message = saved_message;
5507
5508 /* If ok so far, parse the dependent expression. We cannot be
5509 sure it is a cast. Consider `(T ())'. It is a parenthesized
5510 ctor of T, but looks like a cast to function returning T
5511 without a dependent expression. */
5512 if (!cp_parser_error_occurred (parser))
5513 expr = cp_parser_cast_expression (parser,
5514 /*address_p=*/false,
5515 /*cast_p=*/true);
5516
5517 if (cp_parser_parse_definitely (parser))
5518 {
5519 /* Warn about old-style casts, if so requested. */
5520 if (warn_old_style_cast
5521 && !in_system_header
5522 && !VOID_TYPE_P (type)
5523 && current_lang_name != lang_name_c)
5524 warning (OPT_Wold_style_cast, "use of old-style cast");
5525
5526 /* Only type conversions to integral or enumeration types
5527 can be used in constant-expressions. */
5528 if (!cast_valid_in_integral_constant_expression_p (type)
5529 && (cp_parser_non_integral_constant_expression
5530 (parser,
5531 "a cast to a type other than an integral or "
5532 "enumeration type")))
5533 return error_mark_node;
5534
5535 /* Perform the cast. */
5536 expr = build_c_cast (type, expr);
5537 return expr;
5538 }
5539 }
5540
5541 /* If we get here, then it's not a cast, so it must be a
5542 unary-expression. */
5543 return cp_parser_unary_expression (parser, address_p, cast_p);
5544 }
5545
5546 /* Parse a binary expression of the general form:
5547
5548 pm-expression:
5549 cast-expression
5550 pm-expression .* cast-expression
5551 pm-expression ->* cast-expression
5552
5553 multiplicative-expression:
5554 pm-expression
5555 multiplicative-expression * pm-expression
5556 multiplicative-expression / pm-expression
5557 multiplicative-expression % pm-expression
5558
5559 additive-expression:
5560 multiplicative-expression
5561 additive-expression + multiplicative-expression
5562 additive-expression - multiplicative-expression
5563
5564 shift-expression:
5565 additive-expression
5566 shift-expression << additive-expression
5567 shift-expression >> additive-expression
5568
5569 relational-expression:
5570 shift-expression
5571 relational-expression < shift-expression
5572 relational-expression > shift-expression
5573 relational-expression <= shift-expression
5574 relational-expression >= shift-expression
5575
5576 GNU Extension:
5577
5578 relational-expression:
5579 relational-expression <? shift-expression
5580 relational-expression >? shift-expression
5581
5582 equality-expression:
5583 relational-expression
5584 equality-expression == relational-expression
5585 equality-expression != relational-expression
5586
5587 and-expression:
5588 equality-expression
5589 and-expression & equality-expression
5590
5591 exclusive-or-expression:
5592 and-expression
5593 exclusive-or-expression ^ and-expression
5594
5595 inclusive-or-expression:
5596 exclusive-or-expression
5597 inclusive-or-expression | exclusive-or-expression
5598
5599 logical-and-expression:
5600 inclusive-or-expression
5601 logical-and-expression && inclusive-or-expression
5602
5603 logical-or-expression:
5604 logical-and-expression
5605 logical-or-expression || logical-and-expression
5606
5607 All these are implemented with a single function like:
5608
5609 binary-expression:
5610 simple-cast-expression
5611 binary-expression <token> binary-expression
5612
5613 CAST_P is true if this expression is the target of a cast.
5614
5615 The binops_by_token map is used to get the tree codes for each <token> type.
5616 binary-expressions are associated according to a precedence table. */
5617
5618 #define TOKEN_PRECEDENCE(token) \
5619 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5620 ? PREC_NOT_OPERATOR \
5621 : binops_by_token[token->type].prec)
5622
5623 static tree
5624 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5625 {
5626 cp_parser_expression_stack stack;
5627 cp_parser_expression_stack_entry *sp = &stack[0];
5628 tree lhs, rhs;
5629 cp_token *token;
5630 enum tree_code tree_type;
5631 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5632 bool overloaded_p;
5633
5634 /* Parse the first expression. */
5635 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5636
5637 for (;;)
5638 {
5639 /* Get an operator token. */
5640 token = cp_lexer_peek_token (parser->lexer);
5641
5642 new_prec = TOKEN_PRECEDENCE (token);
5643
5644 /* Popping an entry off the stack means we completed a subexpression:
5645 - either we found a token which is not an operator (`>' where it is not
5646 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5647 will happen repeatedly;
5648 - or, we found an operator which has lower priority. This is the case
5649 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5650 parsing `3 * 4'. */
5651 if (new_prec <= prec)
5652 {
5653 if (sp == stack)
5654 break;
5655 else
5656 goto pop;
5657 }
5658
5659 get_rhs:
5660 tree_type = binops_by_token[token->type].tree_type;
5661
5662 /* We used the operator token. */
5663 cp_lexer_consume_token (parser->lexer);
5664
5665 /* Extract another operand. It may be the RHS of this expression
5666 or the LHS of a new, higher priority expression. */
5667 rhs = cp_parser_simple_cast_expression (parser);
5668
5669 /* Get another operator token. Look up its precedence to avoid
5670 building a useless (immediately popped) stack entry for common
5671 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5672 token = cp_lexer_peek_token (parser->lexer);
5673 lookahead_prec = TOKEN_PRECEDENCE (token);
5674 if (lookahead_prec > new_prec)
5675 {
5676 /* ... and prepare to parse the RHS of the new, higher priority
5677 expression. Since precedence levels on the stack are
5678 monotonically increasing, we do not have to care about
5679 stack overflows. */
5680 sp->prec = prec;
5681 sp->tree_type = tree_type;
5682 sp->lhs = lhs;
5683 sp++;
5684 lhs = rhs;
5685 prec = new_prec;
5686 new_prec = lookahead_prec;
5687 goto get_rhs;
5688
5689 pop:
5690 /* If the stack is not empty, we have parsed into LHS the right side
5691 (`4' in the example above) of an expression we had suspended.
5692 We can use the information on the stack to recover the LHS (`3')
5693 from the stack together with the tree code (`MULT_EXPR'), and
5694 the precedence of the higher level subexpression
5695 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5696 which will be used to actually build the additive expression. */
5697 --sp;
5698 prec = sp->prec;
5699 tree_type = sp->tree_type;
5700 rhs = lhs;
5701 lhs = sp->lhs;
5702 }
5703
5704 overloaded_p = false;
5705 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5706
5707 /* If the binary operator required the use of an overloaded operator,
5708 then this expression cannot be an integral constant-expression.
5709 An overloaded operator can be used even if both operands are
5710 otherwise permissible in an integral constant-expression if at
5711 least one of the operands is of enumeration type. */
5712
5713 if (overloaded_p
5714 && (cp_parser_non_integral_constant_expression
5715 (parser, "calls to overloaded operators")))
5716 return error_mark_node;
5717 }
5718
5719 return lhs;
5720 }
5721
5722
5723 /* Parse the `? expression : assignment-expression' part of a
5724 conditional-expression. The LOGICAL_OR_EXPR is the
5725 logical-or-expression that started the conditional-expression.
5726 Returns a representation of the entire conditional-expression.
5727
5728 This routine is used by cp_parser_assignment_expression.
5729
5730 ? expression : assignment-expression
5731
5732 GNU Extensions:
5733
5734 ? : assignment-expression */
5735
5736 static tree
5737 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5738 {
5739 tree expr;
5740 tree assignment_expr;
5741
5742 /* Consume the `?' token. */
5743 cp_lexer_consume_token (parser->lexer);
5744 if (cp_parser_allow_gnu_extensions_p (parser)
5745 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5746 /* Implicit true clause. */
5747 expr = NULL_TREE;
5748 else
5749 /* Parse the expression. */
5750 expr = cp_parser_expression (parser, /*cast_p=*/false);
5751
5752 /* The next token should be a `:'. */
5753 cp_parser_require (parser, CPP_COLON, "`:'");
5754 /* Parse the assignment-expression. */
5755 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5756
5757 /* Build the conditional-expression. */
5758 return build_x_conditional_expr (logical_or_expr,
5759 expr,
5760 assignment_expr);
5761 }
5762
5763 /* Parse an assignment-expression.
5764
5765 assignment-expression:
5766 conditional-expression
5767 logical-or-expression assignment-operator assignment_expression
5768 throw-expression
5769
5770 CAST_P is true if this expression is the target of a cast.
5771
5772 Returns a representation for the expression. */
5773
5774 static tree
5775 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5776 {
5777 tree expr;
5778
5779 /* If the next token is the `throw' keyword, then we're looking at
5780 a throw-expression. */
5781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5782 expr = cp_parser_throw_expression (parser);
5783 /* Otherwise, it must be that we are looking at a
5784 logical-or-expression. */
5785 else
5786 {
5787 /* Parse the binary expressions (logical-or-expression). */
5788 expr = cp_parser_binary_expression (parser, cast_p);
5789 /* If the next token is a `?' then we're actually looking at a
5790 conditional-expression. */
5791 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5792 return cp_parser_question_colon_clause (parser, expr);
5793 else
5794 {
5795 enum tree_code assignment_operator;
5796
5797 /* If it's an assignment-operator, we're using the second
5798 production. */
5799 assignment_operator
5800 = cp_parser_assignment_operator_opt (parser);
5801 if (assignment_operator != ERROR_MARK)
5802 {
5803 tree rhs;
5804
5805 /* Parse the right-hand side of the assignment. */
5806 rhs = cp_parser_assignment_expression (parser, cast_p);
5807 /* An assignment may not appear in a
5808 constant-expression. */
5809 if (cp_parser_non_integral_constant_expression (parser,
5810 "an assignment"))
5811 return error_mark_node;
5812 /* Build the assignment expression. */
5813 expr = build_x_modify_expr (expr,
5814 assignment_operator,
5815 rhs);
5816 }
5817 }
5818 }
5819
5820 return expr;
5821 }
5822
5823 /* Parse an (optional) assignment-operator.
5824
5825 assignment-operator: one of
5826 = *= /= %= += -= >>= <<= &= ^= |=
5827
5828 GNU Extension:
5829
5830 assignment-operator: one of
5831 <?= >?=
5832
5833 If the next token is an assignment operator, the corresponding tree
5834 code is returned, and the token is consumed. For example, for
5835 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5836 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5837 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5838 operator, ERROR_MARK is returned. */
5839
5840 static enum tree_code
5841 cp_parser_assignment_operator_opt (cp_parser* parser)
5842 {
5843 enum tree_code op;
5844 cp_token *token;
5845
5846 /* Peek at the next toen. */
5847 token = cp_lexer_peek_token (parser->lexer);
5848
5849 switch (token->type)
5850 {
5851 case CPP_EQ:
5852 op = NOP_EXPR;
5853 break;
5854
5855 case CPP_MULT_EQ:
5856 op = MULT_EXPR;
5857 break;
5858
5859 case CPP_DIV_EQ:
5860 op = TRUNC_DIV_EXPR;
5861 break;
5862
5863 case CPP_MOD_EQ:
5864 op = TRUNC_MOD_EXPR;
5865 break;
5866
5867 case CPP_PLUS_EQ:
5868 op = PLUS_EXPR;
5869 break;
5870
5871 case CPP_MINUS_EQ:
5872 op = MINUS_EXPR;
5873 break;
5874
5875 case CPP_RSHIFT_EQ:
5876 op = RSHIFT_EXPR;
5877 break;
5878
5879 case CPP_LSHIFT_EQ:
5880 op = LSHIFT_EXPR;
5881 break;
5882
5883 case CPP_AND_EQ:
5884 op = BIT_AND_EXPR;
5885 break;
5886
5887 case CPP_XOR_EQ:
5888 op = BIT_XOR_EXPR;
5889 break;
5890
5891 case CPP_OR_EQ:
5892 op = BIT_IOR_EXPR;
5893 break;
5894
5895 default:
5896 /* Nothing else is an assignment operator. */
5897 op = ERROR_MARK;
5898 }
5899
5900 /* If it was an assignment operator, consume it. */
5901 if (op != ERROR_MARK)
5902 cp_lexer_consume_token (parser->lexer);
5903
5904 return op;
5905 }
5906
5907 /* Parse an expression.
5908
5909 expression:
5910 assignment-expression
5911 expression , assignment-expression
5912
5913 CAST_P is true if this expression is the target of a cast.
5914
5915 Returns a representation of the expression. */
5916
5917 static tree
5918 cp_parser_expression (cp_parser* parser, bool cast_p)
5919 {
5920 tree expression = NULL_TREE;
5921
5922 while (true)
5923 {
5924 tree assignment_expression;
5925
5926 /* Parse the next assignment-expression. */
5927 assignment_expression
5928 = cp_parser_assignment_expression (parser, cast_p);
5929 /* If this is the first assignment-expression, we can just
5930 save it away. */
5931 if (!expression)
5932 expression = assignment_expression;
5933 else
5934 expression = build_x_compound_expr (expression,
5935 assignment_expression);
5936 /* If the next token is not a comma, then we are done with the
5937 expression. */
5938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5939 break;
5940 /* Consume the `,'. */
5941 cp_lexer_consume_token (parser->lexer);
5942 /* A comma operator cannot appear in a constant-expression. */
5943 if (cp_parser_non_integral_constant_expression (parser,
5944 "a comma operator"))
5945 expression = error_mark_node;
5946 }
5947
5948 return expression;
5949 }
5950
5951 /* Parse a constant-expression.
5952
5953 constant-expression:
5954 conditional-expression
5955
5956 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5957 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5958 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5959 is false, NON_CONSTANT_P should be NULL. */
5960
5961 static tree
5962 cp_parser_constant_expression (cp_parser* parser,
5963 bool allow_non_constant_p,
5964 bool *non_constant_p)
5965 {
5966 bool saved_integral_constant_expression_p;
5967 bool saved_allow_non_integral_constant_expression_p;
5968 bool saved_non_integral_constant_expression_p;
5969 tree expression;
5970
5971 /* It might seem that we could simply parse the
5972 conditional-expression, and then check to see if it were
5973 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5974 one that the compiler can figure out is constant, possibly after
5975 doing some simplifications or optimizations. The standard has a
5976 precise definition of constant-expression, and we must honor
5977 that, even though it is somewhat more restrictive.
5978
5979 For example:
5980
5981 int i[(2, 3)];
5982
5983 is not a legal declaration, because `(2, 3)' is not a
5984 constant-expression. The `,' operator is forbidden in a
5985 constant-expression. However, GCC's constant-folding machinery
5986 will fold this operation to an INTEGER_CST for `3'. */
5987
5988 /* Save the old settings. */
5989 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5990 saved_allow_non_integral_constant_expression_p
5991 = parser->allow_non_integral_constant_expression_p;
5992 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5993 /* We are now parsing a constant-expression. */
5994 parser->integral_constant_expression_p = true;
5995 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5996 parser->non_integral_constant_expression_p = false;
5997 /* Although the grammar says "conditional-expression", we parse an
5998 "assignment-expression", which also permits "throw-expression"
5999 and the use of assignment operators. In the case that
6000 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6001 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6002 actually essential that we look for an assignment-expression.
6003 For example, cp_parser_initializer_clauses uses this function to
6004 determine whether a particular assignment-expression is in fact
6005 constant. */
6006 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6007 /* Restore the old settings. */
6008 parser->integral_constant_expression_p
6009 = saved_integral_constant_expression_p;
6010 parser->allow_non_integral_constant_expression_p
6011 = saved_allow_non_integral_constant_expression_p;
6012 if (allow_non_constant_p)
6013 *non_constant_p = parser->non_integral_constant_expression_p;
6014 else if (parser->non_integral_constant_expression_p)
6015 expression = error_mark_node;
6016 parser->non_integral_constant_expression_p
6017 = saved_non_integral_constant_expression_p;
6018
6019 return expression;
6020 }
6021
6022 /* Parse __builtin_offsetof.
6023
6024 offsetof-expression:
6025 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6026
6027 offsetof-member-designator:
6028 id-expression
6029 | offsetof-member-designator "." id-expression
6030 | offsetof-member-designator "[" expression "]" */
6031
6032 static tree
6033 cp_parser_builtin_offsetof (cp_parser *parser)
6034 {
6035 int save_ice_p, save_non_ice_p;
6036 tree type, expr;
6037 cp_id_kind dummy;
6038
6039 /* We're about to accept non-integral-constant things, but will
6040 definitely yield an integral constant expression. Save and
6041 restore these values around our local parsing. */
6042 save_ice_p = parser->integral_constant_expression_p;
6043 save_non_ice_p = parser->non_integral_constant_expression_p;
6044
6045 /* Consume the "__builtin_offsetof" token. */
6046 cp_lexer_consume_token (parser->lexer);
6047 /* Consume the opening `('. */
6048 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6049 /* Parse the type-id. */
6050 type = cp_parser_type_id (parser);
6051 /* Look for the `,'. */
6052 cp_parser_require (parser, CPP_COMMA, "`,'");
6053
6054 /* Build the (type *)null that begins the traditional offsetof macro. */
6055 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6056
6057 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6058 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6059 true, &dummy);
6060 while (true)
6061 {
6062 cp_token *token = cp_lexer_peek_token (parser->lexer);
6063 switch (token->type)
6064 {
6065 case CPP_OPEN_SQUARE:
6066 /* offsetof-member-designator "[" expression "]" */
6067 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6068 break;
6069
6070 case CPP_DOT:
6071 /* offsetof-member-designator "." identifier */
6072 cp_lexer_consume_token (parser->lexer);
6073 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6074 true, &dummy);
6075 break;
6076
6077 case CPP_CLOSE_PAREN:
6078 /* Consume the ")" token. */
6079 cp_lexer_consume_token (parser->lexer);
6080 goto success;
6081
6082 default:
6083 /* Error. We know the following require will fail, but
6084 that gives the proper error message. */
6085 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6086 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6087 expr = error_mark_node;
6088 goto failure;
6089 }
6090 }
6091
6092 success:
6093 /* If we're processing a template, we can't finish the semantics yet.
6094 Otherwise we can fold the entire expression now. */
6095 if (processing_template_decl)
6096 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6097 else
6098 expr = finish_offsetof (expr);
6099
6100 failure:
6101 parser->integral_constant_expression_p = save_ice_p;
6102 parser->non_integral_constant_expression_p = save_non_ice_p;
6103
6104 return expr;
6105 }
6106
6107 /* Statements [gram.stmt.stmt] */
6108
6109 /* Parse a statement.
6110
6111 statement:
6112 labeled-statement
6113 expression-statement
6114 compound-statement
6115 selection-statement
6116 iteration-statement
6117 jump-statement
6118 declaration-statement
6119 try-block
6120
6121 IN_COMPOUND is true when the statement is nested inside a
6122 cp_parser_compound_statement; this matters for certain pragmas. */
6123
6124 static void
6125 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6126 bool in_compound)
6127 {
6128 tree statement;
6129 cp_token *token;
6130 location_t statement_location;
6131
6132 restart:
6133 /* There is no statement yet. */
6134 statement = NULL_TREE;
6135 /* Peek at the next token. */
6136 token = cp_lexer_peek_token (parser->lexer);
6137 /* Remember the location of the first token in the statement. */
6138 statement_location = token->location;
6139 /* If this is a keyword, then that will often determine what kind of
6140 statement we have. */
6141 if (token->type == CPP_KEYWORD)
6142 {
6143 enum rid keyword = token->keyword;
6144
6145 switch (keyword)
6146 {
6147 case RID_CASE:
6148 case RID_DEFAULT:
6149 /* Looks like a labeled-statement with a case label.
6150 Parse the label, and then use tail recursion to parse
6151 the statement. */
6152 cp_parser_label_for_labeled_statement (parser);
6153 goto restart;
6154
6155 case RID_IF:
6156 case RID_SWITCH:
6157 statement = cp_parser_selection_statement (parser);
6158 break;
6159
6160 case RID_WHILE:
6161 case RID_DO:
6162 case RID_FOR:
6163 statement = cp_parser_iteration_statement (parser);
6164 break;
6165
6166 case RID_BREAK:
6167 case RID_CONTINUE:
6168 case RID_RETURN:
6169 case RID_GOTO:
6170 statement = cp_parser_jump_statement (parser);
6171 break;
6172
6173 /* Objective-C++ exception-handling constructs. */
6174 case RID_AT_TRY:
6175 case RID_AT_CATCH:
6176 case RID_AT_FINALLY:
6177 case RID_AT_SYNCHRONIZED:
6178 case RID_AT_THROW:
6179 statement = cp_parser_objc_statement (parser);
6180 break;
6181
6182 case RID_TRY:
6183 statement = cp_parser_try_block (parser);
6184 break;
6185
6186 default:
6187 /* It might be a keyword like `int' that can start a
6188 declaration-statement. */
6189 break;
6190 }
6191 }
6192 else if (token->type == CPP_NAME)
6193 {
6194 /* If the next token is a `:', then we are looking at a
6195 labeled-statement. */
6196 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6197 if (token->type == CPP_COLON)
6198 {
6199 /* Looks like a labeled-statement with an ordinary label.
6200 Parse the label, and then use tail recursion to parse
6201 the statement. */
6202 cp_parser_label_for_labeled_statement (parser);
6203 goto restart;
6204 }
6205 }
6206 /* Anything that starts with a `{' must be a compound-statement. */
6207 else if (token->type == CPP_OPEN_BRACE)
6208 statement = cp_parser_compound_statement (parser, NULL, false);
6209 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6210 a statement all its own. */
6211 else if (token->type == CPP_PRAGMA)
6212 {
6213 /* Only certain OpenMP pragmas are attached to statements, and thus
6214 are considered statements themselves. All others are not. In
6215 the context of a compound, accept the pragma as a "statement" and
6216 return so that we can check for a close brace. Otherwise we
6217 require a real statement and must go back and read one. */
6218 if (in_compound)
6219 cp_parser_pragma (parser, pragma_compound);
6220 else if (!cp_parser_pragma (parser, pragma_stmt))
6221 goto restart;
6222 return;
6223 }
6224 else if (token->type == CPP_EOF)
6225 {
6226 cp_parser_error (parser, "expected statement");
6227 return;
6228 }
6229
6230 /* Everything else must be a declaration-statement or an
6231 expression-statement. Try for the declaration-statement
6232 first, unless we are looking at a `;', in which case we know that
6233 we have an expression-statement. */
6234 if (!statement)
6235 {
6236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6237 {
6238 cp_parser_parse_tentatively (parser);
6239 /* Try to parse the declaration-statement. */
6240 cp_parser_declaration_statement (parser);
6241 /* If that worked, we're done. */
6242 if (cp_parser_parse_definitely (parser))
6243 return;
6244 }
6245 /* Look for an expression-statement instead. */
6246 statement = cp_parser_expression_statement (parser, in_statement_expr);
6247 }
6248
6249 /* Set the line number for the statement. */
6250 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6251 SET_EXPR_LOCATION (statement, statement_location);
6252 }
6253
6254 /* Parse the label for a labeled-statement, i.e.
6255
6256 identifier :
6257 case constant-expression :
6258 default :
6259
6260 GNU Extension:
6261 case constant-expression ... constant-expression : statement
6262
6263 When a label is parsed without errors, the label is added to the
6264 parse tree by the finish_* functions, so this function doesn't
6265 have to return the label. */
6266
6267 static void
6268 cp_parser_label_for_labeled_statement (cp_parser* parser)
6269 {
6270 cp_token *token;
6271
6272 /* The next token should be an identifier. */
6273 token = cp_lexer_peek_token (parser->lexer);
6274 if (token->type != CPP_NAME
6275 && token->type != CPP_KEYWORD)
6276 {
6277 cp_parser_error (parser, "expected labeled-statement");
6278 return;
6279 }
6280
6281 switch (token->keyword)
6282 {
6283 case RID_CASE:
6284 {
6285 tree expr, expr_hi;
6286 cp_token *ellipsis;
6287
6288 /* Consume the `case' token. */
6289 cp_lexer_consume_token (parser->lexer);
6290 /* Parse the constant-expression. */
6291 expr = cp_parser_constant_expression (parser,
6292 /*allow_non_constant_p=*/false,
6293 NULL);
6294
6295 ellipsis = cp_lexer_peek_token (parser->lexer);
6296 if (ellipsis->type == CPP_ELLIPSIS)
6297 {
6298 /* Consume the `...' token. */
6299 cp_lexer_consume_token (parser->lexer);
6300 expr_hi =
6301 cp_parser_constant_expression (parser,
6302 /*allow_non_constant_p=*/false,
6303 NULL);
6304 /* We don't need to emit warnings here, as the common code
6305 will do this for us. */
6306 }
6307 else
6308 expr_hi = NULL_TREE;
6309
6310 if (parser->in_switch_statement_p)
6311 finish_case_label (expr, expr_hi);
6312 else
6313 error ("case label %qE not within a switch statement", expr);
6314 }
6315 break;
6316
6317 case RID_DEFAULT:
6318 /* Consume the `default' token. */
6319 cp_lexer_consume_token (parser->lexer);
6320
6321 if (parser->in_switch_statement_p)
6322 finish_case_label (NULL_TREE, NULL_TREE);
6323 else
6324 error ("case label not within a switch statement");
6325 break;
6326
6327 default:
6328 /* Anything else must be an ordinary label. */
6329 finish_label_stmt (cp_parser_identifier (parser));
6330 break;
6331 }
6332
6333 /* Require the `:' token. */
6334 cp_parser_require (parser, CPP_COLON, "`:'");
6335 }
6336
6337 /* Parse an expression-statement.
6338
6339 expression-statement:
6340 expression [opt] ;
6341
6342 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6343 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6344 indicates whether this expression-statement is part of an
6345 expression statement. */
6346
6347 static tree
6348 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6349 {
6350 tree statement = NULL_TREE;
6351
6352 /* If the next token is a ';', then there is no expression
6353 statement. */
6354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6355 statement = cp_parser_expression (parser, /*cast_p=*/false);
6356
6357 /* Consume the final `;'. */
6358 cp_parser_consume_semicolon_at_end_of_statement (parser);
6359
6360 if (in_statement_expr
6361 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6362 /* This is the final expression statement of a statement
6363 expression. */
6364 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6365 else if (statement)
6366 statement = finish_expr_stmt (statement);
6367 else
6368 finish_stmt ();
6369
6370 return statement;
6371 }
6372
6373 /* Parse a compound-statement.
6374
6375 compound-statement:
6376 { statement-seq [opt] }
6377
6378 Returns a tree representing the statement. */
6379
6380 static tree
6381 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6382 bool in_try)
6383 {
6384 tree compound_stmt;
6385
6386 /* Consume the `{'. */
6387 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6388 return error_mark_node;
6389 /* Begin the compound-statement. */
6390 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6391 /* Parse an (optional) statement-seq. */
6392 cp_parser_statement_seq_opt (parser, in_statement_expr);
6393 /* Finish the compound-statement. */
6394 finish_compound_stmt (compound_stmt);
6395 /* Consume the `}'. */
6396 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6397
6398 return compound_stmt;
6399 }
6400
6401 /* Parse an (optional) statement-seq.
6402
6403 statement-seq:
6404 statement
6405 statement-seq [opt] statement */
6406
6407 static void
6408 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6409 {
6410 /* Scan statements until there aren't any more. */
6411 while (true)
6412 {
6413 cp_token *token = cp_lexer_peek_token (parser->lexer);
6414
6415 /* If we're looking at a `}', then we've run out of statements. */
6416 if (token->type == CPP_CLOSE_BRACE
6417 || token->type == CPP_EOF
6418 || token->type == CPP_PRAGMA_EOL)
6419 break;
6420
6421 /* Parse the statement. */
6422 cp_parser_statement (parser, in_statement_expr, true);
6423 }
6424 }
6425
6426 /* Parse a selection-statement.
6427
6428 selection-statement:
6429 if ( condition ) statement
6430 if ( condition ) statement else statement
6431 switch ( condition ) statement
6432
6433 Returns the new IF_STMT or SWITCH_STMT. */
6434
6435 static tree
6436 cp_parser_selection_statement (cp_parser* parser)
6437 {
6438 cp_token *token;
6439 enum rid keyword;
6440
6441 /* Peek at the next token. */
6442 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6443
6444 /* See what kind of keyword it is. */
6445 keyword = token->keyword;
6446 switch (keyword)
6447 {
6448 case RID_IF:
6449 case RID_SWITCH:
6450 {
6451 tree statement;
6452 tree condition;
6453
6454 /* Look for the `('. */
6455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6456 {
6457 cp_parser_skip_to_end_of_statement (parser);
6458 return error_mark_node;
6459 }
6460
6461 /* Begin the selection-statement. */
6462 if (keyword == RID_IF)
6463 statement = begin_if_stmt ();
6464 else
6465 statement = begin_switch_stmt ();
6466
6467 /* Parse the condition. */
6468 condition = cp_parser_condition (parser);
6469 /* Look for the `)'. */
6470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6471 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6472 /*consume_paren=*/true);
6473
6474 if (keyword == RID_IF)
6475 {
6476 /* Add the condition. */
6477 finish_if_stmt_cond (condition, statement);
6478
6479 /* Parse the then-clause. */
6480 cp_parser_implicitly_scoped_statement (parser);
6481 finish_then_clause (statement);
6482
6483 /* If the next token is `else', parse the else-clause. */
6484 if (cp_lexer_next_token_is_keyword (parser->lexer,
6485 RID_ELSE))
6486 {
6487 /* Consume the `else' keyword. */
6488 cp_lexer_consume_token (parser->lexer);
6489 begin_else_clause (statement);
6490 /* Parse the else-clause. */
6491 cp_parser_implicitly_scoped_statement (parser);
6492 finish_else_clause (statement);
6493 }
6494
6495 /* Now we're all done with the if-statement. */
6496 finish_if_stmt (statement);
6497 }
6498 else
6499 {
6500 bool in_switch_statement_p;
6501 unsigned char in_statement;
6502
6503 /* Add the condition. */
6504 finish_switch_cond (condition, statement);
6505
6506 /* Parse the body of the switch-statement. */
6507 in_switch_statement_p = parser->in_switch_statement_p;
6508 in_statement = parser->in_statement;
6509 parser->in_switch_statement_p = true;
6510 parser->in_statement |= IN_SWITCH_STMT;
6511 cp_parser_implicitly_scoped_statement (parser);
6512 parser->in_switch_statement_p = in_switch_statement_p;
6513 parser->in_statement = in_statement;
6514
6515 /* Now we're all done with the switch-statement. */
6516 finish_switch_stmt (statement);
6517 }
6518
6519 return statement;
6520 }
6521 break;
6522
6523 default:
6524 cp_parser_error (parser, "expected selection-statement");
6525 return error_mark_node;
6526 }
6527 }
6528
6529 /* Parse a condition.
6530
6531 condition:
6532 expression
6533 type-specifier-seq declarator = assignment-expression
6534
6535 GNU Extension:
6536
6537 condition:
6538 type-specifier-seq declarator asm-specification [opt]
6539 attributes [opt] = assignment-expression
6540
6541 Returns the expression that should be tested. */
6542
6543 static tree
6544 cp_parser_condition (cp_parser* parser)
6545 {
6546 cp_decl_specifier_seq type_specifiers;
6547 const char *saved_message;
6548
6549 /* Try the declaration first. */
6550 cp_parser_parse_tentatively (parser);
6551 /* New types are not allowed in the type-specifier-seq for a
6552 condition. */
6553 saved_message = parser->type_definition_forbidden_message;
6554 parser->type_definition_forbidden_message
6555 = "types may not be defined in conditions";
6556 /* Parse the type-specifier-seq. */
6557 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6558 &type_specifiers);
6559 /* Restore the saved message. */
6560 parser->type_definition_forbidden_message = saved_message;
6561 /* If all is well, we might be looking at a declaration. */
6562 if (!cp_parser_error_occurred (parser))
6563 {
6564 tree decl;
6565 tree asm_specification;
6566 tree attributes;
6567 cp_declarator *declarator;
6568 tree initializer = NULL_TREE;
6569
6570 /* Parse the declarator. */
6571 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6572 /*ctor_dtor_or_conv_p=*/NULL,
6573 /*parenthesized_p=*/NULL,
6574 /*member_p=*/false);
6575 /* Parse the attributes. */
6576 attributes = cp_parser_attributes_opt (parser);
6577 /* Parse the asm-specification. */
6578 asm_specification = cp_parser_asm_specification_opt (parser);
6579 /* If the next token is not an `=', then we might still be
6580 looking at an expression. For example:
6581
6582 if (A(a).x)
6583
6584 looks like a decl-specifier-seq and a declarator -- but then
6585 there is no `=', so this is an expression. */
6586 cp_parser_require (parser, CPP_EQ, "`='");
6587 /* If we did see an `=', then we are looking at a declaration
6588 for sure. */
6589 if (cp_parser_parse_definitely (parser))
6590 {
6591 tree pushed_scope;
6592 bool non_constant_p;
6593
6594 /* Create the declaration. */
6595 decl = start_decl (declarator, &type_specifiers,
6596 /*initialized_p=*/true,
6597 attributes, /*prefix_attributes=*/NULL_TREE,
6598 &pushed_scope);
6599 /* Parse the assignment-expression. */
6600 initializer
6601 = cp_parser_constant_expression (parser,
6602 /*allow_non_constant_p=*/true,
6603 &non_constant_p);
6604 if (!non_constant_p)
6605 initializer = fold_non_dependent_expr (initializer);
6606
6607 /* Process the initializer. */
6608 cp_finish_decl (decl,
6609 initializer, !non_constant_p,
6610 asm_specification,
6611 LOOKUP_ONLYCONVERTING);
6612
6613 if (pushed_scope)
6614 pop_scope (pushed_scope);
6615
6616 return convert_from_reference (decl);
6617 }
6618 }
6619 /* If we didn't even get past the declarator successfully, we are
6620 definitely not looking at a declaration. */
6621 else
6622 cp_parser_abort_tentative_parse (parser);
6623
6624 /* Otherwise, we are looking at an expression. */
6625 return cp_parser_expression (parser, /*cast_p=*/false);
6626 }
6627
6628 /* Parse an iteration-statement.
6629
6630 iteration-statement:
6631 while ( condition ) statement
6632 do statement while ( expression ) ;
6633 for ( for-init-statement condition [opt] ; expression [opt] )
6634 statement
6635
6636 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6637
6638 static tree
6639 cp_parser_iteration_statement (cp_parser* parser)
6640 {
6641 cp_token *token;
6642 enum rid keyword;
6643 tree statement;
6644 unsigned char in_statement;
6645
6646 /* Peek at the next token. */
6647 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6648 if (!token)
6649 return error_mark_node;
6650
6651 /* Remember whether or not we are already within an iteration
6652 statement. */
6653 in_statement = parser->in_statement;
6654
6655 /* See what kind of keyword it is. */
6656 keyword = token->keyword;
6657 switch (keyword)
6658 {
6659 case RID_WHILE:
6660 {
6661 tree condition;
6662
6663 /* Begin the while-statement. */
6664 statement = begin_while_stmt ();
6665 /* Look for the `('. */
6666 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6667 /* Parse the condition. */
6668 condition = cp_parser_condition (parser);
6669 finish_while_stmt_cond (condition, statement);
6670 /* Look for the `)'. */
6671 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6672 /* Parse the dependent statement. */
6673 parser->in_statement = IN_ITERATION_STMT;
6674 cp_parser_already_scoped_statement (parser);
6675 parser->in_statement = in_statement;
6676 /* We're done with the while-statement. */
6677 finish_while_stmt (statement);
6678 }
6679 break;
6680
6681 case RID_DO:
6682 {
6683 tree expression;
6684
6685 /* Begin the do-statement. */
6686 statement = begin_do_stmt ();
6687 /* Parse the body of the do-statement. */
6688 parser->in_statement = IN_ITERATION_STMT;
6689 cp_parser_implicitly_scoped_statement (parser);
6690 parser->in_statement = in_statement;
6691 finish_do_body (statement);
6692 /* Look for the `while' keyword. */
6693 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6694 /* Look for the `('. */
6695 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6696 /* Parse the expression. */
6697 expression = cp_parser_expression (parser, /*cast_p=*/false);
6698 /* We're done with the do-statement. */
6699 finish_do_stmt (expression, statement);
6700 /* Look for the `)'. */
6701 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6702 /* Look for the `;'. */
6703 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6704 }
6705 break;
6706
6707 case RID_FOR:
6708 {
6709 tree condition = NULL_TREE;
6710 tree expression = NULL_TREE;
6711
6712 /* Begin the for-statement. */
6713 statement = begin_for_stmt ();
6714 /* Look for the `('. */
6715 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6716 /* Parse the initialization. */
6717 cp_parser_for_init_statement (parser);
6718 finish_for_init_stmt (statement);
6719
6720 /* If there's a condition, process it. */
6721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6722 condition = cp_parser_condition (parser);
6723 finish_for_cond (condition, statement);
6724 /* Look for the `;'. */
6725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6726
6727 /* If there's an expression, process it. */
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6729 expression = cp_parser_expression (parser, /*cast_p=*/false);
6730 finish_for_expr (expression, statement);
6731 /* Look for the `)'. */
6732 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6733
6734 /* Parse the body of the for-statement. */
6735 parser->in_statement = IN_ITERATION_STMT;
6736 cp_parser_already_scoped_statement (parser);
6737 parser->in_statement = in_statement;
6738
6739 /* We're done with the for-statement. */
6740 finish_for_stmt (statement);
6741 }
6742 break;
6743
6744 default:
6745 cp_parser_error (parser, "expected iteration-statement");
6746 statement = error_mark_node;
6747 break;
6748 }
6749
6750 return statement;
6751 }
6752
6753 /* Parse a for-init-statement.
6754
6755 for-init-statement:
6756 expression-statement
6757 simple-declaration */
6758
6759 static void
6760 cp_parser_for_init_statement (cp_parser* parser)
6761 {
6762 /* If the next token is a `;', then we have an empty
6763 expression-statement. Grammatically, this is also a
6764 simple-declaration, but an invalid one, because it does not
6765 declare anything. Therefore, if we did not handle this case
6766 specially, we would issue an error message about an invalid
6767 declaration. */
6768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6769 {
6770 /* We're going to speculatively look for a declaration, falling back
6771 to an expression, if necessary. */
6772 cp_parser_parse_tentatively (parser);
6773 /* Parse the declaration. */
6774 cp_parser_simple_declaration (parser,
6775 /*function_definition_allowed_p=*/false);
6776 /* If the tentative parse failed, then we shall need to look for an
6777 expression-statement. */
6778 if (cp_parser_parse_definitely (parser))
6779 return;
6780 }
6781
6782 cp_parser_expression_statement (parser, false);
6783 }
6784
6785 /* Parse a jump-statement.
6786
6787 jump-statement:
6788 break ;
6789 continue ;
6790 return expression [opt] ;
6791 goto identifier ;
6792
6793 GNU extension:
6794
6795 jump-statement:
6796 goto * expression ;
6797
6798 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6799
6800 static tree
6801 cp_parser_jump_statement (cp_parser* parser)
6802 {
6803 tree statement = error_mark_node;
6804 cp_token *token;
6805 enum rid keyword;
6806
6807 /* Peek at the next token. */
6808 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6809 if (!token)
6810 return error_mark_node;
6811
6812 /* See what kind of keyword it is. */
6813 keyword = token->keyword;
6814 switch (keyword)
6815 {
6816 case RID_BREAK:
6817 switch (parser->in_statement)
6818 {
6819 case 0:
6820 error ("break statement not within loop or switch");
6821 break;
6822 default:
6823 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6824 || parser->in_statement == IN_ITERATION_STMT);
6825 statement = finish_break_stmt ();
6826 break;
6827 case IN_OMP_BLOCK:
6828 error ("invalid exit from OpenMP structured block");
6829 break;
6830 case IN_OMP_FOR:
6831 error ("break statement used with OpenMP for loop");
6832 break;
6833 }
6834 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6835 break;
6836
6837 case RID_CONTINUE:
6838 switch (parser->in_statement & ~IN_SWITCH_STMT)
6839 {
6840 case 0:
6841 error ("continue statement not within a loop");
6842 break;
6843 case IN_ITERATION_STMT:
6844 case IN_OMP_FOR:
6845 statement = finish_continue_stmt ();
6846 break;
6847 case IN_OMP_BLOCK:
6848 error ("invalid exit from OpenMP structured block");
6849 break;
6850 default:
6851 gcc_unreachable ();
6852 }
6853 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6854 break;
6855
6856 case RID_RETURN:
6857 {
6858 tree expr;
6859
6860 /* If the next token is a `;', then there is no
6861 expression. */
6862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6863 expr = cp_parser_expression (parser, /*cast_p=*/false);
6864 else
6865 expr = NULL_TREE;
6866 /* Build the return-statement. */
6867 statement = finish_return_stmt (expr);
6868 /* Look for the final `;'. */
6869 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6870 }
6871 break;
6872
6873 case RID_GOTO:
6874 /* Create the goto-statement. */
6875 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6876 {
6877 /* Issue a warning about this use of a GNU extension. */
6878 if (pedantic)
6879 pedwarn ("ISO C++ forbids computed gotos");
6880 /* Consume the '*' token. */
6881 cp_lexer_consume_token (parser->lexer);
6882 /* Parse the dependent expression. */
6883 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6884 }
6885 else
6886 finish_goto_stmt (cp_parser_identifier (parser));
6887 /* Look for the final `;'. */
6888 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6889 break;
6890
6891 default:
6892 cp_parser_error (parser, "expected jump-statement");
6893 break;
6894 }
6895
6896 return statement;
6897 }
6898
6899 /* Parse a declaration-statement.
6900
6901 declaration-statement:
6902 block-declaration */
6903
6904 static void
6905 cp_parser_declaration_statement (cp_parser* parser)
6906 {
6907 void *p;
6908
6909 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6910 p = obstack_alloc (&declarator_obstack, 0);
6911
6912 /* Parse the block-declaration. */
6913 cp_parser_block_declaration (parser, /*statement_p=*/true);
6914
6915 /* Free any declarators allocated. */
6916 obstack_free (&declarator_obstack, p);
6917
6918 /* Finish off the statement. */
6919 finish_stmt ();
6920 }
6921
6922 /* Some dependent statements (like `if (cond) statement'), are
6923 implicitly in their own scope. In other words, if the statement is
6924 a single statement (as opposed to a compound-statement), it is
6925 none-the-less treated as if it were enclosed in braces. Any
6926 declarations appearing in the dependent statement are out of scope
6927 after control passes that point. This function parses a statement,
6928 but ensures that is in its own scope, even if it is not a
6929 compound-statement.
6930
6931 Returns the new statement. */
6932
6933 static tree
6934 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6935 {
6936 tree statement;
6937
6938 /* Mark if () ; with a special NOP_EXPR. */
6939 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6940 {
6941 cp_lexer_consume_token (parser->lexer);
6942 statement = add_stmt (build_empty_stmt ());
6943 }
6944 /* if a compound is opened, we simply parse the statement directly. */
6945 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6946 statement = cp_parser_compound_statement (parser, NULL, false);
6947 /* If the token is not a `{', then we must take special action. */
6948 else
6949 {
6950 /* Create a compound-statement. */
6951 statement = begin_compound_stmt (0);
6952 /* Parse the dependent-statement. */
6953 cp_parser_statement (parser, NULL_TREE, false);
6954 /* Finish the dummy compound-statement. */
6955 finish_compound_stmt (statement);
6956 }
6957
6958 /* Return the statement. */
6959 return statement;
6960 }
6961
6962 /* For some dependent statements (like `while (cond) statement'), we
6963 have already created a scope. Therefore, even if the dependent
6964 statement is a compound-statement, we do not want to create another
6965 scope. */
6966
6967 static void
6968 cp_parser_already_scoped_statement (cp_parser* parser)
6969 {
6970 /* If the token is a `{', then we must take special action. */
6971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6972 cp_parser_statement (parser, NULL_TREE, false);
6973 else
6974 {
6975 /* Avoid calling cp_parser_compound_statement, so that we
6976 don't create a new scope. Do everything else by hand. */
6977 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6978 cp_parser_statement_seq_opt (parser, NULL_TREE);
6979 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6980 }
6981 }
6982
6983 /* Declarations [gram.dcl.dcl] */
6984
6985 /* Parse an optional declaration-sequence.
6986
6987 declaration-seq:
6988 declaration
6989 declaration-seq declaration */
6990
6991 static void
6992 cp_parser_declaration_seq_opt (cp_parser* parser)
6993 {
6994 while (true)
6995 {
6996 cp_token *token;
6997
6998 token = cp_lexer_peek_token (parser->lexer);
6999
7000 if (token->type == CPP_CLOSE_BRACE
7001 || token->type == CPP_EOF
7002 || token->type == CPP_PRAGMA_EOL)
7003 break;
7004
7005 if (token->type == CPP_SEMICOLON)
7006 {
7007 /* A declaration consisting of a single semicolon is
7008 invalid. Allow it unless we're being pedantic. */
7009 cp_lexer_consume_token (parser->lexer);
7010 if (pedantic && !in_system_header)
7011 pedwarn ("extra %<;%>");
7012 continue;
7013 }
7014
7015 /* If we're entering or exiting a region that's implicitly
7016 extern "C", modify the lang context appropriately. */
7017 if (!parser->implicit_extern_c && token->implicit_extern_c)
7018 {
7019 push_lang_context (lang_name_c);
7020 parser->implicit_extern_c = true;
7021 }
7022 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7023 {
7024 pop_lang_context ();
7025 parser->implicit_extern_c = false;
7026 }
7027
7028 if (token->type == CPP_PRAGMA)
7029 {
7030 /* A top-level declaration can consist solely of a #pragma.
7031 A nested declaration cannot, so this is done here and not
7032 in cp_parser_declaration. (A #pragma at block scope is
7033 handled in cp_parser_statement.) */
7034 cp_parser_pragma (parser, pragma_external);
7035 continue;
7036 }
7037
7038 /* Parse the declaration itself. */
7039 cp_parser_declaration (parser);
7040 }
7041 }
7042
7043 /* Parse a declaration.
7044
7045 declaration:
7046 block-declaration
7047 function-definition
7048 template-declaration
7049 explicit-instantiation
7050 explicit-specialization
7051 linkage-specification
7052 namespace-definition
7053
7054 GNU extension:
7055
7056 declaration:
7057 __extension__ declaration */
7058
7059 static void
7060 cp_parser_declaration (cp_parser* parser)
7061 {
7062 cp_token token1;
7063 cp_token token2;
7064 int saved_pedantic;
7065 void *p;
7066
7067 /* Check for the `__extension__' keyword. */
7068 if (cp_parser_extension_opt (parser, &saved_pedantic))
7069 {
7070 /* Parse the qualified declaration. */
7071 cp_parser_declaration (parser);
7072 /* Restore the PEDANTIC flag. */
7073 pedantic = saved_pedantic;
7074
7075 return;
7076 }
7077
7078 /* Try to figure out what kind of declaration is present. */
7079 token1 = *cp_lexer_peek_token (parser->lexer);
7080
7081 if (token1.type != CPP_EOF)
7082 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7083 else
7084 {
7085 token2.type = CPP_EOF;
7086 token2.keyword = RID_MAX;
7087 }
7088
7089 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7090 p = obstack_alloc (&declarator_obstack, 0);
7091
7092 /* If the next token is `extern' and the following token is a string
7093 literal, then we have a linkage specification. */
7094 if (token1.keyword == RID_EXTERN
7095 && cp_parser_is_string_literal (&token2))
7096 cp_parser_linkage_specification (parser);
7097 /* If the next token is `template', then we have either a template
7098 declaration, an explicit instantiation, or an explicit
7099 specialization. */
7100 else if (token1.keyword == RID_TEMPLATE)
7101 {
7102 /* `template <>' indicates a template specialization. */
7103 if (token2.type == CPP_LESS
7104 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7105 cp_parser_explicit_specialization (parser);
7106 /* `template <' indicates a template declaration. */
7107 else if (token2.type == CPP_LESS)
7108 cp_parser_template_declaration (parser, /*member_p=*/false);
7109 /* Anything else must be an explicit instantiation. */
7110 else
7111 cp_parser_explicit_instantiation (parser);
7112 }
7113 /* If the next token is `export', then we have a template
7114 declaration. */
7115 else if (token1.keyword == RID_EXPORT)
7116 cp_parser_template_declaration (parser, /*member_p=*/false);
7117 /* If the next token is `extern', 'static' or 'inline' and the one
7118 after that is `template', we have a GNU extended explicit
7119 instantiation directive. */
7120 else if (cp_parser_allow_gnu_extensions_p (parser)
7121 && (token1.keyword == RID_EXTERN
7122 || token1.keyword == RID_STATIC
7123 || token1.keyword == RID_INLINE)
7124 && token2.keyword == RID_TEMPLATE)
7125 cp_parser_explicit_instantiation (parser);
7126 /* If the next token is `namespace', check for a named or unnamed
7127 namespace definition. */
7128 else if (token1.keyword == RID_NAMESPACE
7129 && (/* A named namespace definition. */
7130 (token2.type == CPP_NAME
7131 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7132 != CPP_EQ))
7133 /* An unnamed namespace definition. */
7134 || token2.type == CPP_OPEN_BRACE
7135 || token2.keyword == RID_ATTRIBUTE))
7136 cp_parser_namespace_definition (parser);
7137 /* Objective-C++ declaration/definition. */
7138 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7139 cp_parser_objc_declaration (parser);
7140 /* We must have either a block declaration or a function
7141 definition. */
7142 else
7143 /* Try to parse a block-declaration, or a function-definition. */
7144 cp_parser_block_declaration (parser, /*statement_p=*/false);
7145
7146 /* Free any declarators allocated. */
7147 obstack_free (&declarator_obstack, p);
7148 }
7149
7150 /* Parse a block-declaration.
7151
7152 block-declaration:
7153 simple-declaration
7154 asm-definition
7155 namespace-alias-definition
7156 using-declaration
7157 using-directive
7158
7159 GNU Extension:
7160
7161 block-declaration:
7162 __extension__ block-declaration
7163 label-declaration
7164
7165 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7166 part of a declaration-statement. */
7167
7168 static void
7169 cp_parser_block_declaration (cp_parser *parser,
7170 bool statement_p)
7171 {
7172 cp_token *token1;
7173 int saved_pedantic;
7174
7175 /* Check for the `__extension__' keyword. */
7176 if (cp_parser_extension_opt (parser, &saved_pedantic))
7177 {
7178 /* Parse the qualified declaration. */
7179 cp_parser_block_declaration (parser, statement_p);
7180 /* Restore the PEDANTIC flag. */
7181 pedantic = saved_pedantic;
7182
7183 return;
7184 }
7185
7186 /* Peek at the next token to figure out which kind of declaration is
7187 present. */
7188 token1 = cp_lexer_peek_token (parser->lexer);
7189
7190 /* If the next keyword is `asm', we have an asm-definition. */
7191 if (token1->keyword == RID_ASM)
7192 {
7193 if (statement_p)
7194 cp_parser_commit_to_tentative_parse (parser);
7195 cp_parser_asm_definition (parser);
7196 }
7197 /* If the next keyword is `namespace', we have a
7198 namespace-alias-definition. */
7199 else if (token1->keyword == RID_NAMESPACE)
7200 cp_parser_namespace_alias_definition (parser);
7201 /* If the next keyword is `using', we have either a
7202 using-declaration or a using-directive. */
7203 else if (token1->keyword == RID_USING)
7204 {
7205 cp_token *token2;
7206
7207 if (statement_p)
7208 cp_parser_commit_to_tentative_parse (parser);
7209 /* If the token after `using' is `namespace', then we have a
7210 using-directive. */
7211 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7212 if (token2->keyword == RID_NAMESPACE)
7213 cp_parser_using_directive (parser);
7214 /* Otherwise, it's a using-declaration. */
7215 else
7216 cp_parser_using_declaration (parser,
7217 /*access_declaration_p=*/false);
7218 }
7219 /* If the next keyword is `__label__' we have a label declaration. */
7220 else if (token1->keyword == RID_LABEL)
7221 {
7222 if (statement_p)
7223 cp_parser_commit_to_tentative_parse (parser);
7224 cp_parser_label_declaration (parser);
7225 }
7226 /* Anything else must be a simple-declaration. */
7227 else
7228 cp_parser_simple_declaration (parser, !statement_p);
7229 }
7230
7231 /* Parse a simple-declaration.
7232
7233 simple-declaration:
7234 decl-specifier-seq [opt] init-declarator-list [opt] ;
7235
7236 init-declarator-list:
7237 init-declarator
7238 init-declarator-list , init-declarator
7239
7240 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7241 function-definition as a simple-declaration. */
7242
7243 static void
7244 cp_parser_simple_declaration (cp_parser* parser,
7245 bool function_definition_allowed_p)
7246 {
7247 cp_decl_specifier_seq decl_specifiers;
7248 int declares_class_or_enum;
7249 bool saw_declarator;
7250
7251 /* Defer access checks until we know what is being declared; the
7252 checks for names appearing in the decl-specifier-seq should be
7253 done as if we were in the scope of the thing being declared. */
7254 push_deferring_access_checks (dk_deferred);
7255
7256 /* Parse the decl-specifier-seq. We have to keep track of whether
7257 or not the decl-specifier-seq declares a named class or
7258 enumeration type, since that is the only case in which the
7259 init-declarator-list is allowed to be empty.
7260
7261 [dcl.dcl]
7262
7263 In a simple-declaration, the optional init-declarator-list can be
7264 omitted only when declaring a class or enumeration, that is when
7265 the decl-specifier-seq contains either a class-specifier, an
7266 elaborated-type-specifier, or an enum-specifier. */
7267 cp_parser_decl_specifier_seq (parser,
7268 CP_PARSER_FLAGS_OPTIONAL,
7269 &decl_specifiers,
7270 &declares_class_or_enum);
7271 /* We no longer need to defer access checks. */
7272 stop_deferring_access_checks ();
7273
7274 /* In a block scope, a valid declaration must always have a
7275 decl-specifier-seq. By not trying to parse declarators, we can
7276 resolve the declaration/expression ambiguity more quickly. */
7277 if (!function_definition_allowed_p
7278 && !decl_specifiers.any_specifiers_p)
7279 {
7280 cp_parser_error (parser, "expected declaration");
7281 goto done;
7282 }
7283
7284 /* If the next two tokens are both identifiers, the code is
7285 erroneous. The usual cause of this situation is code like:
7286
7287 T t;
7288
7289 where "T" should name a type -- but does not. */
7290 if (!decl_specifiers.type
7291 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7292 {
7293 /* If parsing tentatively, we should commit; we really are
7294 looking at a declaration. */
7295 cp_parser_commit_to_tentative_parse (parser);
7296 /* Give up. */
7297 goto done;
7298 }
7299
7300 /* If we have seen at least one decl-specifier, and the next token
7301 is not a parenthesis, then we must be looking at a declaration.
7302 (After "int (" we might be looking at a functional cast.) */
7303 if (decl_specifiers.any_specifiers_p
7304 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7305 cp_parser_commit_to_tentative_parse (parser);
7306
7307 /* Keep going until we hit the `;' at the end of the simple
7308 declaration. */
7309 saw_declarator = false;
7310 while (cp_lexer_next_token_is_not (parser->lexer,
7311 CPP_SEMICOLON))
7312 {
7313 cp_token *token;
7314 bool function_definition_p;
7315 tree decl;
7316
7317 if (saw_declarator)
7318 {
7319 /* If we are processing next declarator, coma is expected */
7320 token = cp_lexer_peek_token (parser->lexer);
7321 gcc_assert (token->type == CPP_COMMA);
7322 cp_lexer_consume_token (parser->lexer);
7323 }
7324 else
7325 saw_declarator = true;
7326
7327 /* Parse the init-declarator. */
7328 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7329 /*checks=*/NULL_TREE,
7330 function_definition_allowed_p,
7331 /*member_p=*/false,
7332 declares_class_or_enum,
7333 &function_definition_p);
7334 /* If an error occurred while parsing tentatively, exit quickly.
7335 (That usually happens when in the body of a function; each
7336 statement is treated as a declaration-statement until proven
7337 otherwise.) */
7338 if (cp_parser_error_occurred (parser))
7339 goto done;
7340 /* Handle function definitions specially. */
7341 if (function_definition_p)
7342 {
7343 /* If the next token is a `,', then we are probably
7344 processing something like:
7345
7346 void f() {}, *p;
7347
7348 which is erroneous. */
7349 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7350 error ("mixing declarations and function-definitions is forbidden");
7351 /* Otherwise, we're done with the list of declarators. */
7352 else
7353 {
7354 pop_deferring_access_checks ();
7355 return;
7356 }
7357 }
7358 /* The next token should be either a `,' or a `;'. */
7359 token = cp_lexer_peek_token (parser->lexer);
7360 /* If it's a `,', there are more declarators to come. */
7361 if (token->type == CPP_COMMA)
7362 /* will be consumed next time around */;
7363 /* If it's a `;', we are done. */
7364 else if (token->type == CPP_SEMICOLON)
7365 break;
7366 /* Anything else is an error. */
7367 else
7368 {
7369 /* If we have already issued an error message we don't need
7370 to issue another one. */
7371 if (decl != error_mark_node
7372 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7373 cp_parser_error (parser, "expected %<,%> or %<;%>");
7374 /* Skip tokens until we reach the end of the statement. */
7375 cp_parser_skip_to_end_of_statement (parser);
7376 /* If the next token is now a `;', consume it. */
7377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7378 cp_lexer_consume_token (parser->lexer);
7379 goto done;
7380 }
7381 /* After the first time around, a function-definition is not
7382 allowed -- even if it was OK at first. For example:
7383
7384 int i, f() {}
7385
7386 is not valid. */
7387 function_definition_allowed_p = false;
7388 }
7389
7390 /* Issue an error message if no declarators are present, and the
7391 decl-specifier-seq does not itself declare a class or
7392 enumeration. */
7393 if (!saw_declarator)
7394 {
7395 if (cp_parser_declares_only_class_p (parser))
7396 shadow_tag (&decl_specifiers);
7397 /* Perform any deferred access checks. */
7398 perform_deferred_access_checks ();
7399 }
7400
7401 /* Consume the `;'. */
7402 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7403
7404 done:
7405 pop_deferring_access_checks ();
7406 }
7407
7408 /* Parse a decl-specifier-seq.
7409
7410 decl-specifier-seq:
7411 decl-specifier-seq [opt] decl-specifier
7412
7413 decl-specifier:
7414 storage-class-specifier
7415 type-specifier
7416 function-specifier
7417 friend
7418 typedef
7419
7420 GNU Extension:
7421
7422 decl-specifier:
7423 attributes
7424
7425 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7426
7427 The parser flags FLAGS is used to control type-specifier parsing.
7428
7429 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7430 flags:
7431
7432 1: one of the decl-specifiers is an elaborated-type-specifier
7433 (i.e., a type declaration)
7434 2: one of the decl-specifiers is an enum-specifier or a
7435 class-specifier (i.e., a type definition)
7436
7437 */
7438
7439 static void
7440 cp_parser_decl_specifier_seq (cp_parser* parser,
7441 cp_parser_flags flags,
7442 cp_decl_specifier_seq *decl_specs,
7443 int* declares_class_or_enum)
7444 {
7445 bool constructor_possible_p = !parser->in_declarator_p;
7446
7447 /* Clear DECL_SPECS. */
7448 clear_decl_specs (decl_specs);
7449
7450 /* Assume no class or enumeration type is declared. */
7451 *declares_class_or_enum = 0;
7452
7453 /* Keep reading specifiers until there are no more to read. */
7454 while (true)
7455 {
7456 bool constructor_p;
7457 bool found_decl_spec;
7458 cp_token *token;
7459
7460 /* Peek at the next token. */
7461 token = cp_lexer_peek_token (parser->lexer);
7462 /* Handle attributes. */
7463 if (token->keyword == RID_ATTRIBUTE)
7464 {
7465 /* Parse the attributes. */
7466 decl_specs->attributes
7467 = chainon (decl_specs->attributes,
7468 cp_parser_attributes_opt (parser));
7469 continue;
7470 }
7471 /* Assume we will find a decl-specifier keyword. */
7472 found_decl_spec = true;
7473 /* If the next token is an appropriate keyword, we can simply
7474 add it to the list. */
7475 switch (token->keyword)
7476 {
7477 /* decl-specifier:
7478 friend */
7479 case RID_FRIEND:
7480 if (!at_class_scope_p ())
7481 {
7482 error ("%<friend%> used outside of class");
7483 cp_lexer_purge_token (parser->lexer);
7484 }
7485 else
7486 {
7487 ++decl_specs->specs[(int) ds_friend];
7488 /* Consume the token. */
7489 cp_lexer_consume_token (parser->lexer);
7490 }
7491 break;
7492
7493 /* function-specifier:
7494 inline
7495 virtual
7496 explicit */
7497 case RID_INLINE:
7498 case RID_VIRTUAL:
7499 case RID_EXPLICIT:
7500 cp_parser_function_specifier_opt (parser, decl_specs);
7501 break;
7502
7503 /* decl-specifier:
7504 typedef */
7505 case RID_TYPEDEF:
7506 ++decl_specs->specs[(int) ds_typedef];
7507 /* Consume the token. */
7508 cp_lexer_consume_token (parser->lexer);
7509 /* A constructor declarator cannot appear in a typedef. */
7510 constructor_possible_p = false;
7511 /* The "typedef" keyword can only occur in a declaration; we
7512 may as well commit at this point. */
7513 cp_parser_commit_to_tentative_parse (parser);
7514
7515 if (decl_specs->storage_class != sc_none)
7516 decl_specs->conflicting_specifiers_p = true;
7517 break;
7518
7519 /* storage-class-specifier:
7520 auto
7521 register
7522 static
7523 extern
7524 mutable
7525
7526 GNU Extension:
7527 thread */
7528 case RID_AUTO:
7529 case RID_REGISTER:
7530 case RID_STATIC:
7531 case RID_EXTERN:
7532 case RID_MUTABLE:
7533 /* Consume the token. */
7534 cp_lexer_consume_token (parser->lexer);
7535 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7536 break;
7537 case RID_THREAD:
7538 /* Consume the token. */
7539 cp_lexer_consume_token (parser->lexer);
7540 ++decl_specs->specs[(int) ds_thread];
7541 break;
7542
7543 default:
7544 /* We did not yet find a decl-specifier yet. */
7545 found_decl_spec = false;
7546 break;
7547 }
7548
7549 /* Constructors are a special case. The `S' in `S()' is not a
7550 decl-specifier; it is the beginning of the declarator. */
7551 constructor_p
7552 = (!found_decl_spec
7553 && constructor_possible_p
7554 && (cp_parser_constructor_declarator_p
7555 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7556
7557 /* If we don't have a DECL_SPEC yet, then we must be looking at
7558 a type-specifier. */
7559 if (!found_decl_spec && !constructor_p)
7560 {
7561 int decl_spec_declares_class_or_enum;
7562 bool is_cv_qualifier;
7563 tree type_spec;
7564
7565 type_spec
7566 = cp_parser_type_specifier (parser, flags,
7567 decl_specs,
7568 /*is_declaration=*/true,
7569 &decl_spec_declares_class_or_enum,
7570 &is_cv_qualifier);
7571
7572 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7573
7574 /* If this type-specifier referenced a user-defined type
7575 (a typedef, class-name, etc.), then we can't allow any
7576 more such type-specifiers henceforth.
7577
7578 [dcl.spec]
7579
7580 The longest sequence of decl-specifiers that could
7581 possibly be a type name is taken as the
7582 decl-specifier-seq of a declaration. The sequence shall
7583 be self-consistent as described below.
7584
7585 [dcl.type]
7586
7587 As a general rule, at most one type-specifier is allowed
7588 in the complete decl-specifier-seq of a declaration. The
7589 only exceptions are the following:
7590
7591 -- const or volatile can be combined with any other
7592 type-specifier.
7593
7594 -- signed or unsigned can be combined with char, long,
7595 short, or int.
7596
7597 -- ..
7598
7599 Example:
7600
7601 typedef char* Pc;
7602 void g (const int Pc);
7603
7604 Here, Pc is *not* part of the decl-specifier seq; it's
7605 the declarator. Therefore, once we see a type-specifier
7606 (other than a cv-qualifier), we forbid any additional
7607 user-defined types. We *do* still allow things like `int
7608 int' to be considered a decl-specifier-seq, and issue the
7609 error message later. */
7610 if (type_spec && !is_cv_qualifier)
7611 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7612 /* A constructor declarator cannot follow a type-specifier. */
7613 if (type_spec)
7614 {
7615 constructor_possible_p = false;
7616 found_decl_spec = true;
7617 }
7618 }
7619
7620 /* If we still do not have a DECL_SPEC, then there are no more
7621 decl-specifiers. */
7622 if (!found_decl_spec)
7623 break;
7624
7625 decl_specs->any_specifiers_p = true;
7626 /* After we see one decl-specifier, further decl-specifiers are
7627 always optional. */
7628 flags |= CP_PARSER_FLAGS_OPTIONAL;
7629 }
7630
7631 cp_parser_check_decl_spec (decl_specs);
7632
7633 /* Don't allow a friend specifier with a class definition. */
7634 if (decl_specs->specs[(int) ds_friend] != 0
7635 && (*declares_class_or_enum & 2))
7636 error ("class definition may not be declared a friend");
7637 }
7638
7639 /* Parse an (optional) storage-class-specifier.
7640
7641 storage-class-specifier:
7642 auto
7643 register
7644 static
7645 extern
7646 mutable
7647
7648 GNU Extension:
7649
7650 storage-class-specifier:
7651 thread
7652
7653 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7654
7655 static tree
7656 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7657 {
7658 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7659 {
7660 case RID_AUTO:
7661 case RID_REGISTER:
7662 case RID_STATIC:
7663 case RID_EXTERN:
7664 case RID_MUTABLE:
7665 case RID_THREAD:
7666 /* Consume the token. */
7667 return cp_lexer_consume_token (parser->lexer)->value;
7668
7669 default:
7670 return NULL_TREE;
7671 }
7672 }
7673
7674 /* Parse an (optional) function-specifier.
7675
7676 function-specifier:
7677 inline
7678 virtual
7679 explicit
7680
7681 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7682 Updates DECL_SPECS, if it is non-NULL. */
7683
7684 static tree
7685 cp_parser_function_specifier_opt (cp_parser* parser,
7686 cp_decl_specifier_seq *decl_specs)
7687 {
7688 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7689 {
7690 case RID_INLINE:
7691 if (decl_specs)
7692 ++decl_specs->specs[(int) ds_inline];
7693 break;
7694
7695 case RID_VIRTUAL:
7696 /* 14.5.2.3 [temp.mem]
7697
7698 A member function template shall not be virtual. */
7699 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7700 error ("templates may not be %<virtual%>");
7701 else if (decl_specs)
7702 ++decl_specs->specs[(int) ds_virtual];
7703 break;
7704
7705 case RID_EXPLICIT:
7706 if (decl_specs)
7707 ++decl_specs->specs[(int) ds_explicit];
7708 break;
7709
7710 default:
7711 return NULL_TREE;
7712 }
7713
7714 /* Consume the token. */
7715 return cp_lexer_consume_token (parser->lexer)->value;
7716 }
7717
7718 /* Parse a linkage-specification.
7719
7720 linkage-specification:
7721 extern string-literal { declaration-seq [opt] }
7722 extern string-literal declaration */
7723
7724 static void
7725 cp_parser_linkage_specification (cp_parser* parser)
7726 {
7727 tree linkage;
7728
7729 /* Look for the `extern' keyword. */
7730 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7731
7732 /* Look for the string-literal. */
7733 linkage = cp_parser_string_literal (parser, false, false);
7734
7735 /* Transform the literal into an identifier. If the literal is a
7736 wide-character string, or contains embedded NULs, then we can't
7737 handle it as the user wants. */
7738 if (strlen (TREE_STRING_POINTER (linkage))
7739 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7740 {
7741 cp_parser_error (parser, "invalid linkage-specification");
7742 /* Assume C++ linkage. */
7743 linkage = lang_name_cplusplus;
7744 }
7745 else
7746 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7747
7748 /* We're now using the new linkage. */
7749 push_lang_context (linkage);
7750
7751 /* If the next token is a `{', then we're using the first
7752 production. */
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7754 {
7755 /* Consume the `{' token. */
7756 cp_lexer_consume_token (parser->lexer);
7757 /* Parse the declarations. */
7758 cp_parser_declaration_seq_opt (parser);
7759 /* Look for the closing `}'. */
7760 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7761 }
7762 /* Otherwise, there's just one declaration. */
7763 else
7764 {
7765 bool saved_in_unbraced_linkage_specification_p;
7766
7767 saved_in_unbraced_linkage_specification_p
7768 = parser->in_unbraced_linkage_specification_p;
7769 parser->in_unbraced_linkage_specification_p = true;
7770 cp_parser_declaration (parser);
7771 parser->in_unbraced_linkage_specification_p
7772 = saved_in_unbraced_linkage_specification_p;
7773 }
7774
7775 /* We're done with the linkage-specification. */
7776 pop_lang_context ();
7777 }
7778
7779 /* Special member functions [gram.special] */
7780
7781 /* Parse a conversion-function-id.
7782
7783 conversion-function-id:
7784 operator conversion-type-id
7785
7786 Returns an IDENTIFIER_NODE representing the operator. */
7787
7788 static tree
7789 cp_parser_conversion_function_id (cp_parser* parser)
7790 {
7791 tree type;
7792 tree saved_scope;
7793 tree saved_qualifying_scope;
7794 tree saved_object_scope;
7795 tree pushed_scope = NULL_TREE;
7796
7797 /* Look for the `operator' token. */
7798 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7799 return error_mark_node;
7800 /* When we parse the conversion-type-id, the current scope will be
7801 reset. However, we need that information in able to look up the
7802 conversion function later, so we save it here. */
7803 saved_scope = parser->scope;
7804 saved_qualifying_scope = parser->qualifying_scope;
7805 saved_object_scope = parser->object_scope;
7806 /* We must enter the scope of the class so that the names of
7807 entities declared within the class are available in the
7808 conversion-type-id. For example, consider:
7809
7810 struct S {
7811 typedef int I;
7812 operator I();
7813 };
7814
7815 S::operator I() { ... }
7816
7817 In order to see that `I' is a type-name in the definition, we
7818 must be in the scope of `S'. */
7819 if (saved_scope)
7820 pushed_scope = push_scope (saved_scope);
7821 /* Parse the conversion-type-id. */
7822 type = cp_parser_conversion_type_id (parser);
7823 /* Leave the scope of the class, if any. */
7824 if (pushed_scope)
7825 pop_scope (pushed_scope);
7826 /* Restore the saved scope. */
7827 parser->scope = saved_scope;
7828 parser->qualifying_scope = saved_qualifying_scope;
7829 parser->object_scope = saved_object_scope;
7830 /* If the TYPE is invalid, indicate failure. */
7831 if (type == error_mark_node)
7832 return error_mark_node;
7833 return mangle_conv_op_name_for_type (type);
7834 }
7835
7836 /* Parse a conversion-type-id:
7837
7838 conversion-type-id:
7839 type-specifier-seq conversion-declarator [opt]
7840
7841 Returns the TYPE specified. */
7842
7843 static tree
7844 cp_parser_conversion_type_id (cp_parser* parser)
7845 {
7846 tree attributes;
7847 cp_decl_specifier_seq type_specifiers;
7848 cp_declarator *declarator;
7849 tree type_specified;
7850
7851 /* Parse the attributes. */
7852 attributes = cp_parser_attributes_opt (parser);
7853 /* Parse the type-specifiers. */
7854 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7855 &type_specifiers);
7856 /* If that didn't work, stop. */
7857 if (type_specifiers.type == error_mark_node)
7858 return error_mark_node;
7859 /* Parse the conversion-declarator. */
7860 declarator = cp_parser_conversion_declarator_opt (parser);
7861
7862 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7863 /*initialized=*/0, &attributes);
7864 if (attributes)
7865 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7866 return type_specified;
7867 }
7868
7869 /* Parse an (optional) conversion-declarator.
7870
7871 conversion-declarator:
7872 ptr-operator conversion-declarator [opt]
7873
7874 */
7875
7876 static cp_declarator *
7877 cp_parser_conversion_declarator_opt (cp_parser* parser)
7878 {
7879 enum tree_code code;
7880 tree class_type;
7881 cp_cv_quals cv_quals;
7882
7883 /* We don't know if there's a ptr-operator next, or not. */
7884 cp_parser_parse_tentatively (parser);
7885 /* Try the ptr-operator. */
7886 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7887 /* If it worked, look for more conversion-declarators. */
7888 if (cp_parser_parse_definitely (parser))
7889 {
7890 cp_declarator *declarator;
7891
7892 /* Parse another optional declarator. */
7893 declarator = cp_parser_conversion_declarator_opt (parser);
7894
7895 /* Create the representation of the declarator. */
7896 if (class_type)
7897 declarator = make_ptrmem_declarator (cv_quals, class_type,
7898 declarator);
7899 else if (code == INDIRECT_REF)
7900 declarator = make_pointer_declarator (cv_quals, declarator);
7901 else
7902 declarator = make_reference_declarator (cv_quals, declarator);
7903
7904 return declarator;
7905 }
7906
7907 return NULL;
7908 }
7909
7910 /* Parse an (optional) ctor-initializer.
7911
7912 ctor-initializer:
7913 : mem-initializer-list
7914
7915 Returns TRUE iff the ctor-initializer was actually present. */
7916
7917 static bool
7918 cp_parser_ctor_initializer_opt (cp_parser* parser)
7919 {
7920 /* If the next token is not a `:', then there is no
7921 ctor-initializer. */
7922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7923 {
7924 /* Do default initialization of any bases and members. */
7925 if (DECL_CONSTRUCTOR_P (current_function_decl))
7926 finish_mem_initializers (NULL_TREE);
7927
7928 return false;
7929 }
7930
7931 /* Consume the `:' token. */
7932 cp_lexer_consume_token (parser->lexer);
7933 /* And the mem-initializer-list. */
7934 cp_parser_mem_initializer_list (parser);
7935
7936 return true;
7937 }
7938
7939 /* Parse a mem-initializer-list.
7940
7941 mem-initializer-list:
7942 mem-initializer
7943 mem-initializer , mem-initializer-list */
7944
7945 static void
7946 cp_parser_mem_initializer_list (cp_parser* parser)
7947 {
7948 tree mem_initializer_list = NULL_TREE;
7949
7950 /* Let the semantic analysis code know that we are starting the
7951 mem-initializer-list. */
7952 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7953 error ("only constructors take base initializers");
7954
7955 /* Loop through the list. */
7956 while (true)
7957 {
7958 tree mem_initializer;
7959
7960 /* Parse the mem-initializer. */
7961 mem_initializer = cp_parser_mem_initializer (parser);
7962 /* Add it to the list, unless it was erroneous. */
7963 if (mem_initializer != error_mark_node)
7964 {
7965 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7966 mem_initializer_list = mem_initializer;
7967 }
7968 /* If the next token is not a `,', we're done. */
7969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7970 break;
7971 /* Consume the `,' token. */
7972 cp_lexer_consume_token (parser->lexer);
7973 }
7974
7975 /* Perform semantic analysis. */
7976 if (DECL_CONSTRUCTOR_P (current_function_decl))
7977 finish_mem_initializers (mem_initializer_list);
7978 }
7979
7980 /* Parse a mem-initializer.
7981
7982 mem-initializer:
7983 mem-initializer-id ( expression-list [opt] )
7984
7985 GNU extension:
7986
7987 mem-initializer:
7988 ( expression-list [opt] )
7989
7990 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7991 class) or FIELD_DECL (for a non-static data member) to initialize;
7992 the TREE_VALUE is the expression-list. An empty initialization
7993 list is represented by void_list_node. */
7994
7995 static tree
7996 cp_parser_mem_initializer (cp_parser* parser)
7997 {
7998 tree mem_initializer_id;
7999 tree expression_list;
8000 tree member;
8001
8002 /* Find out what is being initialized. */
8003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8004 {
8005 pedwarn ("anachronistic old-style base class initializer");
8006 mem_initializer_id = NULL_TREE;
8007 }
8008 else
8009 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8010 member = expand_member_init (mem_initializer_id);
8011 if (member && !DECL_P (member))
8012 in_base_initializer = 1;
8013
8014 expression_list
8015 = cp_parser_parenthesized_expression_list (parser, false,
8016 /*cast_p=*/false,
8017 /*non_constant_p=*/NULL);
8018 if (expression_list == error_mark_node)
8019 return error_mark_node;
8020 if (!expression_list)
8021 expression_list = void_type_node;
8022
8023 in_base_initializer = 0;
8024
8025 return member ? build_tree_list (member, expression_list) : error_mark_node;
8026 }
8027
8028 /* Parse a mem-initializer-id.
8029
8030 mem-initializer-id:
8031 :: [opt] nested-name-specifier [opt] class-name
8032 identifier
8033
8034 Returns a TYPE indicating the class to be initializer for the first
8035 production. Returns an IDENTIFIER_NODE indicating the data member
8036 to be initialized for the second production. */
8037
8038 static tree
8039 cp_parser_mem_initializer_id (cp_parser* parser)
8040 {
8041 bool global_scope_p;
8042 bool nested_name_specifier_p;
8043 bool template_p = false;
8044 tree id;
8045
8046 /* `typename' is not allowed in this context ([temp.res]). */
8047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8048 {
8049 error ("keyword %<typename%> not allowed in this context (a qualified "
8050 "member initializer is implicitly a type)");
8051 cp_lexer_consume_token (parser->lexer);
8052 }
8053 /* Look for the optional `::' operator. */
8054 global_scope_p
8055 = (cp_parser_global_scope_opt (parser,
8056 /*current_scope_valid_p=*/false)
8057 != NULL_TREE);
8058 /* Look for the optional nested-name-specifier. The simplest way to
8059 implement:
8060
8061 [temp.res]
8062
8063 The keyword `typename' is not permitted in a base-specifier or
8064 mem-initializer; in these contexts a qualified name that
8065 depends on a template-parameter is implicitly assumed to be a
8066 type name.
8067
8068 is to assume that we have seen the `typename' keyword at this
8069 point. */
8070 nested_name_specifier_p
8071 = (cp_parser_nested_name_specifier_opt (parser,
8072 /*typename_keyword_p=*/true,
8073 /*check_dependency_p=*/true,
8074 /*type_p=*/true,
8075 /*is_declaration=*/true)
8076 != NULL_TREE);
8077 if (nested_name_specifier_p)
8078 template_p = cp_parser_optional_template_keyword (parser);
8079 /* If there is a `::' operator or a nested-name-specifier, then we
8080 are definitely looking for a class-name. */
8081 if (global_scope_p || nested_name_specifier_p)
8082 return cp_parser_class_name (parser,
8083 /*typename_keyword_p=*/true,
8084 /*template_keyword_p=*/template_p,
8085 none_type,
8086 /*check_dependency_p=*/true,
8087 /*class_head_p=*/false,
8088 /*is_declaration=*/true);
8089 /* Otherwise, we could also be looking for an ordinary identifier. */
8090 cp_parser_parse_tentatively (parser);
8091 /* Try a class-name. */
8092 id = cp_parser_class_name (parser,
8093 /*typename_keyword_p=*/true,
8094 /*template_keyword_p=*/false,
8095 none_type,
8096 /*check_dependency_p=*/true,
8097 /*class_head_p=*/false,
8098 /*is_declaration=*/true);
8099 /* If we found one, we're done. */
8100 if (cp_parser_parse_definitely (parser))
8101 return id;
8102 /* Otherwise, look for an ordinary identifier. */
8103 return cp_parser_identifier (parser);
8104 }
8105
8106 /* Overloading [gram.over] */
8107
8108 /* Parse an operator-function-id.
8109
8110 operator-function-id:
8111 operator operator
8112
8113 Returns an IDENTIFIER_NODE for the operator which is a
8114 human-readable spelling of the identifier, e.g., `operator +'. */
8115
8116 static tree
8117 cp_parser_operator_function_id (cp_parser* parser)
8118 {
8119 /* Look for the `operator' keyword. */
8120 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8121 return error_mark_node;
8122 /* And then the name of the operator itself. */
8123 return cp_parser_operator (parser);
8124 }
8125
8126 /* Parse an operator.
8127
8128 operator:
8129 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8130 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8131 || ++ -- , ->* -> () []
8132
8133 GNU Extensions:
8134
8135 operator:
8136 <? >? <?= >?=
8137
8138 Returns an IDENTIFIER_NODE for the operator which is a
8139 human-readable spelling of the identifier, e.g., `operator +'. */
8140
8141 static tree
8142 cp_parser_operator (cp_parser* parser)
8143 {
8144 tree id = NULL_TREE;
8145 cp_token *token;
8146
8147 /* Peek at the next token. */
8148 token = cp_lexer_peek_token (parser->lexer);
8149 /* Figure out which operator we have. */
8150 switch (token->type)
8151 {
8152 case CPP_KEYWORD:
8153 {
8154 enum tree_code op;
8155
8156 /* The keyword should be either `new' or `delete'. */
8157 if (token->keyword == RID_NEW)
8158 op = NEW_EXPR;
8159 else if (token->keyword == RID_DELETE)
8160 op = DELETE_EXPR;
8161 else
8162 break;
8163
8164 /* Consume the `new' or `delete' token. */
8165 cp_lexer_consume_token (parser->lexer);
8166
8167 /* Peek at the next token. */
8168 token = cp_lexer_peek_token (parser->lexer);
8169 /* If it's a `[' token then this is the array variant of the
8170 operator. */
8171 if (token->type == CPP_OPEN_SQUARE)
8172 {
8173 /* Consume the `[' token. */
8174 cp_lexer_consume_token (parser->lexer);
8175 /* Look for the `]' token. */
8176 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8177 id = ansi_opname (op == NEW_EXPR
8178 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8179 }
8180 /* Otherwise, we have the non-array variant. */
8181 else
8182 id = ansi_opname (op);
8183
8184 return id;
8185 }
8186
8187 case CPP_PLUS:
8188 id = ansi_opname (PLUS_EXPR);
8189 break;
8190
8191 case CPP_MINUS:
8192 id = ansi_opname (MINUS_EXPR);
8193 break;
8194
8195 case CPP_MULT:
8196 id = ansi_opname (MULT_EXPR);
8197 break;
8198
8199 case CPP_DIV:
8200 id = ansi_opname (TRUNC_DIV_EXPR);
8201 break;
8202
8203 case CPP_MOD:
8204 id = ansi_opname (TRUNC_MOD_EXPR);
8205 break;
8206
8207 case CPP_XOR:
8208 id = ansi_opname (BIT_XOR_EXPR);
8209 break;
8210
8211 case CPP_AND:
8212 id = ansi_opname (BIT_AND_EXPR);
8213 break;
8214
8215 case CPP_OR:
8216 id = ansi_opname (BIT_IOR_EXPR);
8217 break;
8218
8219 case CPP_COMPL:
8220 id = ansi_opname (BIT_NOT_EXPR);
8221 break;
8222
8223 case CPP_NOT:
8224 id = ansi_opname (TRUTH_NOT_EXPR);
8225 break;
8226
8227 case CPP_EQ:
8228 id = ansi_assopname (NOP_EXPR);
8229 break;
8230
8231 case CPP_LESS:
8232 id = ansi_opname (LT_EXPR);
8233 break;
8234
8235 case CPP_GREATER:
8236 id = ansi_opname (GT_EXPR);
8237 break;
8238
8239 case CPP_PLUS_EQ:
8240 id = ansi_assopname (PLUS_EXPR);
8241 break;
8242
8243 case CPP_MINUS_EQ:
8244 id = ansi_assopname (MINUS_EXPR);
8245 break;
8246
8247 case CPP_MULT_EQ:
8248 id = ansi_assopname (MULT_EXPR);
8249 break;
8250
8251 case CPP_DIV_EQ:
8252 id = ansi_assopname (TRUNC_DIV_EXPR);
8253 break;
8254
8255 case CPP_MOD_EQ:
8256 id = ansi_assopname (TRUNC_MOD_EXPR);
8257 break;
8258
8259 case CPP_XOR_EQ:
8260 id = ansi_assopname (BIT_XOR_EXPR);
8261 break;
8262
8263 case CPP_AND_EQ:
8264 id = ansi_assopname (BIT_AND_EXPR);
8265 break;
8266
8267 case CPP_OR_EQ:
8268 id = ansi_assopname (BIT_IOR_EXPR);
8269 break;
8270
8271 case CPP_LSHIFT:
8272 id = ansi_opname (LSHIFT_EXPR);
8273 break;
8274
8275 case CPP_RSHIFT:
8276 id = ansi_opname (RSHIFT_EXPR);
8277 break;
8278
8279 case CPP_LSHIFT_EQ:
8280 id = ansi_assopname (LSHIFT_EXPR);
8281 break;
8282
8283 case CPP_RSHIFT_EQ:
8284 id = ansi_assopname (RSHIFT_EXPR);
8285 break;
8286
8287 case CPP_EQ_EQ:
8288 id = ansi_opname (EQ_EXPR);
8289 break;
8290
8291 case CPP_NOT_EQ:
8292 id = ansi_opname (NE_EXPR);
8293 break;
8294
8295 case CPP_LESS_EQ:
8296 id = ansi_opname (LE_EXPR);
8297 break;
8298
8299 case CPP_GREATER_EQ:
8300 id = ansi_opname (GE_EXPR);
8301 break;
8302
8303 case CPP_AND_AND:
8304 id = ansi_opname (TRUTH_ANDIF_EXPR);
8305 break;
8306
8307 case CPP_OR_OR:
8308 id = ansi_opname (TRUTH_ORIF_EXPR);
8309 break;
8310
8311 case CPP_PLUS_PLUS:
8312 id = ansi_opname (POSTINCREMENT_EXPR);
8313 break;
8314
8315 case CPP_MINUS_MINUS:
8316 id = ansi_opname (PREDECREMENT_EXPR);
8317 break;
8318
8319 case CPP_COMMA:
8320 id = ansi_opname (COMPOUND_EXPR);
8321 break;
8322
8323 case CPP_DEREF_STAR:
8324 id = ansi_opname (MEMBER_REF);
8325 break;
8326
8327 case CPP_DEREF:
8328 id = ansi_opname (COMPONENT_REF);
8329 break;
8330
8331 case CPP_OPEN_PAREN:
8332 /* Consume the `('. */
8333 cp_lexer_consume_token (parser->lexer);
8334 /* Look for the matching `)'. */
8335 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8336 return ansi_opname (CALL_EXPR);
8337
8338 case CPP_OPEN_SQUARE:
8339 /* Consume the `['. */
8340 cp_lexer_consume_token (parser->lexer);
8341 /* Look for the matching `]'. */
8342 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8343 return ansi_opname (ARRAY_REF);
8344
8345 default:
8346 /* Anything else is an error. */
8347 break;
8348 }
8349
8350 /* If we have selected an identifier, we need to consume the
8351 operator token. */
8352 if (id)
8353 cp_lexer_consume_token (parser->lexer);
8354 /* Otherwise, no valid operator name was present. */
8355 else
8356 {
8357 cp_parser_error (parser, "expected operator");
8358 id = error_mark_node;
8359 }
8360
8361 return id;
8362 }
8363
8364 /* Parse a template-declaration.
8365
8366 template-declaration:
8367 export [opt] template < template-parameter-list > declaration
8368
8369 If MEMBER_P is TRUE, this template-declaration occurs within a
8370 class-specifier.
8371
8372 The grammar rule given by the standard isn't correct. What
8373 is really meant is:
8374
8375 template-declaration:
8376 export [opt] template-parameter-list-seq
8377 decl-specifier-seq [opt] init-declarator [opt] ;
8378 export [opt] template-parameter-list-seq
8379 function-definition
8380
8381 template-parameter-list-seq:
8382 template-parameter-list-seq [opt]
8383 template < template-parameter-list > */
8384
8385 static void
8386 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8387 {
8388 /* Check for `export'. */
8389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8390 {
8391 /* Consume the `export' token. */
8392 cp_lexer_consume_token (parser->lexer);
8393 /* Warn that we do not support `export'. */
8394 warning (0, "keyword %<export%> not implemented, and will be ignored");
8395 }
8396
8397 cp_parser_template_declaration_after_export (parser, member_p);
8398 }
8399
8400 /* Parse a template-parameter-list.
8401
8402 template-parameter-list:
8403 template-parameter
8404 template-parameter-list , template-parameter
8405
8406 Returns a TREE_LIST. Each node represents a template parameter.
8407 The nodes are connected via their TREE_CHAINs. */
8408
8409 static tree
8410 cp_parser_template_parameter_list (cp_parser* parser)
8411 {
8412 tree parameter_list = NULL_TREE;
8413
8414 begin_template_parm_list ();
8415 while (true)
8416 {
8417 tree parameter;
8418 cp_token *token;
8419 bool is_non_type;
8420
8421 /* Parse the template-parameter. */
8422 parameter = cp_parser_template_parameter (parser, &is_non_type);
8423 /* Add it to the list. */
8424 if (parameter != error_mark_node)
8425 parameter_list = process_template_parm (parameter_list,
8426 parameter,
8427 is_non_type);
8428 else
8429 {
8430 tree err_parm = build_tree_list (parameter, parameter);
8431 TREE_VALUE (err_parm) = error_mark_node;
8432 parameter_list = chainon (parameter_list, err_parm);
8433 }
8434
8435 /* Peek at the next token. */
8436 token = cp_lexer_peek_token (parser->lexer);
8437 /* If it's not a `,', we're done. */
8438 if (token->type != CPP_COMMA)
8439 break;
8440 /* Otherwise, consume the `,' token. */
8441 cp_lexer_consume_token (parser->lexer);
8442 }
8443
8444 return end_template_parm_list (parameter_list);
8445 }
8446
8447 /* Parse a template-parameter.
8448
8449 template-parameter:
8450 type-parameter
8451 parameter-declaration
8452
8453 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8454 the parameter. The TREE_PURPOSE is the default value, if any.
8455 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8456 iff this parameter is a non-type parameter. */
8457
8458 static tree
8459 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8460 {
8461 cp_token *token;
8462 cp_parameter_declarator *parameter_declarator;
8463 tree parm;
8464
8465 /* Assume it is a type parameter or a template parameter. */
8466 *is_non_type = false;
8467 /* Peek at the next token. */
8468 token = cp_lexer_peek_token (parser->lexer);
8469 /* If it is `class' or `template', we have a type-parameter. */
8470 if (token->keyword == RID_TEMPLATE)
8471 return cp_parser_type_parameter (parser);
8472 /* If it is `class' or `typename' we do not know yet whether it is a
8473 type parameter or a non-type parameter. Consider:
8474
8475 template <typename T, typename T::X X> ...
8476
8477 or:
8478
8479 template <class C, class D*> ...
8480
8481 Here, the first parameter is a type parameter, and the second is
8482 a non-type parameter. We can tell by looking at the token after
8483 the identifier -- if it is a `,', `=', or `>' then we have a type
8484 parameter. */
8485 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8486 {
8487 /* Peek at the token after `class' or `typename'. */
8488 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8489 /* If it's an identifier, skip it. */
8490 if (token->type == CPP_NAME)
8491 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8492 /* Now, see if the token looks like the end of a template
8493 parameter. */
8494 if (token->type == CPP_COMMA
8495 || token->type == CPP_EQ
8496 || token->type == CPP_GREATER)
8497 return cp_parser_type_parameter (parser);
8498 }
8499
8500 /* Otherwise, it is a non-type parameter.
8501
8502 [temp.param]
8503
8504 When parsing a default template-argument for a non-type
8505 template-parameter, the first non-nested `>' is taken as the end
8506 of the template parameter-list rather than a greater-than
8507 operator. */
8508 *is_non_type = true;
8509 parameter_declarator
8510 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8511 /*parenthesized_p=*/NULL);
8512 parm = grokdeclarator (parameter_declarator->declarator,
8513 &parameter_declarator->decl_specifiers,
8514 PARM, /*initialized=*/0,
8515 /*attrlist=*/NULL);
8516 if (parm == error_mark_node)
8517 return error_mark_node;
8518 return build_tree_list (parameter_declarator->default_argument, parm);
8519 }
8520
8521 /* Parse a type-parameter.
8522
8523 type-parameter:
8524 class identifier [opt]
8525 class identifier [opt] = type-id
8526 typename identifier [opt]
8527 typename identifier [opt] = type-id
8528 template < template-parameter-list > class identifier [opt]
8529 template < template-parameter-list > class identifier [opt]
8530 = id-expression
8531
8532 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8533 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8534 the declaration of the parameter. */
8535
8536 static tree
8537 cp_parser_type_parameter (cp_parser* parser)
8538 {
8539 cp_token *token;
8540 tree parameter;
8541
8542 /* Look for a keyword to tell us what kind of parameter this is. */
8543 token = cp_parser_require (parser, CPP_KEYWORD,
8544 "`class', `typename', or `template'");
8545 if (!token)
8546 return error_mark_node;
8547
8548 switch (token->keyword)
8549 {
8550 case RID_CLASS:
8551 case RID_TYPENAME:
8552 {
8553 tree identifier;
8554 tree default_argument;
8555
8556 /* If the next token is an identifier, then it names the
8557 parameter. */
8558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8559 identifier = cp_parser_identifier (parser);
8560 else
8561 identifier = NULL_TREE;
8562
8563 /* Create the parameter. */
8564 parameter = finish_template_type_parm (class_type_node, identifier);
8565
8566 /* If the next token is an `=', we have a default argument. */
8567 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8568 {
8569 /* Consume the `=' token. */
8570 cp_lexer_consume_token (parser->lexer);
8571 /* Parse the default-argument. */
8572 push_deferring_access_checks (dk_no_deferred);
8573 default_argument = cp_parser_type_id (parser);
8574 pop_deferring_access_checks ();
8575 }
8576 else
8577 default_argument = NULL_TREE;
8578
8579 /* Create the combined representation of the parameter and the
8580 default argument. */
8581 parameter = build_tree_list (default_argument, parameter);
8582 }
8583 break;
8584
8585 case RID_TEMPLATE:
8586 {
8587 tree parameter_list;
8588 tree identifier;
8589 tree default_argument;
8590
8591 /* Look for the `<'. */
8592 cp_parser_require (parser, CPP_LESS, "`<'");
8593 /* Parse the template-parameter-list. */
8594 parameter_list = cp_parser_template_parameter_list (parser);
8595 /* Look for the `>'. */
8596 cp_parser_require (parser, CPP_GREATER, "`>'");
8597 /* Look for the `class' keyword. */
8598 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8599 /* If the next token is an `=', then there is a
8600 default-argument. If the next token is a `>', we are at
8601 the end of the parameter-list. If the next token is a `,',
8602 then we are at the end of this parameter. */
8603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8604 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8605 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8606 {
8607 identifier = cp_parser_identifier (parser);
8608 /* Treat invalid names as if the parameter were nameless. */
8609 if (identifier == error_mark_node)
8610 identifier = NULL_TREE;
8611 }
8612 else
8613 identifier = NULL_TREE;
8614
8615 /* Create the template parameter. */
8616 parameter = finish_template_template_parm (class_type_node,
8617 identifier);
8618
8619 /* If the next token is an `=', then there is a
8620 default-argument. */
8621 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8622 {
8623 bool is_template;
8624
8625 /* Consume the `='. */
8626 cp_lexer_consume_token (parser->lexer);
8627 /* Parse the id-expression. */
8628 push_deferring_access_checks (dk_no_deferred);
8629 default_argument
8630 = cp_parser_id_expression (parser,
8631 /*template_keyword_p=*/false,
8632 /*check_dependency_p=*/true,
8633 /*template_p=*/&is_template,
8634 /*declarator_p=*/false,
8635 /*optional_p=*/false);
8636 if (TREE_CODE (default_argument) == TYPE_DECL)
8637 /* If the id-expression was a template-id that refers to
8638 a template-class, we already have the declaration here,
8639 so no further lookup is needed. */
8640 ;
8641 else
8642 /* Look up the name. */
8643 default_argument
8644 = cp_parser_lookup_name (parser, default_argument,
8645 none_type,
8646 /*is_template=*/is_template,
8647 /*is_namespace=*/false,
8648 /*check_dependency=*/true,
8649 /*ambiguous_decls=*/NULL);
8650 /* See if the default argument is valid. */
8651 default_argument
8652 = check_template_template_default_arg (default_argument);
8653 pop_deferring_access_checks ();
8654 }
8655 else
8656 default_argument = NULL_TREE;
8657
8658 /* Create the combined representation of the parameter and the
8659 default argument. */
8660 parameter = build_tree_list (default_argument, parameter);
8661 }
8662 break;
8663
8664 default:
8665 gcc_unreachable ();
8666 break;
8667 }
8668
8669 return parameter;
8670 }
8671
8672 /* Parse a template-id.
8673
8674 template-id:
8675 template-name < template-argument-list [opt] >
8676
8677 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8678 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8679 returned. Otherwise, if the template-name names a function, or set
8680 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8681 names a class, returns a TYPE_DECL for the specialization.
8682
8683 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8684 uninstantiated templates. */
8685
8686 static tree
8687 cp_parser_template_id (cp_parser *parser,
8688 bool template_keyword_p,
8689 bool check_dependency_p,
8690 bool is_declaration)
8691 {
8692 tree template;
8693 tree arguments;
8694 tree template_id;
8695 cp_token_position start_of_id = 0;
8696 tree access_check = NULL_TREE;
8697 cp_token *next_token, *next_token_2;
8698 bool is_identifier;
8699
8700 /* If the next token corresponds to a template-id, there is no need
8701 to reparse it. */
8702 next_token = cp_lexer_peek_token (parser->lexer);
8703 if (next_token->type == CPP_TEMPLATE_ID)
8704 {
8705 tree value;
8706 tree check;
8707
8708 /* Get the stored value. */
8709 value = cp_lexer_consume_token (parser->lexer)->value;
8710 /* Perform any access checks that were deferred. */
8711 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8712 perform_or_defer_access_check (TREE_PURPOSE (check),
8713 TREE_VALUE (check));
8714 /* Return the stored value. */
8715 return TREE_VALUE (value);
8716 }
8717
8718 /* Avoid performing name lookup if there is no possibility of
8719 finding a template-id. */
8720 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8721 || (next_token->type == CPP_NAME
8722 && !cp_parser_nth_token_starts_template_argument_list_p
8723 (parser, 2)))
8724 {
8725 cp_parser_error (parser, "expected template-id");
8726 return error_mark_node;
8727 }
8728
8729 /* Remember where the template-id starts. */
8730 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8731 start_of_id = cp_lexer_token_position (parser->lexer, false);
8732
8733 push_deferring_access_checks (dk_deferred);
8734
8735 /* Parse the template-name. */
8736 is_identifier = false;
8737 template = cp_parser_template_name (parser, template_keyword_p,
8738 check_dependency_p,
8739 is_declaration,
8740 &is_identifier);
8741 if (template == error_mark_node || is_identifier)
8742 {
8743 pop_deferring_access_checks ();
8744 return template;
8745 }
8746
8747 /* If we find the sequence `[:' after a template-name, it's probably
8748 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8749 parse correctly the argument list. */
8750 next_token = cp_lexer_peek_token (parser->lexer);
8751 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8752 if (next_token->type == CPP_OPEN_SQUARE
8753 && next_token->flags & DIGRAPH
8754 && next_token_2->type == CPP_COLON
8755 && !(next_token_2->flags & PREV_WHITE))
8756 {
8757 cp_parser_parse_tentatively (parser);
8758 /* Change `:' into `::'. */
8759 next_token_2->type = CPP_SCOPE;
8760 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8761 CPP_LESS. */
8762 cp_lexer_consume_token (parser->lexer);
8763 /* Parse the arguments. */
8764 arguments = cp_parser_enclosed_template_argument_list (parser);
8765 if (!cp_parser_parse_definitely (parser))
8766 {
8767 /* If we couldn't parse an argument list, then we revert our changes
8768 and return simply an error. Maybe this is not a template-id
8769 after all. */
8770 next_token_2->type = CPP_COLON;
8771 cp_parser_error (parser, "expected %<<%>");
8772 pop_deferring_access_checks ();
8773 return error_mark_node;
8774 }
8775 /* Otherwise, emit an error about the invalid digraph, but continue
8776 parsing because we got our argument list. */
8777 pedwarn ("%<<::%> cannot begin a template-argument list");
8778 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8779 "between %<<%> and %<::%>");
8780 if (!flag_permissive)
8781 {
8782 static bool hint;
8783 if (!hint)
8784 {
8785 inform ("(if you use -fpermissive G++ will accept your code)");
8786 hint = true;
8787 }
8788 }
8789 }
8790 else
8791 {
8792 /* Look for the `<' that starts the template-argument-list. */
8793 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8794 {
8795 pop_deferring_access_checks ();
8796 return error_mark_node;
8797 }
8798 /* Parse the arguments. */
8799 arguments = cp_parser_enclosed_template_argument_list (parser);
8800 }
8801
8802 /* Build a representation of the specialization. */
8803 if (TREE_CODE (template) == IDENTIFIER_NODE)
8804 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8805 else if (DECL_CLASS_TEMPLATE_P (template)
8806 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8807 {
8808 bool entering_scope;
8809 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8810 template (rather than some instantiation thereof) only if
8811 is not nested within some other construct. For example, in
8812 "template <typename T> void f(T) { A<T>::", A<T> is just an
8813 instantiation of A. */
8814 entering_scope = (template_parm_scope_p ()
8815 && cp_lexer_next_token_is (parser->lexer,
8816 CPP_SCOPE));
8817 template_id
8818 = finish_template_type (template, arguments, entering_scope);
8819 }
8820 else
8821 {
8822 /* If it's not a class-template or a template-template, it should be
8823 a function-template. */
8824 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8825 || TREE_CODE (template) == OVERLOAD
8826 || BASELINK_P (template)));
8827
8828 template_id = lookup_template_function (template, arguments);
8829 }
8830
8831 /* Retrieve any deferred checks. Do not pop this access checks yet
8832 so the memory will not be reclaimed during token replacing below. */
8833 access_check = get_deferred_access_checks ();
8834
8835 /* If parsing tentatively, replace the sequence of tokens that makes
8836 up the template-id with a CPP_TEMPLATE_ID token. That way,
8837 should we re-parse the token stream, we will not have to repeat
8838 the effort required to do the parse, nor will we issue duplicate
8839 error messages about problems during instantiation of the
8840 template. */
8841 if (start_of_id)
8842 {
8843 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8844
8845 /* Reset the contents of the START_OF_ID token. */
8846 token->type = CPP_TEMPLATE_ID;
8847 token->value = build_tree_list (access_check, template_id);
8848 token->keyword = RID_MAX;
8849
8850 /* Purge all subsequent tokens. */
8851 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8852
8853 /* ??? Can we actually assume that, if template_id ==
8854 error_mark_node, we will have issued a diagnostic to the
8855 user, as opposed to simply marking the tentative parse as
8856 failed? */
8857 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8858 error ("parse error in template argument list");
8859 }
8860
8861 pop_deferring_access_checks ();
8862 return template_id;
8863 }
8864
8865 /* Parse a template-name.
8866
8867 template-name:
8868 identifier
8869
8870 The standard should actually say:
8871
8872 template-name:
8873 identifier
8874 operator-function-id
8875
8876 A defect report has been filed about this issue.
8877
8878 A conversion-function-id cannot be a template name because they cannot
8879 be part of a template-id. In fact, looking at this code:
8880
8881 a.operator K<int>()
8882
8883 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8884 It is impossible to call a templated conversion-function-id with an
8885 explicit argument list, since the only allowed template parameter is
8886 the type to which it is converting.
8887
8888 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8889 `template' keyword, in a construction like:
8890
8891 T::template f<3>()
8892
8893 In that case `f' is taken to be a template-name, even though there
8894 is no way of knowing for sure.
8895
8896 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8897 name refers to a set of overloaded functions, at least one of which
8898 is a template, or an IDENTIFIER_NODE with the name of the template,
8899 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8900 names are looked up inside uninstantiated templates. */
8901
8902 static tree
8903 cp_parser_template_name (cp_parser* parser,
8904 bool template_keyword_p,
8905 bool check_dependency_p,
8906 bool is_declaration,
8907 bool *is_identifier)
8908 {
8909 tree identifier;
8910 tree decl;
8911 tree fns;
8912
8913 /* If the next token is `operator', then we have either an
8914 operator-function-id or a conversion-function-id. */
8915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8916 {
8917 /* We don't know whether we're looking at an
8918 operator-function-id or a conversion-function-id. */
8919 cp_parser_parse_tentatively (parser);
8920 /* Try an operator-function-id. */
8921 identifier = cp_parser_operator_function_id (parser);
8922 /* If that didn't work, try a conversion-function-id. */
8923 if (!cp_parser_parse_definitely (parser))
8924 {
8925 cp_parser_error (parser, "expected template-name");
8926 return error_mark_node;
8927 }
8928 }
8929 /* Look for the identifier. */
8930 else
8931 identifier = cp_parser_identifier (parser);
8932
8933 /* If we didn't find an identifier, we don't have a template-id. */
8934 if (identifier == error_mark_node)
8935 return error_mark_node;
8936
8937 /* If the name immediately followed the `template' keyword, then it
8938 is a template-name. However, if the next token is not `<', then
8939 we do not treat it as a template-name, since it is not being used
8940 as part of a template-id. This enables us to handle constructs
8941 like:
8942
8943 template <typename T> struct S { S(); };
8944 template <typename T> S<T>::S();
8945
8946 correctly. We would treat `S' as a template -- if it were `S<T>'
8947 -- but we do not if there is no `<'. */
8948
8949 if (processing_template_decl
8950 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8951 {
8952 /* In a declaration, in a dependent context, we pretend that the
8953 "template" keyword was present in order to improve error
8954 recovery. For example, given:
8955
8956 template <typename T> void f(T::X<int>);
8957
8958 we want to treat "X<int>" as a template-id. */
8959 if (is_declaration
8960 && !template_keyword_p
8961 && parser->scope && TYPE_P (parser->scope)
8962 && check_dependency_p
8963 && dependent_type_p (parser->scope)
8964 /* Do not do this for dtors (or ctors), since they never
8965 need the template keyword before their name. */
8966 && !constructor_name_p (identifier, parser->scope))
8967 {
8968 cp_token_position start = 0;
8969
8970 /* Explain what went wrong. */
8971 error ("non-template %qD used as template", identifier);
8972 inform ("use %<%T::template %D%> to indicate that it is a template",
8973 parser->scope, identifier);
8974 /* If parsing tentatively, find the location of the "<" token. */
8975 if (cp_parser_simulate_error (parser))
8976 start = cp_lexer_token_position (parser->lexer, true);
8977 /* Parse the template arguments so that we can issue error
8978 messages about them. */
8979 cp_lexer_consume_token (parser->lexer);
8980 cp_parser_enclosed_template_argument_list (parser);
8981 /* Skip tokens until we find a good place from which to
8982 continue parsing. */
8983 cp_parser_skip_to_closing_parenthesis (parser,
8984 /*recovering=*/true,
8985 /*or_comma=*/true,
8986 /*consume_paren=*/false);
8987 /* If parsing tentatively, permanently remove the
8988 template argument list. That will prevent duplicate
8989 error messages from being issued about the missing
8990 "template" keyword. */
8991 if (start)
8992 cp_lexer_purge_tokens_after (parser->lexer, start);
8993 if (is_identifier)
8994 *is_identifier = true;
8995 return identifier;
8996 }
8997
8998 /* If the "template" keyword is present, then there is generally
8999 no point in doing name-lookup, so we just return IDENTIFIER.
9000 But, if the qualifying scope is non-dependent then we can
9001 (and must) do name-lookup normally. */
9002 if (template_keyword_p
9003 && (!parser->scope
9004 || (TYPE_P (parser->scope)
9005 && dependent_type_p (parser->scope))))
9006 return identifier;
9007 }
9008
9009 /* Look up the name. */
9010 decl = cp_parser_lookup_name (parser, identifier,
9011 none_type,
9012 /*is_template=*/false,
9013 /*is_namespace=*/false,
9014 check_dependency_p,
9015 /*ambiguous_decls=*/NULL);
9016 decl = maybe_get_template_decl_from_type_decl (decl);
9017
9018 /* If DECL is a template, then the name was a template-name. */
9019 if (TREE_CODE (decl) == TEMPLATE_DECL)
9020 ;
9021 else
9022 {
9023 tree fn = NULL_TREE;
9024
9025 /* The standard does not explicitly indicate whether a name that
9026 names a set of overloaded declarations, some of which are
9027 templates, is a template-name. However, such a name should
9028 be a template-name; otherwise, there is no way to form a
9029 template-id for the overloaded templates. */
9030 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9031 if (TREE_CODE (fns) == OVERLOAD)
9032 for (fn = fns; fn; fn = OVL_NEXT (fn))
9033 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9034 break;
9035
9036 if (!fn)
9037 {
9038 /* The name does not name a template. */
9039 cp_parser_error (parser, "expected template-name");
9040 return error_mark_node;
9041 }
9042 }
9043
9044 /* If DECL is dependent, and refers to a function, then just return
9045 its name; we will look it up again during template instantiation. */
9046 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9047 {
9048 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9049 if (TYPE_P (scope) && dependent_type_p (scope))
9050 return identifier;
9051 }
9052
9053 return decl;
9054 }
9055
9056 /* Parse a template-argument-list.
9057
9058 template-argument-list:
9059 template-argument
9060 template-argument-list , template-argument
9061
9062 Returns a TREE_VEC containing the arguments. */
9063
9064 static tree
9065 cp_parser_template_argument_list (cp_parser* parser)
9066 {
9067 tree fixed_args[10];
9068 unsigned n_args = 0;
9069 unsigned alloced = 10;
9070 tree *arg_ary = fixed_args;
9071 tree vec;
9072 bool saved_in_template_argument_list_p;
9073 bool saved_ice_p;
9074 bool saved_non_ice_p;
9075
9076 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9077 parser->in_template_argument_list_p = true;
9078 /* Even if the template-id appears in an integral
9079 constant-expression, the contents of the argument list do
9080 not. */
9081 saved_ice_p = parser->integral_constant_expression_p;
9082 parser->integral_constant_expression_p = false;
9083 saved_non_ice_p = parser->non_integral_constant_expression_p;
9084 parser->non_integral_constant_expression_p = false;
9085 /* Parse the arguments. */
9086 do
9087 {
9088 tree argument;
9089
9090 if (n_args)
9091 /* Consume the comma. */
9092 cp_lexer_consume_token (parser->lexer);
9093
9094 /* Parse the template-argument. */
9095 argument = cp_parser_template_argument (parser);
9096 if (n_args == alloced)
9097 {
9098 alloced *= 2;
9099
9100 if (arg_ary == fixed_args)
9101 {
9102 arg_ary = XNEWVEC (tree, alloced);
9103 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9104 }
9105 else
9106 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9107 }
9108 arg_ary[n_args++] = argument;
9109 }
9110 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9111
9112 vec = make_tree_vec (n_args);
9113
9114 while (n_args--)
9115 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9116
9117 if (arg_ary != fixed_args)
9118 free (arg_ary);
9119 parser->non_integral_constant_expression_p = saved_non_ice_p;
9120 parser->integral_constant_expression_p = saved_ice_p;
9121 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9122 return vec;
9123 }
9124
9125 /* Parse a template-argument.
9126
9127 template-argument:
9128 assignment-expression
9129 type-id
9130 id-expression
9131
9132 The representation is that of an assignment-expression, type-id, or
9133 id-expression -- except that the qualified id-expression is
9134 evaluated, so that the value returned is either a DECL or an
9135 OVERLOAD.
9136
9137 Although the standard says "assignment-expression", it forbids
9138 throw-expressions or assignments in the template argument.
9139 Therefore, we use "conditional-expression" instead. */
9140
9141 static tree
9142 cp_parser_template_argument (cp_parser* parser)
9143 {
9144 tree argument;
9145 bool template_p;
9146 bool address_p;
9147 bool maybe_type_id = false;
9148 cp_token *token;
9149 cp_id_kind idk;
9150
9151 /* There's really no way to know what we're looking at, so we just
9152 try each alternative in order.
9153
9154 [temp.arg]
9155
9156 In a template-argument, an ambiguity between a type-id and an
9157 expression is resolved to a type-id, regardless of the form of
9158 the corresponding template-parameter.
9159
9160 Therefore, we try a type-id first. */
9161 cp_parser_parse_tentatively (parser);
9162 argument = cp_parser_type_id (parser);
9163 /* If there was no error parsing the type-id but the next token is a '>>',
9164 we probably found a typo for '> >'. But there are type-id which are
9165 also valid expressions. For instance:
9166
9167 struct X { int operator >> (int); };
9168 template <int V> struct Foo {};
9169 Foo<X () >> 5> r;
9170
9171 Here 'X()' is a valid type-id of a function type, but the user just
9172 wanted to write the expression "X() >> 5". Thus, we remember that we
9173 found a valid type-id, but we still try to parse the argument as an
9174 expression to see what happens. */
9175 if (!cp_parser_error_occurred (parser)
9176 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9177 {
9178 maybe_type_id = true;
9179 cp_parser_abort_tentative_parse (parser);
9180 }
9181 else
9182 {
9183 /* If the next token isn't a `,' or a `>', then this argument wasn't
9184 really finished. This means that the argument is not a valid
9185 type-id. */
9186 if (!cp_parser_next_token_ends_template_argument_p (parser))
9187 cp_parser_error (parser, "expected template-argument");
9188 /* If that worked, we're done. */
9189 if (cp_parser_parse_definitely (parser))
9190 return argument;
9191 }
9192 /* We're still not sure what the argument will be. */
9193 cp_parser_parse_tentatively (parser);
9194 /* Try a template. */
9195 argument = cp_parser_id_expression (parser,
9196 /*template_keyword_p=*/false,
9197 /*check_dependency_p=*/true,
9198 &template_p,
9199 /*declarator_p=*/false,
9200 /*optional_p=*/false);
9201 /* If the next token isn't a `,' or a `>', then this argument wasn't
9202 really finished. */
9203 if (!cp_parser_next_token_ends_template_argument_p (parser))
9204 cp_parser_error (parser, "expected template-argument");
9205 if (!cp_parser_error_occurred (parser))
9206 {
9207 /* Figure out what is being referred to. If the id-expression
9208 was for a class template specialization, then we will have a
9209 TYPE_DECL at this point. There is no need to do name lookup
9210 at this point in that case. */
9211 if (TREE_CODE (argument) != TYPE_DECL)
9212 argument = cp_parser_lookup_name (parser, argument,
9213 none_type,
9214 /*is_template=*/template_p,
9215 /*is_namespace=*/false,
9216 /*check_dependency=*/true,
9217 /*ambiguous_decls=*/NULL);
9218 if (TREE_CODE (argument) != TEMPLATE_DECL
9219 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9220 cp_parser_error (parser, "expected template-name");
9221 }
9222 if (cp_parser_parse_definitely (parser))
9223 return argument;
9224 /* It must be a non-type argument. There permitted cases are given
9225 in [temp.arg.nontype]:
9226
9227 -- an integral constant-expression of integral or enumeration
9228 type; or
9229
9230 -- the name of a non-type template-parameter; or
9231
9232 -- the name of an object or function with external linkage...
9233
9234 -- the address of an object or function with external linkage...
9235
9236 -- a pointer to member... */
9237 /* Look for a non-type template parameter. */
9238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9239 {
9240 cp_parser_parse_tentatively (parser);
9241 argument = cp_parser_primary_expression (parser,
9242 /*adress_p=*/false,
9243 /*cast_p=*/false,
9244 /*template_arg_p=*/true,
9245 &idk);
9246 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9247 || !cp_parser_next_token_ends_template_argument_p (parser))
9248 cp_parser_simulate_error (parser);
9249 if (cp_parser_parse_definitely (parser))
9250 return argument;
9251 }
9252
9253 /* If the next token is "&", the argument must be the address of an
9254 object or function with external linkage. */
9255 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9256 if (address_p)
9257 cp_lexer_consume_token (parser->lexer);
9258 /* See if we might have an id-expression. */
9259 token = cp_lexer_peek_token (parser->lexer);
9260 if (token->type == CPP_NAME
9261 || token->keyword == RID_OPERATOR
9262 || token->type == CPP_SCOPE
9263 || token->type == CPP_TEMPLATE_ID
9264 || token->type == CPP_NESTED_NAME_SPECIFIER)
9265 {
9266 cp_parser_parse_tentatively (parser);
9267 argument = cp_parser_primary_expression (parser,
9268 address_p,
9269 /*cast_p=*/false,
9270 /*template_arg_p=*/true,
9271 &idk);
9272 if (cp_parser_error_occurred (parser)
9273 || !cp_parser_next_token_ends_template_argument_p (parser))
9274 cp_parser_abort_tentative_parse (parser);
9275 else
9276 {
9277 if (TREE_CODE (argument) == INDIRECT_REF)
9278 {
9279 gcc_assert (REFERENCE_REF_P (argument));
9280 argument = TREE_OPERAND (argument, 0);
9281 }
9282
9283 if (TREE_CODE (argument) == VAR_DECL)
9284 {
9285 /* A variable without external linkage might still be a
9286 valid constant-expression, so no error is issued here
9287 if the external-linkage check fails. */
9288 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9289 cp_parser_simulate_error (parser);
9290 }
9291 else if (is_overloaded_fn (argument))
9292 /* All overloaded functions are allowed; if the external
9293 linkage test does not pass, an error will be issued
9294 later. */
9295 ;
9296 else if (address_p
9297 && (TREE_CODE (argument) == OFFSET_REF
9298 || TREE_CODE (argument) == SCOPE_REF))
9299 /* A pointer-to-member. */
9300 ;
9301 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9302 ;
9303 else
9304 cp_parser_simulate_error (parser);
9305
9306 if (cp_parser_parse_definitely (parser))
9307 {
9308 if (address_p)
9309 argument = build_x_unary_op (ADDR_EXPR, argument);
9310 return argument;
9311 }
9312 }
9313 }
9314 /* If the argument started with "&", there are no other valid
9315 alternatives at this point. */
9316 if (address_p)
9317 {
9318 cp_parser_error (parser, "invalid non-type template argument");
9319 return error_mark_node;
9320 }
9321
9322 /* If the argument wasn't successfully parsed as a type-id followed
9323 by '>>', the argument can only be a constant expression now.
9324 Otherwise, we try parsing the constant-expression tentatively,
9325 because the argument could really be a type-id. */
9326 if (maybe_type_id)
9327 cp_parser_parse_tentatively (parser);
9328 argument = cp_parser_constant_expression (parser,
9329 /*allow_non_constant_p=*/false,
9330 /*non_constant_p=*/NULL);
9331 argument = fold_non_dependent_expr (argument);
9332 if (!maybe_type_id)
9333 return argument;
9334 if (!cp_parser_next_token_ends_template_argument_p (parser))
9335 cp_parser_error (parser, "expected template-argument");
9336 if (cp_parser_parse_definitely (parser))
9337 return argument;
9338 /* We did our best to parse the argument as a non type-id, but that
9339 was the only alternative that matched (albeit with a '>' after
9340 it). We can assume it's just a typo from the user, and a
9341 diagnostic will then be issued. */
9342 return cp_parser_type_id (parser);
9343 }
9344
9345 /* Parse an explicit-instantiation.
9346
9347 explicit-instantiation:
9348 template declaration
9349
9350 Although the standard says `declaration', what it really means is:
9351
9352 explicit-instantiation:
9353 template decl-specifier-seq [opt] declarator [opt] ;
9354
9355 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9356 supposed to be allowed. A defect report has been filed about this
9357 issue.
9358
9359 GNU Extension:
9360
9361 explicit-instantiation:
9362 storage-class-specifier template
9363 decl-specifier-seq [opt] declarator [opt] ;
9364 function-specifier template
9365 decl-specifier-seq [opt] declarator [opt] ; */
9366
9367 static void
9368 cp_parser_explicit_instantiation (cp_parser* parser)
9369 {
9370 int declares_class_or_enum;
9371 cp_decl_specifier_seq decl_specifiers;
9372 tree extension_specifier = NULL_TREE;
9373
9374 /* Look for an (optional) storage-class-specifier or
9375 function-specifier. */
9376 if (cp_parser_allow_gnu_extensions_p (parser))
9377 {
9378 extension_specifier
9379 = cp_parser_storage_class_specifier_opt (parser);
9380 if (!extension_specifier)
9381 extension_specifier
9382 = cp_parser_function_specifier_opt (parser,
9383 /*decl_specs=*/NULL);
9384 }
9385
9386 /* Look for the `template' keyword. */
9387 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9388 /* Let the front end know that we are processing an explicit
9389 instantiation. */
9390 begin_explicit_instantiation ();
9391 /* [temp.explicit] says that we are supposed to ignore access
9392 control while processing explicit instantiation directives. */
9393 push_deferring_access_checks (dk_no_check);
9394 /* Parse a decl-specifier-seq. */
9395 cp_parser_decl_specifier_seq (parser,
9396 CP_PARSER_FLAGS_OPTIONAL,
9397 &decl_specifiers,
9398 &declares_class_or_enum);
9399 /* If there was exactly one decl-specifier, and it declared a class,
9400 and there's no declarator, then we have an explicit type
9401 instantiation. */
9402 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9403 {
9404 tree type;
9405
9406 type = check_tag_decl (&decl_specifiers);
9407 /* Turn access control back on for names used during
9408 template instantiation. */
9409 pop_deferring_access_checks ();
9410 if (type)
9411 do_type_instantiation (type, extension_specifier,
9412 /*complain=*/tf_error);
9413 }
9414 else
9415 {
9416 cp_declarator *declarator;
9417 tree decl;
9418
9419 /* Parse the declarator. */
9420 declarator
9421 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9422 /*ctor_dtor_or_conv_p=*/NULL,
9423 /*parenthesized_p=*/NULL,
9424 /*member_p=*/false);
9425 if (declares_class_or_enum & 2)
9426 cp_parser_check_for_definition_in_return_type (declarator,
9427 decl_specifiers.type);
9428 if (declarator != cp_error_declarator)
9429 {
9430 decl = grokdeclarator (declarator, &decl_specifiers,
9431 NORMAL, 0, &decl_specifiers.attributes);
9432 /* Turn access control back on for names used during
9433 template instantiation. */
9434 pop_deferring_access_checks ();
9435 /* Do the explicit instantiation. */
9436 do_decl_instantiation (decl, extension_specifier);
9437 }
9438 else
9439 {
9440 pop_deferring_access_checks ();
9441 /* Skip the body of the explicit instantiation. */
9442 cp_parser_skip_to_end_of_statement (parser);
9443 }
9444 }
9445 /* We're done with the instantiation. */
9446 end_explicit_instantiation ();
9447
9448 cp_parser_consume_semicolon_at_end_of_statement (parser);
9449 }
9450
9451 /* Parse an explicit-specialization.
9452
9453 explicit-specialization:
9454 template < > declaration
9455
9456 Although the standard says `declaration', what it really means is:
9457
9458 explicit-specialization:
9459 template <> decl-specifier [opt] init-declarator [opt] ;
9460 template <> function-definition
9461 template <> explicit-specialization
9462 template <> template-declaration */
9463
9464 static void
9465 cp_parser_explicit_specialization (cp_parser* parser)
9466 {
9467 bool need_lang_pop;
9468 /* Look for the `template' keyword. */
9469 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9470 /* Look for the `<'. */
9471 cp_parser_require (parser, CPP_LESS, "`<'");
9472 /* Look for the `>'. */
9473 cp_parser_require (parser, CPP_GREATER, "`>'");
9474 /* We have processed another parameter list. */
9475 ++parser->num_template_parameter_lists;
9476 /* [temp]
9477
9478 A template ... explicit specialization ... shall not have C
9479 linkage. */
9480 if (current_lang_name == lang_name_c)
9481 {
9482 error ("template specialization with C linkage");
9483 /* Give it C++ linkage to avoid confusing other parts of the
9484 front end. */
9485 push_lang_context (lang_name_cplusplus);
9486 need_lang_pop = true;
9487 }
9488 else
9489 need_lang_pop = false;
9490 /* Let the front end know that we are beginning a specialization. */
9491 if (!begin_specialization ())
9492 {
9493 end_specialization ();
9494 cp_parser_skip_to_end_of_block_or_statement (parser);
9495 return;
9496 }
9497
9498 /* If the next keyword is `template', we need to figure out whether
9499 or not we're looking a template-declaration. */
9500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9501 {
9502 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9503 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9504 cp_parser_template_declaration_after_export (parser,
9505 /*member_p=*/false);
9506 else
9507 cp_parser_explicit_specialization (parser);
9508 }
9509 else
9510 /* Parse the dependent declaration. */
9511 cp_parser_single_declaration (parser,
9512 /*checks=*/NULL_TREE,
9513 /*member_p=*/false,
9514 /*friend_p=*/NULL);
9515 /* We're done with the specialization. */
9516 end_specialization ();
9517 /* For the erroneous case of a template with C linkage, we pushed an
9518 implicit C++ linkage scope; exit that scope now. */
9519 if (need_lang_pop)
9520 pop_lang_context ();
9521 /* We're done with this parameter list. */
9522 --parser->num_template_parameter_lists;
9523 }
9524
9525 /* Parse a type-specifier.
9526
9527 type-specifier:
9528 simple-type-specifier
9529 class-specifier
9530 enum-specifier
9531 elaborated-type-specifier
9532 cv-qualifier
9533
9534 GNU Extension:
9535
9536 type-specifier:
9537 __complex__
9538
9539 Returns a representation of the type-specifier. For a
9540 class-specifier, enum-specifier, or elaborated-type-specifier, a
9541 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9542
9543 The parser flags FLAGS is used to control type-specifier parsing.
9544
9545 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9546 in a decl-specifier-seq.
9547
9548 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9549 class-specifier, enum-specifier, or elaborated-type-specifier, then
9550 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9551 if a type is declared; 2 if it is defined. Otherwise, it is set to
9552 zero.
9553
9554 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9555 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9556 is set to FALSE. */
9557
9558 static tree
9559 cp_parser_type_specifier (cp_parser* parser,
9560 cp_parser_flags flags,
9561 cp_decl_specifier_seq *decl_specs,
9562 bool is_declaration,
9563 int* declares_class_or_enum,
9564 bool* is_cv_qualifier)
9565 {
9566 tree type_spec = NULL_TREE;
9567 cp_token *token;
9568 enum rid keyword;
9569 cp_decl_spec ds = ds_last;
9570
9571 /* Assume this type-specifier does not declare a new type. */
9572 if (declares_class_or_enum)
9573 *declares_class_or_enum = 0;
9574 /* And that it does not specify a cv-qualifier. */
9575 if (is_cv_qualifier)
9576 *is_cv_qualifier = false;
9577 /* Peek at the next token. */
9578 token = cp_lexer_peek_token (parser->lexer);
9579
9580 /* If we're looking at a keyword, we can use that to guide the
9581 production we choose. */
9582 keyword = token->keyword;
9583 switch (keyword)
9584 {
9585 case RID_ENUM:
9586 /* Look for the enum-specifier. */
9587 type_spec = cp_parser_enum_specifier (parser);
9588 /* If that worked, we're done. */
9589 if (type_spec)
9590 {
9591 if (declares_class_or_enum)
9592 *declares_class_or_enum = 2;
9593 if (decl_specs)
9594 cp_parser_set_decl_spec_type (decl_specs,
9595 type_spec,
9596 /*user_defined_p=*/true);
9597 return type_spec;
9598 }
9599 else
9600 goto elaborated_type_specifier;
9601
9602 /* Any of these indicate either a class-specifier, or an
9603 elaborated-type-specifier. */
9604 case RID_CLASS:
9605 case RID_STRUCT:
9606 case RID_UNION:
9607 /* Parse tentatively so that we can back up if we don't find a
9608 class-specifier. */
9609 cp_parser_parse_tentatively (parser);
9610 /* Look for the class-specifier. */
9611 type_spec = cp_parser_class_specifier (parser);
9612 /* If that worked, we're done. */
9613 if (cp_parser_parse_definitely (parser))
9614 {
9615 if (declares_class_or_enum)
9616 *declares_class_or_enum = 2;
9617 if (decl_specs)
9618 cp_parser_set_decl_spec_type (decl_specs,
9619 type_spec,
9620 /*user_defined_p=*/true);
9621 return type_spec;
9622 }
9623
9624 /* Fall through. */
9625 elaborated_type_specifier:
9626 /* We're declaring (not defining) a class or enum. */
9627 if (declares_class_or_enum)
9628 *declares_class_or_enum = 1;
9629
9630 /* Fall through. */
9631 case RID_TYPENAME:
9632 /* Look for an elaborated-type-specifier. */
9633 type_spec
9634 = (cp_parser_elaborated_type_specifier
9635 (parser,
9636 decl_specs && decl_specs->specs[(int) ds_friend],
9637 is_declaration));
9638 if (decl_specs)
9639 cp_parser_set_decl_spec_type (decl_specs,
9640 type_spec,
9641 /*user_defined_p=*/true);
9642 return type_spec;
9643
9644 case RID_CONST:
9645 ds = ds_const;
9646 if (is_cv_qualifier)
9647 *is_cv_qualifier = true;
9648 break;
9649
9650 case RID_VOLATILE:
9651 ds = ds_volatile;
9652 if (is_cv_qualifier)
9653 *is_cv_qualifier = true;
9654 break;
9655
9656 case RID_RESTRICT:
9657 ds = ds_restrict;
9658 if (is_cv_qualifier)
9659 *is_cv_qualifier = true;
9660 break;
9661
9662 case RID_COMPLEX:
9663 /* The `__complex__' keyword is a GNU extension. */
9664 ds = ds_complex;
9665 break;
9666
9667 default:
9668 break;
9669 }
9670
9671 /* Handle simple keywords. */
9672 if (ds != ds_last)
9673 {
9674 if (decl_specs)
9675 {
9676 ++decl_specs->specs[(int)ds];
9677 decl_specs->any_specifiers_p = true;
9678 }
9679 return cp_lexer_consume_token (parser->lexer)->value;
9680 }
9681
9682 /* If we do not already have a type-specifier, assume we are looking
9683 at a simple-type-specifier. */
9684 type_spec = cp_parser_simple_type_specifier (parser,
9685 decl_specs,
9686 flags);
9687
9688 /* If we didn't find a type-specifier, and a type-specifier was not
9689 optional in this context, issue an error message. */
9690 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9691 {
9692 cp_parser_error (parser, "expected type specifier");
9693 return error_mark_node;
9694 }
9695
9696 return type_spec;
9697 }
9698
9699 /* Parse a simple-type-specifier.
9700
9701 simple-type-specifier:
9702 :: [opt] nested-name-specifier [opt] type-name
9703 :: [opt] nested-name-specifier template template-id
9704 char
9705 wchar_t
9706 bool
9707 short
9708 int
9709 long
9710 signed
9711 unsigned
9712 float
9713 double
9714 void
9715
9716 GNU Extension:
9717
9718 simple-type-specifier:
9719 __typeof__ unary-expression
9720 __typeof__ ( type-id )
9721
9722 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9723 appropriately updated. */
9724
9725 static tree
9726 cp_parser_simple_type_specifier (cp_parser* parser,
9727 cp_decl_specifier_seq *decl_specs,
9728 cp_parser_flags flags)
9729 {
9730 tree type = NULL_TREE;
9731 cp_token *token;
9732
9733 /* Peek at the next token. */
9734 token = cp_lexer_peek_token (parser->lexer);
9735
9736 /* If we're looking at a keyword, things are easy. */
9737 switch (token->keyword)
9738 {
9739 case RID_CHAR:
9740 if (decl_specs)
9741 decl_specs->explicit_char_p = true;
9742 type = char_type_node;
9743 break;
9744 case RID_WCHAR:
9745 type = wchar_type_node;
9746 break;
9747 case RID_BOOL:
9748 type = boolean_type_node;
9749 break;
9750 case RID_SHORT:
9751 if (decl_specs)
9752 ++decl_specs->specs[(int) ds_short];
9753 type = short_integer_type_node;
9754 break;
9755 case RID_INT:
9756 if (decl_specs)
9757 decl_specs->explicit_int_p = true;
9758 type = integer_type_node;
9759 break;
9760 case RID_LONG:
9761 if (decl_specs)
9762 ++decl_specs->specs[(int) ds_long];
9763 type = long_integer_type_node;
9764 break;
9765 case RID_SIGNED:
9766 if (decl_specs)
9767 ++decl_specs->specs[(int) ds_signed];
9768 type = integer_type_node;
9769 break;
9770 case RID_UNSIGNED:
9771 if (decl_specs)
9772 ++decl_specs->specs[(int) ds_unsigned];
9773 type = unsigned_type_node;
9774 break;
9775 case RID_FLOAT:
9776 type = float_type_node;
9777 break;
9778 case RID_DOUBLE:
9779 type = double_type_node;
9780 break;
9781 case RID_VOID:
9782 type = void_type_node;
9783 break;
9784
9785 case RID_TYPEOF:
9786 /* Consume the `typeof' token. */
9787 cp_lexer_consume_token (parser->lexer);
9788 /* Parse the operand to `typeof'. */
9789 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9790 /* If it is not already a TYPE, take its type. */
9791 if (!TYPE_P (type))
9792 type = finish_typeof (type);
9793
9794 if (decl_specs)
9795 cp_parser_set_decl_spec_type (decl_specs, type,
9796 /*user_defined_p=*/true);
9797
9798 return type;
9799
9800 default:
9801 break;
9802 }
9803
9804 /* If the type-specifier was for a built-in type, we're done. */
9805 if (type)
9806 {
9807 tree id;
9808
9809 /* Record the type. */
9810 if (decl_specs
9811 && (token->keyword != RID_SIGNED
9812 && token->keyword != RID_UNSIGNED
9813 && token->keyword != RID_SHORT
9814 && token->keyword != RID_LONG))
9815 cp_parser_set_decl_spec_type (decl_specs,
9816 type,
9817 /*user_defined=*/false);
9818 if (decl_specs)
9819 decl_specs->any_specifiers_p = true;
9820
9821 /* Consume the token. */
9822 id = cp_lexer_consume_token (parser->lexer)->value;
9823
9824 /* There is no valid C++ program where a non-template type is
9825 followed by a "<". That usually indicates that the user thought
9826 that the type was a template. */
9827 cp_parser_check_for_invalid_template_id (parser, type);
9828
9829 return TYPE_NAME (type);
9830 }
9831
9832 /* The type-specifier must be a user-defined type. */
9833 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9834 {
9835 bool qualified_p;
9836 bool global_p;
9837
9838 /* Don't gobble tokens or issue error messages if this is an
9839 optional type-specifier. */
9840 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9841 cp_parser_parse_tentatively (parser);
9842
9843 /* Look for the optional `::' operator. */
9844 global_p
9845 = (cp_parser_global_scope_opt (parser,
9846 /*current_scope_valid_p=*/false)
9847 != NULL_TREE);
9848 /* Look for the nested-name specifier. */
9849 qualified_p
9850 = (cp_parser_nested_name_specifier_opt (parser,
9851 /*typename_keyword_p=*/false,
9852 /*check_dependency_p=*/true,
9853 /*type_p=*/false,
9854 /*is_declaration=*/false)
9855 != NULL_TREE);
9856 /* If we have seen a nested-name-specifier, and the next token
9857 is `template', then we are using the template-id production. */
9858 if (parser->scope
9859 && cp_parser_optional_template_keyword (parser))
9860 {
9861 /* Look for the template-id. */
9862 type = cp_parser_template_id (parser,
9863 /*template_keyword_p=*/true,
9864 /*check_dependency_p=*/true,
9865 /*is_declaration=*/false);
9866 /* If the template-id did not name a type, we are out of
9867 luck. */
9868 if (TREE_CODE (type) != TYPE_DECL)
9869 {
9870 cp_parser_error (parser, "expected template-id for type");
9871 type = NULL_TREE;
9872 }
9873 }
9874 /* Otherwise, look for a type-name. */
9875 else
9876 type = cp_parser_type_name (parser);
9877 /* Keep track of all name-lookups performed in class scopes. */
9878 if (type
9879 && !global_p
9880 && !qualified_p
9881 && TREE_CODE (type) == TYPE_DECL
9882 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9883 maybe_note_name_used_in_class (DECL_NAME (type), type);
9884 /* If it didn't work out, we don't have a TYPE. */
9885 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9886 && !cp_parser_parse_definitely (parser))
9887 type = NULL_TREE;
9888 if (type && decl_specs)
9889 cp_parser_set_decl_spec_type (decl_specs, type,
9890 /*user_defined=*/true);
9891 }
9892
9893 /* If we didn't get a type-name, issue an error message. */
9894 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9895 {
9896 cp_parser_error (parser, "expected type-name");
9897 return error_mark_node;
9898 }
9899
9900 /* There is no valid C++ program where a non-template type is
9901 followed by a "<". That usually indicates that the user thought
9902 that the type was a template. */
9903 if (type && type != error_mark_node)
9904 {
9905 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9906 If it is, then the '<'...'>' enclose protocol names rather than
9907 template arguments, and so everything is fine. */
9908 if (c_dialect_objc ()
9909 && (objc_is_id (type) || objc_is_class_name (type)))
9910 {
9911 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9912 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9913
9914 /* Clobber the "unqualified" type previously entered into
9915 DECL_SPECS with the new, improved protocol-qualified version. */
9916 if (decl_specs)
9917 decl_specs->type = qual_type;
9918
9919 return qual_type;
9920 }
9921
9922 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9923 }
9924
9925 return type;
9926 }
9927
9928 /* Parse a type-name.
9929
9930 type-name:
9931 class-name
9932 enum-name
9933 typedef-name
9934
9935 enum-name:
9936 identifier
9937
9938 typedef-name:
9939 identifier
9940
9941 Returns a TYPE_DECL for the type. */
9942
9943 static tree
9944 cp_parser_type_name (cp_parser* parser)
9945 {
9946 tree type_decl;
9947 tree identifier;
9948
9949 /* We can't know yet whether it is a class-name or not. */
9950 cp_parser_parse_tentatively (parser);
9951 /* Try a class-name. */
9952 type_decl = cp_parser_class_name (parser,
9953 /*typename_keyword_p=*/false,
9954 /*template_keyword_p=*/false,
9955 none_type,
9956 /*check_dependency_p=*/true,
9957 /*class_head_p=*/false,
9958 /*is_declaration=*/false);
9959 /* If it's not a class-name, keep looking. */
9960 if (!cp_parser_parse_definitely (parser))
9961 {
9962 /* It must be a typedef-name or an enum-name. */
9963 identifier = cp_parser_identifier (parser);
9964 if (identifier == error_mark_node)
9965 return error_mark_node;
9966
9967 /* Look up the type-name. */
9968 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9969
9970 if (TREE_CODE (type_decl) != TYPE_DECL
9971 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9972 {
9973 /* See if this is an Objective-C type. */
9974 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9975 tree type = objc_get_protocol_qualified_type (identifier, protos);
9976 if (type)
9977 type_decl = TYPE_NAME (type);
9978 }
9979
9980 /* Issue an error if we did not find a type-name. */
9981 if (TREE_CODE (type_decl) != TYPE_DECL)
9982 {
9983 if (!cp_parser_simulate_error (parser))
9984 cp_parser_name_lookup_error (parser, identifier, type_decl,
9985 "is not a type");
9986 type_decl = error_mark_node;
9987 }
9988 /* Remember that the name was used in the definition of the
9989 current class so that we can check later to see if the
9990 meaning would have been different after the class was
9991 entirely defined. */
9992 else if (type_decl != error_mark_node
9993 && !parser->scope)
9994 maybe_note_name_used_in_class (identifier, type_decl);
9995 }
9996
9997 return type_decl;
9998 }
9999
10000
10001 /* Parse an elaborated-type-specifier. Note that the grammar given
10002 here incorporates the resolution to DR68.
10003
10004 elaborated-type-specifier:
10005 class-key :: [opt] nested-name-specifier [opt] identifier
10006 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10007 enum :: [opt] nested-name-specifier [opt] identifier
10008 typename :: [opt] nested-name-specifier identifier
10009 typename :: [opt] nested-name-specifier template [opt]
10010 template-id
10011
10012 GNU extension:
10013
10014 elaborated-type-specifier:
10015 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10016 class-key attributes :: [opt] nested-name-specifier [opt]
10017 template [opt] template-id
10018 enum attributes :: [opt] nested-name-specifier [opt] identifier
10019
10020 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10021 declared `friend'. If IS_DECLARATION is TRUE, then this
10022 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10023 something is being declared.
10024
10025 Returns the TYPE specified. */
10026
10027 static tree
10028 cp_parser_elaborated_type_specifier (cp_parser* parser,
10029 bool is_friend,
10030 bool is_declaration)
10031 {
10032 enum tag_types tag_type;
10033 tree identifier;
10034 tree type = NULL_TREE;
10035 tree attributes = NULL_TREE;
10036
10037 /* See if we're looking at the `enum' keyword. */
10038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10039 {
10040 /* Consume the `enum' token. */
10041 cp_lexer_consume_token (parser->lexer);
10042 /* Remember that it's an enumeration type. */
10043 tag_type = enum_type;
10044 /* Parse the attributes. */
10045 attributes = cp_parser_attributes_opt (parser);
10046 }
10047 /* Or, it might be `typename'. */
10048 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10049 RID_TYPENAME))
10050 {
10051 /* Consume the `typename' token. */
10052 cp_lexer_consume_token (parser->lexer);
10053 /* Remember that it's a `typename' type. */
10054 tag_type = typename_type;
10055 /* The `typename' keyword is only allowed in templates. */
10056 if (!processing_template_decl)
10057 pedwarn ("using %<typename%> outside of template");
10058 }
10059 /* Otherwise it must be a class-key. */
10060 else
10061 {
10062 tag_type = cp_parser_class_key (parser);
10063 if (tag_type == none_type)
10064 return error_mark_node;
10065 /* Parse the attributes. */
10066 attributes = cp_parser_attributes_opt (parser);
10067 }
10068
10069 /* Look for the `::' operator. */
10070 cp_parser_global_scope_opt (parser,
10071 /*current_scope_valid_p=*/false);
10072 /* Look for the nested-name-specifier. */
10073 if (tag_type == typename_type)
10074 {
10075 if (!cp_parser_nested_name_specifier (parser,
10076 /*typename_keyword_p=*/true,
10077 /*check_dependency_p=*/true,
10078 /*type_p=*/true,
10079 is_declaration))
10080 return error_mark_node;
10081 }
10082 else
10083 /* Even though `typename' is not present, the proposed resolution
10084 to Core Issue 180 says that in `class A<T>::B', `B' should be
10085 considered a type-name, even if `A<T>' is dependent. */
10086 cp_parser_nested_name_specifier_opt (parser,
10087 /*typename_keyword_p=*/true,
10088 /*check_dependency_p=*/true,
10089 /*type_p=*/true,
10090 is_declaration);
10091 /* For everything but enumeration types, consider a template-id. */
10092 /* For an enumeration type, consider only a plain identifier. */
10093 if (tag_type != enum_type)
10094 {
10095 bool template_p = false;
10096 tree decl;
10097
10098 /* Allow the `template' keyword. */
10099 template_p = cp_parser_optional_template_keyword (parser);
10100 /* If we didn't see `template', we don't know if there's a
10101 template-id or not. */
10102 if (!template_p)
10103 cp_parser_parse_tentatively (parser);
10104 /* Parse the template-id. */
10105 decl = cp_parser_template_id (parser, template_p,
10106 /*check_dependency_p=*/true,
10107 is_declaration);
10108 /* If we didn't find a template-id, look for an ordinary
10109 identifier. */
10110 if (!template_p && !cp_parser_parse_definitely (parser))
10111 ;
10112 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10113 in effect, then we must assume that, upon instantiation, the
10114 template will correspond to a class. */
10115 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10116 && tag_type == typename_type)
10117 type = make_typename_type (parser->scope, decl,
10118 typename_type,
10119 /*complain=*/tf_error);
10120 else
10121 type = TREE_TYPE (decl);
10122 }
10123
10124 if (!type)
10125 {
10126 identifier = cp_parser_identifier (parser);
10127
10128 if (identifier == error_mark_node)
10129 {
10130 parser->scope = NULL_TREE;
10131 return error_mark_node;
10132 }
10133
10134 /* For a `typename', we needn't call xref_tag. */
10135 if (tag_type == typename_type
10136 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10137 return cp_parser_make_typename_type (parser, parser->scope,
10138 identifier);
10139 /* Look up a qualified name in the usual way. */
10140 if (parser->scope)
10141 {
10142 tree decl;
10143
10144 decl = cp_parser_lookup_name (parser, identifier,
10145 tag_type,
10146 /*is_template=*/false,
10147 /*is_namespace=*/false,
10148 /*check_dependency=*/true,
10149 /*ambiguous_decls=*/NULL);
10150
10151 /* If we are parsing friend declaration, DECL may be a
10152 TEMPLATE_DECL tree node here. However, we need to check
10153 whether this TEMPLATE_DECL results in valid code. Consider
10154 the following example:
10155
10156 namespace N {
10157 template <class T> class C {};
10158 }
10159 class X {
10160 template <class T> friend class N::C; // #1, valid code
10161 };
10162 template <class T> class Y {
10163 friend class N::C; // #2, invalid code
10164 };
10165
10166 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10167 name lookup of `N::C'. We see that friend declaration must
10168 be template for the code to be valid. Note that
10169 processing_template_decl does not work here since it is
10170 always 1 for the above two cases. */
10171
10172 decl = (cp_parser_maybe_treat_template_as_class
10173 (decl, /*tag_name_p=*/is_friend
10174 && parser->num_template_parameter_lists));
10175
10176 if (TREE_CODE (decl) != TYPE_DECL)
10177 {
10178 cp_parser_diagnose_invalid_type_name (parser,
10179 parser->scope,
10180 identifier);
10181 return error_mark_node;
10182 }
10183
10184 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10185 check_elaborated_type_specifier
10186 (tag_type, decl,
10187 (parser->num_template_parameter_lists
10188 || DECL_SELF_REFERENCE_P (decl)));
10189
10190 type = TREE_TYPE (decl);
10191 }
10192 else
10193 {
10194 /* An elaborated-type-specifier sometimes introduces a new type and
10195 sometimes names an existing type. Normally, the rule is that it
10196 introduces a new type only if there is not an existing type of
10197 the same name already in scope. For example, given:
10198
10199 struct S {};
10200 void f() { struct S s; }
10201
10202 the `struct S' in the body of `f' is the same `struct S' as in
10203 the global scope; the existing definition is used. However, if
10204 there were no global declaration, this would introduce a new
10205 local class named `S'.
10206
10207 An exception to this rule applies to the following code:
10208
10209 namespace N { struct S; }
10210
10211 Here, the elaborated-type-specifier names a new type
10212 unconditionally; even if there is already an `S' in the
10213 containing scope this declaration names a new type.
10214 This exception only applies if the elaborated-type-specifier
10215 forms the complete declaration:
10216
10217 [class.name]
10218
10219 A declaration consisting solely of `class-key identifier ;' is
10220 either a redeclaration of the name in the current scope or a
10221 forward declaration of the identifier as a class name. It
10222 introduces the name into the current scope.
10223
10224 We are in this situation precisely when the next token is a `;'.
10225
10226 An exception to the exception is that a `friend' declaration does
10227 *not* name a new type; i.e., given:
10228
10229 struct S { friend struct T; };
10230
10231 `T' is not a new type in the scope of `S'.
10232
10233 Also, `new struct S' or `sizeof (struct S)' never results in the
10234 definition of a new type; a new type can only be declared in a
10235 declaration context. */
10236
10237 tag_scope ts;
10238 bool template_p;
10239
10240 if (is_friend)
10241 /* Friends have special name lookup rules. */
10242 ts = ts_within_enclosing_non_class;
10243 else if (is_declaration
10244 && cp_lexer_next_token_is (parser->lexer,
10245 CPP_SEMICOLON))
10246 /* This is a `class-key identifier ;' */
10247 ts = ts_current;
10248 else
10249 ts = ts_global;
10250
10251 template_p =
10252 (parser->num_template_parameter_lists
10253 && (cp_parser_next_token_starts_class_definition_p (parser)
10254 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10255 /* An unqualified name was used to reference this type, so
10256 there were no qualifying templates. */
10257 if (!cp_parser_check_template_parameters (parser,
10258 /*num_templates=*/0))
10259 return error_mark_node;
10260 type = xref_tag (tag_type, identifier, ts, template_p);
10261 }
10262 }
10263
10264 if (type == error_mark_node)
10265 return error_mark_node;
10266
10267 /* Allow attributes on forward declarations of classes. */
10268 if (attributes)
10269 {
10270 if (TREE_CODE (type) == TYPENAME_TYPE)
10271 warning (OPT_Wattributes,
10272 "attributes ignored on uninstantiated type");
10273 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10274 && ! processing_explicit_instantiation)
10275 warning (OPT_Wattributes,
10276 "attributes ignored on template instantiation");
10277 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10278 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10279 else
10280 warning (OPT_Wattributes,
10281 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10282 }
10283
10284 if (tag_type != enum_type)
10285 cp_parser_check_class_key (tag_type, type);
10286
10287 /* A "<" cannot follow an elaborated type specifier. If that
10288 happens, the user was probably trying to form a template-id. */
10289 cp_parser_check_for_invalid_template_id (parser, type);
10290
10291 return type;
10292 }
10293
10294 /* Parse an enum-specifier.
10295
10296 enum-specifier:
10297 enum identifier [opt] { enumerator-list [opt] }
10298
10299 GNU Extensions:
10300 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10301 attributes[opt]
10302
10303 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10304 if the token stream isn't an enum-specifier after all. */
10305
10306 static tree
10307 cp_parser_enum_specifier (cp_parser* parser)
10308 {
10309 tree identifier;
10310 tree type;
10311 tree attributes;
10312
10313 /* Parse tentatively so that we can back up if we don't find a
10314 enum-specifier. */
10315 cp_parser_parse_tentatively (parser);
10316
10317 /* Caller guarantees that the current token is 'enum', an identifier
10318 possibly follows, and the token after that is an opening brace.
10319 If we don't have an identifier, fabricate an anonymous name for
10320 the enumeration being defined. */
10321 cp_lexer_consume_token (parser->lexer);
10322
10323 attributes = cp_parser_attributes_opt (parser);
10324
10325 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10326 identifier = cp_parser_identifier (parser);
10327 else
10328 identifier = make_anon_name ();
10329
10330 /* Look for the `{' but don't consume it yet. */
10331 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10332 cp_parser_simulate_error (parser);
10333
10334 if (!cp_parser_parse_definitely (parser))
10335 return NULL_TREE;
10336
10337 /* Issue an error message if type-definitions are forbidden here. */
10338 cp_parser_check_type_definition (parser);
10339
10340 /* Create the new type. We do this before consuming the opening brace
10341 so the enum will be recorded as being on the line of its tag (or the
10342 'enum' keyword, if there is no tag). */
10343 type = start_enum (identifier);
10344
10345 /* Consume the opening brace. */
10346 cp_lexer_consume_token (parser->lexer);
10347
10348 if (type == error_mark_node)
10349 {
10350 cp_parser_skip_to_end_of_block_or_statement (parser);
10351 return error_mark_node;
10352 }
10353
10354 /* If the next token is not '}', then there are some enumerators. */
10355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10356 cp_parser_enumerator_list (parser, type);
10357
10358 /* Consume the final '}'. */
10359 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10360
10361 /* Look for trailing attributes to apply to this enumeration, and
10362 apply them if appropriate. */
10363 if (cp_parser_allow_gnu_extensions_p (parser))
10364 {
10365 tree trailing_attr = cp_parser_attributes_opt (parser);
10366 cplus_decl_attributes (&type,
10367 trailing_attr,
10368 (int) ATTR_FLAG_TYPE_IN_PLACE);
10369 }
10370
10371 /* Finish up the enumeration. */
10372 finish_enum (type);
10373
10374 return type;
10375 }
10376
10377 /* Parse an enumerator-list. The enumerators all have the indicated
10378 TYPE.
10379
10380 enumerator-list:
10381 enumerator-definition
10382 enumerator-list , enumerator-definition */
10383
10384 static void
10385 cp_parser_enumerator_list (cp_parser* parser, tree type)
10386 {
10387 while (true)
10388 {
10389 /* Parse an enumerator-definition. */
10390 cp_parser_enumerator_definition (parser, type);
10391
10392 /* If the next token is not a ',', we've reached the end of
10393 the list. */
10394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10395 break;
10396 /* Otherwise, consume the `,' and keep going. */
10397 cp_lexer_consume_token (parser->lexer);
10398 /* If the next token is a `}', there is a trailing comma. */
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10400 {
10401 if (pedantic && !in_system_header)
10402 pedwarn ("comma at end of enumerator list");
10403 break;
10404 }
10405 }
10406 }
10407
10408 /* Parse an enumerator-definition. The enumerator has the indicated
10409 TYPE.
10410
10411 enumerator-definition:
10412 enumerator
10413 enumerator = constant-expression
10414
10415 enumerator:
10416 identifier */
10417
10418 static void
10419 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10420 {
10421 tree identifier;
10422 tree value;
10423
10424 /* Look for the identifier. */
10425 identifier = cp_parser_identifier (parser);
10426 if (identifier == error_mark_node)
10427 return;
10428
10429 /* If the next token is an '=', then there is an explicit value. */
10430 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10431 {
10432 /* Consume the `=' token. */
10433 cp_lexer_consume_token (parser->lexer);
10434 /* Parse the value. */
10435 value = cp_parser_constant_expression (parser,
10436 /*allow_non_constant_p=*/false,
10437 NULL);
10438 }
10439 else
10440 value = NULL_TREE;
10441
10442 /* Create the enumerator. */
10443 build_enumerator (identifier, value, type);
10444 }
10445
10446 /* Parse a namespace-name.
10447
10448 namespace-name:
10449 original-namespace-name
10450 namespace-alias
10451
10452 Returns the NAMESPACE_DECL for the namespace. */
10453
10454 static tree
10455 cp_parser_namespace_name (cp_parser* parser)
10456 {
10457 tree identifier;
10458 tree namespace_decl;
10459
10460 /* Get the name of the namespace. */
10461 identifier = cp_parser_identifier (parser);
10462 if (identifier == error_mark_node)
10463 return error_mark_node;
10464
10465 /* Look up the identifier in the currently active scope. Look only
10466 for namespaces, due to:
10467
10468 [basic.lookup.udir]
10469
10470 When looking up a namespace-name in a using-directive or alias
10471 definition, only namespace names are considered.
10472
10473 And:
10474
10475 [basic.lookup.qual]
10476
10477 During the lookup of a name preceding the :: scope resolution
10478 operator, object, function, and enumerator names are ignored.
10479
10480 (Note that cp_parser_class_or_namespace_name only calls this
10481 function if the token after the name is the scope resolution
10482 operator.) */
10483 namespace_decl = cp_parser_lookup_name (parser, identifier,
10484 none_type,
10485 /*is_template=*/false,
10486 /*is_namespace=*/true,
10487 /*check_dependency=*/true,
10488 /*ambiguous_decls=*/NULL);
10489 /* If it's not a namespace, issue an error. */
10490 if (namespace_decl == error_mark_node
10491 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10492 {
10493 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10494 error ("%qD is not a namespace-name", identifier);
10495 cp_parser_error (parser, "expected namespace-name");
10496 namespace_decl = error_mark_node;
10497 }
10498
10499 return namespace_decl;
10500 }
10501
10502 /* Parse a namespace-definition.
10503
10504 namespace-definition:
10505 named-namespace-definition
10506 unnamed-namespace-definition
10507
10508 named-namespace-definition:
10509 original-namespace-definition
10510 extension-namespace-definition
10511
10512 original-namespace-definition:
10513 namespace identifier { namespace-body }
10514
10515 extension-namespace-definition:
10516 namespace original-namespace-name { namespace-body }
10517
10518 unnamed-namespace-definition:
10519 namespace { namespace-body } */
10520
10521 static void
10522 cp_parser_namespace_definition (cp_parser* parser)
10523 {
10524 tree identifier, attribs;
10525
10526 /* Look for the `namespace' keyword. */
10527 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10528
10529 /* Get the name of the namespace. We do not attempt to distinguish
10530 between an original-namespace-definition and an
10531 extension-namespace-definition at this point. The semantic
10532 analysis routines are responsible for that. */
10533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10534 identifier = cp_parser_identifier (parser);
10535 else
10536 identifier = NULL_TREE;
10537
10538 /* Parse any specified attributes. */
10539 attribs = cp_parser_attributes_opt (parser);
10540
10541 /* Look for the `{' to start the namespace. */
10542 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10543 /* Start the namespace. */
10544 push_namespace_with_attribs (identifier, attribs);
10545 /* Parse the body of the namespace. */
10546 cp_parser_namespace_body (parser);
10547 /* Finish the namespace. */
10548 pop_namespace ();
10549 /* Look for the final `}'. */
10550 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10551 }
10552
10553 /* Parse a namespace-body.
10554
10555 namespace-body:
10556 declaration-seq [opt] */
10557
10558 static void
10559 cp_parser_namespace_body (cp_parser* parser)
10560 {
10561 cp_parser_declaration_seq_opt (parser);
10562 }
10563
10564 /* Parse a namespace-alias-definition.
10565
10566 namespace-alias-definition:
10567 namespace identifier = qualified-namespace-specifier ; */
10568
10569 static void
10570 cp_parser_namespace_alias_definition (cp_parser* parser)
10571 {
10572 tree identifier;
10573 tree namespace_specifier;
10574
10575 /* Look for the `namespace' keyword. */
10576 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10577 /* Look for the identifier. */
10578 identifier = cp_parser_identifier (parser);
10579 if (identifier == error_mark_node)
10580 return;
10581 /* Look for the `=' token. */
10582 cp_parser_require (parser, CPP_EQ, "`='");
10583 /* Look for the qualified-namespace-specifier. */
10584 namespace_specifier
10585 = cp_parser_qualified_namespace_specifier (parser);
10586 /* Look for the `;' token. */
10587 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10588
10589 /* Register the alias in the symbol table. */
10590 do_namespace_alias (identifier, namespace_specifier);
10591 }
10592
10593 /* Parse a qualified-namespace-specifier.
10594
10595 qualified-namespace-specifier:
10596 :: [opt] nested-name-specifier [opt] namespace-name
10597
10598 Returns a NAMESPACE_DECL corresponding to the specified
10599 namespace. */
10600
10601 static tree
10602 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10603 {
10604 /* Look for the optional `::'. */
10605 cp_parser_global_scope_opt (parser,
10606 /*current_scope_valid_p=*/false);
10607
10608 /* Look for the optional nested-name-specifier. */
10609 cp_parser_nested_name_specifier_opt (parser,
10610 /*typename_keyword_p=*/false,
10611 /*check_dependency_p=*/true,
10612 /*type_p=*/false,
10613 /*is_declaration=*/true);
10614
10615 return cp_parser_namespace_name (parser);
10616 }
10617
10618 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10619 access declaration.
10620
10621 using-declaration:
10622 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10623 using :: unqualified-id ;
10624
10625 access-declaration:
10626 qualified-id ;
10627
10628 */
10629
10630 static bool
10631 cp_parser_using_declaration (cp_parser* parser,
10632 bool access_declaration_p)
10633 {
10634 cp_token *token;
10635 bool typename_p = false;
10636 bool global_scope_p;
10637 tree decl;
10638 tree identifier;
10639 tree qscope;
10640
10641 if (access_declaration_p)
10642 cp_parser_parse_tentatively (parser);
10643 else
10644 {
10645 /* Look for the `using' keyword. */
10646 cp_parser_require_keyword (parser, RID_USING, "`using'");
10647
10648 /* Peek at the next token. */
10649 token = cp_lexer_peek_token (parser->lexer);
10650 /* See if it's `typename'. */
10651 if (token->keyword == RID_TYPENAME)
10652 {
10653 /* Remember that we've seen it. */
10654 typename_p = true;
10655 /* Consume the `typename' token. */
10656 cp_lexer_consume_token (parser->lexer);
10657 }
10658 }
10659
10660 /* Look for the optional global scope qualification. */
10661 global_scope_p
10662 = (cp_parser_global_scope_opt (parser,
10663 /*current_scope_valid_p=*/false)
10664 != NULL_TREE);
10665
10666 /* If we saw `typename', or didn't see `::', then there must be a
10667 nested-name-specifier present. */
10668 if (typename_p || !global_scope_p)
10669 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10670 /*check_dependency_p=*/true,
10671 /*type_p=*/false,
10672 /*is_declaration=*/true);
10673 /* Otherwise, we could be in either of the two productions. In that
10674 case, treat the nested-name-specifier as optional. */
10675 else
10676 qscope = cp_parser_nested_name_specifier_opt (parser,
10677 /*typename_keyword_p=*/false,
10678 /*check_dependency_p=*/true,
10679 /*type_p=*/false,
10680 /*is_declaration=*/true);
10681 if (!qscope)
10682 qscope = global_namespace;
10683
10684 if (access_declaration_p && cp_parser_error_occurred (parser))
10685 /* Something has already gone wrong; there's no need to parse
10686 further. Since an error has occurred, the return value of
10687 cp_parser_parse_definitely will be false, as required. */
10688 return cp_parser_parse_definitely (parser);
10689
10690 /* Parse the unqualified-id. */
10691 identifier = cp_parser_unqualified_id (parser,
10692 /*template_keyword_p=*/false,
10693 /*check_dependency_p=*/true,
10694 /*declarator_p=*/true,
10695 /*optional_p=*/false);
10696
10697 if (access_declaration_p)
10698 {
10699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10700 cp_parser_simulate_error (parser);
10701 if (!cp_parser_parse_definitely (parser))
10702 return false;
10703 }
10704
10705 /* The function we call to handle a using-declaration is different
10706 depending on what scope we are in. */
10707 if (qscope == error_mark_node || identifier == error_mark_node)
10708 ;
10709 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10710 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10711 /* [namespace.udecl]
10712
10713 A using declaration shall not name a template-id. */
10714 error ("a template-id may not appear in a using-declaration");
10715 else
10716 {
10717 if (at_class_scope_p ())
10718 {
10719 /* Create the USING_DECL. */
10720 decl = do_class_using_decl (parser->scope, identifier);
10721 /* Add it to the list of members in this class. */
10722 finish_member_declaration (decl);
10723 }
10724 else
10725 {
10726 decl = cp_parser_lookup_name_simple (parser, identifier);
10727 if (decl == error_mark_node)
10728 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10729 else if (!at_namespace_scope_p ())
10730 do_local_using_decl (decl, qscope, identifier);
10731 else
10732 do_toplevel_using_decl (decl, qscope, identifier);
10733 }
10734 }
10735
10736 /* Look for the final `;'. */
10737 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10738
10739 return true;
10740 }
10741
10742 /* Parse a using-directive.
10743
10744 using-directive:
10745 using namespace :: [opt] nested-name-specifier [opt]
10746 namespace-name ; */
10747
10748 static void
10749 cp_parser_using_directive (cp_parser* parser)
10750 {
10751 tree namespace_decl;
10752 tree attribs;
10753
10754 /* Look for the `using' keyword. */
10755 cp_parser_require_keyword (parser, RID_USING, "`using'");
10756 /* And the `namespace' keyword. */
10757 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10758 /* Look for the optional `::' operator. */
10759 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10760 /* And the optional nested-name-specifier. */
10761 cp_parser_nested_name_specifier_opt (parser,
10762 /*typename_keyword_p=*/false,
10763 /*check_dependency_p=*/true,
10764 /*type_p=*/false,
10765 /*is_declaration=*/true);
10766 /* Get the namespace being used. */
10767 namespace_decl = cp_parser_namespace_name (parser);
10768 /* And any specified attributes. */
10769 attribs = cp_parser_attributes_opt (parser);
10770 /* Update the symbol table. */
10771 parse_using_directive (namespace_decl, attribs);
10772 /* Look for the final `;'. */
10773 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10774 }
10775
10776 /* Parse an asm-definition.
10777
10778 asm-definition:
10779 asm ( string-literal ) ;
10780
10781 GNU Extension:
10782
10783 asm-definition:
10784 asm volatile [opt] ( string-literal ) ;
10785 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10786 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10787 : asm-operand-list [opt] ) ;
10788 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10789 : asm-operand-list [opt]
10790 : asm-operand-list [opt] ) ; */
10791
10792 static void
10793 cp_parser_asm_definition (cp_parser* parser)
10794 {
10795 tree string;
10796 tree outputs = NULL_TREE;
10797 tree inputs = NULL_TREE;
10798 tree clobbers = NULL_TREE;
10799 tree asm_stmt;
10800 bool volatile_p = false;
10801 bool extended_p = false;
10802
10803 /* Look for the `asm' keyword. */
10804 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10805 /* See if the next token is `volatile'. */
10806 if (cp_parser_allow_gnu_extensions_p (parser)
10807 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10808 {
10809 /* Remember that we saw the `volatile' keyword. */
10810 volatile_p = true;
10811 /* Consume the token. */
10812 cp_lexer_consume_token (parser->lexer);
10813 }
10814 /* Look for the opening `('. */
10815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10816 return;
10817 /* Look for the string. */
10818 string = cp_parser_string_literal (parser, false, false);
10819 if (string == error_mark_node)
10820 {
10821 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10822 /*consume_paren=*/true);
10823 return;
10824 }
10825
10826 /* If we're allowing GNU extensions, check for the extended assembly
10827 syntax. Unfortunately, the `:' tokens need not be separated by
10828 a space in C, and so, for compatibility, we tolerate that here
10829 too. Doing that means that we have to treat the `::' operator as
10830 two `:' tokens. */
10831 if (cp_parser_allow_gnu_extensions_p (parser)
10832 && at_function_scope_p ()
10833 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10834 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10835 {
10836 bool inputs_p = false;
10837 bool clobbers_p = false;
10838
10839 /* The extended syntax was used. */
10840 extended_p = true;
10841
10842 /* Look for outputs. */
10843 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10844 {
10845 /* Consume the `:'. */
10846 cp_lexer_consume_token (parser->lexer);
10847 /* Parse the output-operands. */
10848 if (cp_lexer_next_token_is_not (parser->lexer,
10849 CPP_COLON)
10850 && cp_lexer_next_token_is_not (parser->lexer,
10851 CPP_SCOPE)
10852 && cp_lexer_next_token_is_not (parser->lexer,
10853 CPP_CLOSE_PAREN))
10854 outputs = cp_parser_asm_operand_list (parser);
10855 }
10856 /* If the next token is `::', there are no outputs, and the
10857 next token is the beginning of the inputs. */
10858 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10859 /* The inputs are coming next. */
10860 inputs_p = true;
10861
10862 /* Look for inputs. */
10863 if (inputs_p
10864 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10865 {
10866 /* Consume the `:' or `::'. */
10867 cp_lexer_consume_token (parser->lexer);
10868 /* Parse the output-operands. */
10869 if (cp_lexer_next_token_is_not (parser->lexer,
10870 CPP_COLON)
10871 && cp_lexer_next_token_is_not (parser->lexer,
10872 CPP_CLOSE_PAREN))
10873 inputs = cp_parser_asm_operand_list (parser);
10874 }
10875 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10876 /* The clobbers are coming next. */
10877 clobbers_p = true;
10878
10879 /* Look for clobbers. */
10880 if (clobbers_p
10881 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10882 {
10883 /* Consume the `:' or `::'. */
10884 cp_lexer_consume_token (parser->lexer);
10885 /* Parse the clobbers. */
10886 if (cp_lexer_next_token_is_not (parser->lexer,
10887 CPP_CLOSE_PAREN))
10888 clobbers = cp_parser_asm_clobber_list (parser);
10889 }
10890 }
10891 /* Look for the closing `)'. */
10892 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10893 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10894 /*consume_paren=*/true);
10895 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10896
10897 /* Create the ASM_EXPR. */
10898 if (at_function_scope_p ())
10899 {
10900 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10901 inputs, clobbers);
10902 /* If the extended syntax was not used, mark the ASM_EXPR. */
10903 if (!extended_p)
10904 {
10905 tree temp = asm_stmt;
10906 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10907 temp = TREE_OPERAND (temp, 0);
10908
10909 ASM_INPUT_P (temp) = 1;
10910 }
10911 }
10912 else
10913 cgraph_add_asm_node (string);
10914 }
10915
10916 /* Declarators [gram.dcl.decl] */
10917
10918 /* Parse an init-declarator.
10919
10920 init-declarator:
10921 declarator initializer [opt]
10922
10923 GNU Extension:
10924
10925 init-declarator:
10926 declarator asm-specification [opt] attributes [opt] initializer [opt]
10927
10928 function-definition:
10929 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10930 function-body
10931 decl-specifier-seq [opt] declarator function-try-block
10932
10933 GNU Extension:
10934
10935 function-definition:
10936 __extension__ function-definition
10937
10938 The DECL_SPECIFIERS apply to this declarator. Returns a
10939 representation of the entity declared. If MEMBER_P is TRUE, then
10940 this declarator appears in a class scope. The new DECL created by
10941 this declarator is returned.
10942
10943 The CHECKS are access checks that should be performed once we know
10944 what entity is being declared (and, therefore, what classes have
10945 befriended it).
10946
10947 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10948 for a function-definition here as well. If the declarator is a
10949 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10950 be TRUE upon return. By that point, the function-definition will
10951 have been completely parsed.
10952
10953 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10954 is FALSE. */
10955
10956 static tree
10957 cp_parser_init_declarator (cp_parser* parser,
10958 cp_decl_specifier_seq *decl_specifiers,
10959 tree checks,
10960 bool function_definition_allowed_p,
10961 bool member_p,
10962 int declares_class_or_enum,
10963 bool* function_definition_p)
10964 {
10965 cp_token *token;
10966 cp_declarator *declarator;
10967 tree prefix_attributes;
10968 tree attributes;
10969 tree asm_specification;
10970 tree initializer;
10971 tree decl = NULL_TREE;
10972 tree scope;
10973 bool is_initialized;
10974 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10975 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10976 "(...)". */
10977 enum cpp_ttype initialization_kind;
10978 bool is_parenthesized_init = false;
10979 bool is_non_constant_init;
10980 int ctor_dtor_or_conv_p;
10981 bool friend_p;
10982 tree pushed_scope = NULL;
10983
10984 /* Gather the attributes that were provided with the
10985 decl-specifiers. */
10986 prefix_attributes = decl_specifiers->attributes;
10987
10988 /* Assume that this is not the declarator for a function
10989 definition. */
10990 if (function_definition_p)
10991 *function_definition_p = false;
10992
10993 /* Defer access checks while parsing the declarator; we cannot know
10994 what names are accessible until we know what is being
10995 declared. */
10996 resume_deferring_access_checks ();
10997
10998 /* Parse the declarator. */
10999 declarator
11000 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11001 &ctor_dtor_or_conv_p,
11002 /*parenthesized_p=*/NULL,
11003 /*member_p=*/false);
11004 /* Gather up the deferred checks. */
11005 stop_deferring_access_checks ();
11006
11007 /* If the DECLARATOR was erroneous, there's no need to go
11008 further. */
11009 if (declarator == cp_error_declarator)
11010 return error_mark_node;
11011
11012 if (declares_class_or_enum & 2)
11013 cp_parser_check_for_definition_in_return_type (declarator,
11014 decl_specifiers->type);
11015
11016 /* Figure out what scope the entity declared by the DECLARATOR is
11017 located in. `grokdeclarator' sometimes changes the scope, so
11018 we compute it now. */
11019 scope = get_scope_of_declarator (declarator);
11020
11021 /* If we're allowing GNU extensions, look for an asm-specification
11022 and attributes. */
11023 if (cp_parser_allow_gnu_extensions_p (parser))
11024 {
11025 /* Look for an asm-specification. */
11026 asm_specification = cp_parser_asm_specification_opt (parser);
11027 /* And attributes. */
11028 attributes = cp_parser_attributes_opt (parser);
11029 }
11030 else
11031 {
11032 asm_specification = NULL_TREE;
11033 attributes = NULL_TREE;
11034 }
11035
11036 /* Peek at the next token. */
11037 token = cp_lexer_peek_token (parser->lexer);
11038 /* Check to see if the token indicates the start of a
11039 function-definition. */
11040 if (cp_parser_token_starts_function_definition_p (token))
11041 {
11042 if (!function_definition_allowed_p)
11043 {
11044 /* If a function-definition should not appear here, issue an
11045 error message. */
11046 cp_parser_error (parser,
11047 "a function-definition is not allowed here");
11048 return error_mark_node;
11049 }
11050 else
11051 {
11052 /* Neither attributes nor an asm-specification are allowed
11053 on a function-definition. */
11054 if (asm_specification)
11055 error ("an asm-specification is not allowed on a function-definition");
11056 if (attributes)
11057 error ("attributes are not allowed on a function-definition");
11058 /* This is a function-definition. */
11059 *function_definition_p = true;
11060
11061 /* Parse the function definition. */
11062 if (member_p)
11063 decl = cp_parser_save_member_function_body (parser,
11064 decl_specifiers,
11065 declarator,
11066 prefix_attributes);
11067 else
11068 decl
11069 = (cp_parser_function_definition_from_specifiers_and_declarator
11070 (parser, decl_specifiers, prefix_attributes, declarator));
11071
11072 return decl;
11073 }
11074 }
11075
11076 /* [dcl.dcl]
11077
11078 Only in function declarations for constructors, destructors, and
11079 type conversions can the decl-specifier-seq be omitted.
11080
11081 We explicitly postpone this check past the point where we handle
11082 function-definitions because we tolerate function-definitions
11083 that are missing their return types in some modes. */
11084 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11085 {
11086 cp_parser_error (parser,
11087 "expected constructor, destructor, or type conversion");
11088 return error_mark_node;
11089 }
11090
11091 /* An `=' or an `(' indicates an initializer. */
11092 if (token->type == CPP_EQ
11093 || token->type == CPP_OPEN_PAREN)
11094 {
11095 is_initialized = true;
11096 initialization_kind = token->type;
11097 }
11098 else
11099 {
11100 /* If the init-declarator isn't initialized and isn't followed by a
11101 `,' or `;', it's not a valid init-declarator. */
11102 if (token->type != CPP_COMMA
11103 && token->type != CPP_SEMICOLON)
11104 {
11105 cp_parser_error (parser, "expected initializer");
11106 return error_mark_node;
11107 }
11108 is_initialized = false;
11109 initialization_kind = CPP_EOF;
11110 }
11111
11112 /* Because start_decl has side-effects, we should only call it if we
11113 know we're going ahead. By this point, we know that we cannot
11114 possibly be looking at any other construct. */
11115 cp_parser_commit_to_tentative_parse (parser);
11116
11117 /* If the decl specifiers were bad, issue an error now that we're
11118 sure this was intended to be a declarator. Then continue
11119 declaring the variable(s), as int, to try to cut down on further
11120 errors. */
11121 if (decl_specifiers->any_specifiers_p
11122 && decl_specifiers->type == error_mark_node)
11123 {
11124 cp_parser_error (parser, "invalid type in declaration");
11125 decl_specifiers->type = integer_type_node;
11126 }
11127
11128 /* Check to see whether or not this declaration is a friend. */
11129 friend_p = cp_parser_friend_p (decl_specifiers);
11130
11131 /* Check that the number of template-parameter-lists is OK. */
11132 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11133 return error_mark_node;
11134
11135 /* Enter the newly declared entry in the symbol table. If we're
11136 processing a declaration in a class-specifier, we wait until
11137 after processing the initializer. */
11138 if (!member_p)
11139 {
11140 if (parser->in_unbraced_linkage_specification_p)
11141 decl_specifiers->storage_class = sc_extern;
11142 decl = start_decl (declarator, decl_specifiers,
11143 is_initialized, attributes, prefix_attributes,
11144 &pushed_scope);
11145 }
11146 else if (scope)
11147 /* Enter the SCOPE. That way unqualified names appearing in the
11148 initializer will be looked up in SCOPE. */
11149 pushed_scope = push_scope (scope);
11150
11151 /* Perform deferred access control checks, now that we know in which
11152 SCOPE the declared entity resides. */
11153 if (!member_p && decl)
11154 {
11155 tree saved_current_function_decl = NULL_TREE;
11156
11157 /* If the entity being declared is a function, pretend that we
11158 are in its scope. If it is a `friend', it may have access to
11159 things that would not otherwise be accessible. */
11160 if (TREE_CODE (decl) == FUNCTION_DECL)
11161 {
11162 saved_current_function_decl = current_function_decl;
11163 current_function_decl = decl;
11164 }
11165
11166 /* Perform access checks for template parameters. */
11167 cp_parser_perform_template_parameter_access_checks (checks);
11168
11169 /* Perform the access control checks for the declarator and the
11170 the decl-specifiers. */
11171 perform_deferred_access_checks ();
11172
11173 /* Restore the saved value. */
11174 if (TREE_CODE (decl) == FUNCTION_DECL)
11175 current_function_decl = saved_current_function_decl;
11176 }
11177
11178 /* Parse the initializer. */
11179 initializer = NULL_TREE;
11180 is_parenthesized_init = false;
11181 is_non_constant_init = true;
11182 if (is_initialized)
11183 {
11184 if (function_declarator_p (declarator)
11185 && initialization_kind == CPP_EQ)
11186 initializer = cp_parser_pure_specifier (parser);
11187 else
11188 initializer = cp_parser_initializer (parser,
11189 &is_parenthesized_init,
11190 &is_non_constant_init);
11191 }
11192
11193 /* The old parser allows attributes to appear after a parenthesized
11194 initializer. Mark Mitchell proposed removing this functionality
11195 on the GCC mailing lists on 2002-08-13. This parser accepts the
11196 attributes -- but ignores them. */
11197 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11198 if (cp_parser_attributes_opt (parser))
11199 warning (OPT_Wattributes,
11200 "attributes after parenthesized initializer ignored");
11201
11202 /* For an in-class declaration, use `grokfield' to create the
11203 declaration. */
11204 if (member_p)
11205 {
11206 if (pushed_scope)
11207 {
11208 pop_scope (pushed_scope);
11209 pushed_scope = false;
11210 }
11211 decl = grokfield (declarator, decl_specifiers,
11212 initializer, !is_non_constant_init,
11213 /*asmspec=*/NULL_TREE,
11214 prefix_attributes);
11215 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11216 cp_parser_save_default_args (parser, decl);
11217 }
11218
11219 /* Finish processing the declaration. But, skip friend
11220 declarations. */
11221 if (!friend_p && decl && decl != error_mark_node)
11222 {
11223 cp_finish_decl (decl,
11224 initializer, !is_non_constant_init,
11225 asm_specification,
11226 /* If the initializer is in parentheses, then this is
11227 a direct-initialization, which means that an
11228 `explicit' constructor is OK. Otherwise, an
11229 `explicit' constructor cannot be used. */
11230 ((is_parenthesized_init || !is_initialized)
11231 ? 0 : LOOKUP_ONLYCONVERTING));
11232 }
11233 if (!friend_p && pushed_scope)
11234 pop_scope (pushed_scope);
11235
11236 return decl;
11237 }
11238
11239 /* Parse a declarator.
11240
11241 declarator:
11242 direct-declarator
11243 ptr-operator declarator
11244
11245 abstract-declarator:
11246 ptr-operator abstract-declarator [opt]
11247 direct-abstract-declarator
11248
11249 GNU Extensions:
11250
11251 declarator:
11252 attributes [opt] direct-declarator
11253 attributes [opt] ptr-operator declarator
11254
11255 abstract-declarator:
11256 attributes [opt] ptr-operator abstract-declarator [opt]
11257 attributes [opt] direct-abstract-declarator
11258
11259 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11260 detect constructor, destructor or conversion operators. It is set
11261 to -1 if the declarator is a name, and +1 if it is a
11262 function. Otherwise it is set to zero. Usually you just want to
11263 test for >0, but internally the negative value is used.
11264
11265 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11266 a decl-specifier-seq unless it declares a constructor, destructor,
11267 or conversion. It might seem that we could check this condition in
11268 semantic analysis, rather than parsing, but that makes it difficult
11269 to handle something like `f()'. We want to notice that there are
11270 no decl-specifiers, and therefore realize that this is an
11271 expression, not a declaration.)
11272
11273 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11274 the declarator is a direct-declarator of the form "(...)".
11275
11276 MEMBER_P is true iff this declarator is a member-declarator. */
11277
11278 static cp_declarator *
11279 cp_parser_declarator (cp_parser* parser,
11280 cp_parser_declarator_kind dcl_kind,
11281 int* ctor_dtor_or_conv_p,
11282 bool* parenthesized_p,
11283 bool member_p)
11284 {
11285 cp_token *token;
11286 cp_declarator *declarator;
11287 enum tree_code code;
11288 cp_cv_quals cv_quals;
11289 tree class_type;
11290 tree attributes = NULL_TREE;
11291
11292 /* Assume this is not a constructor, destructor, or type-conversion
11293 operator. */
11294 if (ctor_dtor_or_conv_p)
11295 *ctor_dtor_or_conv_p = 0;
11296
11297 if (cp_parser_allow_gnu_extensions_p (parser))
11298 attributes = cp_parser_attributes_opt (parser);
11299
11300 /* Peek at the next token. */
11301 token = cp_lexer_peek_token (parser->lexer);
11302
11303 /* Check for the ptr-operator production. */
11304 cp_parser_parse_tentatively (parser);
11305 /* Parse the ptr-operator. */
11306 code = cp_parser_ptr_operator (parser,
11307 &class_type,
11308 &cv_quals);
11309 /* If that worked, then we have a ptr-operator. */
11310 if (cp_parser_parse_definitely (parser))
11311 {
11312 /* If a ptr-operator was found, then this declarator was not
11313 parenthesized. */
11314 if (parenthesized_p)
11315 *parenthesized_p = true;
11316 /* The dependent declarator is optional if we are parsing an
11317 abstract-declarator. */
11318 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11319 cp_parser_parse_tentatively (parser);
11320
11321 /* Parse the dependent declarator. */
11322 declarator = cp_parser_declarator (parser, dcl_kind,
11323 /*ctor_dtor_or_conv_p=*/NULL,
11324 /*parenthesized_p=*/NULL,
11325 /*member_p=*/false);
11326
11327 /* If we are parsing an abstract-declarator, we must handle the
11328 case where the dependent declarator is absent. */
11329 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11330 && !cp_parser_parse_definitely (parser))
11331 declarator = NULL;
11332
11333 /* Build the representation of the ptr-operator. */
11334 if (class_type)
11335 declarator = make_ptrmem_declarator (cv_quals,
11336 class_type,
11337 declarator);
11338 else if (code == INDIRECT_REF)
11339 declarator = make_pointer_declarator (cv_quals, declarator);
11340 else
11341 declarator = make_reference_declarator (cv_quals, declarator);
11342 }
11343 /* Everything else is a direct-declarator. */
11344 else
11345 {
11346 if (parenthesized_p)
11347 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11348 CPP_OPEN_PAREN);
11349 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11350 ctor_dtor_or_conv_p,
11351 member_p);
11352 }
11353
11354 if (attributes && declarator && declarator != cp_error_declarator)
11355 declarator->attributes = attributes;
11356
11357 return declarator;
11358 }
11359
11360 /* Parse a direct-declarator or direct-abstract-declarator.
11361
11362 direct-declarator:
11363 declarator-id
11364 direct-declarator ( parameter-declaration-clause )
11365 cv-qualifier-seq [opt]
11366 exception-specification [opt]
11367 direct-declarator [ constant-expression [opt] ]
11368 ( declarator )
11369
11370 direct-abstract-declarator:
11371 direct-abstract-declarator [opt]
11372 ( parameter-declaration-clause )
11373 cv-qualifier-seq [opt]
11374 exception-specification [opt]
11375 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11376 ( abstract-declarator )
11377
11378 Returns a representation of the declarator. DCL_KIND is
11379 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11380 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11381 we are parsing a direct-declarator. It is
11382 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11383 of ambiguity we prefer an abstract declarator, as per
11384 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11385 cp_parser_declarator. */
11386
11387 static cp_declarator *
11388 cp_parser_direct_declarator (cp_parser* parser,
11389 cp_parser_declarator_kind dcl_kind,
11390 int* ctor_dtor_or_conv_p,
11391 bool member_p)
11392 {
11393 cp_token *token;
11394 cp_declarator *declarator = NULL;
11395 tree scope = NULL_TREE;
11396 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11397 bool saved_in_declarator_p = parser->in_declarator_p;
11398 bool first = true;
11399 tree pushed_scope = NULL_TREE;
11400
11401 while (true)
11402 {
11403 /* Peek at the next token. */
11404 token = cp_lexer_peek_token (parser->lexer);
11405 if (token->type == CPP_OPEN_PAREN)
11406 {
11407 /* This is either a parameter-declaration-clause, or a
11408 parenthesized declarator. When we know we are parsing a
11409 named declarator, it must be a parenthesized declarator
11410 if FIRST is true. For instance, `(int)' is a
11411 parameter-declaration-clause, with an omitted
11412 direct-abstract-declarator. But `((*))', is a
11413 parenthesized abstract declarator. Finally, when T is a
11414 template parameter `(T)' is a
11415 parameter-declaration-clause, and not a parenthesized
11416 named declarator.
11417
11418 We first try and parse a parameter-declaration-clause,
11419 and then try a nested declarator (if FIRST is true).
11420
11421 It is not an error for it not to be a
11422 parameter-declaration-clause, even when FIRST is
11423 false. Consider,
11424
11425 int i (int);
11426 int i (3);
11427
11428 The first is the declaration of a function while the
11429 second is a the definition of a variable, including its
11430 initializer.
11431
11432 Having seen only the parenthesis, we cannot know which of
11433 these two alternatives should be selected. Even more
11434 complex are examples like:
11435
11436 int i (int (a));
11437 int i (int (3));
11438
11439 The former is a function-declaration; the latter is a
11440 variable initialization.
11441
11442 Thus again, we try a parameter-declaration-clause, and if
11443 that fails, we back out and return. */
11444
11445 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11446 {
11447 cp_parameter_declarator *params;
11448 unsigned saved_num_template_parameter_lists;
11449
11450 /* In a member-declarator, the only valid interpretation
11451 of a parenthesis is the start of a
11452 parameter-declaration-clause. (It is invalid to
11453 initialize a static data member with a parenthesized
11454 initializer; only the "=" form of initialization is
11455 permitted.) */
11456 if (!member_p)
11457 cp_parser_parse_tentatively (parser);
11458
11459 /* Consume the `('. */
11460 cp_lexer_consume_token (parser->lexer);
11461 if (first)
11462 {
11463 /* If this is going to be an abstract declarator, we're
11464 in a declarator and we can't have default args. */
11465 parser->default_arg_ok_p = false;
11466 parser->in_declarator_p = true;
11467 }
11468
11469 /* Inside the function parameter list, surrounding
11470 template-parameter-lists do not apply. */
11471 saved_num_template_parameter_lists
11472 = parser->num_template_parameter_lists;
11473 parser->num_template_parameter_lists = 0;
11474
11475 /* Parse the parameter-declaration-clause. */
11476 params = cp_parser_parameter_declaration_clause (parser);
11477
11478 parser->num_template_parameter_lists
11479 = saved_num_template_parameter_lists;
11480
11481 /* If all went well, parse the cv-qualifier-seq and the
11482 exception-specification. */
11483 if (member_p || cp_parser_parse_definitely (parser))
11484 {
11485 cp_cv_quals cv_quals;
11486 tree exception_specification;
11487
11488 if (ctor_dtor_or_conv_p)
11489 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11490 first = false;
11491 /* Consume the `)'. */
11492 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11493
11494 /* Parse the cv-qualifier-seq. */
11495 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11496 /* And the exception-specification. */
11497 exception_specification
11498 = cp_parser_exception_specification_opt (parser);
11499
11500 /* Create the function-declarator. */
11501 declarator = make_call_declarator (declarator,
11502 params,
11503 cv_quals,
11504 exception_specification);
11505 /* Any subsequent parameter lists are to do with
11506 return type, so are not those of the declared
11507 function. */
11508 parser->default_arg_ok_p = false;
11509
11510 /* Repeat the main loop. */
11511 continue;
11512 }
11513 }
11514
11515 /* If this is the first, we can try a parenthesized
11516 declarator. */
11517 if (first)
11518 {
11519 bool saved_in_type_id_in_expr_p;
11520
11521 parser->default_arg_ok_p = saved_default_arg_ok_p;
11522 parser->in_declarator_p = saved_in_declarator_p;
11523
11524 /* Consume the `('. */
11525 cp_lexer_consume_token (parser->lexer);
11526 /* Parse the nested declarator. */
11527 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11528 parser->in_type_id_in_expr_p = true;
11529 declarator
11530 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11531 /*parenthesized_p=*/NULL,
11532 member_p);
11533 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11534 first = false;
11535 /* Expect a `)'. */
11536 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11537 declarator = cp_error_declarator;
11538 if (declarator == cp_error_declarator)
11539 break;
11540
11541 goto handle_declarator;
11542 }
11543 /* Otherwise, we must be done. */
11544 else
11545 break;
11546 }
11547 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11548 && token->type == CPP_OPEN_SQUARE)
11549 {
11550 /* Parse an array-declarator. */
11551 tree bounds;
11552
11553 if (ctor_dtor_or_conv_p)
11554 *ctor_dtor_or_conv_p = 0;
11555
11556 first = false;
11557 parser->default_arg_ok_p = false;
11558 parser->in_declarator_p = true;
11559 /* Consume the `['. */
11560 cp_lexer_consume_token (parser->lexer);
11561 /* Peek at the next token. */
11562 token = cp_lexer_peek_token (parser->lexer);
11563 /* If the next token is `]', then there is no
11564 constant-expression. */
11565 if (token->type != CPP_CLOSE_SQUARE)
11566 {
11567 bool non_constant_p;
11568
11569 bounds
11570 = cp_parser_constant_expression (parser,
11571 /*allow_non_constant=*/true,
11572 &non_constant_p);
11573 if (!non_constant_p)
11574 bounds = fold_non_dependent_expr (bounds);
11575 /* Normally, the array bound must be an integral constant
11576 expression. However, as an extension, we allow VLAs
11577 in function scopes. */
11578 else if (!at_function_scope_p ())
11579 {
11580 error ("array bound is not an integer constant");
11581 bounds = error_mark_node;
11582 }
11583 }
11584 else
11585 bounds = NULL_TREE;
11586 /* Look for the closing `]'. */
11587 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11588 {
11589 declarator = cp_error_declarator;
11590 break;
11591 }
11592
11593 declarator = make_array_declarator (declarator, bounds);
11594 }
11595 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11596 {
11597 tree qualifying_scope;
11598 tree unqualified_name;
11599 special_function_kind sfk;
11600 bool abstract_ok;
11601
11602 /* Parse a declarator-id */
11603 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11604 if (abstract_ok)
11605 cp_parser_parse_tentatively (parser);
11606 unqualified_name
11607 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11608 qualifying_scope = parser->scope;
11609 if (abstract_ok)
11610 {
11611 if (!cp_parser_parse_definitely (parser))
11612 unqualified_name = error_mark_node;
11613 else if (unqualified_name
11614 && (qualifying_scope
11615 || (TREE_CODE (unqualified_name)
11616 != IDENTIFIER_NODE)))
11617 {
11618 cp_parser_error (parser, "expected unqualified-id");
11619 unqualified_name = error_mark_node;
11620 }
11621 }
11622
11623 if (!unqualified_name)
11624 return NULL;
11625 if (unqualified_name == error_mark_node)
11626 {
11627 declarator = cp_error_declarator;
11628 break;
11629 }
11630
11631 if (qualifying_scope && at_namespace_scope_p ()
11632 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11633 {
11634 /* In the declaration of a member of a template class
11635 outside of the class itself, the SCOPE will sometimes
11636 be a TYPENAME_TYPE. For example, given:
11637
11638 template <typename T>
11639 int S<T>::R::i = 3;
11640
11641 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11642 this context, we must resolve S<T>::R to an ordinary
11643 type, rather than a typename type.
11644
11645 The reason we normally avoid resolving TYPENAME_TYPEs
11646 is that a specialization of `S' might render
11647 `S<T>::R' not a type. However, if `S' is
11648 specialized, then this `i' will not be used, so there
11649 is no harm in resolving the types here. */
11650 tree type;
11651
11652 /* Resolve the TYPENAME_TYPE. */
11653 type = resolve_typename_type (qualifying_scope,
11654 /*only_current_p=*/false);
11655 /* If that failed, the declarator is invalid. */
11656 if (type == error_mark_node)
11657 error ("%<%T::%D%> is not a type",
11658 TYPE_CONTEXT (qualifying_scope),
11659 TYPE_IDENTIFIER (qualifying_scope));
11660 qualifying_scope = type;
11661 }
11662
11663 sfk = sfk_none;
11664 if (unqualified_name)
11665 {
11666 tree class_type;
11667
11668 if (qualifying_scope
11669 && CLASS_TYPE_P (qualifying_scope))
11670 class_type = qualifying_scope;
11671 else
11672 class_type = current_class_type;
11673
11674 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11675 {
11676 tree name_type = TREE_TYPE (unqualified_name);
11677 if (class_type && same_type_p (name_type, class_type))
11678 {
11679 if (qualifying_scope
11680 && CLASSTYPE_USE_TEMPLATE (name_type))
11681 {
11682 error ("invalid use of constructor as a template");
11683 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11684 "name the constructor in a qualified name",
11685 class_type,
11686 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11687 class_type, name_type);
11688 declarator = cp_error_declarator;
11689 break;
11690 }
11691 else
11692 unqualified_name = constructor_name (class_type);
11693 }
11694 else
11695 {
11696 /* We do not attempt to print the declarator
11697 here because we do not have enough
11698 information about its original syntactic
11699 form. */
11700 cp_parser_error (parser, "invalid declarator");
11701 declarator = cp_error_declarator;
11702 break;
11703 }
11704 }
11705
11706 if (class_type)
11707 {
11708 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11709 sfk = sfk_destructor;
11710 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11711 sfk = sfk_conversion;
11712 else if (/* There's no way to declare a constructor
11713 for an anonymous type, even if the type
11714 got a name for linkage purposes. */
11715 !TYPE_WAS_ANONYMOUS (class_type)
11716 && constructor_name_p (unqualified_name,
11717 class_type))
11718 {
11719 unqualified_name = constructor_name (class_type);
11720 sfk = sfk_constructor;
11721 }
11722
11723 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11724 *ctor_dtor_or_conv_p = -1;
11725 }
11726 }
11727 declarator = make_id_declarator (qualifying_scope,
11728 unqualified_name,
11729 sfk);
11730 declarator->id_loc = token->location;
11731
11732 handle_declarator:;
11733 scope = get_scope_of_declarator (declarator);
11734 if (scope)
11735 /* Any names that appear after the declarator-id for a
11736 member are looked up in the containing scope. */
11737 pushed_scope = push_scope (scope);
11738 parser->in_declarator_p = true;
11739 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11740 || (declarator && declarator->kind == cdk_id))
11741 /* Default args are only allowed on function
11742 declarations. */
11743 parser->default_arg_ok_p = saved_default_arg_ok_p;
11744 else
11745 parser->default_arg_ok_p = false;
11746
11747 first = false;
11748 }
11749 /* We're done. */
11750 else
11751 break;
11752 }
11753
11754 /* For an abstract declarator, we might wind up with nothing at this
11755 point. That's an error; the declarator is not optional. */
11756 if (!declarator)
11757 cp_parser_error (parser, "expected declarator");
11758
11759 /* If we entered a scope, we must exit it now. */
11760 if (pushed_scope)
11761 pop_scope (pushed_scope);
11762
11763 parser->default_arg_ok_p = saved_default_arg_ok_p;
11764 parser->in_declarator_p = saved_in_declarator_p;
11765
11766 return declarator;
11767 }
11768
11769 /* Parse a ptr-operator.
11770
11771 ptr-operator:
11772 * cv-qualifier-seq [opt]
11773 &
11774 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11775
11776 GNU Extension:
11777
11778 ptr-operator:
11779 & cv-qualifier-seq [opt]
11780
11781 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11782 Returns ADDR_EXPR if a reference was used. In the case of a
11783 pointer-to-member, *TYPE is filled in with the TYPE containing the
11784 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11785 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11786 ERROR_MARK if an error occurred. */
11787
11788 static enum tree_code
11789 cp_parser_ptr_operator (cp_parser* parser,
11790 tree* type,
11791 cp_cv_quals *cv_quals)
11792 {
11793 enum tree_code code = ERROR_MARK;
11794 cp_token *token;
11795
11796 /* Assume that it's not a pointer-to-member. */
11797 *type = NULL_TREE;
11798 /* And that there are no cv-qualifiers. */
11799 *cv_quals = TYPE_UNQUALIFIED;
11800
11801 /* Peek at the next token. */
11802 token = cp_lexer_peek_token (parser->lexer);
11803 /* If it's a `*' or `&' we have a pointer or reference. */
11804 if (token->type == CPP_MULT || token->type == CPP_AND)
11805 {
11806 /* Remember which ptr-operator we were processing. */
11807 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11808
11809 /* Consume the `*' or `&'. */
11810 cp_lexer_consume_token (parser->lexer);
11811
11812 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11813 `&', if we are allowing GNU extensions. (The only qualifier
11814 that can legally appear after `&' is `restrict', but that is
11815 enforced during semantic analysis. */
11816 if (code == INDIRECT_REF
11817 || cp_parser_allow_gnu_extensions_p (parser))
11818 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11819 }
11820 else
11821 {
11822 /* Try the pointer-to-member case. */
11823 cp_parser_parse_tentatively (parser);
11824 /* Look for the optional `::' operator. */
11825 cp_parser_global_scope_opt (parser,
11826 /*current_scope_valid_p=*/false);
11827 /* Look for the nested-name specifier. */
11828 cp_parser_nested_name_specifier (parser,
11829 /*typename_keyword_p=*/false,
11830 /*check_dependency_p=*/true,
11831 /*type_p=*/false,
11832 /*is_declaration=*/false);
11833 /* If we found it, and the next token is a `*', then we are
11834 indeed looking at a pointer-to-member operator. */
11835 if (!cp_parser_error_occurred (parser)
11836 && cp_parser_require (parser, CPP_MULT, "`*'"))
11837 {
11838 /* Indicate that the `*' operator was used. */
11839 code = INDIRECT_REF;
11840
11841 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11842 error ("%qD is a namespace", parser->scope);
11843 else
11844 {
11845 /* The type of which the member is a member is given by the
11846 current SCOPE. */
11847 *type = parser->scope;
11848 /* The next name will not be qualified. */
11849 parser->scope = NULL_TREE;
11850 parser->qualifying_scope = NULL_TREE;
11851 parser->object_scope = NULL_TREE;
11852 /* Look for the optional cv-qualifier-seq. */
11853 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11854 }
11855 }
11856 /* If that didn't work we don't have a ptr-operator. */
11857 if (!cp_parser_parse_definitely (parser))
11858 cp_parser_error (parser, "expected ptr-operator");
11859 }
11860
11861 return code;
11862 }
11863
11864 /* Parse an (optional) cv-qualifier-seq.
11865
11866 cv-qualifier-seq:
11867 cv-qualifier cv-qualifier-seq [opt]
11868
11869 cv-qualifier:
11870 const
11871 volatile
11872
11873 GNU Extension:
11874
11875 cv-qualifier:
11876 __restrict__
11877
11878 Returns a bitmask representing the cv-qualifiers. */
11879
11880 static cp_cv_quals
11881 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11882 {
11883 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11884
11885 while (true)
11886 {
11887 cp_token *token;
11888 cp_cv_quals cv_qualifier;
11889
11890 /* Peek at the next token. */
11891 token = cp_lexer_peek_token (parser->lexer);
11892 /* See if it's a cv-qualifier. */
11893 switch (token->keyword)
11894 {
11895 case RID_CONST:
11896 cv_qualifier = TYPE_QUAL_CONST;
11897 break;
11898
11899 case RID_VOLATILE:
11900 cv_qualifier = TYPE_QUAL_VOLATILE;
11901 break;
11902
11903 case RID_RESTRICT:
11904 cv_qualifier = TYPE_QUAL_RESTRICT;
11905 break;
11906
11907 default:
11908 cv_qualifier = TYPE_UNQUALIFIED;
11909 break;
11910 }
11911
11912 if (!cv_qualifier)
11913 break;
11914
11915 if (cv_quals & cv_qualifier)
11916 {
11917 error ("duplicate cv-qualifier");
11918 cp_lexer_purge_token (parser->lexer);
11919 }
11920 else
11921 {
11922 cp_lexer_consume_token (parser->lexer);
11923 cv_quals |= cv_qualifier;
11924 }
11925 }
11926
11927 return cv_quals;
11928 }
11929
11930 /* Parse a declarator-id.
11931
11932 declarator-id:
11933 id-expression
11934 :: [opt] nested-name-specifier [opt] type-name
11935
11936 In the `id-expression' case, the value returned is as for
11937 cp_parser_id_expression if the id-expression was an unqualified-id.
11938 If the id-expression was a qualified-id, then a SCOPE_REF is
11939 returned. The first operand is the scope (either a NAMESPACE_DECL
11940 or TREE_TYPE), but the second is still just a representation of an
11941 unqualified-id. */
11942
11943 static tree
11944 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11945 {
11946 tree id;
11947 /* The expression must be an id-expression. Assume that qualified
11948 names are the names of types so that:
11949
11950 template <class T>
11951 int S<T>::R::i = 3;
11952
11953 will work; we must treat `S<T>::R' as the name of a type.
11954 Similarly, assume that qualified names are templates, where
11955 required, so that:
11956
11957 template <class T>
11958 int S<T>::R<T>::i = 3;
11959
11960 will work, too. */
11961 id = cp_parser_id_expression (parser,
11962 /*template_keyword_p=*/false,
11963 /*check_dependency_p=*/false,
11964 /*template_p=*/NULL,
11965 /*declarator_p=*/true,
11966 optional_p);
11967 if (id && BASELINK_P (id))
11968 id = BASELINK_FUNCTIONS (id);
11969 return id;
11970 }
11971
11972 /* Parse a type-id.
11973
11974 type-id:
11975 type-specifier-seq abstract-declarator [opt]
11976
11977 Returns the TYPE specified. */
11978
11979 static tree
11980 cp_parser_type_id (cp_parser* parser)
11981 {
11982 cp_decl_specifier_seq type_specifier_seq;
11983 cp_declarator *abstract_declarator;
11984
11985 /* Parse the type-specifier-seq. */
11986 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11987 &type_specifier_seq);
11988 if (type_specifier_seq.type == error_mark_node)
11989 return error_mark_node;
11990
11991 /* There might or might not be an abstract declarator. */
11992 cp_parser_parse_tentatively (parser);
11993 /* Look for the declarator. */
11994 abstract_declarator
11995 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11996 /*parenthesized_p=*/NULL,
11997 /*member_p=*/false);
11998 /* Check to see if there really was a declarator. */
11999 if (!cp_parser_parse_definitely (parser))
12000 abstract_declarator = NULL;
12001
12002 return groktypename (&type_specifier_seq, abstract_declarator);
12003 }
12004
12005 /* Parse a type-specifier-seq.
12006
12007 type-specifier-seq:
12008 type-specifier type-specifier-seq [opt]
12009
12010 GNU extension:
12011
12012 type-specifier-seq:
12013 attributes type-specifier-seq [opt]
12014
12015 If IS_CONDITION is true, we are at the start of a "condition",
12016 e.g., we've just seen "if (".
12017
12018 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12019
12020 static void
12021 cp_parser_type_specifier_seq (cp_parser* parser,
12022 bool is_condition,
12023 cp_decl_specifier_seq *type_specifier_seq)
12024 {
12025 bool seen_type_specifier = false;
12026 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12027
12028 /* Clear the TYPE_SPECIFIER_SEQ. */
12029 clear_decl_specs (type_specifier_seq);
12030
12031 /* Parse the type-specifiers and attributes. */
12032 while (true)
12033 {
12034 tree type_specifier;
12035 bool is_cv_qualifier;
12036
12037 /* Check for attributes first. */
12038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12039 {
12040 type_specifier_seq->attributes =
12041 chainon (type_specifier_seq->attributes,
12042 cp_parser_attributes_opt (parser));
12043 continue;
12044 }
12045
12046 /* Look for the type-specifier. */
12047 type_specifier = cp_parser_type_specifier (parser,
12048 flags,
12049 type_specifier_seq,
12050 /*is_declaration=*/false,
12051 NULL,
12052 &is_cv_qualifier);
12053 if (!type_specifier)
12054 {
12055 /* If the first type-specifier could not be found, this is not a
12056 type-specifier-seq at all. */
12057 if (!seen_type_specifier)
12058 {
12059 cp_parser_error (parser, "expected type-specifier");
12060 type_specifier_seq->type = error_mark_node;
12061 return;
12062 }
12063 /* If subsequent type-specifiers could not be found, the
12064 type-specifier-seq is complete. */
12065 break;
12066 }
12067
12068 seen_type_specifier = true;
12069 /* The standard says that a condition can be:
12070
12071 type-specifier-seq declarator = assignment-expression
12072
12073 However, given:
12074
12075 struct S {};
12076 if (int S = ...)
12077
12078 we should treat the "S" as a declarator, not as a
12079 type-specifier. The standard doesn't say that explicitly for
12080 type-specifier-seq, but it does say that for
12081 decl-specifier-seq in an ordinary declaration. Perhaps it
12082 would be clearer just to allow a decl-specifier-seq here, and
12083 then add a semantic restriction that if any decl-specifiers
12084 that are not type-specifiers appear, the program is invalid. */
12085 if (is_condition && !is_cv_qualifier)
12086 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12087 }
12088
12089 cp_parser_check_decl_spec (type_specifier_seq);
12090 }
12091
12092 /* Parse a parameter-declaration-clause.
12093
12094 parameter-declaration-clause:
12095 parameter-declaration-list [opt] ... [opt]
12096 parameter-declaration-list , ...
12097
12098 Returns a representation for the parameter declarations. A return
12099 value of NULL indicates a parameter-declaration-clause consisting
12100 only of an ellipsis. */
12101
12102 static cp_parameter_declarator *
12103 cp_parser_parameter_declaration_clause (cp_parser* parser)
12104 {
12105 cp_parameter_declarator *parameters;
12106 cp_token *token;
12107 bool ellipsis_p;
12108 bool is_error;
12109
12110 /* Peek at the next token. */
12111 token = cp_lexer_peek_token (parser->lexer);
12112 /* Check for trivial parameter-declaration-clauses. */
12113 if (token->type == CPP_ELLIPSIS)
12114 {
12115 /* Consume the `...' token. */
12116 cp_lexer_consume_token (parser->lexer);
12117 return NULL;
12118 }
12119 else if (token->type == CPP_CLOSE_PAREN)
12120 /* There are no parameters. */
12121 {
12122 #ifndef NO_IMPLICIT_EXTERN_C
12123 if (in_system_header && current_class_type == NULL
12124 && current_lang_name == lang_name_c)
12125 return NULL;
12126 else
12127 #endif
12128 return no_parameters;
12129 }
12130 /* Check for `(void)', too, which is a special case. */
12131 else if (token->keyword == RID_VOID
12132 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12133 == CPP_CLOSE_PAREN))
12134 {
12135 /* Consume the `void' token. */
12136 cp_lexer_consume_token (parser->lexer);
12137 /* There are no parameters. */
12138 return no_parameters;
12139 }
12140
12141 /* Parse the parameter-declaration-list. */
12142 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12143 /* If a parse error occurred while parsing the
12144 parameter-declaration-list, then the entire
12145 parameter-declaration-clause is erroneous. */
12146 if (is_error)
12147 return NULL;
12148
12149 /* Peek at the next token. */
12150 token = cp_lexer_peek_token (parser->lexer);
12151 /* If it's a `,', the clause should terminate with an ellipsis. */
12152 if (token->type == CPP_COMMA)
12153 {
12154 /* Consume the `,'. */
12155 cp_lexer_consume_token (parser->lexer);
12156 /* Expect an ellipsis. */
12157 ellipsis_p
12158 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12159 }
12160 /* It might also be `...' if the optional trailing `,' was
12161 omitted. */
12162 else if (token->type == CPP_ELLIPSIS)
12163 {
12164 /* Consume the `...' token. */
12165 cp_lexer_consume_token (parser->lexer);
12166 /* And remember that we saw it. */
12167 ellipsis_p = true;
12168 }
12169 else
12170 ellipsis_p = false;
12171
12172 /* Finish the parameter list. */
12173 if (parameters && ellipsis_p)
12174 parameters->ellipsis_p = true;
12175
12176 return parameters;
12177 }
12178
12179 /* Parse a parameter-declaration-list.
12180
12181 parameter-declaration-list:
12182 parameter-declaration
12183 parameter-declaration-list , parameter-declaration
12184
12185 Returns a representation of the parameter-declaration-list, as for
12186 cp_parser_parameter_declaration_clause. However, the
12187 `void_list_node' is never appended to the list. Upon return,
12188 *IS_ERROR will be true iff an error occurred. */
12189
12190 static cp_parameter_declarator *
12191 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12192 {
12193 cp_parameter_declarator *parameters = NULL;
12194 cp_parameter_declarator **tail = &parameters;
12195 bool saved_in_unbraced_linkage_specification_p;
12196
12197 /* Assume all will go well. */
12198 *is_error = false;
12199 /* The special considerations that apply to a function within an
12200 unbraced linkage specifications do not apply to the parameters
12201 to the function. */
12202 saved_in_unbraced_linkage_specification_p
12203 = parser->in_unbraced_linkage_specification_p;
12204 parser->in_unbraced_linkage_specification_p = false;
12205
12206 /* Look for more parameters. */
12207 while (true)
12208 {
12209 cp_parameter_declarator *parameter;
12210 bool parenthesized_p;
12211 /* Parse the parameter. */
12212 parameter
12213 = cp_parser_parameter_declaration (parser,
12214 /*template_parm_p=*/false,
12215 &parenthesized_p);
12216
12217 /* If a parse error occurred parsing the parameter declaration,
12218 then the entire parameter-declaration-list is erroneous. */
12219 if (!parameter)
12220 {
12221 *is_error = true;
12222 parameters = NULL;
12223 break;
12224 }
12225 /* Add the new parameter to the list. */
12226 *tail = parameter;
12227 tail = &parameter->next;
12228
12229 /* Peek at the next token. */
12230 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12231 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12232 /* These are for Objective-C++ */
12233 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12234 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12235 /* The parameter-declaration-list is complete. */
12236 break;
12237 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12238 {
12239 cp_token *token;
12240
12241 /* Peek at the next token. */
12242 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12243 /* If it's an ellipsis, then the list is complete. */
12244 if (token->type == CPP_ELLIPSIS)
12245 break;
12246 /* Otherwise, there must be more parameters. Consume the
12247 `,'. */
12248 cp_lexer_consume_token (parser->lexer);
12249 /* When parsing something like:
12250
12251 int i(float f, double d)
12252
12253 we can tell after seeing the declaration for "f" that we
12254 are not looking at an initialization of a variable "i",
12255 but rather at the declaration of a function "i".
12256
12257 Due to the fact that the parsing of template arguments
12258 (as specified to a template-id) requires backtracking we
12259 cannot use this technique when inside a template argument
12260 list. */
12261 if (!parser->in_template_argument_list_p
12262 && !parser->in_type_id_in_expr_p
12263 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12264 /* However, a parameter-declaration of the form
12265 "foat(f)" (which is a valid declaration of a
12266 parameter "f") can also be interpreted as an
12267 expression (the conversion of "f" to "float"). */
12268 && !parenthesized_p)
12269 cp_parser_commit_to_tentative_parse (parser);
12270 }
12271 else
12272 {
12273 cp_parser_error (parser, "expected %<,%> or %<...%>");
12274 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12275 cp_parser_skip_to_closing_parenthesis (parser,
12276 /*recovering=*/true,
12277 /*or_comma=*/false,
12278 /*consume_paren=*/false);
12279 break;
12280 }
12281 }
12282
12283 parser->in_unbraced_linkage_specification_p
12284 = saved_in_unbraced_linkage_specification_p;
12285
12286 return parameters;
12287 }
12288
12289 /* Parse a parameter declaration.
12290
12291 parameter-declaration:
12292 decl-specifier-seq declarator
12293 decl-specifier-seq declarator = assignment-expression
12294 decl-specifier-seq abstract-declarator [opt]
12295 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12296
12297 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12298 declares a template parameter. (In that case, a non-nested `>'
12299 token encountered during the parsing of the assignment-expression
12300 is not interpreted as a greater-than operator.)
12301
12302 Returns a representation of the parameter, or NULL if an error
12303 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12304 true iff the declarator is of the form "(p)". */
12305
12306 static cp_parameter_declarator *
12307 cp_parser_parameter_declaration (cp_parser *parser,
12308 bool template_parm_p,
12309 bool *parenthesized_p)
12310 {
12311 int declares_class_or_enum;
12312 bool greater_than_is_operator_p;
12313 cp_decl_specifier_seq decl_specifiers;
12314 cp_declarator *declarator;
12315 tree default_argument;
12316 cp_token *token;
12317 const char *saved_message;
12318
12319 /* In a template parameter, `>' is not an operator.
12320
12321 [temp.param]
12322
12323 When parsing a default template-argument for a non-type
12324 template-parameter, the first non-nested `>' is taken as the end
12325 of the template parameter-list rather than a greater-than
12326 operator. */
12327 greater_than_is_operator_p = !template_parm_p;
12328
12329 /* Type definitions may not appear in parameter types. */
12330 saved_message = parser->type_definition_forbidden_message;
12331 parser->type_definition_forbidden_message
12332 = "types may not be defined in parameter types";
12333
12334 /* Parse the declaration-specifiers. */
12335 cp_parser_decl_specifier_seq (parser,
12336 CP_PARSER_FLAGS_NONE,
12337 &decl_specifiers,
12338 &declares_class_or_enum);
12339 /* If an error occurred, there's no reason to attempt to parse the
12340 rest of the declaration. */
12341 if (cp_parser_error_occurred (parser))
12342 {
12343 parser->type_definition_forbidden_message = saved_message;
12344 return NULL;
12345 }
12346
12347 /* Peek at the next token. */
12348 token = cp_lexer_peek_token (parser->lexer);
12349 /* If the next token is a `)', `,', `=', `>', or `...', then there
12350 is no declarator. */
12351 if (token->type == CPP_CLOSE_PAREN
12352 || token->type == CPP_COMMA
12353 || token->type == CPP_EQ
12354 || token->type == CPP_ELLIPSIS
12355 || token->type == CPP_GREATER)
12356 {
12357 declarator = NULL;
12358 if (parenthesized_p)
12359 *parenthesized_p = false;
12360 }
12361 /* Otherwise, there should be a declarator. */
12362 else
12363 {
12364 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12365 parser->default_arg_ok_p = false;
12366
12367 /* After seeing a decl-specifier-seq, if the next token is not a
12368 "(", there is no possibility that the code is a valid
12369 expression. Therefore, if parsing tentatively, we commit at
12370 this point. */
12371 if (!parser->in_template_argument_list_p
12372 /* In an expression context, having seen:
12373
12374 (int((char ...
12375
12376 we cannot be sure whether we are looking at a
12377 function-type (taking a "char" as a parameter) or a cast
12378 of some object of type "char" to "int". */
12379 && !parser->in_type_id_in_expr_p
12380 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12381 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12382 cp_parser_commit_to_tentative_parse (parser);
12383 /* Parse the declarator. */
12384 declarator = cp_parser_declarator (parser,
12385 CP_PARSER_DECLARATOR_EITHER,
12386 /*ctor_dtor_or_conv_p=*/NULL,
12387 parenthesized_p,
12388 /*member_p=*/false);
12389 parser->default_arg_ok_p = saved_default_arg_ok_p;
12390 /* After the declarator, allow more attributes. */
12391 decl_specifiers.attributes
12392 = chainon (decl_specifiers.attributes,
12393 cp_parser_attributes_opt (parser));
12394 }
12395
12396 /* The restriction on defining new types applies only to the type
12397 of the parameter, not to the default argument. */
12398 parser->type_definition_forbidden_message = saved_message;
12399
12400 /* If the next token is `=', then process a default argument. */
12401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12402 {
12403 bool saved_greater_than_is_operator_p;
12404 /* Consume the `='. */
12405 cp_lexer_consume_token (parser->lexer);
12406
12407 /* If we are defining a class, then the tokens that make up the
12408 default argument must be saved and processed later. */
12409 if (!template_parm_p && at_class_scope_p ()
12410 && TYPE_BEING_DEFINED (current_class_type))
12411 {
12412 unsigned depth = 0;
12413 cp_token *first_token;
12414 cp_token *token;
12415
12416 /* Add tokens until we have processed the entire default
12417 argument. We add the range [first_token, token). */
12418 first_token = cp_lexer_peek_token (parser->lexer);
12419 while (true)
12420 {
12421 bool done = false;
12422
12423 /* Peek at the next token. */
12424 token = cp_lexer_peek_token (parser->lexer);
12425 /* What we do depends on what token we have. */
12426 switch (token->type)
12427 {
12428 /* In valid code, a default argument must be
12429 immediately followed by a `,' `)', or `...'. */
12430 case CPP_COMMA:
12431 case CPP_CLOSE_PAREN:
12432 case CPP_ELLIPSIS:
12433 /* If we run into a non-nested `;', `}', or `]',
12434 then the code is invalid -- but the default
12435 argument is certainly over. */
12436 case CPP_SEMICOLON:
12437 case CPP_CLOSE_BRACE:
12438 case CPP_CLOSE_SQUARE:
12439 if (depth == 0)
12440 done = true;
12441 /* Update DEPTH, if necessary. */
12442 else if (token->type == CPP_CLOSE_PAREN
12443 || token->type == CPP_CLOSE_BRACE
12444 || token->type == CPP_CLOSE_SQUARE)
12445 --depth;
12446 break;
12447
12448 case CPP_OPEN_PAREN:
12449 case CPP_OPEN_SQUARE:
12450 case CPP_OPEN_BRACE:
12451 ++depth;
12452 break;
12453
12454 case CPP_GREATER:
12455 /* If we see a non-nested `>', and `>' is not an
12456 operator, then it marks the end of the default
12457 argument. */
12458 if (!depth && !greater_than_is_operator_p)
12459 done = true;
12460 break;
12461
12462 /* If we run out of tokens, issue an error message. */
12463 case CPP_EOF:
12464 case CPP_PRAGMA_EOL:
12465 error ("file ends in default argument");
12466 done = true;
12467 break;
12468
12469 case CPP_NAME:
12470 case CPP_SCOPE:
12471 /* In these cases, we should look for template-ids.
12472 For example, if the default argument is
12473 `X<int, double>()', we need to do name lookup to
12474 figure out whether or not `X' is a template; if
12475 so, the `,' does not end the default argument.
12476
12477 That is not yet done. */
12478 break;
12479
12480 default:
12481 break;
12482 }
12483
12484 /* If we've reached the end, stop. */
12485 if (done)
12486 break;
12487
12488 /* Add the token to the token block. */
12489 token = cp_lexer_consume_token (parser->lexer);
12490 }
12491
12492 /* Create a DEFAULT_ARG to represented the unparsed default
12493 argument. */
12494 default_argument = make_node (DEFAULT_ARG);
12495 DEFARG_TOKENS (default_argument)
12496 = cp_token_cache_new (first_token, token);
12497 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12498 }
12499 /* Outside of a class definition, we can just parse the
12500 assignment-expression. */
12501 else
12502 {
12503 bool saved_local_variables_forbidden_p;
12504
12505 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12506 set correctly. */
12507 saved_greater_than_is_operator_p
12508 = parser->greater_than_is_operator_p;
12509 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12510 /* Local variable names (and the `this' keyword) may not
12511 appear in a default argument. */
12512 saved_local_variables_forbidden_p
12513 = parser->local_variables_forbidden_p;
12514 parser->local_variables_forbidden_p = true;
12515 /* The default argument expression may cause implicitly
12516 defined member functions to be synthesized, which will
12517 result in garbage collection. We must treat this
12518 situation as if we were within the body of function so as
12519 to avoid collecting live data on the stack. */
12520 ++function_depth;
12521 /* Parse the assignment-expression. */
12522 if (template_parm_p)
12523 push_deferring_access_checks (dk_no_deferred);
12524 default_argument
12525 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12526 if (template_parm_p)
12527 pop_deferring_access_checks ();
12528 /* Restore saved state. */
12529 --function_depth;
12530 parser->greater_than_is_operator_p
12531 = saved_greater_than_is_operator_p;
12532 parser->local_variables_forbidden_p
12533 = saved_local_variables_forbidden_p;
12534 }
12535 if (!parser->default_arg_ok_p)
12536 {
12537 if (!flag_pedantic_errors)
12538 warning (0, "deprecated use of default argument for parameter of non-function");
12539 else
12540 {
12541 error ("default arguments are only permitted for function parameters");
12542 default_argument = NULL_TREE;
12543 }
12544 }
12545 }
12546 else
12547 default_argument = NULL_TREE;
12548
12549 return make_parameter_declarator (&decl_specifiers,
12550 declarator,
12551 default_argument);
12552 }
12553
12554 /* Parse a function-body.
12555
12556 function-body:
12557 compound_statement */
12558
12559 static void
12560 cp_parser_function_body (cp_parser *parser)
12561 {
12562 cp_parser_compound_statement (parser, NULL, false);
12563 }
12564
12565 /* Parse a ctor-initializer-opt followed by a function-body. Return
12566 true if a ctor-initializer was present. */
12567
12568 static bool
12569 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12570 {
12571 tree body;
12572 bool ctor_initializer_p;
12573
12574 /* Begin the function body. */
12575 body = begin_function_body ();
12576 /* Parse the optional ctor-initializer. */
12577 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12578 /* Parse the function-body. */
12579 cp_parser_function_body (parser);
12580 /* Finish the function body. */
12581 finish_function_body (body);
12582
12583 return ctor_initializer_p;
12584 }
12585
12586 /* Parse an initializer.
12587
12588 initializer:
12589 = initializer-clause
12590 ( expression-list )
12591
12592 Returns an expression representing the initializer. If no
12593 initializer is present, NULL_TREE is returned.
12594
12595 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12596 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12597 set to FALSE if there is no initializer present. If there is an
12598 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12599 is set to true; otherwise it is set to false. */
12600
12601 static tree
12602 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12603 bool* non_constant_p)
12604 {
12605 cp_token *token;
12606 tree init;
12607
12608 /* Peek at the next token. */
12609 token = cp_lexer_peek_token (parser->lexer);
12610
12611 /* Let our caller know whether or not this initializer was
12612 parenthesized. */
12613 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12614 /* Assume that the initializer is constant. */
12615 *non_constant_p = false;
12616
12617 if (token->type == CPP_EQ)
12618 {
12619 /* Consume the `='. */
12620 cp_lexer_consume_token (parser->lexer);
12621 /* Parse the initializer-clause. */
12622 init = cp_parser_initializer_clause (parser, non_constant_p);
12623 }
12624 else if (token->type == CPP_OPEN_PAREN)
12625 init = cp_parser_parenthesized_expression_list (parser, false,
12626 /*cast_p=*/false,
12627 non_constant_p);
12628 else
12629 {
12630 /* Anything else is an error. */
12631 cp_parser_error (parser, "expected initializer");
12632 init = error_mark_node;
12633 }
12634
12635 return init;
12636 }
12637
12638 /* Parse an initializer-clause.
12639
12640 initializer-clause:
12641 assignment-expression
12642 { initializer-list , [opt] }
12643 { }
12644
12645 Returns an expression representing the initializer.
12646
12647 If the `assignment-expression' production is used the value
12648 returned is simply a representation for the expression.
12649
12650 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12651 the elements of the initializer-list (or NULL, if the last
12652 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12653 NULL_TREE. There is no way to detect whether or not the optional
12654 trailing `,' was provided. NON_CONSTANT_P is as for
12655 cp_parser_initializer. */
12656
12657 static tree
12658 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12659 {
12660 tree initializer;
12661
12662 /* Assume the expression is constant. */
12663 *non_constant_p = false;
12664
12665 /* If it is not a `{', then we are looking at an
12666 assignment-expression. */
12667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12668 {
12669 initializer
12670 = cp_parser_constant_expression (parser,
12671 /*allow_non_constant_p=*/true,
12672 non_constant_p);
12673 if (!*non_constant_p)
12674 initializer = fold_non_dependent_expr (initializer);
12675 }
12676 else
12677 {
12678 /* Consume the `{' token. */
12679 cp_lexer_consume_token (parser->lexer);
12680 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12681 initializer = make_node (CONSTRUCTOR);
12682 /* If it's not a `}', then there is a non-trivial initializer. */
12683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12684 {
12685 /* Parse the initializer list. */
12686 CONSTRUCTOR_ELTS (initializer)
12687 = cp_parser_initializer_list (parser, non_constant_p);
12688 /* A trailing `,' token is allowed. */
12689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12690 cp_lexer_consume_token (parser->lexer);
12691 }
12692 /* Now, there should be a trailing `}'. */
12693 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12694 }
12695
12696 return initializer;
12697 }
12698
12699 /* Parse an initializer-list.
12700
12701 initializer-list:
12702 initializer-clause
12703 initializer-list , initializer-clause
12704
12705 GNU Extension:
12706
12707 initializer-list:
12708 identifier : initializer-clause
12709 initializer-list, identifier : initializer-clause
12710
12711 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12712 for the initializer. If the INDEX of the elt is non-NULL, it is the
12713 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12714 as for cp_parser_initializer. */
12715
12716 static VEC(constructor_elt,gc) *
12717 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12718 {
12719 VEC(constructor_elt,gc) *v = NULL;
12720
12721 /* Assume all of the expressions are constant. */
12722 *non_constant_p = false;
12723
12724 /* Parse the rest of the list. */
12725 while (true)
12726 {
12727 cp_token *token;
12728 tree identifier;
12729 tree initializer;
12730 bool clause_non_constant_p;
12731
12732 /* If the next token is an identifier and the following one is a
12733 colon, we are looking at the GNU designated-initializer
12734 syntax. */
12735 if (cp_parser_allow_gnu_extensions_p (parser)
12736 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12738 {
12739 /* Warn the user that they are using an extension. */
12740 if (pedantic)
12741 pedwarn ("ISO C++ does not allow designated initializers");
12742 /* Consume the identifier. */
12743 identifier = cp_lexer_consume_token (parser->lexer)->value;
12744 /* Consume the `:'. */
12745 cp_lexer_consume_token (parser->lexer);
12746 }
12747 else
12748 identifier = NULL_TREE;
12749
12750 /* Parse the initializer. */
12751 initializer = cp_parser_initializer_clause (parser,
12752 &clause_non_constant_p);
12753 /* If any clause is non-constant, so is the entire initializer. */
12754 if (clause_non_constant_p)
12755 *non_constant_p = true;
12756
12757 /* Add it to the vector. */
12758 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12759
12760 /* If the next token is not a comma, we have reached the end of
12761 the list. */
12762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12763 break;
12764
12765 /* Peek at the next token. */
12766 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12767 /* If the next token is a `}', then we're still done. An
12768 initializer-clause can have a trailing `,' after the
12769 initializer-list and before the closing `}'. */
12770 if (token->type == CPP_CLOSE_BRACE)
12771 break;
12772
12773 /* Consume the `,' token. */
12774 cp_lexer_consume_token (parser->lexer);
12775 }
12776
12777 return v;
12778 }
12779
12780 /* Classes [gram.class] */
12781
12782 /* Parse a class-name.
12783
12784 class-name:
12785 identifier
12786 template-id
12787
12788 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12789 to indicate that names looked up in dependent types should be
12790 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12791 keyword has been used to indicate that the name that appears next
12792 is a template. TAG_TYPE indicates the explicit tag given before
12793 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12794 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12795 is the class being defined in a class-head.
12796
12797 Returns the TYPE_DECL representing the class. */
12798
12799 static tree
12800 cp_parser_class_name (cp_parser *parser,
12801 bool typename_keyword_p,
12802 bool template_keyword_p,
12803 enum tag_types tag_type,
12804 bool check_dependency_p,
12805 bool class_head_p,
12806 bool is_declaration)
12807 {
12808 tree decl;
12809 tree scope;
12810 bool typename_p;
12811 cp_token *token;
12812
12813 /* All class-names start with an identifier. */
12814 token = cp_lexer_peek_token (parser->lexer);
12815 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12816 {
12817 cp_parser_error (parser, "expected class-name");
12818 return error_mark_node;
12819 }
12820
12821 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12822 to a template-id, so we save it here. */
12823 scope = parser->scope;
12824 if (scope == error_mark_node)
12825 return error_mark_node;
12826
12827 /* Any name names a type if we're following the `typename' keyword
12828 in a qualified name where the enclosing scope is type-dependent. */
12829 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12830 && dependent_type_p (scope));
12831 /* Handle the common case (an identifier, but not a template-id)
12832 efficiently. */
12833 if (token->type == CPP_NAME
12834 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12835 {
12836 cp_token *identifier_token;
12837 tree identifier;
12838 bool ambiguous_p;
12839
12840 /* Look for the identifier. */
12841 identifier_token = cp_lexer_peek_token (parser->lexer);
12842 ambiguous_p = identifier_token->ambiguous_p;
12843 identifier = cp_parser_identifier (parser);
12844 /* If the next token isn't an identifier, we are certainly not
12845 looking at a class-name. */
12846 if (identifier == error_mark_node)
12847 decl = error_mark_node;
12848 /* If we know this is a type-name, there's no need to look it
12849 up. */
12850 else if (typename_p)
12851 decl = identifier;
12852 else
12853 {
12854 tree ambiguous_decls;
12855 /* If we already know that this lookup is ambiguous, then
12856 we've already issued an error message; there's no reason
12857 to check again. */
12858 if (ambiguous_p)
12859 {
12860 cp_parser_simulate_error (parser);
12861 return error_mark_node;
12862 }
12863 /* If the next token is a `::', then the name must be a type
12864 name.
12865
12866 [basic.lookup.qual]
12867
12868 During the lookup for a name preceding the :: scope
12869 resolution operator, object, function, and enumerator
12870 names are ignored. */
12871 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12872 tag_type = typename_type;
12873 /* Look up the name. */
12874 decl = cp_parser_lookup_name (parser, identifier,
12875 tag_type,
12876 /*is_template=*/false,
12877 /*is_namespace=*/false,
12878 check_dependency_p,
12879 &ambiguous_decls);
12880 if (ambiguous_decls)
12881 {
12882 error ("reference to %qD is ambiguous", identifier);
12883 print_candidates (ambiguous_decls);
12884 if (cp_parser_parsing_tentatively (parser))
12885 {
12886 identifier_token->ambiguous_p = true;
12887 cp_parser_simulate_error (parser);
12888 }
12889 return error_mark_node;
12890 }
12891 }
12892 }
12893 else
12894 {
12895 /* Try a template-id. */
12896 decl = cp_parser_template_id (parser, template_keyword_p,
12897 check_dependency_p,
12898 is_declaration);
12899 if (decl == error_mark_node)
12900 return error_mark_node;
12901 }
12902
12903 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12904
12905 /* If this is a typename, create a TYPENAME_TYPE. */
12906 if (typename_p && decl != error_mark_node)
12907 {
12908 decl = make_typename_type (scope, decl, typename_type,
12909 /*complain=*/tf_error);
12910 if (decl != error_mark_node)
12911 decl = TYPE_NAME (decl);
12912 }
12913
12914 /* Check to see that it is really the name of a class. */
12915 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12916 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12917 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12918 /* Situations like this:
12919
12920 template <typename T> struct A {
12921 typename T::template X<int>::I i;
12922 };
12923
12924 are problematic. Is `T::template X<int>' a class-name? The
12925 standard does not seem to be definitive, but there is no other
12926 valid interpretation of the following `::'. Therefore, those
12927 names are considered class-names. */
12928 {
12929 decl = make_typename_type (scope, decl, tag_type, tf_error);
12930 if (decl != error_mark_node)
12931 decl = TYPE_NAME (decl);
12932 }
12933 else if (TREE_CODE (decl) != TYPE_DECL
12934 || TREE_TYPE (decl) == error_mark_node
12935 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12936 decl = error_mark_node;
12937
12938 if (decl == error_mark_node)
12939 cp_parser_error (parser, "expected class-name");
12940
12941 return decl;
12942 }
12943
12944 /* Parse a class-specifier.
12945
12946 class-specifier:
12947 class-head { member-specification [opt] }
12948
12949 Returns the TREE_TYPE representing the class. */
12950
12951 static tree
12952 cp_parser_class_specifier (cp_parser* parser)
12953 {
12954 cp_token *token;
12955 tree type;
12956 tree attributes = NULL_TREE;
12957 int has_trailing_semicolon;
12958 bool nested_name_specifier_p;
12959 unsigned saved_num_template_parameter_lists;
12960 tree old_scope = NULL_TREE;
12961 tree scope = NULL_TREE;
12962
12963 push_deferring_access_checks (dk_no_deferred);
12964
12965 /* Parse the class-head. */
12966 type = cp_parser_class_head (parser,
12967 &nested_name_specifier_p,
12968 &attributes);
12969 /* If the class-head was a semantic disaster, skip the entire body
12970 of the class. */
12971 if (!type)
12972 {
12973 cp_parser_skip_to_end_of_block_or_statement (parser);
12974 pop_deferring_access_checks ();
12975 return error_mark_node;
12976 }
12977
12978 /* Look for the `{'. */
12979 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12980 {
12981 pop_deferring_access_checks ();
12982 return error_mark_node;
12983 }
12984
12985 /* Issue an error message if type-definitions are forbidden here. */
12986 cp_parser_check_type_definition (parser);
12987 /* Remember that we are defining one more class. */
12988 ++parser->num_classes_being_defined;
12989 /* Inside the class, surrounding template-parameter-lists do not
12990 apply. */
12991 saved_num_template_parameter_lists
12992 = parser->num_template_parameter_lists;
12993 parser->num_template_parameter_lists = 0;
12994
12995 /* Start the class. */
12996 if (nested_name_specifier_p)
12997 {
12998 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12999 old_scope = push_inner_scope (scope);
13000 }
13001 type = begin_class_definition (type, attributes);
13002
13003 if (type == error_mark_node)
13004 /* If the type is erroneous, skip the entire body of the class. */
13005 cp_parser_skip_to_closing_brace (parser);
13006 else
13007 /* Parse the member-specification. */
13008 cp_parser_member_specification_opt (parser);
13009
13010 /* Look for the trailing `}'. */
13011 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13012 /* We get better error messages by noticing a common problem: a
13013 missing trailing `;'. */
13014 token = cp_lexer_peek_token (parser->lexer);
13015 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13016 /* Look for trailing attributes to apply to this class. */
13017 if (cp_parser_allow_gnu_extensions_p (parser))
13018 attributes = cp_parser_attributes_opt (parser);
13019 if (type != error_mark_node)
13020 type = finish_struct (type, attributes);
13021 if (nested_name_specifier_p)
13022 pop_inner_scope (old_scope, scope);
13023 /* If this class is not itself within the scope of another class,
13024 then we need to parse the bodies of all of the queued function
13025 definitions. Note that the queued functions defined in a class
13026 are not always processed immediately following the
13027 class-specifier for that class. Consider:
13028
13029 struct A {
13030 struct B { void f() { sizeof (A); } };
13031 };
13032
13033 If `f' were processed before the processing of `A' were
13034 completed, there would be no way to compute the size of `A'.
13035 Note that the nesting we are interested in here is lexical --
13036 not the semantic nesting given by TYPE_CONTEXT. In particular,
13037 for:
13038
13039 struct A { struct B; };
13040 struct A::B { void f() { } };
13041
13042 there is no need to delay the parsing of `A::B::f'. */
13043 if (--parser->num_classes_being_defined == 0)
13044 {
13045 tree queue_entry;
13046 tree fn;
13047 tree class_type = NULL_TREE;
13048 tree pushed_scope = NULL_TREE;
13049
13050 /* In a first pass, parse default arguments to the functions.
13051 Then, in a second pass, parse the bodies of the functions.
13052 This two-phased approach handles cases like:
13053
13054 struct S {
13055 void f() { g(); }
13056 void g(int i = 3);
13057 };
13058
13059 */
13060 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13061 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13062 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13063 TREE_PURPOSE (parser->unparsed_functions_queues)
13064 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13065 {
13066 fn = TREE_VALUE (queue_entry);
13067 /* If there are default arguments that have not yet been processed,
13068 take care of them now. */
13069 if (class_type != TREE_PURPOSE (queue_entry))
13070 {
13071 if (pushed_scope)
13072 pop_scope (pushed_scope);
13073 class_type = TREE_PURPOSE (queue_entry);
13074 pushed_scope = push_scope (class_type);
13075 }
13076 /* Make sure that any template parameters are in scope. */
13077 maybe_begin_member_template_processing (fn);
13078 /* Parse the default argument expressions. */
13079 cp_parser_late_parsing_default_args (parser, fn);
13080 /* Remove any template parameters from the symbol table. */
13081 maybe_end_member_template_processing ();
13082 }
13083 if (pushed_scope)
13084 pop_scope (pushed_scope);
13085 /* Now parse the body of the functions. */
13086 for (TREE_VALUE (parser->unparsed_functions_queues)
13087 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13088 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13089 TREE_VALUE (parser->unparsed_functions_queues)
13090 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13091 {
13092 /* Figure out which function we need to process. */
13093 fn = TREE_VALUE (queue_entry);
13094 /* Parse the function. */
13095 cp_parser_late_parsing_for_member (parser, fn);
13096 }
13097 }
13098
13099 /* Put back any saved access checks. */
13100 pop_deferring_access_checks ();
13101
13102 /* Restore the count of active template-parameter-lists. */
13103 parser->num_template_parameter_lists
13104 = saved_num_template_parameter_lists;
13105
13106 return type;
13107 }
13108
13109 /* Parse a class-head.
13110
13111 class-head:
13112 class-key identifier [opt] base-clause [opt]
13113 class-key nested-name-specifier identifier base-clause [opt]
13114 class-key nested-name-specifier [opt] template-id
13115 base-clause [opt]
13116
13117 GNU Extensions:
13118 class-key attributes identifier [opt] base-clause [opt]
13119 class-key attributes nested-name-specifier identifier base-clause [opt]
13120 class-key attributes nested-name-specifier [opt] template-id
13121 base-clause [opt]
13122
13123 Returns the TYPE of the indicated class. Sets
13124 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13125 involving a nested-name-specifier was used, and FALSE otherwise.
13126
13127 Returns error_mark_node if this is not a class-head.
13128
13129 Returns NULL_TREE if the class-head is syntactically valid, but
13130 semantically invalid in a way that means we should skip the entire
13131 body of the class. */
13132
13133 static tree
13134 cp_parser_class_head (cp_parser* parser,
13135 bool* nested_name_specifier_p,
13136 tree *attributes_p)
13137 {
13138 tree nested_name_specifier;
13139 enum tag_types class_key;
13140 tree id = NULL_TREE;
13141 tree type = NULL_TREE;
13142 tree attributes;
13143 bool template_id_p = false;
13144 bool qualified_p = false;
13145 bool invalid_nested_name_p = false;
13146 bool invalid_explicit_specialization_p = false;
13147 tree pushed_scope = NULL_TREE;
13148 unsigned num_templates;
13149 tree bases;
13150
13151 /* Assume no nested-name-specifier will be present. */
13152 *nested_name_specifier_p = false;
13153 /* Assume no template parameter lists will be used in defining the
13154 type. */
13155 num_templates = 0;
13156
13157 /* Look for the class-key. */
13158 class_key = cp_parser_class_key (parser);
13159 if (class_key == none_type)
13160 return error_mark_node;
13161
13162 /* Parse the attributes. */
13163 attributes = cp_parser_attributes_opt (parser);
13164
13165 /* If the next token is `::', that is invalid -- but sometimes
13166 people do try to write:
13167
13168 struct ::S {};
13169
13170 Handle this gracefully by accepting the extra qualifier, and then
13171 issuing an error about it later if this really is a
13172 class-head. If it turns out just to be an elaborated type
13173 specifier, remain silent. */
13174 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13175 qualified_p = true;
13176
13177 push_deferring_access_checks (dk_no_check);
13178
13179 /* Determine the name of the class. Begin by looking for an
13180 optional nested-name-specifier. */
13181 nested_name_specifier
13182 = cp_parser_nested_name_specifier_opt (parser,
13183 /*typename_keyword_p=*/false,
13184 /*check_dependency_p=*/false,
13185 /*type_p=*/false,
13186 /*is_declaration=*/false);
13187 /* If there was a nested-name-specifier, then there *must* be an
13188 identifier. */
13189 if (nested_name_specifier)
13190 {
13191 /* Although the grammar says `identifier', it really means
13192 `class-name' or `template-name'. You are only allowed to
13193 define a class that has already been declared with this
13194 syntax.
13195
13196 The proposed resolution for Core Issue 180 says that wherever
13197 you see `class T::X' you should treat `X' as a type-name.
13198
13199 It is OK to define an inaccessible class; for example:
13200
13201 class A { class B; };
13202 class A::B {};
13203
13204 We do not know if we will see a class-name, or a
13205 template-name. We look for a class-name first, in case the
13206 class-name is a template-id; if we looked for the
13207 template-name first we would stop after the template-name. */
13208 cp_parser_parse_tentatively (parser);
13209 type = cp_parser_class_name (parser,
13210 /*typename_keyword_p=*/false,
13211 /*template_keyword_p=*/false,
13212 class_type,
13213 /*check_dependency_p=*/false,
13214 /*class_head_p=*/true,
13215 /*is_declaration=*/false);
13216 /* If that didn't work, ignore the nested-name-specifier. */
13217 if (!cp_parser_parse_definitely (parser))
13218 {
13219 invalid_nested_name_p = true;
13220 id = cp_parser_identifier (parser);
13221 if (id == error_mark_node)
13222 id = NULL_TREE;
13223 }
13224 /* If we could not find a corresponding TYPE, treat this
13225 declaration like an unqualified declaration. */
13226 if (type == error_mark_node)
13227 nested_name_specifier = NULL_TREE;
13228 /* Otherwise, count the number of templates used in TYPE and its
13229 containing scopes. */
13230 else
13231 {
13232 tree scope;
13233
13234 for (scope = TREE_TYPE (type);
13235 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13236 scope = (TYPE_P (scope)
13237 ? TYPE_CONTEXT (scope)
13238 : DECL_CONTEXT (scope)))
13239 if (TYPE_P (scope)
13240 && CLASS_TYPE_P (scope)
13241 && CLASSTYPE_TEMPLATE_INFO (scope)
13242 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13243 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13244 ++num_templates;
13245 }
13246 }
13247 /* Otherwise, the identifier is optional. */
13248 else
13249 {
13250 /* We don't know whether what comes next is a template-id,
13251 an identifier, or nothing at all. */
13252 cp_parser_parse_tentatively (parser);
13253 /* Check for a template-id. */
13254 id = cp_parser_template_id (parser,
13255 /*template_keyword_p=*/false,
13256 /*check_dependency_p=*/true,
13257 /*is_declaration=*/true);
13258 /* If that didn't work, it could still be an identifier. */
13259 if (!cp_parser_parse_definitely (parser))
13260 {
13261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13262 id = cp_parser_identifier (parser);
13263 else
13264 id = NULL_TREE;
13265 }
13266 else
13267 {
13268 template_id_p = true;
13269 ++num_templates;
13270 }
13271 }
13272
13273 pop_deferring_access_checks ();
13274
13275 if (id)
13276 cp_parser_check_for_invalid_template_id (parser, id);
13277
13278 /* If it's not a `:' or a `{' then we can't really be looking at a
13279 class-head, since a class-head only appears as part of a
13280 class-specifier. We have to detect this situation before calling
13281 xref_tag, since that has irreversible side-effects. */
13282 if (!cp_parser_next_token_starts_class_definition_p (parser))
13283 {
13284 cp_parser_error (parser, "expected %<{%> or %<:%>");
13285 return error_mark_node;
13286 }
13287
13288 /* At this point, we're going ahead with the class-specifier, even
13289 if some other problem occurs. */
13290 cp_parser_commit_to_tentative_parse (parser);
13291 /* Issue the error about the overly-qualified name now. */
13292 if (qualified_p)
13293 cp_parser_error (parser,
13294 "global qualification of class name is invalid");
13295 else if (invalid_nested_name_p)
13296 cp_parser_error (parser,
13297 "qualified name does not name a class");
13298 else if (nested_name_specifier)
13299 {
13300 tree scope;
13301
13302 /* Reject typedef-names in class heads. */
13303 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13304 {
13305 error ("invalid class name in declaration of %qD", type);
13306 type = NULL_TREE;
13307 goto done;
13308 }
13309
13310 /* Figure out in what scope the declaration is being placed. */
13311 scope = current_scope ();
13312 /* If that scope does not contain the scope in which the
13313 class was originally declared, the program is invalid. */
13314 if (scope && !is_ancestor (scope, nested_name_specifier))
13315 {
13316 error ("declaration of %qD in %qD which does not enclose %qD",
13317 type, scope, nested_name_specifier);
13318 type = NULL_TREE;
13319 goto done;
13320 }
13321 /* [dcl.meaning]
13322
13323 A declarator-id shall not be qualified exception of the
13324 definition of a ... nested class outside of its class
13325 ... [or] a the definition or explicit instantiation of a
13326 class member of a namespace outside of its namespace. */
13327 if (scope == nested_name_specifier)
13328 {
13329 pedwarn ("extra qualification ignored");
13330 nested_name_specifier = NULL_TREE;
13331 num_templates = 0;
13332 }
13333 }
13334 /* An explicit-specialization must be preceded by "template <>". If
13335 it is not, try to recover gracefully. */
13336 if (at_namespace_scope_p ()
13337 && parser->num_template_parameter_lists == 0
13338 && template_id_p)
13339 {
13340 error ("an explicit specialization must be preceded by %<template <>%>");
13341 invalid_explicit_specialization_p = true;
13342 /* Take the same action that would have been taken by
13343 cp_parser_explicit_specialization. */
13344 ++parser->num_template_parameter_lists;
13345 begin_specialization ();
13346 }
13347 /* There must be no "return" statements between this point and the
13348 end of this function; set "type "to the correct return value and
13349 use "goto done;" to return. */
13350 /* Make sure that the right number of template parameters were
13351 present. */
13352 if (!cp_parser_check_template_parameters (parser, num_templates))
13353 {
13354 /* If something went wrong, there is no point in even trying to
13355 process the class-definition. */
13356 type = NULL_TREE;
13357 goto done;
13358 }
13359
13360 /* Look up the type. */
13361 if (template_id_p)
13362 {
13363 type = TREE_TYPE (id);
13364 type = maybe_process_partial_specialization (type);
13365 if (nested_name_specifier)
13366 pushed_scope = push_scope (nested_name_specifier);
13367 }
13368 else if (nested_name_specifier)
13369 {
13370 tree class_type;
13371
13372 /* Given:
13373
13374 template <typename T> struct S { struct T };
13375 template <typename T> struct S<T>::T { };
13376
13377 we will get a TYPENAME_TYPE when processing the definition of
13378 `S::T'. We need to resolve it to the actual type before we
13379 try to define it. */
13380 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13381 {
13382 class_type = resolve_typename_type (TREE_TYPE (type),
13383 /*only_current_p=*/false);
13384 if (class_type != error_mark_node)
13385 type = TYPE_NAME (class_type);
13386 else
13387 {
13388 cp_parser_error (parser, "could not resolve typename type");
13389 type = error_mark_node;
13390 }
13391 }
13392
13393 maybe_process_partial_specialization (TREE_TYPE (type));
13394 class_type = current_class_type;
13395 /* Enter the scope indicated by the nested-name-specifier. */
13396 pushed_scope = push_scope (nested_name_specifier);
13397 /* Get the canonical version of this type. */
13398 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13399 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13400 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13401 {
13402 type = push_template_decl (type);
13403 if (type == error_mark_node)
13404 {
13405 type = NULL_TREE;
13406 goto done;
13407 }
13408 }
13409
13410 type = TREE_TYPE (type);
13411 *nested_name_specifier_p = true;
13412 }
13413 else /* The name is not a nested name. */
13414 {
13415 /* If the class was unnamed, create a dummy name. */
13416 if (!id)
13417 id = make_anon_name ();
13418 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13419 parser->num_template_parameter_lists);
13420 }
13421
13422 /* Indicate whether this class was declared as a `class' or as a
13423 `struct'. */
13424 if (TREE_CODE (type) == RECORD_TYPE)
13425 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13426 cp_parser_check_class_key (class_key, type);
13427
13428 /* If this type was already complete, and we see another definition,
13429 that's an error. */
13430 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13431 {
13432 error ("redefinition of %q#T", type);
13433 error ("previous definition of %q+#T", type);
13434 type = NULL_TREE;
13435 goto done;
13436 }
13437
13438 /* We will have entered the scope containing the class; the names of
13439 base classes should be looked up in that context. For example:
13440
13441 struct A { struct B {}; struct C; };
13442 struct A::C : B {};
13443
13444 is valid. */
13445 bases = NULL_TREE;
13446
13447 /* Get the list of base-classes, if there is one. */
13448 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13449 bases = cp_parser_base_clause (parser);
13450
13451 /* Process the base classes. */
13452 xref_basetypes (type, bases);
13453
13454 done:
13455 /* Leave the scope given by the nested-name-specifier. We will
13456 enter the class scope itself while processing the members. */
13457 if (pushed_scope)
13458 pop_scope (pushed_scope);
13459
13460 if (invalid_explicit_specialization_p)
13461 {
13462 end_specialization ();
13463 --parser->num_template_parameter_lists;
13464 }
13465 *attributes_p = attributes;
13466 return type;
13467 }
13468
13469 /* Parse a class-key.
13470
13471 class-key:
13472 class
13473 struct
13474 union
13475
13476 Returns the kind of class-key specified, or none_type to indicate
13477 error. */
13478
13479 static enum tag_types
13480 cp_parser_class_key (cp_parser* parser)
13481 {
13482 cp_token *token;
13483 enum tag_types tag_type;
13484
13485 /* Look for the class-key. */
13486 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13487 if (!token)
13488 return none_type;
13489
13490 /* Check to see if the TOKEN is a class-key. */
13491 tag_type = cp_parser_token_is_class_key (token);
13492 if (!tag_type)
13493 cp_parser_error (parser, "expected class-key");
13494 return tag_type;
13495 }
13496
13497 /* Parse an (optional) member-specification.
13498
13499 member-specification:
13500 member-declaration member-specification [opt]
13501 access-specifier : member-specification [opt] */
13502
13503 static void
13504 cp_parser_member_specification_opt (cp_parser* parser)
13505 {
13506 while (true)
13507 {
13508 cp_token *token;
13509 enum rid keyword;
13510
13511 /* Peek at the next token. */
13512 token = cp_lexer_peek_token (parser->lexer);
13513 /* If it's a `}', or EOF then we've seen all the members. */
13514 if (token->type == CPP_CLOSE_BRACE
13515 || token->type == CPP_EOF
13516 || token->type == CPP_PRAGMA_EOL)
13517 break;
13518
13519 /* See if this token is a keyword. */
13520 keyword = token->keyword;
13521 switch (keyword)
13522 {
13523 case RID_PUBLIC:
13524 case RID_PROTECTED:
13525 case RID_PRIVATE:
13526 /* Consume the access-specifier. */
13527 cp_lexer_consume_token (parser->lexer);
13528 /* Remember which access-specifier is active. */
13529 current_access_specifier = token->value;
13530 /* Look for the `:'. */
13531 cp_parser_require (parser, CPP_COLON, "`:'");
13532 break;
13533
13534 default:
13535 /* Accept #pragmas at class scope. */
13536 if (token->type == CPP_PRAGMA)
13537 {
13538 cp_parser_pragma (parser, pragma_external);
13539 break;
13540 }
13541
13542 /* Otherwise, the next construction must be a
13543 member-declaration. */
13544 cp_parser_member_declaration (parser);
13545 }
13546 }
13547 }
13548
13549 /* Parse a member-declaration.
13550
13551 member-declaration:
13552 decl-specifier-seq [opt] member-declarator-list [opt] ;
13553 function-definition ; [opt]
13554 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13555 using-declaration
13556 template-declaration
13557
13558 member-declarator-list:
13559 member-declarator
13560 member-declarator-list , member-declarator
13561
13562 member-declarator:
13563 declarator pure-specifier [opt]
13564 declarator constant-initializer [opt]
13565 identifier [opt] : constant-expression
13566
13567 GNU Extensions:
13568
13569 member-declaration:
13570 __extension__ member-declaration
13571
13572 member-declarator:
13573 declarator attributes [opt] pure-specifier [opt]
13574 declarator attributes [opt] constant-initializer [opt]
13575 identifier [opt] attributes [opt] : constant-expression */
13576
13577 static void
13578 cp_parser_member_declaration (cp_parser* parser)
13579 {
13580 cp_decl_specifier_seq decl_specifiers;
13581 tree prefix_attributes;
13582 tree decl;
13583 int declares_class_or_enum;
13584 bool friend_p;
13585 cp_token *token;
13586 int saved_pedantic;
13587
13588 /* Check for the `__extension__' keyword. */
13589 if (cp_parser_extension_opt (parser, &saved_pedantic))
13590 {
13591 /* Recurse. */
13592 cp_parser_member_declaration (parser);
13593 /* Restore the old value of the PEDANTIC flag. */
13594 pedantic = saved_pedantic;
13595
13596 return;
13597 }
13598
13599 /* Check for a template-declaration. */
13600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13601 {
13602 /* An explicit specialization here is an error condition, and we
13603 expect the specialization handler to detect and report this. */
13604 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13605 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13606 cp_parser_explicit_specialization (parser);
13607 else
13608 cp_parser_template_declaration (parser, /*member_p=*/true);
13609
13610 return;
13611 }
13612
13613 /* Check for a using-declaration. */
13614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13615 {
13616 /* Parse the using-declaration. */
13617 cp_parser_using_declaration (parser,
13618 /*access_declaration_p=*/false);
13619 return;
13620 }
13621
13622 /* Check for @defs. */
13623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13624 {
13625 tree ivar, member;
13626 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13627 ivar = ivar_chains;
13628 while (ivar)
13629 {
13630 member = ivar;
13631 ivar = TREE_CHAIN (member);
13632 TREE_CHAIN (member) = NULL_TREE;
13633 finish_member_declaration (member);
13634 }
13635 return;
13636 }
13637
13638 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13639 return;
13640
13641 /* Parse the decl-specifier-seq. */
13642 cp_parser_decl_specifier_seq (parser,
13643 CP_PARSER_FLAGS_OPTIONAL,
13644 &decl_specifiers,
13645 &declares_class_or_enum);
13646 prefix_attributes = decl_specifiers.attributes;
13647 decl_specifiers.attributes = NULL_TREE;
13648 /* Check for an invalid type-name. */
13649 if (!decl_specifiers.type
13650 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13651 return;
13652 /* If there is no declarator, then the decl-specifier-seq should
13653 specify a type. */
13654 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13655 {
13656 /* If there was no decl-specifier-seq, and the next token is a
13657 `;', then we have something like:
13658
13659 struct S { ; };
13660
13661 [class.mem]
13662
13663 Each member-declaration shall declare at least one member
13664 name of the class. */
13665 if (!decl_specifiers.any_specifiers_p)
13666 {
13667 cp_token *token = cp_lexer_peek_token (parser->lexer);
13668 if (pedantic && !token->in_system_header)
13669 pedwarn ("%Hextra %<;%>", &token->location);
13670 }
13671 else
13672 {
13673 tree type;
13674
13675 /* See if this declaration is a friend. */
13676 friend_p = cp_parser_friend_p (&decl_specifiers);
13677 /* If there were decl-specifiers, check to see if there was
13678 a class-declaration. */
13679 type = check_tag_decl (&decl_specifiers);
13680 /* Nested classes have already been added to the class, but
13681 a `friend' needs to be explicitly registered. */
13682 if (friend_p)
13683 {
13684 /* If the `friend' keyword was present, the friend must
13685 be introduced with a class-key. */
13686 if (!declares_class_or_enum)
13687 error ("a class-key must be used when declaring a friend");
13688 /* In this case:
13689
13690 template <typename T> struct A {
13691 friend struct A<T>::B;
13692 };
13693
13694 A<T>::B will be represented by a TYPENAME_TYPE, and
13695 therefore not recognized by check_tag_decl. */
13696 if (!type
13697 && decl_specifiers.type
13698 && TYPE_P (decl_specifiers.type))
13699 type = decl_specifiers.type;
13700 if (!type || !TYPE_P (type))
13701 error ("friend declaration does not name a class or "
13702 "function");
13703 else
13704 make_friend_class (current_class_type, type,
13705 /*complain=*/true);
13706 }
13707 /* If there is no TYPE, an error message will already have
13708 been issued. */
13709 else if (!type || type == error_mark_node)
13710 ;
13711 /* An anonymous aggregate has to be handled specially; such
13712 a declaration really declares a data member (with a
13713 particular type), as opposed to a nested class. */
13714 else if (ANON_AGGR_TYPE_P (type))
13715 {
13716 /* Remove constructors and such from TYPE, now that we
13717 know it is an anonymous aggregate. */
13718 fixup_anonymous_aggr (type);
13719 /* And make the corresponding data member. */
13720 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13721 /* Add it to the class. */
13722 finish_member_declaration (decl);
13723 }
13724 else
13725 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13726 }
13727 }
13728 else
13729 {
13730 /* See if these declarations will be friends. */
13731 friend_p = cp_parser_friend_p (&decl_specifiers);
13732
13733 /* Keep going until we hit the `;' at the end of the
13734 declaration. */
13735 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13736 {
13737 tree attributes = NULL_TREE;
13738 tree first_attribute;
13739
13740 /* Peek at the next token. */
13741 token = cp_lexer_peek_token (parser->lexer);
13742
13743 /* Check for a bitfield declaration. */
13744 if (token->type == CPP_COLON
13745 || (token->type == CPP_NAME
13746 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13747 == CPP_COLON))
13748 {
13749 tree identifier;
13750 tree width;
13751
13752 /* Get the name of the bitfield. Note that we cannot just
13753 check TOKEN here because it may have been invalidated by
13754 the call to cp_lexer_peek_nth_token above. */
13755 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13756 identifier = cp_parser_identifier (parser);
13757 else
13758 identifier = NULL_TREE;
13759
13760 /* Consume the `:' token. */
13761 cp_lexer_consume_token (parser->lexer);
13762 /* Get the width of the bitfield. */
13763 width
13764 = cp_parser_constant_expression (parser,
13765 /*allow_non_constant=*/false,
13766 NULL);
13767
13768 /* Look for attributes that apply to the bitfield. */
13769 attributes = cp_parser_attributes_opt (parser);
13770 /* Remember which attributes are prefix attributes and
13771 which are not. */
13772 first_attribute = attributes;
13773 /* Combine the attributes. */
13774 attributes = chainon (prefix_attributes, attributes);
13775
13776 /* Create the bitfield declaration. */
13777 decl = grokbitfield (identifier
13778 ? make_id_declarator (NULL_TREE,
13779 identifier,
13780 sfk_none)
13781 : NULL,
13782 &decl_specifiers,
13783 width);
13784 /* Apply the attributes. */
13785 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13786 }
13787 else
13788 {
13789 cp_declarator *declarator;
13790 tree initializer;
13791 tree asm_specification;
13792 int ctor_dtor_or_conv_p;
13793
13794 /* Parse the declarator. */
13795 declarator
13796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13797 &ctor_dtor_or_conv_p,
13798 /*parenthesized_p=*/NULL,
13799 /*member_p=*/true);
13800
13801 /* If something went wrong parsing the declarator, make sure
13802 that we at least consume some tokens. */
13803 if (declarator == cp_error_declarator)
13804 {
13805 /* Skip to the end of the statement. */
13806 cp_parser_skip_to_end_of_statement (parser);
13807 /* If the next token is not a semicolon, that is
13808 probably because we just skipped over the body of
13809 a function. So, we consume a semicolon if
13810 present, but do not issue an error message if it
13811 is not present. */
13812 if (cp_lexer_next_token_is (parser->lexer,
13813 CPP_SEMICOLON))
13814 cp_lexer_consume_token (parser->lexer);
13815 return;
13816 }
13817
13818 if (declares_class_or_enum & 2)
13819 cp_parser_check_for_definition_in_return_type
13820 (declarator, decl_specifiers.type);
13821
13822 /* Look for an asm-specification. */
13823 asm_specification = cp_parser_asm_specification_opt (parser);
13824 /* Look for attributes that apply to the declaration. */
13825 attributes = cp_parser_attributes_opt (parser);
13826 /* Remember which attributes are prefix attributes and
13827 which are not. */
13828 first_attribute = attributes;
13829 /* Combine the attributes. */
13830 attributes = chainon (prefix_attributes, attributes);
13831
13832 /* If it's an `=', then we have a constant-initializer or a
13833 pure-specifier. It is not correct to parse the
13834 initializer before registering the member declaration
13835 since the member declaration should be in scope while
13836 its initializer is processed. However, the rest of the
13837 front end does not yet provide an interface that allows
13838 us to handle this correctly. */
13839 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13840 {
13841 /* In [class.mem]:
13842
13843 A pure-specifier shall be used only in the declaration of
13844 a virtual function.
13845
13846 A member-declarator can contain a constant-initializer
13847 only if it declares a static member of integral or
13848 enumeration type.
13849
13850 Therefore, if the DECLARATOR is for a function, we look
13851 for a pure-specifier; otherwise, we look for a
13852 constant-initializer. When we call `grokfield', it will
13853 perform more stringent semantics checks. */
13854 if (function_declarator_p (declarator))
13855 initializer = cp_parser_pure_specifier (parser);
13856 else
13857 /* Parse the initializer. */
13858 initializer = cp_parser_constant_initializer (parser);
13859 }
13860 /* Otherwise, there is no initializer. */
13861 else
13862 initializer = NULL_TREE;
13863
13864 /* See if we are probably looking at a function
13865 definition. We are certainly not looking at a
13866 member-declarator. Calling `grokfield' has
13867 side-effects, so we must not do it unless we are sure
13868 that we are looking at a member-declarator. */
13869 if (cp_parser_token_starts_function_definition_p
13870 (cp_lexer_peek_token (parser->lexer)))
13871 {
13872 /* The grammar does not allow a pure-specifier to be
13873 used when a member function is defined. (It is
13874 possible that this fact is an oversight in the
13875 standard, since a pure function may be defined
13876 outside of the class-specifier. */
13877 if (initializer)
13878 error ("pure-specifier on function-definition");
13879 decl = cp_parser_save_member_function_body (parser,
13880 &decl_specifiers,
13881 declarator,
13882 attributes);
13883 /* If the member was not a friend, declare it here. */
13884 if (!friend_p)
13885 finish_member_declaration (decl);
13886 /* Peek at the next token. */
13887 token = cp_lexer_peek_token (parser->lexer);
13888 /* If the next token is a semicolon, consume it. */
13889 if (token->type == CPP_SEMICOLON)
13890 cp_lexer_consume_token (parser->lexer);
13891 return;
13892 }
13893 else
13894 /* Create the declaration. */
13895 decl = grokfield (declarator, &decl_specifiers,
13896 initializer, /*init_const_expr_p=*/true,
13897 asm_specification,
13898 attributes);
13899 }
13900
13901 /* Reset PREFIX_ATTRIBUTES. */
13902 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13903 attributes = TREE_CHAIN (attributes);
13904 if (attributes)
13905 TREE_CHAIN (attributes) = NULL_TREE;
13906
13907 /* If there is any qualification still in effect, clear it
13908 now; we will be starting fresh with the next declarator. */
13909 parser->scope = NULL_TREE;
13910 parser->qualifying_scope = NULL_TREE;
13911 parser->object_scope = NULL_TREE;
13912 /* If it's a `,', then there are more declarators. */
13913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13914 cp_lexer_consume_token (parser->lexer);
13915 /* If the next token isn't a `;', then we have a parse error. */
13916 else if (cp_lexer_next_token_is_not (parser->lexer,
13917 CPP_SEMICOLON))
13918 {
13919 cp_parser_error (parser, "expected %<;%>");
13920 /* Skip tokens until we find a `;'. */
13921 cp_parser_skip_to_end_of_statement (parser);
13922
13923 break;
13924 }
13925
13926 if (decl)
13927 {
13928 /* Add DECL to the list of members. */
13929 if (!friend_p)
13930 finish_member_declaration (decl);
13931
13932 if (TREE_CODE (decl) == FUNCTION_DECL)
13933 cp_parser_save_default_args (parser, decl);
13934 }
13935 }
13936 }
13937
13938 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13939 }
13940
13941 /* Parse a pure-specifier.
13942
13943 pure-specifier:
13944 = 0
13945
13946 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13947 Otherwise, ERROR_MARK_NODE is returned. */
13948
13949 static tree
13950 cp_parser_pure_specifier (cp_parser* parser)
13951 {
13952 cp_token *token;
13953
13954 /* Look for the `=' token. */
13955 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13956 return error_mark_node;
13957 /* Look for the `0' token. */
13958 token = cp_lexer_consume_token (parser->lexer);
13959 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13960 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13961 {
13962 cp_parser_error (parser,
13963 "invalid pure specifier (only `= 0' is allowed)");
13964 cp_parser_skip_to_end_of_statement (parser);
13965 return error_mark_node;
13966 }
13967 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13968 {
13969 error ("templates may not be %<virtual%>");
13970 return error_mark_node;
13971 }
13972
13973 return integer_zero_node;
13974 }
13975
13976 /* Parse a constant-initializer.
13977
13978 constant-initializer:
13979 = constant-expression
13980
13981 Returns a representation of the constant-expression. */
13982
13983 static tree
13984 cp_parser_constant_initializer (cp_parser* parser)
13985 {
13986 /* Look for the `=' token. */
13987 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13988 return error_mark_node;
13989
13990 /* It is invalid to write:
13991
13992 struct S { static const int i = { 7 }; };
13993
13994 */
13995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13996 {
13997 cp_parser_error (parser,
13998 "a brace-enclosed initializer is not allowed here");
13999 /* Consume the opening brace. */
14000 cp_lexer_consume_token (parser->lexer);
14001 /* Skip the initializer. */
14002 cp_parser_skip_to_closing_brace (parser);
14003 /* Look for the trailing `}'. */
14004 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14005
14006 return error_mark_node;
14007 }
14008
14009 return cp_parser_constant_expression (parser,
14010 /*allow_non_constant=*/false,
14011 NULL);
14012 }
14013
14014 /* Derived classes [gram.class.derived] */
14015
14016 /* Parse a base-clause.
14017
14018 base-clause:
14019 : base-specifier-list
14020
14021 base-specifier-list:
14022 base-specifier
14023 base-specifier-list , base-specifier
14024
14025 Returns a TREE_LIST representing the base-classes, in the order in
14026 which they were declared. The representation of each node is as
14027 described by cp_parser_base_specifier.
14028
14029 In the case that no bases are specified, this function will return
14030 NULL_TREE, not ERROR_MARK_NODE. */
14031
14032 static tree
14033 cp_parser_base_clause (cp_parser* parser)
14034 {
14035 tree bases = NULL_TREE;
14036
14037 /* Look for the `:' that begins the list. */
14038 cp_parser_require (parser, CPP_COLON, "`:'");
14039
14040 /* Scan the base-specifier-list. */
14041 while (true)
14042 {
14043 cp_token *token;
14044 tree base;
14045
14046 /* Look for the base-specifier. */
14047 base = cp_parser_base_specifier (parser);
14048 /* Add BASE to the front of the list. */
14049 if (base != error_mark_node)
14050 {
14051 TREE_CHAIN (base) = bases;
14052 bases = base;
14053 }
14054 /* Peek at the next token. */
14055 token = cp_lexer_peek_token (parser->lexer);
14056 /* If it's not a comma, then the list is complete. */
14057 if (token->type != CPP_COMMA)
14058 break;
14059 /* Consume the `,'. */
14060 cp_lexer_consume_token (parser->lexer);
14061 }
14062
14063 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14064 base class had a qualified name. However, the next name that
14065 appears is certainly not qualified. */
14066 parser->scope = NULL_TREE;
14067 parser->qualifying_scope = NULL_TREE;
14068 parser->object_scope = NULL_TREE;
14069
14070 return nreverse (bases);
14071 }
14072
14073 /* Parse a base-specifier.
14074
14075 base-specifier:
14076 :: [opt] nested-name-specifier [opt] class-name
14077 virtual access-specifier [opt] :: [opt] nested-name-specifier
14078 [opt] class-name
14079 access-specifier virtual [opt] :: [opt] nested-name-specifier
14080 [opt] class-name
14081
14082 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14083 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14084 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14085 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14086
14087 static tree
14088 cp_parser_base_specifier (cp_parser* parser)
14089 {
14090 cp_token *token;
14091 bool done = false;
14092 bool virtual_p = false;
14093 bool duplicate_virtual_error_issued_p = false;
14094 bool duplicate_access_error_issued_p = false;
14095 bool class_scope_p, template_p;
14096 tree access = access_default_node;
14097 tree type;
14098
14099 /* Process the optional `virtual' and `access-specifier'. */
14100 while (!done)
14101 {
14102 /* Peek at the next token. */
14103 token = cp_lexer_peek_token (parser->lexer);
14104 /* Process `virtual'. */
14105 switch (token->keyword)
14106 {
14107 case RID_VIRTUAL:
14108 /* If `virtual' appears more than once, issue an error. */
14109 if (virtual_p && !duplicate_virtual_error_issued_p)
14110 {
14111 cp_parser_error (parser,
14112 "%<virtual%> specified more than once in base-specified");
14113 duplicate_virtual_error_issued_p = true;
14114 }
14115
14116 virtual_p = true;
14117
14118 /* Consume the `virtual' token. */
14119 cp_lexer_consume_token (parser->lexer);
14120
14121 break;
14122
14123 case RID_PUBLIC:
14124 case RID_PROTECTED:
14125 case RID_PRIVATE:
14126 /* If more than one access specifier appears, issue an
14127 error. */
14128 if (access != access_default_node
14129 && !duplicate_access_error_issued_p)
14130 {
14131 cp_parser_error (parser,
14132 "more than one access specifier in base-specified");
14133 duplicate_access_error_issued_p = true;
14134 }
14135
14136 access = ridpointers[(int) token->keyword];
14137
14138 /* Consume the access-specifier. */
14139 cp_lexer_consume_token (parser->lexer);
14140
14141 break;
14142
14143 default:
14144 done = true;
14145 break;
14146 }
14147 }
14148 /* It is not uncommon to see programs mechanically, erroneously, use
14149 the 'typename' keyword to denote (dependent) qualified types
14150 as base classes. */
14151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14152 {
14153 if (!processing_template_decl)
14154 error ("keyword %<typename%> not allowed outside of templates");
14155 else
14156 error ("keyword %<typename%> not allowed in this context "
14157 "(the base class is implicitly a type)");
14158 cp_lexer_consume_token (parser->lexer);
14159 }
14160
14161 /* Look for the optional `::' operator. */
14162 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14163 /* Look for the nested-name-specifier. The simplest way to
14164 implement:
14165
14166 [temp.res]
14167
14168 The keyword `typename' is not permitted in a base-specifier or
14169 mem-initializer; in these contexts a qualified name that
14170 depends on a template-parameter is implicitly assumed to be a
14171 type name.
14172
14173 is to pretend that we have seen the `typename' keyword at this
14174 point. */
14175 cp_parser_nested_name_specifier_opt (parser,
14176 /*typename_keyword_p=*/true,
14177 /*check_dependency_p=*/true,
14178 typename_type,
14179 /*is_declaration=*/true);
14180 /* If the base class is given by a qualified name, assume that names
14181 we see are type names or templates, as appropriate. */
14182 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14183 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14184
14185 /* Finally, look for the class-name. */
14186 type = cp_parser_class_name (parser,
14187 class_scope_p,
14188 template_p,
14189 typename_type,
14190 /*check_dependency_p=*/true,
14191 /*class_head_p=*/false,
14192 /*is_declaration=*/true);
14193
14194 if (type == error_mark_node)
14195 return error_mark_node;
14196
14197 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14198 }
14199
14200 /* Exception handling [gram.exception] */
14201
14202 /* Parse an (optional) exception-specification.
14203
14204 exception-specification:
14205 throw ( type-id-list [opt] )
14206
14207 Returns a TREE_LIST representing the exception-specification. The
14208 TREE_VALUE of each node is a type. */
14209
14210 static tree
14211 cp_parser_exception_specification_opt (cp_parser* parser)
14212 {
14213 cp_token *token;
14214 tree type_id_list;
14215
14216 /* Peek at the next token. */
14217 token = cp_lexer_peek_token (parser->lexer);
14218 /* If it's not `throw', then there's no exception-specification. */
14219 if (!cp_parser_is_keyword (token, RID_THROW))
14220 return NULL_TREE;
14221
14222 /* Consume the `throw'. */
14223 cp_lexer_consume_token (parser->lexer);
14224
14225 /* Look for the `('. */
14226 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14227
14228 /* Peek at the next token. */
14229 token = cp_lexer_peek_token (parser->lexer);
14230 /* If it's not a `)', then there is a type-id-list. */
14231 if (token->type != CPP_CLOSE_PAREN)
14232 {
14233 const char *saved_message;
14234
14235 /* Types may not be defined in an exception-specification. */
14236 saved_message = parser->type_definition_forbidden_message;
14237 parser->type_definition_forbidden_message
14238 = "types may not be defined in an exception-specification";
14239 /* Parse the type-id-list. */
14240 type_id_list = cp_parser_type_id_list (parser);
14241 /* Restore the saved message. */
14242 parser->type_definition_forbidden_message = saved_message;
14243 }
14244 else
14245 type_id_list = empty_except_spec;
14246
14247 /* Look for the `)'. */
14248 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14249
14250 return type_id_list;
14251 }
14252
14253 /* Parse an (optional) type-id-list.
14254
14255 type-id-list:
14256 type-id
14257 type-id-list , type-id
14258
14259 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14260 in the order that the types were presented. */
14261
14262 static tree
14263 cp_parser_type_id_list (cp_parser* parser)
14264 {
14265 tree types = NULL_TREE;
14266
14267 while (true)
14268 {
14269 cp_token *token;
14270 tree type;
14271
14272 /* Get the next type-id. */
14273 type = cp_parser_type_id (parser);
14274 /* Add it to the list. */
14275 types = add_exception_specifier (types, type, /*complain=*/1);
14276 /* Peek at the next token. */
14277 token = cp_lexer_peek_token (parser->lexer);
14278 /* If it is not a `,', we are done. */
14279 if (token->type != CPP_COMMA)
14280 break;
14281 /* Consume the `,'. */
14282 cp_lexer_consume_token (parser->lexer);
14283 }
14284
14285 return nreverse (types);
14286 }
14287
14288 /* Parse a try-block.
14289
14290 try-block:
14291 try compound-statement handler-seq */
14292
14293 static tree
14294 cp_parser_try_block (cp_parser* parser)
14295 {
14296 tree try_block;
14297
14298 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14299 try_block = begin_try_block ();
14300 cp_parser_compound_statement (parser, NULL, true);
14301 finish_try_block (try_block);
14302 cp_parser_handler_seq (parser);
14303 finish_handler_sequence (try_block);
14304
14305 return try_block;
14306 }
14307
14308 /* Parse a function-try-block.
14309
14310 function-try-block:
14311 try ctor-initializer [opt] function-body handler-seq */
14312
14313 static bool
14314 cp_parser_function_try_block (cp_parser* parser)
14315 {
14316 tree compound_stmt;
14317 tree try_block;
14318 bool ctor_initializer_p;
14319
14320 /* Look for the `try' keyword. */
14321 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14322 return false;
14323 /* Let the rest of the front-end know where we are. */
14324 try_block = begin_function_try_block (&compound_stmt);
14325 /* Parse the function-body. */
14326 ctor_initializer_p
14327 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14328 /* We're done with the `try' part. */
14329 finish_function_try_block (try_block);
14330 /* Parse the handlers. */
14331 cp_parser_handler_seq (parser);
14332 /* We're done with the handlers. */
14333 finish_function_handler_sequence (try_block, compound_stmt);
14334
14335 return ctor_initializer_p;
14336 }
14337
14338 /* Parse a handler-seq.
14339
14340 handler-seq:
14341 handler handler-seq [opt] */
14342
14343 static void
14344 cp_parser_handler_seq (cp_parser* parser)
14345 {
14346 while (true)
14347 {
14348 cp_token *token;
14349
14350 /* Parse the handler. */
14351 cp_parser_handler (parser);
14352 /* Peek at the next token. */
14353 token = cp_lexer_peek_token (parser->lexer);
14354 /* If it's not `catch' then there are no more handlers. */
14355 if (!cp_parser_is_keyword (token, RID_CATCH))
14356 break;
14357 }
14358 }
14359
14360 /* Parse a handler.
14361
14362 handler:
14363 catch ( exception-declaration ) compound-statement */
14364
14365 static void
14366 cp_parser_handler (cp_parser* parser)
14367 {
14368 tree handler;
14369 tree declaration;
14370
14371 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14372 handler = begin_handler ();
14373 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14374 declaration = cp_parser_exception_declaration (parser);
14375 finish_handler_parms (declaration, handler);
14376 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14377 cp_parser_compound_statement (parser, NULL, false);
14378 finish_handler (handler);
14379 }
14380
14381 /* Parse an exception-declaration.
14382
14383 exception-declaration:
14384 type-specifier-seq declarator
14385 type-specifier-seq abstract-declarator
14386 type-specifier-seq
14387 ...
14388
14389 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14390 ellipsis variant is used. */
14391
14392 static tree
14393 cp_parser_exception_declaration (cp_parser* parser)
14394 {
14395 cp_decl_specifier_seq type_specifiers;
14396 cp_declarator *declarator;
14397 const char *saved_message;
14398
14399 /* If it's an ellipsis, it's easy to handle. */
14400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14401 {
14402 /* Consume the `...' token. */
14403 cp_lexer_consume_token (parser->lexer);
14404 return NULL_TREE;
14405 }
14406
14407 /* Types may not be defined in exception-declarations. */
14408 saved_message = parser->type_definition_forbidden_message;
14409 parser->type_definition_forbidden_message
14410 = "types may not be defined in exception-declarations";
14411
14412 /* Parse the type-specifier-seq. */
14413 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14414 &type_specifiers);
14415 /* If it's a `)', then there is no declarator. */
14416 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14417 declarator = NULL;
14418 else
14419 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14420 /*ctor_dtor_or_conv_p=*/NULL,
14421 /*parenthesized_p=*/NULL,
14422 /*member_p=*/false);
14423
14424 /* Restore the saved message. */
14425 parser->type_definition_forbidden_message = saved_message;
14426
14427 if (!type_specifiers.any_specifiers_p)
14428 return error_mark_node;
14429
14430 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14431 }
14432
14433 /* Parse a throw-expression.
14434
14435 throw-expression:
14436 throw assignment-expression [opt]
14437
14438 Returns a THROW_EXPR representing the throw-expression. */
14439
14440 static tree
14441 cp_parser_throw_expression (cp_parser* parser)
14442 {
14443 tree expression;
14444 cp_token* token;
14445
14446 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14447 token = cp_lexer_peek_token (parser->lexer);
14448 /* Figure out whether or not there is an assignment-expression
14449 following the "throw" keyword. */
14450 if (token->type == CPP_COMMA
14451 || token->type == CPP_SEMICOLON
14452 || token->type == CPP_CLOSE_PAREN
14453 || token->type == CPP_CLOSE_SQUARE
14454 || token->type == CPP_CLOSE_BRACE
14455 || token->type == CPP_COLON)
14456 expression = NULL_TREE;
14457 else
14458 expression = cp_parser_assignment_expression (parser,
14459 /*cast_p=*/false);
14460
14461 return build_throw (expression);
14462 }
14463
14464 /* GNU Extensions */
14465
14466 /* Parse an (optional) asm-specification.
14467
14468 asm-specification:
14469 asm ( string-literal )
14470
14471 If the asm-specification is present, returns a STRING_CST
14472 corresponding to the string-literal. Otherwise, returns
14473 NULL_TREE. */
14474
14475 static tree
14476 cp_parser_asm_specification_opt (cp_parser* parser)
14477 {
14478 cp_token *token;
14479 tree asm_specification;
14480
14481 /* Peek at the next token. */
14482 token = cp_lexer_peek_token (parser->lexer);
14483 /* If the next token isn't the `asm' keyword, then there's no
14484 asm-specification. */
14485 if (!cp_parser_is_keyword (token, RID_ASM))
14486 return NULL_TREE;
14487
14488 /* Consume the `asm' token. */
14489 cp_lexer_consume_token (parser->lexer);
14490 /* Look for the `('. */
14491 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14492
14493 /* Look for the string-literal. */
14494 asm_specification = cp_parser_string_literal (parser, false, false);
14495
14496 /* Look for the `)'. */
14497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14498
14499 return asm_specification;
14500 }
14501
14502 /* Parse an asm-operand-list.
14503
14504 asm-operand-list:
14505 asm-operand
14506 asm-operand-list , asm-operand
14507
14508 asm-operand:
14509 string-literal ( expression )
14510 [ string-literal ] string-literal ( expression )
14511
14512 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14513 each node is the expression. The TREE_PURPOSE is itself a
14514 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14515 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14516 is a STRING_CST for the string literal before the parenthesis. */
14517
14518 static tree
14519 cp_parser_asm_operand_list (cp_parser* parser)
14520 {
14521 tree asm_operands = NULL_TREE;
14522
14523 while (true)
14524 {
14525 tree string_literal;
14526 tree expression;
14527 tree name;
14528
14529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14530 {
14531 /* Consume the `[' token. */
14532 cp_lexer_consume_token (parser->lexer);
14533 /* Read the operand name. */
14534 name = cp_parser_identifier (parser);
14535 if (name != error_mark_node)
14536 name = build_string (IDENTIFIER_LENGTH (name),
14537 IDENTIFIER_POINTER (name));
14538 /* Look for the closing `]'. */
14539 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14540 }
14541 else
14542 name = NULL_TREE;
14543 /* Look for the string-literal. */
14544 string_literal = cp_parser_string_literal (parser, false, false);
14545
14546 /* Look for the `('. */
14547 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14548 /* Parse the expression. */
14549 expression = cp_parser_expression (parser, /*cast_p=*/false);
14550 /* Look for the `)'. */
14551 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14552
14553 /* Add this operand to the list. */
14554 asm_operands = tree_cons (build_tree_list (name, string_literal),
14555 expression,
14556 asm_operands);
14557 /* If the next token is not a `,', there are no more
14558 operands. */
14559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14560 break;
14561 /* Consume the `,'. */
14562 cp_lexer_consume_token (parser->lexer);
14563 }
14564
14565 return nreverse (asm_operands);
14566 }
14567
14568 /* Parse an asm-clobber-list.
14569
14570 asm-clobber-list:
14571 string-literal
14572 asm-clobber-list , string-literal
14573
14574 Returns a TREE_LIST, indicating the clobbers in the order that they
14575 appeared. The TREE_VALUE of each node is a STRING_CST. */
14576
14577 static tree
14578 cp_parser_asm_clobber_list (cp_parser* parser)
14579 {
14580 tree clobbers = NULL_TREE;
14581
14582 while (true)
14583 {
14584 tree string_literal;
14585
14586 /* Look for the string literal. */
14587 string_literal = cp_parser_string_literal (parser, false, false);
14588 /* Add it to the list. */
14589 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14590 /* If the next token is not a `,', then the list is
14591 complete. */
14592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14593 break;
14594 /* Consume the `,' token. */
14595 cp_lexer_consume_token (parser->lexer);
14596 }
14597
14598 return clobbers;
14599 }
14600
14601 /* Parse an (optional) series of attributes.
14602
14603 attributes:
14604 attributes attribute
14605
14606 attribute:
14607 __attribute__ (( attribute-list [opt] ))
14608
14609 The return value is as for cp_parser_attribute_list. */
14610
14611 static tree
14612 cp_parser_attributes_opt (cp_parser* parser)
14613 {
14614 tree attributes = NULL_TREE;
14615
14616 while (true)
14617 {
14618 cp_token *token;
14619 tree attribute_list;
14620
14621 /* Peek at the next token. */
14622 token = cp_lexer_peek_token (parser->lexer);
14623 /* If it's not `__attribute__', then we're done. */
14624 if (token->keyword != RID_ATTRIBUTE)
14625 break;
14626
14627 /* Consume the `__attribute__' keyword. */
14628 cp_lexer_consume_token (parser->lexer);
14629 /* Look for the two `(' tokens. */
14630 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14631 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14632
14633 /* Peek at the next token. */
14634 token = cp_lexer_peek_token (parser->lexer);
14635 if (token->type != CPP_CLOSE_PAREN)
14636 /* Parse the attribute-list. */
14637 attribute_list = cp_parser_attribute_list (parser);
14638 else
14639 /* If the next token is a `)', then there is no attribute
14640 list. */
14641 attribute_list = NULL;
14642
14643 /* Look for the two `)' tokens. */
14644 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14645 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14646
14647 /* Add these new attributes to the list. */
14648 attributes = chainon (attributes, attribute_list);
14649 }
14650
14651 return attributes;
14652 }
14653
14654 /* Parse an attribute-list.
14655
14656 attribute-list:
14657 attribute
14658 attribute-list , attribute
14659
14660 attribute:
14661 identifier
14662 identifier ( identifier )
14663 identifier ( identifier , expression-list )
14664 identifier ( expression-list )
14665
14666 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14667 to an attribute. The TREE_PURPOSE of each node is the identifier
14668 indicating which attribute is in use. The TREE_VALUE represents
14669 the arguments, if any. */
14670
14671 static tree
14672 cp_parser_attribute_list (cp_parser* parser)
14673 {
14674 tree attribute_list = NULL_TREE;
14675 bool save_translate_strings_p = parser->translate_strings_p;
14676
14677 parser->translate_strings_p = false;
14678 while (true)
14679 {
14680 cp_token *token;
14681 tree identifier;
14682 tree attribute;
14683
14684 /* Look for the identifier. We also allow keywords here; for
14685 example `__attribute__ ((const))' is legal. */
14686 token = cp_lexer_peek_token (parser->lexer);
14687 if (token->type == CPP_NAME
14688 || token->type == CPP_KEYWORD)
14689 {
14690 tree arguments = NULL_TREE;
14691
14692 /* Consume the token. */
14693 token = cp_lexer_consume_token (parser->lexer);
14694
14695 /* Save away the identifier that indicates which attribute
14696 this is. */
14697 identifier = token->value;
14698 attribute = build_tree_list (identifier, NULL_TREE);
14699
14700 /* Peek at the next token. */
14701 token = cp_lexer_peek_token (parser->lexer);
14702 /* If it's an `(', then parse the attribute arguments. */
14703 if (token->type == CPP_OPEN_PAREN)
14704 {
14705 arguments = cp_parser_parenthesized_expression_list
14706 (parser, true, /*cast_p=*/false,
14707 /*non_constant_p=*/NULL);
14708 /* Save the arguments away. */
14709 TREE_VALUE (attribute) = arguments;
14710 }
14711
14712 if (arguments != error_mark_node)
14713 {
14714 /* Add this attribute to the list. */
14715 TREE_CHAIN (attribute) = attribute_list;
14716 attribute_list = attribute;
14717 }
14718
14719 token = cp_lexer_peek_token (parser->lexer);
14720 }
14721 /* Now, look for more attributes. If the next token isn't a
14722 `,', we're done. */
14723 if (token->type != CPP_COMMA)
14724 break;
14725
14726 /* Consume the comma and keep going. */
14727 cp_lexer_consume_token (parser->lexer);
14728 }
14729 parser->translate_strings_p = save_translate_strings_p;
14730
14731 /* We built up the list in reverse order. */
14732 return nreverse (attribute_list);
14733 }
14734
14735 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14736 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14737 current value of the PEDANTIC flag, regardless of whether or not
14738 the `__extension__' keyword is present. The caller is responsible
14739 for restoring the value of the PEDANTIC flag. */
14740
14741 static bool
14742 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14743 {
14744 /* Save the old value of the PEDANTIC flag. */
14745 *saved_pedantic = pedantic;
14746
14747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14748 {
14749 /* Consume the `__extension__' token. */
14750 cp_lexer_consume_token (parser->lexer);
14751 /* We're not being pedantic while the `__extension__' keyword is
14752 in effect. */
14753 pedantic = 0;
14754
14755 return true;
14756 }
14757
14758 return false;
14759 }
14760
14761 /* Parse a label declaration.
14762
14763 label-declaration:
14764 __label__ label-declarator-seq ;
14765
14766 label-declarator-seq:
14767 identifier , label-declarator-seq
14768 identifier */
14769
14770 static void
14771 cp_parser_label_declaration (cp_parser* parser)
14772 {
14773 /* Look for the `__label__' keyword. */
14774 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14775
14776 while (true)
14777 {
14778 tree identifier;
14779
14780 /* Look for an identifier. */
14781 identifier = cp_parser_identifier (parser);
14782 /* If we failed, stop. */
14783 if (identifier == error_mark_node)
14784 break;
14785 /* Declare it as a label. */
14786 finish_label_decl (identifier);
14787 /* If the next token is a `;', stop. */
14788 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14789 break;
14790 /* Look for the `,' separating the label declarations. */
14791 cp_parser_require (parser, CPP_COMMA, "`,'");
14792 }
14793
14794 /* Look for the final `;'. */
14795 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14796 }
14797
14798 /* Support Functions */
14799
14800 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14801 NAME should have one of the representations used for an
14802 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14803 is returned. If PARSER->SCOPE is a dependent type, then a
14804 SCOPE_REF is returned.
14805
14806 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14807 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14808 was formed. Abstractly, such entities should not be passed to this
14809 function, because they do not need to be looked up, but it is
14810 simpler to check for this special case here, rather than at the
14811 call-sites.
14812
14813 In cases not explicitly covered above, this function returns a
14814 DECL, OVERLOAD, or baselink representing the result of the lookup.
14815 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14816 is returned.
14817
14818 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14819 (e.g., "struct") that was used. In that case bindings that do not
14820 refer to types are ignored.
14821
14822 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14823 ignored.
14824
14825 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14826 are ignored.
14827
14828 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14829 types.
14830
14831 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14832 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14833 NULL_TREE otherwise. */
14834
14835 static tree
14836 cp_parser_lookup_name (cp_parser *parser, tree name,
14837 enum tag_types tag_type,
14838 bool is_template,
14839 bool is_namespace,
14840 bool check_dependency,
14841 tree *ambiguous_decls)
14842 {
14843 int flags = 0;
14844 tree decl;
14845 tree object_type = parser->context->object_type;
14846
14847 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14848 flags |= LOOKUP_COMPLAIN;
14849
14850 /* Assume that the lookup will be unambiguous. */
14851 if (ambiguous_decls)
14852 *ambiguous_decls = NULL_TREE;
14853
14854 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14855 no longer valid. Note that if we are parsing tentatively, and
14856 the parse fails, OBJECT_TYPE will be automatically restored. */
14857 parser->context->object_type = NULL_TREE;
14858
14859 if (name == error_mark_node)
14860 return error_mark_node;
14861
14862 /* A template-id has already been resolved; there is no lookup to
14863 do. */
14864 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14865 return name;
14866 if (BASELINK_P (name))
14867 {
14868 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14869 == TEMPLATE_ID_EXPR);
14870 return name;
14871 }
14872
14873 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14874 it should already have been checked to make sure that the name
14875 used matches the type being destroyed. */
14876 if (TREE_CODE (name) == BIT_NOT_EXPR)
14877 {
14878 tree type;
14879
14880 /* Figure out to which type this destructor applies. */
14881 if (parser->scope)
14882 type = parser->scope;
14883 else if (object_type)
14884 type = object_type;
14885 else
14886 type = current_class_type;
14887 /* If that's not a class type, there is no destructor. */
14888 if (!type || !CLASS_TYPE_P (type))
14889 return error_mark_node;
14890 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14891 lazily_declare_fn (sfk_destructor, type);
14892 if (!CLASSTYPE_DESTRUCTORS (type))
14893 return error_mark_node;
14894 /* If it was a class type, return the destructor. */
14895 return CLASSTYPE_DESTRUCTORS (type);
14896 }
14897
14898 /* By this point, the NAME should be an ordinary identifier. If
14899 the id-expression was a qualified name, the qualifying scope is
14900 stored in PARSER->SCOPE at this point. */
14901 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14902
14903 /* Perform the lookup. */
14904 if (parser->scope)
14905 {
14906 bool dependent_p;
14907
14908 if (parser->scope == error_mark_node)
14909 return error_mark_node;
14910
14911 /* If the SCOPE is dependent, the lookup must be deferred until
14912 the template is instantiated -- unless we are explicitly
14913 looking up names in uninstantiated templates. Even then, we
14914 cannot look up the name if the scope is not a class type; it
14915 might, for example, be a template type parameter. */
14916 dependent_p = (TYPE_P (parser->scope)
14917 && !(parser->in_declarator_p
14918 && currently_open_class (parser->scope))
14919 && dependent_type_p (parser->scope));
14920 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14921 && dependent_p)
14922 {
14923 if (tag_type)
14924 {
14925 tree type;
14926
14927 /* The resolution to Core Issue 180 says that `struct
14928 A::B' should be considered a type-name, even if `A'
14929 is dependent. */
14930 type = make_typename_type (parser->scope, name, tag_type,
14931 /*complain=*/tf_error);
14932 decl = TYPE_NAME (type);
14933 }
14934 else if (is_template
14935 && (cp_parser_next_token_ends_template_argument_p (parser)
14936 || cp_lexer_next_token_is (parser->lexer,
14937 CPP_CLOSE_PAREN)))
14938 decl = make_unbound_class_template (parser->scope,
14939 name, NULL_TREE,
14940 /*complain=*/tf_error);
14941 else
14942 decl = build_qualified_name (/*type=*/NULL_TREE,
14943 parser->scope, name,
14944 is_template);
14945 }
14946 else
14947 {
14948 tree pushed_scope = NULL_TREE;
14949
14950 /* If PARSER->SCOPE is a dependent type, then it must be a
14951 class type, and we must not be checking dependencies;
14952 otherwise, we would have processed this lookup above. So
14953 that PARSER->SCOPE is not considered a dependent base by
14954 lookup_member, we must enter the scope here. */
14955 if (dependent_p)
14956 pushed_scope = push_scope (parser->scope);
14957 /* If the PARSER->SCOPE is a template specialization, it
14958 may be instantiated during name lookup. In that case,
14959 errors may be issued. Even if we rollback the current
14960 tentative parse, those errors are valid. */
14961 decl = lookup_qualified_name (parser->scope, name,
14962 tag_type != none_type,
14963 /*complain=*/true);
14964 if (pushed_scope)
14965 pop_scope (pushed_scope);
14966 }
14967 parser->qualifying_scope = parser->scope;
14968 parser->object_scope = NULL_TREE;
14969 }
14970 else if (object_type)
14971 {
14972 tree object_decl = NULL_TREE;
14973 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14974 OBJECT_TYPE is not a class. */
14975 if (CLASS_TYPE_P (object_type))
14976 /* If the OBJECT_TYPE is a template specialization, it may
14977 be instantiated during name lookup. In that case, errors
14978 may be issued. Even if we rollback the current tentative
14979 parse, those errors are valid. */
14980 object_decl = lookup_member (object_type,
14981 name,
14982 /*protect=*/0,
14983 tag_type != none_type);
14984 /* Look it up in the enclosing context, too. */
14985 decl = lookup_name_real (name, tag_type != none_type,
14986 /*nonclass=*/0,
14987 /*block_p=*/true, is_namespace, flags);
14988 parser->object_scope = object_type;
14989 parser->qualifying_scope = NULL_TREE;
14990 if (object_decl)
14991 decl = object_decl;
14992 }
14993 else
14994 {
14995 decl = lookup_name_real (name, tag_type != none_type,
14996 /*nonclass=*/0,
14997 /*block_p=*/true, is_namespace, flags);
14998 parser->qualifying_scope = NULL_TREE;
14999 parser->object_scope = NULL_TREE;
15000 }
15001
15002 /* If the lookup failed, let our caller know. */
15003 if (!decl || decl == error_mark_node)
15004 return error_mark_node;
15005
15006 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15007 if (TREE_CODE (decl) == TREE_LIST)
15008 {
15009 if (ambiguous_decls)
15010 *ambiguous_decls = decl;
15011 /* The error message we have to print is too complicated for
15012 cp_parser_error, so we incorporate its actions directly. */
15013 if (!cp_parser_simulate_error (parser))
15014 {
15015 error ("reference to %qD is ambiguous", name);
15016 print_candidates (decl);
15017 }
15018 return error_mark_node;
15019 }
15020
15021 gcc_assert (DECL_P (decl)
15022 || TREE_CODE (decl) == OVERLOAD
15023 || TREE_CODE (decl) == SCOPE_REF
15024 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15025 || BASELINK_P (decl));
15026
15027 /* If we have resolved the name of a member declaration, check to
15028 see if the declaration is accessible. When the name resolves to
15029 set of overloaded functions, accessibility is checked when
15030 overload resolution is done.
15031
15032 During an explicit instantiation, access is not checked at all,
15033 as per [temp.explicit]. */
15034 if (DECL_P (decl))
15035 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15036
15037 return decl;
15038 }
15039
15040 /* Like cp_parser_lookup_name, but for use in the typical case where
15041 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15042 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15043
15044 static tree
15045 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15046 {
15047 return cp_parser_lookup_name (parser, name,
15048 none_type,
15049 /*is_template=*/false,
15050 /*is_namespace=*/false,
15051 /*check_dependency=*/true,
15052 /*ambiguous_decls=*/NULL);
15053 }
15054
15055 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15056 the current context, return the TYPE_DECL. If TAG_NAME_P is
15057 true, the DECL indicates the class being defined in a class-head,
15058 or declared in an elaborated-type-specifier.
15059
15060 Otherwise, return DECL. */
15061
15062 static tree
15063 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15064 {
15065 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15066 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15067
15068 struct A {
15069 template <typename T> struct B;
15070 };
15071
15072 template <typename T> struct A::B {};
15073
15074 Similarly, in an elaborated-type-specifier:
15075
15076 namespace N { struct X{}; }
15077
15078 struct A {
15079 template <typename T> friend struct N::X;
15080 };
15081
15082 However, if the DECL refers to a class type, and we are in
15083 the scope of the class, then the name lookup automatically
15084 finds the TYPE_DECL created by build_self_reference rather
15085 than a TEMPLATE_DECL. For example, in:
15086
15087 template <class T> struct S {
15088 S s;
15089 };
15090
15091 there is no need to handle such case. */
15092
15093 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15094 return DECL_TEMPLATE_RESULT (decl);
15095
15096 return decl;
15097 }
15098
15099 /* If too many, or too few, template-parameter lists apply to the
15100 declarator, issue an error message. Returns TRUE if all went well,
15101 and FALSE otherwise. */
15102
15103 static bool
15104 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15105 cp_declarator *declarator)
15106 {
15107 unsigned num_templates;
15108
15109 /* We haven't seen any classes that involve template parameters yet. */
15110 num_templates = 0;
15111
15112 switch (declarator->kind)
15113 {
15114 case cdk_id:
15115 if (declarator->u.id.qualifying_scope)
15116 {
15117 tree scope;
15118 tree member;
15119
15120 scope = declarator->u.id.qualifying_scope;
15121 member = declarator->u.id.unqualified_name;
15122
15123 while (scope && CLASS_TYPE_P (scope))
15124 {
15125 /* You're supposed to have one `template <...>'
15126 for every template class, but you don't need one
15127 for a full specialization. For example:
15128
15129 template <class T> struct S{};
15130 template <> struct S<int> { void f(); };
15131 void S<int>::f () {}
15132
15133 is correct; there shouldn't be a `template <>' for
15134 the definition of `S<int>::f'. */
15135 if (CLASSTYPE_TEMPLATE_INFO (scope)
15136 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15137 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15138 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15139 ++num_templates;
15140
15141 scope = TYPE_CONTEXT (scope);
15142 }
15143 }
15144 else if (TREE_CODE (declarator->u.id.unqualified_name)
15145 == TEMPLATE_ID_EXPR)
15146 /* If the DECLARATOR has the form `X<y>' then it uses one
15147 additional level of template parameters. */
15148 ++num_templates;
15149
15150 return cp_parser_check_template_parameters (parser,
15151 num_templates);
15152
15153 case cdk_function:
15154 case cdk_array:
15155 case cdk_pointer:
15156 case cdk_reference:
15157 case cdk_ptrmem:
15158 return (cp_parser_check_declarator_template_parameters
15159 (parser, declarator->declarator));
15160
15161 case cdk_error:
15162 return true;
15163
15164 default:
15165 gcc_unreachable ();
15166 }
15167 return false;
15168 }
15169
15170 /* NUM_TEMPLATES were used in the current declaration. If that is
15171 invalid, return FALSE and issue an error messages. Otherwise,
15172 return TRUE. */
15173
15174 static bool
15175 cp_parser_check_template_parameters (cp_parser* parser,
15176 unsigned num_templates)
15177 {
15178 /* If there are more template classes than parameter lists, we have
15179 something like:
15180
15181 template <class T> void S<T>::R<T>::f (); */
15182 if (parser->num_template_parameter_lists < num_templates)
15183 {
15184 error ("too few template-parameter-lists");
15185 return false;
15186 }
15187 /* If there are the same number of template classes and parameter
15188 lists, that's OK. */
15189 if (parser->num_template_parameter_lists == num_templates)
15190 return true;
15191 /* If there are more, but only one more, then we are referring to a
15192 member template. That's OK too. */
15193 if (parser->num_template_parameter_lists == num_templates + 1)
15194 return true;
15195 /* Otherwise, there are too many template parameter lists. We have
15196 something like:
15197
15198 template <class T> template <class U> void S::f(); */
15199 error ("too many template-parameter-lists");
15200 return false;
15201 }
15202
15203 /* Parse an optional `::' token indicating that the following name is
15204 from the global namespace. If so, PARSER->SCOPE is set to the
15205 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15206 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15207 Returns the new value of PARSER->SCOPE, if the `::' token is
15208 present, and NULL_TREE otherwise. */
15209
15210 static tree
15211 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15212 {
15213 cp_token *token;
15214
15215 /* Peek at the next token. */
15216 token = cp_lexer_peek_token (parser->lexer);
15217 /* If we're looking at a `::' token then we're starting from the
15218 global namespace, not our current location. */
15219 if (token->type == CPP_SCOPE)
15220 {
15221 /* Consume the `::' token. */
15222 cp_lexer_consume_token (parser->lexer);
15223 /* Set the SCOPE so that we know where to start the lookup. */
15224 parser->scope = global_namespace;
15225 parser->qualifying_scope = global_namespace;
15226 parser->object_scope = NULL_TREE;
15227
15228 return parser->scope;
15229 }
15230 else if (!current_scope_valid_p)
15231 {
15232 parser->scope = NULL_TREE;
15233 parser->qualifying_scope = NULL_TREE;
15234 parser->object_scope = NULL_TREE;
15235 }
15236
15237 return NULL_TREE;
15238 }
15239
15240 /* Returns TRUE if the upcoming token sequence is the start of a
15241 constructor declarator. If FRIEND_P is true, the declarator is
15242 preceded by the `friend' specifier. */
15243
15244 static bool
15245 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15246 {
15247 bool constructor_p;
15248 tree type_decl = NULL_TREE;
15249 bool nested_name_p;
15250 cp_token *next_token;
15251
15252 /* The common case is that this is not a constructor declarator, so
15253 try to avoid doing lots of work if at all possible. It's not
15254 valid declare a constructor at function scope. */
15255 if (at_function_scope_p ())
15256 return false;
15257 /* And only certain tokens can begin a constructor declarator. */
15258 next_token = cp_lexer_peek_token (parser->lexer);
15259 if (next_token->type != CPP_NAME
15260 && next_token->type != CPP_SCOPE
15261 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15262 && next_token->type != CPP_TEMPLATE_ID)
15263 return false;
15264
15265 /* Parse tentatively; we are going to roll back all of the tokens
15266 consumed here. */
15267 cp_parser_parse_tentatively (parser);
15268 /* Assume that we are looking at a constructor declarator. */
15269 constructor_p = true;
15270
15271 /* Look for the optional `::' operator. */
15272 cp_parser_global_scope_opt (parser,
15273 /*current_scope_valid_p=*/false);
15274 /* Look for the nested-name-specifier. */
15275 nested_name_p
15276 = (cp_parser_nested_name_specifier_opt (parser,
15277 /*typename_keyword_p=*/false,
15278 /*check_dependency_p=*/false,
15279 /*type_p=*/false,
15280 /*is_declaration=*/false)
15281 != NULL_TREE);
15282 /* Outside of a class-specifier, there must be a
15283 nested-name-specifier. */
15284 if (!nested_name_p &&
15285 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15286 || friend_p))
15287 constructor_p = false;
15288 /* If we still think that this might be a constructor-declarator,
15289 look for a class-name. */
15290 if (constructor_p)
15291 {
15292 /* If we have:
15293
15294 template <typename T> struct S { S(); };
15295 template <typename T> S<T>::S ();
15296
15297 we must recognize that the nested `S' names a class.
15298 Similarly, for:
15299
15300 template <typename T> S<T>::S<T> ();
15301
15302 we must recognize that the nested `S' names a template. */
15303 type_decl = cp_parser_class_name (parser,
15304 /*typename_keyword_p=*/false,
15305 /*template_keyword_p=*/false,
15306 none_type,
15307 /*check_dependency_p=*/false,
15308 /*class_head_p=*/false,
15309 /*is_declaration=*/false);
15310 /* If there was no class-name, then this is not a constructor. */
15311 constructor_p = !cp_parser_error_occurred (parser);
15312 }
15313
15314 /* If we're still considering a constructor, we have to see a `(',
15315 to begin the parameter-declaration-clause, followed by either a
15316 `)', an `...', or a decl-specifier. We need to check for a
15317 type-specifier to avoid being fooled into thinking that:
15318
15319 S::S (f) (int);
15320
15321 is a constructor. (It is actually a function named `f' that
15322 takes one parameter (of type `int') and returns a value of type
15323 `S::S'. */
15324 if (constructor_p
15325 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15326 {
15327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15328 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15329 /* A parameter declaration begins with a decl-specifier,
15330 which is either the "attribute" keyword, a storage class
15331 specifier, or (usually) a type-specifier. */
15332 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15333 && !cp_parser_storage_class_specifier_opt (parser))
15334 {
15335 tree type;
15336 tree pushed_scope = NULL_TREE;
15337 unsigned saved_num_template_parameter_lists;
15338
15339 /* Names appearing in the type-specifier should be looked up
15340 in the scope of the class. */
15341 if (current_class_type)
15342 type = NULL_TREE;
15343 else
15344 {
15345 type = TREE_TYPE (type_decl);
15346 if (TREE_CODE (type) == TYPENAME_TYPE)
15347 {
15348 type = resolve_typename_type (type,
15349 /*only_current_p=*/false);
15350 if (type == error_mark_node)
15351 {
15352 cp_parser_abort_tentative_parse (parser);
15353 return false;
15354 }
15355 }
15356 pushed_scope = push_scope (type);
15357 }
15358
15359 /* Inside the constructor parameter list, surrounding
15360 template-parameter-lists do not apply. */
15361 saved_num_template_parameter_lists
15362 = parser->num_template_parameter_lists;
15363 parser->num_template_parameter_lists = 0;
15364
15365 /* Look for the type-specifier. */
15366 cp_parser_type_specifier (parser,
15367 CP_PARSER_FLAGS_NONE,
15368 /*decl_specs=*/NULL,
15369 /*is_declarator=*/true,
15370 /*declares_class_or_enum=*/NULL,
15371 /*is_cv_qualifier=*/NULL);
15372
15373 parser->num_template_parameter_lists
15374 = saved_num_template_parameter_lists;
15375
15376 /* Leave the scope of the class. */
15377 if (pushed_scope)
15378 pop_scope (pushed_scope);
15379
15380 constructor_p = !cp_parser_error_occurred (parser);
15381 }
15382 }
15383 else
15384 constructor_p = false;
15385 /* We did not really want to consume any tokens. */
15386 cp_parser_abort_tentative_parse (parser);
15387
15388 return constructor_p;
15389 }
15390
15391 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15392 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15393 they must be performed once we are in the scope of the function.
15394
15395 Returns the function defined. */
15396
15397 static tree
15398 cp_parser_function_definition_from_specifiers_and_declarator
15399 (cp_parser* parser,
15400 cp_decl_specifier_seq *decl_specifiers,
15401 tree attributes,
15402 const cp_declarator *declarator)
15403 {
15404 tree fn;
15405 bool success_p;
15406
15407 /* Begin the function-definition. */
15408 success_p = start_function (decl_specifiers, declarator, attributes);
15409
15410 /* The things we're about to see are not directly qualified by any
15411 template headers we've seen thus far. */
15412 reset_specialization ();
15413
15414 /* If there were names looked up in the decl-specifier-seq that we
15415 did not check, check them now. We must wait until we are in the
15416 scope of the function to perform the checks, since the function
15417 might be a friend. */
15418 perform_deferred_access_checks ();
15419
15420 if (!success_p)
15421 {
15422 /* Skip the entire function. */
15423 cp_parser_skip_to_end_of_block_or_statement (parser);
15424 fn = error_mark_node;
15425 }
15426 else
15427 fn = cp_parser_function_definition_after_declarator (parser,
15428 /*inline_p=*/false);
15429
15430 return fn;
15431 }
15432
15433 /* Parse the part of a function-definition that follows the
15434 declarator. INLINE_P is TRUE iff this function is an inline
15435 function defined with a class-specifier.
15436
15437 Returns the function defined. */
15438
15439 static tree
15440 cp_parser_function_definition_after_declarator (cp_parser* parser,
15441 bool inline_p)
15442 {
15443 tree fn;
15444 bool ctor_initializer_p = false;
15445 bool saved_in_unbraced_linkage_specification_p;
15446 unsigned saved_num_template_parameter_lists;
15447
15448 /* If the next token is `return', then the code may be trying to
15449 make use of the "named return value" extension that G++ used to
15450 support. */
15451 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15452 {
15453 /* Consume the `return' keyword. */
15454 cp_lexer_consume_token (parser->lexer);
15455 /* Look for the identifier that indicates what value is to be
15456 returned. */
15457 cp_parser_identifier (parser);
15458 /* Issue an error message. */
15459 error ("named return values are no longer supported");
15460 /* Skip tokens until we reach the start of the function body. */
15461 while (true)
15462 {
15463 cp_token *token = cp_lexer_peek_token (parser->lexer);
15464 if (token->type == CPP_OPEN_BRACE
15465 || token->type == CPP_EOF
15466 || token->type == CPP_PRAGMA_EOL)
15467 break;
15468 cp_lexer_consume_token (parser->lexer);
15469 }
15470 }
15471 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15472 anything declared inside `f'. */
15473 saved_in_unbraced_linkage_specification_p
15474 = parser->in_unbraced_linkage_specification_p;
15475 parser->in_unbraced_linkage_specification_p = false;
15476 /* Inside the function, surrounding template-parameter-lists do not
15477 apply. */
15478 saved_num_template_parameter_lists
15479 = parser->num_template_parameter_lists;
15480 parser->num_template_parameter_lists = 0;
15481 /* If the next token is `try', then we are looking at a
15482 function-try-block. */
15483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15484 ctor_initializer_p = cp_parser_function_try_block (parser);
15485 /* A function-try-block includes the function-body, so we only do
15486 this next part if we're not processing a function-try-block. */
15487 else
15488 ctor_initializer_p
15489 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15490
15491 /* Finish the function. */
15492 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15493 (inline_p ? 2 : 0));
15494 /* Generate code for it, if necessary. */
15495 expand_or_defer_fn (fn);
15496 /* Restore the saved values. */
15497 parser->in_unbraced_linkage_specification_p
15498 = saved_in_unbraced_linkage_specification_p;
15499 parser->num_template_parameter_lists
15500 = saved_num_template_parameter_lists;
15501
15502 return fn;
15503 }
15504
15505 /* Parse a template-declaration, assuming that the `export' (and
15506 `extern') keywords, if present, has already been scanned. MEMBER_P
15507 is as for cp_parser_template_declaration. */
15508
15509 static void
15510 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15511 {
15512 tree decl = NULL_TREE;
15513 tree checks;
15514 tree parameter_list;
15515 bool friend_p = false;
15516 bool need_lang_pop;
15517
15518 /* Look for the `template' keyword. */
15519 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15520 return;
15521
15522 /* And the `<'. */
15523 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15524 return;
15525 /* [temp]
15526
15527 A template ... shall not have C linkage. */
15528 if (current_lang_name == lang_name_c)
15529 {
15530 error ("template with C linkage");
15531 /* Give it C++ linkage to avoid confusing other parts of the
15532 front end. */
15533 push_lang_context (lang_name_cplusplus);
15534 need_lang_pop = true;
15535 }
15536 else
15537 need_lang_pop = false;
15538
15539 /* We cannot perform access checks on the template parameter
15540 declarations until we know what is being declared, just as we
15541 cannot check the decl-specifier list. */
15542 push_deferring_access_checks (dk_deferred);
15543
15544 /* If the next token is `>', then we have an invalid
15545 specialization. Rather than complain about an invalid template
15546 parameter, issue an error message here. */
15547 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15548 {
15549 cp_parser_error (parser, "invalid explicit specialization");
15550 begin_specialization ();
15551 parameter_list = NULL_TREE;
15552 }
15553 else
15554 /* Parse the template parameters. */
15555 parameter_list = cp_parser_template_parameter_list (parser);
15556
15557 /* Get the deferred access checks from the parameter list. These
15558 will be checked once we know what is being declared, as for a
15559 member template the checks must be performed in the scope of the
15560 class containing the member. */
15561 checks = get_deferred_access_checks ();
15562
15563 /* Look for the `>'. */
15564 cp_parser_skip_to_end_of_template_parameter_list (parser);
15565 /* We just processed one more parameter list. */
15566 ++parser->num_template_parameter_lists;
15567 /* If the next token is `template', there are more template
15568 parameters. */
15569 if (cp_lexer_next_token_is_keyword (parser->lexer,
15570 RID_TEMPLATE))
15571 cp_parser_template_declaration_after_export (parser, member_p);
15572 else
15573 {
15574 /* There are no access checks when parsing a template, as we do not
15575 know if a specialization will be a friend. */
15576 push_deferring_access_checks (dk_no_check);
15577 decl = cp_parser_single_declaration (parser,
15578 checks,
15579 member_p,
15580 &friend_p);
15581 pop_deferring_access_checks ();
15582
15583 /* If this is a member template declaration, let the front
15584 end know. */
15585 if (member_p && !friend_p && decl)
15586 {
15587 if (TREE_CODE (decl) == TYPE_DECL)
15588 cp_parser_check_access_in_redeclaration (decl);
15589
15590 decl = finish_member_template_decl (decl);
15591 }
15592 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15593 make_friend_class (current_class_type, TREE_TYPE (decl),
15594 /*complain=*/true);
15595 }
15596 /* We are done with the current parameter list. */
15597 --parser->num_template_parameter_lists;
15598
15599 pop_deferring_access_checks ();
15600
15601 /* Finish up. */
15602 finish_template_decl (parameter_list);
15603
15604 /* Register member declarations. */
15605 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15606 finish_member_declaration (decl);
15607 /* For the erroneous case of a template with C linkage, we pushed an
15608 implicit C++ linkage scope; exit that scope now. */
15609 if (need_lang_pop)
15610 pop_lang_context ();
15611 /* If DECL is a function template, we must return to parse it later.
15612 (Even though there is no definition, there might be default
15613 arguments that need handling.) */
15614 if (member_p && decl
15615 && (TREE_CODE (decl) == FUNCTION_DECL
15616 || DECL_FUNCTION_TEMPLATE_P (decl)))
15617 TREE_VALUE (parser->unparsed_functions_queues)
15618 = tree_cons (NULL_TREE, decl,
15619 TREE_VALUE (parser->unparsed_functions_queues));
15620 }
15621
15622 /* Perform the deferred access checks from a template-parameter-list.
15623 CHECKS is a TREE_LIST of access checks, as returned by
15624 get_deferred_access_checks. */
15625
15626 static void
15627 cp_parser_perform_template_parameter_access_checks (tree checks)
15628 {
15629 ++processing_template_parmlist;
15630 perform_access_checks (checks);
15631 --processing_template_parmlist;
15632 }
15633
15634 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15635 `function-definition' sequence. MEMBER_P is true, this declaration
15636 appears in a class scope.
15637
15638 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15639 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15640
15641 static tree
15642 cp_parser_single_declaration (cp_parser* parser,
15643 tree checks,
15644 bool member_p,
15645 bool* friend_p)
15646 {
15647 int declares_class_or_enum;
15648 tree decl = NULL_TREE;
15649 cp_decl_specifier_seq decl_specifiers;
15650 bool function_definition_p = false;
15651
15652 /* This function is only used when processing a template
15653 declaration. */
15654 gcc_assert (innermost_scope_kind () == sk_template_parms
15655 || innermost_scope_kind () == sk_template_spec);
15656
15657 /* Defer access checks until we know what is being declared. */
15658 push_deferring_access_checks (dk_deferred);
15659
15660 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15661 alternative. */
15662 cp_parser_decl_specifier_seq (parser,
15663 CP_PARSER_FLAGS_OPTIONAL,
15664 &decl_specifiers,
15665 &declares_class_or_enum);
15666 if (friend_p)
15667 *friend_p = cp_parser_friend_p (&decl_specifiers);
15668
15669 /* There are no template typedefs. */
15670 if (decl_specifiers.specs[(int) ds_typedef])
15671 {
15672 error ("template declaration of %qs", "typedef");
15673 decl = error_mark_node;
15674 }
15675
15676 /* Gather up the access checks that occurred the
15677 decl-specifier-seq. */
15678 stop_deferring_access_checks ();
15679
15680 /* Check for the declaration of a template class. */
15681 if (declares_class_or_enum)
15682 {
15683 if (cp_parser_declares_only_class_p (parser))
15684 {
15685 decl = shadow_tag (&decl_specifiers);
15686
15687 /* In this case:
15688
15689 struct C {
15690 friend template <typename T> struct A<T>::B;
15691 };
15692
15693 A<T>::B will be represented by a TYPENAME_TYPE, and
15694 therefore not recognized by shadow_tag. */
15695 if (friend_p && *friend_p
15696 && !decl
15697 && decl_specifiers.type
15698 && TYPE_P (decl_specifiers.type))
15699 decl = decl_specifiers.type;
15700
15701 if (decl && decl != error_mark_node)
15702 decl = TYPE_NAME (decl);
15703 else
15704 decl = error_mark_node;
15705
15706 /* Perform access checks for template parameters. */
15707 cp_parser_perform_template_parameter_access_checks (checks);
15708 }
15709 }
15710 /* If it's not a template class, try for a template function. If
15711 the next token is a `;', then this declaration does not declare
15712 anything. But, if there were errors in the decl-specifiers, then
15713 the error might well have come from an attempted class-specifier.
15714 In that case, there's no need to warn about a missing declarator. */
15715 if (!decl
15716 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15717 || decl_specifiers.type != error_mark_node))
15718 decl = cp_parser_init_declarator (parser,
15719 &decl_specifiers,
15720 checks,
15721 /*function_definition_allowed_p=*/true,
15722 member_p,
15723 declares_class_or_enum,
15724 &function_definition_p);
15725
15726 pop_deferring_access_checks ();
15727
15728 /* Clear any current qualification; whatever comes next is the start
15729 of something new. */
15730 parser->scope = NULL_TREE;
15731 parser->qualifying_scope = NULL_TREE;
15732 parser->object_scope = NULL_TREE;
15733 /* Look for a trailing `;' after the declaration. */
15734 if (!function_definition_p
15735 && (decl == error_mark_node
15736 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15737 cp_parser_skip_to_end_of_block_or_statement (parser);
15738
15739 return decl;
15740 }
15741
15742 /* Parse a cast-expression that is not the operand of a unary "&". */
15743
15744 static tree
15745 cp_parser_simple_cast_expression (cp_parser *parser)
15746 {
15747 return cp_parser_cast_expression (parser, /*address_p=*/false,
15748 /*cast_p=*/false);
15749 }
15750
15751 /* Parse a functional cast to TYPE. Returns an expression
15752 representing the cast. */
15753
15754 static tree
15755 cp_parser_functional_cast (cp_parser* parser, tree type)
15756 {
15757 tree expression_list;
15758 tree cast;
15759
15760 expression_list
15761 = cp_parser_parenthesized_expression_list (parser, false,
15762 /*cast_p=*/true,
15763 /*non_constant_p=*/NULL);
15764
15765 cast = build_functional_cast (type, expression_list);
15766 /* [expr.const]/1: In an integral constant expression "only type
15767 conversions to integral or enumeration type can be used". */
15768 if (TREE_CODE (type) == TYPE_DECL)
15769 type = TREE_TYPE (type);
15770 if (cast != error_mark_node
15771 && !cast_valid_in_integral_constant_expression_p (type)
15772 && (cp_parser_non_integral_constant_expression
15773 (parser, "a call to a constructor")))
15774 return error_mark_node;
15775 return cast;
15776 }
15777
15778 /* Save the tokens that make up the body of a member function defined
15779 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15780 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15781 specifiers applied to the declaration. Returns the FUNCTION_DECL
15782 for the member function. */
15783
15784 static tree
15785 cp_parser_save_member_function_body (cp_parser* parser,
15786 cp_decl_specifier_seq *decl_specifiers,
15787 cp_declarator *declarator,
15788 tree attributes)
15789 {
15790 cp_token *first;
15791 cp_token *last;
15792 tree fn;
15793
15794 /* Create the function-declaration. */
15795 fn = start_method (decl_specifiers, declarator, attributes);
15796 /* If something went badly wrong, bail out now. */
15797 if (fn == error_mark_node)
15798 {
15799 /* If there's a function-body, skip it. */
15800 if (cp_parser_token_starts_function_definition_p
15801 (cp_lexer_peek_token (parser->lexer)))
15802 cp_parser_skip_to_end_of_block_or_statement (parser);
15803 return error_mark_node;
15804 }
15805
15806 /* Remember it, if there default args to post process. */
15807 cp_parser_save_default_args (parser, fn);
15808
15809 /* Save away the tokens that make up the body of the
15810 function. */
15811 first = parser->lexer->next_token;
15812 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15813 /* Handle function try blocks. */
15814 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15815 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15816 last = parser->lexer->next_token;
15817
15818 /* Save away the inline definition; we will process it when the
15819 class is complete. */
15820 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15821 DECL_PENDING_INLINE_P (fn) = 1;
15822
15823 /* We need to know that this was defined in the class, so that
15824 friend templates are handled correctly. */
15825 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15826
15827 /* We're done with the inline definition. */
15828 finish_method (fn);
15829
15830 /* Add FN to the queue of functions to be parsed later. */
15831 TREE_VALUE (parser->unparsed_functions_queues)
15832 = tree_cons (NULL_TREE, fn,
15833 TREE_VALUE (parser->unparsed_functions_queues));
15834
15835 return fn;
15836 }
15837
15838 /* Parse a template-argument-list, as well as the trailing ">" (but
15839 not the opening ">"). See cp_parser_template_argument_list for the
15840 return value. */
15841
15842 static tree
15843 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15844 {
15845 tree arguments;
15846 tree saved_scope;
15847 tree saved_qualifying_scope;
15848 tree saved_object_scope;
15849 bool saved_greater_than_is_operator_p;
15850 bool saved_skip_evaluation;
15851
15852 /* [temp.names]
15853
15854 When parsing a template-id, the first non-nested `>' is taken as
15855 the end of the template-argument-list rather than a greater-than
15856 operator. */
15857 saved_greater_than_is_operator_p
15858 = parser->greater_than_is_operator_p;
15859 parser->greater_than_is_operator_p = false;
15860 /* Parsing the argument list may modify SCOPE, so we save it
15861 here. */
15862 saved_scope = parser->scope;
15863 saved_qualifying_scope = parser->qualifying_scope;
15864 saved_object_scope = parser->object_scope;
15865 /* We need to evaluate the template arguments, even though this
15866 template-id may be nested within a "sizeof". */
15867 saved_skip_evaluation = skip_evaluation;
15868 skip_evaluation = false;
15869 /* Parse the template-argument-list itself. */
15870 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15871 arguments = NULL_TREE;
15872 else
15873 arguments = cp_parser_template_argument_list (parser);
15874 /* Look for the `>' that ends the template-argument-list. If we find
15875 a '>>' instead, it's probably just a typo. */
15876 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15877 {
15878 if (!saved_greater_than_is_operator_p)
15879 {
15880 /* If we're in a nested template argument list, the '>>' has
15881 to be a typo for '> >'. We emit the error message, but we
15882 continue parsing and we push a '>' as next token, so that
15883 the argument list will be parsed correctly. Note that the
15884 global source location is still on the token before the
15885 '>>', so we need to say explicitly where we want it. */
15886 cp_token *token = cp_lexer_peek_token (parser->lexer);
15887 error ("%H%<>>%> should be %<> >%> "
15888 "within a nested template argument list",
15889 &token->location);
15890
15891 /* ??? Proper recovery should terminate two levels of
15892 template argument list here. */
15893 token->type = CPP_GREATER;
15894 }
15895 else
15896 {
15897 /* If this is not a nested template argument list, the '>>'
15898 is a typo for '>'. Emit an error message and continue.
15899 Same deal about the token location, but here we can get it
15900 right by consuming the '>>' before issuing the diagnostic. */
15901 cp_lexer_consume_token (parser->lexer);
15902 error ("spurious %<>>%>, use %<>%> to terminate "
15903 "a template argument list");
15904 }
15905 }
15906 else
15907 cp_parser_skip_to_end_of_template_parameter_list (parser);
15908 /* The `>' token might be a greater-than operator again now. */
15909 parser->greater_than_is_operator_p
15910 = saved_greater_than_is_operator_p;
15911 /* Restore the SAVED_SCOPE. */
15912 parser->scope = saved_scope;
15913 parser->qualifying_scope = saved_qualifying_scope;
15914 parser->object_scope = saved_object_scope;
15915 skip_evaluation = saved_skip_evaluation;
15916
15917 return arguments;
15918 }
15919
15920 /* MEMBER_FUNCTION is a member function, or a friend. If default
15921 arguments, or the body of the function have not yet been parsed,
15922 parse them now. */
15923
15924 static void
15925 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15926 {
15927 /* If this member is a template, get the underlying
15928 FUNCTION_DECL. */
15929 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15930 member_function = DECL_TEMPLATE_RESULT (member_function);
15931
15932 /* There should not be any class definitions in progress at this
15933 point; the bodies of members are only parsed outside of all class
15934 definitions. */
15935 gcc_assert (parser->num_classes_being_defined == 0);
15936 /* While we're parsing the member functions we might encounter more
15937 classes. We want to handle them right away, but we don't want
15938 them getting mixed up with functions that are currently in the
15939 queue. */
15940 parser->unparsed_functions_queues
15941 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15942
15943 /* Make sure that any template parameters are in scope. */
15944 maybe_begin_member_template_processing (member_function);
15945
15946 /* If the body of the function has not yet been parsed, parse it
15947 now. */
15948 if (DECL_PENDING_INLINE_P (member_function))
15949 {
15950 tree function_scope;
15951 cp_token_cache *tokens;
15952
15953 /* The function is no longer pending; we are processing it. */
15954 tokens = DECL_PENDING_INLINE_INFO (member_function);
15955 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15956 DECL_PENDING_INLINE_P (member_function) = 0;
15957
15958 /* If this is a local class, enter the scope of the containing
15959 function. */
15960 function_scope = current_function_decl;
15961 if (function_scope)
15962 push_function_context_to (function_scope);
15963
15964
15965 /* Push the body of the function onto the lexer stack. */
15966 cp_parser_push_lexer_for_tokens (parser, tokens);
15967
15968 /* Let the front end know that we going to be defining this
15969 function. */
15970 start_preparsed_function (member_function, NULL_TREE,
15971 SF_PRE_PARSED | SF_INCLASS_INLINE);
15972
15973 /* Don't do access checking if it is a templated function. */
15974 if (processing_template_decl)
15975 push_deferring_access_checks (dk_no_check);
15976
15977 /* Now, parse the body of the function. */
15978 cp_parser_function_definition_after_declarator (parser,
15979 /*inline_p=*/true);
15980
15981 if (processing_template_decl)
15982 pop_deferring_access_checks ();
15983
15984 /* Leave the scope of the containing function. */
15985 if (function_scope)
15986 pop_function_context_from (function_scope);
15987 cp_parser_pop_lexer (parser);
15988 }
15989
15990 /* Remove any template parameters from the symbol table. */
15991 maybe_end_member_template_processing ();
15992
15993 /* Restore the queue. */
15994 parser->unparsed_functions_queues
15995 = TREE_CHAIN (parser->unparsed_functions_queues);
15996 }
15997
15998 /* If DECL contains any default args, remember it on the unparsed
15999 functions queue. */
16000
16001 static void
16002 cp_parser_save_default_args (cp_parser* parser, tree decl)
16003 {
16004 tree probe;
16005
16006 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16007 probe;
16008 probe = TREE_CHAIN (probe))
16009 if (TREE_PURPOSE (probe))
16010 {
16011 TREE_PURPOSE (parser->unparsed_functions_queues)
16012 = tree_cons (current_class_type, decl,
16013 TREE_PURPOSE (parser->unparsed_functions_queues));
16014 break;
16015 }
16016 }
16017
16018 /* FN is a FUNCTION_DECL which may contains a parameter with an
16019 unparsed DEFAULT_ARG. Parse the default args now. This function
16020 assumes that the current scope is the scope in which the default
16021 argument should be processed. */
16022
16023 static void
16024 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16025 {
16026 bool saved_local_variables_forbidden_p;
16027 tree parm;
16028
16029 /* While we're parsing the default args, we might (due to the
16030 statement expression extension) encounter more classes. We want
16031 to handle them right away, but we don't want them getting mixed
16032 up with default args that are currently in the queue. */
16033 parser->unparsed_functions_queues
16034 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16035
16036 /* Local variable names (and the `this' keyword) may not appear
16037 in a default argument. */
16038 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16039 parser->local_variables_forbidden_p = true;
16040
16041 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16042 parm;
16043 parm = TREE_CHAIN (parm))
16044 {
16045 cp_token_cache *tokens;
16046 tree default_arg = TREE_PURPOSE (parm);
16047 tree parsed_arg;
16048 VEC(tree,gc) *insts;
16049 tree copy;
16050 unsigned ix;
16051
16052 if (!default_arg)
16053 continue;
16054
16055 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16056 /* This can happen for a friend declaration for a function
16057 already declared with default arguments. */
16058 continue;
16059
16060 /* Push the saved tokens for the default argument onto the parser's
16061 lexer stack. */
16062 tokens = DEFARG_TOKENS (default_arg);
16063 cp_parser_push_lexer_for_tokens (parser, tokens);
16064
16065 /* Parse the assignment-expression. */
16066 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16067
16068 if (!processing_template_decl)
16069 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16070
16071 TREE_PURPOSE (parm) = parsed_arg;
16072
16073 /* Update any instantiations we've already created. */
16074 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16075 VEC_iterate (tree, insts, ix, copy); ix++)
16076 TREE_PURPOSE (copy) = parsed_arg;
16077
16078 /* If the token stream has not been completely used up, then
16079 there was extra junk after the end of the default
16080 argument. */
16081 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16082 cp_parser_error (parser, "expected %<,%>");
16083
16084 /* Revert to the main lexer. */
16085 cp_parser_pop_lexer (parser);
16086 }
16087
16088 /* Make sure no default arg is missing. */
16089 check_default_args (fn);
16090
16091 /* Restore the state of local_variables_forbidden_p. */
16092 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16093
16094 /* Restore the queue. */
16095 parser->unparsed_functions_queues
16096 = TREE_CHAIN (parser->unparsed_functions_queues);
16097 }
16098
16099 /* Parse the operand of `sizeof' (or a similar operator). Returns
16100 either a TYPE or an expression, depending on the form of the
16101 input. The KEYWORD indicates which kind of expression we have
16102 encountered. */
16103
16104 static tree
16105 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16106 {
16107 static const char *format;
16108 tree expr = NULL_TREE;
16109 const char *saved_message;
16110 bool saved_integral_constant_expression_p;
16111 bool saved_non_integral_constant_expression_p;
16112
16113 /* Initialize FORMAT the first time we get here. */
16114 if (!format)
16115 format = "types may not be defined in '%s' expressions";
16116
16117 /* Types cannot be defined in a `sizeof' expression. Save away the
16118 old message. */
16119 saved_message = parser->type_definition_forbidden_message;
16120 /* And create the new one. */
16121 parser->type_definition_forbidden_message
16122 = XNEWVEC (const char, strlen (format)
16123 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16124 + 1 /* `\0' */);
16125 sprintf ((char *) parser->type_definition_forbidden_message,
16126 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16127
16128 /* The restrictions on constant-expressions do not apply inside
16129 sizeof expressions. */
16130 saved_integral_constant_expression_p
16131 = parser->integral_constant_expression_p;
16132 saved_non_integral_constant_expression_p
16133 = parser->non_integral_constant_expression_p;
16134 parser->integral_constant_expression_p = false;
16135
16136 /* Do not actually evaluate the expression. */
16137 ++skip_evaluation;
16138 /* If it's a `(', then we might be looking at the type-id
16139 construction. */
16140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16141 {
16142 tree type;
16143 bool saved_in_type_id_in_expr_p;
16144
16145 /* We can't be sure yet whether we're looking at a type-id or an
16146 expression. */
16147 cp_parser_parse_tentatively (parser);
16148 /* Consume the `('. */
16149 cp_lexer_consume_token (parser->lexer);
16150 /* Parse the type-id. */
16151 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16152 parser->in_type_id_in_expr_p = true;
16153 type = cp_parser_type_id (parser);
16154 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16155 /* Now, look for the trailing `)'. */
16156 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16157 /* If all went well, then we're done. */
16158 if (cp_parser_parse_definitely (parser))
16159 {
16160 cp_decl_specifier_seq decl_specs;
16161
16162 /* Build a trivial decl-specifier-seq. */
16163 clear_decl_specs (&decl_specs);
16164 decl_specs.type = type;
16165
16166 /* Call grokdeclarator to figure out what type this is. */
16167 expr = grokdeclarator (NULL,
16168 &decl_specs,
16169 TYPENAME,
16170 /*initialized=*/0,
16171 /*attrlist=*/NULL);
16172 }
16173 }
16174
16175 /* If the type-id production did not work out, then we must be
16176 looking at the unary-expression production. */
16177 if (!expr)
16178 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16179 /*cast_p=*/false);
16180 /* Go back to evaluating expressions. */
16181 --skip_evaluation;
16182
16183 /* Free the message we created. */
16184 free ((char *) parser->type_definition_forbidden_message);
16185 /* And restore the old one. */
16186 parser->type_definition_forbidden_message = saved_message;
16187 parser->integral_constant_expression_p
16188 = saved_integral_constant_expression_p;
16189 parser->non_integral_constant_expression_p
16190 = saved_non_integral_constant_expression_p;
16191
16192 return expr;
16193 }
16194
16195 /* If the current declaration has no declarator, return true. */
16196
16197 static bool
16198 cp_parser_declares_only_class_p (cp_parser *parser)
16199 {
16200 /* If the next token is a `;' or a `,' then there is no
16201 declarator. */
16202 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16203 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16204 }
16205
16206 /* Update the DECL_SPECS to reflect the storage class indicated by
16207 KEYWORD. */
16208
16209 static void
16210 cp_parser_set_storage_class (cp_parser *parser,
16211 cp_decl_specifier_seq *decl_specs,
16212 enum rid keyword)
16213 {
16214 cp_storage_class storage_class;
16215
16216 if (parser->in_unbraced_linkage_specification_p)
16217 {
16218 error ("invalid use of %qD in linkage specification",
16219 ridpointers[keyword]);
16220 return;
16221 }
16222 else if (decl_specs->storage_class != sc_none)
16223 {
16224 decl_specs->conflicting_specifiers_p = true;
16225 return;
16226 }
16227
16228 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16229 && decl_specs->specs[(int) ds_thread])
16230 {
16231 error ("%<__thread%> before %qD", ridpointers[keyword]);
16232 decl_specs->specs[(int) ds_thread] = 0;
16233 }
16234
16235 switch (keyword)
16236 {
16237 case RID_AUTO:
16238 storage_class = sc_auto;
16239 break;
16240 case RID_REGISTER:
16241 storage_class = sc_register;
16242 break;
16243 case RID_STATIC:
16244 storage_class = sc_static;
16245 break;
16246 case RID_EXTERN:
16247 storage_class = sc_extern;
16248 break;
16249 case RID_MUTABLE:
16250 storage_class = sc_mutable;
16251 break;
16252 default:
16253 gcc_unreachable ();
16254 }
16255 decl_specs->storage_class = storage_class;
16256
16257 /* A storage class specifier cannot be applied alongside a typedef
16258 specifier. If there is a typedef specifier present then set
16259 conflicting_specifiers_p which will trigger an error later
16260 on in grokdeclarator. */
16261 if (decl_specs->specs[(int)ds_typedef])
16262 decl_specs->conflicting_specifiers_p = true;
16263 }
16264
16265 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16266 is true, the type is a user-defined type; otherwise it is a
16267 built-in type specified by a keyword. */
16268
16269 static void
16270 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16271 tree type_spec,
16272 bool user_defined_p)
16273 {
16274 decl_specs->any_specifiers_p = true;
16275
16276 /* If the user tries to redeclare bool or wchar_t (with, for
16277 example, in "typedef int wchar_t;") we remember that this is what
16278 happened. In system headers, we ignore these declarations so
16279 that G++ can work with system headers that are not C++-safe. */
16280 if (decl_specs->specs[(int) ds_typedef]
16281 && !user_defined_p
16282 && (type_spec == boolean_type_node
16283 || type_spec == wchar_type_node)
16284 && (decl_specs->type
16285 || decl_specs->specs[(int) ds_long]
16286 || decl_specs->specs[(int) ds_short]
16287 || decl_specs->specs[(int) ds_unsigned]
16288 || decl_specs->specs[(int) ds_signed]))
16289 {
16290 decl_specs->redefined_builtin_type = type_spec;
16291 if (!decl_specs->type)
16292 {
16293 decl_specs->type = type_spec;
16294 decl_specs->user_defined_type_p = false;
16295 }
16296 }
16297 else if (decl_specs->type)
16298 decl_specs->multiple_types_p = true;
16299 else
16300 {
16301 decl_specs->type = type_spec;
16302 decl_specs->user_defined_type_p = user_defined_p;
16303 decl_specs->redefined_builtin_type = NULL_TREE;
16304 }
16305 }
16306
16307 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16308 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16309
16310 static bool
16311 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16312 {
16313 return decl_specifiers->specs[(int) ds_friend] != 0;
16314 }
16315
16316 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16317 issue an error message indicating that TOKEN_DESC was expected.
16318
16319 Returns the token consumed, if the token had the appropriate type.
16320 Otherwise, returns NULL. */
16321
16322 static cp_token *
16323 cp_parser_require (cp_parser* parser,
16324 enum cpp_ttype type,
16325 const char* token_desc)
16326 {
16327 if (cp_lexer_next_token_is (parser->lexer, type))
16328 return cp_lexer_consume_token (parser->lexer);
16329 else
16330 {
16331 /* Output the MESSAGE -- unless we're parsing tentatively. */
16332 if (!cp_parser_simulate_error (parser))
16333 {
16334 char *message = concat ("expected ", token_desc, NULL);
16335 cp_parser_error (parser, message);
16336 free (message);
16337 }
16338 return NULL;
16339 }
16340 }
16341
16342 /* An error message is produced if the next token is not '>'.
16343 All further tokens are skipped until the desired token is
16344 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16345
16346 static void
16347 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16348 {
16349 /* Current level of '< ... >'. */
16350 unsigned level = 0;
16351 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16352 unsigned nesting_depth = 0;
16353
16354 /* Are we ready, yet? If not, issue error message. */
16355 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16356 return;
16357
16358 /* Skip tokens until the desired token is found. */
16359 while (true)
16360 {
16361 /* Peek at the next token. */
16362 switch (cp_lexer_peek_token (parser->lexer)->type)
16363 {
16364 case CPP_LESS:
16365 if (!nesting_depth)
16366 ++level;
16367 break;
16368
16369 case CPP_GREATER:
16370 if (!nesting_depth && level-- == 0)
16371 {
16372 /* We've reached the token we want, consume it and stop. */
16373 cp_lexer_consume_token (parser->lexer);
16374 return;
16375 }
16376 break;
16377
16378 case CPP_OPEN_PAREN:
16379 case CPP_OPEN_SQUARE:
16380 ++nesting_depth;
16381 break;
16382
16383 case CPP_CLOSE_PAREN:
16384 case CPP_CLOSE_SQUARE:
16385 if (nesting_depth-- == 0)
16386 return;
16387 break;
16388
16389 case CPP_EOF:
16390 case CPP_PRAGMA_EOL:
16391 case CPP_SEMICOLON:
16392 case CPP_OPEN_BRACE:
16393 case CPP_CLOSE_BRACE:
16394 /* The '>' was probably forgotten, don't look further. */
16395 return;
16396
16397 default:
16398 break;
16399 }
16400
16401 /* Consume this token. */
16402 cp_lexer_consume_token (parser->lexer);
16403 }
16404 }
16405
16406 /* If the next token is the indicated keyword, consume it. Otherwise,
16407 issue an error message indicating that TOKEN_DESC was expected.
16408
16409 Returns the token consumed, if the token had the appropriate type.
16410 Otherwise, returns NULL. */
16411
16412 static cp_token *
16413 cp_parser_require_keyword (cp_parser* parser,
16414 enum rid keyword,
16415 const char* token_desc)
16416 {
16417 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16418
16419 if (token && token->keyword != keyword)
16420 {
16421 dyn_string_t error_msg;
16422
16423 /* Format the error message. */
16424 error_msg = dyn_string_new (0);
16425 dyn_string_append_cstr (error_msg, "expected ");
16426 dyn_string_append_cstr (error_msg, token_desc);
16427 cp_parser_error (parser, error_msg->s);
16428 dyn_string_delete (error_msg);
16429 return NULL;
16430 }
16431
16432 return token;
16433 }
16434
16435 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16436 function-definition. */
16437
16438 static bool
16439 cp_parser_token_starts_function_definition_p (cp_token* token)
16440 {
16441 return (/* An ordinary function-body begins with an `{'. */
16442 token->type == CPP_OPEN_BRACE
16443 /* A ctor-initializer begins with a `:'. */
16444 || token->type == CPP_COLON
16445 /* A function-try-block begins with `try'. */
16446 || token->keyword == RID_TRY
16447 /* The named return value extension begins with `return'. */
16448 || token->keyword == RID_RETURN);
16449 }
16450
16451 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16452 definition. */
16453
16454 static bool
16455 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16456 {
16457 cp_token *token;
16458
16459 token = cp_lexer_peek_token (parser->lexer);
16460 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16461 }
16462
16463 /* Returns TRUE iff the next token is the "," or ">" ending a
16464 template-argument. */
16465
16466 static bool
16467 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16468 {
16469 cp_token *token;
16470
16471 token = cp_lexer_peek_token (parser->lexer);
16472 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16473 }
16474
16475 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16476 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16477
16478 static bool
16479 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16480 size_t n)
16481 {
16482 cp_token *token;
16483
16484 token = cp_lexer_peek_nth_token (parser->lexer, n);
16485 if (token->type == CPP_LESS)
16486 return true;
16487 /* Check for the sequence `<::' in the original code. It would be lexed as
16488 `[:', where `[' is a digraph, and there is no whitespace before
16489 `:'. */
16490 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16491 {
16492 cp_token *token2;
16493 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16494 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16495 return true;
16496 }
16497 return false;
16498 }
16499
16500 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16501 or none_type otherwise. */
16502
16503 static enum tag_types
16504 cp_parser_token_is_class_key (cp_token* token)
16505 {
16506 switch (token->keyword)
16507 {
16508 case RID_CLASS:
16509 return class_type;
16510 case RID_STRUCT:
16511 return record_type;
16512 case RID_UNION:
16513 return union_type;
16514
16515 default:
16516 return none_type;
16517 }
16518 }
16519
16520 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16521
16522 static void
16523 cp_parser_check_class_key (enum tag_types class_key, tree type)
16524 {
16525 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16526 pedwarn ("%qs tag used in naming %q#T",
16527 class_key == union_type ? "union"
16528 : class_key == record_type ? "struct" : "class",
16529 type);
16530 }
16531
16532 /* Issue an error message if DECL is redeclared with different
16533 access than its original declaration [class.access.spec/3].
16534 This applies to nested classes and nested class templates.
16535 [class.mem/1]. */
16536
16537 static void
16538 cp_parser_check_access_in_redeclaration (tree decl)
16539 {
16540 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16541 return;
16542
16543 if ((TREE_PRIVATE (decl)
16544 != (current_access_specifier == access_private_node))
16545 || (TREE_PROTECTED (decl)
16546 != (current_access_specifier == access_protected_node)))
16547 error ("%qD redeclared with different access", decl);
16548 }
16549
16550 /* Look for the `template' keyword, as a syntactic disambiguator.
16551 Return TRUE iff it is present, in which case it will be
16552 consumed. */
16553
16554 static bool
16555 cp_parser_optional_template_keyword (cp_parser *parser)
16556 {
16557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16558 {
16559 /* The `template' keyword can only be used within templates;
16560 outside templates the parser can always figure out what is a
16561 template and what is not. */
16562 if (!processing_template_decl)
16563 {
16564 error ("%<template%> (as a disambiguator) is only allowed "
16565 "within templates");
16566 /* If this part of the token stream is rescanned, the same
16567 error message would be generated. So, we purge the token
16568 from the stream. */
16569 cp_lexer_purge_token (parser->lexer);
16570 return false;
16571 }
16572 else
16573 {
16574 /* Consume the `template' keyword. */
16575 cp_lexer_consume_token (parser->lexer);
16576 return true;
16577 }
16578 }
16579
16580 return false;
16581 }
16582
16583 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16584 set PARSER->SCOPE, and perform other related actions. */
16585
16586 static void
16587 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16588 {
16589 tree value;
16590 tree check;
16591
16592 /* Get the stored value. */
16593 value = cp_lexer_consume_token (parser->lexer)->value;
16594 /* Perform any access checks that were deferred. */
16595 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16596 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16597 /* Set the scope from the stored value. */
16598 parser->scope = TREE_VALUE (value);
16599 parser->qualifying_scope = TREE_TYPE (value);
16600 parser->object_scope = NULL_TREE;
16601 }
16602
16603 /* Consume tokens up through a non-nested END token. */
16604
16605 static void
16606 cp_parser_cache_group (cp_parser *parser,
16607 enum cpp_ttype end,
16608 unsigned depth)
16609 {
16610 while (true)
16611 {
16612 cp_token *token;
16613
16614 /* Abort a parenthesized expression if we encounter a brace. */
16615 if ((end == CPP_CLOSE_PAREN || depth == 0)
16616 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16617 return;
16618 /* If we've reached the end of the file, stop. */
16619 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16620 || (end != CPP_PRAGMA_EOL
16621 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16622 return;
16623 /* Consume the next token. */
16624 token = cp_lexer_consume_token (parser->lexer);
16625 /* See if it starts a new group. */
16626 if (token->type == CPP_OPEN_BRACE)
16627 {
16628 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16629 if (depth == 0)
16630 return;
16631 }
16632 else if (token->type == CPP_OPEN_PAREN)
16633 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16634 else if (token->type == CPP_PRAGMA)
16635 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16636 else if (token->type == end)
16637 return;
16638 }
16639 }
16640
16641 /* Begin parsing tentatively. We always save tokens while parsing
16642 tentatively so that if the tentative parsing fails we can restore the
16643 tokens. */
16644
16645 static void
16646 cp_parser_parse_tentatively (cp_parser* parser)
16647 {
16648 /* Enter a new parsing context. */
16649 parser->context = cp_parser_context_new (parser->context);
16650 /* Begin saving tokens. */
16651 cp_lexer_save_tokens (parser->lexer);
16652 /* In order to avoid repetitive access control error messages,
16653 access checks are queued up until we are no longer parsing
16654 tentatively. */
16655 push_deferring_access_checks (dk_deferred);
16656 }
16657
16658 /* Commit to the currently active tentative parse. */
16659
16660 static void
16661 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16662 {
16663 cp_parser_context *context;
16664 cp_lexer *lexer;
16665
16666 /* Mark all of the levels as committed. */
16667 lexer = parser->lexer;
16668 for (context = parser->context; context->next; context = context->next)
16669 {
16670 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16671 break;
16672 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16673 while (!cp_lexer_saving_tokens (lexer))
16674 lexer = lexer->next;
16675 cp_lexer_commit_tokens (lexer);
16676 }
16677 }
16678
16679 /* Abort the currently active tentative parse. All consumed tokens
16680 will be rolled back, and no diagnostics will be issued. */
16681
16682 static void
16683 cp_parser_abort_tentative_parse (cp_parser* parser)
16684 {
16685 cp_parser_simulate_error (parser);
16686 /* Now, pretend that we want to see if the construct was
16687 successfully parsed. */
16688 cp_parser_parse_definitely (parser);
16689 }
16690
16691 /* Stop parsing tentatively. If a parse error has occurred, restore the
16692 token stream. Otherwise, commit to the tokens we have consumed.
16693 Returns true if no error occurred; false otherwise. */
16694
16695 static bool
16696 cp_parser_parse_definitely (cp_parser* parser)
16697 {
16698 bool error_occurred;
16699 cp_parser_context *context;
16700
16701 /* Remember whether or not an error occurred, since we are about to
16702 destroy that information. */
16703 error_occurred = cp_parser_error_occurred (parser);
16704 /* Remove the topmost context from the stack. */
16705 context = parser->context;
16706 parser->context = context->next;
16707 /* If no parse errors occurred, commit to the tentative parse. */
16708 if (!error_occurred)
16709 {
16710 /* Commit to the tokens read tentatively, unless that was
16711 already done. */
16712 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16713 cp_lexer_commit_tokens (parser->lexer);
16714
16715 pop_to_parent_deferring_access_checks ();
16716 }
16717 /* Otherwise, if errors occurred, roll back our state so that things
16718 are just as they were before we began the tentative parse. */
16719 else
16720 {
16721 cp_lexer_rollback_tokens (parser->lexer);
16722 pop_deferring_access_checks ();
16723 }
16724 /* Add the context to the front of the free list. */
16725 context->next = cp_parser_context_free_list;
16726 cp_parser_context_free_list = context;
16727
16728 return !error_occurred;
16729 }
16730
16731 /* Returns true if we are parsing tentatively and are not committed to
16732 this tentative parse. */
16733
16734 static bool
16735 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16736 {
16737 return (cp_parser_parsing_tentatively (parser)
16738 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16739 }
16740
16741 /* Returns nonzero iff an error has occurred during the most recent
16742 tentative parse. */
16743
16744 static bool
16745 cp_parser_error_occurred (cp_parser* parser)
16746 {
16747 return (cp_parser_parsing_tentatively (parser)
16748 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16749 }
16750
16751 /* Returns nonzero if GNU extensions are allowed. */
16752
16753 static bool
16754 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16755 {
16756 return parser->allow_gnu_extensions_p;
16757 }
16758 \f
16759 /* Objective-C++ Productions */
16760
16761
16762 /* Parse an Objective-C expression, which feeds into a primary-expression
16763 above.
16764
16765 objc-expression:
16766 objc-message-expression
16767 objc-string-literal
16768 objc-encode-expression
16769 objc-protocol-expression
16770 objc-selector-expression
16771
16772 Returns a tree representation of the expression. */
16773
16774 static tree
16775 cp_parser_objc_expression (cp_parser* parser)
16776 {
16777 /* Try to figure out what kind of declaration is present. */
16778 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16779
16780 switch (kwd->type)
16781 {
16782 case CPP_OPEN_SQUARE:
16783 return cp_parser_objc_message_expression (parser);
16784
16785 case CPP_OBJC_STRING:
16786 kwd = cp_lexer_consume_token (parser->lexer);
16787 return objc_build_string_object (kwd->value);
16788
16789 case CPP_KEYWORD:
16790 switch (kwd->keyword)
16791 {
16792 case RID_AT_ENCODE:
16793 return cp_parser_objc_encode_expression (parser);
16794
16795 case RID_AT_PROTOCOL:
16796 return cp_parser_objc_protocol_expression (parser);
16797
16798 case RID_AT_SELECTOR:
16799 return cp_parser_objc_selector_expression (parser);
16800
16801 default:
16802 break;
16803 }
16804 default:
16805 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16806 cp_parser_skip_to_end_of_block_or_statement (parser);
16807 }
16808
16809 return error_mark_node;
16810 }
16811
16812 /* Parse an Objective-C message expression.
16813
16814 objc-message-expression:
16815 [ objc-message-receiver objc-message-args ]
16816
16817 Returns a representation of an Objective-C message. */
16818
16819 static tree
16820 cp_parser_objc_message_expression (cp_parser* parser)
16821 {
16822 tree receiver, messageargs;
16823
16824 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16825 receiver = cp_parser_objc_message_receiver (parser);
16826 messageargs = cp_parser_objc_message_args (parser);
16827 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16828
16829 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16830 }
16831
16832 /* Parse an objc-message-receiver.
16833
16834 objc-message-receiver:
16835 expression
16836 simple-type-specifier
16837
16838 Returns a representation of the type or expression. */
16839
16840 static tree
16841 cp_parser_objc_message_receiver (cp_parser* parser)
16842 {
16843 tree rcv;
16844
16845 /* An Objective-C message receiver may be either (1) a type
16846 or (2) an expression. */
16847 cp_parser_parse_tentatively (parser);
16848 rcv = cp_parser_expression (parser, false);
16849
16850 if (cp_parser_parse_definitely (parser))
16851 return rcv;
16852
16853 rcv = cp_parser_simple_type_specifier (parser,
16854 /*decl_specs=*/NULL,
16855 CP_PARSER_FLAGS_NONE);
16856
16857 return objc_get_class_reference (rcv);
16858 }
16859
16860 /* Parse the arguments and selectors comprising an Objective-C message.
16861
16862 objc-message-args:
16863 objc-selector
16864 objc-selector-args
16865 objc-selector-args , objc-comma-args
16866
16867 objc-selector-args:
16868 objc-selector [opt] : assignment-expression
16869 objc-selector-args objc-selector [opt] : assignment-expression
16870
16871 objc-comma-args:
16872 assignment-expression
16873 objc-comma-args , assignment-expression
16874
16875 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16876 selector arguments and TREE_VALUE containing a list of comma
16877 arguments. */
16878
16879 static tree
16880 cp_parser_objc_message_args (cp_parser* parser)
16881 {
16882 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16883 bool maybe_unary_selector_p = true;
16884 cp_token *token = cp_lexer_peek_token (parser->lexer);
16885
16886 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16887 {
16888 tree selector = NULL_TREE, arg;
16889
16890 if (token->type != CPP_COLON)
16891 selector = cp_parser_objc_selector (parser);
16892
16893 /* Detect if we have a unary selector. */
16894 if (maybe_unary_selector_p
16895 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16896 return build_tree_list (selector, NULL_TREE);
16897
16898 maybe_unary_selector_p = false;
16899 cp_parser_require (parser, CPP_COLON, "`:'");
16900 arg = cp_parser_assignment_expression (parser, false);
16901
16902 sel_args
16903 = chainon (sel_args,
16904 build_tree_list (selector, arg));
16905
16906 token = cp_lexer_peek_token (parser->lexer);
16907 }
16908
16909 /* Handle non-selector arguments, if any. */
16910 while (token->type == CPP_COMMA)
16911 {
16912 tree arg;
16913
16914 cp_lexer_consume_token (parser->lexer);
16915 arg = cp_parser_assignment_expression (parser, false);
16916
16917 addl_args
16918 = chainon (addl_args,
16919 build_tree_list (NULL_TREE, arg));
16920
16921 token = cp_lexer_peek_token (parser->lexer);
16922 }
16923
16924 return build_tree_list (sel_args, addl_args);
16925 }
16926
16927 /* Parse an Objective-C encode expression.
16928
16929 objc-encode-expression:
16930 @encode objc-typename
16931
16932 Returns an encoded representation of the type argument. */
16933
16934 static tree
16935 cp_parser_objc_encode_expression (cp_parser* parser)
16936 {
16937 tree type;
16938
16939 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16940 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16941 type = complete_type (cp_parser_type_id (parser));
16942 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16943
16944 if (!type)
16945 {
16946 error ("%<@encode%> must specify a type as an argument");
16947 return error_mark_node;
16948 }
16949
16950 return objc_build_encode_expr (type);
16951 }
16952
16953 /* Parse an Objective-C @defs expression. */
16954
16955 static tree
16956 cp_parser_objc_defs_expression (cp_parser *parser)
16957 {
16958 tree name;
16959
16960 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16961 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16962 name = cp_parser_identifier (parser);
16963 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16964
16965 return objc_get_class_ivars (name);
16966 }
16967
16968 /* Parse an Objective-C protocol expression.
16969
16970 objc-protocol-expression:
16971 @protocol ( identifier )
16972
16973 Returns a representation of the protocol expression. */
16974
16975 static tree
16976 cp_parser_objc_protocol_expression (cp_parser* parser)
16977 {
16978 tree proto;
16979
16980 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16981 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16982 proto = cp_parser_identifier (parser);
16983 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16984
16985 return objc_build_protocol_expr (proto);
16986 }
16987
16988 /* Parse an Objective-C selector expression.
16989
16990 objc-selector-expression:
16991 @selector ( objc-method-signature )
16992
16993 objc-method-signature:
16994 objc-selector
16995 objc-selector-seq
16996
16997 objc-selector-seq:
16998 objc-selector :
16999 objc-selector-seq objc-selector :
17000
17001 Returns a representation of the method selector. */
17002
17003 static tree
17004 cp_parser_objc_selector_expression (cp_parser* parser)
17005 {
17006 tree sel_seq = NULL_TREE;
17007 bool maybe_unary_selector_p = true;
17008 cp_token *token;
17009
17010 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17011 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17012 token = cp_lexer_peek_token (parser->lexer);
17013
17014 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17015 || token->type == CPP_SCOPE)
17016 {
17017 tree selector = NULL_TREE;
17018
17019 if (token->type != CPP_COLON
17020 || token->type == CPP_SCOPE)
17021 selector = cp_parser_objc_selector (parser);
17022
17023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17024 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17025 {
17026 /* Detect if we have a unary selector. */
17027 if (maybe_unary_selector_p)
17028 {
17029 sel_seq = selector;
17030 goto finish_selector;
17031 }
17032 else
17033 {
17034 cp_parser_error (parser, "expected %<:%>");
17035 }
17036 }
17037 maybe_unary_selector_p = false;
17038 token = cp_lexer_consume_token (parser->lexer);
17039
17040 if (token->type == CPP_SCOPE)
17041 {
17042 sel_seq
17043 = chainon (sel_seq,
17044 build_tree_list (selector, NULL_TREE));
17045 sel_seq
17046 = chainon (sel_seq,
17047 build_tree_list (NULL_TREE, NULL_TREE));
17048 }
17049 else
17050 sel_seq
17051 = chainon (sel_seq,
17052 build_tree_list (selector, NULL_TREE));
17053
17054 token = cp_lexer_peek_token (parser->lexer);
17055 }
17056
17057 finish_selector:
17058 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17059
17060 return objc_build_selector_expr (sel_seq);
17061 }
17062
17063 /* Parse a list of identifiers.
17064
17065 objc-identifier-list:
17066 identifier
17067 objc-identifier-list , identifier
17068
17069 Returns a TREE_LIST of identifier nodes. */
17070
17071 static tree
17072 cp_parser_objc_identifier_list (cp_parser* parser)
17073 {
17074 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17075 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17076
17077 while (sep->type == CPP_COMMA)
17078 {
17079 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17080 list = chainon (list,
17081 build_tree_list (NULL_TREE,
17082 cp_parser_identifier (parser)));
17083 sep = cp_lexer_peek_token (parser->lexer);
17084 }
17085
17086 return list;
17087 }
17088
17089 /* Parse an Objective-C alias declaration.
17090
17091 objc-alias-declaration:
17092 @compatibility_alias identifier identifier ;
17093
17094 This function registers the alias mapping with the Objective-C front-end.
17095 It returns nothing. */
17096
17097 static void
17098 cp_parser_objc_alias_declaration (cp_parser* parser)
17099 {
17100 tree alias, orig;
17101
17102 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17103 alias = cp_parser_identifier (parser);
17104 orig = cp_parser_identifier (parser);
17105 objc_declare_alias (alias, orig);
17106 cp_parser_consume_semicolon_at_end_of_statement (parser);
17107 }
17108
17109 /* Parse an Objective-C class forward-declaration.
17110
17111 objc-class-declaration:
17112 @class objc-identifier-list ;
17113
17114 The function registers the forward declarations with the Objective-C
17115 front-end. It returns nothing. */
17116
17117 static void
17118 cp_parser_objc_class_declaration (cp_parser* parser)
17119 {
17120 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17121 objc_declare_class (cp_parser_objc_identifier_list (parser));
17122 cp_parser_consume_semicolon_at_end_of_statement (parser);
17123 }
17124
17125 /* Parse a list of Objective-C protocol references.
17126
17127 objc-protocol-refs-opt:
17128 objc-protocol-refs [opt]
17129
17130 objc-protocol-refs:
17131 < objc-identifier-list >
17132
17133 Returns a TREE_LIST of identifiers, if any. */
17134
17135 static tree
17136 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17137 {
17138 tree protorefs = NULL_TREE;
17139
17140 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17141 {
17142 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17143 protorefs = cp_parser_objc_identifier_list (parser);
17144 cp_parser_require (parser, CPP_GREATER, "`>'");
17145 }
17146
17147 return protorefs;
17148 }
17149
17150 /* Parse a Objective-C visibility specification. */
17151
17152 static void
17153 cp_parser_objc_visibility_spec (cp_parser* parser)
17154 {
17155 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17156
17157 switch (vis->keyword)
17158 {
17159 case RID_AT_PRIVATE:
17160 objc_set_visibility (2);
17161 break;
17162 case RID_AT_PROTECTED:
17163 objc_set_visibility (0);
17164 break;
17165 case RID_AT_PUBLIC:
17166 objc_set_visibility (1);
17167 break;
17168 default:
17169 return;
17170 }
17171
17172 /* Eat '@private'/'@protected'/'@public'. */
17173 cp_lexer_consume_token (parser->lexer);
17174 }
17175
17176 /* Parse an Objective-C method type. */
17177
17178 static void
17179 cp_parser_objc_method_type (cp_parser* parser)
17180 {
17181 objc_set_method_type
17182 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17183 ? PLUS_EXPR
17184 : MINUS_EXPR);
17185 }
17186
17187 /* Parse an Objective-C protocol qualifier. */
17188
17189 static tree
17190 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17191 {
17192 tree quals = NULL_TREE, node;
17193 cp_token *token = cp_lexer_peek_token (parser->lexer);
17194
17195 node = token->value;
17196
17197 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17198 && (node == ridpointers [(int) RID_IN]
17199 || node == ridpointers [(int) RID_OUT]
17200 || node == ridpointers [(int) RID_INOUT]
17201 || node == ridpointers [(int) RID_BYCOPY]
17202 || node == ridpointers [(int) RID_BYREF]
17203 || node == ridpointers [(int) RID_ONEWAY]))
17204 {
17205 quals = tree_cons (NULL_TREE, node, quals);
17206 cp_lexer_consume_token (parser->lexer);
17207 token = cp_lexer_peek_token (parser->lexer);
17208 node = token->value;
17209 }
17210
17211 return quals;
17212 }
17213
17214 /* Parse an Objective-C typename. */
17215
17216 static tree
17217 cp_parser_objc_typename (cp_parser* parser)
17218 {
17219 tree typename = NULL_TREE;
17220
17221 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17222 {
17223 tree proto_quals, cp_type = NULL_TREE;
17224
17225 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17226 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17227
17228 /* An ObjC type name may consist of just protocol qualifiers, in which
17229 case the type shall default to 'id'. */
17230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17231 cp_type = cp_parser_type_id (parser);
17232
17233 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17234 typename = build_tree_list (proto_quals, cp_type);
17235 }
17236
17237 return typename;
17238 }
17239
17240 /* Check to see if TYPE refers to an Objective-C selector name. */
17241
17242 static bool
17243 cp_parser_objc_selector_p (enum cpp_ttype type)
17244 {
17245 return (type == CPP_NAME || type == CPP_KEYWORD
17246 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17247 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17248 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17249 || type == CPP_XOR || type == CPP_XOR_EQ);
17250 }
17251
17252 /* Parse an Objective-C selector. */
17253
17254 static tree
17255 cp_parser_objc_selector (cp_parser* parser)
17256 {
17257 cp_token *token = cp_lexer_consume_token (parser->lexer);
17258
17259 if (!cp_parser_objc_selector_p (token->type))
17260 {
17261 error ("invalid Objective-C++ selector name");
17262 return error_mark_node;
17263 }
17264
17265 /* C++ operator names are allowed to appear in ObjC selectors. */
17266 switch (token->type)
17267 {
17268 case CPP_AND_AND: return get_identifier ("and");
17269 case CPP_AND_EQ: return get_identifier ("and_eq");
17270 case CPP_AND: return get_identifier ("bitand");
17271 case CPP_OR: return get_identifier ("bitor");
17272 case CPP_COMPL: return get_identifier ("compl");
17273 case CPP_NOT: return get_identifier ("not");
17274 case CPP_NOT_EQ: return get_identifier ("not_eq");
17275 case CPP_OR_OR: return get_identifier ("or");
17276 case CPP_OR_EQ: return get_identifier ("or_eq");
17277 case CPP_XOR: return get_identifier ("xor");
17278 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17279 default: return token->value;
17280 }
17281 }
17282
17283 /* Parse an Objective-C params list. */
17284
17285 static tree
17286 cp_parser_objc_method_keyword_params (cp_parser* parser)
17287 {
17288 tree params = NULL_TREE;
17289 bool maybe_unary_selector_p = true;
17290 cp_token *token = cp_lexer_peek_token (parser->lexer);
17291
17292 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17293 {
17294 tree selector = NULL_TREE, typename, identifier;
17295
17296 if (token->type != CPP_COLON)
17297 selector = cp_parser_objc_selector (parser);
17298
17299 /* Detect if we have a unary selector. */
17300 if (maybe_unary_selector_p
17301 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17302 return selector;
17303
17304 maybe_unary_selector_p = false;
17305 cp_parser_require (parser, CPP_COLON, "`:'");
17306 typename = cp_parser_objc_typename (parser);
17307 identifier = cp_parser_identifier (parser);
17308
17309 params
17310 = chainon (params,
17311 objc_build_keyword_decl (selector,
17312 typename,
17313 identifier));
17314
17315 token = cp_lexer_peek_token (parser->lexer);
17316 }
17317
17318 return params;
17319 }
17320
17321 /* Parse the non-keyword Objective-C params. */
17322
17323 static tree
17324 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17325 {
17326 tree params = make_node (TREE_LIST);
17327 cp_token *token = cp_lexer_peek_token (parser->lexer);
17328 *ellipsisp = false; /* Initially, assume no ellipsis. */
17329
17330 while (token->type == CPP_COMMA)
17331 {
17332 cp_parameter_declarator *parmdecl;
17333 tree parm;
17334
17335 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17336 token = cp_lexer_peek_token (parser->lexer);
17337
17338 if (token->type == CPP_ELLIPSIS)
17339 {
17340 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17341 *ellipsisp = true;
17342 break;
17343 }
17344
17345 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17346 parm = grokdeclarator (parmdecl->declarator,
17347 &parmdecl->decl_specifiers,
17348 PARM, /*initialized=*/0,
17349 /*attrlist=*/NULL);
17350
17351 chainon (params, build_tree_list (NULL_TREE, parm));
17352 token = cp_lexer_peek_token (parser->lexer);
17353 }
17354
17355 return params;
17356 }
17357
17358 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17359
17360 static void
17361 cp_parser_objc_interstitial_code (cp_parser* parser)
17362 {
17363 cp_token *token = cp_lexer_peek_token (parser->lexer);
17364
17365 /* If the next token is `extern' and the following token is a string
17366 literal, then we have a linkage specification. */
17367 if (token->keyword == RID_EXTERN
17368 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17369 cp_parser_linkage_specification (parser);
17370 /* Handle #pragma, if any. */
17371 else if (token->type == CPP_PRAGMA)
17372 cp_parser_pragma (parser, pragma_external);
17373 /* Allow stray semicolons. */
17374 else if (token->type == CPP_SEMICOLON)
17375 cp_lexer_consume_token (parser->lexer);
17376 /* Finally, try to parse a block-declaration, or a function-definition. */
17377 else
17378 cp_parser_block_declaration (parser, /*statement_p=*/false);
17379 }
17380
17381 /* Parse a method signature. */
17382
17383 static tree
17384 cp_parser_objc_method_signature (cp_parser* parser)
17385 {
17386 tree rettype, kwdparms, optparms;
17387 bool ellipsis = false;
17388
17389 cp_parser_objc_method_type (parser);
17390 rettype = cp_parser_objc_typename (parser);
17391 kwdparms = cp_parser_objc_method_keyword_params (parser);
17392 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17393
17394 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17395 }
17396
17397 /* Pars an Objective-C method prototype list. */
17398
17399 static void
17400 cp_parser_objc_method_prototype_list (cp_parser* parser)
17401 {
17402 cp_token *token = cp_lexer_peek_token (parser->lexer);
17403
17404 while (token->keyword != RID_AT_END)
17405 {
17406 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17407 {
17408 objc_add_method_declaration
17409 (cp_parser_objc_method_signature (parser));
17410 cp_parser_consume_semicolon_at_end_of_statement (parser);
17411 }
17412 else
17413 /* Allow for interspersed non-ObjC++ code. */
17414 cp_parser_objc_interstitial_code (parser);
17415
17416 token = cp_lexer_peek_token (parser->lexer);
17417 }
17418
17419 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17420 objc_finish_interface ();
17421 }
17422
17423 /* Parse an Objective-C method definition list. */
17424
17425 static void
17426 cp_parser_objc_method_definition_list (cp_parser* parser)
17427 {
17428 cp_token *token = cp_lexer_peek_token (parser->lexer);
17429
17430 while (token->keyword != RID_AT_END)
17431 {
17432 tree meth;
17433
17434 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17435 {
17436 push_deferring_access_checks (dk_deferred);
17437 objc_start_method_definition
17438 (cp_parser_objc_method_signature (parser));
17439
17440 /* For historical reasons, we accept an optional semicolon. */
17441 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17442 cp_lexer_consume_token (parser->lexer);
17443
17444 perform_deferred_access_checks ();
17445 stop_deferring_access_checks ();
17446 meth = cp_parser_function_definition_after_declarator (parser,
17447 false);
17448 pop_deferring_access_checks ();
17449 objc_finish_method_definition (meth);
17450 }
17451 else
17452 /* Allow for interspersed non-ObjC++ code. */
17453 cp_parser_objc_interstitial_code (parser);
17454
17455 token = cp_lexer_peek_token (parser->lexer);
17456 }
17457
17458 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17459 objc_finish_implementation ();
17460 }
17461
17462 /* Parse Objective-C ivars. */
17463
17464 static void
17465 cp_parser_objc_class_ivars (cp_parser* parser)
17466 {
17467 cp_token *token = cp_lexer_peek_token (parser->lexer);
17468
17469 if (token->type != CPP_OPEN_BRACE)
17470 return; /* No ivars specified. */
17471
17472 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17473 token = cp_lexer_peek_token (parser->lexer);
17474
17475 while (token->type != CPP_CLOSE_BRACE)
17476 {
17477 cp_decl_specifier_seq declspecs;
17478 int decl_class_or_enum_p;
17479 tree prefix_attributes;
17480
17481 cp_parser_objc_visibility_spec (parser);
17482
17483 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17484 break;
17485
17486 cp_parser_decl_specifier_seq (parser,
17487 CP_PARSER_FLAGS_OPTIONAL,
17488 &declspecs,
17489 &decl_class_or_enum_p);
17490 prefix_attributes = declspecs.attributes;
17491 declspecs.attributes = NULL_TREE;
17492
17493 /* Keep going until we hit the `;' at the end of the
17494 declaration. */
17495 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17496 {
17497 tree width = NULL_TREE, attributes, first_attribute, decl;
17498 cp_declarator *declarator = NULL;
17499 int ctor_dtor_or_conv_p;
17500
17501 /* Check for a (possibly unnamed) bitfield declaration. */
17502 token = cp_lexer_peek_token (parser->lexer);
17503 if (token->type == CPP_COLON)
17504 goto eat_colon;
17505
17506 if (token->type == CPP_NAME
17507 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17508 == CPP_COLON))
17509 {
17510 /* Get the name of the bitfield. */
17511 declarator = make_id_declarator (NULL_TREE,
17512 cp_parser_identifier (parser),
17513 sfk_none);
17514
17515 eat_colon:
17516 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17517 /* Get the width of the bitfield. */
17518 width
17519 = cp_parser_constant_expression (parser,
17520 /*allow_non_constant=*/false,
17521 NULL);
17522 }
17523 else
17524 {
17525 /* Parse the declarator. */
17526 declarator
17527 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17528 &ctor_dtor_or_conv_p,
17529 /*parenthesized_p=*/NULL,
17530 /*member_p=*/false);
17531 }
17532
17533 /* Look for attributes that apply to the ivar. */
17534 attributes = cp_parser_attributes_opt (parser);
17535 /* Remember which attributes are prefix attributes and
17536 which are not. */
17537 first_attribute = attributes;
17538 /* Combine the attributes. */
17539 attributes = chainon (prefix_attributes, attributes);
17540
17541 if (width)
17542 {
17543 /* Create the bitfield declaration. */
17544 decl = grokbitfield (declarator, &declspecs, width);
17545 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17546 }
17547 else
17548 decl = grokfield (declarator, &declspecs,
17549 NULL_TREE, /*init_const_expr_p=*/false,
17550 NULL_TREE, attributes);
17551
17552 /* Add the instance variable. */
17553 objc_add_instance_variable (decl);
17554
17555 /* Reset PREFIX_ATTRIBUTES. */
17556 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17557 attributes = TREE_CHAIN (attributes);
17558 if (attributes)
17559 TREE_CHAIN (attributes) = NULL_TREE;
17560
17561 token = cp_lexer_peek_token (parser->lexer);
17562
17563 if (token->type == CPP_COMMA)
17564 {
17565 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17566 continue;
17567 }
17568 break;
17569 }
17570
17571 cp_parser_consume_semicolon_at_end_of_statement (parser);
17572 token = cp_lexer_peek_token (parser->lexer);
17573 }
17574
17575 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17576 /* For historical reasons, we accept an optional semicolon. */
17577 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17578 cp_lexer_consume_token (parser->lexer);
17579 }
17580
17581 /* Parse an Objective-C protocol declaration. */
17582
17583 static void
17584 cp_parser_objc_protocol_declaration (cp_parser* parser)
17585 {
17586 tree proto, protorefs;
17587 cp_token *tok;
17588
17589 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17591 {
17592 error ("identifier expected after %<@protocol%>");
17593 goto finish;
17594 }
17595
17596 /* See if we have a forward declaration or a definition. */
17597 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17598
17599 /* Try a forward declaration first. */
17600 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17601 {
17602 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17603 finish:
17604 cp_parser_consume_semicolon_at_end_of_statement (parser);
17605 }
17606
17607 /* Ok, we got a full-fledged definition (or at least should). */
17608 else
17609 {
17610 proto = cp_parser_identifier (parser);
17611 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17612 objc_start_protocol (proto, protorefs);
17613 cp_parser_objc_method_prototype_list (parser);
17614 }
17615 }
17616
17617 /* Parse an Objective-C superclass or category. */
17618
17619 static void
17620 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17621 tree *categ)
17622 {
17623 cp_token *next = cp_lexer_peek_token (parser->lexer);
17624
17625 *super = *categ = NULL_TREE;
17626 if (next->type == CPP_COLON)
17627 {
17628 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17629 *super = cp_parser_identifier (parser);
17630 }
17631 else if (next->type == CPP_OPEN_PAREN)
17632 {
17633 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17634 *categ = cp_parser_identifier (parser);
17635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17636 }
17637 }
17638
17639 /* Parse an Objective-C class interface. */
17640
17641 static void
17642 cp_parser_objc_class_interface (cp_parser* parser)
17643 {
17644 tree name, super, categ, protos;
17645
17646 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17647 name = cp_parser_identifier (parser);
17648 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17649 protos = cp_parser_objc_protocol_refs_opt (parser);
17650
17651 /* We have either a class or a category on our hands. */
17652 if (categ)
17653 objc_start_category_interface (name, categ, protos);
17654 else
17655 {
17656 objc_start_class_interface (name, super, protos);
17657 /* Handle instance variable declarations, if any. */
17658 cp_parser_objc_class_ivars (parser);
17659 objc_continue_interface ();
17660 }
17661
17662 cp_parser_objc_method_prototype_list (parser);
17663 }
17664
17665 /* Parse an Objective-C class implementation. */
17666
17667 static void
17668 cp_parser_objc_class_implementation (cp_parser* parser)
17669 {
17670 tree name, super, categ;
17671
17672 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17673 name = cp_parser_identifier (parser);
17674 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17675
17676 /* We have either a class or a category on our hands. */
17677 if (categ)
17678 objc_start_category_implementation (name, categ);
17679 else
17680 {
17681 objc_start_class_implementation (name, super);
17682 /* Handle instance variable declarations, if any. */
17683 cp_parser_objc_class_ivars (parser);
17684 objc_continue_implementation ();
17685 }
17686
17687 cp_parser_objc_method_definition_list (parser);
17688 }
17689
17690 /* Consume the @end token and finish off the implementation. */
17691
17692 static void
17693 cp_parser_objc_end_implementation (cp_parser* parser)
17694 {
17695 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17696 objc_finish_implementation ();
17697 }
17698
17699 /* Parse an Objective-C declaration. */
17700
17701 static void
17702 cp_parser_objc_declaration (cp_parser* parser)
17703 {
17704 /* Try to figure out what kind of declaration is present. */
17705 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17706
17707 switch (kwd->keyword)
17708 {
17709 case RID_AT_ALIAS:
17710 cp_parser_objc_alias_declaration (parser);
17711 break;
17712 case RID_AT_CLASS:
17713 cp_parser_objc_class_declaration (parser);
17714 break;
17715 case RID_AT_PROTOCOL:
17716 cp_parser_objc_protocol_declaration (parser);
17717 break;
17718 case RID_AT_INTERFACE:
17719 cp_parser_objc_class_interface (parser);
17720 break;
17721 case RID_AT_IMPLEMENTATION:
17722 cp_parser_objc_class_implementation (parser);
17723 break;
17724 case RID_AT_END:
17725 cp_parser_objc_end_implementation (parser);
17726 break;
17727 default:
17728 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17729 cp_parser_skip_to_end_of_block_or_statement (parser);
17730 }
17731 }
17732
17733 /* Parse an Objective-C try-catch-finally statement.
17734
17735 objc-try-catch-finally-stmt:
17736 @try compound-statement objc-catch-clause-seq [opt]
17737 objc-finally-clause [opt]
17738
17739 objc-catch-clause-seq:
17740 objc-catch-clause objc-catch-clause-seq [opt]
17741
17742 objc-catch-clause:
17743 @catch ( exception-declaration ) compound-statement
17744
17745 objc-finally-clause
17746 @finally compound-statement
17747
17748 Returns NULL_TREE. */
17749
17750 static tree
17751 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17752 location_t location;
17753 tree stmt;
17754
17755 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17756 location = cp_lexer_peek_token (parser->lexer)->location;
17757 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17758 node, lest it get absorbed into the surrounding block. */
17759 stmt = push_stmt_list ();
17760 cp_parser_compound_statement (parser, NULL, false);
17761 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17762
17763 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17764 {
17765 cp_parameter_declarator *parmdecl;
17766 tree parm;
17767
17768 cp_lexer_consume_token (parser->lexer);
17769 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17770 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17771 parm = grokdeclarator (parmdecl->declarator,
17772 &parmdecl->decl_specifiers,
17773 PARM, /*initialized=*/0,
17774 /*attrlist=*/NULL);
17775 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17776 objc_begin_catch_clause (parm);
17777 cp_parser_compound_statement (parser, NULL, false);
17778 objc_finish_catch_clause ();
17779 }
17780
17781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17782 {
17783 cp_lexer_consume_token (parser->lexer);
17784 location = cp_lexer_peek_token (parser->lexer)->location;
17785 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17786 node, lest it get absorbed into the surrounding block. */
17787 stmt = push_stmt_list ();
17788 cp_parser_compound_statement (parser, NULL, false);
17789 objc_build_finally_clause (location, pop_stmt_list (stmt));
17790 }
17791
17792 return objc_finish_try_stmt ();
17793 }
17794
17795 /* Parse an Objective-C synchronized statement.
17796
17797 objc-synchronized-stmt:
17798 @synchronized ( expression ) compound-statement
17799
17800 Returns NULL_TREE. */
17801
17802 static tree
17803 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17804 location_t location;
17805 tree lock, stmt;
17806
17807 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17808
17809 location = cp_lexer_peek_token (parser->lexer)->location;
17810 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17811 lock = cp_parser_expression (parser, false);
17812 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17813
17814 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17815 node, lest it get absorbed into the surrounding block. */
17816 stmt = push_stmt_list ();
17817 cp_parser_compound_statement (parser, NULL, false);
17818
17819 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17820 }
17821
17822 /* Parse an Objective-C throw statement.
17823
17824 objc-throw-stmt:
17825 @throw assignment-expression [opt] ;
17826
17827 Returns a constructed '@throw' statement. */
17828
17829 static tree
17830 cp_parser_objc_throw_statement (cp_parser *parser) {
17831 tree expr = NULL_TREE;
17832
17833 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17834
17835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17836 expr = cp_parser_assignment_expression (parser, false);
17837
17838 cp_parser_consume_semicolon_at_end_of_statement (parser);
17839
17840 return objc_build_throw_stmt (expr);
17841 }
17842
17843 /* Parse an Objective-C statement. */
17844
17845 static tree
17846 cp_parser_objc_statement (cp_parser * parser) {
17847 /* Try to figure out what kind of declaration is present. */
17848 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17849
17850 switch (kwd->keyword)
17851 {
17852 case RID_AT_TRY:
17853 return cp_parser_objc_try_catch_finally_statement (parser);
17854 case RID_AT_SYNCHRONIZED:
17855 return cp_parser_objc_synchronized_statement (parser);
17856 case RID_AT_THROW:
17857 return cp_parser_objc_throw_statement (parser);
17858 default:
17859 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17860 cp_parser_skip_to_end_of_block_or_statement (parser);
17861 }
17862
17863 return error_mark_node;
17864 }
17865 \f
17866 /* OpenMP 2.5 parsing routines. */
17867
17868 /* All OpenMP clauses. OpenMP 2.5. */
17869 typedef enum pragma_omp_clause {
17870 PRAGMA_OMP_CLAUSE_NONE = 0,
17871
17872 PRAGMA_OMP_CLAUSE_COPYIN,
17873 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17874 PRAGMA_OMP_CLAUSE_DEFAULT,
17875 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17876 PRAGMA_OMP_CLAUSE_IF,
17877 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17878 PRAGMA_OMP_CLAUSE_NOWAIT,
17879 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17880 PRAGMA_OMP_CLAUSE_ORDERED,
17881 PRAGMA_OMP_CLAUSE_PRIVATE,
17882 PRAGMA_OMP_CLAUSE_REDUCTION,
17883 PRAGMA_OMP_CLAUSE_SCHEDULE,
17884 PRAGMA_OMP_CLAUSE_SHARED
17885 } pragma_omp_clause;
17886
17887 /* Returns name of the next clause.
17888 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17889 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17890 returned and the token is consumed. */
17891
17892 static pragma_omp_clause
17893 cp_parser_omp_clause_name (cp_parser *parser)
17894 {
17895 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17896
17897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17898 result = PRAGMA_OMP_CLAUSE_IF;
17899 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17900 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17901 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17902 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17903 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17904 {
17905 tree id = cp_lexer_peek_token (parser->lexer)->value;
17906 const char *p = IDENTIFIER_POINTER (id);
17907
17908 switch (p[0])
17909 {
17910 case 'c':
17911 if (!strcmp ("copyin", p))
17912 result = PRAGMA_OMP_CLAUSE_COPYIN;
17913 else if (!strcmp ("copyprivate", p))
17914 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17915 break;
17916 case 'f':
17917 if (!strcmp ("firstprivate", p))
17918 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17919 break;
17920 case 'l':
17921 if (!strcmp ("lastprivate", p))
17922 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17923 break;
17924 case 'n':
17925 if (!strcmp ("nowait", p))
17926 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17927 else if (!strcmp ("num_threads", p))
17928 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17929 break;
17930 case 'o':
17931 if (!strcmp ("ordered", p))
17932 result = PRAGMA_OMP_CLAUSE_ORDERED;
17933 break;
17934 case 'r':
17935 if (!strcmp ("reduction", p))
17936 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17937 break;
17938 case 's':
17939 if (!strcmp ("schedule", p))
17940 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17941 else if (!strcmp ("shared", p))
17942 result = PRAGMA_OMP_CLAUSE_SHARED;
17943 break;
17944 }
17945 }
17946
17947 if (result != PRAGMA_OMP_CLAUSE_NONE)
17948 cp_lexer_consume_token (parser->lexer);
17949
17950 return result;
17951 }
17952
17953 /* Validate that a clause of the given type does not already exist. */
17954
17955 static void
17956 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17957 {
17958 tree c;
17959
17960 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17961 if (OMP_CLAUSE_CODE (c) == code)
17962 {
17963 error ("too many %qs clauses", name);
17964 break;
17965 }
17966 }
17967
17968 /* OpenMP 2.5:
17969 variable-list:
17970 identifier
17971 variable-list , identifier
17972
17973 In addition, we match a closing parenthesis. An opening parenthesis
17974 will have been consumed by the caller.
17975
17976 If KIND is nonzero, create the appropriate node and install the decl
17977 in OMP_CLAUSE_DECL and add the node to the head of the list.
17978
17979 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17980 return the list created. */
17981
17982 static tree
17983 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17984 tree list)
17985 {
17986 while (1)
17987 {
17988 tree name, decl;
17989
17990 name = cp_parser_id_expression (parser, /*template_p=*/false,
17991 /*check_dependency_p=*/true,
17992 /*template_p=*/NULL,
17993 /*declarator_p=*/false,
17994 /*optional_p=*/false);
17995 if (name == error_mark_node)
17996 goto skip_comma;
17997
17998 decl = cp_parser_lookup_name_simple (parser, name);
17999 if (decl == error_mark_node)
18000 cp_parser_name_lookup_error (parser, name, decl, NULL);
18001 else if (kind != 0)
18002 {
18003 tree u = build_omp_clause (kind);
18004 OMP_CLAUSE_DECL (u) = decl;
18005 OMP_CLAUSE_CHAIN (u) = list;
18006 list = u;
18007 }
18008 else
18009 list = tree_cons (decl, NULL_TREE, list);
18010
18011 get_comma:
18012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18013 break;
18014 cp_lexer_consume_token (parser->lexer);
18015 }
18016
18017 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18018 {
18019 int ending;
18020
18021 /* Try to resync to an unnested comma. Copied from
18022 cp_parser_parenthesized_expression_list. */
18023 skip_comma:
18024 ending = cp_parser_skip_to_closing_parenthesis (parser,
18025 /*recovering=*/true,
18026 /*or_comma=*/true,
18027 /*consume_paren=*/true);
18028 if (ending < 0)
18029 goto get_comma;
18030 }
18031
18032 return list;
18033 }
18034
18035 /* Similarly, but expect leading and trailing parenthesis. This is a very
18036 common case for omp clauses. */
18037
18038 static tree
18039 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18040 {
18041 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18042 return cp_parser_omp_var_list_no_open (parser, kind, list);
18043 return list;
18044 }
18045
18046 /* OpenMP 2.5:
18047 default ( shared | none ) */
18048
18049 static tree
18050 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18051 {
18052 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18053 tree c;
18054
18055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18056 return list;
18057 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18058 {
18059 tree id = cp_lexer_peek_token (parser->lexer)->value;
18060 const char *p = IDENTIFIER_POINTER (id);
18061
18062 switch (p[0])
18063 {
18064 case 'n':
18065 if (strcmp ("none", p) != 0)
18066 goto invalid_kind;
18067 kind = OMP_CLAUSE_DEFAULT_NONE;
18068 break;
18069
18070 case 's':
18071 if (strcmp ("shared", p) != 0)
18072 goto invalid_kind;
18073 kind = OMP_CLAUSE_DEFAULT_SHARED;
18074 break;
18075
18076 default:
18077 goto invalid_kind;
18078 }
18079
18080 cp_lexer_consume_token (parser->lexer);
18081 }
18082 else
18083 {
18084 invalid_kind:
18085 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18086 }
18087
18088 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18090 /*or_comma=*/false,
18091 /*consume_paren=*/true);
18092
18093 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18094 return list;
18095
18096 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18097 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18098 OMP_CLAUSE_CHAIN (c) = list;
18099 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18100
18101 return c;
18102 }
18103
18104 /* OpenMP 2.5:
18105 if ( expression ) */
18106
18107 static tree
18108 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18109 {
18110 tree t, c;
18111
18112 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18113 return list;
18114
18115 t = cp_parser_condition (parser);
18116
18117 if (t == error_mark_node
18118 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18119 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18120 /*or_comma=*/false,
18121 /*consume_paren=*/true);
18122
18123 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18124
18125 c = build_omp_clause (OMP_CLAUSE_IF);
18126 OMP_CLAUSE_IF_EXPR (c) = t;
18127 OMP_CLAUSE_CHAIN (c) = list;
18128
18129 return c;
18130 }
18131
18132 /* OpenMP 2.5:
18133 nowait */
18134
18135 static tree
18136 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18137 {
18138 tree c;
18139
18140 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18141
18142 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18143 OMP_CLAUSE_CHAIN (c) = list;
18144 return c;
18145 }
18146
18147 /* OpenMP 2.5:
18148 num_threads ( expression ) */
18149
18150 static tree
18151 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18152 {
18153 tree t, c;
18154
18155 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18156 return list;
18157
18158 t = cp_parser_expression (parser, false);
18159
18160 if (t == error_mark_node
18161 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18162 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18163 /*or_comma=*/false,
18164 /*consume_paren=*/true);
18165
18166 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18167
18168 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18169 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18170 OMP_CLAUSE_CHAIN (c) = list;
18171
18172 return c;
18173 }
18174
18175 /* OpenMP 2.5:
18176 ordered */
18177
18178 static tree
18179 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18180 {
18181 tree c;
18182
18183 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18184
18185 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18186 OMP_CLAUSE_CHAIN (c) = list;
18187 return c;
18188 }
18189
18190 /* OpenMP 2.5:
18191 reduction ( reduction-operator : variable-list )
18192
18193 reduction-operator:
18194 One of: + * - & ^ | && || */
18195
18196 static tree
18197 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18198 {
18199 enum tree_code code;
18200 tree nlist, c;
18201
18202 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18203 return list;
18204
18205 switch (cp_lexer_peek_token (parser->lexer)->type)
18206 {
18207 case CPP_PLUS:
18208 code = PLUS_EXPR;
18209 break;
18210 case CPP_MULT:
18211 code = MULT_EXPR;
18212 break;
18213 case CPP_MINUS:
18214 code = MINUS_EXPR;
18215 break;
18216 case CPP_AND:
18217 code = BIT_AND_EXPR;
18218 break;
18219 case CPP_XOR:
18220 code = BIT_XOR_EXPR;
18221 break;
18222 case CPP_OR:
18223 code = BIT_IOR_EXPR;
18224 break;
18225 case CPP_AND_AND:
18226 code = TRUTH_ANDIF_EXPR;
18227 break;
18228 case CPP_OR_OR:
18229 code = TRUTH_ORIF_EXPR;
18230 break;
18231 default:
18232 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18233 resync_fail:
18234 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18235 /*or_comma=*/false,
18236 /*consume_paren=*/true);
18237 return list;
18238 }
18239 cp_lexer_consume_token (parser->lexer);
18240
18241 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18242 goto resync_fail;
18243
18244 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18245 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18246 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18247
18248 return nlist;
18249 }
18250
18251 /* OpenMP 2.5:
18252 schedule ( schedule-kind )
18253 schedule ( schedule-kind , expression )
18254
18255 schedule-kind:
18256 static | dynamic | guided | runtime */
18257
18258 static tree
18259 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18260 {
18261 tree c, t;
18262
18263 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18264 return list;
18265
18266 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18267
18268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18269 {
18270 tree id = cp_lexer_peek_token (parser->lexer)->value;
18271 const char *p = IDENTIFIER_POINTER (id);
18272
18273 switch (p[0])
18274 {
18275 case 'd':
18276 if (strcmp ("dynamic", p) != 0)
18277 goto invalid_kind;
18278 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18279 break;
18280
18281 case 'g':
18282 if (strcmp ("guided", p) != 0)
18283 goto invalid_kind;
18284 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18285 break;
18286
18287 case 'r':
18288 if (strcmp ("runtime", p) != 0)
18289 goto invalid_kind;
18290 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18291 break;
18292
18293 default:
18294 goto invalid_kind;
18295 }
18296 }
18297 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18298 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18299 else
18300 goto invalid_kind;
18301 cp_lexer_consume_token (parser->lexer);
18302
18303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18304 {
18305 cp_lexer_consume_token (parser->lexer);
18306
18307 t = cp_parser_assignment_expression (parser, false);
18308
18309 if (t == error_mark_node)
18310 goto resync_fail;
18311 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18312 error ("schedule %<runtime%> does not take "
18313 "a %<chunk_size%> parameter");
18314 else
18315 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18316
18317 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18318 goto resync_fail;
18319 }
18320 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18321 goto resync_fail;
18322
18323 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18324 OMP_CLAUSE_CHAIN (c) = list;
18325 return c;
18326
18327 invalid_kind:
18328 cp_parser_error (parser, "invalid schedule kind");
18329 resync_fail:
18330 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18331 /*or_comma=*/false,
18332 /*consume_paren=*/true);
18333 return list;
18334 }
18335
18336 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18337 is a bitmask in MASK. Return the list of clauses found; the result
18338 of clause default goes in *pdefault. */
18339
18340 static tree
18341 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18342 const char *where, cp_token *pragma_tok)
18343 {
18344 tree clauses = NULL;
18345
18346 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18347 {
18348 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18349 const char *c_name;
18350 tree prev = clauses;
18351
18352 switch (c_kind)
18353 {
18354 case PRAGMA_OMP_CLAUSE_COPYIN:
18355 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18356 c_name = "copyin";
18357 break;
18358 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18359 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18360 clauses);
18361 c_name = "copyprivate";
18362 break;
18363 case PRAGMA_OMP_CLAUSE_DEFAULT:
18364 clauses = cp_parser_omp_clause_default (parser, clauses);
18365 c_name = "default";
18366 break;
18367 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18368 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18369 clauses);
18370 c_name = "firstprivate";
18371 break;
18372 case PRAGMA_OMP_CLAUSE_IF:
18373 clauses = cp_parser_omp_clause_if (parser, clauses);
18374 c_name = "if";
18375 break;
18376 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18377 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18378 clauses);
18379 c_name = "lastprivate";
18380 break;
18381 case PRAGMA_OMP_CLAUSE_NOWAIT:
18382 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18383 c_name = "nowait";
18384 break;
18385 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18386 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18387 c_name = "num_threads";
18388 break;
18389 case PRAGMA_OMP_CLAUSE_ORDERED:
18390 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18391 c_name = "ordered";
18392 break;
18393 case PRAGMA_OMP_CLAUSE_PRIVATE:
18394 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18395 clauses);
18396 c_name = "private";
18397 break;
18398 case PRAGMA_OMP_CLAUSE_REDUCTION:
18399 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18400 c_name = "reduction";
18401 break;
18402 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18403 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18404 c_name = "schedule";
18405 break;
18406 case PRAGMA_OMP_CLAUSE_SHARED:
18407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18408 clauses);
18409 c_name = "shared";
18410 break;
18411 default:
18412 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18413 goto saw_error;
18414 }
18415
18416 if (((mask >> c_kind) & 1) == 0)
18417 {
18418 /* Remove the invalid clause(s) from the list to avoid
18419 confusing the rest of the compiler. */
18420 clauses = prev;
18421 error ("%qs is not valid for %qs", c_name, where);
18422 }
18423 }
18424 saw_error:
18425 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18426 return finish_omp_clauses (clauses);
18427 }
18428
18429 /* OpenMP 2.5:
18430 structured-block:
18431 statement
18432
18433 In practice, we're also interested in adding the statement to an
18434 outer node. So it is convenient if we work around the fact that
18435 cp_parser_statement calls add_stmt. */
18436
18437 static unsigned
18438 cp_parser_begin_omp_structured_block (cp_parser *parser)
18439 {
18440 unsigned save = parser->in_statement;
18441
18442 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18443 This preserves the "not within loop or switch" style error messages
18444 for nonsense cases like
18445 void foo() {
18446 #pragma omp single
18447 break;
18448 }
18449 */
18450 if (parser->in_statement)
18451 parser->in_statement = IN_OMP_BLOCK;
18452
18453 return save;
18454 }
18455
18456 static void
18457 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18458 {
18459 parser->in_statement = save;
18460 }
18461
18462 static tree
18463 cp_parser_omp_structured_block (cp_parser *parser)
18464 {
18465 tree stmt = begin_omp_structured_block ();
18466 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18467
18468 cp_parser_statement (parser, NULL_TREE, false);
18469
18470 cp_parser_end_omp_structured_block (parser, save);
18471 return finish_omp_structured_block (stmt);
18472 }
18473
18474 /* OpenMP 2.5:
18475 # pragma omp atomic new-line
18476 expression-stmt
18477
18478 expression-stmt:
18479 x binop= expr | x++ | ++x | x-- | --x
18480 binop:
18481 +, *, -, /, &, ^, |, <<, >>
18482
18483 where x is an lvalue expression with scalar type. */
18484
18485 static void
18486 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18487 {
18488 tree lhs, rhs;
18489 enum tree_code code;
18490
18491 cp_parser_require_pragma_eol (parser, pragma_tok);
18492
18493 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18494 /*cast_p=*/false);
18495 switch (TREE_CODE (lhs))
18496 {
18497 case ERROR_MARK:
18498 goto saw_error;
18499
18500 case PREINCREMENT_EXPR:
18501 case POSTINCREMENT_EXPR:
18502 lhs = TREE_OPERAND (lhs, 0);
18503 code = PLUS_EXPR;
18504 rhs = integer_one_node;
18505 break;
18506
18507 case PREDECREMENT_EXPR:
18508 case POSTDECREMENT_EXPR:
18509 lhs = TREE_OPERAND (lhs, 0);
18510 code = MINUS_EXPR;
18511 rhs = integer_one_node;
18512 break;
18513
18514 default:
18515 switch (cp_lexer_peek_token (parser->lexer)->type)
18516 {
18517 case CPP_MULT_EQ:
18518 code = MULT_EXPR;
18519 break;
18520 case CPP_DIV_EQ:
18521 code = TRUNC_DIV_EXPR;
18522 break;
18523 case CPP_PLUS_EQ:
18524 code = PLUS_EXPR;
18525 break;
18526 case CPP_MINUS_EQ:
18527 code = MINUS_EXPR;
18528 break;
18529 case CPP_LSHIFT_EQ:
18530 code = LSHIFT_EXPR;
18531 break;
18532 case CPP_RSHIFT_EQ:
18533 code = RSHIFT_EXPR;
18534 break;
18535 case CPP_AND_EQ:
18536 code = BIT_AND_EXPR;
18537 break;
18538 case CPP_OR_EQ:
18539 code = BIT_IOR_EXPR;
18540 break;
18541 case CPP_XOR_EQ:
18542 code = BIT_XOR_EXPR;
18543 break;
18544 default:
18545 cp_parser_error (parser,
18546 "invalid operator for %<#pragma omp atomic%>");
18547 goto saw_error;
18548 }
18549 cp_lexer_consume_token (parser->lexer);
18550
18551 rhs = cp_parser_expression (parser, false);
18552 if (rhs == error_mark_node)
18553 goto saw_error;
18554 break;
18555 }
18556 finish_omp_atomic (code, lhs, rhs);
18557 cp_parser_consume_semicolon_at_end_of_statement (parser);
18558 return;
18559
18560 saw_error:
18561 cp_parser_skip_to_end_of_block_or_statement (parser);
18562 }
18563
18564
18565 /* OpenMP 2.5:
18566 # pragma omp barrier new-line */
18567
18568 static void
18569 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18570 {
18571 cp_parser_require_pragma_eol (parser, pragma_tok);
18572 finish_omp_barrier ();
18573 }
18574
18575 /* OpenMP 2.5:
18576 # pragma omp critical [(name)] new-line
18577 structured-block */
18578
18579 static tree
18580 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18581 {
18582 tree stmt, name = NULL;
18583
18584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18585 {
18586 cp_lexer_consume_token (parser->lexer);
18587
18588 name = cp_parser_identifier (parser);
18589
18590 if (name == error_mark_node
18591 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18593 /*or_comma=*/false,
18594 /*consume_paren=*/true);
18595 if (name == error_mark_node)
18596 name = NULL;
18597 }
18598 cp_parser_require_pragma_eol (parser, pragma_tok);
18599
18600 stmt = cp_parser_omp_structured_block (parser);
18601 return c_finish_omp_critical (stmt, name);
18602 }
18603
18604 /* OpenMP 2.5:
18605 # pragma omp flush flush-vars[opt] new-line
18606
18607 flush-vars:
18608 ( variable-list ) */
18609
18610 static void
18611 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18612 {
18613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18614 (void) cp_parser_omp_var_list (parser, 0, NULL);
18615 cp_parser_require_pragma_eol (parser, pragma_tok);
18616
18617 finish_omp_flush ();
18618 }
18619
18620 /* Parse the restricted form of the for statment allowed by OpenMP. */
18621
18622 static tree
18623 cp_parser_omp_for_loop (cp_parser *parser)
18624 {
18625 tree init, cond, incr, body, decl, pre_body;
18626 location_t loc;
18627
18628 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18629 {
18630 cp_parser_error (parser, "for statement expected");
18631 return NULL;
18632 }
18633 loc = cp_lexer_consume_token (parser->lexer)->location;
18634 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18635 return NULL;
18636
18637 init = decl = NULL;
18638 pre_body = push_stmt_list ();
18639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18640 {
18641 cp_decl_specifier_seq type_specifiers;
18642
18643 /* First, try to parse as an initialized declaration. See
18644 cp_parser_condition, from whence the bulk of this is copied. */
18645
18646 cp_parser_parse_tentatively (parser);
18647 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18648 &type_specifiers);
18649 if (!cp_parser_error_occurred (parser))
18650 {
18651 tree asm_specification, attributes;
18652 cp_declarator *declarator;
18653
18654 declarator = cp_parser_declarator (parser,
18655 CP_PARSER_DECLARATOR_NAMED,
18656 /*ctor_dtor_or_conv_p=*/NULL,
18657 /*parenthesized_p=*/NULL,
18658 /*member_p=*/false);
18659 attributes = cp_parser_attributes_opt (parser);
18660 asm_specification = cp_parser_asm_specification_opt (parser);
18661
18662 cp_parser_require (parser, CPP_EQ, "`='");
18663 if (cp_parser_parse_definitely (parser))
18664 {
18665 tree pushed_scope;
18666
18667 decl = start_decl (declarator, &type_specifiers,
18668 /*initialized_p=*/false, attributes,
18669 /*prefix_attributes=*/NULL_TREE,
18670 &pushed_scope);
18671
18672 init = cp_parser_assignment_expression (parser, false);
18673
18674 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18675 asm_specification, LOOKUP_ONLYCONVERTING);
18676
18677 if (pushed_scope)
18678 pop_scope (pushed_scope);
18679 }
18680 }
18681 else
18682 cp_parser_abort_tentative_parse (parser);
18683
18684 /* If parsing as an initialized declaration failed, try again as
18685 a simple expression. */
18686 if (decl == NULL)
18687 init = cp_parser_expression (parser, false);
18688 }
18689 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18690 pre_body = pop_stmt_list (pre_body);
18691
18692 cond = NULL;
18693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18694 cond = cp_parser_condition (parser);
18695 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18696
18697 incr = NULL;
18698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18699 incr = cp_parser_expression (parser, false);
18700
18701 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18702 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18703 /*or_comma=*/false,
18704 /*consume_paren=*/true);
18705
18706 /* Note that we saved the original contents of this flag when we entered
18707 the structured block, and so we don't need to re-save it here. */
18708 parser->in_statement = IN_OMP_FOR;
18709
18710 /* Note that the grammar doesn't call for a structured block here,
18711 though the loop as a whole is a structured block. */
18712 body = push_stmt_list ();
18713 cp_parser_statement (parser, NULL_TREE, false);
18714 body = pop_stmt_list (body);
18715
18716 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18717 }
18718
18719 /* OpenMP 2.5:
18720 #pragma omp for for-clause[optseq] new-line
18721 for-loop */
18722
18723 #define OMP_FOR_CLAUSE_MASK \
18724 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18725 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18726 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18727 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18728 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18729 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18730 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18731
18732 static tree
18733 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18734 {
18735 tree clauses, sb, ret;
18736 unsigned int save;
18737
18738 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18739 "#pragma omp for", pragma_tok);
18740
18741 sb = begin_omp_structured_block ();
18742 save = cp_parser_begin_omp_structured_block (parser);
18743
18744 ret = cp_parser_omp_for_loop (parser);
18745 if (ret)
18746 OMP_FOR_CLAUSES (ret) = clauses;
18747
18748 cp_parser_end_omp_structured_block (parser, save);
18749 add_stmt (finish_omp_structured_block (sb));
18750
18751 return ret;
18752 }
18753
18754 /* OpenMP 2.5:
18755 # pragma omp master new-line
18756 structured-block */
18757
18758 static tree
18759 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18760 {
18761 cp_parser_require_pragma_eol (parser, pragma_tok);
18762 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18763 }
18764
18765 /* OpenMP 2.5:
18766 # pragma omp ordered new-line
18767 structured-block */
18768
18769 static tree
18770 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18771 {
18772 cp_parser_require_pragma_eol (parser, pragma_tok);
18773 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18774 }
18775
18776 /* OpenMP 2.5:
18777
18778 section-scope:
18779 { section-sequence }
18780
18781 section-sequence:
18782 section-directive[opt] structured-block
18783 section-sequence section-directive structured-block */
18784
18785 static tree
18786 cp_parser_omp_sections_scope (cp_parser *parser)
18787 {
18788 tree stmt, substmt;
18789 bool error_suppress = false;
18790 cp_token *tok;
18791
18792 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18793 return NULL_TREE;
18794
18795 stmt = push_stmt_list ();
18796
18797 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18798 {
18799 unsigned save;
18800
18801 substmt = begin_omp_structured_block ();
18802 save = cp_parser_begin_omp_structured_block (parser);
18803
18804 while (1)
18805 {
18806 cp_parser_statement (parser, NULL_TREE, false);
18807
18808 tok = cp_lexer_peek_token (parser->lexer);
18809 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18810 break;
18811 if (tok->type == CPP_CLOSE_BRACE)
18812 break;
18813 if (tok->type == CPP_EOF)
18814 break;
18815 }
18816
18817 cp_parser_end_omp_structured_block (parser, save);
18818 substmt = finish_omp_structured_block (substmt);
18819 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18820 add_stmt (substmt);
18821 }
18822
18823 while (1)
18824 {
18825 tok = cp_lexer_peek_token (parser->lexer);
18826 if (tok->type == CPP_CLOSE_BRACE)
18827 break;
18828 if (tok->type == CPP_EOF)
18829 break;
18830
18831 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18832 {
18833 cp_lexer_consume_token (parser->lexer);
18834 cp_parser_require_pragma_eol (parser, tok);
18835 error_suppress = false;
18836 }
18837 else if (!error_suppress)
18838 {
18839 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18840 error_suppress = true;
18841 }
18842
18843 substmt = cp_parser_omp_structured_block (parser);
18844 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18845 add_stmt (substmt);
18846 }
18847 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18848
18849 substmt = pop_stmt_list (stmt);
18850
18851 stmt = make_node (OMP_SECTIONS);
18852 TREE_TYPE (stmt) = void_type_node;
18853 OMP_SECTIONS_BODY (stmt) = substmt;
18854
18855 add_stmt (stmt);
18856 return stmt;
18857 }
18858
18859 /* OpenMP 2.5:
18860 # pragma omp sections sections-clause[optseq] newline
18861 sections-scope */
18862
18863 #define OMP_SECTIONS_CLAUSE_MASK \
18864 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18865 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18866 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18867 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18868 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18869
18870 static tree
18871 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18872 {
18873 tree clauses, ret;
18874
18875 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18876 "#pragma omp sections", pragma_tok);
18877
18878 ret = cp_parser_omp_sections_scope (parser);
18879 if (ret)
18880 OMP_SECTIONS_CLAUSES (ret) = clauses;
18881
18882 return ret;
18883 }
18884
18885 /* OpenMP 2.5:
18886 # pragma parallel parallel-clause new-line
18887 # pragma parallel for parallel-for-clause new-line
18888 # pragma parallel sections parallel-sections-clause new-line */
18889
18890 #define OMP_PARALLEL_CLAUSE_MASK \
18891 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18892 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18893 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18894 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18895 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18896 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18897 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18898 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18899
18900 static tree
18901 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18902 {
18903 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18904 const char *p_name = "#pragma omp parallel";
18905 tree stmt, clauses, par_clause, ws_clause, block;
18906 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18907 unsigned int save;
18908
18909 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18910 {
18911 cp_lexer_consume_token (parser->lexer);
18912 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18913 p_name = "#pragma omp parallel for";
18914 mask |= OMP_FOR_CLAUSE_MASK;
18915 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18916 }
18917 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18918 {
18919 tree id = cp_lexer_peek_token (parser->lexer)->value;
18920 const char *p = IDENTIFIER_POINTER (id);
18921 if (strcmp (p, "sections") == 0)
18922 {
18923 cp_lexer_consume_token (parser->lexer);
18924 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18925 p_name = "#pragma omp parallel sections";
18926 mask |= OMP_SECTIONS_CLAUSE_MASK;
18927 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18928 }
18929 }
18930
18931 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18932 block = begin_omp_parallel ();
18933 save = cp_parser_begin_omp_structured_block (parser);
18934
18935 switch (p_kind)
18936 {
18937 case PRAGMA_OMP_PARALLEL:
18938 cp_parser_already_scoped_statement (parser);
18939 par_clause = clauses;
18940 break;
18941
18942 case PRAGMA_OMP_PARALLEL_FOR:
18943 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18944 stmt = cp_parser_omp_for_loop (parser);
18945 if (stmt)
18946 OMP_FOR_CLAUSES (stmt) = ws_clause;
18947 break;
18948
18949 case PRAGMA_OMP_PARALLEL_SECTIONS:
18950 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18951 stmt = cp_parser_omp_sections_scope (parser);
18952 if (stmt)
18953 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18954 break;
18955
18956 default:
18957 gcc_unreachable ();
18958 }
18959
18960 cp_parser_end_omp_structured_block (parser, save);
18961 stmt = finish_omp_parallel (par_clause, block);
18962 if (p_kind != PRAGMA_OMP_PARALLEL)
18963 OMP_PARALLEL_COMBINED (stmt) = 1;
18964 return stmt;
18965 }
18966
18967 /* OpenMP 2.5:
18968 # pragma omp single single-clause[optseq] new-line
18969 structured-block */
18970
18971 #define OMP_SINGLE_CLAUSE_MASK \
18972 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18973 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18974 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18975 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18976
18977 static tree
18978 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18979 {
18980 tree stmt = make_node (OMP_SINGLE);
18981 TREE_TYPE (stmt) = void_type_node;
18982
18983 OMP_SINGLE_CLAUSES (stmt)
18984 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18985 "#pragma omp single", pragma_tok);
18986 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18987
18988 return add_stmt (stmt);
18989 }
18990
18991 /* OpenMP 2.5:
18992 # pragma omp threadprivate (variable-list) */
18993
18994 static void
18995 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18996 {
18997 tree vars;
18998
18999 vars = cp_parser_omp_var_list (parser, 0, NULL);
19000 cp_parser_require_pragma_eol (parser, pragma_tok);
19001
19002 if (!targetm.have_tls)
19003 sorry ("threadprivate variables not supported in this target");
19004
19005 finish_omp_threadprivate (vars);
19006 }
19007
19008 /* Main entry point to OpenMP statement pragmas. */
19009
19010 static void
19011 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19012 {
19013 tree stmt;
19014
19015 switch (pragma_tok->pragma_kind)
19016 {
19017 case PRAGMA_OMP_ATOMIC:
19018 cp_parser_omp_atomic (parser, pragma_tok);
19019 return;
19020 case PRAGMA_OMP_CRITICAL:
19021 stmt = cp_parser_omp_critical (parser, pragma_tok);
19022 break;
19023 case PRAGMA_OMP_FOR:
19024 stmt = cp_parser_omp_for (parser, pragma_tok);
19025 break;
19026 case PRAGMA_OMP_MASTER:
19027 stmt = cp_parser_omp_master (parser, pragma_tok);
19028 break;
19029 case PRAGMA_OMP_ORDERED:
19030 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19031 break;
19032 case PRAGMA_OMP_PARALLEL:
19033 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19034 break;
19035 case PRAGMA_OMP_SECTIONS:
19036 stmt = cp_parser_omp_sections (parser, pragma_tok);
19037 break;
19038 case PRAGMA_OMP_SINGLE:
19039 stmt = cp_parser_omp_single (parser, pragma_tok);
19040 break;
19041 default:
19042 gcc_unreachable ();
19043 }
19044
19045 if (stmt)
19046 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19047 }
19048 \f
19049 /* The parser. */
19050
19051 static GTY (()) cp_parser *the_parser;
19052
19053 \f
19054 /* Special handling for the first token or line in the file. The first
19055 thing in the file might be #pragma GCC pch_preprocess, which loads a
19056 PCH file, which is a GC collection point. So we need to handle this
19057 first pragma without benefit of an existing lexer structure.
19058
19059 Always returns one token to the caller in *FIRST_TOKEN. This is
19060 either the true first token of the file, or the first token after
19061 the initial pragma. */
19062
19063 static void
19064 cp_parser_initial_pragma (cp_token *first_token)
19065 {
19066 tree name = NULL;
19067
19068 cp_lexer_get_preprocessor_token (NULL, first_token);
19069 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19070 return;
19071
19072 cp_lexer_get_preprocessor_token (NULL, first_token);
19073 if (first_token->type == CPP_STRING)
19074 {
19075 name = first_token->value;
19076
19077 cp_lexer_get_preprocessor_token (NULL, first_token);
19078 if (first_token->type != CPP_PRAGMA_EOL)
19079 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19080 }
19081 else
19082 error ("expected string literal");
19083
19084 /* Skip to the end of the pragma. */
19085 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19086 cp_lexer_get_preprocessor_token (NULL, first_token);
19087
19088 /* Now actually load the PCH file. */
19089 if (name)
19090 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19091
19092 /* Read one more token to return to our caller. We have to do this
19093 after reading the PCH file in, since its pointers have to be
19094 live. */
19095 cp_lexer_get_preprocessor_token (NULL, first_token);
19096 }
19097
19098 /* Normal parsing of a pragma token. Here we can (and must) use the
19099 regular lexer. */
19100
19101 static bool
19102 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19103 {
19104 cp_token *pragma_tok;
19105 unsigned int id;
19106
19107 pragma_tok = cp_lexer_consume_token (parser->lexer);
19108 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19109 parser->lexer->in_pragma = true;
19110
19111 id = pragma_tok->pragma_kind;
19112 switch (id)
19113 {
19114 case PRAGMA_GCC_PCH_PREPROCESS:
19115 error ("%<#pragma GCC pch_preprocess%> must be first");
19116 break;
19117
19118 case PRAGMA_OMP_BARRIER:
19119 switch (context)
19120 {
19121 case pragma_compound:
19122 cp_parser_omp_barrier (parser, pragma_tok);
19123 return false;
19124 case pragma_stmt:
19125 error ("%<#pragma omp barrier%> may only be "
19126 "used in compound statements");
19127 break;
19128 default:
19129 goto bad_stmt;
19130 }
19131 break;
19132
19133 case PRAGMA_OMP_FLUSH:
19134 switch (context)
19135 {
19136 case pragma_compound:
19137 cp_parser_omp_flush (parser, pragma_tok);
19138 return false;
19139 case pragma_stmt:
19140 error ("%<#pragma omp flush%> may only be "
19141 "used in compound statements");
19142 break;
19143 default:
19144 goto bad_stmt;
19145 }
19146 break;
19147
19148 case PRAGMA_OMP_THREADPRIVATE:
19149 cp_parser_omp_threadprivate (parser, pragma_tok);
19150 return false;
19151
19152 case PRAGMA_OMP_ATOMIC:
19153 case PRAGMA_OMP_CRITICAL:
19154 case PRAGMA_OMP_FOR:
19155 case PRAGMA_OMP_MASTER:
19156 case PRAGMA_OMP_ORDERED:
19157 case PRAGMA_OMP_PARALLEL:
19158 case PRAGMA_OMP_SECTIONS:
19159 case PRAGMA_OMP_SINGLE:
19160 if (context == pragma_external)
19161 goto bad_stmt;
19162 cp_parser_omp_construct (parser, pragma_tok);
19163 return true;
19164
19165 case PRAGMA_OMP_SECTION:
19166 error ("%<#pragma omp section%> may only be used in "
19167 "%<#pragma omp sections%> construct");
19168 break;
19169
19170 default:
19171 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19172 c_invoke_pragma_handler (id);
19173 break;
19174
19175 bad_stmt:
19176 cp_parser_error (parser, "expected declaration specifiers");
19177 break;
19178 }
19179
19180 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19181 return false;
19182 }
19183
19184 /* The interface the pragma parsers have to the lexer. */
19185
19186 enum cpp_ttype
19187 pragma_lex (tree *value)
19188 {
19189 cp_token *tok;
19190 enum cpp_ttype ret;
19191
19192 tok = cp_lexer_peek_token (the_parser->lexer);
19193
19194 ret = tok->type;
19195 *value = tok->value;
19196
19197 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19198 ret = CPP_EOF;
19199 else if (ret == CPP_STRING)
19200 *value = cp_parser_string_literal (the_parser, false, false);
19201 else
19202 {
19203 cp_lexer_consume_token (the_parser->lexer);
19204 if (ret == CPP_KEYWORD)
19205 ret = CPP_NAME;
19206 }
19207
19208 return ret;
19209 }
19210
19211 \f
19212 /* External interface. */
19213
19214 /* Parse one entire translation unit. */
19215
19216 void
19217 c_parse_file (void)
19218 {
19219 bool error_occurred;
19220 static bool already_called = false;
19221
19222 if (already_called)
19223 {
19224 sorry ("inter-module optimizations not implemented for C++");
19225 return;
19226 }
19227 already_called = true;
19228
19229 the_parser = cp_parser_new ();
19230 push_deferring_access_checks (flag_access_control
19231 ? dk_no_deferred : dk_no_check);
19232 error_occurred = cp_parser_translation_unit (the_parser);
19233 the_parser = NULL;
19234 }
19235
19236 /* This variable must be provided by every front end. */
19237
19238 int yydebug;
19239
19240 #include "gt-cp-parser.h"
This page took 0.847394 seconds and 6 git commands to generate.